6 from datetime import datetime
7 from vcpecommon import *
14 def __init__(self, vcpecommon):
15 self.logger = logging.getLogger(__name__)
16 self.logger.setLevel(logging.DEBUG)
17 self.vcpecommon = vcpecommon
19 def replace(self, sz, replace_dict):
20 for old_string, new_string in replace_dict.items():
21 sz = sz.replace(old_string, new_string)
22 if self.vcpecommon.template_variable_symbol in sz:
23 self.logger.error('Error! Cannot find a value to replace ' + sz)
26 def generate_json(self, template_file, replace_dict):
27 with open(template_file) as json_input:
28 json_data = json.load(json_input)
32 for k, v in data.items():
37 elif type(v) is str or type(v) is unicode:
38 if self.vcpecommon.template_variable_symbol in v:
39 data[k] = self.replace(v, replace_dict)
41 self.logger.warning('Unexpected line in template: %s. Look for value %s', template_file, v)
44 def reset_sniro(self):
45 self.logger.debug('Clearing SNIRO data')
46 r = requests.post(self.vcpecommon.sniro_url + '/reset', headers=self.vcpecommon.sniro_headers)
47 if 2 != r.status_code / 100:
48 self.logger.debug(r.content)
49 self.logger.error('Clearing SNIRO date failed.')
52 def preload_sniro(self, template_sniro_data, template_sniro_request, tunnelxconn_ar_name, vgw_name, vbrg_ar_name,
53 vgmux_svc_instance_uuid, vbrg_svc_instance_uuid):
55 self.logger.info('Preloading SNIRO for homing service')
56 replace_dict = {'${tunnelxconn_ar_name}': tunnelxconn_ar_name,
57 '${vgw_name}': vgw_name,
58 '${brg_ar_name}': vbrg_ar_name,
59 '${vgmux_svc_instance_uuid}': vgmux_svc_instance_uuid,
60 '${vbrg_svc_instance_uuid}': vbrg_svc_instance_uuid
62 sniro_data = self.generate_json(template_sniro_data, replace_dict)
63 self.logger.debug('SNIRO data:')
64 self.logger.debug(json.dumps(sniro_data, indent=4, sort_keys=True))
66 base64_sniro_data = base64.b64encode(json.dumps(sniro_data))
67 self.logger.debug('SNIRO data: 64')
68 self.logger.debug(base64_sniro_data)
69 replace_dict = {'${base64_sniro_data}': base64_sniro_data, '${sniro_ip}': self.vcpecommon.hosts['robot']}
70 sniro_request = self.generate_json(template_sniro_request, replace_dict)
71 self.logger.debug('SNIRO request:')
72 self.logger.debug(json.dumps(sniro_request, indent=4, sort_keys=True))
74 r = requests.post(self.vcpecommon.sniro_url, headers=self.vcpecommon.sniro_headers, json=sniro_request)
75 if 2 != r.status_code / 100:
77 self.logger.debug(json.dumps(response, indent=4, sort_keys=True))
78 self.logger.error('SNIRO preloading failed.')
83 def preload_network(self, template_file, network_role, subnet_start_ip, subnet_gateway, common_dict, name_suffix):
86 :param network_role: cpe_signal, cpe_public, brg_bng, bng_mux, mux_gw
87 :param subnet_start_ip:
88 :param subnet_gateway:
89 :param name_suffix: e.g. '201711201311'
92 network_name = '_'.join([self.vcpecommon.instance_name_prefix['network'], network_role.lower(), name_suffix])
93 subnet_name = self.vcpecommon.network_name_to_subnet_name(network_name)
94 common_dict['${' + network_role+'_net}'] = network_name
95 common_dict['${' + network_role+'_subnet}'] = subnet_name
96 replace_dict = {'${network_role}': network_role,
97 '${service_type}': 'vCPE',
98 '${network_type}': 'Generic NeutronNet',
99 '${network_name}': network_name,
100 '${subnet_start_ip}': subnet_start_ip,
101 '${subnet_gateway}': subnet_gateway
103 self.logger.info('Preloading network ' + network_role)
104 return self.preload(template_file, replace_dict, self.vcpecommon.sdnc_preload_network_url)
106 def preload(self, template_file, replace_dict, url):
107 json_data = self.generate_json(template_file, replace_dict)
108 self.logger.debug(json.dumps(json_data, indent=4, sort_keys=True))
109 r = requests.post(url, headers=self.vcpecommon.sdnc_headers, auth=self.vcpecommon.sdnc_userpass, json=json_data)
111 if int(response.get('output', {}).get('response-code', 0)) != 200:
112 self.logger.debug(json.dumps(response, indent=4, sort_keys=True))
113 self.logger.error('Preloading failed.')
117 def preload_vgw(self, template_file, brg_mac, commont_dict, name_suffix):
118 replace_dict = {'${brg_mac}': brg_mac,
119 '${suffix}': name_suffix
121 replace_dict.update(commont_dict)
122 self.logger.info('Preloading vGW')
123 return self.preload(template_file, replace_dict, self.vcpecommon.sdnc_preload_vnf_url)
125 def preload_vgw_gra(self, template_file, brg_mac, commont_dict, name_suffix, vgw_vfmod_name_index):
126 replace_dict = {'${brg_mac}': brg_mac,
127 '${suffix}': name_suffix,
128 '${vgw_vfmod_name_index}': vgw_vfmod_name_index
130 replace_dict.update(commont_dict)
131 self.logger.info('Preloading vGW-GRA')
132 return self.preload(template_file, replace_dict, self.vcpecommon.sdnc_preload_gra_url)
134 def preload_vfmodule(self, template_file, service_instance_id, vnf_model, vfmodule_model, common_dict, name_suffix):
136 :param template_file:
137 :param service_instance_id:
138 :param vnf_model: parsing results from csar_parser
139 :param vfmodule_model: parsing results from csar_parser
146 # vfmodule_model['modelCustomizationName']: "Vspinfra111601..base_vcpe_infra..module-0",
147 # vnf_model['modelCustomizationName']: "vspinfra111601 0",
149 vfmodule_name = '_'.join([self.vcpecommon.instance_name_prefix['vfmodule'],
150 vfmodule_model['modelCustomizationName'].split('..')[0].lower(), name_suffix])
152 # vnf_type and generic_vnf_type are identical
153 replace_dict = {'${vnf_type}': vfmodule_model['modelCustomizationName'],
154 '${generic_vnf_type}': vfmodule_model['modelCustomizationName'],
155 '${service_type}': service_instance_id,
156 '${generic_vnf_name}': vnf_model['modelCustomizationName'],
157 '${vnf_name}': vfmodule_name,
158 '${mr_ip_addr}': self.vcpecommon.mr_ip_addr,
159 '${mr_ip_port}': self.vcpecommon.mr_ip_port,
160 '${sdnc_oam_ip}': self.vcpecommon.sdnc_oam_ip,
161 '${suffix}': name_suffix}
162 replace_dict.update(common_dict)
163 self.logger.info('Preloading VF Module ' + vfmodule_name)
164 return self.preload(template_file, replace_dict, self.vcpecommon.sdnc_preload_vnf_url)
166 def preload_all_networks(self, template_file, name_suffix):
167 common_dict = {'${' + k + '}': v for k, v in self.vcpecommon.common_preload_config.items()}
168 for network, v in self.vcpecommon.preload_network_config.items():
169 subnet_start_ip, subnet_gateway_ip = v
170 if not self.preload_network(template_file, network, subnet_start_ip, subnet_gateway_ip,
171 common_dict, name_suffix):
176 # this is for testing purpose
177 name_suffix = datetime.now().strftime('%Y%m%d%H%M')
178 vcpecommon = VcpeCommon()
179 preloader = Preload(vcpecommon)
181 network_dict = {'${' + k + '}': v for k, v in self.vcpecommon.common_preload_config.items()}
182 template_file = 'preload_templates/template.network.json'
183 for k, v in self.vcpecommon.preload_network_config.items():
184 if not preloader.preload_network(template_file, k, v[0], v[1], network_dict, name_suffix):
187 print('---------------------------------------------------------------')
188 print('Network related replacement dictionary:')
189 print(json.dumps(network_dict, indent=4, sort_keys=True))
190 print('---------------------------------------------------------------')
192 keys = ['infra', 'bng', 'gmux', 'brg']
194 csar_file = self.vcpecommon.find_file(key, 'csar', 'csar')
195 template_file = self.vcpecommon.find_file(key, 'json', 'preload_templates')
196 if csar_file and template_file:
197 parser = csar_parser.CsarParser()
198 parser.parse_csar(csar_file)
199 service_instance_id = 'test112233'
200 preloader.preload_vfmodule(template_file, service_instance_id, parser.vnf_models[0],
201 parser.vfmodule_models[0], network_dict, name_suffix)
203 def test_sniro(self):
204 template_sniro_data = self.vcpecommon.find_file('sniro_data', 'json', 'preload_templates')
205 template_sniro_request = self.vcpecommon.find_file('sniro_request', 'json', 'preload_templates')
207 vcperescust_csar = self.vcpecommon.find_file('rescust', 'csar', 'csar')
208 parser = csar_parser.CsarParser()
209 parser.parse_csar(vcperescust_csar)
210 tunnelxconn_ar_name = None
213 for model in parser.vnf_models:
214 if 'tunnel' in model['modelCustomizationName']:
215 tunnelxconn_ar_name = model['modelCustomizationName']
216 elif 'brg' in model['modelCustomizationName']:
217 brg_ar_name = model['modelCustomizationName']
218 elif 'vgw' in model['modelCustomizationName']:
219 vgw_name = model['modelCustomizationName']
221 if not (tunnelxconn_ar_name and brg_ar_name and vgw_name):
222 self.logger.error('Cannot find all names from %s.', vcperescust_csar)
225 vgmux_svc_instance_uuid = '88888888888888'
226 vbrg_svc_instance_uuid = '999999999999999'
228 self.preload_sniro(template_sniro_data, template_sniro_request, tunnelxconn_ar_name, vgw_name, brg_ar_name,
229 vgmux_svc_instance_uuid, vbrg_svc_instance_uuid)