diff options
-rw-r--r-- | catalog/packages/biz/nsdm_subscription.py | 42 | ||||
-rw-r--r-- | catalog/packages/tests/test_nsdm_subscription.py | 187 | ||||
-rwxr-xr-x | catalog/packages/urls.py | 2 | ||||
-rw-r--r-- | catalog/packages/views/nsdm_subscription_views.py | 158 | ||||
-rw-r--r-- | catalog/pub/config/config.py | 6 |
5 files changed, 389 insertions, 6 deletions
diff --git a/catalog/packages/biz/nsdm_subscription.py b/catalog/packages/biz/nsdm_subscription.py index 19df46e0..ec305076 100644 --- a/catalog/packages/biz/nsdm_subscription.py +++ b/catalog/packages/biz/nsdm_subscription.py @@ -25,6 +25,7 @@ from rest_framework import status from catalog.packages import const from catalog.pub.database.models import NsdmSubscriptionModel from catalog.pub.exceptions import CatalogException, \ + ResourceNotFoundException, \ NsdmBadRequestException, NsdmDuplicateSubscriptionException from catalog.pub.utils.values import ignore_case_get @@ -45,6 +46,47 @@ class NsdmSubscription: def __init__(self): pass + def query_single_subscription(self, subscription_id): + logger.debug("Start Query Subscription... ") + subscription = \ + NsdmSubscriptionModel.objects.filter( + subscriptionid=subscription_id) + if not subscription.exists(): + raise ResourceNotFoundException( + "Subscription(%s) doesn't exists" % subscription_id) + logger.debug("Subscription found... ") + return self.fill_resp_data(subscription[0]) + + def delete_single_subscription(self, subscription_id): + logger.debug("Start Delete Subscription... ") + subscription = \ + NsdmSubscriptionModel.objects.filter( + subscriptionid=subscription_id) + if not subscription.exists(): + raise ResourceNotFoundException( + "Subscription(%s) doesn't exists" % subscription_id) + subscription.delete() + logger.debug("Deleted Subscription... ") + + def query_multi_subscriptions(self, query_params): + self.params = query_params + query_data = {} + logger.debug("Start QueryMultiSubscriptions get --> " + "Check for filters in query params" % self.params) + for query, value in self.params.iteritems(): + if query in const.NSDM_NOTIFICATION_FILTERS and value: + query_data[query + '__icontains'] = json.dumps(list(set(value))) + # Query the database with filters if the request + # has fields in request params, else fetch all records + if query_data: + subscriptions = NsdmSubscriptionModel.objects.filter(**query_data) + else: + subscriptions = NsdmSubscriptionModel.objects.all() + if not subscriptions.exists(): + raise ResourceNotFoundException("Subscriptions doesn't exist") + return [self.fill_resp_data(subscription) + for subscription in subscriptions] + def check_callbackuri_connection(self): logger.debug("Create Subscription --> Test Callback URI --" "Sending GET request to %s" % self.callback_uri) diff --git a/catalog/packages/tests/test_nsdm_subscription.py b/catalog/packages/tests/test_nsdm_subscription.py index fb355552..d2db19cb 100644 --- a/catalog/packages/tests/test_nsdm_subscription.py +++ b/catalog/packages/tests/test_nsdm_subscription.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import json import mock import uuid from django.test import TestCase @@ -338,3 +339,189 @@ class TestNsdmSubscription(TestCase): data=self.subscription, format='json') self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR) + + def test_nsdm_get_subscriptions(self): + NsdmSubscriptionModel(subscriptionid=self.subscription_id, + callback_uri="http://callbackuri.com", + auth_info={}, + notificationTypes=json.dumps( + ["NsdOnBoardingNotification"]), + nsdId=[], nsdVersion=[], + nsdInfoId=[], nsdDesigner=[], + nsdName=[], nsdInvariantId=[], + vnfPkgIds=[], pnfdInfoIds=[], + nestedNsdInfoIds=[], nsdOnboardingState=[], + nsdOperationalState=[], nsdUsageState=[], + pnfdId=[], pnfdVersion=[], pnfdProvider=[], + pnfdName=[], pnfdInvariantId=[], + pnfdOnboardingState=[], pnfdUsageState=[], + links=json.dumps(self.links)).save() + response = self.client.get("/api/nsd/v1/subscriptions", + format='json') + self.assertEqual(status.HTTP_200_OK, response.status_code) + self.assertEqual([self.test_subscription], response.data) + + def test_nsdm_get_subscriptions_filter(self): + NsdmSubscriptionModel(subscriptionid=self.subscription_id, + callback_uri="http://callbackuri.com", + auth_info={}, + notificationTypes=json.dumps( + ["NsdOnBoardingNotification"]), + nsdId=[], nsdVersion=[], + nsdInfoId=[], nsdDesigner=[], + nsdName=[], nsdInvariantId=[], + vnfPkgIds=[], pnfdInfoIds=[], + nestedNsdInfoIds=[], nsdOnboardingState=[], + nsdOperationalState=[], nsdUsageState=[], + pnfdId=[], pnfdVersion=[], pnfdProvider=[], + pnfdName=[], pnfdInvariantId=[], + pnfdOnboardingState=[], pnfdUsageState=[], + links=json.dumps(self.links)).save() + response = self.client.get("/api/nsd/v1/subscriptions" + "?notificationTypes" + "=NsdOnBoardingNotification", + format='json') + self.assertEqual(status.HTTP_200_OK, response.status_code) + self.assertEqual([self.test_subscription], response.data) + + def test_nsdm_get_subscriptions_filter_failure(self): + NsdmSubscriptionModel(subscriptionid=self.subscription_id, + callback_uri="http://callbackuri.com", + auth_info={}, + notificationTypes=json.dumps( + ["NsdOnBoardingNotification"]), + nsdId=[], nsdVersion=[], + nsdInfoId=[], nsdDesigner=[], + nsdName=[], nsdInvariantId=[], + vnfPkgIds=[], pnfdInfoIds=[], + nestedNsdInfoIds=[], nsdOnboardingState=[], + nsdOperationalState=[], nsdUsageState=[], + pnfdId=[], pnfdVersion=[], pnfdProvider=[], + pnfdName=[], pnfdInvariantId=[], + pnfdOnboardingState=[], pnfdUsageState=[], + links=json.dumps(self.links)).save() + response = self.client.get("/api/nsd/v1/subscriptions" + "?notificationTypes=" + "PnfdOnBoardingFailureNotification", + format='json') + self.assertEqual(status.HTTP_404_NOT_FOUND, response.status_code) + + def test_nsdm_get_subscriptions_invalid_filter(self): + NsdmSubscriptionModel(subscriptionid=self.subscription_id, + callback_uri="http://callbackuri.com", + auth_info={}, + notificationTypes=json.dumps( + ["NsdOnBoardingNotification"]), + nsdId=[], nsdVersion=[], + nsdInfoId=[], nsdDesigner=[], + nsdName=[], nsdInvariantId=[], + vnfPkgIds=[], pnfdInfoIds=[], + nestedNsdInfoIds=[], nsdOnboardingState=[], + nsdOperationalState=[], nsdUsageState=[], + pnfdId=[], pnfdVersion=[], pnfdProvider=[], + pnfdName=[], pnfdInvariantId=[], + pnfdOnboardingState=[], pnfdUsageState=[], + links=json.dumps(self.links)).save() + response = self.client.get("/api/nsd/v1/subscriptions" + "?notificationTypes=" + "PnfdOnBoardingFailureNotificati", + format='json') + self.assertEqual(status.HTTP_400_BAD_REQUEST, response.status_code) + + @mock.patch.object(NsdmSubscription, 'query_multi_subscriptions') + def test_nsdmsubscription_get_when_catch_exception(self, mock_create): + mock_create.side_effect = TypeError("Unicode type") + response = self.client.get('/api/nsd/v1/subscriptions', format='json') + self.assertEqual(response.status_code, + status.HTTP_500_INTERNAL_SERVER_ERROR) + + def test_nsdm_get_subscription(self): + NsdmSubscriptionModel(subscriptionid=self.subscription_id, + callback_uri="http://callbackuri.com", + auth_info={}, + notificationTypes=json.dumps( + ["NsdOnBoardingNotification"]), + nsdId=[], nsdVersion=[], + nsdInfoId=[], nsdDesigner=[], + nsdName=[], nsdInvariantId=[], + vnfPkgIds=[], pnfdInfoIds=[], + nestedNsdInfoIds=[], nsdOnboardingState=[], + nsdOperationalState=[], nsdUsageState=[], + pnfdId=[], pnfdVersion=[], pnfdProvider=[], + pnfdName=[], pnfdInvariantId=[], + pnfdOnboardingState=[], pnfdUsageState=[], + links=json.dumps(self.links)).save() + response = self.client.get('/api/nsd/v1/' + 'subscriptions/' + self.subscription_id, + format='json') + self.assertEqual(status.HTTP_200_OK, response.status_code) + self.assertEqual(self.test_subscription, response.data) + + def test_nsdm_get_subscription_failure(self): + expected_data = { + "title": "Query Subscription Failed!", + "status": 404, + "detail": "Subscription(" + self.subscription_id + ") " + "doesn't exists" + } + response = self.client.get('/api/nsd/v1/' + 'subscriptions/' + self.subscription_id, + format='json') + self.assertEqual(status.HTTP_404_NOT_FOUND, response.status_code) + self.assertEqual(expected_data, response.data) + + def test_nsdm_get_subscription_failure_bad_request(self): + response = self.client.get("/api/nsd/v1/subscriptions/123", + format='json') + self.assertEqual(status.HTTP_400_BAD_REQUEST, response.status_code) + + @mock.patch.object(NsdmSubscription, 'query_single_subscription') + def test_nsdmsubscription_getsingle_when_catch_exception( + self, mock_create): + mock_create.side_effect = TypeError("Unicode type") + response = self.client.get('/api/nsd/v1/' + 'subscriptions/' + self.subscription_id, + format='json') + self.assertEqual(response.status_code, + status.HTTP_500_INTERNAL_SERVER_ERROR) + + def test_ndsm_delete_subscription(self): + NsdmSubscriptionModel(subscriptionid=self.subscription_id, + callback_uri="http://callbackuri.com", + auth_info={}, + notificationTypes=json.dumps( + ["NsdOnBoardingNotification"]), + nsdId=[], nsdVersion=[], + nsdInfoId=[], nsdDesigner=[], + nsdName=[], nsdInvariantId=[], + vnfPkgIds=[], pnfdInfoIds=[], + nestedNsdInfoIds=[], nsdOnboardingState=[], + nsdOperationalState=[], nsdUsageState=[], + pnfdId=[], pnfdVersion=[], pnfdProvider=[], + pnfdName=[], pnfdInvariantId=[], + pnfdOnboardingState=[], pnfdUsageState=[], + links=json.dumps(self.links)).save() + response = self.client.delete('/api/nsd/v1/' + 'subscriptions/' + self.subscription_id, + format='json') + self.assertEqual(status.HTTP_204_NO_CONTENT, response.status_code) + + def test_ndsm_delete_subscription_failure(self): + response = self.client.delete('/api/nsd/v1/' + 'subscriptions/' + self.subscription_id, + format='json') + self.assertEqual(status.HTTP_404_NOT_FOUND, response.status_code) + + def test_nsdm_delete_subscription_failure_bad_request(self): + response = self.client.delete("/api/nsd/v1/subscriptions/123", + format='json') + self.assertEqual(status.HTTP_400_BAD_REQUEST, response.status_code) + + @mock.patch.object(NsdmSubscription, 'delete_single_subscription') + def test_nsdmsubscription_delete_when_catch_exception(self, mock_create): + mock_create.side_effect = TypeError("Unicode type") + response = self.client.delete('/api/nsd/v1/' + 'subscriptions/' + self.subscription_id, + format='json') + self.assertEqual(response.status_code, + status.HTTP_500_INTERNAL_SERVER_ERROR) diff --git a/catalog/packages/urls.py b/catalog/packages/urls.py index 3de7361b..a6fb6003 100755 --- a/catalog/packages/urls.py +++ b/catalog/packages/urls.py @@ -37,7 +37,7 @@ urlpatterns = [ url(r'^api/nsd/v1/ns_descriptors/(?P<nsdInfoId>[0-9a-zA-Z\-\_]+)$', ns_descriptor_views.ns_info_rd, name='ns_info_rd'), url(r'^api/nsd/v1/ns_descriptors/(?P<nsdInfoId>[0-9a-zA-Z\-\_]+)/nsd_content$', ns_descriptor_views.nsd_content_ru, name='nsd_content_ru'), url(r'^api/nsd/v1/subscriptions$', nsdm_subscription_views.nsd_subscription_rc, name='nsd_subscription_rc'), - # url(r'^api/nsd/v1/subscriptions/(?P<subscriptionId>[0-9a-zA-Z\-\_]+)$', nsdm_subscription_views.nsd_subscription_rd, name='nsd_subscription_rd'), + url(r'^api/nsd/v1/subscriptions/(?P<subscriptionId>[0-9a-zA-Z\-\_]+)$', nsdm_subscription_views.nsd_subscription_rd, name='nsd_subscription_rd'), # ETSI SOL005 PNFD url(r'^api/nsd/v1/pnf_descriptors$', pnf_descriptor_views.pnf_descriptors_rc, name='pnf_descriptors_rc'), diff --git a/catalog/packages/views/nsdm_subscription_views.py b/catalog/packages/views/nsdm_subscription_views.py index 6869d782..ed1cbd95 100644 --- a/catalog/packages/views/nsdm_subscription_views.py +++ b/catalog/packages/views/nsdm_subscription_views.py @@ -15,17 +15,22 @@ import logging import traceback -from drf_yasg.utils import swagger_auto_schema +from drf_yasg.utils import swagger_auto_schema, no_body from rest_framework import status from rest_framework.decorators import api_view from rest_framework.response import Response +from catalog.packages.serializers.nsdm_filter_data \ + import NsdmNotificationsFilter from catalog.packages.serializers.nsdm_subscription import \ + NsdmSubscriptionsSerializer, \ + NsdmSubscriptionIdSerializer, \ NsdmSubscriptionSerializer, \ NsdmSubscriptionRequestSerializer from catalog.packages.serializers.response \ import ProblemDetailsSerializer from catalog.pub.exceptions import \ + ResourceNotFoundException, \ NsdmBadRequestException, NsdmDuplicateSubscriptionException from catalog.packages.biz.nsdm_subscription import NsdmSubscription @@ -63,7 +68,18 @@ def get_problem_details_serializer(title, status_code, error_message): status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer() } ) -@api_view(http_method_names=['POST']) +@swagger_auto_schema( + method='GET', + operation_description="Query subscriptions for Nsd Management", + request_body=no_body, + responses={ + status.HTTP_200_OK: NsdmSubscriptionsSerializer(), + status.HTTP_400_BAD_REQUEST: ProblemDetailsSerializer(), + status.HTTP_404_NOT_FOUND: ProblemDetailsSerializer(), + status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer(), + } +) +@api_view(http_method_names=['POST', 'GET']) def nsd_subscription_rc(request): if request.method == 'POST': logger.debug("SubscribeNotification--post::> %s" % request.data) @@ -103,3 +119,141 @@ def nsd_subscription_rc(request): e.message) return Response(data=problem_details_serializer.data, status=status.HTTP_500_INTERNAL_SERVER_ERROR) + if request.method == 'GET': + logger.debug("Subscription Notification GET %s" % request.query_params) + try: + title = 'Query Subscription Failed!' + request_query_params = {} + if request.query_params: + request_query_params = \ + validate_data(request.query_params, + NsdmNotificationsFilter).data + subscription_data = \ + NsdmSubscription().query_multi_subscriptions( + request_query_params) + subscriptions = validate_data(subscription_data, + NsdmSubscriptionsSerializer) + return Response(data=subscriptions.data, status=status.HTTP_200_OK) + except NsdmBadRequestException as e: + logger.error(e.message) + problem_details_serializer = \ + get_problem_details_serializer(title, + status.HTTP_400_BAD_REQUEST, + e.message) + return Response(data=problem_details_serializer.data, + status=status.HTTP_400_BAD_REQUEST) + except ResourceNotFoundException as e: + problem_details_serializer = \ + get_problem_details_serializer(title, + status.HTTP_404_NOT_FOUND, + e.message) + return Response(data=problem_details_serializer.data, + status=status.HTTP_404_NOT_FOUND) + except Exception as e: + logger.error(e.message) + problem_details_serializer = \ + get_problem_details_serializer( + title, + status.HTTP_500_INTERNAL_SERVER_ERROR, + traceback.format_exc()) + return Response(data=problem_details_serializer.data, + status=status.HTTP_500_INTERNAL_SERVER_ERROR) + + +@swagger_auto_schema( + method='GET', + operation_description="Query subscriptions for Nsd Management", + request_body=no_body, + responses={ + status.HTTP_200_OK: NsdmSubscriptionSerializer(), + status.HTTP_400_BAD_REQUEST: ProblemDetailsSerializer(), + status.HTTP_404_NOT_FOUND: ProblemDetailsSerializer(), + status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer() + } +) +@swagger_auto_schema( + method='DELETE', + operation_description="Delete subscription for Nsd Management", + request_body=no_body, + responses={ + status.HTTP_204_NO_CONTENT: 'No_Content', + status.HTTP_400_BAD_REQUEST: ProblemDetailsSerializer(), + status.HTTP_404_NOT_FOUND: ProblemDetailsSerializer(), + status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer() + } +) +@api_view(http_method_names=['GET', 'DELETE']) +def nsd_subscription_rd(request, **kwargs): + subscription_id = kwargs.get("subscriptionId") + if request.method == 'GET': + try: + title = 'Query Subscription Failed!' + validate_data({'subscription_id': subscription_id}, + NsdmSubscriptionIdSerializer) + subscription_data = \ + NsdmSubscription().query_single_subscription(subscription_id) + subscription = validate_data(subscription_data, + NsdmSubscriptionSerializer) + return Response(data=subscription.data, status=status.HTTP_200_OK) + except NsdmBadRequestException as e: + logger.error(e.message) + problem_details_serializer = \ + get_problem_details_serializer(title, + status.HTTP_400_BAD_REQUEST, + e.message) + return Response(data=problem_details_serializer.data, + status=status.HTTP_400_BAD_REQUEST) + except ResourceNotFoundException as e: + logger.error(e.message) + problem_details_serializer = \ + get_problem_details_serializer(title, + status.HTTP_404_NOT_FOUND, + e.message) + return Response(data=problem_details_serializer.data, + status=status.HTTP_404_NOT_FOUND) + except Exception as e: + logger.error(e.message) + logger.error(traceback.format_exc()) + problem_details_serializer = \ + get_problem_details_serializer( + title, + status.HTTP_500_INTERNAL_SERVER_ERROR, + "Query of subscriptioni(%s) Failed" + % subscription_id) + return Response(data=problem_details_serializer.data, + status=status.HTTP_500_INTERNAL_SERVER_ERROR) + elif request.method == 'DELETE': + try: + title = 'Delete Subscription Failed!' + validate_data({'subscription_id': subscription_id}, + NsdmSubscriptionIdSerializer) + subscription_data = NsdmSubscription().\ + delete_single_subscription(subscription_id) + return Response(status=status.HTTP_204_NO_CONTENT) + except NsdmBadRequestException as e: + logger.error(e.message) + problem_details_serializer = \ + get_problem_details_serializer(title, + status.HTTP_400_BAD_REQUEST, + e.message) + return Response(data=problem_details_serializer.data, + status=status.HTTP_400_BAD_REQUEST) + except ResourceNotFoundException as e: + logger.error(e.message) + problem_details_serializer = \ + get_problem_details_serializer(title, + status.HTTP_404_NOT_FOUND, + e.message) + return Response(data=problem_details_serializer.data, + status=status.HTTP_404_NOT_FOUND) + except Exception as e: + logger.error(e.message) + logger.error(traceback.format_exc()) + problem_details_serializer = \ + get_problem_details_serializer( + title, + status.HTTP_500_INTERNAL_SERVER_ERROR, + "Delete of subscription(%s) Failed" + % subscription_id) + return Response(data=problem_details_serializer.data, + status=status.HTTP_500_INTERNAL_SERVER_ERROR) diff --git a/catalog/pub/config/config.py b/catalog/pub/config/config.py index 2aaf3941..10c8c9dd 100644 --- a/catalog/pub/config/config.py +++ b/catalog/pub/config/config.py @@ -76,6 +76,6 @@ CATALOG_ROOT_PATH = None CATALOG_URL_PATH = None # [sdc config] -SDC_BASE_URL = "http://127.0.0.1/api" -SDC_USER = "SDC" -SDC_PASSWD = "SDC" +SDC_BASE_URL = "http://msb-iag/api" +SDC_USER = "aai" +SDC_PASSWD = "Kp8bJ4SXszM0WXlhak3eHlcse2gAw84vaoGGmJvUy2U" |