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
37 from lcm.ns.vnfs.serializers import GetVnfRespSerializer
38 from lcm.ns.vnfs.serializers import TerminateVnfReqSerializer
39 from lcm.ns.vnfs.serializers import TerminateVnfRespSerializer
40 from lcm.ns.vnfs.serializers import GrantVnfReqSerializer
41 from lcm.ns.vnfs.serializers import GrantVnfRespSerializer
42 from lcm.ns.vnfs.serializers import NotifyLcmReqSerializer
44 logger = logging.getLogger(__name__)
47 class NfView(APIView):
49 request_body=InstVnfReqSerializer(),
51 status.HTTP_202_ACCEPTED: InstVnfRespSerializer()
54 def post(self, request):
55 logger.debug("VnfCreateView--post::> %s" % request.data)
57 req_serializer = InstVnfReqSerializer(data=request.data)
58 if not req_serializer.is_valid():
59 logger.error(req_serializer.errors)
61 data = {'ns_instance_id': ignore_case_get(request.data, 'nsInstanceId'),
62 'additional_param_for_ns': ignore_case_get(request.data, 'additionalParamForVnf'),
63 'additional_param_for_vnf': ignore_case_get(request.data, 'additionalParamForVnf'),
64 'vnf_index': ignore_case_get(request.data, 'vnfIndex')}
65 nf_inst_id, job_id = create_vnfs.prepare_create_params()
66 CreateVnfs(data, nf_inst_id, job_id).start()
68 "vnfInstId": nf_inst_id,
71 resp_serializer = InstVnfRespSerializer(data=rsp)
72 if not resp_serializer.is_valid():
73 logger.error(resp_serializer.errors)
75 return Response(data=rsp, status=status.HTTP_202_ACCEPTED)
78 class NfDetailView(APIView):
82 status.HTTP_200_OK: GetVnfRespSerializer(),
83 status.HTTP_404_NOT_FOUND: "VNF not found"
86 def get(self, request, vnfinstid):
87 logger.debug("VnfQueryView--get::> %s" % vnfinstid)
88 nf_inst_info = GetVnf(vnfinstid).do_biz()
90 return Response(status=status.HTTP_404_NOT_FOUND)
93 'vnfInstId': nf_inst_info[0].nfinstid,
94 'vnfName': nf_inst_info[0].nf_name,
95 'vnfStatus': nf_inst_info[0].status
97 resp_serializer = GetVnfRespSerializer(data=rsp)
98 if not resp_serializer.is_valid():
99 logger.error(resp_serializer.errors)
101 return Response(status=status.HTTP_200_OK, data=rsp)
103 @swagger_auto_schema(
104 request_body=TerminateVnfReqSerializer(),
106 status.HTTP_200_OK: TerminateVnfRespSerializer(),
107 status.HTTP_409_CONFLICT: "Inner error"
110 def post(self, request_paras, vnfinstid):
111 logger.debug("VnfTerminateView--post::> %s, %s", vnfinstid, request_paras.data)
113 req_serializer = TerminateVnfReqSerializer(data=request_paras.data)
114 if not req_serializer.is_valid():
115 logger.error(req_serializer.errors)
117 vnf_inst_id = vnfinstid
118 terminationType = ignore_case_get(request_paras.data, 'terminationType')
119 gracefulTerminationTimeout = ignore_case_get(request_paras.data, 'gracefulTerminationTimeout')
120 job_id = JobUtil.create_job("VNF", JOB_TYPE.TERMINATE_VNF, vnf_inst_id)
121 data = {'terminationType': terminationType, 'gracefulTerminationTimeout': gracefulTerminationTimeout}
122 logger.debug("data=%s", data)
124 TerminateVnfs(data, vnf_inst_id, job_id).start()
125 except Exception as e:
126 logger.error(e.message)
127 return Response(data={'error': '%s' % e.message}, status=status.HTTP_409_CONFLICT)
128 rsp = {'jobId': job_id}
130 resp_serializer = TerminateVnfRespSerializer(data=rsp)
131 if not resp_serializer.is_valid():
132 logger.error(resp_serializer.errors)
134 return Response(data=rsp, status=status.HTTP_201_CREATED)
137 class NfGrant(APIView):
138 @swagger_auto_schema(
139 request_body=GrantVnfReqSerializer(),
141 status.HTTP_201_CREATED: GrantVnfRespSerializer(),
142 status.HTTP_409_CONFLICT: "Inner error"
145 def post(self, request):
146 logger.debug("NfGrant--post::> %s" % request.data)
148 req_serializer = GrantVnfReqSerializer(data=request.data)
149 if not req_serializer.is_valid():
150 raise Exception(req_serializer.errors)
152 vnf_inst_id = ignore_case_get(request.data, 'vnfInstanceId')
153 job_id = JobUtil.create_job("VNF", JOB_TYPE.GRANT_VNF, vnf_inst_id)
154 rsp = GrantVnfs(request.data, job_id).send_grant_vnf_to_resMgr()
158 "vimid": ignore_case_get(ignore_case_get(request.data, 'additionalparam'), 'vimid'),
165 resp_serializer = GrantVnfRespSerializer(data=rsp)
166 if not resp_serializer.is_valid():
167 raise Exception(resp_serializer.errors)
169 return Response(data=rsp, status=status.HTTP_201_CREATED)
170 except Exception as e:
171 logger.error(e.message)
172 logger.error(traceback.format_exc())
173 return Response(data={'error': '%s' % e.message}, status=status.HTTP_409_CONFLICT)
176 class LcmNotify(APIView):
177 @swagger_auto_schema(
178 request_body=NotifyLcmReqSerializer(),
180 status.HTTP_201_CREATED: None,
181 status.HTTP_409_CONFLICT: "Inner error"
184 def post(self, request, vnfmid, vnfInstanceId):
185 logger.debug("LcmNotify--post::> %s" % request.data)
187 req_serializer = NotifyLcmReqSerializer(data=request.data)
188 if not req_serializer.is_valid():
189 raise Exception(req_serializer.errors)
190 NotifyLcm(vnfmid, vnfInstanceId, request.data).do_biz()
191 return Response(data={}, status=status.HTTP_201_CREATED)
192 except Exception as e:
193 logger.error(e.message)
194 return Response(data={'error': '%s' % e.message}, status=status.HTTP_409_CONFLICT)
197 class NfScaleView(APIView):
198 def post(self, request_paras, vnfinstid):
199 logger.debug("NfScaleView--post::> %s" % request_paras.data)
201 NFManualScaleService(vnfinstid, request_paras.data).start()
202 return Response(data={}, status=status.HTTP_202_ACCEPTED)
203 except Exception as e:
204 logger.error(e.message)
205 return Response(data={'error': '%s' % e.message}, status=status.HTTP_409_CONFLICT)
208 class NfVerifyView(APIView):
209 def post(self, request):
210 job_id = "VNFSDK_" + str(uuid.uuid4())
211 logger.debug("NfVerifyView--post::%s> %s", job_id, request.data)
212 VerifyVnfs(request.data, job_id).start()
213 return Response(data={"jobId": job_id}, status=status.HTTP_202_ACCEPTED)
216 class NfVnfmInfoView(APIView):
217 def get(self, request, vnfmid):
218 logger.debug("NfVnfmInfoView--get::> %s" % vnfmid)
220 vnfm_info = get_vnfm_by_id(vnfmid)
221 except NSLCMException as e:
222 logger.error(e.message)
223 return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
224 except Exception as e:
225 logger.error(e.message)
226 logger.error(traceback.format_exc())
227 return Response(data={'error': 'Failed to get vnfm info.'},
228 status=status.HTTP_500_INTERNAL_SERVER_ERROR)
229 return Response(data=vnfm_info, status=status.HTTP_200_OK)
232 class NfVimInfoView(APIView):
233 def get(self, request, vimid):
234 logger.debug("NfVimInfoView--get::> %s" % vimid)
236 vim_info = get_vim_by_id(vimid)
237 except NSLCMException as e:
238 logger.error(e.message)
239 return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
240 except Exception as e:
241 logger.error(e.message)
242 logger.error(traceback.format_exc())
243 return Response(data={'error': 'Failed to get vim info.'},
244 status=status.HTTP_500_INTERNAL_SERVER_ERROR)
245 return Response(data=vim_info, status=status.HTTP_200_OK)