ee8fa67f5734c3da0d6c07a2ed4e3486788443f0
[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', 'sleep_time'):
50             test_params['sleep_time'] = int(testconfig.get('TestParameters', 'sleep_time'))
51             if test_params['sleep_time'] < 2:
52                 test_params['sleep_time'] = 2
53         else:
54             test_params['sleep_time'] = 2
55
56         if testconfig.has_option('TestParameters', 'ipv6'):
57             test_params['ipv6'] = testconfig.getboolean('TestParameters','ipv6')
58         else:
59             test_params['ipv6'] = False
60         config = configparser.RawConfigParser()
61         config.read(test_params['environment_file'])
62         test_params['vim_type'] = config.get('Varia', 'vim')
63         test_params['user'] = config.get('ssh', 'user')
64         if config.has_option('ssh', 'key'):
65             test_params['key'] = config.get('ssh', 'key')
66             if test_params['user'] in ['rapid']:
67                 if test_params['key'] != 'rapid_rsa_key':
68                     RapidLog.debug(("Key file {} for user {} overruled by key file:"
69                             " rapid_rsa_key").format(test_params['key'],
70                             test_params['user']))
71                     test_params['key'] = 'rapid_rsa_key'
72         else:
73             test_params['key'] = None
74         if config.has_option('ssh', 'password'):
75             test_params['password'] = config.get('ssh', 'password')
76         else:
77             test_params['password'] = None
78         test_params['total_number_of_machines'] = int(config.get('rapid',
79             'total_number_of_machines'))
80         tests = []
81         test = {}
82         for test_index in range(1, test_params['number_of_tests']+1):
83             test.clear()
84             section = 'test%d'%test_index
85             options = testconfig.options(section)
86             for option in options:
87                 if option in ['imix','imixs','flows', 'warmupimix']:
88                     test[option] = ast.literal_eval(testconfig.get(section,
89                         option))
90                 elif option in ['maxframespersecondallingress','stepsize',
91                         'flowsize','warmupflowsize','warmuptime', 'steps']:
92                     test[option] = int(testconfig.get(section, option))
93                 elif option in ['startspeed', 'step', 'drop_rate_threshold',
94                         'generator_threshold','lat_avg_threshold','lat_perc_threshold',
95                         'lat_max_threshold','accuracy','maxr','maxz',
96                         'ramp_step','warmupspeed','mis_ordered_threshold']:
97                     test[option] = float(testconfig.get(section, option))
98                 else:
99                     test[option] = testconfig.get(section, option)
100             tests.append(dict(test))
101         for test in tests:
102             if test['test'] in ['flowsizetest', 'TST009test', 'increment_till_fail']:
103                 if 'drop_rate_threshold' not in test.keys():
104                     test['drop_rate_threshold'] = 0
105                 thresholds = ['generator_threshold','lat_avg_threshold', \
106                         'lat_perc_threshold','lat_max_threshold','mis_ordered_threshold']
107                 for threshold in thresholds:
108                     if threshold not in test.keys():
109                         test[threshold] = inf
110         test_params['tests'] = tests
111         if test_params['required_number_of_test_machines'] > test_params[
112                 'total_number_of_machines']:
113             RapidLog.exception("Not enough VMs for this test: %d needed and only %d available" % (required_number_of_test_machines,total_number_of_machines))
114             raise Exception("Not enough VMs for this test: %d needed and only %d available" % (required_number_of_test_machines,total_number_of_machines))
115         map_info = test_params['machine_map_file'].strip('[]').split(',')
116         map_info_length = len(map_info)
117         # If map_info is a list where the first entry is numeric, we assume we
118         # are dealing with a list of machines and NOT the machine.map file
119         if map_info[0].isnumeric():
120             if map_info_length < test_params[
121                     'required_number_of_test_machines']:
122                 RapidLog.exception('Not enough machine indices in --map \
123                         parameter: {}. Needing {} entries'.format(map_info,
124                             test_params['required_number_of_test_machines']))
125             machine_index = list(map(int,map_info))
126         else:
127             machine_map = configparser.RawConfigParser()
128             machine_map.read(test_params['machine_map_file'])
129             machine_index = []
130             for test_machine in range(1,
131                     test_params['required_number_of_test_machines']+1):
132                 machine_index.append(int(machine_map.get(
133                     'TestM%d'%test_machine, 'machine_index')))
134         machine_map = configparser.RawConfigParser()
135         machine_map.read(test_params['machine_map_file'])
136         machines = []
137         machine = {}
138         for test_machine in range(1, test_params[
139             'required_number_of_test_machines']+1):
140             machine.clear()
141             section = 'TestM%d'%test_machine
142             options = testconfig.options(section)
143             for option in options:
144                 if option in ['prox_socket','prox_launch_exit','monitor']:
145                     machine[option] = testconfig.getboolean(section, option)
146                 elif option in ['mcore', 'cores', 'gencores','latcores']:
147                     machine[option] = ast.literal_eval(testconfig.get(
148                         section, option))
149                 elif option in ['bucket_size_exp']:
150                     machine[option] = int(testconfig.get(section, option))
151                     if machine[option] < 11:
152                         RapidLog.exception(
153                                 "Minimum Value for bucket_size_exp is 11")
154                 else:
155                     machine[option] = testconfig.get(section, option)
156                 for key in ['prox_socket','prox_launch_exit']:
157                    if key not in machine.keys():
158                        machine[key] = True
159             if 'monitor' not in machine.keys():
160                 machine['monitor'] = True
161             section = 'M%d'%machine_index[test_machine-1]
162             options = config.options(section)
163             for option in options:
164                 machine[option] = config.get(section, option)
165             machines.append(dict(machine))
166         for machine in machines:
167             dp_ports = []
168             if 'dest_vm' in machine.keys():
169                 index = 1
170                 while True: 
171                     dp_ip_key = 'dp_ip{}'.format(index)
172                     dp_mac_key = 'dp_mac{}'.format(index)
173                     if dp_ip_key in machines[int(machine['dest_vm'])-1].keys() and \
174                             dp_mac_key in machines[int(machine['dest_vm'])-1].keys():
175                         dp_port = {'ip': machines[int(machine['dest_vm'])-1][dp_ip_key],
176                                 'mac' : machines[int(machine['dest_vm'])-1][dp_mac_key]}
177                         dp_ports.append(dict(dp_port))
178                         index += 1
179                     else:
180                         break
181                     machine['dest_ports'] = list(dp_ports)
182             gw_ips = []
183             if 'gw_vm' in machine.keys():
184                 index = 1
185                 while True:
186                     gw_ip_key = 'dp_ip{}'.format(index)
187                     if gw_ip_key in machines[int(machine['gw_vm'])-1].keys():
188                         gw_ip = machines[int(machine['gw_vm'])-1][gw_ip_key]
189                         gw_ips.append(gw_ip)
190                         index += 1
191                     else:
192                         break
193                     machine['gw_ips'] = list(gw_ips)
194         test_params['machines'] = machines
195         return (test_params)