1 # Copyright (C) 2019 Verizon. All Rights Reserved
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.
19 from drf_yasg.utils import swagger_auto_schema
20 from rest_framework import status
21 from rest_framework.views import APIView
22 from rest_framework.response import Response
24 from genericparser.packages.serializers.vnf_pkg_subscription import PkgmSubscriptionRequestSerializer, \
25 PkgmSubscriptionSerializer, PkgmSubscriptionsSerializer
26 from genericparser.packages.serializers.response import ProblemDetailsSerializer
27 from genericparser.packages.biz.vnf_pkg_subscription import CreateSubscription, QuerySubscription, TerminateSubscription
28 from genericparser.packages.views.common import validate_data
29 from genericparser.pub.exceptions import VnfPkgDuplicateSubscriptionException, VnfPkgSubscriptionException, \
30 SubscriptionDoesNotExistsException
32 logger = logging.getLogger(__name__)
33 VALID_FILTERS = ["callbackUri", "notificationTypes", "vnfdId", "vnfPkgId", "operationalState", "usageState"]
36 def get_problem_details_serializer(status_code, error_message):
38 "status": status_code,
39 "detail": error_message
41 problem_details_serializer = ProblemDetailsSerializer(data=problem_details)
42 problem_details_serializer.is_valid()
43 return problem_details_serializer
46 class CreateQuerySubscriptionView(APIView):
49 request_body=PkgmSubscriptionRequestSerializer,
51 status.HTTP_201_CREATED: PkgmSubscriptionSerializer(),
52 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
55 def post(self, request):
56 logger.debug("Create VNF package Subscription> %s" % request.data)
58 vnf_pkg_subscription_request = validate_data(request.data, PkgmSubscriptionRequestSerializer)
59 data = CreateSubscription(vnf_pkg_subscription_request.data).do_biz()
60 subscription_info = validate_data(data, PkgmSubscriptionSerializer)
61 return Response(data=subscription_info.data, status=status.HTTP_201_CREATED)
62 except VnfPkgDuplicateSubscriptionException as e:
63 logger.error(e.message)
64 logger.error(traceback.format_exc())
65 problem_details_serializer = get_problem_details_serializer(status.HTTP_303_SEE_OTHER,
66 traceback.format_exc())
67 return Response(data=problem_details_serializer.data, status=status.HTTP_303_SEE_OTHER)
68 except Exception as e:
69 logger.error(e.message)
70 logger.error(traceback.format_exc())
71 problem_details_serializer = get_problem_details_serializer(status.HTTP_500_INTERNAL_SERVER_ERROR,
72 traceback.format_exc())
73 return Response(data=problem_details_serializer.data, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
77 status.HTTP_200_OK: PkgmSubscriptionSerializer(),
78 status.HTTP_400_BAD_REQUEST: ProblemDetailsSerializer(),
79 status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
82 def get(self, request):
83 logger.debug("SubscribeNotification--get::> %s" % request.query_params)
85 if request.query_params and not set(request.query_params).issubset(set(VALID_FILTERS)):
86 problem_details_serializer = get_problem_details_serializer(status.HTTP_400_BAD_REQUEST,
88 return Response(data=problem_details_serializer.data, status=status.HTTP_400_BAD_REQUEST)
89 resp_data = QuerySubscription().query_multi_subscriptions(request.query_params)
91 subscriptions_serializer = PkgmSubscriptionsSerializer(data=resp_data)
92 if not subscriptions_serializer.is_valid():
93 raise VnfPkgSubscriptionException(subscriptions_serializer.errors)
95 return Response(data=subscriptions_serializer.data, status=status.HTTP_200_OK)
97 except Exception as e:
98 logger.error(e.message)
99 logger.error(traceback.format_exc())
100 problem_details_serializer = get_problem_details_serializer(status.HTTP_500_INTERNAL_SERVER_ERROR,
101 traceback.format_exc())
102 return Response(data=problem_details_serializer.data, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
105 class QueryTerminateSubscriptionView(APIView):
107 @swagger_auto_schema(
109 status.HTTP_200_OK: PkgmSubscriptionSerializer(),
110 status.HTTP_404_NOT_FOUND: ProblemDetailsSerializer(),
111 status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
114 def get(self, request, subscriptionId):
115 logger.debug("SubscribeNotification--get::> %s" % subscriptionId)
118 resp_data = QuerySubscription().query_single_subscription(subscriptionId)
120 subscription_serializer = PkgmSubscriptionSerializer(data=resp_data)
121 if not subscription_serializer.is_valid():
122 raise VnfPkgSubscriptionException(subscription_serializer.errors)
124 return Response(data=subscription_serializer.data, status=status.HTTP_200_OK)
125 except SubscriptionDoesNotExistsException as e:
126 logger.error(e.message)
127 logger.error(traceback.format_exc())
128 problem_details_serializer = get_problem_details_serializer(status.HTTP_404_NOT_FOUND,
129 traceback.format_exc())
130 return Response(data=problem_details_serializer.data, status=status.HTTP_404_NOT_FOUND)
131 except Exception as e:
132 logger.error(e.message)
133 logger.error(traceback.format_exc())
134 problem_details_serializer = get_problem_details_serializer(status.HTTP_500_INTERNAL_SERVER_ERROR,
135 traceback.format_exc())
136 return Response(data=problem_details_serializer.data, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
138 @swagger_auto_schema(
140 status.HTTP_204_NO_CONTENT: "",
141 status.HTTP_404_NOT_FOUND: ProblemDetailsSerializer(),
142 status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
145 def delete(self, request, subscriptionId):
146 logger.debug("SubscribeNotification--get::> %s" % subscriptionId)
148 TerminateSubscription().terminate(subscriptionId)
149 return Response(status=status.HTTP_204_NO_CONTENT)
150 except SubscriptionDoesNotExistsException as e:
151 logger.error(e.message)
152 logger.error(traceback.format_exc())
153 problem_details_serializer = get_problem_details_serializer(status.HTTP_404_NOT_FOUND,
154 traceback.format_exc())
155 return Response(data=problem_details_serializer.data, status=status.HTTP_404_NOT_FOUND)
156 except Exception as e:
157 logger.error(e.message)
158 logger.error(traceback.format_exc())
159 problem_details_serializer = get_problem_details_serializer(status.HTTP_500_INTERNAL_SERVER_ERROR,
160 traceback.format_exc())
161 return Response(data=problem_details_serializer.data, status=status.HTTP_500_INTERNAL_SERVER_ERROR)