diff options
author | biancunkang <bian.cunkang@zte.com.cn> | 2018-08-30 10:26:07 +0800 |
---|---|---|
committer | biancunkang <bian.cunkang@zte.com.cn> | 2018-08-30 10:26:07 +0800 |
commit | fce8c304dfb22382377d76d8cfabda5ab553b009 (patch) | |
tree | 11fc0b211f3904bc35503c822d3224edb4b372ad | |
parent | 8962e05b3fe8d4ad4f570b0186ba8622a5b4d19a (diff) |
Add const file
Add a const file and deal with the associated change
Change-Id: Ic9e3231b6afd4d5cffe28ab4d997b574af4f52d2
Issue-ID: VFC-1038
Signed-off-by: biancunkang <bian.cunkang@zte.com.cn>
-rw-r--r-- | catalog/packages/biz/ns_descriptor.py | 29 | ||||
-rw-r--r-- | catalog/packages/biz/pnf_descriptor.py | 23 | ||||
-rw-r--r-- | catalog/packages/biz/vnf_package.py | 37 | ||||
-rw-r--r-- | catalog/packages/const.py | 18 | ||||
-rw-r--r-- | catalog/packages/serializers/nsd_infos.py | 3 | ||||
-rw-r--r-- | catalog/packages/serializers/pnfd_infos.py | 3 | ||||
-rw-r--r-- | catalog/packages/tests/test_ns_descriptor.py | 3 | ||||
-rw-r--r-- | catalog/packages/tests/test_pnf_descriptor.py | 3 | ||||
-rw-r--r-- | catalog/packages/tests/test_vnf_package.py | 10 | ||||
-rw-r--r-- | catalog/packages/views/vnf_package_views.py | 3 |
10 files changed, 82 insertions, 50 deletions
diff --git a/catalog/packages/biz/ns_descriptor.py b/catalog/packages/biz/ns_descriptor.py index fc771082..56136d7c 100644 --- a/catalog/packages/biz/ns_descriptor.py +++ b/catalog/packages/biz/ns_descriptor.py @@ -23,6 +23,7 @@ from catalog.pub.database.models import NSPackageModel, PnfPackageModel, VnfPack from catalog.pub.exceptions import CatalogException, ResourceNotFoundException from catalog.pub.utils import fileutil, toscaparser from catalog.pub.utils.values import ignore_case_get +from catalog.packages.const import PKG_STATUS logger = logging.getLogger(__name__) @@ -32,19 +33,19 @@ def create(data): user_defined_data = ignore_case_get(data, 'userDefinedData') data = { 'id': str(uuid.uuid4()), - 'nsdOnboardingState': 'CREATED', - 'nsdOperationalState': 'DISABLED', - 'nsdUsageState': 'NOT_IN_USE', + 'nsdOnboardingState': PKG_STATUS.CREATED, + 'nsdOperationalState': PKG_STATUS.DISABLED, + 'nsdUsageState': PKG_STATUS.NOT_IN_USE, 'userDefinedData': user_defined_data, '_links': None # TODO } - NSPackageModel( + NSPackageModel.objects.create( nsPackageId=data['id'], onboardingState=data['nsdOnboardingState'], operationalState=data['nsdOperationalState'], usageState=data['nsdUsageState'], userDefinedData=data['userDefinedData'] - ).save() + ) logger.info('A NSD(%s) has been created.' % data['id']) return data @@ -73,10 +74,10 @@ def delete_single(nsd_info_id): logger.info('NSD(%s) has been deleted.' % nsd_info_id) return ''' - if ns_pkgs[0].operationalState != 'DISABLED': + if ns_pkgs[0].operationalState != PKG_STATUS.DISABLED: logger.error('NSD(%s) shall be DISABLED.' % nsd_info_id) raise CatalogException('NSD(%s) shall be DISABLED.' % nsd_info_id) - if ns_pkgs[0].usageState != 'NOT_IN_USE': + if ns_pkgs[0].usageState != PKG_STATUS.NOT_IN_USE: logger.error('NSD(%s) shall be NOT_IN_USE.' % nsd_info_id) raise CatalogException('NSD(%s) shall be NOT_IN_USE.' % nsd_info_id) ''' @@ -93,7 +94,7 @@ def upload(remote_file, nsd_info_id): logger.info('NSD(%s) does not exist.' % nsd_info_id) raise CatalogException('NSD(%s) does not exist.' % nsd_info_id) - ns_pkgs.update(onboardingState='UPLOADING') + ns_pkgs.update(onboardingState=PKG_STATUS.UPLOADING) local_file_name = remote_file.name local_file_dir = os.path.join(CATALOG_ROOT_PATH, nsd_info_id) local_file_name = os.path.join(local_file_dir, local_file_name) @@ -109,7 +110,7 @@ def upload(remote_file, nsd_info_id): def parse_nsd_and_save(nsd_info_id, local_file_name): logger.info('Start to process NSD(%s)...' % nsd_info_id) ns_pkgs = NSPackageModel.objects.filter(nsPackageId=nsd_info_id) - ns_pkgs.update(onboardingState='PROCESSING') + ns_pkgs.update(onboardingState=PKG_STATUS.PROCESSING) nsd_json = toscaparser.parse_nsd(local_file_name) nsd = json.JSONDecoder().decode(nsd_json) @@ -131,9 +132,9 @@ def parse_nsd_and_save(nsd_info_id, local_file_name): nsdDesginer=nsd["metadata"].get("vendor", "undefined"), nsdDescription=nsd["metadata"].get("description", ""), nsdVersion=nsd["metadata"].get("version", "undefined"), - onboardingState="ONBOARDED", - operationalState="ENABLED", - usageState="NOT_IN_USE", + onboardingState=PKG_STATUS.ONBOARDED, + operationalState=PKG_STATUS.ENABLED, + usageState=PKG_STATUS.NOT_IN_USE, nsPackageUri=local_file_name, sdcCsarId=nsd_info_id, localFilePath=local_file_name, @@ -148,7 +149,7 @@ def download(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 != 'ONBOARDED': + if ns_pkgs[0].onboardingState != 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 @@ -204,4 +205,4 @@ def fill_resp_data(ns_pkg): def handle_upload_failed(nsd_info_id): ns_pkg = NSPackageModel.objects.filter(nsPackageId=nsd_info_id) - ns_pkg.update(onboardingState="CREATED") + ns_pkg.update(onboardingState=PKG_STATUS.CREATED) diff --git a/catalog/packages/biz/pnf_descriptor.py b/catalog/packages/biz/pnf_descriptor.py index 0eeb833c..2a07d245 100644 --- a/catalog/packages/biz/pnf_descriptor.py +++ b/catalog/packages/biz/pnf_descriptor.py @@ -23,6 +23,7 @@ from catalog.pub.database.models import NSPackageModel, PnfPackageModel from catalog.pub.exceptions import CatalogException, ResourceNotFoundException from catalog.pub.utils import fileutil, toscaparser from catalog.pub.utils.values import ignore_case_get +from catalog.packages.const import PKG_STATUS logger = logging.getLogger(__name__) @@ -32,17 +33,17 @@ def create(data): user_defined_data = ignore_case_get(data, 'userDefinedData') data = { 'id': str(uuid.uuid4()), - 'pnfdOnboardingState': 'CREATED', - 'pnfdUsageState': 'NOT_IN_USE', + 'pnfdOnboardingState': PKG_STATUS.CREATED, + 'pnfdUsageState': PKG_STATUS.NOT_IN_USE, 'userDefinedData': user_defined_data, '_links': None # TODO } - PnfPackageModel( + PnfPackageModel.objects.create( pnfPackageId=data['id'], onboardingState=data['pnfdOnboardingState'], usageState=data['pnfdUsageState'], userDefinedData=data['userDefinedData'] - ).save() + ) logger.info('A PNFD(%s) has been created.' % data['id']) return data @@ -71,7 +72,7 @@ def upload(remote_file, pnfd_info_id): logger.info('PNFD(%s) does not exist.' % pnfd_info_id) raise CatalogException('PNFD (%s) does not exist.' % pnfd_info_id) - pnf_pkgs.update(onboardingState='UPLOADING') + pnf_pkgs.update(onboardingState=PKG_STATUS.UPLOADING) local_file_name = remote_file.name local_file_dir = os.path.join(CATALOG_ROOT_PATH, pnfd_info_id) local_file_name = os.path.join(local_file_dir, local_file_name) @@ -87,7 +88,7 @@ def upload(remote_file, pnfd_info_id): def parse_pnfd_and_save(pnfd_info_id, local_file_name): logger.info('Start to process PNFD(%s)...' % pnfd_info_id) pnf_pkgs = PnfPackageModel.objects.filter(pnfPackageId=pnfd_info_id) - pnf_pkgs.update(onboardingState='PROCESSING') + pnf_pkgs.update(onboardingState=PKG_STATUS.PROCESSING) pnfd_json = toscaparser.parse_pnfd(local_file_name) pnfd = json.JSONDecoder().decode(pnfd_json) @@ -101,8 +102,8 @@ def parse_pnfd_and_save(pnfd_info_id, local_file_name): pnfdId=pnfd_id, pnfdVersion=pnfd["metadata"].get("version", "undefined"), pnfPackageUri=local_file_name, - onboardingState="ONBOARDED", - usageState="NOT_IN_USE", + onboardingState=PKG_STATUS.ONBOARDED, + usageState=PKG_STATUS.NOT_IN_USE, localFilePath=local_file_name, pnfdModel=pnfd_json ) @@ -115,7 +116,7 @@ def download(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 != 'ONBOARDED': + 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 @@ -132,7 +133,7 @@ def delete_single(pnfd_info_id): logger.info('PNFD(%s) has been deleted.' % pnfd_info_id) return ''' - if pnf_pkgs[0].usageState != 'NOT_IN_USE': + 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) ''' @@ -177,4 +178,4 @@ def fill_response_data(pnf_pkg): def handle_upload_failed(pnf_pkg_id): pnf_pkg = PnfPackageModel.objects.filter(pnfPackageId=pnf_pkg_id) - pnf_pkg.update(onboardingState="CREATED") + pnf_pkg.update(onboardingState=PKG_STATUS.CREATED) diff --git a/catalog/packages/biz/vnf_package.py b/catalog/packages/biz/vnf_package.py index d1792501..d8fdfde8 100644 --- a/catalog/packages/biz/vnf_package.py +++ b/catalog/packages/biz/vnf_package.py @@ -28,6 +28,7 @@ from catalog.pub.database.models import VnfPackageModel 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.const import PKG_STATUS logger = logging.getLogger(__name__) @@ -38,16 +39,16 @@ def create_vnf_pkg(data): vnf_pkg_id = str(uuid.uuid4()) VnfPackageModel.objects.create( vnfPackageId=vnf_pkg_id, - onboardingState="CREATED", - operationalState="DISABLED", - usageState="NOT_IN_USE", + onboardingState=PKG_STATUS.CREATED, + operationalState=PKG_STATUS.DISABLED, + usageState=PKG_STATUS.NOT_IN_USE, userDefinedData=user_defined_data ) data = { "id": vnf_pkg_id, - "onboardingState": "CREATED", - "operationalState": "DISABLED", - "usageState": "NOT_IN_USE", + "onboardingState": PKG_STATUS.CREATED, + "operationalState": PKG_STATUS.DISABLED, + "usageState": PKG_STATUS.NOT_IN_USE, "userDefinedData": user_defined_data, "_links": None } @@ -76,9 +77,9 @@ def delete_vnf_pkg(vnf_pkg_id): logger.debug('VNF package(%s) is deleted.' % vnf_pkg_id) return ''' - if vnf_pkg[0].operationalState != "DISABLED": + 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 != "NOT_IN_USE": + if vnf_pkg[0].usageState != PKG_STATUS.NOT_IN_USE: raise CatalogException("The VNF package (%s) is in use" % vnf_pkg_id) ''' vnf_pkg.delete() @@ -88,7 +89,7 @@ def delete_vnf_pkg(vnf_pkg_id): def parse_vnfd_and_save(vnf_pkg_id, vnf_pkg_path): vnf_pkg = VnfPackageModel.objects.filter(vnfPackageId=vnf_pkg_id) - vnf_pkg.update(onboardingState="PROCESSING") + vnf_pkg.update(onboardingState=PKG_STATUS.PROCESSING) vnfd_json = toscaparser.parse_vnfd(vnf_pkg_path) vnfd = json.JSONDecoder().decode(vnfd_json) @@ -99,18 +100,18 @@ def parse_vnfd_and_save(vnf_pkg_id, vnf_pkg_path): vnfd_ver = vnfd["metadata"].get("vnfd_version") if not vnfd_ver: vnfd_ver = vnfd["metadata"].get("vnfdVersion", "undefined") - VnfPackageModel( + vnf_pkg.update( vnfPackageId=vnf_pkg_id, vnfdId=vnfd_id, vnfVendor=vnfd["metadata"].get("vendor", "undefined"), vnfdVersion=vnfd_ver, vnfSoftwareVersion=vnfd["metadata"].get("version", "undefined"), vnfdModel=vnfd_json, - onboardingState="ONBOARDED", - operationalState="ENABLED", - usageState="NOT_IN_USE", + onboardingState=PKG_STATUS.ONBOARDED, + operationalState=PKG_STATUS.ENABLED, + usageState=PKG_STATUS.NOT_IN_USE, localFilePath=vnf_pkg_path - ).save() + ) class VnfPkgUploadThread(threading.Thread): @@ -134,9 +135,9 @@ class VnfPkgUploadThread(threading.Thread): def upload_vnf_pkg_from_uri(self): logger.debug("UploadVnf %s" % self.vnf_pkg_id) vnf_pkg = VnfPackageModel.objects.filter(vnfPackageId=self.vnf_pkg_id) - if vnf_pkg[0].onboardingState != "CREATED": + if vnf_pkg[0].onboardingState != PKG_STATUS.CREATED: raise CatalogException("VNF package (%s) is not created" % self.vnf_pkg_id) - vnf_pkg.update(onboardingState="UPLOADING") + vnf_pkg.update(onboardingState=PKG_STATUS.UPLOADING) uri = ignore_case_get(self.data, "addressInformation") upload_path = os.path.join(CATALOG_ROOT_PATH, self.vnf_pkg_id) if not os.path.exists(upload_path): @@ -175,7 +176,7 @@ def fetch_vnf_pkg(request, vnf_pkg_id): nf_pkg = VnfPackageModel.objects.filter(vnfPackageId=vnf_pkg_id) if not nf_pkg.exists(): raise ResourceNotFoundException('VNF package(%s) does not exist.' % vnf_pkg_id) - if nf_pkg[0].onboardingState != "ONBOARDED": + if nf_pkg[0].onboardingState != PKG_STATUS.ONBOARDED: raise CatalogException("VNF package (%s) is not on-boarded" % vnf_pkg_id) file_path = nf_pkg[0].localFilePath file_name = file_path.split('/')[-1] @@ -199,4 +200,4 @@ def fetch_vnf_pkg(request, vnf_pkg_id): def handle_upload_failed(vnf_pkg_id): vnf_pkg = VnfPackageModel.objects.filter(vnfPackageId=vnf_pkg_id) - vnf_pkg.update(onboardingState="CREATED") + vnf_pkg.update(onboardingState=PKG_STATUS.CREATED) diff --git a/catalog/packages/const.py b/catalog/packages/const.py new file mode 100644 index 00000000..9d3f6ebd --- /dev/null +++ b/catalog/packages/const.py @@ -0,0 +1,18 @@ +# Copyright 2018 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. + +from catalog.pub.utils.jobutil import enum + +PKG_STATUS = enum(CREATED="CREATED", UPLOADING="UPLOADING", PROCESSING="PROCESSING", ONBOARDED="ONBOARDED", + IN_USE="IN_USE", NOT_IN_USE="NOT_IN_USE", ENABLED="ENABLED", DISABLED="DISABLED") diff --git a/catalog/packages/serializers/nsd_infos.py b/catalog/packages/serializers/nsd_infos.py index b5055ecf..f8ad029e 100644 --- a/catalog/packages/serializers/nsd_infos.py +++ b/catalog/packages/serializers/nsd_infos.py @@ -1,3 +1,6 @@ +# Copyright 2018 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 # diff --git a/catalog/packages/serializers/pnfd_infos.py b/catalog/packages/serializers/pnfd_infos.py index 26df221c..4509fc2b 100644 --- a/catalog/packages/serializers/pnfd_infos.py +++ b/catalog/packages/serializers/pnfd_infos.py @@ -1,3 +1,6 @@ +# Copyright 2018 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 # diff --git a/catalog/packages/tests/test_ns_descriptor.py b/catalog/packages/tests/test_ns_descriptor.py index 83fbd9ba..e1ea124e 100644 --- a/catalog/packages/tests/test_ns_descriptor.py +++ b/catalog/packages/tests/test_ns_descriptor.py @@ -23,6 +23,7 @@ from rest_framework.test import APIClient from catalog.pub.database.models import NSPackageModel, VnfPackageModel from catalog.pub.config.config import CATALOG_ROOT_PATH from catalog.pub.utils import toscaparser +from catalog.packages.const import PKG_STATUS class TestNsDescriptor(TestCase): @@ -366,7 +367,7 @@ class TestNsDescriptor(TestCase): file_content = '%s%s' % (file_content, data) ns_pkg = NSPackageModel.objects.filter(nsPackageId="22") self.assertEqual("VCPE_NS", ns_pkg[0].nsdId) - self.assertEqual("ONBOARDED", ns_pkg[0].onboardingState) + self.assertEqual(PKG_STATUS.ONBOARDED, ns_pkg[0].onboardingState) self.assertEqual(resp.status_code, status.HTTP_204_NO_CONTENT) self.assertEqual(None, resp.data) self.assertEqual(file_content, 'test') diff --git a/catalog/packages/tests/test_pnf_descriptor.py b/catalog/packages/tests/test_pnf_descriptor.py index 21953a09..a7dcd822 100644 --- a/catalog/packages/tests/test_pnf_descriptor.py +++ b/catalog/packages/tests/test_pnf_descriptor.py @@ -24,6 +24,7 @@ from rest_framework import status from rest_framework.test import APIClient from catalog.pub.database.models import PnfPackageModel from catalog.pub.utils import toscaparser +from catalog.packages.const import PKG_STATUS class TestPnfDescriptor(TestCase): @@ -142,7 +143,7 @@ class TestPnfDescriptor(TestCase): ) pnf_pkg = PnfPackageModel.objects.filter(pnfPackageId="22") self.assertEqual(pnf_pkg[0].pnfdId, "zte-1.0") - self.assertEqual(pnf_pkg[0].onboardingState, "ONBOARDED") + self.assertEqual(pnf_pkg[0].onboardingState, PKG_STATUS.ONBOARDED) self.assertEqual(resp.status_code, status.HTTP_204_NO_CONTENT) self.assertEqual(None, resp.data) os.remove('pnfd_content.txt') diff --git a/catalog/packages/tests/test_vnf_package.py b/catalog/packages/tests/test_vnf_package.py index 10527cc2..05307cb6 100644 --- a/catalog/packages/tests/test_vnf_package.py +++ b/catalog/packages/tests/test_vnf_package.py @@ -24,6 +24,7 @@ from catalog.pub.config.config import CATALOG_ROOT_PATH from catalog.packages.biz.vnf_package import VnfPkgUploadThread from catalog.pub.database.models import VnfPackageModel from catalog.pub.utils import toscaparser +from catalog.packages.const import PKG_STATUS class MockReq(): @@ -254,12 +255,13 @@ class TestVnfPackage(TestCase): ) mock_parse_vnfd.return_value = json.JSONEncoder().encode(self.vnfd_data) response = self.client.put("/api/vnfpkgm/v1/vnf_packages/222/package_content", data=data) - vnf_pkg1 = VnfPackageModel.objects.filter(vnfPackageId="222") - self.assertEqual("zte-hss-1.0", vnf_pkg1[0].vnfdId) + vnf_pkg = VnfPackageModel.objects.filter(vnfPackageId="222") + self.assertEqual("zte-hss-1.0", vnf_pkg[0].vnfdId) + self.assertEqual(PKG_STATUS.ONBOARDED, vnf_pkg[0].onboardingState) self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED) - os.remove(vnf_pkg1[0].localFilePath) - os.removedirs(os.path.join(CATALOG_ROOT_PATH, vnf_pkg1[0].vnfPackageId)) + os.remove(vnf_pkg[0].localFilePath) + os.removedirs(os.path.join(CATALOG_ROOT_PATH, vnf_pkg[0].vnfPackageId)) @mock.patch.object(toscaparser, 'parse_vnfd') @mock.patch.object(urllib2, 'urlopen') diff --git a/catalog/packages/views/vnf_package_views.py b/catalog/packages/views/vnf_package_views.py index b4488713..28c680a3 100644 --- a/catalog/packages/views/vnf_package_views.py +++ b/catalog/packages/views/vnf_package_views.py @@ -30,6 +30,7 @@ from catalog.packages.biz.vnf_package import create_vnf_pkg, query_multiple, Vnf query_single, delete_vnf_pkg, parse_vnfd_and_save, fetch_vnf_pkg, handle_upload_failed from catalog.pub.database.models import VnfPackageModel from catalog.packages.views.ns_descriptor_views import validate_data +from catalog.packages.const import PKG_STATUS logger = logging.getLogger(__name__) @@ -111,7 +112,7 @@ def upload_vnf_pkg_content(request, vnfPkgId): logger.debug("Upload VNF package %s" % vnfPkgId) try: vnf_pkg = VnfPackageModel.objects.filter(vnfPackageId=vnfPkgId) - if vnf_pkg[0].onboardingState != "CREATED": + if vnf_pkg[0].onboardingState != PKG_STATUS.CREATED: raise CatalogException("VNF package (%s) is not created" % vnfPkgId) file_object = request.FILES.get('file') upload_path = os.path.join(CATALOG_ROOT_PATH, vnfPkgId) |