exclude files from the search in the Loader
[vswitchperf.git] / core / vswitch_controller_pvvp.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 """VSwitch controller for Physical to VM to Physical deployment
16 """
17
18 import logging
19
20 from core.vswitch_controller import IVswitchController
21 from vswitches.utils import add_ports_to_flow
22 from conf import settings
23
24 _FLOW_TEMPLATE = {
25     'idle_timeout': '0'
26 }
27
28 class VswitchControllerPVVP(IVswitchController):
29     """VSwitch controller for PVVP deployment scenario.
30
31     Attributes:
32         _vswitch_class: The vSwitch class to be used.
33         _vswitch: The vSwitch object controlled by this controller
34         _deployment_scenario: A string describing the scenario to set-up in the
35             constructor.
36     """
37     def __init__(self, vswitch_class, bidir=False):
38         """Initializes up the prerequisites for the PVVP deployment scenario.
39
40         :vswitch_class: the vSwitch class to be used.
41         """
42         self._logger = logging.getLogger(__name__)
43         self._vswitch_class = vswitch_class
44         self._vswitch = vswitch_class()
45         self._deployment_scenario = "PVVP"
46         self._bidir = bidir
47         self._logger.debug('Creation using ' + str(self._vswitch_class))
48
49     def setup(self):
50         """ Sets up the switch for PVVP
51         """
52         self._logger.debug('Setup using ' + str(self._vswitch_class))
53
54         try:
55             self._vswitch.start()
56
57             bridge = settings.getValue('VSWITCH_BRIDGE_NAME')
58             self._vswitch.add_switch(bridge)
59
60             (_, phy1_number) = self._vswitch.add_phy_port(bridge)
61             (_, phy2_number) = self._vswitch.add_phy_port(bridge)
62             (_, vport1_number) = self._vswitch.add_vport(bridge)
63             (_, vport2_number) = self._vswitch.add_vport(bridge)
64             (_, vport3_number) = self._vswitch.add_vport(bridge)
65             (_, vport4_number) = self._vswitch.add_vport(bridge)
66
67             self._vswitch.del_flow(bridge)
68             flow1 = add_ports_to_flow(_FLOW_TEMPLATE, phy1_number,
69                                       vport1_number)
70             flow2 = add_ports_to_flow(_FLOW_TEMPLATE, vport2_number,
71                                       vport3_number)
72             flow3 = add_ports_to_flow(_FLOW_TEMPLATE, vport4_number,
73                                       phy2_number)
74             self._vswitch.add_flow(bridge, flow1)
75             self._vswitch.add_flow(bridge, flow2)
76             self._vswitch.add_flow(bridge, flow3)
77
78             if self._bidir:
79                 flow4 = add_ports_to_flow(_FLOW_TEMPLATE, phy2_number,
80                                           vport4_number)
81                 flow5 = add_ports_to_flow(_FLOW_TEMPLATE, vport3_number,
82                                           vport2_number)
83                 flow6 = add_ports_to_flow(_FLOW_TEMPLATE, vport1_number,
84                                           phy1_number)
85                 self._vswitch.add_flow(bridge, flow4)
86                 self._vswitch.add_flow(bridge, flow5)
87                 self._vswitch.add_flow(bridge, flow6)
88
89         except:
90             self._vswitch.stop()
91             raise
92
93     def stop(self):
94         """Tears down the switch created in setup().
95         """
96         self._logger.debug('Stop using ' + str(self._vswitch_class))
97         self._vswitch.stop()
98
99     def __enter__(self):
100         self.setup()
101
102     def __exit__(self, type_, value, traceback):
103         self.stop()
104
105     def get_vswitch(self):
106         """See IVswitchController for description
107         """
108         return self._vswitch
109
110     def get_ports_info(self):
111         """See IVswitchController for description
112         """
113         self._logger.debug('get_ports_info  using ' + str(self._vswitch_class))
114         return self._vswitch.get_ports(settings.getValue('VSWITCH_BRIDGE_NAME'))
115
116     def dump_vswitch_flows(self):
117         """See IVswitchController for description
118         """
119         self._vswitch.dump_flows(settings.getValue('VSWITCH_BRIDGE_NAME'))