Ns descriptor related stuffs. 59/63959/1
authorlaili <lai.li@zte.com.cn>
Fri, 31 Aug 2018 09:54:12 +0000 (17:54 +0800)
committerlaili <lai.li@zte.com.cn>
Fri, 31 Aug 2018 09:54:30 +0000 (17:54 +0800)
Refactor and add a test.

Change-Id: Ic32fe45fd70a1c74e165b890584034df3e723606
Issue-ID: VFC-1037
Signed-off-by: laili <lai.li@zte.com.cn>
catalog/packages/biz/common.py [new file with mode: 0644]
catalog/packages/biz/ns_descriptor.py
catalog/packages/biz/pnf_descriptor.py
catalog/packages/tests/test_pnf_descriptor.py

diff --git a/catalog/packages/biz/common.py b/catalog/packages/biz/common.py
new file mode 100644 (file)
index 0000000..b2fcee5
--- /dev/null
@@ -0,0 +1,30 @@
+# 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.
+
+import os
+
+from catalog.pub.config.config import CATALOG_ROOT_PATH
+from catalog.pub.utils import fileutil
+
+
+def save(remote_file, descriptor_id):
+    local_file_name = remote_file.name
+    local_file_dir = os.path.join(CATALOG_ROOT_PATH, descriptor_id)
+    local_file_name = os.path.join(local_file_dir, local_file_name)
+    if not os.path.exists(local_file_dir):
+        fileutil.make_dirs(local_file_dir)
+    with open(local_file_name, 'wb') as local_file:
+        for chunk in remote_file.chunks(chunk_size=1024 * 8):
+            local_file.write(chunk)
+    return local_file_name
index 099a700..8a89099 100644 (file)
@@ -24,6 +24,7 @@ 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
+from catalog.packages.biz.common import save
 
 logger = logging.getLogger(__name__)
 
@@ -94,16 +95,9 @@ class NsDescriptor(object):
         if not ns_pkgs.exists():
             logger.info('NSD(%s) does not exist.' % nsd_info_id)
             raise CatalogException('NSD(%s) does not exist.' % nsd_info_id)
-
         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)
-        if not os.path.exists(local_file_dir):
-            fileutil.make_dirs(local_file_dir)
-        with open(local_file_name, 'wb') as local_file:
-            for chunk in remote_file.chunks(chunk_size=1024 * 8):
-                local_file.write(chunk)
+
+        local_file_name = save(remote_file, nsd_info_id)
         logger.info('NSD(%s) content has been uploaded.' % nsd_info_id)
         return local_file_name
 
index 188bc70..8caf98b 100644 (file)
@@ -24,6 +24,7 @@ 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
+from catalog.packages.biz.common import save
 
 logger = logging.getLogger(__name__)
 
@@ -73,16 +74,9 @@ class PnfPackage(object):
         if not pnf_pkgs.exists():
             logger.info('PNFD(%s) does not exist.' % pnfd_info_id)
             raise CatalogException('PNFD (%s) does not exist.' % pnfd_info_id)
-
         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)
-        if not os.path.exists(local_file_dir):
-            fileutil.make_dirs(local_file_dir)
-        with open(local_file_name, 'wb') as local_file:
-            for chunk in remote_file.chunks(chunk_size=1024 * 8):
-                local_file.write(chunk)
+
+        local_file_name = save(remote_file, pnfd_info_id)
         logger.info('PNFD(%s) content has been uploaded.' % pnfd_info_id)
         return local_file_name
 
@@ -137,7 +131,7 @@ 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=PKG_STATUS.PROCESSING)
-
+    PnfPackageModel
     pnfd_json = toscaparser.parse_pnfd(local_file_name)
     pnfd = json.JSONDecoder().decode(pnfd_json)
 
index 60460df..fc60711 100644 (file)
 
 import copy
 import json
-import os
 import mock
+import os
 
 
 from django.test import TestCase
 from rest_framework import status
 from rest_framework.test import APIClient
-from catalog.pub.database.models import PnfPackageModel, NSPackageModel
-from catalog.pub.utils import toscaparser
+
+from catalog.packages.biz.pnf_descriptor import PnfPackage
 from catalog.packages.const import PKG_STATUS
 from catalog.packages.tests.const import pnfd_data
-from catalog.packages.biz.pnf_descriptor import PnfPackage
+from catalog.pub.database.models import PnfPackageModel, NSPackageModel
+from catalog.pub.utils import toscaparser
 
 
 class TestPnfDescriptor(TestCase):
@@ -136,6 +137,11 @@ class TestPnfDescriptor(TestCase):
         self.assertEqual(resp.status_code, status.HTTP_204_NO_CONTENT)
         self.assertEqual(None, resp.data)
 
+    def test_delete_single_pnfd_when_not_exist(self):
+        resp = self.client.delete("/api/nsd/v1/pnf_descriptors/22", format='json')
+        self.assertEqual(resp.status_code, status.HTTP_204_NO_CONTENT)
+        self.assertEqual(None, resp.data)
+
     @mock.patch.object(toscaparser, "parse_pnfd")
     def test_pnfd_content_upload_normal(self, mock_parse_pnfd):
         user_defined_data_json = json.JSONEncoder().encode(self.user_defined_data)