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 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
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
28 logger = logging.getLogger(__name__)
31 class VnfGrantView(APIView):
33 request_body=GrantRequestSerializer(),
35 status.HTTP_201_CREATED: GrantSerializer(
36 help_text="The grant was created successfully (synchronous mode)."
38 status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error"
41 def post(self, request):
42 logger.debug("VnfGrantView Post: %s" % request.data)
44 req_serializer = GrantRequestSerializer(data=request.data)
45 if not req_serializer.is_valid():
46 raise Exception(req_serializer.errors)
48 grant_resp = GrantVnf(request.data).exec_grant()
50 resp_serializer = GrantSerializer(data=grant_resp)
51 if not resp_serializer.is_valid():
52 raise Exception(resp_serializer.errors)
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)
61 class VnfNotifyView(APIView):
63 request_body=VnfLcmOperationOccurrenceNotificationSerializer(
64 help_text="A notification about lifecycle changes triggered by a VNF LCM operation occurrence."
67 status.HTTP_204_NO_CONTENT: "The notification was delivered successfully.",
68 status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error"
71 def post(self, request, vnfmId, vnfInstanceId):
72 logger.debug("VnfNotifyView post: %s" % request.data)
73 logger.debug("vnfmId: %s vnfInstanceId: %s", vnfmId, vnfInstanceId)
75 vnfLcmOocNotificationSerializer = VnfLcmOperationOccurrenceNotificationSerializer(data=request.data)
76 if not vnfLcmOocNotificationSerializer.is_valid():
77 raise Exception(vnfLcmOocNotificationSerializer.errors)
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)
88 status.HTTP_204_NO_CONTENT: "The notification endpoint was tested successfully.",
89 status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error"
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)