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 # TODO: fill resp_data
42 # resp_data = {"vnfInstanceId": vnf_inst_id, "vnfInstanceName": vnf_inst[0].nf_name, "vnfInstanceDescription":}
43 resp_data = fill_resp_data(vnf_inst[0])
44 return Response(data=resp_data, status=status.HTTP_200_OK)
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 # cp_insts = CPInstModel.objects.filter(ownerid=vnf_inst_id)
53 # raise VNFRESException('CpInst(%s) does not exist.' % vnf_inst_id)
54 # cp_inst = cp_insts.first()
55 logger.info('Get the StorageInstModel of list')
56 storage_inst = StorageInstModel.objects.filter(instid=vnf.nfinstid)
58 for s in storage_inst:
60 "virtualStorageInstanceId": s.storageid,
61 "virtualStorageDescId": s.storageDesc,
64 "resourceId": s.resouceid
68 logger.info('Get the VLInstModel of list.')
69 vl_inst = VLInstModel.objects.filter(ownerid=vnf.nfinstid)
72 net = NetworkInstModel.objects.filter(networkid=v.relatednetworkid)
74 raise VNFRESException('NetworkInst(%s) does not exist.' % v.relatednetworkid)
76 "virtualLinkInstanceId": v.vlinstanceid,
77 "virtualLinkDescId": v.vldid,
79 "vimId": net[0].vimid,
80 "resourceId": net[0].resouceid
84 logger.info('Get VNFCInstModel of list.')
85 vnfc_insts = VNFCInstModel.objects.filter(nfinstid=vnf.nfinstid)
87 for vnfc in vnfc_insts:
88 vm = VmInstModel.objects.filter(vmid=vnfc.vmid)
90 raise VNFRESException('VmInst(%s) does not exist.' % vnfc.vmid)
91 storage = StorageInstModel.objects.filter(ownerid=vm[0].vmid)
93 raise VNFRESException('StorageInst(%s) does not exist.' % vm[0].vmid)
95 "vnfcInstanceId": vnfc.vnfcinstanceid,
99 "resourceId": vm[0].resouceid
101 "storageResourceIds": [s.storageid for s in storage]
103 vnfc_arr.append(vnfc_dic)
104 logger.info('Get the VimInstModel of list.')
105 vms = VmInstModel.objects.filter(instid=vnf.nfinstid)
107 # The 'vimInfoId' and 'vimId' each value are same
109 vims = VimModel.objects.filter(vimid=vm.vimid)
111 vim_users = VimUserModel.objects.filter(vimid=vim.vimid)
113 "vimInfoId": vim.vimid,
117 "apiVersion": vim.version,
118 "protocolType": (vim.apiurl.split(':')[0] if vim.apiurl and vim.apiurl.index(':') else 'http')
121 "tenant": (vim_users[0].defaulttenant if vim_users and vim_users[0].defaulttenant else ''),
122 "username": (vim_users[0].username if vim_users and vim_users[0].username else ''),
123 "password": (vim_users[0].password if vim_users and vim_users[0].password else '')
125 "interfaceEndpoint": vim.apiurl
127 vim_arr.append(vim_dic)
130 "vnfInstanceId": vnf.nfinstid,
131 "vnfInstanceName": vnf.nf_name,
132 "vnfInstanceDescription": vnf.nf_desc,
133 "onboardedVnfPkgInfoId": vnf.package_id,
134 "vnfdId": vnf.vnfdid,
135 "vnfdVersion": vnf.version,
136 "vnfSoftwareVersion": vnf.vnfSoftwareVersion,
137 "vnfProvider": vnf.vendor,
138 "vnfProductName": vnf.producttype,
139 "vnfConfigurableProperties": {vnf.vnfConfigurableProperties},
140 "instantiationState": vnf.instantiationState,
141 "instantiatedVnfInfo": {
142 "flavourId": vnf.flavour_id,
143 "vnfState": vnf.status,
146 "extVirtualLink": [],
147 "monitoringParameters": {},
148 "localizationLanguage": vnf.localizationLanguage,
150 "vnfcResourceInfo": vnfc_arr,
151 "virtualLinkResourceInfo": vl_arr,
152 "virtualStorageResourceInfo": arr
154 "metadata": vnf.input_params,
155 "extensions": vnf.extension
160 @api_view(http_method_names=['GET'])
161 def get_vnfs(request):
162 logger.debug("Query all the vnfs[%s]", fun_name())
164 vnf_insts = NfInstModel.objects.all()
166 return Response(data={'error': 'Vnfs does not exist'}, status=status.HTTP_404_NOT_FOUND)
167 # FIXME: fill resp_data
169 for vnf_inst in vnf_insts:
170 arr.append(fill_resp_data(vnf_inst))
171 return Response(data={'resp_data': arr}, status=status.HTTP_200_OK)
173 logger.error(traceback.format_exc())
174 return Response(data={'error': 'Failed to get Vnfs'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
177 @api_view(http_method_names=['GET'])
178 def get_vms(request, *args, **kwargs):
179 logger.debug("Query all the vms by vnfInstanceId[%s]", fun_name())
181 vnf_inst_id = ignore_case_get(kwargs, "vnfInstanceId")
182 vms = VmInstModel.objects.filter(instid=vnf_inst_id)
184 return Response(data={'error': 'Vms does not exist'}, status=status.HTTP_404_NOT_FOUND)
185 # FIXME: fill resp_data
188 arr.append(fill_vms_data(vm))
189 return Response(data={'resp_data': arr}, status=status.HTTP_200_OK)
191 logger.error(traceback.format_exc())
192 return Response(data={'error': 'Failed to get Vms'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
195 def fill_vms_data(vm):
199 "resouceid": vm.resouceid,
200 "insttype": vm.insttype,
203 "operationalstate": vm.operationalstate,
207 "detailinfo": vm.detailinfo,
208 "is_predefined": vm.is_predefined
213 @api_view(http_method_names=['GET'])
214 def get_flavors(request, *args, **kwargs):
215 logger.debug("Query all the flavors by vnfInstanceId[%s]", fun_name())
217 vnf_inst_id = ignore_case_get(kwargs, "vnfInstanceId")
218 flavours = FlavourInstModel.objects.filter(instid=vnf_inst_id)
220 return Response(data={'error': 'Flavours does not exist'}, status=status.HTTP_404_NOT_FOUND)
221 # FIXME: fill resp_data
223 for flavour in flavours:
224 arr.append(fill_flavours_data(flavour))
225 return Response(data={'resp_data': arr}, status=status.HTTP_200_OK)
227 logger.error(traceback.format_exc())
228 return Response(data={'error': 'Failed to get flavours'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
231 def fill_flavours_data(f):
233 "flavourid": f.flavourid,
237 "extraspecs": f.extraspecs,
241 "create_time": f.create_time
245 @api_view(http_method_names=['GET'])
246 def get_networks(request, *args, **kwargs):
247 logger.debug("Query all the networks by vnfInstanceId[%s]", fun_name())
249 vnf_inst_id = ignore_case_get(kwargs, "vnfInstanceId")
250 networks = NetworkInstModel.objects.filter(instid=vnf_inst_id)
252 return Response(data={'error': 'Networks does not exist'}, status=status.HTTP_404_NOT_FOUND)
253 # FIXME: fill resp_data
255 for network in networks:
256 arr.append(fill_networks_data(network))
257 return Response(data={'resp_data': arr}, status=status.HTTP_200_OK)
259 logger.error(traceback.format_exc())
260 return Response(data={'error': 'Failed to get networks'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
263 def fill_networks_data(network):
265 "networkid": network.networkid,
266 "vimid": network.vimid,
267 "resouceid": network.resouceid,
268 "insttype": network.insttype,
269 "instid": network.instid,
271 # "tenant": network.tenant,
272 # "is_shared": network.is_shared,
273 # "is_predefined": network.is_predefined,
274 # "desc": network.desc,
275 # "vendor": network.vendor,
276 # "bandwidth": network.bandwidth,
277 # "mtu": network.mtu,
278 # "network_type": network.network_type,
279 # "segmentid": network.segmentid,
280 # "vlantrans": network.vlantrans,
281 # "networkqos": network.networkqos
286 @api_view(http_method_names=['GET'])
287 def get_subnets(request, *args, **kwargs):
288 logger.debug("Query all the subnets by vnfInstanceId[%s]", fun_name())
290 vnf_inst_id = ignore_case_get(kwargs, "vnfInstanceId")
291 subnets = SubNetworkInstModel.objects.filter(instid=vnf_inst_id)
293 return Response(data={'error': 'Subnets does not exist'}, status=status.HTTP_404_NOT_FOUND)
294 # FIXME: fill resp_data
296 for subnet in subnets:
297 arr.append(fill_subnets_data(subnet))
298 return Response(data={'resp_data': arr}, status=status.HTTP_200_OK)
300 logger.error(traceback.format_exc())
301 return Response(data={'error': 'Failed to get subnets'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
304 def fill_subnets_data(subnet):
306 "subnetworkid": subnet.subnetworkid,
307 "vimid": subnet.vimid,
308 "resouceid": subnet.resouceid,
309 "networkid": subnet.networkid,
310 "insttype": subnet.insttype,
311 "instid": subnet.instid,
318 @api_view(http_method_names=['GET'])
319 def get_cps(request, *args, **kwargs):
320 logger.debug("Query all the cps by vnfInstanceId[%s]", fun_name())
322 vnf_inst_id = ignore_case_get(kwargs, "vnfInstanceId")
323 cps = CPInstModel.objects.filter(ownerid=vnf_inst_id)
325 return Response(data={'error': 'Cps does not exist'}, status=status.HTTP_404_NOT_FOUND)
326 # FIXME: fill resp_data
329 arr.append(fill_cps_data(cp))
330 return Response(data={'resp_data': arr}, status=status.HTTP_200_OK)
332 logger.error(traceback.format_exc())
333 return Response(data={'error': 'Failed to get cps'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
336 def fill_cps_data(cp):
338 "cpinstanceid": cp.cpinstanceid,
340 "cpinstancename": cp.cpinstancename,
341 "vlinstanceid": cp.vlinstanceid,
342 "ownertype": cp.ownertype,
343 "ownerid": cp.ownerid,
344 "relatedtype": cp.relatedtype
348 @api_view(http_method_names=['GET'])
349 def get_volumes(request, *args, **kwargs):
350 logger.debug("Query all the volumes by vnfInstanceId[%s]", fun_name())
352 vnf_inst_id = ignore_case_get(kwargs, "vnfInstanceId")
353 volumes = StorageInstModel.objects.filter(instid=vnf_inst_id)
355 return Response(data={'error': 'Volumes does not exist'}, status=status.HTTP_404_NOT_FOUND)
356 # FIXME: fill resp_data
359 arr.append(fill_volumes_data(v))
360 return Response(data={'resp_data': arr}, status=status.HTTP_200_OK)
362 logger.error(traceback.format_exc())
363 return Response(data={'error': 'Failed to get volumes'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
366 def fill_volumes_data(v):
368 "storageid": v.storageid,
370 "resouceid": v.resouceid,
371 "insttype": v.insttype,
373 "storagetype": v.storagetype,
375 "disktype": v.disktype
380 class SwaggerJsonView(APIView):
381 def get(self, request):
382 json_file = os.path.join(os.path.dirname(__file__), 'swagger.json')
384 json_data = json.JSONDecoder().decode(f.read())
386 return Response(json_data)