1 # Copyright (c) 2019, CMCC Technologies Co., Ltd.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
20 from drf_yasg.utils import swagger_auto_schema
21 from lcm.ns.serializers.sol.lccn_subscription import LccnSubscriptionSerializer
22 from lcm.ns.serializers.sol.lccn_subscription import LccnSubscriptionsSerializer
23 from rest_framework import status
24 from rest_framework.response import Response
25 from rest_framework.views import APIView
27 from lcm.ns.biz.create_subscription import CreateSubscription
28 from lcm.ns.biz.query_subscription import QuerySubscription
29 from lcm.ns.serializers.sol.lccn_subscription_request import LccnSubscriptionRequestSerializer
30 from lcm.ns.serializers.sol.pub_serializers import ProblemDetailsSerializer
31 from lcm.pub.exceptions import NSLCMException
32 from lcm.pub.exceptions import BadRequestException
34 logger = logging.getLogger(__name__)
35 VALID_FILTERS = ["operationTypes", "operationStates", "notificationTypes", "nsInstanceId",
36 "nsComponentTypes", "lcmOpNameImpactingNsComponent", "lcmOpOccStatusImpactingNsComponent"]
39 def get_problem_details_serializer(status_code, error_message):
41 "status": status_code,
42 "detail": error_message
44 problem_details_serializer = ProblemDetailsSerializer(data=problem_details)
45 problem_details_serializer.is_valid()
46 return problem_details_serializer
49 class SubscriptionsView(APIView):
52 request_body=LccnSubscriptionRequestSerializer(),
54 status.HTTP_201_CREATED: LccnSubscriptionSerializer(),
55 status.HTTP_303_SEE_OTHER: ProblemDetailsSerializer(),
56 status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
59 def post(self, request):
60 logger.debug("SubscribeNotification--post::> %s" % request.data)
62 lccn_subscription_request_serializer = LccnSubscriptionRequestSerializer(
64 if not lccn_subscription_request_serializer.is_valid():
65 raise BadRequestException(
66 lccn_subscription_request_serializer.errors)
67 subscription = CreateSubscription(
68 lccn_subscription_request_serializer.data).do_biz()
69 lccn_notifications_filter = {
70 "notificationTypes": ast.literal_eval(subscription.notification_types),
71 "operationTypes": ast.literal_eval(subscription.operation_types),
72 "operationStates": ast.literal_eval(subscription.operation_states),
73 "nsInstanceSubscriptionFilter": json.loads(subscription.ns_instance_filter),
74 "nsComponentTypes": ast.literal_eval(subscription.ns_component_types),
75 "lcmOpNameImpactingNsComponent": ast.literal_eval(subscription.
76 lcm_opname_impacting_nscomponent),
77 "lcmOpOccStatusImpactingNsComponent": ast.literal_eval(subscription.
78 lcm_opoccstatus_impacting_nscomponent)
81 "id": subscription.subscription_id,
82 "callbackUri": subscription.callback_uri,
83 "_links": json.loads(subscription.links),
84 "filter": lccn_notifications_filter
86 sub_resp_serializer = LccnSubscriptionSerializer(
87 data=subscription_data)
88 if not sub_resp_serializer.is_valid():
89 raise NSLCMException(sub_resp_serializer.errors)
90 return Response(data=sub_resp_serializer.data, status=status.HTTP_201_CREATED)
91 except BadRequestException as e:
92 logger.error("Exception in InstantiateNsView: %s", e.message)
93 data = {'status': status.HTTP_400_BAD_REQUEST, 'detail': e.message}
94 return Response(data=data, status=status.HTTP_400_BAD_REQUEST)
95 except NSLCMException as e:
96 logger.error(e.message)
97 if "exists" in e.message:
98 data = {'status': status.HTTP_303_SEE_OTHER, 'detail': e.message}
99 return Response(data=data, status=status.HTTP_303_SEE_OTHER)
100 data = {'status': status.HTTP_500_INTERNAL_SERVER_ERROR, 'detail': e.message}
101 return Response(data=data, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
102 except Exception as e:
103 logger.error(e.message)
104 logger.error(traceback.format_exc())
105 data = {'status': status.HTTP_500_INTERNAL_SERVER_ERROR, 'detail': e.message}
106 return Response(data=data, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
108 @swagger_auto_schema(
110 status.HTTP_200_OK: LccnSubscriptionsSerializer(),
111 status.HTTP_400_BAD_REQUEST: ProblemDetailsSerializer(),
112 status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
115 def get(self, request):
116 logger.debug("SubscribeNotification--get::> %s" % request.query_params)
118 if request.query_params and not set(request.query_params).issubset(set(VALID_FILTERS)):
119 problem_details_serializer = get_problem_details_serializer(
120 status.HTTP_400_BAD_REQUEST, "Not a valid filter")
121 return Response(data=problem_details_serializer.data, status=status.HTTP_400_BAD_REQUEST)
122 resp_data = QuerySubscription(request.query_params).query_multi_subscriptions()
123 subscriptions_serializer = LccnSubscriptionsSerializer(data=resp_data)
124 if not subscriptions_serializer.is_valid():
125 raise NSLCMException(subscriptions_serializer.errors)
126 logger.debug("SubscribeNotification--get::> Remove default fields if exclude_default is "
128 return Response(data=subscriptions_serializer.data, status=status.HTTP_200_OK)
129 except NSLCMException as e:
130 logger.error(e.message)
131 problem_details_serializer = get_problem_details_serializer(
132 status.HTTP_500_INTERNAL_SERVER_ERROR, traceback.format_exc())
133 return Response(data=problem_details_serializer.data,
134 status=status.HTTP_500_INTERNAL_SERVER_ERROR)
135 except Exception as e:
136 logger.error(e.message)
137 logger.error(traceback.format_exc())
138 problem_details_serializer = get_problem_details_serializer(
139 status.HTTP_500_INTERNAL_SERVER_ERROR, traceback.format_exc())
140 return Response(data=problem_details_serializer.data,
141 status=status.HTTP_500_INTERNAL_SERVER_ERROR)