1 # Copyright 2018 ZTE Corporation.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
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
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
27 logger = logging.getLogger(__name__)
30 class VnfGrantView(APIView):
32 request_body=GrantRequestSerializer(),
34 status.HTTP_201_CREATED: GrantSerializer(
35 help_text="The grant was created successfully (synchronous mode)."
37 status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error"
40 def post(self, request):
41 logger.debug("VnfGrantView Post: %s" % request.data)
43 req_serializer = GrantRequestSerializer(data=request.data)
44 if not req_serializer.is_valid():
45 raise Exception(req_serializer.errors)
47 grant_resp = GrantVnf(request.data).exec_grant()
49 resp_serializer = GrantSerializer(data=grant_resp)
50 if not resp_serializer.is_valid():
51 raise Exception(resp_serializer.errors)
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)
60 class VnfNotifyView(APIView):
62 request_body=VnfLcmOperationOccurrenceNotificationSerializer(
63 help_text="A notification about lifecycle changes triggered by a VNF LCM operation occurrence."
66 status.HTTP_204_NO_CONTENT: "The notification was delivered successfully.",
67 status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error"
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)
77 status.HTTP_204_NO_CONTENT: "The notification endpoint was tested successfully.",
78 status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error"
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)