Refactor codes for query vnf
[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
18 from lcm.pub.database.models import StorageInstModel
19 from lcm.pub.database.models import VLInstModel
20 from lcm.pub.database.models import NetworkInstModel
21 from lcm.pub.database.models import VNFCInstModel
22 from lcm.pub.database.models import VmInstModel
23 from lcm.pub.exceptions import NFLCMException
24
25 logger = logging.getLogger(__name__)
26
27
28 class QueryVnf:
29     def __init__(self, data, instanceid=''):
30         self.vnf_inst_id = instanceid
31         self.data = data
32
33     def query_single_vnf(self):
34         vnf_inst = NfInstModel.objects.filter(nfinstid=self.vnf_inst_id)
35         if not vnf_inst.exists():
36             raise NFLCMException('VnfInst(%s) does not exist.' % self.vnf_inst_id)
37         return self.fill_resp_data(vnf_inst[0])
38
39     def query_multi_vnf(self):
40         vnf_insts = NfInstModel.objects.all()
41         return [self.fill_resp_data(vnf_inst) for vnf_inst in vnf_insts]
42
43     def fill_resp_data(self, vnf):
44         logger.info('Get storages')
45         storage_inst = StorageInstModel.objects.filter(instid=vnf.nfinstid)
46         arr = []
47         for s in storage_inst:
48             storage = {
49                 "id": s.storageid,
50                 "storageResource": {
51                     "vimConnectionId": s.vimid,
52                     "resourceId": s.resourceid
53                 }
54             }
55             arr.append(storage)
56
57         logger.info('Get networks')
58         vl_inst = VLInstModel.objects.filter(ownerid=vnf.nfinstid)
59         vl_arr = []
60         for v in vl_inst:
61             net = NetworkInstModel.objects.filter(networkid=v.relatednetworkid)
62             if not net:
63                 raise NFLCMException('NetworkInst(%s) does not exist.' % v.relatednetworkid)
64             v_dic = {
65                 "id": v.vlinstanceid,
66                 "virtualLinkDescId": v.vldid,
67                 "networkResource": {
68                     "vimConnectionId": net[0].vimid,
69                     "resourceId": net[0].resourceid
70                 }
71             }
72             vl_arr.append(v_dic)
73
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
96         resp_data = {
97             "id": vnf.nfinstid,
98             "vnfInstanceName": vnf.nf_name,
99             "vnfInstanceDescription": vnf.nf_desc,
100             "vnfdId": vnf.vnfdid,
101             "vnfProvider": vnf.vendor,
102             "vnfProductName": vnf.netype,
103             "vnfSoftwareVersion": vnf.vnfSoftwareVersion,
104             "vnfdVersion": vnf.version,
105             "vnfPkgId": vnf.package_id,
106             "instantiationState": vnf.status,
107             "instantiatedVnfInfo": {
108                 "flavourId": vnf.flavour_id,
109                 "vnfState": "STARTED",
110                 "scaleStatus": [],
111                 "extCpInfo": [],
112                 "extVirtualLinkInfo": [],
113                 "monitoringParameters": [],
114                 "vnfcResourceInfo": vnfc_arr,
115                 "vnfVirtualLinkResourceInfo": vl_arr,
116                 "virtualStorageResourceInfo": arr
117             }
118         }
119         logger.debug("vnf instance: %s", resp_data)
120         return resp_data