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