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_notification import HandleVnfLcmOocNotification, HandleVnfIdentifierCreationNotification, HandleVnfIdentifierDeletionNotification
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, VnfIdentifierCreationNotificationSerializer, VnfIdentifierDeletionNotificationSerializer
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 grant_request = GrantRequestSerializer(data=request.data)
45 if not grant_request.is_valid():
46 raise Exception(grant_request.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)
74 notification_type = request.data['notificationType']
76 if notification_type == 'VnfLcmOperationOccurrenceNotification':
77 notification = VnfLcmOperationOccurrenceNotificationSerializer(data=request.data)
78 if not notification.is_valid():
79 raise Exception(notification.errors)
80 HandleVnfLcmOocNotification(vnfmId, vnfInstanceId, notification.data).do_biz()
81 elif notification_type == 'VnfIdentifierCreationNotification':
82 notification = VnfIdentifierCreationNotificationSerializer(data=request.data)
83 if not notification.is_valid():
84 raise Exception(notification.errors)
85 HandleVnfIdentifierCreationNotification(vnfmId, vnfInstanceId, notification.data).do_biz()
86 elif notification_type == 'VnfIdentifierDeletionNotification':
87 notification = VnfIdentifierDeletionNotificationSerializer(data=request.data)
88 if not notification.is_valid():
89 raise Exception(notification.errors)
90 HandleVnfIdentifierDeletionNotification(vnfmId, vnfInstanceId, notification.data).do_biz()
92 raise Exception('Unexpected noitifcation type value.')
93 return Response(data={}, status=status.HTTP_204_NO_CONTENT)
94 except Exception as e:
95 logger.error(traceback.format_exc())
96 logger.error("Exception in VnfLcmOoc Notification: %s", e.message)
97 return Response(data={'error': e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
101 status.HTTP_204_NO_CONTENT: "The notification endpoint was tested successfully.",
102 status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error"
105 def get(self, request, vnfmId, vnfInstanceId):
106 logger.debug("VnfNotifyView get")
107 logger.debug("vnfmId: %s vnfInstanceId: %s", vnfmId, vnfInstanceId)
108 return Response(data={}, status=status.HTTP_204_NO_CONTENT)