Modify notification endpoint interface definition in swagger
[modeling/etsicatalog.git] / catalog / packages / views / vnf_package_subscription_views.py
1 # Copyright (C) 2019 Verizon. All Rights Reserved
2 #
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
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 import logging
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
21
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
36
37 logger = logging.getLogger(__name__)
38
39 VALID_FILTERS = [
40     "callbackUri",
41     "notificationTypes",
42     "vnfdId",
43     "vnfPkgId",
44     "operationalState",
45     "usageState"
46 ]
47
48
49 class CreateQuerySubscriptionView(APIView):
50
51     @swagger_auto_schema(
52         tags=[TAG_VNF_PACKAGE_API],
53         request_body=PkgmSubscriptionRequestSerializer,
54         responses={
55             status.HTTP_201_CREATED: PkgmSubscriptionSerializer(),
56             status.HTTP_400_BAD_REQUEST: ProblemDetailsSerializer(),
57             status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
58         }
59     )
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)
67
68     @swagger_auto_schema(
69         tags=[TAG_VNF_PACKAGE_API],
70         responses={
71             status.HTTP_200_OK: PkgmSubscriptionSerializer(),
72             status.HTTP_400_BAD_REQUEST: ProblemDetailsSerializer(),
73             status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
74         }
75     )
76     @view_safe_call_with_log(logger=logger)
77     def get(self, request):
78         logger.debug("SubscribeNotification--get::> %s" % request.query_params)
79
80         if request.query_params and not set(request.query_params).issubset(set(VALID_FILTERS)):
81             raise BadRequestException("Not a valid filter")
82
83         resp_data = QuerySubscription().query_multi_subscriptions(request.query_params)
84
85         subscriptions_serializer = PkgmSubscriptionsSerializer(data=resp_data)
86         if not subscriptions_serializer.is_valid():
87             raise VnfPkgSubscriptionException(subscriptions_serializer.errors)
88
89         return Response(data=subscriptions_serializer.data, status=status.HTTP_200_OK)
90
91
92 class QueryTerminateSubscriptionView(APIView):
93
94     @swagger_auto_schema(
95         tags=[TAG_VNF_PACKAGE_API],
96         responses={
97             status.HTTP_200_OK: PkgmSubscriptionSerializer(),
98             status.HTTP_404_NOT_FOUND: ProblemDetailsSerializer(),
99             status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
100         }
101     )
102     @view_safe_call_with_log(logger=logger)
103     def get(self, request, subscriptionId):
104         logger.debug("SubscribeNotification--get::> %s" % subscriptionId)
105
106         resp_data = QuerySubscription().query_single_subscription(subscriptionId)
107
108         subscription_serializer = PkgmSubscriptionSerializer(data=resp_data)
109         if not subscription_serializer.is_valid():
110             raise VnfPkgSubscriptionException(subscription_serializer.errors)
111
112         return Response(data=subscription_serializer.data, status=status.HTTP_200_OK)
113
114     @swagger_auto_schema(
115         tags=[TAG_VNF_PACKAGE_API],
116         responses={
117             status.HTTP_204_NO_CONTENT: "",
118             status.HTTP_404_NOT_FOUND: ProblemDetailsSerializer(),
119             status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
120         }
121     )
122     @view_safe_call_with_log(logger=logger)
123     def delete(self, request, subscriptionId):
124         logger.debug("SubscribeNotification--get::> %s" % subscriptionId)
125
126         TerminateSubscription().terminate(subscriptionId)
127         return Response(status=status.HTTP_204_NO_CONTENT)
128
129
130 class PkgOnboardingNotificationView(APIView):
131     @swagger_auto_schema(
132         tags=[TAG_VNF_PACKAGE_API],
133         request_body=PkgOnboardingNotificationSerializer,
134         responses={
135             status.HTTP_204_NO_CONTENT: ""
136         }
137     )
138     def post(self):
139         pass
140
141     @swagger_auto_schema(
142         tags=[TAG_VNF_PACKAGE_API],
143         responses={
144             status.HTTP_204_NO_CONTENT: "",
145             status.HTTP_500_INTERNAL_SERVER_ERROR: openapi.Response('error message',
146                                                                     openapi.Schema(type=openapi.TYPE_STRING))}
147     )
148     def get(self):
149         pass
150
151
152 class PkgChangeNotificationView(APIView):
153     @swagger_auto_schema(
154         tags=[TAG_VNF_PACKAGE_API],
155         request_body=PkgChangeNotificationSerializer,
156         responses={
157             status.HTTP_204_NO_CONTENT: ""
158         }
159     )
160     def post(self):
161         pass
162
163     @swagger_auto_schema(
164         tags=[TAG_VNF_PACKAGE_API],
165         responses={
166             status.HTTP_204_NO_CONTENT: "",
167             status.HTTP_500_INTERNAL_SERVER_ERROR: openapi.Response('error message',
168                                                                     openapi.Schema(type=openapi.TYPE_STRING))}
169     )
170     def get(self):
171         pass