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