Merge "Namespace_veth: Add funtionality for network namespace, veth ports"
[vswitchperf.git] / core / vnf_controller.py
1 # Copyright 2015-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 """ VNF Controller interface
15 """
16
17 import logging
18 import pexpect
19 from vnfs.vnf.vnf import IVnf
20
21 class VnfController(object):
22     """VNF controller class
23
24     Used to set-up and control VNFs for specified scenario
25
26     Attributes:
27         _vnf_class: A class object representing the VNF to be used.
28         _deployment_scenario: A string describing the scenario to set-up in the
29             constructor.
30         _vnfs: A list of vnfs controlled by the controller.
31     """
32
33     def __init__(self, deployment_scenario, vnf_class):
34         """Sets up the VNF infrastructure for the PVP deployment scenario.
35
36         :param vnf_class: The VNF class to be used.
37         """
38         # reset VNF ID counter for each testcase
39         IVnf.reset_vnf_counter()
40
41         # setup controller with requested number of VNFs
42         self._logger = logging.getLogger(__name__)
43         self._vnf_class = vnf_class
44         self._deployment_scenario = deployment_scenario.upper()
45         if self._deployment_scenario == 'P2P':
46             self._vnfs = []
47         elif self._deployment_scenario == 'PVP':
48             self._vnfs = [vnf_class()]
49         elif self._deployment_scenario == 'PVVP':
50             self._vnfs = [vnf_class(), vnf_class()]
51         elif self._deployment_scenario == 'OP2P':
52             self._vnfs = []
53         else:
54             self._vnfs = []
55         self._logger.debug('__init__ ' + str(len(self._vnfs)) +
56                            ' VNF[s] with ' + ' '.join(map(str, self._vnfs)))
57
58     def get_vnfs(self):
59         """Returns a list of vnfs controlled by this controller.
60         """
61         self._logger.debug('get_vnfs ' + str(len(self._vnfs)) +
62                            ' VNF[s] with ' + ' '.join(map(str, self._vnfs)))
63         return self._vnfs
64
65     def start(self):
66         """Boots all VNFs set-up by __init__.
67
68         This is a blocking function.
69         """
70         self._logger.debug('start ' + str(len(self._vnfs)) +
71                            ' VNF[s] with ' + ' '.join(map(str, self._vnfs)))
72         try:
73             for vnf in self._vnfs:
74                 vnf.start()
75         except pexpect.TIMEOUT:
76             self.stop()
77             raise
78
79     def stop(self):
80         """Stops all VNFs set-up by __init__.
81
82         This is a blocking function.
83         """
84         self._logger.debug('stop ' + str(len(self._vnfs)) +
85                            ' VNF[s] with ' + ' '.join(map(str, self._vnfs)))
86         for vnf in self._vnfs:
87             vnf.stop()
88
89     def __enter__(self):
90         self.start()
91
92     def __exit__(self, type_, value, traceback):
93         self.stop()