019f0825f20117c472b3bab517dc3bb719e4aaaa
[vfc/nfvo/lcm.git] / lcm / pub / msapi / extsys.py
1 # Copyright 2016 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 json
16 import logging
17
18 from lcm.pub.exceptions import NSLCMException
19 from lcm.pub.msapi.aai import call_aai
20 from lcm.pub.utils.values import ignore_case_get
21
22 logger = logging.getLogger(__name__)
23
24
25 def get_vims():
26     ret = call_aai("/cloud-infrastructure/cloud-regions?depth=all", "GET")
27     if ret[0] != 0:
28         logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
29         raise NSLCMException("Failed to query vims from extsys.")
30     # convert vim_info_aai to internal vim_info
31     vims_aai = json.JSONDecoder().decode(ret[1])
32     vims_aai = ignore_case_get(vims_aai, "cloud-region")
33     vims_info = []
34     for vim in vims_aai:
35         vim = convert_vim_info(vim)
36         vims_info.append(vim)
37     logger.debug("vims_info=%s", vims_info)
38     return vims_info
39
40
41 def get_vim_by_id(vim_id):
42     cloud_owner, cloud_region = split_vim_to_owner_region(vim_id)
43     ret = call_aai("/cloud-infrastructure/cloud-regions/cloud-region/%s/%s?depth=all"
44                    % (cloud_owner, cloud_region), "GET")
45     if ret[0] != 0:
46         logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
47         raise NSLCMException("Failed to query vim(%s) from extsys." % vim_id)
48     # convert vim_info_aai to internal vim_info
49     vim_info_aai = json.JSONDecoder().decode(ret[1])
50     vim_info = convert_vim_info(vim_info_aai)
51     logger.debug("vim_id=%s, vim_info=%s", vim_id, vim_info)
52     return vim_info
53
54
55 def split_vim_to_owner_region(vim_id):
56     split_vim = vim_id.split('_')
57     cloud_owner = split_vim[0]
58     cloud_region = "".join(split_vim[1:])
59     return cloud_owner, cloud_region
60
61
62 def convert_vim_info(vim_info_aai):
63     vim_id = vim_info_aai["cloud-owner"] + "_" + vim_info_aai["cloud-region-id"]
64     vim_type_aai = vim_info_aai["cloud-type"]
65     vim_type = vim_type_aai if vim_type_aai else "openstack"
66     esr_system_info = ignore_case_get(ignore_case_get(vim_info_aai, "esr-system-info-list"), "esr-system-info")
67     # tenants = ignore_case_get(vim_info_aai, "tenants")
68     default_tenant = ignore_case_get(esr_system_info[0], "default-tenant")
69     tenants = ignore_case_get(ignore_case_get(vim_info_aai, "tenants"), "tenant")
70     tenant_id = ""
71     for tenant_info in tenants:
72         if tenant_info["tenant-name"] == default_tenant:
73             tenant_id = tenant_info["tenant-id"]
74             break
75     vim_info = {
76         "vimId": vim_id,
77         "name": vim_id,
78         "url": ignore_case_get(esr_system_info[0], "service-url"),
79         "userName": ignore_case_get(esr_system_info[0], "user-name"),
80         "password": ignore_case_get(esr_system_info[0], "password"),
81         # "tenant": ignore_case_get(tenants[0], "tenant-id"),
82         "tenantId": tenant_id,
83         "tenant": default_tenant,
84         "vendor": ignore_case_get(esr_system_info[0], "vendor"),
85         "version": ignore_case_get(esr_system_info[0], "version"),
86         "description": "vim",
87         "domain": ignore_case_get(esr_system_info[0], "cloud-domain"),
88         "type": vim_type,
89         "createTime": "",
90         "sslCacert": ignore_case_get(esr_system_info[0], "ssl-cacert"),
91         "sslInsecure": str(ignore_case_get(esr_system_info[0], "ssl-insecure")),
92         "status": ignore_case_get(esr_system_info[0], "system-status")
93     }
94     return vim_info
95
96
97 def get_sdn_controller_by_id(sdn_ontroller_id):
98     ret = call_aai("/external-system/esr-thirdparty-sdnc-list/esr-thirdparty-sdnc/%s?depth=all"
99                    % sdn_ontroller_id, "GET")
100     if ret[0] != 0:
101         logger.error("Failed to query sdn ontroller(%s) from extsys. detail is %s.", sdn_ontroller_id, ret[1])
102         raise NSLCMException("Failed to query sdn ontroller(%s) from extsys." % sdn_ontroller_id)
103     # convert vim_info_aai to internal vim_info
104     sdnc_info_aai = json.JSONDecoder().decode(ret[1])
105     sdnc_info = convert_sdnc_info(sdnc_info_aai)
106     logger.debug("sdn_ontroller_id=%s, sdnc_info=%s", sdn_ontroller_id, sdnc_info)
107     return sdnc_info
108
109
110 def convert_sdnc_info(sdnc_info_aai):
111     esr_system_info = ignore_case_get(ignore_case_get(sdnc_info_aai, "esr-system-info-list"), "esr-system-info")
112     sdnc_info = {
113         "sdnControllerId": sdnc_info_aai["thirdparty-sdnc-id"],
114         "name": sdnc_info_aai["thirdparty-sdnc-id"],
115         "url": ignore_case_get(esr_system_info[0], "service-url"),
116         "userName": ignore_case_get(esr_system_info[0], "user-name"),
117         "password": ignore_case_get(esr_system_info[0], "password"),
118         "vendor": ignore_case_get(esr_system_info[0], "vendor"),
119         "version": ignore_case_get(esr_system_info[0], "version"),
120         "description": "",
121         "protocol": ignore_case_get(esr_system_info[0], "protocal"),
122         "productName": ignore_case_get(sdnc_info_aai, "product-name"),
123         "type": ignore_case_get(esr_system_info[0], "type"),
124         "createTime": ""
125     }
126     return sdnc_info
127
128
129 def get_vnfm_by_id(vnfm_inst_id):
130     uri = "/external-system/esr-vnfm-list/esr-vnfm/%s?depth=all" % vnfm_inst_id
131     ret = call_aai(uri, "GET")
132     if ret[0] > 0:
133         logger.error('Send get VNFM information request to extsys failed.')
134         raise NSLCMException('Send get VNFM information request to extsys failed.')
135     # convert vnfm_info_aai to internal vnfm_info
136     vnfm_info_aai = json.JSONDecoder().decode(ret[1])
137     vnfm_info = convert_vnfm_info(vnfm_info_aai)
138     logger.debug("vnfm_inst_id=%s, vnfm_info=%s", vnfm_inst_id, vnfm_info)
139     return vnfm_info
140
141
142 def convert_vnfm_info(vnfm_info_aai):
143     esr_system_info = ignore_case_get(ignore_case_get(vnfm_info_aai, "esr-system-info-list"), "esr-system-info")
144     vnfm_info = {
145         "vnfmId": vnfm_info_aai["vnfm-id"],
146         "name": vnfm_info_aai["vnfm-id"],
147         "type": ignore_case_get(esr_system_info[0], "type"),
148         "vimId": vnfm_info_aai["vim-id"],
149         "vendor": ignore_case_get(esr_system_info[0], "vendor"),
150         "version": ignore_case_get(esr_system_info[0], "version"),
151         "description": "vnfm",
152         "certificateUrl": vnfm_info_aai["certificate-url"],
153         "url": ignore_case_get(esr_system_info[0], "service-url"),
154         "userName": ignore_case_get(esr_system_info[0], "user-name"),
155         "password": ignore_case_get(esr_system_info[0], "password"),
156         "createTime": ""
157     }
158     return vnfm_info
159
160
161 def select_vnfm(vnfm_type, vim_id):
162     uri = "/external-system/esr-vnfm-list"
163     ret = call_aai(uri, "GET")
164     if ret[0] > 0:
165         logger.error("Failed to call %s: %s", uri, ret[1])
166         raise NSLCMException('Failed to get vnfms from extsys.')
167     vnfms = json.JSONDecoder().decode(ret[1])
168     vnfms = ignore_case_get(vnfms, "esr-vnfm")
169     for vnfm in vnfms:
170         vnfm_info = get_vnfm_by_id(vnfm.get("vnfm-id"))
171         vnfmtype = ignore_case_get(vnfm_info, "type")
172         vimid = ignore_case_get(vnfm_info, "vimId")
173         if vnfmtype == vnfm_type and vimid == vim_id:
174             return vnfm_info
175     raise NSLCMException('No vnfm found with %s in vim(%s)' % (vnfm_type, vim_id))
176
177
178 def get_ems_by_id(ems_inst_id):
179     uri = "/external-system/esr-ems-list/esr-ems/%s?depth=all" % ems_inst_id
180     ret = call_aai(uri, "GET")
181     if ret[0] > 0:
182         logger.error('Send get EMS information request to extsys failed.')
183         raise NSLCMException('Send get EMS information request to extsys failed.')
184     # convert vnfm_info_aai to internal vnfm_info
185     ems_info_aai = json.JSONDecoder().decode(ret[1])
186     ems_info = convert_ems_info(ems_info_aai)
187     logger.debug("ems_inst_id=%s, ems_info=%s", ems_inst_id, ems_info)
188     return ems_info
189
190
191 def convert_ems_info(ems_info_aai):
192     esr_system_info = ignore_case_get(ignore_case_get(ems_info_aai, "esr-system-info-list"), "esr-system-info")
193     ems_info_aai = {
194         "emsId": ems_info_aai["ems-id"],
195         "type": ignore_case_get(esr_system_info[0], "type"),
196         "vendor": ignore_case_get(esr_system_info[0], "vendor"),
197         "version": ignore_case_get(esr_system_info[0], "version"),
198         "url": ignore_case_get(esr_system_info[0], "service-url"),
199         "userName": ignore_case_get(esr_system_info[0], "user-name"),
200         "password": ignore_case_get(esr_system_info[0], "password"),
201         "createTime": ""
202     }
203     return ems_info_aai