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
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 from lcm.v2.vnf_query.query_vnf import QueryVnf
29 logger = logging.getLogger(__name__)
32 class CreateVnfAndQueryVnfs(APIView):
35 status.HTTP_200_OK: VnfInstanceSerializer(),
36 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
39 def get(self, request):
40 logger.debug("QueryMultiVnf--get::> %s" % request.data)
42 resp_data = QueryVnf(request.data).query_multi_vnf()
44 # vnfs_info_serializer = VnfsInfoSerializer(data=resp_data)
45 # if not vnfs_info_serializer.is_valid():
46 # raise NFLCMException(vnfs_info_serializer.errors)
48 # return Response(data=vnfs_info_serializer.data, status=status.HTTP_200_OK)
50 vnfInstanceSerializer = VnfInstanceSerializer(data=resp_data)
51 if not vnfInstanceSerializer.is_valid():
52 raise NFLCMException(vnfInstanceSerializer.errors)
53 return Response(data=vnfInstanceSerializer.data, status=status.HTTP_200_OK)
54 except NFLCMException as e:
55 logger.error(e.message)
56 return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
57 except Exception as e:
58 logger.error(e.message)
59 logger.error(traceback.format_exc())
60 return Response(data={'error': 'Failed to get Vnfs'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
63 request_body=CreateVnfReqSerializer(),
65 status.HTTP_201_CREATED: VnfInstanceSerializer(),
66 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
69 def post(self, request):
70 logger.debug("CreateVnfIdentifier--post::> %s" % request.data)
72 req_serializer = CreateVnfReqSerializer(data=request.data)
73 if not req_serializer.is_valid():
74 raise NFLCMException(req_serializer.errors)
76 resp = CreateVnf(req_serializer.data).do_biz()
78 vnfInstanceSerializer = VnfInstanceSerializer(data=resp)
79 if not vnfInstanceSerializer.is_valid():
80 raise NFLCMException(vnfInstanceSerializer.errors)
81 return Response(data=vnfInstanceSerializer.data, status=status.HTTP_201_CREATED)
82 except NFLCMException as e:
83 logger.error(e.message)
84 return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
85 except Exception as e:
86 logger.error(e.message)
87 logger.error(traceback.format_exc())
88 return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)