diff options
-rw-r--r-- | lcm/packages/sdc_nf_package.py | 190 | ||||
-rw-r--r-- | lcm/packages/urls.py | 1 | ||||
-rw-r--r-- | lcm/packages/views.py | 21 |
3 files changed, 211 insertions, 1 deletions
diff --git a/lcm/packages/sdc_nf_package.py b/lcm/packages/sdc_nf_package.py new file mode 100644 index 00000000..8887f83b --- /dev/null +++ b/lcm/packages/sdc_nf_package.py @@ -0,0 +1,190 @@ +# Copyright 2016-2017 ZTE Corporation. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import json +import logging +import uuid +import os +import time +import threading +import traceback +import sys + +from lcm.pub.database.models import NfPackageModel, NfInstModel +from lcm.pub.utils.values import ignore_case_get +from lcm.pub.utils import fileutil +from lcm.pub.exceptions import NSLCMException +from lcm.pub.config.config import CATALOG_ROOT_PATH +from lcm.pub.msapi.extsys import get_vims +from lcm.pub.utils.jobutil import JobUtil +from lcm.pub.utils import toscautil + +logger = logging.getLogger(__name__) + +JOB_ERROR = 255 + + +class SdcNfDistributeThread(threading.Thread): + """ + Sdc NF Package Distribute + """ + + def __init__(self, csar_id, vim_ids, lab_vim_id, job_id): + threading.Thread.__init__(self) + self.csar_id = csar_id + self.vim_ids = vim_ids + self.lab_vim_id = lab_vim_id + self.job_id = job_id + + self.csar_save_path = os.path.join(CATALOG_ROOT_PATH, csar_id) + + def run(self): + try: + self.on_distribute() + except NSLCMException as e: + self.rollback_distribute() + JobUtil.add_job_status(self.job_id, JOB_ERROR, e.message) + except: + logger.error(traceback.format_exc()) + logger.error(str(sys.exc_info())) + self.rollback_distribute() + JobUtil.add_job_status(self.job_id, JOB_ERROR, "Failed to distribute CSAR(%s)" % self.csar_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 NfPackageModel.objects.filter(nfpackageid=self.csar_id): + raise NSLCMException("NF CSAR(%s) already exists." % self.csar_id) + artifact = sdc.get_artifact(sdc.ASSETTYPE_RESOURCES, csar_id) + download_artifacts(artifact["toscaModelURL"], "TODO:Local Path") + + JobUtil.add_job_status(self.job_id, 30, "Save CSAR(%s) to database." % self.csar_id) + NfPackageModel( + uuid=csar_id, + nfpackageid=csar_id, + vnfdid="TODO", + vendor="TODO", + vnfdversion="TODO", + vnfversion="TODO", + vnfdmodel="TODO").save() + + JobUtil.add_job_status(self.job_id, 100, "CSAR(%s) distribute successfully." % self.csar_id) + + + def rollback_distribute(self): + try: + NfPackageModel.objects.filter(nfpackageid=self.csar_id).delete() + fileutil.delete_dirs(self.csar_save_path) + except: + logger.error(traceback.format_exc()) + logger.error(str(sys.exc_info())) + + +###################################################################################################################### + + +class SdcNfPkgDeleteThread(threading.Thread): + """ + Sdc NF Package Deleting + """ + + def __init__(self, csar_id, job_id, force_delete): + threading.Thread.__init__(self) + self.csar_id = csar_id + self.job_id = job_id + self.force_delete = force_delete + + def run(self): + try: + self.delete_csar() + except NSLCMException as e: + JobUtil.add_job_status(self.job_id, JOB_ERROR, e.message) + except: + logger.error(traceback.format_exc()) + logger.error(str(sys.exc_info())) + JobUtil.add_job_status(self.job_id, JOB_ERROR, "Failed to delete CSAR(%s)" % self.csar_id) + + def delete_csar(self): + JobUtil.create_job( + inst_type='nf', + jobaction='delete', + inst_id=self.csar_id, + job_id=self.job_id) + JobUtil.add_job_status(self.job_id, 5, "Start to delete CSAR(%s)." % self.csar_id) + + if self.force_delete: + NfInstModel.objects.filter(package_id=self.csar_id).delete() + else: + if NfInstModel.objects.filter(package_id=self.csar_id): + raise NSLCMException("NfInst(%s) exists, cannot delete." % self.csar_id) + + JobUtil.add_job_status(self.job_id, 50, "Delete CSAR(%s) from Database." % self.csar_id) + + VnfPackageFileModel.objects.filter(vnfpid=csar_id).delete() + NfPackageModel.objects.filter(nfpackageid=csar_id).delete() + + JobUtil.add_job_status(self.job_id, 80, "Delete local CSAR(%s) file." % self.csar_id) + + csar_save_path = os.path.join(CATALOG_ROOT_PATH, self.csar_id) + fileutil.delete_dirs(self.csar_save_path) + + JobUtil.add_job_status(self.job_id, 100, "Delete CSAR(%s) successfully." % self.csar_id) + + +###################################################################################################################### + +class SdcNfPackage(object): + """ + Actions for sdc nf package. + """ + + def __init__(self): + pass + + def get_csars(self): + ret = {"csars": []} + nf_pkgs = NfPackageModel.objects.filter() + for nf_pkg in nf_pkgs: + ret["csars"].append({ + "csarId": nf_pkg.nfpackageid, + "vnfdId": nf_pkg.vnfdid + }) + return ret + + def get_csar(self, csar_id): + pkg_info = {} + nf_pkg = NfPackageModel.objects.filter(nfpackageid=csar_id) + if nf_pkg: + pkg_info["vnfdId"] = nf_pkg[0].vnfdid + pkg_info["vnfdProvider"] = nf_pkg[0].vendor + pkg_info["vnfdVersion"] = nf_pkg[0].vnfdversion + pkg_info["vnfVersion"] = nf_pkg[0].vnfversion + + + vnf_insts = NfInstModel.objects.filter(package_id=csar_id) + vnf_inst_info = [{"vnfInstanceId": vnf_inst.nfinstid, + "vnfInstanceName": vnf_inst.nf_name} for vnf_inst in vnf_insts] + + return [0, {"csarId": csar_id, + "packageInfo": pkg_info, + "imageInfo": [], + "vnfInstanceInfo": vnf_inst_info}] + + + diff --git a/lcm/packages/urls.py b/lcm/packages/urls.py index 86af4270..d4b139eb 100644 --- a/lcm/packages/urls.py +++ b/lcm/packages/urls.py @@ -18,6 +18,7 @@ from lcm.packages import views urlpatterns = [ url(r'^api/nslcm/v1/nspackage$', views.ns_distribute, name='ns_distribute'), + url(r'^api/nslcm/v1/vnfpackage$', views.nf_distribute, name='nf_distribute'), ######################################################################################### url(r'^api/nslcm/v0/nspackage/(?P<csarId>[0-9a-zA-Z\-\_]+)$', views.ns_access_csar, name='ns_access_csar'), url(r'^api/nslcm/v0/nspackage$', views.ns_on_boarding, name='ns_on_boarding'), diff --git a/lcm/packages/views.py b/lcm/packages/views.py index dd29d76d..28a9bf66 100644 --- a/lcm/packages/views.py +++ b/lcm/packages/views.py @@ -21,7 +21,7 @@ from rest_framework.response import Response from lcm.pub.utils.values import ignore_case_get from lcm.pub.utils.syscomm import fun_name -from lcm.packages import ns_package, nf_package, sdc_ns_package +from lcm.packages import ns_package, nf_package, sdc_ns_package, sdc_nf_package logger = logging.getLogger(__name__) @@ -37,6 +37,25 @@ def ns_distribute(request, *args, **kwargs): #################################################################################################### +@api_view(http_method_names=['POST', 'GET']) +def nf_distribute(request, *args, **kwargs): + logger.info("Enter %s%s, method is %s", fun_name(), request.data, request.method) + if request.method == 'GET': + ret = sdc_nf_package.SdcNfPackage().get_csars() + logger.debug("csars=%s", ret) + return Response(data=ret, status=status.HTTP_200_OK) + csar_id = ignore_case_get(request.data, "csarId") + vim_ids = ignore_case_get(request.data, "vimIds") + lab_vim_id = ignore_case_get(request.data, "labVimId") + job_id = str(uuid.uuid4()) + sdc_nf_package.SdcNfDistributeThread(csar_id, vim_ids, lab_vim_id, job_id).start() + ret = {"jobId": job_id} + logger.info("Leave %s, Return value is %s", fun_name(), ret) + return Response(data=ret, status=status.HTTP_202_ACCEPTED) + + +#################################################################################################### + @api_view(http_method_names=['POST']) def ns_on_boarding(request, *args, **kwargs): csar_id = ignore_case_get(request.data, "csarId") |