1 # Copyright (C) 2018 Verizon. All Rights Reserved
\r
3 # Licensed under the Apache License, Version 2.0 (the "License");
\r
4 # you may not use this file except in compliance with the License.
\r
5 # You may obtain a copy of the License at
\r
7 # http://www.apache.org/licenses/LICENSE-2.0
\r
9 # Unless required by applicable law or agreed to in writing, software
\r
10 # distributed under the License is distributed on an "AS IS" BASIS,
\r
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
12 # See the License for the specific language governing permissions and
\r
13 # limitations under the License.
\r
20 from drf_yasg.utils import swagger_auto_schema
\r
21 from lcm.nf.biz.create_subscription import CreateSubscription
\r
22 from rest_framework import status
\r
23 from rest_framework.response import Response
\r
24 from rest_framework.views import APIView
\r
26 from lcm.nf.serializers.lccn_subscription_request import LccnSubscriptionRequestSerializer
\r
27 from lcm.nf.serializers.lccn_subscription import LccnSubscriptionSerializer
\r
28 from lcm.pub.exceptions import NFLCMException
\r
30 logger = logging.getLogger(__name__)
\r
33 class SubscriptionsView(APIView):
\r
34 @swagger_auto_schema(
\r
35 request_body=LccnSubscriptionRequestSerializer(),
\r
37 status.HTTP_201_CREATED: LccnSubscriptionSerializer(),
\r
38 status.HTTP_303_SEE_OTHER: "",
\r
39 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
\r
42 def post(self, request):
\r
43 logger.debug("SubscribeNotification--post::> %s" % request.data)
\r
45 lccn_subscription_request_serializer = LccnSubscriptionRequestSerializer(data=request.data)
\r
46 if not lccn_subscription_request_serializer.is_valid():
\r
47 raise NFLCMException(lccn_subscription_request_serializer.errors)
\r
48 subscription = CreateSubscription(
\r
49 lccn_subscription_request_serializer.data).do_biz()
\r
50 lccn_notifications_filter = {
\r
51 "notificationTypes": ast.literal_eval(subscription.notification_types),
\r
52 "operationTypes": ast.literal_eval(subscription.operation_types),
\r
53 "operationStates": ast.literal_eval(subscription.operation_states),
\r
54 "vnfInstanceSubscriptionFilter": json.loads(subscription.vnf_instance_filter)
\r
56 subscription_data = {
\r
57 "id": subscription.subscription_id,
\r
58 "callbackUri": subscription.callback_uri,
\r
59 "_links": json.loads(subscription.links),
\r
60 "filter": lccn_notifications_filter
\r
62 sub_resp_serializer = LccnSubscriptionSerializer(data=subscription_data)
\r
63 if not sub_resp_serializer.is_valid():
\r
64 raise NFLCMException(sub_resp_serializer.errors)
\r
65 return Response(data=sub_resp_serializer.data, status=status.HTTP_201_CREATED)
\r
66 except NFLCMException as e:
\r
67 logger.error(e.message)
\r
68 if "exists" in e.message:
\r
69 return Response(data={'error': '%s' % e.message}, status=status.HTTP_303_SEE_OTHER)
\r
70 return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
\r
71 except Exception as e:
\r
72 logger.error(e.message)
\r
73 logger.error(traceback.format_exc())
\r
74 return Response(data={'error': e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
\r