Modify gvnfm-vnflcm code and unit tests
[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.restcall import rest_no_auth, call_req
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 call_req(AAI_BASE_URL,
32                     AAI_USER,
33                     AAI_PASSWORD,
34                     rest_no_auth,
35                     resource,
36                     method,
37                     data,
38                     additional_headers)
39
40
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)
46     if ret[0] != 0:
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])
50
51
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")
57     if ret[0] != 0:
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])
61
62
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)
68     if ret[0] != 0:
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])
72
73
74 def create_vnf(vnf_id, data):
75     resource = "/network/generic-vnfs/generic-vnf/%s" % vnf_id
76     ret = call_aai(resource, "PUT", data)
77     if ret[0] != 0:
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])
81
82
83 def delete_vnf(vnf_id):
84     resource = "/network/generic-vnfs/generic-vnf/%s" % vnf_id
85     ret = call_aai(resource, "DELETE")
86     if ret[0] != 0:
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])
90
91
92 def query_vnf(vnf_id, data):
93     resource = "/network/generic-vnfs/generic-vnf/%s" % vnf_id
94     ret = call_aai(resource, "GET", data)
95     if ret[0] != 0:
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])
99
100
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)
106     if ret[0] != 0:
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])
110
111
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")
117     if ret[0] != 0:
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])
121
122
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)
128     if ret[0] != 0:
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])
132
133
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)
139     if ret[0] != 0:
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])
143
144
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")
150     if ret[0] != 0:
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])
154
155
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)
159     if ret[0] != 0:
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])
163
164
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")
168     if ret[0] != 0:
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])
172
173
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)
179     if ret[0] != 0:
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])
183
184
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")
190     if ret[0] != 0:
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])