Bartek Grzybowski | fbbdbec | 2019-09-25 16:37:05 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python |
Kang Xi | 11d278c | 2018-04-06 16:56:04 -0400 | [diff] [blame] | 2 | |
| 3 | import sys |
| 4 | import logging |
| 5 | import requests |
| 6 | import json |
| 7 | from datetime import datetime |
| 8 | import progressbar |
| 9 | import time |
| 10 | import csar_parser |
| 11 | import preload |
| 12 | from vcpecommon import * |
| 13 | |
| 14 | |
| 15 | class SoUtils: |
| 16 | def __init__(self, vcpecommon, api_version): |
| 17 | """ |
| 18 | :param vcpecommon: |
| 19 | :param api_version: must be 'v4' or 'v5' |
| 20 | """ |
Kang Xi | 268b74d | 2018-05-23 15:53:42 -0400 | [diff] [blame] | 21 | self.tmp_solution_for_so_bug = False |
Kang Xi | 11d278c | 2018-04-06 16:56:04 -0400 | [diff] [blame] | 22 | self.logger = logging.getLogger(__name__) |
| 23 | self.vcpecommon = vcpecommon |
| 24 | if api_version not in self.vcpecommon.so_req_api_url: |
| 25 | self.logger.error('Incorrect SO API version: %s', api_version) |
Bartek Grzybowski | 68d93c2 | 2019-10-30 10:55:55 +0100 | [diff] [blame] | 26 | sys.exit(1) |
Kang Xi | 11d278c | 2018-04-06 16:56:04 -0400 | [diff] [blame] | 27 | self.service_req_api_url = self.vcpecommon.so_req_api_url[api_version] |
| 28 | |
| 29 | def submit_create_req(self, req_json, req_type, service_instance_id=None, vnf_instance_id=None): |
| 30 | """ |
| 31 | POST {serverRoot}/serviceInstances/v4 |
| 32 | POST {serverRoot}/serviceInstances/v4/{serviceInstanceId}/vnfs |
| 33 | POST {serverRoot}/serviceInstances/v4/{serviceInstanceId}/networks |
| 34 | POST {serverRoot}/serviceInstances/v4/{serviceInstanceId}/vnfs/{vnfInstanceId}/vfModules |
| 35 | :param req_json: |
| 36 | :param service_instance_id: this is required only for networks, vnfs, and vf modules |
| 37 | :param req_type: |
| 38 | :param vnf_instance_id: |
| 39 | :return: req_id, instance_id |
| 40 | """ |
| 41 | if req_type == 'service': |
| 42 | url = self.service_req_api_url |
| 43 | elif req_type == 'vnf': |
| 44 | url = '/'.join([self.service_req_api_url, service_instance_id, 'vnfs']) |
| 45 | elif req_type == 'network': |
| 46 | url = '/'.join([self.service_req_api_url, service_instance_id, 'networks']) |
| 47 | elif req_type == 'vfmodule': |
| 48 | url = '/'.join([self.service_req_api_url, service_instance_id, 'vnfs', vnf_instance_id, 'vfModules']) |
| 49 | else: |
| 50 | self.logger.error('Invalid request type: {0}. Can only be service/vnf/network/vfmodule'.format(req_type)) |
| 51 | return None, None |
| 52 | |
Kang Xi | 268b74d | 2018-05-23 15:53:42 -0400 | [diff] [blame] | 53 | self.logger.info(url) |
Kang Xi | 11d278c | 2018-04-06 16:56:04 -0400 | [diff] [blame] | 54 | r = requests.post(url, headers=self.vcpecommon.so_headers, auth=self.vcpecommon.so_userpass, json=req_json) |
Kang Xi | 268b74d | 2018-05-23 15:53:42 -0400 | [diff] [blame] | 55 | self.logger.debug(r) |
Kang Xi | 11d278c | 2018-04-06 16:56:04 -0400 | [diff] [blame] | 56 | response = r.json() |
| 57 | |
| 58 | self.logger.debug('---------------------------------------------------------------') |
| 59 | self.logger.debug('------- Creation request submitted to SO, got response --------') |
| 60 | self.logger.debug(json.dumps(response, indent=4, sort_keys=True)) |
| 61 | self.logger.debug('---------------------------------------------------------------') |
| 62 | req_id = response.get('requestReferences', {}).get('requestId', '') |
| 63 | instance_id = response.get('requestReferences', {}).get('instanceId', '') |
Kang Xi | 268b74d | 2018-05-23 15:53:42 -0400 | [diff] [blame] | 64 | |
Kang Xi | 11d278c | 2018-04-06 16:56:04 -0400 | [diff] [blame] | 65 | return req_id, instance_id |
| 66 | |
| 67 | def check_progress(self, req_id, eta=0, interval=5): |
| 68 | if not req_id: |
| 69 | self.logger.error('Error when checking SO request progress, invalid request ID: ' + req_id) |
| 70 | return False |
| 71 | duration = 0.0 |
| 72 | bar = progressbar.ProgressBar(redirect_stdout=True) |
| 73 | url = self.vcpecommon.so_check_progress_api_url + '/' + req_id |
| 74 | |
| 75 | while True: |
| 76 | time.sleep(interval) |
| 77 | r = requests.get(url, headers=self.vcpecommon.so_headers, auth=self.vcpecommon.so_userpass) |
| 78 | response = r.json() |
| 79 | |
| 80 | duration += interval |
| 81 | if eta > 0: |
| 82 | percentage = min(95, 100 * duration / eta) |
| 83 | else: |
| 84 | percentage = int(response['request']['requestStatus']['percentProgress']) |
| 85 | |
| 86 | if response['request']['requestStatus']['requestState'] == 'IN_PROGRESS': |
| 87 | self.logger.debug('------------------Request Status-------------------------------') |
| 88 | self.logger.debug(json.dumps(response, indent=4, sort_keys=True)) |
| 89 | bar.update(percentage) |
| 90 | else: |
| 91 | self.logger.debug('---------------------------------------------------------------') |
| 92 | self.logger.debug('----------------- Creation Request Results --------------------') |
| 93 | self.logger.debug(json.dumps(response, indent=4, sort_keys=True)) |
| 94 | self.logger.debug('---------------------------------------------------------------') |
| 95 | flag = response['request']['requestStatus']['requestState'] == 'COMPLETE' |
| 96 | if not flag: |
| 97 | self.logger.error('Request failed.') |
| 98 | self.logger.error(json.dumps(response, indent=4, sort_keys=True)) |
| 99 | bar.update(100) |
| 100 | bar.finish() |
| 101 | return flag |
| 102 | |
| 103 | def add_req_info(self, req_details, instance_name, product_family_id=None): |
| 104 | req_details['requestInfo'] = { |
| 105 | 'instanceName': instance_name, |
| 106 | 'source': 'VID', |
| 107 | 'suppressRollback': 'true', |
| 108 | 'requestorId': 'vCPE-Robot' |
| 109 | } |
| 110 | if product_family_id: |
| 111 | req_details['requestInfo']['productFamilyId'] = product_family_id |
| 112 | |
| 113 | def add_related_instance(self, req_details, instance_id, instance_model): |
| 114 | instance = {"instanceId": instance_id, "modelInfo": instance_model} |
| 115 | if 'relatedInstanceList' not in req_details: |
| 116 | req_details['relatedInstanceList'] = [{"relatedInstance": instance}] |
| 117 | else: |
| 118 | req_details['relatedInstanceList'].append({"relatedInstance": instance}) |
| 119 | |
| 120 | def generate_vnf_or_network_request(self, req_type, instance_name, vnf_or_network_model, service_instance_id, |
| 121 | service_model): |
| 122 | req_details = { |
| 123 | 'modelInfo': vnf_or_network_model, |
| 124 | 'cloudConfiguration': {"lcpCloudRegionId": self.vcpecommon.os_region_name, |
| 125 | "tenantId": self.vcpecommon.os_tenant_id}, |
Kang Xi | 268b74d | 2018-05-23 15:53:42 -0400 | [diff] [blame] | 126 | 'requestParameters': {"userParams": []}, |
| 127 | 'platform': {"platformName": "Platform-Demonstration"} |
Kang Xi | 11d278c | 2018-04-06 16:56:04 -0400 | [diff] [blame] | 128 | } |
| 129 | self.add_req_info(req_details, instance_name, self.vcpecommon.product_family_id) |
| 130 | self.add_related_instance(req_details, service_instance_id, service_model) |
| 131 | return {'requestDetails': req_details} |
| 132 | |
| 133 | def generate_vfmodule_request(self, instance_name, vfmodule_model, service_instance_id, |
| 134 | service_model, vnf_instance_id, vnf_model): |
| 135 | req_details = { |
| 136 | 'modelInfo': vfmodule_model, |
| 137 | 'cloudConfiguration': {"lcpCloudRegionId": self.vcpecommon.os_region_name, |
| 138 | "tenantId": self.vcpecommon.os_tenant_id}, |
| 139 | 'requestParameters': {"usePreload": 'true'} |
| 140 | } |
| 141 | self.add_req_info(req_details, instance_name, self.vcpecommon.product_family_id) |
| 142 | self.add_related_instance(req_details, service_instance_id, service_model) |
| 143 | self.add_related_instance(req_details, vnf_instance_id, vnf_model) |
| 144 | return {'requestDetails': req_details} |
| 145 | |
| 146 | def generate_service_request(self, instance_name, model): |
| 147 | req_details = { |
| 148 | 'modelInfo': model, |
| 149 | 'subscriberInfo': {'globalSubscriberId': self.vcpecommon.global_subscriber_id}, |
| 150 | 'requestParameters': { |
| 151 | "userParams": [], |
| 152 | "subscriptionServiceType": "vCPE", |
| 153 | "aLaCarte": 'true' |
| 154 | } |
| 155 | } |
| 156 | self.add_req_info(req_details, instance_name) |
Kang Xi | 268b74d | 2018-05-23 15:53:42 -0400 | [diff] [blame] | 157 | self.add_project_info(req_details) |
| 158 | self.add_owning_entity(req_details) |
Kang Xi | 11d278c | 2018-04-06 16:56:04 -0400 | [diff] [blame] | 159 | return {'requestDetails': req_details} |
| 160 | |
Kang Xi | 268b74d | 2018-05-23 15:53:42 -0400 | [diff] [blame] | 161 | def add_project_info(self, req_details): |
| 162 | req_details['project'] = {'projectName': self.vcpecommon.project_name} |
| 163 | |
| 164 | def add_owning_entity(self, req_details): |
| 165 | req_details['owningEntity'] = {'owningEntityId': self.vcpecommon.owning_entity_id, |
| 166 | 'owningEntityName': self.vcpecommon.owning_entity_name} |
| 167 | |
Kang Xi | 11d278c | 2018-04-06 16:56:04 -0400 | [diff] [blame] | 168 | def generate_custom_service_request(self, instance_name, model, brg_mac): |
Yang Xu | 9f935b2 | 2018-11-22 10:56:52 -0500 | [diff] [blame] | 169 | brg_mac_enc = brg_mac.replace(':', '-') |
Kang Xi | 11d278c | 2018-04-06 16:56:04 -0400 | [diff] [blame] | 170 | req_details = { |
| 171 | 'modelInfo': model, |
| 172 | 'subscriberInfo': {'subscriberName': 'Kaneohe', |
| 173 | 'globalSubscriberId': self.vcpecommon.global_subscriber_id}, |
Brian Freeman | 8076a87 | 2018-11-13 11:34:48 -0500 | [diff] [blame] | 174 | 'cloudConfiguration': {"lcpCloudRegionId": 'RegionOne', #self.vcpecommon.os_region_name, |
Kang Xi | 11d278c | 2018-04-06 16:56:04 -0400 | [diff] [blame] | 175 | "tenantId": self.vcpecommon.os_tenant_id}, |
| 176 | 'requestParameters': { |
| 177 | "userParams": [ |
| 178 | { |
| 179 | 'name': 'BRG_WAN_MAC_Address', |
| 180 | 'value': brg_mac |
Yang Xu | 63a0afd | 2018-11-20 16:01:01 -0500 | [diff] [blame] | 181 | }, |
| 182 | { |
| 183 | 'name': 'VfModuleNames', |
| 184 | 'value': [ |
| 185 | { |
Yang Xu | 9f935b2 | 2018-11-22 10:56:52 -0500 | [diff] [blame] | 186 | 'VfModuleModelInvariantUuid': self.vcpecommon.vgw_VfModuleModelInvariantUuid, |
| 187 | 'VfModuleName': 'VGW2BRG-{0}'.format(brg_mac_enc) |
Yang Xu | 63a0afd | 2018-11-20 16:01:01 -0500 | [diff] [blame] | 188 | } |
| 189 | ] |
| 190 | }, |
Kang Xi | 268b74d | 2018-05-23 15:53:42 -0400 | [diff] [blame] | 191 | { |
| 192 | "name": "Customer_Location", |
| 193 | "value": self.vcpecommon.customer_location_used_by_oof |
| 194 | }, |
| 195 | { |
| 196 | "name": "Homing_Solution", |
| 197 | "value": self.vcpecommon.homing_solution |
| 198 | } |
Kang Xi | 11d278c | 2018-04-06 16:56:04 -0400 | [diff] [blame] | 199 | ], |
| 200 | "subscriptionServiceType": "vCPE", |
| 201 | 'aLaCarte': 'false' |
| 202 | } |
| 203 | } |
| 204 | self.add_req_info(req_details, instance_name, self.vcpecommon.custom_product_family_id) |
Kang Xi | 268b74d | 2018-05-23 15:53:42 -0400 | [diff] [blame] | 205 | self.add_project_info(req_details) |
| 206 | self.add_owning_entity(req_details) |
Kang Xi | 11d278c | 2018-04-06 16:56:04 -0400 | [diff] [blame] | 207 | return {'requestDetails': req_details} |
| 208 | |
| 209 | def create_custom_service(self, csar_file, brg_mac, name_suffix=None): |
| 210 | parser = csar_parser.CsarParser() |
| 211 | if not parser.parse_csar(csar_file): |
| 212 | return False |
| 213 | |
| 214 | # yyyymmdd_hhmm |
| 215 | if not name_suffix: |
| 216 | name_suffix = '_' + datetime.now().strftime('%Y%m%d%H%M') |
| 217 | |
| 218 | # create service |
| 219 | instance_name = '_'.join([self.vcpecommon.instance_name_prefix['service'], |
Yang Xu | 9eae154 | 2018-11-27 13:00:05 -0500 | [diff] [blame] | 220 | parser.svc_model['modelName'][0:10], name_suffix]) |
Kang Xi | 11d278c | 2018-04-06 16:56:04 -0400 | [diff] [blame] | 221 | instance_name = instance_name.lower() |
| 222 | req = self.generate_custom_service_request(instance_name, parser.svc_model, brg_mac) |
Kang Xi | 268b74d | 2018-05-23 15:53:42 -0400 | [diff] [blame] | 223 | self.logger.info(json.dumps(req, indent=2, sort_keys=True)) |
Kang Xi | 11d278c | 2018-04-06 16:56:04 -0400 | [diff] [blame] | 224 | self.logger.info('Creating custom service {0}.'.format(instance_name)) |
| 225 | req_id, svc_instance_id = self.submit_create_req(req, 'service') |
| 226 | if not self.check_progress(req_id, 140): |
| 227 | return False |
| 228 | return True |
| 229 | |
| 230 | def wait_for_aai(self, node_type, uuid): |
| 231 | self.logger.info('Waiting for AAI traversal to complete...') |
| 232 | bar = progressbar.ProgressBar() |
| 233 | for i in range(30): |
| 234 | time.sleep(1) |
| 235 | bar.update(i*100.0/30) |
| 236 | if self.vcpecommon.is_node_in_aai(node_type, uuid): |
| 237 | bar.update(100) |
| 238 | bar.finish() |
| 239 | return |
| 240 | |
| 241 | self.logger.error("AAI traversal didn't finish in 30 seconds. Something is wrong. Type {0}, UUID {1}".format( |
| 242 | node_type, uuid)) |
Bartek Grzybowski | 68d93c2 | 2019-10-30 10:55:55 +0100 | [diff] [blame] | 243 | sys.exit(1) |
Kang Xi | 11d278c | 2018-04-06 16:56:04 -0400 | [diff] [blame] | 244 | |
| 245 | def create_entire_service(self, csar_file, vnf_template_file, preload_dict, name_suffix, heatbridge=False): |
| 246 | """ |
| 247 | :param csar_file: |
| 248 | :param vnf_template_file: |
| 249 | :param preload_dict: |
| 250 | :param name_suffix: |
| 251 | :return: service instance UUID |
| 252 | """ |
| 253 | self.logger.info('\n----------------------------------------------------------------------------------') |
| 254 | self.logger.info('Start to create entire service defined in csar: {0}'.format(csar_file)) |
| 255 | parser = csar_parser.CsarParser() |
| 256 | self.logger.info('Parsing csar ...') |
| 257 | if not parser.parse_csar(csar_file): |
| 258 | self.logger.error('Cannot parse csar: {0}'.format(csar_file)) |
| 259 | return None |
| 260 | |
| 261 | # create service |
| 262 | instance_name = '_'.join([self.vcpecommon.instance_name_prefix['service'], |
| 263 | parser.svc_model['modelName'], name_suffix]) |
| 264 | instance_name = instance_name.lower() |
| 265 | self.logger.info('Creating service instance: {0}.'.format(instance_name)) |
| 266 | req = self.generate_service_request(instance_name, parser.svc_model) |
| 267 | self.logger.debug(json.dumps(req, indent=2, sort_keys=True)) |
| 268 | req_id, svc_instance_id = self.submit_create_req(req, 'service') |
| 269 | if not self.check_progress(req_id, eta=2, interval=1): |
| 270 | return None |
| 271 | |
| 272 | # wait for AAI to complete traversal |
| 273 | self.wait_for_aai('service', svc_instance_id) |
| 274 | |
| 275 | # create networks |
| 276 | for model in parser.net_models: |
| 277 | base_name = model['modelCustomizationName'].lower().replace('mux_vg', 'mux_gw') |
| 278 | network_name = '_'.join([self.vcpecommon.instance_name_prefix['network'], base_name, name_suffix]) |
| 279 | network_name = network_name.lower() |
| 280 | self.logger.info('Creating network: ' + network_name) |
| 281 | req = self.generate_vnf_or_network_request('network', network_name, model, svc_instance_id, |
| 282 | parser.svc_model) |
| 283 | self.logger.debug(json.dumps(req, indent=2, sort_keys=True)) |
| 284 | req_id, net_instance_id = self.submit_create_req(req, 'network', svc_instance_id) |
| 285 | if not self.check_progress(req_id, eta=20): |
| 286 | return None |
| 287 | |
| 288 | self.logger.info('Changing subnet name to ' + self.vcpecommon.network_name_to_subnet_name(network_name)) |
| 289 | self.vcpecommon.set_network_name(network_name) |
| 290 | subnet_name_changed = False |
| 291 | for i in range(20): |
| 292 | time.sleep(3) |
| 293 | if self.vcpecommon.set_subnet_name(network_name): |
| 294 | subnet_name_changed = True |
| 295 | break |
| 296 | |
| 297 | if not subnet_name_changed: |
| 298 | self.logger.error('Failed to change subnet name for ' + network_name) |
| 299 | return None |
| 300 | |
| 301 | |
| 302 | vnf_model = None |
| 303 | vnf_instance_id = None |
| 304 | # create VNF |
| 305 | if len(parser.vnf_models) == 1: |
| 306 | vnf_model = parser.vnf_models[0] |
| 307 | vnf_instance_name = '_'.join([self.vcpecommon.instance_name_prefix['vnf'], |
| 308 | vnf_model['modelCustomizationName'].split(' ')[0], name_suffix]) |
| 309 | vnf_instance_name = vnf_instance_name.lower() |
| 310 | self.logger.info('Creating VNF: ' + vnf_instance_name) |
| 311 | req = self.generate_vnf_or_network_request('vnf', vnf_instance_name, vnf_model, svc_instance_id, |
| 312 | parser.svc_model) |
| 313 | self.logger.debug(json.dumps(req, indent=2, sort_keys=True)) |
| 314 | req_id, vnf_instance_id = self.submit_create_req(req, 'vnf', svc_instance_id) |
| 315 | if not self.check_progress(req_id, eta=2, interval=1): |
| 316 | self.logger.error('Failed to create VNF {0}.'.format(vnf_instance_name)) |
| 317 | return False |
| 318 | |
| 319 | # wait for AAI to complete traversal |
| 320 | if not vnf_instance_id: |
| 321 | self.logger.error('No VNF instance ID returned!') |
Bartek Grzybowski | 68d93c2 | 2019-10-30 10:55:55 +0100 | [diff] [blame] | 322 | sys.exit(1) |
Kang Xi | 11d278c | 2018-04-06 16:56:04 -0400 | [diff] [blame] | 323 | self.wait_for_aai('vnf', vnf_instance_id) |
| 324 | |
| 325 | preloader = preload.Preload(self.vcpecommon) |
| 326 | preloader.preload_vfmodule(vnf_template_file, svc_instance_id, parser.vnf_models[0], parser.vfmodule_models[0], |
| 327 | preload_dict, name_suffix) |
| 328 | # create VF Module |
| 329 | if len(parser.vfmodule_models) == 1: |
| 330 | if not vnf_instance_id or not vnf_model: |
| 331 | self.logger.error('Invalid VNF instance ID or VNF model!') |
Bartek Grzybowski | 68d93c2 | 2019-10-30 10:55:55 +0100 | [diff] [blame] | 332 | sys.exit(1) |
Kang Xi | 11d278c | 2018-04-06 16:56:04 -0400 | [diff] [blame] | 333 | |
| 334 | model = parser.vfmodule_models[0] |
| 335 | vfmodule_instance_name = '_'.join([self.vcpecommon.instance_name_prefix['vfmodule'], |
| 336 | model['modelCustomizationName'].split('..')[0], name_suffix]) |
| 337 | vfmodule_instance_name = vfmodule_instance_name.lower() |
| 338 | self.logger.info('Creating VF Module: ' + vfmodule_instance_name) |
| 339 | req = self.generate_vfmodule_request(vfmodule_instance_name, model, svc_instance_id, parser.svc_model, |
| 340 | vnf_instance_id, vnf_model) |
| 341 | self.logger.debug(json.dumps(req, indent=2, sort_keys=True)) |
| 342 | req_id, vfmodule_instance_id = self.submit_create_req(req, 'vfmodule', svc_instance_id, vnf_instance_id) |
Yang Xu | aa8c584 | 2019-01-02 12:05:40 -0500 | [diff] [blame] | 343 | if not self.check_progress(req_id, eta=70, interval=50): |
Kang Xi | 11d278c | 2018-04-06 16:56:04 -0400 | [diff] [blame] | 344 | self.logger.error('Failed to create VF Module {0}.'.format(vfmodule_instance_name)) |
| 345 | return None |
| 346 | |
| 347 | # run heatbridge |
| 348 | if heatbridge: |
Bartek Grzybowski | bfeeb24 | 2019-09-13 08:35:48 +0200 | [diff] [blame] | 349 | self.vcpecommon.heatbridge(vfmodule_instance_name, svc_instance_id) |
Kang Xi | 11d278c | 2018-04-06 16:56:04 -0400 | [diff] [blame] | 350 | self.vcpecommon.save_vgmux_vnf_name(vnf_instance_name) |
| 351 | |
| 352 | return svc_instance_id |