1 # Copyright 2015-2017 Intel Corporation.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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
19 from conf import settings
20 from vnfs.vnf.vnf import IVnf
22 class VnfController(object):
23 """VNF controller class
25 Used to set-up and control VNFs for specified scenario
28 _vnf_class: A class object representing the VNF to be used.
29 _deployment: A string describing the scenario to set-up in the
31 _vnfs: A list of vnfs controlled by the controller.
34 def __init__(self, deployment, vnf_class, extra_vnfs):
35 """Sets up the VNF infrastructure based on deployment scenario
37 :param vnf_class: The VNF class to be used.
38 :param extra_vnfs: The number of VNFs not involved in given
39 deployment scenario. It will be used to correctly expand
40 configuration values and initialize shared dirs. This parameter
41 is used in case, that additional VNFs are executed by TestSteps.
43 # reset VNF ID counter for each testcase
44 IVnf.reset_vnf_counter()
46 # setup controller with requested number of VNFs
47 self._logger = logging.getLogger(__name__)
48 self._vnf_class = vnf_class
49 self._deployment = deployment.lower()
51 if self._deployment == 'pvp':
53 elif (self._deployment.startswith('pvvp') or
54 self._deployment.startswith('pvpv')):
55 if len(self._deployment) > 4:
56 vm_number = int(self._deployment[4:])
60 # VnfController is created for all deployments, including deployments
61 # without VNFs like p2p
64 if vm_number + extra_vnfs > 0:
65 self._logger.debug('Check configuration for %s guests.', vm_number + extra_vnfs)
66 settings.check_vm_settings(vm_number + extra_vnfs)
67 # enforce that GUEST_NIC_NR is 1 or even number of NICs
69 nics_nr = settings.getValue('GUEST_NICS_NR')
70 for index, value in enumerate(nics_nr):
71 if value > 1 and value % 2:
73 nics_nr[index] = int(value / 2) * 2
75 settings.setValue('GUEST_NICS_NR', nics_nr)
76 self._logger.warning('Odd number of NICs was detected. Configuration '
77 'was updated to GUEST_NICS_NR = %s',
78 settings.getValue('GUEST_NICS_NR'))
81 self._vnfs = [vnf_class() for _ in range(vm_number)]
83 self._logger.debug('__init__ ' + str(len(self._vnfs)) +
84 ' VNF[s] with ' + ' '.join(map(str, self._vnfs)))
87 """Returns a list of vnfs controlled by this controller.
89 self._logger.debug('get_vnfs ' + str(len(self._vnfs)) +
90 ' VNF[s] with ' + ' '.join(map(str, self._vnfs)))
93 def get_vnfs_number(self):
94 """Returns a number of vnfs controlled by this controller.
96 self._logger.debug('get_vnfs_number %s VNF[s]', str(len(self._vnfs)))
97 return len(self._vnfs)
100 """Boots all VNFs set-up by __init__.
102 This is a blocking function.
104 self._logger.debug('start ' + str(len(self._vnfs)) +
105 ' VNF[s] with ' + ' '.join(map(str, self._vnfs)))
107 for vnf in self._vnfs:
109 except pexpect.TIMEOUT:
114 """Stops all VNFs set-up by __init__.
116 This is a blocking function.
118 self._logger.debug('stop ' + str(len(self._vnfs)) +
119 ' VNF[s] with ' + ' '.join(map(str, self._vnfs)))
120 for vnf in self._vnfs:
126 def __exit__(self, type_, value, traceback):