992d2d0a0c7d3dbf316168305f28522b3549a441
[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
28 class RapidConfigParser(object):
29     """
30     Class to deal with rapid configuration files
31     """
32     @staticmethod
33     def parse_config(test_params):
34         testconfig = configparser.RawConfigParser()
35         testconfig.read(test_params['test_file'])
36         test_params['required_number_of_test_machines'] = int(testconfig.get(
37             'TestParameters', 'total_number_of_test_machines'))
38         test_params['number_of_tests'] = int(testconfig.get('TestParameters',
39             'number_of_tests'))
40         test_params['TestName'] = testconfig.get('TestParameters', 'name')
41         if testconfig.has_option('TestParameters', 'lat_percentile'):
42             test_params['lat_percentile'] = old_div(float(
43                 testconfig.get('TestParameters', 'lat_percentile')),100.0)
44         else:
45             test_params['lat_percentile'] = 0.99
46         RapidLog.info('Latency percentile at {:.0f}%'.format(
47             test_params['lat_percentile']*100))
48         if testconfig.has_option('TestParameters', 'ipv6'):
49             test_params['ipv6'] = testconfig.getboolean('TestParameters','ipv6')
50         else:
51             test_params['ipv6'] = False
52         config = configparser.RawConfigParser()
53         config.read(test_params['environment_file'])
54         test_params['vim_type'] = config.get('Varia', 'vim')
55         test_params['key'] = config.get('ssh', 'key')
56         test_params['user'] = config.get('ssh', 'user')
57         test_params['total_number_of_machines'] = int(config.get('rapid',
58             'total_number_of_machines'))
59         tests = []
60         test = {}
61         for test_index in range(1, test_params['number_of_tests']+1):
62             test.clear()
63             section = 'test%d'%test_index
64             options = testconfig.options(section)
65             for option in options:
66                 if option in ['imix','imixs','flows']:
67                     test[option] = ast.literal_eval(testconfig.get(section,
68                         option))
69                 elif option in ['maxframespersecondallingress','stepsize',
70                         'flowsize']:
71                     test[option] = int(testconfig.get(section, option))
72                 elif option in ['startspeed', 'step', 'drop_rate_threshold',
73                         'lat_avg_threshold','lat_perc_threshold',
74                         'lat_max_threshold','accuracy','maxr','maxz',
75                         'pass_threshold','ramp_step']:
76                     test[option] = float(testconfig.get(section, option))
77                 else:
78                     test[option] = testconfig.get(section, option)
79             tests.append(dict(test))
80         for test in tests:
81             if test['test'] in ['flowsizetest','TST009test']:
82                 if 'drop_rate_threshold' not in test.keys():
83                     test['drop_rate_threshold'] = 0
84                 latency_thresholds = ['lat_avg_threshold','lat_perc_threshold','lat_max_threshold']
85                 for threshold in latency_thresholds:
86                     if threshold not in test.keys():
87                         test[threshold] = 'inf'
88         test_params['tests'] = tests
89         if test_params['required_number_of_test_machines'] > test_params[
90                 'total_number_of_machines']:
91             RapidLog.exception("Not enough VMs for this test: %d needed and only %d available" % (required_number_of_test_machines,total_number_of_machines))
92             raise Exception("Not enough VMs for this test: %d needed and only %d available" % (required_number_of_test_machines,total_number_of_machines))
93         machine_map = configparser.RawConfigParser()
94         machine_map.read(test_params['machine_map_file'])
95         machines = []
96         machine = {}
97         for test_machine in range(1, test_params['required_number_of_test_machines']+1):
98             machine.clear()
99             section = 'TestM%d'%test_machine
100             options = testconfig.options(section)
101             for option in options:
102                 if option in ['prox_socket','prox_launch_exit','monitor']:
103                     machine[option] = testconfig.getboolean(section, option)
104                 elif option in ['cores', 'gencores','latcores']:
105                     machine[option] = ast.literal_eval(testconfig.get(
106                         section, option))
107                 elif option in ['bucket_size_exp']:
108                     machine[option] = int(testconfig.get(section, option))
109                     if machine[option] < 11:
110                         RapidLog.exception("Minimum Value for bucket_size_exp is 11")
111                 else:
112                     machine[option] = testconfig.get(section, option)
113                 for key in ['prox_socket','prox_launch_exit']:
114                    if key not in machine.keys():
115                        machine[key] = True
116             if 'monitor' not in machine.keys():
117                 machine['monitor'] = True
118             index = int(machine_map.get('TestM%d'%test_machine,
119                 'machine_index'))
120             section = 'M%d'%index
121             options = config.options(section)
122             for option in options:
123                 machine[option] = config.get(section, option)
124             machines.append(dict(machine))
125         for machine in machines:
126             dp_ports = []
127             if 'dest_vm' in machine.keys():
128                 index = 1
129                 while True: 
130                     dp_ip_key = 'dp_ip{}'.format(index)
131                     dp_mac_key = 'dp_mac{}'.format(index)
132                     if dp_ip_key in machines[int(machine['dest_vm'])-1].keys() and \
133                             dp_mac_key in machines[int(machine['dest_vm'])-1].keys():
134                         dp_port = {'ip': machines[int(machine['dest_vm'])-1][dp_ip_key],
135                                 'mac' : machines[int(machine['dest_vm'])-1][dp_mac_key]}
136                         dp_ports.append(dict(dp_port))
137                         index += 1
138                     else:
139                         break
140                     machine['dest_ports'] = list(dp_ports)
141             gw_ips = []
142             if 'gw_vm' in machine.keys():
143                 index = 1
144                 while True:
145                     gw_ip_key = 'dp_ip{}'.format(index)
146                     if gw_ip_key in machines[int(machine['gw_vm'])-1].keys():
147                         gw_ip = machines[int(machine['gw_vm'])-1][gw_ip_key]
148                         gw_ips.append(gw_ip)
149                         index += 1
150                     else:
151                         break
152                     machine['gw_ips'] = list(gw_ips)
153         test_params['machines'] = machines
154         return (test_params)