genericparser seed code
[modeling/etsicatalog.git] / genericparser / 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 import uuid
18
19 from genericparser.pub.config.config import AAI_BASE_URL, AAI_USER, AAI_PASSWD
20 from genericparser.pub.exceptions import GenericparserException
21 from genericparser.pub.utils import restcall
22 from genericparser.pub.utils.values import ignore_case_get
23
24 logger = logging.getLogger(__name__)
25
26
27 def call_aai(resource, method, content=''):
28     additional_headers = {
29         'X-FromAppId': 'MODEL-GENERICPARSER',
30         'X-TransactionId': str(uuid.uuid1())
31     }
32     return restcall.call_req(AAI_BASE_URL,
33                              AAI_USER,
34                              AAI_PASSWD,
35                              restcall.rest_no_auth,
36                              resource,
37                              method,
38                              content,
39                              additional_headers)
40
41
42 def get_vims():
43     ret = call_aai("/cloud-infrastructure/cloud-regions?depth=all", "GET")
44     if ret[0] != 0:
45         logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
46         raise GenericparserException("Failed to query vims from extsys.")
47     # convert vim_info_aai to internal vim_info
48     vims_aai = json.JSONDecoder().decode(ret[1])
49     vims_aai = ignore_case_get(vims_aai, "cloud-region")
50     vims_info = []
51     for vim in vims_aai:
52         vim = convert_vim_info(vim)
53         vims_info.append(vim)
54     return vims_info
55
56
57 def get_vim_by_id(vim_id):
58     cloud_owner, cloud_region = split_vim_to_owner_region(vim_id)
59     ret = call_aai("/cloud-infrastructure/cloud-regions/cloud-region/%s/%s?depth=all"
60                    % (cloud_owner, cloud_region), "GET")
61     if ret[0] != 0:
62         logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
63         raise GenericparserException("Failed to query vim(%s) from extsys." % vim_id)
64     # convert vim_info_aai to internal vim_info
65     vim_info_aai = json.JSONDecoder().decode(ret[1])
66     vim_info = convert_vim_info(vim_info_aai)
67     return vim_info
68
69
70 def split_vim_to_owner_region(vim_id):
71     split_vim = vim_id.split('_')
72     cloud_owner = split_vim[0]
73     cloud_region = "".join(split_vim[1:])
74     return cloud_owner, cloud_region
75
76
77 def convert_vim_info(vim_info_aai):
78     vim_id = vim_info_aai["cloud-owner"] + "_" + vim_info_aai["cloud-region-id"]
79     esr_system_info = ignore_case_get(ignore_case_get(vim_info_aai, "esr-system-info-list"), "esr-system-info")
80     vim_info = {
81         "vimId": vim_id,
82         "name": vim_id,
83         "url": ignore_case_get(esr_system_info[0], "service-url"),
84         "userName": ignore_case_get(esr_system_info[0], "user-name"),
85         "password": ignore_case_get(esr_system_info[0], "password"),
86         "tenant": ignore_case_get(esr_system_info[0], "default-tenant"),
87         "vendor": ignore_case_get(esr_system_info[0], "vendor"),
88         "version": ignore_case_get(esr_system_info[0], "version"),
89         "description": "vim",
90         "domain": "",
91         "type": ignore_case_get(esr_system_info[0], "type"),
92         "createTime": "2016-07-18 12:22:53"
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 GenericparserException("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     return sdnc_info
107
108
109 def convert_sdnc_info(sdnc_info_aai):
110     esr_system_info = ignore_case_get(ignore_case_get(sdnc_info_aai, "esr-system-info-list"), "esr-system-info")
111     sdnc_info = {
112         "sdnControllerId": sdnc_info_aai["thirdparty-sdnc-id"],
113         "name": sdnc_info_aai["thirdparty-sdnc-id"],
114         "url": ignore_case_get(esr_system_info[0], "service-url"),
115         "userName": ignore_case_get(esr_system_info[0], "user-name"),
116         "password": ignore_case_get(esr_system_info[0], "password"),
117         "vendor": ignore_case_get(esr_system_info[0], "vendor"),
118         "version": ignore_case_get(esr_system_info[0], "version"),
119         "description": "",
120         "protocol": ignore_case_get(esr_system_info[0], "protocal"),
121         "productName": ignore_case_get(sdnc_info_aai, "product-name"),
122         "type": ignore_case_get(esr_system_info[0], "type"),
123         "createTime": "2016-07-18 12:22:53"
124     }
125     return sdnc_info
126
127
128 def get_vnfm_by_id(vnfm_inst_id):
129     uri = "/external-system/esr-vnfm-list/esr-vnfm/%s?depth=all" % vnfm_inst_id
130     ret = call_aai(uri, "GET")
131     if ret[0] > 0:
132         logger.error('Send get VNFM information request to extsys failed.')
133         raise GenericparserException('Send get VNFM information request to extsys failed.')
134     # convert vnfm_info_aai to internal vnfm_info
135     vnfm_info_aai = json.JSONDecoder().decode(ret[1])
136     vnfm_info = convert_vnfm_info(vnfm_info_aai)
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": "2016-07-06 15:33:18"
155     }
156     return vnfm_info
157
158
159 def select_vnfm(vnfm_type, vim_id):
160     uri = "/external-system/esr-vnfm-list?depth=all"
161     ret = call_aai(uri, "GET")
162     if ret[0] > 0:
163         logger.error("Failed to call %s: %s", uri, ret[1])
164         raise GenericparserException('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         esr_system_info = ignore_case_get(vnfm, "esr-system-info")
169         type = ignore_case_get(esr_system_info, "type")
170         vimId = vnfm["vnfm-id"]
171         if type == vnfm_type and vimId == vim_id:
172             # convert vnfm_info_aai to internal vnfm_info
173             vnfm = convert_vnfm_info(vnfm)
174             return vnfm
175     raise GenericparserException('No vnfm found with %s in vim(%s)' % (vnfm_type, vim_id))