fix vcpeutils
[testsuite/python-testing-utils.git] / robotframework-onap / vcpeutils / preload.py
1 #! /usr/bin/python
2
3 from datetime import datetime
4 from vcpeutils.vcpecommon import *
5 from vcpeutils.csar_parser import *
6 from robot.api import logger
7 from past import builtins
8 import base64
9
10
11 class Preload:
12     def __init__(self, vcpecommon):
13         self.logger = logger
14         self.vcpecommon = vcpecommon
15
16     def replace(self, sz, replace_dict):
17         for old_string, new_string in list(replace_dict.items()):
18             sz = sz.replace(old_string, new_string)
19         if self.vcpecommon.template_variable_symbol in sz:
20             self.logger.error('Error! Cannot find a value to replace ' + sz)
21         return sz
22
23     def generate_json(self, template_file, replace_dict):
24         with open(template_file) as json_input:
25             json_data = json.load(json_input)
26             stk = [json_data]
27             while len(stk) > 0:
28                 data = stk.pop()
29                 for k, v in list(data.items()):
30                     if type(v) is dict:
31                         stk.append(v)
32                     elif type(v) is list:
33                         stk.extend(v)
34                     elif type(v) is builtins.basestring:
35                         if self.vcpecommon.template_variable_symbol in v:
36                             data[k] = self.replace(v, replace_dict)
37                     else:
38                         self.logger.warn('Unexpected line in template: {}. Look for value {}'.format(template_file, v))
39         return json_data
40
41     def reset_sniro(self):
42         self.logger.debug('Clearing SNIRO data')
43         r = requests.post(self.vcpecommon.sniro_url + '/reset', headers=self.vcpecommon.sniro_headers)
44         if 2 != r.status_code / 100:
45             self.logger.debug(r.content)
46             self.logger.error('Clearing SNIRO date failed.')
47             sys.exit()
48
49     def preload_sniro(self, template_sniro_data, template_sniro_request, tunnelxconn_ar_name, vgw_name, vbrg_ar_name,
50                       vgmux_svc_instance_uuid, vbrg_svc_instance_uuid):
51         self.reset_sniro()
52         self.logger.info('Preloading SNIRO for homing service')
53         replace_dict = {'${tunnelxconn_ar_name}': tunnelxconn_ar_name,
54                         '${vgw_name}': vgw_name,
55                         '${brg_ar_name}': vbrg_ar_name,
56                         '${vgmux_svc_instance_uuid}': vgmux_svc_instance_uuid,
57                         '${vbrg_svc_instance_uuid}': vbrg_svc_instance_uuid
58                         }
59         sniro_data = self.generate_json(template_sniro_data, replace_dict)
60         self.logger.debug('SNIRO data:')
61         self.logger.debug(json.dumps(sniro_data, indent=4, sort_keys=True))
62
63         base64_sniro_data = base64.b64encode(json.dumps(sniro_data))
64         self.logger.debug('SNIRO data: 64')
65         self.logger.debug(base64_sniro_data)
66         replace_dict = {'${base64_sniro_data}': base64_sniro_data, '${sniro_ip}': self.vcpecommon.hosts['robot']}
67         sniro_request = self.generate_json(template_sniro_request, replace_dict)
68         self.logger.debug('SNIRO request:')
69         self.logger.debug(json.dumps(sniro_request, indent=4, sort_keys=True))
70
71         r = requests.post(self.vcpecommon.sniro_url, headers=self.vcpecommon.sniro_headers, json=sniro_request)
72         if 2 != r.status_code / 100:
73             response = r.json()
74             self.logger.debug(json.dumps(response, indent=4, sort_keys=True))
75             self.logger.error('SNIRO preloading failed.')
76             sys.exit()
77
78         return True
79
80     def preload_network(self, template_file, network_role, subnet_start_ip, subnet_gateway, common_dict, name_suffix):
81         """
82         :param template_file:
83         :param network_role: cpe_signal, cpe_public, brg_bng, bng_mux, mux_gw
84         :param subnet_start_ip:
85         :param subnet_gateway:
86         :param common_dict:
87         :param name_suffix: e.g. '201711201311'
88         :return:
89         """
90         network_name = '_'.join([self.vcpecommon.instance_name_prefix['network'], network_role.lower(), name_suffix])
91         subnet_name = self.vcpecommon.network_name_to_subnet_name(network_name)
92         common_dict['${' + network_role+'_net}'] = network_name
93         common_dict['${' + network_role+'_subnet}'] = subnet_name
94         replace_dict = {'${network_role}': network_role,
95                         '${service_type}': 'vCPE',
96                         '${network_type}': 'Generic NeutronNet',
97                         '${network_name}': network_name,
98                         '${subnet_start_ip}': subnet_start_ip,
99                         '${subnet_gateway}': subnet_gateway
100                         }
101         self.logger.info('Preloading network ' + network_role)
102         return self.preload(template_file, replace_dict, self.vcpecommon.sdnc_preload_network_url)
103
104     def preload(self, template_file, replace_dict, url):
105         self.logger.debug(json.dumps(replace_dict, indent=4, sort_keys=True))
106         json_data = self.generate_json(template_file, replace_dict)
107         self.logger.debug(json.dumps(json_data, indent=4, sort_keys=True))
108         r = requests.post(url, headers=self.vcpecommon.sdnc_headers, auth=self.vcpecommon.sdnc_userpass, json=json_data)
109         response = r.json()
110         if int(response.get('output', {}).get('response-code', 0)) != 200:
111             self.logger.debug(json.dumps(response, indent=4, sort_keys=True))
112             self.logger.error('Preloading failed.')
113             return False
114         return True
115
116     def preload_vgw(self, template_file, brg_mac, commont_dict, name_suffix):
117         replace_dict = {'${brg_mac}': brg_mac,
118                         '${suffix}': name_suffix
119                         }
120         replace_dict.update(commont_dict)
121         self.logger.info('Preloading vGW')
122         return self.preload(template_file, replace_dict, self.vcpecommon.sdnc_preload_vnf_url)
123
124     def preload_vgw_gra(self, template_file, brg_mac, commont_dict, name_suffix, vgw_vfmod_name_index):
125         replace_dict = {'${brg_mac}': brg_mac,
126                         '${suffix}': name_suffix,
127                         '${vgw_vfmod_name_index}': vgw_vfmod_name_index
128                         }
129         replace_dict.update(commont_dict)
130         self.logger.info('Preloading vGW-GRA')
131         return self.preload(template_file, replace_dict, self.vcpecommon.sdnc_preload_gra_url)
132
133     def preload_vfmodule(self, template_file, service_instance_id, vnf_model, vfmodule_model, common_dict, name_suffix):
134         """
135         :param template_file:
136         :param service_instance_id:
137         :param vnf_model:  parsing results from csar_parser
138         :param vfmodule_model:  parsing results from csar_parser
139         :param common_dict:
140         :param name_suffix:
141         :return:
142         """
143
144         # examples:
145         # vfmodule_model['modelCustomizationName']: "Vspinfra111601..base_vcpe_infra..module-0",
146         # vnf_model['modelCustomizationName']: "vspinfra111601 0",
147
148         vfmodule_name = '_'.join([self.vcpecommon.instance_name_prefix['vfmodule'],
149                                   vfmodule_model['modelCustomizationName'].split('..')[0].lower(), name_suffix])
150
151         # vnf_type and generic_vnf_type are identical
152         replace_dict = {'${vnf_type}': vfmodule_model['modelCustomizationName'],
153                         '${generic_vnf_type}': vfmodule_model['modelCustomizationName'],
154                         '${service_type}': service_instance_id,
155                         '${generic_vnf_name}': vnf_model['modelCustomizationName'],
156                         '${vnf_name}': vfmodule_name,
157                         '${mr_ip_addr}': self.vcpecommon.mr_ip_addr,
158                         '${mr_ip_port}': self.vcpecommon.mr_ip_port,
159                         '${sdnc_oam_ip}': self.vcpecommon.sdnc_oam_ip,
160                         '${suffix}': name_suffix}
161         replace_dict.update(common_dict)
162         self.logger.info('Preloading VF Module ' + vfmodule_name)
163         return self.preload(template_file, replace_dict, self.vcpecommon.sdnc_preload_vnf_url)
164
165     def preload_all_networks(self, template_file, name_suffix):
166         common_dict = {'${' + k + '}': v for k, v in list(self.vcpecommon.common_preload_config.items())}
167         for network, v in list(self.vcpecommon.preload_network_config.items()):
168             subnet_start_ip, subnet_gateway_ip = v
169             if not self.preload_network(template_file, network, subnet_start_ip, subnet_gateway_ip,
170                                         common_dict, name_suffix):
171                 return None
172         return common_dict
173
174     def test(self):
175         # this is for testing purpose
176         name_suffix = datetime.now().strftime('%Y%m%d%H%M')
177         vcpecommon = VcpeCommon()
178         preloader = Preload(vcpecommon)
179
180         network_dict = {'${' + k + '}': v for k, v in list(self.vcpecommon.common_preload_config.items())}
181         template_file = 'preload_templates/template.network.json'
182         for k, v in list(self.vcpecommon.preload_network_config.items()):
183             if not preloader.preload_network(template_file, k, v[0], v[1], network_dict, name_suffix):
184                 break
185
186         print('---------------------------------------------------------------')
187         print('Network related replacement dictionary:')
188         print(json.dumps(network_dict, indent=4, sort_keys=True))
189         print('---------------------------------------------------------------')
190
191         keys = ['infra', 'bng', 'gmux', 'brg']
192         for key in keys:
193             csar_file = self.vcpecommon.find_file(key, 'csar', 'csar')
194             template_file = self.vcpecommon.find_file(key, 'json', 'preload_templates')
195             if csar_file and template_file:
196                 parser = CsarParser()
197                 parser.parse_csar(csar_file)
198                 service_instance_id = 'test112233'
199                 preloader.preload_vfmodule(template_file, service_instance_id, parser.vnf_models[0],
200                                            parser.vfmodule_models[0], network_dict, name_suffix)
201
202     def test_sniro(self):
203         template_sniro_data = self.vcpecommon.find_file('sniro_data', 'json', 'preload_templates')
204         template_sniro_request = self.vcpecommon.find_file('sniro_request', 'json', 'preload_templates')
205
206         vcperescust_csar = self.vcpecommon.find_file('rescust', 'csar', 'csar')
207         parser = CsarParser()
208         parser.parse_csar(vcperescust_csar)
209         tunnelxconn_ar_name = None
210         brg_ar_name = None
211         vgw_name = None
212         for model in parser.vnf_models:
213             if 'tunnel' in model['modelCustomizationName']:
214                 tunnelxconn_ar_name = model['modelCustomizationName']
215             elif 'brg' in model['modelCustomizationName']:
216                 brg_ar_name = model['modelCustomizationName']
217             elif 'vgw' in model['modelCustomizationName']:
218                 vgw_name = model['modelCustomizationName']
219
220         if not (tunnelxconn_ar_name and brg_ar_name and vgw_name):
221             self.logger.error('Cannot find all names from %s.', vcperescust_csar)
222             sys.exit()
223
224         vgmux_svc_instance_uuid = '88888888888888'
225         vbrg_svc_instance_uuid = '999999999999999'
226
227         self.preload_sniro(template_sniro_data, template_sniro_request, tunnelxconn_ar_name, vgw_name, brg_ar_name,
228                            vgmux_svc_instance_uuid, vbrg_svc_instance_uuid)