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
39 logger = logging.getLogger(__name__)
42 class NfView(APIView):
44 request_body=InstVnfReqSerializer(),
46 status.HTTP_202_ACCEPTED: InstVnfRespSerializer()
49 def post(self, request):
50 logger.debug("VnfCreateView--post::> %s" % request.data)
52 req_serializer = InstVnfReqSerializer(data=request.data)
53 if not req_serializer.is_valid():
54 logger.error(req_serializer.errors)
56 data = {'ns_instance_id': ignore_case_get(request.data, 'nsInstanceId'),
57 'additional_param_for_ns': ignore_case_get(request.data, 'additionalParamForVnf'),
58 'additional_param_for_vnf': ignore_case_get(request.data, 'additionalParamForVnf'),
59 'vnf_index': ignore_case_get(request.data, 'vnfIndex')}
60 nf_inst_id, job_id = create_vnfs.prepare_create_params()
61 CreateVnfs(data, nf_inst_id, job_id).start()
63 "vnfInstId": nf_inst_id,
66 resp_serializer = InstVnfRespSerializer(data=rsp)
67 if not resp_serializer.is_valid():
68 logger.error(resp_serializer.errors)
70 return Response(data=rsp, status=status.HTTP_202_ACCEPTED)
73 class NfDetailView(APIView):
77 status.HTTP_200_OK: GetVnfRespSerializer(),
78 status.HTTP_404_NOT_FOUND: "VNF not found"
81 def get(self, request, vnfinstid):
82 logger.debug("VnfQueryView--get::> %s" % vnfinstid)
83 nf_inst_info = GetVnf(vnfinstid).do_biz()
85 return Response(status=status.HTTP_404_NOT_FOUND)
88 'vnfInstId': nf_inst_info[0].nfinstid,
89 'vnfName': nf_inst_info[0].nf_name,
90 'vnfStatus': nf_inst_info[0].status
92 resp_serializer = GetVnfRespSerializer(data=rsp)
93 if not resp_serializer.is_valid():
94 logger.error(resp_serializer.errors)
96 return Response(status=status.HTTP_200_OK, data=rsp)
98 def post(self, request_paras, vnfinstid):
99 logger.debug("VnfTerminateView--post::> %s, %s", vnfinstid, request_paras.data)
100 vnf_inst_id = vnfinstid
101 terminationType = ignore_case_get(request_paras.data, 'terminationType')
102 gracefulTerminationTimeout = ignore_case_get(request_paras.data, 'gracefulTerminationTimeout')
103 job_id = JobUtil.create_job("VNF", JOB_TYPE.TERMINATE_VNF, vnf_inst_id)
104 data = {'terminationType': terminationType, 'gracefulTerminationTimeout': gracefulTerminationTimeout}
105 logger.debug("data=%s", data)
107 TerminateVnfs(data, vnf_inst_id, job_id).start()
108 except Exception as e:
109 logger.error(e.message)
110 return Response(data={'error': '%s' % e.message}, status=status.HTTP_409_CONFLICT)
111 rsp = {'jobId': job_id}
112 return Response(data=rsp, status=status.HTTP_201_CREATED)
115 class NfGrant(APIView):
116 def post(self, request):
117 logger.debug("NfGrant--post::> %s" % request.data)
119 vnf_inst_id = ignore_case_get(request.data, 'vnfInstanceId')
120 job_id = JobUtil.create_job("VNF", JOB_TYPE.GRANT_VNF, vnf_inst_id)
121 rsp = GrantVnfs(request.data, job_id).send_grant_vnf_to_resMgr()
125 "vimid": ignore_case_get(ignore_case_get(request.data, 'additionalparam'), 'vimid'),
132 return Response(data=rsp, status=status.HTTP_201_CREATED)
133 except Exception as e:
134 logger.error(e.message)
135 logger.error(traceback.format_exc())
136 return Response(data={'error': '%s' % e.message}, status=status.HTTP_409_CONFLICT)
139 class LcmNotify(APIView):
140 def post(self, request_paras, vnfmid, vnfInstanceId):
141 logger.debug("LcmNotify--post::> %s" % request_paras.data)
143 NotifyLcm(vnfmid, vnfInstanceId, request_paras.data).do_biz()
144 return Response(data={}, status=status.HTTP_201_CREATED)
145 except Exception as e:
146 logger.error(e.message)
147 return Response(data={'error': '%s' % e.message}, status=status.HTTP_409_CONFLICT)
150 class NfScaleView(APIView):
151 def post(self, request_paras, vnfinstid):
152 logger.debug("NfScaleView--post::> %s" % request_paras.data)
154 NFManualScaleService(vnfinstid, request_paras.data).start()
155 return Response(data={}, status=status.HTTP_202_ACCEPTED)
156 except Exception as e:
157 logger.error(e.message)
158 return Response(data={'error': '%s' % e.message}, status=status.HTTP_409_CONFLICT)
161 class NfVerifyView(APIView):
162 def post(self, request):
163 job_id = "VNFSDK_" + str(uuid.uuid4())
164 logger.debug("NfVerifyView--post::%s> %s", job_id, request.data)
165 VerifyVnfs(request.data, job_id).start()
166 return Response(data={"jobId": job_id}, status=status.HTTP_202_ACCEPTED)
169 class NfVnfmInfoView(APIView):
170 def get(self, request, vnfmid):
171 logger.debug("NfVnfmInfoView--get::> %s" % vnfmid)
173 vnfm_info = get_vnfm_by_id(vnfmid)
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 vnfm info.'},
181 status=status.HTTP_500_INTERNAL_SERVER_ERROR)
182 return Response(data=vnfm_info, status=status.HTTP_200_OK)
185 class NfVimInfoView(APIView):
186 def get(self, request, vimid):
187 logger.debug("NfVimInfoView--get::> %s" % vimid)
189 vim_info = get_vim_by_id(vimid)
190 except NSLCMException as e:
191 logger.error(e.message)
192 return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
193 except Exception as e:
194 logger.error(e.message)
195 logger.error(traceback.format_exc())
196 return Response(data={'error': 'Failed to get vim info.'},
197 status=status.HTTP_500_INTERNAL_SERVER_ERROR)
198 return Response(data=vim_info, status=status.HTTP_200_OK)