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.restcall import rest_no_auth, call_req
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 call_req(AAI_BASE_URL,
41 def create_ns(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 NFLCMException("Ns instance creation exception in AAI")
49 return json.JSONDecoder().decode(ret[1])
52 def delete_ns(global_customer_id, service_type, service_instance_id):
53 resource = "/business/customers/customer/%s/service-subscriptions/service-subscription/" \
54 "%s/service-instances/service-instance/%s" % \
55 (global_customer_id, service_type, service_instance_id)
56 ret = call_aai(resource, "DELETE")
58 logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
59 raise NFLCMException("Ns instance delete exception in AAI")
60 return json.JSONDecoder().decode(ret[1])
63 def query_ns(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 NFLCMException("Ns instance query exception in AAI")
71 return json.JSONDecoder().decode(ret[1])
74 def create_vnf(vnf_id, data):
75 resource = "/network/generic-vnfs/generic-vnf/%s" % vnf_id
76 ret = call_aai(resource, "PUT", data)
78 logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
79 raise NFLCMException("Vnf instance creation exception in AAI")
80 return json.JSONDecoder().decode(ret[1])
83 def delete_vnf(vnf_id):
84 resource = "/network/generic-vnfs/generic-vnf/%s" % vnf_id
85 ret = call_aai(resource, "DELETE")
87 logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
88 raise NFLCMException("Vnf instance delete exception in AAI")
89 return json.JSONDecoder().decode(ret[1])
92 def query_vnf(vnf_id, data):
93 resource = "/network/generic-vnfs/generic-vnf/%s" % vnf_id
94 ret = call_aai(resource, "GET", data)
96 logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
97 raise NFLCMException("Vnf instance query exception in AAI")
98 return json.JSONDecoder().decode(ret[1])
101 def create_vserver(cloud_owner, cloud_region_id, tenant_id, vserver_id, data):
102 resource = "/cloud-infrastructure/cloud-regions/cloud-region/%s/" \
103 "%s/tenants/tenant/%s/vservers/vserver/%s" % \
104 (cloud_owner, cloud_region_id, tenant_id, vserver_id)
105 ret = call_aai(resource, "PUT", data)
107 logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
108 raise NFLCMException("Vserver creation exception in AAI")
109 return json.JSONDecoder().decode(ret[1])
112 def delete_vserver(cloud_owner, cloud_region_id, tenant_id, vserver_id):
113 resource = "/cloud-infrastructure/cloud-regions/cloud-region/%s/" \
114 "%s/tenants/tenant/%s/vservers/vserver/%s" % \
115 (cloud_owner, cloud_region_id, tenant_id, vserver_id)
116 ret = call_aai(resource, "DELETE")
118 logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
119 raise NFLCMException("Vserver delete exception in AAI")
120 return json.JSONDecoder().decode(ret[1])
123 def query_vserver(cloud_owner, cloud_region_id, tenant_id, vserver_id, data):
124 resource = "/cloud-infrastructure/cloud-regions/cloud-region/%s/" \
125 "%s/tenants/tenant/%s/vservers/vserver/%s" % \
126 (cloud_owner, cloud_region_id, tenant_id, vserver_id)
127 ret = call_aai(resource, "GET", data)
129 logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
130 raise NFLCMException("Vserver query exception in AAI")
131 return json.JSONDecoder().decode(ret[1])
134 def put_vserver_relationship(cloud_owner, cloud_region_id, tenant_id, vserver_id, data):
135 resource = "/cloud-infrastructure/cloud-regions/cloud-region/%s/" \
136 "%s/tenants/tenant/%s/vservers/vserver/%s/relationship-list/relationship" % \
137 (cloud_owner, cloud_region_id, tenant_id, vserver_id)
138 ret = call_aai(resource, "PUT", data)
140 logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
141 raise NFLCMException("Put or update vserver relationship exception in AAI")
142 return json.JSONDecoder().decode(ret[1])
145 def delete_vserver_relationship(cloud_owner, cloud_region_id, tenant_id, vserver_id):
146 resource = "/cloud-infrastructure/cloud-regions/cloud-region/%s/" \
147 "%s/tenants/tenant/%s/vservers/vserver/%s/relationship-list/relationship" % \
148 (cloud_owner, cloud_region_id, tenant_id, vserver_id)
149 ret = call_aai(resource, "DELETE")
151 logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
152 raise NFLCMException("Delete vserver relationship exception in AAI")
153 return json.JSONDecoder().decode(ret[1])
156 def put_vnf_relationship(vnf_id, data):
157 resource = "/network/generic-vnfs/generic-vnf/%s/relationship-list/relationship" % vnf_id
158 ret = call_aai(resource, "PUT", data)
160 logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
161 raise NFLCMException("Put or update vnf instance relationship exception in AAI")
162 return json.JSONDecoder().decode(ret[1])
165 def delete_vnf_relationship(vnf_id):
166 resource = "/network/generic-vnfs/generic-vnf/%s/relationship-list/relationship" % vnf_id
167 ret = call_aai(resource, "DELETE")
169 logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
170 raise NFLCMException("Delete vnf instance relationship exception in AAI")
171 return json.JSONDecoder().decode(ret[1])
174 def put_ns_relationship(global_customer_id, service_type, service_instance_id, data):
175 resource = "/business/customers/customer/%s/service-subscriptions/service-subscription/" \
176 "%s/service-instances/service-instance/%s/relationship-list/relationship" % \
177 (global_customer_id, service_type, service_instance_id)
178 ret = call_aai(resource, "PUT", data)
180 logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
181 raise NFLCMException("Put or update ns instance relationship exception in AAI")
182 return json.JSONDecoder().decode(ret[1])
185 def delete_ns_relationship(global_customer_id, service_type, service_instance_id):
186 resource = "/business/customers/customer/%s/service-subscriptions/service-subscription/" \
187 "%s/service-instances/service-instance/%s/relationship-list/relationship" % \
188 (global_customer_id, service_type, service_instance_id)
189 ret = call_aai(resource, "DELETE")
191 logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
192 raise NFLCMException("Delete ns instance relationship exception in AAI")
193 return json.JSONDecoder().decode(ret[1])