1 # Copyright 2015-2016 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):
35 """Sets up the VNF infrastructure based on deployment scenario
37 :param vnf_class: The VNF class to be used.
39 # reset VNF ID counter for each testcase
40 IVnf.reset_vnf_counter()
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()
47 if self._deployment == 'pvp':
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:])
56 raise RuntimeError('Deployment {} is not supported by '
57 'VnfController.'.format(self._deployment))
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
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:
68 nics_nr[index] = int(nics_nr[index] / 2) * 2
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'))
75 self._vnfs = [vnf_class() for _ in range(vm_number)]
77 self._logger.debug('__init__ ' + str(len(self._vnfs)) +
78 ' VNF[s] with ' + ' '.join(map(str, self._vnfs)))
81 """Returns a list of vnfs controlled by this controller.
83 self._logger.debug('get_vnfs ' + str(len(self._vnfs)) +
84 ' VNF[s] with ' + ' '.join(map(str, self._vnfs)))
87 def get_vnfs_number(self):
88 """Returns a number of vnfs controlled by this controller.
90 self._logger.debug('get_vnfs_number ' + str(len(self._vnfs)) +
92 return len(self._vnfs)
95 """Boots all VNFs set-up by __init__.
97 This is a blocking function.
99 self._logger.debug('start ' + str(len(self._vnfs)) +
100 ' VNF[s] with ' + ' '.join(map(str, self._vnfs)))
102 for vnf in self._vnfs:
104 except pexpect.TIMEOUT:
109 """Stops all VNFs set-up by __init__.
111 This is a blocking function.
113 self._logger.debug('stop ' + str(len(self._vnfs)) +
114 ' VNF[s] with ' + ' '.join(map(str, self._vnfs)))
115 for vnf in self._vnfs:
121 def __exit__(self, type_, value, traceback):