summaryrefslogtreecommitdiffstats
path: root/lcm
diff options
context:
space:
mode:
authorfujinhua <fu.jinhua@zte.com.cn>2019-04-06 09:22:07 +0800
committerfujinhua <fu.jinhua@zte.com.cn>2019-04-06 10:05:26 +0800
commit906531a0b17c93a9c442366456032a046122034c (patch)
tree127fdbbb2fe7ac25c0c76f9334f328fea545a723 /lcm
parent61b127d5e54d2e2ce892c1c4360a23136321f125 (diff)
Refactor codes for subscriptions
Change-Id: I046b347903bd37ef647ac5473c367bc26c20c14b Issue-ID: VFC-1306 Signed-off-by: fujinhua <fu.jinhua@zte.com.cn>
Diffstat (limited to 'lcm')
-rw-r--r--lcm/lcm/nf/biz/create_subscription.py5
-rw-r--r--lcm/lcm/nf/tests/test_subscribe_notification.py12
-rw-r--r--lcm/lcm/nf/views/subscriptions_view.py90
3 files changed, 46 insertions, 61 deletions
diff --git a/lcm/lcm/nf/biz/create_subscription.py b/lcm/lcm/nf/biz/create_subscription.py
index db529a73..6802c221 100644
--- a/lcm/lcm/nf/biz/create_subscription.py
+++ b/lcm/lcm/nf/biz/create_subscription.py
@@ -25,6 +25,7 @@ from rest_framework import status
from lcm.nf import const
from lcm.pub.database.models import SubscriptionModel
from lcm.pub.exceptions import NFLCMException
+from lcm.pub.exceptions import NFLCMExceptionSeeOther
from lcm.pub.utils.values import ignore_case_get
logger = logging.getLogger(__name__)
@@ -116,8 +117,8 @@ class CreateSubscription:
return True
for subscription in subscriptions:
if self.check_filter_exists(subscription):
- raise NFLCMException("Already Subscription exists with the "
- "same callbackUri and filter")
+ raise NFLCMExceptionSeeOther("Already Subscription exists with the "
+ "same callbackUri and filter")
return False
def save_db(self):
diff --git a/lcm/lcm/nf/tests/test_subscribe_notification.py b/lcm/lcm/nf/tests/test_subscribe_notification.py
index 8aeab63f..73463efa 100644
--- a/lcm/lcm/nf/tests/test_subscribe_notification.py
+++ b/lcm/lcm/nf/tests/test_subscribe_notification.py
@@ -95,7 +95,8 @@ class TestSubscription(TestCase):
mock_requests.return_value.status_code = 204
mock_requests.get.return_value.status_code = 204
expected_data = {
- 'error': 'Auth type should be BASIC'
+ 'detail': 'Auth type should be BASIC',
+ 'status': 500
}
response = self.client.post("/api/vnflcm/v1/subscriptions", data=dummy_subscription, format='json')
self.assertEqual(500, response.status_code)
@@ -118,8 +119,9 @@ class TestSubscription(TestCase):
mock_requests.return_value.status_code = 204
mock_requests.get.return_value.status_code = 204
expected_data = {
- 'error': 'If you are setting operationTypes,then ' +
- 'notificationTypes must be VnfLcmOperationOccurrenceNotification'
+ 'detail': 'If you are setting operationTypes,then ' +
+ 'notificationTypes must be VnfLcmOperationOccurrenceNotification',
+ 'status': 500
}
response = self.client.post("/api/vnflcm/v1/subscriptions", data=dummy_subscription, format='json')
self.assertEqual(500, response.status_code)
@@ -150,7 +152,3 @@ class TestSubscription(TestCase):
self.assertEqual(temp_uuid, response.data["id"])
response = self.client.post("/api/vnflcm/v1/subscriptions", data=dummy_subscription, format='json')
self.assertEqual(303, response.status_code)
- expected_data = {
- "error": "Already Subscription exists with the same callbackUri and filter"
- }
- self.assertEqual(expected_data, response.data)
diff --git a/lcm/lcm/nf/views/subscriptions_view.py b/lcm/lcm/nf/views/subscriptions_view.py
index 4c013ee4..a9ffed9b 100644
--- a/lcm/lcm/nf/views/subscriptions_view.py
+++ b/lcm/lcm/nf/views/subscriptions_view.py
@@ -15,7 +15,6 @@
import ast
import json
import logging
-import traceback
from drf_yasg.utils import swagger_auto_schema
from lcm.nf.biz.create_subscription import CreateSubscription
@@ -29,6 +28,7 @@ from lcm.nf.serializers.lccn_subscription import LccnSubscriptionSerializer
from lcm.nf.serializers.lccn_subscriptions import LccnSubscriptionsSerializer
from lcm.nf.serializers.response import ProblemDetailsSerializer
from lcm.pub.exceptions import NFLCMException
+from .common import view_safe_call_with_log
logger = logging.getLogger(__name__)
VALID_FILTERS = ["operationTypes", "operationStates", "notificationTypes", "vnfInstanceId"]
@@ -53,39 +53,31 @@ class SubscriptionsView(APIView):
status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
}
)
+ @view_safe_call_with_log(logger=logger)
def post(self, request):
logger.debug("SubscribeNotification--post::> %s" % request.data)
- try:
- lccn_subscription_request_serializer = LccnSubscriptionRequestSerializer(data=request.data)
- if not lccn_subscription_request_serializer.is_valid():
- raise NFLCMException(lccn_subscription_request_serializer.errors)
- subscription = CreateSubscription(
- lccn_subscription_request_serializer.data).do_biz()
- lccn_notifications_filter = {
- "notificationTypes": ast.literal_eval(subscription.notification_types),
- "operationTypes": ast.literal_eval(subscription.operation_types),
- "operationStates": ast.literal_eval(subscription.operation_states),
- "vnfInstanceSubscriptionFilter": json.loads(subscription.vnf_instance_filter)
- }
- subscription_data = {
- "id": subscription.subscription_id,
- "callbackUri": subscription.callback_uri,
- "_links": json.loads(subscription.links),
- "filter": lccn_notifications_filter
- }
- sub_resp_serializer = LccnSubscriptionSerializer(data=subscription_data)
- if not sub_resp_serializer.is_valid():
- raise NFLCMException(sub_resp_serializer.errors)
- return Response(data=sub_resp_serializer.data, status=status.HTTP_201_CREATED)
- except NFLCMException as e:
- logger.error(e.message)
- if "exists" in e.message:
- return Response(data={'error': '%s' % e.message}, status=status.HTTP_303_SEE_OTHER)
- return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
- except Exception as e:
- logger.error(e.message)
- logger.error(traceback.format_exc())
- return Response(data={'error': e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+
+ lccn_subscription_request_serializer = LccnSubscriptionRequestSerializer(data=request.data)
+ if not lccn_subscription_request_serializer.is_valid():
+ raise NFLCMException(lccn_subscription_request_serializer.errors)
+ subscription = CreateSubscription(
+ lccn_subscription_request_serializer.data).do_biz()
+ lccn_notifications_filter = {
+ "notificationTypes": ast.literal_eval(subscription.notification_types),
+ "operationTypes": ast.literal_eval(subscription.operation_types),
+ "operationStates": ast.literal_eval(subscription.operation_states),
+ "vnfInstanceSubscriptionFilter": json.loads(subscription.vnf_instance_filter)
+ }
+ subscription_data = {
+ "id": subscription.subscription_id,
+ "callbackUri": subscription.callback_uri,
+ "_links": json.loads(subscription.links),
+ "filter": lccn_notifications_filter
+ }
+ sub_resp_serializer = LccnSubscriptionSerializer(data=subscription_data)
+ if not sub_resp_serializer.is_valid():
+ raise NFLCMException(sub_resp_serializer.errors)
+ return Response(data=sub_resp_serializer.data, status=status.HTTP_201_CREATED)
@swagger_auto_schema(
responses={
@@ -94,28 +86,22 @@ class SubscriptionsView(APIView):
status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
}
)
+ @view_safe_call_with_log(logger=logger)
def get(self, request):
logger.debug("SubscribeNotification--get::> %s" % request.query_params)
- try:
- if request.query_params and not set(request.query_params).issubset(set(VALID_FILTERS)):
- problem_details_serializer = get_problem_details_serializer(status.HTTP_400_BAD_REQUEST, "Not a valid filter")
- return Response(data=problem_details_serializer.data, status=status.HTTP_400_BAD_REQUEST)
- resp_data = QuerySubscription(request.query_params).query_multi_subscriptions()
- subscriptions_serializer = LccnSubscriptionsSerializer(data=resp_data)
- if not subscriptions_serializer.is_valid():
- raise NFLCMException(subscriptions_serializer.errors)
+ if request.query_params and not set(request.query_params).issubset(set(VALID_FILTERS)):
+ problem_details_serializer = get_problem_details_serializer(
+ status.HTTP_400_BAD_REQUEST,
+ "Not a valid filter"
+ )
+ return Response(data=problem_details_serializer.data,
+ status=status.HTTP_400_BAD_REQUEST)
+ resp_data = QuerySubscription(request.query_params).query_multi_subscriptions()
- logger.debug("SubscribeNotification--get::> Remove default fields if exclude_default" +
- " is specified")
- return Response(data=subscriptions_serializer.data, status=status.HTTP_200_OK)
- except NFLCMException as e:
- logger.error(e.message)
- problem_details_serializer = get_problem_details_serializer(status.HTTP_500_INTERNAL_SERVER_ERROR, traceback.format_exc())
- return Response(data=problem_details_serializer.data, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+ subscriptions_serializer = LccnSubscriptionsSerializer(data=resp_data)
+ if not subscriptions_serializer.is_valid():
+ raise NFLCMException(subscriptions_serializer.errors)
- except Exception as e:
- logger.error(e.message)
- logger.error(traceback.format_exc())
- problem_details_serializer = get_problem_details_serializer(status.HTTP_500_INTERNAL_SERVER_ERROR, traceback.format_exc())
- return Response(data=problem_details_serializer.data, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+ logger.debug("SubscribeNotification--get::> Remove default fields")
+ return Response(data=subscriptions_serializer.data, status=status.HTTP_200_OK)