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
31 logger = logging.getLogger(__name__)
34 class CreateVnfAndQueryVnfs(APIView):
37 status.HTTP_200_OK: VnfInstancesSerializer(),
38 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
41 def get(self, request):
42 logger.debug("QueryMultiVnf--get::> %s" % request.data)
44 resp_data = QueryVnf(request.data).query_multi_vnf()
45 if len(resp_data) == 0:
46 return Response(data=[], status=status.HTTP_200_OK)
47 vnf_instances_serializer = VnfInstancesSerializer(data=resp_data)
48 if not vnf_instances_serializer.is_valid():
49 raise NFLCMException(vnf_instances_serializer.errors)
51 return Response(data=vnf_instances_serializer.data, status=status.HTTP_200_OK)
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': 'Failed to get Vnfs'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
61 request_body=CreateVnfReqSerializer(),
63 status.HTTP_201_CREATED: VnfInstanceSerializer(),
64 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
67 def post(self, request):
68 logger.debug("CreateVnfIdentifier--post::> %s" % request.data)
70 req_serializer = CreateVnfReqSerializer(data=request.data)
71 if not req_serializer.is_valid():
72 raise NFLCMException(req_serializer.errors)
74 nf_inst = CreateVnf(request.data).do_biz()
75 create_vnf_resp_serializer = VnfInstanceSerializer(data={"id": nf_inst.nfinstid,
76 "vnfProvider": nf_inst.vendor,
77 "vnfdVersion": nf_inst.version,
78 "vnfPkgId": nf_inst.package_id,
79 "instantiationState": nf_inst.status})
80 if not create_vnf_resp_serializer.is_valid():
81 raise NFLCMException(create_vnf_resp_serializer.errors)
82 return Response(data=create_vnf_resp_serializer.data, status=status.HTTP_201_CREATED)
83 except NFLCMException as e:
84 logger.error(e.message)
85 return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
86 except Exception as e:
87 logger.error(e.message)
88 logger.error(traceback.format_exc())
89 return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
92 class DeleteVnfAndQueryVnf(APIView):
95 status.HTTP_200_OK: VnfInstanceSerializer(),
96 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
99 def get(self, request, instanceid):
100 logger.debug("QuerySingleVnf--get::> %s" % request.data)
102 resp_data = QueryVnf(request.data, instanceid).query_single_vnf()
104 vnfs_instance_serializer = VnfInstanceSerializer(data=resp_data)
105 if not vnfs_instance_serializer.is_valid():
106 raise NFLCMException(vnfs_instance_serializer.errors)
108 return Response(data=vnfs_instance_serializer.data, status=status.HTTP_200_OK)
109 except NFLCMException as e:
110 logger.error(e.message)
111 return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
112 except Exception as e:
113 logger.error(e.message)
114 logger.error(traceback.format_exc())
115 return Response(data={'error': 'Failed to get Vnf(%s)' % instanceid},
116 status=status.HTTP_500_INTERNAL_SERVER_ERROR)
118 @swagger_auto_schema(
120 status.HTTP_204_NO_CONTENT: "Successfully",
121 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
124 def delete(self, request, instanceid):
125 logger.debug("DeleteVnfIdentifier--delete::> %s" % request.data)
127 DeleteVnf(request.data, instanceid).do_biz()
129 return Response(data=None, status=status.HTTP_204_NO_CONTENT)
130 except NFLCMException as e:
131 logger.error(e.message)
132 logger.debug('Delete VNF instance[%s] failed' % instanceid)
133 return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
134 except Exception as e:
135 logger.error(e.message)
136 logger.error(traceback.format_exc())
137 logger.debug('Delete VNF instance[%s] failed' % instanceid)
138 return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)