897b43c23103ecdf5211c7381af1b47a14400af9
[modeling/etsicatalog.git] / genericparser / 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
16 import traceback
17 import logging
18
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
23
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
31
32 logger = logging.getLogger(__name__)
33 VALID_FILTERS = ["callbackUri", "notificationTypes", "vnfdId", "vnfPkgId", "operationalState", "usageState"]
34
35
36 def get_problem_details_serializer(status_code, error_message):
37     problem_details = {
38         "status": status_code,
39         "detail": error_message
40     }
41     problem_details_serializer = ProblemDetailsSerializer(data=problem_details)
42     problem_details_serializer.is_valid()
43     return problem_details_serializer
44
45
46 class CreateQuerySubscriptionView(APIView):
47
48     @swagger_auto_schema(
49         request_body=PkgmSubscriptionRequestSerializer,
50         responses={
51             status.HTTP_201_CREATED: PkgmSubscriptionSerializer(),
52             status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
53         }
54     )
55     def post(self, request):
56         logger.debug("Create VNF package Subscription> %s" % request.data)
57         try:
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)
74
75     @swagger_auto_schema(
76         responses={
77             status.HTTP_200_OK: PkgmSubscriptionSerializer(),
78             status.HTTP_400_BAD_REQUEST: ProblemDetailsSerializer(),
79             status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
80         }
81     )
82     def get(self, request):
83         logger.debug("SubscribeNotification--get::> %s" % request.query_params)
84         try:
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,
87                                                                             "Not a valid filter")
88                 return Response(data=problem_details_serializer.data, status=status.HTTP_400_BAD_REQUEST)
89             resp_data = QuerySubscription().query_multi_subscriptions(request.query_params)
90
91             subscriptions_serializer = PkgmSubscriptionsSerializer(data=resp_data)
92             if not subscriptions_serializer.is_valid():
93                 raise VnfPkgSubscriptionException(subscriptions_serializer.errors)
94
95             return Response(data=subscriptions_serializer.data, status=status.HTTP_200_OK)
96
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)
103
104
105 class QueryTerminateSubscriptionView(APIView):
106
107     @swagger_auto_schema(
108         responses={
109             status.HTTP_200_OK: PkgmSubscriptionSerializer(),
110             status.HTTP_404_NOT_FOUND: ProblemDetailsSerializer(),
111             status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
112         }
113     )
114     def get(self, request, subscriptionId):
115         logger.debug("SubscribeNotification--get::> %s" % subscriptionId)
116         try:
117
118             resp_data = QuerySubscription().query_single_subscription(subscriptionId)
119
120             subscription_serializer = PkgmSubscriptionSerializer(data=resp_data)
121             if not subscription_serializer.is_valid():
122                 raise VnfPkgSubscriptionException(subscription_serializer.errors)
123
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)
137
138     @swagger_auto_schema(
139         responses={
140             status.HTTP_204_NO_CONTENT: "",
141             status.HTTP_404_NOT_FOUND: ProblemDetailsSerializer(),
142             status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
143         }
144     )
145     def delete(self, request, subscriptionId):
146         logger.debug("SubscribeNotification--get::> %s" % subscriptionId)
147         try:
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)