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.
19 from rest_framework import status
20 from rest_framework.response import Response
21 from rest_framework.views import APIView
23 from lcm.nf.vnfs.vnf_cancel.delete_vnf_identifier import DeleteVnf
24 from lcm.nf.vnfs.vnf_cancel.term_vnf import TermVnf
25 from lcm.nf.vnfs.vnf_create.create_vnf_identifier import CreateVnf
26 from lcm.nf.vnfs.vnf_create.inst_vnf import InstVnf
27 from lcm.pub.exceptions import NFLCMException
28 from lcm.pub.utils.jobutil import JobUtil
30 logger = logging.getLogger(__name__)
33 class CreateVnfIdentifier(APIView):
34 def post(self, request):
35 logger.debug("CreateVnfIdentifier--post::> %s" % request.data)
37 nf_inst_id = CreateVnf(request.data).do_biz()
38 except NFLCMException as e:
39 logger.error(e.message)
40 return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
42 logger.error(traceback.format_exc())
43 return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
44 rsp = {"vnfInstanceId": nf_inst_id}
45 return Response(data=rsp, status=status.HTTP_201_CREATED)
48 class InstantiateVnf(APIView):
49 def post(self, request, instanceid):
51 logger.debug("InstantiateVnf--post::> %s" % request.data)
52 job_id = JobUtil.create_job('NF', 'INSTANTIATE', instanceid)
53 JobUtil.add_job_status(job_id, 0, "INST_VNF_READY")
54 InstVnf(request.data, instanceid, job_id).start()
55 except NFLCMException as e:
56 logger.error(e.message)
57 return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
59 logger.error(traceback.format_exc())
60 return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
61 rsp = {"jobId": job_id}
62 return Response(data=rsp, status=status.HTTP_202_ACCEPTED)
65 class DeleteVnfIdentifier(APIView):
66 def delete(self, request, instanceid):
67 logger.debug("DeleteVnfIdentifier--delete::> %s" % request.data)
69 DeleteVnf(request.data, instanceid).do_biz()
70 except NFLCMException as e:
71 logger.error(e.message)
72 return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
74 logger.error(traceback.format_exc())
75 return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
76 return Response(data=None, status=status.HTTP_204_NO_CONTENT)
79 class TerminateVnf(APIView):
80 def post(self, request, instanceid):
81 logger.debug("TerminateVnf--post::> %s" % request.data)
82 job_id = JobUtil.create_job('NF', 'TERMINATE', instanceid)
83 JobUtil.add_job_status(job_id, 0, "TERM_VNF_READY")
84 TermVnf(request.data, instanceid, job_id).start()
85 rsp = {"jobId": job_id}
86 return Response(data=rsp, status=status.HTTP_202_ACCEPTED)
89 class QueryMultipleVnf(APIView):
90 def get(self, request):
91 logger.debug("QueryMultipleVnf--get::> %s" % request.data)
92 return Response(data='', status=status.HTTP_202_ACCEPTED)
95 class QuerySingleVnf(APIView):
96 def get(self, request):
97 logger.debug("QuerySingleVnf--get::> %s" % request.data)
98 return Response(data='', status=status.HTTP_202_ACCEPTED)
101 # class GetOperationStatus(APIView):
102 # def get(self, request):
103 # logger.debug("GetOperationStatus--get::> %s" % request.data)
104 # return Response(data='', status=status.HTTP_202_ACCEPTED)
107 class SwaggerJsonView(APIView):
108 def get(self, request):
109 json_file = os.path.join(os.path.dirname(__file__), 'swagger.json')
111 json_data = json.JSONDecoder().decode(f.read())
113 return Response(json_data)