示例#1
0
 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, verify=False)
         if response.status_code != status.HTTP_204_NO_CONTENT:
             raise CatalogException(
                 "callbackUri %s returns %s status "
                 "code." % (self.callback_uri, response.status_code))
     except Exception:
         raise CatalogException("callbackUri %s didn't return 204 status"
                                "code." % self.callback_uri)
示例#2
0
def create_consumer(name, salt, password):
    """
    Create a consumer to access the SDC
    :param name:
    :param salt:
    :param password:
    :return:
    """
    req_data = {
        'consumerName': name,
        'consumerSalt': salt,
        'consumerPassword': password
    }
    req_data = json.JSONEncoder().encode(req_data)
    resource = '/sdc2/rest/v1/consumers'
    headers = {'USER_ID': 'jh0003'}
    ret = restcall.call_req(base_url=SDC_BASE_URL,
                            user="",
                            passwd="",
                            auth_type=restcall.rest_no_auth,
                            resource=resource,
                            method="POST",
                            content=req_data,
                            additional_headers=headers)
    if ret[0] != 0:
        logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
        raise CatalogException("Failed to create consumer from sdc.")
示例#3
0
    def delete_vnf_pkg(self, vnf_pkg_id):
        """
        Delete a VNF package by give id
        :param vnf_pkg_id: The id of VNF package
        :return:
        """
        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 CatalogException("The VNF package (%s) is not disabled" % vnf_pkg_id)
        if vnf_pkg[0].usageState != PKG_STATUS.NOT_IN_USE:
            raise CatalogException("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 CatalogException('VNFD(%s) is referenced.' %
                                           del_vnfd_id)
        vnf_pkg.delete()
        send_notification(vnf_pkg_id, const.PKG_NOTIFICATION_TYPE.CHANGE,
                          const.PKG_CHANGE_TYPE.PKG_DELETE)

        vnf_pkg_path = os.path.join(CATALOG_ROOT_PATH, vnf_pkg_id)
        fileutil.delete_dirs(vnf_pkg_path)
        logger.info('VNF package(%s) has been deleted.' % vnf_pkg_id)
示例#4
0
    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 != const.PKG_STATUS.CREATED:
            logger.error("VNF package(%s) is not CREATED" % self.vnf_pkg_id)
            raise CatalogException("VNF package (%s) is not created" %
                                   self.vnf_pkg_id)
        vnf_pkg.update(onboardingState=const.PKG_STATUS.UPLOADING)
        send_notification(self.vnf_pkg_id,
                          const.PKG_NOTIFICATION_TYPE.ONBOARDING,
                          const.PKG_CHANGE_TYPE.OP_STATE_CHANGE)

        uri = ignore_case_get(self.data, "addressInformation")
        response = urllib.request.urlopen(uri)

        local_file_dir = os.path.join(CATALOG_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 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"], CATALOG_URL_PATH,
                db_csar_id, nf_pkg[0].vnfPackageUri)
        else:
            raise CatalogException("Vnf package[%s] not Found." % csar_id)

        csar_info = {
            "csarId": db_csar_id,
            "packageInfo": pkg_info,
            "imageInfo": []
        }
        return [0, csar_info]
示例#6
0
def download_artifacts(download_url, local_path, file_name):
    """
    Downlaod artifacts from SDC
    :param download_url:
    :param local_path:
    :param file_name:
    :return:
    """
    additional_headers = {
        'X-ECOMP-InstanceID': 'VFC',
        '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 CatalogException("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
示例#7
0
 def delete_single(self, pnfd_info_id):
     """
     Delete PNF package by id
     :param pnfd_info_id:
     :return:
     """
     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 CatalogException('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 CatalogException('PNFD(%s) is referenced.' % pnfd_info_id)
     pnf_pkgs.delete()
     pnf_pkg_path = os.path.join(CATALOG_ROOT_PATH, pnfd_info_id)
     fileutil.delete_dirs(pnf_pkg_path)
     send_notification(const.NSD_NOTIFICATION_TYPE.PNFD_DELETION, pnfd_info_id, del_pnfd_id)
     logger.debug('PNFD(%s) has been deleted.' % pnfd_info_id)
示例#8
0
def get_artifact(asset_type, csar_id):
    """
    Get artifact by given asset type and csar id
    :param asset_type:
    :param csar_id:
    :return:
    """
    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 CatalogException(
                    "The artifact (%s,%s) is not distributed from sdc." %
                    (asset_type, csar_id))
            else:
                return artifact
    raise CatalogException("Failed to query artifact(%s,%s) from sdc." %
                           (asset_type, csar_id))
 def test_service_pkg_distribute_when_fail_get_artifacts(
         self, mock_get_asset):
     mock_get_asset.side_effect = CatalogException(
         "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, CatalogException))
         self.assertEqual("Failed to query artifact(services,1) from sdc.",
                          e.args[0])
示例#10
0
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=const.PKG_STATUS.PROCESSING)
    vnfd_json = toscaparser.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 CatalogException("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=const.PKG_STATUS.ONBOARDED,
                       operationalState=const.PKG_STATUS.ENABLED,
                       usageState=const.PKG_STATUS.NOT_IN_USE,
                       localFilePath=vnf_pkg_path,
                       vnfPackageUri=os.path.split(vnf_pkg_path)[-1])
        send_notification(vnf_pkg_id, const.PKG_NOTIFICATION_TYPE.ONBOARDING,
                          const.PKG_CHANGE_TYPE.OP_STATE_CHANGE)
    else:
        raise CatalogException(
            "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)
示例#11
0
def get_artifacts(asset_type):
    """
    Get artifacts by given asset type
    :param asset_type:
    :return:
    """
    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 CatalogException("Failed to query artifacts(%s) from sdc." %
                               asset_type)
    return json.JSONDecoder().decode(ret[1])
示例#12
0
def get_asset(asset_type, uuid):
    """
    Get asset by given type and UUID
    :param asset_type:
    :param uuid:
    :return:
    """
    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 CatalogException("Failed to get asset(%s, %s) from sdc." %
                               (asset_type, uuid))
    asset = json.JSONDecoder().decode(ret[1])
    if len(asset) == 0:
        raise CatalogException("Failed to get asset(%s, %s) from sdc." %
                               (asset_type, uuid))
    if asset.get("distributionStatus", None) != DISTRIBUTED:
        raise CatalogException(
            "The asset (%s,%s) is not distributed from sdc." %
            (asset_type, uuid))
    else:
        return asset
示例#13
0
    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 != const.PKG_STATUS.ONBOARDED:
            raise CatalogException("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 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 = CatalogException(
            "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, CatalogException))
            self.assertEqual("Failed to download 1 from sdc.", e.args[0])
示例#15
0
def process_notification(msg):
    logger.info('Receive a callback notification, nb of resources: %s',
                len(msg['resources']))
    try:
        ns = sdc.get_asset(sdc.ASSETTYPE_SERVICES, msg['serviceUUID'])
        # check if the related resources exist
        resources = ns.get('resources', None)
        job_array = []
        resource_threads = []
        if resources:
            for resource in resources:
                if (resource['resoucreType']
                        == 'VF') and not VnfPackageModel.objects.filter(
                            vnfPackageId=resource['resourceUUID']):
                    logger.debug("VF [%s] is not distributed.",
                                 resource['resourceUUID'])
                    # raise CatalogException("VF (%s) is not distributed." % resource['resourceUUID'])
                    logger.info("VF [%s] begin to distribute.",
                                resource['resourceUUID'])
                    csar_id = resource['resourceUUID']
                    vim_ids = ignore_case_get(resource, "vimIds")
                    lab_vim_id = ignore_case_get(resource, "labVimId")
                    job_id = str(uuid.uuid4())
                    job_array.append(job_id)
                    resource_threads.append(
                        sdc_vnf_package.NfDistributeThread(
                            csar_id, vim_ids, lab_vim_id, job_id))
                    # sdc_vnf_package.NfDistributeThread(csar_id, vim_ids, lab_vim_id, job_id).start()
                else:
                    logger.debug("resource [%s] has been distributed",
                                 resource['resourceUUID'])
        for resource_thread in resource_threads:
            resource_thread.start()
        for resource_thread in resource_threads:
            resource_thread.join()
        for jobID in job_array:
            job_status = JobUtil.query_job_status(jobID)
            if job_status[0].status == 'error':
                raise CatalogException("VF resource fail to distributed.")
        csar_id = msg['serviceUUID']
        sdc_ns_package.ns_on_distribute(csar_id)
        logger.debug("Csar package [%s] has been destributed successfully",
                     csar_id)
    except CatalogException as e:
        logger.error("Failed to download the resource")
        logger.error(str(e))
示例#16
0
    def upload(self, nsd_info_id, remote_file):
        """
        Upload NS package file
        :param nsd_info_id:
        :param remote_file:
        :return:
        """
        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 CatalogException('NSD(%s) does not exist.' % nsd_info_id)
        ns_pkgs.update(onboardingState=const.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
示例#17
0
def delete_artifact(asset_type, asset_id, artifact_id):
    """
    Delete artifact by conditions from SDC
    :param asset_type:
    :param asset_id:
    :param artifact_id:
    :return:
    """
    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 CatalogException("Failed to delete artifacts(%s) from sdc." %
                               artifact_id)
    return json.JSONDecoder().decode(ret[1])
示例#18
0
    def on_distribute(self, csar_id):
        """
        Fetch NS package csar from SDC
        :param csar_id:
        :return:
        """
        if NSPackageModel.objects.filter(nsPackageId=csar_id):
            return [1, "NS CSAR(%s) already exists." % csar_id]

        ns = sdc.get_asset(sdc.ASSETTYPE_SERVICES, csar_id)
        # check if the related resources exist
        resources = ns.get('resources', None)
        if resources:
            for resource in resources:
                if resource[
                        'resoucreType'].upper == 'VF' and not VnfPackageModel.objects.filter(
                            vnfPackageId=resource['resourceUUID']):
                    logger.error("VF [%s] is not distributed.",
                                 resource['resourceUUID'])
                    raise CatalogException("VF (%s) is not distributed." %
                                           resource['resourceUUID'])
                # if resource['resoucreType'] == 'PNF' and not PnfPackageModel.objects.filter(
                #         pnfPackageId=resource['resourceUUID']):
                #     logger.error("PNF [%s] is not distributed.", resource['resourceUUID'])
                #     raise CatalogException("PNF (%s) is not distributed." % resource['resourceUUID'])

        # download csar package
        local_path = os.path.join(CATALOG_ROOT_PATH, csar_id)
        csar_name = "%s.csar" % ns.get("name", csar_id)
        local_file_name = sdc.download_artifacts(ns["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/ns.csar")
            if os.path.exists(artifact_vnf_file):
                local_file_name = artifact_vnf_file

        data = {'userDefinedData': {}}
        nsd = NsDescriptor()
        nsd.create(data, csar_id)
        nsd.parse_nsd_and_save(csar_id, local_file_name)
        return [0, "CSAR(%s) distributed successfully." % csar_id]
示例#19
0
    def download(self, pnfd_info_id):
        """
        Download PNF package file by id
        :param pnfd_info_id:
        :return:
        """
        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 CatalogException('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)
示例#20
0
def register_for_topics(key):
    """
    Register a topics of SDC
    :param key:
    :return:
    """
    req_data = {
        'apiPublicKey': key,
        'distrEnvName': 'AUTO',
        'isConsumerToSdcDistrStatusTopic': False,
        'distEnvEndPoints': []
    }
    req_data = json.JSONEncoder().encode(req_data)
    url = '/sdc/v1/registerForDistribution'
    ret = call_sdc(url, 'POST', req_data)
    if ret[0] != 0:
        logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
        raise CatalogException("Failed to register from sdc.")
    return json.JSONDecoder().decode(ret[1])
示例#21
0
 def parse_pnfd(self, csar_id, inputs):
     """
     Parse PNFD
     :param csar_id:
     :param inputs:
     :return:
     """
     try:
         pnf_pkg = PnfPackageModel.objects.filter(pnfPackageId=csar_id)
         if not pnf_pkg:
             raise CatalogException("PNF CSAR(%s) does not exist." % csar_id)
         csar_path = pnf_pkg[0].localFilePath
         ret = {"model": toscaparser.parse_pnfd(csar_path, inputs)}
     except CatalogException as e:
         return [1, e.args[0]]
     except Exception as e:
         logger.error(e.args[0])
         return [1, e.args[0]]
     return [0, ret]
示例#22
0
    def download_vnfd(self, vnf_pkg_id):
        """
        Download VNFD for given id
        :param vnf_pkg_id: The id of VNF package
        :return: VNFD
        """
        logger.info('Start to download VNFD of 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 != const.PKG_STATUS.ONBOARDED:
            raise CatalogException("VNF package (%s) is not on-boarded" %
                                   vnf_pkg_id)

        vnfd_zip_file = self.creat_vnfd(vnf_pkg_id, nf_pkg[0].localFilePath)
        logger.info('VNFD of VNF package (%s) has been downloaded.' %
                    vnf_pkg_id)
        return read(vnfd_zip_file, 0, os.path.getsize(vnfd_zip_file))
示例#23
0
    def download(self, nsd_info_id, file_range):
        """
        Download NS package file
        :param nsd_info_id:
        :param file_range:
        :return:
        """
        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 != const.PKG_STATUS.ONBOARDED:
            logger.error('NSD(%s) is not ONBOARDED.' % nsd_info_id)
            raise CatalogException('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(csar_id, inputs):
    """
    Parse VNFD
    :param csar_id:
    :param inputs:
    :return:
    """
    ret = None
    try:
        nf_pkg = VnfPackageModel.objects.filter(vnfPackageId=csar_id)
        if not nf_pkg:
            raise CatalogException("VNF CSAR(%s) does not exist." % csar_id)
        csar_path = nf_pkg[0].localFilePath
        ret = {"model": toscaparser.parse_vnfd(csar_path, inputs)}
    except CatalogException 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 on_distribute(self, csar_id):
        """
        Get service packge from SDC and process
        :param csar_id:
        :return:
        """
        if ServicePackageModel.objects.filter(servicePackageId=csar_id):
            err_msg = "Service CSAR(%s) already exists." % csar_id
            logger.warn(err_msg)
            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 CatalogException("Resource (%s) is not distributed." % resource['resourceUUID'])

            # download csar package
            local_path = os.path.join(CATALOG_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
示例#26
0
    def upload(self, remote_file, pnfd_info_id):
        """
        Upload PNF package file
        :param remote_file:
        :param pnfd_info_id:
        :return:
        """
        logger.info('Start to upload PNFD(%s)...' % pnfd_info_id)
        pnf_pkgs = PnfPackageModel.objects.filter(pnfPackageId=pnfd_info_id)
        if not pnf_pkgs.exists():
            details = 'PNFD(%s) is not CREATED.' % pnfd_info_id
            logger.info(details)
            send_notification(
                type=const.NSD_NOTIFICATION_TYPE.PNFD_ONBOARDING_FAILURE,
                pnfd_info_id=pnfd_info_id,
                failure_details=details
            )
            raise CatalogException(details)
        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
示例#27
0
    def get_csar(self, csar_id):
        """
        Get NS package by id
        :param csar_id:
        :return:
        """
        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"], CATALOG_URL_PATH,
                csar_id, csars[0].nsPackageUri)
        else:
            raise CatalogException("Ns package[%s] not Found." % csar_id)

        return [0, {"csarId": csar_id, "packageInfo": package_info}]
    def parse_serviced_and_save(self, serviced_info_id, local_file_name):
        """
        Parse service package and save information to DB
        :param serviced_info_id:
        :param local_file_name:
        :return:
        """
        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 = toscaparser.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 CatalogException(
                "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 CatalogException("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 CatalogException("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 CatalogException("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 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,
                                   error_code=JOB_ERROR_CODE.PACKAGE_EXIST)
            return

        artifact = sdc.get_artifact(sdc.ASSETTYPE_RESOURCES, self.csar_id)
        local_path = os.path.join(CATALOG_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"):
            fileutil.unzip_csar(local_file_name, local_path)
            vendor_vnf_file = ''
            # find original vendor ETSI package under the ETSI_PACKAGE directory
            etsi_package_dir = os.path.join(
                local_path, "Artifacts/Deployment/ETSI_PACKAGE")
            if os.path.exists(etsi_package_dir):
                files = os.listdir(etsi_package_dir)
                for file_name in files:
                    a_file = os.path.join(etsi_package_dir, file_name)
                    if os.path.isfile(a_file) & file_name.endswith(".csar"):
                        vendor_vnf_file = a_file
                        break

            # find original vendor ETSI package under Artifacts/Deployment/OTHER directory
            if vendor_vnf_file.strip() == '':
                vendor_vnf_file = os.path.join(
                    local_path, "Artifacts/Deployment/OTHER/vnf.csar")
                if os.path.exists(vendor_vnf_file):
                    local_file_name = vendor_vnf_file
            else:
                local_file_name = vendor_vnf_file

        # create VNFD zip file
        self.create_vnfd_zip(self.csar_id, vendor_vnf_file)

        vnfd_json = toscaparser.parse_vnfd(local_file_name)
        vnfd = json.JSONDecoder().decode(vnfd_json)

        if not vnfd.get("vnf"):
            raise CatalogException(
                "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 PackageHasExistsException("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", "")
        # Update VNF package Model to DB
        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=const.PKG_STATUS.ONBOARDED,
                        operationalState=const.PKG_STATUS.ENABLED,
                        usageState=const.PKG_STATUS.NOT_IN_USE).save()
        JobUtil.add_job_status(
            self.job_id, 100,
            "CSAR(%s) distribute successfully." % self.csar_id)
        send_notification(self.csar_id, const.PKG_NOTIFICATION_TYPE.ONBOARDING,
                          const.PKG_CHANGE_TYPE.OP_STATE_CHANGE)
示例#30
0
    def parse_nsd_and_save(self, nsd_info_id, local_file_name):
        """
        Parse NSD and save the information
        :param nsd_info_id:
        :param local_file_name:
        :return:
        """
        logger.info('Start to process NSD(%s)...' % nsd_info_id)
        ns_pkgs = NSPackageModel.objects.filter(nsPackageId=nsd_info_id)
        ns_pkgs.update(onboardingState=const.PKG_STATUS.PROCESSING)

        nsd_json = toscaparser.parse_nsd(local_file_name)
        logger.debug("%s", nsd_json)
        nsd = json.JSONDecoder().decode(nsd_json)

        nsd_id = nsd.get("ns", {}).get("properties",
                                       {}).get("descriptor_id", "")
        nsd_name = nsd.get("ns", {}).get("properties", {}).get("name", "")
        nsd_version = nsd.get("ns", {}).get("properties",
                                            {}).get("version", "")
        nsd_designer = nsd.get("ns", {}).get("properties",
                                             {}).get("designer", "")
        invariant_id = nsd.get("ns", {}).get("properties",
                                             {}).get("invariant_id", "")
        if nsd_id == "":
            raise CatalogException("nsd_id(%s) does not exist in metadata." %
                                   nsd_id)
        other_nspkg = NSPackageModel.objects.filter(nsdId=nsd_id)
        if other_nspkg and other_nspkg[0].nsPackageId != nsd_info_id:
            failure_details = "NSD(%s,%s) already exists.", nsd_id, other_nspkg[
                0].nsPackageId
            logger.warn(failure_details)
            send_notification(
                const.NSD_NOTIFICATION_TYPE.NSD_ONBOARDING_FAILURE,
                nsd_info_id,
                nsd_id,
                failure_details=failure_details)
            raise CatalogException("NSD(%s) already exists." % nsd_id)

        for vnf in nsd["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 CatalogException("VNF package(%s) is not distributed." %
                                       vnfd_id)

        for pnf in nsd["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 CatalogException("PNF package(%s) is not distributed." %
                                       pnfd_name)

        ns_pkgs.update(nsdId=nsd_id,
                       nsdName=nsd_name,
                       nsdDesginer=nsd_designer,
                       nsdDescription=nsd.get("description", ""),
                       nsdVersion=nsd_version,
                       invariantId=invariant_id,
                       onboardingState=const.PKG_STATUS.ONBOARDED,
                       operationalState=const.PKG_STATUS.ENABLED,
                       usageState=const.PKG_STATUS.NOT_IN_USE,
                       nsPackageUri=local_file_name,
                       sdcCsarId=nsd_info_id,
                       localFilePath=local_file_name,
                       nsdModel=nsd_json)
        send_notification(const.NSD_NOTIFICATION_TYPE.NSD_ONBOARDING,
                          nsd_info_id, nsd_id)
        logger.info('NSD(%s) has been processed.' % nsd_info_id)