| peusterm | 72f0988 | 2018-05-15 17:10:27 +0200 | [diff] [blame] | 1 | # Copyright (c) 2015 SONATA-NFV and Paderborn University |
| 2 | # ALL RIGHTS RESERVED. |
| 3 | # |
| 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 |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 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. |
| 15 | # |
| 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 |
| 19 | # permission. |
| 20 | # |
| 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). |
| peusterm | 0019978 | 2017-05-17 08:48:12 +0200 | [diff] [blame] | 26 | from mininet.link import Link |
| splietker | 7b38ee1 | 2017-06-28 17:24:01 +0200 | [diff] [blame] | 27 | |
| peusterm | 72f0988 | 2018-05-15 17:10:27 +0200 | [diff] [blame] | 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 |
| 37 | |
| peusterm | 0019978 | 2017-05-17 08:48:12 +0200 | [diff] [blame] | 38 | from docker import DockerClient |
| 39 | import logging |
| 40 | import threading |
| 41 | import uuid |
| 42 | import time |
| 43 | import ip_handler as IP |
| peusterm | 9219060 | 2017-12-19 15:18:11 +0100 | [diff] [blame] | 44 | import hashlib |
| peusterm | 0019978 | 2017-05-17 08:48:12 +0200 | [diff] [blame] | 45 | |
| 46 | |
| peusterm | bbf4f74 | 2017-06-19 11:22:11 +0200 | [diff] [blame] | 47 | LOG = logging.getLogger("api.openstack.compute") |
| 48 | |
| 49 | |
| peusterm | 0019978 | 2017-05-17 08:48:12 +0200 | [diff] [blame] | 50 | class HeatApiStackInvalidException(Exception): |
| 51 | """ |
| 52 | Exception thrown when a submitted stack is invalid. |
| 53 | """ |
| 54 | |
| 55 | def __init__(self, value): |
| 56 | self.value = value |
| 57 | |
| 58 | def __str__(self): |
| 59 | return repr(self.value) |
| 60 | |
| 61 | |
| 62 | class OpenstackCompute(object): |
| 63 | """ |
| 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. |
| 67 | |
| 68 | It also handles start and stop of containers. |
| 69 | """ |
| 70 | |
| 71 | def __init__(self): |
| 72 | self.dc = None |
| 73 | self.stacks = dict() |
| 74 | self.computeUnits = dict() |
| 75 | self.routers = dict() |
| 76 | self.flavors = dict() |
| 77 | self._images = dict() |
| 78 | self.nets = dict() |
| 79 | self.ports = dict() |
| splietker | 7b38ee1 | 2017-06-28 17:24:01 +0200 | [diff] [blame] | 80 | self.port_pairs = dict() |
| 81 | self.port_pair_groups = dict() |
| 82 | self.flow_classifiers = dict() |
| 83 | self.port_chains = dict() |
| peusterm | 0019978 | 2017-05-17 08:48:12 +0200 | [diff] [blame] | 84 | self.compute_nets = dict() |
| 85 | self.dcli = DockerClient(base_url='unix://var/run/docker.sock') |
| 86 | |
| 87 | @property |
| 88 | def images(self): |
| 89 | """ |
| 90 | Updates the known images. Asks the docker daemon for a list of all known images and returns |
| 91 | the new dictionary. |
| 92 | |
| 93 | :return: Returns the new image dictionary. |
| 94 | :rtype: ``dict`` |
| 95 | """ |
| 96 | for image in self.dcli.images.list(): |
| 97 | if len(image.tags) > 0: |
| 98 | for t in image.tags: |
| peusterm | 0019978 | 2017-05-17 08:48:12 +0200 | [diff] [blame] | 99 | if t not in self._images: |
| 100 | self._images[t] = Image(t) |
| 101 | return self._images |
| 102 | |
| 103 | def add_stack(self, stack): |
| 104 | """ |
| 105 | Adds a new stack to the compute node. |
| 106 | |
| 107 | :param stack: Stack dictionary. |
| 108 | :type stack: :class:`heat.resources.stack` |
| 109 | """ |
| 110 | if not self.check_stack(stack): |
| 111 | self.clean_broken_stack(stack) |
| peusterm | 72f0988 | 2018-05-15 17:10:27 +0200 | [diff] [blame] | 112 | raise HeatApiStackInvalidException( |
| 113 | "Stack did not pass validity checks") |
| peusterm | 0019978 | 2017-05-17 08:48:12 +0200 | [diff] [blame] | 114 | self.stacks[stack.id] = stack |
| 115 | |
| 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] |
| 126 | |
| 127 | def check_stack(self, stack): |
| 128 | """ |
| 129 | Checks all dependencies of all servers, ports and routers and their most important parameters. |
| 130 | |
| 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. |
| 134 | * *False*: Else |
| 135 | :rtype: ``bool`` |
| 136 | """ |
| 137 | everything_ok = True |
| 138 | for server in stack.servers.values(): |
| 139 | for port_name in server.port_names: |
| 140 | if port_name not in stack.ports: |
| peusterm | bbf4f74 | 2017-06-19 11:22:11 +0200 | [diff] [blame] | 141 | LOG.warning("Server %s of stack %s has a port named %s that is not known." % |
| peusterm | 72f0988 | 2018-05-15 17:10:27 +0200 | [diff] [blame] | 142 | (server.name, stack.stack_name, port_name)) |
| peusterm | 0019978 | 2017-05-17 08:48:12 +0200 | [diff] [blame] | 143 | everything_ok = False |
| 144 | if server.image is None: |
| peusterm | bbf4f74 | 2017-06-19 11:22:11 +0200 | [diff] [blame] | 145 | LOG.warning("Server %s holds no image." % (server.name)) |
| peusterm | 0019978 | 2017-05-17 08:48:12 +0200 | [diff] [blame] | 146 | everything_ok = False |
| 147 | if server.command is None: |
| peusterm | bbf4f74 | 2017-06-19 11:22:11 +0200 | [diff] [blame] | 148 | LOG.warning("Server %s holds no command." % (server.name)) |
| peusterm | 0019978 | 2017-05-17 08:48:12 +0200 | [diff] [blame] | 149 | everything_ok = False |
| 150 | for port in stack.ports.values(): |
| 151 | if port.net_name not in stack.nets: |
| peusterm | bbf4f74 | 2017-06-19 11:22:11 +0200 | [diff] [blame] | 152 | LOG.warning("Port %s of stack %s has a network named %s that is not known." % |
| peusterm | 72f0988 | 2018-05-15 17:10:27 +0200 | [diff] [blame] | 153 | (port.name, stack.stack_name, port.net_name)) |
| peusterm | 0019978 | 2017-05-17 08:48:12 +0200 | [diff] [blame] | 154 | everything_ok = False |
| 155 | if port.intf_name is None: |
| peusterm | bbf4f74 | 2017-06-19 11:22:11 +0200 | [diff] [blame] | 156 | LOG.warning("Port %s has no interface name." % (port.name)) |
| peusterm | 0019978 | 2017-05-17 08:48:12 +0200 | [diff] [blame] | 157 | everything_ok = False |
| 158 | if port.ip_address is None: |
| peusterm | bbf4f74 | 2017-06-19 11:22:11 +0200 | [diff] [blame] | 159 | LOG.warning("Port %s has no IP address." % (port.name)) |
| peusterm | 0019978 | 2017-05-17 08:48:12 +0200 | [diff] [blame] | 160 | everything_ok = False |
| 161 | for router in stack.routers.values(): |
| 162 | for subnet_name in router.subnet_names: |
| 163 | found = False |
| 164 | for net in stack.nets.values(): |
| 165 | if net.subnet_name == subnet_name: |
| 166 | found = True |
| 167 | break |
| 168 | if not found: |
| peusterm | bbf4f74 | 2017-06-19 11:22:11 +0200 | [diff] [blame] | 169 | LOG.warning("Router %s of stack %s has a network named %s that is not known." % |
| peusterm | 72f0988 | 2018-05-15 17:10:27 +0200 | [diff] [blame] | 170 | (router.name, stack.stack_name, subnet_name)) |
| peusterm | 0019978 | 2017-05-17 08:48:12 +0200 | [diff] [blame] | 171 | everything_ok = False |
| 172 | return everything_ok |
| 173 | |
| peusterm | 72f0988 | 2018-05-15 17:10:27 +0200 | [diff] [blame] | 174 | def add_flavor(self, name, cpu, memory, |
| 175 | memory_unit, storage, storage_unit): |
| peusterm | 0019978 | 2017-05-17 08:48:12 +0200 | [diff] [blame] | 176 | """ |
| 177 | Adds a flavor to the stack. |
| 178 | |
| 179 | :param name: Specifies the name of the flavor. |
| 180 | :type name: ``str`` |
| 181 | :param cpu: |
| 182 | :type cpu: ``str`` |
| 183 | :param memory: |
| 184 | :type memory: ``str`` |
| 185 | :param memory_unit: |
| 186 | :type memory_unit: ``str`` |
| 187 | :param storage: |
| 188 | :type storage: ``str`` |
| 189 | :param storage_unit: |
| 190 | :type storage_unit: ``str`` |
| 191 | """ |
| peusterm | 72f0988 | 2018-05-15 17:10:27 +0200 | [diff] [blame] | 192 | flavor = InstanceFlavor( |
| 193 | name, cpu, memory, memory_unit, storage, storage_unit) |
| peusterm | 0019978 | 2017-05-17 08:48:12 +0200 | [diff] [blame] | 194 | self.flavors[flavor.name] = flavor |
| 195 | return flavor |
| 196 | |
| 197 | def deploy_stack(self, stackid): |
| 198 | """ |
| 199 | Deploys the stack and starts the emulation. |
| 200 | |
| 201 | :param stackid: An UUID str of the stack |
| 202 | :type stackid: ``str`` |
| 203 | :return: * *False*: If the Datacenter is None |
| 204 | * *True*: Else |
| 205 | :rtype: ``bool`` |
| 206 | """ |
| 207 | if self.dc is None: |
| 208 | return False |
| 209 | |
| 210 | stack = self.stacks[stackid] |
| 211 | self.update_compute_dicts(stack) |
| 212 | |
| 213 | # Create the networks first |
| 214 | for server in stack.servers.values(): |
| 215 | self._start_compute(server) |
| 216 | return True |
| 217 | |
| 218 | def delete_stack(self, stack_id): |
| 219 | """ |
| 220 | Delete a stack and all its components. |
| 221 | |
| 222 | :param stack_id: An UUID str of the stack |
| 223 | :type stack_id: ``str`` |
| 224 | :return: * *False*: If the Datacenter is None |
| 225 | * *True*: Else |
| 226 | :rtype: ``bool`` |
| 227 | """ |
| 228 | if self.dc is None: |
| 229 | return False |
| 230 | |
| 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) |
| 239 | |
| 240 | del self.stacks[stack_id] |
| 241 | return True |
| 242 | |
| 243 | def update_stack(self, old_stack_id, new_stack): |
| 244 | """ |
| 245 | Determines differences within the old and the new stack and deletes, create or changes only parts that |
| 246 | differ between the two stacks. |
| 247 | |
| 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. |
| 253 | * *False*: else |
| 254 | :rtype: ``bool`` |
| 255 | """ |
| peusterm | 72f0988 | 2018-05-15 17:10:27 +0200 | [diff] [blame] | 256 | LOG.debug("updating stack {} with new_stack {}".format( |
| 257 | old_stack_id, new_stack)) |
| peusterm | 0019978 | 2017-05-17 08:48:12 +0200 | [diff] [blame] | 258 | if old_stack_id not in self.stacks: |
| 259 | return False |
| 260 | old_stack = self.stacks[old_stack_id] |
| 261 | |
| 262 | # Update Stack IDs |
| 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 |
| 272 | break |
| 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 |
| 279 | |
| 280 | # Update the compute dicts to now contain the new_stack components |
| 281 | self.update_compute_dicts(new_stack) |
| 282 | |
| 283 | self.update_ip_addresses(old_stack, new_stack) |
| 284 | |
| 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() |
| 288 | |
| 289 | if not self.check_stack(new_stack): |
| 290 | return False |
| 291 | |
| 292 | # Remove unnecessary networks |
| 293 | for net in old_stack.nets.values(): |
| peusterm | 72f0988 | 2018-05-15 17:10:27 +0200 | [diff] [blame] | 294 | if net.name not in new_stack.nets: |
| peusterm | 0019978 | 2017-05-17 08:48:12 +0200 | [diff] [blame] | 295 | self.delete_network(net.id) |
| 296 | |
| 297 | # Remove all unnecessary servers |
| 298 | for server in old_stack.servers.values(): |
| 299 | if server.name in new_stack.servers: |
| peusterm | 72f0988 | 2018-05-15 17:10:27 +0200 | [diff] [blame] | 300 | if not server.compare_attributes( |
| 301 | new_stack.servers[server.name]): |
| peusterm | 0019978 | 2017-05-17 08:48:12 +0200 | [diff] [blame] | 302 | self.stop_compute(server) |
| 303 | else: |
| 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: |
| peusterm | 72f0988 | 2018-05-15 17:10:27 +0200 | [diff] [blame] | 307 | if not old_stack.ports.get( |
| 308 | port_name) == new_stack.ports.get(port_name): |
| peusterm | 0019978 | 2017-05-17 08:48:12 +0200 | [diff] [blame] | 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 \ |
| peusterm | 72f0988 | 2018-05-15 17:10:27 +0200 | [diff] [blame] | 312 | str(link.intf1.ip) == \ |
| 313 | old_stack.ports[port_name].ip_address.split('/')[0]: |
| peusterm | 0019978 | 2017-05-17 08:48:12 +0200 | [diff] [blame] | 314 | self._remove_link(server.name, link) |
| 315 | |
| 316 | # Add changed 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) |
| 321 | break |
| 322 | else: |
| 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) |
| 328 | break |
| 329 | |
| 330 | # Create new links |
| 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) |
| 337 | else: |
| 338 | self.stop_compute(server) |
| 339 | |
| 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) |
| 344 | else: |
| 345 | server.emulator_compute = self.dc.containers.get(server.name) |
| 346 | |
| 347 | del self.stacks[old_stack_id] |
| 348 | self.stacks[new_stack.id] = new_stack |
| 349 | return True |
| 350 | |
| 351 | def update_ip_addresses(self, old_stack, new_stack): |
| 352 | """ |
| 353 | Updates the subnet and the port IP addresses - which should always be in this order! |
| 354 | |
| 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` |
| 359 | """ |
| 360 | self.update_subnet_cidr(old_stack, new_stack) |
| 361 | self.update_port_addresses(old_stack, new_stack) |
| 362 | |
| 363 | def update_port_addresses(self, old_stack, new_stack): |
| 364 | """ |
| 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 |
| 367 | IP addresses. |
| 368 | |
| 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` |
| 373 | """ |
| 374 | for net in new_stack.nets.values(): |
| 375 | net.reset_issued_ip_addresses() |
| 376 | |
| 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: |
| peusterm | 72f0988 | 2018-05-15 17:10:27 +0200 | [diff] [blame] | 382 | if net.assign_ip_address( |
| 383 | old_port.ip_address, port.name): |
| peusterm | 0019978 | 2017-05-17 08:48:12 +0200 | [diff] [blame] | 384 | port.ip_address = old_port.ip_address |
| 385 | port.mac_address = old_port.mac_address |
| 386 | else: |
| peusterm | 72f0988 | 2018-05-15 17:10:27 +0200 | [diff] [blame] | 387 | port.ip_address = net.get_new_ip_address( |
| 388 | port.name) |
| peusterm | 0019978 | 2017-05-17 08:48:12 +0200 | [diff] [blame] | 389 | |
| 390 | for port in new_stack.ports.values(): |
| 391 | for net in new_stack.nets.values(): |
| peusterm | 72f0988 | 2018-05-15 17:10:27 +0200 | [diff] [blame] | 392 | if port.net_name == net.name and not net.is_my_ip( |
| 393 | port.ip_address, port.name): |
| peusterm | 0019978 | 2017-05-17 08:48:12 +0200 | [diff] [blame] | 394 | port.ip_address = net.get_new_ip_address(port.name) |
| 395 | |
| 396 | def update_subnet_cidr(self, old_stack, new_stack): |
| 397 | """ |
| 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. |
| 400 | |
| 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` |
| 405 | """ |
| 406 | for old_subnet in old_stack.nets.values(): |
| 407 | IP.free_cidr(old_subnet.get_cidr(), old_subnet.subnet_id) |
| 408 | |
| 409 | for subnet in new_stack.nets.values(): |
| 410 | subnet.clear_cidr() |
| 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()) |
| 415 | |
| 416 | for subnet in new_stack.nets.values(): |
| 417 | if IP.is_cidr_issued(subnet.get_cidr()): |
| 418 | continue |
| 419 | |
| 420 | cird = IP.get_new_cidr(subnet.subnet_id) |
| 421 | subnet.set_cidr(cird) |
| 422 | return |
| 423 | |
| 424 | def update_compute_dicts(self, stack): |
| 425 | """ |
| 426 | Update and add all stack components tho the compute dictionaries. |
| 427 | |
| 428 | :param stack: A stack reference, to get all required components. |
| 429 | :type stack: :class:`heat.resources.stack` |
| 430 | """ |
| 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 |
| 445 | |
| 446 | def _start_compute(self, server): |
| 447 | """ |
| 448 | Starts a new compute object (docker container) inside the emulator. |
| 449 | Should only be called by stack modifications and not directly. |
| 450 | |
| 451 | :param server: Specifies the compute resource. |
| 452 | :type server: :class:`heat.resources.server` |
| 453 | """ |
| peusterm | bbf4f74 | 2017-06-19 11:22:11 +0200 | [diff] [blame] | 454 | LOG.debug("Starting new compute resources %s" % server.name) |
| peusterm | 0019978 | 2017-05-17 08:48:12 +0200 | [diff] [blame] | 455 | network = list() |
| peusterm | a7161aa | 2017-06-29 15:32:20 +0200 | [diff] [blame] | 456 | network_dict = dict() |
| peusterm | 0019978 | 2017-05-17 08:48:12 +0200 | [diff] [blame] | 457 | |
| 458 | for port_name in server.port_names: |
| 459 | network_dict = dict() |
| 460 | port = self.find_port_by_name_or_id(port_name) |
| 461 | if port is not None: |
| 462 | network_dict['id'] = port.intf_name |
| 463 | network_dict['ip'] = port.ip_address |
| peusterm | 72f0988 | 2018-05-15 17:10:27 +0200 | [diff] [blame] | 464 | network_dict[network_dict['id']] = self.find_network_by_name_or_id( |
| 465 | port.net_name).name |
| peusterm | 0019978 | 2017-05-17 08:48:12 +0200 | [diff] [blame] | 466 | network.append(network_dict) |
| peusterm | a7161aa | 2017-06-29 15:32:20 +0200 | [diff] [blame] | 467 | # default network dict |
| 468 | if len(network) < 1: |
| 469 | network_dict['id'] = server.name + "-eth0" |
| 470 | network_dict[network_dict['id']] = network_dict['id'] |
| 471 | network.append(network_dict) |
| peusterm | 6e354f1 | 2017-06-27 12:44:13 +0200 | [diff] [blame] | 472 | |
| peusterm | 0019978 | 2017-05-17 08:48:12 +0200 | [diff] [blame] | 473 | self.compute_nets[server.name] = network |
| peusterm | a7161aa | 2017-06-29 15:32:20 +0200 | [diff] [blame] | 474 | LOG.debug("Network dict: {}".format(network)) |
| peusterm | 0019978 | 2017-05-17 08:48:12 +0200 | [diff] [blame] | 475 | c = self.dc.startCompute(server.name, image=server.image, command=server.command, |
| splietker | 7b38ee1 | 2017-06-28 17:24:01 +0200 | [diff] [blame] | 476 | network=network, flavor_name=server.flavor, |
| 477 | properties=server.properties) |
| peusterm | 0019978 | 2017-05-17 08:48:12 +0200 | [diff] [blame] | 478 | server.emulator_compute = c |
| 479 | |
| 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) |
| 483 | if port is not None: |
| 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) |
| 489 | else: |
| 490 | port.mac_address = intf.MAC() |
| schillinge | b87991a | 2018-12-03 11:34:01 +0100 | [diff] [blame] | 491 | port.assigned_container = c |
| peusterm | 0019978 | 2017-05-17 08:48:12 +0200 | [diff] [blame] | 492 | |
| 493 | # Start the real emulator command now as specified in the dockerfile |
| peusterm | 0019978 | 2017-05-17 08:48:12 +0200 | [diff] [blame] | 494 | config = c.dcinfo.get("Config", dict()) |
| 495 | env = config.get("Env", list()) |
| schillinge | 3bb8b25 | 2019-03-14 22:46:14 +0100 | [diff] [blame^] | 496 | legacy_command_execution = False |
| peusterm | 0019978 | 2017-05-17 08:48:12 +0200 | [diff] [blame] | 497 | for env_var in env: |
| peusterm | 5b8ac4f | 2019-02-12 19:58:24 +0100 | [diff] [blame] | 498 | var, cmd = map(str.strip, map(str, env_var.split('=', 1))) |
| 499 | if var == "SON_EMU_CMD" or var == "VIM_EMU_CMD": |
| 500 | LOG.info("Executing script in '{}': {}={}" |
| 501 | .format(server.name, var, cmd)) |
| peusterm | 72f0988 | 2018-05-15 17:10:27 +0200 | [diff] [blame] | 502 | # execute command in new thread to ensure that GK is not |
| 503 | # blocked by VNF |
| peusterm | 0019978 | 2017-05-17 08:48:12 +0200 | [diff] [blame] | 504 | t = threading.Thread(target=c.cmdPrint, args=(cmd,)) |
| 505 | t.daemon = True |
| 506 | t.start() |
| schillinge | 3bb8b25 | 2019-03-14 22:46:14 +0100 | [diff] [blame^] | 507 | legacy_command_execution = True |
| peusterm | 5b8ac4f | 2019-02-12 19:58:24 +0100 | [diff] [blame] | 508 | break # only execute one command |
| schillinge | 3bb8b25 | 2019-03-14 22:46:14 +0100 | [diff] [blame^] | 509 | if not legacy_command_execution: |
| 510 | c.start() |
| peusterm | 0019978 | 2017-05-17 08:48:12 +0200 | [diff] [blame] | 511 | |
| 512 | def stop_compute(self, server): |
| 513 | """ |
| 514 | Determines which links should be removed before removing the server itself. |
| 515 | |
| 516 | :param server: The server that should be removed |
| 517 | :type server: ``heat.resources.server`` |
| 518 | """ |
| peusterm | 72f0988 | 2018-05-15 17:10:27 +0200 | [diff] [blame] | 519 | LOG.debug("Stopping container %s with full name %s" % |
| 520 | (server.name, server.full_name)) |
| peusterm | 0019978 | 2017-05-17 08:48:12 +0200 | [diff] [blame] | 521 | link_names = list() |
| 522 | for port_name in server.port_names: |
| peusterm | 832a0b8 | 2017-07-11 16:08:35 +0200 | [diff] [blame] | 523 | prt = self.find_port_by_name_or_id(port_name) |
| 524 | if prt is not None: |
| 525 | link_names.append(prt.intf_name) |
| peusterm | 0019978 | 2017-05-17 08:48:12 +0200 | [diff] [blame] | 526 | my_links = self.dc.net.links |
| 527 | for link in my_links: |
| 528 | if str(link.intf1) in link_names: |
| peusterm | 72f0988 | 2018-05-15 17:10:27 +0200 | [diff] [blame] | 529 | # Remove all self created links that connect the server to the |
| 530 | # main switch |
| peusterm | 0019978 | 2017-05-17 08:48:12 +0200 | [diff] [blame] | 531 | self._remove_link(server.name, link) |
| 532 | |
| 533 | # Stop the server and the remaining connection to the datacenter switch |
| 534 | self.dc.stopCompute(server.name) |
| 535 | # Only now delete all its ports and the server itself |
| 536 | for port_name in server.port_names: |
| 537 | self.delete_port(port_name) |
| 538 | self.delete_server(server) |
| 539 | |
| 540 | def find_server_by_name_or_id(self, name_or_id): |
| 541 | """ |
| 542 | Tries to find the server by ID and if this does not succeed then tries to find it via name. |
| 543 | |
| 544 | :param name_or_id: UUID or name of the server. |
| 545 | :type name_or_id: ``str`` |
| 546 | :return: Returns the server reference if it was found or None |
| 547 | :rtype: :class:`heat.resources.server` |
| 548 | """ |
| 549 | if name_or_id in self.computeUnits: |
| 550 | return self.computeUnits[name_or_id] |
| 551 | |
| peusterm | 3f2f565 | 2017-06-23 15:00:21 +0200 | [diff] [blame] | 552 | if self._shorten_server_name(name_or_id) in self.computeUnits: |
| 553 | return self.computeUnits[name_or_id] |
| 554 | |
| peusterm | 0019978 | 2017-05-17 08:48:12 +0200 | [diff] [blame] | 555 | for server in self.computeUnits.values(): |
| peusterm | 72f0988 | 2018-05-15 17:10:27 +0200 | [diff] [blame] | 556 | if (server.name == name_or_id or |
| 557 | server.template_name == name_or_id or |
| 558 | server.full_name == name_or_id): |
| peusterm | 0019978 | 2017-05-17 08:48:12 +0200 | [diff] [blame] | 559 | return server |
| peusterm | 72f0988 | 2018-05-15 17:10:27 +0200 | [diff] [blame] | 560 | if (server.name == self._shorten_server_name(name_or_id) or |
| 561 | server.template_name == self._shorten_server_name(name_or_id) or |
| 562 | server.full_name == self._shorten_server_name(name_or_id)): |
| peusterm | 3f2f565 | 2017-06-23 15:00:21 +0200 | [diff] [blame] | 563 | return server |
| peusterm | 0019978 | 2017-05-17 08:48:12 +0200 | [diff] [blame] | 564 | return None |
| 565 | |
| 566 | def create_server(self, name, stack_operation=False): |
| 567 | """ |
| 568 | Creates a server with the specified name. Raises an exception when a server with the given name already |
| 569 | exists! |
| 570 | |
| 571 | :param name: Name of the new server. |
| 572 | :type name: ``str`` |
| 573 | :param stack_operation: Allows the heat parser to create modules without adapting the current emulation. |
| 574 | :type stack_operation: ``bool`` |
| 575 | :return: Returns the created server. |
| 576 | :rtype: :class:`heat.resources.server` |
| 577 | """ |
| peusterm | 72f0988 | 2018-05-15 17:10:27 +0200 | [diff] [blame] | 578 | if self.find_server_by_name_or_id( |
| 579 | name) is not None and not stack_operation: |
| peusterm | 0019978 | 2017-05-17 08:48:12 +0200 | [diff] [blame] | 580 | raise Exception("Server with name %s already exists." % name) |
| peusterm | 3f2f565 | 2017-06-23 15:00:21 +0200 | [diff] [blame] | 581 | safe_name = self._shorten_server_name(name) |
| 582 | server = Server(safe_name) |
| peusterm | 0019978 | 2017-05-17 08:48:12 +0200 | [diff] [blame] | 583 | server.id = str(uuid.uuid4()) |
| 584 | if not stack_operation: |
| 585 | self.computeUnits[server.id] = server |
| 586 | return server |
| 587 | |
| peusterm | 646b585 | 2017-06-26 09:59:12 +0200 | [diff] [blame] | 588 | def _shorten_server_name(self, name, char_limit=9): |
| peusterm | 3f2f565 | 2017-06-23 15:00:21 +0200 | [diff] [blame] | 589 | """ |
| 590 | Docker does not like too long instance names. |
| 591 | This function provides a shorter name if needed |
| 592 | """ |
| peusterm | 3f2f565 | 2017-06-23 15:00:21 +0200 | [diff] [blame] | 593 | if len(name) > char_limit: |
| 594 | # construct a short name |
| peusterm | 9219060 | 2017-12-19 15:18:11 +0100 | [diff] [blame] | 595 | h = hashlib.sha224(name).hexdigest() |
| 596 | h = h[0:char_limit] |
| schillinge | 2efa0c5 | 2019-02-13 08:07:52 +0100 | [diff] [blame] | 597 | LOG.debug("Shortened server name '%s' to '%s'" % (name, h)) |
| peusterm | 3f2f565 | 2017-06-23 15:00:21 +0200 | [diff] [blame] | 598 | return name |
| 599 | |
| peusterm | 0019978 | 2017-05-17 08:48:12 +0200 | [diff] [blame] | 600 | def delete_server(self, server): |
| 601 | """ |
| 602 | Deletes the given server from the stack dictionary and the computeUnits dictionary. |
| 603 | |
| 604 | :param server: Reference of the server that should be deleted. |
| 605 | :type server: :class:`heat.resources.server` |
| 606 | :return: * *False*: If the server name is not in the correct format ('datacentername_stackname_servername') \ |
| 607 | or when no stack with the correct stackname was found. |
| 608 | * *True*: Else |
| 609 | :rtype: ``bool`` |
| 610 | """ |
| 611 | if server is None: |
| 612 | return False |
| 613 | name_parts = server.name.split('_') |
| peusterm | 543430b | 2018-04-24 16:52:51 +0200 | [diff] [blame] | 614 | if len(name_parts) > 1: |
| 615 | for stack in self.stacks.values(): |
| 616 | if stack.stack_name == name_parts[1]: |
| 617 | stack.servers.pop(server.id, None) |
| peusterm | 0019978 | 2017-05-17 08:48:12 +0200 | [diff] [blame] | 618 | if self.computeUnits.pop(server.id, None) is None: |
| 619 | return False |
| 620 | return True |
| 621 | |
| 622 | def find_network_by_name_or_id(self, name_or_id): |
| 623 | """ |
| 624 | Tries to find the network by ID and if this does not succeed then tries to find it via name. |
| 625 | |
| 626 | :param name_or_id: UUID or name of the network. |
| 627 | :type name_or_id: ``str`` |
| 628 | :return: Returns the network reference if it was found or None |
| 629 | :rtype: :class:`heat.resources.net` |
| 630 | """ |
| 631 | if name_or_id in self.nets: |
| 632 | return self.nets[name_or_id] |
| 633 | for net in self.nets.values(): |
| 634 | if net.name == name_or_id: |
| 635 | return net |
| peusterm | 7c16ebc | 2018-04-25 15:36:15 +0200 | [diff] [blame] | 636 | LOG.warning("Could not find net '{}' in {} or {}" |
| 637 | .format(name_or_id, |
| 638 | self.nets.keys(), |
| 639 | [n.name for n in self.nets.values()])) |
| peusterm | 0019978 | 2017-05-17 08:48:12 +0200 | [diff] [blame] | 640 | return None |
| 641 | |
| 642 | def create_network(self, name, stack_operation=False): |
| 643 | """ |
| 644 | Creates a new network with the given name. Raises an exception when a network with the given name already |
| 645 | exists! |
| 646 | |
| 647 | :param name: Name of the new network. |
| 648 | :type name: ``str`` |
| 649 | :param stack_operation: Allows the heat parser to create modules without adapting the current emulation. |
| 650 | :type stack_operation: ``bool`` |
| 651 | :return: :class:`heat.resources.net` |
| 652 | """ |
| peusterm | bbf4f74 | 2017-06-19 11:22:11 +0200 | [diff] [blame] | 653 | LOG.debug("Creating network with name %s" % name) |
| peusterm | 72f0988 | 2018-05-15 17:10:27 +0200 | [diff] [blame] | 654 | if self.find_network_by_name_or_id( |
| 655 | name) is not None and not stack_operation: |
| 656 | LOG.warning( |
| 657 | "Creating network with name %s failed, as it already exists" % name) |
| peusterm | 0019978 | 2017-05-17 08:48:12 +0200 | [diff] [blame] | 658 | raise Exception("Network with name %s already exists." % name) |
| 659 | network = Net(name) |
| 660 | network.id = str(uuid.uuid4()) |
| 661 | if not stack_operation: |
| 662 | self.nets[network.id] = network |
| 663 | return network |
| 664 | |
| 665 | def delete_network(self, name_or_id): |
| 666 | """ |
| 667 | Deletes the given network. |
| 668 | |
| 669 | :param name_or_id: Name or UUID of the network. |
| 670 | :type name_or_id: ``str`` |
| 671 | """ |
| 672 | net = self.find_network_by_name_or_id(name_or_id) |
| 673 | if net is None: |
| peusterm | 72f0988 | 2018-05-15 17:10:27 +0200 | [diff] [blame] | 674 | raise Exception( |
| 675 | "Network with name or id %s does not exists." % name_or_id) |
| peusterm | 0019978 | 2017-05-17 08:48:12 +0200 | [diff] [blame] | 676 | |
| 677 | for stack in self.stacks.values(): |
| 678 | stack.nets.pop(net.name, None) |
| 679 | |
| 680 | self.nets.pop(net.id, None) |
| 681 | |
| 682 | def create_port(self, name, stack_operation=False): |
| 683 | """ |
| 684 | Creates a new port with the given name. Raises an exception when a port with the given name already |
| 685 | exists! |
| 686 | |
| 687 | :param name: Name of the new port. |
| 688 | :type name: ``str`` |
| 689 | :param stack_operation: Allows the heat parser to create modules without adapting the current emulation. |
| 690 | :type stack_operation: ``bool`` |
| 691 | :return: Returns the created port. |
| 692 | :rtype: :class:`heat.resources.port` |
| 693 | """ |
| peusterm | 0019978 | 2017-05-17 08:48:12 +0200 | [diff] [blame] | 694 | port = Port(name) |
| 695 | if not stack_operation: |
| 696 | self.ports[port.id] = port |
| 697 | port.create_intf_name() |
| 698 | return port |
| 699 | |
| 700 | def find_port_by_name_or_id(self, name_or_id): |
| 701 | """ |
| 702 | Tries to find the port by ID and if this does not succeed then tries to find it via name. |
| 703 | |
| 704 | :param name_or_id: UUID or name of the network. |
| 705 | :type name_or_id: ``str`` |
| 706 | :return: Returns the port reference if it was found or None |
| 707 | :rtype: :class:`heat.resources.port` |
| 708 | """ |
| schillinge | 0e10819 | 2019-01-30 17:48:27 +0100 | [diff] [blame] | 709 | # find by id |
| peusterm | 0019978 | 2017-05-17 08:48:12 +0200 | [diff] [blame] | 710 | if name_or_id in self.ports: |
| 711 | return self.ports[name_or_id] |
| schillinge | 0e10819 | 2019-01-30 17:48:27 +0100 | [diff] [blame] | 712 | # find by name |
| 713 | matching_ports = filter( |
| 714 | lambda port: port.name == name_or_id or port.template_name == name_or_id, |
| 715 | self.ports.values() |
| 716 | ) |
| 717 | matching_ports_count = len(matching_ports) |
| 718 | if matching_ports_count == 1: |
| 719 | return matching_ports[0] |
| 720 | if matching_ports_count > 1: |
| 721 | raise RuntimeError("Ambiguous port name %s" % name_or_id) |
| peusterm | 0019978 | 2017-05-17 08:48:12 +0200 | [diff] [blame] | 722 | return None |
| 723 | |
| 724 | def delete_port(self, name_or_id): |
| 725 | """ |
| 726 | Deletes the given port. Raises an exception when the port was not found! |
| 727 | |
| 728 | :param name_or_id: UUID or name of the port. |
| 729 | :type name_or_id: ``str`` |
| 730 | """ |
| 731 | port = self.find_port_by_name_or_id(name_or_id) |
| 732 | if port is None: |
| peusterm | 72f0988 | 2018-05-15 17:10:27 +0200 | [diff] [blame] | 733 | LOG.warning( |
| 734 | "Port with name or id %s does not exist. Can't delete it." % name_or_id) |
| peusterm | 832a0b8 | 2017-07-11 16:08:35 +0200 | [diff] [blame] | 735 | return |
| peusterm | 0019978 | 2017-05-17 08:48:12 +0200 | [diff] [blame] | 736 | |
| 737 | my_links = self.dc.net.links |
| 738 | for link in my_links: |
| peusterm | 9679e9a | 2018-05-16 13:10:30 +0200 | [diff] [blame] | 739 | if str(link.intf1) == port.intf_name: |
| peusterm | 0019978 | 2017-05-17 08:48:12 +0200 | [diff] [blame] | 740 | self._remove_link(link.intf1.node.name, link) |
| 741 | break |
| 742 | |
| 743 | self.ports.pop(port.id, None) |
| 744 | for stack in self.stacks.values(): |
| 745 | stack.ports.pop(port.name, None) |
| 746 | |
| splietker | 7b38ee1 | 2017-06-28 17:24:01 +0200 | [diff] [blame] | 747 | def create_port_pair(self, name, stack_operation=False): |
| 748 | """ |
| 749 | Creates a new port pair with the given name. Raises an exception when a port pair with the given name already |
| 750 | exists! |
| 751 | |
| 752 | :param name: Name of the new port pair. |
| 753 | :type name: ``str`` |
| 754 | :param stack_operation: Allows the heat parser to create modules without adapting the current emulation. |
| 755 | :type stack_operation: ``bool`` |
| 756 | :return: Returns the created port pair. |
| 757 | :rtype: :class:`openstack.resources.port_pair` |
| 758 | """ |
| 759 | port_pair = self.find_port_pair_by_name_or_id(name) |
| 760 | if port_pair is not None and not stack_operation: |
| peusterm | 72f0988 | 2018-05-15 17:10:27 +0200 | [diff] [blame] | 761 | logging.warning( |
| 762 | "Creating port pair with name %s failed, as it already exists" % name) |
| splietker | 7b38ee1 | 2017-06-28 17:24:01 +0200 | [diff] [blame] | 763 | raise Exception("Port pair with name %s already exists." % name) |
| 764 | logging.debug("Creating port pair with name %s" % name) |
| 765 | port_pair = PortPair(name) |
| 766 | if not stack_operation: |
| 767 | self.port_pairs[port_pair.id] = port_pair |
| 768 | return port_pair |
| 769 | |
| 770 | def find_port_pair_by_name_or_id(self, name_or_id): |
| 771 | """ |
| 772 | Tries to find the port pair by ID and if this does not succeed then tries to find it via name. |
| 773 | |
| 774 | :param name_or_id: UUID or name of the port pair. |
| 775 | :type name_or_id: ``str`` |
| 776 | :return: Returns the port pair reference if it was found or None |
| 777 | :rtype: :class:`openstack.resources.port_pair` |
| 778 | """ |
| 779 | if name_or_id in self.port_pairs: |
| 780 | return self.port_pairs[name_or_id] |
| 781 | for port_pair in self.port_pairs.values(): |
| 782 | if port_pair.name == name_or_id: |
| 783 | return port_pair |
| 784 | |
| 785 | return None |
| 786 | |
| 787 | def delete_port_pair(self, name_or_id): |
| 788 | """ |
| 789 | Deletes the given port pair. Raises an exception when the port pair was not found! |
| 790 | |
| 791 | :param name_or_id: UUID or name of the port pair. |
| 792 | :type name_or_id: ``str`` |
| 793 | """ |
| 794 | port_pair = self.find_port_pair_by_name_or_id(name_or_id) |
| 795 | if port_pair is None: |
| peusterm | 72f0988 | 2018-05-15 17:10:27 +0200 | [diff] [blame] | 796 | raise Exception( |
| 797 | "Port pair with name or id %s does not exists." % name_or_id) |
| splietker | 7b38ee1 | 2017-06-28 17:24:01 +0200 | [diff] [blame] | 798 | |
| 799 | self.port_pairs.pop(port_pair.id, None) |
| 800 | |
| 801 | def create_port_pair_group(self, name, stack_operation=False): |
| 802 | """ |
| 803 | Creates a new port pair group with the given name. Raises an exception when a port pair group |
| 804 | with the given name already exists! |
| 805 | |
| 806 | :param name: Name of the new port pair group. |
| 807 | :type name: ``str`` |
| 808 | :param stack_operation: Allows the heat parser to create modules without adapting the current emulation. |
| 809 | :type stack_operation: ``bool`` |
| 810 | :return: Returns the created port pair group . |
| 811 | :rtype: :class:`openstack.resources.port_pair_group` |
| 812 | """ |
| 813 | port_pair_group = self.find_port_pair_group_by_name_or_id(name) |
| 814 | if port_pair_group is not None and not stack_operation: |
| peusterm | 72f0988 | 2018-05-15 17:10:27 +0200 | [diff] [blame] | 815 | logging.warning( |
| 816 | "Creating port pair group with name %s failed, as it already exists" % name) |
| 817 | raise Exception( |
| 818 | "Port pair group with name %s already exists." % name) |
| splietker | 7b38ee1 | 2017-06-28 17:24:01 +0200 | [diff] [blame] | 819 | logging.debug("Creating port pair group with name %s" % name) |
| 820 | port_pair_group = PortPairGroup(name) |
| 821 | if not stack_operation: |
| 822 | self.port_pair_groups[port_pair_group.id] = port_pair_group |
| 823 | return port_pair_group |
| 824 | |
| 825 | def find_port_pair_group_by_name_or_id(self, name_or_id): |
| 826 | """ |
| 827 | Tries to find the port pair group by ID and if this does not succeed then tries to find it via name. |
| 828 | |
| 829 | :param name_or_id: UUID or name of the port pair group. |
| 830 | :type name_or_id: ``str`` |
| 831 | :return: Returns the port pair group reference if it was found or None |
| 832 | :rtype: :class:`openstack.resources.port_pair_group` |
| 833 | """ |
| 834 | if name_or_id in self.port_pair_groups: |
| 835 | return self.port_pair_groups[name_or_id] |
| 836 | for port_pair_group in self.port_pair_groups.values(): |
| 837 | if port_pair_group.name == name_or_id: |
| 838 | return port_pair_group |
| 839 | |
| 840 | return None |
| 841 | |
| 842 | def delete_port_pair_group(self, name_or_id): |
| 843 | """ |
| 844 | Deletes the given port pair group. Raises an exception when the port pair group was not found! |
| 845 | |
| 846 | :param name_or_id: UUID or name of the port pair group. |
| 847 | :type name_or_id: ``str`` |
| 848 | """ |
| 849 | port_pair_group = self.find_port_pair_group_by_name_or_id(name_or_id) |
| 850 | if port_pair_group is None: |
| peusterm | 72f0988 | 2018-05-15 17:10:27 +0200 | [diff] [blame] | 851 | raise Exception( |
| 852 | "Port pair with name or id %s does not exists." % name_or_id) |
| splietker | 7b38ee1 | 2017-06-28 17:24:01 +0200 | [diff] [blame] | 853 | |
| 854 | self.port_pair_groups.pop(port_pair_group.id, None) |
| 855 | |
| 856 | def create_port_chain(self, name, stack_operation=False): |
| 857 | """ |
| 858 | Creates a new port chain with the given name. Raises an exception when a port chain with the given name already |
| 859 | exists! |
| 860 | |
| 861 | :param name: Name of the new port chain |
| 862 | :type name: ``str`` |
| 863 | :param stack_operation: Allows the heat parser to create modules without adapting the current emulation. |
| 864 | :type stack_operation: ``bool`` |
| 865 | :return: Returns the created port chain. |
| 866 | :rtype: :class:`openstack.resources.port_chain.PortChain` |
| 867 | """ |
| 868 | port_chain = self.find_port_chain_by_name_or_id(name) |
| 869 | if port_chain is not None and not stack_operation: |
| peusterm | 72f0988 | 2018-05-15 17:10:27 +0200 | [diff] [blame] | 870 | logging.warning( |
| 871 | "Creating port chain with name %s failed, as it already exists" % name) |
| splietker | 7b38ee1 | 2017-06-28 17:24:01 +0200 | [diff] [blame] | 872 | raise Exception("Port chain with name %s already exists." % name) |
| 873 | logging.debug("Creating port chain with name %s" % name) |
| 874 | port_chain = PortChain(name) |
| 875 | if not stack_operation: |
| 876 | self.port_chains[port_chain.id] = port_chain |
| 877 | return port_chain |
| 878 | |
| 879 | def find_port_chain_by_name_or_id(self, name_or_id): |
| 880 | """ |
| 881 | Tries to find the port chain by ID and if this does not succeed then tries to find it via name. |
| 882 | |
| 883 | :param name_or_id: UUID or name of the port chain. |
| 884 | :type name_or_id: ``str`` |
| 885 | :return: Returns the port chain reference if it was found or None |
| 886 | :rtype: :class:`openstack.resources.port_chain.PortChain` |
| 887 | """ |
| 888 | if name_or_id in self.port_chains: |
| 889 | return self.port_chains[name_or_id] |
| 890 | for port_chain in self.port_chains.values(): |
| 891 | if port_chain.name == name_or_id: |
| 892 | return port_chain |
| 893 | return None |
| 894 | |
| 895 | def delete_port_chain(self, name_or_id): |
| 896 | """ |
| 897 | Deletes the given port chain. Raises an exception when the port chain was not found! |
| 898 | |
| 899 | :param name_or_id: UUID or name of the port chain. |
| 900 | :type name_or_id: ``str`` |
| 901 | """ |
| 902 | port_chain = self.find_port_chain_by_name_or_id(name_or_id) |
| 903 | port_chain.uninstall(self) |
| 904 | if port_chain is None: |
| peusterm | 72f0988 | 2018-05-15 17:10:27 +0200 | [diff] [blame] | 905 | raise Exception( |
| 906 | "Port chain with name or id %s does not exists." % name_or_id) |
| splietker | 7b38ee1 | 2017-06-28 17:24:01 +0200 | [diff] [blame] | 907 | |
| 908 | self.port_chains.pop(port_chain.id, None) |
| 909 | |
| 910 | def create_flow_classifier(self, name, stack_operation=False): |
| 911 | """ |
| 912 | Creates a new flow classifier with the given name. Raises an exception when a flow classifier with the given name already |
| 913 | exists! |
| 914 | |
| 915 | :param name: Name of the new flow classifier. |
| 916 | :type name: ``str`` |
| 917 | :param stack_operation: Allows the heat parser to create modules without adapting the current emulation. |
| 918 | :type stack_operation: ``bool`` |
| 919 | :return: Returns the created flow classifier. |
| 920 | :rtype: :class:`openstack.resources.flow_classifier` |
| 921 | """ |
| 922 | flow_classifier = self.find_flow_classifier_by_name_or_id(name) |
| 923 | if flow_classifier is not None and not stack_operation: |
| peusterm | 72f0988 | 2018-05-15 17:10:27 +0200 | [diff] [blame] | 924 | logging.warning( |
| 925 | "Creating flow classifier with name %s failed, as it already exists" % name) |
| 926 | raise Exception( |
| 927 | "Flow classifier with name %s already exists." % name) |
| splietker | 7b38ee1 | 2017-06-28 17:24:01 +0200 | [diff] [blame] | 928 | logging.debug("Creating flow classifier with name %s" % name) |
| 929 | flow_classifier = FlowClassifier(name) |
| 930 | if not stack_operation: |
| 931 | self.flow_classifiers[flow_classifier.id] = flow_classifier |
| 932 | return flow_classifier |
| 933 | |
| 934 | def find_flow_classifier_by_name_or_id(self, name_or_id): |
| 935 | """ |
| 936 | Tries to find the flow classifier by ID and if this does not succeed then tries to find it via name. |
| 937 | |
| 938 | :param name_or_id: UUID or name of the flow classifier. |
| 939 | :type name_or_id: ``str`` |
| 940 | :return: Returns the flow classifier reference if it was found or None |
| 941 | :rtype: :class:`openstack.resources.flow_classifier` |
| 942 | """ |
| 943 | if name_or_id in self.flow_classifiers: |
| 944 | return self.flow_classifiers[name_or_id] |
| 945 | for flow_classifier in self.flow_classifiers.values(): |
| 946 | if flow_classifier.name == name_or_id: |
| 947 | return flow_classifier |
| 948 | |
| 949 | return None |
| 950 | |
| 951 | def delete_flow_classifier(self, name_or_id): |
| 952 | """ |
| 953 | Deletes the given flow classifier. Raises an exception when the flow classifier was not found! |
| 954 | |
| 955 | :param name_or_id: UUID or name of the flow classifier. |
| 956 | :type name_or_id: ``str`` |
| 957 | """ |
| 958 | flow_classifier = self.find_flow_classifier_by_name_or_id(name_or_id) |
| 959 | if flow_classifier is None: |
| peusterm | 72f0988 | 2018-05-15 17:10:27 +0200 | [diff] [blame] | 960 | raise Exception( |
| 961 | "Flow classifier with name or id %s does not exists." % name_or_id) |
| splietker | 7b38ee1 | 2017-06-28 17:24:01 +0200 | [diff] [blame] | 962 | |
| 963 | self.flow_classifiers.pop(flow_classifier.id, None) |
| 964 | |
| peusterm | 0019978 | 2017-05-17 08:48:12 +0200 | [diff] [blame] | 965 | def _add_link(self, node_name, ip_address, link_name, net_name): |
| 966 | """ |
| 967 | Adds a new link between datacenter switch and the node with the given name. |
| 968 | |
| 969 | :param node_name: Name of the required node. |
| 970 | :type node_name: ``str`` |
| 971 | :param ip_address: IP-Address of the node. |
| 972 | :type ip_address: ``str`` |
| 973 | :param link_name: Link name. |
| 974 | :type link_name: ``str`` |
| 975 | :param net_name: Network name. |
| 976 | :type net_name: ``str`` |
| 977 | """ |
| 978 | node = self.dc.net.get(node_name) |
| 979 | params = {'params1': {'ip': ip_address, |
| 980 | 'id': link_name, |
| 981 | link_name: net_name}, |
| 982 | 'intfName1': link_name, |
| 983 | 'cls': Link} |
| 984 | link = self.dc.net.addLink(node, self.dc.switch, **params) |
| 985 | OpenstackCompute.timeout_sleep(link.intf1.isUp, 1) |
| 986 | |
| 987 | def _remove_link(self, server_name, link): |
| 988 | """ |
| 989 | Removes a link between server and datacenter switch. |
| 990 | |
| 991 | :param server_name: Specifies the server where the link starts. |
| 992 | :type server_name: ``str`` |
| 993 | :param link: A reference of the link which should be removed. |
| 994 | :type link: :class:`mininet.link` |
| 995 | """ |
| 996 | self.dc.switch.detach(link.intf2) |
| 997 | del self.dc.switch.intfs[self.dc.switch.ports[link.intf2]] |
| 998 | del self.dc.switch.ports[link.intf2] |
| 999 | del self.dc.switch.nameToIntf[link.intf2.name] |
| 1000 | self.dc.net.removeLink(link=link) |
| peusterm | 0019978 | 2017-05-17 08:48:12 +0200 | [diff] [blame] | 1001 | for intf_key in self.dc.net[server_name].intfs.keys(): |
| 1002 | if self.dc.net[server_name].intfs[intf_key].link == link: |
| 1003 | self.dc.net[server_name].intfs[intf_key].delete() |
| 1004 | del self.dc.net[server_name].intfs[intf_key] |
| 1005 | |
| 1006 | @staticmethod |
| 1007 | def timeout_sleep(function, max_sleep): |
| 1008 | """ |
| 1009 | This function will execute a function all 0.1 seconds until it successfully returns. |
| 1010 | Will return after `max_sleep` seconds if not successful. |
| 1011 | |
| 1012 | :param function: The function to execute. Should return true if done. |
| 1013 | :type function: ``function`` |
| 1014 | :param max_sleep: Max seconds to sleep. 1 equals 1 second. |
| 1015 | :type max_sleep: ``float`` |
| 1016 | """ |
| 1017 | current_time = time.time() |
| 1018 | stop_time = current_time + max_sleep |
| 1019 | while not function() and current_time < stop_time: |
| 1020 | current_time = time.time() |
| 1021 | time.sleep(0.1) |