Interface alignment with ETSI for createVnf
[vfc/gvnfm/vnflcm.git] / lcm / 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
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.nf.serializers import CreateVnfReqSerializer
24 from lcm.nf.vnf_create.create_vnf_identifier import CreateVnf
25 from lcm.pub.exceptions import NFLCMException
26 from lcm.v2.serializers import VnfInstanceSerializer
27
28 logger = logging.getLogger(__name__)
29
30
31 class CreateVnfAndQueryVnfs(APIView):
32     @swagger_auto_schema(
33         request_body=CreateVnfReqSerializer(),
34         responses={
35             status.HTTP_201_CREATED: VnfInstanceSerializer(),
36             status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
37         }
38     )
39     def post(self, request):
40         logger.debug("CreateVnfIdentifier--post::> %s" % request.data)
41         try:
42             req_serializer = CreateVnfReqSerializer(data=request.data)
43             if not req_serializer.is_valid():
44                 raise NFLCMException(req_serializer.errors)
45
46             resp = CreateVnf(req_serializer.data).do_biz()
47
48             vnfInstanceSerializer = VnfInstanceSerializer(data=resp)
49             if not vnfInstanceSerializer.is_valid():
50                 raise NFLCMException(vnfInstanceSerializer.errors)
51             return Response(data=vnfInstanceSerializer.data, status=status.HTTP_201_CREATED)
52         except NFLCMException as e:
53             logger.error(e.message)
54             return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
55         except Exception as e:
56             logger.error(e.message)
57             logger.error(traceback.format_exc())
58             return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)