1 # Copyright (c) 2015 SONATA-NFV and Paderborn University
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
16 # Neither the name of the SONATA-NFV, Paderborn University
17 # nor the names of its contributors may be used to endorse or promote
18 # products derived from this software without specific prior written
21 # This work has been performed in the framework of the SONATA project,
22 # funded by the European Commission under Grant number 671517 through
23 # the Horizon 2020 and 5G-PPP programmes. The authors would like to
24 # acknowledge the contributions of their colleagues of the SONATA
25 # partner consortium (www.sonata-nfv.eu).
26 from mininet
.link
import Link
28 from resources
.instance_flavor
import InstanceFlavor
29 from resources
.net
import Net
30 from resources
.port
import Port
31 from resources
.port_pair
import PortPair
32 from resources
.port_pair_group
import PortPairGroup
33 from resources
.flow_classifier
import FlowClassifier
34 from resources
.port_chain
import PortChain
35 from resources
.server
import Server
36 from resources
.image
import Image
38 from docker
import DockerClient
43 import ip_handler
as IP
47 LOG
= logging
.getLogger("api.openstack.compute")
50 class HeatApiStackInvalidException(Exception):
52 Exception thrown when a submitted stack is invalid.
55 def __init__(self
, value
):
59 return repr(self
.value
)
62 class OpenstackCompute(object):
64 This class is a datacenter specific compute object that tracks all containers that are running in a datacenter,
65 as well as networks and configured ports.
66 It has some stack dependet logic and can check if a received stack is valid.
68 It also handles start and stop of containers.
74 self
.computeUnits
= dict()
80 self
.port_pairs
= dict()
81 self
.port_pair_groups
= dict()
82 self
.flow_classifiers
= dict()
83 self
.port_chains
= dict()
84 self
.compute_nets
= dict()
85 self
.dcli
= DockerClient(base_url
='unix://var/run/docker.sock')
90 Updates the known images. Asks the docker daemon for a list of all known images and returns
93 :return: Returns the new image dictionary.
96 for image
in self
.dcli
.images
.list():
97 if len(image
.tags
) > 0:
99 if t
not in self
._images
:
100 self
._images
[t
] = Image(t
)
103 def add_stack(self
, stack
):
105 Adds a new stack to the compute node.
107 :param stack: Stack dictionary.
108 :type stack: :class:`heat.resources.stack`
110 if not self
.check_stack(stack
):
111 self
.clean_broken_stack(stack
)
112 raise HeatApiStackInvalidException(
113 "Stack did not pass validity checks")
114 self
.stacks
[stack
.id] = stack
116 def clean_broken_stack(self
, stack
):
117 for port
in stack
.ports
.values():
118 if port
.id in self
.ports
:
119 del self
.ports
[port
.id]
120 for server
in stack
.servers
.values():
121 if server
.id in self
.computeUnits
:
122 del self
.computeUnits
[server
.id]
123 for net
in stack
.nets
.values():
124 if net
.id in self
.nets
:
125 del self
.nets
[net
.id]
127 def check_stack(self
, stack
):
129 Checks all dependencies of all servers, ports and routers and their most important parameters.
131 :param stack: A reference of the stack that should be checked.
132 :type stack: :class:`heat.resources.stack`
133 :return: * *True*: If the stack is completely fine.
138 for server
in stack
.servers
.values():
139 for port_name
in server
.port_names
:
140 if port_name
not in stack
.ports
:
141 LOG
.warning("Server %s of stack %s has a port named %s that is not known." %
142 (server
.name
, stack
.stack_name
, port_name
))
143 everything_ok
= False
144 if server
.image
is None:
145 LOG
.warning("Server %s holds no image." % (server
.name
))
146 everything_ok
= False
147 if server
.command
is None:
148 LOG
.warning("Server %s holds no command." % (server
.name
))
149 everything_ok
= False
150 for port
in stack
.ports
.values():
151 if port
.net_name
not in stack
.nets
:
152 LOG
.warning("Port %s of stack %s has a network named %s that is not known." %
153 (port
.name
, stack
.stack_name
, port
.net_name
))
154 everything_ok
= False
155 if port
.intf_name
is None:
156 LOG
.warning("Port %s has no interface name." % (port
.name
))
157 everything_ok
= False
158 if port
.ip_address
is None:
159 LOG
.warning("Port %s has no IP address." % (port
.name
))
160 everything_ok
= False
161 for router
in stack
.routers
.values():
162 for subnet_name
in router
.subnet_names
:
164 for net
in stack
.nets
.values():
165 if net
.subnet_name
== subnet_name
:
169 LOG
.warning("Router %s of stack %s has a network named %s that is not known." %
170 (router
.name
, stack
.stack_name
, subnet_name
))
171 everything_ok
= False
174 def add_flavor(self
, name
, cpu
, memory
,
175 memory_unit
, storage
, storage_unit
):
177 Adds a flavor to the stack.
179 :param name: Specifies the name of the flavor.
184 :type memory: ``str``
186 :type memory_unit: ``str``
188 :type storage: ``str``
190 :type storage_unit: ``str``
192 flavor
= InstanceFlavor(
193 name
, cpu
, memory
, memory_unit
, storage
, storage_unit
)
194 self
.flavors
[flavor
.name
] = flavor
197 def deploy_stack(self
, stackid
):
199 Deploys the stack and starts the emulation.
201 :param stackid: An UUID str of the stack
202 :type stackid: ``str``
203 :return: * *False*: If the Datacenter is None
210 stack
= self
.stacks
[stackid
]
211 self
.update_compute_dicts(stack
)
213 # Create the networks first
214 for server
in stack
.servers
.values():
215 self
._start
_compute
(server
)
218 def delete_stack(self
, stack_id
):
220 Delete a stack and all its components.
222 :param stack_id: An UUID str of the stack
223 :type stack_id: ``str``
224 :return: * *False*: If the Datacenter is None
231 # Stop all servers and their links of this stack
232 for server
in self
.stacks
[stack_id
].servers
.values():
233 self
.stop_compute(server
)
234 self
.delete_server(server
)
235 for net
in self
.stacks
[stack_id
].nets
.values():
236 self
.delete_network(net
.id)
237 for port
in self
.stacks
[stack_id
].ports
.values():
238 self
.delete_port(port
.id)
240 del self
.stacks
[stack_id
]
243 def update_stack(self
, old_stack_id
, new_stack
):
245 Determines differences within the old and the new stack and deletes, create or changes only parts that
246 differ between the two stacks.
248 :param old_stack_id: The ID of the old stack.
249 :type old_stack_id: ``str``
250 :param new_stack: A reference of the new stack.
251 :type new_stack: :class:`heat.resources.stack`
252 :return: * *True*: if the old stack could be updated to the new stack without any error.
256 LOG
.debug("updating stack {} with new_stack {}".format(
257 old_stack_id
, new_stack
))
258 if old_stack_id
not in self
.stacks
:
260 old_stack
= self
.stacks
[old_stack_id
]
263 for server
in old_stack
.servers
.values():
264 if server
.name
in new_stack
.servers
:
265 new_stack
.servers
[server
.name
].id = server
.id
266 for net
in old_stack
.nets
.values():
267 if net
.name
in new_stack
.nets
:
268 new_stack
.nets
[net
.name
].id = net
.id
269 for subnet
in new_stack
.nets
.values():
270 if subnet
.subnet_name
== net
.subnet_name
:
271 subnet
.subnet_id
= net
.subnet_id
273 for port
in old_stack
.ports
.values():
274 if port
.name
in new_stack
.ports
:
275 new_stack
.ports
[port
.name
].id = port
.id
276 for router
in old_stack
.routers
.values():
277 if router
.name
in new_stack
.routers
:
278 new_stack
.routers
[router
.name
].id = router
.id
280 # Update the compute dicts to now contain the new_stack components
281 self
.update_compute_dicts(new_stack
)
283 self
.update_ip_addresses(old_stack
, new_stack
)
285 # Update all interface names - after each port has the correct UUID!!
286 for port
in new_stack
.ports
.values():
287 port
.create_intf_name()
289 if not self
.check_stack(new_stack
):
292 # Remove unnecessary networks
293 for net
in old_stack
.nets
.values():
294 if net
.name
not in new_stack
.nets
:
295 self
.delete_network(net
.id)
297 # Remove all unnecessary servers
298 for server
in old_stack
.servers
.values():
299 if server
.name
in new_stack
.servers
:
300 if not server
.compare_attributes(
301 new_stack
.servers
[server
.name
]):
302 self
.stop_compute(server
)
304 # Delete unused and changed links
305 for port_name
in server
.port_names
:
306 if port_name
in old_stack
.ports
and port_name
in new_stack
.ports
:
307 if not old_stack
.ports
.get(
308 port_name
) == new_stack
.ports
.get(port_name
):
309 my_links
= self
.dc
.net
.links
310 for link
in my_links
:
311 if str(link
.intf1
) == old_stack
.ports
[port_name
].intf_name
and \
312 str(link
.intf1
.ip
) == \
313 old_stack
.ports
[port_name
].ip_address
.split('/')[0]:
314 self
._remove
_link
(server
.name
, link
)
317 self
._add
_link
(server
.name
,
318 new_stack
.ports
[port_name
].ip_address
,
319 new_stack
.ports
[port_name
].intf_name
,
320 new_stack
.ports
[port_name
].net_name
)
323 my_links
= self
.dc
.net
.links
324 for link
in my_links
:
325 if str(link
.intf1
) == old_stack
.ports
[port_name
].intf_name
and \
326 str(link
.intf1
.ip
) == old_stack
.ports
[port_name
].ip_address
.split('/')[0]:
327 self
._remove
_link
(server
.name
, link
)
331 for port_name
in new_stack
.servers
[server
.name
].port_names
:
332 if port_name
not in server
.port_names
:
333 self
._add
_link
(server
.name
,
334 new_stack
.ports
[port_name
].ip_address
,
335 new_stack
.ports
[port_name
].intf_name
,
336 new_stack
.ports
[port_name
].net_name
)
338 self
.stop_compute(server
)
340 # Start all new servers
341 for server
in new_stack
.servers
.values():
342 if server
.name
not in self
.dc
.containers
:
343 self
._start
_compute
(server
)
345 server
.emulator_compute
= self
.dc
.containers
.get(server
.name
)
347 del self
.stacks
[old_stack_id
]
348 self
.stacks
[new_stack
.id] = new_stack
351 def update_ip_addresses(self
, old_stack
, new_stack
):
353 Updates the subnet and the port IP addresses - which should always be in this order!
355 :param old_stack: The currently running stack
356 :type old_stack: :class:`heat.resources.stack`
357 :param new_stack: The new created stack
358 :type new_stack: :class:`heat.resources.stack`
360 self
.update_subnet_cidr(old_stack
, new_stack
)
361 self
.update_port_addresses(old_stack
, new_stack
)
363 def update_port_addresses(self
, old_stack
, new_stack
):
365 Updates the port IP addresses. First resets all issued addresses. Then get all IP addresses from the old
366 stack and sets them to the same ports in the new stack. Finally all new or changed instances will get new
369 :param old_stack: The currently running stack
370 :type old_stack: :class:`heat.resources.stack`
371 :param new_stack: The new created stack
372 :type new_stack: :class:`heat.resources.stack`
374 for net
in new_stack
.nets
.values():
375 net
.reset_issued_ip_addresses()
377 for old_port
in old_stack
.ports
.values():
378 for port
in new_stack
.ports
.values():
379 if port
.compare_attributes(old_port
):
380 for net
in new_stack
.nets
.values():
381 if net
.name
== port
.net_name
:
382 if net
.assign_ip_address(
383 old_port
.ip_address
, port
.name
):
384 port
.ip_address
= old_port
.ip_address
385 port
.mac_address
= old_port
.mac_address
387 port
.ip_address
= net
.get_new_ip_address(
390 for port
in new_stack
.ports
.values():
391 for net
in new_stack
.nets
.values():
392 if port
.net_name
== net
.name
and not net
.is_my_ip(
393 port
.ip_address
, port
.name
):
394 port
.ip_address
= net
.get_new_ip_address(port
.name
)
396 def update_subnet_cidr(self
, old_stack
, new_stack
):
398 Updates the subnet IP addresses. If the new stack contains subnets from the old stack it will take those
399 IP addresses. Otherwise it will create new IP addresses for the subnet.
401 :param old_stack: The currently running stack
402 :type old_stack: :class:`heat.resources.stack`
403 :param new_stack: The new created stack
404 :type new_stack: :class:`heat.resources.stack`
406 for old_subnet
in old_stack
.nets
.values():
407 IP
.free_cidr(old_subnet
.get_cidr(), old_subnet
.subnet_id
)
409 for subnet
in new_stack
.nets
.values():
411 for old_subnet
in old_stack
.nets
.values():
412 if subnet
.subnet_name
== old_subnet
.subnet_name
:
413 if IP
.assign_cidr(old_subnet
.get_cidr(), subnet
.subnet_id
):
414 subnet
.set_cidr(old_subnet
.get_cidr())
416 for subnet
in new_stack
.nets
.values():
417 if IP
.is_cidr_issued(subnet
.get_cidr()):
420 cird
= IP
.get_new_cidr(subnet
.subnet_id
)
421 subnet
.set_cidr(cird
)
424 def update_compute_dicts(self
, stack
):
426 Update and add all stack components tho the compute dictionaries.
428 :param stack: A stack reference, to get all required components.
429 :type stack: :class:`heat.resources.stack`
431 for server
in stack
.servers
.values():
432 self
.computeUnits
[server
.id] = server
433 if isinstance(server
.flavor
, dict):
434 self
.add_flavor(server
.flavor
['flavorName'],
435 server
.flavor
['vcpu'],
436 server
.flavor
['ram'], 'MB',
437 server
.flavor
['storage'], 'GB')
438 server
.flavor
= server
.flavor
['flavorName']
439 for router
in stack
.routers
.values():
440 self
.routers
[router
.id] = router
441 for net
in stack
.nets
.values():
442 self
.nets
[net
.id] = net
443 for port
in stack
.ports
.values():
444 self
.ports
[port
.id] = port
446 def _start_compute(self
, server
):
448 Starts a new compute object (docker container) inside the emulator.
449 Should only be called by stack modifications and not directly.
451 :param server: Specifies the compute resource.
452 :type server: :class:`heat.resources.server`
454 LOG
.debug("Starting new compute resources %s" % server
.name
)
456 network_dict
= dict()
458 for port_name
in server
.port_names
:
459 network_dict
= dict()
460 port
= self
.find_port_by_name_or_id(port_name
)
462 network_dict
['id'] = port
.intf_name
463 network_dict
['ip'] = port
.ip_address
464 network_dict
[network_dict
['id']] = self
.find_network_by_name_or_id(
466 network
.append(network_dict
)
467 # default network dict
469 network_dict
['id'] = server
.name
+ "-eth0"
470 network_dict
[network_dict
['id']] = network_dict
['id']
471 network
.append(network_dict
)
473 self
.compute_nets
[server
.name
] = network
474 LOG
.debug("Network dict: {}".format(network
))
475 c
= self
.dc
.startCompute(server
.name
, image
=server
.image
, command
=server
.command
,
476 network
=network
, flavor_name
=server
.flavor
,
477 properties
=server
.properties
)
478 server
.emulator_compute
= c
480 for intf
in c
.intfs
.values():
481 for port_name
in server
.port_names
:
482 port
= self
.find_port_by_name_or_id(port_name
)
484 if intf
.name
== port
.intf_name
:
485 # wait up to one second for the intf to come up
486 self
.timeout_sleep(intf
.isUp
, 1)
487 if port
.mac_address
is not None:
488 intf
.setMAC(port
.mac_address
)
490 port
.mac_address
= intf
.MAC()
491 port
.assigned_container
= c
493 # Start the real emulator command now as specified in the dockerfile
494 config
= c
.dcinfo
.get("Config", dict())
495 env
= config
.get("Env", list())
497 var
, cmd
= map(str.strip
, map(str, env_var
.split('=', 1)))
498 if var
== "SON_EMU_CMD" or var
== "VIM_EMU_CMD":
499 LOG
.info("Executing script in '{}': {}={}"
500 .format(server
.name
, var
, cmd
))
501 # execute command in new thread to ensure that GK is not
503 t
= threading
.Thread(target
=c
.cmdPrint
, args
=(cmd
,))
506 break # only execute one command
508 def stop_compute(self
, server
):
510 Determines which links should be removed before removing the server itself.
512 :param server: The server that should be removed
513 :type server: ``heat.resources.server``
515 LOG
.debug("Stopping container %s with full name %s" %
516 (server
.name
, server
.full_name
))
518 for port_name
in server
.port_names
:
519 prt
= self
.find_port_by_name_or_id(port_name
)
521 link_names
.append(prt
.intf_name
)
522 my_links
= self
.dc
.net
.links
523 for link
in my_links
:
524 if str(link
.intf1
) in link_names
:
525 # Remove all self created links that connect the server to the
527 self
._remove
_link
(server
.name
, link
)
529 # Stop the server and the remaining connection to the datacenter switch
530 self
.dc
.stopCompute(server
.name
)
531 # Only now delete all its ports and the server itself
532 for port_name
in server
.port_names
:
533 self
.delete_port(port_name
)
534 self
.delete_server(server
)
536 def find_server_by_name_or_id(self
, name_or_id
):
538 Tries to find the server by ID and if this does not succeed then tries to find it via name.
540 :param name_or_id: UUID or name of the server.
541 :type name_or_id: ``str``
542 :return: Returns the server reference if it was found or None
543 :rtype: :class:`heat.resources.server`
545 if name_or_id
in self
.computeUnits
:
546 return self
.computeUnits
[name_or_id
]
548 if self
._shorten
_server
_name
(name_or_id
) in self
.computeUnits
:
549 return self
.computeUnits
[name_or_id
]
551 for server
in self
.computeUnits
.values():
552 if (server
.name
== name_or_id
or
553 server
.template_name
== name_or_id
or
554 server
.full_name
== name_or_id
):
556 if (server
.name
== self
._shorten
_server
_name
(name_or_id
) or
557 server
.template_name
== self
._shorten
_server
_name
(name_or_id
) or
558 server
.full_name
== self
._shorten
_server
_name
(name_or_id
)):
562 def create_server(self
, name
, stack_operation
=False):
564 Creates a server with the specified name. Raises an exception when a server with the given name already
567 :param name: Name of the new server.
569 :param stack_operation: Allows the heat parser to create modules without adapting the current emulation.
570 :type stack_operation: ``bool``
571 :return: Returns the created server.
572 :rtype: :class:`heat.resources.server`
574 if self
.find_server_by_name_or_id(
575 name
) is not None and not stack_operation
:
576 raise Exception("Server with name %s already exists." % name
)
577 safe_name
= self
._shorten
_server
_name
(name
)
578 server
= Server(safe_name
)
579 server
.id = str(uuid
.uuid4())
580 if not stack_operation
:
581 self
.computeUnits
[server
.id] = server
584 def _shorten_server_name(self
, name
, char_limit
=9):
586 Docker does not like too long instance names.
587 This function provides a shorter name if needed
589 if len(name
) > char_limit
:
590 LOG
.info("Long server name: {}".format(name
))
591 # construct a short name
592 h
= hashlib
.sha224(name
).hexdigest()
594 LOG
.info("Short server name: {}".format(h
))
597 def delete_server(self
, server
):
599 Deletes the given server from the stack dictionary and the computeUnits dictionary.
601 :param server: Reference of the server that should be deleted.
602 :type server: :class:`heat.resources.server`
603 :return: * *False*: If the server name is not in the correct format ('datacentername_stackname_servername') \
604 or when no stack with the correct stackname was found.
610 name_parts
= server
.name
.split('_')
611 if len(name_parts
) > 1:
612 for stack
in self
.stacks
.values():
613 if stack
.stack_name
== name_parts
[1]:
614 stack
.servers
.pop(server
.id, None)
615 if self
.computeUnits
.pop(server
.id, None) is None:
619 def find_network_by_name_or_id(self
, name_or_id
):
621 Tries to find the network by ID and if this does not succeed then tries to find it via name.
623 :param name_or_id: UUID or name of the network.
624 :type name_or_id: ``str``
625 :return: Returns the network reference if it was found or None
626 :rtype: :class:`heat.resources.net`
628 if name_or_id
in self
.nets
:
629 return self
.nets
[name_or_id
]
630 for net
in self
.nets
.values():
631 if net
.name
== name_or_id
:
633 LOG
.warning("Could not find net '{}' in {} or {}"
636 [n
.name
for n
in self
.nets
.values()]))
639 def create_network(self
, name
, stack_operation
=False):
641 Creates a new network with the given name. Raises an exception when a network with the given name already
644 :param name: Name of the new network.
646 :param stack_operation: Allows the heat parser to create modules without adapting the current emulation.
647 :type stack_operation: ``bool``
648 :return: :class:`heat.resources.net`
650 LOG
.debug("Creating network with name %s" % name
)
651 if self
.find_network_by_name_or_id(
652 name
) is not None and not stack_operation
:
654 "Creating network with name %s failed, as it already exists" % name
)
655 raise Exception("Network with name %s already exists." % name
)
657 network
.id = str(uuid
.uuid4())
658 if not stack_operation
:
659 self
.nets
[network
.id] = network
662 def delete_network(self
, name_or_id
):
664 Deletes the given network.
666 :param name_or_id: Name or UUID of the network.
667 :type name_or_id: ``str``
669 net
= self
.find_network_by_name_or_id(name_or_id
)
672 "Network with name or id %s does not exists." % name_or_id
)
674 for stack
in self
.stacks
.values():
675 stack
.nets
.pop(net
.name
, None)
677 self
.nets
.pop(net
.id, None)
679 def create_port(self
, name
, stack_operation
=False):
681 Creates a new port with the given name. Raises an exception when a port with the given name already
684 :param name: Name of the new port.
686 :param stack_operation: Allows the heat parser to create modules without adapting the current emulation.
687 :type stack_operation: ``bool``
688 :return: Returns the created port.
689 :rtype: :class:`heat.resources.port`
692 if not stack_operation
:
693 self
.ports
[port
.id] = port
694 port
.create_intf_name()
697 def find_port_by_name_or_id(self
, name_or_id
):
699 Tries to find the port by ID and if this does not succeed then tries to find it via name.
701 :param name_or_id: UUID or name of the network.
702 :type name_or_id: ``str``
703 :return: Returns the port reference if it was found or None
704 :rtype: :class:`heat.resources.port`
707 if name_or_id
in self
.ports
:
708 return self
.ports
[name_or_id
]
710 matching_ports
= filter(
711 lambda port
: port
.name
== name_or_id
or port
.template_name
== name_or_id
,
714 matching_ports_count
= len(matching_ports
)
715 if matching_ports_count
== 1:
716 return matching_ports
[0]
717 if matching_ports_count
> 1:
718 raise RuntimeError("Ambiguous port name %s" % name_or_id
)
721 def delete_port(self
, name_or_id
):
723 Deletes the given port. Raises an exception when the port was not found!
725 :param name_or_id: UUID or name of the port.
726 :type name_or_id: ``str``
728 port
= self
.find_port_by_name_or_id(name_or_id
)
731 "Port with name or id %s does not exist. Can't delete it." % name_or_id
)
734 my_links
= self
.dc
.net
.links
735 for link
in my_links
:
736 if str(link
.intf1
) == port
.intf_name
:
737 self
._remove
_link
(link
.intf1
.node
.name
, link
)
740 self
.ports
.pop(port
.id, None)
741 for stack
in self
.stacks
.values():
742 stack
.ports
.pop(port
.name
, None)
744 def create_port_pair(self
, name
, stack_operation
=False):
746 Creates a new port pair with the given name. Raises an exception when a port pair with the given name already
749 :param name: Name of the new port pair.
751 :param stack_operation: Allows the heat parser to create modules without adapting the current emulation.
752 :type stack_operation: ``bool``
753 :return: Returns the created port pair.
754 :rtype: :class:`openstack.resources.port_pair`
756 port_pair
= self
.find_port_pair_by_name_or_id(name
)
757 if port_pair
is not None and not stack_operation
:
759 "Creating port pair with name %s failed, as it already exists" % name
)
760 raise Exception("Port pair with name %s already exists." % name
)
761 logging
.debug("Creating port pair with name %s" % name
)
762 port_pair
= PortPair(name
)
763 if not stack_operation
:
764 self
.port_pairs
[port_pair
.id] = port_pair
767 def find_port_pair_by_name_or_id(self
, name_or_id
):
769 Tries to find the port pair by ID and if this does not succeed then tries to find it via name.
771 :param name_or_id: UUID or name of the port pair.
772 :type name_or_id: ``str``
773 :return: Returns the port pair reference if it was found or None
774 :rtype: :class:`openstack.resources.port_pair`
776 if name_or_id
in self
.port_pairs
:
777 return self
.port_pairs
[name_or_id
]
778 for port_pair
in self
.port_pairs
.values():
779 if port_pair
.name
== name_or_id
:
784 def delete_port_pair(self
, name_or_id
):
786 Deletes the given port pair. Raises an exception when the port pair was not found!
788 :param name_or_id: UUID or name of the port pair.
789 :type name_or_id: ``str``
791 port_pair
= self
.find_port_pair_by_name_or_id(name_or_id
)
792 if port_pair
is None:
794 "Port pair with name or id %s does not exists." % name_or_id
)
796 self
.port_pairs
.pop(port_pair
.id, None)
798 def create_port_pair_group(self
, name
, stack_operation
=False):
800 Creates a new port pair group with the given name. Raises an exception when a port pair group
801 with the given name already exists!
803 :param name: Name of the new port pair group.
805 :param stack_operation: Allows the heat parser to create modules without adapting the current emulation.
806 :type stack_operation: ``bool``
807 :return: Returns the created port pair group .
808 :rtype: :class:`openstack.resources.port_pair_group`
810 port_pair_group
= self
.find_port_pair_group_by_name_or_id(name
)
811 if port_pair_group
is not None and not stack_operation
:
813 "Creating port pair group with name %s failed, as it already exists" % name
)
815 "Port pair group with name %s already exists." % name
)
816 logging
.debug("Creating port pair group with name %s" % name
)
817 port_pair_group
= PortPairGroup(name
)
818 if not stack_operation
:
819 self
.port_pair_groups
[port_pair_group
.id] = port_pair_group
820 return port_pair_group
822 def find_port_pair_group_by_name_or_id(self
, name_or_id
):
824 Tries to find the port pair group by ID and if this does not succeed then tries to find it via name.
826 :param name_or_id: UUID or name of the port pair group.
827 :type name_or_id: ``str``
828 :return: Returns the port pair group reference if it was found or None
829 :rtype: :class:`openstack.resources.port_pair_group`
831 if name_or_id
in self
.port_pair_groups
:
832 return self
.port_pair_groups
[name_or_id
]
833 for port_pair_group
in self
.port_pair_groups
.values():
834 if port_pair_group
.name
== name_or_id
:
835 return port_pair_group
839 def delete_port_pair_group(self
, name_or_id
):
841 Deletes the given port pair group. Raises an exception when the port pair group was not found!
843 :param name_or_id: UUID or name of the port pair group.
844 :type name_or_id: ``str``
846 port_pair_group
= self
.find_port_pair_group_by_name_or_id(name_or_id
)
847 if port_pair_group
is None:
849 "Port pair with name or id %s does not exists." % name_or_id
)
851 self
.port_pair_groups
.pop(port_pair_group
.id, None)
853 def create_port_chain(self
, name
, stack_operation
=False):
855 Creates a new port chain with the given name. Raises an exception when a port chain with the given name already
858 :param name: Name of the new port chain
860 :param stack_operation: Allows the heat parser to create modules without adapting the current emulation.
861 :type stack_operation: ``bool``
862 :return: Returns the created port chain.
863 :rtype: :class:`openstack.resources.port_chain.PortChain`
865 port_chain
= self
.find_port_chain_by_name_or_id(name
)
866 if port_chain
is not None and not stack_operation
:
868 "Creating port chain with name %s failed, as it already exists" % name
)
869 raise Exception("Port chain with name %s already exists." % name
)
870 logging
.debug("Creating port chain with name %s" % name
)
871 port_chain
= PortChain(name
)
872 if not stack_operation
:
873 self
.port_chains
[port_chain
.id] = port_chain
876 def find_port_chain_by_name_or_id(self
, name_or_id
):
878 Tries to find the port chain by ID and if this does not succeed then tries to find it via name.
880 :param name_or_id: UUID or name of the port chain.
881 :type name_or_id: ``str``
882 :return: Returns the port chain reference if it was found or None
883 :rtype: :class:`openstack.resources.port_chain.PortChain`
885 if name_or_id
in self
.port_chains
:
886 return self
.port_chains
[name_or_id
]
887 for port_chain
in self
.port_chains
.values():
888 if port_chain
.name
== name_or_id
:
892 def delete_port_chain(self
, name_or_id
):
894 Deletes the given port chain. Raises an exception when the port chain was not found!
896 :param name_or_id: UUID or name of the port chain.
897 :type name_or_id: ``str``
899 port_chain
= self
.find_port_chain_by_name_or_id(name_or_id
)
900 port_chain
.uninstall(self
)
901 if port_chain
is None:
903 "Port chain with name or id %s does not exists." % name_or_id
)
905 self
.port_chains
.pop(port_chain
.id, None)
907 def create_flow_classifier(self
, name
, stack_operation
=False):
909 Creates a new flow classifier with the given name. Raises an exception when a flow classifier with the given name already
912 :param name: Name of the new flow classifier.
914 :param stack_operation: Allows the heat parser to create modules without adapting the current emulation.
915 :type stack_operation: ``bool``
916 :return: Returns the created flow classifier.
917 :rtype: :class:`openstack.resources.flow_classifier`
919 flow_classifier
= self
.find_flow_classifier_by_name_or_id(name
)
920 if flow_classifier
is not None and not stack_operation
:
922 "Creating flow classifier with name %s failed, as it already exists" % name
)
924 "Flow classifier with name %s already exists." % name
)
925 logging
.debug("Creating flow classifier with name %s" % name
)
926 flow_classifier
= FlowClassifier(name
)
927 if not stack_operation
:
928 self
.flow_classifiers
[flow_classifier
.id] = flow_classifier
929 return flow_classifier
931 def find_flow_classifier_by_name_or_id(self
, name_or_id
):
933 Tries to find the flow classifier by ID and if this does not succeed then tries to find it via name.
935 :param name_or_id: UUID or name of the flow classifier.
936 :type name_or_id: ``str``
937 :return: Returns the flow classifier reference if it was found or None
938 :rtype: :class:`openstack.resources.flow_classifier`
940 if name_or_id
in self
.flow_classifiers
:
941 return self
.flow_classifiers
[name_or_id
]
942 for flow_classifier
in self
.flow_classifiers
.values():
943 if flow_classifier
.name
== name_or_id
:
944 return flow_classifier
948 def delete_flow_classifier(self
, name_or_id
):
950 Deletes the given flow classifier. Raises an exception when the flow classifier was not found!
952 :param name_or_id: UUID or name of the flow classifier.
953 :type name_or_id: ``str``
955 flow_classifier
= self
.find_flow_classifier_by_name_or_id(name_or_id
)
956 if flow_classifier
is None:
958 "Flow classifier with name or id %s does not exists." % name_or_id
)
960 self
.flow_classifiers
.pop(flow_classifier
.id, None)
962 def _add_link(self
, node_name
, ip_address
, link_name
, net_name
):
964 Adds a new link between datacenter switch and the node with the given name.
966 :param node_name: Name of the required node.
967 :type node_name: ``str``
968 :param ip_address: IP-Address of the node.
969 :type ip_address: ``str``
970 :param link_name: Link name.
971 :type link_name: ``str``
972 :param net_name: Network name.
973 :type net_name: ``str``
975 node
= self
.dc
.net
.get(node_name
)
976 params
= {'params1': {'ip': ip_address
,
978 link_name
: net_name
},
979 'intfName1': link_name
,
981 link
= self
.dc
.net
.addLink(node
, self
.dc
.switch
, **params
)
982 OpenstackCompute
.timeout_sleep(link
.intf1
.isUp
, 1)
984 def _remove_link(self
, server_name
, link
):
986 Removes a link between server and datacenter switch.
988 :param server_name: Specifies the server where the link starts.
989 :type server_name: ``str``
990 :param link: A reference of the link which should be removed.
991 :type link: :class:`mininet.link`
993 self
.dc
.switch
.detach(link
.intf2
)
994 del self
.dc
.switch
.intfs
[self
.dc
.switch
.ports
[link
.intf2
]]
995 del self
.dc
.switch
.ports
[link
.intf2
]
996 del self
.dc
.switch
.nameToIntf
[link
.intf2
.name
]
997 self
.dc
.net
.removeLink(link
=link
)
998 for intf_key
in self
.dc
.net
[server_name
].intfs
.keys():
999 if self
.dc
.net
[server_name
].intfs
[intf_key
].link
== link
:
1000 self
.dc
.net
[server_name
].intfs
[intf_key
].delete()
1001 del self
.dc
.net
[server_name
].intfs
[intf_key
]
1004 def timeout_sleep(function
, max_sleep
):
1006 This function will execute a function all 0.1 seconds until it successfully returns.
1007 Will return after `max_sleep` seconds if not successful.
1009 :param function: The function to execute. Should return true if done.
1010 :type function: ``function``
1011 :param max_sleep: Max seconds to sleep. 1 equals 1 second.
1012 :type max_sleep: ``float``
1014 current_time
= time
.time()
1015 stop_time
= current_time
+ max_sleep
1016 while not function() and current_time
< stop_time
:
1017 current_time
= time
.time()