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