aboutsummaryrefslogtreecommitdiffstats
path: root/catalog/packages/views/vnf_package_subscription_views.py
blob: 6698429ebc93bcb66da3f307d8cf4fe2c2594cd3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# Copyright (C) 2019 Verizon. All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import logging
from drf_yasg import openapi
from drf_yasg.utils import swagger_auto_schema
from rest_framework import status
from rest_framework.response import Response
from rest_framework.views import APIView

from catalog.packages.biz.vnf_pkg_subscription import CreateSubscription
from catalog.packages.biz.vnf_pkg_subscription import QuerySubscription
from catalog.packages.biz.vnf_pkg_subscription import TerminateSubscription
from catalog.packages.const import TAG_VNF_PACKAGE_API
from catalog.packages.serializers.response import ProblemDetailsSerializer
from catalog.packages.serializers.vnf_pkg_subscription import PkgmSubscriptionRequestSerializer
from catalog.packages.serializers.vnf_pkg_subscription import PkgmSubscriptionSerializer
from catalog.packages.serializers.vnf_pkg_subscription import PkgmSubscriptionsSerializer
from catalog.packages.serializers.vnf_pkg_notifications import PkgOnboardingNotificationSerializer
from catalog.packages.serializers.vnf_pkg_notifications import PkgChangeNotificationSerializer
from catalog.packages.views.common import validate_data, validate_req_data
from catalog.pub.exceptions import BadRequestException
from catalog.pub.exceptions import VnfPkgSubscriptionException
from .common import view_safe_call_with_log

logger = logging.getLogger(__name__)

VALID_FILTERS = [
    "callbackUri",
    "notificationTypes",
    "vnfdId",
    "vnfPkgId",
    "operationalState",
    "usageState"
]


class CreateQuerySubscriptionView(APIView):

    @swagger_auto_schema(
        tags=[TAG_VNF_PACKAGE_API],
        request_body=PkgmSubscriptionRequestSerializer,
        responses={
            status.HTTP_201_CREATED: PkgmSubscriptionSerializer(),
            status.HTTP_400_BAD_REQUEST: ProblemDetailsSerializer(),
            status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
        }
    )
    @view_safe_call_with_log(logger=logger)
    def post(self, request):
        logger.debug("Create VNF package Subscription> %s" % request.data)
        vnf_pkg_subscription_request = validate_req_data(request.data, PkgmSubscriptionRequestSerializer)
        data = CreateSubscription(vnf_pkg_subscription_request.data).do_biz()
        subscription_info = validate_data(data, PkgmSubscriptionSerializer)
        return Response(data=subscription_info.data, status=status.HTTP_201_CREATED)

    @swagger_auto_schema(
        tags=[TAG_VNF_PACKAGE_API],
        responses={
            status.HTTP_200_OK: PkgmSubscriptionSerializer(),
            status.HTTP_400_BAD_REQUEST: ProblemDetailsSerializer(),
            status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
        }
    )
    @view_safe_call_with_log(logger=logger)
    def get(self, request):
        logger.debug("SubscribeNotification--get::> %s" % request.query_params)

        if request.query_params and not set(request.query_params).issubset(set(VALID_FILTERS)):
            raise BadRequestException("Not a valid filter")

        resp_data = QuerySubscription().query_multi_subscriptions(request.query_params)

        subscriptions_serializer = PkgmSubscriptionsSerializer(data=resp_data)
        if not subscriptions_serializer.is_valid():
            raise VnfPkgSubscriptionException(subscriptions_serializer.errors)

        return Response(data=subscriptions_serializer.data, status=status.HTTP_200_OK)


class QueryTerminateSubscriptionView(APIView):

    @swagger_auto_schema(
        tags=[TAG_VNF_PACKAGE_API],
        responses={
            status.HTTP_200_OK: PkgmSubscriptionSerializer(),
            status.HTTP_404_NOT_FOUND: ProblemDetailsSerializer(),
            status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
        }
    )
    @view_safe_call_with_log(logger=logger)
    def get(self, request, subscriptionId):
        logger.debug("SubscribeNotification--get::> %s" % subscriptionId)

        resp_data = QuerySubscription().query_single_subscription(subscriptionId)

        subscription_serializer = PkgmSubscriptionSerializer(data=resp_data)
        if not subscription_serializer.is_valid():
            raise VnfPkgSubscriptionException(subscription_serializer.errors)

        return Response(data=subscription_serializer.data, status=status.HTTP_200_OK)

    @swagger_auto_schema(
        tags=[TAG_VNF_PACKAGE_API],
        responses={
            status.HTTP_204_NO_CONTENT: "",
            status.HTTP_404_NOT_FOUND: ProblemDetailsSerializer(),
            status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
        }
    )
    @view_safe_call_with_log(logger=logger)
    def delete(self, request, subscriptionId):
        logger.debug("SubscribeNotification--get::> %s" % subscriptionId)

        TerminateSubscription().terminate(subscriptionId)
        return Response(status=status.HTTP_204_NO_CONTENT)


class PkgOnboardingNotificationView(APIView):
    @swagger_auto_schema(
        tags=[TAG_VNF_PACKAGE_API],
        request_body=PkgOnboardingNotificationSerializer,
        responses={
            status.HTTP_204_NO_CONTENT: ""
        }
    )
    def post(self):
        pass

    @swagger_auto_schema(
        tags=[TAG_VNF_PACKAGE_API],
        responses={
            status.HTTP_204_NO_CONTENT: "",
            status.HTTP_500_INTERNAL_SERVER_ERROR: openapi.Response('error message',
                                                                    openapi.Schema(type=openapi.TYPE_STRING))}
    )
    def get(self):
        pass


class PkgChangeNotificationView(APIView):
    @swagger_auto_schema(
        tags=[TAG_VNF_PACKAGE_API],
        request_body=PkgChangeNotificationSerializer,
        responses={
            status.HTTP_204_NO_CONTENT: ""
        }
    )
    def post(self):
        pass

    @swagger_auto_schema(
        tags=[TAG_VNF_PACKAGE_API],
        responses={
            status.HTTP_204_NO_CONTENT: "",
            status.HTTP_500_INTERNAL_SERVER_ERROR: openapi.Response('error message',
                                                                    openapi.Schema(type=openapi.TYPE_STRING))}
    )
    def get(self):
        pass