Enable PVVP deployment for DPDK Vhost User and Vhost Cuse
[vswitchperf.git] / core / vnf_controller.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 """ VNF Controller interface
15 """
16
17 import logging
18
19 class VnfController(object):
20     """VNF controller class
21
22     Used to set-up and control VNFs for specified scenario
23
24     Attributes:
25         _vnf_class: A class object representing the VNF to be used.
26         _deployment_scenario: A string describing the scenario to set-up in the
27             constructor.
28         _vnfs: A list of vnfs controlled by the controller.
29     """
30
31     def __init__(self, deployment_scenario, vnf_class):
32         """Sets up the VNF infrastructure for the PVP deployment scenario.
33
34         :param vnf_class: The VNF class to be used.
35         """
36         self._logger = logging.getLogger(__name__)
37         self._vnf_class = vnf_class
38         self._deployment_scenario = deployment_scenario.upper()
39         if self._deployment_scenario == 'P2P':
40             self._vnfs = []
41         if self._deployment_scenario == 'PVP':
42             self._vnfs = [vnf_class()]
43         elif self._deployment_scenario == 'PVVP':
44             self._vnfs = [vnf_class(), vnf_class()]
45         self._logger.debug('__init__ ' + str(len(self._vnfs)) +
46                            ' VNF[s] with ' + ' '.join(map(str, self._vnfs)))
47
48     def get_vnfs(self):
49         """Returns a list of vnfs controlled by this controller.
50         """
51         self._logger.debug('get_vnfs ' + str(len(self._vnfs)) +
52                            ' VNF[s] with ' + ' '.join(map(str, self._vnfs)))
53         return self._vnfs
54
55     def start(self):
56         """Boots all VNFs set-up by __init__.
57
58         This is a blocking function.
59         """
60         self._logger.debug('start ' + str(len(self._vnfs)) +
61                            ' VNF[s] with ' + ' '.join(map(str, self._vnfs)))
62         for vnf in self._vnfs:
63             vnf.start()
64
65     def stop(self):
66         """Stops all VNFs set-up by __init__.
67
68         This is a blocking function.
69         """
70         self._logger.debug('stop ' + str(len(self._vnfs)) +
71                            ' VNF[s] with ' + ' '.join(map(str, self._vnfs)))
72         for vnf in self._vnfs:
73             vnf.stop()
74
75     def __enter__(self):
76         self.start()
77
78     def __exit__(self, type_, value, traceback):
79         self.stop()