Merge "fix VDU parsing during granting"
[vfc/nfvo/lcm.git] / lcm / v2 / 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 rest_framework.response import Response
18 from rest_framework.views import APIView
19 from rest_framework import status
20 from drf_yasg.utils import swagger_auto_schema
21
22 from lcm.v2.serializers import GrantRequestSerializer
23 from lcm.v2.serializers import GrantSerializer
24 from lcm.v2.serializers import VnfLcmOperationOccurrenceNotificationSerializer
25 from lcm.v2.grant_vnf import GrantVnf
26
27 logger = logging.getLogger(__name__)
28
29
30 class VnfGrantView(APIView):
31     @swagger_auto_schema(
32         request_body=GrantRequestSerializer(),
33         responses={
34             status.HTTP_201_CREATED: GrantSerializer(
35                 help_text="The grant was created successfully (synchronous mode)."
36             ),
37             status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error"
38         }
39     )
40     def post(self, request):
41         logger.debug("VnfGrantView Post: %s" % request.data)
42         try:
43             req_serializer = GrantRequestSerializer(data=request.data)
44             if not req_serializer.is_valid():
45                 raise Exception(req_serializer.errors)
46
47             grant_resp = GrantVnf(request.data).exec_grant()
48
49             resp_serializer = GrantSerializer(data=grant_resp)
50             if not resp_serializer.is_valid():
51                 raise Exception(resp_serializer.errors)
52
53             return Response(data=resp_serializer.data, status=status.HTTP_201_CREATED)
54         except Exception as e:
55             logger.error(traceback.format_exc())
56             logger.error("Exception in VnfGrant: %s", e.message)
57             return Response(data={'error': e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
58
59
60 class VnfNotifyView(APIView):
61     @swagger_auto_schema(
62         request_body=VnfLcmOperationOccurrenceNotificationSerializer(
63             help_text="A notification about lifecycle changes triggered by a VNF LCM operation occurrence."
64         ),
65         responses={
66             status.HTTP_204_NO_CONTENT: "The notification was delivered successfully.",
67             status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error"
68         }
69     )
70     def post(self, request, vnfmId, vnfInstanceId):
71         logger.debug("VnfNotifyView post: %s" % request.data)
72         logger.debug("vnfmId: %s vnfInstanceId: %s", vnfmId, vnfInstanceId)
73         return Response(data={}, status=status.HTTP_204_NO_CONTENT)
74
75     @swagger_auto_schema(
76         responses={
77             status.HTTP_204_NO_CONTENT: "The notification endpoint was tested successfully.",
78             status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error"
79         }
80     )
81     def get(self, request, vnfmId, vnfInstanceId):
82         logger.debug("VnfNotifyView get")
83         logger.debug("vnfmId: %s vnfInstanceId: %s", vnfmId, vnfInstanceId)
84         return Response(data={}, status=status.HTTP_204_NO_CONTENT)