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 resource = resource + "?depth=all"
33 return restcall.call_req(base_url=AAI_BASE_URL,
40 additional_headers=additional_headers)
43 def create_ns_aai(global_customer_id, service_type, service_instance_id, data):
44 resource = "/business/customers/customer/%s/service-subscriptions/service-subscription/" \
45 "%s/service-instances/service-instance/%s" % \
46 (global_customer_id, service_type, service_instance_id)
47 ret = call_aai(resource, "PUT", data)
49 logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
50 raise NSLCMException("Ns instance creation exception in AAI")
51 return json.JSONDecoder().decode(ret[1])
53 def delete_ns_aai(global_customer_id, service_type, service_instance_id, data=''):
54 resource = "/business/customers/customer/%s/service-subscriptions/service-subscription/" \
55 "%s/service-instances/service-instance/%s" % \
56 (global_customer_id, service_type, service_instance_id)
57 ret = call_aai(resource, "DELETE", data)
59 logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
60 raise NSLCMException("Ns instance delete exception in AAI")
61 return json.JSONDecoder().decode(ret[1])
63 def query_ns_aai(global_customer_id, service_type, service_instance_id, data):
64 resource = "/business/customers/customer/%s/service-subscriptions/service-subscription/" \
65 "%s/service-instances/service-instance/%s" % \
66 (global_customer_id, service_type, service_instance_id)
67 ret = call_aai(resource, "GET", data)
69 logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
70 raise NSLCMException("Ns instance query exception in AAI")
71 return json.JSONDecoder().decode(ret[1])
73 def create_vnf_aai(vnf_id, data):
74 resource = "/network/generic-vnfs/generic-vnf/%s" % vnf_id
75 ret = call_aai(resource, "PUT", data)
77 logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
78 raise NSLCMException("Vnf instance creation exception in AAI")
79 return json.JSONDecoder().decode(ret[1])
81 def delete_vnf_aai(vnf_id, data=''):
82 resource = "/network/generic-vnfs/generic-vnf/%s" % vnf_id
83 ret = call_aai(resource, "DELETE", data)
85 logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
86 raise NSLCMException("Vnf instance delete exception in AAI")
87 return json.JSONDecoder().decode(ret[1])
89 def query_vnf_aai(vnf_id, data):
90 resource = "/network/generic-vnfs/generic-vnf/%s" % vnf_id
91 ret = call_aai(resource, "GET", data)
93 logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
94 raise NSLCMException("Vnf instance query exception in AAI")
95 return json.JSONDecoder().decode(ret[1])
97 def create_vserver_aai(cloud_owner, cloud_region_id, tenant_id, vserver_id, data):
98 resource = "/cloud-infrastructure/cloud-regions/cloud-region/%s/" \
99 "%s/tenants/tenant/%s/vservers/vserver/%s" % \
100 (cloud_owner, cloud_region_id, tenant_id, vserver_id)
101 ret = call_aai(resource, "PUT", data)
103 logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
104 raise NSLCMException("Vserver creation exception in AAI")
105 return json.JSONDecoder().decode(ret[1])
107 def delete_vserver_aai(cloud_owner, cloud_region_id, tenant_id, vserver_id, data=''):
108 resource = "/cloud-infrastructure/cloud-regions/cloud-region/%s/" \
109 "%s/tenants/tenant/%s/vservers/vserver/%s" % \
110 (cloud_owner, cloud_region_id, tenant_id, vserver_id)
111 ret = call_aai(resource, "DELETE", data)
113 logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
114 raise NSLCMException("Vserver delete exception in AAI")
115 return json.JSONDecoder().decode(ret[1])
117 def query_vserver_aai(cloud_owner, cloud_region_id, tenant_id, vserver_id, data):
118 resource = "/cloud-infrastructure/cloud-regions/cloud-region/%s/" \
119 "%s/tenants/tenant/%s/vservers/vserver/%s" % \
120 (cloud_owner, cloud_region_id, tenant_id, vserver_id)
121 ret = call_aai(resource, "GET", data)
123 logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
124 raise NSLCMException("Vserver query exception in AAI")
125 return json.JSONDecoder().decode(ret[1])
127 def put_vserver_relationship(cloud_owner, cloud_region_id, tenant_id, vserver_id, data):
128 resource = "/cloud-infrastructure/cloud-regions/cloud-region/%s/" \
129 "%s/tenants/tenant/%s/vservers/vserver/%s/relationship-list/relationship" % \
130 (cloud_owner, cloud_region_id, tenant_id, vserver_id)
131 ret = call_aai(resource, "PUT", data)
133 logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
134 raise NSLCMException("Put or update vserver relationship exception in AAI")
135 return json.JSONDecoder().decode(ret[1])
138 def delete_vserver_relationship(cloud_owner, cloud_region_id, tenant_id, vserver_id):
139 resource = "/cloud-infrastructure/cloud-regions/cloud-region/%s/" \
140 "%s/tenants/tenant/%s/vservers/vserver/%s/relationship-list/relationship" % \
141 (cloud_owner, cloud_region_id, tenant_id, vserver_id)
142 ret = call_aai(resource, "DELETE")
144 logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
145 raise NSLCMException("Delete vserver relationship exception in AAI")
146 return json.JSONDecoder().decode(ret[1])
149 def put_vnf_relationship(vnf_id, data):
150 resource = "/network/generic-vnfs/generic-vnf/%s/relationship-list/relationship" % vnf_id
151 ret = call_aai(resource, "PUT", data)
153 logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
154 raise NSLCMException("Put or update vnf instance relationship exception in AAI")
155 return json.JSONDecoder().decode(ret[1])
158 def delete_vnf_relationship(vnf_id):
159 resource = "/network/generic-vnfs/generic-vnf/%s/relationship-list/relationship" % vnf_id
160 ret = call_aai(resource, "DELETE")
162 logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
163 raise NSLCMException("Delete vnf instance relationship exception in AAI")
164 return json.JSONDecoder().decode(ret[1])
167 def put_ns_relationship(global_customer_id, service_type, service_instance_id, data):
168 resource = "/business/customers/customer/%s/service-subscriptions/service-subscription/" \
169 "%s/service-instances/service-instance/%s/relationship-list/relationship" % \
170 (global_customer_id, service_type, service_instance_id)
171 ret = call_aai(resource, "PUT", data)
173 logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
174 raise NSLCMException("Put or update ns instance relationship exception in AAI")
175 return json.JSONDecoder().decode(ret[1])
178 def delete_ns_relationship(global_customer_id, service_type, service_instance_id):
179 resource = "/business/customers/customer/%s/service-subscriptions/service-subscription/" \
180 "%s/service-instances/service-instance/%s/relationship-list/relationship" % \
181 (global_customer_id, service_type, service_instance_id)
182 ret = call_aai(resource, "DELETE")
184 logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
185 raise NSLCMException("Delete ns instance relationship exception in AAI")
186 return json.JSONDecoder().decode(ret[1])
189 def split_vim_to_owner_region(vim_id):
190 split_vim = vim_id.split('_')
191 cloud_owner = split_vim[0]
192 cloud_region = "".join(split_vim[1:])
193 return cloud_owner, cloud_region
195 def convert_vim_info(vim_info_aai):
196 vim_id = vim_info_aai["cloud-owner"] + "_" + vim_info_aai["cloud-region-id"]
197 esr_system_info = ignore_case_get(ignore_case_get(vim_info_aai, "esr-system-info-list"), "esr-system-info")
198 # tenants = ignore_case_get(vim_info_aai, "tenants")
202 "url": ignore_case_get(esr_system_info[0], "service-url"),
203 "userName": ignore_case_get(esr_system_info[0], "user-name"),
204 "password": ignore_case_get(esr_system_info[0], "password"),
205 # "tenant": ignore_case_get(tenants[0], "tenant-id"),
206 "tenant": ignore_case_get(esr_system_info[0], "default-tenant"),
207 "vendor": ignore_case_get(esr_system_info[0], "vendor"),
208 "version": ignore_case_get(esr_system_info[0], "version"),
209 "description": "vim",
211 "type": ignore_case_get(esr_system_info[0], "type"),
212 "createTime": "2016-07-18 12:22:53"
218 ret = call_aai("/cloud-infrastructure/cloud-regions", "GET")
220 logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
221 raise NSLCMException("Failed to query vims from extsys.")
222 # convert vim_info_aai to internal vim_info
223 vims_aai = json.JSONDecoder().decode(ret[1])
224 vims_aai = ignore_case_get(vims_aai, "cloud-region")
227 vim = convert_vim_info(vim)
228 vims_info.append(vim)