Merge "load_gen: Supporting loading of load_gen via loader."
[vswitchperf.git] / vswitches / utils.py
1 # Copyright 2015 Intel Corporation.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #   http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 """Utility functions for working with vSwitches and flows
16 """
17
18 import copy
19
20 def add_ports_to_flow(flow, in_port, out_port):
21     """Creates a new flow based on the given flow and adds in port and out port
22     to it.
23
24     The flow dictionary structure is described in IVswitch
25
26     :param flow: Description of the flow as a dictionary
27     :param in_port: OpenFlow number of the ingress port for the rule
28     :param out_port: OpenFlow number of the eggress port for the rule
29
30     :returns: A new dictionary describing a flow combining the parameters
31     """
32     new_flow = copy.deepcopy(flow)
33     new_flow['in_port'] = in_port
34
35     if 'actions' in new_flow:
36         new_flow['actions'].append('output:' + str(out_port))
37     else:
38         new_flow['actions'] = ['output:' + str(out_port)]
39
40     return new_flow