SOL003 API Align
[vfc/nfvo/lcm.git] / lcm / ns_vnfs / views / grant_vnf_views.py
1 # Copyright 2018 ZTE Corporation.
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 import logging
15 import traceback
16
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 lcm.ns_vnfs.biz.grant_vnf import GrantVnf
23 from lcm.ns_vnfs.biz.handle_vnflcmooc_notification import HandleVnfLcmOocNotification
24 from lcm.ns_vnfs.serializers.grant_vnf_serializer import GrantRequestSerializer
25 from lcm.ns_vnfs.serializers.grant_vnf_serializer import GrantSerializer
26 from lcm.ns_vnfs.serializers.grant_vnf_serializer import VnfLcmOperationOccurrenceNotificationSerializer
27
28 logger = logging.getLogger(__name__)
29
30
31 class VnfGrantView(APIView):
32     @swagger_auto_schema(
33         request_body=GrantRequestSerializer(),
34         responses={
35             status.HTTP_201_CREATED: GrantSerializer(
36                 help_text="The grant was created successfully (synchronous mode)."
37             ),
38             status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error"
39         }
40     )
41     def post(self, request):
42         logger.debug("VnfGrantView Post: %s" % request.data)
43         try:
44             req_serializer = GrantRequestSerializer(data=request.data)
45             if not req_serializer.is_valid():
46                 raise Exception(req_serializer.errors)
47
48             grant_resp = GrantVnf(request.data).exec_grant()
49
50             resp_serializer = GrantSerializer(data=grant_resp)
51             if not resp_serializer.is_valid():
52                 raise Exception(resp_serializer.errors)
53
54             return Response(data=resp_serializer.data, status=status.HTTP_201_CREATED)
55         except Exception as e:
56             logger.error(traceback.format_exc())
57             logger.error("Exception in VnfGrant: %s", e.message)
58             return Response(data={'error': e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
59
60
61 class VnfNotifyView(APIView):
62     @swagger_auto_schema(
63         request_body=VnfLcmOperationOccurrenceNotificationSerializer(
64             help_text="A notification about lifecycle changes triggered by a VNF LCM operation occurrence."
65         ),
66         responses={
67             status.HTTP_204_NO_CONTENT: "The notification was delivered successfully.",
68             status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error"
69         }
70     )
71     def post(self, request, vnfmId, vnfInstanceId):
72         logger.debug("VnfNotifyView post: %s" % request.data)
73         logger.debug("vnfmId: %s vnfInstanceId: %s", vnfmId, vnfInstanceId)
74         try:
75             vnfLcmOocNotificationSerializer = VnfLcmOperationOccurrenceNotificationSerializer(data=request.data)
76             if not vnfLcmOocNotificationSerializer.is_valid():
77                 raise Exception(vnfLcmOocNotificationSerializer.errors)
78
79             HandleVnfLcmOocNotification(vnfmId, vnfInstanceId, vnfLcmOocNotificationSerializer.data).do_biz()
80             return Response(data={}, status=status.HTTP_204_NO_CONTENT)
81         except Exception as e:
82             logger.error(traceback.format_exc())
83             logger.error("Exception in VnfLcmOoc Notification: %s", e.message)
84             return Response(data={'error': e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
85
86     @swagger_auto_schema(
87         responses={
88             status.HTTP_204_NO_CONTENT: "The notification endpoint was tested successfully.",
89             status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error"
90         }
91     )
92     def get(self, request, vnfmId, vnfInstanceId):
93         logger.debug("VnfNotifyView get")
94         logger.debug("vnfmId: %s vnfInstanceId: %s", vnfmId, vnfInstanceId)
95         return Response(data={}, status=status.HTTP_204_NO_CONTENT)