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):
52 tags=[TAG_VNF_PACKAGE_API],
53 request_body=PkgmSubscriptionRequestSerializer,
55 status.HTTP_201_CREATED: PkgmSubscriptionSerializer(),
56 status.HTTP_400_BAD_REQUEST: ProblemDetailsSerializer(),
57 status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
60 @view_safe_call_with_log(logger=logger)
61 def post(self, request):
62 logger.debug("Create VNF package Subscription> %s" % request.data)
63 vnf_pkg_subscription_request = validate_req_data(request.data, PkgmSubscriptionRequestSerializer)
64 data = CreateSubscription(vnf_pkg_subscription_request.data).do_biz()
65 subscription_info = validate_data(data, PkgmSubscriptionSerializer)
66 return Response(data=subscription_info.data, status=status.HTTP_201_CREATED)
69 tags=[TAG_VNF_PACKAGE_API],
71 status.HTTP_200_OK: PkgmSubscriptionSerializer(),
72 status.HTTP_400_BAD_REQUEST: ProblemDetailsSerializer(),
73 status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
76 @view_safe_call_with_log(logger=logger)
77 def get(self, request):
78 logger.debug("SubscribeNotification--get::> %s" % request.query_params)
80 if request.query_params and not set(request.query_params).issubset(set(VALID_FILTERS)):
81 raise BadRequestException("Not a valid filter")
83 resp_data = QuerySubscription().query_multi_subscriptions(request.query_params)
85 subscriptions_serializer = PkgmSubscriptionsSerializer(data=resp_data)
86 if not subscriptions_serializer.is_valid():
87 raise VnfPkgSubscriptionException(subscriptions_serializer.errors)
89 return Response(data=subscriptions_serializer.data, status=status.HTTP_200_OK)
92 class QueryTerminateSubscriptionView(APIView):
95 tags=[TAG_VNF_PACKAGE_API],
97 status.HTTP_200_OK: PkgmSubscriptionSerializer(),
98 status.HTTP_404_NOT_FOUND: ProblemDetailsSerializer(),
99 status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
102 @view_safe_call_with_log(logger=logger)
103 def get(self, request, subscriptionId):
104 logger.debug("SubscribeNotification--get::> %s" % subscriptionId)
106 resp_data = QuerySubscription().query_single_subscription(subscriptionId)
108 subscription_serializer = PkgmSubscriptionSerializer(data=resp_data)
109 if not subscription_serializer.is_valid():
110 raise VnfPkgSubscriptionException(subscription_serializer.errors)
112 return Response(data=subscription_serializer.data, status=status.HTTP_200_OK)
114 @swagger_auto_schema(
115 tags=[TAG_VNF_PACKAGE_API],
117 status.HTTP_204_NO_CONTENT: "",
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 delete(self, request, subscriptionId):
124 logger.debug("SubscribeNotification--get::> %s" % subscriptionId)
126 TerminateSubscription().terminate(subscriptionId)
127 return Response(status=status.HTTP_204_NO_CONTENT)
130 class PkgOnboardingNotificationView(APIView):
131 @swagger_auto_schema(
132 tags=[TAG_VNF_PACKAGE_API],
133 request_body=PkgOnboardingNotificationSerializer,
135 status.HTTP_204_NO_CONTENT: ""
141 @swagger_auto_schema(
142 tags=[TAG_VNF_PACKAGE_API],
144 status.HTTP_204_NO_CONTENT: "",
145 status.HTTP_500_INTERNAL_SERVER_ERROR: openapi.Response('error message',
146 openapi.Schema(type=openapi.TYPE_STRING))}
152 class PkgChangeNotificationView(APIView):
153 @swagger_auto_schema(
154 tags=[TAG_VNF_PACKAGE_API],
155 request_body=PkgChangeNotificationSerializer,
157 status.HTTP_204_NO_CONTENT: ""
163 @swagger_auto_schema(
164 tags=[TAG_VNF_PACKAGE_API],
166 status.HTTP_204_NO_CONTENT: "",
167 status.HTTP_500_INTERNAL_SERVER_ERROR: openapi.Response('error message',
168 openapi.Schema(type=openapi.TYPE_STRING))}