Fix docker file of vfc-gvnfm-vnflcm
[vfc/gvnfm/vnflcm.git] / lcm / lcm / pub / aaiapi / aai.py
1 # Copyright 2017 ZTE Corporation.\r
2 #\r
3 # Licensed under the Apache License, Version 2.0 (the "License");\r
4 # you may not use this file except in compliance with the License.\r
5 # You may obtain a copy of the License at\r
6 #\r
7 #         http://www.apache.org/licenses/LICENSE-2.0\r
8 #\r
9 # Unless required by applicable law or agreed to in writing, software\r
10 # distributed under the License is distributed on an "AS IS" BASIS,\r
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
12 # See the License for the specific language governing permissions and\r
13 # limitations under the License.\r
14 \r
15 import json\r
16 import logging\r
17 \r
18 from lcm.pub.config.config import AAI_BASE_URL, AAI_USER, AAI_PASSWORD\r
19 from lcm.pub.exceptions import NFLCMException\r
20 from lcm.pub.utils.restcall import call_req_aai, rest_no_auth\r
21 \r
22 logger = logging.getLogger(__name__)\r
23 \r
24 def call_aai(resource, method, data=''):\r
25     return call_req_aai(AAI_BASE_URL, AAI_USER, AAI_PASSWORD, rest_no_auth, resource, method, data)\r
26 \r
27 \r
28 def create_ns(global_customer_id, service_type, service_instance_id, data):\r
29     resource = "/business/customers/customer/%s/service-subscriptions/service-subscription/" \\r
30                "%s/service-instances/service-instance/%s" % \\r
31                (global_customer_id, service_type, service_instance_id)\r
32     ret = call_aai(resource, "PUT", data)\r
33     if ret[0] != 0:\r
34         logger.error("Status code is %s, detail is %s.", ret[2], ret[1])\r
35         raise NFLCMException("Ns instance creation exception in AAI")\r
36     return json.JSONDecoder().decode(ret[1])\r
37 \r
38 \r
39 def delete_ns(global_customer_id, service_type, service_instance_id):\r
40     resource = "/business/customers/customer/%s/service-subscriptions/service-subscription/" \\r
41                "%s/service-instances/service-instance/%s" % \\r
42                (global_customer_id, service_type, service_instance_id)\r
43     ret = call_aai(resource, "DELETE")\r
44     if ret[0] != 0:\r
45         logger.error("Status code is %s, detail is %s.", ret[2], ret[1])\r
46         raise NFLCMException("Ns instance delete exception in AAI")\r
47     return json.JSONDecoder().decode(ret[1])\r
48 \r
49 \r
50 def query_ns(global_customer_id, service_type, service_instance_id, data):\r
51     resource = "/business/customers/customer/%s/service-subscriptions/service-subscription/" \\r
52                "%s/service-instances/service-instance/%s" % \\r
53                (global_customer_id, service_type, service_instance_id)\r
54     ret = call_aai(resource, "GET", data)\r
55     if ret[0] != 0:\r
56         logger.error("Status code is %s, detail is %s.", ret[2], ret[1])\r
57         raise NFLCMException("Ns instance query exception in AAI")\r
58     return json.JSONDecoder().decode(ret[1])\r
59 \r
60 \r
61 def create_vnf(vnf_id, data):\r
62     resource = "/network/generic-vnfs/generic-vnf/%s" % vnf_id\r
63     ret = call_aai(resource, "PUT", data)\r
64     if ret[0] != 0:\r
65         logger.error("Status code is %s, detail is %s.", ret[2], ret[1])\r
66         raise NFLCMException("Vnf instance creation exception in AAI")\r
67     return json.JSONDecoder().decode(ret[1])\r
68 \r
69 \r
70 def delete_vnf(vnf_id):\r
71     resource = "/network/generic-vnfs/generic-vnf/%s" % vnf_id\r
72     ret = call_aai(resource, "DELETE")\r
73     if ret[0] != 0:\r
74         logger.error("Status code is %s, detail is %s.", ret[2], ret[1])\r
75         raise NFLCMException("Vnf instance delete exception in AAI")\r
76     return json.JSONDecoder().decode(ret[1])\r
77 \r
78 \r
79 def query_vnf(vnf_id, data):\r
80     resource = "/network/generic-vnfs/generic-vnf/%s" % vnf_id\r
81     ret = call_aai(resource, "GET", data)\r
82     if ret[0] != 0:\r
83         logger.error("Status code is %s, detail is %s.", ret[2], ret[1])\r
84         raise NFLCMException("Vnf instance query exception in AAI")\r
85     return json.JSONDecoder().decode(ret[1])\r
86 \r
87 \r
88 def create_vserver(cloud_owner, cloud_region_id, tenant_id, vserver_id, data):\r
89     resource = "/cloud-infrastructure/cloud-regions/cloud-region/%s/" \\r
90                "%s/tenants/tenant/%s/vservers/vserver/%s" % \\r
91                (cloud_owner, cloud_region_id, tenant_id, vserver_id)\r
92     ret = call_aai(resource, "PUT", data)\r
93     if ret[0] != 0:\r
94         logger.error("Status code is %s, detail is %s.", ret[2], ret[1])\r
95         raise NFLCMException("Vserver creation exception in AAI")\r
96     return json.JSONDecoder().decode(ret[1])\r
97 \r
98 \r
99 def delete_vserver(cloud_owner, cloud_region_id, tenant_id, vserver_id):\r
100     resource = "/cloud-infrastructure/cloud-regions/cloud-region/%s/" \\r
101                "%s/tenants/tenant/%s/vservers/vserver/%s" % \\r
102                (cloud_owner, cloud_region_id, tenant_id, vserver_id)\r
103     ret = call_aai(resource, "DELETE")\r
104     if ret[0] != 0:\r
105         logger.error("Status code is %s, detail is %s.", ret[2], ret[1])\r
106         raise NFLCMException("Vserver delete exception in AAI")\r
107     return json.JSONDecoder().decode(ret[1])\r
108 \r
109 \r
110 def query_vserver(cloud_owner, cloud_region_id, tenant_id, vserver_id, data):\r
111     resource = "/cloud-infrastructure/cloud-regions/cloud-region/%s/" \\r
112                "%s/tenants/tenant/%s/vservers/vserver/%s" % \\r
113                (cloud_owner, cloud_region_id, tenant_id, vserver_id)\r
114     ret = call_aai(resource, "GET", data)\r
115     if ret[0] != 0:\r
116         logger.error("Status code is %s, detail is %s.", ret[2], ret[1])\r
117         raise NFLCMException("Vserver query exception in AAI")\r
118     return json.JSONDecoder().decode(ret[1])\r
119 \r
120 \r
121 def put_vserver_relationship(cloud_owner, cloud_region_id, tenant_id, vserver_id, data):\r
122     resource = "/cloud-infrastructure/cloud-regions/cloud-region/%s/" \\r
123                "%s/tenants/tenant/%s/vservers/vserver/%s/relationship-list/relationship" % \\r
124                (cloud_owner, cloud_region_id, tenant_id, vserver_id)\r
125     ret = call_aai(resource, "PUT", data)\r
126     if ret[0] != 0:\r
127         logger.error("Status code is %s, detail is %s.", ret[2], ret[1])\r
128         raise NFLCMException("Put or update vserver relationship exception in AAI")\r
129     return json.JSONDecoder().decode(ret[1])\r
130 \r
131 \r
132 def delete_vserver_relationship(cloud_owner, cloud_region_id, tenant_id, vserver_id):\r
133     resource = "/cloud-infrastructure/cloud-regions/cloud-region/%s/" \\r
134                "%s/tenants/tenant/%s/vservers/vserver/%s/relationship-list/relationship" % \\r
135                (cloud_owner, cloud_region_id, tenant_id, vserver_id)\r
136     ret = call_aai(resource, "DELETE")\r
137     if ret[0] != 0:\r
138         logger.error("Status code is %s, detail is %s.", ret[2], ret[1])\r
139         raise NFLCMException("Delete vserver relationship exception in AAI")\r
140     return json.JSONDecoder().decode(ret[1])\r
141 \r
142 \r
143 def put_vnf_relationship(vnf_id, data):\r
144     resource = "/network/generic-vnfs/generic-vnf/%s/relationship-list/relationship" % vnf_id\r
145     ret = call_aai(resource, "PUT", data)\r
146     if ret[0] != 0:\r
147         logger.error("Status code is %s, detail is %s.", ret[2], ret[1])\r
148         raise NFLCMException("Put or update vnf instance relationship exception in AAI")\r
149     return json.JSONDecoder().decode(ret[1])\r
150 \r
151 \r
152 def delete_vnf_relationship(vnf_id):\r
153     resource = "/network/generic-vnfs/generic-vnf/%s/relationship-list/relationship" % vnf_id\r
154     ret = call_aai(resource, "DELETE")\r
155     if ret[0] != 0:\r
156         logger.error("Status code is %s, detail is %s.", ret[2], ret[1])\r
157         raise NFLCMException("Delete vnf instance relationship exception in AAI")\r
158     return json.JSONDecoder().decode(ret[1])\r
159 \r
160 \r
161 def put_ns_relationship(global_customer_id, service_type, service_instance_id, data):\r
162     resource = "/business/customers/customer/%s/service-subscriptions/service-subscription/" \\r
163                "%s/service-instances/service-instance/%s/relationship-list/relationship" % \\r
164                (global_customer_id, service_type, service_instance_id)\r
165     ret = call_aai(resource, "PUT", data)\r
166     if ret[0] != 0:\r
167         logger.error("Status code is %s, detail is %s.", ret[2], ret[1])\r
168         raise NFLCMException("Put or update ns instance relationship exception in AAI")\r
169     return json.JSONDecoder().decode(ret[1])\r
170 \r
171 \r
172 def delete_ns_relationship(global_customer_id, service_type, service_instance_id):\r
173     resource = "/business/customers/customer/%s/service-subscriptions/service-subscription/" \\r
174                "%s/service-instances/service-instance/%s/relationship-list/relationship" % \\r
175                (global_customer_id, service_type, service_instance_id)\r
176     ret = call_aai(resource, "DELETE")\r
177     if ret[0] != 0:\r
178         logger.error("Status code is %s, detail is %s.", ret[2], ret[1])\r
179         raise NFLCMException("Delete ns instance relationship exception in AAI")\r
180     return json.JSONDecoder().decode(ret[1])\r