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.decorators import api_view
21 from rest_framework.response import Response
22 from rest_framework.views import APIView
24 from res.pub.database.models import NfInstModel, StorageInstModel, NetworkInstModel, VLInstModel, \
25 VNFCInstModel, VmInstModel, FlavourInstModel, SubNetworkInstModel, CPInstModel
26 from res.pub.exceptions import VNFRESException
27 from res.pub.utils.syscomm import fun_name
28 from res.pub.utils.values import ignore_case_get
29 from res.resources.serializers import VolumeInfoSerializer, NoneSerializer, CpsInfoSerializer
31 logger = logging.getLogger(__name__)
34 @api_view(http_method_names=['GET'])
35 def get_vnf(request, *args, **kwargs):
36 vnf_inst_id = ignore_case_get(kwargs, "vnfInstanceId")
37 logger.debug("[%s]vnf_inst_id=%s", fun_name(), vnf_inst_id)
39 vnf_inst = NfInstModel.objects.filter(nfinstid=vnf_inst_id)
41 return Response(data={'error': 'Vnf(%s) does not exist' % vnf_inst_id}, status=status.HTTP_404_NOT_FOUND)
42 resp_data = fill_resp_data(vnf_inst[0])
43 return Response(data=resp_data, status=status.HTTP_200_OK)
44 except Exception as e:
45 logger.error(e.message)
46 logger.error(traceback.format_exc())
47 return Response(data={'error': 'Failed to get Vnf(%s)' % vnf_inst_id}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
50 def fill_resp_data(vnf):
51 logger.info('Get the StorageInstModel of list')
52 storage_inst = StorageInstModel.objects.filter(instid=vnf.nfinstid)
54 for s in storage_inst:
56 "virtualStorageInstanceId": s.storageid,
57 "virtualStorageDescId": s.storagetype,
60 "resourceId": s.resouceid
64 logger.info('Get the VLInstModel of list.')
65 vl_inst = VLInstModel.objects.filter(ownerid=vnf.nfinstid)
68 net = NetworkInstModel.objects.filter(networkid=v.relatednetworkid)
70 raise VNFRESException('NetworkInst(%s) does not exist.' % v.relatednetworkid)
72 "virtualLinkInstanceId": v.vlinstanceid,
73 "virtualLinkDescId": v.vldid,
75 "vimId": net[0].vimid,
76 "resourceId": net[0].resouceid
80 logger.info('Get VNFCInstModel of list.')
81 vnfc_insts = VNFCInstModel.objects.filter(instid=vnf.nfinstid)
83 for vnfc in vnfc_insts:
84 vm = VmInstModel.objects.filter(vmid=vnfc.vmid)
86 raise VNFRESException('VmInst(%s) does not exist.' % vnfc.vmid)
87 storage = StorageInstModel.objects.filter(ownerid=vm[0].vmid)
89 raise VNFRESException('StorageInst(%s) does not exist.' % vm[0].vmid)
91 "vnfcInstanceId": vnfc.vnfcinstanceid,
95 "resourceId": vm[0].resouceid
97 "storageResourceIds": [s.storageid for s in storage]
99 vnfc_arr.append(vnfc_dic)
100 logger.info('Get the VimInstModel of list.')
101 vms = VmInstModel.objects.filter(instid=vnf.nfinstid)
103 # The 'vimInfoId' and 'vimId' each value are same
109 "resouceid": vm.resouceid,
111 "nic_array": vm.nic_array,
112 "metadata": vm.metadata,
113 "volume_array": vm.volume_array,
114 "server_group": vm.server_group,
115 "availability_zone": vm.availability_zone,
116 "flavor_id": vm.flavor_id,
117 "security_groups": vm.security_groups,
118 "operationalstate": vm.operationalstate,
119 "insttype": vm.insttype,
120 "is_predefined": vm.is_predefined,
121 "create_time": vm.create_time,
125 vm_arr.append(vm_dic)
128 "vnfInstanceId": vnf.nfinstid,
129 "vnfInstanceName": vnf.nf_name,
130 "vnfInstanceDescription": vnf.nf_desc,
131 "onboardedVnfPkgInfoId": vnf.package_id,
132 "vnfdId": vnf.vnfdid,
133 "vnfdVersion": vnf.version,
134 "vnfSoftwareVersion": vnf.vnfSoftwareVersion,
135 "vnfProvider": vnf.vendor,
136 "vnfProductName": vnf.netype,
137 "vnfConfigurableProperties": {vnf.vnfConfigurableProperties},
138 "instantiationState": vnf.status,
139 "instantiatedVnfInfo": {
140 "flavourId": vnf.flavour_id,
141 "vnfState": vnf.status,
144 "extVirtualLink": [],
145 "monitoringParameters": {},
146 "localizationLanguage": vnf.localizationLanguage,
148 "vnfcResourceInfo": vnfc_arr,
149 "virtualLinkResourceInfo": vl_arr,
150 "virtualStorageResourceInfo": arr
152 "metadata": vnf.input_params,
153 "extensions": vnf.vnfd_model
158 @api_view(http_method_names=['GET'])
159 def get_vnfs(request):
160 logger.debug("Query all the vnfs[%s]", fun_name())
162 vnf_insts = NfInstModel.objects.all()
164 return Response(data={'error': 'Vnfs does not exist'}, status=status.HTTP_404_NOT_FOUND)
166 for vnf_inst in vnf_insts:
167 arr.append(fill_resp_data(vnf_inst))
168 return Response(data={'resp_data': arr}, status=status.HTTP_200_OK)
169 except Exception as e:
170 logger.error(e.message)
171 logger.error(traceback.format_exc())
172 return Response(data={'error': 'Failed to get Vnfs'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
175 @api_view(http_method_names=['GET'])
176 def get_vms(request, *args, **kwargs):
177 logger.debug("Query all the vms by vnfInstanceId[%s]", fun_name())
179 vnf_inst_id = ignore_case_get(kwargs, "vnfInstanceId")
180 vms = VmInstModel.objects.filter(instid=vnf_inst_id)
182 return Response(data={'error': 'Vms does not exist'}, status=status.HTTP_404_NOT_FOUND)
185 arr.append(fill_vms_data(vm))
186 return Response(data={'resp_data': arr}, status=status.HTTP_200_OK)
187 except Exception as e:
188 logger.error(e.message)
189 logger.error(traceback.format_exc())
190 return Response(data={'error': 'Failed to get Vms'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
193 def fill_vms_data(vm):
197 "resouceid": vm.resouceid,
198 "insttype": vm.insttype,
201 "operationalstate": vm.operationalstate,
203 "is_predefined": vm.is_predefined,
204 "security_groups": vm.security_groups,
205 "flavor_id": vm.flavor_id,
206 "availability_zone": vm.availability_zone,
207 "server_group": vm.server_group,
208 "volume_array": vm.volume_array,
209 "metadata": vm.metadata,
210 "nic_array": vm.nic_array
215 @api_view(http_method_names=['GET'])
216 def get_flavors(request, *args, **kwargs):
217 logger.debug("Query all the flavors by vnfInstanceId[%s]", fun_name())
219 vnf_inst_id = ignore_case_get(kwargs, "vnfInstanceId")
220 flavours = FlavourInstModel.objects.filter(instid=vnf_inst_id)
222 return Response(data={'error': 'Flavours does not exist'}, status=status.HTTP_404_NOT_FOUND)
224 for flavour in flavours:
225 arr.append(fill_flavours_data(flavour))
226 return Response(data={'resp_data': arr}, status=status.HTTP_200_OK)
227 except Exception as e:
228 logger.error(e.message)
229 logger.error(traceback.format_exc())
230 return Response(data={'error': 'Failed to get flavours'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
233 def fill_flavours_data(f):
235 "flavourid": f.flavourid,
239 "extraspecs": f.extraspecs,
243 "resouceid": f.resouceid,
244 "create_time": f.create_time
249 @api_view(http_method_names=['GET'])
250 def get_networks(request, *args, **kwargs):
251 logger.debug("Query all the networks by vnfInstanceId[%s]", fun_name())
253 vnf_inst_id = ignore_case_get(kwargs, "vnfInstanceId")
254 networks = NetworkInstModel.objects.filter(instid=vnf_inst_id)
256 return Response(data={'error': 'Networks does not exist'}, status=status.HTTP_404_NOT_FOUND)
258 for network in networks:
259 arr.append(fill_networks_data(network))
260 return Response(data={'resp_data': arr}, status=status.HTTP_200_OK)
261 except Exception as e:
262 logger.error(e.message)
263 logger.error(traceback.format_exc())
264 return Response(data={'error': 'Failed to get networks'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
267 def fill_networks_data(network):
269 "networkid": network.networkid,
270 "vimid": network.vimid,
271 "resouceid": network.resouceid,
272 "insttype": network.insttype,
273 "instid": network.instid,
279 @api_view(http_method_names=['GET'])
280 def get_subnets(request, *args, **kwargs):
281 logger.debug("Query all the subnets by vnfInstanceId[%s]", fun_name())
283 vnf_inst_id = ignore_case_get(kwargs, "vnfInstanceId")
284 subnets = SubNetworkInstModel.objects.filter(instid=vnf_inst_id)
286 return Response(data={'error': 'Subnets does not exist'}, status=status.HTTP_404_NOT_FOUND)
288 for subnet in subnets:
289 arr.append(fill_subnets_data(subnet))
290 return Response(data={'resp_data': arr}, status=status.HTTP_200_OK)
291 except Exception as e:
292 logger.error(e.message)
293 logger.error(traceback.format_exc())
294 return Response(data={'error': 'Failed to get subnets'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
297 def fill_subnets_data(subnet):
299 "subnetworkid": subnet.subnetworkid,
300 "vimid": subnet.vimid,
301 "resouceid": subnet.resouceid,
302 "networkid": subnet.networkid,
303 "insttype": subnet.insttype,
304 "instid": subnet.instid,
311 class getCps(APIView):
312 @swagger_auto_schema(request_body=NoneSerializer(),
314 status.HTTP_200_OK: CpsInfoSerializer(),
315 status.HTTP_404_NOT_FOUND: 'Cps does not exist',
316 status.HTTP_500_INTERNAL_SERVER_ERROR: 'internal error'})
317 def get(self, request, vnfInstanceId):
318 logger.debug("Query all the cps by vnfInstanceId[%s]", fun_name())
320 cps = CPInstModel.objects.filter(ownerid=vnfInstanceId)
322 return Response(data={'error': 'Cps does not exist'}, status=status.HTTP_404_NOT_FOUND)
325 arr.append(fill_cps_data(cp))
326 cpInfoSerializer = CpsInfoSerializer(data={'resp_data': arr})
327 isValid = cpInfoSerializer.is_valid()
329 raise Exception(cpInfoSerializer.errors)
331 return Response(data=cpInfoSerializer.data, status=status.HTTP_200_OK)
332 except Exception as e:
333 logger.error(e.message)
334 logger.error(traceback.format_exc())
335 return Response(data={'error': 'Failed to get cps'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
338 def fill_cps_data(cp):
340 "cpinstanceid": cp.cpinstanceid,
342 "cpinstancename": cp.cpinstancename,
343 "vlinstanceid": cp.vlinstanceid,
344 "ownertype": cp.ownertype,
345 "ownerid": cp.ownerid,
346 "relatedtype": cp.relatedtype
351 class getVolumes(APIView):
352 @swagger_auto_schema(request_body=NoneSerializer(),
354 status.HTTP_200_OK: VolumeInfoSerializer(),
355 status.HTTP_404_NOT_FOUND: 'Volumes does not exist',
356 status.HTTP_500_INTERNAL_SERVER_ERROR: 'internal error'})
357 def get(self, request, vnfInstanceId):
358 logger.debug("Query all the volumes by vnfInstanceId[%s]", fun_name())
360 volumes = StorageInstModel.objects.filter(instid=vnfInstanceId)
362 return Response(data={'error': 'Volumes does not exist'}, status=status.HTTP_404_NOT_FOUND)
365 arr.append(fill_volumes_data(v))
366 volumeSerializer = VolumeInfoSerializer(data={'resp_data': arr})
367 isValid = volumeSerializer.is_valid()
369 raise Exception(volumeSerializer.errors)
371 return Response(data=volumeSerializer.data, status=status.HTTP_200_OK)
372 except Exception as e:
373 logger.error(e.message)
374 logger.error(traceback.format_exc())
375 return Response(data={'error': 'Failed to get volumes'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
378 def fill_volumes_data(v):
380 "storageid": v.storageid,
382 "resouceid": v.resouceid,
383 "insttype": v.insttype,
385 "storagetype": v.storagetype,