1 # Copyright 2017 ZTE Corporation.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
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
24 logger = logging.getLogger(__name__)
27 def call_aai(resource, method, content=''):
28 additional_headers = {
29 'X-FromAppId': 'VFC-NFVO-LCM',
30 'X-TransactionId': str(uuid.uuid1())
32 return restcall.call_req(base_url=AAI_BASE_URL,
35 auth_type=restcall.rest_no_auth,
39 additional_headers=additional_headers)
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)
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])
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)
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])
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)
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])
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)
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])
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)
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])
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)
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])
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)
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])
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)
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])
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)
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])
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)
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])
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")
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])
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)
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])
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")
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])
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)
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])
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")
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])
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")
191 logger.error('Send get VNFM information request to extsys failed.')
192 raise NSLCMException('Send get VNFM information request to extsys failed.')
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)
199 def convert_vnfm_info(vnfm_info_aai):
200 esr_system_info = ignore_case_get(vnfm_info_aai, "esr-system-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"
218 def select_vnfm(vnfm_type, vim_id):
219 uri = '/external-system/esr-vnfm-list'
220 ret = call_aai(uri, "GET")
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])
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)
233 raise NSLCMException('No vnfm found with %s in vim(%s)' % (vnfm_type, vim_id))
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")
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)
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)
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
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")
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",
270 "createTime": "2016-07-18 12:22:53"
276 ret = call_aai("/cloud-infrastructure/cloud-regions", "GET")
278 logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
279 raise NSLCMException("Failed to query vims from extsys.")
281 # convert vim_info_aai to internal vim_info
282 vims_aai = json.JSONDecoder().decode(ret[1])
285 vim = convert_vim_info(vim)
286 vims_info.append(vim)