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.
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.exceptions import VNFRESException
25 from res.pub.utils.values import ignore_case_get
26 from res.pub.utils.syscomm import fun_name
27 from res.pub.database.models import NfInstModel, StorageInstModel, NetworkInstModel, VLInstModel, \
28 VNFCInstModel, VmInstModel, FlavourInstModel, SubNetworkInstModel, CPInstModel
30 logger = logging.getLogger(__name__)
33 @api_view(http_method_names=['GET'])
34 def get_vnf(request, *args, **kwargs):
35 vnf_inst_id = ignore_case_get(kwargs, "vnfInstanceId")
36 logger.debug("[%s]vnf_inst_id=%s", fun_name(), vnf_inst_id)
38 vnf_inst = NfInstModel.objects.filter(nfinstid=vnf_inst_id)
40 return Response(data={'error': 'Vnf(%s) does not exist' % vnf_inst_id}, status=status.HTTP_404_NOT_FOUND)
41 resp_data = fill_resp_data(vnf_inst[0])
42 return Response(data=resp_data, status=status.HTTP_200_OK)
44 logger.error(traceback.format_exc())
45 return Response(data={'error': 'Failed to get Vnf(%s)' % vnf_inst_id}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
48 def fill_resp_data(vnf):
49 logger.info('Get the StorageInstModel of list')
50 storage_inst = StorageInstModel.objects.filter(instid=vnf.nfinstid)
52 for s in storage_inst:
54 "virtualStorageInstanceId": s.storageid,
55 "virtualStorageDescId": s.storagetype,
58 "resourceId": s.resouceid
62 logger.info('Get the VLInstModel of list.')
63 vl_inst = VLInstModel.objects.filter(ownerid=vnf.nfinstid)
66 net = NetworkInstModel.objects.filter(networkid=v.relatednetworkid)
68 raise VNFRESException('NetworkInst(%s) does not exist.' % v.relatednetworkid)
70 "virtualLinkInstanceId": v.vlinstanceid,
71 "virtualLinkDescId": v.vldid,
73 "vimId": net[0].vimid,
74 "resourceId": net[0].resouceid
78 logger.info('Get VNFCInstModel of list.')
79 vnfc_insts = VNFCInstModel.objects.filter(instid=vnf.nfinstid)
81 for vnfc in vnfc_insts:
82 vm = VmInstModel.objects.filter(vmid=vnfc.vmid)
84 raise VNFRESException('VmInst(%s) does not exist.' % vnfc.vmid)
85 storage = StorageInstModel.objects.filter(ownerid=vm[0].vmid)
87 raise VNFRESException('StorageInst(%s) does not exist.' % vm[0].vmid)
89 "vnfcInstanceId": vnfc.vnfcinstanceid,
93 "resourceId": vm[0].resouceid
95 "storageResourceIds": [s.storageid for s in storage]
97 vnfc_arr.append(vnfc_dic)
98 logger.info('Get the VimInstModel of list.')
99 vms = VmInstModel.objects.filter(instid=vnf.nfinstid)
101 # The 'vimInfoId' and 'vimId' each value are same
107 "resouceid": vm.resouceid,
109 "nic_array": vm.nic_array,
110 "metadata": vm.metadata,
111 "volume_array": vm.volume_array,
112 "server_group": vm.server_group,
113 "availability_zone": vm.availability_zone,
114 "flavor_id": vm.flavor_id,
115 "security_groups": vm.security_groups,
116 "operationalstate": vm.operationalstate,
117 "insttype": vm.insttype,
118 "is_predefined": vm.is_predefined,
119 "create_time": vm.create_time,
123 vm_arr.append(vm_dic)
126 "vnfInstanceId": vnf.nfinstid,
127 "vnfInstanceName": vnf.nf_name,
128 "vnfInstanceDescription": vnf.nf_desc,
129 "onboardedVnfPkgInfoId": vnf.package_id,
130 "vnfdId": vnf.vnfdid,
131 "vnfdVersion": vnf.version,
132 "vnfSoftwareVersion": vnf.vnfSoftwareVersion,
133 "vnfProvider": vnf.vendor,
134 "vnfProductName": vnf.netype,
135 "vnfConfigurableProperties": {vnf.vnfConfigurableProperties},
136 "instantiationState": vnf.status,
137 "instantiatedVnfInfo": {
138 "flavourId": vnf.flavour_id,
139 "vnfState": vnf.status,
142 "extVirtualLink": [],
143 "monitoringParameters": {},
144 "localizationLanguage": vnf.localizationLanguage,
146 "vnfcResourceInfo": vnfc_arr,
147 "virtualLinkResourceInfo": vl_arr,
148 "virtualStorageResourceInfo": arr
150 "metadata": vnf.input_params,
151 "extensions": vnf.vnfd_model
156 @api_view(http_method_names=['GET'])
157 def get_vnfs(request):
158 logger.debug("Query all the vnfs[%s]", fun_name())
160 vnf_insts = NfInstModel.objects.all()
162 return Response(data={'error': 'Vnfs does not exist'}, status=status.HTTP_404_NOT_FOUND)
164 for vnf_inst in vnf_insts:
165 arr.append(fill_resp_data(vnf_inst))
166 return Response(data={'resp_data': arr}, status=status.HTTP_200_OK)
168 logger.error(traceback.format_exc())
169 return Response(data={'error': 'Failed to get Vnfs'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
172 @api_view(http_method_names=['GET'])
173 def get_vms(request, *args, **kwargs):
174 logger.debug("Query all the vms by vnfInstanceId[%s]", fun_name())
176 vnf_inst_id = ignore_case_get(kwargs, "vnfInstanceId")
177 vms = VmInstModel.objects.filter(instid=vnf_inst_id)
179 return Response(data={'error': 'Vms does not exist'}, status=status.HTTP_404_NOT_FOUND)
182 arr.append(fill_vms_data(vm))
183 return Response(data={'resp_data': arr}, status=status.HTTP_200_OK)
185 logger.error(traceback.format_exc())
186 return Response(data={'error': 'Failed to get Vms'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
189 def fill_vms_data(vm):
193 "resouceid": vm.resouceid,
194 "insttype": vm.insttype,
197 "operationalstate": vm.operationalstate,
199 "is_predefined": vm.is_predefined,
200 "security_groups": vm.security_groups,
201 "flavor_id": vm.flavor_id,
202 "availability_zone": vm.availability_zone,
203 "server_group": vm.server_group,
204 "volume_array": vm.volume_array,
205 "metadata": vm.metadata,
206 "nic_array": vm.nic_array
211 @api_view(http_method_names=['GET'])
212 def get_flavors(request, *args, **kwargs):
213 logger.debug("Query all the flavors by vnfInstanceId[%s]", fun_name())
215 vnf_inst_id = ignore_case_get(kwargs, "vnfInstanceId")
216 flavours = FlavourInstModel.objects.filter(instid=vnf_inst_id)
218 return Response(data={'error': 'Flavours does not exist'}, status=status.HTTP_404_NOT_FOUND)
220 for flavour in flavours:
221 arr.append(fill_flavours_data(flavour))
222 return Response(data={'resp_data': arr}, status=status.HTTP_200_OK)
224 logger.error(traceback.format_exc())
225 return Response(data={'error': 'Failed to get flavours'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
228 def fill_flavours_data(f):
230 "flavourid": f.flavourid,
234 "extraspecs": f.extraspecs,
238 "resouceid": f.resouceid,
239 "create_time": f.create_time
243 @api_view(http_method_names=['GET'])
244 def get_networks(request, *args, **kwargs):
245 logger.debug("Query all the networks by vnfInstanceId[%s]", fun_name())
247 vnf_inst_id = ignore_case_get(kwargs, "vnfInstanceId")
248 networks = NetworkInstModel.objects.filter(instid=vnf_inst_id)
250 return Response(data={'error': 'Networks does not exist'}, status=status.HTTP_404_NOT_FOUND)
252 for network in networks:
253 arr.append(fill_networks_data(network))
254 return Response(data={'resp_data': arr}, status=status.HTTP_200_OK)
256 logger.error(traceback.format_exc())
257 return Response(data={'error': 'Failed to get networks'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
260 def fill_networks_data(network):
262 "networkid": network.networkid,
263 "vimid": network.vimid,
264 "resouceid": network.resouceid,
265 "insttype": network.insttype,
266 "instid": network.instid,
268 # "tenant": network.tenant,
269 # "is_shared": network.is_shared,
270 # "is_predefined": network.is_predefined,
271 # "desc": network.desc,
272 # "vendor": network.vendor,
273 # "bandwidth": network.bandwidth,
274 # "mtu": network.mtu,
275 # "network_type": network.network_type,
276 # "segmentid": network.segmentid,
277 # "vlantrans": network.vlantrans,
278 # "networkqos": network.networkqos
283 @api_view(http_method_names=['GET'])
284 def get_subnets(request, *args, **kwargs):
285 logger.debug("Query all the subnets by vnfInstanceId[%s]", fun_name())
287 vnf_inst_id = ignore_case_get(kwargs, "vnfInstanceId")
288 subnets = SubNetworkInstModel.objects.filter(instid=vnf_inst_id)
290 return Response(data={'error': 'Subnets does not exist'}, status=status.HTTP_404_NOT_FOUND)
292 for subnet in subnets:
293 arr.append(fill_subnets_data(subnet))
294 return Response(data={'resp_data': arr}, status=status.HTTP_200_OK)
296 logger.error(traceback.format_exc())
297 return Response(data={'error': 'Failed to get subnets'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
300 def fill_subnets_data(subnet):
302 "subnetworkid": subnet.subnetworkid,
303 "vimid": subnet.vimid,
304 "resouceid": subnet.resouceid,
305 "networkid": subnet.networkid,
306 "insttype": subnet.insttype,
307 "instid": subnet.instid,
314 @api_view(http_method_names=['GET'])
315 def get_cps(request, *args, **kwargs):
316 logger.debug("Query all the cps by vnfInstanceId[%s]", fun_name())
318 vnf_inst_id = ignore_case_get(kwargs, "vnfInstanceId")
319 cps = CPInstModel.objects.filter(ownerid=vnf_inst_id)
321 return Response(data={'error': 'Cps does not exist'}, status=status.HTTP_404_NOT_FOUND)
324 arr.append(fill_cps_data(cp))
325 return Response(data={'resp_data': arr}, status=status.HTTP_200_OK)
327 logger.error(traceback.format_exc())
328 return Response(data={'error': 'Failed to get cps'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
331 def fill_cps_data(cp):
333 "cpinstanceid": cp.cpinstanceid,
335 "cpinstancename": cp.cpinstancename,
336 "vlinstanceid": cp.vlinstanceid,
337 "ownertype": cp.ownertype,
338 "ownerid": cp.ownerid,
339 "relatedtype": cp.relatedtype
343 @api_view(http_method_names=['GET'])
344 def get_volumes(request, *args, **kwargs):
345 logger.debug("Query all the volumes by vnfInstanceId[%s]", fun_name())
347 vnf_inst_id = ignore_case_get(kwargs, "vnfInstanceId")
348 volumes = StorageInstModel.objects.filter(instid=vnf_inst_id)
350 return Response(data={'error': 'Volumes does not exist'}, status=status.HTTP_404_NOT_FOUND)
353 arr.append(fill_volumes_data(v))
354 return Response(data={'resp_data': arr}, status=status.HTTP_200_OK)
356 logger.error(traceback.format_exc())
357 return Response(data={'error': 'Failed to get volumes'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
360 def fill_volumes_data(v):
362 "storageid": v.storageid,
364 "resouceid": v.resouceid,
365 "insttype": v.insttype,
367 "storagetype": v.storagetype,
373 class SwaggerJsonView(APIView):
374 def get(self, request):
375 json_file = os.path.join(os.path.dirname(__file__), 'swagger.json')
377 json_data = json.JSONDecoder().decode(f.read())
379 return Response(json_data)