diff options
author | maopengzhang <zhang.maopeng1@zte.com.cn> | 2017-09-01 16:15:51 +0800 |
---|---|---|
committer | maopengzhang <zhang.maopeng1@zte.com.cn> | 2017-09-01 16:15:51 +0800 |
commit | 56595aa7d8c94ace97bad92ebeb6f98b8e9864bd (patch) | |
tree | ec973fd38ee7f03add22aa4916ef290aa8ab1039 | |
parent | c04406d6cf9401e2178042482f19200cc336f8dd (diff) |
add the Parser function in the catalog
add the Parser function in the catalog
Change-Id: I18465a8153aa5f519d6384c5c3cf40588241ab7e
Issue-ID: VFC-35
Signed-off-by: maopengzhang <zhang.maopeng1@zte.com.cn>
-rw-r--r-- | catalog/packages/nf_package.py | 30 | ||||
-rw-r--r-- | catalog/packages/ns_package.py | 31 | ||||
-rw-r--r-- | catalog/packages/tests.py | 13 | ||||
-rw-r--r-- | catalog/packages/urls.py | 3 | ||||
-rw-r--r-- | catalog/packages/views.py | 26 |
5 files changed, 85 insertions, 18 deletions
diff --git a/catalog/packages/nf_package.py b/catalog/packages/nf_package.py index 1e5dc985..10fccdab 100644 --- a/catalog/packages/nf_package.py +++ b/catalog/packages/nf_package.py @@ -24,7 +24,7 @@ import sys from catalog.pub.database.models import NfPackageModel from catalog.pub.utils.values import ignore_case_get from catalog.pub.utils import fileutil -from catalog.pub.exceptions import NSLCMException +from catalog.pub.exceptions import CatalogException from catalog.pub.config.config import CATALOG_ROOT_PATH from catalog.pub.msapi.extsys import get_vims from catalog.pub.utils.jobutil import JobUtil @@ -40,7 +40,7 @@ def nf_get_csars(): ret = None try: ret = NfPackage().get_csars() - except NSLCMException as e: + except CatalogException as e: return [1, e.message] except: logger.error(traceback.format_exc()) @@ -51,13 +51,26 @@ def nf_get_csar(csar_id): ret = None try: ret = NfPackage().get_csar(csar_id) - except NSLCMException as e: + except CatalogException as e: return [1, e.message] except: logger.error(traceback.format_exc()) return [1, str(sys.exc_info())] return ret +def parser_vnfmodel(csar_id,inputs): + ret = None + try: + nf_pkg = NfPackageModel.objects.filter(nfpackageid=csar_id) + if nf_pkg: + csar_path=nf_pkg["vnfd_path"] + ret=toscaparser.parse_vnfd(csar_path,inputs) + except CatalogException as e: + return [1, e.message] + except: + logger.error(traceback.format_exc()) + return [1, str(sys.exc_info())] + return ret ##################################################################################### class NfDistributeThread(threading.Thread): @@ -77,7 +90,7 @@ class NfDistributeThread(threading.Thread): def run(self): try: self.on_distribute() - except NSLCMException as e: + except CatalogException as e: self.rollback_distribute() JobUtil.add_job_status(self.job_id, JOB_ERROR, e.message) except: @@ -95,13 +108,13 @@ class NfDistributeThread(threading.Thread): 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) + raise CatalogException("NF CSAR(%s) already exists." % self.csar_id) vnfd,local_file_name,vnfd_json = self.get_vnfd(self.csar_id) nfd_id = vnfd["metadata"]["id"] if NfPackageModel.objects.filter(vnfdid=nfd_id): - raise NSLCMException("NFD(%s) already exists." % nfd_id) + raise CatalogException("NFD(%s) already exists." % nfd_id) JobUtil.add_job_status(self.job_id, 30, "Save CSAR(%s) to database." % self.csar_id) @@ -138,7 +151,6 @@ class NfDistributeThread(threading.Thread): logger.error(traceback.format_exc()) logger.error(str(sys.exc_info())) - ###################################################################################################################### @@ -156,7 +168,7 @@ class NfPkgDeleteThread(threading.Thread): def run(self): try: self.delete_csar() - except NSLCMException as e: + except CatalogException as e: JobUtil.add_job_status(self.job_id, JOB_ERROR, e.message) except: logger.error(traceback.format_exc()) @@ -176,7 +188,7 @@ class NfPkgDeleteThread(threading.Thread): NfInstModel.objects.filter(package_id=self.csar_id).delete() else: if NfInstModel.objects.filter(package_id=self.csar_id): - raise NSLCMException("NfInst by csar(%s) exists, cannot delete." % self.csar_id) + raise CatalogException("NfInst by csar(%s) exists, cannot delete." % self.csar_id) ''' nfvolcm.delete_nf_inst_mock() JobUtil.add_job_status(self.job_id, 50, "Delete CSAR(%s) from Database." % self.csar_id) diff --git a/catalog/packages/ns_package.py b/catalog/packages/ns_package.py index 29113d08..5c32e4e6 100644 --- a/catalog/packages/ns_package.py +++ b/catalog/packages/ns_package.py @@ -21,7 +21,7 @@ import os from catalog.pub.database.models import NSDModel, NfPackageModel from catalog.pub.utils.values import ignore_case_get -from catalog.pub.exceptions import NSLCMException +from catalog.pub.exceptions import CatalogException from catalog.pub.msapi import sdc from catalog.pub.config.config import CATALOG_ROOT_PATH from catalog.pub.utils import toscaparser @@ -41,7 +41,7 @@ def ns_on_distribute(csar_id): ret = None try: ret = NsPackage().on_distribute(csar_id) - except NSLCMException as e: + except CatalogException as e: NsPackage().delete_catalog(csar_id) return fmt_ns_pkg_rsp(STATUS_FAILED, e.message) except: @@ -55,7 +55,7 @@ def ns_delete_csar(csar_id, force_delete): ret = None try: ret = NsPackage().delete_csar(csar_id, force_delete) - except NSLCMException as e: + except CatalogException as e: return fmt_ns_pkg_rsp(STATUS_FAILED, e.message) except: logger.error(traceback.format_exc()) @@ -66,7 +66,7 @@ def ns_get_csars(): ret = None try: ret = NsPackage().get_csars() - except NSLCMException as e: + except CatalogException as e: return [1, e.message] except: logger.error(traceback.format_exc()) @@ -77,13 +77,26 @@ def ns_get_csar(csar_id): ret = None try: ret = NsPackage().get_csar(csar_id) - except NSLCMException as e: + except CatalogException as e: return [1, e.message] except: logger.error(traceback.format_exc()) return [1, str(sys.exc_info())] return ret +def parser_nsmodel(csar_id,inputs): + ret = None + try: + nf_pkg = NSDModel.objects.filter(id=csar_id) + if nf_pkg: + csar_path=nf_pkg["nsd_path"] + ret=toscaparser.parse_nsd(csar_path,inputs) + except CatalogException as e: + return [1, e.message] + except: + logger.error(traceback.format_exc()) + return [1, str(sys.exc_info())] + return ret ############################################################################################################### @@ -97,19 +110,19 @@ class NsPackage(object): def on_distribute(self, csar_id): if NSDModel.objects.filter(id=csar_id): - raise NSLCMException("NS CSAR(%s) already exists." % csar_id) + raise CatalogException("NS CSAR(%s) already exists." % csar_id) nsd,local_file_name,nsd_json = self.get_nsd(csar_id) nsd_id = nsd["metadata"]["id"] if NSDModel.objects.filter(nsd_id=nsd_id): - raise NSLCMException("NSD(%s) already exists." % nsd_id) + raise CatalogException("NSD(%s) already exists." % nsd_id) for vnf in nsd["vnfs"]: vnfd_id = vnf["properties"]["id"] pkg = NfPackageModel.objects.filter(vnfdid=vnfd_id) if not pkg: - raise NSLCMException("VNF package(%s) is not distributed." % vnfd_id) + raise CatalogException("VNF package(%s) is not distributed." % vnfd_id) NSDModel( id=csar_id, @@ -139,7 +152,7 @@ class NsPackage(object): NSInstModel.objects.filter(nspackage_id=csar_id).delete() else: if NSInstModel.objects.filter(nspackage_id=csar_id): - raise NSLCMException("CSAR(%s) is in using, cannot be deleted." % csar_id) + raise CatalogException("CSAR(%s) is in using, cannot be deleted." % csar_id) ''' nfvolcm.delete_ns_inst_mock() NSDModel.objects.filter(id=csar_id).delete() diff --git a/catalog/packages/tests.py b/catalog/packages/tests.py index 9bb5ea09..94d3a21c 100644 --- a/catalog/packages/tests.py +++ b/catalog/packages/tests.py @@ -542,3 +542,16 @@ class PackageTest(unittest.TestCase): ) self.assertEquals(size, len(vnfdmodels)) + + + def test_nf_package_parser(self): + # reqdata={"csarId":"1"} + # response = self.client.post("api/catalog/v1/vnfpackages/model",reqdata) + # self.assertEqual(status.HTTP_202_ACCEPTED, response.status_code, response.content) + pass + + def test_ns_package_parser(self): + # reqdata={"csarId":"1"} + # response = self.client.post("api/catalog/v1/nspackages/model",reqdata) + # self.assertEqual(status.HTTP_202_ACCEPTED, response.status_code, response.content) + pass
\ No newline at end of file diff --git a/catalog/packages/urls.py b/catalog/packages/urls.py index 70bb8320..2768ee3b 100644 --- a/catalog/packages/urls.py +++ b/catalog/packages/urls.py @@ -20,6 +20,9 @@ urlpatterns = [ url(r'^api/catalog/v1/nspackages/(?P<csarId>[0-9a-zA-Z\-\_]+)$', views.ns_rd_csar, name='nspackage_rd'), url(r'^api/catalog/v1/vnfpackages$', views.nfpackages_rc, name='nfpackages_rc'), url(r'^api/catalog/v1/vnfpackages/(?P<csarId>[0-9a-zA-Z\-\_]+)$', views.nf_rd_csar, name='nfpackage_rd'), + url(r'^api/catalog/v1/nspackages/model', views.ns_model_parser, name='ns_model_parser'), + url(r'^api/catalog/v1/vnfpackages/model', views.vnf_model_parser, name='vnf_model_parser') + ] diff --git a/catalog/packages/views.py b/catalog/packages/views.py index 8d250be2..248c6a11 100644 --- a/catalog/packages/views.py +++ b/catalog/packages/views.py @@ -104,3 +104,29 @@ def nf_rd_csar(request, *args, **kwargs): if ret[0] != 0: return Response(data={'error': ret[1]}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) return Response(data=ret[1], status=normal_status) + +@api_view(http_method_names=['POST']) +def ns_model_parser(request, *args, **kwargs): + csar_id = ignore_case_get(kwargs, "csarId") + inputs = ignore_case_get(kwargs, "inputs") + if request.method == 'POST': + ret = ns_package.parser_nsmodel(csar_id,inputs) + normal_status = status.HTTP_200_OK + + logger.info("Leave %s, Return value is %s", fun_name(), str(ret)) + if ret[0] != 0: + return Response(data={'error': ret[1]}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) + return Response(data=ret[1], status=normal_status) + +@api_view(http_method_names=['POST']) +def vnf_model_parser(request, *args, **kwargs): + csar_id = ignore_case_get(kwargs, "csarId") + inputs = ignore_case_get(kwargs, "inputs") + if request.method == 'POST': + ret = nf_package.parser_nfmodel(csar_id,inputs) + normal_status = status.HTTP_200_OK + + logger.info("Leave %s, Return value is %s", fun_name(), str(ret)) + if ret[0] != 0: + return Response(data={'error': ret[1]}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) + return Response(data=ret[1], status=normal_status) |