fix storage query error
[vfc/gvnfm/vnflcm.git] / lcm / lcm / nf / biz / query_vnf.py
1 # Copyright 2017 ZTE Corporation.
2 #
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
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 import logging
16
17 from lcm.pub.database.models import NfInstModel, StorageInstModel, VLInstModel, NetworkInstModel, VNFCInstModel, \
18     VmInstModel
19 from lcm.pub.exceptions import NFLCMException
20
21 logger = logging.getLogger(__name__)
22
23
24 class QueryVnf:
25     def __init__(self, data, instanceid=''):
26         self.vnf_inst_id = instanceid
27         self.data = data
28
29     def query_single_vnf(self):
30         vnf_inst = NfInstModel.objects.filter(nfinstid=self.vnf_inst_id)
31         if not vnf_inst.exists():
32             raise NFLCMException('VnfInst(%s) does not exist.' % self.vnf_inst_id)
33         resp_data = self.fill_resp_data(vnf_inst[0])
34         return resp_data
35
36     def query_multi_vnf(self):
37         vnf_insts = NfInstModel.objects.all()
38         if not vnf_insts:
39             raise NFLCMException('VnfInsts does not exist.')
40         resp_data = []
41         for vnf_inst in vnf_insts:
42             resp_data.append(self.fill_resp_data(vnf_inst))
43         return resp_data
44
45     def fill_resp_data(self, vnf):
46         logger.info('Get storages')
47         storage_inst = StorageInstModel.objects.filter(instid=vnf.nfinstid)
48         arr = []
49         for s in storage_inst:
50             storage = {
51                 "id": s.storageid,
52                 "storageResource": {
53                     "vimConnectionId": s.vimid,
54                     "resourceId": s.resourceid
55                 }
56             }
57             arr.append(storage)
58         logger.info('Get networks')
59         vl_inst = VLInstModel.objects.filter(ownerid=vnf.nfinstid)
60         vl_arr = []
61         for v in vl_inst:
62             net = NetworkInstModel.objects.filter(networkid=v.relatednetworkid)
63             if not net:
64                 raise NFLCMException('NetworkInst(%s) does not exist.' % v.relatednetworkid)
65             v_dic = {
66                 "id": v.vlinstanceid,
67                 "virtualLinkDescId": v.vldid,
68                 "networkResource": {
69                     "vimConnectionId": net[0].vimid,
70                     "resourceId": net[0].resourceid
71                 }
72             }
73             vl_arr.append(v_dic)
74         logger.info('Get vnfcs')
75         vnfc_insts = VNFCInstModel.objects.filter(instid=vnf.nfinstid)
76         vnfc_arr = []
77         for vnfc in vnfc_insts:
78             vm = VmInstModel.objects.filter(vmid=vnfc.vmid)
79             if not vm:
80                 raise NFLCMException('VmInst(%s) does not exist.' % vnfc.vmid)
81             if vm[0].volume_array:
82                 storage = StorageInstModel.objects.filter(resourceid__in=vm[0].volume_array)
83             else:
84                 storage = []
85             vnfc_dic = {
86                 "id": vnfc.vnfcinstanceid,
87                 "vduId": vnfc.vduid,
88                 "computeResource": {
89                     "vimConnectionId": vm[0].vimid,
90                     "resourceId": vm[0].resourceid
91                 },
92                 "storageResourceIds": [s.storageid for s in storage]
93             }
94             vnfc_arr.append(vnfc_dic)
95         logger.info('Get vms')
96
97         resp_data = {
98             "id": vnf.nfinstid,
99             "vnfInstanceName": vnf.nf_name,
100             "vnfPkgId": vnf.package_id,
101             "vnfdVersion": vnf.version,
102             "vnfProvider": vnf.vendor,
103             "instantiationState": vnf.status,
104             "instantiatedVnfInfo": {
105                 "flavourId": vnf.flavour_id,
106                 "vnfState": "STARTED",
107                 "scaleStatus": [],
108                 "extCpInfo": [],
109                 "extVirtualLinkInfo": [],
110                 "monitoringParameters": {},
111                 "vnfcResourceInfo": vnfc_arr,
112                 "vnfVirtualLinkResourceInfo": vl_arr,
113                 "virtualStorageResourceInfo": arr
114             }
115         }
116         return resp_data