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