Add query vim swagger generate logic
[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     esr_system_info = ignore_case_get(ignore_case_get(vim_info_aai, "esr-system-info-list"), "esr-system-info")
65     # tenants = ignore_case_get(vim_info_aai, "tenants")
66     default_tenant = ignore_case_get(esr_system_info[0], "default-tenant")
67     tenants = ignore_case_get(ignore_case_get(vim_info_aai, "tenants"), "tenant")
68     tenant_id = ""
69     for tenant_info in tenants:
70         if tenant_info["tenant-name"] == default_tenant:
71             tenant_id = tenant_info["tenant-id"]
72             break
73     vim_info = {
74         "vimId": vim_id,
75         "name": vim_id,
76         "url": ignore_case_get(esr_system_info[0], "service-url"),
77         "userName": ignore_case_get(esr_system_info[0], "user-name"),
78         "password": ignore_case_get(esr_system_info[0], "password"),
79         # "tenant": ignore_case_get(tenants[0], "tenant-id"),
80         "tenantId": tenant_id,
81         "tenant": default_tenant,
82         "vendor": ignore_case_get(esr_system_info[0], "vendor"),
83         "version": ignore_case_get(esr_system_info[0], "version"),
84         "description": "vim",
85         "domain": "",
86         "type": ignore_case_get(esr_system_info[0], "type"),
87         "createTime": "",
88         "sslCacert": ignore_case_get(esr_system_info[0], "ssl-cacert"),
89         "sslInsecure": str(ignore_case_get(esr_system_info[0], "ssl-insecure")),
90         "status": ignore_case_get(esr_system_info[0], "system-status")
91     }
92     return vim_info
93
94
95 def get_sdn_controller_by_id(sdn_ontroller_id):
96     ret = call_aai("/external-system/esr-thirdparty-sdnc-list/esr-thirdparty-sdnc/%s?depth=all"
97                    % sdn_ontroller_id, "GET")
98     if ret[0] != 0:
99         logger.error("Failed to query sdn ontroller(%s) from extsys. detail is %s.", sdn_ontroller_id, ret[1])
100         raise NSLCMException("Failed to query sdn ontroller(%s) from extsys." % sdn_ontroller_id)
101     # convert vim_info_aai to internal vim_info
102     sdnc_info_aai = json.JSONDecoder().decode(ret[1])
103     sdnc_info = convert_sdnc_info(sdnc_info_aai)
104     logger.debug("sdn_ontroller_id=%s, sdnc_info=%s", sdn_ontroller_id, sdnc_info)
105     return sdnc_info
106
107
108 def convert_sdnc_info(sdnc_info_aai):
109     esr_system_info = ignore_case_get(ignore_case_get(sdnc_info_aai, "esr-system-info-list"), "esr-system-info")
110     sdnc_info = {
111         "sdnControllerId": sdnc_info_aai["thirdparty-sdnc-id"],
112         "name": sdnc_info_aai["thirdparty-sdnc-id"],
113         "url": ignore_case_get(esr_system_info[0], "service-url"),
114         "userName": ignore_case_get(esr_system_info[0], "user-name"),
115         "password": ignore_case_get(esr_system_info[0], "password"),
116         "vendor": ignore_case_get(esr_system_info[0], "vendor"),
117         "version": ignore_case_get(esr_system_info[0], "version"),
118         "description": "",
119         "protocol": ignore_case_get(esr_system_info[0], "protocal"),
120         "productName": ignore_case_get(sdnc_info_aai, "product-name"),
121         "type": ignore_case_get(esr_system_info[0], "type"),
122         "createTime": ""
123     }
124     return sdnc_info
125
126
127 def get_vnfm_by_id(vnfm_inst_id):
128     uri = "/external-system/esr-vnfm-list/esr-vnfm/%s?depth=all" % vnfm_inst_id
129     ret = call_aai(uri, "GET")
130     if ret[0] > 0:
131         logger.error('Send get VNFM information request to extsys failed.')
132         raise NSLCMException('Send get VNFM information request to extsys failed.')
133     # convert vnfm_info_aai to internal vnfm_info
134     vnfm_info_aai = json.JSONDecoder().decode(ret[1])
135     vnfm_info = convert_vnfm_info(vnfm_info_aai)
136     logger.debug("vnfm_inst_id=%s, vnfm_info=%s", vnfm_inst_id, vnfm_info)
137     return vnfm_info
138
139
140 def convert_vnfm_info(vnfm_info_aai):
141     esr_system_info = ignore_case_get(ignore_case_get(vnfm_info_aai, "esr-system-info-list"), "esr-system-info")
142     vnfm_info = {
143         "vnfmId": vnfm_info_aai["vnfm-id"],
144         "name": vnfm_info_aai["vnfm-id"],
145         "type": ignore_case_get(esr_system_info[0], "type"),
146         "vimId": vnfm_info_aai["vim-id"],
147         "vendor": ignore_case_get(esr_system_info[0], "vendor"),
148         "version": ignore_case_get(esr_system_info[0], "version"),
149         "description": "vnfm",
150         "certificateUrl": vnfm_info_aai["certificate-url"],
151         "url": ignore_case_get(esr_system_info[0], "service-url"),
152         "userName": ignore_case_get(esr_system_info[0], "user-name"),
153         "password": ignore_case_get(esr_system_info[0], "password"),
154         "createTime": ""
155     }
156     return vnfm_info
157
158
159 def select_vnfm(vnfm_type, vim_id):
160     uri = "/external-system/esr-vnfm-list"
161     ret = call_aai(uri, "GET")
162     if ret[0] > 0:
163         logger.error("Failed to call %s: %s", uri, ret[1])
164         raise NSLCMException('Failed to get vnfms from extsys.')
165     vnfms = json.JSONDecoder().decode(ret[1])
166     vnfms = ignore_case_get(vnfms, "esr-vnfm")
167     for vnfm in vnfms:
168         vnfm_info = get_vnfm_by_id(vnfm.get("vnfm-id"))
169         vnfmtype = ignore_case_get(vnfm_info, "type")
170         vimid = ignore_case_get(vnfm_info, "vimId")
171         if vnfmtype == vnfm_type and vimid == vim_id:
172             return vnfm_info
173     raise NSLCMException('No vnfm found with %s in vim(%s)' % (vnfm_type, vim_id))