1 # Copyright 2017 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 lcm.nf.biz.delete_vnf import DeleteVnf
20 from rest_framework import status
21 from rest_framework.response import Response
22 from rest_framework.views import APIView
24 from lcm.nf.biz.create_vnf import CreateVnf
25 from lcm.nf.biz.query_vnf import QueryVnf
26 from lcm.nf.serializers.create_vnf_req import CreateVnfReqSerializer
27 from lcm.nf.serializers.vnf_instance import VnfInstanceSerializer
28 from lcm.nf.serializers.vnf_instances import VnfInstancesSerializer
29 from lcm.pub.exceptions import NFLCMException
30 from lcm.pub.exceptions import NFLCMExceptionNotFound
32 logger = logging.getLogger(__name__)
35 class CreateVnfAndQueryVnfs(APIView):
38 status.HTTP_200_OK: VnfInstancesSerializer(),
39 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
42 def get(self, request):
43 logger.debug("QueryMultiVnf--get::> %s" % request.data)
45 resp_data = QueryVnf(request.data).query_multi_vnf()
46 if len(resp_data) == 0:
47 return Response(data=[], status=status.HTTP_200_OK)
48 vnf_instances_serializer = VnfInstancesSerializer(data=resp_data)
49 if not vnf_instances_serializer.is_valid():
50 raise NFLCMException(vnf_instances_serializer.errors)
52 return Response(data=vnf_instances_serializer.data, status=status.HTTP_200_OK)
53 except NFLCMException as e:
54 logger.error(e.message)
55 return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
56 except Exception as e:
57 logger.error(e.message)
58 logger.error(traceback.format_exc())
59 return Response(data={'error': 'Failed to get Vnfs'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
62 request_body=CreateVnfReqSerializer(),
64 status.HTTP_201_CREATED: VnfInstanceSerializer(),
65 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
68 def post(self, request):
69 logger.debug("CreateVnfIdentifier--post::> %s" % request.data)
71 req_serializer = CreateVnfReqSerializer(data=request.data)
72 if not req_serializer.is_valid():
73 raise NFLCMException(req_serializer.errors)
75 nf_inst = CreateVnf(request.data).do_biz()
76 create_vnf_resp_serializer = VnfInstanceSerializer(data={"id": nf_inst.nfinstid,
77 "vnfProvider": nf_inst.vendor,
78 "vnfdVersion": nf_inst.version,
79 "vnfPkgId": nf_inst.package_id,
80 "instantiationState": nf_inst.status})
81 if not create_vnf_resp_serializer.is_valid():
82 raise NFLCMException(create_vnf_resp_serializer.errors)
83 return Response(data=create_vnf_resp_serializer.data, status=status.HTTP_201_CREATED)
84 except NFLCMException as e:
85 logger.error(e.message)
86 return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
87 except Exception as e:
88 logger.error(e.message)
89 logger.error(traceback.format_exc())
90 return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
93 class DeleteVnfAndQueryVnf(APIView):
96 status.HTTP_200_OK: VnfInstanceSerializer(),
97 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
100 def get(self, request, instanceid):
101 logger.debug("QuerySingleVnf--get::> %s" % request.data)
103 resp_data = QueryVnf(request.data, instanceid).query_single_vnf()
105 vnfs_instance_serializer = VnfInstanceSerializer(data=resp_data)
106 if not vnfs_instance_serializer.is_valid():
107 raise NFLCMException(vnfs_instance_serializer.errors)
109 return Response(data=vnfs_instance_serializer.data, status=status.HTTP_200_OK)
110 except NFLCMException as e:
111 logger.error(e.message)
112 return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
113 except NFLCMExceptionNotFound as e:
114 return Response(data={'error': '%s' % e.message}, status=status.HTTP_404_NOT_FOUND)
115 except Exception as e:
116 logger.error(e.message)
117 logger.error(traceback.format_exc())
118 return Response(data={'error': 'Failed to get Vnf(%s)' % instanceid},
119 status=status.HTTP_500_INTERNAL_SERVER_ERROR)
121 @swagger_auto_schema(
123 status.HTTP_204_NO_CONTENT: "Successfully",
124 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
127 def delete(self, request, instanceid):
128 logger.debug("DeleteVnfIdentifier--delete::> %s" % request.data)
130 DeleteVnf(request.data, instanceid).do_biz()
132 return Response(data=None, status=status.HTTP_204_NO_CONTENT)
133 except NFLCMException as e:
134 logger.error(e.message)
135 logger.debug('Delete VNF instance[%s] failed' % instanceid)
136 return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
137 except Exception as e:
138 logger.error(e.message)
139 logger.error(traceback.format_exc())
140 logger.debug('Delete VNF instance[%s] failed' % instanceid)
141 return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)