diff options
author | biancunkang <bian.cunkang@zte.com.cn> | 2018-08-24 16:27:43 +0800 |
---|---|---|
committer | Cunkang Bian <bian.cunkang@zte.com.cn> | 2018-08-25 08:47:55 +0000 |
commit | 3fc3bae4484a8001c9a10ea69cda84b8e72f3d79 (patch) | |
tree | fd95f3e611e5bebec177e49c95209dc29fdcddce | |
parent | 91ea3262f96ff2e73c19f002acb565a13226ee04 (diff) |
Deal with nfPackage
Modify upload views
Change-Id: Ie5d6e7679b7050d58884ccfebc364f37b710c5fb
Issue-ID: VFC-1038
Signed-off-by: biancunkang <bian.cunkang@zte.com.cn>
-rw-r--r-- | catalog/packages/biz/vnf_package.py | 9 | ||||
-rw-r--r-- | catalog/packages/tests/test_vnf_package.py | 19 | ||||
-rw-r--r-- | catalog/packages/views/vnf_package_views.py | 19 |
3 files changed, 34 insertions, 13 deletions
diff --git a/catalog/packages/biz/vnf_package.py b/catalog/packages/biz/vnf_package.py index e9eef520..d601e696 100644 --- a/catalog/packages/biz/vnf_package.py +++ b/catalog/packages/biz/vnf_package.py @@ -121,7 +121,8 @@ def parse_vnfd_and_save(vnf_pkg_id, vnf_pkg_path): vnfdModel=vnfd_json, onboardingState="ONBOARDED", operationalState="ENABLED", - usageState="NOT_IN_USE" + usageState="NOT_IN_USE", + localFilePath=vnf_pkg_path ).save() @@ -130,10 +131,12 @@ class VnfPkgUploadThread(threading.Thread): threading.Thread.__init__(self) self.vnf_pkg_id = vnf_pkg_id self.data = data + self.upload_file_name = None def run(self): try: self.upload_vnf_pkg_from_uri() + parse_vnfd_and_save(self.vnf_pkg_id, self.upload_file_name) except CatalogException as e: logger.error(e.message) except Exception as e: @@ -153,8 +156,8 @@ class VnfPkgUploadThread(threading.Thread): r = urllib2.Request(uri) req = urllib2.urlopen(r) - upload_file_name = os.path.join(upload_path, os.path.basename(uri)) - save_file = open(upload_file_name, "wb") + self.upload_file_name = os.path.join(upload_path, os.path.basename(uri)) + save_file = open(self.upload_file_name, "wb") save_file.write(req.read()) save_file.close() req.close() diff --git a/catalog/packages/tests/test_vnf_package.py b/catalog/packages/tests/test_vnf_package.py index f01ff7f2..7191ba87 100644 --- a/catalog/packages/tests/test_vnf_package.py +++ b/catalog/packages/tests/test_vnf_package.py @@ -23,6 +23,7 @@ from rest_framework import status 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 class MockReq(): @@ -244,25 +245,39 @@ class TestVnfPackage(TestCase): def tearDown(self): pass - def test_upload_vnf_pkg(self): + @mock.patch.object(toscaparser, 'parse_vnfd') + def test_upload_vnf_pkg(self, mock_parse_vnfd): data = {'file': open(os.path.join(CATALOG_ROOT_PATH, "empty.txt"), "rb")} VnfPackageModel.objects.create( vnfPackageId="222", onboardingState="CREATED" ) + 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) 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)) + + @mock.patch.object(toscaparser, 'parse_vnfd') @mock.patch.object(urllib2, 'urlopen') - def test_upload_nf_pkg_from_uri(self, mock_urlopen): + def test_upload_nf_pkg_from_uri(self, mock_urlopen, mock_parse_vnfd): vnf_pkg = VnfPackageModel.objects.create( vnfPackageId="222", onboardingState="CREATED" ) + mock_parse_vnfd.return_value = json.JSONEncoder().encode(self.vnfd_data) req_data = {"addressInformation": "https://127.0.0.1:1234/sdc/v1/hss.csar"} mock_urlopen.return_value = MockReq() vnf_pkg_id = vnf_pkg.vnfPackageId VnfPkgUploadThread(req_data, vnf_pkg_id).run() + vnf_pkg1 = VnfPackageModel.objects.filter(vnfPackageId="222") + self.assertEqual("zte-hss-1.0", vnf_pkg1[0].vnfdId) + + os.remove(vnf_pkg1[0].localFilePath) + os.removedirs(os.path.join(CATALOG_ROOT_PATH, vnf_pkg1[0].vnfPackageId)) def test_create_vnf_pkg(self): req_data = { diff --git a/catalog/packages/views/vnf_package_views.py b/catalog/packages/views/vnf_package_views.py index 4bc2fd6a..7defc347 100644 --- a/catalog/packages/views/vnf_package_views.py +++ b/catalog/packages/views/vnf_package_views.py @@ -27,7 +27,7 @@ from catalog.packages.serializers.create_vnf_pkg_info_req import CreateVnfPkgInf from catalog.packages.serializers.vnf_pkg_info import VnfPkgInfoSerializer from catalog.packages.serializers.vnf_pkg_infos import VnfPkgInfosSerializer from catalog.packages.biz.vnf_package import create_vnf_pkg, query_multiple, VnfPkgUploadThread, \ - query_single, delete_vnf_pkg + query_single, delete_vnf_pkg, parse_vnfd_and_save from catalog.pub.database.models import VnfPackageModel logger = logging.getLogger(__name__) @@ -101,18 +101,21 @@ def vnf_packages_rc(request): @api_view(http_method_names=['PUT']) def upload_vnf_pkg_content(request, vnfPkgId): logger.debug("Upload VNF package %s" % vnfPkgId) - vnf_pkg = VnfPackageModel.objects.filter(vnfPackageId=vnfPkgId) - if vnf_pkg[0].onboardingState != "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) - if not os.path.exists(upload_path): - os.makedirs(upload_path, 0o777) try: + vnf_pkg = VnfPackageModel.objects.filter(vnfPackageId=vnfPkgId) + if vnf_pkg[0].onboardingState != "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) + if not os.path.exists(upload_path): + os.makedirs(upload_path, 0o777) + upload_file_name = os.path.join(upload_path, file_object.name) with open(upload_file_name, 'wb+') as dest_file: for chunk in file_object.chunks(): dest_file.write(chunk) + + parse_vnfd_and_save(vnfPkgId, upload_file_name) return Response(None, status=status.HTTP_202_ACCEPTED) except CatalogException: logger.error(traceback.format_exc()) |