diff options
author | dyh <dengyuanhong@chinamobile.com> | 2019-11-15 09:26:18 +0800 |
---|---|---|
committer | dyh <dengyuanhong@chinamobile.com> | 2019-11-15 09:26:33 +0800 |
commit | 2f4d7a5b1d2a629e5856a5e3e17576bc618452f0 (patch) | |
tree | b000330cd97d55b15b7b627d60ae567038dc15f0 /catalog/packages/biz | |
parent | 4004afdf54c246b4afd481003aca16d2dab816fb (diff) |
Implement read VNFD API
Issue-ID: MODELING-266
Change-Id: Ib5e4a965c080329f68d407b414a347298da6a84b
Signed-off-by: dyh <dengyuanhong@chinamobile.com>
Diffstat (limited to 'catalog/packages/biz')
-rw-r--r-- | catalog/packages/biz/sdc_vnf_package.py | 35 | ||||
-rw-r--r-- | catalog/packages/biz/vnf_package.py | 60 |
2 files changed, 89 insertions, 6 deletions
diff --git a/catalog/packages/biz/sdc_vnf_package.py b/catalog/packages/biz/sdc_vnf_package.py index e5be4a1..2160a00 100644 --- a/catalog/packages/biz/sdc_vnf_package.py +++ b/catalog/packages/biz/sdc_vnf_package.py @@ -18,7 +18,9 @@ import os import sys import threading import traceback +import zipfile +from catalog.packages.biz.vnf_package import VnfPackage from catalog.packages.const import PKG_STATUS from catalog.pub.config.config import CATALOG_ROOT_PATH, CATALOG_URL_PATH from catalog.pub.config.config import REG_TO_MSB_REG_PARAM @@ -123,7 +125,7 @@ class NfDistributeThread(threading.Thread): 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 = "" + vendor_vnf_file = '' # find original vendor ETSI package under the ONBOARDING_PACKAGE directory onboarding_package_dir = os.path.join(local_path, "Artifacts/Deployment/ONBOARDED_PACKAGE") if os.path.exists(onboarding_package_dir): @@ -135,13 +137,16 @@ class NfDistributeThread(threading.Thread): break # find original vendor ETSI package under Artifacts/Deployment/OTHER directory - if vendor_vnf_file.isspace(): + 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) @@ -173,6 +178,32 @@ class NfDistributeThread(threading.Thread): ).save() JobUtil.add_job_status(self.job_id, 100, "CSAR(%s) distribute successfully." % self.csar_id) + def create_vnfd_zip(self, csar_id, vendor_vnf_file): + """ + Create VNFD zip file. + :param csar_id: CSAR Id + :param vendor_vnf_file: the vendor original package(csar) + :return: + """ + if os.path.exists(vendor_vnf_file): + # create VNFD from vendor original package + VnfPackage().creat_vnfd(csar_id, vendor_vnf_file) + else: + try: + vnf_package_path = os.path.join(CATALOG_ROOT_PATH, self.csar_id) + vnfd_zip_file = os.path.join(vnf_package_path, 'VNFD.zip') + with zipfile.ZipFile(vnfd_zip_file, 'w', zipfile.ZIP_DEFLATED) as vnfd_zip: + def_path = os.path.join(vnf_package_path, "Definitions") + if os.path.exists(def_path): + def_files = os.listdir(def_path) + for def_file in def_files: + full_path = os.path.join(def_path, def_file) + vnfd_zip.write(full_path, def_file) + except Exception as e: + logger.error(e) + if os.path.exists(vnfd_zip_file): + os.remove(vnfd_zip_file) + def rollback_distribute(self): try: VnfPackageModel.objects.filter(vnfPackageId=self.csar_id).delete() diff --git a/catalog/packages/biz/vnf_package.py b/catalog/packages/biz/vnf_package.py index 21d5819..5a51e9a 100644 --- a/catalog/packages/biz/vnf_package.py +++ b/catalog/packages/biz/vnf_package.py @@ -20,16 +20,16 @@ import threading import traceback import urllib import uuid +import zipfile +from catalog.packages import const from catalog.packages.biz.common import parse_file_range, read, save +from catalog.packages.biz.notificationsutil import prepare_vnfpkg_notification, NotificationsUtil from catalog.pub.config.config import CATALOG_ROOT_PATH from catalog.pub.database.models import VnfPackageModel, NSPackageModel from catalog.pub.exceptions import CatalogException, ResourceNotFoundException -from catalog.pub.utils.values import ignore_case_get from catalog.pub.utils import fileutil, toscaparser -from catalog.packages import const -from catalog.packages.biz.notificationsutil import prepare_vnfpkg_notification, NotificationsUtil - +from catalog.pub.utils.values import ignore_case_get logger = logging.getLogger(__name__) @@ -132,6 +132,58 @@ class VnfPackage(object): logger.info('VNF package (%s) has been downloaded.' % vnf_pkg_id) return read(local_file_path, start, end) + def download_vnfd(self, vnf_pkg_id): + 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)) + + def creat_vnfd(self, vnf_pkg_id, vendor_pkg_file): + """ + Create VNFD zip file from vendor original package + :param self: + :param vnf_pkg_id: VNF package id (CSAR id) + :param vendor_pkg_file: vendor original package + :return: + """ + vnf_package_path = os.path.join(CATALOG_ROOT_PATH, vnf_pkg_id) + if not os.path.exists(vnf_package_path): + os.makedirs(vnf_package_path) + vnfd_zip_file = os.path.join(vnf_package_path, "VNFD.zip") + if os.path.exists(vnfd_zip_file): + return vnfd_zip_file + else: + if vendor_pkg_file.endswith(".csar") or vendor_pkg_file.endswith(".zip"): + try: + vnfd_path = os.path.join(vnf_package_path, "vnfd") + with zipfile.ZipFile(vendor_pkg_file, 'r') as vendor_zip: + vender_files = vendor_zip.namelist() + for vender_file in vender_files: + if str(vender_file).startswith("Definitions"): + vendor_zip.extract(vender_file, vnfd_path) + with zipfile.ZipFile(vnfd_zip_file, 'w', zipfile.ZIP_DEFLATED) as vnfd_zip: + def_path = os.path.join(vnfd_path, "Definitions") + if os.path.exists(def_path): + def_files = os.listdir(def_path) + for def_file in def_files: + full_path = os.path.join(def_path, def_file) + vnfd_zip.write(full_path, def_file) + return vnfd_zip_file + except Exception as e: + logger.error(e) + if os.path.exists(vnfd_zip): + os.remove(vnfd_zip) + raise e + finally: + fileutil.delete_dirs(vnfd_path) + class VnfPkgUploadThread(threading.Thread): def __init__(self, data, vnf_pkg_id): |