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.config.config import AAI_BASE_URL, AAI_USER, AAI_PASSWORD
20 from lcm.pub.exceptions import NFLCMException
21 from lcm.pub.utils import restcall
23 logger = logging.getLogger(__name__)
26 def call_aai(resource, method, data=''):
27 additional_headers = {
28 'X-FromAppId': 'VFC-GVNFM-VNFLCM',
29 'X-TransactionId': str(uuid.uuid1())
31 return restcall.call_req(AAI_BASE_URL,
34 restcall.rest_no_auth,
41 def create_customer_aai(global_customer_id, data):
42 resource = "/business/customers/customer/%s" % global_customer_id
43 ret = call_aai(resource, "PUT", data)
45 logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
46 raise NFLCMException("Customer creation exception in AAI")
47 return json.JSONDecoder().decode(ret[1]), ret[2]
50 def query_customer_aai(global_customer_id):
51 resource = "/business/customers/customer/%s?depth=all" % global_customer_id
52 ret = call_aai(resource, "GET")
54 logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
55 raise NFLCMException("Get customer info exception in AAI")
56 return json.JSONDecoder().decode(ret[1]), ret[2]
59 def delete_customer_aai(global_customer_id, resource_version=""):
60 resource = "/business/customers/customer/%s" % global_customer_id
62 resource = resource + "?resource-version=%s" % resource_version
63 ret = call_aai(resource, "DELETE")
65 logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
66 raise NFLCMException("Customer delete exception in AAI")
67 return json.JSONDecoder().decode(ret[1]), ret[2]
70 def put_customer_relationship(global_customer_id, data):
71 resource = "/business/customers/customer/{global-customer-id}/relationship-list/relationship" % global_customer_id
72 ret = call_aai(resource, "PUT", data)
74 logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
75 raise NFLCMException("Put or update customer relationship exception in AAI")
76 return json.JSONDecoder().decode(ret[1]), ret[2]
79 def delete_customer_relationship(global_customer_id):
80 resource = "/business/customers/customer/{global-customer-id}/relationship-list/relationship" % global_customer_id
81 ret = call_aai(resource, "DELETE")
83 logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
84 raise NFLCMException("Delete customer relationship exception in AAI")
85 return json.JSONDecoder().decode(ret[1]), ret[2]
88 def create_ns_aai(global_customer_id, service_type, service_instance_id, data):
89 resource = "/business/customers/customer/%s/service-subscriptions/service-subscription/" \
90 "%s/service-instances/service-instance/%s" % \
91 (global_customer_id, service_type, service_instance_id)
92 ret = call_aai(resource, "PUT", data)
94 logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
95 raise NFLCMException("Ns instance creation exception in AAI")
96 return json.JSONDecoder().decode(ret[1]), ret[2]
99 def query_ns_aai(global_customer_id, service_type, service_instance_id):
100 resource = "/business/customers/customer/%s/service-subscriptions/service-subscription/" \
101 "%s/service-instances/service-instance/%s?depth=all" % \
102 (global_customer_id, service_type, service_instance_id)
103 ret = call_aai(resource, "GET")
105 logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
106 raise NFLCMException("Ns instance query exception in AAI")
107 return json.JSONDecoder().decode(ret[1])
110 def delete_ns_aai(global_customer_id, service_type, service_instance_id, resource_version=""):
111 resource = "/business/customers/customer/%s/service-subscriptions/service-subscription/" \
112 "%s/service-instances/service-instance/%s" % \
113 (global_customer_id, service_type, service_instance_id)
115 resource = resource + "?resource-version=%s" % resource_version
116 ret = call_aai(resource, "DELETE")
118 logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
119 raise NFLCMException("Ns instance delete exception in AAI")
120 return json.JSONDecoder().decode(ret[1]), ret[2]
123 def put_ns_relationship(global_customer_id, service_type, service_instance_id, data):
124 resource = "/business/customers/customer/%s/service-subscriptions/service-subscription/" \
125 "%s/service-instances/service-instance/%s/relationship-list/relationship" % \
126 (global_customer_id, service_type, service_instance_id)
127 ret = call_aai(resource, "PUT", data)
129 logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
130 raise NFLCMException("Put or update ns instance relationship exception in AAI")
131 return json.JSONDecoder().decode(ret[1]), ret[2]
134 def delete_ns_relationship(global_customer_id, service_type, service_instance_id):
135 resource = "/business/customers/customer/%s/service-subscriptions/service-subscription/" \
136 "%s/service-instances/service-instance/%s/relationship-list/relationship" % \
137 (global_customer_id, service_type, service_instance_id)
138 ret = call_aai(resource, "DELETE")
140 logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
141 raise NFLCMException("Delete ns instance relationship exception in AAI")
142 return json.JSONDecoder().decode(ret[1]), ret[2]
145 def create_vnf_aai(vnf_id, data):
146 resource = "/network/generic-vnfs/generic-vnf/%s" % vnf_id
147 ret = call_aai(resource, "PUT", data)
149 logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
150 raise NFLCMException("Vnf instance creation exception in AAI")
151 return json.JSONDecoder().decode(ret[1]), ret[2]
154 def query_vnf_aai(vnf_id):
155 resource = "/network/generic-vnfs/generic-vnf/%s?depth=all" % vnf_id
156 ret = call_aai(resource, "GET")
158 logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
159 raise NFLCMException("Vnf instance query exception in AAI")
160 return json.JSONDecoder().decode(ret[1])
163 def delete_vnf_aai(vnf_id, resource_version=""):
164 resource = "/network/generic-vnfs/generic-vnf/%s" % vnf_id
166 resource = resource + "?resource-version=%s" % resource_version
167 ret = call_aai(resource, "DELETE")
169 logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
170 raise NFLCMException("Vnf instance delete exception in AAI")
171 return json.JSONDecoder().decode(ret[1]), ret[2]
174 def put_vnf_relationship(vnf_id, data):
175 resource = "/network/generic-vnfs/generic-vnf/%s/relationship-list/relationship" % vnf_id
176 ret = call_aai(resource, "PUT", data)
178 logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
179 raise NFLCMException("Put or update vnf instance relationship exception in AAI")
180 return json.JSONDecoder().decode(ret[1]), ret[2]
183 def delete_vnf_relationship(vnf_id):
184 resource = "/network/generic-vnfs/generic-vnf/%s/relationship-list/relationship" % vnf_id
185 ret = call_aai(resource, "DELETE")
187 logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
188 raise NFLCMException("Delete vnf instance relationship exception in AAI")
189 return json.JSONDecoder().decode(ret[1]), ret[2]
192 def create_vserver_aai(cloud_owner, cloud_region_id, tenant_id, vserver_id, data):
193 resource = "/cloud-infrastructure/cloud-regions/cloud-region/%s/" \
194 "%s/tenants/tenant/%s/vservers/vserver/%s" % \
195 (cloud_owner, cloud_region_id, tenant_id, vserver_id)
196 ret = call_aai(resource, "PUT", data)
198 logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
199 raise NFLCMException("Vserver creation exception in AAI")
200 return json.JSONDecoder().decode(ret[1]), ret[2]
203 def query_vserver_aai(cloud_owner, cloud_region_id, tenant_id, vserver_id):
204 resource = "/cloud-infrastructure/cloud-regions/cloud-region/%s/" \
205 "%s/tenants/tenant/%s/vservers/vserver/%s?depth=all" % \
206 (cloud_owner, cloud_region_id, tenant_id, vserver_id)
207 ret = call_aai(resource, "GET")
209 logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
210 raise NFLCMException("Vserver query exception in AAI")
211 return json.JSONDecoder().decode(ret[1])
214 def delete_vserver_aai(cloud_owner, cloud_region_id, tenant_id, vserver_id, resource_version=""):
215 resource = "/cloud-infrastructure/cloud-regions/cloud-region/%s/" \
216 "%s/tenants/tenant/%s/vservers/vserver/%s" % \
217 (cloud_owner, cloud_region_id, tenant_id, vserver_id)
219 resource = resource + "?resource-version=%s" % resource_version
220 ret = call_aai(resource, "DELETE")
222 logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
223 raise NFLCMException("Vserver delete exception in AAI")
224 return json.JSONDecoder().decode(ret[1]), ret[2]
227 def put_vserver_relationship(cloud_owner, cloud_region_id, tenant_id, vserver_id, data):
228 resource = "/cloud-infrastructure/cloud-regions/cloud-region/%s/" \
229 "%s/tenants/tenant/%s/vservers/vserver/%s/relationship-list/relationship" % \
230 (cloud_owner, cloud_region_id, tenant_id, vserver_id)
231 ret = call_aai(resource, "PUT", data)
233 logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
234 raise NFLCMException("Put or update vserver relationship exception in AAI")
235 return json.JSONDecoder().decode(ret[1]), ret[2]
238 def delete_vserver_relationship(cloud_owner, cloud_region_id, tenant_id, vserver_id):
239 resource = "/cloud-infrastructure/cloud-regions/cloud-region/%s/" \
240 "%s/tenants/tenant/%s/vservers/vserver/%s/relationship-list/relationship" % \
241 (cloud_owner, cloud_region_id, tenant_id, vserver_id)
242 ret = call_aai(resource, "DELETE")
244 logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
245 raise NFLCMException("Delete vserver relationship exception in AAI")
246 return json.JSONDecoder().decode(ret[1]), ret[2]