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"):
163 def get_network(self
, net_id
):
164 if net_id
not in self
.nets
:
165 raise vimconn
.VimConnNotFoundException(
166 "network with id {} not found".format(net_id
)
169 return self
.nets
[net_id
]
171 def delete_network(self
, net_id
, created_items
=None):
172 if net_id
not in self
.nets
:
173 raise vimconn
.VimConnNotFoundException(
174 "network with id {} not found".format(net_id
)
178 "delete network id={}, created_items={}".format(net_id
, created_items
)
180 self
.nets
.pop(net_id
)
184 def refresh_nets_status(self
, net_list
):
187 for net_id
in net_list
:
188 if net_id
not in self
.nets
:
189 net
= {"status": "DELETED"}
191 net
= self
.nets
[net_id
].copy()
192 net
["vim_info"] = yaml
.dump(
193 {"status": "ACTIVE", "name": net
["name"]},
194 default_flow_style
=True,
202 def get_flavor(self
, flavor_id
):
203 if flavor_id
not in self
.flavors
:
204 raise vimconn
.VimConnNotFoundException(
205 "flavor with id {} not found".format(flavor_id
)
208 return self
.flavors
[flavor_id
]
210 def new_flavor(self
, flavor_data
):
211 flavor_id
= str(uuid4())
213 "new flavor id={}, flavor_data={}".format(flavor_id
, flavor_data
)
215 flavor
= deepcopy(flavor_data
)
216 flavor
["id"] = flavor_id
218 if "name" not in flavor
:
219 flavor
["name"] = flavor_id
221 self
.flavors
[flavor_id
] = flavor
225 def delete_flavor(self
, flavor_id
):
226 if flavor_id
not in self
.flavors
:
227 raise vimconn
.VimConnNotFoundException(
228 "flavor with id {} not found".format(flavor_id
)
231 self
.logger
.debug("delete flavor id={}".format(flavor_id
))
232 self
.flavors
.pop(flavor_id
)
236 def get_flavor_id_from_data(self
, flavor_dict
):
237 for flavor_id
, flavor_data
in self
.flavors
.items():
238 for k
in ("ram", "vcpus", "disk", "extended"):
239 if flavor_data
.get(k
) != flavor_dict
.get(k
):
244 raise vimconn
.VimConnNotFoundException(
245 "flavor with ram={} cpu={} disk={} {} not found".format(
247 flavor_dict
["vcpus"],
249 "and extended" if flavor_dict
.get("extended") else "",
253 def new_tenant(self
, tenant_name
, tenant_description
):
254 tenant_id
= str(uuid4())
256 "new tenant id={}, description={}".format(tenant_id
, tenant_description
)
260 "description": tenant_description
,
263 self
.tenants
[tenant_id
] = tenant
267 def delete_tenant(self
, tenant_id
):
268 if tenant_id
not in self
.tenants
:
269 raise vimconn
.VimConnNotFoundException(
270 "tenant with id {} not found".format(tenant_id
)
273 self
.tenants
.pop(tenant_id
)
274 self
.logger
.debug("delete tenant id={}".format(tenant_id
))
278 def get_tenant_list(self
, filter_dict
=None):
281 for tenant_id
, tenant
in self
.tenants
.items():
282 if filter_dict
and filter_dict
.get("name"):
283 if tenant
["name"] != filter_dict
.get("name"):
286 if filter_dict
and filter_dict
.get("id"):
287 if tenant_id
!= filter_dict
.get("id"):
290 tenants
.append(tenant
)
294 def new_image(self
, image_dict
):
295 image_id
= str(uuid4())
296 self
.logger
.debug("new image id={}, iamge_dict={}".format(image_id
, image_dict
))
297 image
= deepcopy(image_dict
)
298 image
["id"] = image_id
300 if "name" not in image
:
301 image
["id"] = image_id
303 self
.images
[image_id
] = image
307 def delete_image(self
, image_id
):
308 if image_id
not in self
.images
:
309 raise vimconn
.VimConnNotFoundException(
310 "image with id {} not found".format(image_id
)
313 self
.logger
.debug("delete image id={}".format(image_id
))
314 self
.images
.pop(image_id
)
318 def get_image_list(self
, filter_dict
=None):
320 for image_id
, image
in self
.images
.items():
321 if filter_dict
and filter_dict
.get("name"):
322 if image
["name"] != filter_dict
.get("name"):
325 if filter_dict
and filter_dict
.get("checksum"):
326 if image
["checksum"] != filter_dict
.get("checksum"):
329 if filter_dict
and filter_dict
.get("id"):
330 if image_id
!= filter_dict
.get("id"):
347 availability_zone_index
=None,
348 availability_zone_list
=None,
353 "new vm id={}, name={}, image_id={}, flavor_id={}, net_list={}, cloud_config={}".format(
354 vm_id
, name
, image_id
, flavor_id
, net_list
, cloud_config
358 for iface_index
, iface
in enumerate(net_list
):
359 iface
["vim_id"] = str(iface_index
)
361 "ip_address": iface
.get("ip_address")
362 or self
.config
.get("vm_ip")
364 "mac_address": iface
.get("mac_address")
365 or self
.config
.get("vm_mac")
366 or "00:11:22:33:44:55",
367 "vim_interface_id": str(iface_index
),
368 "vim_net_id": iface
["net_id"],
371 if iface
.get("type") in ("SR-IOV", "PCI-PASSTHROUGH") and self
.config
.get(
374 compute_index
= randrange(len(self
.config
["sdn-port-mapping"]))
375 port_index
= randrange(
376 len(self
.config
["sdn-port-mapping"][compute_index
]["ports"])
378 interface
["compute_node"] = self
.config
["sdn-port-mapping"][
381 interface
["pci"] = self
.config
["sdn-port-mapping"][compute_index
][
385 interfaces
.append(interface
)
391 "description": description
,
392 "interfaces": interfaces
,
393 "image_id": image_id
,
394 "flavor_id": flavor_id
,
397 if image_id
not in self
.images
:
399 "vm create, image_id '{}' not found. Skip".format(image_id
)
402 if flavor_id
not in self
.flavors
:
404 "vm create flavor_id '{}' not found. Skip".format(flavor_id
)
411 def get_vminstance(self
, vm_id
):
412 if vm_id
not in self
.vms
:
413 raise vimconn
.VimConnNotFoundException(
414 "vm with id {} not found".format(vm_id
)
417 return self
.vms
[vm_id
]
419 def delete_vminstance(self
, vm_id
, created_items
=None):
420 if vm_id
not in self
.vms
:
421 raise vimconn
.VimConnNotFoundException(
422 "vm with id {} not found".format(vm_id
)
427 "delete vm id={}, created_items={}".format(vm_id
, created_items
)
432 def refresh_vms_status(self
, vm_list
):
435 for vm_id
in vm_list
:
436 if vm_id
not in self
.vms
:
437 vm
= {"status": "DELETED"}
439 vm
= deepcopy(self
.vms
[vm_id
])
440 vm
["vim_info"] = yaml
.dump(
441 {"status": "ACTIVE", "name": vm
["name"]},
442 default_flow_style
=True,
450 def action_vminstance(self
, vm_id
, action_dict
, created_items
={}):
454 self
, ip_addr
=None, user
=None, key
=None, ro_key
=None, password
=None
456 if self
.config
.get("ssh_key"):
457 ro_key
= self
.config
.get("ssh_key")
459 return super().inject_user_key(
460 ip_addr
=ip_addr
, user
=user
, key
=key
, ro_key
=ro_key
, password
=password