| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
| 3 | ## |
| tierno | 9202102 | 2018-09-12 16:29:23 +0200 | [diff] [blame] | 4 | # Copyright 2015 Telefonica Investigacion y Desarrollo, S.A.U. |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5 | # This file is part of openmano |
| 6 | # All Rights Reserved. |
| 7 | # |
| 8 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 9 | # not use this file except in compliance with the License. You may obtain |
| 10 | # a copy of the License at |
| 11 | # |
| 12 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | # |
| 14 | # Unless required by applicable law or agreed to in writing, software |
| 15 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 16 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 17 | # License for the specific language governing permissions and limitations |
| 18 | # under the License. |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 19 | ## |
| 20 | |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 21 | """ |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 22 | osconnector implements all the methods to interact with openstack using the python-neutronclient. |
| 23 | |
| 24 | For the VNF forwarding graph, The OpenStack VIM connector calls the |
| 25 | networking-sfc Neutron extension methods, whose resources are mapped |
| 26 | to the VIM connector's SFC resources as follows: |
| 27 | - Classification (OSM) -> Flow Classifier (Neutron) |
| 28 | - Service Function Instance (OSM) -> Port Pair (Neutron) |
| 29 | - Service Function (OSM) -> Port Pair Group (Neutron) |
| 30 | - Service Function Path (OSM) -> Port Chain (Neutron) |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 31 | """ |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 32 | |
| sousaedu | 049cbb1 | 2022-01-05 11:39:35 +0000 | [diff] [blame] | 33 | import copy |
| 34 | from http.client import HTTPException |
| elumalai | 8658c2c | 2022-04-28 19:09:31 +0530 | [diff] [blame] | 35 | import json |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 36 | import logging |
| sousaedu | 049cbb1 | 2022-01-05 11:39:35 +0000 | [diff] [blame] | 37 | from pprint import pformat |
| garciadeblas | 2299e3b | 2017-01-26 14:35:55 +0000 | [diff] [blame] | 38 | import random |
| kate | 721d79b | 2017-06-24 04:21:38 -0700 | [diff] [blame] | 39 | import re |
| sousaedu | 049cbb1 | 2022-01-05 11:39:35 +0000 | [diff] [blame] | 40 | import time |
| 41 | |
| 42 | from cinderclient import client as cClient |
| tierno | b5cef37 | 2017-06-19 15:52:22 +0200 | [diff] [blame] | 43 | from glanceclient import client as glClient |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 44 | import glanceclient.exc as gl1Exceptions |
| sousaedu | 049cbb1 | 2022-01-05 11:39:35 +0000 | [diff] [blame] | 45 | from keystoneauth1 import session |
| 46 | from keystoneauth1.identity import v2, v3 |
| 47 | import keystoneclient.exceptions as ksExceptions |
| 48 | import keystoneclient.v2_0.client as ksClient_v2 |
| 49 | import keystoneclient.v3.client as ksClient_v3 |
| 50 | import netaddr |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 51 | from neutronclient.common import exceptions as neExceptions |
| sousaedu | 049cbb1 | 2022-01-05 11:39:35 +0000 | [diff] [blame] | 52 | from neutronclient.neutron import client as neClient |
| 53 | from novaclient import client as nClient, exceptions as nvExceptions |
| 54 | from osm_ro_plugin import vimconn |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 55 | from requests.exceptions import ConnectionError |
| sousaedu | 049cbb1 | 2022-01-05 11:39:35 +0000 | [diff] [blame] | 56 | import yaml |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 57 | |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 58 | __author__ = "Alfonso Tierno, Gerardo Garcia, Pablo Montes, xFlow Research, Igor D.C., Eduardo Sousa" |
| 59 | __date__ = "$22-sep-2017 23:59:59$" |
| tierno | 40e1bce | 2017-08-09 09:12:04 +0200 | [diff] [blame] | 60 | |
| 61 | """contain the openstack virtual machine status to openmano status""" |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 62 | vmStatus2manoFormat = { |
| 63 | "ACTIVE": "ACTIVE", |
| 64 | "PAUSED": "PAUSED", |
| 65 | "SUSPENDED": "SUSPENDED", |
| 66 | "SHUTOFF": "INACTIVE", |
| 67 | "BUILD": "BUILD", |
| 68 | "ERROR": "ERROR", |
| 69 | "DELETED": "DELETED", |
| 70 | } |
| 71 | netStatus2manoFormat = { |
| 72 | "ACTIVE": "ACTIVE", |
| 73 | "PAUSED": "PAUSED", |
| 74 | "INACTIVE": "INACTIVE", |
| 75 | "BUILD": "BUILD", |
| 76 | "ERROR": "ERROR", |
| 77 | "DELETED": "DELETED", |
| 78 | } |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 79 | |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 80 | supportedClassificationTypes = ["legacy_flow_classifier"] |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 81 | |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 82 | # global var to have a timeout creating and deleting volumes |
| garciadeblas | 64b39c5 | 2020-05-21 08:07:25 +0000 | [diff] [blame] | 83 | volume_timeout = 1800 |
| 84 | server_timeout = 1800 |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 85 | |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 86 | |
| 87 | class SafeDumper(yaml.SafeDumper): |
| 88 | def represent_data(self, data): |
| 89 | # Openstack APIs use custom subclasses of dict and YAML safe dumper |
| 90 | # is designed to not handle that (reference issue 142 of pyyaml) |
| 91 | if isinstance(data, dict) and data.__class__ != dict: |
| 92 | # A simple solution is to convert those items back to dicts |
| 93 | data = dict(data.items()) |
| 94 | |
| 95 | return super(SafeDumper, self).represent_data(data) |
| 96 | |
| 97 | |
| tierno | 7277486 | 2020-05-04 11:44:15 +0000 | [diff] [blame] | 98 | class vimconnector(vimconn.VimConnector): |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 99 | def __init__( |
| 100 | self, |
| 101 | uuid, |
| 102 | name, |
| 103 | tenant_id, |
| 104 | tenant_name, |
| 105 | url, |
| 106 | url_admin=None, |
| 107 | user=None, |
| 108 | passwd=None, |
| 109 | log_level=None, |
| 110 | config={}, |
| 111 | persistent_info={}, |
| 112 | ): |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 113 | """using common constructor parameters. In this case |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 114 | 'url' is the keystone authorization url, |
| 115 | 'url_admin' is not use |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 116 | """ |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 117 | api_version = config.get("APIversion") |
| kate | 721d79b | 2017-06-24 04:21:38 -0700 | [diff] [blame] | 118 | |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 119 | if api_version and api_version not in ("v3.3", "v2.0", "2", "3"): |
| 120 | raise vimconn.VimConnException( |
| 121 | "Invalid value '{}' for config:APIversion. " |
| 122 | "Allowed values are 'v3.3', 'v2.0', '2' or '3'".format(api_version) |
| 123 | ) |
| 124 | |
| 125 | vim_type = config.get("vim_type") |
| 126 | |
| 127 | if vim_type and vim_type not in ("vio", "VIO"): |
| 128 | raise vimconn.VimConnException( |
| 129 | "Invalid value '{}' for config:vim_type." |
| 130 | "Allowed values are 'vio' or 'VIO'".format(vim_type) |
| 131 | ) |
| 132 | |
| 133 | if config.get("dataplane_net_vlan_range") is not None: |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 134 | # validate vlan ranges provided by user |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 135 | self._validate_vlan_ranges( |
| 136 | config.get("dataplane_net_vlan_range"), "dataplane_net_vlan_range" |
| 137 | ) |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame] | 138 | |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 139 | if config.get("multisegment_vlan_range") is not None: |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 140 | # validate vlan ranges provided by user |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 141 | self._validate_vlan_ranges( |
| 142 | config.get("multisegment_vlan_range"), "multisegment_vlan_range" |
| 143 | ) |
| kate | 721d79b | 2017-06-24 04:21:38 -0700 | [diff] [blame] | 144 | |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 145 | vimconn.VimConnector.__init__( |
| 146 | self, |
| 147 | uuid, |
| 148 | name, |
| 149 | tenant_id, |
| 150 | tenant_name, |
| 151 | url, |
| 152 | url_admin, |
| 153 | user, |
| 154 | passwd, |
| 155 | log_level, |
| 156 | config, |
| 157 | ) |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 158 | |
| tierno | 4d1ce22 | 2018-04-06 10:41:06 +0200 | [diff] [blame] | 159 | if self.config.get("insecure") and self.config.get("ca_cert"): |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 160 | raise vimconn.VimConnException( |
| 161 | "options insecure and ca_cert are mutually exclusive" |
| 162 | ) |
| 163 | |
| tierno | 4d1ce22 | 2018-04-06 10:41:06 +0200 | [diff] [blame] | 164 | self.verify = True |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 165 | |
| tierno | 4d1ce22 | 2018-04-06 10:41:06 +0200 | [diff] [blame] | 166 | if self.config.get("insecure"): |
| 167 | self.verify = False |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 168 | |
| tierno | 4d1ce22 | 2018-04-06 10:41:06 +0200 | [diff] [blame] | 169 | if self.config.get("ca_cert"): |
| 170 | self.verify = self.config.get("ca_cert") |
| tierno | 4d1ce22 | 2018-04-06 10:41:06 +0200 | [diff] [blame] | 171 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 172 | if not url: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 173 | raise TypeError("url param can not be NoneType") |
| 174 | |
| tierno | b5cef37 | 2017-06-19 15:52:22 +0200 | [diff] [blame] | 175 | self.persistent_info = persistent_info |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 176 | self.availability_zone = persistent_info.get("availability_zone", None) |
| 177 | self.session = persistent_info.get("session", {"reload_client": True}) |
| 178 | self.my_tenant_id = self.session.get("my_tenant_id") |
| 179 | self.nova = self.session.get("nova") |
| 180 | self.neutron = self.session.get("neutron") |
| 181 | self.cinder = self.session.get("cinder") |
| 182 | self.glance = self.session.get("glance") |
| 183 | # self.glancev1 = self.session.get("glancev1") |
| 184 | self.keystone = self.session.get("keystone") |
| 185 | self.api_version3 = self.session.get("api_version3") |
| kate | 721d79b | 2017-06-24 04:21:38 -0700 | [diff] [blame] | 186 | self.vim_type = self.config.get("vim_type") |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 187 | |
| kate | 721d79b | 2017-06-24 04:21:38 -0700 | [diff] [blame] | 188 | if self.vim_type: |
| 189 | self.vim_type = self.vim_type.upper() |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 190 | |
| kate | 721d79b | 2017-06-24 04:21:38 -0700 | [diff] [blame] | 191 | if self.config.get("use_internal_endpoint"): |
| 192 | self.endpoint_type = "internalURL" |
| 193 | else: |
| 194 | self.endpoint_type = None |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 195 | |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 196 | logging.getLogger("urllib3").setLevel(logging.WARNING) |
| 197 | logging.getLogger("keystoneauth").setLevel(logging.WARNING) |
| 198 | logging.getLogger("novaclient").setLevel(logging.WARNING) |
| 199 | self.logger = logging.getLogger("ro.vim.openstack") |
| kate | 721d79b | 2017-06-24 04:21:38 -0700 | [diff] [blame] | 200 | |
| tierno | a05b65a | 2019-02-01 12:30:27 +0000 | [diff] [blame] | 201 | # allow security_groups to be a list or a single string |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 202 | if isinstance(self.config.get("security_groups"), str): |
| 203 | self.config["security_groups"] = [self.config["security_groups"]] |
| 204 | |
| tierno | a05b65a | 2019-02-01 12:30:27 +0000 | [diff] [blame] | 205 | self.security_groups_id = None |
| 206 | |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 207 | # ###### VIO Specific Changes ######### |
| kate | 721d79b | 2017-06-24 04:21:38 -0700 | [diff] [blame] | 208 | if self.vim_type == "VIO": |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 209 | self.logger = logging.getLogger("ro.vim.vio") |
| kate | 721d79b | 2017-06-24 04:21:38 -0700 | [diff] [blame] | 210 | |
| tierno | fe78990 | 2016-09-29 14:20:44 +0000 | [diff] [blame] | 211 | if log_level: |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 212 | self.logger.setLevel(getattr(logging, log_level)) |
| tierno | f716aea | 2017-06-21 18:01:40 +0200 | [diff] [blame] | 213 | |
| 214 | def __getitem__(self, index): |
| 215 | """Get individuals parameters. |
| 216 | Throw KeyError""" |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 217 | if index == "project_domain_id": |
| tierno | f716aea | 2017-06-21 18:01:40 +0200 | [diff] [blame] | 218 | return self.config.get("project_domain_id") |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 219 | elif index == "user_domain_id": |
| tierno | f716aea | 2017-06-21 18:01:40 +0200 | [diff] [blame] | 220 | return self.config.get("user_domain_id") |
| 221 | else: |
| tierno | 7277486 | 2020-05-04 11:44:15 +0000 | [diff] [blame] | 222 | return vimconn.VimConnector.__getitem__(self, index) |
| tierno | f716aea | 2017-06-21 18:01:40 +0200 | [diff] [blame] | 223 | |
| 224 | def __setitem__(self, index, value): |
| 225 | """Set individuals parameters and it is marked as dirty so to force connection reload. |
| 226 | Throw KeyError""" |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 227 | if index == "project_domain_id": |
| tierno | f716aea | 2017-06-21 18:01:40 +0200 | [diff] [blame] | 228 | self.config["project_domain_id"] = value |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 229 | elif index == "user_domain_id": |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 230 | self.config["user_domain_id"] = value |
| tierno | f716aea | 2017-06-21 18:01:40 +0200 | [diff] [blame] | 231 | else: |
| tierno | 7277486 | 2020-05-04 11:44:15 +0000 | [diff] [blame] | 232 | vimconn.VimConnector.__setitem__(self, index, value) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 233 | |
| 234 | self.session["reload_client"] = True |
| tierno | f716aea | 2017-06-21 18:01:40 +0200 | [diff] [blame] | 235 | |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 236 | def serialize(self, value): |
| 237 | """Serialization of python basic types. |
| 238 | |
| 239 | In the case value is not serializable a message will be logged and a |
| 240 | simple representation of the data that cannot be converted back to |
| 241 | python is returned. |
| 242 | """ |
| tierno | 7d782ef | 2019-10-04 12:56:31 +0000 | [diff] [blame] | 243 | if isinstance(value, str): |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 244 | return value |
| 245 | |
| 246 | try: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 247 | return yaml.dump( |
| 248 | value, Dumper=SafeDumper, default_flow_style=True, width=256 |
| 249 | ) |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 250 | except yaml.representer.RepresenterError: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 251 | self.logger.debug( |
| 252 | "The following entity cannot be serialized in YAML:\n\n%s\n\n", |
| 253 | pformat(value), |
| 254 | exc_info=True, |
| 255 | ) |
| 256 | |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 257 | return str(value) |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 258 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 259 | def _reload_connection(self): |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 260 | """Called before any operation, it check if credentials has changed |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 261 | Throw keystoneclient.apiclient.exceptions.AuthorizationFailure |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 262 | """ |
| 263 | # TODO control the timing and possible token timeout, but it seams that python client does this task for us :-) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 264 | if self.session["reload_client"]: |
| 265 | if self.config.get("APIversion"): |
| 266 | self.api_version3 = ( |
| 267 | self.config["APIversion"] == "v3.3" |
| 268 | or self.config["APIversion"] == "3" |
| 269 | ) |
| tierno | f716aea | 2017-06-21 18:01:40 +0200 | [diff] [blame] | 270 | else: # get from ending auth_url that end with v3 or with v2.0 |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 271 | self.api_version3 = self.url.endswith("/v3") or self.url.endswith( |
| 272 | "/v3/" |
| 273 | ) |
| 274 | |
| 275 | self.session["api_version3"] = self.api_version3 |
| 276 | |
| tierno | f716aea | 2017-06-21 18:01:40 +0200 | [diff] [blame] | 277 | if self.api_version3: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 278 | if self.config.get("project_domain_id") or self.config.get( |
| 279 | "project_domain_name" |
| 280 | ): |
| tierno | 3cb8dc3 | 2017-10-24 18:13:19 +0200 | [diff] [blame] | 281 | project_domain_id_default = None |
| 282 | else: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 283 | project_domain_id_default = "default" |
| 284 | |
| 285 | if self.config.get("user_domain_id") or self.config.get( |
| 286 | "user_domain_name" |
| 287 | ): |
| tierno | 3cb8dc3 | 2017-10-24 18:13:19 +0200 | [diff] [blame] | 288 | user_domain_id_default = None |
| 289 | else: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 290 | user_domain_id_default = "default" |
| 291 | auth = v3.Password( |
| 292 | auth_url=self.url, |
| 293 | username=self.user, |
| 294 | password=self.passwd, |
| 295 | project_name=self.tenant_name, |
| 296 | project_id=self.tenant_id, |
| 297 | project_domain_id=self.config.get( |
| 298 | "project_domain_id", project_domain_id_default |
| 299 | ), |
| 300 | user_domain_id=self.config.get( |
| 301 | "user_domain_id", user_domain_id_default |
| 302 | ), |
| 303 | project_domain_name=self.config.get("project_domain_name"), |
| 304 | user_domain_name=self.config.get("user_domain_name"), |
| 305 | ) |
| ahmadsa | 95baa27 | 2016-11-30 09:14:11 +0500 | [diff] [blame] | 306 | else: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 307 | auth = v2.Password( |
| 308 | auth_url=self.url, |
| 309 | username=self.user, |
| 310 | password=self.passwd, |
| 311 | tenant_name=self.tenant_name, |
| 312 | tenant_id=self.tenant_id, |
| 313 | ) |
| 314 | |
| tierno | 4d1ce22 | 2018-04-06 10:41:06 +0200 | [diff] [blame] | 315 | sess = session.Session(auth=auth, verify=self.verify) |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 316 | # addedd region_name to keystone, nova, neutron and cinder to support distributed cloud for Wind River |
| 317 | # Titanium cloud and StarlingX |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 318 | region_name = self.config.get("region_name") |
| 319 | |
| tierno | f716aea | 2017-06-21 18:01:40 +0200 | [diff] [blame] | 320 | if self.api_version3: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 321 | self.keystone = ksClient_v3.Client( |
| 322 | session=sess, |
| 323 | endpoint_type=self.endpoint_type, |
| 324 | region_name=region_name, |
| 325 | ) |
| tierno | f716aea | 2017-06-21 18:01:40 +0200 | [diff] [blame] | 326 | else: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 327 | self.keystone = ksClient_v2.Client( |
| 328 | session=sess, endpoint_type=self.endpoint_type |
| 329 | ) |
| 330 | |
| 331 | self.session["keystone"] = self.keystone |
| 332 | # In order to enable microversion functionality an explicit microversion must be specified in "config". |
| montesmoreno | 9317d30 | 2017-08-16 12:48:23 +0200 | [diff] [blame] | 333 | # This implementation approach is due to the warning message in |
| 334 | # https://developer.openstack.org/api-guide/compute/microversions.html |
| 335 | # where it is stated that microversion backwards compatibility is not guaranteed and clients should |
| 336 | # always require an specific microversion. |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 337 | # To be able to use "device role tagging" functionality define "microversion: 2.32" in datacenter config |
| montesmoreno | 9317d30 | 2017-08-16 12:48:23 +0200 | [diff] [blame] | 338 | version = self.config.get("microversion") |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 339 | |
| montesmoreno | 9317d30 | 2017-08-16 12:48:23 +0200 | [diff] [blame] | 340 | if not version: |
| 341 | version = "2.1" |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 342 | |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 343 | # addedd region_name to keystone, nova, neutron and cinder to support distributed cloud for Wind River |
| 344 | # Titanium cloud and StarlingX |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 345 | self.nova = self.session["nova"] = nClient.Client( |
| 346 | str(version), |
| 347 | session=sess, |
| 348 | endpoint_type=self.endpoint_type, |
| 349 | region_name=region_name, |
| 350 | ) |
| 351 | self.neutron = self.session["neutron"] = neClient.Client( |
| 352 | "2.0", |
| 353 | session=sess, |
| 354 | endpoint_type=self.endpoint_type, |
| 355 | region_name=region_name, |
| 356 | ) |
| 357 | self.cinder = self.session["cinder"] = cClient.Client( |
| 358 | 2, |
| 359 | session=sess, |
| 360 | endpoint_type=self.endpoint_type, |
| 361 | region_name=region_name, |
| 362 | ) |
| 363 | |
| tierno | a05b65a | 2019-02-01 12:30:27 +0000 | [diff] [blame] | 364 | try: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 365 | self.my_tenant_id = self.session["my_tenant_id"] = sess.get_project_id() |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 366 | except Exception: |
| tierno | a05b65a | 2019-02-01 12:30:27 +0000 | [diff] [blame] | 367 | self.logger.error("Cannot get project_id from session", exc_info=True) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 368 | |
| kate | 721d79b | 2017-06-24 04:21:38 -0700 | [diff] [blame] | 369 | if self.endpoint_type == "internalURL": |
| 370 | glance_service_id = self.keystone.services.list(name="glance")[0].id |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 371 | glance_endpoint = self.keystone.endpoints.list( |
| 372 | glance_service_id, interface="internal" |
| 373 | )[0].url |
| kate | 721d79b | 2017-06-24 04:21:38 -0700 | [diff] [blame] | 374 | else: |
| 375 | glance_endpoint = None |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 376 | |
| 377 | self.glance = self.session["glance"] = glClient.Client( |
| 378 | 2, session=sess, endpoint=glance_endpoint |
| 379 | ) |
| tierno | a05b65a | 2019-02-01 12:30:27 +0000 | [diff] [blame] | 380 | # using version 1 of glance client in new_image() |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 381 | # self.glancev1 = self.session["glancev1"] = glClient.Client("1", session=sess, |
| tierno | 1beea86 | 2018-07-11 15:47:37 +0200 | [diff] [blame] | 382 | # endpoint=glance_endpoint) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 383 | self.session["reload_client"] = False |
| 384 | self.persistent_info["session"] = self.session |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 385 | # add availablity zone info inside self.persistent_info |
| 386 | self._set_availablity_zones() |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 387 | self.persistent_info["availability_zone"] = self.availability_zone |
| 388 | # force to get again security_groups_ids next time they are needed |
| 389 | self.security_groups_id = None |
| ahmadsa | 95baa27 | 2016-11-30 09:14:11 +0500 | [diff] [blame] | 390 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 391 | def __net_os2mano(self, net_list_dict): |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 392 | """Transform the net openstack format to mano format |
| 393 | net_list_dict can be a list of dict or a single dict""" |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 394 | if type(net_list_dict) is dict: |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 395 | net_list_ = (net_list_dict,) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 396 | elif type(net_list_dict) is list: |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 397 | net_list_ = net_list_dict |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 398 | else: |
| 399 | raise TypeError("param net_list_dict must be a list or a dictionary") |
| 400 | for net in net_list_: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 401 | if net.get("provider:network_type") == "vlan": |
| 402 | net["type"] = "data" |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 403 | else: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 404 | net["type"] = "bridge" |
| Pablo Montes Moreno | 6a7785b | 2017-07-03 10:44:30 +0200 | [diff] [blame] | 405 | |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 406 | def __classification_os2mano(self, class_list_dict): |
| 407 | """Transform the openstack format (Flow Classifier) to mano format |
| 408 | (Classification) class_list_dict can be a list of dict or a single dict |
| 409 | """ |
| 410 | if isinstance(class_list_dict, dict): |
| 411 | class_list_ = [class_list_dict] |
| 412 | elif isinstance(class_list_dict, list): |
| 413 | class_list_ = class_list_dict |
| 414 | else: |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 415 | raise TypeError("param class_list_dict must be a list or a dictionary") |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 416 | for classification in class_list_: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 417 | id = classification.pop("id") |
| 418 | name = classification.pop("name") |
| 419 | description = classification.pop("description") |
| 420 | project_id = classification.pop("project_id") |
| 421 | tenant_id = classification.pop("tenant_id") |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 422 | original_classification = copy.deepcopy(classification) |
| 423 | classification.clear() |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 424 | classification["ctype"] = "legacy_flow_classifier" |
| 425 | classification["definition"] = original_classification |
| 426 | classification["id"] = id |
| 427 | classification["name"] = name |
| 428 | classification["description"] = description |
| 429 | classification["project_id"] = project_id |
| 430 | classification["tenant_id"] = tenant_id |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 431 | |
| 432 | def __sfi_os2mano(self, sfi_list_dict): |
| 433 | """Transform the openstack format (Port Pair) to mano format (SFI) |
| 434 | sfi_list_dict can be a list of dict or a single dict |
| 435 | """ |
| 436 | if isinstance(sfi_list_dict, dict): |
| 437 | sfi_list_ = [sfi_list_dict] |
| 438 | elif isinstance(sfi_list_dict, list): |
| 439 | sfi_list_ = sfi_list_dict |
| 440 | else: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 441 | raise TypeError("param sfi_list_dict must be a list or a dictionary") |
| 442 | |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 443 | for sfi in sfi_list_: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 444 | sfi["ingress_ports"] = [] |
| 445 | sfi["egress_ports"] = [] |
| 446 | |
| 447 | if sfi.get("ingress"): |
| 448 | sfi["ingress_ports"].append(sfi["ingress"]) |
| 449 | |
| 450 | if sfi.get("egress"): |
| 451 | sfi["egress_ports"].append(sfi["egress"]) |
| 452 | |
| 453 | del sfi["ingress"] |
| 454 | del sfi["egress"] |
| 455 | params = sfi.get("service_function_parameters") |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 456 | sfc_encap = False |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 457 | |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 458 | if params: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 459 | correlation = params.get("correlation") |
| 460 | |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 461 | if correlation: |
| 462 | sfc_encap = True |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 463 | |
| 464 | sfi["sfc_encap"] = sfc_encap |
| 465 | del sfi["service_function_parameters"] |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 466 | |
| 467 | def __sf_os2mano(self, sf_list_dict): |
| 468 | """Transform the openstack format (Port Pair Group) to mano format (SF) |
| 469 | sf_list_dict can be a list of dict or a single dict |
| 470 | """ |
| 471 | if isinstance(sf_list_dict, dict): |
| 472 | sf_list_ = [sf_list_dict] |
| 473 | elif isinstance(sf_list_dict, list): |
| 474 | sf_list_ = sf_list_dict |
| 475 | else: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 476 | raise TypeError("param sf_list_dict must be a list or a dictionary") |
| 477 | |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 478 | for sf in sf_list_: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 479 | del sf["port_pair_group_parameters"] |
| 480 | sf["sfis"] = sf["port_pairs"] |
| 481 | del sf["port_pairs"] |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 482 | |
| 483 | def __sfp_os2mano(self, sfp_list_dict): |
| 484 | """Transform the openstack format (Port Chain) to mano format (SFP) |
| 485 | sfp_list_dict can be a list of dict or a single dict |
| 486 | """ |
| 487 | if isinstance(sfp_list_dict, dict): |
| 488 | sfp_list_ = [sfp_list_dict] |
| 489 | elif isinstance(sfp_list_dict, list): |
| 490 | sfp_list_ = sfp_list_dict |
| 491 | else: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 492 | raise TypeError("param sfp_list_dict must be a list or a dictionary") |
| 493 | |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 494 | for sfp in sfp_list_: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 495 | params = sfp.pop("chain_parameters") |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 496 | sfc_encap = False |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 497 | |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 498 | if params: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 499 | correlation = params.get("correlation") |
| 500 | |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 501 | if correlation: |
| 502 | sfc_encap = True |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 503 | |
| 504 | sfp["sfc_encap"] = sfc_encap |
| 505 | sfp["spi"] = sfp.pop("chain_id") |
| 506 | sfp["classifications"] = sfp.pop("flow_classifiers") |
| 507 | sfp["service_functions"] = sfp.pop("port_pair_groups") |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 508 | |
| 509 | # placeholder for now; read TODO note below |
| 510 | def _validate_classification(self, type, definition): |
| 511 | # only legacy_flow_classifier Type is supported at this point |
| 512 | return True |
| 513 | # TODO(igordcard): this method should be an abstract method of an |
| 514 | # abstract Classification class to be implemented by the specific |
| 515 | # Types. Also, abstract vimconnector should call the validation |
| 516 | # method before the implemented VIM connectors are called. |
| 517 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 518 | def _format_exception(self, exception): |
| tierno | 6964779 | 2020-03-05 16:45:48 +0000 | [diff] [blame] | 519 | """Transform a keystone, nova, neutron exception into a vimconn exception discovering the cause""" |
| tierno | 6964779 | 2020-03-05 16:45:48 +0000 | [diff] [blame] | 520 | message_error = str(exception) |
| tierno | 5ad826a | 2020-08-11 11:19:44 +0000 | [diff] [blame] | 521 | tip = "" |
| tierno | de12f78 | 2019-04-05 12:46:42 +0000 | [diff] [blame] | 522 | |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 523 | if isinstance( |
| 524 | exception, |
| 525 | ( |
| 526 | neExceptions.NetworkNotFoundClient, |
| 527 | nvExceptions.NotFound, |
| 528 | ksExceptions.NotFound, |
| 529 | gl1Exceptions.HTTPNotFound, |
| 530 | ), |
| 531 | ): |
| 532 | raise vimconn.VimConnNotFoundException( |
| 533 | type(exception).__name__ + ": " + message_error |
| 534 | ) |
| 535 | elif isinstance( |
| 536 | exception, |
| 537 | ( |
| 538 | HTTPException, |
| 539 | gl1Exceptions.HTTPException, |
| 540 | gl1Exceptions.CommunicationError, |
| 541 | ConnectionError, |
| 542 | ksExceptions.ConnectionError, |
| 543 | neExceptions.ConnectionFailed, |
| 544 | ), |
| 545 | ): |
| tierno | 5ad826a | 2020-08-11 11:19:44 +0000 | [diff] [blame] | 546 | if type(exception).__name__ == "SSLError": |
| 547 | tip = " (maybe option 'insecure' must be added to the VIM)" |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 548 | |
| 549 | raise vimconn.VimConnConnectionException( |
| 550 | "Invalid URL or credentials{}: {}".format(tip, message_error) |
| 551 | ) |
| 552 | elif isinstance( |
| 553 | exception, |
| 554 | ( |
| 555 | KeyError, |
| 556 | nvExceptions.BadRequest, |
| 557 | ksExceptions.BadRequest, |
| 558 | ), |
| 559 | ): |
| 560 | raise vimconn.VimConnException( |
| 561 | type(exception).__name__ + ": " + message_error |
| 562 | ) |
| 563 | elif isinstance( |
| 564 | exception, |
| 565 | ( |
| 566 | nvExceptions.ClientException, |
| 567 | ksExceptions.ClientException, |
| 568 | neExceptions.NeutronException, |
| 569 | ), |
| 570 | ): |
| 571 | raise vimconn.VimConnUnexpectedResponse( |
| 572 | type(exception).__name__ + ": " + message_error |
| 573 | ) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 574 | elif isinstance(exception, nvExceptions.Conflict): |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 575 | raise vimconn.VimConnConflictException( |
| 576 | type(exception).__name__ + ": " + message_error |
| 577 | ) |
| tierno | 7277486 | 2020-05-04 11:44:15 +0000 | [diff] [blame] | 578 | elif isinstance(exception, vimconn.VimConnException): |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 579 | raise exception |
| tierno | f716aea | 2017-06-21 18:01:40 +0200 | [diff] [blame] | 580 | else: # () |
| tierno | de12f78 | 2019-04-05 12:46:42 +0000 | [diff] [blame] | 581 | self.logger.error("General Exception " + message_error, exc_info=True) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 582 | |
| 583 | raise vimconn.VimConnConnectionException( |
| 584 | type(exception).__name__ + ": " + message_error |
| 585 | ) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 586 | |
| tierno | a05b65a | 2019-02-01 12:30:27 +0000 | [diff] [blame] | 587 | def _get_ids_from_name(self): |
| 588 | """ |
| 589 | Obtain ids from name of tenant and security_groups. Store at self .security_groups_id" |
| 590 | :return: None |
| 591 | """ |
| 592 | # get tenant_id if only tenant_name is supplied |
| 593 | self._reload_connection() |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 594 | |
| tierno | a05b65a | 2019-02-01 12:30:27 +0000 | [diff] [blame] | 595 | if not self.my_tenant_id: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 596 | raise vimconn.VimConnConnectionException( |
| 597 | "Error getting tenant information from name={} id={}".format( |
| 598 | self.tenant_name, self.tenant_id |
| 599 | ) |
| 600 | ) |
| 601 | |
| 602 | if self.config.get("security_groups") and not self.security_groups_id: |
| tierno | a05b65a | 2019-02-01 12:30:27 +0000 | [diff] [blame] | 603 | # convert from name to id |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 604 | neutron_sg_list = self.neutron.list_security_groups( |
| 605 | tenant_id=self.my_tenant_id |
| 606 | )["security_groups"] |
| tierno | a05b65a | 2019-02-01 12:30:27 +0000 | [diff] [blame] | 607 | |
| 608 | self.security_groups_id = [] |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 609 | for sg in self.config.get("security_groups"): |
| tierno | a05b65a | 2019-02-01 12:30:27 +0000 | [diff] [blame] | 610 | for neutron_sg in neutron_sg_list: |
| 611 | if sg in (neutron_sg["id"], neutron_sg["name"]): |
| 612 | self.security_groups_id.append(neutron_sg["id"]) |
| 613 | break |
| 614 | else: |
| 615 | self.security_groups_id = None |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 616 | |
| 617 | raise vimconn.VimConnConnectionException( |
| 618 | "Not found security group {} for this tenant".format(sg) |
| 619 | ) |
| tierno | a05b65a | 2019-02-01 12:30:27 +0000 | [diff] [blame] | 620 | |
| tierno | 5509c2e | 2019-07-04 16:23:20 +0000 | [diff] [blame] | 621 | def check_vim_connectivity(self): |
| 622 | # just get network list to check connectivity and credentials |
| 623 | self.get_network_list(filter_dict={}) |
| 624 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 625 | def get_tenant_list(self, filter_dict={}): |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 626 | """Obtain tenants of VIM |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 627 | filter_dict can contain the following keys: |
| 628 | name: filter by tenant name |
| 629 | id: filter by tenant uuid/id |
| 630 | <other VIM specific> |
| 631 | Returns the tenant list of dictionaries: [{'name':'<name>, 'id':'<id>, ...}, ...] |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 632 | """ |
| ahmadsa | 95baa27 | 2016-11-30 09:14:11 +0500 | [diff] [blame] | 633 | self.logger.debug("Getting tenants from VIM filter: '%s'", str(filter_dict)) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 634 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 635 | try: |
| 636 | self._reload_connection() |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 637 | |
| tierno | f716aea | 2017-06-21 18:01:40 +0200 | [diff] [blame] | 638 | if self.api_version3: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 639 | project_class_list = self.keystone.projects.list( |
| 640 | name=filter_dict.get("name") |
| 641 | ) |
| ahmadsa | 95baa27 | 2016-11-30 09:14:11 +0500 | [diff] [blame] | 642 | else: |
| tierno | f716aea | 2017-06-21 18:01:40 +0200 | [diff] [blame] | 643 | project_class_list = self.keystone.tenants.findall(**filter_dict) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 644 | |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 645 | project_list = [] |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 646 | |
| ahmadsa | 95baa27 | 2016-11-30 09:14:11 +0500 | [diff] [blame] | 647 | for project in project_class_list: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 648 | if filter_dict.get("id") and filter_dict["id"] != project.id: |
| tierno | f716aea | 2017-06-21 18:01:40 +0200 | [diff] [blame] | 649 | continue |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 650 | |
| ahmadsa | 95baa27 | 2016-11-30 09:14:11 +0500 | [diff] [blame] | 651 | project_list.append(project.to_dict()) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 652 | |
| ahmadsa | 95baa27 | 2016-11-30 09:14:11 +0500 | [diff] [blame] | 653 | return project_list |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 654 | except ( |
| 655 | ksExceptions.ConnectionError, |
| 656 | ksExceptions.ClientException, |
| 657 | ConnectionError, |
| 658 | ) as e: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 659 | self._format_exception(e) |
| 660 | |
| 661 | def new_tenant(self, tenant_name, tenant_description): |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 662 | """Adds a new tenant to openstack VIM. Returns the tenant identifier""" |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 663 | self.logger.debug("Adding a new tenant name: %s", tenant_name) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 664 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 665 | try: |
| 666 | self._reload_connection() |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 667 | |
| tierno | f716aea | 2017-06-21 18:01:40 +0200 | [diff] [blame] | 668 | if self.api_version3: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 669 | project = self.keystone.projects.create( |
| 670 | tenant_name, |
| 671 | self.config.get("project_domain_id", "default"), |
| 672 | description=tenant_description, |
| 673 | is_domain=False, |
| 674 | ) |
| ahmadsa | 95baa27 | 2016-11-30 09:14:11 +0500 | [diff] [blame] | 675 | else: |
| tierno | f716aea | 2017-06-21 18:01:40 +0200 | [diff] [blame] | 676 | project = self.keystone.tenants.create(tenant_name, tenant_description) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 677 | |
| ahmadsa | 95baa27 | 2016-11-30 09:14:11 +0500 | [diff] [blame] | 678 | return project.id |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 679 | except ( |
| 680 | ksExceptions.ConnectionError, |
| 681 | ksExceptions.ClientException, |
| 682 | ksExceptions.BadRequest, |
| 683 | ConnectionError, |
| 684 | ) as e: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 685 | self._format_exception(e) |
| 686 | |
| 687 | def delete_tenant(self, tenant_id): |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 688 | """Delete a tenant from openstack VIM. Returns the old tenant identifier""" |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 689 | self.logger.debug("Deleting tenant %s from VIM", tenant_id) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 690 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 691 | try: |
| 692 | self._reload_connection() |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 693 | |
| tierno | f716aea | 2017-06-21 18:01:40 +0200 | [diff] [blame] | 694 | if self.api_version3: |
| ahmadsa | 95baa27 | 2016-11-30 09:14:11 +0500 | [diff] [blame] | 695 | self.keystone.projects.delete(tenant_id) |
| 696 | else: |
| 697 | self.keystone.tenants.delete(tenant_id) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 698 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 699 | return tenant_id |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 700 | except ( |
| 701 | ksExceptions.ConnectionError, |
| 702 | ksExceptions.ClientException, |
| 703 | ksExceptions.NotFound, |
| 704 | ConnectionError, |
| 705 | ) as e: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 706 | self._format_exception(e) |
| ahmadsa | 95baa27 | 2016-11-30 09:14:11 +0500 | [diff] [blame] | 707 | |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 708 | def new_network( |
| 709 | self, |
| 710 | net_name, |
| 711 | net_type, |
| 712 | ip_profile=None, |
| 713 | shared=False, |
| 714 | provider_network_profile=None, |
| 715 | ): |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame] | 716 | """Adds a tenant network to VIM |
| 717 | Params: |
| 718 | 'net_name': name of the network |
| 719 | 'net_type': one of: |
| 720 | 'bridge': overlay isolated network |
| 721 | 'data': underlay E-LAN network for Passthrough and SRIOV interfaces |
| 722 | 'ptp': underlay E-LINE network for Passthrough and SRIOV interfaces. |
| 723 | 'ip_profile': is a dict containing the IP parameters of the network |
| 724 | 'ip_version': can be "IPv4" or "IPv6" (Currently only IPv4 is implemented) |
| 725 | 'subnet_address': ip_prefix_schema, that is X.X.X.X/Y |
| 726 | 'gateway_address': (Optional) ip_schema, that is X.X.X.X |
| 727 | 'dns_address': (Optional) comma separated list of ip_schema, e.g. X.X.X.X[,X,X,X,X] |
| 728 | 'dhcp_enabled': True or False |
| 729 | 'dhcp_start_address': ip_schema, first IP to grant |
| 730 | 'dhcp_count': number of IPs to grant. |
| 731 | 'shared': if this network can be seen/use by other tenants/organization |
| garciadeblas | 4af0d54 | 2020-02-18 16:01:13 +0100 | [diff] [blame] | 732 | 'provider_network_profile': (optional) contains {segmentation-id: vlan, network-type: vlan|vxlan, |
| 733 | physical-network: physnet-label} |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame] | 734 | Returns a tuple with the network identifier and created_items, or raises an exception on error |
| 735 | created_items can be None or a dictionary where this method can include key-values that will be passed to |
| 736 | the method delete_network. Can be used to store created segments, created l2gw connections, etc. |
| 737 | Format is vimconnector dependent, but do not use nested dictionaries and a value of None should be the same |
| 738 | as not present. |
| 739 | """ |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 740 | self.logger.debug( |
| 741 | "Adding a new network to VIM name '%s', type '%s'", net_name, net_type |
| 742 | ) |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame] | 743 | # self.logger.debug(">>>>>>>>>>>>>>>>>> IP profile %s", str(ip_profile)) |
| kbsub | a85c54d | 2019-10-17 16:30:32 +0000 | [diff] [blame] | 744 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 745 | try: |
| kbsub | a85c54d | 2019-10-17 16:30:32 +0000 | [diff] [blame] | 746 | vlan = None |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 747 | |
| kbsub | a85c54d | 2019-10-17 16:30:32 +0000 | [diff] [blame] | 748 | if provider_network_profile: |
| 749 | vlan = provider_network_profile.get("segmentation-id") |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 750 | |
| garciadeblas | edca7b3 | 2016-09-29 14:01:52 +0000 | [diff] [blame] | 751 | new_net = None |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame] | 752 | created_items = {} |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 753 | self._reload_connection() |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 754 | network_dict = {"name": net_name, "admin_state_up": True} |
| 755 | |
| Gabriel Cuba | b22d91e | 2022-12-14 18:33:50 -0500 | [diff] [blame] | 756 | if net_type in ("data", "ptp") or provider_network_profile: |
| tierno | 6869ae7 | 2020-01-09 17:37:34 +0000 | [diff] [blame] | 757 | provider_physical_network = None |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 758 | |
| 759 | if provider_network_profile and provider_network_profile.get( |
| 760 | "physical-network" |
| 761 | ): |
| 762 | provider_physical_network = provider_network_profile.get( |
| 763 | "physical-network" |
| 764 | ) |
| 765 | |
| tierno | 6869ae7 | 2020-01-09 17:37:34 +0000 | [diff] [blame] | 766 | # provider-network must be one of the dataplane_physcial_netowrk if this is a list. If it is string |
| 767 | # or not declared, just ignore the checking |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 768 | if ( |
| 769 | isinstance( |
| 770 | self.config.get("dataplane_physical_net"), (tuple, list) |
| 771 | ) |
| 772 | and provider_physical_network |
| 773 | not in self.config["dataplane_physical_net"] |
| 774 | ): |
| tierno | 7277486 | 2020-05-04 11:44:15 +0000 | [diff] [blame] | 775 | raise vimconn.VimConnConflictException( |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 776 | "Invalid parameter 'provider-network:physical-network' " |
| 777 | "for network creation. '{}' is not one of the declared " |
| 778 | "list at VIM_config:dataplane_physical_net".format( |
| 779 | provider_physical_network |
| 780 | ) |
| 781 | ) |
| 782 | |
| 783 | # use the default dataplane_physical_net |
| 784 | if not provider_physical_network: |
| 785 | provider_physical_network = self.config.get( |
| 786 | "dataplane_physical_net" |
| 787 | ) |
| 788 | |
| tierno | 6869ae7 | 2020-01-09 17:37:34 +0000 | [diff] [blame] | 789 | # if it is non empty list, use the first value. If it is a string use the value directly |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 790 | if ( |
| 791 | isinstance(provider_physical_network, (tuple, list)) |
| 792 | and provider_physical_network |
| 793 | ): |
| tierno | 6869ae7 | 2020-01-09 17:37:34 +0000 | [diff] [blame] | 794 | provider_physical_network = provider_physical_network[0] |
| 795 | |
| 796 | if not provider_physical_network: |
| tierno | 5ad826a | 2020-08-11 11:19:44 +0000 | [diff] [blame] | 797 | raise vimconn.VimConnConflictException( |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 798 | "missing information needed for underlay networks. Provide " |
| 799 | "'dataplane_physical_net' configuration at VIM or use the NS " |
| 800 | "instantiation parameter 'provider-network.physical-network'" |
| 801 | " for the VLD" |
| 802 | ) |
| tierno | 6869ae7 | 2020-01-09 17:37:34 +0000 | [diff] [blame] | 803 | |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 804 | if not self.config.get("multisegment_support"): |
| 805 | network_dict[ |
| 806 | "provider:physical_network" |
| 807 | ] = provider_physical_network |
| 808 | |
| 809 | if ( |
| 810 | provider_network_profile |
| 811 | and "network-type" in provider_network_profile |
| 812 | ): |
| 813 | network_dict[ |
| 814 | "provider:network_type" |
| 815 | ] = provider_network_profile["network-type"] |
| garciadeblas | 4af0d54 | 2020-02-18 16:01:13 +0100 | [diff] [blame] | 816 | else: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 817 | network_dict["provider:network_type"] = self.config.get( |
| 818 | "dataplane_network_type", "vlan" |
| 819 | ) |
| 820 | |
| tierno | 6869ae7 | 2020-01-09 17:37:34 +0000 | [diff] [blame] | 821 | if vlan: |
| 822 | network_dict["provider:segmentation_id"] = vlan |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame] | 823 | else: |
| tierno | 6869ae7 | 2020-01-09 17:37:34 +0000 | [diff] [blame] | 824 | # Multi-segment case |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame] | 825 | segment_list = [] |
| tierno | 6869ae7 | 2020-01-09 17:37:34 +0000 | [diff] [blame] | 826 | segment1_dict = { |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 827 | "provider:physical_network": "", |
| 828 | "provider:network_type": "vxlan", |
| tierno | 6869ae7 | 2020-01-09 17:37:34 +0000 | [diff] [blame] | 829 | } |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame] | 830 | segment_list.append(segment1_dict) |
| tierno | 6869ae7 | 2020-01-09 17:37:34 +0000 | [diff] [blame] | 831 | segment2_dict = { |
| 832 | "provider:physical_network": provider_physical_network, |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 833 | "provider:network_type": "vlan", |
| tierno | 6869ae7 | 2020-01-09 17:37:34 +0000 | [diff] [blame] | 834 | } |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 835 | |
| tierno | 6869ae7 | 2020-01-09 17:37:34 +0000 | [diff] [blame] | 836 | if vlan: |
| 837 | segment2_dict["provider:segmentation_id"] = vlan |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 838 | elif self.config.get("multisegment_vlan_range"): |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame] | 839 | vlanID = self._generate_multisegment_vlanID() |
| 840 | segment2_dict["provider:segmentation_id"] = vlanID |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 841 | |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame] | 842 | # else |
| tierno | 7277486 | 2020-05-04 11:44:15 +0000 | [diff] [blame] | 843 | # raise vimconn.VimConnConflictException( |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 844 | # "You must provide "multisegment_vlan_range" at config dict before creating a multisegment |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 845 | # network") |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame] | 846 | segment_list.append(segment2_dict) |
| 847 | network_dict["segments"] = segment_list |
| kate | 721d79b | 2017-06-24 04:21:38 -0700 | [diff] [blame] | 848 | |
| tierno | 6869ae7 | 2020-01-09 17:37:34 +0000 | [diff] [blame] | 849 | # VIO Specific Changes. It needs a concrete VLAN |
| 850 | if self.vim_type == "VIO" and vlan is None: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 851 | if self.config.get("dataplane_net_vlan_range") is None: |
| tierno | 7277486 | 2020-05-04 11:44:15 +0000 | [diff] [blame] | 852 | raise vimconn.VimConnConflictException( |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 853 | "You must provide 'dataplane_net_vlan_range' in format " |
| 854 | "[start_ID - end_ID] at VIM_config for creating underlay " |
| 855 | "networks" |
| 856 | ) |
| 857 | |
| tierno | 6869ae7 | 2020-01-09 17:37:34 +0000 | [diff] [blame] | 858 | network_dict["provider:segmentation_id"] = self._generate_vlanID() |
| kate | 721d79b | 2017-06-24 04:21:38 -0700 | [diff] [blame] | 859 | |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame] | 860 | network_dict["shared"] = shared |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 861 | |
| anwars | ff16819 | 2019-05-06 11:23:07 +0530 | [diff] [blame] | 862 | if self.config.get("disable_network_port_security"): |
| 863 | network_dict["port_security_enabled"] = False |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 864 | |
| sousaedu | 2aa5f80 | 2021-06-17 15:39:29 +0100 | [diff] [blame] | 865 | if self.config.get("neutron_availability_zone_hints"): |
| 866 | hints = self.config.get("neutron_availability_zone_hints") |
| 867 | |
| 868 | if isinstance(hints, str): |
| 869 | hints = [hints] |
| 870 | |
| 871 | network_dict["availability_zone_hints"] = hints |
| 872 | |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 873 | new_net = self.neutron.create_network({"network": network_dict}) |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame] | 874 | # print new_net |
| 875 | # create subnetwork, even if there is no profile |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 876 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 877 | if not ip_profile: |
| 878 | ip_profile = {} |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 879 | |
| 880 | if not ip_profile.get("subnet_address"): |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 881 | # Fake subnet is required |
| garciadeblas | 2299e3b | 2017-01-26 14:35:55 +0000 | [diff] [blame] | 882 | subnet_rand = random.randint(0, 255) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 883 | ip_profile["subnet_address"] = "192.168.{}.0/24".format(subnet_rand) |
| 884 | |
| 885 | if "ip_version" not in ip_profile: |
| 886 | ip_profile["ip_version"] = "IPv4" |
| 887 | |
| 888 | subnet = { |
| 889 | "name": net_name + "-subnet", |
| 890 | "network_id": new_net["network"]["id"], |
| 891 | "ip_version": 4 if ip_profile["ip_version"] == "IPv4" else 6, |
| 892 | "cidr": ip_profile["subnet_address"], |
| 893 | } |
| 894 | |
| tierno | a1fb446 | 2017-06-30 12:25:50 +0200 | [diff] [blame] | 895 | # Gateway should be set to None if not needed. Otherwise openstack assigns one by default |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 896 | if ip_profile.get("gateway_address"): |
| 897 | subnet["gateway_ip"] = ip_profile["gateway_address"] |
| tierno | 55d234c | 2018-07-04 18:29:21 +0200 | [diff] [blame] | 898 | else: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 899 | subnet["gateway_ip"] = None |
| 900 | |
| 901 | if ip_profile.get("dns_address"): |
| 902 | subnet["dns_nameservers"] = ip_profile["dns_address"].split(";") |
| 903 | |
| 904 | if "dhcp_enabled" in ip_profile: |
| 905 | subnet["enable_dhcp"] = ( |
| 906 | False |
| 907 | if ip_profile["dhcp_enabled"] == "false" |
| 908 | or ip_profile["dhcp_enabled"] is False |
| 909 | else True |
| 910 | ) |
| 911 | |
| 912 | if ip_profile.get("dhcp_start_address"): |
| 913 | subnet["allocation_pools"] = [] |
| 914 | subnet["allocation_pools"].append(dict()) |
| 915 | subnet["allocation_pools"][0]["start"] = ip_profile[ |
| 916 | "dhcp_start_address" |
| 917 | ] |
| 918 | |
| 919 | if ip_profile.get("dhcp_count"): |
| 920 | # parts = ip_profile["dhcp_start_address"].split(".") |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 921 | # ip_int = (int(parts[0]) << 24) + (int(parts[1]) << 16) + (int(parts[2]) << 8) + int(parts[3]) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 922 | ip_int = int(netaddr.IPAddress(ip_profile["dhcp_start_address"])) |
| 923 | ip_int += ip_profile["dhcp_count"] - 1 |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 924 | ip_str = str(netaddr.IPAddress(ip_int)) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 925 | subnet["allocation_pools"][0]["end"] = ip_str |
| 926 | |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 927 | # self.logger.debug(">>>>>>>>>>>>>>>>>> Subnet: %s", str(subnet)) |
| 928 | self.neutron.create_subnet({"subnet": subnet}) |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame] | 929 | |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 930 | if net_type == "data" and self.config.get("multisegment_support"): |
| 931 | if self.config.get("l2gw_support"): |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame] | 932 | l2gw_list = self.neutron.list_l2_gateways().get("l2_gateways", ()) |
| 933 | for l2gw in l2gw_list: |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 934 | l2gw_conn = { |
| 935 | "l2_gateway_id": l2gw["id"], |
| 936 | "network_id": new_net["network"]["id"], |
| 937 | "segmentation_id": str(vlanID), |
| 938 | } |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 939 | new_l2gw_conn = self.neutron.create_l2_gateway_connection( |
| 940 | {"l2_gateway_connection": l2gw_conn} |
| 941 | ) |
| 942 | created_items[ |
| 943 | "l2gwconn:" |
| 944 | + str(new_l2gw_conn["l2_gateway_connection"]["id"]) |
| 945 | ] = True |
| 946 | |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame] | 947 | return new_net["network"]["id"], created_items |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 948 | except Exception as e: |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 949 | # delete l2gw connections (if any) before deleting the network |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame] | 950 | for k, v in created_items.items(): |
| 951 | if not v: # skip already deleted |
| 952 | continue |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 953 | |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame] | 954 | try: |
| 955 | k_item, _, k_id = k.partition(":") |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 956 | |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame] | 957 | if k_item == "l2gwconn": |
| 958 | self.neutron.delete_l2_gateway_connection(k_id) |
| 959 | except Exception as e2: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 960 | self.logger.error( |
| 961 | "Error deleting l2 gateway connection: {}: {}".format( |
| 962 | type(e2).__name__, e2 |
| 963 | ) |
| 964 | ) |
| 965 | |
| garciadeblas | edca7b3 | 2016-09-29 14:01:52 +0000 | [diff] [blame] | 966 | if new_net: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 967 | self.neutron.delete_network(new_net["network"]["id"]) |
| 968 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 969 | self._format_exception(e) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 970 | |
| 971 | def get_network_list(self, filter_dict={}): |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 972 | """Obtain tenant networks of VIM |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 973 | Filter_dict can be: |
| 974 | name: network name |
| 975 | id: network uuid |
| 976 | shared: boolean |
| 977 | tenant_id: tenant |
| 978 | admin_state_up: boolean |
| 979 | status: 'ACTIVE' |
| 980 | Returns the network list of dictionaries |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 981 | """ |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 982 | self.logger.debug("Getting network from VIM filter: '%s'", str(filter_dict)) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 983 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 984 | try: |
| 985 | self._reload_connection() |
| tierno | 69b590e | 2018-03-13 18:52:23 +0100 | [diff] [blame] | 986 | filter_dict_os = filter_dict.copy() |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 987 | |
| tierno | 69b590e | 2018-03-13 18:52:23 +0100 | [diff] [blame] | 988 | if self.api_version3 and "tenant_id" in filter_dict_os: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 989 | # TODO check |
| 990 | filter_dict_os["project_id"] = filter_dict_os.pop("tenant_id") |
| 991 | |
| tierno | 69b590e | 2018-03-13 18:52:23 +0100 | [diff] [blame] | 992 | net_dict = self.neutron.list_networks(**filter_dict_os) |
| tierno | 00e3df7 | 2017-11-29 17:20:13 +0100 | [diff] [blame] | 993 | net_list = net_dict["networks"] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 994 | self.__net_os2mano(net_list) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 995 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 996 | return net_list |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 997 | except ( |
| 998 | neExceptions.ConnectionFailed, |
| 999 | ksExceptions.ClientException, |
| 1000 | neExceptions.NeutronException, |
| 1001 | ConnectionError, |
| 1002 | ) as e: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1003 | self._format_exception(e) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1004 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1005 | def get_network(self, net_id): |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1006 | """Obtain details of network from VIM |
| 1007 | Returns the network information from a network id""" |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1008 | self.logger.debug(" Getting tenant network %s from VIM", net_id) |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1009 | filter_dict = {"id": net_id} |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1010 | net_list = self.get_network_list(filter_dict) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1011 | |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1012 | if len(net_list) == 0: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1013 | raise vimconn.VimConnNotFoundException( |
| 1014 | "Network '{}' not found".format(net_id) |
| 1015 | ) |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1016 | elif len(net_list) > 1: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1017 | raise vimconn.VimConnConflictException( |
| 1018 | "Found more than one network with this criteria" |
| 1019 | ) |
| 1020 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1021 | net = net_list[0] |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1022 | subnets = [] |
| 1023 | for subnet_id in net.get("subnets", ()): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1024 | try: |
| 1025 | subnet = self.neutron.show_subnet(subnet_id) |
| 1026 | except Exception as e: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1027 | self.logger.error( |
| 1028 | "osconnector.get_network(): Error getting subnet %s %s" |
| 1029 | % (net_id, str(e)) |
| 1030 | ) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1031 | subnet = {"id": subnet_id, "fault": str(e)} |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1032 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1033 | subnets.append(subnet) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1034 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1035 | net["subnets"] = subnets |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1036 | net["encapsulation"] = net.get("provider:network_type") |
| 1037 | net["encapsulation_type"] = net.get("provider:network_type") |
| 1038 | net["segmentation_id"] = net.get("provider:segmentation_id") |
| 1039 | net["encapsulation_id"] = net.get("provider:segmentation_id") |
| 1040 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1041 | return net |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1042 | |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame] | 1043 | def delete_network(self, net_id, created_items=None): |
| 1044 | """ |
| 1045 | Removes a tenant network from VIM and its associated elements |
| 1046 | :param net_id: VIM identifier of the network, provided by method new_network |
| 1047 | :param created_items: dictionary with extra items to be deleted. provided by method new_network |
| 1048 | Returns the network identifier or raises an exception upon error or when network is not found |
| 1049 | """ |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1050 | self.logger.debug("Deleting network '%s' from VIM", net_id) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1051 | |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1052 | if created_items is None: |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame] | 1053 | created_items = {} |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1054 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1055 | try: |
| 1056 | self._reload_connection() |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1057 | # delete l2gw connections (if any) before deleting the network |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame] | 1058 | for k, v in created_items.items(): |
| 1059 | if not v: # skip already deleted |
| 1060 | continue |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1061 | |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame] | 1062 | try: |
| 1063 | k_item, _, k_id = k.partition(":") |
| 1064 | if k_item == "l2gwconn": |
| 1065 | self.neutron.delete_l2_gateway_connection(k_id) |
| 1066 | except Exception as e: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1067 | self.logger.error( |
| 1068 | "Error deleting l2 gateway connection: {}: {}".format( |
| 1069 | type(e).__name__, e |
| 1070 | ) |
| 1071 | ) |
| 1072 | |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1073 | # delete VM ports attached to this networks before the network |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1074 | ports = self.neutron.list_ports(network_id=net_id) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1075 | for p in ports["ports"]: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1076 | try: |
| 1077 | self.neutron.delete_port(p["id"]) |
| 1078 | except Exception as e: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1079 | self.logger.error("Error deleting port %s: %s", p["id"], str(e)) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1080 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1081 | self.neutron.delete_network(net_id) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1082 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1083 | return net_id |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1084 | except ( |
| 1085 | neExceptions.ConnectionFailed, |
| 1086 | neExceptions.NetworkNotFoundClient, |
| 1087 | neExceptions.NeutronException, |
| 1088 | ksExceptions.ClientException, |
| 1089 | neExceptions.NeutronException, |
| 1090 | ConnectionError, |
| 1091 | ) as e: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1092 | self._format_exception(e) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1093 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1094 | def refresh_nets_status(self, net_list): |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1095 | """Get the status of the networks |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1096 | Params: the list of network identifiers |
| 1097 | Returns a dictionary with: |
| 1098 | net_id: #VIM id of this network |
| 1099 | status: #Mandatory. Text with one of: |
| 1100 | # DELETED (not found at vim) |
| 1101 | # VIM_ERROR (Cannot connect to VIM, VIM response error, ...) |
| 1102 | # OTHER (Vim reported other status not understood) |
| 1103 | # ERROR (VIM indicates an ERROR status) |
| 1104 | # ACTIVE, INACTIVE, DOWN (admin down), |
| 1105 | # BUILD (on building process) |
| 1106 | # |
| 1107 | error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR |
| 1108 | vim_info: #Text with plain information obtained from vim (yaml.safe_dump) |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1109 | """ |
| 1110 | net_dict = {} |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1111 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1112 | for net_id in net_list: |
| 1113 | net = {} |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1114 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1115 | try: |
| 1116 | net_vim = self.get_network(net_id) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1117 | |
| 1118 | if net_vim["status"] in netStatus2manoFormat: |
| 1119 | net["status"] = netStatus2manoFormat[net_vim["status"]] |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1120 | else: |
| 1121 | net["status"] = "OTHER" |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1122 | net["error_msg"] = "VIM status reported " + net_vim["status"] |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 1123 | |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1124 | if net["status"] == "ACTIVE" and not net_vim["admin_state_up"]: |
| 1125 | net["status"] = "DOWN" |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1126 | |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1127 | net["vim_info"] = self.serialize(net_vim) |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1128 | |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1129 | if net_vim.get("fault"): # TODO |
| 1130 | net["error_msg"] = str(net_vim["fault"]) |
| tierno | 7277486 | 2020-05-04 11:44:15 +0000 | [diff] [blame] | 1131 | except vimconn.VimConnNotFoundException as e: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1132 | self.logger.error("Exception getting net status: %s", str(e)) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1133 | net["status"] = "DELETED" |
| 1134 | net["error_msg"] = str(e) |
| tierno | 7277486 | 2020-05-04 11:44:15 +0000 | [diff] [blame] | 1135 | except vimconn.VimConnException as e: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1136 | self.logger.error("Exception getting net status: %s", str(e)) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1137 | net["status"] = "VIM_ERROR" |
| 1138 | net["error_msg"] = str(e) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1139 | net_dict[net_id] = net |
| 1140 | return net_dict |
| 1141 | |
| 1142 | def get_flavor(self, flavor_id): |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1143 | """Obtain flavor details from the VIM. Returns the flavor dict details""" |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1144 | self.logger.debug("Getting flavor '%s'", flavor_id) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1145 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1146 | try: |
| 1147 | self._reload_connection() |
| 1148 | flavor = self.nova.flavors.find(id=flavor_id) |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1149 | # TODO parse input and translate to VIM format (openmano_schemas.new_vminstance_response_schema) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1150 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1151 | return flavor.to_dict() |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1152 | except ( |
| 1153 | nvExceptions.NotFound, |
| 1154 | nvExceptions.ClientException, |
| 1155 | ksExceptions.ClientException, |
| 1156 | ConnectionError, |
| 1157 | ) as e: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1158 | self._format_exception(e) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1159 | |
| tierno | cf157a8 | 2017-01-30 14:07:06 +0100 | [diff] [blame] | 1160 | def get_flavor_id_from_data(self, flavor_dict): |
| 1161 | """Obtain flavor id that match the flavor description |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1162 | Returns the flavor_id or raises a vimconnNotFoundException |
| 1163 | flavor_dict: contains the required ram, vcpus, disk |
| 1164 | If 'use_existing_flavors' is set to True at config, the closer flavor that provides same or more ram, vcpus |
| 1165 | and disk is returned. Otherwise a flavor with exactly same ram, vcpus and disk is returned or a |
| 1166 | vimconnNotFoundException is raised |
| tierno | cf157a8 | 2017-01-30 14:07:06 +0100 | [diff] [blame] | 1167 | """ |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1168 | exact_match = False if self.config.get("use_existing_flavors") else True |
| 1169 | |
| tierno | cf157a8 | 2017-01-30 14:07:06 +0100 | [diff] [blame] | 1170 | try: |
| 1171 | self._reload_connection() |
| tierno | e26fc7a | 2017-05-30 14:43:03 +0200 | [diff] [blame] | 1172 | flavor_candidate_id = None |
| 1173 | flavor_candidate_data = (10000, 10000, 10000) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1174 | flavor_target = ( |
| 1175 | flavor_dict["ram"], |
| 1176 | flavor_dict["vcpus"], |
| 1177 | flavor_dict["disk"], |
| sousaedu | 648ee3d | 2021-11-22 14:09:15 +0000 | [diff] [blame] | 1178 | flavor_dict.get("ephemeral", 0), |
| 1179 | flavor_dict.get("swap", 0), |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1180 | ) |
| tierno | e26fc7a | 2017-05-30 14:43:03 +0200 | [diff] [blame] | 1181 | # numa=None |
| anwars | ae5f52c | 2019-04-22 10:35:27 +0530 | [diff] [blame] | 1182 | extended = flavor_dict.get("extended", {}) |
| 1183 | if extended: |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1184 | # TODO |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1185 | raise vimconn.VimConnNotFoundException( |
| 1186 | "Flavor with EPA still not implemented" |
| 1187 | ) |
| tierno | cf157a8 | 2017-01-30 14:07:06 +0100 | [diff] [blame] | 1188 | # if len(numas) > 1: |
| tierno | 7277486 | 2020-05-04 11:44:15 +0000 | [diff] [blame] | 1189 | # raise vimconn.VimConnNotFoundException("Cannot find any flavor with more than one numa") |
| tierno | cf157a8 | 2017-01-30 14:07:06 +0100 | [diff] [blame] | 1190 | # numa=numas[0] |
| 1191 | # numas = extended.get("numas") |
| 1192 | for flavor in self.nova.flavors.list(): |
| 1193 | epa = flavor.get_keys() |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1194 | |
| tierno | cf157a8 | 2017-01-30 14:07:06 +0100 | [diff] [blame] | 1195 | if epa: |
| 1196 | continue |
| tierno | e26fc7a | 2017-05-30 14:43:03 +0200 | [diff] [blame] | 1197 | # TODO |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1198 | |
| sousaedu | 648ee3d | 2021-11-22 14:09:15 +0000 | [diff] [blame] | 1199 | flavor_data = ( |
| 1200 | flavor.ram, |
| 1201 | flavor.vcpus, |
| 1202 | flavor.disk, |
| 1203 | flavor.ephemeral, |
| preethika.p | ebaba1f | 2022-01-20 07:24:18 +0000 | [diff] [blame] | 1204 | flavor.swap if isinstance(flavor.swap, int) else 0, |
| sousaedu | 648ee3d | 2021-11-22 14:09:15 +0000 | [diff] [blame] | 1205 | ) |
| tierno | e26fc7a | 2017-05-30 14:43:03 +0200 | [diff] [blame] | 1206 | if flavor_data == flavor_target: |
| 1207 | return flavor.id |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1208 | elif ( |
| 1209 | not exact_match |
| 1210 | and flavor_target < flavor_data < flavor_candidate_data |
| 1211 | ): |
| tierno | e26fc7a | 2017-05-30 14:43:03 +0200 | [diff] [blame] | 1212 | flavor_candidate_id = flavor.id |
| 1213 | flavor_candidate_data = flavor_data |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1214 | |
| tierno | e26fc7a | 2017-05-30 14:43:03 +0200 | [diff] [blame] | 1215 | if not exact_match and flavor_candidate_id: |
| 1216 | return flavor_candidate_id |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1217 | |
| 1218 | raise vimconn.VimConnNotFoundException( |
| 1219 | "Cannot find any flavor matching '{}'".format(flavor_dict) |
| 1220 | ) |
| 1221 | except ( |
| 1222 | nvExceptions.NotFound, |
| 1223 | nvExceptions.ClientException, |
| 1224 | ksExceptions.ClientException, |
| 1225 | ConnectionError, |
| 1226 | ) as e: |
| tierno | cf157a8 | 2017-01-30 14:07:06 +0100 | [diff] [blame] | 1227 | self._format_exception(e) |
| 1228 | |
| anwars | ae5f52c | 2019-04-22 10:35:27 +0530 | [diff] [blame] | 1229 | def process_resource_quota(self, quota, prefix, extra_specs): |
| 1230 | """ |
| 1231 | :param prefix: |
| borsatti | 8a2dda3 | 2019-12-18 15:08:57 +0000 | [diff] [blame] | 1232 | :param extra_specs: |
| anwars | ae5f52c | 2019-04-22 10:35:27 +0530 | [diff] [blame] | 1233 | :return: |
| 1234 | """ |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1235 | if "limit" in quota: |
| 1236 | extra_specs["quota:" + prefix + "_limit"] = quota["limit"] |
| 1237 | |
| 1238 | if "reserve" in quota: |
| 1239 | extra_specs["quota:" + prefix + "_reservation"] = quota["reserve"] |
| 1240 | |
| 1241 | if "shares" in quota: |
| anwars | ae5f52c | 2019-04-22 10:35:27 +0530 | [diff] [blame] | 1242 | extra_specs["quota:" + prefix + "_shares_level"] = "custom" |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1243 | extra_specs["quota:" + prefix + "_shares_share"] = quota["shares"] |
| anwars | ae5f52c | 2019-04-22 10:35:27 +0530 | [diff] [blame] | 1244 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1245 | def new_flavor(self, flavor_data, change_name_if_used=True): |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1246 | """Adds a tenant flavor to openstack VIM |
| 1247 | if change_name_if_used is True, it will change name in case of conflict, because it is not supported name |
| 1248 | repetition |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1249 | Returns the flavor identifier |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1250 | """ |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1251 | self.logger.debug("Adding flavor '%s'", str(flavor_data)) |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1252 | retry = 0 |
| 1253 | max_retries = 3 |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1254 | name_suffix = 0 |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1255 | |
| anwars | c76a3ee | 2018-10-04 14:05:32 +0530 | [diff] [blame] | 1256 | try: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1257 | name = flavor_data["name"] |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1258 | while retry < max_retries: |
| 1259 | retry += 1 |
| anwars | c76a3ee | 2018-10-04 14:05:32 +0530 | [diff] [blame] | 1260 | try: |
| 1261 | self._reload_connection() |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1262 | |
| anwars | c76a3ee | 2018-10-04 14:05:32 +0530 | [diff] [blame] | 1263 | if change_name_if_used: |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1264 | # get used names |
| 1265 | fl_names = [] |
| 1266 | fl = self.nova.flavors.list() |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1267 | |
| anwars | c76a3ee | 2018-10-04 14:05:32 +0530 | [diff] [blame] | 1268 | for f in fl: |
| 1269 | fl_names.append(f.name) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1270 | |
| anwars | c76a3ee | 2018-10-04 14:05:32 +0530 | [diff] [blame] | 1271 | while name in fl_names: |
| 1272 | name_suffix += 1 |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1273 | name = flavor_data["name"] + "-" + str(name_suffix) |
| kate | 721d79b | 2017-06-24 04:21:38 -0700 | [diff] [blame] | 1274 | |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1275 | ram = flavor_data.get("ram", 64) |
| 1276 | vcpus = flavor_data.get("vcpus", 1) |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1277 | extra_specs = {} |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1278 | |
| anwars | c76a3ee | 2018-10-04 14:05:32 +0530 | [diff] [blame] | 1279 | extended = flavor_data.get("extended") |
| 1280 | if extended: |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1281 | numas = extended.get("numas") |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1282 | |
| anwars | c76a3ee | 2018-10-04 14:05:32 +0530 | [diff] [blame] | 1283 | if numas: |
| 1284 | numa_nodes = len(numas) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1285 | |
| anwars | ae5f52c | 2019-04-22 10:35:27 +0530 | [diff] [blame] | 1286 | extra_specs["hw:numa_nodes"] = str(numa_nodes) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1287 | |
| anwars | c76a3ee | 2018-10-04 14:05:32 +0530 | [diff] [blame] | 1288 | if self.vim_type == "VIO": |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1289 | extra_specs[ |
| 1290 | "vmware:extra_config" |
| 1291 | ] = '{"numa.nodeAffinity":"0"}' |
| anwars | ae5f52c | 2019-04-22 10:35:27 +0530 | [diff] [blame] | 1292 | extra_specs["vmware:latency_sensitivity_level"] = "high" |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1293 | |
| anwars | c76a3ee | 2018-10-04 14:05:32 +0530 | [diff] [blame] | 1294 | for numa in numas: |
| sritharan | 1d12984 | 2022-10-13 05:43:13 +0000 | [diff] [blame^] | 1295 | if "id" in numa: |
| 1296 | node_id = numa["id"] |
| 1297 | |
| 1298 | if "memory" in numa: |
| 1299 | memory_mb = numa["memory"] * 1024 |
| 1300 | memory = "hw:numa_mem.{}".format(node_id) |
| 1301 | extra_specs[memory] = int(memory_mb) |
| 1302 | |
| 1303 | if "vcpu" in numa: |
| 1304 | vcpu = numa["vcpu"] |
| 1305 | cpu = "hw:numa_cpus.{}".format(node_id) |
| 1306 | vcpu = ",".join(map(str, vcpu)) |
| 1307 | extra_specs[cpu] = vcpu |
| 1308 | |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1309 | # overwrite ram and vcpus |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1310 | # check if key "memory" is present in numa else use ram value at flavor |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1311 | # See for reference: https://specs.openstack.org/openstack/nova-specs/specs/mitaka/ |
| 1312 | # implemented/virt-driver-cpu-thread-pinning.html |
| sritharan | 1d12984 | 2022-10-13 05:43:13 +0000 | [diff] [blame^] | 1313 | extra_specs["hw:cpu_sockets"] = str(numa_nodes) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1314 | |
| 1315 | if "paired-threads" in numa: |
| 1316 | vcpus = numa["paired-threads"] * 2 |
| 1317 | # cpu_thread_policy "require" implies that the compute node must have an |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1318 | # STM architecture |
| anwars | ae5f52c | 2019-04-22 10:35:27 +0530 | [diff] [blame] | 1319 | extra_specs["hw:cpu_thread_policy"] = "require" |
| 1320 | extra_specs["hw:cpu_policy"] = "dedicated" |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1321 | elif "cores" in numa: |
| 1322 | vcpus = numa["cores"] |
| 1323 | # cpu_thread_policy "prefer" implies that the host must not have an SMT |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1324 | # architecture, or a non-SMT architecture will be emulated |
| anwars | ae5f52c | 2019-04-22 10:35:27 +0530 | [diff] [blame] | 1325 | extra_specs["hw:cpu_thread_policy"] = "isolate" |
| 1326 | extra_specs["hw:cpu_policy"] = "dedicated" |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1327 | elif "threads" in numa: |
| 1328 | vcpus = numa["threads"] |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1329 | # cpu_thread_policy "prefer" implies that the host may or may not have an SMT |
| 1330 | # architecture |
| anwars | ae5f52c | 2019-04-22 10:35:27 +0530 | [diff] [blame] | 1331 | extra_specs["hw:cpu_thread_policy"] = "prefer" |
| 1332 | extra_specs["hw:cpu_policy"] = "dedicated" |
| anwars | c76a3ee | 2018-10-04 14:05:32 +0530 | [diff] [blame] | 1333 | # for interface in numa.get("interfaces",() ): |
| 1334 | # if interface["dedicated"]=="yes": |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1335 | # raise vimconn.VimConnException("Passthrough interfaces are not supported |
| 1336 | # for the openstack connector", http_code=vimconn.HTTP_Service_Unavailable) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1337 | # #TODO, add the key 'pci_passthrough:alias"="<label at config>:<number ifaces>"' |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1338 | # when a way to connect it is available |
| anwars | ae5f52c | 2019-04-22 10:35:27 +0530 | [diff] [blame] | 1339 | elif extended.get("cpu-quota"): |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1340 | self.process_resource_quota( |
| 1341 | extended.get("cpu-quota"), "cpu", extra_specs |
| 1342 | ) |
| 1343 | |
| anwars | ae5f52c | 2019-04-22 10:35:27 +0530 | [diff] [blame] | 1344 | if extended.get("mem-quota"): |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1345 | self.process_resource_quota( |
| 1346 | extended.get("mem-quota"), "memory", extra_specs |
| 1347 | ) |
| 1348 | |
| anwars | ae5f52c | 2019-04-22 10:35:27 +0530 | [diff] [blame] | 1349 | if extended.get("vif-quota"): |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1350 | self.process_resource_quota( |
| 1351 | extended.get("vif-quota"), "vif", extra_specs |
| 1352 | ) |
| 1353 | |
| anwars | ae5f52c | 2019-04-22 10:35:27 +0530 | [diff] [blame] | 1354 | if extended.get("disk-io-quota"): |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1355 | self.process_resource_quota( |
| 1356 | extended.get("disk-io-quota"), "disk_io", extra_specs |
| 1357 | ) |
| 1358 | |
| rodriguezgar | 0dd83fd | 2022-05-15 00:42:45 +0200 | [diff] [blame] | 1359 | # Set the mempage size as specified in the descriptor |
| 1360 | if extended.get("mempage-size"): |
| 1361 | if extended.get("mempage-size") == "LARGE": |
| 1362 | extra_specs["hw:mem_page_size"] = "large" |
| 1363 | elif extended.get("mempage-size") == "SMALL": |
| 1364 | extra_specs["hw:mem_page_size"] = "small" |
| 1365 | elif extended.get("mempage-size") == "SIZE_2MB": |
| 1366 | extra_specs["hw:mem_page_size"] = "2MB" |
| 1367 | elif extended.get("mempage-size") == "SIZE_1GB": |
| 1368 | extra_specs["hw:mem_page_size"] = "1GB" |
| 1369 | elif extended.get("mempage-size") == "PREFER_LARGE": |
| 1370 | extra_specs["hw:mem_page_size"] = "any" |
| 1371 | else: |
| 1372 | # The validations in NBI should make reaching here not possible. |
| 1373 | # If this message is shown, check validations |
| 1374 | self.logger.debug( |
| 1375 | "Invalid mempage-size %s. Will be ignored", |
| 1376 | extended.get("mempage-size"), |
| 1377 | ) |
| sritharan | 1d12984 | 2022-10-13 05:43:13 +0000 | [diff] [blame^] | 1378 | if extended.get("cpu-pinning-policy"): |
| 1379 | extra_specs["hw:cpu_policy"] = extended.get( |
| 1380 | "cpu-pinning-policy" |
| 1381 | ).lower() |
| 1382 | |
| 1383 | # Set the cpu thread pinning policy as specified in the descriptor |
| 1384 | if extended.get("cpu-thread-pinning-policy"): |
| 1385 | extra_specs["hw:cpu_thread_policy"] = extended.get( |
| 1386 | "cpu-thread-pinning-policy" |
| 1387 | ).lower() |
| 1388 | |
| 1389 | # Set the mem policy as specified in the descriptor |
| 1390 | if extended.get("mem-policy"): |
| 1391 | extra_specs["hw:numa_mempolicy"] = extended.get( |
| 1392 | "mem-policy" |
| 1393 | ).lower() |
| 1394 | |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1395 | # create flavor |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1396 | new_flavor = self.nova.flavors.create( |
| sousaedu | f524da8 | 2021-11-22 14:02:17 +0000 | [diff] [blame] | 1397 | name=name, |
| 1398 | ram=ram, |
| 1399 | vcpus=vcpus, |
| 1400 | disk=flavor_data.get("disk", 0), |
| 1401 | ephemeral=flavor_data.get("ephemeral", 0), |
| sousaedu | 648ee3d | 2021-11-22 14:09:15 +0000 | [diff] [blame] | 1402 | swap=flavor_data.get("swap", 0), |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1403 | is_public=flavor_data.get("is_public", True), |
| 1404 | ) |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1405 | # add metadata |
| anwars | ae5f52c | 2019-04-22 10:35:27 +0530 | [diff] [blame] | 1406 | if extra_specs: |
| 1407 | new_flavor.set_keys(extra_specs) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1408 | |
| anwars | c76a3ee | 2018-10-04 14:05:32 +0530 | [diff] [blame] | 1409 | return new_flavor.id |
| 1410 | except nvExceptions.Conflict as e: |
| 1411 | if change_name_if_used and retry < max_retries: |
| 1412 | continue |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1413 | |
| anwars | c76a3ee | 2018-10-04 14:05:32 +0530 | [diff] [blame] | 1414 | self._format_exception(e) |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1415 | # except nvExceptions.BadRequest as e: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1416 | except ( |
| 1417 | ksExceptions.ClientException, |
| 1418 | nvExceptions.ClientException, |
| 1419 | ConnectionError, |
| 1420 | KeyError, |
| 1421 | ) as e: |
| anwars | c76a3ee | 2018-10-04 14:05:32 +0530 | [diff] [blame] | 1422 | self._format_exception(e) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1423 | |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1424 | def delete_flavor(self, flavor_id): |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1425 | """Deletes a tenant flavor from openstack VIM. Returns the old flavor_id""" |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1426 | try: |
| 1427 | self._reload_connection() |
| 1428 | self.nova.flavors.delete(flavor_id) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1429 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1430 | return flavor_id |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1431 | # except nvExceptions.BadRequest as e: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1432 | except ( |
| 1433 | nvExceptions.NotFound, |
| 1434 | ksExceptions.ClientException, |
| 1435 | nvExceptions.ClientException, |
| 1436 | ConnectionError, |
| 1437 | ) as e: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1438 | self._format_exception(e) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1439 | |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1440 | def new_image(self, image_dict): |
| 1441 | """ |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1442 | Adds a tenant image to VIM. imge_dict is a dictionary with: |
| 1443 | name: name |
| 1444 | disk_format: qcow2, vhd, vmdk, raw (by default), ... |
| 1445 | location: path or URI |
| 1446 | public: "yes" or "no" |
| 1447 | metadata: metadata of the image |
| 1448 | Returns the image_id |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1449 | """ |
| 1450 | retry = 0 |
| 1451 | max_retries = 3 |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1452 | |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1453 | while retry < max_retries: |
| 1454 | retry += 1 |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1455 | try: |
| 1456 | self._reload_connection() |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1457 | |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1458 | # determine format http://docs.openstack.org/developer/glance/formats.html |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1459 | if "disk_format" in image_dict: |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1460 | disk_format = image_dict["disk_format"] |
| 1461 | else: # autodiscover based on extension |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1462 | if image_dict["location"].endswith(".qcow2"): |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1463 | disk_format = "qcow2" |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1464 | elif image_dict["location"].endswith(".vhd"): |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1465 | disk_format = "vhd" |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1466 | elif image_dict["location"].endswith(".vmdk"): |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1467 | disk_format = "vmdk" |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1468 | elif image_dict["location"].endswith(".vdi"): |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1469 | disk_format = "vdi" |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1470 | elif image_dict["location"].endswith(".iso"): |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1471 | disk_format = "iso" |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1472 | elif image_dict["location"].endswith(".aki"): |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1473 | disk_format = "aki" |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1474 | elif image_dict["location"].endswith(".ari"): |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1475 | disk_format = "ari" |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1476 | elif image_dict["location"].endswith(".ami"): |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1477 | disk_format = "ami" |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1478 | else: |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1479 | disk_format = "raw" |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1480 | |
| 1481 | self.logger.debug( |
| 1482 | "new_image: '%s' loading from '%s'", |
| 1483 | image_dict["name"], |
| 1484 | image_dict["location"], |
| 1485 | ) |
| shashankjain | 3c83a21 | 2018-10-04 13:05:46 +0530 | [diff] [blame] | 1486 | if self.vim_type == "VIO": |
| 1487 | container_format = "bare" |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1488 | if "container_format" in image_dict: |
| 1489 | container_format = image_dict["container_format"] |
| 1490 | |
| 1491 | new_image = self.glance.images.create( |
| 1492 | name=image_dict["name"], |
| 1493 | container_format=container_format, |
| 1494 | disk_format=disk_format, |
| 1495 | ) |
| shashankjain | 3c83a21 | 2018-10-04 13:05:46 +0530 | [diff] [blame] | 1496 | else: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1497 | new_image = self.glance.images.create(name=image_dict["name"]) |
| 1498 | |
| 1499 | if image_dict["location"].startswith("http"): |
| tierno | 1beea86 | 2018-07-11 15:47:37 +0200 | [diff] [blame] | 1500 | # TODO there is not a method to direct download. It must be downloaded locally with requests |
| tierno | 7277486 | 2020-05-04 11:44:15 +0000 | [diff] [blame] | 1501 | raise vimconn.VimConnNotImplemented("Cannot create image from URL") |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1502 | else: # local path |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1503 | with open(image_dict["location"]) as fimage: |
| tierno | 1beea86 | 2018-07-11 15:47:37 +0200 | [diff] [blame] | 1504 | self.glance.images.upload(new_image.id, fimage) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1505 | # new_image = self.glancev1.images.create(name=image_dict["name"], is_public= |
| 1506 | # image_dict.get("public","yes")=="yes", |
| tierno | 1beea86 | 2018-07-11 15:47:37 +0200 | [diff] [blame] | 1507 | # container_format="bare", data=fimage, disk_format=disk_format) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1508 | |
| 1509 | metadata_to_load = image_dict.get("metadata") |
| 1510 | |
| 1511 | # TODO location is a reserved word for current openstack versions. fixed for VIO please check |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1512 | # for openstack |
| shashankjain | 3c83a21 | 2018-10-04 13:05:46 +0530 | [diff] [blame] | 1513 | if self.vim_type == "VIO": |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1514 | metadata_to_load["upload_location"] = image_dict["location"] |
| shashankjain | 3c83a21 | 2018-10-04 13:05:46 +0530 | [diff] [blame] | 1515 | else: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1516 | metadata_to_load["location"] = image_dict["location"] |
| 1517 | |
| tierno | 1beea86 | 2018-07-11 15:47:37 +0200 | [diff] [blame] | 1518 | self.glance.images.update(new_image.id, **metadata_to_load) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1519 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1520 | return new_image.id |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1521 | except ( |
| 1522 | nvExceptions.Conflict, |
| 1523 | ksExceptions.ClientException, |
| 1524 | nvExceptions.ClientException, |
| 1525 | ) as e: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1526 | self._format_exception(e) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1527 | except ( |
| 1528 | HTTPException, |
| 1529 | gl1Exceptions.HTTPException, |
| 1530 | gl1Exceptions.CommunicationError, |
| 1531 | ConnectionError, |
| 1532 | ) as e: |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1533 | if retry == max_retries: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1534 | continue |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1535 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1536 | self._format_exception(e) |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1537 | except IOError as e: # can not open the file |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1538 | raise vimconn.VimConnConnectionException( |
| 1539 | "{}: {} for {}".format(type(e).__name__, e, image_dict["location"]), |
| 1540 | http_code=vimconn.HTTP_Bad_Request, |
| 1541 | ) |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 1542 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1543 | def delete_image(self, image_id): |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1544 | """Deletes a tenant image from openstack VIM. Returns the old id""" |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1545 | try: |
| 1546 | self._reload_connection() |
| tierno | 1beea86 | 2018-07-11 15:47:37 +0200 | [diff] [blame] | 1547 | self.glance.images.delete(image_id) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1548 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1549 | return image_id |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1550 | except ( |
| 1551 | nvExceptions.NotFound, |
| 1552 | ksExceptions.ClientException, |
| 1553 | nvExceptions.ClientException, |
| 1554 | gl1Exceptions.CommunicationError, |
| 1555 | gl1Exceptions.HTTPNotFound, |
| 1556 | ConnectionError, |
| 1557 | ) as e: # TODO remove |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1558 | self._format_exception(e) |
| 1559 | |
| 1560 | def get_image_id_from_path(self, path): |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1561 | """Get the image id from image path in the VIM database. Returns the image_id""" |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1562 | try: |
| 1563 | self._reload_connection() |
| tierno | 1beea86 | 2018-07-11 15:47:37 +0200 | [diff] [blame] | 1564 | images = self.glance.images.list() |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1565 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1566 | for image in images: |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1567 | if image.metadata.get("location") == path: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1568 | return image.id |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1569 | |
| 1570 | raise vimconn.VimConnNotFoundException( |
| 1571 | "image with location '{}' not found".format(path) |
| 1572 | ) |
| 1573 | except ( |
| 1574 | ksExceptions.ClientException, |
| 1575 | nvExceptions.ClientException, |
| 1576 | gl1Exceptions.CommunicationError, |
| 1577 | ConnectionError, |
| 1578 | ) as e: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1579 | self._format_exception(e) |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 1580 | |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 1581 | def get_image_list(self, filter_dict={}): |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1582 | """Obtain tenant images from VIM |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 1583 | Filter_dict can be: |
| 1584 | id: image id |
| 1585 | name: image name |
| 1586 | checksum: image checksum |
| 1587 | Returns the image list of dictionaries: |
| 1588 | [{<the fields at Filter_dict plus some VIM specific>}, ...] |
| 1589 | List can be empty |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1590 | """ |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 1591 | self.logger.debug("Getting image list from VIM filter: '%s'", str(filter_dict)) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1592 | |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 1593 | try: |
| 1594 | self._reload_connection() |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1595 | # filter_dict_os = filter_dict.copy() |
| 1596 | # First we filter by the available filter fields: name, id. The others are removed. |
| tierno | 1beea86 | 2018-07-11 15:47:37 +0200 | [diff] [blame] | 1597 | image_list = self.glance.images.list() |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 1598 | filtered_list = [] |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1599 | |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 1600 | for image in image_list: |
| tierno | 3cb8dc3 | 2017-10-24 18:13:19 +0200 | [diff] [blame] | 1601 | try: |
| tierno | 1beea86 | 2018-07-11 15:47:37 +0200 | [diff] [blame] | 1602 | if filter_dict.get("name") and image["name"] != filter_dict["name"]: |
| 1603 | continue |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1604 | |
| tierno | 1beea86 | 2018-07-11 15:47:37 +0200 | [diff] [blame] | 1605 | if filter_dict.get("id") and image["id"] != filter_dict["id"]: |
| 1606 | continue |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1607 | |
| 1608 | if ( |
| 1609 | filter_dict.get("checksum") |
| 1610 | and image["checksum"] != filter_dict["checksum"] |
| 1611 | ): |
| tierno | 1beea86 | 2018-07-11 15:47:37 +0200 | [diff] [blame] | 1612 | continue |
| 1613 | |
| 1614 | filtered_list.append(image.copy()) |
| tierno | 3cb8dc3 | 2017-10-24 18:13:19 +0200 | [diff] [blame] | 1615 | except gl1Exceptions.HTTPNotFound: |
| 1616 | pass |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1617 | |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 1618 | return filtered_list |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1619 | except ( |
| 1620 | ksExceptions.ClientException, |
| 1621 | nvExceptions.ClientException, |
| 1622 | gl1Exceptions.CommunicationError, |
| 1623 | ConnectionError, |
| 1624 | ) as e: |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 1625 | self._format_exception(e) |
| 1626 | |
| Pablo Montes Moreno | 6a7785b | 2017-07-03 10:44:30 +0200 | [diff] [blame] | 1627 | def __wait_for_vm(self, vm_id, status): |
| 1628 | """wait until vm is in the desired status and return True. |
| 1629 | If the VM gets in ERROR status, return false. |
| 1630 | If the timeout is reached generate an exception""" |
| 1631 | elapsed_time = 0 |
| 1632 | while elapsed_time < server_timeout: |
| 1633 | vm_status = self.nova.servers.get(vm_id).status |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1634 | |
| Pablo Montes Moreno | 6a7785b | 2017-07-03 10:44:30 +0200 | [diff] [blame] | 1635 | if vm_status == status: |
| 1636 | return True |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1637 | |
| 1638 | if vm_status == "ERROR": |
| Pablo Montes Moreno | 6a7785b | 2017-07-03 10:44:30 +0200 | [diff] [blame] | 1639 | return False |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1640 | |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 1641 | time.sleep(5) |
| 1642 | elapsed_time += 5 |
| Pablo Montes Moreno | 6a7785b | 2017-07-03 10:44:30 +0200 | [diff] [blame] | 1643 | |
| 1644 | # if we exceeded the timeout rollback |
| 1645 | if elapsed_time >= server_timeout: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1646 | raise vimconn.VimConnException( |
| 1647 | "Timeout waiting for instance " + vm_id + " to get " + status, |
| 1648 | http_code=vimconn.HTTP_Request_Timeout, |
| 1649 | ) |
| Pablo Montes Moreno | 6a7785b | 2017-07-03 10:44:30 +0200 | [diff] [blame] | 1650 | |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 1651 | def _get_openstack_availablity_zones(self): |
| 1652 | """ |
| 1653 | Get from openstack availability zones available |
| 1654 | :return: |
| 1655 | """ |
| 1656 | try: |
| 1657 | openstack_availability_zone = self.nova.availability_zones.list() |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1658 | openstack_availability_zone = [ |
| 1659 | str(zone.zoneName) |
| 1660 | for zone in openstack_availability_zone |
| 1661 | if zone.zoneName != "internal" |
| 1662 | ] |
| 1663 | |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 1664 | return openstack_availability_zone |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1665 | except Exception: |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 1666 | return None |
| 1667 | |
| 1668 | def _set_availablity_zones(self): |
| 1669 | """ |
| 1670 | Set vim availablity zone |
| 1671 | :return: |
| 1672 | """ |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1673 | if "availability_zone" in self.config: |
| 1674 | vim_availability_zones = self.config.get("availability_zone") |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 1675 | |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 1676 | if isinstance(vim_availability_zones, str): |
| 1677 | self.availability_zone = [vim_availability_zones] |
| 1678 | elif isinstance(vim_availability_zones, list): |
| 1679 | self.availability_zone = vim_availability_zones |
| 1680 | else: |
| 1681 | self.availability_zone = self._get_openstack_availablity_zones() |
| 1682 | |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1683 | def _get_vm_availability_zone( |
| 1684 | self, availability_zone_index, availability_zone_list |
| 1685 | ): |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 1686 | """ |
| tierno | 5a3273c | 2017-08-29 11:43:46 +0200 | [diff] [blame] | 1687 | Return thge availability zone to be used by the created VM. |
| 1688 | :return: The VIM availability zone to be used or None |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 1689 | """ |
| tierno | 5a3273c | 2017-08-29 11:43:46 +0200 | [diff] [blame] | 1690 | if availability_zone_index is None: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1691 | if not self.config.get("availability_zone"): |
| tierno | 5a3273c | 2017-08-29 11:43:46 +0200 | [diff] [blame] | 1692 | return None |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1693 | elif isinstance(self.config.get("availability_zone"), str): |
| 1694 | return self.config["availability_zone"] |
| tierno | 5a3273c | 2017-08-29 11:43:46 +0200 | [diff] [blame] | 1695 | else: |
| 1696 | # TODO consider using a different parameter at config for default AV and AV list match |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1697 | return self.config["availability_zone"][0] |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 1698 | |
| tierno | 5a3273c | 2017-08-29 11:43:46 +0200 | [diff] [blame] | 1699 | vim_availability_zones = self.availability_zone |
| 1700 | # check if VIM offer enough availability zones describe in the VNFD |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1701 | if vim_availability_zones and len(availability_zone_list) <= len( |
| 1702 | vim_availability_zones |
| 1703 | ): |
| tierno | 5a3273c | 2017-08-29 11:43:46 +0200 | [diff] [blame] | 1704 | # check if all the names of NFV AV match VIM AV names |
| 1705 | match_by_index = False |
| 1706 | for av in availability_zone_list: |
| 1707 | if av not in vim_availability_zones: |
| 1708 | match_by_index = True |
| 1709 | break |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1710 | |
| tierno | 5a3273c | 2017-08-29 11:43:46 +0200 | [diff] [blame] | 1711 | if match_by_index: |
| 1712 | return vim_availability_zones[availability_zone_index] |
| 1713 | else: |
| 1714 | return availability_zone_list[availability_zone_index] |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 1715 | else: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1716 | raise vimconn.VimConnConflictException( |
| 1717 | "No enough availability zones at VIM for this deployment" |
| 1718 | ) |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 1719 | |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1720 | def new_vminstance( |
| 1721 | self, |
| 1722 | name, |
| 1723 | description, |
| 1724 | start, |
| 1725 | image_id, |
| 1726 | flavor_id, |
| Alexis Romero | b70f4ed | 2022-03-11 18:00:49 +0100 | [diff] [blame] | 1727 | affinity_group_list, |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1728 | net_list, |
| 1729 | cloud_config=None, |
| 1730 | disk_list=None, |
| 1731 | availability_zone_index=None, |
| 1732 | availability_zone_list=None, |
| 1733 | ): |
| tierno | 98e909c | 2017-10-14 13:27:03 +0200 | [diff] [blame] | 1734 | """Adds a VM instance to VIM |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1735 | Params: |
| 1736 | start: indicates if VM must start or boot in pause mode. Ignored |
| Alexis Romero | b70f4ed | 2022-03-11 18:00:49 +0100 | [diff] [blame] | 1737 | image_id,flavor_id: image and flavor uuid |
| 1738 | affinity_group_list: list of affinity groups, each one is a dictionary. |
| 1739 | Ignore if empty. |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1740 | net_list: list of interfaces, each one is a dictionary with: |
| 1741 | name: |
| 1742 | net_id: network uuid to connect |
| 1743 | vpci: virtual vcpi to assign, ignored because openstack lack #TODO |
| 1744 | model: interface model, ignored #TODO |
| 1745 | mac_address: used for SR-IOV ifaces #TODO for other types |
| 1746 | use: 'data', 'bridge', 'mgmt' |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1747 | type: 'virtual', 'PCI-PASSTHROUGH'('PF'), 'SR-IOV'('VF'), 'VFnotShared' |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1748 | vim_id: filled/added by this function |
| ahmadsa | f853d45 | 2016-12-22 11:33:47 +0500 | [diff] [blame] | 1749 | floating_ip: True/False (or it can be None) |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 1750 | port_security: True/False |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 1751 | 'cloud_config': (optional) dictionary with: |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 1752 | 'key-pairs': (optional) list of strings with the public key to be inserted to the default user |
| 1753 | 'users': (optional) list of users to be inserted, each item is a dict with: |
| 1754 | 'name': (mandatory) user name, |
| 1755 | 'key-pairs': (optional) list of strings with the public key to be inserted to the user |
| 1756 | 'user-data': (optional) string is a text script to be passed directly to cloud-init |
| 1757 | 'config-files': (optional). List of files to be transferred. Each item is a dict with: |
| 1758 | 'dest': (mandatory) string with the destination absolute path |
| 1759 | 'encoding': (optional, by default text). Can be one of: |
| 1760 | 'b64', 'base64', 'gz', 'gz+b64', 'gz+base64', 'gzip+b64', 'gzip+base64' |
| 1761 | 'content' (mandatory): string with the content of the file |
| 1762 | 'permissions': (optional) string with file permissions, typically octal notation '0644' |
| 1763 | 'owner': (optional) file owner, string with the format 'owner:group' |
| 1764 | 'boot-data-drive': boolean to indicate if user-data must be passed using a boot drive (hard disk) |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 1765 | 'disk_list': (optional) list with additional disks to the VM. Each item is a dict with: |
| 1766 | 'image_id': (optional). VIM id of an existing image. If not provided an empty disk must be mounted |
| 1767 | 'size': (mandatory) string with the size of the disk in GB |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 1768 | 'vim_id' (optional) should use this existing volume id |
| tierno | 5a3273c | 2017-08-29 11:43:46 +0200 | [diff] [blame] | 1769 | availability_zone_index: Index of availability_zone_list to use for this this VM. None if not AV required |
| 1770 | availability_zone_list: list of availability zones given by user in the VNFD descriptor. Ignore if |
| 1771 | availability_zone_index is None |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1772 | #TODO ip, security groups |
| tierno | 98e909c | 2017-10-14 13:27:03 +0200 | [diff] [blame] | 1773 | Returns a tuple with the instance identifier and created_items or raises an exception on error |
| 1774 | created_items can be None or a dictionary where this method can include key-values that will be passed to |
| 1775 | the method delete_vminstance and action_vminstance. Can be used to store created ports, volumes, etc. |
| 1776 | Format is vimconnector dependent, but do not use nested dictionaries and a value of None should be the same |
| 1777 | as not present. |
| 1778 | """ |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1779 | self.logger.debug( |
| 1780 | "new_vminstance input: image='%s' flavor='%s' nics='%s'", |
| 1781 | image_id, |
| 1782 | flavor_id, |
| 1783 | str(net_list), |
| 1784 | ) |
| 1785 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1786 | try: |
| Pablo Montes Moreno | 6a7785b | 2017-07-03 10:44:30 +0200 | [diff] [blame] | 1787 | server = None |
| tierno | 98e909c | 2017-10-14 13:27:03 +0200 | [diff] [blame] | 1788 | created_items = {} |
| tierno | b0b9dab | 2017-10-14 14:25:20 +0200 | [diff] [blame] | 1789 | # metadata = {} |
| tierno | 98e909c | 2017-10-14 13:27:03 +0200 | [diff] [blame] | 1790 | net_list_vim = [] |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1791 | external_network = [] |
| 1792 | # ^list of external networks to be connected to instance, later on used to create floating_ip |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1793 | no_secured_ports = [] # List of port-is with port-security disabled |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1794 | self._reload_connection() |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1795 | # metadata_vpci = {} # For a specific neutron plugin |
| tierno | b84cbdc | 2017-07-07 14:30:30 +0200 | [diff] [blame] | 1796 | block_device_mapping = None |
| tierno | a05b65a | 2019-02-01 12:30:27 +0000 | [diff] [blame] | 1797 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1798 | for net in net_list: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1799 | if not net.get("net_id"): # skip non connected iface |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1800 | continue |
| Pablo Montes Moreno | 3be0b2a | 2017-03-30 13:22:15 +0200 | [diff] [blame] | 1801 | |
| tierno | a05b65a | 2019-02-01 12:30:27 +0000 | [diff] [blame] | 1802 | port_dict = { |
| Pablo Montes Moreno | 3be0b2a | 2017-03-30 13:22:15 +0200 | [diff] [blame] | 1803 | "network_id": net["net_id"], |
| 1804 | "name": net.get("name"), |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1805 | "admin_state_up": True, |
| Pablo Montes Moreno | 3be0b2a | 2017-03-30 13:22:15 +0200 | [diff] [blame] | 1806 | } |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1807 | |
| 1808 | if ( |
| 1809 | self.config.get("security_groups") |
| 1810 | and net.get("port_security") is not False |
| 1811 | and not self.config.get("no_port_security_extension") |
| 1812 | ): |
| tierno | a05b65a | 2019-02-01 12:30:27 +0000 | [diff] [blame] | 1813 | if not self.security_groups_id: |
| 1814 | self._get_ids_from_name() |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1815 | |
| tierno | a05b65a | 2019-02-01 12:30:27 +0000 | [diff] [blame] | 1816 | port_dict["security_groups"] = self.security_groups_id |
| 1817 | |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1818 | if net["type"] == "virtual": |
| tierno | b0b9dab | 2017-10-14 14:25:20 +0200 | [diff] [blame] | 1819 | pass |
| 1820 | # if "vpci" in net: |
| 1821 | # metadata_vpci[ net["net_id"] ] = [[ net["vpci"], "" ]] |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1822 | elif net["type"] == "VF" or net["type"] == "SR-IOV": # for VF |
| tierno | b0b9dab | 2017-10-14 14:25:20 +0200 | [diff] [blame] | 1823 | # if "vpci" in net: |
| 1824 | # if "VF" not in metadata_vpci: |
| 1825 | # metadata_vpci["VF"]=[] |
| 1826 | # metadata_vpci["VF"].append([ net["vpci"], "" ]) |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1827 | port_dict["binding:vnic_type"] = "direct" |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1828 | |
| tierno | b0b9dab | 2017-10-14 14:25:20 +0200 | [diff] [blame] | 1829 | # VIO specific Changes |
| kate | 721d79b | 2017-06-24 04:21:38 -0700 | [diff] [blame] | 1830 | if self.vim_type == "VIO": |
| tierno | b0b9dab | 2017-10-14 14:25:20 +0200 | [diff] [blame] | 1831 | # Need to create port with port_security_enabled = False and no-security-groups |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1832 | port_dict["port_security_enabled"] = False |
| 1833 | port_dict["provider_security_groups"] = [] |
| 1834 | port_dict["security_groups"] = [] |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1835 | else: # For PT PCI-PASSTHROUGH |
| tierno | b0b9dab | 2017-10-14 14:25:20 +0200 | [diff] [blame] | 1836 | # if "vpci" in net: |
| 1837 | # if "PF" not in metadata_vpci: |
| 1838 | # metadata_vpci["PF"]=[] |
| 1839 | # metadata_vpci["PF"].append([ net["vpci"], "" ]) |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1840 | port_dict["binding:vnic_type"] = "direct-physical" |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1841 | |
| Pablo Montes Moreno | 3be0b2a | 2017-03-30 13:22:15 +0200 | [diff] [blame] | 1842 | if not port_dict["name"]: |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1843 | port_dict["name"] = name |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1844 | |
| Pablo Montes Moreno | 3be0b2a | 2017-03-30 13:22:15 +0200 | [diff] [blame] | 1845 | if net.get("mac_address"): |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1846 | port_dict["mac_address"] = net["mac_address"] |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1847 | |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 1848 | if net.get("ip_address"): |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1849 | port_dict["fixed_ips"] = [{"ip_address": net["ip_address"]}] |
| 1850 | # TODO add "subnet_id": <subnet_id> |
| 1851 | |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1852 | new_port = self.neutron.create_port({"port": port_dict}) |
| tierno | 00e3df7 | 2017-11-29 17:20:13 +0100 | [diff] [blame] | 1853 | created_items["port:" + str(new_port["port"]["id"])] = True |
| Pablo Montes Moreno | 3be0b2a | 2017-03-30 13:22:15 +0200 | [diff] [blame] | 1854 | net["mac_adress"] = new_port["port"]["mac_address"] |
| 1855 | net["vim_id"] = new_port["port"]["id"] |
| tierno | b84cbdc | 2017-07-07 14:30:30 +0200 | [diff] [blame] | 1856 | # if try to use a network without subnetwork, it will return a emtpy list |
| 1857 | fixed_ips = new_port["port"].get("fixed_ips") |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1858 | |
| tierno | b84cbdc | 2017-07-07 14:30:30 +0200 | [diff] [blame] | 1859 | if fixed_ips: |
| 1860 | net["ip"] = fixed_ips[0].get("ip_address") |
| 1861 | else: |
| 1862 | net["ip"] = None |
| montesmoreno | 994a29d | 2017-08-22 11:23:06 +0200 | [diff] [blame] | 1863 | |
| 1864 | port = {"port-id": new_port["port"]["id"]} |
| 1865 | if float(self.nova.api_version.get_string()) >= 2.32: |
| 1866 | port["tag"] = new_port["port"]["name"] |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1867 | |
| montesmoreno | 994a29d | 2017-08-22 11:23:06 +0200 | [diff] [blame] | 1868 | net_list_vim.append(port) |
| Pablo Montes Moreno | 3be0b2a | 2017-03-30 13:22:15 +0200 | [diff] [blame] | 1869 | |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1870 | if net.get("floating_ip", False): |
| 1871 | net["exit_on_floating_ip_error"] = True |
| ahmadsa | f853d45 | 2016-12-22 11:33:47 +0500 | [diff] [blame] | 1872 | external_network.append(net) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1873 | elif net["use"] == "mgmt" and self.config.get("use_floating_ip"): |
| 1874 | net["exit_on_floating_ip_error"] = False |
| tierno | f8383b8 | 2017-01-18 15:49:48 +0100 | [diff] [blame] | 1875 | external_network.append(net) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1876 | net["floating_ip"] = self.config.get("use_floating_ip") |
| tierno | f8383b8 | 2017-01-18 15:49:48 +0100 | [diff] [blame] | 1877 | |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 1878 | # If port security is disabled when the port has not yet been attached to the VM, then all vm traffic |
| 1879 | # is dropped. |
| Pablo Montes Moreno | 6a7785b | 2017-07-03 10:44:30 +0200 | [diff] [blame] | 1880 | # As a workaround we wait until the VM is active and then disable the port-security |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1881 | if net.get("port_security") is False and not self.config.get( |
| 1882 | "no_port_security_extension" |
| 1883 | ): |
| 1884 | no_secured_ports.append( |
| 1885 | ( |
| 1886 | new_port["port"]["id"], |
| 1887 | net.get("port_security_disable_strategy"), |
| 1888 | ) |
| 1889 | ) |
| Pablo Montes Moreno | 6a7785b | 2017-07-03 10:44:30 +0200 | [diff] [blame] | 1890 | |
| tierno | b0b9dab | 2017-10-14 14:25:20 +0200 | [diff] [blame] | 1891 | # if metadata_vpci: |
| 1892 | # metadata = {"pci_assignement": json.dumps(metadata_vpci)} |
| 1893 | # if len(metadata["pci_assignement"]) >255: |
| 1894 | # #limit the metadata size |
| 1895 | # #metadata["pci_assignement"] = metadata["pci_assignement"][0:255] |
| 1896 | # self.logger.warn("Metadata deleted since it exceeds the expected length (255) ") |
| 1897 | # metadata = {} |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 1898 | |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1899 | self.logger.debug( |
| 1900 | "name '%s' image_id '%s'flavor_id '%s' net_list_vim '%s' description '%s'", |
| 1901 | name, |
| 1902 | image_id, |
| 1903 | flavor_id, |
| 1904 | str(net_list_vim), |
| 1905 | description, |
| 1906 | ) |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 1907 | |
| tierno | 98e909c | 2017-10-14 13:27:03 +0200 | [diff] [blame] | 1908 | # cloud config |
| tierno | 0a1437e | 2017-10-02 00:17:43 +0200 | [diff] [blame] | 1909 | config_drive, userdata = self._create_user_data(cloud_config) |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 1910 | |
| Alexis Romero | 247cc43 | 2022-05-12 13:23:25 +0200 | [diff] [blame] | 1911 | # get availability Zone |
| 1912 | vm_av_zone = self._get_vm_availability_zone( |
| 1913 | availability_zone_index, availability_zone_list |
| 1914 | ) |
| 1915 | |
| tierno | 98e909c | 2017-10-14 13:27:03 +0200 | [diff] [blame] | 1916 | # Create additional volumes in case these are present in disk_list |
| palaciosj | 8f2060b | 2022-02-24 12:05:59 +0000 | [diff] [blame] | 1917 | existing_vim_volumes = [] |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1918 | base_disk_index = ord("b") |
| aticig | 179e002 | 2022-03-29 13:15:45 +0300 | [diff] [blame] | 1919 | boot_volume_id = None |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 1920 | if disk_list: |
| tierno | b84cbdc | 2017-07-07 14:30:30 +0200 | [diff] [blame] | 1921 | block_device_mapping = {} |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 1922 | for disk in disk_list: |
| aticig | cf14bb1 | 2022-05-19 13:03:17 +0300 | [diff] [blame] | 1923 | if "image_id" in disk: |
| 1924 | # persistent root volume |
| 1925 | base_disk_index = ord("a") |
| 1926 | image_id = "" |
| aticig | 343c520 | 2022-06-25 23:31:19 +0300 | [diff] [blame] | 1927 | # use existing persistent root volume |
| aticig | cf14bb1 | 2022-05-19 13:03:17 +0300 | [diff] [blame] | 1928 | if disk.get("vim_volume_id"): |
| aticig | cf14bb1 | 2022-05-19 13:03:17 +0300 | [diff] [blame] | 1929 | block_device_mapping["vd" + chr(base_disk_index)] = disk[ |
| 1930 | "vim_volume_id" |
| 1931 | ] |
| 1932 | existing_vim_volumes.append({"id": disk["vim_volume_id"]}) |
| aticig | 343c520 | 2022-06-25 23:31:19 +0300 | [diff] [blame] | 1933 | # use existing persistent root volume |
| 1934 | elif disk.get("vim_id"): |
| 1935 | block_device_mapping["vd" + chr(base_disk_index)] = disk[ |
| 1936 | "vim_id" |
| 1937 | ] |
| 1938 | existing_vim_volumes.append({"id": disk["vim_id"]}) |
| aticig | cf14bb1 | 2022-05-19 13:03:17 +0300 | [diff] [blame] | 1939 | else: |
| 1940 | # create persistent root volume |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1941 | volume = self.cinder.volumes.create( |
| 1942 | size=disk["size"], |
| aticig | cf14bb1 | 2022-05-19 13:03:17 +0300 | [diff] [blame] | 1943 | name=name + "vd" + chr(base_disk_index), |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1944 | imageRef=disk["image_id"], |
| Alexis Romero | 247cc43 | 2022-05-12 13:23:25 +0200 | [diff] [blame] | 1945 | # Make sure volume is in the same AZ as the VM to be attached to |
| 1946 | availability_zone=vm_av_zone, |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1947 | ) |
| aticig | 179e002 | 2022-03-29 13:15:45 +0300 | [diff] [blame] | 1948 | boot_volume_id = volume.id |
| aticig | cf14bb1 | 2022-05-19 13:03:17 +0300 | [diff] [blame] | 1949 | created_items["volume:" + str(volume.id)] = True |
| 1950 | block_device_mapping[ |
| 1951 | "vd" + chr(base_disk_index) |
| 1952 | ] = volume.id |
| 1953 | else: |
| 1954 | # non-root persistent volume |
| aticig | 343c520 | 2022-06-25 23:31:19 +0300 | [diff] [blame] | 1955 | key_id = ( |
| 1956 | "vim_volume_id" |
| 1957 | if "vim_volume_id" in disk.keys() |
| 1958 | else "vim_id" |
| 1959 | ) |
| 1960 | if disk.get(key_id): |
| aticig | cf14bb1 | 2022-05-19 13:03:17 +0300 | [diff] [blame] | 1961 | # use existing persistent volume |
| 1962 | block_device_mapping["vd" + chr(base_disk_index)] = disk[ |
| aticig | 343c520 | 2022-06-25 23:31:19 +0300 | [diff] [blame] | 1963 | key_id |
| aticig | cf14bb1 | 2022-05-19 13:03:17 +0300 | [diff] [blame] | 1964 | ] |
| aticig | 343c520 | 2022-06-25 23:31:19 +0300 | [diff] [blame] | 1965 | existing_vim_volumes.append({"id": disk[key_id]}) |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 1966 | else: |
| aticig | cf14bb1 | 2022-05-19 13:03:17 +0300 | [diff] [blame] | 1967 | # create persistent volume |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1968 | volume = self.cinder.volumes.create( |
| 1969 | size=disk["size"], |
| aticig | cf14bb1 | 2022-05-19 13:03:17 +0300 | [diff] [blame] | 1970 | name=name + "vd" + chr(base_disk_index), |
| Alexis Romero | 247cc43 | 2022-05-12 13:23:25 +0200 | [diff] [blame] | 1971 | # Make sure volume is in the same AZ as the VM to be attached to |
| 1972 | availability_zone=vm_av_zone, |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1973 | ) |
| aticig | cf14bb1 | 2022-05-19 13:03:17 +0300 | [diff] [blame] | 1974 | created_items["volume:" + str(volume.id)] = True |
| 1975 | block_device_mapping[ |
| 1976 | "vd" + chr(base_disk_index) |
| 1977 | ] = volume.id |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1978 | |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 1979 | base_disk_index += 1 |
| 1980 | |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 1981 | # Wait until created volumes are with status available |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 1982 | elapsed_time = 0 |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 1983 | while elapsed_time < volume_timeout: |
| 1984 | for created_item in created_items: |
| 1985 | v, _, volume_id = created_item.partition(":") |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1986 | if v == "volume": |
| 1987 | if self.cinder.volumes.get(volume_id).status != "available": |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 1988 | break |
| 1989 | else: # all ready: break from while |
| 1990 | break |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1991 | |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 1992 | time.sleep(5) |
| 1993 | elapsed_time += 5 |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1994 | |
| palaciosj | 8f2060b | 2022-02-24 12:05:59 +0000 | [diff] [blame] | 1995 | # Wait until existing volumes in vim are with status available |
| 1996 | while elapsed_time < volume_timeout: |
| 1997 | for volume in existing_vim_volumes: |
| 1998 | if self.cinder.volumes.get(volume["id"]).status != "available": |
| 1999 | break |
| 2000 | else: # all ready: break from while |
| 2001 | break |
| 2002 | |
| 2003 | time.sleep(5) |
| 2004 | elapsed_time += 5 |
| 2005 | |
| tierno | b0b9dab | 2017-10-14 14:25:20 +0200 | [diff] [blame] | 2006 | # If we exceeded the timeout rollback |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 2007 | if elapsed_time >= volume_timeout: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2008 | raise vimconn.VimConnException( |
| 2009 | "Timeout creating volumes for instance " + name, |
| 2010 | http_code=vimconn.HTTP_Request_Timeout, |
| 2011 | ) |
| aticig | 179e002 | 2022-03-29 13:15:45 +0300 | [diff] [blame] | 2012 | if boot_volume_id: |
| 2013 | self.cinder.volumes.set_bootable(boot_volume_id, True) |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 2014 | |
| Alexis Romero | b70f4ed | 2022-03-11 18:00:49 +0100 | [diff] [blame] | 2015 | # Manage affinity groups/server groups |
| 2016 | server_group_id = None |
| 2017 | scheduller_hints = {} |
| 2018 | |
| 2019 | if affinity_group_list: |
| 2020 | # Only first id on the list will be used. Openstack restriction |
| 2021 | server_group_id = affinity_group_list[0]["affinity_group_id"] |
| 2022 | scheduller_hints["group"] = server_group_id |
| 2023 | |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2024 | self.logger.debug( |
| 2025 | "nova.servers.create({}, {}, {}, nics={}, security_groups={}, " |
| 2026 | "availability_zone={}, key_name={}, userdata={}, config_drive={}, " |
| Alexis Romero | b70f4ed | 2022-03-11 18:00:49 +0100 | [diff] [blame] | 2027 | "block_device_mapping={}, server_group={})".format( |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2028 | name, |
| 2029 | image_id, |
| 2030 | flavor_id, |
| 2031 | net_list_vim, |
| 2032 | self.config.get("security_groups"), |
| 2033 | vm_av_zone, |
| 2034 | self.config.get("keypair"), |
| 2035 | userdata, |
| 2036 | config_drive, |
| 2037 | block_device_mapping, |
| Alexis Romero | b70f4ed | 2022-03-11 18:00:49 +0100 | [diff] [blame] | 2038 | server_group_id, |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2039 | ) |
| 2040 | ) |
| 2041 | server = self.nova.servers.create( |
| aticig | cf14bb1 | 2022-05-19 13:03:17 +0300 | [diff] [blame] | 2042 | name=name, |
| 2043 | image=image_id, |
| 2044 | flavor=flavor_id, |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2045 | nics=net_list_vim, |
| 2046 | security_groups=self.config.get("security_groups"), |
| 2047 | # TODO remove security_groups in future versions. Already at neutron port |
| 2048 | availability_zone=vm_av_zone, |
| 2049 | key_name=self.config.get("keypair"), |
| 2050 | userdata=userdata, |
| 2051 | config_drive=config_drive, |
| 2052 | block_device_mapping=block_device_mapping, |
| Alexis Romero | b70f4ed | 2022-03-11 18:00:49 +0100 | [diff] [blame] | 2053 | scheduler_hints=scheduller_hints, |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2054 | ) # , description=description) |
| Pablo Montes Moreno | 6a7785b | 2017-07-03 10:44:30 +0200 | [diff] [blame] | 2055 | |
| tierno | 326fd5e | 2018-02-22 11:58:59 +0100 | [diff] [blame] | 2056 | vm_start_time = time.time() |
| Pablo Montes Moreno | 6a7785b | 2017-07-03 10:44:30 +0200 | [diff] [blame] | 2057 | # Previously mentioned workaround to wait until the VM is active and then disable the port-security |
| 2058 | if no_secured_ports: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2059 | self.__wait_for_vm(server.id, "ACTIVE") |
| Pablo Montes Moreno | 6a7785b | 2017-07-03 10:44:30 +0200 | [diff] [blame] | 2060 | |
| bravof | 7a1f525 | 2020-10-20 10:27:42 -0300 | [diff] [blame] | 2061 | for port in no_secured_ports: |
| 2062 | port_update = { |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2063 | "port": {"port_security_enabled": False, "security_groups": None} |
| bravof | 7a1f525 | 2020-10-20 10:27:42 -0300 | [diff] [blame] | 2064 | } |
| 2065 | |
| 2066 | if port[1] == "allow-address-pairs": |
| 2067 | port_update = { |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2068 | "port": {"allowed_address_pairs": [{"ip_address": "0.0.0.0/0"}]} |
| bravof | 7a1f525 | 2020-10-20 10:27:42 -0300 | [diff] [blame] | 2069 | } |
| 2070 | |
| Pablo Montes Moreno | 6a7785b | 2017-07-03 10:44:30 +0200 | [diff] [blame] | 2071 | try: |
| bravof | 7a1f525 | 2020-10-20 10:27:42 -0300 | [diff] [blame] | 2072 | self.neutron.update_port(port[0], port_update) |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2073 | except Exception: |
| bravof | 7a1f525 | 2020-10-20 10:27:42 -0300 | [diff] [blame] | 2074 | raise vimconn.VimConnException( |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2075 | "It was not possible to disable port security for port {}".format( |
| 2076 | port[0] |
| 2077 | ) |
| bravof | 7a1f525 | 2020-10-20 10:27:42 -0300 | [diff] [blame] | 2078 | ) |
| 2079 | |
| tierno | 98e909c | 2017-10-14 13:27:03 +0200 | [diff] [blame] | 2080 | # print "DONE :-)", server |
| Pablo Montes Moreno | 6a7785b | 2017-07-03 10:44:30 +0200 | [diff] [blame] | 2081 | |
| tierno | 4d1ce22 | 2018-04-06 10:41:06 +0200 | [diff] [blame] | 2082 | # pool_id = None |
| ahmadsa | f853d45 | 2016-12-22 11:33:47 +0500 | [diff] [blame] | 2083 | for floating_network in external_network: |
| tierno | f8383b8 | 2017-01-18 15:49:48 +0100 | [diff] [blame] | 2084 | try: |
| tierno | f8383b8 | 2017-01-18 15:49:48 +0100 | [diff] [blame] | 2085 | assigned = False |
| tierno | cb66c7e | 2020-07-22 10:42:58 +0000 | [diff] [blame] | 2086 | floating_ip_retries = 3 |
| 2087 | # In case of RO in HA there can be conflicts, two RO trying to assign same floating IP, so retry |
| 2088 | # several times |
| tierno | 98e909c | 2017-10-14 13:27:03 +0200 | [diff] [blame] | 2089 | while not assigned: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2090 | floating_ips = self.neutron.list_floatingips().get( |
| 2091 | "floatingips", () |
| 2092 | ) |
| 2093 | random.shuffle(floating_ips) # randomize |
| tierno | cb66c7e | 2020-07-22 10:42:58 +0000 | [diff] [blame] | 2094 | for fip in floating_ips: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2095 | if ( |
| 2096 | fip.get("port_id") |
| 2097 | or fip.get("tenant_id") != server.tenant_id |
| 2098 | ): |
| tierno | 326fd5e | 2018-02-22 11:58:59 +0100 | [diff] [blame] | 2099 | continue |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2100 | |
| 2101 | if isinstance(floating_network["floating_ip"], str): |
| 2102 | if ( |
| 2103 | fip.get("floating_network_id") |
| 2104 | != floating_network["floating_ip"] |
| 2105 | ): |
| tierno | 326fd5e | 2018-02-22 11:58:59 +0100 | [diff] [blame] | 2106 | continue |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2107 | |
| tierno | cb66c7e | 2020-07-22 10:42:58 +0000 | [diff] [blame] | 2108 | free_floating_ip = fip["id"] |
| 2109 | break |
| tierno | f8383b8 | 2017-01-18 15:49:48 +0100 | [diff] [blame] | 2110 | else: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2111 | if ( |
| 2112 | isinstance(floating_network["floating_ip"], str) |
| 2113 | and floating_network["floating_ip"].lower() != "true" |
| 2114 | ): |
| 2115 | pool_id = floating_network["floating_ip"] |
| tierno | 326fd5e | 2018-02-22 11:58:59 +0100 | [diff] [blame] | 2116 | else: |
| tierno | 4d1ce22 | 2018-04-06 10:41:06 +0200 | [diff] [blame] | 2117 | # Find the external network |
| tierno | 326fd5e | 2018-02-22 11:58:59 +0100 | [diff] [blame] | 2118 | external_nets = list() |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2119 | |
| 2120 | for net in self.neutron.list_networks()["networks"]: |
| 2121 | if net["router:external"]: |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2122 | external_nets.append(net) |
| tierno | f8383b8 | 2017-01-18 15:49:48 +0100 | [diff] [blame] | 2123 | |
| tierno | 326fd5e | 2018-02-22 11:58:59 +0100 | [diff] [blame] | 2124 | if len(external_nets) == 0: |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2125 | raise vimconn.VimConnException( |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2126 | "Cannot create floating_ip automatically since " |
| 2127 | "no external network is present", |
| 2128 | http_code=vimconn.HTTP_Conflict, |
| 2129 | ) |
| 2130 | |
| tierno | 326fd5e | 2018-02-22 11:58:59 +0100 | [diff] [blame] | 2131 | if len(external_nets) > 1: |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2132 | raise vimconn.VimConnException( |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2133 | "Cannot create floating_ip automatically since " |
| 2134 | "multiple external networks are present", |
| 2135 | http_code=vimconn.HTTP_Conflict, |
| 2136 | ) |
| tierno | f8383b8 | 2017-01-18 15:49:48 +0100 | [diff] [blame] | 2137 | |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2138 | pool_id = external_nets[0].get("id") |
| 2139 | |
| 2140 | param = { |
| 2141 | "floatingip": { |
| 2142 | "floating_network_id": pool_id, |
| 2143 | "tenant_id": server.tenant_id, |
| 2144 | } |
| 2145 | } |
| 2146 | |
| ahmadsa | f853d45 | 2016-12-22 11:33:47 +0500 | [diff] [blame] | 2147 | try: |
| tierno | 4d1ce22 | 2018-04-06 10:41:06 +0200 | [diff] [blame] | 2148 | # self.logger.debug("Creating floating IP") |
| tierno | f8383b8 | 2017-01-18 15:49:48 +0100 | [diff] [blame] | 2149 | new_floating_ip = self.neutron.create_floatingip(param) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2150 | free_floating_ip = new_floating_ip["floatingip"]["id"] |
| 2151 | created_items[ |
| 2152 | "floating_ip:" + str(free_floating_ip) |
| 2153 | ] = True |
| ahmadsa | f853d45 | 2016-12-22 11:33:47 +0500 | [diff] [blame] | 2154 | except Exception as e: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2155 | raise vimconn.VimConnException( |
| 2156 | type(e).__name__ |
| 2157 | + ": Cannot create new floating_ip " |
| 2158 | + str(e), |
| 2159 | http_code=vimconn.HTTP_Conflict, |
| 2160 | ) |
| tierno | 326fd5e | 2018-02-22 11:58:59 +0100 | [diff] [blame] | 2161 | |
| tierno | cb66c7e | 2020-07-22 10:42:58 +0000 | [diff] [blame] | 2162 | try: |
| 2163 | # for race condition ensure not already assigned |
| 2164 | fip = self.neutron.show_floatingip(free_floating_ip) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2165 | |
| 2166 | if fip["floatingip"]["port_id"]: |
| tierno | cb66c7e | 2020-07-22 10:42:58 +0000 | [diff] [blame] | 2167 | continue |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2168 | |
| tierno | cb66c7e | 2020-07-22 10:42:58 +0000 | [diff] [blame] | 2169 | # the vim_id key contains the neutron.port_id |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2170 | self.neutron.update_floatingip( |
| 2171 | free_floating_ip, |
| 2172 | {"floatingip": {"port_id": floating_network["vim_id"]}}, |
| 2173 | ) |
| tierno | cb66c7e | 2020-07-22 10:42:58 +0000 | [diff] [blame] | 2174 | # for race condition ensure not re-assigned to other VM after 5 seconds |
| 2175 | time.sleep(5) |
| 2176 | fip = self.neutron.show_floatingip(free_floating_ip) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2177 | |
| 2178 | if ( |
| 2179 | fip["floatingip"]["port_id"] |
| 2180 | != floating_network["vim_id"] |
| 2181 | ): |
| 2182 | self.logger.error( |
| 2183 | "floating_ip {} re-assigned to other port".format( |
| 2184 | free_floating_ip |
| 2185 | ) |
| 2186 | ) |
| tierno | cb66c7e | 2020-07-22 10:42:58 +0000 | [diff] [blame] | 2187 | continue |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2188 | |
| 2189 | self.logger.debug( |
| 2190 | "Assigned floating_ip {} to VM {}".format( |
| 2191 | free_floating_ip, server.id |
| 2192 | ) |
| 2193 | ) |
| tierno | cb66c7e | 2020-07-22 10:42:58 +0000 | [diff] [blame] | 2194 | assigned = True |
| 2195 | except Exception as e: |
| 2196 | # openstack need some time after VM creation to assign an IP. So retry if fails |
| 2197 | vm_status = self.nova.servers.get(server.id).status |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2198 | |
| 2199 | if vm_status not in ("ACTIVE", "ERROR"): |
| tierno | cb66c7e | 2020-07-22 10:42:58 +0000 | [diff] [blame] | 2200 | if time.time() - vm_start_time < server_timeout: |
| 2201 | time.sleep(5) |
| 2202 | continue |
| 2203 | elif floating_ip_retries > 0: |
| 2204 | floating_ip_retries -= 1 |
| 2205 | continue |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2206 | |
| tierno | cb66c7e | 2020-07-22 10:42:58 +0000 | [diff] [blame] | 2207 | raise vimconn.VimConnException( |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2208 | "Cannot create floating_ip: {} {}".format( |
| 2209 | type(e).__name__, e |
| 2210 | ), |
| 2211 | http_code=vimconn.HTTP_Conflict, |
| 2212 | ) |
| tierno | 326fd5e | 2018-02-22 11:58:59 +0100 | [diff] [blame] | 2213 | |
| tierno | f8383b8 | 2017-01-18 15:49:48 +0100 | [diff] [blame] | 2214 | except Exception as e: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2215 | if not floating_network["exit_on_floating_ip_error"]: |
| tierno | cb66c7e | 2020-07-22 10:42:58 +0000 | [diff] [blame] | 2216 | self.logger.error("Cannot create floating_ip. %s", str(e)) |
| tierno | f8383b8 | 2017-01-18 15:49:48 +0100 | [diff] [blame] | 2217 | continue |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2218 | |
| tierno | f8383b8 | 2017-01-18 15:49:48 +0100 | [diff] [blame] | 2219 | raise |
| montesmoreno | 2a1fc4e | 2017-01-09 16:46:04 +0000 | [diff] [blame] | 2220 | |
| tierno | 98e909c | 2017-10-14 13:27:03 +0200 | [diff] [blame] | 2221 | return server.id, created_items |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2222 | # except nvExceptions.NotFound as e: |
| 2223 | # error_value=-vimconn.HTTP_Not_Found |
| 2224 | # error_text= "vm instance %s not found" % vm_id |
| 2225 | # except TypeError as e: |
| 2226 | # raise vimconn.VimConnException(type(e).__name__ + ": "+ str(e), http_code=vimconn.HTTP_Bad_Request) |
| Pablo Montes Moreno | 6a7785b | 2017-07-03 10:44:30 +0200 | [diff] [blame] | 2227 | |
| 2228 | except Exception as e: |
| tierno | 98e909c | 2017-10-14 13:27:03 +0200 | [diff] [blame] | 2229 | server_id = None |
| 2230 | if server: |
| 2231 | server_id = server.id |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2232 | |
| tierno | 98e909c | 2017-10-14 13:27:03 +0200 | [diff] [blame] | 2233 | try: |
| 2234 | self.delete_vminstance(server_id, created_items) |
| 2235 | except Exception as e2: |
| 2236 | self.logger.error("new_vminstance rollback fail {}".format(e2)) |
| Pablo Montes Moreno | 6a7785b | 2017-07-03 10:44:30 +0200 | [diff] [blame] | 2237 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2238 | self._format_exception(e) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2239 | |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2240 | def get_vminstance(self, vm_id): |
| 2241 | """Returns the VM instance information from VIM""" |
| 2242 | # self.logger.debug("Getting VM from VIM") |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2243 | try: |
| 2244 | self._reload_connection() |
| 2245 | server = self.nova.servers.find(id=vm_id) |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2246 | # TODO parse input and translate to VIM format (openmano_schemas.new_vminstance_response_schema) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2247 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2248 | return server.to_dict() |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2249 | except ( |
| 2250 | ksExceptions.ClientException, |
| 2251 | nvExceptions.ClientException, |
| 2252 | nvExceptions.NotFound, |
| 2253 | ConnectionError, |
| 2254 | ) as e: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2255 | self._format_exception(e) |
| 2256 | |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2257 | def get_vminstance_console(self, vm_id, console_type="vnc"): |
| 2258 | """ |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2259 | Get a console for the virtual machine |
| 2260 | Params: |
| 2261 | vm_id: uuid of the VM |
| 2262 | console_type, can be: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2263 | "novnc" (by default), "xvpvnc" for VNC types, |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2264 | "rdp-html5" for RDP types, "spice-html5" for SPICE types |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2265 | Returns dict with the console parameters: |
| 2266 | protocol: ssh, ftp, http, https, ... |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2267 | server: usually ip address |
| 2268 | port: the http, ssh, ... port |
| 2269 | suffix: extra text, e.g. the http path and query string |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2270 | """ |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2271 | self.logger.debug("Getting VM CONSOLE from VIM") |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2272 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2273 | try: |
| 2274 | self._reload_connection() |
| 2275 | server = self.nova.servers.find(id=vm_id) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2276 | |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2277 | if console_type is None or console_type == "novnc": |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2278 | console_dict = server.get_vnc_console("novnc") |
| 2279 | elif console_type == "xvpvnc": |
| 2280 | console_dict = server.get_vnc_console(console_type) |
| 2281 | elif console_type == "rdp-html5": |
| 2282 | console_dict = server.get_rdp_console(console_type) |
| 2283 | elif console_type == "spice-html5": |
| 2284 | console_dict = server.get_spice_console(console_type) |
| 2285 | else: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2286 | raise vimconn.VimConnException( |
| 2287 | "console type '{}' not allowed".format(console_type), |
| 2288 | http_code=vimconn.HTTP_Bad_Request, |
| 2289 | ) |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 2290 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2291 | console_dict1 = console_dict.get("console") |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2292 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2293 | if console_dict1: |
| 2294 | console_url = console_dict1.get("url") |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2295 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2296 | if console_url: |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2297 | # parse console_url |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2298 | protocol_index = console_url.find("//") |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2299 | suffix_index = ( |
| 2300 | console_url[protocol_index + 2 :].find("/") + protocol_index + 2 |
| 2301 | ) |
| 2302 | port_index = ( |
| 2303 | console_url[protocol_index + 2 : suffix_index].find(":") |
| 2304 | + protocol_index |
| 2305 | + 2 |
| 2306 | ) |
| 2307 | |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2308 | if protocol_index < 0 or port_index < 0 or suffix_index < 0: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2309 | return ( |
| 2310 | -vimconn.HTTP_Internal_Server_Error, |
| 2311 | "Unexpected response from VIM", |
| 2312 | ) |
| 2313 | |
| 2314 | console_dict = { |
| 2315 | "protocol": console_url[0:protocol_index], |
| 2316 | "server": console_url[protocol_index + 2 : port_index], |
| 2317 | "port": console_url[port_index:suffix_index], |
| 2318 | "suffix": console_url[suffix_index + 1 :], |
| 2319 | } |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2320 | protocol_index += 2 |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2321 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2322 | return console_dict |
| tierno | 7277486 | 2020-05-04 11:44:15 +0000 | [diff] [blame] | 2323 | raise vimconn.VimConnUnexpectedResponse("Unexpected response from VIM") |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2324 | except ( |
| 2325 | nvExceptions.NotFound, |
| 2326 | ksExceptions.ClientException, |
| 2327 | nvExceptions.ClientException, |
| 2328 | nvExceptions.BadRequest, |
| 2329 | ConnectionError, |
| 2330 | ) as e: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2331 | self._format_exception(e) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2332 | |
| palaciosj | 8f2060b | 2022-02-24 12:05:59 +0000 | [diff] [blame] | 2333 | def delete_vminstance(self, vm_id, created_items=None, volumes_to_hold=None): |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2334 | """Removes a VM instance from VIM. Returns the old identifier""" |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2335 | # print "osconnector: Getting VM from VIM" |
| 2336 | if created_items is None: |
| tierno | 98e909c | 2017-10-14 13:27:03 +0200 | [diff] [blame] | 2337 | created_items = {} |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2338 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2339 | try: |
| 2340 | self._reload_connection() |
| tierno | 98e909c | 2017-10-14 13:27:03 +0200 | [diff] [blame] | 2341 | # delete VM ports attached to this networks before the virtual machine |
| 2342 | for k, v in created_items.items(): |
| 2343 | if not v: # skip already deleted |
| 2344 | continue |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2345 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2346 | try: |
| tierno | ad6bdd4 | 2018-01-10 10:43:46 +0100 | [diff] [blame] | 2347 | k_item, _, k_id = k.partition(":") |
| 2348 | if k_item == "port": |
| aticig | 36cad66 | 2022-06-22 22:03:37 +0300 | [diff] [blame] | 2349 | port_dict = self.neutron.list_ports() |
| aticig | fe57572 | 2022-06-07 15:00:35 +0300 | [diff] [blame] | 2350 | existing_ports = [ |
| 2351 | port["id"] for port in port_dict["ports"] if port_dict |
| 2352 | ] |
| 2353 | if k_id in existing_ports: |
| 2354 | self.neutron.delete_port(k_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2355 | except Exception as e: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2356 | self.logger.error( |
| 2357 | "Error deleting port: {}: {}".format(type(e).__name__, e) |
| 2358 | ) |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 2359 | |
| tierno | 98e909c | 2017-10-14 13:27:03 +0200 | [diff] [blame] | 2360 | # #commented because detaching the volumes makes the servers.delete not work properly ?!? |
| 2361 | # #dettach volumes attached |
| 2362 | # server = self.nova.servers.get(vm_id) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2363 | # volumes_attached_dict = server._info["os-extended-volumes:volumes_attached"] #volume["id"] |
| tierno | 98e909c | 2017-10-14 13:27:03 +0200 | [diff] [blame] | 2364 | # #for volume in volumes_attached_dict: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2365 | # # self.cinder.volumes.detach(volume["id"]) |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 2366 | |
| tierno | 98e909c | 2017-10-14 13:27:03 +0200 | [diff] [blame] | 2367 | if vm_id: |
| 2368 | self.nova.servers.delete(vm_id) |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 2369 | |
| tierno | 98e909c | 2017-10-14 13:27:03 +0200 | [diff] [blame] | 2370 | # delete volumes. Although having detached, they should have in active status before deleting |
| 2371 | # we ensure in this loop |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 2372 | keep_waiting = True |
| 2373 | elapsed_time = 0 |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2374 | |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 2375 | while keep_waiting and elapsed_time < volume_timeout: |
| 2376 | keep_waiting = False |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2377 | |
| tierno | 98e909c | 2017-10-14 13:27:03 +0200 | [diff] [blame] | 2378 | for k, v in created_items.items(): |
| 2379 | if not v: # skip already deleted |
| 2380 | continue |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2381 | |
| tierno | 98e909c | 2017-10-14 13:27:03 +0200 | [diff] [blame] | 2382 | try: |
| tierno | ad6bdd4 | 2018-01-10 10:43:46 +0100 | [diff] [blame] | 2383 | k_item, _, k_id = k.partition(":") |
| 2384 | if k_item == "volume": |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2385 | if self.cinder.volumes.get(k_id).status != "available": |
| tierno | 98e909c | 2017-10-14 13:27:03 +0200 | [diff] [blame] | 2386 | keep_waiting = True |
| 2387 | else: |
| palaciosj | 8f2060b | 2022-02-24 12:05:59 +0000 | [diff] [blame] | 2388 | if k_id not in volumes_to_hold: |
| 2389 | self.cinder.volumes.delete(k_id) |
| 2390 | created_items[k] = None |
| tierno | cb66c7e | 2020-07-22 10:42:58 +0000 | [diff] [blame] | 2391 | elif k_item == "floating_ip": # floating ip |
| 2392 | self.neutron.delete_floatingip(k_id) |
| 2393 | created_items[k] = None |
| 2394 | |
| tierno | 98e909c | 2017-10-14 13:27:03 +0200 | [diff] [blame] | 2395 | except Exception as e: |
| tierno | cb66c7e | 2020-07-22 10:42:58 +0000 | [diff] [blame] | 2396 | self.logger.error("Error deleting {}: {}".format(k, e)) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2397 | |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 2398 | if keep_waiting: |
| 2399 | time.sleep(1) |
| 2400 | elapsed_time += 1 |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2401 | |
| tierno | 98e909c | 2017-10-14 13:27:03 +0200 | [diff] [blame] | 2402 | return None |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2403 | except ( |
| 2404 | nvExceptions.NotFound, |
| 2405 | ksExceptions.ClientException, |
| 2406 | nvExceptions.ClientException, |
| 2407 | ConnectionError, |
| 2408 | ) as e: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2409 | self._format_exception(e) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2410 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2411 | def refresh_vms_status(self, vm_list): |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2412 | """Get the status of the virtual machines and their interfaces/ports |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2413 | Params: the list of VM identifiers |
| 2414 | Returns a dictionary with: |
| 2415 | vm_id: #VIM id of this Virtual Machine |
| 2416 | status: #Mandatory. Text with one of: |
| 2417 | # DELETED (not found at vim) |
| 2418 | # VIM_ERROR (Cannot connect to VIM, VIM response error, ...) |
| 2419 | # OTHER (Vim reported other status not understood) |
| 2420 | # ERROR (VIM indicates an ERROR status) |
| 2421 | # ACTIVE, PAUSED, SUSPENDED, INACTIVE (not running), |
| 2422 | # CREATING (on building process), ERROR |
| 2423 | # ACTIVE:NoMgmtIP (Active but any of its interface has an IP address |
| 2424 | # |
| 2425 | error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR |
| 2426 | vim_info: #Text with plain information obtained from vim (yaml.safe_dump) |
| 2427 | interfaces: |
| 2428 | - vim_info: #Text with plain information obtained from vim (yaml.safe_dump) |
| 2429 | mac_address: #Text format XX:XX:XX:XX:XX:XX |
| 2430 | vim_net_id: #network id where this interface is connected |
| 2431 | vim_interface_id: #interface/port VIM id |
| 2432 | ip_address: #null, or text with IPv4, IPv6 address |
| 2433 | compute_node: #identification of compute node where PF,VF interface is allocated |
| 2434 | pci: #PCI address of the NIC that hosts the PF,VF |
| 2435 | vlan: #physical VLAN used for VF |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2436 | """ |
| 2437 | vm_dict = {} |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2438 | self.logger.debug( |
| 2439 | "refresh_vms status: Getting tenant VM instance information from VIM" |
| 2440 | ) |
| 2441 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2442 | for vm_id in vm_list: |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2443 | vm = {} |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2444 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2445 | try: |
| 2446 | vm_vim = self.get_vminstance(vm_id) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2447 | |
| 2448 | if vm_vim["status"] in vmStatus2manoFormat: |
| 2449 | vm["status"] = vmStatus2manoFormat[vm_vim["status"]] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2450 | else: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2451 | vm["status"] = "OTHER" |
| 2452 | vm["error_msg"] = "VIM status reported " + vm_vim["status"] |
| 2453 | |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 2454 | vm_vim.pop("OS-EXT-SRV-ATTR:user_data", None) |
| 2455 | vm_vim.pop("user_data", None) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2456 | vm["vim_info"] = self.serialize(vm_vim) |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2457 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2458 | vm["interfaces"] = [] |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2459 | if vm_vim.get("fault"): |
| 2460 | vm["error_msg"] = str(vm_vim["fault"]) |
| 2461 | |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2462 | # get interfaces |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2463 | try: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2464 | self._reload_connection() |
| tierno | b42fd9b | 2018-06-20 10:44:32 +0200 | [diff] [blame] | 2465 | port_dict = self.neutron.list_ports(device_id=vm_id) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2466 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2467 | for port in port_dict["ports"]: |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2468 | interface = {} |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2469 | interface["vim_info"] = self.serialize(port) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2470 | interface["mac_address"] = port.get("mac_address") |
| 2471 | interface["vim_net_id"] = port["network_id"] |
| 2472 | interface["vim_interface_id"] = port["id"] |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2473 | # check if OS-EXT-SRV-ATTR:host is there, |
| Mike Marchetti | 5b9da42 | 2017-05-02 15:35:47 -0400 | [diff] [blame] | 2474 | # in case of non-admin credentials, it will be missing |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2475 | |
| 2476 | if vm_vim.get("OS-EXT-SRV-ATTR:host"): |
| 2477 | interface["compute_node"] = vm_vim["OS-EXT-SRV-ATTR:host"] |
| 2478 | |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 2479 | interface["pci"] = None |
| Mike Marchetti | 5b9da42 | 2017-05-02 15:35:47 -0400 | [diff] [blame] | 2480 | |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2481 | # check if binding:profile is there, |
| Mike Marchetti | 5b9da42 | 2017-05-02 15:35:47 -0400 | [diff] [blame] | 2482 | # in case of non-admin credentials, it will be missing |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2483 | if port.get("binding:profile"): |
| 2484 | if port["binding:profile"].get("pci_slot"): |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2485 | # TODO: At the moment sr-iov pci addresses are converted to PF pci addresses by setting |
| 2486 | # the slot to 0x00 |
| Mike Marchetti | 5b9da42 | 2017-05-02 15:35:47 -0400 | [diff] [blame] | 2487 | # TODO: This is just a workaround valid for niantinc. Find a better way to do so |
| 2488 | # CHANGE DDDD:BB:SS.F to DDDD:BB:00.(F%2) assuming there are 2 ports per nic |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2489 | pci = port["binding:profile"]["pci_slot"] |
| Mike Marchetti | 5b9da42 | 2017-05-02 15:35:47 -0400 | [diff] [blame] | 2490 | # interface["pci"] = pci[:-4] + "00." + str(int(pci[-1]) % 2) |
| 2491 | interface["pci"] = pci |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2492 | |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 2493 | interface["vlan"] = None |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2494 | |
| 2495 | if port.get("binding:vif_details"): |
| 2496 | interface["vlan"] = port["binding:vif_details"].get("vlan") |
| 2497 | |
| tierno | 1dfe993 | 2020-06-18 08:50:10 +0000 | [diff] [blame] | 2498 | # Get vlan from network in case not present in port for those old openstacks and cases where |
| 2499 | # it is needed vlan at PT |
| 2500 | if not interface["vlan"]: |
| 2501 | # if network is of type vlan and port is of type direct (sr-iov) then set vlan id |
| 2502 | network = self.neutron.show_network(port["network_id"]) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2503 | |
| 2504 | if ( |
| 2505 | network["network"].get("provider:network_type") |
| 2506 | == "vlan" |
| 2507 | ): |
| tierno | 1dfe993 | 2020-06-18 08:50:10 +0000 | [diff] [blame] | 2508 | # and port.get("binding:vnic_type") in ("direct", "direct-physical"): |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2509 | interface["vlan"] = network["network"].get( |
| 2510 | "provider:segmentation_id" |
| 2511 | ) |
| 2512 | |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2513 | ips = [] |
| 2514 | # look for floating ip address |
| tierno | b42fd9b | 2018-06-20 10:44:32 +0200 | [diff] [blame] | 2515 | try: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2516 | floating_ip_dict = self.neutron.list_floatingips( |
| 2517 | port_id=port["id"] |
| 2518 | ) |
| 2519 | |
| tierno | b42fd9b | 2018-06-20 10:44:32 +0200 | [diff] [blame] | 2520 | if floating_ip_dict.get("floatingips"): |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2521 | ips.append( |
| 2522 | floating_ip_dict["floatingips"][0].get( |
| 2523 | "floating_ip_address" |
| 2524 | ) |
| 2525 | ) |
| tierno | b42fd9b | 2018-06-20 10:44:32 +0200 | [diff] [blame] | 2526 | except Exception: |
| 2527 | pass |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2528 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2529 | for subnet in port["fixed_ips"]: |
| 2530 | ips.append(subnet["ip_address"]) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2531 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2532 | interface["ip_address"] = ";".join(ips) |
| 2533 | vm["interfaces"].append(interface) |
| 2534 | except Exception as e: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2535 | self.logger.error( |
| 2536 | "Error getting vm interface information {}: {}".format( |
| 2537 | type(e).__name__, e |
| 2538 | ), |
| 2539 | exc_info=True, |
| 2540 | ) |
| tierno | 7277486 | 2020-05-04 11:44:15 +0000 | [diff] [blame] | 2541 | except vimconn.VimConnNotFoundException as e: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2542 | self.logger.error("Exception getting vm status: %s", str(e)) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2543 | vm["status"] = "DELETED" |
| 2544 | vm["error_msg"] = str(e) |
| tierno | 7277486 | 2020-05-04 11:44:15 +0000 | [diff] [blame] | 2545 | except vimconn.VimConnException as e: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2546 | self.logger.error("Exception getting vm status: %s", str(e)) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2547 | vm["status"] = "VIM_ERROR" |
| 2548 | vm["error_msg"] = str(e) |
| 2549 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2550 | vm_dict[vm_id] = vm |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2551 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2552 | return vm_dict |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 2553 | |
| tierno | 98e909c | 2017-10-14 13:27:03 +0200 | [diff] [blame] | 2554 | def action_vminstance(self, vm_id, action_dict, created_items={}): |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2555 | """Send and action over a VM instance from VIM |
| 2556 | Returns None or the console dict if the action was successfully sent to the VIM""" |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2557 | self.logger.debug("Action over VM '%s': %s", vm_id, str(action_dict)) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2558 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2559 | try: |
| 2560 | self._reload_connection() |
| 2561 | server = self.nova.servers.find(id=vm_id) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2562 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2563 | if "start" in action_dict: |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2564 | if action_dict["start"] == "rebuild": |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2565 | server.rebuild() |
| 2566 | else: |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2567 | if server.status == "PAUSED": |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2568 | server.unpause() |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2569 | elif server.status == "SUSPENDED": |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2570 | server.resume() |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2571 | elif server.status == "SHUTOFF": |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2572 | server.start() |
| k4.rahul | 78f474e | 2022-05-02 15:47:57 +0000 | [diff] [blame] | 2573 | else: |
| 2574 | self.logger.debug( |
| 2575 | "ERROR : Instance is not in SHUTOFF/PAUSE/SUSPEND state" |
| 2576 | ) |
| 2577 | raise vimconn.VimConnException( |
| 2578 | "Cannot 'start' instance while it is in active state", |
| 2579 | http_code=vimconn.HTTP_Bad_Request, |
| 2580 | ) |
| 2581 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2582 | elif "pause" in action_dict: |
| 2583 | server.pause() |
| 2584 | elif "resume" in action_dict: |
| 2585 | server.resume() |
| 2586 | elif "shutoff" in action_dict or "shutdown" in action_dict: |
| k4.rahul | 78f474e | 2022-05-02 15:47:57 +0000 | [diff] [blame] | 2587 | self.logger.debug("server status %s", server.status) |
| 2588 | if server.status == "ACTIVE": |
| 2589 | server.stop() |
| 2590 | else: |
| 2591 | self.logger.debug("ERROR: VM is not in Active state") |
| 2592 | raise vimconn.VimConnException( |
| 2593 | "VM is not in active state, stop operation is not allowed", |
| 2594 | http_code=vimconn.HTTP_Bad_Request, |
| 2595 | ) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2596 | elif "forceOff" in action_dict: |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2597 | server.stop() # TODO |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2598 | elif "terminate" in action_dict: |
| 2599 | server.delete() |
| 2600 | elif "createImage" in action_dict: |
| 2601 | server.create_image() |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2602 | # "path":path_schema, |
| 2603 | # "description":description_schema, |
| 2604 | # "name":name_schema, |
| 2605 | # "metadata":metadata_schema, |
| 2606 | # "imageRef": id_schema, |
| 2607 | # "disk": {"oneOf":[{"type": "null"}, {"type":"string"}] }, |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2608 | elif "rebuild" in action_dict: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2609 | server.rebuild(server.image["id"]) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2610 | elif "reboot" in action_dict: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2611 | server.reboot() # reboot_type="SOFT" |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2612 | elif "console" in action_dict: |
| 2613 | console_type = action_dict["console"] |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2614 | |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2615 | if console_type is None or console_type == "novnc": |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2616 | console_dict = server.get_vnc_console("novnc") |
| 2617 | elif console_type == "xvpvnc": |
| 2618 | console_dict = server.get_vnc_console(console_type) |
| 2619 | elif console_type == "rdp-html5": |
| 2620 | console_dict = server.get_rdp_console(console_type) |
| 2621 | elif console_type == "spice-html5": |
| 2622 | console_dict = server.get_spice_console(console_type) |
| 2623 | else: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2624 | raise vimconn.VimConnException( |
| 2625 | "console type '{}' not allowed".format(console_type), |
| 2626 | http_code=vimconn.HTTP_Bad_Request, |
| 2627 | ) |
| 2628 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2629 | try: |
| 2630 | console_url = console_dict["console"]["url"] |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2631 | # parse console_url |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2632 | protocol_index = console_url.find("//") |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2633 | suffix_index = ( |
| 2634 | console_url[protocol_index + 2 :].find("/") + protocol_index + 2 |
| 2635 | ) |
| 2636 | port_index = ( |
| 2637 | console_url[protocol_index + 2 : suffix_index].find(":") |
| 2638 | + protocol_index |
| 2639 | + 2 |
| 2640 | ) |
| 2641 | |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2642 | if protocol_index < 0 or port_index < 0 or suffix_index < 0: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2643 | raise vimconn.VimConnException( |
| 2644 | "Unexpected response from VIM " + str(console_dict) |
| 2645 | ) |
| 2646 | |
| 2647 | console_dict2 = { |
| 2648 | "protocol": console_url[0:protocol_index], |
| 2649 | "server": console_url[protocol_index + 2 : port_index], |
| 2650 | "port": int(console_url[port_index + 1 : suffix_index]), |
| 2651 | "suffix": console_url[suffix_index + 1 :], |
| 2652 | } |
| 2653 | |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 2654 | return console_dict2 |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2655 | except Exception: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2656 | raise vimconn.VimConnException( |
| 2657 | "Unexpected response from VIM " + str(console_dict) |
| 2658 | ) |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 2659 | |
| tierno | 98e909c | 2017-10-14 13:27:03 +0200 | [diff] [blame] | 2660 | return None |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2661 | except ( |
| 2662 | ksExceptions.ClientException, |
| 2663 | nvExceptions.ClientException, |
| 2664 | nvExceptions.NotFound, |
| 2665 | ConnectionError, |
| 2666 | ) as e: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2667 | self._format_exception(e) |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2668 | # TODO insert exception vimconn.HTTP_Unauthorized |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2669 | |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2670 | # ###### VIO Specific Changes ######### |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame] | 2671 | def _generate_vlanID(self): |
| kate | 721d79b | 2017-06-24 04:21:38 -0700 | [diff] [blame] | 2672 | """ |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2673 | Method to get unused vlanID |
| kate | 721d79b | 2017-06-24 04:21:38 -0700 | [diff] [blame] | 2674 | Args: |
| 2675 | None |
| 2676 | Returns: |
| 2677 | vlanID |
| 2678 | """ |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2679 | # Get used VLAN IDs |
| kate | 721d79b | 2017-06-24 04:21:38 -0700 | [diff] [blame] | 2680 | usedVlanIDs = [] |
| 2681 | networks = self.get_network_list() |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2682 | |
| kate | 721d79b | 2017-06-24 04:21:38 -0700 | [diff] [blame] | 2683 | for net in networks: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2684 | if net.get("provider:segmentation_id"): |
| 2685 | usedVlanIDs.append(net.get("provider:segmentation_id")) |
| 2686 | |
| kate | 721d79b | 2017-06-24 04:21:38 -0700 | [diff] [blame] | 2687 | used_vlanIDs = set(usedVlanIDs) |
| 2688 | |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2689 | # find unused VLAN ID |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2690 | for vlanID_range in self.config.get("dataplane_net_vlan_range"): |
| kate | 721d79b | 2017-06-24 04:21:38 -0700 | [diff] [blame] | 2691 | try: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2692 | start_vlanid, end_vlanid = map( |
| 2693 | int, vlanID_range.replace(" ", "").split("-") |
| 2694 | ) |
| 2695 | |
| tierno | 7d782ef | 2019-10-04 12:56:31 +0000 | [diff] [blame] | 2696 | for vlanID in range(start_vlanid, end_vlanid + 1): |
| kate | 721d79b | 2017-06-24 04:21:38 -0700 | [diff] [blame] | 2697 | if vlanID not in used_vlanIDs: |
| 2698 | return vlanID |
| 2699 | except Exception as exp: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2700 | raise vimconn.VimConnException( |
| 2701 | "Exception {} occurred while generating VLAN ID.".format(exp) |
| 2702 | ) |
| kate | 721d79b | 2017-06-24 04:21:38 -0700 | [diff] [blame] | 2703 | else: |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2704 | raise vimconn.VimConnConflictException( |
| 2705 | "Unable to create the SRIOV VLAN network. All given Vlan IDs {} are in use.".format( |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2706 | self.config.get("dataplane_net_vlan_range") |
| 2707 | ) |
| 2708 | ) |
| kate | 721d79b | 2017-06-24 04:21:38 -0700 | [diff] [blame] | 2709 | |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame] | 2710 | def _generate_multisegment_vlanID(self): |
| 2711 | """ |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2712 | Method to get unused vlanID |
| 2713 | Args: |
| 2714 | None |
| 2715 | Returns: |
| 2716 | vlanID |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame] | 2717 | """ |
| tierno | 6869ae7 | 2020-01-09 17:37:34 +0000 | [diff] [blame] | 2718 | # Get used VLAN IDs |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame] | 2719 | usedVlanIDs = [] |
| 2720 | networks = self.get_network_list() |
| 2721 | for net in networks: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2722 | if net.get("provider:network_type") == "vlan" and net.get( |
| 2723 | "provider:segmentation_id" |
| 2724 | ): |
| 2725 | usedVlanIDs.append(net.get("provider:segmentation_id")) |
| 2726 | elif net.get("segments"): |
| 2727 | for segment in net.get("segments"): |
| 2728 | if segment.get("provider:network_type") == "vlan" and segment.get( |
| 2729 | "provider:segmentation_id" |
| 2730 | ): |
| 2731 | usedVlanIDs.append(segment.get("provider:segmentation_id")) |
| 2732 | |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame] | 2733 | used_vlanIDs = set(usedVlanIDs) |
| 2734 | |
| tierno | 6869ae7 | 2020-01-09 17:37:34 +0000 | [diff] [blame] | 2735 | # find unused VLAN ID |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2736 | for vlanID_range in self.config.get("multisegment_vlan_range"): |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame] | 2737 | try: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2738 | start_vlanid, end_vlanid = map( |
| 2739 | int, vlanID_range.replace(" ", "").split("-") |
| 2740 | ) |
| 2741 | |
| tierno | 7d782ef | 2019-10-04 12:56:31 +0000 | [diff] [blame] | 2742 | for vlanID in range(start_vlanid, end_vlanid + 1): |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame] | 2743 | if vlanID not in used_vlanIDs: |
| 2744 | return vlanID |
| 2745 | except Exception as exp: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2746 | raise vimconn.VimConnException( |
| 2747 | "Exception {} occurred while generating VLAN ID.".format(exp) |
| 2748 | ) |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame] | 2749 | else: |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2750 | raise vimconn.VimConnConflictException( |
| 2751 | "Unable to create the VLAN segment. All VLAN IDs {} are in use.".format( |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2752 | self.config.get("multisegment_vlan_range") |
| 2753 | ) |
| 2754 | ) |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame] | 2755 | |
| 2756 | def _validate_vlan_ranges(self, input_vlan_range, text_vlan_range): |
| kate | 721d79b | 2017-06-24 04:21:38 -0700 | [diff] [blame] | 2757 | """ |
| 2758 | Method to validate user given vlanID ranges |
| 2759 | Args: None |
| 2760 | Returns: None |
| 2761 | """ |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame] | 2762 | for vlanID_range in input_vlan_range: |
| kate | 721d79b | 2017-06-24 04:21:38 -0700 | [diff] [blame] | 2763 | vlan_range = vlanID_range.replace(" ", "") |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2764 | # validate format |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2765 | vlanID_pattern = r"(\d)*-(\d)*$" |
| kate | 721d79b | 2017-06-24 04:21:38 -0700 | [diff] [blame] | 2766 | match_obj = re.match(vlanID_pattern, vlan_range) |
| 2767 | if not match_obj: |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2768 | raise vimconn.VimConnConflictException( |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2769 | "Invalid VLAN range for {}: {}.You must provide " |
| 2770 | "'{}' in format [start_ID - end_ID].".format( |
| 2771 | text_vlan_range, vlanID_range, text_vlan_range |
| 2772 | ) |
| 2773 | ) |
| kate | 721d79b | 2017-06-24 04:21:38 -0700 | [diff] [blame] | 2774 | |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2775 | start_vlanid, end_vlanid = map(int, vlan_range.split("-")) |
| 2776 | if start_vlanid <= 0: |
| 2777 | raise vimconn.VimConnConflictException( |
| 2778 | "Invalid VLAN range for {}: {}. Start ID can not be zero. For VLAN " |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2779 | "networks valid IDs are 1 to 4094 ".format( |
| 2780 | text_vlan_range, vlanID_range |
| 2781 | ) |
| 2782 | ) |
| 2783 | |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2784 | if end_vlanid > 4094: |
| 2785 | raise vimconn.VimConnConflictException( |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2786 | "Invalid VLAN range for {}: {}. End VLAN ID can not be " |
| 2787 | "greater than 4094. For VLAN networks valid IDs are 1 to 4094 ".format( |
| 2788 | text_vlan_range, vlanID_range |
| 2789 | ) |
| 2790 | ) |
| kate | 721d79b | 2017-06-24 04:21:38 -0700 | [diff] [blame] | 2791 | |
| 2792 | if start_vlanid > end_vlanid: |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2793 | raise vimconn.VimConnConflictException( |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2794 | "Invalid VLAN range for {}: {}. You must provide '{}'" |
| 2795 | " in format start_ID - end_ID and start_ID < end_ID ".format( |
| 2796 | text_vlan_range, vlanID_range, text_vlan_range |
| 2797 | ) |
| 2798 | ) |
| kate | 721d79b | 2017-06-24 04:21:38 -0700 | [diff] [blame] | 2799 | |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2800 | # NOT USED FUNCTIONS |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 2801 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2802 | def new_external_port(self, port_data): |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2803 | """Adds a external port to VIM |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2804 | Returns the port identifier""" |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2805 | # TODO openstack if needed |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2806 | return ( |
| 2807 | -vimconn.HTTP_Internal_Server_Error, |
| 2808 | "osconnector.new_external_port() not implemented", |
| 2809 | ) |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 2810 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2811 | def connect_port_network(self, port_id, network_id, admin=False): |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2812 | """Connects a external port to a network |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2813 | Returns status code of the VIM response""" |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2814 | # TODO openstack if needed |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2815 | return ( |
| 2816 | -vimconn.HTTP_Internal_Server_Error, |
| 2817 | "osconnector.connect_port_network() not implemented", |
| 2818 | ) |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 2819 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2820 | def new_user(self, user_name, user_passwd, tenant_id=None): |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2821 | """Adds a new user to openstack VIM |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2822 | Returns the user identifier""" |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2823 | self.logger.debug("osconnector: Adding a new user to VIM") |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2824 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2825 | try: |
| 2826 | self._reload_connection() |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2827 | user = self.keystone.users.create( |
| 2828 | user_name, password=user_passwd, default_project=tenant_id |
| 2829 | ) |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2830 | # self.keystone.tenants.add_user(self.k_creds["username"], #role) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2831 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2832 | return user.id |
| 2833 | except ksExceptions.ConnectionError as e: |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2834 | error_value = -vimconn.HTTP_Bad_Request |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2835 | error_text = ( |
| 2836 | type(e).__name__ |
| 2837 | + ": " |
| 2838 | + (str(e) if len(e.args) == 0 else str(e.args[0])) |
| 2839 | ) |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2840 | except ksExceptions.ClientException as e: # TODO remove |
| 2841 | error_value = -vimconn.HTTP_Bad_Request |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2842 | error_text = ( |
| 2843 | type(e).__name__ |
| 2844 | + ": " |
| 2845 | + (str(e) if len(e.args) == 0 else str(e.args[0])) |
| 2846 | ) |
| 2847 | |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2848 | # TODO insert exception vimconn.HTTP_Unauthorized |
| 2849 | # if reaching here is because an exception |
| tierno | 9c5c832 | 2018-03-23 15:44:03 +0100 | [diff] [blame] | 2850 | self.logger.debug("new_user " + error_text) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2851 | |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 2852 | return error_value, error_text |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2853 | |
| 2854 | def delete_user(self, user_id): |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2855 | """Delete a user from openstack VIM |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2856 | Returns the user identifier""" |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2857 | if self.debug: |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 2858 | print("osconnector: Deleting a user from VIM") |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2859 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2860 | try: |
| 2861 | self._reload_connection() |
| 2862 | self.keystone.users.delete(user_id) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2863 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2864 | return 1, user_id |
| 2865 | except ksExceptions.ConnectionError as e: |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2866 | error_value = -vimconn.HTTP_Bad_Request |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2867 | error_text = ( |
| 2868 | type(e).__name__ |
| 2869 | + ": " |
| 2870 | + (str(e) if len(e.args) == 0 else str(e.args[0])) |
| 2871 | ) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2872 | except ksExceptions.NotFound as e: |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2873 | error_value = -vimconn.HTTP_Not_Found |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2874 | error_text = ( |
| 2875 | type(e).__name__ |
| 2876 | + ": " |
| 2877 | + (str(e) if len(e.args) == 0 else str(e.args[0])) |
| 2878 | ) |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2879 | except ksExceptions.ClientException as e: # TODO remove |
| 2880 | error_value = -vimconn.HTTP_Bad_Request |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2881 | error_text = ( |
| 2882 | type(e).__name__ |
| 2883 | + ": " |
| 2884 | + (str(e) if len(e.args) == 0 else str(e.args[0])) |
| 2885 | ) |
| 2886 | |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2887 | # TODO insert exception vimconn.HTTP_Unauthorized |
| 2888 | # if reaching here is because an exception |
| 2889 | self.logger.debug("delete_tenant " + error_text) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2890 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2891 | return error_value, error_text |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 2892 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2893 | def get_hosts_info(self): |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2894 | """Get the information of deployed hosts |
| 2895 | Returns the hosts content""" |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2896 | if self.debug: |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 2897 | print("osconnector: Getting Host info from VIM") |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2898 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2899 | try: |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2900 | h_list = [] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2901 | self._reload_connection() |
| 2902 | hypervisors = self.nova.hypervisors.list() |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2903 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2904 | for hype in hypervisors: |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2905 | h_list.append(hype.to_dict()) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2906 | |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2907 | return 1, {"hosts": h_list} |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2908 | except nvExceptions.NotFound as e: |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2909 | error_value = -vimconn.HTTP_Not_Found |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2910 | error_text = str(e) if len(e.args) == 0 else str(e.args[0]) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2911 | except (ksExceptions.ClientException, nvExceptions.ClientException) as e: |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2912 | error_value = -vimconn.HTTP_Bad_Request |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2913 | error_text = ( |
| 2914 | type(e).__name__ |
| 2915 | + ": " |
| 2916 | + (str(e) if len(e.args) == 0 else str(e.args[0])) |
| 2917 | ) |
| 2918 | |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2919 | # TODO insert exception vimconn.HTTP_Unauthorized |
| 2920 | # if reaching here is because an exception |
| tierno | 9c5c832 | 2018-03-23 15:44:03 +0100 | [diff] [blame] | 2921 | self.logger.debug("get_hosts_info " + error_text) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2922 | |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 2923 | return error_value, error_text |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2924 | |
| 2925 | def get_hosts(self, vim_tenant): |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2926 | """Get the hosts and deployed instances |
| 2927 | Returns the hosts content""" |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2928 | r, hype_dict = self.get_hosts_info() |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2929 | |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2930 | if r < 0: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2931 | return r, hype_dict |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2932 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2933 | hypervisors = hype_dict["hosts"] |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2934 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2935 | try: |
| 2936 | servers = self.nova.servers.list() |
| 2937 | for hype in hypervisors: |
| 2938 | for server in servers: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2939 | if ( |
| 2940 | server.to_dict()["OS-EXT-SRV-ATTR:hypervisor_hostname"] |
| 2941 | == hype["hypervisor_hostname"] |
| 2942 | ): |
| 2943 | if "vm" in hype: |
| 2944 | hype["vm"].append(server.id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2945 | else: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2946 | hype["vm"] = [server.id] |
| 2947 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2948 | return 1, hype_dict |
| 2949 | except nvExceptions.NotFound as e: |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2950 | error_value = -vimconn.HTTP_Not_Found |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2951 | error_text = str(e) if len(e.args) == 0 else str(e.args[0]) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2952 | except (ksExceptions.ClientException, nvExceptions.ClientException) as e: |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2953 | error_value = -vimconn.HTTP_Bad_Request |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2954 | error_text = ( |
| 2955 | type(e).__name__ |
| 2956 | + ": " |
| 2957 | + (str(e) if len(e.args) == 0 else str(e.args[0])) |
| 2958 | ) |
| 2959 | |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 2960 | # TODO insert exception vimconn.HTTP_Unauthorized |
| 2961 | # if reaching here is because an exception |
| tierno | 9c5c832 | 2018-03-23 15:44:03 +0100 | [diff] [blame] | 2962 | self.logger.debug("get_hosts " + error_text) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2963 | |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 2964 | return error_value, error_text |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2965 | |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 2966 | def new_classification(self, name, ctype, definition): |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2967 | self.logger.debug( |
| 2968 | "Adding a new (Traffic) Classification to VIM, named %s", name |
| 2969 | ) |
| 2970 | |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 2971 | try: |
| 2972 | new_class = None |
| 2973 | self._reload_connection() |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2974 | |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 2975 | if ctype not in supportedClassificationTypes: |
| tierno | 7277486 | 2020-05-04 11:44:15 +0000 | [diff] [blame] | 2976 | raise vimconn.VimConnNotSupportedException( |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2977 | "OpenStack VIM connector does not support provided " |
| 2978 | "Classification Type {}, supported ones are: {}".format( |
| 2979 | ctype, supportedClassificationTypes |
| 2980 | ) |
| 2981 | ) |
| 2982 | |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 2983 | if not self._validate_classification(ctype, definition): |
| tierno | 7277486 | 2020-05-04 11:44:15 +0000 | [diff] [blame] | 2984 | raise vimconn.VimConnException( |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2985 | "Incorrect Classification definition for the type specified." |
| 2986 | ) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2987 | |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2988 | classification_dict = definition |
| 2989 | classification_dict["name"] = name |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2990 | new_class = self.neutron.create_sfc_flow_classifier( |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2991 | {"flow_classifier": classification_dict} |
| 2992 | ) |
| 2993 | |
| 2994 | return new_class["flow_classifier"]["id"] |
| 2995 | except ( |
| 2996 | neExceptions.ConnectionFailed, |
| 2997 | ksExceptions.ClientException, |
| 2998 | neExceptions.NeutronException, |
| 2999 | ConnectionError, |
| 3000 | ) as e: |
| 3001 | self.logger.error("Creation of Classification failed.") |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3002 | self._format_exception(e) |
| 3003 | |
| 3004 | def get_classification(self, class_id): |
| 3005 | self.logger.debug(" Getting Classification %s from VIM", class_id) |
| 3006 | filter_dict = {"id": class_id} |
| 3007 | class_list = self.get_classification_list(filter_dict) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3008 | |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3009 | if len(class_list) == 0: |
| tierno | 7277486 | 2020-05-04 11:44:15 +0000 | [diff] [blame] | 3010 | raise vimconn.VimConnNotFoundException( |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3011 | "Classification '{}' not found".format(class_id) |
| 3012 | ) |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3013 | elif len(class_list) > 1: |
| tierno | 7277486 | 2020-05-04 11:44:15 +0000 | [diff] [blame] | 3014 | raise vimconn.VimConnConflictException( |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3015 | "Found more than one Classification with this criteria" |
| 3016 | ) |
| 3017 | |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3018 | classification = class_list[0] |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3019 | |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3020 | return classification |
| 3021 | |
| 3022 | def get_classification_list(self, filter_dict={}): |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3023 | self.logger.debug( |
| 3024 | "Getting Classifications from VIM filter: '%s'", str(filter_dict) |
| 3025 | ) |
| 3026 | |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3027 | try: |
| tierno | 69b590e | 2018-03-13 18:52:23 +0100 | [diff] [blame] | 3028 | filter_dict_os = filter_dict.copy() |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3029 | self._reload_connection() |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3030 | |
| tierno | 69b590e | 2018-03-13 18:52:23 +0100 | [diff] [blame] | 3031 | if self.api_version3 and "tenant_id" in filter_dict_os: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3032 | filter_dict_os["project_id"] = filter_dict_os.pop("tenant_id") |
| 3033 | |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 3034 | classification_dict = self.neutron.list_sfc_flow_classifiers( |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3035 | **filter_dict_os |
| 3036 | ) |
| tierno | 69b590e | 2018-03-13 18:52:23 +0100 | [diff] [blame] | 3037 | classification_list = classification_dict["flow_classifiers"] |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3038 | self.__classification_os2mano(classification_list) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3039 | |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3040 | return classification_list |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3041 | except ( |
| 3042 | neExceptions.ConnectionFailed, |
| 3043 | ksExceptions.ClientException, |
| 3044 | neExceptions.NeutronException, |
| 3045 | ConnectionError, |
| 3046 | ) as e: |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3047 | self._format_exception(e) |
| 3048 | |
| 3049 | def delete_classification(self, class_id): |
| 3050 | self.logger.debug("Deleting Classification '%s' from VIM", class_id) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3051 | |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3052 | try: |
| 3053 | self._reload_connection() |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 3054 | self.neutron.delete_sfc_flow_classifier(class_id) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3055 | |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3056 | return class_id |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3057 | except ( |
| 3058 | neExceptions.ConnectionFailed, |
| 3059 | neExceptions.NeutronException, |
| 3060 | ksExceptions.ClientException, |
| 3061 | neExceptions.NeutronException, |
| 3062 | ConnectionError, |
| 3063 | ) as e: |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3064 | self._format_exception(e) |
| 3065 | |
| 3066 | def new_sfi(self, name, ingress_ports, egress_ports, sfc_encap=True): |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3067 | self.logger.debug( |
| 3068 | "Adding a new Service Function Instance to VIM, named '%s'", name |
| 3069 | ) |
| 3070 | |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3071 | try: |
| 3072 | new_sfi = None |
| 3073 | self._reload_connection() |
| 3074 | correlation = None |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3075 | |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3076 | if sfc_encap: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3077 | correlation = "nsh" |
| 3078 | |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3079 | if len(ingress_ports) != 1: |
| tierno | 7277486 | 2020-05-04 11:44:15 +0000 | [diff] [blame] | 3080 | raise vimconn.VimConnNotSupportedException( |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3081 | "OpenStack VIM connector can only have 1 ingress port per SFI" |
| 3082 | ) |
| 3083 | |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3084 | if len(egress_ports) != 1: |
| tierno | 7277486 | 2020-05-04 11:44:15 +0000 | [diff] [blame] | 3085 | raise vimconn.VimConnNotSupportedException( |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3086 | "OpenStack VIM connector can only have 1 egress port per SFI" |
| 3087 | ) |
| 3088 | |
| 3089 | sfi_dict = { |
| 3090 | "name": name, |
| 3091 | "ingress": ingress_ports[0], |
| 3092 | "egress": egress_ports[0], |
| 3093 | "service_function_parameters": {"correlation": correlation}, |
| 3094 | } |
| 3095 | new_sfi = self.neutron.create_sfc_port_pair({"port_pair": sfi_dict}) |
| 3096 | |
| 3097 | return new_sfi["port_pair"]["id"] |
| 3098 | except ( |
| 3099 | neExceptions.ConnectionFailed, |
| 3100 | ksExceptions.ClientException, |
| 3101 | neExceptions.NeutronException, |
| 3102 | ConnectionError, |
| 3103 | ) as e: |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3104 | if new_sfi: |
| 3105 | try: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3106 | self.neutron.delete_sfc_port_pair(new_sfi["port_pair"]["id"]) |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3107 | except Exception: |
| 3108 | self.logger.error( |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3109 | "Creation of Service Function Instance failed, with " |
| 3110 | "subsequent deletion failure as well." |
| 3111 | ) |
| 3112 | |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3113 | self._format_exception(e) |
| 3114 | |
| 3115 | def get_sfi(self, sfi_id): |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3116 | self.logger.debug("Getting Service Function Instance %s from VIM", sfi_id) |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3117 | filter_dict = {"id": sfi_id} |
| 3118 | sfi_list = self.get_sfi_list(filter_dict) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3119 | |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3120 | if len(sfi_list) == 0: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3121 | raise vimconn.VimConnNotFoundException( |
| 3122 | "Service Function Instance '{}' not found".format(sfi_id) |
| 3123 | ) |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3124 | elif len(sfi_list) > 1: |
| tierno | 7277486 | 2020-05-04 11:44:15 +0000 | [diff] [blame] | 3125 | raise vimconn.VimConnConflictException( |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3126 | "Found more than one Service Function Instance with this criteria" |
| 3127 | ) |
| 3128 | |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3129 | sfi = sfi_list[0] |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3130 | |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3131 | return sfi |
| 3132 | |
| 3133 | def get_sfi_list(self, filter_dict={}): |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3134 | self.logger.debug( |
| 3135 | "Getting Service Function Instances from VIM filter: '%s'", str(filter_dict) |
| 3136 | ) |
| 3137 | |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3138 | try: |
| 3139 | self._reload_connection() |
| tierno | 69b590e | 2018-03-13 18:52:23 +0100 | [diff] [blame] | 3140 | filter_dict_os = filter_dict.copy() |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3141 | |
| tierno | 69b590e | 2018-03-13 18:52:23 +0100 | [diff] [blame] | 3142 | if self.api_version3 and "tenant_id" in filter_dict_os: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3143 | filter_dict_os["project_id"] = filter_dict_os.pop("tenant_id") |
| 3144 | |
| tierno | 69b590e | 2018-03-13 18:52:23 +0100 | [diff] [blame] | 3145 | sfi_dict = self.neutron.list_sfc_port_pairs(**filter_dict_os) |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3146 | sfi_list = sfi_dict["port_pairs"] |
| 3147 | self.__sfi_os2mano(sfi_list) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3148 | |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3149 | return sfi_list |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3150 | except ( |
| 3151 | neExceptions.ConnectionFailed, |
| 3152 | ksExceptions.ClientException, |
| 3153 | neExceptions.NeutronException, |
| 3154 | ConnectionError, |
| 3155 | ) as e: |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3156 | self._format_exception(e) |
| 3157 | |
| 3158 | def delete_sfi(self, sfi_id): |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3159 | self.logger.debug("Deleting Service Function Instance '%s' from VIM", sfi_id) |
| 3160 | |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3161 | try: |
| 3162 | self._reload_connection() |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 3163 | self.neutron.delete_sfc_port_pair(sfi_id) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3164 | |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3165 | return sfi_id |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3166 | except ( |
| 3167 | neExceptions.ConnectionFailed, |
| 3168 | neExceptions.NeutronException, |
| 3169 | ksExceptions.ClientException, |
| 3170 | neExceptions.NeutronException, |
| 3171 | ConnectionError, |
| 3172 | ) as e: |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3173 | self._format_exception(e) |
| 3174 | |
| 3175 | def new_sf(self, name, sfis, sfc_encap=True): |
| tierno | 7d782ef | 2019-10-04 12:56:31 +0000 | [diff] [blame] | 3176 | self.logger.debug("Adding a new Service Function to VIM, named '%s'", name) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3177 | |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3178 | try: |
| 3179 | new_sf = None |
| 3180 | self._reload_connection() |
| tierno | 9c5c832 | 2018-03-23 15:44:03 +0100 | [diff] [blame] | 3181 | # correlation = None |
| 3182 | # if sfc_encap: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3183 | # correlation = "nsh" |
| 3184 | |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3185 | for instance in sfis: |
| 3186 | sfi = self.get_sfi(instance) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3187 | |
| 3188 | if sfi.get("sfc_encap") != sfc_encap: |
| tierno | 7277486 | 2020-05-04 11:44:15 +0000 | [diff] [blame] | 3189 | raise vimconn.VimConnNotSupportedException( |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3190 | "OpenStack VIM connector requires all SFIs of the " |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3191 | "same SF to share the same SFC Encapsulation" |
| 3192 | ) |
| 3193 | |
| 3194 | sf_dict = {"name": name, "port_pairs": sfis} |
| 3195 | new_sf = self.neutron.create_sfc_port_pair_group( |
| 3196 | {"port_pair_group": sf_dict} |
| 3197 | ) |
| 3198 | |
| 3199 | return new_sf["port_pair_group"]["id"] |
| 3200 | except ( |
| 3201 | neExceptions.ConnectionFailed, |
| 3202 | ksExceptions.ClientException, |
| 3203 | neExceptions.NeutronException, |
| 3204 | ConnectionError, |
| 3205 | ) as e: |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3206 | if new_sf: |
| 3207 | try: |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 3208 | self.neutron.delete_sfc_port_pair_group( |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3209 | new_sf["port_pair_group"]["id"] |
| 3210 | ) |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3211 | except Exception: |
| 3212 | self.logger.error( |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3213 | "Creation of Service Function failed, with " |
| 3214 | "subsequent deletion failure as well." |
| 3215 | ) |
| 3216 | |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3217 | self._format_exception(e) |
| 3218 | |
| 3219 | def get_sf(self, sf_id): |
| 3220 | self.logger.debug("Getting Service Function %s from VIM", sf_id) |
| 3221 | filter_dict = {"id": sf_id} |
| 3222 | sf_list = self.get_sf_list(filter_dict) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3223 | |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3224 | if len(sf_list) == 0: |
| tierno | 7277486 | 2020-05-04 11:44:15 +0000 | [diff] [blame] | 3225 | raise vimconn.VimConnNotFoundException( |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3226 | "Service Function '{}' not found".format(sf_id) |
| 3227 | ) |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3228 | elif len(sf_list) > 1: |
| tierno | 7277486 | 2020-05-04 11:44:15 +0000 | [diff] [blame] | 3229 | raise vimconn.VimConnConflictException( |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3230 | "Found more than one Service Function with this criteria" |
| 3231 | ) |
| 3232 | |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3233 | sf = sf_list[0] |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3234 | |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3235 | return sf |
| 3236 | |
| 3237 | def get_sf_list(self, filter_dict={}): |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3238 | self.logger.debug( |
| 3239 | "Getting Service Function from VIM filter: '%s'", str(filter_dict) |
| 3240 | ) |
| 3241 | |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3242 | try: |
| 3243 | self._reload_connection() |
| tierno | 69b590e | 2018-03-13 18:52:23 +0100 | [diff] [blame] | 3244 | filter_dict_os = filter_dict.copy() |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3245 | |
| tierno | 69b590e | 2018-03-13 18:52:23 +0100 | [diff] [blame] | 3246 | if self.api_version3 and "tenant_id" in filter_dict_os: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3247 | filter_dict_os["project_id"] = filter_dict_os.pop("tenant_id") |
| 3248 | |
| tierno | 69b590e | 2018-03-13 18:52:23 +0100 | [diff] [blame] | 3249 | sf_dict = self.neutron.list_sfc_port_pair_groups(**filter_dict_os) |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3250 | sf_list = sf_dict["port_pair_groups"] |
| 3251 | self.__sf_os2mano(sf_list) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3252 | |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3253 | return sf_list |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3254 | except ( |
| 3255 | neExceptions.ConnectionFailed, |
| 3256 | ksExceptions.ClientException, |
| 3257 | neExceptions.NeutronException, |
| 3258 | ConnectionError, |
| 3259 | ) as e: |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3260 | self._format_exception(e) |
| 3261 | |
| 3262 | def delete_sf(self, sf_id): |
| 3263 | self.logger.debug("Deleting Service Function '%s' from VIM", sf_id) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3264 | |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3265 | try: |
| 3266 | self._reload_connection() |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 3267 | self.neutron.delete_sfc_port_pair_group(sf_id) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3268 | |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3269 | return sf_id |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3270 | except ( |
| 3271 | neExceptions.ConnectionFailed, |
| 3272 | neExceptions.NeutronException, |
| 3273 | ksExceptions.ClientException, |
| 3274 | neExceptions.NeutronException, |
| 3275 | ConnectionError, |
| 3276 | ) as e: |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3277 | self._format_exception(e) |
| 3278 | |
| 3279 | def new_sfp(self, name, classifications, sfs, sfc_encap=True, spi=None): |
| tierno | 7d782ef | 2019-10-04 12:56:31 +0000 | [diff] [blame] | 3280 | self.logger.debug("Adding a new Service Function Path to VIM, named '%s'", name) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3281 | |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3282 | try: |
| 3283 | new_sfp = None |
| 3284 | self._reload_connection() |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 3285 | # In networking-sfc the MPLS encapsulation is legacy |
| 3286 | # should be used when no full SFC Encapsulation is intended |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3287 | correlation = "mpls" |
| 3288 | |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 3289 | if sfc_encap: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3290 | correlation = "nsh" |
| 3291 | |
| 3292 | sfp_dict = { |
| 3293 | "name": name, |
| 3294 | "flow_classifiers": classifications, |
| 3295 | "port_pair_groups": sfs, |
| 3296 | "chain_parameters": {"correlation": correlation}, |
| 3297 | } |
| 3298 | |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3299 | if spi: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3300 | sfp_dict["chain_id"] = spi |
| 3301 | |
| 3302 | new_sfp = self.neutron.create_sfc_port_chain({"port_chain": sfp_dict}) |
| 3303 | |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3304 | return new_sfp["port_chain"]["id"] |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3305 | except ( |
| 3306 | neExceptions.ConnectionFailed, |
| 3307 | ksExceptions.ClientException, |
| 3308 | neExceptions.NeutronException, |
| 3309 | ConnectionError, |
| 3310 | ) as e: |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3311 | if new_sfp: |
| 3312 | try: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3313 | self.neutron.delete_sfc_port_chain(new_sfp["port_chain"]["id"]) |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3314 | except Exception: |
| 3315 | self.logger.error( |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3316 | "Creation of Service Function Path failed, with " |
| 3317 | "subsequent deletion failure as well." |
| 3318 | ) |
| 3319 | |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3320 | self._format_exception(e) |
| 3321 | |
| 3322 | def get_sfp(self, sfp_id): |
| 3323 | self.logger.debug(" Getting Service Function Path %s from VIM", sfp_id) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3324 | |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3325 | filter_dict = {"id": sfp_id} |
| 3326 | sfp_list = self.get_sfp_list(filter_dict) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3327 | |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3328 | if len(sfp_list) == 0: |
| tierno | 7277486 | 2020-05-04 11:44:15 +0000 | [diff] [blame] | 3329 | raise vimconn.VimConnNotFoundException( |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3330 | "Service Function Path '{}' not found".format(sfp_id) |
| 3331 | ) |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3332 | elif len(sfp_list) > 1: |
| tierno | 7277486 | 2020-05-04 11:44:15 +0000 | [diff] [blame] | 3333 | raise vimconn.VimConnConflictException( |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3334 | "Found more than one Service Function Path with this criteria" |
| 3335 | ) |
| 3336 | |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3337 | sfp = sfp_list[0] |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3338 | |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3339 | return sfp |
| 3340 | |
| 3341 | def get_sfp_list(self, filter_dict={}): |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3342 | self.logger.debug( |
| 3343 | "Getting Service Function Paths from VIM filter: '%s'", str(filter_dict) |
| 3344 | ) |
| 3345 | |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3346 | try: |
| 3347 | self._reload_connection() |
| tierno | 69b590e | 2018-03-13 18:52:23 +0100 | [diff] [blame] | 3348 | filter_dict_os = filter_dict.copy() |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3349 | |
| tierno | 69b590e | 2018-03-13 18:52:23 +0100 | [diff] [blame] | 3350 | if self.api_version3 and "tenant_id" in filter_dict_os: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3351 | filter_dict_os["project_id"] = filter_dict_os.pop("tenant_id") |
| 3352 | |
| tierno | 69b590e | 2018-03-13 18:52:23 +0100 | [diff] [blame] | 3353 | sfp_dict = self.neutron.list_sfc_port_chains(**filter_dict_os) |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3354 | sfp_list = sfp_dict["port_chains"] |
| 3355 | self.__sfp_os2mano(sfp_list) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3356 | |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3357 | return sfp_list |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3358 | except ( |
| 3359 | neExceptions.ConnectionFailed, |
| 3360 | ksExceptions.ClientException, |
| 3361 | neExceptions.NeutronException, |
| 3362 | ConnectionError, |
| 3363 | ) as e: |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3364 | self._format_exception(e) |
| 3365 | |
| 3366 | def delete_sfp(self, sfp_id): |
| tierno | 7d782ef | 2019-10-04 12:56:31 +0000 | [diff] [blame] | 3367 | self.logger.debug("Deleting Service Function Path '%s' from VIM", sfp_id) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3368 | |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3369 | try: |
| 3370 | self._reload_connection() |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 3371 | self.neutron.delete_sfc_port_chain(sfp_id) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3372 | |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3373 | return sfp_id |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3374 | except ( |
| 3375 | neExceptions.ConnectionFailed, |
| 3376 | neExceptions.NeutronException, |
| 3377 | ksExceptions.ClientException, |
| 3378 | neExceptions.NeutronException, |
| 3379 | ConnectionError, |
| 3380 | ) as e: |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 3381 | self._format_exception(e) |
| borsatti | 8a2dda3 | 2019-12-18 15:08:57 +0000 | [diff] [blame] | 3382 | |
| borsatti | 8a2dda3 | 2019-12-18 15:08:57 +0000 | [diff] [blame] | 3383 | def refresh_sfps_status(self, sfp_list): |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 3384 | """Get the status of the service function path |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3385 | Params: the list of sfp identifiers |
| 3386 | Returns a dictionary with: |
| 3387 | vm_id: #VIM id of this service function path |
| 3388 | status: #Mandatory. Text with one of: |
| 3389 | # DELETED (not found at vim) |
| 3390 | # VIM_ERROR (Cannot connect to VIM, VIM response error, ...) |
| 3391 | # OTHER (Vim reported other status not understood) |
| 3392 | # ERROR (VIM indicates an ERROR status) |
| 3393 | # ACTIVE, |
| 3394 | # CREATING (on building process) |
| 3395 | error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR |
| 3396 | vim_info: #Text with plain information obtained from vim (yaml.safe_dump)F |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 3397 | """ |
| 3398 | sfp_dict = {} |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3399 | self.logger.debug( |
| 3400 | "refresh_sfps status: Getting tenant SFP information from VIM" |
| 3401 | ) |
| 3402 | |
| borsatti | 8a2dda3 | 2019-12-18 15:08:57 +0000 | [diff] [blame] | 3403 | for sfp_id in sfp_list: |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 3404 | sfp = {} |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3405 | |
| borsatti | 8a2dda3 | 2019-12-18 15:08:57 +0000 | [diff] [blame] | 3406 | try: |
| 3407 | sfp_vim = self.get_sfp(sfp_id) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3408 | |
| 3409 | if sfp_vim["spi"]: |
| 3410 | sfp["status"] = vmStatus2manoFormat["ACTIVE"] |
| borsatti | 8a2dda3 | 2019-12-18 15:08:57 +0000 | [diff] [blame] | 3411 | else: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3412 | sfp["status"] = "OTHER" |
| 3413 | sfp["error_msg"] = "VIM status reported " + sfp["status"] |
| borsatti | 8a2dda3 | 2019-12-18 15:08:57 +0000 | [diff] [blame] | 3414 | |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3415 | sfp["vim_info"] = self.serialize(sfp_vim) |
| borsatti | 8a2dda3 | 2019-12-18 15:08:57 +0000 | [diff] [blame] | 3416 | |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3417 | if sfp_vim.get("fault"): |
| 3418 | sfp["error_msg"] = str(sfp_vim["fault"]) |
| tierno | 7277486 | 2020-05-04 11:44:15 +0000 | [diff] [blame] | 3419 | except vimconn.VimConnNotFoundException as e: |
| borsatti | 8a2dda3 | 2019-12-18 15:08:57 +0000 | [diff] [blame] | 3420 | self.logger.error("Exception getting sfp status: %s", str(e)) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3421 | sfp["status"] = "DELETED" |
| 3422 | sfp["error_msg"] = str(e) |
| tierno | 7277486 | 2020-05-04 11:44:15 +0000 | [diff] [blame] | 3423 | except vimconn.VimConnException as e: |
| borsatti | 8a2dda3 | 2019-12-18 15:08:57 +0000 | [diff] [blame] | 3424 | self.logger.error("Exception getting sfp status: %s", str(e)) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3425 | sfp["status"] = "VIM_ERROR" |
| 3426 | sfp["error_msg"] = str(e) |
| 3427 | |
| borsatti | 8a2dda3 | 2019-12-18 15:08:57 +0000 | [diff] [blame] | 3428 | sfp_dict[sfp_id] = sfp |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3429 | |
| borsatti | 8a2dda3 | 2019-12-18 15:08:57 +0000 | [diff] [blame] | 3430 | return sfp_dict |
| 3431 | |
| borsatti | 8a2dda3 | 2019-12-18 15:08:57 +0000 | [diff] [blame] | 3432 | def refresh_sfis_status(self, sfi_list): |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 3433 | """Get the status of the service function instances |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3434 | Params: the list of sfi identifiers |
| 3435 | Returns a dictionary with: |
| 3436 | vm_id: #VIM id of this service function instance |
| 3437 | status: #Mandatory. Text with one of: |
| 3438 | # DELETED (not found at vim) |
| 3439 | # VIM_ERROR (Cannot connect to VIM, VIM response error, ...) |
| 3440 | # OTHER (Vim reported other status not understood) |
| 3441 | # ERROR (VIM indicates an ERROR status) |
| 3442 | # ACTIVE, |
| 3443 | # CREATING (on building process) |
| 3444 | error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR |
| 3445 | vim_info: #Text with plain information obtained from vim (yaml.safe_dump) |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 3446 | """ |
| 3447 | sfi_dict = {} |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3448 | self.logger.debug( |
| 3449 | "refresh_sfis status: Getting tenant sfi information from VIM" |
| 3450 | ) |
| 3451 | |
| borsatti | 8a2dda3 | 2019-12-18 15:08:57 +0000 | [diff] [blame] | 3452 | for sfi_id in sfi_list: |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 3453 | sfi = {} |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3454 | |
| borsatti | 8a2dda3 | 2019-12-18 15:08:57 +0000 | [diff] [blame] | 3455 | try: |
| 3456 | sfi_vim = self.get_sfi(sfi_id) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3457 | |
| borsatti | 8a2dda3 | 2019-12-18 15:08:57 +0000 | [diff] [blame] | 3458 | if sfi_vim: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3459 | sfi["status"] = vmStatus2manoFormat["ACTIVE"] |
| borsatti | 8a2dda3 | 2019-12-18 15:08:57 +0000 | [diff] [blame] | 3460 | else: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3461 | sfi["status"] = "OTHER" |
| 3462 | sfi["error_msg"] = "VIM status reported " + sfi["status"] |
| borsatti | 8a2dda3 | 2019-12-18 15:08:57 +0000 | [diff] [blame] | 3463 | |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3464 | sfi["vim_info"] = self.serialize(sfi_vim) |
| borsatti | 8a2dda3 | 2019-12-18 15:08:57 +0000 | [diff] [blame] | 3465 | |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3466 | if sfi_vim.get("fault"): |
| 3467 | sfi["error_msg"] = str(sfi_vim["fault"]) |
| tierno | 7277486 | 2020-05-04 11:44:15 +0000 | [diff] [blame] | 3468 | except vimconn.VimConnNotFoundException as e: |
| borsatti | 8a2dda3 | 2019-12-18 15:08:57 +0000 | [diff] [blame] | 3469 | self.logger.error("Exception getting sfi status: %s", str(e)) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3470 | sfi["status"] = "DELETED" |
| 3471 | sfi["error_msg"] = str(e) |
| tierno | 7277486 | 2020-05-04 11:44:15 +0000 | [diff] [blame] | 3472 | except vimconn.VimConnException as e: |
| borsatti | 8a2dda3 | 2019-12-18 15:08:57 +0000 | [diff] [blame] | 3473 | self.logger.error("Exception getting sfi status: %s", str(e)) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3474 | sfi["status"] = "VIM_ERROR" |
| 3475 | sfi["error_msg"] = str(e) |
| 3476 | |
| borsatti | 8a2dda3 | 2019-12-18 15:08:57 +0000 | [diff] [blame] | 3477 | sfi_dict[sfi_id] = sfi |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3478 | |
| borsatti | 8a2dda3 | 2019-12-18 15:08:57 +0000 | [diff] [blame] | 3479 | return sfi_dict |
| 3480 | |
| borsatti | 8a2dda3 | 2019-12-18 15:08:57 +0000 | [diff] [blame] | 3481 | def refresh_sfs_status(self, sf_list): |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 3482 | """Get the status of the service functions |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3483 | Params: the list of sf identifiers |
| 3484 | Returns a dictionary with: |
| 3485 | vm_id: #VIM id of this service function |
| 3486 | status: #Mandatory. Text with one of: |
| 3487 | # DELETED (not found at vim) |
| 3488 | # VIM_ERROR (Cannot connect to VIM, VIM response error, ...) |
| 3489 | # OTHER (Vim reported other status not understood) |
| 3490 | # ERROR (VIM indicates an ERROR status) |
| 3491 | # ACTIVE, |
| 3492 | # CREATING (on building process) |
| 3493 | error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR |
| 3494 | vim_info: #Text with plain information obtained from vim (yaml.safe_dump) |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 3495 | """ |
| 3496 | sf_dict = {} |
| borsatti | 8a2dda3 | 2019-12-18 15:08:57 +0000 | [diff] [blame] | 3497 | self.logger.debug("refresh_sfs status: Getting tenant sf information from VIM") |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3498 | |
| borsatti | 8a2dda3 | 2019-12-18 15:08:57 +0000 | [diff] [blame] | 3499 | for sf_id in sf_list: |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 3500 | sf = {} |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3501 | |
| borsatti | 8a2dda3 | 2019-12-18 15:08:57 +0000 | [diff] [blame] | 3502 | try: |
| 3503 | sf_vim = self.get_sf(sf_id) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3504 | |
| borsatti | 8a2dda3 | 2019-12-18 15:08:57 +0000 | [diff] [blame] | 3505 | if sf_vim: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3506 | sf["status"] = vmStatus2manoFormat["ACTIVE"] |
| borsatti | 8a2dda3 | 2019-12-18 15:08:57 +0000 | [diff] [blame] | 3507 | else: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3508 | sf["status"] = "OTHER" |
| 3509 | sf["error_msg"] = "VIM status reported " + sf_vim["status"] |
| borsatti | 8a2dda3 | 2019-12-18 15:08:57 +0000 | [diff] [blame] | 3510 | |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3511 | sf["vim_info"] = self.serialize(sf_vim) |
| borsatti | 8a2dda3 | 2019-12-18 15:08:57 +0000 | [diff] [blame] | 3512 | |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3513 | if sf_vim.get("fault"): |
| 3514 | sf["error_msg"] = str(sf_vim["fault"]) |
| tierno | 7277486 | 2020-05-04 11:44:15 +0000 | [diff] [blame] | 3515 | except vimconn.VimConnNotFoundException as e: |
| borsatti | 8a2dda3 | 2019-12-18 15:08:57 +0000 | [diff] [blame] | 3516 | self.logger.error("Exception getting sf status: %s", str(e)) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3517 | sf["status"] = "DELETED" |
| 3518 | sf["error_msg"] = str(e) |
| tierno | 7277486 | 2020-05-04 11:44:15 +0000 | [diff] [blame] | 3519 | except vimconn.VimConnException as e: |
| borsatti | 8a2dda3 | 2019-12-18 15:08:57 +0000 | [diff] [blame] | 3520 | self.logger.error("Exception getting sf status: %s", str(e)) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3521 | sf["status"] = "VIM_ERROR" |
| 3522 | sf["error_msg"] = str(e) |
| 3523 | |
| borsatti | 8a2dda3 | 2019-12-18 15:08:57 +0000 | [diff] [blame] | 3524 | sf_dict[sf_id] = sf |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3525 | |
| borsatti | 8a2dda3 | 2019-12-18 15:08:57 +0000 | [diff] [blame] | 3526 | return sf_dict |
| 3527 | |
| borsatti | 8a2dda3 | 2019-12-18 15:08:57 +0000 | [diff] [blame] | 3528 | def refresh_classifications_status(self, classification_list): |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 3529 | """Get the status of the classifications |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3530 | Params: the list of classification identifiers |
| 3531 | Returns a dictionary with: |
| 3532 | vm_id: #VIM id of this classifier |
| 3533 | status: #Mandatory. Text with one of: |
| 3534 | # DELETED (not found at vim) |
| 3535 | # VIM_ERROR (Cannot connect to VIM, VIM response error, ...) |
| 3536 | # OTHER (Vim reported other status not understood) |
| 3537 | # ERROR (VIM indicates an ERROR status) |
| 3538 | # ACTIVE, |
| 3539 | # CREATING (on building process) |
| 3540 | error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR |
| 3541 | vim_info: #Text with plain information obtained from vim (yaml.safe_dump) |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 3542 | """ |
| 3543 | classification_dict = {} |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3544 | self.logger.debug( |
| 3545 | "refresh_classifications status: Getting tenant classification information from VIM" |
| 3546 | ) |
| 3547 | |
| borsatti | 8a2dda3 | 2019-12-18 15:08:57 +0000 | [diff] [blame] | 3548 | for classification_id in classification_list: |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 3549 | classification = {} |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3550 | |
| borsatti | 8a2dda3 | 2019-12-18 15:08:57 +0000 | [diff] [blame] | 3551 | try: |
| 3552 | classification_vim = self.get_classification(classification_id) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3553 | |
| borsatti | 8a2dda3 | 2019-12-18 15:08:57 +0000 | [diff] [blame] | 3554 | if classification_vim: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3555 | classification["status"] = vmStatus2manoFormat["ACTIVE"] |
| borsatti | 8a2dda3 | 2019-12-18 15:08:57 +0000 | [diff] [blame] | 3556 | else: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3557 | classification["status"] = "OTHER" |
| 3558 | classification["error_msg"] = ( |
| 3559 | "VIM status reported " + classification["status"] |
| 3560 | ) |
| borsatti | 8a2dda3 | 2019-12-18 15:08:57 +0000 | [diff] [blame] | 3561 | |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3562 | classification["vim_info"] = self.serialize(classification_vim) |
| borsatti | 8a2dda3 | 2019-12-18 15:08:57 +0000 | [diff] [blame] | 3563 | |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3564 | if classification_vim.get("fault"): |
| 3565 | classification["error_msg"] = str(classification_vim["fault"]) |
| tierno | 7277486 | 2020-05-04 11:44:15 +0000 | [diff] [blame] | 3566 | except vimconn.VimConnNotFoundException as e: |
| borsatti | 8a2dda3 | 2019-12-18 15:08:57 +0000 | [diff] [blame] | 3567 | self.logger.error("Exception getting classification status: %s", str(e)) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3568 | classification["status"] = "DELETED" |
| 3569 | classification["error_msg"] = str(e) |
| tierno | 7277486 | 2020-05-04 11:44:15 +0000 | [diff] [blame] | 3570 | except vimconn.VimConnException as e: |
| borsatti | 8a2dda3 | 2019-12-18 15:08:57 +0000 | [diff] [blame] | 3571 | self.logger.error("Exception getting classification status: %s", str(e)) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3572 | classification["status"] = "VIM_ERROR" |
| 3573 | classification["error_msg"] = str(e) |
| 3574 | |
| borsatti | 8a2dda3 | 2019-12-18 15:08:57 +0000 | [diff] [blame] | 3575 | classification_dict[classification_id] = classification |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 3576 | |
| borsatti | 8a2dda3 | 2019-12-18 15:08:57 +0000 | [diff] [blame] | 3577 | return classification_dict |
| Alexis Romero | b70f4ed | 2022-03-11 18:00:49 +0100 | [diff] [blame] | 3578 | |
| 3579 | def new_affinity_group(self, affinity_group_data): |
| 3580 | """Adds a server group to VIM |
| 3581 | affinity_group_data contains a dictionary with information, keys: |
| 3582 | name: name in VIM for the server group |
| 3583 | type: affinity or anti-affinity |
| 3584 | scope: Only nfvi-node allowed |
| 3585 | Returns the server group identifier""" |
| 3586 | self.logger.debug("Adding Server Group '%s'", str(affinity_group_data)) |
| 3587 | |
| 3588 | try: |
| 3589 | name = affinity_group_data["name"] |
| 3590 | policy = affinity_group_data["type"] |
| 3591 | |
| 3592 | self._reload_connection() |
| 3593 | new_server_group = self.nova.server_groups.create(name, policy) |
| 3594 | |
| 3595 | return new_server_group.id |
| 3596 | except ( |
| 3597 | ksExceptions.ClientException, |
| 3598 | nvExceptions.ClientException, |
| 3599 | ConnectionError, |
| 3600 | KeyError, |
| 3601 | ) as e: |
| 3602 | self._format_exception(e) |
| 3603 | |
| 3604 | def get_affinity_group(self, affinity_group_id): |
| 3605 | """Obtain server group details from the VIM. Returns the server group detais as a dict""" |
| 3606 | self.logger.debug("Getting flavor '%s'", affinity_group_id) |
| 3607 | try: |
| 3608 | self._reload_connection() |
| 3609 | server_group = self.nova.server_groups.find(id=affinity_group_id) |
| 3610 | |
| 3611 | return server_group.to_dict() |
| 3612 | except ( |
| 3613 | nvExceptions.NotFound, |
| 3614 | nvExceptions.ClientException, |
| 3615 | ksExceptions.ClientException, |
| 3616 | ConnectionError, |
| 3617 | ) as e: |
| 3618 | self._format_exception(e) |
| 3619 | |
| 3620 | def delete_affinity_group(self, affinity_group_id): |
| 3621 | """Deletes a server group from the VIM. Returns the old affinity_group_id""" |
| 3622 | self.logger.debug("Getting server group '%s'", affinity_group_id) |
| 3623 | try: |
| 3624 | self._reload_connection() |
| 3625 | self.nova.server_groups.delete(affinity_group_id) |
| 3626 | |
| 3627 | return affinity_group_id |
| 3628 | except ( |
| 3629 | nvExceptions.NotFound, |
| 3630 | ksExceptions.ClientException, |
| 3631 | nvExceptions.ClientException, |
| 3632 | ConnectionError, |
| 3633 | ) as e: |
| 3634 | self._format_exception(e) |
| elumalai | 8658c2c | 2022-04-28 19:09:31 +0530 | [diff] [blame] | 3635 | |
| 3636 | def get_vdu_state(self, vm_id): |
| 3637 | """ |
| 3638 | Getting the state of a vdu |
| 3639 | param: |
| 3640 | vm_id: ID of an instance |
| 3641 | """ |
| 3642 | self.logger.debug("Getting the status of VM") |
| 3643 | self.logger.debug("VIM VM ID %s", vm_id) |
| 3644 | self._reload_connection() |
| 3645 | server = self.nova.servers.find(id=vm_id) |
| 3646 | server_dict = server.to_dict() |
| 3647 | vdu_data = [ |
| 3648 | server_dict["status"], |
| 3649 | server_dict["flavor"]["id"], |
| 3650 | server_dict["OS-EXT-SRV-ATTR:host"], |
| 3651 | server_dict["OS-EXT-AZ:availability_zone"], |
| 3652 | ] |
| 3653 | self.logger.debug("vdu_data %s", vdu_data) |
| 3654 | return vdu_data |
| 3655 | |
| 3656 | def check_compute_availability(self, host, server_flavor_details): |
| 3657 | self._reload_connection() |
| 3658 | hypervisor_search = self.nova.hypervisors.search( |
| 3659 | hypervisor_match=host, servers=True |
| 3660 | ) |
| 3661 | for hypervisor in hypervisor_search: |
| 3662 | hypervisor_id = hypervisor.to_dict()["id"] |
| 3663 | hypervisor_details = self.nova.hypervisors.get(hypervisor=hypervisor_id) |
| 3664 | hypervisor_dict = hypervisor_details.to_dict() |
| 3665 | hypervisor_temp = json.dumps(hypervisor_dict) |
| 3666 | hypervisor_json = json.loads(hypervisor_temp) |
| 3667 | resources_available = [ |
| 3668 | hypervisor_json["free_ram_mb"], |
| 3669 | hypervisor_json["disk_available_least"], |
| 3670 | hypervisor_json["vcpus"] - hypervisor_json["vcpus_used"], |
| 3671 | ] |
| 3672 | compute_available = all( |
| 3673 | x > y for x, y in zip(resources_available, server_flavor_details) |
| 3674 | ) |
| 3675 | if compute_available: |
| 3676 | return host |
| 3677 | |
| 3678 | def check_availability_zone( |
| 3679 | self, old_az, server_flavor_details, old_host, host=None |
| 3680 | ): |
| 3681 | self._reload_connection() |
| 3682 | az_check = {"zone_check": False, "compute_availability": None} |
| 3683 | aggregates_list = self.nova.aggregates.list() |
| 3684 | for aggregate in aggregates_list: |
| 3685 | aggregate_details = aggregate.to_dict() |
| 3686 | aggregate_temp = json.dumps(aggregate_details) |
| 3687 | aggregate_json = json.loads(aggregate_temp) |
| 3688 | if aggregate_json["availability_zone"] == old_az: |
| 3689 | hosts_list = aggregate_json["hosts"] |
| 3690 | if host is not None: |
| 3691 | if host in hosts_list: |
| 3692 | az_check["zone_check"] = True |
| 3693 | available_compute_id = self.check_compute_availability( |
| 3694 | host, server_flavor_details |
| 3695 | ) |
| 3696 | if available_compute_id is not None: |
| 3697 | az_check["compute_availability"] = available_compute_id |
| 3698 | else: |
| 3699 | for check_host in hosts_list: |
| 3700 | if check_host != old_host: |
| 3701 | available_compute_id = self.check_compute_availability( |
| 3702 | check_host, server_flavor_details |
| 3703 | ) |
| 3704 | if available_compute_id is not None: |
| 3705 | az_check["zone_check"] = True |
| 3706 | az_check["compute_availability"] = available_compute_id |
| 3707 | break |
| 3708 | else: |
| 3709 | az_check["zone_check"] = True |
| 3710 | return az_check |
| 3711 | |
| 3712 | def migrate_instance(self, vm_id, compute_host=None): |
| 3713 | """ |
| 3714 | Migrate a vdu |
| 3715 | param: |
| 3716 | vm_id: ID of an instance |
| 3717 | compute_host: Host to migrate the vdu to |
| 3718 | """ |
| 3719 | self._reload_connection() |
| 3720 | vm_state = False |
| 3721 | instance_state = self.get_vdu_state(vm_id) |
| 3722 | server_flavor_id = instance_state[1] |
| 3723 | server_hypervisor_name = instance_state[2] |
| 3724 | server_availability_zone = instance_state[3] |
| 3725 | try: |
| 3726 | server_flavor = self.nova.flavors.find(id=server_flavor_id).to_dict() |
| 3727 | server_flavor_details = [ |
| 3728 | server_flavor["ram"], |
| 3729 | server_flavor["disk"], |
| 3730 | server_flavor["vcpus"], |
| 3731 | ] |
| 3732 | if compute_host == server_hypervisor_name: |
| 3733 | raise vimconn.VimConnException( |
| 3734 | "Unable to migrate instance '{}' to the same host '{}'".format( |
| 3735 | vm_id, compute_host |
| 3736 | ), |
| 3737 | http_code=vimconn.HTTP_Bad_Request, |
| 3738 | ) |
| 3739 | az_status = self.check_availability_zone( |
| 3740 | server_availability_zone, |
| 3741 | server_flavor_details, |
| 3742 | server_hypervisor_name, |
| 3743 | compute_host, |
| 3744 | ) |
| 3745 | availability_zone_check = az_status["zone_check"] |
| 3746 | available_compute_id = az_status.get("compute_availability") |
| 3747 | |
| 3748 | if availability_zone_check is False: |
| 3749 | raise vimconn.VimConnException( |
| 3750 | "Unable to migrate instance '{}' to a different availability zone".format( |
| 3751 | vm_id |
| 3752 | ), |
| 3753 | http_code=vimconn.HTTP_Bad_Request, |
| 3754 | ) |
| 3755 | if available_compute_id is not None: |
| 3756 | self.nova.servers.live_migrate( |
| 3757 | server=vm_id, |
| 3758 | host=available_compute_id, |
| 3759 | block_migration=True, |
| 3760 | disk_over_commit=False, |
| 3761 | ) |
| 3762 | state = "MIGRATING" |
| 3763 | changed_compute_host = "" |
| 3764 | if state == "MIGRATING": |
| 3765 | vm_state = self.__wait_for_vm(vm_id, "ACTIVE") |
| 3766 | changed_compute_host = self.get_vdu_state(vm_id)[2] |
| 3767 | if vm_state and changed_compute_host == available_compute_id: |
| 3768 | self.logger.debug( |
| 3769 | "Instance '{}' migrated to the new compute host '{}'".format( |
| 3770 | vm_id, changed_compute_host |
| 3771 | ) |
| 3772 | ) |
| 3773 | return state, available_compute_id |
| 3774 | else: |
| 3775 | raise vimconn.VimConnException( |
| 3776 | "Migration Failed. Instance '{}' not moved to the new host {}".format( |
| 3777 | vm_id, available_compute_id |
| 3778 | ), |
| 3779 | http_code=vimconn.HTTP_Bad_Request, |
| 3780 | ) |
| 3781 | else: |
| 3782 | raise vimconn.VimConnException( |
| 3783 | "Compute '{}' not available or does not have enough resources to migrate the instance".format( |
| 3784 | available_compute_id |
| 3785 | ), |
| 3786 | http_code=vimconn.HTTP_Bad_Request, |
| 3787 | ) |
| 3788 | except ( |
| 3789 | nvExceptions.BadRequest, |
| 3790 | nvExceptions.ClientException, |
| 3791 | nvExceptions.NotFound, |
| 3792 | ) as e: |
| 3793 | self._format_exception(e) |
| sritharan | 29a4c1a | 2022-05-05 12:15:04 +0000 | [diff] [blame] | 3794 | |
| 3795 | def resize_instance(self, vm_id, new_flavor_id): |
| 3796 | """ |
| 3797 | For resizing the vm based on the given |
| 3798 | flavor details |
| 3799 | param: |
| 3800 | vm_id : ID of an instance |
| 3801 | new_flavor_id : Flavor id to be resized |
| 3802 | Return the status of a resized instance |
| 3803 | """ |
| 3804 | self._reload_connection() |
| 3805 | self.logger.debug("resize the flavor of an instance") |
| 3806 | instance_status, old_flavor_id, compute_host, az = self.get_vdu_state(vm_id) |
| 3807 | old_flavor_disk = self.nova.flavors.find(id=old_flavor_id).to_dict()["disk"] |
| 3808 | new_flavor_disk = self.nova.flavors.find(id=new_flavor_id).to_dict()["disk"] |
| 3809 | try: |
| 3810 | if instance_status == "ACTIVE" or instance_status == "SHUTOFF": |
| 3811 | if old_flavor_disk > new_flavor_disk: |
| 3812 | raise nvExceptions.BadRequest( |
| 3813 | 400, |
| 3814 | message="Server disk resize failed. Resize to lower disk flavor is not allowed", |
| 3815 | ) |
| 3816 | else: |
| 3817 | self.nova.servers.resize(server=vm_id, flavor=new_flavor_id) |
| 3818 | vm_state = self.__wait_for_vm(vm_id, "VERIFY_RESIZE") |
| 3819 | if vm_state: |
| 3820 | instance_resized_status = self.confirm_resize(vm_id) |
| 3821 | return instance_resized_status |
| 3822 | else: |
| 3823 | raise nvExceptions.BadRequest( |
| 3824 | 409, |
| 3825 | message="Cannot 'resize' vm_state is in ERROR", |
| 3826 | ) |
| 3827 | |
| 3828 | else: |
| 3829 | self.logger.debug("ERROR : Instance is not in ACTIVE or SHUTOFF state") |
| 3830 | raise nvExceptions.BadRequest( |
| 3831 | 409, |
| 3832 | message="Cannot 'resize' instance while it is in vm_state resized", |
| 3833 | ) |
| 3834 | except ( |
| 3835 | nvExceptions.BadRequest, |
| 3836 | nvExceptions.ClientException, |
| 3837 | nvExceptions.NotFound, |
| 3838 | ) as e: |
| 3839 | self._format_exception(e) |
| 3840 | |
| 3841 | def confirm_resize(self, vm_id): |
| 3842 | """ |
| 3843 | Confirm the resize of an instance |
| 3844 | param: |
| 3845 | vm_id: ID of an instance |
| 3846 | """ |
| 3847 | self._reload_connection() |
| 3848 | self.nova.servers.confirm_resize(server=vm_id) |
| 3849 | if self.get_vdu_state(vm_id)[0] == "VERIFY_RESIZE": |
| 3850 | self.__wait_for_vm(vm_id, "ACTIVE") |
| 3851 | instance_status = self.get_vdu_state(vm_id)[0] |
| 3852 | return instance_status |