Add code and testcase of query single vnf
[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         pass
38
39     def fill_resp_data(self, vnf):
40         logger.info('Get the list of vloumes')
41         storage_inst = StorageInstModel.objects.filter(instid=vnf.nfinstid)
42         arr = []
43         for s in storage_inst:
44             storage = {
45                 "virtualStorageInstanceId": s.storageid,
46                 "storageResource": {
47                     "vimId": s.vimid,
48                     "resourceId": s.resouceid
49                 }
50             }
51             arr.append(storage)
52         logger.info('Get the VLInstModel of list.')
53         vl_inst = VLInstModel.objects.filter(ownerid=vnf.nfinstid)
54         vl_arr = []
55         for v in vl_inst:
56             net = NetworkInstModel.objects.filter(networkid=v.relatednetworkid)
57             if not net:
58                 raise NFLCMException('NetworkInst(%s) does not exist.' % v.relatednetworkid)
59             v_dic = {
60                 "virtualLinkInstanceId": v.vlinstanceid,
61                 "virtualLinkDescId": v.vldid,
62                 "networkResource": {
63                     "vimId": net[0].vimid,
64                     "resourceId": net[0].resouceid
65                 }
66             }
67             vl_arr.append(v_dic)
68         logger.info('Get VNFCInstModel of list.')
69         vnfc_insts = VNFCInstModel.objects.filter(nfinstid=vnf.nfinstid)
70         vnfc_arr = []
71         for vnfc in vnfc_insts:
72             vm = VmInstModel.objects.filter(vmid=vnfc.vmid)
73             if not vm:
74                 raise NFLCMException('VmInst(%s) does not exist.' % vnfc.vmid)
75             storage = StorageInstModel.objects.filter(ownerid=vm[0].vmid)
76             if not storage:
77                 raise NFLCMException('StorageInst(%s) does not exist.' % vm[0].vmid)
78             vnfc_dic = {
79                 "vnfcInstanceId": vnfc.vnfcinstanceid,
80                 "vduId": vnfc.vduid,
81                 "computeResource": {
82                     "vimId": vm[0].vimid,
83                     "resourceId": vm[0].resouceid
84                 },
85                 "storageResourceIds": [s.storageid for s in storage]
86             }
87             vnfc_arr.append(vnfc_dic)
88         logger.info('Get the VimInstModel of list.')
89         vms = VmInstModel.objects.filter(instid=vnf.nfinstid)
90         vim_arr = []
91         # The 'vimInfoId' and 'vimId' each value are same
92         for vm in vms:
93             vims = VimModel.objects.filter(vimid=vm.vimid)
94             for vim in vims:
95                 vim_users = VimUserModel.objects.filter(vimid=vim.vimid)
96                 vim_dic = {
97                     "vimInfoId": vim.vimid,
98                     "vimId": vim.vimid,
99                     "interfaceInfo": {
100                         "vimType": vim.type,
101                         "apiVersion": vim.version,
102                         "protocolType": (vim.apiurl.split(':')[0] if vim.apiurl and vim.apiurl.index(':') else 'http')
103                     },
104                     "accessInfo": {
105                         "tenant": (vim_users[0].defaulttenant if vim_users and vim_users[0].defaulttenant else ''),
106                         "username": (vim_users[0].username if vim_users and vim_users[0].username else ''),
107                         "password": (vim_users[0].password if vim_users and vim_users[0].password else '')
108                     },
109                     "interfaceEndpoint": vim.apiurl
110                 }
111                 vim_arr.append(vim_dic)
112
113         resp_data = {
114             "vnfInstanceId": vnf.nfinstid,
115             "vnfInstanceName": vnf.nf_name,
116             # "vnfInstanceDescription": vnf.nf_desc,
117             "onboardedVnfPkgInfoId": vnf.package_id,
118             # "vnfdId": vnf.vnfdid,
119             "vnfdVersion": vnf.version,
120             # "vnfSoftwareVersion": vnf.vnfSoftwareVersion,
121             "vnfProvider": vnf.vendor,
122             # "vnfProductName": vnf.producttype,
123             # "vnfConfigurableProperties": {vnf.vnfConfigurableProperties},
124             # "instantiationState": vnf.status,
125             "instantiatedVnfInfo": {
126                 "flavourId": vnf.flavour_id,
127                 "vnfState": vnf.status,
128                 "scaleStatus": [],
129                 "extCpInfo": [],
130                 "extVirtualLink": [],
131                 "monitoringParameters": {},
132                 # "localizationLanguage": vnf.localizationLanguage,
133                 "vimInfo": vim_arr,
134                 "vnfcResourceInfo": vnfc_arr,
135                 "virtualLinkResourceInfo": vl_arr,
136                 "virtualStorageResourceInfo": arr
137             },
138             # "metadata": vnf.input_params,
139             # "extensions": vnf.extension
140         }
141         return resp_data