summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--catalog/packages/biz/ns_descriptor.py184
-rw-r--r--catalog/packages/biz/vnf_package.py4
-rw-r--r--catalog/packages/tests/test_ns_descriptor.py39
-rw-r--r--catalog/packages/views/ns_descriptor_views.py15
4 files changed, 138 insertions, 104 deletions
diff --git a/catalog/packages/biz/ns_descriptor.py b/catalog/packages/biz/ns_descriptor.py
index 56136d7c..099a7006 100644
--- a/catalog/packages/biz/ns_descriptor.py
+++ b/catalog/packages/biz/ns_descriptor.py
@@ -28,83 +28,99 @@ from catalog.packages.const import PKG_STATUS
logger = logging.getLogger(__name__)
-def create(data):
- logger.info('Start to create a NSD...')
- user_defined_data = ignore_case_get(data, 'userDefinedData')
- data = {
- 'id': str(uuid.uuid4()),
- 'nsdOnboardingState': PKG_STATUS.CREATED,
- 'nsdOperationalState': PKG_STATUS.DISABLED,
- 'nsdUsageState': PKG_STATUS.NOT_IN_USE,
- 'userDefinedData': user_defined_data,
- '_links': None # TODO
- }
- NSPackageModel.objects.create(
- nsPackageId=data['id'],
- onboardingState=data['nsdOnboardingState'],
- operationalState=data['nsdOperationalState'],
- usageState=data['nsdUsageState'],
- userDefinedData=data['userDefinedData']
- )
- logger.info('A NSD(%s) has been created.' % data['id'])
- return data
-
-
-def query_multiple():
- ns_pkgs = NSPackageModel.objects.all()
- response_data = []
- for ns_pkg in ns_pkgs:
- data = fill_resp_data(ns_pkg)
- response_data.append(data)
- return response_data
-
-
-def query_single(nsd_info_id):
- ns_pkgs = NSPackageModel.objects.filter(nsPackageId=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)
- return fill_resp_data(ns_pkgs[0])
-
-
-def delete_single(nsd_info_id):
- logger.info('Start to delete NSD(%s)...' % nsd_info_id)
- ns_pkgs = NSPackageModel.objects.filter(nsPackageId=nsd_info_id)
- if not ns_pkgs.exists():
+class NsDescriptor(object):
+
+ def __init__(self):
+ pass
+
+ def create(self, data):
+ logger.info('Start to create a NSD...')
+ user_defined_data = ignore_case_get(data, 'userDefinedData')
+ data = {
+ 'id': str(uuid.uuid4()),
+ 'nsdOnboardingState': PKG_STATUS.CREATED,
+ 'nsdOperationalState': PKG_STATUS.DISABLED,
+ 'nsdUsageState': PKG_STATUS.NOT_IN_USE,
+ 'userDefinedData': user_defined_data,
+ '_links': None # TODO
+ }
+ NSPackageModel.objects.create(
+ nsPackageId=data['id'],
+ onboardingState=data['nsdOnboardingState'],
+ operationalState=data['nsdOperationalState'],
+ usageState=data['nsdUsageState'],
+ userDefinedData=data['userDefinedData']
+ )
+ logger.info('A NSD(%s) has been created.' % data['id'])
+ return data
+
+ def query_multiple(self):
+ ns_pkgs = NSPackageModel.objects.all()
+ response_data = []
+ for ns_pkg in ns_pkgs:
+ data = fill_resp_data(ns_pkg)
+ response_data.append(data)
+ return response_data
+
+ def query_single(self, nsd_info_id):
+ ns_pkgs = NSPackageModel.objects.filter(nsPackageId=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)
+ return fill_resp_data(ns_pkgs[0])
+
+ def delete_single(self, nsd_info_id):
+ logger.info('Start to delete NSD(%s)...' % nsd_info_id)
+ ns_pkgs = NSPackageModel.objects.filter(nsPackageId=nsd_info_id)
+ if not ns_pkgs.exists():
+ logger.info('NSD(%s) has been deleted.' % nsd_info_id)
+ return
+ '''
+ 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 != 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)
+ '''
+ ns_pkgs.delete()
+ ns_pkg_path = os.path.join(CATALOG_ROOT_PATH, nsd_info_id)
+ fileutil.delete_dirs(ns_pkg_path)
logger.info('NSD(%s) has been deleted.' % nsd_info_id)
- return
- '''
- 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 != 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)
- '''
- ns_pkgs.delete()
- ns_pkg_path = os.path.join(CATALOG_ROOT_PATH, nsd_info_id)
- fileutil.delete_dirs(ns_pkg_path)
- logger.info('NSD(%s) has been deleted.' % nsd_info_id)
-
-
-def upload(remote_file, nsd_info_id):
- logger.info('Start to upload NSD(%s)...' % nsd_info_id)
- ns_pkgs = NSPackageModel.objects.filter(nsPackageId=nsd_info_id)
- 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)
- logger.info('NSD(%s) content has been uploaded.' % nsd_info_id)
- return local_file_name
+
+ def upload(self, remote_file, nsd_info_id):
+ logger.info('Start to upload NSD(%s)...' % nsd_info_id)
+ ns_pkgs = NSPackageModel.objects.filter(nsPackageId=nsd_info_id)
+ 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)
+ logger.info('NSD(%s) content has been uploaded.' % nsd_info_id)
+ return local_file_name
+
+ def download(self, nsd_info_id):
+ logger.info('Start to download NSD(%s)...' % nsd_info_id)
+ ns_pkgs = NSPackageModel.objects.filter(nsPackageId=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 != 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
+ local_file_name = local_file_path.split('/')[-1]
+ local_file_name = local_file_name.split('\\')[-1]
+ logger.info('NSD(%s) has been downloaded.' % nsd_info_id)
+ return local_file_path, local_file_name, os.path.getsize(local_file_path)
def parse_nsd_and_save(nsd_info_id, local_file_name):
@@ -143,22 +159,6 @@ def parse_nsd_and_save(nsd_info_id, local_file_name):
logger.info('NSD(%s) has been processed.' % nsd_info_id)
-def download(nsd_info_id):
- logger.info('Start to download NSD(%s)...' % nsd_info_id)
- ns_pkgs = NSPackageModel.objects.filter(nsPackageId=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 != 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
- local_file_name = local_file_path.split('/')[-1]
- local_file_name = local_file_name.split('\\')[-1]
- logger.info('NSD(%s) has been downloaded.' % nsd_info_id)
- return local_file_path, local_file_name, os.path.getsize(local_file_path)
-
-
def fill_resp_data(ns_pkg):
data = {
'id': ns_pkg.nsPackageId,
diff --git a/catalog/packages/biz/vnf_package.py b/catalog/packages/biz/vnf_package.py
index 12bdb17c..bb830926 100644
--- a/catalog/packages/biz/vnf_package.py
+++ b/catalog/packages/biz/vnf_package.py
@@ -35,6 +35,10 @@ logger = logging.getLogger(__name__)
class VnfPackage(object):
+
+ def __init__(self):
+ pass
+
def create_vnf_pkg(self, data):
user_defined_data = ignore_case_get(data, "userDefinedData")
vnf_pkg_id = str(uuid.uuid4())
diff --git a/catalog/packages/tests/test_ns_descriptor.py b/catalog/packages/tests/test_ns_descriptor.py
index 9e2ad68e..341e39be 100644
--- a/catalog/packages/tests/test_ns_descriptor.py
+++ b/catalog/packages/tests/test_ns_descriptor.py
@@ -52,6 +52,10 @@ class TestNsDescriptor(TestCase):
'userDefinedData': self.user_defined_data,
'_links': None
}
+ self.nsdModel = {
+ "pnfs": [{"properties": {"id": "m6000_s"}}],
+ "vnfs": [{"properties": {"id": "123"}}]
+ }
def tearDown(self):
pass
@@ -107,13 +111,18 @@ class TestNsDescriptor(TestCase):
onboardingState='CREATED',
operationalState='DISABLED',
usageState='NOT_IN_USE',
- userDefinedData=user_defined_data
+ userDefinedData=user_defined_data,
+ nsdModel=json.JSONEncoder().encode(self.nsdModel)
).save()
response = self.client.get('/api/nsd/v1/ns_descriptors/22', format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(expected_reponse_data, response.data)
+ def test_query_single_when_ns_not_exist(self):
+ response = self.client.get('/api/nsd/v1/ns_descriptors/22', format='json')
+ self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
+
def test_delete_single_nsd_normal(self):
user_defined_data = json.JSONEncoder().encode(self.user_defined_data)
NSPackageModel(
@@ -128,6 +137,10 @@ class TestNsDescriptor(TestCase):
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
self.assertEqual(None, response.data)
+ def test_delete_when_ns_not_exist(self):
+ response = self.client.delete("/api/nsd/v1/ns_descriptors/21", format='json')
+ self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
+
@mock.patch.object(toscaparser, 'parse_nsd')
def test_nsd_content_upload_normal(self, mock_parse_nsd):
user_defined_data_json = json.JSONEncoder().encode(self.user_defined_data)
@@ -161,11 +174,16 @@ class TestNsDescriptor(TestCase):
self.assertEqual(None, resp.data)
self.assertEqual(file_content, 'test')
os.remove('nsd_content.txt')
- os.remove(ns_pkg[0].localFilePath)
- os.removedirs(os.path.join(CATALOG_ROOT_PATH, ns_pkg[0].nsPackageId))
def test_nsd_content_upload_failure(self):
- pass
+ with open('nsd_content.txt', 'wb') as fp:
+ fp.write('test')
+ with open('nsd_content.txt', 'rb') as fp:
+ response = self.client.put(
+ "/api/nsd/v1/ns_descriptors/22/nsd_content",
+ {'file': fp},
+ )
+ self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
def test_nsd_content_download_normal(self):
with open('nsd_content.txt', 'wb') as fp:
@@ -186,6 +204,19 @@ class TestNsDescriptor(TestCase):
self.assertEqual('test1test2', file_content)
os.remove('nsd_content.txt')
+ def test_nsd_content_download_when_ns_not_exist(self):
+ response = self.client.get("/api/nsd/v1/ns_descriptors/23/nsd_content", format='json')
+ self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
+
+ def test_nsd_content_download_failed(self):
+ NSPackageModel.objects.create(
+ nsPackageId='23',
+ onboardingState='CREATED',
+ localFilePath='nsd_content.txt'
+ )
+ response = self.client.get("/api/nsd/v1/ns_descriptors/23/nsd_content", format='json')
+ self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
+
def test_nsd_content_partial_download_normal(self):
with open('nsd_content.txt', 'wb') as fp:
fp.writelines('test1')
diff --git a/catalog/packages/views/ns_descriptor_views.py b/catalog/packages/views/ns_descriptor_views.py
index 30fe6e9e..aa99637d 100644
--- a/catalog/packages/views/ns_descriptor_views.py
+++ b/catalog/packages/views/ns_descriptor_views.py
@@ -16,8 +16,7 @@ import logging
import traceback
from django.http import StreamingHttpResponse
-from catalog.packages.biz.ns_descriptor import create, delete_single, download, query_multiple, query_single, upload, \
- parse_nsd_and_save, handle_upload_failed
+from catalog.packages.biz.ns_descriptor import NsDescriptor, parse_nsd_and_save, handle_upload_failed
from catalog.packages.serializers.create_nsd_info_request import CreateNsdInfoRequestSerializer
from catalog.packages.serializers.nsd_info import NsdInfoSerializer
from catalog.packages.serializers.nsd_infos import NsdInfosSerializer
@@ -53,7 +52,7 @@ logger = logging.getLogger(__name__)
def ns_info_rd(request, nsdInfoId): # TODO
if request.method == 'GET':
try:
- data = query_single(nsdInfoId)
+ data = NsDescriptor().query_single(nsdInfoId)
nsd_info = validate_data(data, NsdInfoSerializer)
return Response(data=nsd_info.data, status=status.HTTP_200_OK)
except ResourceNotFoundException as e:
@@ -70,7 +69,7 @@ def ns_info_rd(request, nsdInfoId): # TODO
if request.method == 'DELETE':
try:
- delete_single(nsdInfoId)
+ NsDescriptor().delete_single(nsdInfoId)
return Response(status=status.HTTP_204_NO_CONTENT)
except CatalogException as e:
logger.error(e.message)
@@ -105,7 +104,7 @@ def ns_descriptors_rc(request, *args, **kwargs):
if request.method == 'POST':
try:
create_nsd_info_requst = validate_data(request.data, CreateNsdInfoRequestSerializer)
- data = create(create_nsd_info_requst.data)
+ data = NsDescriptor().create(create_nsd_info_requst.data)
nsd_info = validate_data(data, NsdInfoSerializer)
return Response(data=nsd_info.data, status=status.HTTP_201_CREATED)
except CatalogException as e:
@@ -119,7 +118,7 @@ def ns_descriptors_rc(request, *args, **kwargs):
if request.method == 'GET':
try:
- data = query_multiple()
+ data = NsDescriptor().query_multiple()
nsd_infos = validate_data(data, NsdInfosSerializer)
return Response(data=nsd_infos.data, status=status.HTTP_200_OK)
except CatalogException as e:
@@ -157,7 +156,7 @@ def nsd_content_ru(request, *args, **kwargs):
if request.method == 'PUT':
files = request.FILES.getlist('file')
try:
- local_file_name = upload(files[0], nsd_info_id)
+ local_file_name = NsDescriptor().upload(files[0], nsd_info_id)
parse_nsd_and_save(nsd_info_id, local_file_name)
return Response(data=None, status=status.HTTP_204_NO_CONTENT)
except CatalogException as e:
@@ -173,7 +172,7 @@ def nsd_content_ru(request, *args, **kwargs):
if request.method == 'GET':
try:
- file_path, file_name, file_size = download(nsd_info_id)
+ file_path, file_name, file_size = NsDescriptor().download(nsd_info_id)
start, end = 0, file_size
file_range = request.META.get('RANGE')
if file_range: