multi VM: Multi VMs in serial or parallel
[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 conf import settings
20 from vnfs.vnf.vnf import IVnf
21
22 class VnfController(object):
23     """VNF controller class
24
25     Used to set-up and control VNFs for specified scenario
26
27     Attributes:
28         _vnf_class: A class object representing the VNF to be used.
29         _deployment: A string describing the scenario to set-up in the
30             constructor.
31         _vnfs: A list of vnfs controlled by the controller.
32     """
33
34     def __init__(self, deployment, vnf_class):
35         """Sets up the VNF infrastructure based on deployment scenario
36
37         :param vnf_class: The VNF class to be used.
38         """
39         # reset VNF ID counter for each testcase
40         IVnf.reset_vnf_counter()
41
42         # setup controller with requested number of VNFs
43         self._logger = logging.getLogger(__name__)
44         self._vnf_class = vnf_class
45         self._deployment = deployment.lower()
46         self._vnfs = []
47         if self._deployment == 'pvp':
48             vm_number = 1
49         elif (self._deployment.startswith('pvvp') or
50               self._deployment.startswith('pvpv')):
51             if len(self._deployment) > 4:
52                 vm_number = int(self._deployment[4:])
53             else:
54                 vm_number = 2
55         else:
56             raise RuntimeError('Deployment {} is not supported by '
57                                'VnfController.'.format(self._deployment))
58
59         if vm_number:
60             self._logger.debug('Check configuration for %s guests.', vm_number)
61             settings.check_vm_settings(vm_number)
62             # enforce that GUEST_NIC_NR is 1 or even number of NICs
63             updated = False
64             nics_nr = settings.getValue('GUEST_NICS_NR')
65             for index in range(len(nics_nr)):
66                 if nics_nr[index] > 1 and nics_nr[index] % 2:
67                     updated = True
68                     nics_nr[index] = int(nics_nr[index] / 2) * 2
69             if updated:
70                 settings.setValue('GUEST_NICS_NR', nics_nr)
71                 self._logger.warning('Odd number of NICs was detected. Configuration '
72                                      'was updated to GUEST_NICS_NR = %s',
73                                      settings.getValue('GUEST_NICS_NR'))
74
75             self._vnfs = [vnf_class() for _ in range(vm_number)]
76
77         self._logger.debug('__init__ ' + str(len(self._vnfs)) +
78                            ' VNF[s] with ' + ' '.join(map(str, self._vnfs)))
79
80     def get_vnfs(self):
81         """Returns a list of vnfs controlled by this controller.
82         """
83         self._logger.debug('get_vnfs ' + str(len(self._vnfs)) +
84                            ' VNF[s] with ' + ' '.join(map(str, self._vnfs)))
85         return self._vnfs
86
87     def get_vnfs_number(self):
88         """Returns a number of vnfs controlled by this controller.
89         """
90         self._logger.debug('get_vnfs_number ' + str(len(self._vnfs)) +
91                            ' VNF[s]')
92         return len(self._vnfs)
93
94     def start(self):
95         """Boots all VNFs set-up by __init__.
96
97         This is a blocking function.
98         """
99         self._logger.debug('start ' + str(len(self._vnfs)) +
100                            ' VNF[s] with ' + ' '.join(map(str, self._vnfs)))
101         try:
102             for vnf in self._vnfs:
103                 vnf.start()
104         except pexpect.TIMEOUT:
105             self.stop()
106             raise
107
108     def stop(self):
109         """Stops all VNFs set-up by __init__.
110
111         This is a blocking function.
112         """
113         self._logger.debug('stop ' + str(len(self._vnfs)) +
114                            ' VNF[s] with ' + ' '.join(map(str, self._vnfs)))
115         for vnf in self._vnfs:
116             vnf.stop()
117
118     def __enter__(self):
119         self.start()
120
121     def __exit__(self, type_, value, traceback):
122         self.stop()