Resolve VFC code and swagger inconsistencies
[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     return vims_info
38
39
40 def get_vim_by_id(vim_id):
41     cloud_owner, cloud_region = split_vim_to_owner_region(vim_id)
42     ret = call_aai("/cloud-infrastructure/cloud-regions/cloud-region/%s/%s?depth=all"
43                    % (cloud_owner, cloud_region), "GET")
44     if ret[0] != 0:
45         logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
46         raise NSLCMException("Failed to query vim(%s) from extsys." % vim_id)
47     # convert vim_info_aai to internal vim_info
48     vim_info_aai = json.JSONDecoder().decode(ret[1])
49     vim_info = convert_vim_info(vim_info_aai)
50     logger.debug("vim_id=%s, vim_info=%s", vim_id, vim_info)
51     return vim_info
52
53
54 def split_vim_to_owner_region(vim_id):
55     split_vim = vim_id.split('_')
56     cloud_owner = split_vim[0]
57     cloud_region = "".join(split_vim[1:])
58     return cloud_owner, cloud_region
59
60
61 def convert_vim_info(vim_info_aai):
62     vim_id = vim_info_aai["cloud-owner"] + "_" + vim_info_aai["cloud-region-id"]
63     esr_system_info = ignore_case_get(ignore_case_get(vim_info_aai, "esr-system-info-list"), "esr-system-info")
64     # tenants = ignore_case_get(vim_info_aai, "tenants")
65     default_tenant = ignore_case_get(esr_system_info[0], "default-tenant")
66     tenants = ignore_case_get(ignore_case_get(vim_info_aai, "tenants"), "tenant")
67     tenant_id = ""
68     for tenant_info in tenants:
69         if tenant_info["tenant-name"] == default_tenant:
70             tenant_id = tenant_info["tenant-id"]
71             break
72     vim_info = {
73         "vimId": vim_id,
74         "name": vim_id,
75         "url": ignore_case_get(esr_system_info[0], "service-url"),
76         "userName": ignore_case_get(esr_system_info[0], "user-name"),
77         "password": ignore_case_get(esr_system_info[0], "password"),
78         # "tenant": ignore_case_get(tenants[0], "tenant-id"),
79         "tenantId": tenant_id,
80         "tenant": default_tenant,
81         "vendor": ignore_case_get(esr_system_info[0], "vendor"),
82         "version": ignore_case_get(esr_system_info[0], "version"),
83         "description": "vim",
84         "domain": "",
85         "type": ignore_case_get(esr_system_info[0], "type"),
86         "createTime": "2016-07-18 12:22:53",
87         "sslCacert": ignore_case_get(esr_system_info[0], "ssl-cacert"),
88         "sslInsecure": ignore_case_get(esr_system_info[0], "ssl-insecure"),
89         "status": ignore_case_get(esr_system_info[0], "system-status")
90     }
91     return vim_info
92
93
94 def get_sdn_controller_by_id(sdn_ontroller_id):
95     ret = call_aai("/external-system/esr-thirdparty-sdnc-list/esr-thirdparty-sdnc/%s?depth=all"
96                    % sdn_ontroller_id, "GET")
97     if ret[0] != 0:
98         logger.error("Failed to query sdn ontroller(%s) from extsys. detail is %s.", sdn_ontroller_id, ret[1])
99         raise NSLCMException("Failed to query sdn ontroller(%s) from extsys." % sdn_ontroller_id)
100     # convert vim_info_aai to internal vim_info
101     sdnc_info_aai = json.JSONDecoder().decode(ret[1])
102     sdnc_info = convert_sdnc_info(sdnc_info_aai)
103     return sdnc_info
104
105
106 def convert_sdnc_info(sdnc_info_aai):
107     esr_system_info = ignore_case_get(ignore_case_get(sdnc_info_aai, "esr-system-info-list"), "esr-system-info")
108     sdnc_info = {
109         "sdnControllerId": sdnc_info_aai["thirdparty-sdnc-id"],
110         "name": sdnc_info_aai["thirdparty-sdnc-id"],
111         "url": ignore_case_get(esr_system_info[0], "service-url"),
112         "userName": ignore_case_get(esr_system_info[0], "user-name"),
113         "password": ignore_case_get(esr_system_info[0], "password"),
114         "vendor": ignore_case_get(esr_system_info[0], "vendor"),
115         "version": ignore_case_get(esr_system_info[0], "version"),
116         "description": "",
117         "protocol": ignore_case_get(esr_system_info[0], "protocal"),
118         "productName": ignore_case_get(sdnc_info_aai, "product-name"),
119         "type": ignore_case_get(esr_system_info[0], "type"),
120         "createTime": "2016-07-18 12:22:53"
121     }
122     return sdnc_info
123
124
125 def get_vnfm_by_id(vnfm_inst_id):
126     uri = "/external-system/esr-vnfm-list/esr-vnfm/%s?depth=all" % vnfm_inst_id
127     ret = call_aai(uri, "GET")
128     if ret[0] > 0:
129         logger.error('Send get VNFM information request to extsys failed.')
130         raise NSLCMException('Send get VNFM information request to extsys failed.')
131     # convert vnfm_info_aai to internal vnfm_info
132     vnfm_info_aai = json.JSONDecoder().decode(ret[1])
133     vnfm_info = convert_vnfm_info(vnfm_info_aai)
134     return vnfm_info
135
136
137 def convert_vnfm_info(vnfm_info_aai):
138     esr_system_info = ignore_case_get(ignore_case_get(vnfm_info_aai, "esr-system-info-list"), "esr-system-info")
139     vnfm_info = {
140         "vnfmId": vnfm_info_aai["vnfm-id"],
141         "name": vnfm_info_aai["vnfm-id"],
142         "type": ignore_case_get(esr_system_info[0], "type"),
143         "vimId": vnfm_info_aai["vim-id"],
144         "vendor": ignore_case_get(esr_system_info[0], "vendor"),
145         "version": ignore_case_get(esr_system_info[0], "version"),
146         "description": "vnfm",
147         "certificateUrl": vnfm_info_aai["certificate-url"],
148         "url": ignore_case_get(esr_system_info[0], "service-url"),
149         "userName": ignore_case_get(esr_system_info[0], "user-name"),
150         "password": ignore_case_get(esr_system_info[0], "password"),
151         "createTime": "2016-07-06 15:33:18"
152     }
153     return vnfm_info
154
155
156 def select_vnfm(vnfm_type, vim_id):
157     uri = "/external-system/esr-vnfm-list"
158     ret = call_aai(uri, "GET")
159     if ret[0] > 0:
160         logger.error("Failed to call %s: %s", uri, ret[1])
161         raise NSLCMException('Failed to get vnfms from extsys.')
162     vnfms = json.JSONDecoder().decode(ret[1])
163     vnfms = ignore_case_get(vnfms, "esr-vnfm")
164     for vnfm in vnfms:
165         vnfm_info = get_vnfm_by_id(vnfm.get("vnfm-id"))
166         vnfmtype = ignore_case_get(vnfm_info, "type")
167         vimid = ignore_case_get(vnfm_info, "vimId")
168         if vnfmtype == vnfm_type and vimid == vim_id:
169             return vnfm_info
170     raise NSLCMException('No vnfm found with %s in vim(%s)' % (vnfm_type, vim_id))