summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--catalog/packages/biz/common.py9
-rw-r--r--catalog/packages/biz/ns_descriptor.py8
-rw-r--r--catalog/packages/biz/vnf_package.py41
-rw-r--r--catalog/packages/tests/test_vnf_package.py8
-rw-r--r--catalog/packages/urls.py9
-rw-r--r--catalog/packages/views/ns_descriptor_views.py16
-rw-r--r--catalog/packages/views/vnf_package_views.py122
7 files changed, 103 insertions, 110 deletions
diff --git a/catalog/packages/biz/common.py b/catalog/packages/biz/common.py
index deef071e..f2b37795 100644
--- a/catalog/packages/biz/common.py
+++ b/catalog/packages/biz/common.py
@@ -40,3 +40,12 @@ def read(file_path, start, end):
yield fp.read(CHUNK_SIZE)
pos = fp.tell()
yield fp.read(end - pos)
+
+
+def parse_file_range(file_path, file_range):
+ start, end = 0, os.path.getsize(file_path)
+ if file_range:
+ [start, end] = file_range.split('-')
+ start, end = start.strip(), end.strip()
+ start, end = int(start), int(end)
+ return start, end
diff --git a/catalog/packages/biz/ns_descriptor.py b/catalog/packages/biz/ns_descriptor.py
index 21b57353..8f10b114 100644
--- a/catalog/packages/biz/ns_descriptor.py
+++ b/catalog/packages/biz/ns_descriptor.py
@@ -18,7 +18,7 @@ import logging
import os
import uuid
-from catalog.packages.biz.common import read, save
+from catalog.packages.biz.common import parse_file_range, read, save
from catalog.packages.const import PKG_STATUS
from catalog.pub.config.config import CATALOG_ROOT_PATH
from catalog.pub.database.models import NSPackageModel, PnfPackageModel, VnfPackageModel
@@ -112,11 +112,7 @@ class NsDescriptor(object):
raise CatalogException('NSD(%s) is not ONBOARDED.' % nsd_info_id)
local_file_path = ns_pkgs[0].localFilePath
- start, end = 0, os.path.getsize(local_file_path)
- if file_range:
- [start, end] = file_range.split('-')
- start, end = start.strip(), end.strip()
- start, end = int(start), int(end)
+ start, end = parse_file_range(local_file_path, file_range)
logger.info('NSD(%s) has been downloaded.' % nsd_info_id)
return read(local_file_path, start, end)
diff --git a/catalog/packages/biz/vnf_package.py b/catalog/packages/biz/vnf_package.py
index bb830926..51ab635e 100644
--- a/catalog/packages/biz/vnf_package.py
+++ b/catalog/packages/biz/vnf_package.py
@@ -21,8 +21,7 @@ import traceback
import urllib2
import uuid
-from rest_framework import status
-from django.http import StreamingHttpResponse
+from catalog.packages.biz.common import parse_file_range, read, save
from catalog.pub.config.config import CATALOG_ROOT_PATH
from catalog.pub.database.models import VnfPackageModel
from catalog.pub.exceptions import CatalogException, ResourceNotFoundException
@@ -88,30 +87,30 @@ class VnfPackage(object):
vnf_pkg_path = os.path.join(CATALOG_ROOT_PATH, vnf_pkg_id)
fileutil.delete_dirs(vnf_pkg_path)
- def fetch_vnf_pkg(self, request, vnf_pkg_id):
+ def upload(self, vnf_pkg_id, remote_file):
+ logger.info('Start to upload VNF package(%s)...' % vnf_pkg_id)
+ vnf_pkg = VnfPackageModel.objects.filter(vnfPackageId=vnf_pkg_id)
+ if vnf_pkg[0].onboardingState != PKG_STATUS.CREATED:
+ raise CatalogException("VNF package(%s) is not CREATED" % vnf_pkg_id)
+ vnf_pkg.update(onboardingState=PKG_STATUS.UPLOADING)
+
+ local_file_name = save(remote_file, vnf_pkg_id)
+ logger.info('VNF package(%s) has been uploaded.' % vnf_pkg_id)
+ return local_file_name
+
+ def download(self, vnf_pkg_id, file_range):
+ logger.info('Start to download VNF package(%s)...' % vnf_pkg_id)
nf_pkg = VnfPackageModel.objects.filter(vnfPackageId=vnf_pkg_id)
if not nf_pkg.exists():
+ logger.error('VNF package(%s) does not exist.' % vnf_pkg_id)
raise ResourceNotFoundException('VNF package(%s) does not exist.' % vnf_pkg_id)
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]
- file_name = file_name.split('\\')[-1]
- file_range = request.META.get('RANGE')
- if file_range:
- start_end = file_range.split('-')
- start = int(start_end[0])
- end = int(start_end[1])
- f = open(file_path, "rb")
- f.seek(start, 0)
- fs = f.read(end - start + 1)
- response = StreamingHttpResponse(fs, status=status.HTTP_200_OK)
- response['Content-Range'] = file_range
- else:
- response = StreamingHttpResponse(open(file_path, 'rb'), status=status.HTTP_200_OK)
- response['Content-Type'] = 'application/octet-stream'
- response['Content-Disposition'] = 'attachment; filename=%s' % file_name.encode('utf-8')
- return response
+
+ local_file_path = nf_pkg[0].localFilePath
+ start, end = parse_file_range(local_file_path, file_range)
+ logger.info('VNF package (%s) has been downloaded.' % vnf_pkg_id)
+ return read(local_file_path, start, end)
class VnfPkgUploadThread(threading.Thread):
diff --git a/catalog/packages/tests/test_vnf_package.py b/catalog/packages/tests/test_vnf_package.py
index ef2dac49..701a22ed 100644
--- a/catalog/packages/tests/test_vnf_package.py
+++ b/catalog/packages/tests/test_vnf_package.py
@@ -255,7 +255,7 @@ class TestVnfPackage(TestCase):
for data in response.streaming_content:
partial_file_content = partial_file_content + data
self.assertEqual(response.status_code, status.HTTP_200_OK)
- self.assertEqual('BBBB', partial_file_content)
+ self.assertEqual('BBB', partial_file_content)
os.remove("vnfPackage.csar")
def test_fetch_vnf_pkg_when_pkg_not_exist(self):
@@ -316,8 +316,8 @@ class TestVnfPackage(TestCase):
response = self.client.post("/api/vnfpkgm/v1/vnf_packages/111/package_content/upload_from_uri", data=req_data)
self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
- @mock.patch.object(VnfPackage, 'fetch_vnf_pkg')
- def test_fetch_vnf_pkg_when_catch_exception(self, mock_fetch_vnf_pkg):
- mock_fetch_vnf_pkg.side_effect = TypeError("integer type")
+ @mock.patch.object(VnfPackage, 'download')
+ def test_fetch_vnf_pkg_when_catch_exception(self, mock_download):
+ mock_download.side_effect = TypeError("integer type")
response = self.client.get("/api/vnfpkgm/v1/vnf_packages/222/package_content")
self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
diff --git a/catalog/packages/urls.py b/catalog/packages/urls.py
index 28a60145..883c3749 100644
--- a/catalog/packages/urls.py
+++ b/catalog/packages/urls.py
@@ -41,12 +41,9 @@ urlpatterns = [
# TODO SOL005 & SOL003
url(r'^api/vnfpkgm/v1/vnf_packages$', vnf_package_views.vnf_packages_rc, name='vnf_packages_rc'),
url(r'^api/vnfpkgm/v1/vnf_packages/(?P<vnfPkgId>[0-9a-zA-Z\-\_]+)$', vnf_package_views.vnf_package_rd, name='vnf_package_rd'),
- # url(r'^api/vnfpkgm/v1/vnf_packages/(?P<vnfPkgId>[0-9a-zA-Z\-\_]+)/vnfd$', vnfd.as_view(), name='vnfd_r'),
- url(r'^api/vnfpkgm/v1/vnf_packages/(?P<vnfPkgId>[0-9a-zA-Z\-\_]+)/package_content$',
- vnf_package_views.upload_vnf_pkg_content, name='package_content_ru'),
- url(r'^api/vnfpkgm/v1/vnf_packages/(?P<vnfPkgId>[0-9a-zA-Z\-\_]+)/package_content/upload_from_uri$',
- vnf_package_views.upload_vnf_pkg_from_uri, name='upload_from_uri_c'),
- # url(r'^api/vnfpkgm/v1/vnf_packages/(?P<vnfPkgId>[0-9a-zA-Z\-\_]+)/artifacts/artifactPath$', artifacts.as_view(), name='artifacts_r'),
+ url(r'^api/vnfpkgm/v1/vnf_packages/(?P<vnfPkgId>[0-9a-zA-Z\-\_]+)/package_content$', vnf_package_views.package_content_ru, name='package_content_ru'),
+ url(r'^api/vnfpkgm/v1/vnf_packages/(?P<vnfPkgId>[0-9a-zA-Z\-\_]+)/package_content/upload_from_uri$', vnf_package_views.upload_from_uri_c, name='upload_from_uri_c'),
+ # url(r'^api/vnfpkgm/v1/vnf_packages/(?P<vnfPkgId>[0-9a-zA-Z\-\_]+)/vnfd$', vnfd.as_view(), name='vnfd_r'),# url(r'^api/vnfpkgm/v1/vnf_packages/(?P<vnfPkgId>[0-9a-zA-Z\-\_]+)/artifacts/artifactPath$', artifacts.as_view(), name='artifacts_r'),
# url(r'^api/vnfpkgm/v1/subscriptions', vnfpkg_subscriptions.as_view(), name='subscriptions_rc'),
# url(r'^api/vnfpkgm/v1/subscriptions/(?P<subscriptionId>[0-9a-zA-Z\-\_]+)$', vnfpkg_subscription.as_view(), name='subscription_rd'),
]
diff --git a/catalog/packages/views/ns_descriptor_views.py b/catalog/packages/views/ns_descriptor_views.py
index aa68accf..d47fb176 100644
--- a/catalog/packages/views/ns_descriptor_views.py
+++ b/catalog/packages/views/ns_descriptor_views.py
@@ -76,8 +76,8 @@ def ns_info_rd(request, **kwargs):
except Exception as e:
logger.error(e.message)
logger.error(traceback.format_exc())
- error_msg = {'error': 'Deletion of NSD(%s) failed.' % nsd_info_id}
- return Response(data=error_msg, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+ error_data = {'error': 'Deletion of NSD(%s) failed.' % nsd_info_id}
+ return Response(data=error_data, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
@swagger_auto_schema(
@@ -102,15 +102,15 @@ def ns_info_rd(request, **kwargs):
def ns_descriptors_rc(request):
if request.method == 'POST':
try:
- create_nsd_info_requst = validate_data(request.data, CreateNsdInfoRequestSerializer)
- data = NsDescriptor().create(create_nsd_info_requst.data)
+ create_nsd_info_request = validate_data(request.data, CreateNsdInfoRequestSerializer)
+ data = NsDescriptor().create(create_nsd_info_request.data)
nsd_info = validate_data(data, NsdInfoSerializer)
return Response(data=nsd_info.data, status=status.HTTP_201_CREATED)
except Exception as e:
logger.error(e.message)
logger.error(traceback.format_exc())
- error_msg = {'error': 'Creating a NSD failed.'}
- return Response(data=error_msg, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+ error_data = {'error': 'Creating a NSD failed.'}
+ return Response(data=error_data, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
if request.method == 'GET':
try:
@@ -120,8 +120,8 @@ def ns_descriptors_rc(request):
except Exception as e:
logger.error(e.message)
logger.error(traceback.format_exc())
- error_msg = {'error': 'Query of multiple NSDs failed.'}
- return Response(data=error_msg, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+ error_data = {'error': 'Query of multiple NSDs failed.'}
+ return Response(data=error_data, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
@swagger_auto_schema(
diff --git a/catalog/packages/views/vnf_package_views.py b/catalog/packages/views/vnf_package_views.py
index d77f02eb..01eb5be2 100644
--- a/catalog/packages/views/vnf_package_views.py
+++ b/catalog/packages/views/vnf_package_views.py
@@ -14,9 +14,8 @@
import traceback
import logging
-import os
-from catalog.pub.config.config import CATALOG_ROOT_PATH
+from django.http import StreamingHttpResponse
from drf_yasg.utils import swagger_auto_schema, no_body
from rest_framework import status
from rest_framework.decorators import api_view
@@ -27,9 +26,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 VnfPackage, VnfPkgUploadThread, parse_vnfd_and_save, 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
+from catalog.packages.views.common import validate_data
logger = logging.getLogger(__name__)
@@ -57,27 +54,27 @@ def vnf_packages_rc(request):
if request.method == 'GET':
logger.debug("Query VNF packages> %s" % request.data)
try:
- res = VnfPackage().query_multiple()
- query_serializer = validate_data(res, VnfPkgInfosSerializer)
- return Response(data=query_serializer.data, status=status.HTTP_200_OK)
+ data = VnfPackage().query_multiple()
+ vnf_pkg_infos = validate_data(data, VnfPkgInfosSerializer)
+ return Response(data=vnf_pkg_infos.data, status=status.HTTP_200_OK)
except Exception as e:
logger.error(e.message)
logger.error(traceback.format_exc())
- error_msg = {'error': 'unexpected exception'}
- return Response(data=error_msg, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+ error_data = {'error': 'Query of multiple VNF packages failed'}
+ return Response(data=error_data, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
if request.method == 'POST':
logger.debug("Create VNF package> %s" % request.data)
try:
- req_serializer = validate_data(request.data, CreateVnfPkgInfoRequestSerializer)
- res = VnfPackage().create_vnf_pkg(req_serializer.data)
- create_vnf_pkg_resp_serializer = validate_data(res, VnfPkgInfoSerializer)
- return Response(data=create_vnf_pkg_resp_serializer.data, status=status.HTTP_201_CREATED)
+ create_vnf_pkg_info_request = validate_data(request.data, CreateVnfPkgInfoRequestSerializer)
+ data = VnfPackage().create_vnf_pkg(create_vnf_pkg_info_request.data)
+ vnf_pkg_info = validate_data(data, VnfPkgInfoSerializer)
+ return Response(data=vnf_pkg_info.data, status=status.HTTP_201_CREATED)
except Exception as e:
logger.error(e.message)
logger.error(traceback.format_exc())
- error_msg = {'error': 'unexpected exception'}
- return Response(data=error_msg, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+ error_data = {'error': 'Creating a VNF Package failed'}
+ return Response(data=error_data, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
@swagger_auto_schema(
@@ -100,51 +97,45 @@ def vnf_packages_rc(request):
}
)
@api_view(http_method_names=["PUT", "GET"])
-def upload_vnf_pkg_content(request, vnfPkgId):
+def package_content_ru(request, **kwargs):
+ vnf_pkg_id = kwargs.get("vnfPkgId")
if request.method == "PUT":
- logger.debug("Upload VNF package %s" % vnfPkgId)
+ logger.debug("Upload VNF package %s" % vnf_pkg_id)
+ files = request.FILES.getlist('file')
try:
- vnf_pkg = VnfPackageModel.objects.filter(vnfPackageId=vnfPkgId)
- 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)
- 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)
+ local_file_name = VnfPackage().upload(vnf_pkg_id, files[0])
+ parse_vnfd_and_save(vnf_pkg_id, local_file_name)
return Response(None, status=status.HTTP_202_ACCEPTED)
except CatalogException as e:
- handle_upload_failed(vnfPkgId)
- logger.error(e.message)
- error_msg = {'error': 'Upload VNF package failed.'}
+ handle_upload_failed(vnf_pkg_id)
+ logger.error(e.message)
+ error_data = {'error': e.message}
except Exception as e:
- handle_upload_failed(vnfPkgId)
+ handle_upload_failed(vnf_pkg_id)
logger.error(e.message)
logger.error(traceback.format_exc())
- error_msg = {'error': 'unexpected exception'}
- return Response(data=error_msg, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+ error_data = {'error': 'Uploading VNF package(%s) failed.' % vnf_pkg_id}
+ return Response(data=error_data, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
if request.method == "GET":
try:
- response = VnfPackage().fetch_vnf_pkg(request, vnfPkgId)
- return response
+ file_range = request.META.get('RANGE')
+ file_iterator = VnfPackage().download(vnf_pkg_id, file_range)
+ return StreamingHttpResponse(file_iterator, status=status.HTTP_200_OK)
except ResourceNotFoundException as e:
logger.error(e.message)
- return Response(data={'error': "VNF package does not exist"}, status=status.HTTP_404_NOT_FOUND)
+ error_data = {'error': e.message}
+ error_code = status.HTTP_404_NOT_FOUND
except CatalogException as e:
logger.error(e.message)
- error_msg = {'error': 'Fetch VNF package failed.'}
+ error_data = {'error': e.message}
+ error_code = status.HTTP_500_INTERNAL_SERVER_ERROR
except Exception as e:
logger.error(e.message)
logger.error(traceback.format_exc())
- error_msg = {'error': 'unexpected exception'}
- return Response(data=error_msg, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+ error_data = {'error': 'Downloading VNF package(%s) failed.' % vnf_pkg_id}
+ error_code = status.HTTP_500_INTERNAL_SERVER_ERROR
+ return Response(data=error_data, status=error_code)
@swagger_auto_schema(
@@ -157,21 +148,22 @@ def upload_vnf_pkg_content(request, vnfPkgId):
}
)
@api_view(http_method_names=['POST'])
-def upload_vnf_pkg_from_uri(request, vnfPkgId):
+def upload_from_uri_c(request, **kwargs):
+ vnf_pkg_id = kwargs.get("vnfPkgId")
try:
- req_serializer = validate_data(request.data, UploadVnfPackageFromUriRequestSerializer)
- VnfPkgUploadThread(req_serializer.data, vnfPkgId).start()
+ upload_vnf_from_uri_request = validate_data(request.data, UploadVnfPackageFromUriRequestSerializer)
+ VnfPkgUploadThread(upload_vnf_from_uri_request.data, vnf_pkg_id).start()
return Response(None, status=status.HTTP_202_ACCEPTED)
except CatalogException as e:
- handle_upload_failed(vnfPkgId)
+ handle_upload_failed(vnf_pkg_id)
logger.error(e.message)
- error_msg = {'error': 'Upload VNF package failed.'}
+ error_data = {'error': e.message}
except Exception as e:
- handle_upload_failed(vnfPkgId)
+ handle_upload_failed(vnf_pkg_id)
logger.error(e.message)
logger.error(traceback.format_exc())
- error_msg = {'error': 'unexpected exception'}
- return Response(data=error_msg, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+ error_data = {'error': 'Uploading VNF package(%s) from URI failed.' % vnf_pkg_id}
+ return Response(data=error_data, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
@swagger_auto_schema(
@@ -194,32 +186,32 @@ def upload_vnf_pkg_from_uri(request, vnfPkgId):
}
)
@api_view(http_method_names=['GET', 'DELETE'])
-def vnf_package_rd(request, vnfPkgId):
+def vnf_package_rd(request, **kwargs):
+ vnf_pkg_id = kwargs.get("vnfPkgId")
if request.method == 'GET':
logger.debug("Query an individual VNF package> %s" % request.data)
try:
- res = VnfPackage().query_single(vnfPkgId)
- query_serializer = validate_data(res, VnfPkgInfoSerializer)
- return Response(data=query_serializer.data, status=status.HTTP_200_OK)
+ data = VnfPackage().query_single(vnf_pkg_id)
+ vnf_pkg_info = validate_data(data, VnfPkgInfoSerializer)
+ return Response(data=vnf_pkg_info.data, status=status.HTTP_200_OK)
except ResourceNotFoundException as e:
logger.error(e.message)
- return Response(data={'error': "VNF package does not exist"}, status=status.HTTP_404_NOT_FOUND)
+ error_data = {'error': e.message}
+ error_code = status.HTTP_404_NOT_FOUND
except Exception as e:
logger.error(e.message)
logger.error(traceback.format_exc())
- error_msg = {'error': 'unexpected exception'}
- return Response(data=error_msg, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+ error_data = {'error': 'Query of VNF package(%s) failed.' % vnf_pkg_id}
+ error_code = status.HTTP_500_INTERNAL_SERVER_ERROR
+ return Response(data=error_data, status=error_code)
if request.method == 'DELETE':
logger.debug("Delete an individual VNF package> %s" % request.data)
try:
- VnfPackage().delete_vnf_pkg(vnfPkgId)
+ VnfPackage().delete_vnf_pkg(vnf_pkg_id)
return Response(data=None, status=status.HTTP_204_NO_CONTENT)
- except CatalogException as e:
- logger.error(e.message)
- error_msg = {'error': 'Delete an individual VNF package failed.'}
except Exception as e:
logger.error(e.message)
logger.error(traceback.format_exc())
- error_msg = {'error': 'unexpected exception'}
- return Response(data=error_msg, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+ error_data = {'error': 'Deletion of VNF package(%s) failed.' % vnf_pkg_id}
+ return Response(data=error_data, status=status.HTTP_500_INTERNAL_SERVER_ERROR)