150303c3c01f64cf103280a2525d24172a210f12
[vfc/gvnfm/vnflcm.git] / lcm / 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.config.config import AAI_BASE_URL, AAI_USER, AAI_PASSWORD
20 from lcm.pub.exceptions import NFLCMException
21 from lcm.pub.utils import restcall
22
23 logger = logging.getLogger(__name__)
24
25
26 def call_aai(resource, method, data=''):
27     additional_headers = {
28         'X-FromAppId': 'VFC-GVNFM-VNFLCM',
29         'X-TransactionId': str(uuid.uuid1())
30     }
31     return restcall.call_req(AAI_BASE_URL,
32                     AAI_USER,
33                     AAI_PASSWORD,
34                     restcall.rest_no_auth,
35                     resource,
36                     method,
37                     data,
38                     additional_headers)
39
40
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)
44     if ret[0] != 0:
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]
48
49
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")
53     if ret[0] != 0:
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]
57
58
59 def delete_customer_aai(global_customer_id, resource_version=""):
60     resource = "/business/customers/customer/%s" % global_customer_id
61     if resource_version:
62         resource = resource + "?resource-version=%s" % resource_version
63     ret = call_aai(resource, "DELETE")
64     if ret[0] != 0:
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]
68
69
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)
73     if ret[0] != 0:
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]
77
78
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")
82     if ret[0] != 0:
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]
86
87
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)
93     if ret[0] != 0:
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]
97
98
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")
104     if ret[0] != 0:
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])
108
109
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)
114     if resource_version:
115         resource = resource + "?resource-version=%s" % resource_version
116     ret = call_aai(resource, "DELETE")
117     if ret[0] != 0:
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]
121
122
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)
128     if ret[0] != 0:
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]
132
133
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")
139     if ret[0] != 0:
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]
143
144
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)
148     if ret[0] != 0:
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]
152
153
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")
157     if ret[0] != 0:
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])
161
162
163 def delete_vnf_aai(vnf_id, resource_version=""):
164     resource = "/network/generic-vnfs/generic-vnf/%s" % vnf_id
165     if resource_version:
166         resource = resource + "?resource-version=%s" % resource_version
167     ret = call_aai(resource, "DELETE")
168     if ret[0] != 0:
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]
172
173
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)
177     if ret[0] != 0:
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]
181
182
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")
186     if ret[0] != 0:
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]
190
191
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)
197     if ret[0] != 0:
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]
201
202
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")
208     if ret[0] != 0:
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])
212
213
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)
218     if resource_version:
219         resource = resource + "?resource-version=%s" % resource_version
220     ret = call_aai(resource, "DELETE")
221     if ret[0] != 0:
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]
225
226
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)
232     if ret[0] != 0:
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]
236
237
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")
243     if ret[0] != 0:
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]