aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFu Jinhua <fu.jinhua@zte.com.cn>2018-08-28 01:19:28 +0000
committerGerrit Code Review <gerrit@onap.org>2018-08-28 01:19:28 +0000
commitd0d7df18908ac86878c68c21ae958ad3388870a0 (patch)
tree5870da49e25082b812fee1b976cdb40ad7cd0162
parent56a500b5eb0ae3f1df53aa0ca9fe8c0c149e6c67 (diff)
parent38343ad192c1b3c4c1ceedcac19f98cce424b1d9 (diff)
Merge "Ns descriptor related stuffs."
-rw-r--r--catalog/packages/biz/ns_descriptor.py16
-rw-r--r--catalog/packages/biz/pnf_descriptor.py9
-rw-r--r--catalog/packages/tests/test_ns_descriptor.py75
3 files changed, 57 insertions, 43 deletions
diff --git a/catalog/packages/biz/ns_descriptor.py b/catalog/packages/biz/ns_descriptor.py
index ff25c4b6..4846bfab 100644
--- a/catalog/packages/biz/ns_descriptor.py
+++ b/catalog/packages/biz/ns_descriptor.py
@@ -1,4 +1,4 @@
-# Copyright 2017 ZTE Corporation.
+# 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.
@@ -20,7 +20,7 @@ import uuid
from catalog.pub.config.config import CATALOG_ROOT_PATH
from catalog.pub.utils import fileutil
from catalog.pub.utils.values import ignore_case_get
-from catalog.pub.database.models import NSPackageModel, VnfPackageModel
+from catalog.pub.database.models import NSPackageModel, PnfPackageModel, VnfPackageModel
from catalog.pub.exceptions import CatalogException
from catalog.pub.utils import toscaparser
@@ -134,9 +134,9 @@ def upload(remote_file, nsd_info_id):
def download(nsd_info_id):
ns_pkgs = NSPackageModel.objects.filter(nsPackageId=nsd_info_id)
if not ns_pkgs.exists():
- raise CatalogException('The PNF Descriptor (%s) does not exist.' % nsd_info_id)
+ raise CatalogException('The NS Descriptor (%s) does not exist.' % nsd_info_id)
if ns_pkgs[0].onboardingState != 'ONBOARDED':
- raise CatalogException('The PNF Descriptor (%s) is not ONBOARDED.' % nsd_info_id)
+ raise CatalogException('The NS Descriptor (%s) is not ONBOARDED.' % nsd_info_id)
local_file_path = ns_pkgs[0].localFilePath
return local_file_path
@@ -170,6 +170,14 @@ def fill_resp_data(ns_pkg):
vnf_pkg_ids.append(pkg.vnfPackageId)
data['vnfPkgIds'] = vnf_pkg_ids
+ pnf_info_ids = []
+ for pnf in nsd_model['pnfs']:
+ pnfd_id = pnf["properties"]["id"]
+ pkgs = PnfPackageModel.objects.filter(pnfdId=pnfd_id)
+ for pkg in pkgs:
+ pnf_info_ids.append(pkg.pnfPackageId)
+ data['pnfInfoIds'] = pnf_info_ids # TODO: need reconfirming
+
if ns_pkg.userDefinedData:
user_defined_data = json.JSONDecoder().decode(ns_pkg.userDefinedData)
data['userDefinedData'] = user_defined_data
diff --git a/catalog/packages/biz/pnf_descriptor.py b/catalog/packages/biz/pnf_descriptor.py
index eb9a2a97..4d11e4e9 100644
--- a/catalog/packages/biz/pnf_descriptor.py
+++ b/catalog/packages/biz/pnf_descriptor.py
@@ -118,7 +118,6 @@ def download(pnfd_info_id):
def delete_pnf(pnfd_info_id):
- # TODO
pnf_pkgs = PnfPackageModel.objects.filter(pnfPackageId=pnfd_info_id)
if not pnf_pkgs.exists():
logger.debug('PNF descriptor (%s) is deleted.' % pnfd_info_id)
@@ -127,7 +126,13 @@ def delete_pnf(pnfd_info_id):
raise CatalogException('The PNF descriptor (%s) shall be NOT_IN_USE.' % pnfd_info_id)
ns_pkgs = NSPackageModel.objects.all()
for ns_pkg in ns_pkgs:
- if pnfd_info_id in ns_pkg.pnfdInfoIds:
+ pnf_info_ids = []
+ for pnf in ns_pkg.nsd_model['pnfs']:
+ pnfd_id = pnf["properties"]["id"]
+ pkgs = PnfPackageModel.objects.filter(pnfdId=pnfd_id)
+ for pkg in pkgs:
+ pnf_info_ids.append(pkg.pnfPackageId)
+ if pnfd_info_id in pnf_info_ids:
raise CatalogException('The PNF descriptor (%s) is referenced.' % pnfd_info_id)
break
pnf_pkgs.delete()
diff --git a/catalog/packages/tests/test_ns_descriptor.py b/catalog/packages/tests/test_ns_descriptor.py
index c6eda2fe..285a1cd4 100644
--- a/catalog/packages/tests/test_ns_descriptor.py
+++ b/catalog/packages/tests/test_ns_descriptor.py
@@ -20,6 +20,7 @@ from django.test import TestCase
from rest_framework import status
from rest_framework.test import APIClient
from catalog.pub.database.models import NSPackageModel
+from catalog.pub.config.config import CATALOG_ROOT_PATH
class TestNsDescriptor(TestCase):
@@ -70,11 +71,7 @@ class TestNsDescriptor(TestCase):
'onboardingFailureDetails': None,
'nsdOperationalState': 'DISABLED',
'nsdUsageState': 'NOT_IN_USE',
- 'userDefinedData': {
- 'key1': 'value1',
- 'key2': 'value2',
- 'key3': 'value3',
- },
+ 'userDefinedData': self.user_defined_data,
'_links': None
},
{
@@ -91,20 +88,11 @@ class TestNsDescriptor(TestCase):
'onboardingFailureDetails': None,
'nsdOperationalState': 'DISABLED',
'nsdUsageState': 'NOT_IN_USE',
- 'userDefinedData': {
- 'key1': 'value1',
- 'key2': 'value2',
- 'key3': 'value3',
- },
+ 'userDefinedData': self.user_defined_data,
'_links': None
}
]
- user_defined_data = {
- 'key1': 'value1',
- 'key2': 'value2',
- 'key3': 'value3',
- }
- user_defined_data = json.JSONEncoder().encode(user_defined_data)
+ user_defined_data = json.JSONEncoder().encode(self.user_defined_data)
for i in range(2):
NSPackageModel(
nsPackageId=str(i),
@@ -132,19 +120,10 @@ class TestNsDescriptor(TestCase):
'onboardingFailureDetails': None,
'nsdOperationalState': 'DISABLED',
'nsdUsageState': 'NOT_IN_USE',
- 'userDefinedData': {
- 'key1': 'value1',
- 'key2': 'value2',
- 'key3': 'value3',
- },
+ 'userDefinedData': self.user_defined_data,
'_links': None
}
- user_defined_data = {
- 'key1': 'value1',
- 'key2': 'value2',
- 'key3': 'value3',
- }
- user_defined_data = json.JSONEncoder().encode(user_defined_data)
+ user_defined_data = json.JSONEncoder().encode(self.user_defined_data)
NSPackageModel(
nsPackageId='22',
onboardingState='CREATED',
@@ -158,20 +137,15 @@ class TestNsDescriptor(TestCase):
self.assertEqual(expected_reponse_data, response.data)
def test_delete_single_nsd_normal(self):
- user_defined_data = {
- 'key1': 'value1',
- 'key2': 'value2',
- 'key3': 'value3',
- }
- user_defined_data = json.JSONEncoder().encode(user_defined_data)
+ user_defined_data = json.JSONEncoder().encode(self.user_defined_data)
NSPackageModel(
- nsPackageId='22',
+ nsPackageId='21',
operationalState='DISABLED',
usageState='NOT_IN_USE',
userDefinedData=user_defined_data,
nsdModel='test'
).save()
- resp = self.client.delete("/api/nsd/v1/ns_descriptors/22", format='json')
+ resp = self.client.delete("/api/nsd/v1/ns_descriptors/21", format='json')
self.assertEqual(resp.status_code, status.HTTP_204_NO_CONTENT)
self.assertEqual({}, resp.data)
@@ -185,16 +159,43 @@ class TestNsDescriptor(TestCase):
).save()
with open('nsd_content.txt', 'wb') as fp:
fp.write('test')
-
with open('nsd_content.txt', 'rb') as fp:
resp = self.client.put(
"/api/nsd/v1/ns_descriptors/22/nsd_content",
{'file': fp},
)
+ file_content = ''
+ with open(os.path.join(CATALOG_ROOT_PATH, '22/nsd_content.txt')) as fp:
+ data = fp.read()
+ file_content = '%s%s' % (file_content, data)
self.assertEqual(resp.status_code, status.HTTP_204_NO_CONTENT)
self.assertEqual({}, resp.data)
-
+ self.assertEqual(file_content, 'test')
os.remove('nsd_content.txt')
def test_nsd_content_upload_failure(self):
pass
+
+ def test_nsd_content_download_normal(self):
+ pass
+
+ def test_nsd_content_partial_download_normal(self):
+ with open('nsd_content.txt', 'wb') as fp:
+ fp.writelines('test1')
+ fp.writelines('test2')
+ NSPackageModel(
+ nsPackageId='23',
+ onboardingState='ONBOARDED',
+ localFilePath='nsd_content.txt'
+ ).save()
+ response = self.client.get(
+ "/api/nsd/v1/ns_descriptors/23/nsd_content",
+ RANGE='5-10',
+ format='json'
+ )
+ partial_file_content = ''
+ for data in response.streaming_content:
+ partial_file_content = '%s%s' % (partial_file_content, data)
+ self.assertEqual(response.status_code, status.HTTP_200_OK)
+ self.assertEqual('test2', partial_file_content)
+ os.remove('nsd_content.txt')