Reinstate installation of igb_uio
[samplevnf.git] / VNFs / DPPD-PROX / helper-scripts / rapid / rapid_parser.py
1 #!/usr/bin/python
2
3 ##
4 ## Copyright (c) 2020 Intel Corporation
5 ##
6 ## Licensed under the Apache License, Version 2.0 (the "License");
7 ## you may not use this file except in compliance with the License.
8 ## You may obtain a copy of the License at
9 ##
10 ##     http://www.apache.org/licenses/LICENSE-2.0
11 ##
12 ## Unless required by applicable law or agreed to in writing, software
13 ## distributed under the License is distributed on an "AS IS" BASIS,
14 ## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 ## See the License for the specific language governing permissions and
16 ## limitations under the License.
17 ##
18
19 from rapid_log import RapidLog 
20 from past.utils import old_div
21 try:
22     import configparser
23 except ImportError:
24     # Python 2.x fallback
25     import ConfigParser as configparser
26 import ast
27 inf = float("inf")
28
29 class RapidConfigParser(object):
30     """
31     Class to deal with rapid configuration files
32     """
33     @staticmethod
34     def parse_config(test_params):
35         testconfig = configparser.RawConfigParser()
36         testconfig.read(test_params['test_file'])
37         test_params['required_number_of_test_machines'] = int(testconfig.get(
38             'TestParameters', 'total_number_of_test_machines'))
39         test_params['number_of_tests'] = int(testconfig.get('TestParameters',
40             'number_of_tests'))
41         test_params['TestName'] = testconfig.get('TestParameters', 'name')
42         if testconfig.has_option('TestParameters', 'lat_percentile'):
43             test_params['lat_percentile'] = old_div(float(
44                 testconfig.get('TestParameters', 'lat_percentile')),100.0)
45         else:
46             test_params['lat_percentile'] = 0.99
47         RapidLog.info('Latency percentile at {:.0f}%'.format(
48             test_params['lat_percentile']*100))
49         if testconfig.has_option('TestParameters', 'ipv6'):
50             test_params['ipv6'] = testconfig.getboolean('TestParameters','ipv6')
51         else:
52             test_params['ipv6'] = False
53         config = configparser.RawConfigParser()
54         config.read(test_params['environment_file'])
55         test_params['vim_type'] = config.get('Varia', 'vim')
56         test_params['key'] = config.get('ssh', 'key')
57         test_params['user'] = config.get('ssh', 'user')
58         if test_params['user'] in ['rapid']:
59             if test_params['key'] != 'rapid_rsa_key':
60                 RapidLog.debug(("Key file {} for user {} overruled by key file:"
61                         " rapid_rsa_key").format(test_params['key'],
62                         test_params['user']))
63                 test_params['key'] = 'rapid_rsa_key'
64         test_params['total_number_of_machines'] = int(config.get('rapid',
65             'total_number_of_machines'))
66         tests = []
67         test = {}
68         for test_index in range(1, test_params['number_of_tests']+1):
69             test.clear()
70             section = 'test%d'%test_index
71             options = testconfig.options(section)
72             for option in options:
73                 if option in ['imix','imixs','flows', 'warmupimix']:
74                     test[option] = ast.literal_eval(testconfig.get(section,
75                         option))
76                 elif option in ['maxframespersecondallingress','stepsize',
77                         'flowsize','warmupflowsize','warmuptime', 'steps']:
78                     test[option] = int(testconfig.get(section, option))
79                 elif option in ['startspeed', 'step', 'drop_rate_threshold',
80                         'lat_avg_threshold','lat_perc_threshold',
81                         'lat_max_threshold','accuracy','maxr','maxz',
82                         'ramp_step','warmupspeed']:
83                     test[option] = float(testconfig.get(section, option))
84                 else:
85                     test[option] = testconfig.get(section, option)
86             tests.append(dict(test))
87         for test in tests:
88             if test['test'] in ['flowsizetest','TST009test']:
89                 if 'drop_rate_threshold' not in test.keys():
90                     test['drop_rate_threshold'] = 0
91                 latency_thresholds = ['lat_avg_threshold','lat_perc_threshold','lat_max_threshold']
92                 for threshold in latency_thresholds:
93                     if threshold not in test.keys():
94                         test[threshold] = inf
95         test_params['tests'] = tests
96         if test_params['required_number_of_test_machines'] > test_params[
97                 'total_number_of_machines']:
98             RapidLog.exception("Not enough VMs for this test: %d needed and only %d available" % (required_number_of_test_machines,total_number_of_machines))
99             raise Exception("Not enough VMs for this test: %d needed and only %d available" % (required_number_of_test_machines,total_number_of_machines))
100         map_info = test_params['machine_map_file'].strip('[]').split(',')
101         map_info_length = len(map_info)
102         # If map_info is a list where the first entry is numeric, we assume we
103         # are dealing with a list of machines and NOT the machine.map file
104         if map_info[0].isnumeric():
105             if map_info_length < test_params[
106                     'required_number_of_test_machines']:
107                 RapidLog.exception('Not enough machine indices in --map \
108                         parameter: {}. Needing {} entries'.format(map_info,
109                             test_params['required_number_of_test_machines']))
110             machine_index = list(map(int,map_info))
111         else:
112             machine_map = configparser.RawConfigParser()
113             machine_map.read(test_params['machine_map_file'])
114             machine_index = []
115             for test_machine in range(1,
116                     test_params['required_number_of_test_machines']+1):
117                 machine_index.append(int(machine_map.get(
118                     'TestM%d'%test_machine, 'machine_index')))
119         machine_map = configparser.RawConfigParser()
120         machine_map.read(test_params['machine_map_file'])
121         machines = []
122         machine = {}
123         for test_machine in range(1, test_params[
124             'required_number_of_test_machines']+1):
125             machine.clear()
126             section = 'TestM%d'%test_machine
127             options = testconfig.options(section)
128             for option in options:
129                 if option in ['prox_socket','prox_launch_exit','monitor']:
130                     machine[option] = testconfig.getboolean(section, option)
131                 elif option in ['mcore', 'cores', 'gencores','latcores']:
132                     machine[option] = ast.literal_eval(testconfig.get(
133                         section, option))
134                 elif option in ['bucket_size_exp']:
135                     machine[option] = int(testconfig.get(section, option))
136                     if machine[option] < 11:
137                         RapidLog.exception(
138                                 "Minimum Value for bucket_size_exp is 11")
139                 else:
140                     machine[option] = testconfig.get(section, option)
141                 for key in ['prox_socket','prox_launch_exit']:
142                    if key not in machine.keys():
143                        machine[key] = True
144             if 'monitor' not in machine.keys():
145                 machine['monitor'] = True
146             section = 'M%d'%machine_index[test_machine-1]
147             options = config.options(section)
148             for option in options:
149                 machine[option] = config.get(section, option)
150             machines.append(dict(machine))
151         for machine in machines:
152             dp_ports = []
153             if 'dest_vm' in machine.keys():
154                 index = 1
155                 while True: 
156                     dp_ip_key = 'dp_ip{}'.format(index)
157                     dp_mac_key = 'dp_mac{}'.format(index)
158                     if dp_ip_key in machines[int(machine['dest_vm'])-1].keys() and \
159                             dp_mac_key in machines[int(machine['dest_vm'])-1].keys():
160                         dp_port = {'ip': machines[int(machine['dest_vm'])-1][dp_ip_key],
161                                 'mac' : machines[int(machine['dest_vm'])-1][dp_mac_key]}
162                         dp_ports.append(dict(dp_port))
163                         index += 1
164                     else:
165                         break
166                     machine['dest_ports'] = list(dp_ports)
167             gw_ips = []
168             if 'gw_vm' in machine.keys():
169                 index = 1
170                 while True:
171                     gw_ip_key = 'dp_ip{}'.format(index)
172                     if gw_ip_key in machines[int(machine['gw_vm'])-1].keys():
173                         gw_ip = machines[int(machine['gw_vm'])-1][gw_ip_key]
174                         gw_ips.append(gw_ip)
175                         index += 1
176                     else:
177                         break
178                     machine['gw_ips'] = list(gw_ips)
179         test_params['machines'] = machines
180         return (test_params)