1 # -*- coding: utf-8 -*-
4 # Copyright 2020 Telefonica Investigacion y Desarrollo, S.A.U.
6 # Licensed under the Apache License, Version 2.0 (the "License"); you may
7 # not use this file except in compliance with the License. You may obtain
8 # a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15 # License for the specific language governing permissions and limitations
20 Implements a Dummy vim plugin.
24 from osm_ro_plugin
import vimconn
25 from uuid
import uuid4
26 from copy
import deepcopy
28 from random
import randrange
30 __author__
= "Alfonso Tierno"
31 __date__
= "2020-04-20"
34 class VimDummyConnector(vimconn
.VimConnector
):
35 """Dummy vim connector that does nothing
37 vm_ip: ip address to provide at VM creation. For some tests must be a valid reachable VM
38 ssh_key: private ssh key to use for inserting an authorized ssh key
68 self
.logger
= logging
.getLogger("ro.vim.dummy")
71 self
.logger
.setLevel(getattr(logging
, log_level
))
78 "vim_info": "{status: ACTIVE}",
86 "90681b39-dc09-49b7-ba2e-2c00c6b33b76": {
87 "id": "90681b39-dc09-49b7-ba2e-2c00c6b33b76",
89 "checksum": "ee1eca47dc88f4879d8a229cc70a07c6",
91 "83a39656-65db-47dc-af03-b55289115a53": {
94 "checksum": "443b7623e27ecf03dc9e01ee93f67afe",
96 "208314f2-8eb6-4101-965d-fe2ffbaedf3c": {
97 "id": "208314f2-8eb6-4101-965d-fe2ffbaedf3c",
98 "name": "ubuntu18.04",
99 "checksum": "b6fc7b9b91bca32e989e1edbcdeecb95",
101 "c03321f8-4b6e-4045-a309-1b3878bd32c1": {
102 "id": "c03321f8-4b6e-4045-a309-1b3878bd32c1",
103 "name": "ubuntu16.04",
104 "checksum": "8f08442faebad2d4a99fedb22fca11b5",
106 "4f6399a2-3554-457e-916e-ada01f8b950b": {
107 "id": "4f6399a2-3554-457e-916e-ada01f8b950b",
108 "name": "ubuntu1604",
109 "checksum": "8f08442faebad2d4a99fedb22fca11b5",
111 "59ac0b79-5c7d-4e83-b517-4c6c6a8ac1d3": {
112 "id": "59ac0b79-5c7d-4e83-b517-4c6c6a8ac1d3",
113 "name": "hackfest3-mgmt",
114 "checksum": "acec1e5d5ad7be9be7e6342a16bcf66a",
116 "f8818a03-f099-4c18-b1c7-26b1324203c1": {
117 "id": "f8818a03-f099-4c18-b1c7-26b1324203c1",
118 "name": "hackfest-pktgen",
119 "checksum": "f8818a03-f099-4c18-b1c7-26b1324203c1",
129 provider_network_profile
=None,
131 net_id
= str(uuid4())
133 "new network id={}, name={}, net_type={}, ip_profile={}, provider_network_profile={}".format(
134 net_id
, net_name
, net_type
, ip_profile
, provider_network_profile
140 "net_type": net_type
,
143 self
.nets
[net_id
] = net
147 def get_network_list(self
, filter_dict
=None):
150 for net_id
, net
in self
.nets
.items():
151 if filter_dict
and filter_dict
.get("name"):
152 if net
["name"] != filter_dict
.get("name"):
155 if filter_dict
and filter_dict
.get("id"):
156 if net_id
!= filter_dict
.get("id"):
161 # if no network is returned and search by name create a new one
162 if not nets
and filter_dict
and filter_dict
.get("name"):
163 net_id
, net
= self
.new_network(filter_dict
.get("name"), "mgmt")
168 def get_network(self
, net_id
):
169 if net_id
not in self
.nets
:
170 raise vimconn
.VimConnNotFoundException(
171 "network with id {} not found".format(net_id
)
174 return self
.nets
[net_id
]
176 def delete_network(self
, net_id
, created_items
=None):
177 if net_id
not in self
.nets
:
178 raise vimconn
.VimConnNotFoundException(
179 "network with id {} not found".format(net_id
)
183 "delete network id={}, created_items={}".format(net_id
, created_items
)
185 self
.nets
.pop(net_id
)
189 def refresh_nets_status(self
, net_list
):
192 for net_id
in net_list
:
193 if net_id
not in self
.nets
:
194 net
= {"status": "DELETED"}
196 net
= self
.nets
[net_id
].copy()
197 net
["vim_info"] = yaml
.dump(
198 {"status": "ACTIVE", "name": net
["name"]},
199 default_flow_style
=True,
207 def get_flavor(self
, flavor_id
):
208 if flavor_id
not in self
.flavors
:
209 raise vimconn
.VimConnNotFoundException(
210 "flavor with id {} not found".format(flavor_id
)
213 return self
.flavors
[flavor_id
]
215 def new_flavor(self
, flavor_data
):
216 flavor_id
= str(uuid4())
218 "new flavor id={}, flavor_data={}".format(flavor_id
, flavor_data
)
220 flavor
= deepcopy(flavor_data
)
221 flavor
["id"] = flavor_id
223 if "name" not in flavor
:
224 flavor
["name"] = flavor_id
226 self
.flavors
[flavor_id
] = flavor
230 def delete_flavor(self
, flavor_id
):
231 if flavor_id
not in self
.flavors
:
232 raise vimconn
.VimConnNotFoundException(
233 "flavor with id {} not found".format(flavor_id
)
236 self
.logger
.debug("delete flavor id={}".format(flavor_id
))
237 self
.flavors
.pop(flavor_id
)
241 def get_flavor_id_from_data(self
, flavor_dict
):
242 for flavor_id
, flavor_data
in self
.flavors
.items():
243 for k
in ("ram", "vcpus", "disk", "extended"):
244 if flavor_data
.get(k
) != flavor_dict
.get(k
):
249 raise vimconn
.VimConnNotFoundException(
250 "flavor with ram={} cpu={} disk={} {} not found".format(
252 flavor_dict
["vcpus"],
254 "and extended" if flavor_dict
.get("extended") else "",
258 def new_tenant(self
, tenant_name
, tenant_description
):
259 tenant_id
= str(uuid4())
261 "new tenant id={}, description={}".format(tenant_id
, tenant_description
)
265 "description": tenant_description
,
268 self
.tenants
[tenant_id
] = tenant
272 def delete_tenant(self
, tenant_id
):
273 if tenant_id
not in self
.tenants
:
274 raise vimconn
.VimConnNotFoundException(
275 "tenant with id {} not found".format(tenant_id
)
278 self
.tenants
.pop(tenant_id
)
279 self
.logger
.debug("delete tenant id={}".format(tenant_id
))
283 def get_tenant_list(self
, filter_dict
=None):
286 for tenant_id
, tenant
in self
.tenants
.items():
287 if filter_dict
and filter_dict
.get("name"):
288 if tenant
["name"] != filter_dict
.get("name"):
291 if filter_dict
and filter_dict
.get("id"):
292 if tenant_id
!= filter_dict
.get("id"):
295 tenants
.append(tenant
)
299 def new_image(self
, image_dict
):
300 image_id
= str(uuid4())
301 self
.logger
.debug("new image id={}, iamge_dict={}".format(image_id
, image_dict
))
302 image
= deepcopy(image_dict
)
303 image
["id"] = image_id
305 if "name" not in image
:
306 image
["id"] = image_id
308 self
.images
[image_id
] = image
312 def delete_image(self
, image_id
):
313 if image_id
not in self
.images
:
314 raise vimconn
.VimConnNotFoundException(
315 "image with id {} not found".format(image_id
)
318 self
.logger
.debug("delete image id={}".format(image_id
))
319 self
.images
.pop(image_id
)
323 def get_image_list(self
, filter_dict
=None):
325 for image_id
, image
in self
.images
.items():
326 if filter_dict
and filter_dict
.get("name"):
327 if image
["name"] != filter_dict
.get("name"):
330 if filter_dict
and filter_dict
.get("checksum"):
331 if image
["checksum"] != filter_dict
.get("checksum"):
334 if filter_dict
and filter_dict
.get("id"):
335 if image_id
!= filter_dict
.get("id"):
352 availability_zone_index
=None,
353 availability_zone_list
=None,
358 "new vm id={}, name={}, image_id={}, flavor_id={}, net_list={}, cloud_config={}".format(
359 vm_id
, name
, image_id
, flavor_id
, net_list
, cloud_config
363 for iface_index
, iface
in enumerate(net_list
):
364 iface
["vim_id"] = str(iface_index
)
366 "ip_address": iface
.get("ip_address")
367 or self
.config
.get("vm_ip")
369 "mac_address": iface
.get("mac_address")
370 or self
.config
.get("vm_mac")
371 or "00:11:22:33:44:55",
372 "vim_interface_id": str(iface_index
),
373 "vim_net_id": iface
["net_id"],
376 if iface
.get("type") in ("SR-IOV", "PCI-PASSTHROUGH") and self
.config
.get(
379 compute_index
= randrange(len(self
.config
["sdn-port-mapping"]))
380 port_index
= randrange(
381 len(self
.config
["sdn-port-mapping"][compute_index
]["ports"])
383 interface
["compute_node"] = self
.config
["sdn-port-mapping"][
386 interface
["pci"] = self
.config
["sdn-port-mapping"][compute_index
][
390 interfaces
.append(interface
)
396 "description": description
,
397 "interfaces": interfaces
,
398 "image_id": image_id
,
399 "flavor_id": flavor_id
,
402 if image_id
not in self
.images
:
404 "vm create, image_id '{}' not found. Skip".format(image_id
)
407 if flavor_id
not in self
.flavors
:
409 "vm create flavor_id '{}' not found. Skip".format(flavor_id
)
416 def get_vminstance(self
, vm_id
):
417 if vm_id
not in self
.vms
:
418 raise vimconn
.VimConnNotFoundException(
419 "vm with id {} not found".format(vm_id
)
422 return self
.vms
[vm_id
]
424 def delete_vminstance(self
, vm_id
, created_items
=None):
425 if vm_id
not in self
.vms
:
426 raise vimconn
.VimConnNotFoundException(
427 "vm with id {} not found".format(vm_id
)
432 "delete vm id={}, created_items={}".format(vm_id
, created_items
)
437 def refresh_vms_status(self
, vm_list
):
440 for vm_id
in vm_list
:
441 if vm_id
not in self
.vms
:
442 vm
= {"status": "DELETED"}
444 vm
= deepcopy(self
.vms
[vm_id
])
445 vm
["vim_info"] = yaml
.dump(
446 {"status": "ACTIVE", "name": vm
["name"]},
447 default_flow_style
=True,
455 def action_vminstance(self
, vm_id
, action_dict
, created_items
={}):
459 self
, ip_addr
=None, user
=None, key
=None, ro_key
=None, password
=None
461 if self
.config
.get("ssh_key"):
462 ro_key
= self
.config
.get("ssh_key")
464 return super().inject_user_key(
465 ip_addr
=ip_addr
, user
=user
, key
=key
, ro_key
=ro_key
, password
=password