def check_callbackuri_connection(self): logger.debug("Create Subscription --> Test Callback URI --" "Sending GET request to %s" % self.callback_uri) try: response = requests.get(self.callback_uri, timeout=2) if response.status_code != status.HTTP_204_NO_CONTENT: raise GenericparserException("callbackUri %s returns %s status " "code." % (self.callback_uri, response.status_code)) except Exception: raise GenericparserException("callbackUri %s didn't return 204 status" "code." % self.callback_uri)
def on_distribute(self): JobUtil.create_job( inst_type='nf', jobaction='on_distribute', inst_id=self.csar_id, job_id=self.job_id) JobUtil.add_job_status(self.job_id, 5, "Start CSAR(%s) distribute." % self.csar_id) if VnfPackageModel.objects.filter(vnfPackageId=self.csar_id): err_msg = "NF CSAR(%s) already exists." % self.csar_id JobUtil.add_job_status(self.job_id, JOB_ERROR, err_msg) return artifact = sdc.get_artifact(sdc.ASSETTYPE_RESOURCES, self.csar_id) local_path = os.path.join(GENERICPARSER_ROOT_PATH, self.csar_id) csar_name = "%s.csar" % artifact.get("name", self.csar_id) local_file_name = sdc.download_artifacts(artifact["toscaModelURL"], local_path, csar_name) if local_file_name.endswith(".csar") or local_file_name.endswith(".zip"): artifact_vnf_file = fileutil.unzip_file(local_file_name, local_path, "Artifacts/Deployment/OTHER/vnf.csar") if os.path.exists(artifact_vnf_file): local_file_name = artifact_vnf_file vnfd_json = toscaparsers.parse_vnfd(local_file_name) vnfd = json.JSONDecoder().decode(vnfd_json) if not vnfd.get("vnf"): raise GenericparserException("VNF properties and metadata in VNF Package(id=%s) are empty." % self.csar_id) vnfd_id = vnfd["vnf"]["properties"].get("descriptor_id", "") if VnfPackageModel.objects.filter(vnfdId=vnfd_id): logger.error("VNF package(%s) already exists.", vnfd_id) raise GenericparserException("VNF package(%s) already exists." % vnfd_id) JobUtil.add_job_status(self.job_id, 30, "Save CSAR(%s) to database." % self.csar_id) vnfd_ver = vnfd["vnf"]["properties"].get("descriptor_version", "") vnf_provider = vnfd["vnf"]["properties"].get("provider", "") vnf_software_version = vnfd["vnf"]["properties"].get("software_version", "") vnfd_product_name = vnfd["vnf"]["properties"].get("product_name", "") VnfPackageModel( vnfPackageId=self.csar_id, vnfdId=vnfd_id, vnfVendor=vnf_provider, vnfdProductName=vnfd_product_name, vnfdVersion=vnfd_ver, vnfSoftwareVersion=vnf_software_version, vnfdModel=vnfd_json, localFilePath=local_file_name, vnfPackageUri=csar_name, onboardingState=PKG_STATUS.ONBOARDED, operationalState=PKG_STATUS.ENABLED, usageState=PKG_STATUS.NOT_IN_USE ).save() JobUtil.add_job_status(self.job_id, 100, "CSAR(%s) distribute successfully." % self.csar_id)
def get_artifact(asset_type, csar_id): artifacts = get_artifacts(asset_type) for artifact in artifacts: if artifact["uuid"] == csar_id: if asset_type == ASSETTYPE_SERVICES and \ artifact.get("distributionStatus", None) != DISTRIBUTED: raise GenericparserException( "The artifact (%s,%s) is not distributed from sdc." % (asset_type, csar_id)) else: return artifact raise GenericparserException("Failed to query artifact(%s,%s) from sdc." % (asset_type, csar_id))
def get_asset(asset_type, uuid): resource = "/sdc/v1/catalog/{assetType}/{uuid}/metadata".format( assetType=asset_type, uuid=uuid) ret = call_sdc(resource, "GET") if ret[0] != 0: logger.error("Status code is %s, detail is %s.", ret[2], ret[1]) raise GenericparserException("Failed to get asset(%s, %s) from sdc." % (asset_type, uuid)) asset = json.JSONDecoder().decode(ret[1]) if asset.get("distributionStatus", None) != DISTRIBUTED: raise GenericparserException( "The asset (%s,%s) is not distributed from sdc." % (asset_type, uuid)) else: return asset
def delete_vnf_pkg(self, vnf_pkg_id): vnf_pkg = VnfPackageModel.objects.filter(vnfPackageId=vnf_pkg_id) if not vnf_pkg.exists(): logger.debug('VNF package(%s) has been deleted.' % vnf_pkg_id) return ''' if vnf_pkg[0].operationalState != PKG_STATUS.DISABLED: raise GenericparserException("The VNF package (%s) is not disabled" % vnf_pkg_id) if vnf_pkg[0].usageState != PKG_STATUS.NOT_IN_USE: raise GenericparserException("The VNF package (%s) is in use" % vnf_pkg_id) ''' del_vnfd_id = vnf_pkg[0].vnfdId ns_pkgs = NSPackageModel.objects.all() for ns_pkg in ns_pkgs: nsd_model = None if ns_pkg.nsdModel: nsd_model = json.JSONDecoder().decode(ns_pkg.nsdModel) if not nsd_model: continue for vnf in nsd_model['vnfs']: if del_vnfd_id == vnf["properties"]["descriptor_id"]: raise GenericparserException('VNFD(%s) is referenced.' % del_vnfd_id) vnf_pkg.delete() vnf_pkg_path = os.path.join(GENERICPARSER_ROOT_PATH, vnf_pkg_id) fileutil.delete_dirs(vnf_pkg_path) logger.info('VNF package(%s) has been deleted.' % vnf_pkg_id)
def delete_single(self, pnfd_info_id): logger.info('Start to delete PNFD(%s)...' % pnfd_info_id) pnf_pkgs = PnfPackageModel.objects.filter(pnfPackageId=pnfd_info_id) if not pnf_pkgs.exists(): logger.info('PNFD(%s) has been deleted.' % pnfd_info_id) return ''' if pnf_pkgs[0].usageState != PKG_STATUS.NOT_IN_USE: logger.info('PNFD(%s) shall be NOT_IN_USE.' % pnfd_info_id) raise GenericparserException('PNFD(%s) shall be NOT_IN_USE.' % pnfd_info_id) ''' del_pnfd_id = pnf_pkgs[0].pnfdId ns_pkgs = NSPackageModel.objects.all() for ns_pkg in ns_pkgs: nsd_model = None if ns_pkg.nsdModel: nsd_model = json.JSONDecoder().decode(ns_pkg.nsdModel) if not nsd_model: continue for pnf in nsd_model['pnfs']: if del_pnfd_id == pnf["properties"]["id"]: logger.warn("PNFD(%s) is referenced in NSD", del_pnfd_id) raise GenericparserException('PNFD(%s) is referenced.' % pnfd_info_id) pnf_pkgs.delete() pnf_pkg_path = os.path.join(GENERICPARSER_ROOT_PATH, pnfd_info_id) fileutil.delete_dirs(pnf_pkg_path) logger.debug('PNFD(%s) has been deleted.' % pnfd_info_id)
def get_csar(self, csar_id): pkg_info = {} nf_pkg = VnfPackageModel.objects.filter(vnfPackageId=csar_id) if not nf_pkg: nf_pkg = VnfPackageModel.objects.filter(vnfdId=csar_id) if nf_pkg: db_csar_id = nf_pkg[0].vnfPackageId pkg_info["vnfdId"] = nf_pkg[0].vnfdId pkg_info["vnfPackageId"] = nf_pkg[0].vnfPackageId pkg_info["vnfdProvider"] = nf_pkg[0].vnfVendor pkg_info["vnfdVersion"] = nf_pkg[0].vnfdVersion pkg_info["vnfVersion"] = nf_pkg[0].vnfSoftwareVersion pkg_info["csarName"] = nf_pkg[0].vnfPackageUri pkg_info["vnfdModel"] = nf_pkg[0].vnfdModel pkg_info["downloadUrl"] = "http://%s:%s/%s/%s/%s" % ( REG_TO_MSB_REG_PARAM[0]["nodes"][0]["ip"], REG_TO_MSB_REG_PARAM[0]["nodes"][0]["port"], GENERICPARSER_URL_PATH, db_csar_id, nf_pkg[0].vnfPackageUri) else: raise GenericparserException("Vnf package[%s] not Found." % csar_id) csar_info = { "csarId": db_csar_id, "packageInfo": pkg_info, "imageInfo": [] } return [0, csar_info]
def select_vnfm(vnfm_type, vim_id): uri = "/external-system/esr-vnfm-list?depth=all" ret = call_aai(uri, "GET") if ret[0] > 0: logger.error("Failed to call %s: %s", uri, ret[1]) raise GenericparserException('Failed to get vnfms from extsys.') vnfms = json.JSONDecoder().decode(ret[1]) vnfms = ignore_case_get(vnfms, "esr-vnfm") for vnfm in vnfms: esr_system_info = ignore_case_get(vnfm, "esr-system-info") type = ignore_case_get(esr_system_info, "type") vimId = vnfm["vnfm-id"] if type == vnfm_type and vimId == vim_id: # convert vnfm_info_aai to internal vnfm_info vnfm = convert_vnfm_info(vnfm) return vnfm raise GenericparserException('No vnfm found with %s in vim(%s)' % (vnfm_type, vim_id))
def get_artifacts(asset_type): resource = "/sdc/v1/catalog/{assetType}" resource = resource.format(assetType=asset_type) ret = call_sdc(resource, "GET") if ret[0] != 0: logger.error("Status code is %s, detail is %s.", ret[2], ret[1]) raise GenericparserException( "Failed to query artifacts(%s) from sdc." % asset_type) return json.JSONDecoder().decode(ret[1])
def upload(self, nsd_info_id, remote_file): logger.info('Start to upload NSD(%s)...' % nsd_info_id) ns_pkgs = NSPackageModel.objects.filter(nsPackageId=nsd_info_id) if not ns_pkgs.exists(): logger.error('NSD(%s) does not exist.' % nsd_info_id) raise GenericparserException('NSD(%s) does not exist.' % nsd_info_id) ns_pkgs.update(onboardingState=PKG_STATUS.UPLOADING) local_file_name = save(remote_file, nsd_info_id) logger.info('NSD(%s) content has been uploaded.' % nsd_info_id) return local_file_name
def test_service_pkg_distribute_when_fail_get_artifacts( self, mock_get_asset): mock_get_asset.side_effect = GenericparserException( "Failed to query artifact(services,1) from sdc.") csar_id = "1" try: ServicePackage().on_distribute(csar_id) except Exception as e: self.assertTrue(isinstance(e, GenericparserException)) self.assertEqual("Failed to query artifact(services,1) from sdc.", e.args[0])
def delete_artifact(asset_type, asset_id, artifact_id): resource = "/sdc/v1/catalog/{assetType}/{uuid}/artifacts/{artifactUUID}" resource = resource.format(assetType=asset_type, uuid=asset_id, artifactUUID=artifact_id) ret = call_sdc(resource, "DELETE") if ret[0] != 0: logger.error("Status code is %s, detail is %s.", ret[2], ret[1]) raise GenericparserException( "Failed to delete artifacts(%s) from sdc." % artifact_id) return json.JSONDecoder().decode(ret[1])
def get_vnfm_by_id(vnfm_inst_id): uri = "/external-system/esr-vnfm-list/esr-vnfm/%s?depth=all" % vnfm_inst_id ret = call_aai(uri, "GET") if ret[0] > 0: logger.error('Send get VNFM information request to extsys failed.') raise GenericparserException( 'Send get VNFM information request to extsys failed.') # convert vnfm_info_aai to internal vnfm_info vnfm_info_aai = json.JSONDecoder().decode(ret[1]) vnfm_info = convert_vnfm_info(vnfm_info_aai) return vnfm_info
def upload(self, remote_file, pnfd_info_id): logger.info('Start to upload PNFD(%s)...' % pnfd_info_id) pnf_pkgs = PnfPackageModel.objects.filter(pnfPackageId=pnfd_info_id) if not pnf_pkgs.exists(): logger.info('PNFD(%s) does not exist.' % pnfd_info_id) raise GenericparserException('PNFD (%s) does not exist.' % pnfd_info_id) pnf_pkgs.update(onboardingState=PKG_STATUS.UPLOADING) local_file_name = save(remote_file, pnfd_info_id) logger.info('PNFD(%s) content has been uploaded.' % pnfd_info_id) return local_file_name
def get_vims(): ret = call_aai("/cloud-infrastructure/cloud-regions?depth=all", "GET") if ret[0] != 0: logger.error("Status code is %s, detail is %s.", ret[2], ret[1]) raise GenericparserException("Failed to query vims from extsys.") # convert vim_info_aai to internal vim_info vims_aai = json.JSONDecoder().decode(ret[1]) vims_aai = ignore_case_get(vims_aai, "cloud-region") vims_info = [] for vim in vims_aai: vim = convert_vim_info(vim) vims_info.append(vim) return vims_info
def get_vim_by_id(vim_id): cloud_owner, cloud_region = split_vim_to_owner_region(vim_id) ret = call_aai( "/cloud-infrastructure/cloud-regions/cloud-region/%s/%s?depth=all" % (cloud_owner, cloud_region), "GET") if ret[0] != 0: logger.error("Status code is %s, detail is %s.", ret[2], ret[1]) raise GenericparserException("Failed to query vim(%s) from extsys." % vim_id) # convert vim_info_aai to internal vim_info vim_info_aai = json.JSONDecoder().decode(ret[1]) vim_info = convert_vim_info(vim_info_aai) return vim_info
def download(self, nsd_info_id, file_range): logger.info('Start to download NSD(%s)...' % nsd_info_id) ns_pkgs = NSPackageModel.objects.filter(nsPackageId=nsd_info_id) if not ns_pkgs.exists(): logger.error('NSD(%s) does not exist.' % nsd_info_id) raise ResourceNotFoundException('NSD(%s) does not exist.' % nsd_info_id) if ns_pkgs[0].onboardingState != PKG_STATUS.ONBOARDED: logger.error('NSD(%s) is not ONBOARDED.' % nsd_info_id) raise GenericparserException('NSD(%s) is not ONBOARDED.' % nsd_info_id) local_file_path = ns_pkgs[0].localFilePath start, end = parse_file_range(local_file_path, file_range) logger.info('NSD(%s) has been downloaded.' % nsd_info_id) return read(local_file_path, start, end)
def parse_vnfd_and_save(vnf_pkg_id, vnf_pkg_path): logger.info('Start to process VNF package(%s)...' % vnf_pkg_id) vnf_pkg = VnfPackageModel.objects.filter(vnfPackageId=vnf_pkg_id) vnf_pkg.update(onboardingState=PKG_STATUS.PROCESSING) vnfd_json = toscaparsers.parse_vnfd(vnf_pkg_path) vnfd = json.JSONDecoder().decode(vnfd_json) if vnfd.get("vnf", "") != "": vnfd_id = vnfd["vnf"]["properties"].get("descriptor_id", "") other_pkg = VnfPackageModel.objects.filter(vnfdId=vnfd_id) if other_pkg and other_pkg[0].vnfPackageId != vnf_pkg_id: logger.error("VNF package(%s,%s) already exists.", other_pkg[0].vnfPackageId, vnfd_id) raise GenericparserException("VNF package(%s) already exists." % vnfd_id) vnf_provider = vnfd["vnf"]["properties"].get("provider", "") vnfd_ver = vnfd["vnf"]["properties"].get("descriptor_version", "") vnf_software_version = vnfd["vnf"]["properties"].get( "software_version", "") vnfd_product_name = vnfd["vnf"]["properties"].get("product_name", "") vnf_pkg.update(vnfPackageId=vnf_pkg_id, vnfdId=vnfd_id, vnfdProductName=vnfd_product_name, vnfVendor=vnf_provider, vnfdVersion=vnfd_ver, vnfSoftwareVersion=vnf_software_version, vnfdModel=vnfd_json, onboardingState=PKG_STATUS.ONBOARDED, operationalState=PKG_STATUS.ENABLED, usageState=PKG_STATUS.NOT_IN_USE, localFilePath=vnf_pkg_path, vnfPackageUri=os.path.split(vnf_pkg_path)[-1]) else: raise GenericparserException( "VNF propeties and metadata in VNF Package(id=%s) are empty." % vnf_pkg_id) logger.info('VNF package(%s) has been processed(done).' % vnf_pkg_id)
def download(self, vnf_pkg_id, file_range): logger.info('Start to download VNF package(%s)...' % vnf_pkg_id) nf_pkg = VnfPackageModel.objects.filter(vnfPackageId=vnf_pkg_id) if not nf_pkg.exists(): logger.error('VNF package(%s) does not exist.' % vnf_pkg_id) raise ResourceNotFoundException('VNF package(%s) does not exist.' % vnf_pkg_id) if nf_pkg[0].onboardingState != PKG_STATUS.ONBOARDED: raise GenericparserException("VNF package (%s) is not on-boarded" % vnf_pkg_id) local_file_path = nf_pkg[0].localFilePath start, end = parse_file_range(local_file_path, file_range) logger.info('VNF package (%s) has been downloaded.' % vnf_pkg_id) return read(local_file_path, start, end)
def parse_pnfd(self, csar_id, inputs): try: pnf_pkg = PnfPackageModel.objects.filter(pnfPackageId=csar_id) if not pnf_pkg: raise GenericparserException("PNF CSAR(%s) does not exist." % csar_id) csar_path = pnf_pkg[0].localFilePath ret = {"model": toscaparsers.parse_pnfd(csar_path, inputs)} except GenericparserException as e: return [1, e.args[0]] except Exception as e: logger.error(e.args[0]) return [1, e.args[0]] return [0, ret]
def test_service_pkg_distribute_when_fail_download_artifacts( self, mock_get_asset, mock_download_artifacts): mock_get_asset.return_value = self.asset_data mock_download_artifacts.side_effect = GenericparserException( "Failed to download 1 from sdc.") csar_id = "1" VnfPackageModel(vnfPackageId="cd557883-ac4b-462d-aa01-421b5fa606b1", vnfdId="cd557883-ac4b-462d-aa01-421b5fa606b1").save() PnfPackageModel(pnfPackageId="m6000_s", pnfdId="m6000_s").save() try: ServicePackage().on_distribute(csar_id) except Exception as e: self.assertTrue(isinstance(e, GenericparserException)) self.assertEqual("Failed to download 1 from sdc.", e.args[0])
def parse_vnfd(csar_id, inputs): ret = None try: nf_pkg = VnfPackageModel.objects.filter(vnfPackageId=csar_id) if not nf_pkg: raise GenericparserException("VNF CSAR(%s) does not exist." % csar_id) csar_path = nf_pkg[0].localFilePath ret = {"model": toscaparsers.parse_vnfd(csar_path, inputs)} except GenericparserException as e: return [1, e.args[0]] except Exception as e: logger.error(e.args[0]) logger.error(traceback.format_exc()) return [1, str(sys.exc_info())] return [0, ret]
def get_sdn_controller_by_id(sdn_ontroller_id): ret = call_aai( "/external-system/esr-thirdparty-sdnc-list/esr-thirdparty-sdnc/%s?depth=all" % sdn_ontroller_id, "GET") if ret[0] != 0: logger.error( "Failed to query sdn ontroller(%s) from extsys. detail is %s.", sdn_ontroller_id, ret[1]) raise GenericparserException( "Failed to query sdn ontroller(%s) from extsys." % sdn_ontroller_id) # convert vim_info_aai to internal vim_info sdnc_info_aai = json.JSONDecoder().decode(ret[1]) sdnc_info = convert_sdnc_info(sdnc_info_aai) return sdnc_info
def download(self, pnfd_info_id): logger.info('Start to download PNFD(%s)...' % pnfd_info_id) pnf_pkgs = PnfPackageModel.objects.filter(pnfPackageId=pnfd_info_id) if not pnf_pkgs.exists(): logger.error('PNFD(%s) does not exist.' % pnfd_info_id) raise ResourceNotFoundException('PNFD(%s) does not exist.' % pnfd_info_id) if pnf_pkgs[0].onboardingState != PKG_STATUS.ONBOARDED: logger.error('PNFD(%s) is not ONBOARDED.' % pnfd_info_id) raise GenericparserException('PNFD(%s) is not ONBOARDED.' % pnfd_info_id) local_file_path = pnf_pkgs[0].localFilePath start, end = 0, os.path.getsize(local_file_path) logger.info('PNFD(%s) has been downloaded.' % pnfd_info_id) return read(local_file_path, start, end)
def get_csar(self, csar_id): package_info = {} csars = NSPackageModel.objects.filter(nsPackageId=csar_id) if csars: package_info["nsdId"] = csars[0].nsdId package_info["nsPackageId"] = csars[0].nsPackageId package_info["nsdProvider"] = csars[0].nsdDesginer package_info["nsdVersion"] = csars[0].nsdVersion package_info["csarName"] = csars[0].nsPackageUri package_info["nsdModel"] = csars[0].nsdModel package_info["nsdInvariantId"] = csars[0].invariantId package_info["downloadUrl"] = "http://%s:%s/%s/%s/%s" % ( REG_TO_MSB_REG_PARAM[0]["nodes"][0]["ip"], REG_TO_MSB_REG_PARAM[0]["nodes"][0]["port"], GENERICPARSER_URL_PATH, csar_id, csars[0].nsPackageUri) else: raise GenericparserException("Ns package[%s] not Found." % csar_id) return [0, {"csarId": csar_id, "packageInfo": package_info}]
def download_artifacts(download_url, local_path, file_name): additional_headers = { 'X-ECOMP-InstanceID': 'Modeling', 'accept': 'application/octet-stream' } ret = restcall.call_req(base_url=SDC_BASE_URL, user=SDC_USER, passwd=SDC_PASSWD, auth_type=restcall.rest_no_auth, resource=download_url, method="GET", additional_headers=additional_headers) if ret[0] != 0: logger.error("Status code is %s, detail is %s.", ret[2], ret[1]) raise GenericparserException("Failed to download %s from sdc." % download_url) fileutil.make_dirs(local_path) local_file_name = os.path.join(local_path, file_name) local_file = open(local_file_name, 'wb') local_file.write(ret[1]) local_file.close() return local_file_name
def upload_vnf_pkg_from_uri(self): logger.info("Start to upload VNF packge(%s) from URI..." % self.vnf_pkg_id) vnf_pkg = VnfPackageModel.objects.filter(vnfPackageId=self.vnf_pkg_id) if vnf_pkg[0].onboardingState != PKG_STATUS.CREATED: logger.error("VNF package(%s) is not CREATED" % self.vnf_pkg_id) raise GenericparserException("VNF package (%s) is not created" % self.vnf_pkg_id) vnf_pkg.update(onboardingState=PKG_STATUS.UPLOADING) uri = ignore_case_get(self.data, "addressInformation") response = urllib.request.urlopen(uri) local_file_dir = os.path.join(GENERICPARSER_ROOT_PATH, self.vnf_pkg_id) self.upload_file_name = os.path.join(local_file_dir, os.path.basename(uri)) if not os.path.exists(local_file_dir): fileutil.make_dirs(local_file_dir) with open(self.upload_file_name, "wt") as local_file: local_file.write(response.read()) response.close() logger.info('VNF packge(%s) has been uploaded.' % self.vnf_pkg_id)
def on_distribute(self, csar_id): if ServicePackageModel.objects.filter(servicePackageId=csar_id): raise PackageHasExistsException( "Service CSAR(%s) already exists." % csar_id) try: service = sdc.get_asset(sdc.ASSETTYPE_SERVICES, csar_id) # check if the related resources exist resources = service.get('resources', None) if resources: for resource in resources: if not VnfPackageModel.objects.filter(vnfPackageId=resource['resourceUUID']) and \ not PnfPackageModel.objects.filter(pnfPackageId=resource['resourceUUID']): logger.error("Resource [%s] is not distributed.", resource['resourceUUID']) raise GenericparserException( "Resource (%s) is not distributed." % resource['resourceUUID']) # download csar package local_path = os.path.join(GENERICPARSER_ROOT_PATH, csar_id) csar_name = "%s.csar" % service.get("name", csar_id) local_file_name = sdc.download_artifacts(service["toscaModelURL"], local_path, csar_name) if local_file_name.endswith(".csar") or local_file_name.endswith( ".zip"): fileutil.unzip_file(local_file_name, local_path, "") data = {'userDefinedData': {}} serviced = ServiceDescriptor() serviced.create(data, csar_id) serviced.parse_serviced_and_save(csar_id, local_file_name) except Exception as e: logger.error(traceback.format_exc()) if ServicePackageModel.objects.filter(servicePackageId=csar_id): ServicePackage().delete_csar(csar_id) raise e
def parse_serviced_and_save(self, serviced_info_id, local_file_name): logger.info('Start to process ServiceD(%s)...' % serviced_info_id) service_pkgs = ServicePackageModel.objects.filter( servicePackageId=serviced_info_id) service_pkgs.update(onboardingState=PKG_STATUS.PROCESSING) serviced_json = toscaparsers.parse_sd(local_file_name) serviced = json.JSONDecoder().decode(serviced_json) serviced_id = serviced.get("service", {}).get("properties", {}).get("descriptor_id", "") serviced_name = serviced.get("service", {}).get("properties", {}).get("name", "") serviced_version = serviced.get("service", {}).get("properties", {}).get("version", "") serviced_designer = serviced.get("service", {}).get("properties", {}).get("designer", "") invariant_id = serviced.get("service", {}).get("properties", {}).get("invariant_id", "") if serviced_id == "": raise GenericparserException( "serviced_id(%s) does not exist in metadata." % serviced_id) other_nspkg = ServicePackageModel.objects.filter( servicedId=serviced_id) if other_nspkg and other_nspkg[0].servicePackageId != serviced_info_id: logger.warn("ServiceD(%s,%s) already exists.", serviced_id, other_nspkg[0].servicePackageId) raise GenericparserException("ServiceD(%s) already exists." % serviced_id) for vnf in serviced["vnfs"]: vnfd_id = vnf["properties"].get("descriptor_id", "undefined") if vnfd_id == "undefined": vnfd_id = vnf["properties"].get("id", "undefined") pkg = VnfPackageModel.objects.filter(vnfdId=vnfd_id) if not pkg: pkg = VnfPackageModel.objects.filter(vnfPackageId=vnfd_id) if not pkg: vnfd_name = vnf.get("vnf_id", "undefined") logger.error("[%s] is not distributed.", vnfd_name) raise GenericparserException( "VNF package(%s) is not distributed." % vnfd_id) for pnf in serviced["pnfs"]: pnfd_id = pnf["properties"].get("descriptor_id", "undefined") if pnfd_id == "undefined": pnfd_id = pnf["properties"].get("id", "undefined") pkg = PnfPackageModel.objects.filter(pnfdId=pnfd_id) if not pkg: pkg = PnfPackageModel.objects.filter(pnfPackageId=pnfd_id) if not pkg: pnfd_name = pnf.get("pnf_id", "undefined") logger.error("[%s] is not distributed.", pnfd_name) raise GenericparserException( "PNF package(%s) is not distributed." % pnfd_name) service_pkgs.update(servicedId=serviced_id, servicedName=serviced_name, servicedDesigner=serviced_designer, servicedDescription=serviced.get( "description", ""), servicedVersion=serviced_version, invariantId=invariant_id, onboardingState=PKG_STATUS.ONBOARDED, operationalState=PKG_STATUS.ENABLED, usageState=PKG_STATUS.NOT_IN_USE, servicePackageUri=local_file_name, sdcCsarId=serviced_info_id, localFilePath=local_file_name, servicedModel=serviced_json) logger.info('ServiceD(%s) has been processed.' % serviced_info_id)
def validate_data(data, serializer): serialized_data = serializer(data=data) if not serialized_data.is_valid(): logger.error('Data validation failed.') raise GenericparserException(serialized_data.errors) return serialized_data