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 resp_isvalid = vnf_info_serializer.is_valid()
53 raise Exception(vnf_info_serializer.errors)
56 data=vnf_info_serializer.data,
57 status=status.HTTP_200_OK)
58 except Exception as e:
59 logger.error(e.message)
60 logger.error(traceback.format_exc())
63 'error': 'Failed to get Vnf(%s)' % vnfInstanceId},
64 status=status.HTTP_500_INTERNAL_SERVER_ERROR)
67 def fill_resp_data(vnf):
68 logger.info('Get the StorageInstModel of list')
69 storage_inst = StorageInstModel.objects.filter(instid=vnf.nfinstid)
71 for s in storage_inst:
73 "virtualStorageInstanceId": s.storageid,
74 "virtualStorageDescId": s.storagetype,
77 "resourceId": s.resouceid
81 logger.info('Get the VLInstModel of list.')
82 vl_inst = VLInstModel.objects.filter(ownerid=vnf.nfinstid)
85 net = NetworkInstModel.objects.filter(networkid=v.relatednetworkid)
87 raise VNFRESException(
88 'NetworkInst(%s) does not exist.' %
91 "virtualLinkInstanceId": v.vlinstanceid,
92 "virtualLinkDescId": v.vldid,
94 "vimId": net[0].vimid,
95 "resourceId": net[0].resouceid
99 logger.info('Get VNFCInstModel of list.')
100 vnfc_insts = VNFCInstModel.objects.filter(instid=vnf.nfinstid)
102 for vnfc in vnfc_insts:
103 vm = VmInstModel.objects.filter(vmid=vnfc.vmid)
105 raise VNFRESException('VmInst(%s) does not exist.' % vnfc.vmid)
106 storage = StorageInstModel.objects.filter(ownerid=vm[0].vmid)
108 raise VNFRESException(
109 'StorageInst(%s) does not exist.' %
112 "vnfcInstanceId": vnfc.vnfcinstanceid,
115 "vimId": vm[0].vimid,
116 "resourceId": vm[0].resouceid
118 "storageResourceIds": [s.storageid for s in storage]
120 vnfc_arr.append(vnfc_dic)
121 logger.info('Get the VimInstModel of list.')
122 vms = VmInstModel.objects.filter(instid=vnf.nfinstid)
129 "resouceid": vm.resouceid,
131 "nic_array": vm.nic_array,
132 "metadata": vm.metadata,
133 "volume_array": vm.volume_array,
134 "server_group": vm.server_group,
135 "availability_zone": vm.availability_zone,
136 "flavor_id": vm.flavor_id,
137 "security_groups": vm.security_groups,
138 "operationalstate": vm.operationalstate,
139 "insttype": vm.insttype,
140 "is_predefined": vm.is_predefined,
141 "create_time": vm.create_time,
145 vm_arr.append(vm_dic)
148 "vnfInstanceId": vnf.nfinstid,
149 "vnfInstanceName": vnf.nf_name,
150 "vnfInstanceDescription": vnf.nf_desc,
151 "onboardedVnfPkgInfoId": vnf.package_id,
152 "vnfdId": vnf.vnfdid,
153 "vnfdVersion": vnf.version,
154 "vnfSoftwareVersion": vnf.vnfSoftwareVersion,
155 "vnfProvider": vnf.vendor,
156 "vnfProductName": vnf.netype,
157 "vnfConfigurableProperties": vnf.vnfConfigurableProperties,
158 "instantiationState": vnf.status,
159 "instantiatedVnfInfo": {
160 "flavourId": vnf.flavour_id,
161 "vnfState": vnf.status,
164 "extVirtualLink": [],
165 "monitoringParameters": {},
166 "localizationLanguage": vnf.localizationLanguage,
168 "vnfcResourceInfo": vnfc_arr,
169 "virtualLinkResourceInfo": vl_arr,
170 "virtualStorageResourceInfo": arr
172 "metadata": vnf.input_params,
173 "extensions": vnf.vnfd_model
178 class getVnfs(APIView):
179 @swagger_auto_schema(
181 status.HTTP_200_OK: VnfsInfoSerializer(),
182 status.HTTP_404_NOT_FOUND: 'Vnfs does not exist',
183 status.HTTP_500_INTERNAL_SERVER_ERROR: 'internal error'})
184 def get(self, request):
185 logger.debug("Query all the vnfs[%s]", fun_name())
187 vnf_insts = NfInstModel.objects.all()
191 'error': 'Vnfs does not exist'},
192 status=status.HTTP_404_NOT_FOUND)
194 for vnf_inst in vnf_insts:
195 arr.append(fill_resp_data(vnf_inst))
197 vnfs_info_serializer = VnfsInfoSerializer(data={'resp_data': arr})
198 resp_isvalid = vnfs_info_serializer.is_valid()
200 raise Exception(vnfs_info_serializer.errors)
203 data=vnfs_info_serializer.data,
204 status=status.HTTP_200_OK)
205 except Exception as e:
206 logger.error(e.message)
207 logger.error(traceback.format_exc())
210 'error': 'Failed to get Vnfs'},
211 status=status.HTTP_500_INTERNAL_SERVER_ERROR)
214 class getVms(APIView):
215 @swagger_auto_schema(
217 status.HTTP_200_OK: VmInfoSerializer(),
218 status.HTTP_404_NOT_FOUND: 'Vms does not exist',
219 status.HTTP_500_INTERNAL_SERVER_ERROR: 'internal error'})
220 def get(self, request, vnfInstanceId):
221 logger.debug("Query all the vms by vnfInstanceId[%s]", fun_name())
223 vms = VmInstModel.objects.filter(instid=vnfInstanceId)
227 'error': 'Vms does not exist'},
228 status=status.HTTP_404_NOT_FOUND)
231 arr.append(fill_vms_data(vm))
233 vm_info_serializer = VmInfoSerializer(data={'resp_data': arr})
234 resp_isvalid = vm_info_serializer.is_valid()
236 raise Exception(vm_info_serializer.errors)
239 data=vm_info_serializer.data,
240 status=status.HTTP_200_OK)
241 except Exception as e:
242 logger.error(e.message)
243 logger.error(traceback.format_exc())
246 'error': 'Failed to get Vms'},
247 status=status.HTTP_500_INTERNAL_SERVER_ERROR)
250 def fill_vms_data(vm):
254 "resouceid": vm.resouceid,
255 "insttype": vm.insttype,
258 "operationalstate": vm.operationalstate,
260 "is_predefined": vm.is_predefined,
261 "security_groups": vm.security_groups,
262 "flavor_id": vm.flavor_id,
263 "availability_zone": vm.availability_zone,
264 "server_group": vm.server_group,
265 "volume_array": vm.volume_array,
266 "metadata": vm.metadata,
267 "nic_array": vm.nic_array
272 class getFlavors(APIView):
273 @swagger_auto_schema(
275 status.HTTP_200_OK: FlavorInfoSerializer(),
276 status.HTTP_404_NOT_FOUND: 'Flavours does not exist',
277 status.HTTP_500_INTERNAL_SERVER_ERROR: 'internal error'})
278 def get(self, request, vnfInstanceId):
279 logger.debug("Query all the flavors by vnfInstanceId[%s]", fun_name())
281 flavours = FlavourInstModel.objects.filter(instid=vnfInstanceId)
285 'error': 'Flavours does not exist'},
286 status=status.HTTP_404_NOT_FOUND)
288 for flavour in flavours:
289 arr.append(fill_flavours_data(flavour))
291 flavor_info_serializer = FlavorInfoSerializer(
292 data={'resp_data': arr})
293 resp_isvalid = flavor_info_serializer.is_valid()
295 raise Exception(flavor_info_serializer.errors)
298 data=flavor_info_serializer.data,
299 status=status.HTTP_200_OK)
300 except Exception as e:
301 logger.error(e.message)
302 logger.error(traceback.format_exc())
305 'error': 'Failed to get flavours'},
306 status=status.HTTP_500_INTERNAL_SERVER_ERROR)
309 def fill_flavours_data(f):
311 "flavourid": f.flavourid,
315 "extraspecs": f.extraspecs,
319 "resouceid": f.resouceid,
320 "create_time": f.create_time
325 class getNetworks(APIView):
326 @swagger_auto_schema(
328 status.HTTP_200_OK: NetworkInfoSerializer(),
329 status.HTTP_404_NOT_FOUND: 'Networks does not exist',
330 status.HTTP_500_INTERNAL_SERVER_ERROR: 'internal error'})
331 def get(self, request, vnfInstanceId):
332 logger.debug("Query all the networks by vnfInstanceId[%s]", fun_name())
334 networks = NetworkInstModel.objects.filter(instid=vnfInstanceId)
338 'error': 'Networks does not exist'},
339 status=status.HTTP_404_NOT_FOUND)
341 for network in networks:
342 arr.append(fill_networks_data(network))
344 network_info_serializer = NetworkInfoSerializer(
345 data={'resp_data': arr})
346 resp_isvalid = network_info_serializer.is_valid()
348 raise Exception(network_info_serializer.errors)
351 data=network_info_serializer.data,
352 status=status.HTTP_200_OK)
353 except Exception as e:
354 logger.error(e.message)
355 logger.error(traceback.format_exc())
358 'error': 'Failed to get networks'},
359 status=status.HTTP_500_INTERNAL_SERVER_ERROR)
362 def fill_networks_data(network):
364 "networkid": network.networkid,
365 "vimid": network.vimid,
366 "resouceid": network.resouceid,
367 "insttype": network.insttype,
368 "instid": network.instid,
374 class getSubnets(APIView):
375 @swagger_auto_schema(
377 status.HTTP_200_OK: SubnetInfoSerializer(),
378 status.HTTP_404_NOT_FOUND: 'Subnets does not exist',
379 status.HTTP_500_INTERNAL_SERVER_ERROR: 'internal error'})
380 def get(self, request, vnfInstanceId):
381 logger.debug("Query all the subnets by vnfInstanceId[%s]", fun_name())
383 subnets = SubNetworkInstModel.objects.filter(instid=vnfInstanceId)
387 'error': 'Subnets does not exist'},
388 status=status.HTTP_404_NOT_FOUND)
390 for subnet in subnets:
391 arr.append(fill_subnets_data(subnet))
392 subnet_info_serializer = SubnetInfoSerializer(
393 data={'resp_data': arr})
394 resp_isvalid = subnet_info_serializer.is_valid()
396 raise Exception(subnet_info_serializer.errors)
399 data=subnet_info_serializer.data,
400 status=status.HTTP_200_OK)
401 except Exception as e:
402 logger.error(e.message)
403 logger.error(traceback.format_exc())
406 'error': 'Failed to get subnets'},
407 status=status.HTTP_500_INTERNAL_SERVER_ERROR)
410 def fill_subnets_data(subnet):
412 "subnetworkid": subnet.subnetworkid,
413 "vimid": subnet.vimid,
414 "resouceid": subnet.resouceid,
415 "networkid": subnet.networkid,
416 "insttype": subnet.insttype,
417 "instid": subnet.instid,
424 class getCps(APIView):
425 @swagger_auto_schema(
427 status.HTTP_200_OK: CpsInfoSerializer(),
428 status.HTTP_404_NOT_FOUND: 'Cps does not exist',
429 status.HTTP_500_INTERNAL_SERVER_ERROR: 'internal error'})
430 def get(self, request, vnfInstanceId):
431 logger.debug("Query all the cps by vnfInstanceId[%s]", fun_name())
433 cps = CPInstModel.objects.filter(ownerid=vnfInstanceId)
437 'error': 'Cps does not exist'},
438 status=status.HTTP_404_NOT_FOUND)
441 arr.append(fill_cps_data(cp))
442 cp_info_serializer = CpsInfoSerializer(data={'resp_data': arr})
443 resp_isvalid = cp_info_serializer.is_valid()
445 raise Exception(cp_info_serializer.errors)
448 data=cp_info_serializer.data,
449 status=status.HTTP_200_OK)
450 except Exception as e:
451 logger.error(e.message)
452 logger.error(traceback.format_exc())
455 'error': 'Failed to get cps'},
456 status=status.HTTP_500_INTERNAL_SERVER_ERROR)
459 def fill_cps_data(cp):
461 "cpinstanceid": cp.cpinstanceid,
463 "cpinstancename": cp.cpinstancename,
464 "vlinstanceid": cp.vlinstanceid,
465 "ownertype": cp.ownertype,
466 "ownerid": cp.ownerid,
467 "relatedtype": cp.relatedtype
472 class getVolumes(APIView):
473 @swagger_auto_schema(
475 status.HTTP_200_OK: VolumeInfoSerializer(),
476 status.HTTP_404_NOT_FOUND: 'Volumes does not exist',
477 status.HTTP_500_INTERNAL_SERVER_ERROR: 'internal error'})
478 def get(self, request, vnfInstanceId):
479 logger.debug("Query all the volumes by vnfInstanceId[%s]", fun_name())
481 volumes = StorageInstModel.objects.filter(instid=vnfInstanceId)
485 'error': 'Volumes does not exist'},
486 status=status.HTTP_404_NOT_FOUND)
489 arr.append(fill_volumes_data(v))
490 volume_serializer = VolumeInfoSerializer(data={'resp_data': arr})
491 resp_isvalid = volume_serializer.is_valid()
493 raise Exception(volume_serializer.errors)
496 data=volume_serializer.data,
497 status=status.HTTP_200_OK)
498 except Exception as e:
499 logger.error(e.message)
500 logger.error(traceback.format_exc())
503 'error': 'Failed to get volumes'},
504 status=status.HTTP_500_INTERNAL_SERVER_ERROR)
507 def fill_volumes_data(v):
509 "storageid": v.storageid,
511 "resouceid": v.resouceid,
512 "insttype": v.insttype,
514 "storagetype": v.storagetype,