Add vnf Grant bussiness logic
[vfc/nfvo/lcm.git] / lcm / v2 / 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 import logging
15 import traceback
16
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
21
22 from lcm.v2.serializers import GrantRequestSerializer
23 from lcm.v2.serializers import GrantSerializer
24 from lcm.v2.grant_vnf import GrantVnf
25
26 logger = logging.getLogger(__name__)
27
28
29 class VnfGrantView(APIView):
30     @swagger_auto_schema(
31         request_body=GrantRequestSerializer(),
32         responses={
33             status.HTTP_201_CREATED: GrantSerializer(
34                 help_text="The grant was created successfully (synchronous mode)."
35             ),
36             status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error"
37         }
38     )
39     def post(self, request):
40         logger.debug("VnfGrantView Post: %s" % request.data)
41         try:
42             req_serializer = GrantRequestSerializer(data=request.data)
43             if not req_serializer.is_valid():
44                 raise Exception(req_serializer.errors)
45
46             grant_resp = GrantVnf(request.data).exec_grant()
47
48             resp_serializer = GrantSerializer(data=grant_resp)
49             if not resp_serializer.is_valid():
50                 raise Exception(resp_serializer.errors)
51
52             return Response(data=resp_serializer.data, status=status.HTTP_201_CREATED)
53         except Exception as e:
54             logger.error(traceback.format_exc())
55             logger.error("Exception in VnfGrant: %s", e.message)
56             return Response(data={'error': e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)