d460c03bbf1d0b37c2f7c5a149db80a66cd2f0e9
[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         if vnf_insts:
42             for vnf_inst in vnf_insts:
43                 resp_data.append(self.fill_resp_data(vnf_inst))
44         return resp_data
45
46     def fill_resp_data(self, vnf):
47         logger.info('Get storages')
48         storage_inst = StorageInstModel.objects.filter(instid=vnf.nfinstid)
49         arr = []
50         for s in storage_inst:
51             storage = {
52                 "id": s.storageid,
53                 "storageResource": {
54                     "vimConnectionId": s.vimid,
55                     "resourceId": s.resourceid
56                 }
57             }
58             arr.append(storage)
59         logger.info('Get networks')
60         vl_inst = VLInstModel.objects.filter(ownerid=vnf.nfinstid)
61         vl_arr = []
62         for v in vl_inst:
63             net = NetworkInstModel.objects.filter(networkid=v.relatednetworkid)
64             if not net:
65                 raise NFLCMException('NetworkInst(%s) does not exist.' % v.relatednetworkid)
66             v_dic = {
67                 "id": v.vlinstanceid,
68                 "virtualLinkDescId": v.vldid,
69                 "networkResource": {
70                     "vimConnectionId": net[0].vimid,
71                     "resourceId": net[0].resourceid
72                 }
73             }
74             vl_arr.append(v_dic)
75         logger.info('Get vnfcs')
76         vnfc_insts = VNFCInstModel.objects.filter(instid=vnf.nfinstid)
77         vnfc_arr = []
78         for vnfc in vnfc_insts:
79             vm = VmInstModel.objects.filter(vmid=vnfc.vmid)
80             if not vm:
81                 raise NFLCMException('VmInst(%s) does not exist.' % vnfc.vmid)
82             if vm[0].volume_array:
83                 storage = StorageInstModel.objects.filter(resourceid__in=vm[0].volume_array)
84             else:
85                 storage = []
86             vnfc_dic = {
87                 "id": vnfc.vnfcinstanceid,
88                 "vduId": vnfc.vduid,
89                 "computeResource": {
90                     "vimConnectionId": vm[0].vimid,
91                     "resourceId": vm[0].resourceid
92                 },
93                 "storageResourceIds": [s.storageid for s in storage]
94             }
95             vnfc_arr.append(vnfc_dic)
96         logger.info('Get vms')
97
98         resp_data = {
99             "id": vnf.nfinstid,
100             "vnfInstanceName": vnf.nf_name,
101             "vnfInstanceDescription": vnf.nf_desc,
102             "vnfdId": vnf.vnfdid,
103             "vnfProvider": vnf.vendor,
104             "vnfProductName": vnf.netype,
105             "vnfSoftwareVersion": vnf.vnfSoftwareVersion,
106             "vnfdVersion": vnf.version,
107             "vnfPkgId": vnf.package_id,
108             "instantiationState": vnf.status,
109             "instantiatedVnfInfo": {
110                 "flavourId": vnf.flavour_id,
111                 "vnfState": "STARTED",
112                 "scaleStatus": [],
113                 "extCpInfo": [],
114                 "extVirtualLinkInfo": [],
115                 "monitoringParameters": [],
116                 "vnfcResourceInfo": vnfc_arr,
117                 "vnfVirtualLinkResourceInfo": vl_arr,
118                 "virtualStorageResourceInfo": arr
119             }
120         }
121         logger.debug("vnf instance: %s", resp_data)
122         return resp_data