Merge "Namespace_veth: Add funtionality for network namespace, veth ports"
[vswitchperf.git] / core / pktfwd_controller.py
1 # Copyright 2016 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 """Packet forwarder controller for Physical to Physical deployment
16 """
17
18 import logging
19
20 class PktFwdController(object):
21     """Packet forwarder controller for P2P deployment scenario.
22
23     Attributes:
24         _pktfwd_class: The packet forwarder class to be used.
25         _pktfwd: The packet forwarder object controlled by this controller
26     """
27     def __init__(self, deployment, pktfwd_class):
28         """Initializes up the prerequisites for the P2P deployment scenario.
29
30         :vswitch_class: the vSwitch class to be used.
31         """
32         self._deployment = deployment
33         self._logger = logging.getLogger(__name__)
34         self._pktfwd_class = pktfwd_class
35         self._pktfwd = pktfwd_class()
36         self._logger.debug('Creation using ' + str(self._pktfwd_class))
37
38     def setup(self):
39         """Sets up the packet forwarder for p2p.
40         """
41         self._logger.debug('Setup using ' + str(self._pktfwd_class))
42
43         try:
44             self._pktfwd.start()
45         except:
46             self._pktfwd.stop()
47             raise
48
49     def stop(self):
50         """Tears down the packet forwarder created in setup().
51         """
52         self._logger.debug('Stop using ' + str(self._pktfwd_class))
53         self._pktfwd.stop()
54
55     def __enter__(self):
56         if self._deployment.find("p2p") == 0:
57             self.setup()
58
59     def __exit__(self, type_, value, traceback):
60         if self._deployment.find("p2p") == 0:
61             self.stop()
62
63     def get_pktfwd(self):
64         """Get the controlled packet forwarder
65
66         :return: The controlled IPktFwd
67         """
68         return self._pktfwd
69
70     def dump_vswitch_flows(self):
71         """ Dumps flows from vswitch
72         """
73         raise NotImplementedError(
74             "The PktFwdController does not implement the "
75             "\"dump_vswitch_flows\" function.")