Add log and comment
[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     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.
54     """
55
56     @swagger_auto_schema(
57         tags=[TAG_VNF_PACKAGE_API],
58         request_body=PkgmSubscriptionRequestSerializer,
59         responses={
60             status.HTTP_201_CREATED: PkgmSubscriptionSerializer(),
61             status.HTTP_400_BAD_REQUEST: ProblemDetailsSerializer(),
62             status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
63         }
64     )
65     @view_safe_call_with_log(logger=logger)
66     def post(self, request):
67         """
68         The POST method creates a new subscription
69         :param request:
70         :return:
71         """
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)
77
78     @swagger_auto_schema(
79         tags=[TAG_VNF_PACKAGE_API],
80         responses={
81             status.HTTP_200_OK: PkgmSubscriptionSerializer(),
82             status.HTTP_400_BAD_REQUEST: ProblemDetailsSerializer(),
83             status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
84         }
85     )
86     @view_safe_call_with_log(logger=logger)
87     def get(self, request):
88         """
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.
91         :param request:
92         :return:
93         """
94         logger.debug("SubscribeNotification--get::> %s" % request.query_params)
95
96         if request.query_params and not set(request.query_params).issubset(set(VALID_FILTERS)):
97             raise BadRequestException("Not a valid filter")
98
99         resp_data = QuerySubscription().query_multi_subscriptions(request.query_params)
100
101         subscriptions_serializer = PkgmSubscriptionsSerializer(data=resp_data)
102         if not subscriptions_serializer.is_valid():
103             raise VnfPkgSubscriptionException(subscriptions_serializer.errors)
104
105         return Response(data=subscriptions_serializer.data, status=status.HTTP_200_OK)
106
107
108 class QueryTerminateSubscriptionView(APIView):
109     """
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.
112     """
113
114     @swagger_auto_schema(
115         tags=[TAG_VNF_PACKAGE_API],
116         responses={
117             status.HTTP_200_OK: PkgmSubscriptionSerializer(),
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 get(self, request, subscriptionId):
124         """
125         The GET method retrieves information about a subscription by reading an individual subscription resource.
126         :param request:
127         :param subscriptionId:
128         :return:
129         """
130         logger.debug("SubscribeNotification--get::> %s" % subscriptionId)
131
132         resp_data = QuerySubscription().query_single_subscription(subscriptionId)
133
134         subscription_serializer = PkgmSubscriptionSerializer(data=resp_data)
135         if not subscription_serializer.is_valid():
136             raise VnfPkgSubscriptionException(subscription_serializer.errors)
137
138         return Response(data=subscription_serializer.data, status=status.HTTP_200_OK)
139
140     @swagger_auto_schema(
141         tags=[TAG_VNF_PACKAGE_API],
142         responses={
143             status.HTTP_204_NO_CONTENT: "",
144             status.HTTP_404_NOT_FOUND: ProblemDetailsSerializer(),
145             status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
146         }
147     )
148     @view_safe_call_with_log(logger=logger)
149     def delete(self, request, subscriptionId):
150         """
151         The DELETE method terminates an individual subscription.
152         :param request:
153         :param subscriptionId:
154         :return:
155         """
156         logger.debug("SubscribeNotification--get::> %s" % subscriptionId)
157
158         TerminateSubscription().terminate(subscriptionId)
159         return Response(status=status.HTTP_204_NO_CONTENT)
160
161
162 class PkgOnboardingNotificationView(APIView):
163     """
164     This resource represents a notification endpoint about package onboarding
165     """
166
167     @swagger_auto_schema(
168         tags=[TAG_VNF_PACKAGE_API],
169         request_body=PkgOnboardingNotificationSerializer,
170         responses={
171             status.HTTP_204_NO_CONTENT: ""
172         }
173     )
174     def post(self):
175         pass
176
177     @swagger_auto_schema(
178         tags=[TAG_VNF_PACKAGE_API],
179         responses={
180             status.HTTP_204_NO_CONTENT: "",
181             status.HTTP_500_INTERNAL_SERVER_ERROR: openapi.Response('error message',
182                                                                     openapi.Schema(type=openapi.TYPE_STRING))}
183     )
184     def get(self):
185         pass
186
187
188 class PkgChangeNotificationView(APIView):
189     """
190     This resource represents a notification endpoint about package change
191     """
192
193     @swagger_auto_schema(
194         tags=[TAG_VNF_PACKAGE_API],
195         request_body=PkgChangeNotificationSerializer,
196         responses={
197             status.HTTP_204_NO_CONTENT: ""
198         }
199     )
200     def post(self):
201         pass
202
203     @swagger_auto_schema(
204         tags=[TAG_VNF_PACKAGE_API],
205         responses={
206             status.HTTP_204_NO_CONTENT: "",
207             status.HTTP_500_INTERNAL_SERVER_ERROR: openapi.Response('error message',
208                                                                     openapi.Schema(type=openapi.TYPE_STRING))}
209     )
210     def get(self):
211         pass