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