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.nf.serializers import CreateVnfReqSerializer, CreateVnfRespSerializer
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
28 logger = logging.getLogger(__name__)
31 class CreateVnfAndQueryVnfs(APIView):
33 request_body=CreateVnfReqSerializer(),
35 status.HTTP_201_CREATED: VnfInstanceSerializer(),
36 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
39 def post(self, request):
40 logger.debug("CreateVnfIdentifier--post::> %s" % request.data)
42 req_serializer = CreateVnfReqSerializer(data=request.data)
43 if not req_serializer.is_valid():
44 raise NFLCMException(req_serializer.errors)
46 nf_inst_id = CreateVnf(req_serializer.data).do_biz()
48 create_vnf_resp_serializer = CreateVnfRespSerializer(data={"vnfInstanceId": nf_inst_id})
49 if not create_vnf_resp_serializer.is_valid():
50 raise NFLCMException(create_vnf_resp_serializer.errors)
51 return Response(data=create_vnf_resp_serializer.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)