Implement query vim info from ESR
[vfc/nfvo/lcm.git] / lcm / pub / msapi / aai.py
1 # Copyright 2017 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 lcm.pub.exceptions import NSLCMException
20 from lcm.pub.utils import restcall
21 from lcm.pub.config.config import AAI_BASE_URL, AAI_USER, AAI_PASSWD
22 from lcm.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': 'VFC-NFVO-LCM',
30         'X-TransactionId': str(uuid.uuid1())
31     }
32     return restcall.call_req(base_url=AAI_BASE_URL,
33         user=AAI_USER, 
34         passwd=AAI_PASSWD, 
35         auth_type=restcall.rest_no_auth, 
36         resource=resource, 
37         method=method, 
38         content=content,
39         additional_headers=additional_headers)
40
41 def create_ns_aai(global_customer_id, service_type, service_instance_id, data):
42     resource = "/business/customers/customer/%s/service-subscriptions/service-subscription/" \
43                "%s/service-instances/service-instance/%s" % \
44                (global_customer_id, service_type, service_instance_id)
45     ret = call_aai(resource, "PUT", data)
46     if ret[0] != 0:
47         logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
48         raise NSLCMException("Ns instance creation exception in AAI")
49     return json.JSONDecoder().decode(ret[1])
50
51 def delete_ns_aai(global_customer_id, service_type, service_instance_id, data):
52     resource = "/business/customers/customer/%s/service-subscriptions/service-subscription/" \
53                "%s/service-instances/service-instance/%s" % \
54                (global_customer_id, service_type, service_instance_id)
55     ret = call_aai(resource, "DELETE", data)
56     if ret[0] != 0:
57         logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
58         raise NSLCMException("Ns instance delete exception in AAI")
59     return json.JSONDecoder().decode(ret[1])
60
61 def query_ns_aai(global_customer_id, service_type, service_instance_id, data):
62     resource = "/business/customers/customer/%s/service-subscriptions/service-subscription/" \
63                "%s/service-instances/service-instance/%s" % \
64                (global_customer_id, service_type, service_instance_id)
65     ret = call_aai(resource, "GET", data)
66     if ret[0] != 0:
67         logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
68         raise NSLCMException("Ns instance query exception in AAI")
69     return json.JSONDecoder().decode(ret[1])
70
71 def create_vnf_aai(vnf_id, data):
72     resource = "/network/generic-vnfs/generic-vnf/%s" % vnf_id
73     ret = call_aai(resource, "PUT", data)
74     if ret[0] != 0:
75         logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
76         raise NSLCMException("Vnf instance creation exception in AAI")
77     return json.JSONDecoder().decode(ret[1])
78
79 def delete_vnf_aai(vnf_id, data=''):
80     resource = "/network/generic-vnfs/generic-vnf/%s" % vnf_id
81     ret = call_aai(resource, "DELETE", data)
82     if ret[0] != 0:
83         logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
84         raise NSLCMException("Vnf instance delete exception in AAI")
85     return json.JSONDecoder().decode(ret[1])
86
87 def query_vnf_aai(vnf_id, data):
88     resource = "/network/generic-vnfs/generic-vnf/%s" % vnf_id
89     ret = call_aai(resource, "GET", data)
90     if ret[0] != 0:
91         logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
92         raise NSLCMException("Vnf instance query exception in AAI")
93     return json.JSONDecoder().decode(ret[1])
94
95 def create_vserver_aai(cloud_owner, cloud_region_id, tenant_id, vserver_id, data):
96     resource = "/cloud-infrastructure/cloud-regions/cloud-region/%s/" \
97                "%s/tenants/tenant/%s/vservers/vserver/%s" % \
98                (cloud_owner, cloud_region_id, tenant_id, vserver_id)
99     ret = call_aai(resource, "PUT", data)
100     if ret[0] != 0:
101         logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
102         raise NSLCMException("Vserver creation exception in AAI")
103     return json.JSONDecoder().decode(ret[1])
104
105 def delete_vserver_aai(cloud_owner, cloud_region_id, tenant_id, vserver_id, data):
106     resource = "/cloud-infrastructure/cloud-regions/cloud-region/%s/" \
107                "%s/tenants/tenant/%s/vservers/vserver/%s" % \
108                (cloud_owner, cloud_region_id, tenant_id, vserver_id)
109     ret = call_aai(resource, "DELETE", data)
110     if ret[0] != 0:
111         logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
112         raise NSLCMException("Vserver delete exception in AAI")
113     return json.JSONDecoder().decode(ret[1])
114
115 def query_vserver_aai(cloud_owner, cloud_region_id, tenant_id, vserver_id, data):
116     resource = "/cloud-infrastructure/cloud-regions/cloud-region/%s/" \
117                "%s/tenants/tenant/%s/vservers/vserver/%s" % \
118                (cloud_owner, cloud_region_id, tenant_id, vserver_id)
119     ret = call_aai(resource, "GET", data)
120     if ret[0] != 0:
121         logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
122         raise NSLCMException("Vserver query exception in AAI")
123     return json.JSONDecoder().decode(ret[1])
124
125 def put_vserver_relationship(cloud_owner, cloud_region_id, tenant_id, vserver_id, data):
126     resource = "/cloud-infrastructure/cloud-regions/cloud-region/%s/" \
127                "%s/tenants/tenant/%s/vservers/vserver/%s/relationship-list/relationship" % \
128                (cloud_owner, cloud_region_id, tenant_id, vserver_id)
129     ret = call_aai(resource, "PUT", data)
130     if ret[0] != 0:
131         logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
132         raise NSLCMException("Put or update vserver relationship exception in AAI")
133     return json.JSONDecoder().decode(ret[1])
134
135
136 def delete_vserver_relationship(cloud_owner, cloud_region_id, tenant_id, vserver_id):
137     resource = "/cloud-infrastructure/cloud-regions/cloud-region/%s/" \
138                "%s/tenants/tenant/%s/vservers/vserver/%s/relationship-list/relationship" % \
139                (cloud_owner, cloud_region_id, tenant_id, vserver_id)
140     ret = call_aai(resource, "DELETE")
141     if ret[0] != 0:
142         logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
143         raise NSLCMException("Delete vserver relationship exception in AAI")
144     return json.JSONDecoder().decode(ret[1])
145
146
147 def put_vnf_relationship(vnf_id, data):
148     resource = "/network/generic-vnfs/generic-vnf/%s/relationship-list/relationship" % vnf_id
149     ret = call_aai(resource, "PUT", data)
150     if ret[0] != 0:
151         logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
152         raise NSLCMException("Put or update vnf instance relationship exception in AAI")
153     return json.JSONDecoder().decode(ret[1])
154
155
156 def delete_vnf_relationship(vnf_id):
157     resource = "/network/generic-vnfs/generic-vnf/%s/relationship-list/relationship" % vnf_id
158     ret = call_aai(resource, "DELETE")
159     if ret[0] != 0:
160         logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
161         raise NSLCMException("Delete vnf instance relationship exception in AAI")
162     return json.JSONDecoder().decode(ret[1])
163
164
165 def put_ns_relationship(global_customer_id, service_type, service_instance_id, data):
166     resource = "/business/customers/customer/%s/service-subscriptions/service-subscription/" \
167                "%s/service-instances/service-instance/%s/relationship-list/relationship" % \
168                (global_customer_id, service_type, service_instance_id)
169     ret = call_aai(resource, "PUT", data)
170     if ret[0] != 0:
171         logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
172         raise NSLCMException("Put or update ns instance relationship exception in AAI")
173     return json.JSONDecoder().decode(ret[1])
174
175
176 def delete_ns_relationship(global_customer_id, service_type, service_instance_id):
177     resource = "/business/customers/customer/%s/service-subscriptions/service-subscription/" \
178                "%s/service-instances/service-instance/%s/relationship-list/relationship" % \
179                (global_customer_id, service_type, service_instance_id)
180     ret = call_aai(resource, "DELETE")
181     if ret[0] != 0:
182         logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
183         raise NSLCMException("Delete ns instance relationship exception in AAI")
184     return json.JSONDecoder().decode(ret[1])
185
186
187 def get_vnfm_by_id(vnfm_inst_id):
188     uri = '/external-system/esr-vnfm-list/esr-vnfm/%s' % vnfm_inst_id
189     ret = call_aai(uri, "GET")
190     if ret[0] > 0:
191         logger.error('Send get VNFM information request to extsys failed.')
192         raise NSLCMException('Send get VNFM information request to extsys failed.')
193
194     # convert vnfm_info_aai to internal vnfm_info
195     vnfm_info_aai = json.JSONDecoder().decode(ret[1])
196     vnfm_info = convert_vnfm_info(vnfm_info_aai)
197     return vnfm_info
198
199 def convert_vnfm_info(vnfm_info_aai):
200     esr_system_info = ignore_case_get(vnfm_info_aai, "esr-system-info")
201     vnfm_info = {
202         "vnfmId": vnfm_info_aai["vnfm-id"],
203         "name": vnfm_info_aai["vnfm-id"],
204         "type": ignore_case_get(esr_system_info, "type"),
205         "vimId": vnfm_info_aai["vim-id"],
206         "vendor": ignore_case_get(esr_system_info, "vendor"),
207         "version": ignore_case_get(esr_system_info, "version"),
208         "description": "vnfm",
209         "certificateUrl": vnfm_info_aai["certificate-url"],
210         "url": ignore_case_get(esr_system_info, "service-url"),
211         "userName": ignore_case_get(esr_system_info, "service-url"),
212         "password": ignore_case_get(esr_system_info, "service-url"),
213         "createTime": "2016-07-06 15:33:18"
214     }
215     return vnfm_info
216
217
218 def select_vnfm(vnfm_type, vim_id):
219     uri = '/external-system/esr-vnfm-list'
220     ret = call_aai(uri, "GET")
221     if ret[0] > 0:
222         logger.error("Failed to call %s: %s", uri, ret[1])
223         raise NSLCMException('Failed to get vnfms from extsys.')
224     vnfms = json.JSONDecoder().decode(ret[1])
225     for vnfm in vnfms:
226         esr_system_info = ignore_case_get(vnfm, "esr-system-info")
227         type = ignore_case_get(esr_system_info, "type")
228         vimId = vnfm["vnfm-id"]
229         if type == vnfm_type and vimId == vim_id:
230             # convert vnfm_info_aai to internal vnfm_info
231             vnfm = convert_vnfm_info(vnfm)
232             return vnfm
233     raise NSLCMException('No vnfm found with %s in vim(%s)' % (vnfm_type, vim_id))
234
235
236 def get_vim_by_id(vim_id):
237     cloud_owner, cloud_region = split_vim_to_owner_region(vim_id)
238     ret = call_aai("/cloud-infrastructure/cloud-regions/cloud-region/%s/%s" % (cloud_owner, cloud_region), "GET")
239     if ret[0] != 0:
240         logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
241         raise NSLCMException("Failed to query vim(%s) from extsys." % vim_id)
242
243     # convert vim_info_aai to internal vim_info
244     vim_info_aai = json.JSONDecoder().decode(ret[1])
245     vim_info = convert_vim_info(vim_info_aai)
246     return vim_info
247
248 def split_vim_to_owner_region(vim_id):
249     split_vim = vim_id.split('_')
250     cloud_owner = split_vim[0]
251     cloud_region = "".join(split_vim[1:])
252     return cloud_owner, cloud_region
253
254 def convert_vim_info(vim_info_aai):
255     vim_id = vim_info_aai["cloud-owner"] + '_' + vim_info_aai["cloud-region-id"]
256     esr_system_info = ignore_case_get(vim_info_aai, "esr-system-info")
257     tenants = ignore_case_get(vim_info_aai, "tenants")
258     vim_info = {
259         "vimId": vim_id,
260         "name": vim_id,
261         "url": ignore_case_get(esr_system_info, "service-url"),
262         "userName": ignore_case_get(esr_system_info, "service-url"),
263         "password": ignore_case_get(esr_system_info, "service-url"),
264         "tenant": ignore_case_get(tenants[0], "tenant-id"),
265         "vendor": ignore_case_get(esr_system_info, "vendor"),
266         "version": ignore_case_get(esr_system_info, "version"),
267         "description": "vim",
268         "domain": "",
269         "type": "openstack",
270         "createTime": "2016-07-18 12:22:53"
271     }
272     return vim_info
273
274
275 def get_vims():
276     ret = call_aai("/cloud-infrastructure/cloud-regions", "GET")
277     if ret[0] != 0:
278         logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
279         raise NSLCMException("Failed to query vims from extsys.")
280
281     # convert vim_info_aai to internal vim_info
282     vims_aai = json.JSONDecoder().decode(ret[1])
283     vims_info = []
284     for vim in vims_aai:
285         vim = convert_vim_info(vim)
286         vims_info.append(vim)
287
288     return vims_info