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.
16 from drf_yasg import openapi
17 from drf_yasg.utils import swagger_auto_schema
18 from rest_framework import status
19 from rest_framework.response import Response
20 from rest_framework.views import APIView
22 from catalog.packages.biz.vnf_pkg_subscription import CreateSubscription
23 from catalog.packages.biz.vnf_pkg_subscription import QuerySubscription
24 from catalog.packages.biz.vnf_pkg_subscription import TerminateSubscription
25 from catalog.packages.const import TAG_VNF_PACKAGE_API
26 from catalog.packages.serializers.response import ProblemDetailsSerializer
27 from catalog.packages.serializers.vnf_pkg_subscription import PkgmSubscriptionRequestSerializer
28 from catalog.packages.serializers.vnf_pkg_subscription import PkgmSubscriptionSerializer
29 from catalog.packages.serializers.vnf_pkg_subscription import PkgmSubscriptionsSerializer
30 from catalog.packages.serializers.vnf_pkg_notifications import PkgOnboardingNotificationSerializer
31 from catalog.packages.serializers.vnf_pkg_notifications import PkgChangeNotificationSerializer
32 from catalog.packages.views.common import validate_data, validate_req_data
33 from catalog.pub.exceptions import BadRequestException
34 from catalog.pub.exceptions import VnfPkgSubscriptionException
35 from .common import view_safe_call_with_log
37 logger = logging.getLogger(__name__)
49 class CreateQuerySubscriptionView(APIView):
51 This resource represents subscriptions.
52 The client can use this resource to subscribe to notifications related to NS lifecycle management,
53 and to query its subscriptions.
57 tags=[TAG_VNF_PACKAGE_API],
58 request_body=PkgmSubscriptionRequestSerializer,
60 status.HTTP_201_CREATED: PkgmSubscriptionSerializer(),
61 status.HTTP_400_BAD_REQUEST: ProblemDetailsSerializer(),
62 status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
65 @view_safe_call_with_log(logger=logger)
66 def post(self, request):
68 The POST method creates a new subscription
72 logger.debug("Create VNF package Subscription> %s" % request.data)
73 vnf_pkg_subscription_request = validate_req_data(request.data, PkgmSubscriptionRequestSerializer)
74 data = CreateSubscription(vnf_pkg_subscription_request.data).do_biz()
75 subscription_info = validate_data(data, PkgmSubscriptionSerializer)
76 return Response(data=subscription_info.data, status=status.HTTP_201_CREATED)
79 tags=[TAG_VNF_PACKAGE_API],
81 status.HTTP_200_OK: PkgmSubscriptionSerializer(),
82 status.HTTP_400_BAD_REQUEST: ProblemDetailsSerializer(),
83 status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
86 @view_safe_call_with_log(logger=logger)
87 def get(self, request):
89 The GET method queries the list of active subscriptions of the functional block that invokes the method.
90 It can be used e.g. for resynchronization after error situations.
94 logger.debug("SubscribeNotification--get::> %s" % request.query_params)
96 if request.query_params and not set(request.query_params).issubset(set(VALID_FILTERS)):
97 raise BadRequestException("Not a valid filter")
99 resp_data = QuerySubscription().query_multi_subscriptions(request.query_params)
101 subscriptions_serializer = PkgmSubscriptionsSerializer(data=resp_data)
102 if not subscriptions_serializer.is_valid():
103 raise VnfPkgSubscriptionException(subscriptions_serializer.errors)
105 return Response(data=subscriptions_serializer.data, status=status.HTTP_200_OK)
108 class QueryTerminateSubscriptionView(APIView):
110 This resource represents an individual subscription.
111 It can be used by the client to read and to terminate a subscription to Notifications related to NS lifecycle management.
114 @swagger_auto_schema(
115 tags=[TAG_VNF_PACKAGE_API],
117 status.HTTP_200_OK: PkgmSubscriptionSerializer(),
118 status.HTTP_404_NOT_FOUND: ProblemDetailsSerializer(),
119 status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
122 @view_safe_call_with_log(logger=logger)
123 def get(self, request, subscriptionId):
125 The GET method retrieves information about a subscription by reading an individual subscription resource.
127 :param subscriptionId:
130 logger.debug("SubscribeNotification--get::> %s" % subscriptionId)
132 resp_data = QuerySubscription().query_single_subscription(subscriptionId)
134 subscription_serializer = PkgmSubscriptionSerializer(data=resp_data)
135 if not subscription_serializer.is_valid():
136 raise VnfPkgSubscriptionException(subscription_serializer.errors)
138 return Response(data=subscription_serializer.data, status=status.HTTP_200_OK)
140 @swagger_auto_schema(
141 tags=[TAG_VNF_PACKAGE_API],
143 status.HTTP_204_NO_CONTENT: "",
144 status.HTTP_404_NOT_FOUND: ProblemDetailsSerializer(),
145 status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
148 @view_safe_call_with_log(logger=logger)
149 def delete(self, request, subscriptionId):
151 The DELETE method terminates an individual subscription.
153 :param subscriptionId:
156 logger.debug("SubscribeNotification--get::> %s" % subscriptionId)
158 TerminateSubscription().terminate(subscriptionId)
159 return Response(status=status.HTTP_204_NO_CONTENT)
162 class PkgOnboardingNotificationView(APIView):
164 This resource represents a notification endpoint about package onboarding
167 @swagger_auto_schema(
168 tags=[TAG_VNF_PACKAGE_API],
169 request_body=PkgOnboardingNotificationSerializer,
171 status.HTTP_204_NO_CONTENT: ""
177 @swagger_auto_schema(
178 tags=[TAG_VNF_PACKAGE_API],
180 status.HTTP_204_NO_CONTENT: "",
181 status.HTTP_500_INTERNAL_SERVER_ERROR: openapi.Response('error message',
182 openapi.Schema(type=openapi.TYPE_STRING))}
188 class PkgChangeNotificationView(APIView):
190 This resource represents a notification endpoint about package change
193 @swagger_auto_schema(
194 tags=[TAG_VNF_PACKAGE_API],
195 request_body=PkgChangeNotificationSerializer,
197 status.HTTP_204_NO_CONTENT: ""
203 @swagger_auto_schema(
204 tags=[TAG_VNF_PACKAGE_API],
206 status.HTTP_204_NO_CONTENT: "",
207 status.HTTP_500_INTERNAL_SERVER_ERROR: openapi.Response('error message',
208 openapi.Schema(type=openapi.TYPE_STRING))}