3b73a0f421616c6168d3787f05de5684e081cb58
[vfc/nfvo/lcm.git] / lcm / pub / nfvi / vim / api / multivim / api.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 import json
15 import logging
16
17 from lcm.pub.nfvi.vim.lib.vimexception import VimException
18 from lcm.pub.utils.restcall import req_by_msb
19 from lcm.pub.nfvi.vim import const
20
21 logger = logging.getLogger(__name__)
22
23 VIM_DRIVER_BASE_URL = "api/multicloud/v0"
24
25
26 def call(vim_id, tenant_id, res, method, data=''):
27     if data and not isinstance(data, (str, unicode)):
28         data = json.JSONEncoder().encode(data)
29     url = "{base_url}/{vim_id}{tenant_id}/{res}".format(
30         base_url=VIM_DRIVER_BASE_URL,
31         vim_id=vim_id,
32         tenant_id="/" + tenant_id if tenant_id else "",
33         res=res)
34     ret = req_by_msb(url, method, data)
35     if ret[0] > 0:
36         raise VimException(ret[1], ret[2])
37     return json.JSONDecoder().decode(ret[1]) if ret[1] else {}
38
39
40 #######################################################################
41
42
43 def create_image(vim_id, tenant_id, data):
44     return call(vim_id, tenant_id, "images", "POST", data)
45
46
47 def delete_image(vim_id, tenant_id, image_id):
48     return call(vim_id, tenant_id, "images/%s" % image_id, "DELETE")
49
50
51 def get_image(vim_id, tenant_id, image_id):
52     return call(vim_id, tenant_id, "images/%s" % image_id, "GET")
53
54
55 def list_image(vim_id, tenant_id):
56     return call(vim_id, tenant_id, "images", "GET")
57
58
59 ######################################################################
60
61
62 def create_network(vim_id, tenant_id, data):
63     return call(vim_id, tenant_id, "networks", "POST", data)
64
65
66 def delete_network(vim_id, tenant_id, network_id):
67     return call(vim_id, tenant_id, "networks/%s" % network_id, "DELETE")
68
69
70 def get_network(vim_id, tenant_id, network_id):
71     return call(vim_id, tenant_id, "networks/%s" % network_id, "GET")
72
73
74 def list_network(vim_id, tenant_id):
75     return call(vim_id, tenant_id, "networks", "GET")
76
77
78 ######################################################################
79
80
81 def create_subnet(vim_id, tenant_id, data):
82     return call(vim_id, tenant_id, "subnets", "POST", data)
83
84
85 def delete_subnet(vim_id, tenant_id, subnet_id):
86     return call(vim_id, tenant_id, "subnets/%s" % subnet_id, "DELETE")
87
88
89 def get_subnet(vim_id, tenant_id, subnet_id):
90     return call(vim_id, tenant_id, "subnets/%s" % subnet_id, "GET")
91
92
93 def list_subnet(vim_id, tenant_id):
94     return call(vim_id, tenant_id, "subnets", "GET")
95
96
97 ######################################################################
98
99
100 def create_port(vim_id, tenant_id, data):
101     return call(vim_id, tenant_id, "ports", "POST", data)
102
103
104 def delete_port(vim_id, tenant_id, port_id):
105     return call(vim_id, tenant_id, "ports/%s" % port_id, "DELETE")
106
107
108 def get_port(vim_id, tenant_id, port_id):
109     return call(vim_id, tenant_id, "ports/%s" % port_id, "GET")
110
111
112 def list_port(vim_id, tenant_id):
113     return call(vim_id, tenant_id, "ports", "GET")
114
115
116 ######################################################################
117
118
119 def create_flavor(vim_id, tenant_id, data):
120     return call(vim_id, tenant_id, "flavors", "POST", data)
121
122
123 def delete_flavor(vim_id, tenant_id, flavor_id):
124     return call(vim_id, tenant_id, "flavors/%s" % flavor_id, "DELETE")
125
126
127 def get_flavor(vim_id, tenant_id, flavor_id):
128     return call(vim_id, tenant_id, "flavors/%s" % flavor_id, "GET")
129
130
131 def list_flavor(vim_id, tenant_id):
132     return call(vim_id, tenant_id, "flavors", "GET")
133
134
135 ######################################################################
136
137
138 def create_vm(vim_id, tenant_id, data):
139     return call(vim_id, tenant_id, "servers", "POST", data)
140
141
142 def delete_vm(vim_id, tenant_id, vm_id):
143     return call(vim_id, tenant_id, "servers/%s" % vm_id, "DELETE")
144
145
146 def get_vm(vim_id, tenant_id, vm_id):
147     return call(vim_id, tenant_id, "servers/%s" % vm_id, "GET")
148
149
150 def list_vm(vim_id, tenant_id):
151     return call(vim_id, tenant_id, "servers", "GET")
152
153
154 ######################################################################
155
156
157 def create_volume(vim_id, tenant_id, data):
158     return call(vim_id, tenant_id, "volumes", "POST", data)
159
160
161 def delete_volume(vim_id, tenant_id, volume_id):
162     return call(vim_id, tenant_id, "volumes/%s" % volume_id, "DELETE")
163
164
165 def get_volume(vim_id, tenant_id, volume_id):
166     return call(vim_id, tenant_id, "volumes/%s" % volume_id, "GET")
167
168
169 def list_volume(vim_id, tenant_id):
170     return call(vim_id, tenant_id, "volumes", "GET")
171
172
173 ######################################################################
174
175
176 def list_tenant(vim_id, tenant_name=""):
177     res = "tenants"
178     if tenant_name:
179         res = "%s?name=%s" % (res, tenant_name)
180     return call(vim_id, "", res, "GET")
181
182
183 ######################################################################
184
185
186 class MultiVimApi:
187
188     def login(self, connect_info):
189         self.vim_id = connect_info["vimid"]
190         self.tenant_name = connect_info["tenant"]
191         tenants = list_tenant(self.vim_id)
192         for tenant in tenants["tenants"]:
193             if self.tenant_name == tenant["name"]:
194                 self.tenant_id = tenant["id"]
195                 return [0, connect_info]
196         raise VimException(1, "Tenant(%s) not exist" % self.tenant_name)
197
198     def query_net(self, auth_info, net_id):
199         net = get_network(self.vim_id, self.tenant_id, net_id)
200         return [0, {
201             "id": net.get("id", ""),
202             "name": net.get("name", ""),
203             "status": net.get("status", ""),
204             "admin_state_up": net.get("admin_state_up", True),
205             "network_type": net.get("networkType", ""),
206             "physical_network": net.get("physicalNetwork", ""),
207             "segmentation_id": net.get("segmentationId", ""),
208             "tenant_id": self.tenant_id,
209             "tenant_name": self.tenant_name,
210             "subnets": net.get("subnets", []),
211             "shared": net.get("shared", True),
212             "router_external": net.get("routerExternal", "")
213         }]
214
215     def query_nets(self, auth_info):
216         nets = list_network(self.vim_id, self.tenant_id)
217         return [0, {"networks": [{
218             "id": net.get("id", ""),
219             "name": net.get("name", ""),
220             "status": net.get("status", ""),
221             "admin_state_up": net.get("admin_state_up", True),
222             "network_type": net.get("networkType", ""),
223             "physical_network": net.get("physicalNetwork", ""),
224             "segmentation_id": net.get("segmentationId", ""),
225             "tenant_id": self.tenant_id,
226             "tenant_name": self.tenant_name,
227             "subnets": net.get("subnets", []),
228             "shared": net.get("shared", True),
229             "router_external": net.get("routerExternal", "")
230         } for net in nets["networks"]]}]
231
232     def query_subnet(self, auth_info, subnet_id):
233         subnet_info = get_subnet(self.vim_id, self.tenant_id, subnet_id)
234         ret = [0, {}]
235         ret[1]["id"] = subnet_id
236         ret[1]["name"] = subnet_info.get("name", "")
237         ret[1]["status"] = ""
238         ret[1]["ip_version"] = subnet_info.get("ipVersion", 4)
239         ret[1]["cidr"] = subnet_info.get("cidr", "")
240         ret[1]["allocation_pools"] = subnet_info.get("allocationPools", [])
241         ret[1]["enable_dhcp"] = subnet_info.get("enableDhcp", False)
242         ret[1]["gateway_ip"] = subnet_info.get("gatewayIp", "")
243         ret[1]["host_routes"] = subnet_info.get("hostRoutes", [])
244         ret[1]["dns_nameservers"] = subnet_info.get("dnsNameservers", [])
245         return ret
246
247     def query_port(self, auth_info, port_id):
248         port_info = get_port(self.vim_id, self.tenant_id, port_id)
249         ret = [0, {}]
250         ret[1]["id"] = port_id
251         ret[1]["name"] = port_info.get("name", "")
252         ret[1]["network_id"] = port_info.get("networkId", "")
253         ret[1]["tenant_id"] = self.tenant_id,
254         ret[1]["ip"] = port_info.get("ip", "")
255         ret[1]["subnet_id"] = port_info.get("subnetId", "")
256         ret[1]["mac_address"] = port_info.get("macAddress", "")
257         ret[1]["status"] = port_info.get("status", "")
258         ret[1]["admin_state_up"] = port_info.get("admin_state_up", True)
259         ret[1]["device_id"] = port_info.get("device_id", "")
260         return ret
261
262     def create_port(self, auth_info, data):
263         return [0, data]
264
265     def delete_port(self, auth_info, port_id):
266         return [0, ""]
267
268     def create_image(self, auth_info, data):
269         image_data = {
270             "name": data["image_name"],
271             "imagePath": data["image_url"],
272             "imageType": data["image_type"],
273             "containerFormat": "bare",
274             "visibility": "public",
275             "properties": []
276         }
277         image = create_image(self.vim_id, self.tenant_id, image_data)
278         return [0, {
279             "id": image["id"],
280             "name": image["name"],
281             const.RES_TYPE_KEY: image["returnCode"]}
282         ]
283
284     def get_image(self, auth_info, image_id):
285         image = get_image(self.vim_id, self.tenant_id, image_id)
286         return [0, {
287             "id": image["id"],
288             "name": image["name"],
289             "size": image["size"],
290             "status": image["status"]}
291         ]
292
293     def get_images(self, auth_info):
294         images = list_image(self.vim_id, self.tenant_id)
295         return [0, {"image_list": [{
296             "id": img["id"],
297             "name": img["name"],
298             "size": img["size"],
299             "status": img["status"]
300         } for img in images["images"]]}]
301
302     def delete_image(self, auth_info, image_id):
303         return [0, ""]
304
305     def create_network(self, auth_info, data):
306         net_data = {
307             "name": data["network_name"],
308             "shared": True,
309             "networkType": data["network_type"]
310         }
311         if "physical_network" in data and data['physical_network']:
312             net_data["physicalNetwork"] = data['physical_network']
313         if "vlan_transparent" in data and data["vlan_transparent"]:
314             net_data["vlanTransparent"] = data["vlan_transparent"]
315         if "segmentation_id" in data and data['segmentation_id']:
316             net_data["segmentationId"] = data["segmentation_id"]
317         if "routerExternal" in data and data['routerExternal']:
318             net_data["routerExternal"] = data["routerExternal"]
319         net = create_network(self.vim_id, self.tenant_id, net_data)
320         network_id = net["id"]
321         ret_net = {
322             "status": net.get("status", ""),
323             "id": network_id,
324             "name": net.get("name", ""),
325             "provider:segmentation_id": net.get("segmentationId", ""),
326             "provider:network_type": net.get("networkType", ""),
327             const.RES_TYPE_KEY: net["returnCode"],
328             "subnet_list": []
329         }
330         if "subnet_list" in data and data["subnet_list"]:
331             subnet = data["subnet_list"][0]
332             subnet_data = {
333                 "networkId": network_id,
334                 "name": subnet["subnet_name"],
335                 "cidr": subnet["cidr"],
336                 "ipVersion": const.IPV4,
337                 "enableDhcp": False
338             }
339             if "ip_version" in subnet and subnet["ip_version"]:
340                 subnet_data["ipVersion"] = int(subnet["ip_version"])
341             if "enable_dhcp" in subnet and subnet["enable_dhcp"]:
342                 subnet_data["enableDhcp"] = int(subnet["enable_dhcp"]) == const.ENABLE_DHCP
343             if "gateway_ip" in subnet and subnet["gateway_ip"]:
344                 subnet_data["gatewayIp"] = subnet["gateway_ip"]
345             if "dns_nameservers" in subnet and subnet["dns_nameservers"]:
346                 subnet_data["dnsNameservers"] = subnet["dns_nameservers"]
347             if "allocation_pools" in subnet and subnet["allocation_pools"]:
348                 subnet_data["allocationPools"] = subnet["allocation_pools"]
349             if "host_routes" in subnet and subnet["host_routes"]:
350                 subnet_data["hostRoutes"] = subnet["host_routes"]
351             subnet_create = create_subnet(self.vim_id, self.tenant_id, subnet_data)
352             ret_net["subnet_list"].append({
353                 "id": subnet_create["id"],
354                 "name": subnet_create["name"],
355                 const.RES_TYPE_KEY: net["returnCode"]})
356         return [0, ret_net]
357
358     def delete_network(self, auth_info, network_id):
359         return delete_network(self.vim_id, self.tenant_id, network_id)
360
361     def delete_subnet(self, auth_info, subnet_id):
362         return delete_subnet(self.vim_id, self.tenant_id, subnet_id)