multi VM: Fix p2p deployment
[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             # VnfController is created for all deployments, including deployments
57             # without VNFs like p2p
58             vm_number = 0
59
60         if vm_number:
61             self._logger.debug('Check configuration for %s guests.', vm_number)
62             settings.check_vm_settings(vm_number)
63             # enforce that GUEST_NIC_NR is 1 or even number of NICs
64             updated = False
65             nics_nr = settings.getValue('GUEST_NICS_NR')
66             for index in range(len(nics_nr)):
67                 if nics_nr[index] > 1 and nics_nr[index] % 2:
68                     updated = True
69                     nics_nr[index] = int(nics_nr[index] / 2) * 2
70             if updated:
71                 settings.setValue('GUEST_NICS_NR', nics_nr)
72                 self._logger.warning('Odd number of NICs was detected. Configuration '
73                                      'was updated to GUEST_NICS_NR = %s',
74                                      settings.getValue('GUEST_NICS_NR'))
75
76             self._vnfs = [vnf_class() for _ in range(vm_number)]
77
78         self._logger.debug('__init__ ' + str(len(self._vnfs)) +
79                            ' VNF[s] with ' + ' '.join(map(str, self._vnfs)))
80
81     def get_vnfs(self):
82         """Returns a list of vnfs controlled by this controller.
83         """
84         self._logger.debug('get_vnfs ' + str(len(self._vnfs)) +
85                            ' VNF[s] with ' + ' '.join(map(str, self._vnfs)))
86         return self._vnfs
87
88     def get_vnfs_number(self):
89         """Returns a number of vnfs controlled by this controller.
90         """
91         self._logger.debug('get_vnfs_number ' + str(len(self._vnfs)) +
92                            ' VNF[s]')
93         return len(self._vnfs)
94
95     def start(self):
96         """Boots all VNFs set-up by __init__.
97
98         This is a blocking function.
99         """
100         self._logger.debug('start ' + str(len(self._vnfs)) +
101                            ' VNF[s] with ' + ' '.join(map(str, self._vnfs)))
102         try:
103             for vnf in self._vnfs:
104                 vnf.start()
105         except pexpect.TIMEOUT:
106             self.stop()
107             raise
108
109     def stop(self):
110         """Stops all VNFs set-up by __init__.
111
112         This is a blocking function.
113         """
114         self._logger.debug('stop ' + str(len(self._vnfs)) +
115                            ' VNF[s] with ' + ' '.join(map(str, self._vnfs)))
116         for vnf in self._vnfs:
117             vnf.stop()
118
119     def __enter__(self):
120         self.start()
121
122     def __exit__(self, type_, value, traceback):
123         self.stop()