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.
18 from drf_yasg.utils import swagger_auto_schema
19 from rest_framework import status
20 from rest_framework.response import Response
21 from rest_framework.views import APIView
23 from res.pub.database.models import NfInstModel, StorageInstModel, NetworkInstModel, VLInstModel, \
24 VNFCInstModel, VmInstModel, FlavourInstModel, SubNetworkInstModel, CPInstModel
25 from res.pub.exceptions import VNFRESException
26 from res.pub.utils.syscomm import fun_name
27 from res.resources.serializers import VolumeInfoSerializer, CpsInfoSerializer, SubnetInfoSerializer, \
28 NetworkInfoSerializer, FlavorInfoSerializer, VmInfoSerializer, VnfInfoSerializer, VnfsInfoSerializer
30 logger = logging.getLogger(__name__)
33 class getVnf(APIView):
36 status.HTTP_200_OK: VnfInfoSerializer(),
37 status.HTTP_404_NOT_FOUND: 'Vnf does not exist',
38 status.HTTP_500_INTERNAL_SERVER_ERROR: 'internal error'})
39 def get(self, request, vnfInstanceId):
40 logger.debug("[%s]vnf_inst_id=%s", fun_name(), vnfInstanceId)
42 vnf_inst = NfInstModel.objects.filter(nfinstid=vnfInstanceId)
46 'error': 'Vnf(%s) does not exist' % vnfInstanceId},
47 status=status.HTTP_404_NOT_FOUND)
48 resp_data = fill_resp_data(vnf_inst[0])
50 vnf_info_serializer = VnfInfoSerializer(data=resp_data)
51 if not vnf_info_serializer.is_valid():
52 raise Exception(vnf_info_serializer.errors)
56 status=status.HTTP_200_OK)
57 except Exception as e:
58 logger.error(e.args[0])
59 logger.error(traceback.format_exc())
62 'error': 'Failed to get Vnf(%s)' % vnfInstanceId},
63 status=status.HTTP_500_INTERNAL_SERVER_ERROR)
66 def fill_resp_data(vnf):
67 logger.info('Get the StorageInstModel of list')
68 storage_inst = StorageInstModel.objects.filter(instid=vnf.nfinstid)
70 for s in storage_inst:
72 "virtualStorageInstanceId": s.storageid,
73 "virtualStorageDescId": s.storagetype,
76 "resourceId": s.resouceid
80 logger.info('Get the VLInstModel of list.')
81 vl_inst = VLInstModel.objects.filter(ownerid=vnf.nfinstid)
84 net = NetworkInstModel.objects.filter(networkid=v.relatednetworkid)
86 raise VNFRESException(
87 'NetworkInst(%s) does not exist.' %
90 "virtualLinkInstanceId": v.vlinstanceid,
91 "virtualLinkDescId": v.vldid,
93 "vimId": net[0].vimid,
94 "resourceId": net[0].resouceid
98 logger.info('Get VNFCInstModel of list.')
99 vnfc_insts = VNFCInstModel.objects.filter(instid=vnf.nfinstid)
101 for vnfc in vnfc_insts:
102 vm = VmInstModel.objects.filter(vmid=vnfc.vmid)
104 raise VNFRESException('VmInst(%s) does not exist.' % vnfc.vmid)
105 storage = StorageInstModel.objects.filter(ownerid=vm[0].vmid)
107 raise VNFRESException(
108 'StorageInst(%s) does not exist.' %
111 "vnfcInstanceId": vnfc.vnfcinstanceid,
114 "vimId": vm[0].vimid,
115 "resourceId": vm[0].resouceid
117 "storageResourceIds": [s.storageid for s in storage]
119 vnfc_arr.append(vnfc_dic)
120 logger.info('Get the VimInstModel of list.')
121 vms = VmInstModel.objects.filter(instid=vnf.nfinstid)
128 "resouceid": vm.resouceid,
130 "nic_array": vm.nic_array,
131 "metadata": vm.metadata,
132 "volume_array": vm.volume_array,
133 "server_group": vm.server_group,
134 "availability_zone": vm.availability_zone,
135 "flavor_id": vm.flavor_id,
136 "security_groups": vm.security_groups,
137 "operationalstate": vm.operationalstate,
138 "insttype": vm.insttype,
139 "is_predefined": vm.is_predefined,
140 "create_time": vm.create_time,
144 vm_arr.append(vm_dic)
147 "vnfInstanceId": vnf.nfinstid,
148 "vnfInstanceName": vnf.nf_name,
149 "vnfInstanceDescription": vnf.nf_desc,
150 "onboardedVnfPkgInfoId": vnf.package_id,
151 "vnfdId": vnf.vnfdid,
152 "vnfdVersion": vnf.version,
153 "vnfSoftwareVersion": vnf.vnfSoftwareVersion,
154 "vnfProvider": vnf.vendor,
155 "vnfProductName": vnf.netype,
156 "vnfConfigurableProperties": vnf.vnfConfigurableProperties,
157 "instantiationState": vnf.status,
158 "instantiatedVnfInfo": {
159 "flavourId": vnf.flavour_id,
160 "vnfState": vnf.status,
163 "extVirtualLink": [],
164 "monitoringParameters": {},
165 "localizationLanguage": vnf.localizationLanguage,
167 "vnfcResourceInfo": vnfc_arr,
168 "virtualLinkResourceInfo": vl_arr,
169 "virtualStorageResourceInfo": arr
171 "metadata": vnf.input_params,
172 "extensions": vnf.vnfd_model
177 class getVnfs(APIView):
178 @swagger_auto_schema(
180 status.HTTP_200_OK: VnfsInfoSerializer(),
181 status.HTTP_404_NOT_FOUND: 'Vnfs does not exist',
182 status.HTTP_500_INTERNAL_SERVER_ERROR: 'internal error'})
183 def get(self, request):
184 logger.debug("Query all the vnfs[%s]", fun_name())
186 vnf_insts = NfInstModel.objects.all()
190 'error': 'Vnfs does not exist'},
191 status=status.HTTP_404_NOT_FOUND)
193 for vnf_inst in vnf_insts:
194 arr.append(fill_resp_data(vnf_inst))
196 vnfs_info_serializer = VnfsInfoSerializer(data={'resp_data': arr})
197 if not vnfs_info_serializer.is_valid():
198 raise Exception(vnfs_info_serializer.errors)
201 data={'resp_data': arr},
202 status=status.HTTP_200_OK)
203 except Exception as e:
204 logger.error(e.args[0])
205 logger.error(traceback.format_exc())
208 'error': 'Failed to get Vnfs'},
209 status=status.HTTP_500_INTERNAL_SERVER_ERROR)
212 class getVms(APIView):
213 @swagger_auto_schema(
215 status.HTTP_200_OK: VmInfoSerializer(),
216 status.HTTP_404_NOT_FOUND: 'Vms does not exist',
217 status.HTTP_500_INTERNAL_SERVER_ERROR: 'internal error'})
218 def get(self, request, vnfInstanceId):
219 logger.debug("Query all the vms by vnfInstanceId[%s]", fun_name())
221 vms = VmInstModel.objects.filter(instid=vnfInstanceId)
225 'error': 'Vms does not exist'},
226 status=status.HTTP_404_NOT_FOUND)
229 arr.append(fill_vms_data(vm))
231 vm_info_serializer = VmInfoSerializer(data={'resp_data': arr})
232 if not vm_info_serializer.is_valid():
233 raise Exception(vm_info_serializer.errors)
236 data={'resp_data': arr},
237 status=status.HTTP_200_OK)
238 except Exception as e:
239 logger.error(e.args[0])
240 logger.error(traceback.format_exc())
243 'error': 'Failed to get Vms'},
244 status=status.HTTP_500_INTERNAL_SERVER_ERROR)
247 def fill_vms_data(vm):
251 "resouceid": vm.resouceid,
252 "insttype": vm.insttype,
255 "operationalstate": vm.operationalstate,
257 "is_predefined": vm.is_predefined,
258 "security_groups": vm.security_groups,
259 "flavor_id": vm.flavor_id,
260 "availability_zone": vm.availability_zone,
261 "server_group": vm.server_group,
262 "volume_array": vm.volume_array,
263 "metadata": vm.metadata,
264 "nic_array": vm.nic_array
269 class getFlavors(APIView):
270 @swagger_auto_schema(
272 status.HTTP_200_OK: FlavorInfoSerializer(),
273 status.HTTP_404_NOT_FOUND: 'Flavours does not exist',
274 status.HTTP_500_INTERNAL_SERVER_ERROR: 'internal error'})
275 def get(self, request, vnfInstanceId):
276 logger.debug("Query all the flavors by vnfInstanceId[%s]", fun_name())
278 flavours = FlavourInstModel.objects.filter(instid=vnfInstanceId)
282 'error': 'Flavours does not exist'},
283 status=status.HTTP_404_NOT_FOUND)
285 for flavour in flavours:
286 arr.append(fill_flavours_data(flavour))
288 flavor_info_serializer = FlavorInfoSerializer(data={'resp_data': arr})
289 if not flavor_info_serializer.is_valid():
290 raise Exception(flavor_info_serializer.errors)
293 data=flavor_info_serializer.data,
294 status=status.HTTP_200_OK)
295 except Exception as e:
296 logger.error(e.args[0])
297 logger.error(traceback.format_exc())
300 'error': 'Failed to get flavours'},
301 status=status.HTTP_500_INTERNAL_SERVER_ERROR)
304 def fill_flavours_data(f):
306 "flavourid": f.flavourid,
310 "extraspecs": f.extraspecs,
314 "resouceid": f.resouceid,
315 "create_time": f.create_time
320 class getNetworks(APIView):
321 @swagger_auto_schema(
323 status.HTTP_200_OK: NetworkInfoSerializer(),
324 status.HTTP_404_NOT_FOUND: 'Networks does not exist',
325 status.HTTP_500_INTERNAL_SERVER_ERROR: 'internal error'})
326 def get(self, request, vnfInstanceId):
327 logger.debug("Query all the networks by vnfInstanceId[%s]", fun_name())
329 networks = NetworkInstModel.objects.filter(instid=vnfInstanceId)
333 'error': 'Networks does not exist'},
334 status=status.HTTP_404_NOT_FOUND)
336 for network in networks:
337 arr.append(fill_networks_data(network))
339 network_info_serializer = NetworkInfoSerializer(data={'resp_data': arr})
340 if not network_info_serializer.is_valid():
341 raise Exception(network_info_serializer.errors)
344 data=network_info_serializer.data,
345 status=status.HTTP_200_OK)
346 except Exception as e:
347 logger.error(e.args[0])
348 logger.error(traceback.format_exc())
351 'error': 'Failed to get networks'},
352 status=status.HTTP_500_INTERNAL_SERVER_ERROR)
355 def fill_networks_data(network):
357 "networkid": network.networkid,
358 "vimid": network.vimid,
359 "resouceid": network.resouceid,
360 "insttype": network.insttype,
361 "instid": network.instid,
367 class getSubnets(APIView):
368 @swagger_auto_schema(
370 status.HTTP_200_OK: SubnetInfoSerializer(),
371 status.HTTP_404_NOT_FOUND: 'Subnets does not exist',
372 status.HTTP_500_INTERNAL_SERVER_ERROR: 'internal error'})
373 def get(self, request, vnfInstanceId):
374 logger.debug("Query all the subnets by vnfInstanceId[%s]", fun_name())
376 subnets = SubNetworkInstModel.objects.filter(instid=vnfInstanceId)
380 'error': 'Subnets does not exist'},
381 status=status.HTTP_404_NOT_FOUND)
383 for subnet in subnets:
384 arr.append(fill_subnets_data(subnet))
385 subnet_info_serializer = SubnetInfoSerializer(data={'resp_data': arr})
386 if not subnet_info_serializer.is_valid():
387 raise Exception(subnet_info_serializer.errors)
390 data=subnet_info_serializer.data,
391 status=status.HTTP_200_OK)
392 except Exception as e:
393 logger.error(e.args[0])
394 logger.error(traceback.format_exc())
397 'error': 'Failed to get subnets'},
398 status=status.HTTP_500_INTERNAL_SERVER_ERROR)
401 def fill_subnets_data(subnet):
403 "subnetworkid": subnet.subnetworkid,
404 "vimid": subnet.vimid,
405 "resouceid": subnet.resouceid,
406 "networkid": subnet.networkid,
407 "insttype": subnet.insttype,
408 "instid": subnet.instid,
415 class getCps(APIView):
416 @swagger_auto_schema(
418 status.HTTP_200_OK: CpsInfoSerializer(),
419 status.HTTP_404_NOT_FOUND: 'Cps does not exist',
420 status.HTTP_500_INTERNAL_SERVER_ERROR: 'internal error'})
421 def get(self, request, vnfInstanceId):
422 logger.debug("Query all the cps by vnfInstanceId[%s]", fun_name())
424 cps = CPInstModel.objects.filter(ownerid=vnfInstanceId)
428 'error': 'Cps does not exist'},
429 status=status.HTTP_404_NOT_FOUND)
432 arr.append(fill_cps_data(cp))
433 cp_info_serializer = CpsInfoSerializer(data={'resp_data': arr})
434 if not cp_info_serializer.is_valid():
435 raise Exception(cp_info_serializer.errors)
438 data=cp_info_serializer.data,
439 status=status.HTTP_200_OK)
440 except Exception as e:
441 logger.error(e.args[0])
442 logger.error(traceback.format_exc())
445 'error': 'Failed to get cps'},
446 status=status.HTTP_500_INTERNAL_SERVER_ERROR)
449 def fill_cps_data(cp):
451 "cpinstanceid": cp.cpinstanceid,
453 "cpinstancename": cp.cpinstancename,
454 "vlinstanceid": cp.vlinstanceid,
455 "ownertype": cp.ownertype,
456 "ownerid": cp.ownerid,
457 "relatedtype": cp.relatedtype
462 class getVolumes(APIView):
463 @swagger_auto_schema(
465 status.HTTP_200_OK: VolumeInfoSerializer(),
466 status.HTTP_404_NOT_FOUND: 'Volumes does not exist',
467 status.HTTP_500_INTERNAL_SERVER_ERROR: 'internal error'})
468 def get(self, request, vnfInstanceId):
469 logger.debug("Query all the volumes by vnfInstanceId[%s]", fun_name())
471 volumes = StorageInstModel.objects.filter(instid=vnfInstanceId)
475 'error': 'Volumes does not exist'},
476 status=status.HTTP_404_NOT_FOUND)
479 arr.append(fill_volumes_data(v))
480 volume_serializer = VolumeInfoSerializer(data={'resp_data': arr})
481 if not volume_serializer.is_valid():
482 raise Exception(volume_serializer.errors)
485 data=volume_serializer.data,
486 status=status.HTTP_200_OK)
487 except Exception as e:
488 logger.error(e.args[0])
489 logger.error(traceback.format_exc())
492 'error': 'Failed to get volumes'},
493 status=status.HTTP_500_INTERNAL_SERVER_ERROR)
496 def fill_volumes_data(v):
498 "storageid": v.storageid,
500 "resouceid": v.resouceid,
501 "insttype": v.insttype,
503 "storagetype": v.storagetype,