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.
18 from drf_yasg.utils import swagger_auto_schema
19 from rest_framework import status
20 from rest_framework.response import Response
21 from rest_framework.views import APIView
23 from lcm.ns_vnfs.biz.grant_vnf import GrantVnf
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.biz.handle_notification import HandleVnfLcmOocNotification, HandleVnfIdentifierCreationNotification, HandleVnfIdentifierDeletionNotification
27 from lcm.ns_vnfs.serializers.grant_vnf_serializer import VnfLcmOperationOccurrenceNotificationSerializer, VnfIdentifierCreationNotificationSerializer, VnfIdentifierDeletionNotificationSerializer
29 logger = logging.getLogger(__name__)
32 class VnfGrantView(APIView):
34 request_body=GrantRequestSerializer(),
36 status.HTTP_201_CREATED: GrantSerializer(
37 help_text="The grant was created successfully (synchronous mode)."
39 status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error"
42 def post(self, request):
43 logger.debug("VnfGrantView Post: %s" % request.data)
45 grant_request = GrantRequestSerializer(data=request.data)
46 if not grant_request.is_valid():
47 raise Exception(grant_request.errors)
49 grant_resp = GrantVnf(request.data).exec_grant()
51 resp_serializer = GrantSerializer(data=grant_resp)
52 if not resp_serializer.is_valid():
53 raise Exception(resp_serializer.errors)
55 return Response(data=resp_serializer.data, status=status.HTTP_201_CREATED)
56 except Exception as e:
57 logger.error(traceback.format_exc())
58 logger.error("Exception in VnfGrant: %s", e.message)
59 return Response(data={'error': e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
62 class VnfNotifyView(APIView):
64 request_body=VnfLcmOperationOccurrenceNotificationSerializer(
65 help_text="A notification about lifecycle changes triggered by a VNF LCM operation occurrence."
68 status.HTTP_204_NO_CONTENT: "The notification was delivered successfully.",
69 status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error"
72 def post(self, request, vnfmId, vnfInstanceId):
73 logger.debug("VnfNotifyView post: %s" % request.data)
74 logger.debug("vnfmId: %s vnfInstanceId: %s", vnfmId, vnfInstanceId)
75 notification_type = request.data['notificationType']
77 if notification_type == 'VnfLcmOperationOccurrenceNotification':
78 notification = VnfLcmOperationOccurrenceNotificationSerializer(data=request.data)
79 if not notification.is_valid():
80 raise Exception(notification.errors)
81 HandleVnfLcmOocNotification(vnfmId, vnfInstanceId, notification.data).do_biz()
82 elif notification_type == 'VnfIdentifierCreationNotification':
83 notification = VnfIdentifierCreationNotificationSerializer(data=request.data)
84 if not notification.is_valid():
85 raise Exception(notification.errors)
86 HandleVnfIdentifierCreationNotification(vnfmId, vnfInstanceId, notification.data).do_biz()
87 elif notification_type == 'VnfIdentifierDeletionNotification':
88 notification = VnfIdentifierDeletionNotificationSerializer(data=request.data)
89 if not notification.is_valid():
90 raise Exception(notification.errors)
91 HandleVnfIdentifierDeletionNotification(vnfmId, vnfInstanceId, notification.data).do_biz()
93 raise Exception('Unexpected noitifcation type value.')
94 return Response(data={}, status=status.HTTP_204_NO_CONTENT)
95 except Exception as e:
96 logger.error(traceback.format_exc())
97 logger.error("Exception in VnfLcmOoc Notification: %s", e.message)
98 return Response(data={'error': e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
100 @swagger_auto_schema(
102 status.HTTP_204_NO_CONTENT: "The notification endpoint was tested successfully.",
103 status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error"
106 def get(self, request, vnfmId, vnfInstanceId):
107 logger.debug("VnfNotifyView get")
108 logger.debug("vnfmId: %s vnfInstanceId: %s", vnfmId, vnfInstanceId)
109 return Response(data={}, status=status.HTTP_204_NO_CONTENT)