summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorfujinhua <fu.jinhua@zte.com.cn>2019-04-10 14:56:31 +0800
committerfujinhua <fu.jinhua@zte.com.cn>2019-04-10 14:56:31 +0800
commitecd97c37b55d065fbea61e275e5369155537c6d8 (patch)
tree6d3c14208798a6daeae617614e04b0df18ee82f2
parent3bd58a8918d6b70cbdec37bfc96da8bf59f71d41 (diff)
Refactor codes for subscription
Change-Id: I32735fb66a2e7715ec2a710d408747b42d3487d5 Issue-ID: VFC-1306 Signed-off-by: fujinhua <fu.jinhua@zte.com.cn>
-rw-r--r--lcm/ns/biz/create_subscription.py3
-rw-r--r--lcm/ns/views/sol/common.py7
-rw-r--r--lcm/ns/views/sol/subscriptions_view.py126
-rw-r--r--lcm/pub/exceptions.py4
4 files changed, 66 insertions, 74 deletions
diff --git a/lcm/ns/biz/create_subscription.py b/lcm/ns/biz/create_subscription.py
index 76f478ba..06ec4b00 100644
--- a/lcm/ns/biz/create_subscription.py
+++ b/lcm/ns/biz/create_subscription.py
@@ -25,6 +25,7 @@ from rest_framework import status
from lcm.ns import const
from lcm.pub.database.models import SubscriptionModel
from lcm.pub.exceptions import NSLCMException
+from lcm.pub.exceptions import SeeOtherException
from lcm.pub.utils.values import ignore_case_get
logger = logging.getLogger(__name__)
@@ -127,7 +128,7 @@ class CreateSubscription:
return True
for subscription in subscriptions:
if self.check_filter_exists(subscription):
- raise NSLCMException("Already Subscription exists with the same callbackUri and filter")
+ raise SeeOtherException("Already Subscription exists with the same callbackUri and filter")
return False
def save_db(self):
diff --git a/lcm/ns/views/sol/common.py b/lcm/ns/views/sol/common.py
index 2d9c7178..b29423b2 100644
--- a/lcm/ns/views/sol/common.py
+++ b/lcm/ns/views/sol/common.py
@@ -20,6 +20,7 @@ from rest_framework.response import Response
from lcm.pub.exceptions import BadRequestException
from lcm.pub.exceptions import NSLCMException
+from lcm.pub.exceptions import SeeOtherException
logger = logging.getLogger(__name__)
@@ -39,6 +40,12 @@ def view_safe_call_with_log(logger):
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
+ except SeeOtherException as e:
+ logger.error(e.message)
+ return make_error_resp(
+ detail=e.message,
+ status=status.HTTP_303_SEE_OTHER
+ )
except BadRequestException as e:
logger.error(e.message)
return make_error_resp(
diff --git a/lcm/ns/views/sol/subscriptions_view.py b/lcm/ns/views/sol/subscriptions_view.py
index d962d1dc..c10e501d 100644
--- a/lcm/ns/views/sol/subscriptions_view.py
+++ b/lcm/ns/views/sol/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.ns.serializers.sol.lccn_subscription import LccnSubscriptionSerializer
@@ -30,10 +29,18 @@ from lcm.ns.serializers.sol.lccn_subscription_request import LccnSubscriptionReq
from lcm.ns.serializers.sol.pub_serializers import ProblemDetailsSerializer
from lcm.pub.exceptions import NSLCMException
from lcm.pub.exceptions import BadRequestException
+from .common import view_safe_call_with_log
logger = logging.getLogger(__name__)
-VALID_FILTERS = ["operationTypes", "operationStates", "notificationTypes", "nsInstanceId",
- "nsComponentTypes", "lcmOpNameImpactingNsComponent", "lcmOpOccStatusImpactingNsComponent"]
+VALID_FILTERS = [
+ "operationTypes",
+ "operationStates",
+ "notificationTypes",
+ "nsInstanceId",
+ "nsComponentTypes",
+ "lcmOpNameImpactingNsComponent",
+ "lcmOpOccStatusImpactingNsComponent"
+]
def get_problem_details_serializer(status_code, error_message):
@@ -56,54 +63,39 @@ 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 BadRequestException(
- 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),
- "nsInstanceSubscriptionFilter": json.loads(subscription.ns_instance_filter),
- "nsComponentTypes": ast.literal_eval(subscription.ns_component_types),
- "lcmOpNameImpactingNsComponent": ast.literal_eval(subscription.
- lcm_opname_impacting_nscomponent),
- "lcmOpOccStatusImpactingNsComponent": ast.literal_eval(subscription.
- lcm_opoccstatus_impacting_nscomponent)
- }
- 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 NSLCMException(sub_resp_serializer.errors)
- return Response(data=sub_resp_serializer.data, status=status.HTTP_201_CREATED)
- except BadRequestException as e:
- logger.error("Exception in InstantiateNsView: %s", e.message)
- data = {'status': status.HTTP_400_BAD_REQUEST, 'detail': e.message}
- return Response(data=data, status=status.HTTP_400_BAD_REQUEST)
- except NSLCMException as e:
- logger.error(e.message)
- if "exists" in e.message:
- data = {'status': status.HTTP_303_SEE_OTHER, 'detail': e.message}
- return Response(data=data, status=status.HTTP_303_SEE_OTHER)
- data = {'status': status.HTTP_500_INTERNAL_SERVER_ERROR, 'detail': e.message}
- return Response(data=data, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
- except Exception as e:
- logger.error(e.message)
- logger.error(traceback.format_exc())
- data = {'status': status.HTTP_500_INTERNAL_SERVER_ERROR, 'detail': e.message}
- return Response(data=data, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+
+ lccn_subscription_request_serializer = LccnSubscriptionRequestSerializer(
+ data=request.data)
+ if not lccn_subscription_request_serializer.is_valid():
+ raise BadRequestException(
+ 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),
+ "nsInstanceSubscriptionFilter": json.loads(subscription.ns_instance_filter),
+ "nsComponentTypes": ast.literal_eval(subscription.ns_component_types),
+ "lcmOpNameImpactingNsComponent": ast.literal_eval(subscription.
+ lcm_opname_impacting_nscomponent),
+ "lcmOpOccStatusImpactingNsComponent": ast.literal_eval(subscription.
+ lcm_opoccstatus_impacting_nscomponent)
+ }
+ 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 NSLCMException(sub_resp_serializer.errors)
+ return Response(data=sub_resp_serializer.data, status=status.HTTP_201_CREATED)
@swagger_auto_schema(
responses={
@@ -112,30 +104,18 @@ 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 NSLCMException(subscriptions_serializer.errors)
- logger.debug("SubscribeNotification--get::> Remove default fields if exclude_default is "
- "specified")
- return Response(data=subscriptions_serializer.data, status=status.HTTP_200_OK)
- except NSLCMException 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)
- except Exception as e:
- logger.error(e.message)
- logger.error(traceback.format_exc())
+
+ if request.query_params and not set(request.query_params).issubset(set(VALID_FILTERS)):
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)
+ 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 NSLCMException(subscriptions_serializer.errors)
+ logger.debug("SubscribeNotification--get::> Remove default fields if exclude_default is "
+ "specified")
+ return Response(data=subscriptions_serializer.data, status=status.HTTP_200_OK)
diff --git a/lcm/pub/exceptions.py b/lcm/pub/exceptions.py
index 6dd5dcbe..6078ce7d 100644
--- a/lcm/pub/exceptions.py
+++ b/lcm/pub/exceptions.py
@@ -19,3 +19,7 @@ class NSLCMException(Exception):
class BadRequestException(Exception):
pass
+
+
+class SeeOtherException(Exception):
+ pass