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