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
21 from drf_yasg.utils import swagger_auto_schema
23 from lcm.ns.vnfs import create_vnfs
24 from lcm.ns.vnfs.create_vnfs import CreateVnfs
25 from lcm.ns.vnfs.verify_vnfs import VerifyVnfs
26 from lcm.ns.vnfs.get_vnfs import GetVnf
27 from lcm.ns.vnfs.scale_vnfs import NFManualScaleService
28 from lcm.ns.vnfs.terminate_nfs import TerminateVnfs
29 from lcm.ns.vnfs.grant_vnfs import GrantVnfs
30 from lcm.ns.vnfs.notify_lcm import NotifyLcm
31 from lcm.pub.exceptions import NSLCMException
32 from lcm.pub.msapi.extsys import get_vnfm_by_id, get_vim_by_id
33 from lcm.pub.utils.jobutil import JobUtil, JOB_TYPE
34 from lcm.pub.utils.values import ignore_case_get
35 from lcm.ns.vnfs.serializers import InstVnfReqSerializer
36 from lcm.ns.vnfs.serializers import InstVnfRespSerializer
38 logger = logging.getLogger(__name__)
41 class NfView(APIView):
43 request_body=InstVnfReqSerializer(),
45 status.HTTP_202_ACCEPTED: InstVnfRespSerializer(),
48 def post(self, request):
49 logger.debug("VnfCreateView--post::> %s" % request.data)
51 req_serializer = InstVnfReqSerializer(data=request.data)
52 if not req_serializer.is_valid():
53 logger.error(req_serializer.errors)
55 data = {'ns_instance_id': ignore_case_get(request.data, 'nsInstanceId'),
56 'additional_param_for_ns': ignore_case_get(request.data, 'additionalParamForVnf'),
57 'additional_param_for_vnf': ignore_case_get(request.data, 'additionalParamForVnf'),
58 'vnf_index': ignore_case_get(request.data, 'vnfIndex')}
59 nf_inst_id, job_id = create_vnfs.prepare_create_params()
60 CreateVnfs(data, nf_inst_id, job_id).start()
62 "vnfInstId": nf_inst_id,
65 resp_serializer = InstVnfRespSerializer(data=rsp)
66 if not resp_serializer.is_valid():
67 logger.error(resp_serializer.errors)
69 return Response(data=rsp, status=status.HTTP_202_ACCEPTED)
72 class NfDetailView(APIView):
73 def get(self, request, vnfinstid):
74 logger.debug("VnfQueryView--get::> %s" % vnfinstid)
75 nf_inst_info = GetVnf(vnfinstid).do_biz()
77 return Response(status=status.HTTP_404_NOT_FOUND)
78 return Response(status=status.HTTP_200_OK,
79 data={'vnfInstId': nf_inst_info[0].nfinstid, 'vnfName': nf_inst_info[0].nf_name,
80 'vnfStatus': nf_inst_info[0].status})
82 def post(self, request_paras, vnfinstid):
83 logger.debug("VnfTerminateView--post::> %s, %s", vnfinstid, request_paras.data)
84 vnf_inst_id = vnfinstid
85 terminationType = ignore_case_get(request_paras.data, 'terminationType')
86 gracefulTerminationTimeout = ignore_case_get(request_paras.data, 'gracefulTerminationTimeout')
87 job_id = JobUtil.create_job("VNF", JOB_TYPE.TERMINATE_VNF, vnf_inst_id)
88 data = {'terminationType': terminationType, 'gracefulTerminationTimeout': gracefulTerminationTimeout}
89 logger.debug("data=%s", data)
91 TerminateVnfs(data, vnf_inst_id, job_id).start()
92 except Exception as e:
93 logger.error(e.message)
94 return Response(data={'error': '%s' % e.message}, status=status.HTTP_409_CONFLICT)
95 rsp = {'jobId': job_id}
96 return Response(data=rsp, status=status.HTTP_201_CREATED)
99 class NfGrant(APIView):
100 def post(self, request):
101 logger.debug("NfGrant--post::> %s" % request.data)
103 vnf_inst_id = ignore_case_get(request.data, 'vnfInstanceId')
104 job_id = JobUtil.create_job("VNF", JOB_TYPE.GRANT_VNF, vnf_inst_id)
105 rsp = GrantVnfs(request.data, job_id).send_grant_vnf_to_resMgr()
109 "vimid": ignore_case_get(ignore_case_get(request.data, 'additionalparam'), 'vimid'),
116 return Response(data=rsp, status=status.HTTP_201_CREATED)
117 except Exception as e:
118 logger.error(e.message)
119 logger.error(traceback.format_exc())
120 return Response(data={'error': '%s' % e.message}, status=status.HTTP_409_CONFLICT)
123 class LcmNotify(APIView):
124 def post(self, request_paras, vnfmid, vnfInstanceId):
125 logger.debug("LcmNotify--post::> %s" % request_paras.data)
127 NotifyLcm(vnfmid, vnfInstanceId, request_paras.data).do_biz()
128 return Response(data={}, status=status.HTTP_201_CREATED)
129 except Exception as e:
130 logger.error(e.message)
131 return Response(data={'error': '%s' % e.message}, status=status.HTTP_409_CONFLICT)
134 class NfScaleView(APIView):
135 def post(self, request_paras, vnfinstid):
136 logger.debug("NfScaleView--post::> %s" % request_paras.data)
138 NFManualScaleService(vnfinstid, request_paras.data).start()
139 return Response(data={}, status=status.HTTP_202_ACCEPTED)
140 except Exception as e:
141 logger.error(e.message)
142 return Response(data={'error': '%s' % e.message}, status=status.HTTP_409_CONFLICT)
145 class NfVerifyView(APIView):
146 def post(self, request):
147 job_id = "VNFSDK_" + str(uuid.uuid4())
148 logger.debug("NfVerifyView--post::%s> %s", job_id, request.data)
149 VerifyVnfs(request.data, job_id).start()
150 return Response(data={"jobId": job_id}, status=status.HTTP_202_ACCEPTED)
153 class NfVnfmInfoView(APIView):
154 def get(self, request, vnfmid):
155 logger.debug("NfVnfmInfoView--get::> %s" % vnfmid)
157 vnfm_info = get_vnfm_by_id(vnfmid)
158 except NSLCMException as e:
159 logger.error(e.message)
160 return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
161 except Exception as e:
162 logger.error(e.message)
163 logger.error(traceback.format_exc())
164 return Response(data={'error': 'Failed to get vnfm info.'},
165 status=status.HTTP_500_INTERNAL_SERVER_ERROR)
166 return Response(data=vnfm_info, status=status.HTTP_200_OK)
169 class NfVimInfoView(APIView):
170 def get(self, request, vimid):
171 logger.debug("NfVimInfoView--get::> %s" % vimid)
173 vim_info = get_vim_by_id(vimid)
174 except NSLCMException as e:
175 logger.error(e.message)
176 return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
177 except Exception as e:
178 logger.error(e.message)
179 logger.error(traceback.format_exc())
180 return Response(data={'error': 'Failed to get vim info.'},
181 status=status.HTTP_500_INTERNAL_SERVER_ERROR)
182 return Response(data=vim_info, status=status.HTTP_200_OK)