c096c02e02823a38b4622de5155be7773a5ca505
[vfc/nfvo/lcm.git] / lcm / ns_vnfs / views / vnf_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
15 import logging
16 import traceback
17
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
22
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
28
29 logger = logging.getLogger(__name__)
30
31
32 class VnfGrantView(APIView):
33     @swagger_auto_schema(
34         request_body=GrantRequestSerializer(),
35         responses={
36             status.HTTP_201_CREATED: GrantSerializer(
37                 help_text="The grant was created successfully (synchronous mode)."
38             ),
39             status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error"
40         }
41     )
42     def post(self, request):
43         logger.debug("VnfGrantView Post: %s" % request.data)
44         try:
45             grant_request = GrantRequestSerializer(data=request.data)
46             if not grant_request.is_valid():
47                 raise Exception(grant_request.errors)
48
49             grant_resp = GrantVnf(request.data).exec_grant()
50
51             resp_serializer = GrantSerializer(data=grant_resp)
52             if not resp_serializer.is_valid():
53                 raise Exception(resp_serializer.errors)
54
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)
60
61
62 class VnfNotifyView(APIView):
63     @swagger_auto_schema(
64         request_body=VnfLcmOperationOccurrenceNotificationSerializer(
65             help_text="A notification about lifecycle changes triggered by a VNF LCM operation occurrence."
66         ),
67         responses={
68             status.HTTP_204_NO_CONTENT: "The notification was delivered successfully.",
69             status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error"
70         }
71     )
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']
76         try:
77             if notification_type == 'VnfLcmOperationOccurrenceNotification':
78                 notification = VnfLcmOperationOccurrenceNotificationSerializer(data=request.data)
79                 if not notification.is_valid():
80                     logger.warn(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                     logger.warn(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                     logger.warn(notification.errors)
91                 HandleVnfIdentifierDeletionNotification(vnfmId, vnfInstanceId, notification.data).do_biz()
92             else:
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)
99
100     @swagger_auto_schema(
101         responses={
102             status.HTTP_204_NO_CONTENT: "The notification endpoint was tested successfully.",
103             status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error"
104         }
105     )
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)