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, VimModel, VimUserModel, 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.storageDesc,
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(nfinstid=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
103 vims = VimModel.objects.filter(vimid=vm.vimid)
105 vim_users = VimUserModel.objects.filter(vimid=vim.vimid)
107 "vimInfoId": vim.vimid,
111 "apiVersion": vim.version,
112 "protocolType": (vim.apiurl.split(':')[0] if vim.apiurl and vim.apiurl.index(':') else 'http')
115 "tenant": (vim_users[0].defaulttenant if vim_users and vim_users[0].defaulttenant else ''),
116 "username": (vim_users[0].username if vim_users and vim_users[0].username else ''),
117 "password": (vim_users[0].password if vim_users and vim_users[0].password else '')
119 "interfaceEndpoint": vim.apiurl
121 vim_arr.append(vim_dic)
124 "vnfInstanceId": vnf.nfinstid,
125 "vnfInstanceName": vnf.nf_name,
126 "vnfInstanceDescription": vnf.nf_desc,
127 "onboardedVnfPkgInfoId": vnf.package_id,
128 "vnfdId": vnf.vnfdid,
129 "vnfdVersion": vnf.version,
130 "vnfSoftwareVersion": vnf.vnfSoftwareVersion,
131 "vnfProvider": vnf.vendor,
132 "vnfProductName": vnf.producttype,
133 "vnfConfigurableProperties": {vnf.vnfConfigurableProperties},
134 "instantiationState": vnf.instantiationState,
135 "instantiatedVnfInfo": {
136 "flavourId": vnf.flavour_id,
137 "vnfState": vnf.status,
140 "extVirtualLink": [],
141 "monitoringParameters": {},
142 "localizationLanguage": vnf.localizationLanguage,
144 "vnfcResourceInfo": vnfc_arr,
145 "virtualLinkResourceInfo": vl_arr,
146 "virtualStorageResourceInfo": arr
148 "metadata": vnf.input_params,
149 "extensions": vnf.extension
154 @api_view(http_method_names=['GET'])
155 def get_vnfs(request):
156 logger.debug("Query all the vnfs[%s]", fun_name())
158 vnf_insts = NfInstModel.objects.all()
160 return Response(data={'error': 'Vnfs does not exist'}, status=status.HTTP_404_NOT_FOUND)
162 for vnf_inst in vnf_insts:
163 arr.append(fill_resp_data(vnf_inst))
164 return Response(data={'resp_data': arr}, status=status.HTTP_200_OK)
166 logger.error(traceback.format_exc())
167 return Response(data={'error': 'Failed to get Vnfs'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
170 @api_view(http_method_names=['GET'])
171 def get_vms(request, *args, **kwargs):
172 logger.debug("Query all the vms by vnfInstanceId[%s]", fun_name())
174 vnf_inst_id = ignore_case_get(kwargs, "vnfInstanceId")
175 vms = VmInstModel.objects.filter(instid=vnf_inst_id)
177 return Response(data={'error': 'Vms does not exist'}, status=status.HTTP_404_NOT_FOUND)
180 arr.append(fill_vms_data(vm))
181 return Response(data={'resp_data': arr}, status=status.HTTP_200_OK)
183 logger.error(traceback.format_exc())
184 return Response(data={'error': 'Failed to get Vms'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
187 def fill_vms_data(vm):
191 "resouceid": vm.resouceid,
192 "insttype": vm.insttype,
195 "operationalstate": vm.operationalstate,
199 "detailinfo": vm.detailinfo,
200 "is_predefined": vm.is_predefined
205 @api_view(http_method_names=['GET'])
206 def get_flavors(request, *args, **kwargs):
207 logger.debug("Query all the flavors by vnfInstanceId[%s]", fun_name())
209 vnf_inst_id = ignore_case_get(kwargs, "vnfInstanceId")
210 flavours = FlavourInstModel.objects.filter(instid=vnf_inst_id)
212 return Response(data={'error': 'Flavours does not exist'}, status=status.HTTP_404_NOT_FOUND)
214 for flavour in flavours:
215 arr.append(fill_flavours_data(flavour))
216 return Response(data={'resp_data': arr}, status=status.HTTP_200_OK)
218 logger.error(traceback.format_exc())
219 return Response(data={'error': 'Failed to get flavours'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
222 def fill_flavours_data(f):
224 "flavourid": f.flavourid,
228 "extraspecs": f.extraspecs,
232 "create_time": f.create_time
237 @api_view(http_method_names=['GET'])
238 def get_networks(request, *args, **kwargs):
239 logger.debug("Query all the networks by vnfInstanceId[%s]", fun_name())
241 vnf_inst_id = ignore_case_get(kwargs, "vnfInstanceId")
242 networks = NetworkInstModel.objects.filter(instid=vnf_inst_id)
244 return Response(data={'error': 'Networks does not exist'}, status=status.HTTP_404_NOT_FOUND)
246 for network in networks:
247 arr.append(fill_networks_data(network))
248 return Response(data={'resp_data': arr}, status=status.HTTP_200_OK)
250 logger.error(traceback.format_exc())
251 return Response(data={'error': 'Failed to get networks'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
254 def fill_networks_data(network):
256 "networkid": network.networkid,
257 "vimid": network.vimid,
258 "resouceid": network.resouceid,
259 "insttype": network.insttype,
260 "instid": network.instid,
262 # "tenant": network.tenant,
263 # "is_shared": network.is_shared,
264 # "is_predefined": network.is_predefined,
265 # "desc": network.desc,
266 # "vendor": network.vendor,
267 # "bandwidth": network.bandwidth,
268 # "mtu": network.mtu,
269 # "network_type": network.network_type,
270 # "segmentid": network.segmentid,
271 # "vlantrans": network.vlantrans,
272 # "networkqos": network.networkqos
277 @api_view(http_method_names=['GET'])
278 def get_subnets(request, *args, **kwargs):
279 logger.debug("Query all the subnets by vnfInstanceId[%s]", fun_name())
281 vnf_inst_id = ignore_case_get(kwargs, "vnfInstanceId")
282 subnets = SubNetworkInstModel.objects.filter(instid=vnf_inst_id)
284 return Response(data={'error': 'Subnets does not exist'}, status=status.HTTP_404_NOT_FOUND)
286 for subnet in subnets:
287 arr.append(fill_subnets_data(subnet))
288 return Response(data={'resp_data': arr}, status=status.HTTP_200_OK)
290 logger.error(traceback.format_exc())
291 return Response(data={'error': 'Failed to get subnets'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
294 def fill_subnets_data(subnet):
296 "subnetworkid": subnet.subnetworkid,
297 "vimid": subnet.vimid,
298 "resouceid": subnet.resouceid,
299 "networkid": subnet.networkid,
300 "insttype": subnet.insttype,
301 "instid": subnet.instid,
308 @api_view(http_method_names=['GET'])
309 def get_cps(request, *args, **kwargs):
310 logger.debug("Query all the cps by vnfInstanceId[%s]", fun_name())
312 vnf_inst_id = ignore_case_get(kwargs, "vnfInstanceId")
313 cps = CPInstModel.objects.filter(ownerid=vnf_inst_id)
315 return Response(data={'error': 'Cps does not exist'}, status=status.HTTP_404_NOT_FOUND)
318 arr.append(fill_cps_data(cp))
319 return Response(data={'resp_data': arr}, status=status.HTTP_200_OK)
321 logger.error(traceback.format_exc())
322 return Response(data={'error': 'Failed to get cps'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
325 def fill_cps_data(cp):
327 "cpinstanceid": cp.cpinstanceid,
329 "cpinstancename": cp.cpinstancename,
330 "vlinstanceid": cp.vlinstanceid,
331 "ownertype": cp.ownertype,
332 "ownerid": cp.ownerid,
333 "relatedtype": cp.relatedtype
338 @api_view(http_method_names=['GET'])
339 def get_volumes(request, *args, **kwargs):
340 logger.debug("Query all the volumes by vnfInstanceId[%s]", fun_name())
342 vnf_inst_id = ignore_case_get(kwargs, "vnfInstanceId")
343 volumes = StorageInstModel.objects.filter(instid=vnf_inst_id)
345 return Response(data={'error': 'Volumes does not exist'}, status=status.HTTP_404_NOT_FOUND)
348 arr.append(fill_volumes_data(v))
349 return Response(data={'resp_data': arr}, status=status.HTTP_200_OK)
351 logger.error(traceback.format_exc())
352 return Response(data={'error': 'Failed to get volumes'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
355 def fill_volumes_data(v):
357 "storageid": v.storageid,
359 "resouceid": v.resouceid,
360 "insttype": v.insttype,
362 "storagetype": v.storagetype,
364 "disktype": v.disktype
369 class SwaggerJsonView(APIView):
370 def get(self, request):
371 json_file = os.path.join(os.path.dirname(__file__), 'swagger.json')
373 json_data = json.JSONDecoder().decode(f.read())
375 return Response(json_data)