3ef1a8d708f015f14d2fb9af1435ce35ded6f781
[vfc/gvnfm/vnflcm.git] / lcm / lcm / nf / vnfs / vnf_query / 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 import logging
15
16 from lcm.pub.database.models import NfInstModel, StorageInstModel, VLInstModel, NetworkInstModel, VNFCInstModel, \
17     VmInstModel, VimModel, VimUserModel
18 from lcm.pub.exceptions import NFLCMException
19
20 logger = logging.getLogger(__name__)
21
22
23 class QueryVnf:
24     def __init__(self, data, instanceid=''):
25         self.vnf_inst_id = instanceid
26         self.data = data
27         pass
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 the list of vloumes')
47         storage_inst = StorageInstModel.objects.filter(instid=vnf.nfinstid)
48         arr = []
49         for s in storage_inst:
50             storage = {
51                 "virtualStorageInstanceId": s.storageid,
52                 "storageResource": {
53                     "vimId": s.vimid,
54                     "resourceId": s.resouceid
55                 }
56             }
57             arr.append(storage)
58         logger.info('Get the VLInstModel of list.')
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                 "virtualLinkInstanceId": v.vlinstanceid,
67                 "virtualLinkDescId": v.vldid,
68                 "networkResource": {
69                     "vimId": net[0].vimid,
70                     "resourceId": net[0].resouceid
71                 }
72             }
73             vl_arr.append(v_dic)
74         logger.info('Get VNFCInstModel of list.')
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             storage = StorageInstModel.objects.filter(ownerid=vm[0].vmid)
82             if not storage:
83                 raise NFLCMException('StorageInst(%s) does not exist.' % vm[0].vmid)
84             vnfc_dic = {
85                 "vnfcInstanceId": vnfc.vnfcinstanceid,
86                 "vduId": vnfc.vduid,
87                 "computeResource": {
88                     "vimId": vm[0].vimid,
89                     "resourceId": vm[0].resouceid
90                 },
91                 "storageResourceIds": [s.storageid for s in storage]
92             }
93             vnfc_arr.append(vnfc_dic)
94         logger.info('Get the VimInstModel of list.')
95         vms = VmInstModel.objects.filter(instid=vnf.nfinstid)
96         vim_arr = []
97         # The 'vimInfoId' and 'vimId' each value are same
98         for vm in vms:
99             vims = VimModel.objects.filter(vimid=vm.vimid)
100             for vim in vims:
101                 vim_users = VimUserModel.objects.filter(vimid=vim.vimid)
102                 vim_dic = {
103                     "vimInfoId": vim.vimid,
104                     "vimId": vim.vimid,
105                     "interfaceInfo": {
106                         "vimType": vim.type,
107                         "apiVersion": vim.version,
108                         "protocolType": (vim.apiurl.split(':')[0] if vim.apiurl and vim.apiurl.index(':') else 'http')
109                     },
110                     "accessInfo": {
111                         "tenant": (vim_users[0].defaulttenant if vim_users and vim_users[0].defaulttenant else ''),
112                         "username": (vim_users[0].username if vim_users and vim_users[0].username else ''),
113                         "password": (vim_users[0].password if vim_users and vim_users[0].password else '')
114                     },
115                     "interfaceEndpoint": vim.apiurl
116                 }
117                 vim_arr.append(vim_dic)
118
119         resp_data = {
120             "vnfInstanceId": vnf.nfinstid,
121             "vnfInstanceName": vnf.nf_name,
122             # "vnfInstanceDescription": vnf.nf_desc,
123             "onboardedVnfPkgInfoId": vnf.package_id,
124             # "vnfdId": vnf.vnfdid,
125             "vnfdVersion": vnf.version,
126             # "vnfSoftwareVersion": vnf.vnfSoftwareVersion,
127             "vnfProvider": vnf.vendor,
128             # "vnfProductName": vnf.producttype,
129             # "vnfConfigurableProperties": {vnf.vnfConfigurableProperties},
130             # "instantiationState": vnf.status,
131             "instantiatedVnfInfo": {
132                 "flavourId": vnf.flavour_id,
133                 "vnfState": vnf.status,
134                 "scaleStatus": [],
135                 "extCpInfo": [],
136                 "extVirtualLink": [],
137                 "monitoringParameters": {},
138                 # "localizationLanguage": vnf.localizationLanguage,
139                 "vimInfo": vim_arr,
140                 "vnfcResourceInfo": vnfc_arr,
141                 "virtualLinkResourceInfo": vl_arr,
142                 "virtualStorageResourceInfo": arr
143             },
144             # "metadata": vnf.input_params,
145             # "extensions": vnf.extension
146         }
147         return resp_data