1 # Copyright 2016-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 rest_framework import status
19 from rest_framework.response import Response
20 from rest_framework.views import APIView
22 from lcm.ns.vnfs import create_vnfs
23 from lcm.ns.vnfs.create_vnfs import CreateVnfs
24 from lcm.ns.vnfs.verify_vnfs import VerifyVnfs
25 from lcm.ns.vnfs.get_vnfs import GetVnf
26 from lcm.ns.vnfs.scale_vnfs import NFManualScaleService
27 from lcm.ns.vnfs.terminate_nfs import TerminateVnfs
28 from lcm.ns.vnfs.grant_vnfs import GrantVnfs
29 from lcm.ns.vnfs.notify_lcm import NotifyLcm
30 from lcm.pub.utils.jobutil import JobUtil, JOB_TYPE
31 from lcm.pub.utils.values import ignore_case_get
33 logger = logging.getLogger(__name__)
36 class NfView(APIView):
37 def post(self, request):
38 logger.debug("VnfCreateView--post::> %s" % request.data)
39 data = {'ns_instance_id': ignore_case_get(request.data, 'nsInstanceId'),
40 'additional_param_for_ns': ignore_case_get(request.data, 'additionalParamForVnf'),
41 'additional_param_for_vnf': ignore_case_get(request.data, 'additionalParamForVnf'),
42 'vnf_index': ignore_case_get(request.data, 'vnfIndex')}
43 nf_inst_id, job_id = create_vnfs.prepare_create_params()
44 CreateVnfs(data, nf_inst_id, job_id).start()
46 "vnfInstId": nf_inst_id,
48 return Response(data=rsp, status=status.HTTP_202_ACCEPTED)
51 class NfDetailView(APIView):
52 def get(self, request, vnfinstid):
53 logger.debug("VnfQueryView--get::> %s" % vnfinstid)
54 nf_inst_info = GetVnf(vnfinstid).do_biz()
56 return Response(status=status.HTTP_404_NOT_FOUND)
57 return Response(status=status.HTTP_200_OK,
58 data={'vnfInstId': nf_inst_info[0].nfinstid, 'vnfName': nf_inst_info[0].nf_name,
59 'vnfStatus': nf_inst_info[0].status})
61 def post(self, request_paras, vnfinstid):
62 logger.debug("VnfTerminateView--post::> %s, %s", vnfinstid, request_paras.data)
63 vnf_inst_id = vnfinstid
64 terminationType = ignore_case_get(request_paras.data, 'terminationType')
65 gracefulTerminationTimeout = ignore_case_get(request_paras.data, 'gracefulTerminationTimeout')
66 job_id = JobUtil.create_job("VNF", JOB_TYPE.TERMINATE_VNF, vnf_inst_id)
67 data = {'terminationType': terminationType, 'gracefulTerminationTimeout': gracefulTerminationTimeout}
68 logger.debug("data=%s", data)
70 TerminateVnfs(data, vnf_inst_id, job_id).start()
71 except Exception as e:
72 return Response(data={'error': '%s' % e.message}, status=status.HTTP_409_CONFLICT)
73 rsp = {'jobId': job_id}
74 return Response(data=rsp, status=status.HTTP_201_CREATED)
77 class NfGrant(APIView):
78 def post(self, request):
79 logger.debug("NfGrant--post::> %s" % request.data)
81 vnf_inst_id = ignore_case_get(request.data, 'vnfInstanceId')
82 job_id = JobUtil.create_job("VNF", JOB_TYPE.GRANT_VNF, vnf_inst_id)
83 rsp = GrantVnfs(request.data, job_id).send_grant_vnf_to_resMgr()
87 "vimid": ignore_case_get(ignore_case_get(request.data, 'additionalparam'), 'vimid'),
94 return Response(data=rsp, status=status.HTTP_201_CREATED)
95 except Exception as e:
96 logger.error(traceback.format_exc())
97 return Response(data={'error': '%s' % e.message}, status=status.HTTP_409_CONFLICT)
100 class LcmNotify(APIView):
101 def post(self, request_paras, vnfmid, vnfInstanceId):
102 logger.debug("LcmNotify--post::> %s" % request_paras.data)
104 NotifyLcm(vnfmid, vnfInstanceId, request_paras.data).do_biz()
105 return Response(data={}, status=status.HTTP_201_CREATED)
106 except Exception as e:
107 return Response(data={'error': '%s' % e.message}, status=status.HTTP_409_CONFLICT)
110 class NfScaleView(APIView):
111 def post(self, request_paras, vnfinstid):
112 logger.debug("NfScaleView--post::> %s" % request_paras.data)
114 NFManualScaleService(vnfinstid, request_paras.data).start()
115 return Response(data={}, status=status.HTTP_202_ACCEPTED)
116 except Exception as e:
117 return Response(data={'error': '%s' % e.message}, status=status.HTTP_409_CONFLICT)
119 class NfVerifyView(APIView):
120 def post(self, request):
121 job_id = "VNFSDK_" + str(uuid.uuid4())
122 logger.debug("NfVerifyView--post::%s> %s", job_id, request.data)
123 VerifyVnfs(request.data, job_id).start()
124 return Response(data={"jobId": job_id}, status=status.HTTP_202_ACCEPTED)