| 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. |
| 19 | # |
| 20 | # For those usages not covered by the Apache License, Version 2.0 please |
| 21 | # contact with: nfvlabs@tid.es |
| 22 | ## |
| 23 | |
| 24 | ''' |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 25 | osconnector implements all the methods to interact with openstack using the python-neutronclient. |
| 26 | |
| 27 | For the VNF forwarding graph, The OpenStack VIM connector calls the |
| 28 | networking-sfc Neutron extension methods, whose resources are mapped |
| 29 | to the VIM connector's SFC resources as follows: |
| 30 | - Classification (OSM) -> Flow Classifier (Neutron) |
| 31 | - Service Function Instance (OSM) -> Port Pair (Neutron) |
| 32 | - Service Function (OSM) -> Port Pair Group (Neutron) |
| 33 | - Service Function Path (OSM) -> Port Chain (Neutron) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 34 | ''' |
| Eduardo Sousa | e3c0dbc | 2018-09-03 11:56:07 +0100 | [diff] [blame] | 35 | __author__ = "Alfonso Tierno, Gerardo Garcia, Pablo Montes, xFlow Research, Igor D.C., Eduardo Sousa" |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 36 | __date__ = "$22-sep-2017 23:59:59$" |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 37 | |
| 38 | import vimconn |
| tierno | 69b590e | 2018-03-13 18:52:23 +0100 | [diff] [blame] | 39 | # import json |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 40 | import logging |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 41 | import netaddr |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 42 | import time |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 43 | import yaml |
| garciadeblas | 2299e3b | 2017-01-26 14:35:55 +0000 | [diff] [blame] | 44 | import random |
| kate | 721d79b | 2017-06-24 04:21:38 -0700 | [diff] [blame] | 45 | import re |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 46 | import copy |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 47 | from pprint import pformat |
| 48 | from types import StringTypes |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 49 | |
| tierno | b5cef37 | 2017-06-19 15:52:22 +0200 | [diff] [blame] | 50 | from novaclient import client as nClient, exceptions as nvExceptions |
| 51 | from keystoneauth1.identity import v2, v3 |
| 52 | from keystoneauth1 import session |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 53 | import keystoneclient.exceptions as ksExceptions |
| tierno | f716aea | 2017-06-21 18:01:40 +0200 | [diff] [blame] | 54 | import keystoneclient.v3.client as ksClient_v3 |
| 55 | import keystoneclient.v2_0.client as ksClient_v2 |
| tierno | b5cef37 | 2017-06-19 15:52:22 +0200 | [diff] [blame] | 56 | from glanceclient import client as glClient |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 57 | import glanceclient.exc as gl1Exceptions |
| tierno | b5cef37 | 2017-06-19 15:52:22 +0200 | [diff] [blame] | 58 | from cinderclient import client as cClient |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 59 | from httplib import HTTPException |
| tierno | b5cef37 | 2017-06-19 15:52:22 +0200 | [diff] [blame] | 60 | from neutronclient.neutron import client as neClient |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 61 | from neutronclient.common import exceptions as neExceptions |
| 62 | from requests.exceptions import ConnectionError |
| 63 | |
| tierno | 40e1bce | 2017-08-09 09:12:04 +0200 | [diff] [blame] | 64 | |
| 65 | """contain the openstack virtual machine status to openmano status""" |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 66 | vmStatus2manoFormat={'ACTIVE':'ACTIVE', |
| 67 | 'PAUSED':'PAUSED', |
| 68 | 'SUSPENDED': 'SUSPENDED', |
| 69 | 'SHUTOFF':'INACTIVE', |
| 70 | 'BUILD':'BUILD', |
| 71 | 'ERROR':'ERROR','DELETED':'DELETED' |
| 72 | } |
| 73 | netStatus2manoFormat={'ACTIVE':'ACTIVE','PAUSED':'PAUSED','INACTIVE':'INACTIVE','BUILD':'BUILD','ERROR':'ERROR','DELETED':'DELETED' |
| 74 | } |
| 75 | |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 76 | supportedClassificationTypes = ['legacy_flow_classifier'] |
| 77 | |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 78 | #global var to have a timeout creating and deleting volumes |
| tierno | 00e3df7 | 2017-11-29 17:20:13 +0100 | [diff] [blame] | 79 | volume_timeout = 600 |
| 80 | server_timeout = 600 |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 81 | |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 82 | |
| 83 | class SafeDumper(yaml.SafeDumper): |
| 84 | def represent_data(self, data): |
| 85 | # Openstack APIs use custom subclasses of dict and YAML safe dumper |
| 86 | # is designed to not handle that (reference issue 142 of pyyaml) |
| 87 | if isinstance(data, dict) and data.__class__ != dict: |
| 88 | # A simple solution is to convert those items back to dicts |
| 89 | data = dict(data.items()) |
| 90 | |
| 91 | return super(SafeDumper, self).represent_data(data) |
| 92 | |
| 93 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 94 | class vimconnector(vimconn.vimconnector): |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 95 | def __init__(self, uuid, name, tenant_id, tenant_name, url, url_admin=None, user=None, passwd=None, |
| 96 | log_level=None, config={}, persistent_info={}): |
| ahmadsa | 96af9f4 | 2017-01-31 16:17:14 +0500 | [diff] [blame] | 97 | '''using common constructor parameters. In this case |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 98 | 'url' is the keystone authorization url, |
| 99 | 'url_admin' is not use |
| 100 | ''' |
| tierno | f716aea | 2017-06-21 18:01:40 +0200 | [diff] [blame] | 101 | api_version = config.get('APIversion') |
| 102 | if api_version and api_version not in ('v3.3', 'v2.0', '2', '3'): |
| tierno | b5cef37 | 2017-06-19 15:52:22 +0200 | [diff] [blame] | 103 | raise vimconn.vimconnException("Invalid value '{}' for config:APIversion. " |
| tierno | f716aea | 2017-06-21 18:01:40 +0200 | [diff] [blame] | 104 | "Allowed values are 'v3.3', 'v2.0', '2' or '3'".format(api_version)) |
| kate | 721d79b | 2017-06-24 04:21:38 -0700 | [diff] [blame] | 105 | vim_type = config.get('vim_type') |
| 106 | if vim_type and vim_type not in ('vio', 'VIO'): |
| 107 | raise vimconn.vimconnException("Invalid value '{}' for config:vim_type." |
| 108 | "Allowed values are 'vio' or 'VIO'".format(vim_type)) |
| 109 | |
| 110 | if config.get('dataplane_net_vlan_range') is not None: |
| 111 | #validate vlan ranges provided by user |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame^] | 112 | self._validate_vlan_ranges(config.get('dataplane_net_vlan_range'), 'dataplane_net_vlan_range') |
| 113 | |
| 114 | if config.get('multisegment_vlan_range') is not None: |
| 115 | #validate vlan ranges provided by user |
| 116 | self._validate_vlan_ranges(config.get('multisegment_vlan_range'), 'multisegment_vlan_range') |
| kate | 721d79b | 2017-06-24 04:21:38 -0700 | [diff] [blame] | 117 | |
| tierno | b5cef37 | 2017-06-19 15:52:22 +0200 | [diff] [blame] | 118 | vimconn.vimconnector.__init__(self, uuid, name, tenant_id, tenant_name, url, url_admin, user, passwd, log_level, |
| 119 | config) |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 120 | |
| tierno | 4d1ce22 | 2018-04-06 10:41:06 +0200 | [diff] [blame] | 121 | if self.config.get("insecure") and self.config.get("ca_cert"): |
| 122 | raise vimconn.vimconnException("options insecure and ca_cert are mutually exclusive") |
| 123 | self.verify = True |
| 124 | if self.config.get("insecure"): |
| 125 | self.verify = False |
| 126 | if self.config.get("ca_cert"): |
| 127 | self.verify = self.config.get("ca_cert") |
| tierno | 4d1ce22 | 2018-04-06 10:41:06 +0200 | [diff] [blame] | 128 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 129 | if not url: |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 130 | raise TypeError('url param can not be NoneType') |
| tierno | b5cef37 | 2017-06-19 15:52:22 +0200 | [diff] [blame] | 131 | self.persistent_info = persistent_info |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 132 | self.availability_zone = persistent_info.get('availability_zone', None) |
| tierno | b5cef37 | 2017-06-19 15:52:22 +0200 | [diff] [blame] | 133 | self.session = persistent_info.get('session', {'reload_client': True}) |
| tierno | a05b65a | 2019-02-01 12:30:27 +0000 | [diff] [blame] | 134 | self.my_tenant_id = self.session.get('my_tenant_id') |
| tierno | b5cef37 | 2017-06-19 15:52:22 +0200 | [diff] [blame] | 135 | self.nova = self.session.get('nova') |
| 136 | self.neutron = self.session.get('neutron') |
| 137 | self.cinder = self.session.get('cinder') |
| 138 | self.glance = self.session.get('glance') |
| tierno | 1beea86 | 2018-07-11 15:47:37 +0200 | [diff] [blame] | 139 | # self.glancev1 = self.session.get('glancev1') |
| tierno | f716aea | 2017-06-21 18:01:40 +0200 | [diff] [blame] | 140 | self.keystone = self.session.get('keystone') |
| 141 | self.api_version3 = self.session.get('api_version3') |
| kate | 721d79b | 2017-06-24 04:21:38 -0700 | [diff] [blame] | 142 | self.vim_type = self.config.get("vim_type") |
| 143 | if self.vim_type: |
| 144 | self.vim_type = self.vim_type.upper() |
| 145 | if self.config.get("use_internal_endpoint"): |
| 146 | self.endpoint_type = "internalURL" |
| 147 | else: |
| 148 | self.endpoint_type = None |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 149 | |
| tierno | 73ad9e4 | 2016-09-12 18:11:11 +0200 | [diff] [blame] | 150 | self.logger = logging.getLogger('openmano.vim.openstack') |
| kate | 721d79b | 2017-06-24 04:21:38 -0700 | [diff] [blame] | 151 | |
| tierno | a05b65a | 2019-02-01 12:30:27 +0000 | [diff] [blame] | 152 | # allow security_groups to be a list or a single string |
| 153 | if isinstance(self.config.get('security_groups'), str): |
| 154 | self.config['security_groups'] = [self.config['security_groups']] |
| 155 | self.security_groups_id = None |
| 156 | |
| kate | 721d79b | 2017-06-24 04:21:38 -0700 | [diff] [blame] | 157 | ####### VIO Specific Changes ######### |
| 158 | if self.vim_type == "VIO": |
| 159 | self.logger = logging.getLogger('openmano.vim.vio') |
| 160 | |
| tierno | fe78990 | 2016-09-29 14:20:44 +0000 | [diff] [blame] | 161 | if log_level: |
| kate | 5461675 | 2017-09-05 23:26:28 -0700 | [diff] [blame] | 162 | self.logger.setLevel( getattr(logging, log_level)) |
| tierno | f716aea | 2017-06-21 18:01:40 +0200 | [diff] [blame] | 163 | |
| 164 | def __getitem__(self, index): |
| 165 | """Get individuals parameters. |
| 166 | Throw KeyError""" |
| 167 | if index == 'project_domain_id': |
| 168 | return self.config.get("project_domain_id") |
| 169 | elif index == 'user_domain_id': |
| 170 | return self.config.get("user_domain_id") |
| 171 | else: |
| tierno | 76a3c31 | 2017-06-29 16:42:15 +0200 | [diff] [blame] | 172 | return vimconn.vimconnector.__getitem__(self, index) |
| tierno | f716aea | 2017-06-21 18:01:40 +0200 | [diff] [blame] | 173 | |
| 174 | def __setitem__(self, index, value): |
| 175 | """Set individuals parameters and it is marked as dirty so to force connection reload. |
| 176 | Throw KeyError""" |
| 177 | if index == 'project_domain_id': |
| 178 | self.config["project_domain_id"] = value |
| 179 | elif index == 'user_domain_id': |
| 180 | self.config["user_domain_id"] = value |
| 181 | else: |
| 182 | vimconn.vimconnector.__setitem__(self, index, value) |
| tierno | b5cef37 | 2017-06-19 15:52:22 +0200 | [diff] [blame] | 183 | self.session['reload_client'] = True |
| tierno | f716aea | 2017-06-21 18:01:40 +0200 | [diff] [blame] | 184 | |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 185 | def serialize(self, value): |
| 186 | """Serialization of python basic types. |
| 187 | |
| 188 | In the case value is not serializable a message will be logged and a |
| 189 | simple representation of the data that cannot be converted back to |
| 190 | python is returned. |
| 191 | """ |
| 192 | if isinstance(value, StringTypes): |
| 193 | return value |
| 194 | |
| 195 | try: |
| 196 | return yaml.dump(value, Dumper=SafeDumper, |
| 197 | default_flow_style=True, width=256) |
| 198 | except yaml.representer.RepresenterError: |
| 199 | self.logger.debug( |
| 200 | 'The following entity cannot be serialized in YAML:' |
| 201 | '\n\n%s\n\n', pformat(value), exc_info=True) |
| 202 | return str(value) |
| 203 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 204 | def _reload_connection(self): |
| 205 | '''Called before any operation, it check if credentials has changed |
| 206 | Throw keystoneclient.apiclient.exceptions.AuthorizationFailure |
| 207 | ''' |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 208 | #TODO control the timing and possible token timeout, but it seams that python client does this task for us :-) |
| tierno | b5cef37 | 2017-06-19 15:52:22 +0200 | [diff] [blame] | 209 | if self.session['reload_client']: |
| tierno | f716aea | 2017-06-21 18:01:40 +0200 | [diff] [blame] | 210 | if self.config.get('APIversion'): |
| 211 | self.api_version3 = self.config['APIversion'] == 'v3.3' or self.config['APIversion'] == '3' |
| 212 | else: # get from ending auth_url that end with v3 or with v2.0 |
| tierno | 3cb8dc3 | 2017-10-24 18:13:19 +0200 | [diff] [blame] | 213 | self.api_version3 = self.url.endswith("/v3") or self.url.endswith("/v3/") |
| tierno | f716aea | 2017-06-21 18:01:40 +0200 | [diff] [blame] | 214 | self.session['api_version3'] = self.api_version3 |
| 215 | if self.api_version3: |
| tierno | 3cb8dc3 | 2017-10-24 18:13:19 +0200 | [diff] [blame] | 216 | if self.config.get('project_domain_id') or self.config.get('project_domain_name'): |
| 217 | project_domain_id_default = None |
| 218 | else: |
| 219 | project_domain_id_default = 'default' |
| 220 | if self.config.get('user_domain_id') or self.config.get('user_domain_name'): |
| 221 | user_domain_id_default = None |
| 222 | else: |
| 223 | user_domain_id_default = 'default' |
| tierno | f716aea | 2017-06-21 18:01:40 +0200 | [diff] [blame] | 224 | auth = v3.Password(auth_url=self.url, |
| tierno | b5cef37 | 2017-06-19 15:52:22 +0200 | [diff] [blame] | 225 | username=self.user, |
| 226 | password=self.passwd, |
| 227 | project_name=self.tenant_name, |
| 228 | project_id=self.tenant_id, |
| tierno | 3cb8dc3 | 2017-10-24 18:13:19 +0200 | [diff] [blame] | 229 | project_domain_id=self.config.get('project_domain_id', project_domain_id_default), |
| 230 | user_domain_id=self.config.get('user_domain_id', user_domain_id_default), |
| 231 | project_domain_name=self.config.get('project_domain_name'), |
| 232 | user_domain_name=self.config.get('user_domain_name')) |
| ahmadsa | 95baa27 | 2016-11-30 09:14:11 +0500 | [diff] [blame] | 233 | else: |
| tierno | f716aea | 2017-06-21 18:01:40 +0200 | [diff] [blame] | 234 | auth = v2.Password(auth_url=self.url, |
| tierno | b5cef37 | 2017-06-19 15:52:22 +0200 | [diff] [blame] | 235 | username=self.user, |
| 236 | password=self.passwd, |
| 237 | tenant_name=self.tenant_name, |
| 238 | tenant_id=self.tenant_id) |
| tierno | 4d1ce22 | 2018-04-06 10:41:06 +0200 | [diff] [blame] | 239 | sess = session.Session(auth=auth, verify=self.verify) |
| fatollahy | 40c6a3f | 2019-02-19 12:53:40 +0000 | [diff] [blame] | 240 | # addedd region_name to keystone, nova, neutron and cinder to support distributed cloud for Wind River Titanium cloud and StarlingX |
| 241 | region_name = self.config.get('region_name') |
| tierno | f716aea | 2017-06-21 18:01:40 +0200 | [diff] [blame] | 242 | if self.api_version3: |
| fatollahy | 40c6a3f | 2019-02-19 12:53:40 +0000 | [diff] [blame] | 243 | self.keystone = ksClient_v3.Client(session=sess, endpoint_type=self.endpoint_type, region_name=region_name) |
| tierno | f716aea | 2017-06-21 18:01:40 +0200 | [diff] [blame] | 244 | else: |
| kate | 721d79b | 2017-06-24 04:21:38 -0700 | [diff] [blame] | 245 | self.keystone = ksClient_v2.Client(session=sess, endpoint_type=self.endpoint_type) |
| tierno | f716aea | 2017-06-21 18:01:40 +0200 | [diff] [blame] | 246 | self.session['keystone'] = self.keystone |
| montesmoreno | 9317d30 | 2017-08-16 12:48:23 +0200 | [diff] [blame] | 247 | # In order to enable microversion functionality an explicit microversion must be specified in 'config'. |
| 248 | # This implementation approach is due to the warning message in |
| 249 | # https://developer.openstack.org/api-guide/compute/microversions.html |
| 250 | # where it is stated that microversion backwards compatibility is not guaranteed and clients should |
| 251 | # always require an specific microversion. |
| 252 | # To be able to use 'device role tagging' functionality define 'microversion: 2.32' in datacenter config |
| 253 | version = self.config.get("microversion") |
| 254 | if not version: |
| 255 | version = "2.1" |
| fatollahy | 40c6a3f | 2019-02-19 12:53:40 +0000 | [diff] [blame] | 256 | # addedd region_name to keystone, nova, neutron and cinder to support distributed cloud for Wind River Titanium cloud and StarlingX |
| 257 | self.nova = self.session['nova'] = nClient.Client(str(version), session=sess, endpoint_type=self.endpoint_type, region_name=region_name) |
| 258 | self.neutron = self.session['neutron'] = neClient.Client('2.0', session=sess, endpoint_type=self.endpoint_type, region_name=region_name) |
| 259 | self.cinder = self.session['cinder'] = cClient.Client(2, session=sess, endpoint_type=self.endpoint_type, region_name=region_name) |
| tierno | a05b65a | 2019-02-01 12:30:27 +0000 | [diff] [blame] | 260 | try: |
| 261 | self.my_tenant_id = self.session['my_tenant_id'] = sess.get_project_id() |
| 262 | except Exception as e: |
| 263 | self.logger.error("Cannot get project_id from session", exc_info=True) |
| kate | 721d79b | 2017-06-24 04:21:38 -0700 | [diff] [blame] | 264 | if self.endpoint_type == "internalURL": |
| 265 | glance_service_id = self.keystone.services.list(name="glance")[0].id |
| 266 | glance_endpoint = self.keystone.endpoints.list(glance_service_id, interface="internal")[0].url |
| 267 | else: |
| 268 | glance_endpoint = None |
| 269 | self.glance = self.session['glance'] = glClient.Client(2, session=sess, endpoint=glance_endpoint) |
| tierno | a05b65a | 2019-02-01 12:30:27 +0000 | [diff] [blame] | 270 | # using version 1 of glance client in new_image() |
| tierno | 1beea86 | 2018-07-11 15:47:37 +0200 | [diff] [blame] | 271 | # self.glancev1 = self.session['glancev1'] = glClient.Client('1', session=sess, |
| 272 | # endpoint=glance_endpoint) |
| tierno | b5cef37 | 2017-06-19 15:52:22 +0200 | [diff] [blame] | 273 | self.session['reload_client'] = False |
| 274 | self.persistent_info['session'] = self.session |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 275 | # add availablity zone info inside self.persistent_info |
| 276 | self._set_availablity_zones() |
| 277 | self.persistent_info['availability_zone'] = self.availability_zone |
| tierno | a05b65a | 2019-02-01 12:30:27 +0000 | [diff] [blame] | 278 | self.security_groups_id = None # force to get again security_groups_ids next time they are needed |
| ahmadsa | 95baa27 | 2016-11-30 09:14:11 +0500 | [diff] [blame] | 279 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 280 | def __net_os2mano(self, net_list_dict): |
| 281 | '''Transform the net openstack format to mano format |
| 282 | net_list_dict can be a list of dict or a single dict''' |
| 283 | if type(net_list_dict) is dict: |
| 284 | net_list_=(net_list_dict,) |
| 285 | elif type(net_list_dict) is list: |
| 286 | net_list_=net_list_dict |
| 287 | else: |
| 288 | raise TypeError("param net_list_dict must be a list or a dictionary") |
| 289 | for net in net_list_: |
| 290 | if net.get('provider:network_type') == "vlan": |
| 291 | net['type']='data' |
| 292 | else: |
| 293 | net['type']='bridge' |
| Pablo Montes Moreno | 6a7785b | 2017-07-03 10:44:30 +0200 | [diff] [blame] | 294 | |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 295 | def __classification_os2mano(self, class_list_dict): |
| 296 | """Transform the openstack format (Flow Classifier) to mano format |
| 297 | (Classification) class_list_dict can be a list of dict or a single dict |
| 298 | """ |
| 299 | if isinstance(class_list_dict, dict): |
| 300 | class_list_ = [class_list_dict] |
| 301 | elif isinstance(class_list_dict, list): |
| 302 | class_list_ = class_list_dict |
| 303 | else: |
| 304 | raise TypeError( |
| 305 | "param class_list_dict must be a list or a dictionary") |
| 306 | for classification in class_list_: |
| 307 | id = classification.pop('id') |
| 308 | name = classification.pop('name') |
| 309 | description = classification.pop('description') |
| 310 | project_id = classification.pop('project_id') |
| 311 | tenant_id = classification.pop('tenant_id') |
| 312 | original_classification = copy.deepcopy(classification) |
| 313 | classification.clear() |
| 314 | classification['ctype'] = 'legacy_flow_classifier' |
| 315 | classification['definition'] = original_classification |
| 316 | classification['id'] = id |
| 317 | classification['name'] = name |
| 318 | classification['description'] = description |
| 319 | classification['project_id'] = project_id |
| 320 | classification['tenant_id'] = tenant_id |
| 321 | |
| 322 | def __sfi_os2mano(self, sfi_list_dict): |
| 323 | """Transform the openstack format (Port Pair) to mano format (SFI) |
| 324 | sfi_list_dict can be a list of dict or a single dict |
| 325 | """ |
| 326 | if isinstance(sfi_list_dict, dict): |
| 327 | sfi_list_ = [sfi_list_dict] |
| 328 | elif isinstance(sfi_list_dict, list): |
| 329 | sfi_list_ = sfi_list_dict |
| 330 | else: |
| 331 | raise TypeError( |
| 332 | "param sfi_list_dict must be a list or a dictionary") |
| 333 | for sfi in sfi_list_: |
| 334 | sfi['ingress_ports'] = [] |
| 335 | sfi['egress_ports'] = [] |
| 336 | if sfi.get('ingress'): |
| 337 | sfi['ingress_ports'].append(sfi['ingress']) |
| 338 | if sfi.get('egress'): |
| 339 | sfi['egress_ports'].append(sfi['egress']) |
| 340 | del sfi['ingress'] |
| 341 | del sfi['egress'] |
| 342 | params = sfi.get('service_function_parameters') |
| 343 | sfc_encap = False |
| 344 | if params: |
| 345 | correlation = params.get('correlation') |
| 346 | if correlation: |
| 347 | sfc_encap = True |
| 348 | sfi['sfc_encap'] = sfc_encap |
| 349 | del sfi['service_function_parameters'] |
| 350 | |
| 351 | def __sf_os2mano(self, sf_list_dict): |
| 352 | """Transform the openstack format (Port Pair Group) to mano format (SF) |
| 353 | sf_list_dict can be a list of dict or a single dict |
| 354 | """ |
| 355 | if isinstance(sf_list_dict, dict): |
| 356 | sf_list_ = [sf_list_dict] |
| 357 | elif isinstance(sf_list_dict, list): |
| 358 | sf_list_ = sf_list_dict |
| 359 | else: |
| 360 | raise TypeError( |
| 361 | "param sf_list_dict must be a list or a dictionary") |
| 362 | for sf in sf_list_: |
| 363 | del sf['port_pair_group_parameters'] |
| 364 | sf['sfis'] = sf['port_pairs'] |
| 365 | del sf['port_pairs'] |
| 366 | |
| 367 | def __sfp_os2mano(self, sfp_list_dict): |
| 368 | """Transform the openstack format (Port Chain) to mano format (SFP) |
| 369 | sfp_list_dict can be a list of dict or a single dict |
| 370 | """ |
| 371 | if isinstance(sfp_list_dict, dict): |
| 372 | sfp_list_ = [sfp_list_dict] |
| 373 | elif isinstance(sfp_list_dict, list): |
| 374 | sfp_list_ = sfp_list_dict |
| 375 | else: |
| 376 | raise TypeError( |
| 377 | "param sfp_list_dict must be a list or a dictionary") |
| 378 | for sfp in sfp_list_: |
| 379 | params = sfp.pop('chain_parameters') |
| 380 | sfc_encap = False |
| 381 | if params: |
| 382 | correlation = params.get('correlation') |
| 383 | if correlation: |
| 384 | sfc_encap = True |
| 385 | sfp['sfc_encap'] = sfc_encap |
| 386 | sfp['spi'] = sfp.pop('chain_id') |
| 387 | sfp['classifications'] = sfp.pop('flow_classifiers') |
| 388 | sfp['service_functions'] = sfp.pop('port_pair_groups') |
| 389 | |
| 390 | # placeholder for now; read TODO note below |
| 391 | def _validate_classification(self, type, definition): |
| 392 | # only legacy_flow_classifier Type is supported at this point |
| 393 | return True |
| 394 | # TODO(igordcard): this method should be an abstract method of an |
| 395 | # abstract Classification class to be implemented by the specific |
| 396 | # Types. Also, abstract vimconnector should call the validation |
| 397 | # method before the implemented VIM connectors are called. |
| 398 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 399 | def _format_exception(self, exception): |
| 400 | '''Transform a keystone, nova, neutron exception into a vimconn exception''' |
| shashankjain | 3c83a21 | 2018-10-04 13:05:46 +0530 | [diff] [blame] | 401 | if isinstance(exception, (neExceptions.NetworkNotFoundClient, nvExceptions.NotFound, ksExceptions.NotFound, gl1Exceptions.HTTPNotFound)): |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 402 | raise vimconn.vimconnNotFoundException(type(exception).__name__ + ": " + str(exception)) |
| shashankjain | 3c83a21 | 2018-10-04 13:05:46 +0530 | [diff] [blame] | 403 | elif isinstance(exception, (HTTPException, gl1Exceptions.HTTPException, gl1Exceptions.CommunicationError, |
| 404 | ConnectionError, ksExceptions.ConnectionError, neExceptions.ConnectionFailed)): |
| 405 | raise vimconn.vimconnConnectionException(type(exception).__name__ + ": " + str(exception)) |
| 406 | elif isinstance(exception, (KeyError, nvExceptions.BadRequest, ksExceptions.BadRequest)): |
| anwars | c76a3ee | 2018-10-04 14:05:32 +0530 | [diff] [blame] | 407 | raise vimconn.vimconnException(type(exception).__name__ + ": " + str(exception)) |
| 408 | elif isinstance(exception, (nvExceptions.ClientException, ksExceptions.ClientException, |
| 409 | neExceptions.NeutronException)): |
| 410 | raise vimconn.vimconnUnexpectedResponse(type(exception).__name__ + ": " + str(exception)) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 411 | elif isinstance(exception, nvExceptions.Conflict): |
| 412 | raise vimconn.vimconnConflictException(type(exception).__name__ + ": " + str(exception)) |
| Pablo Montes Moreno | 6a7785b | 2017-07-03 10:44:30 +0200 | [diff] [blame] | 413 | elif isinstance(exception, vimconn.vimconnException): |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 414 | raise exception |
| tierno | f716aea | 2017-06-21 18:01:40 +0200 | [diff] [blame] | 415 | else: # () |
| tierno | b84cbdc | 2017-07-07 14:30:30 +0200 | [diff] [blame] | 416 | self.logger.error("General Exception " + str(exception), exc_info=True) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 417 | raise vimconn.vimconnConnectionException(type(exception).__name__ + ": " + str(exception)) |
| 418 | |
| tierno | a05b65a | 2019-02-01 12:30:27 +0000 | [diff] [blame] | 419 | def _get_ids_from_name(self): |
| 420 | """ |
| 421 | Obtain ids from name of tenant and security_groups. Store at self .security_groups_id" |
| 422 | :return: None |
| 423 | """ |
| 424 | # get tenant_id if only tenant_name is supplied |
| 425 | self._reload_connection() |
| 426 | if not self.my_tenant_id: |
| 427 | raise vimconn.vimconnConnectionException("Error getting tenant information from name={} id={}". |
| 428 | format(self.tenant_name, self.tenant_id)) |
| 429 | if self.config.get('security_groups') and not self.security_groups_id: |
| 430 | # convert from name to id |
| 431 | neutron_sg_list = self.neutron.list_security_groups(tenant_id=self.my_tenant_id)["security_groups"] |
| 432 | |
| 433 | self.security_groups_id = [] |
| 434 | for sg in self.config.get('security_groups'): |
| 435 | for neutron_sg in neutron_sg_list: |
| 436 | if sg in (neutron_sg["id"], neutron_sg["name"]): |
| 437 | self.security_groups_id.append(neutron_sg["id"]) |
| 438 | break |
| 439 | else: |
| 440 | self.security_groups_id = None |
| 441 | raise vimconn.vimconnConnectionException("Not found security group {} for this tenant".format(sg)) |
| 442 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 443 | def get_tenant_list(self, filter_dict={}): |
| 444 | '''Obtain tenants of VIM |
| 445 | filter_dict can contain the following keys: |
| 446 | name: filter by tenant name |
| 447 | id: filter by tenant uuid/id |
| 448 | <other VIM specific> |
| 449 | Returns the tenant list of dictionaries: [{'name':'<name>, 'id':'<id>, ...}, ...] |
| 450 | ''' |
| ahmadsa | 95baa27 | 2016-11-30 09:14:11 +0500 | [diff] [blame] | 451 | self.logger.debug("Getting tenants from VIM filter: '%s'", str(filter_dict)) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 452 | try: |
| 453 | self._reload_connection() |
| tierno | f716aea | 2017-06-21 18:01:40 +0200 | [diff] [blame] | 454 | if self.api_version3: |
| 455 | project_class_list = self.keystone.projects.list(name=filter_dict.get("name")) |
| ahmadsa | 95baa27 | 2016-11-30 09:14:11 +0500 | [diff] [blame] | 456 | else: |
| tierno | f716aea | 2017-06-21 18:01:40 +0200 | [diff] [blame] | 457 | project_class_list = self.keystone.tenants.findall(**filter_dict) |
| ahmadsa | 95baa27 | 2016-11-30 09:14:11 +0500 | [diff] [blame] | 458 | project_list=[] |
| 459 | for project in project_class_list: |
| tierno | f716aea | 2017-06-21 18:01:40 +0200 | [diff] [blame] | 460 | if filter_dict.get('id') and filter_dict["id"] != project.id: |
| 461 | continue |
| ahmadsa | 95baa27 | 2016-11-30 09:14:11 +0500 | [diff] [blame] | 462 | project_list.append(project.to_dict()) |
| 463 | return project_list |
| tierno | f716aea | 2017-06-21 18:01:40 +0200 | [diff] [blame] | 464 | except (ksExceptions.ConnectionError, ksExceptions.ClientException, ConnectionError) as e: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 465 | self._format_exception(e) |
| 466 | |
| 467 | def new_tenant(self, tenant_name, tenant_description): |
| 468 | '''Adds a new tenant to openstack VIM. Returns the tenant identifier''' |
| 469 | self.logger.debug("Adding a new tenant name: %s", tenant_name) |
| 470 | try: |
| 471 | self._reload_connection() |
| tierno | f716aea | 2017-06-21 18:01:40 +0200 | [diff] [blame] | 472 | if self.api_version3: |
| 473 | project = self.keystone.projects.create(tenant_name, self.config.get("project_domain_id", "default"), |
| 474 | description=tenant_description, is_domain=False) |
| ahmadsa | 95baa27 | 2016-11-30 09:14:11 +0500 | [diff] [blame] | 475 | else: |
| tierno | f716aea | 2017-06-21 18:01:40 +0200 | [diff] [blame] | 476 | project = self.keystone.tenants.create(tenant_name, tenant_description) |
| ahmadsa | 95baa27 | 2016-11-30 09:14:11 +0500 | [diff] [blame] | 477 | return project.id |
| shashankjain | 3c83a21 | 2018-10-04 13:05:46 +0530 | [diff] [blame] | 478 | except (ksExceptions.ConnectionError, ksExceptions.ClientException, ksExceptions.BadRequest, ConnectionError) as e: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 479 | self._format_exception(e) |
| 480 | |
| 481 | def delete_tenant(self, tenant_id): |
| 482 | '''Delete a tenant from openstack VIM. Returns the old tenant identifier''' |
| 483 | self.logger.debug("Deleting tenant %s from VIM", tenant_id) |
| 484 | try: |
| 485 | self._reload_connection() |
| tierno | f716aea | 2017-06-21 18:01:40 +0200 | [diff] [blame] | 486 | if self.api_version3: |
| ahmadsa | 95baa27 | 2016-11-30 09:14:11 +0500 | [diff] [blame] | 487 | self.keystone.projects.delete(tenant_id) |
| 488 | else: |
| 489 | self.keystone.tenants.delete(tenant_id) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 490 | return tenant_id |
| shashankjain | 3c83a21 | 2018-10-04 13:05:46 +0530 | [diff] [blame] | 491 | except (ksExceptions.ConnectionError, ksExceptions.ClientException, ksExceptions.NotFound, ConnectionError) as e: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 492 | self._format_exception(e) |
| ahmadsa | 95baa27 | 2016-11-30 09:14:11 +0500 | [diff] [blame] | 493 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 494 | def new_network(self,net_name, net_type, ip_profile=None, shared=False, vlan=None): |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame^] | 495 | """Adds a tenant network to VIM |
| 496 | Params: |
| 497 | 'net_name': name of the network |
| 498 | 'net_type': one of: |
| 499 | 'bridge': overlay isolated network |
| 500 | 'data': underlay E-LAN network for Passthrough and SRIOV interfaces |
| 501 | 'ptp': underlay E-LINE network for Passthrough and SRIOV interfaces. |
| 502 | 'ip_profile': is a dict containing the IP parameters of the network |
| 503 | 'ip_version': can be "IPv4" or "IPv6" (Currently only IPv4 is implemented) |
| 504 | 'subnet_address': ip_prefix_schema, that is X.X.X.X/Y |
| 505 | 'gateway_address': (Optional) ip_schema, that is X.X.X.X |
| 506 | 'dns_address': (Optional) comma separated list of ip_schema, e.g. X.X.X.X[,X,X,X,X] |
| 507 | 'dhcp_enabled': True or False |
| 508 | 'dhcp_start_address': ip_schema, first IP to grant |
| 509 | 'dhcp_count': number of IPs to grant. |
| 510 | 'shared': if this network can be seen/use by other tenants/organization |
| 511 | 'vlan': in case of a data or ptp net_type, the intended vlan tag to be used for the network |
| 512 | Returns a tuple with the network identifier and created_items, or raises an exception on error |
| 513 | created_items can be None or a dictionary where this method can include key-values that will be passed to |
| 514 | the method delete_network. Can be used to store created segments, created l2gw connections, etc. |
| 515 | Format is vimconnector dependent, but do not use nested dictionaries and a value of None should be the same |
| 516 | as not present. |
| 517 | """ |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 518 | self.logger.debug("Adding a new network to VIM name '%s', type '%s'", net_name, net_type) |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame^] | 519 | # self.logger.debug(">>>>>>>>>>>>>>>>>> IP profile %s", str(ip_profile)) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 520 | try: |
| garciadeblas | edca7b3 | 2016-09-29 14:01:52 +0000 | [diff] [blame] | 521 | new_net = None |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame^] | 522 | created_items = {} |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 523 | self._reload_connection() |
| 524 | network_dict = {'name': net_name, 'admin_state_up': True} |
| 525 | if net_type=="data" or net_type=="ptp": |
| 526 | if self.config.get('dataplane_physical_net') == None: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 527 | raise vimconn.vimconnConflictException("You must provide a 'dataplane_physical_net' at config value before creating sriov network") |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame^] | 528 | if not self.config.get('multisegment_support'): |
| 529 | network_dict["provider:physical_network"] = self.config[ |
| 530 | 'dataplane_physical_net'] # "physnet_sriov" #TODO physical |
| 531 | network_dict["provider:network_type"] = "vlan" |
| 532 | if vlan!=None: |
| 533 | network_dict["provider:network_type"] = vlan |
| 534 | else: |
| 535 | ###### Multi-segment case ###### |
| 536 | segment_list = [] |
| 537 | segment1_dict = {} |
| 538 | segment1_dict["provider:physical_network"] = '' |
| 539 | segment1_dict["provider:network_type"] = 'vxlan' |
| 540 | segment_list.append(segment1_dict) |
| 541 | segment2_dict = {} |
| 542 | segment2_dict["provider:physical_network"] = self.config['dataplane_physical_net'] |
| 543 | segment2_dict["provider:network_type"] = "vlan" |
| 544 | if self.config.get('multisegment_vlan_range'): |
| 545 | vlanID = self._generate_multisegment_vlanID() |
| 546 | segment2_dict["provider:segmentation_id"] = vlanID |
| 547 | # else |
| 548 | # raise vimconn.vimconnConflictException( |
| 549 | # "You must provide 'multisegment_vlan_range' at config dict before creating a multisegment network") |
| 550 | segment_list.append(segment2_dict) |
| 551 | network_dict["segments"] = segment_list |
| kate | 721d79b | 2017-06-24 04:21:38 -0700 | [diff] [blame] | 552 | |
| 553 | ####### VIO Specific Changes ######### |
| 554 | if self.vim_type == "VIO": |
| 555 | if vlan is not None: |
| 556 | network_dict["provider:segmentation_id"] = vlan |
| 557 | else: |
| 558 | if self.config.get('dataplane_net_vlan_range') is None: |
| 559 | raise vimconn.vimconnConflictException("You must provide "\ |
| 560 | "'dataplane_net_vlan_range' in format [start_ID - end_ID]"\ |
| 561 | "at config value before creating sriov network with vlan tag") |
| 562 | |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame^] | 563 | network_dict["provider:segmentation_id"] = self._generate_vlanID() |
| kate | 721d79b | 2017-06-24 04:21:38 -0700 | [diff] [blame] | 564 | |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame^] | 565 | network_dict["shared"] = shared |
| 566 | new_net = self.neutron.create_network({'network':network_dict}) |
| 567 | # print new_net |
| 568 | # create subnetwork, even if there is no profile |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 569 | if not ip_profile: |
| 570 | ip_profile = {} |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 571 | if not ip_profile.get('subnet_address'): |
| garciadeblas | 2299e3b | 2017-01-26 14:35:55 +0000 | [diff] [blame] | 572 | #Fake subnet is required |
| 573 | subnet_rand = random.randint(0, 255) |
| 574 | ip_profile['subnet_address'] = "192.168.{}.0/24".format(subnet_rand) |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 575 | if 'ip_version' not in ip_profile: |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 576 | ip_profile['ip_version'] = "IPv4" |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame^] | 577 | subnet = {"name": net_name+"-subnet", |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 578 | "network_id": new_net["network"]["id"], |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 579 | "ip_version": 4 if ip_profile['ip_version']=="IPv4" else 6, |
| 580 | "cidr": ip_profile['subnet_address'] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 581 | } |
| tierno | a1fb446 | 2017-06-30 12:25:50 +0200 | [diff] [blame] | 582 | # Gateway should be set to None if not needed. Otherwise openstack assigns one by default |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 583 | if ip_profile.get('gateway_address'): |
| tierno | 55d234c | 2018-07-04 18:29:21 +0200 | [diff] [blame] | 584 | subnet['gateway_ip'] = ip_profile['gateway_address'] |
| 585 | else: |
| 586 | subnet['gateway_ip'] = None |
| garciadeblas | edca7b3 | 2016-09-29 14:01:52 +0000 | [diff] [blame] | 587 | if ip_profile.get('dns_address'): |
| tierno | 455612d | 2017-05-30 16:40:10 +0200 | [diff] [blame] | 588 | subnet['dns_nameservers'] = ip_profile['dns_address'].split(";") |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 589 | if 'dhcp_enabled' in ip_profile: |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 590 | subnet['enable_dhcp'] = False if \ |
| 591 | ip_profile['dhcp_enabled']=="false" or ip_profile['dhcp_enabled']==False else True |
| 592 | if ip_profile.get('dhcp_start_address'): |
| tierno | a1fb446 | 2017-06-30 12:25:50 +0200 | [diff] [blame] | 593 | subnet['allocation_pools'] = [] |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 594 | subnet['allocation_pools'].append(dict()) |
| 595 | subnet['allocation_pools'][0]['start'] = ip_profile['dhcp_start_address'] |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 596 | if ip_profile.get('dhcp_count'): |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 597 | #parts = ip_profile['dhcp_start_address'].split('.') |
| 598 | #ip_int = (int(parts[0]) << 24) + (int(parts[1]) << 16) + (int(parts[2]) << 8) + int(parts[3]) |
| 599 | ip_int = int(netaddr.IPAddress(ip_profile['dhcp_start_address'])) |
| garciadeblas | 21d795b | 2016-09-29 17:31:46 +0200 | [diff] [blame] | 600 | ip_int += ip_profile['dhcp_count'] - 1 |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 601 | ip_str = str(netaddr.IPAddress(ip_int)) |
| 602 | subnet['allocation_pools'][0]['end'] = ip_str |
| garciadeblas | edca7b3 | 2016-09-29 14:01:52 +0000 | [diff] [blame] | 603 | #self.logger.debug(">>>>>>>>>>>>>>>>>> Subnet: %s", str(subnet)) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 604 | self.neutron.create_subnet({"subnet": subnet} ) |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame^] | 605 | |
| 606 | if net_type == "data" and self.config.get('multisegment_support'): |
| 607 | if self.config.get('l2gw_support'): |
| 608 | l2gw_list = self.neutron.list_l2_gateways().get("l2_gateways", ()) |
| 609 | for l2gw in l2gw_list: |
| 610 | l2gw_conn = {} |
| 611 | l2gw_conn["l2_gateway_id"] = l2gw["id"] |
| 612 | l2gw_conn["network_id"] = new_net["network"]["id"] |
| 613 | l2gw_conn["segmentation_id"] = str(vlanID) |
| 614 | new_l2gw_conn = self.neutron.create_l2_gateway_connection({"l2_gateway_connection": l2gw_conn}) |
| 615 | created_items["l2gwconn:" + str(new_l2gw_conn["l2_gateway_connection"]["id"])] = True |
| 616 | return new_net["network"]["id"], created_items |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 617 | except Exception as e: |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame^] | 618 | #delete l2gw connections (if any) before deleting the network |
| 619 | for k, v in created_items.items(): |
| 620 | if not v: # skip already deleted |
| 621 | continue |
| 622 | try: |
| 623 | k_item, _, k_id = k.partition(":") |
| 624 | if k_item == "l2gwconn": |
| 625 | self.neutron.delete_l2_gateway_connection(k_id) |
| 626 | except Exception as e2: |
| 627 | self.logger.error("Error deleting l2 gateway connection: {}: {}".format(type(e2).__name__, e2)) |
| garciadeblas | edca7b3 | 2016-09-29 14:01:52 +0000 | [diff] [blame] | 628 | if new_net: |
| 629 | self.neutron.delete_network(new_net['network']['id']) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 630 | self._format_exception(e) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 631 | |
| 632 | def get_network_list(self, filter_dict={}): |
| 633 | '''Obtain tenant networks of VIM |
| 634 | Filter_dict can be: |
| 635 | name: network name |
| 636 | id: network uuid |
| 637 | shared: boolean |
| 638 | tenant_id: tenant |
| 639 | admin_state_up: boolean |
| 640 | status: 'ACTIVE' |
| 641 | Returns the network list of dictionaries |
| 642 | ''' |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 643 | self.logger.debug("Getting network from VIM filter: '%s'", str(filter_dict)) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 644 | try: |
| 645 | self._reload_connection() |
| tierno | 69b590e | 2018-03-13 18:52:23 +0100 | [diff] [blame] | 646 | filter_dict_os = filter_dict.copy() |
| 647 | if self.api_version3 and "tenant_id" in filter_dict_os: |
| 648 | filter_dict_os['project_id'] = filter_dict_os.pop('tenant_id') #T ODO check |
| 649 | net_dict = self.neutron.list_networks(**filter_dict_os) |
| tierno | 00e3df7 | 2017-11-29 17:20:13 +0100 | [diff] [blame] | 650 | net_list = net_dict["networks"] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 651 | self.__net_os2mano(net_list) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 652 | return net_list |
| tierno | 8e995ce | 2016-09-22 08:13:00 +0000 | [diff] [blame] | 653 | except (neExceptions.ConnectionFailed, ksExceptions.ClientException, neExceptions.NeutronException, ConnectionError) as e: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 654 | self._format_exception(e) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 655 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 656 | def get_network(self, net_id): |
| 657 | '''Obtain details of network from VIM |
| 658 | Returns the network information from a network id''' |
| 659 | self.logger.debug(" Getting tenant network %s from VIM", net_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 660 | filter_dict={"id": net_id} |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 661 | net_list = self.get_network_list(filter_dict) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 662 | if len(net_list)==0: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 663 | raise vimconn.vimconnNotFoundException("Network '{}' not found".format(net_id)) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 664 | elif len(net_list)>1: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 665 | raise vimconn.vimconnConflictException("Found more than one network with this criteria") |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 666 | net = net_list[0] |
| 667 | subnets=[] |
| 668 | for subnet_id in net.get("subnets", () ): |
| 669 | try: |
| 670 | subnet = self.neutron.show_subnet(subnet_id) |
| 671 | except Exception as e: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 672 | self.logger.error("osconnector.get_network(): Error getting subnet %s %s" % (net_id, str(e))) |
| 673 | subnet = {"id": subnet_id, "fault": str(e)} |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 674 | subnets.append(subnet) |
| 675 | net["subnets"] = subnets |
| Pablo Montes Moreno | 51e553b | 2017-03-23 16:39:12 +0100 | [diff] [blame] | 676 | net["encapsulation"] = net.get('provider:network_type') |
| Anderson Bravalheri | 0fb7028 | 2018-12-16 19:28:37 +0000 | [diff] [blame] | 677 | net["encapsulation_type"] = net.get('provider:network_type') |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 678 | net["segmentation_id"] = net.get('provider:segmentation_id') |
| Anderson Bravalheri | 0fb7028 | 2018-12-16 19:28:37 +0000 | [diff] [blame] | 679 | net["encapsulation_id"] = net.get('provider:segmentation_id') |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 680 | return net |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 681 | |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame^] | 682 | def delete_network(self, net_id, created_items=None): |
| 683 | """ |
| 684 | Removes a tenant network from VIM and its associated elements |
| 685 | :param net_id: VIM identifier of the network, provided by method new_network |
| 686 | :param created_items: dictionary with extra items to be deleted. provided by method new_network |
| 687 | Returns the network identifier or raises an exception upon error or when network is not found |
| 688 | """ |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 689 | self.logger.debug("Deleting network '%s' from VIM", net_id) |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame^] | 690 | if created_items == None: |
| 691 | created_items = {} |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 692 | try: |
| 693 | self._reload_connection() |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame^] | 694 | #delete l2gw connections (if any) before deleting the network |
| 695 | for k, v in created_items.items(): |
| 696 | if not v: # skip already deleted |
| 697 | continue |
| 698 | try: |
| 699 | k_item, _, k_id = k.partition(":") |
| 700 | if k_item == "l2gwconn": |
| 701 | self.neutron.delete_l2_gateway_connection(k_id) |
| 702 | except Exception as e: |
| 703 | self.logger.error("Error deleting l2 gateway connection: {}: {}".format(type(e).__name__, e)) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 704 | #delete VM ports attached to this networks before the network |
| 705 | ports = self.neutron.list_ports(network_id=net_id) |
| 706 | for p in ports['ports']: |
| 707 | try: |
| 708 | self.neutron.delete_port(p["id"]) |
| 709 | except Exception as e: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 710 | self.logger.error("Error deleting port %s: %s", p["id"], str(e)) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 711 | self.neutron.delete_network(net_id) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 712 | return net_id |
| 713 | except (neExceptions.ConnectionFailed, neExceptions.NetworkNotFoundClient, neExceptions.NeutronException, |
| tierno | 8e995ce | 2016-09-22 08:13:00 +0000 | [diff] [blame] | 714 | ksExceptions.ClientException, neExceptions.NeutronException, ConnectionError) as e: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 715 | self._format_exception(e) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 716 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 717 | def refresh_nets_status(self, net_list): |
| 718 | '''Get the status of the networks |
| 719 | Params: the list of network identifiers |
| 720 | Returns a dictionary with: |
| 721 | net_id: #VIM id of this network |
| 722 | status: #Mandatory. Text with one of: |
| 723 | # DELETED (not found at vim) |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 724 | # VIM_ERROR (Cannot connect to VIM, VIM response error, ...) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 725 | # OTHER (Vim reported other status not understood) |
| 726 | # ERROR (VIM indicates an ERROR status) |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 727 | # ACTIVE, INACTIVE, DOWN (admin down), |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 728 | # BUILD (on building process) |
| 729 | # |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 730 | error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 731 | vim_info: #Text with plain information obtained from vim (yaml.safe_dump) |
| 732 | |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 733 | ''' |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 734 | net_dict={} |
| 735 | for net_id in net_list: |
| 736 | net = {} |
| 737 | try: |
| 738 | net_vim = self.get_network(net_id) |
| 739 | if net_vim['status'] in netStatus2manoFormat: |
| 740 | net["status"] = netStatus2manoFormat[ net_vim['status'] ] |
| 741 | else: |
| 742 | net["status"] = "OTHER" |
| 743 | net["error_msg"] = "VIM status reported " + net_vim['status'] |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 744 | |
| tierno | 8e995ce | 2016-09-22 08:13:00 +0000 | [diff] [blame] | 745 | if net['status'] == "ACTIVE" and not net_vim['admin_state_up']: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 746 | net['status'] = 'DOWN' |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 747 | |
| 748 | net['vim_info'] = self.serialize(net_vim) |
| 749 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 750 | if net_vim.get('fault'): #TODO |
| 751 | net['error_msg'] = str(net_vim['fault']) |
| 752 | except vimconn.vimconnNotFoundException as e: |
| 753 | self.logger.error("Exception getting net status: %s", str(e)) |
| 754 | net['status'] = "DELETED" |
| 755 | net['error_msg'] = str(e) |
| 756 | except vimconn.vimconnException as e: |
| 757 | self.logger.error("Exception getting net status: %s", str(e)) |
| 758 | net['status'] = "VIM_ERROR" |
| 759 | net['error_msg'] = str(e) |
| 760 | net_dict[net_id] = net |
| 761 | return net_dict |
| 762 | |
| 763 | def get_flavor(self, flavor_id): |
| 764 | '''Obtain flavor details from the VIM. Returns the flavor dict details''' |
| 765 | self.logger.debug("Getting flavor '%s'", flavor_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 766 | try: |
| 767 | self._reload_connection() |
| 768 | flavor = self.nova.flavors.find(id=flavor_id) |
| 769 | #TODO parse input and translate to VIM format (openmano_schemas.new_vminstance_response_schema) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 770 | return flavor.to_dict() |
| tierno | 8e995ce | 2016-09-22 08:13:00 +0000 | [diff] [blame] | 771 | except (nvExceptions.NotFound, nvExceptions.ClientException, ksExceptions.ClientException, ConnectionError) as e: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 772 | self._format_exception(e) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 773 | |
| tierno | cf157a8 | 2017-01-30 14:07:06 +0100 | [diff] [blame] | 774 | def get_flavor_id_from_data(self, flavor_dict): |
| 775 | """Obtain flavor id that match the flavor description |
| 776 | Returns the flavor_id or raises a vimconnNotFoundException |
| tierno | e26fc7a | 2017-05-30 14:43:03 +0200 | [diff] [blame] | 777 | flavor_dict: contains the required ram, vcpus, disk |
| 778 | If 'use_existing_flavors' is set to True at config, the closer flavor that provides same or more ram, vcpus |
| 779 | and disk is returned. Otherwise a flavor with exactly same ram, vcpus and disk is returned or a |
| 780 | vimconnNotFoundException is raised |
| tierno | cf157a8 | 2017-01-30 14:07:06 +0100 | [diff] [blame] | 781 | """ |
| tierno | e26fc7a | 2017-05-30 14:43:03 +0200 | [diff] [blame] | 782 | exact_match = False if self.config.get('use_existing_flavors') else True |
| tierno | cf157a8 | 2017-01-30 14:07:06 +0100 | [diff] [blame] | 783 | try: |
| 784 | self._reload_connection() |
| tierno | e26fc7a | 2017-05-30 14:43:03 +0200 | [diff] [blame] | 785 | flavor_candidate_id = None |
| 786 | flavor_candidate_data = (10000, 10000, 10000) |
| 787 | flavor_target = (flavor_dict["ram"], flavor_dict["vcpus"], flavor_dict["disk"]) |
| 788 | # numa=None |
| 789 | numas = flavor_dict.get("extended", {}).get("numas") |
| tierno | cf157a8 | 2017-01-30 14:07:06 +0100 | [diff] [blame] | 790 | if numas: |
| 791 | #TODO |
| 792 | raise vimconn.vimconnNotFoundException("Flavor with EPA still not implemted") |
| 793 | # if len(numas) > 1: |
| 794 | # raise vimconn.vimconnNotFoundException("Cannot find any flavor with more than one numa") |
| 795 | # numa=numas[0] |
| 796 | # numas = extended.get("numas") |
| 797 | for flavor in self.nova.flavors.list(): |
| 798 | epa = flavor.get_keys() |
| 799 | if epa: |
| 800 | continue |
| tierno | e26fc7a | 2017-05-30 14:43:03 +0200 | [diff] [blame] | 801 | # TODO |
| 802 | flavor_data = (flavor.ram, flavor.vcpus, flavor.disk) |
| 803 | if flavor_data == flavor_target: |
| 804 | return flavor.id |
| 805 | elif not exact_match and flavor_target < flavor_data < flavor_candidate_data: |
| 806 | flavor_candidate_id = flavor.id |
| 807 | flavor_candidate_data = flavor_data |
| 808 | if not exact_match and flavor_candidate_id: |
| 809 | return flavor_candidate_id |
| tierno | cf157a8 | 2017-01-30 14:07:06 +0100 | [diff] [blame] | 810 | raise vimconn.vimconnNotFoundException("Cannot find any flavor matching '{}'".format(str(flavor_dict))) |
| 811 | except (nvExceptions.NotFound, nvExceptions.ClientException, ksExceptions.ClientException, ConnectionError) as e: |
| 812 | self._format_exception(e) |
| 813 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 814 | def new_flavor(self, flavor_data, change_name_if_used=True): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 815 | '''Adds a tenant flavor to openstack VIM |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 816 | if change_name_if_used is True, it will change name in case of conflict, because it is not supported name repetition |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 817 | Returns the flavor identifier |
| 818 | ''' |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 819 | self.logger.debug("Adding flavor '%s'", str(flavor_data)) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 820 | retry=0 |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 821 | max_retries=3 |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 822 | name_suffix = 0 |
| anwars | c76a3ee | 2018-10-04 14:05:32 +0530 | [diff] [blame] | 823 | try: |
| 824 | name=flavor_data['name'] |
| 825 | while retry<max_retries: |
| 826 | retry+=1 |
| 827 | try: |
| 828 | self._reload_connection() |
| 829 | if change_name_if_used: |
| 830 | #get used names |
| 831 | fl_names=[] |
| 832 | fl=self.nova.flavors.list() |
| 833 | for f in fl: |
| 834 | fl_names.append(f.name) |
| 835 | while name in fl_names: |
| 836 | name_suffix += 1 |
| 837 | name = flavor_data['name']+"-" + str(name_suffix) |
| kate | 721d79b | 2017-06-24 04:21:38 -0700 | [diff] [blame] | 838 | |
| anwars | c76a3ee | 2018-10-04 14:05:32 +0530 | [diff] [blame] | 839 | ram = flavor_data.get('ram',64) |
| 840 | vcpus = flavor_data.get('vcpus',1) |
| 841 | numa_properties=None |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 842 | |
| anwars | c76a3ee | 2018-10-04 14:05:32 +0530 | [diff] [blame] | 843 | extended = flavor_data.get("extended") |
| 844 | if extended: |
| 845 | numas=extended.get("numas") |
| 846 | if numas: |
| 847 | numa_nodes = len(numas) |
| 848 | if numa_nodes > 1: |
| 849 | return -1, "Can not add flavor with more than one numa" |
| 850 | numa_properties = {"hw:numa_nodes":str(numa_nodes)} |
| 851 | numa_properties["hw:mem_page_size"] = "large" |
| 852 | numa_properties["hw:cpu_policy"] = "dedicated" |
| 853 | numa_properties["hw:numa_mempolicy"] = "strict" |
| 854 | if self.vim_type == "VIO": |
| 855 | numa_properties["vmware:extra_config"] = '{"numa.nodeAffinity":"0"}' |
| 856 | numa_properties["vmware:latency_sensitivity_level"] = "high" |
| 857 | for numa in numas: |
| 858 | #overwrite ram and vcpus |
| 859 | #check if key 'memory' is present in numa else use ram value at flavor |
| 860 | if 'memory' in numa: |
| 861 | ram = numa['memory']*1024 |
| 862 | #See for reference: https://specs.openstack.org/openstack/nova-specs/specs/mitaka/implemented/virt-driver-cpu-thread-pinning.html |
| 863 | if 'paired-threads' in numa: |
| 864 | vcpus = numa['paired-threads']*2 |
| 865 | #cpu_thread_policy "require" implies that the compute node must have an STM architecture |
| 866 | numa_properties["hw:cpu_thread_policy"] = "require" |
| 867 | numa_properties["hw:cpu_policy"] = "dedicated" |
| 868 | elif 'cores' in numa: |
| 869 | vcpus = numa['cores'] |
| 870 | # cpu_thread_policy "prefer" implies that the host must not have an SMT architecture, or a non-SMT architecture will be emulated |
| 871 | numa_properties["hw:cpu_thread_policy"] = "isolate" |
| 872 | numa_properties["hw:cpu_policy"] = "dedicated" |
| 873 | elif 'threads' in numa: |
| 874 | vcpus = numa['threads'] |
| 875 | # cpu_thread_policy "prefer" implies that the host may or may not have an SMT architecture |
| 876 | numa_properties["hw:cpu_thread_policy"] = "prefer" |
| 877 | numa_properties["hw:cpu_policy"] = "dedicated" |
| 878 | # for interface in numa.get("interfaces",() ): |
| 879 | # if interface["dedicated"]=="yes": |
| 880 | # raise vimconn.vimconnException("Passthrough interfaces are not supported for the openstack connector", http_code=vimconn.HTTP_Service_Unavailable) |
| 881 | # #TODO, add the key 'pci_passthrough:alias"="<label at config>:<number ifaces>"' when a way to connect it is available |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 882 | |
| anwars | c76a3ee | 2018-10-04 14:05:32 +0530 | [diff] [blame] | 883 | #create flavor |
| 884 | new_flavor=self.nova.flavors.create(name, |
| 885 | ram, |
| 886 | vcpus, |
| 887 | flavor_data.get('disk',0), |
| 888 | is_public=flavor_data.get('is_public', True) |
| 889 | ) |
| 890 | #add metadata |
| 891 | if numa_properties: |
| 892 | new_flavor.set_keys(numa_properties) |
| 893 | return new_flavor.id |
| 894 | except nvExceptions.Conflict as e: |
| 895 | if change_name_if_used and retry < max_retries: |
| 896 | continue |
| 897 | self._format_exception(e) |
| 898 | #except nvExceptions.BadRequest as e: |
| 899 | except (ksExceptions.ClientException, nvExceptions.ClientException, ConnectionError, KeyError) as e: |
| 900 | self._format_exception(e) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 901 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 902 | def delete_flavor(self,flavor_id): |
| 903 | '''Deletes a tenant flavor from openstack VIM. Returns the old flavor_id |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 904 | ''' |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 905 | try: |
| 906 | self._reload_connection() |
| 907 | self.nova.flavors.delete(flavor_id) |
| 908 | return flavor_id |
| 909 | #except nvExceptions.BadRequest as e: |
| tierno | 8e995ce | 2016-09-22 08:13:00 +0000 | [diff] [blame] | 910 | except (nvExceptions.NotFound, ksExceptions.ClientException, nvExceptions.ClientException, ConnectionError) as e: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 911 | self._format_exception(e) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 912 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 913 | def new_image(self,image_dict): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 914 | ''' |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 915 | Adds a tenant image to VIM. imge_dict is a dictionary with: |
| 916 | name: name |
| 917 | disk_format: qcow2, vhd, vmdk, raw (by default), ... |
| 918 | location: path or URI |
| 919 | public: "yes" or "no" |
| 920 | metadata: metadata of the image |
| 921 | Returns the image_id |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 922 | ''' |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 923 | retry=0 |
| 924 | max_retries=3 |
| 925 | while retry<max_retries: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 926 | retry+=1 |
| 927 | try: |
| 928 | self._reload_connection() |
| 929 | #determine format http://docs.openstack.org/developer/glance/formats.html |
| 930 | if "disk_format" in image_dict: |
| 931 | disk_format=image_dict["disk_format"] |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 932 | else: #autodiscover based on extension |
| tierno | 1beea86 | 2018-07-11 15:47:37 +0200 | [diff] [blame] | 933 | if image_dict['location'].endswith(".qcow2"): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 934 | disk_format="qcow2" |
| tierno | 1beea86 | 2018-07-11 15:47:37 +0200 | [diff] [blame] | 935 | elif image_dict['location'].endswith(".vhd"): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 936 | disk_format="vhd" |
| tierno | 1beea86 | 2018-07-11 15:47:37 +0200 | [diff] [blame] | 937 | elif image_dict['location'].endswith(".vmdk"): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 938 | disk_format="vmdk" |
| tierno | 1beea86 | 2018-07-11 15:47:37 +0200 | [diff] [blame] | 939 | elif image_dict['location'].endswith(".vdi"): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 940 | disk_format="vdi" |
| tierno | 1beea86 | 2018-07-11 15:47:37 +0200 | [diff] [blame] | 941 | elif image_dict['location'].endswith(".iso"): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 942 | disk_format="iso" |
| tierno | 1beea86 | 2018-07-11 15:47:37 +0200 | [diff] [blame] | 943 | elif image_dict['location'].endswith(".aki"): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 944 | disk_format="aki" |
| tierno | 1beea86 | 2018-07-11 15:47:37 +0200 | [diff] [blame] | 945 | elif image_dict['location'].endswith(".ari"): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 946 | disk_format="ari" |
| tierno | 1beea86 | 2018-07-11 15:47:37 +0200 | [diff] [blame] | 947 | elif image_dict['location'].endswith(".ami"): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 948 | disk_format="ami" |
| 949 | else: |
| 950 | disk_format="raw" |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 951 | self.logger.debug("new_image: '%s' loading from '%s'", image_dict['name'], image_dict['location']) |
| shashankjain | 3c83a21 | 2018-10-04 13:05:46 +0530 | [diff] [blame] | 952 | if self.vim_type == "VIO": |
| 953 | container_format = "bare" |
| 954 | if 'container_format' in image_dict: |
| 955 | container_format = image_dict['container_format'] |
| 956 | new_image = self.glance.images.create(name=image_dict['name'], container_format=container_format, |
| 957 | disk_format=disk_format) |
| 958 | else: |
| 959 | new_image = self.glance.images.create(name=image_dict['name']) |
| tierno | 1beea86 | 2018-07-11 15:47:37 +0200 | [diff] [blame] | 960 | if image_dict['location'].startswith("http"): |
| 961 | # TODO there is not a method to direct download. It must be downloaded locally with requests |
| 962 | raise vimconn.vimconnNotImplemented("Cannot create image from URL") |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 963 | else: #local path |
| 964 | with open(image_dict['location']) as fimage: |
| tierno | 1beea86 | 2018-07-11 15:47:37 +0200 | [diff] [blame] | 965 | self.glance.images.upload(new_image.id, fimage) |
| 966 | #new_image = self.glancev1.images.create(name=image_dict['name'], is_public=image_dict.get('public',"yes")=="yes", |
| 967 | # container_format="bare", data=fimage, disk_format=disk_format) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 968 | metadata_to_load = image_dict.get('metadata') |
| shashankjain | 3c83a21 | 2018-10-04 13:05:46 +0530 | [diff] [blame] | 969 | # TODO location is a reserved word for current openstack versions. fixed for VIO please check for openstack |
| 970 | if self.vim_type == "VIO": |
| 971 | metadata_to_load['upload_location'] = image_dict['location'] |
| 972 | else: |
| 973 | metadata_to_load['location'] = image_dict['location'] |
| tierno | 1beea86 | 2018-07-11 15:47:37 +0200 | [diff] [blame] | 974 | self.glance.images.update(new_image.id, **metadata_to_load) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 975 | return new_image.id |
| 976 | except (nvExceptions.Conflict, ksExceptions.ClientException, nvExceptions.ClientException) as e: |
| 977 | self._format_exception(e) |
| tierno | 8e995ce | 2016-09-22 08:13:00 +0000 | [diff] [blame] | 978 | except (HTTPException, gl1Exceptions.HTTPException, gl1Exceptions.CommunicationError, ConnectionError) as e: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 979 | if retry==max_retries: |
| 980 | continue |
| 981 | self._format_exception(e) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 982 | except IOError as e: #can not open the file |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 983 | raise vimconn.vimconnConnectionException(type(e).__name__ + ": " + str(e)+ " for " + image_dict['location'], |
| 984 | http_code=vimconn.HTTP_Bad_Request) |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 985 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 986 | def delete_image(self, image_id): |
| 987 | '''Deletes a tenant image from openstack VIM. Returns the old id |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 988 | ''' |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 989 | try: |
| 990 | self._reload_connection() |
| tierno | 1beea86 | 2018-07-11 15:47:37 +0200 | [diff] [blame] | 991 | self.glance.images.delete(image_id) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 992 | return image_id |
| shashankjain | 3c83a21 | 2018-10-04 13:05:46 +0530 | [diff] [blame] | 993 | except (nvExceptions.NotFound, ksExceptions.ClientException, nvExceptions.ClientException, gl1Exceptions.CommunicationError, gl1Exceptions.HTTPNotFound, ConnectionError) as e: #TODO remove |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 994 | self._format_exception(e) |
| 995 | |
| 996 | def get_image_id_from_path(self, path): |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 997 | '''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] | 998 | try: |
| 999 | self._reload_connection() |
| tierno | 1beea86 | 2018-07-11 15:47:37 +0200 | [diff] [blame] | 1000 | images = self.glance.images.list() |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1001 | for image in images: |
| 1002 | if image.metadata.get("location")==path: |
| 1003 | return image.id |
| 1004 | raise vimconn.vimconnNotFoundException("image with location '{}' not found".format( path)) |
| tierno | 8e995ce | 2016-09-22 08:13:00 +0000 | [diff] [blame] | 1005 | except (ksExceptions.ClientException, nvExceptions.ClientException, gl1Exceptions.CommunicationError, ConnectionError) as e: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1006 | self._format_exception(e) |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 1007 | |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 1008 | def get_image_list(self, filter_dict={}): |
| 1009 | '''Obtain tenant images from VIM |
| 1010 | Filter_dict can be: |
| 1011 | id: image id |
| 1012 | name: image name |
| 1013 | checksum: image checksum |
| 1014 | Returns the image list of dictionaries: |
| 1015 | [{<the fields at Filter_dict plus some VIM specific>}, ...] |
| 1016 | List can be empty |
| 1017 | ''' |
| 1018 | self.logger.debug("Getting image list from VIM filter: '%s'", str(filter_dict)) |
| 1019 | try: |
| 1020 | self._reload_connection() |
| tierno | 69b590e | 2018-03-13 18:52:23 +0100 | [diff] [blame] | 1021 | filter_dict_os = filter_dict.copy() |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 1022 | #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] | 1023 | image_list = self.glance.images.list() |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 1024 | filtered_list = [] |
| 1025 | for image in image_list: |
| tierno | 3cb8dc3 | 2017-10-24 18:13:19 +0200 | [diff] [blame] | 1026 | try: |
| tierno | 1beea86 | 2018-07-11 15:47:37 +0200 | [diff] [blame] | 1027 | if filter_dict.get("name") and image["name"] != filter_dict["name"]: |
| 1028 | continue |
| 1029 | if filter_dict.get("id") and image["id"] != filter_dict["id"]: |
| 1030 | continue |
| 1031 | if filter_dict.get("checksum") and image["checksum"] != filter_dict["checksum"]: |
| 1032 | continue |
| 1033 | |
| 1034 | filtered_list.append(image.copy()) |
| tierno | 3cb8dc3 | 2017-10-24 18:13:19 +0200 | [diff] [blame] | 1035 | except gl1Exceptions.HTTPNotFound: |
| 1036 | pass |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 1037 | return filtered_list |
| 1038 | except (ksExceptions.ClientException, nvExceptions.ClientException, gl1Exceptions.CommunicationError, ConnectionError) as e: |
| 1039 | self._format_exception(e) |
| 1040 | |
| Pablo Montes Moreno | 6a7785b | 2017-07-03 10:44:30 +0200 | [diff] [blame] | 1041 | def __wait_for_vm(self, vm_id, status): |
| 1042 | """wait until vm is in the desired status and return True. |
| 1043 | If the VM gets in ERROR status, return false. |
| 1044 | If the timeout is reached generate an exception""" |
| 1045 | elapsed_time = 0 |
| 1046 | while elapsed_time < server_timeout: |
| 1047 | vm_status = self.nova.servers.get(vm_id).status |
| 1048 | if vm_status == status: |
| 1049 | return True |
| 1050 | if vm_status == 'ERROR': |
| 1051 | return False |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 1052 | time.sleep(5) |
| 1053 | elapsed_time += 5 |
| Pablo Montes Moreno | 6a7785b | 2017-07-03 10:44:30 +0200 | [diff] [blame] | 1054 | |
| 1055 | # if we exceeded the timeout rollback |
| 1056 | if elapsed_time >= server_timeout: |
| 1057 | raise vimconn.vimconnException('Timeout waiting for instance ' + vm_id + ' to get ' + status, |
| 1058 | http_code=vimconn.HTTP_Request_Timeout) |
| 1059 | |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 1060 | def _get_openstack_availablity_zones(self): |
| 1061 | """ |
| 1062 | Get from openstack availability zones available |
| 1063 | :return: |
| 1064 | """ |
| 1065 | try: |
| 1066 | openstack_availability_zone = self.nova.availability_zones.list() |
| 1067 | openstack_availability_zone = [str(zone.zoneName) for zone in openstack_availability_zone |
| 1068 | if zone.zoneName != 'internal'] |
| 1069 | return openstack_availability_zone |
| 1070 | except Exception as e: |
| 1071 | return None |
| 1072 | |
| 1073 | def _set_availablity_zones(self): |
| 1074 | """ |
| 1075 | Set vim availablity zone |
| 1076 | :return: |
| 1077 | """ |
| 1078 | |
| 1079 | if 'availability_zone' in self.config: |
| 1080 | vim_availability_zones = self.config.get('availability_zone') |
| 1081 | if isinstance(vim_availability_zones, str): |
| 1082 | self.availability_zone = [vim_availability_zones] |
| 1083 | elif isinstance(vim_availability_zones, list): |
| 1084 | self.availability_zone = vim_availability_zones |
| 1085 | else: |
| 1086 | self.availability_zone = self._get_openstack_availablity_zones() |
| 1087 | |
| tierno | 5a3273c | 2017-08-29 11:43:46 +0200 | [diff] [blame] | 1088 | def _get_vm_availability_zone(self, availability_zone_index, availability_zone_list): |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 1089 | """ |
| tierno | 5a3273c | 2017-08-29 11:43:46 +0200 | [diff] [blame] | 1090 | Return thge availability zone to be used by the created VM. |
| 1091 | :return: The VIM availability zone to be used or None |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 1092 | """ |
| tierno | 5a3273c | 2017-08-29 11:43:46 +0200 | [diff] [blame] | 1093 | if availability_zone_index is None: |
| 1094 | if not self.config.get('availability_zone'): |
| 1095 | return None |
| 1096 | elif isinstance(self.config.get('availability_zone'), str): |
| 1097 | return self.config['availability_zone'] |
| 1098 | else: |
| 1099 | # TODO consider using a different parameter at config for default AV and AV list match |
| 1100 | return self.config['availability_zone'][0] |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 1101 | |
| tierno | 5a3273c | 2017-08-29 11:43:46 +0200 | [diff] [blame] | 1102 | vim_availability_zones = self.availability_zone |
| 1103 | # check if VIM offer enough availability zones describe in the VNFD |
| 1104 | if vim_availability_zones and len(availability_zone_list) <= len(vim_availability_zones): |
| 1105 | # check if all the names of NFV AV match VIM AV names |
| 1106 | match_by_index = False |
| 1107 | for av in availability_zone_list: |
| 1108 | if av not in vim_availability_zones: |
| 1109 | match_by_index = True |
| 1110 | break |
| 1111 | if match_by_index: |
| 1112 | return vim_availability_zones[availability_zone_index] |
| 1113 | else: |
| 1114 | return availability_zone_list[availability_zone_index] |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 1115 | else: |
| tierno | 5a3273c | 2017-08-29 11:43:46 +0200 | [diff] [blame] | 1116 | raise vimconn.vimconnConflictException("No enough availability zones at VIM for this deployment") |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 1117 | |
| tierno | 5a3273c | 2017-08-29 11:43:46 +0200 | [diff] [blame] | 1118 | def new_vminstance(self, name, description, start, image_id, flavor_id, net_list, cloud_config=None, disk_list=None, |
| 1119 | availability_zone_index=None, availability_zone_list=None): |
| tierno | 98e909c | 2017-10-14 13:27:03 +0200 | [diff] [blame] | 1120 | """Adds a VM instance to VIM |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1121 | Params: |
| 1122 | start: indicates if VM must start or boot in pause mode. Ignored |
| 1123 | image_id,flavor_id: iamge and flavor uuid |
| 1124 | net_list: list of interfaces, each one is a dictionary with: |
| 1125 | name: |
| 1126 | net_id: network uuid to connect |
| 1127 | vpci: virtual vcpi to assign, ignored because openstack lack #TODO |
| 1128 | model: interface model, ignored #TODO |
| 1129 | mac_address: used for SR-IOV ifaces #TODO for other types |
| 1130 | use: 'data', 'bridge', 'mgmt' |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1131 | type: 'virtual', 'PCI-PASSTHROUGH'('PF'), 'SR-IOV'('VF'), 'VFnotShared' |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1132 | vim_id: filled/added by this function |
| ahmadsa | f853d45 | 2016-12-22 11:33:47 +0500 | [diff] [blame] | 1133 | floating_ip: True/False (or it can be None) |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 1134 | 'cloud_config': (optional) dictionary with: |
| 1135 | 'key-pairs': (optional) list of strings with the public key to be inserted to the default user |
| 1136 | 'users': (optional) list of users to be inserted, each item is a dict with: |
| 1137 | 'name': (mandatory) user name, |
| 1138 | 'key-pairs': (optional) list of strings with the public key to be inserted to the user |
| 1139 | 'user-data': (optional) string is a text script to be passed directly to cloud-init |
| 1140 | 'config-files': (optional). List of files to be transferred. Each item is a dict with: |
| 1141 | 'dest': (mandatory) string with the destination absolute path |
| 1142 | 'encoding': (optional, by default text). Can be one of: |
| 1143 | 'b64', 'base64', 'gz', 'gz+b64', 'gz+base64', 'gzip+b64', 'gzip+base64' |
| 1144 | 'content' (mandatory): string with the content of the file |
| 1145 | 'permissions': (optional) string with file permissions, typically octal notation '0644' |
| 1146 | 'owner': (optional) file owner, string with the format 'owner:group' |
| 1147 | '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] | 1148 | 'disk_list': (optional) list with additional disks to the VM. Each item is a dict with: |
| 1149 | 'image_id': (optional). VIM id of an existing image. If not provided an empty disk must be mounted |
| 1150 | 'size': (mandatory) string with the size of the disk in GB |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 1151 | 'vim_id' (optional) should use this existing volume id |
| tierno | 5a3273c | 2017-08-29 11:43:46 +0200 | [diff] [blame] | 1152 | availability_zone_index: Index of availability_zone_list to use for this this VM. None if not AV required |
| 1153 | availability_zone_list: list of availability zones given by user in the VNFD descriptor. Ignore if |
| 1154 | availability_zone_index is None |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1155 | #TODO ip, security groups |
| tierno | 98e909c | 2017-10-14 13:27:03 +0200 | [diff] [blame] | 1156 | Returns a tuple with the instance identifier and created_items or raises an exception on error |
| 1157 | created_items can be None or a dictionary where this method can include key-values that will be passed to |
| 1158 | the method delete_vminstance and action_vminstance. Can be used to store created ports, volumes, etc. |
| 1159 | Format is vimconnector dependent, but do not use nested dictionaries and a value of None should be the same |
| 1160 | as not present. |
| 1161 | """ |
| tierno | fa51c20 | 2017-01-27 14:58:17 +0100 | [diff] [blame] | 1162 | self.logger.debug("new_vminstance input: image='%s' flavor='%s' nics='%s'",image_id, flavor_id,str(net_list)) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1163 | try: |
| Pablo Montes Moreno | 6a7785b | 2017-07-03 10:44:30 +0200 | [diff] [blame] | 1164 | server = None |
| tierno | 98e909c | 2017-10-14 13:27:03 +0200 | [diff] [blame] | 1165 | created_items = {} |
| tierno | b0b9dab | 2017-10-14 14:25:20 +0200 | [diff] [blame] | 1166 | # metadata = {} |
| tierno | 98e909c | 2017-10-14 13:27:03 +0200 | [diff] [blame] | 1167 | net_list_vim = [] |
| 1168 | external_network = [] # list of external networks to be connected to instance, later on used to create floating_ip |
| Pablo Montes Moreno | 6a7785b | 2017-07-03 10:44:30 +0200 | [diff] [blame] | 1169 | no_secured_ports = [] # List of port-is with port-security disabled |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1170 | self._reload_connection() |
| tierno | b0b9dab | 2017-10-14 14:25:20 +0200 | [diff] [blame] | 1171 | # metadata_vpci = {} # For a specific neutron plugin |
| tierno | b84cbdc | 2017-07-07 14:30:30 +0200 | [diff] [blame] | 1172 | block_device_mapping = None |
| tierno | a05b65a | 2019-02-01 12:30:27 +0000 | [diff] [blame] | 1173 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1174 | for net in net_list: |
| tierno | 98e909c | 2017-10-14 13:27:03 +0200 | [diff] [blame] | 1175 | if not net.get("net_id"): # skip non connected iface |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1176 | continue |
| Pablo Montes Moreno | 3be0b2a | 2017-03-30 13:22:15 +0200 | [diff] [blame] | 1177 | |
| tierno | a05b65a | 2019-02-01 12:30:27 +0000 | [diff] [blame] | 1178 | port_dict = { |
| Pablo Montes Moreno | 3be0b2a | 2017-03-30 13:22:15 +0200 | [diff] [blame] | 1179 | "network_id": net["net_id"], |
| 1180 | "name": net.get("name"), |
| 1181 | "admin_state_up": True |
| 1182 | } |
| tierno | a05b65a | 2019-02-01 12:30:27 +0000 | [diff] [blame] | 1183 | if self.config.get("security_groups") and net.get("port_security") is not False and \ |
| 1184 | not self.config.get("no_port_security_extension"): |
| 1185 | if not self.security_groups_id: |
| 1186 | self._get_ids_from_name() |
| 1187 | port_dict["security_groups"] = self.security_groups_id |
| 1188 | |
| Pablo Montes Moreno | 3be0b2a | 2017-03-30 13:22:15 +0200 | [diff] [blame] | 1189 | if net["type"]=="virtual": |
| tierno | b0b9dab | 2017-10-14 14:25:20 +0200 | [diff] [blame] | 1190 | pass |
| 1191 | # if "vpci" in net: |
| 1192 | # metadata_vpci[ net["net_id"] ] = [[ net["vpci"], "" ]] |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1193 | elif net["type"] == "VF" or net["type"] == "SR-IOV": # for VF |
| tierno | b0b9dab | 2017-10-14 14:25:20 +0200 | [diff] [blame] | 1194 | # if "vpci" in net: |
| 1195 | # if "VF" not in metadata_vpci: |
| 1196 | # metadata_vpci["VF"]=[] |
| 1197 | # metadata_vpci["VF"].append([ net["vpci"], "" ]) |
| Pablo Montes Moreno | 3be0b2a | 2017-03-30 13:22:15 +0200 | [diff] [blame] | 1198 | port_dict["binding:vnic_type"]="direct" |
| tierno | b0b9dab | 2017-10-14 14:25:20 +0200 | [diff] [blame] | 1199 | # VIO specific Changes |
| kate | 721d79b | 2017-06-24 04:21:38 -0700 | [diff] [blame] | 1200 | if self.vim_type == "VIO": |
| tierno | b0b9dab | 2017-10-14 14:25:20 +0200 | [diff] [blame] | 1201 | # Need to create port with port_security_enabled = False and no-security-groups |
| kate | 721d79b | 2017-06-24 04:21:38 -0700 | [diff] [blame] | 1202 | port_dict["port_security_enabled"]=False |
| 1203 | port_dict["provider_security_groups"]=[] |
| 1204 | port_dict["security_groups"]=[] |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1205 | else: # For PT PCI-PASSTHROUGH |
| tierno | b0b9dab | 2017-10-14 14:25:20 +0200 | [diff] [blame] | 1206 | # VIO specific Changes |
| 1207 | # Current VIO release does not support port with type 'direct-physical' |
| 1208 | # So no need to create virtual port in case of PCI-device. |
| 1209 | # Will update port_dict code when support gets added in next VIO release |
| kate | 721d79b | 2017-06-24 04:21:38 -0700 | [diff] [blame] | 1210 | if self.vim_type == "VIO": |
| tierno | b0b9dab | 2017-10-14 14:25:20 +0200 | [diff] [blame] | 1211 | raise vimconn.vimconnNotSupportedException( |
| 1212 | "Current VIO release does not support full passthrough (PT)") |
| 1213 | # if "vpci" in net: |
| 1214 | # if "PF" not in metadata_vpci: |
| 1215 | # metadata_vpci["PF"]=[] |
| 1216 | # metadata_vpci["PF"].append([ net["vpci"], "" ]) |
| Pablo Montes Moreno | 3be0b2a | 2017-03-30 13:22:15 +0200 | [diff] [blame] | 1217 | port_dict["binding:vnic_type"]="direct-physical" |
| 1218 | if not port_dict["name"]: |
| 1219 | port_dict["name"]=name |
| 1220 | if net.get("mac_address"): |
| 1221 | port_dict["mac_address"]=net["mac_address"] |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 1222 | if net.get("ip_address"): |
| 1223 | port_dict["fixed_ips"] = [{'ip_address': net["ip_address"]}] |
| 1224 | # TODO add 'subnet_id': <subnet_id> |
| Pablo Montes Moreno | 3be0b2a | 2017-03-30 13:22:15 +0200 | [diff] [blame] | 1225 | new_port = self.neutron.create_port({"port": port_dict }) |
| tierno | 00e3df7 | 2017-11-29 17:20:13 +0100 | [diff] [blame] | 1226 | created_items["port:" + str(new_port["port"]["id"])] = True |
| Pablo Montes Moreno | 3be0b2a | 2017-03-30 13:22:15 +0200 | [diff] [blame] | 1227 | net["mac_adress"] = new_port["port"]["mac_address"] |
| 1228 | net["vim_id"] = new_port["port"]["id"] |
| tierno | b84cbdc | 2017-07-07 14:30:30 +0200 | [diff] [blame] | 1229 | # if try to use a network without subnetwork, it will return a emtpy list |
| 1230 | fixed_ips = new_port["port"].get("fixed_ips") |
| 1231 | if fixed_ips: |
| 1232 | net["ip"] = fixed_ips[0].get("ip_address") |
| 1233 | else: |
| 1234 | net["ip"] = None |
| montesmoreno | 994a29d | 2017-08-22 11:23:06 +0200 | [diff] [blame] | 1235 | |
| 1236 | port = {"port-id": new_port["port"]["id"]} |
| 1237 | if float(self.nova.api_version.get_string()) >= 2.32: |
| 1238 | port["tag"] = new_port["port"]["name"] |
| 1239 | net_list_vim.append(port) |
| Pablo Montes Moreno | 3be0b2a | 2017-03-30 13:22:15 +0200 | [diff] [blame] | 1240 | |
| ahmadsa | f853d45 | 2016-12-22 11:33:47 +0500 | [diff] [blame] | 1241 | if net.get('floating_ip', False): |
| tierno | f8383b8 | 2017-01-18 15:49:48 +0100 | [diff] [blame] | 1242 | net['exit_on_floating_ip_error'] = True |
| ahmadsa | f853d45 | 2016-12-22 11:33:47 +0500 | [diff] [blame] | 1243 | external_network.append(net) |
| tierno | f8383b8 | 2017-01-18 15:49:48 +0100 | [diff] [blame] | 1244 | elif net['use'] == 'mgmt' and self.config.get('use_floating_ip'): |
| 1245 | net['exit_on_floating_ip_error'] = False |
| 1246 | external_network.append(net) |
| tierno | 326fd5e | 2018-02-22 11:58:59 +0100 | [diff] [blame] | 1247 | net['floating_ip'] = self.config.get('use_floating_ip') |
| tierno | f8383b8 | 2017-01-18 15:49:48 +0100 | [diff] [blame] | 1248 | |
| Pablo Montes Moreno | 6a7785b | 2017-07-03 10:44:30 +0200 | [diff] [blame] | 1249 | # If port security is disabled when the port has not yet been attached to the VM, then all vm traffic is dropped. |
| 1250 | # As a workaround we wait until the VM is active and then disable the port-security |
| tierno | 4d1ce22 | 2018-04-06 10:41:06 +0200 | [diff] [blame] | 1251 | if net.get("port_security") == False and not self.config.get("no_port_security_extension"): |
| Pablo Montes Moreno | 6a7785b | 2017-07-03 10:44:30 +0200 | [diff] [blame] | 1252 | no_secured_ports.append(new_port["port"]["id"]) |
| 1253 | |
| tierno | b0b9dab | 2017-10-14 14:25:20 +0200 | [diff] [blame] | 1254 | # if metadata_vpci: |
| 1255 | # metadata = {"pci_assignement": json.dumps(metadata_vpci)} |
| 1256 | # if len(metadata["pci_assignement"]) >255: |
| 1257 | # #limit the metadata size |
| 1258 | # #metadata["pci_assignement"] = metadata["pci_assignement"][0:255] |
| 1259 | # self.logger.warn("Metadata deleted since it exceeds the expected length (255) ") |
| 1260 | # metadata = {} |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 1261 | |
| tierno | b0b9dab | 2017-10-14 14:25:20 +0200 | [diff] [blame] | 1262 | self.logger.debug("name '%s' image_id '%s'flavor_id '%s' net_list_vim '%s' description '%s'", |
| 1263 | name, image_id, flavor_id, str(net_list_vim), description) |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 1264 | |
| tierno | 98e909c | 2017-10-14 13:27:03 +0200 | [diff] [blame] | 1265 | # cloud config |
| tierno | 0a1437e | 2017-10-02 00:17:43 +0200 | [diff] [blame] | 1266 | config_drive, userdata = self._create_user_data(cloud_config) |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 1267 | |
| tierno | 98e909c | 2017-10-14 13:27:03 +0200 | [diff] [blame] | 1268 | # Create additional volumes in case these are present in disk_list |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 1269 | base_disk_index = ord('b') |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 1270 | if disk_list: |
| tierno | b84cbdc | 2017-07-07 14:30:30 +0200 | [diff] [blame] | 1271 | block_device_mapping = {} |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 1272 | for disk in disk_list: |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 1273 | if disk.get('vim_id'): |
| 1274 | block_device_mapping['_vd' + chr(base_disk_index)] = disk['vim_id'] |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 1275 | else: |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 1276 | if 'image_id' in disk: |
| 1277 | volume = self.cinder.volumes.create(size=disk['size'], name=name + '_vd' + |
| 1278 | chr(base_disk_index), imageRef=disk['image_id']) |
| 1279 | else: |
| 1280 | volume = self.cinder.volumes.create(size=disk['size'], name=name + '_vd' + |
| 1281 | chr(base_disk_index)) |
| 1282 | created_items["volume:" + str(volume.id)] = True |
| 1283 | block_device_mapping['_vd' + chr(base_disk_index)] = volume.id |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 1284 | base_disk_index += 1 |
| 1285 | |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 1286 | # Wait until created volumes are with status available |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 1287 | elapsed_time = 0 |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 1288 | while elapsed_time < volume_timeout: |
| 1289 | for created_item in created_items: |
| 1290 | v, _, volume_id = created_item.partition(":") |
| 1291 | if v == 'volume': |
| 1292 | if self.cinder.volumes.get(volume_id).status != 'available': |
| 1293 | break |
| 1294 | else: # all ready: break from while |
| 1295 | break |
| 1296 | time.sleep(5) |
| 1297 | elapsed_time += 5 |
| tierno | b0b9dab | 2017-10-14 14:25:20 +0200 | [diff] [blame] | 1298 | # If we exceeded the timeout rollback |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 1299 | if elapsed_time >= volume_timeout: |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 1300 | raise vimconn.vimconnException('Timeout creating volumes for instance ' + name, |
| 1301 | http_code=vimconn.HTTP_Request_Timeout) |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 1302 | # get availability Zone |
| tierno | 5a3273c | 2017-08-29 11:43:46 +0200 | [diff] [blame] | 1303 | vm_av_zone = self._get_vm_availability_zone(availability_zone_index, availability_zone_list) |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 1304 | |
| tierno | b0b9dab | 2017-10-14 14:25:20 +0200 | [diff] [blame] | 1305 | self.logger.debug("nova.servers.create({}, {}, {}, nics={}, security_groups={}, " |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 1306 | "availability_zone={}, key_name={}, userdata={}, config_drive={}, " |
| tierno | b0b9dab | 2017-10-14 14:25:20 +0200 | [diff] [blame] | 1307 | "block_device_mapping={})".format(name, image_id, flavor_id, net_list_vim, |
| tierno | a05b65a | 2019-02-01 12:30:27 +0000 | [diff] [blame] | 1308 | self.config.get("security_groups"), vm_av_zone, |
| 1309 | self.config.get('keypair'), userdata, config_drive, |
| 1310 | block_device_mapping)) |
| tierno | b0b9dab | 2017-10-14 14:25:20 +0200 | [diff] [blame] | 1311 | server = self.nova.servers.create(name, image_id, flavor_id, nics=net_list_vim, |
| tierno | a05b65a | 2019-02-01 12:30:27 +0000 | [diff] [blame] | 1312 | security_groups=self.config.get("security_groups"), |
| 1313 | # TODO remove security_groups in future versions. Already at neutron port |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 1314 | availability_zone=vm_av_zone, |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 1315 | key_name=self.config.get('keypair'), |
| 1316 | userdata=userdata, |
| tierno | b84cbdc | 2017-07-07 14:30:30 +0200 | [diff] [blame] | 1317 | config_drive=config_drive, |
| 1318 | block_device_mapping=block_device_mapping |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 1319 | ) # , description=description) |
| Pablo Montes Moreno | 6a7785b | 2017-07-03 10:44:30 +0200 | [diff] [blame] | 1320 | |
| tierno | 326fd5e | 2018-02-22 11:58:59 +0100 | [diff] [blame] | 1321 | vm_start_time = time.time() |
| Pablo Montes Moreno | 6a7785b | 2017-07-03 10:44:30 +0200 | [diff] [blame] | 1322 | # Previously mentioned workaround to wait until the VM is active and then disable the port-security |
| 1323 | if no_secured_ports: |
| 1324 | self.__wait_for_vm(server.id, 'ACTIVE') |
| 1325 | |
| 1326 | for port_id in no_secured_ports: |
| 1327 | try: |
| tierno | 4d1ce22 | 2018-04-06 10:41:06 +0200 | [diff] [blame] | 1328 | self.neutron.update_port(port_id, |
| 1329 | {"port": {"port_security_enabled": False, "security_groups": None}}) |
| Pablo Montes Moreno | 6a7785b | 2017-07-03 10:44:30 +0200 | [diff] [blame] | 1330 | except Exception as e: |
| tierno | 4d1ce22 | 2018-04-06 10:41:06 +0200 | [diff] [blame] | 1331 | raise vimconn.vimconnException("It was not possible to disable port security for port {}".format( |
| 1332 | port_id)) |
| tierno | 98e909c | 2017-10-14 13:27:03 +0200 | [diff] [blame] | 1333 | # print "DONE :-)", server |
| Pablo Montes Moreno | 6a7785b | 2017-07-03 10:44:30 +0200 | [diff] [blame] | 1334 | |
| tierno | 4d1ce22 | 2018-04-06 10:41:06 +0200 | [diff] [blame] | 1335 | # pool_id = None |
| Pablo Montes Moreno | 6a7785b | 2017-07-03 10:44:30 +0200 | [diff] [blame] | 1336 | if external_network: |
| tierno | 98e909c | 2017-10-14 13:27:03 +0200 | [diff] [blame] | 1337 | floating_ips = self.neutron.list_floatingips().get("floatingips", ()) |
| ahmadsa | f853d45 | 2016-12-22 11:33:47 +0500 | [diff] [blame] | 1338 | for floating_network in external_network: |
| tierno | f8383b8 | 2017-01-18 15:49:48 +0100 | [diff] [blame] | 1339 | try: |
| tierno | f8383b8 | 2017-01-18 15:49:48 +0100 | [diff] [blame] | 1340 | assigned = False |
| tierno | 98e909c | 2017-10-14 13:27:03 +0200 | [diff] [blame] | 1341 | while not assigned: |
| tierno | f8383b8 | 2017-01-18 15:49:48 +0100 | [diff] [blame] | 1342 | if floating_ips: |
| 1343 | ip = floating_ips.pop(0) |
| tierno | 326fd5e | 2018-02-22 11:58:59 +0100 | [diff] [blame] | 1344 | if ip.get("port_id", False) or ip.get('tenant_id') != server.tenant_id: |
| 1345 | continue |
| 1346 | if isinstance(floating_network['floating_ip'], str): |
| 1347 | if ip.get("floating_network_id") != floating_network['floating_ip']: |
| 1348 | continue |
| 1349 | free_floating_ip = ip.get("floating_ip_address") |
| tierno | f8383b8 | 2017-01-18 15:49:48 +0100 | [diff] [blame] | 1350 | else: |
| tierno | cb3cca2 | 2018-05-31 15:08:52 +0200 | [diff] [blame] | 1351 | if isinstance(floating_network['floating_ip'], str) and \ |
| 1352 | floating_network['floating_ip'].lower() != "true": |
| tierno | 326fd5e | 2018-02-22 11:58:59 +0100 | [diff] [blame] | 1353 | pool_id = floating_network['floating_ip'] |
| 1354 | else: |
| tierno | 4d1ce22 | 2018-04-06 10:41:06 +0200 | [diff] [blame] | 1355 | # Find the external network |
| tierno | 326fd5e | 2018-02-22 11:58:59 +0100 | [diff] [blame] | 1356 | external_nets = list() |
| 1357 | for net in self.neutron.list_networks()['networks']: |
| 1358 | if net['router:external']: |
| 1359 | external_nets.append(net) |
| tierno | f8383b8 | 2017-01-18 15:49:48 +0100 | [diff] [blame] | 1360 | |
| tierno | 326fd5e | 2018-02-22 11:58:59 +0100 | [diff] [blame] | 1361 | if len(external_nets) == 0: |
| 1362 | raise vimconn.vimconnException("Cannot create floating_ip automatically since no external " |
| 1363 | "network is present", |
| 1364 | http_code=vimconn.HTTP_Conflict) |
| 1365 | if len(external_nets) > 1: |
| 1366 | raise vimconn.vimconnException("Cannot create floating_ip automatically since multiple " |
| 1367 | "external networks are present", |
| 1368 | http_code=vimconn.HTTP_Conflict) |
| tierno | f8383b8 | 2017-01-18 15:49:48 +0100 | [diff] [blame] | 1369 | |
| tierno | 326fd5e | 2018-02-22 11:58:59 +0100 | [diff] [blame] | 1370 | pool_id = external_nets[0].get('id') |
| tierno | f8383b8 | 2017-01-18 15:49:48 +0100 | [diff] [blame] | 1371 | param = {'floatingip': {'floating_network_id': pool_id, 'tenant_id': server.tenant_id}} |
| ahmadsa | f853d45 | 2016-12-22 11:33:47 +0500 | [diff] [blame] | 1372 | try: |
| tierno | 4d1ce22 | 2018-04-06 10:41:06 +0200 | [diff] [blame] | 1373 | # self.logger.debug("Creating floating IP") |
| tierno | f8383b8 | 2017-01-18 15:49:48 +0100 | [diff] [blame] | 1374 | new_floating_ip = self.neutron.create_floatingip(param) |
| 1375 | free_floating_ip = new_floating_ip['floatingip']['floating_ip_address'] |
| ahmadsa | f853d45 | 2016-12-22 11:33:47 +0500 | [diff] [blame] | 1376 | except Exception as e: |
| tierno | 326fd5e | 2018-02-22 11:58:59 +0100 | [diff] [blame] | 1377 | raise vimconn.vimconnException(type(e).__name__ + ": Cannot create new floating_ip " + |
| 1378 | str(e), http_code=vimconn.HTTP_Conflict) |
| 1379 | |
| 1380 | fix_ip = floating_network.get('ip') |
| 1381 | while not assigned: |
| 1382 | try: |
| 1383 | server.add_floating_ip(free_floating_ip, fix_ip) |
| 1384 | assigned = True |
| 1385 | except Exception as e: |
| tierno | 4d1ce22 | 2018-04-06 10:41:06 +0200 | [diff] [blame] | 1386 | # openstack need some time after VM creation to asign an IP. So retry if fails |
| tierno | 326fd5e | 2018-02-22 11:58:59 +0100 | [diff] [blame] | 1387 | vm_status = self.nova.servers.get(server.id).status |
| 1388 | if vm_status != 'ACTIVE' and vm_status != 'ERROR': |
| 1389 | if time.time() - vm_start_time < server_timeout: |
| 1390 | time.sleep(5) |
| 1391 | continue |
| tierno | 4d1ce22 | 2018-04-06 10:41:06 +0200 | [diff] [blame] | 1392 | raise vimconn.vimconnException( |
| 1393 | "Cannot create floating_ip: {} {}".format(type(e).__name__, e), |
| 1394 | http_code=vimconn.HTTP_Conflict) |
| tierno | 326fd5e | 2018-02-22 11:58:59 +0100 | [diff] [blame] | 1395 | |
| tierno | f8383b8 | 2017-01-18 15:49:48 +0100 | [diff] [blame] | 1396 | except Exception as e: |
| 1397 | if not floating_network['exit_on_floating_ip_error']: |
| 1398 | self.logger.warn("Cannot create floating_ip. %s", str(e)) |
| 1399 | continue |
| tierno | f8383b8 | 2017-01-18 15:49:48 +0100 | [diff] [blame] | 1400 | raise |
| montesmoreno | 2a1fc4e | 2017-01-09 16:46:04 +0000 | [diff] [blame] | 1401 | |
| tierno | 98e909c | 2017-10-14 13:27:03 +0200 | [diff] [blame] | 1402 | return server.id, created_items |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1403 | # except nvExceptions.NotFound as e: |
| 1404 | # error_value=-vimconn.HTTP_Not_Found |
| 1405 | # error_text= "vm instance %s not found" % vm_id |
| Pablo Montes Moreno | 6a7785b | 2017-07-03 10:44:30 +0200 | [diff] [blame] | 1406 | # except TypeError as e: |
| 1407 | # raise vimconn.vimconnException(type(e).__name__ + ": "+ str(e), http_code=vimconn.HTTP_Bad_Request) |
| 1408 | |
| 1409 | except Exception as e: |
| tierno | 98e909c | 2017-10-14 13:27:03 +0200 | [diff] [blame] | 1410 | server_id = None |
| 1411 | if server: |
| 1412 | server_id = server.id |
| 1413 | try: |
| 1414 | self.delete_vminstance(server_id, created_items) |
| 1415 | except Exception as e2: |
| 1416 | self.logger.error("new_vminstance rollback fail {}".format(e2)) |
| Pablo Montes Moreno | 6a7785b | 2017-07-03 10:44:30 +0200 | [diff] [blame] | 1417 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1418 | self._format_exception(e) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1419 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1420 | def get_vminstance(self,vm_id): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1421 | '''Returns the VM instance information from VIM''' |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1422 | #self.logger.debug("Getting VM from VIM") |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1423 | try: |
| 1424 | self._reload_connection() |
| 1425 | server = self.nova.servers.find(id=vm_id) |
| 1426 | #TODO parse input and translate to VIM format (openmano_schemas.new_vminstance_response_schema) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1427 | return server.to_dict() |
| tierno | 8e995ce | 2016-09-22 08:13:00 +0000 | [diff] [blame] | 1428 | except (ksExceptions.ClientException, nvExceptions.ClientException, nvExceptions.NotFound, ConnectionError) as e: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1429 | self._format_exception(e) |
| 1430 | |
| 1431 | def get_vminstance_console(self,vm_id, console_type="vnc"): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1432 | ''' |
| 1433 | Get a console for the virtual machine |
| 1434 | Params: |
| 1435 | vm_id: uuid of the VM |
| 1436 | console_type, can be: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1437 | "novnc" (by default), "xvpvnc" for VNC types, |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1438 | "rdp-html5" for RDP types, "spice-html5" for SPICE types |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1439 | Returns dict with the console parameters: |
| 1440 | protocol: ssh, ftp, http, https, ... |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1441 | server: usually ip address |
| 1442 | port: the http, ssh, ... port |
| 1443 | suffix: extra text, e.g. the http path and query string |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1444 | ''' |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1445 | self.logger.debug("Getting VM CONSOLE from VIM") |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1446 | try: |
| 1447 | self._reload_connection() |
| 1448 | server = self.nova.servers.find(id=vm_id) |
| 1449 | if console_type == None or console_type == "novnc": |
| 1450 | console_dict = server.get_vnc_console("novnc") |
| 1451 | elif console_type == "xvpvnc": |
| 1452 | console_dict = server.get_vnc_console(console_type) |
| 1453 | elif console_type == "rdp-html5": |
| 1454 | console_dict = server.get_rdp_console(console_type) |
| 1455 | elif console_type == "spice-html5": |
| 1456 | console_dict = server.get_spice_console(console_type) |
| 1457 | else: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1458 | raise vimconn.vimconnException("console type '{}' not allowed".format(console_type), http_code=vimconn.HTTP_Bad_Request) |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 1459 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1460 | console_dict1 = console_dict.get("console") |
| 1461 | if console_dict1: |
| 1462 | console_url = console_dict1.get("url") |
| 1463 | if console_url: |
| 1464 | #parse console_url |
| 1465 | protocol_index = console_url.find("//") |
| 1466 | suffix_index = console_url[protocol_index+2:].find("/") + protocol_index+2 |
| 1467 | port_index = console_url[protocol_index+2:suffix_index].find(":") + protocol_index+2 |
| 1468 | if protocol_index < 0 or port_index<0 or suffix_index<0: |
| 1469 | return -vimconn.HTTP_Internal_Server_Error, "Unexpected response from VIM" |
| 1470 | console_dict={"protocol": console_url[0:protocol_index], |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 1471 | "server": console_url[protocol_index+2:port_index], |
| 1472 | "port": console_url[port_index:suffix_index], |
| 1473 | "suffix": console_url[suffix_index+1:] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1474 | } |
| 1475 | protocol_index += 2 |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1476 | return console_dict |
| 1477 | raise vimconn.vimconnUnexpectedResponse("Unexpected response from VIM") |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 1478 | |
| tierno | 8e995ce | 2016-09-22 08:13:00 +0000 | [diff] [blame] | 1479 | except (nvExceptions.NotFound, ksExceptions.ClientException, nvExceptions.ClientException, nvExceptions.BadRequest, ConnectionError) as e: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1480 | self._format_exception(e) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1481 | |
| tierno | 98e909c | 2017-10-14 13:27:03 +0200 | [diff] [blame] | 1482 | def delete_vminstance(self, vm_id, created_items=None): |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1483 | '''Removes a VM instance from VIM. Returns the old identifier |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1484 | ''' |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1485 | #print "osconnector: Getting VM from VIM" |
| tierno | 98e909c | 2017-10-14 13:27:03 +0200 | [diff] [blame] | 1486 | if created_items == None: |
| 1487 | created_items = {} |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1488 | try: |
| 1489 | self._reload_connection() |
| tierno | 98e909c | 2017-10-14 13:27:03 +0200 | [diff] [blame] | 1490 | # delete VM ports attached to this networks before the virtual machine |
| 1491 | for k, v in created_items.items(): |
| 1492 | if not v: # skip already deleted |
| 1493 | continue |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1494 | try: |
| tierno | ad6bdd4 | 2018-01-10 10:43:46 +0100 | [diff] [blame] | 1495 | k_item, _, k_id = k.partition(":") |
| 1496 | if k_item == "port": |
| 1497 | self.neutron.delete_port(k_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1498 | except Exception as e: |
| tierno | 00e3df7 | 2017-11-29 17:20:13 +0100 | [diff] [blame] | 1499 | self.logger.error("Error deleting port: {}: {}".format(type(e).__name__, e)) |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 1500 | |
| tierno | 98e909c | 2017-10-14 13:27:03 +0200 | [diff] [blame] | 1501 | # #commented because detaching the volumes makes the servers.delete not work properly ?!? |
| 1502 | # #dettach volumes attached |
| 1503 | # server = self.nova.servers.get(vm_id) |
| 1504 | # volumes_attached_dict = server._info['os-extended-volumes:volumes_attached'] #volume['id'] |
| 1505 | # #for volume in volumes_attached_dict: |
| 1506 | # # self.cinder.volumes.detach(volume['id']) |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 1507 | |
| tierno | 98e909c | 2017-10-14 13:27:03 +0200 | [diff] [blame] | 1508 | if vm_id: |
| 1509 | self.nova.servers.delete(vm_id) |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 1510 | |
| tierno | 98e909c | 2017-10-14 13:27:03 +0200 | [diff] [blame] | 1511 | # delete volumes. Although having detached, they should have in active status before deleting |
| 1512 | # we ensure in this loop |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 1513 | keep_waiting = True |
| 1514 | elapsed_time = 0 |
| 1515 | while keep_waiting and elapsed_time < volume_timeout: |
| 1516 | keep_waiting = False |
| tierno | 98e909c | 2017-10-14 13:27:03 +0200 | [diff] [blame] | 1517 | for k, v in created_items.items(): |
| 1518 | if not v: # skip already deleted |
| 1519 | continue |
| 1520 | try: |
| tierno | ad6bdd4 | 2018-01-10 10:43:46 +0100 | [diff] [blame] | 1521 | k_item, _, k_id = k.partition(":") |
| 1522 | if k_item == "volume": |
| 1523 | if self.cinder.volumes.get(k_id).status != 'available': |
| tierno | 98e909c | 2017-10-14 13:27:03 +0200 | [diff] [blame] | 1524 | keep_waiting = True |
| 1525 | else: |
| tierno | ad6bdd4 | 2018-01-10 10:43:46 +0100 | [diff] [blame] | 1526 | self.cinder.volumes.delete(k_id) |
| tierno | 98e909c | 2017-10-14 13:27:03 +0200 | [diff] [blame] | 1527 | except Exception as e: |
| tierno | 00e3df7 | 2017-11-29 17:20:13 +0100 | [diff] [blame] | 1528 | self.logger.error("Error deleting volume: {}: {}".format(type(e).__name__, e)) |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 1529 | if keep_waiting: |
| 1530 | time.sleep(1) |
| 1531 | elapsed_time += 1 |
| tierno | 98e909c | 2017-10-14 13:27:03 +0200 | [diff] [blame] | 1532 | return None |
| tierno | 8e995ce | 2016-09-22 08:13:00 +0000 | [diff] [blame] | 1533 | except (nvExceptions.NotFound, ksExceptions.ClientException, nvExceptions.ClientException, ConnectionError) as e: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1534 | self._format_exception(e) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1535 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1536 | def refresh_vms_status(self, vm_list): |
| 1537 | '''Get the status of the virtual machines and their interfaces/ports |
| 1538 | Params: the list of VM identifiers |
| 1539 | Returns a dictionary with: |
| 1540 | vm_id: #VIM id of this Virtual Machine |
| 1541 | status: #Mandatory. Text with one of: |
| 1542 | # DELETED (not found at vim) |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1543 | # VIM_ERROR (Cannot connect to VIM, VIM response error, ...) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1544 | # OTHER (Vim reported other status not understood) |
| 1545 | # ERROR (VIM indicates an ERROR status) |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1546 | # ACTIVE, PAUSED, SUSPENDED, INACTIVE (not running), |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1547 | # CREATING (on building process), ERROR |
| 1548 | # ACTIVE:NoMgmtIP (Active but any of its interface has an IP address |
| 1549 | # |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1550 | error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1551 | vim_info: #Text with plain information obtained from vim (yaml.safe_dump) |
| 1552 | interfaces: |
| 1553 | - vim_info: #Text with plain information obtained from vim (yaml.safe_dump) |
| 1554 | mac_address: #Text format XX:XX:XX:XX:XX:XX |
| 1555 | vim_net_id: #network id where this interface is connected |
| 1556 | vim_interface_id: #interface/port VIM id |
| 1557 | ip_address: #null, or text with IPv4, IPv6 address |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 1558 | compute_node: #identification of compute node where PF,VF interface is allocated |
| 1559 | pci: #PCI address of the NIC that hosts the PF,VF |
| 1560 | vlan: #physical VLAN used for VF |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1561 | ''' |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1562 | vm_dict={} |
| 1563 | self.logger.debug("refresh_vms status: Getting tenant VM instance information from VIM") |
| 1564 | for vm_id in vm_list: |
| 1565 | vm={} |
| 1566 | try: |
| 1567 | vm_vim = self.get_vminstance(vm_id) |
| 1568 | if vm_vim['status'] in vmStatus2manoFormat: |
| 1569 | vm['status'] = vmStatus2manoFormat[ vm_vim['status'] ] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1570 | else: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1571 | vm['status'] = "OTHER" |
| 1572 | vm['error_msg'] = "VIM status reported " + vm_vim['status'] |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1573 | |
| 1574 | vm['vim_info'] = self.serialize(vm_vim) |
| 1575 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1576 | vm["interfaces"] = [] |
| 1577 | if vm_vim.get('fault'): |
| 1578 | vm['error_msg'] = str(vm_vim['fault']) |
| 1579 | #get interfaces |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1580 | try: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1581 | self._reload_connection() |
| tierno | b42fd9b | 2018-06-20 10:44:32 +0200 | [diff] [blame] | 1582 | port_dict = self.neutron.list_ports(device_id=vm_id) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1583 | for port in port_dict["ports"]: |
| 1584 | interface={} |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1585 | interface['vim_info'] = self.serialize(port) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1586 | interface["mac_address"] = port.get("mac_address") |
| 1587 | interface["vim_net_id"] = port["network_id"] |
| 1588 | interface["vim_interface_id"] = port["id"] |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1589 | # check if OS-EXT-SRV-ATTR:host is there, |
| Mike Marchetti | 5b9da42 | 2017-05-02 15:35:47 -0400 | [diff] [blame] | 1590 | # in case of non-admin credentials, it will be missing |
| 1591 | if vm_vim.get('OS-EXT-SRV-ATTR:host'): |
| 1592 | interface["compute_node"] = vm_vim['OS-EXT-SRV-ATTR:host'] |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 1593 | interface["pci"] = None |
| Mike Marchetti | 5b9da42 | 2017-05-02 15:35:47 -0400 | [diff] [blame] | 1594 | |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1595 | # check if binding:profile is there, |
| Mike Marchetti | 5b9da42 | 2017-05-02 15:35:47 -0400 | [diff] [blame] | 1596 | # in case of non-admin credentials, it will be missing |
| 1597 | if port.get('binding:profile'): |
| 1598 | if port['binding:profile'].get('pci_slot'): |
| 1599 | # TODO: At the moment sr-iov pci addresses are converted to PF pci addresses by setting the slot to 0x00 |
| 1600 | # TODO: This is just a workaround valid for niantinc. Find a better way to do so |
| 1601 | # CHANGE DDDD:BB:SS.F to DDDD:BB:00.(F%2) assuming there are 2 ports per nic |
| 1602 | pci = port['binding:profile']['pci_slot'] |
| 1603 | # interface["pci"] = pci[:-4] + "00." + str(int(pci[-1]) % 2) |
| 1604 | interface["pci"] = pci |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 1605 | interface["vlan"] = None |
| Pablo Montes Moreno | 3be0b2a | 2017-03-30 13:22:15 +0200 | [diff] [blame] | 1606 | #if network is of type vlan and port is of type direct (sr-iov) then set vlan id |
| Pablo Montes Moreno | 51e553b | 2017-03-23 16:39:12 +0100 | [diff] [blame] | 1607 | network = self.neutron.show_network(port["network_id"]) |
| Pablo Montes Moreno | 3be0b2a | 2017-03-30 13:22:15 +0200 | [diff] [blame] | 1608 | if network['network'].get('provider:network_type') == 'vlan' and \ |
| 1609 | port.get("binding:vnic_type") == "direct": |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 1610 | interface["vlan"] = network['network'].get('provider:segmentation_id') |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1611 | ips=[] |
| 1612 | #look for floating ip address |
| tierno | b42fd9b | 2018-06-20 10:44:32 +0200 | [diff] [blame] | 1613 | try: |
| 1614 | floating_ip_dict = self.neutron.list_floatingips(port_id=port["id"]) |
| 1615 | if floating_ip_dict.get("floatingips"): |
| 1616 | ips.append(floating_ip_dict["floatingips"][0].get("floating_ip_address") ) |
| 1617 | except Exception: |
| 1618 | pass |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1619 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1620 | for subnet in port["fixed_ips"]: |
| 1621 | ips.append(subnet["ip_address"]) |
| 1622 | interface["ip_address"] = ";".join(ips) |
| 1623 | vm["interfaces"].append(interface) |
| 1624 | except Exception as e: |
| tierno | b42fd9b | 2018-06-20 10:44:32 +0200 | [diff] [blame] | 1625 | self.logger.error("Error getting vm interface information {}: {}".format(type(e).__name__, e), |
| 1626 | exc_info=True) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1627 | except vimconn.vimconnNotFoundException as e: |
| 1628 | self.logger.error("Exception getting vm status: %s", str(e)) |
| 1629 | vm['status'] = "DELETED" |
| 1630 | vm['error_msg'] = str(e) |
| 1631 | except vimconn.vimconnException as e: |
| 1632 | self.logger.error("Exception getting vm status: %s", str(e)) |
| 1633 | vm['status'] = "VIM_ERROR" |
| 1634 | vm['error_msg'] = str(e) |
| 1635 | vm_dict[vm_id] = vm |
| 1636 | return vm_dict |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 1637 | |
| tierno | 98e909c | 2017-10-14 13:27:03 +0200 | [diff] [blame] | 1638 | def action_vminstance(self, vm_id, action_dict, created_items={}): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1639 | '''Send and action over a VM instance from VIM |
| tierno | 98e909c | 2017-10-14 13:27:03 +0200 | [diff] [blame] | 1640 | 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] | 1641 | self.logger.debug("Action over VM '%s': %s", vm_id, str(action_dict)) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1642 | try: |
| 1643 | self._reload_connection() |
| 1644 | server = self.nova.servers.find(id=vm_id) |
| 1645 | if "start" in action_dict: |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 1646 | if action_dict["start"]=="rebuild": |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1647 | server.rebuild() |
| 1648 | else: |
| 1649 | if server.status=="PAUSED": |
| 1650 | server.unpause() |
| 1651 | elif server.status=="SUSPENDED": |
| 1652 | server.resume() |
| 1653 | elif server.status=="SHUTOFF": |
| 1654 | server.start() |
| 1655 | elif "pause" in action_dict: |
| 1656 | server.pause() |
| 1657 | elif "resume" in action_dict: |
| 1658 | server.resume() |
| 1659 | elif "shutoff" in action_dict or "shutdown" in action_dict: |
| 1660 | server.stop() |
| 1661 | elif "forceOff" in action_dict: |
| 1662 | server.stop() #TODO |
| 1663 | elif "terminate" in action_dict: |
| 1664 | server.delete() |
| 1665 | elif "createImage" in action_dict: |
| 1666 | server.create_image() |
| 1667 | #"path":path_schema, |
| 1668 | #"description":description_schema, |
| 1669 | #"name":name_schema, |
| 1670 | #"metadata":metadata_schema, |
| 1671 | #"imageRef": id_schema, |
| 1672 | #"disk": {"oneOf":[{"type": "null"}, {"type":"string"}] }, |
| 1673 | elif "rebuild" in action_dict: |
| 1674 | server.rebuild(server.image['id']) |
| 1675 | elif "reboot" in action_dict: |
| 1676 | server.reboot() #reboot_type='SOFT' |
| 1677 | elif "console" in action_dict: |
| 1678 | console_type = action_dict["console"] |
| 1679 | if console_type == None or console_type == "novnc": |
| 1680 | console_dict = server.get_vnc_console("novnc") |
| 1681 | elif console_type == "xvpvnc": |
| 1682 | console_dict = server.get_vnc_console(console_type) |
| 1683 | elif console_type == "rdp-html5": |
| 1684 | console_dict = server.get_rdp_console(console_type) |
| 1685 | elif console_type == "spice-html5": |
| 1686 | console_dict = server.get_spice_console(console_type) |
| 1687 | else: |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 1688 | raise vimconn.vimconnException("console type '{}' not allowed".format(console_type), |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1689 | http_code=vimconn.HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1690 | try: |
| 1691 | console_url = console_dict["console"]["url"] |
| 1692 | #parse console_url |
| 1693 | protocol_index = console_url.find("//") |
| 1694 | suffix_index = console_url[protocol_index+2:].find("/") + protocol_index+2 |
| 1695 | port_index = console_url[protocol_index+2:suffix_index].find(":") + protocol_index+2 |
| 1696 | if protocol_index < 0 or port_index<0 or suffix_index<0: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1697 | raise vimconn.vimconnException("Unexpected response from VIM " + str(console_dict)) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1698 | console_dict2={"protocol": console_url[0:protocol_index], |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 1699 | "server": console_url[protocol_index+2 : port_index], |
| 1700 | "port": int(console_url[port_index+1 : suffix_index]), |
| 1701 | "suffix": console_url[suffix_index+1:] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1702 | } |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 1703 | return console_dict2 |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1704 | except Exception as e: |
| 1705 | raise vimconn.vimconnException("Unexpected response from VIM " + str(console_dict)) |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 1706 | |
| tierno | 98e909c | 2017-10-14 13:27:03 +0200 | [diff] [blame] | 1707 | return None |
| tierno | 8e995ce | 2016-09-22 08:13:00 +0000 | [diff] [blame] | 1708 | except (ksExceptions.ClientException, nvExceptions.ClientException, nvExceptions.NotFound, ConnectionError) as e: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1709 | self._format_exception(e) |
| 1710 | #TODO insert exception vimconn.HTTP_Unauthorized |
| 1711 | |
| kate | 721d79b | 2017-06-24 04:21:38 -0700 | [diff] [blame] | 1712 | ####### VIO Specific Changes ######### |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame^] | 1713 | def _generate_vlanID(self): |
| kate | 721d79b | 2017-06-24 04:21:38 -0700 | [diff] [blame] | 1714 | """ |
| 1715 | Method to get unused vlanID |
| 1716 | Args: |
| 1717 | None |
| 1718 | Returns: |
| 1719 | vlanID |
| 1720 | """ |
| 1721 | #Get used VLAN IDs |
| 1722 | usedVlanIDs = [] |
| 1723 | networks = self.get_network_list() |
| 1724 | for net in networks: |
| 1725 | if net.get('provider:segmentation_id'): |
| 1726 | usedVlanIDs.append(net.get('provider:segmentation_id')) |
| 1727 | used_vlanIDs = set(usedVlanIDs) |
| 1728 | |
| 1729 | #find unused VLAN ID |
| 1730 | for vlanID_range in self.config.get('dataplane_net_vlan_range'): |
| 1731 | try: |
| 1732 | start_vlanid , end_vlanid = map(int, vlanID_range.replace(" ", "").split("-")) |
| 1733 | for vlanID in xrange(start_vlanid, end_vlanid + 1): |
| 1734 | if vlanID not in used_vlanIDs: |
| 1735 | return vlanID |
| 1736 | except Exception as exp: |
| 1737 | raise vimconn.vimconnException("Exception {} occurred while generating VLAN ID.".format(exp)) |
| 1738 | else: |
| 1739 | raise vimconn.vimconnConflictException("Unable to create the SRIOV VLAN network."\ |
| 1740 | " All given Vlan IDs {} are in use.".format(self.config.get('dataplane_net_vlan_range'))) |
| 1741 | |
| 1742 | |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame^] | 1743 | def _generate_multisegment_vlanID(self): |
| 1744 | """ |
| 1745 | Method to get unused vlanID |
| 1746 | Args: |
| 1747 | None |
| 1748 | Returns: |
| 1749 | vlanID |
| 1750 | """ |
| 1751 | #Get used VLAN IDs |
| 1752 | usedVlanIDs = [] |
| 1753 | networks = self.get_network_list() |
| 1754 | for net in networks: |
| 1755 | if net.get('provider:network_type') == "vlan" and net.get('provider:segmentation_id'): |
| 1756 | usedVlanIDs.append(net.get('provider:segmentation_id')) |
| 1757 | elif net.get('segments'): |
| 1758 | for segment in net.get('segments'): |
| 1759 | if segment.get('provider:network_type') == "vlan" and segment.get('provider:segmentation_id'): |
| 1760 | usedVlanIDs.append(segment.get('provider:segmentation_id')) |
| 1761 | used_vlanIDs = set(usedVlanIDs) |
| 1762 | |
| 1763 | #find unused VLAN ID |
| 1764 | for vlanID_range in self.config.get('multisegment_vlan_range'): |
| 1765 | try: |
| 1766 | start_vlanid , end_vlanid = map(int, vlanID_range.replace(" ", "").split("-")) |
| 1767 | for vlanID in xrange(start_vlanid, end_vlanid + 1): |
| 1768 | if vlanID not in used_vlanIDs: |
| 1769 | return vlanID |
| 1770 | except Exception as exp: |
| 1771 | raise vimconn.vimconnException("Exception {} occurred while generating VLAN ID.".format(exp)) |
| 1772 | else: |
| 1773 | raise vimconn.vimconnConflictException("Unable to create the VLAN segment."\ |
| 1774 | " All VLAN IDs {} are in use.".format(self.config.get('multisegment_vlan_range'))) |
| 1775 | |
| 1776 | |
| 1777 | def _validate_vlan_ranges(self, input_vlan_range, text_vlan_range): |
| kate | 721d79b | 2017-06-24 04:21:38 -0700 | [diff] [blame] | 1778 | """ |
| 1779 | Method to validate user given vlanID ranges |
| 1780 | Args: None |
| 1781 | Returns: None |
| 1782 | """ |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame^] | 1783 | for vlanID_range in input_vlan_range: |
| kate | 721d79b | 2017-06-24 04:21:38 -0700 | [diff] [blame] | 1784 | vlan_range = vlanID_range.replace(" ", "") |
| 1785 | #validate format |
| 1786 | vlanID_pattern = r'(\d)*-(\d)*$' |
| 1787 | match_obj = re.match(vlanID_pattern, vlan_range) |
| 1788 | if not match_obj: |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame^] | 1789 | raise vimconn.vimconnConflictException("Invalid VLAN range for {}: {}.You must provide "\ |
| 1790 | "'{}' in format [start_ID - end_ID].".format(text_vlan_range, vlanID_range, text_vlan_range)) |
| kate | 721d79b | 2017-06-24 04:21:38 -0700 | [diff] [blame] | 1791 | |
| 1792 | start_vlanid , end_vlanid = map(int,vlan_range.split("-")) |
| 1793 | if start_vlanid <= 0 : |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame^] | 1794 | raise vimconn.vimconnConflictException("Invalid VLAN range for {}: {}."\ |
| kate | 721d79b | 2017-06-24 04:21:38 -0700 | [diff] [blame] | 1795 | "Start ID can not be zero. For VLAN "\ |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame^] | 1796 | "networks valid IDs are 1 to 4094 ".format(text_vlan_range, vlanID_range)) |
| kate | 721d79b | 2017-06-24 04:21:38 -0700 | [diff] [blame] | 1797 | if end_vlanid > 4094 : |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame^] | 1798 | raise vimconn.vimconnConflictException("Invalid VLAN range for {}: {}."\ |
| kate | 721d79b | 2017-06-24 04:21:38 -0700 | [diff] [blame] | 1799 | "End VLAN ID can not be greater than 4094. For VLAN "\ |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame^] | 1800 | "networks valid IDs are 1 to 4094 ".format(text_vlan_range, vlanID_range)) |
| kate | 721d79b | 2017-06-24 04:21:38 -0700 | [diff] [blame] | 1801 | |
| 1802 | if start_vlanid > end_vlanid: |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame^] | 1803 | raise vimconn.vimconnConflictException("Invalid VLAN range for {}: {}."\ |
| 1804 | "You must provide '{}' in format start_ID - end_ID and "\ |
| 1805 | "start_ID < end_ID ".format(text_vlan_range, vlanID_range, text_vlan_range)) |
| kate | 721d79b | 2017-06-24 04:21:38 -0700 | [diff] [blame] | 1806 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1807 | #NOT USED FUNCTIONS |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 1808 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1809 | def new_external_port(self, port_data): |
| 1810 | #TODO openstack if needed |
| 1811 | '''Adds a external port to VIM''' |
| 1812 | '''Returns the port identifier''' |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 1813 | return -vimconn.HTTP_Internal_Server_Error, "osconnector.new_external_port() not implemented" |
| 1814 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1815 | def connect_port_network(self, port_id, network_id, admin=False): |
| 1816 | #TODO openstack if needed |
| 1817 | '''Connects a external port to a network''' |
| 1818 | '''Returns status code of the VIM response''' |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 1819 | return -vimconn.HTTP_Internal_Server_Error, "osconnector.connect_port_network() not implemented" |
| 1820 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1821 | def new_user(self, user_name, user_passwd, tenant_id=None): |
| 1822 | '''Adds a new user to openstack VIM''' |
| 1823 | '''Returns the user identifier''' |
| 1824 | self.logger.debug("osconnector: Adding a new user to VIM") |
| 1825 | try: |
| 1826 | self._reload_connection() |
| Eduardo Sousa | e3c0dbc | 2018-09-03 11:56:07 +0100 | [diff] [blame] | 1827 | user=self.keystone.users.create(user_name, password=user_passwd, default_project=tenant_id) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1828 | #self.keystone.tenants.add_user(self.k_creds["username"], #role) |
| 1829 | return user.id |
| 1830 | except ksExceptions.ConnectionError as e: |
| 1831 | error_value=-vimconn.HTTP_Bad_Request |
| 1832 | error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0])) |
| 1833 | except ksExceptions.ClientException as e: #TODO remove |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1834 | error_value=-vimconn.HTTP_Bad_Request |
| 1835 | error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0])) |
| 1836 | #TODO insert exception vimconn.HTTP_Unauthorized |
| 1837 | #if reaching here is because an exception |
| tierno | 9c5c832 | 2018-03-23 15:44:03 +0100 | [diff] [blame] | 1838 | self.logger.debug("new_user " + error_text) |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 1839 | return error_value, error_text |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1840 | |
| 1841 | def delete_user(self, user_id): |
| 1842 | '''Delete a user from openstack VIM''' |
| 1843 | '''Returns the user identifier''' |
| 1844 | if self.debug: |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 1845 | print("osconnector: Deleting a user from VIM") |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1846 | try: |
| 1847 | self._reload_connection() |
| 1848 | self.keystone.users.delete(user_id) |
| 1849 | return 1, user_id |
| 1850 | except ksExceptions.ConnectionError as e: |
| 1851 | error_value=-vimconn.HTTP_Bad_Request |
| 1852 | error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0])) |
| 1853 | except ksExceptions.NotFound as e: |
| 1854 | error_value=-vimconn.HTTP_Not_Found |
| 1855 | error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0])) |
| 1856 | except ksExceptions.ClientException as e: #TODO remove |
| 1857 | error_value=-vimconn.HTTP_Bad_Request |
| 1858 | error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0])) |
| 1859 | #TODO insert exception vimconn.HTTP_Unauthorized |
| 1860 | #if reaching here is because an exception |
| tierno | 9c5c832 | 2018-03-23 15:44:03 +0100 | [diff] [blame] | 1861 | self.logger.debug("delete_tenant " + error_text) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1862 | return error_value, error_text |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 1863 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1864 | def get_hosts_info(self): |
| 1865 | '''Get the information of deployed hosts |
| 1866 | Returns the hosts content''' |
| 1867 | if self.debug: |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 1868 | print("osconnector: Getting Host info from VIM") |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1869 | try: |
| 1870 | h_list=[] |
| 1871 | self._reload_connection() |
| 1872 | hypervisors = self.nova.hypervisors.list() |
| 1873 | for hype in hypervisors: |
| 1874 | h_list.append( hype.to_dict() ) |
| 1875 | return 1, {"hosts":h_list} |
| 1876 | except nvExceptions.NotFound as e: |
| 1877 | error_value=-vimconn.HTTP_Not_Found |
| 1878 | error_text= (str(e) if len(e.args)==0 else str(e.args[0])) |
| 1879 | except (ksExceptions.ClientException, nvExceptions.ClientException) as e: |
| 1880 | error_value=-vimconn.HTTP_Bad_Request |
| 1881 | error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0])) |
| 1882 | #TODO insert exception vimconn.HTTP_Unauthorized |
| 1883 | #if reaching here is because an exception |
| tierno | 9c5c832 | 2018-03-23 15:44:03 +0100 | [diff] [blame] | 1884 | self.logger.debug("get_hosts_info " + error_text) |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 1885 | return error_value, error_text |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1886 | |
| 1887 | def get_hosts(self, vim_tenant): |
| 1888 | '''Get the hosts and deployed instances |
| 1889 | Returns the hosts content''' |
| 1890 | r, hype_dict = self.get_hosts_info() |
| 1891 | if r<0: |
| 1892 | return r, hype_dict |
| 1893 | hypervisors = hype_dict["hosts"] |
| 1894 | try: |
| 1895 | servers = self.nova.servers.list() |
| 1896 | for hype in hypervisors: |
| 1897 | for server in servers: |
| 1898 | if server.to_dict()['OS-EXT-SRV-ATTR:hypervisor_hostname']==hype['hypervisor_hostname']: |
| 1899 | if 'vm' in hype: |
| 1900 | hype['vm'].append(server.id) |
| 1901 | else: |
| 1902 | hype['vm'] = [server.id] |
| 1903 | return 1, hype_dict |
| 1904 | except nvExceptions.NotFound as e: |
| 1905 | error_value=-vimconn.HTTP_Not_Found |
| 1906 | error_text= (str(e) if len(e.args)==0 else str(e.args[0])) |
| 1907 | except (ksExceptions.ClientException, nvExceptions.ClientException) as e: |
| 1908 | error_value=-vimconn.HTTP_Bad_Request |
| 1909 | error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0])) |
| 1910 | #TODO insert exception vimconn.HTTP_Unauthorized |
| 1911 | #if reaching here is because an exception |
| tierno | 9c5c832 | 2018-03-23 15:44:03 +0100 | [diff] [blame] | 1912 | self.logger.debug("get_hosts " + error_text) |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 1913 | return error_value, error_text |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1914 | |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 1915 | def new_classification(self, name, ctype, definition): |
| 1916 | self.logger.debug( |
| 1917 | 'Adding a new (Traffic) Classification to VIM, named %s', name) |
| 1918 | try: |
| 1919 | new_class = None |
| 1920 | self._reload_connection() |
| 1921 | if ctype not in supportedClassificationTypes: |
| 1922 | raise vimconn.vimconnNotSupportedException( |
| 1923 | 'OpenStack VIM connector doesn\'t support provided ' |
| 1924 | 'Classification Type {}, supported ones are: ' |
| 1925 | '{}'.format(ctype, supportedClassificationTypes)) |
| 1926 | if not self._validate_classification(ctype, definition): |
| 1927 | raise vimconn.vimconnException( |
| 1928 | 'Incorrect Classification definition ' |
| 1929 | 'for the type specified.') |
| 1930 | classification_dict = definition |
| 1931 | classification_dict['name'] = name |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1932 | |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 1933 | new_class = self.neutron.create_sfc_flow_classifier( |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 1934 | {'flow_classifier': classification_dict}) |
| 1935 | return new_class['flow_classifier']['id'] |
| 1936 | except (neExceptions.ConnectionFailed, ksExceptions.ClientException, |
| 1937 | neExceptions.NeutronException, ConnectionError) as e: |
| 1938 | self.logger.error( |
| 1939 | 'Creation of Classification failed.') |
| 1940 | self._format_exception(e) |
| 1941 | |
| 1942 | def get_classification(self, class_id): |
| 1943 | self.logger.debug(" Getting Classification %s from VIM", class_id) |
| 1944 | filter_dict = {"id": class_id} |
| 1945 | class_list = self.get_classification_list(filter_dict) |
| 1946 | if len(class_list) == 0: |
| 1947 | raise vimconn.vimconnNotFoundException( |
| 1948 | "Classification '{}' not found".format(class_id)) |
| 1949 | elif len(class_list) > 1: |
| 1950 | raise vimconn.vimconnConflictException( |
| 1951 | "Found more than one Classification with this criteria") |
| 1952 | classification = class_list[0] |
| 1953 | return classification |
| 1954 | |
| 1955 | def get_classification_list(self, filter_dict={}): |
| 1956 | self.logger.debug("Getting Classifications from VIM filter: '%s'", |
| 1957 | str(filter_dict)) |
| 1958 | try: |
| tierno | 69b590e | 2018-03-13 18:52:23 +0100 | [diff] [blame] | 1959 | filter_dict_os = filter_dict.copy() |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 1960 | self._reload_connection() |
| tierno | 69b590e | 2018-03-13 18:52:23 +0100 | [diff] [blame] | 1961 | if self.api_version3 and "tenant_id" in filter_dict_os: |
| 1962 | filter_dict_os['project_id'] = filter_dict_os.pop('tenant_id') |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 1963 | classification_dict = self.neutron.list_sfc_flow_classifiers( |
| tierno | 69b590e | 2018-03-13 18:52:23 +0100 | [diff] [blame] | 1964 | **filter_dict_os) |
| 1965 | classification_list = classification_dict["flow_classifiers"] |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 1966 | self.__classification_os2mano(classification_list) |
| 1967 | return classification_list |
| 1968 | except (neExceptions.ConnectionFailed, ksExceptions.ClientException, |
| 1969 | neExceptions.NeutronException, ConnectionError) as e: |
| 1970 | self._format_exception(e) |
| 1971 | |
| 1972 | def delete_classification(self, class_id): |
| 1973 | self.logger.debug("Deleting Classification '%s' from VIM", class_id) |
| 1974 | try: |
| 1975 | self._reload_connection() |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 1976 | self.neutron.delete_sfc_flow_classifier(class_id) |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 1977 | return class_id |
| 1978 | except (neExceptions.ConnectionFailed, neExceptions.NeutronException, |
| 1979 | ksExceptions.ClientException, neExceptions.NeutronException, |
| 1980 | ConnectionError) as e: |
| 1981 | self._format_exception(e) |
| 1982 | |
| 1983 | def new_sfi(self, name, ingress_ports, egress_ports, sfc_encap=True): |
| 1984 | self.logger.debug( |
| 1985 | "Adding a new Service Function Instance to VIM, named '%s'", name) |
| 1986 | try: |
| 1987 | new_sfi = None |
| 1988 | self._reload_connection() |
| 1989 | correlation = None |
| 1990 | if sfc_encap: |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 1991 | correlation = 'nsh' |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 1992 | if len(ingress_ports) != 1: |
| 1993 | raise vimconn.vimconnNotSupportedException( |
| 1994 | "OpenStack VIM connector can only have " |
| 1995 | "1 ingress port per SFI") |
| 1996 | if len(egress_ports) != 1: |
| 1997 | raise vimconn.vimconnNotSupportedException( |
| 1998 | "OpenStack VIM connector can only have " |
| 1999 | "1 egress port per SFI") |
| 2000 | sfi_dict = {'name': name, |
| 2001 | 'ingress': ingress_ports[0], |
| 2002 | 'egress': egress_ports[0], |
| 2003 | 'service_function_parameters': { |
| 2004 | 'correlation': correlation}} |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2005 | new_sfi = self.neutron.create_sfc_port_pair({'port_pair': sfi_dict}) |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 2006 | return new_sfi['port_pair']['id'] |
| 2007 | except (neExceptions.ConnectionFailed, ksExceptions.ClientException, |
| 2008 | neExceptions.NeutronException, ConnectionError) as e: |
| 2009 | if new_sfi: |
| 2010 | try: |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2011 | self.neutron.delete_sfc_port_pair( |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 2012 | new_sfi['port_pair']['id']) |
| 2013 | except Exception: |
| 2014 | self.logger.error( |
| 2015 | 'Creation of Service Function Instance failed, with ' |
| 2016 | 'subsequent deletion failure as well.') |
| 2017 | self._format_exception(e) |
| 2018 | |
| 2019 | def get_sfi(self, sfi_id): |
| 2020 | self.logger.debug( |
| 2021 | 'Getting Service Function Instance %s from VIM', sfi_id) |
| 2022 | filter_dict = {"id": sfi_id} |
| 2023 | sfi_list = self.get_sfi_list(filter_dict) |
| 2024 | if len(sfi_list) == 0: |
| 2025 | raise vimconn.vimconnNotFoundException( |
| 2026 | "Service Function Instance '{}' not found".format(sfi_id)) |
| 2027 | elif len(sfi_list) > 1: |
| 2028 | raise vimconn.vimconnConflictException( |
| 2029 | 'Found more than one Service Function Instance ' |
| 2030 | 'with this criteria') |
| 2031 | sfi = sfi_list[0] |
| 2032 | return sfi |
| 2033 | |
| 2034 | def get_sfi_list(self, filter_dict={}): |
| 2035 | self.logger.debug("Getting Service Function Instances from " |
| 2036 | "VIM filter: '%s'", str(filter_dict)) |
| 2037 | try: |
| 2038 | self._reload_connection() |
| tierno | 69b590e | 2018-03-13 18:52:23 +0100 | [diff] [blame] | 2039 | filter_dict_os = filter_dict.copy() |
| 2040 | if self.api_version3 and "tenant_id" in filter_dict_os: |
| 2041 | filter_dict_os['project_id'] = filter_dict_os.pop('tenant_id') |
| 2042 | sfi_dict = self.neutron.list_sfc_port_pairs(**filter_dict_os) |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 2043 | sfi_list = sfi_dict["port_pairs"] |
| 2044 | self.__sfi_os2mano(sfi_list) |
| 2045 | return sfi_list |
| 2046 | except (neExceptions.ConnectionFailed, ksExceptions.ClientException, |
| 2047 | neExceptions.NeutronException, ConnectionError) as e: |
| 2048 | self._format_exception(e) |
| 2049 | |
| 2050 | def delete_sfi(self, sfi_id): |
| 2051 | self.logger.debug("Deleting Service Function Instance '%s' " |
| 2052 | "from VIM", sfi_id) |
| 2053 | try: |
| 2054 | self._reload_connection() |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2055 | self.neutron.delete_sfc_port_pair(sfi_id) |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 2056 | return sfi_id |
| 2057 | except (neExceptions.ConnectionFailed, neExceptions.NeutronException, |
| 2058 | ksExceptions.ClientException, neExceptions.NeutronException, |
| 2059 | ConnectionError) as e: |
| 2060 | self._format_exception(e) |
| 2061 | |
| 2062 | def new_sf(self, name, sfis, sfc_encap=True): |
| 2063 | self.logger.debug("Adding a new Service Function to VIM, " |
| 2064 | "named '%s'", name) |
| 2065 | try: |
| 2066 | new_sf = None |
| 2067 | self._reload_connection() |
| tierno | 9c5c832 | 2018-03-23 15:44:03 +0100 | [diff] [blame] | 2068 | # correlation = None |
| 2069 | # if sfc_encap: |
| 2070 | # correlation = 'nsh' |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 2071 | for instance in sfis: |
| 2072 | sfi = self.get_sfi(instance) |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2073 | if sfi.get('sfc_encap') != sfc_encap: |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 2074 | raise vimconn.vimconnNotSupportedException( |
| 2075 | "OpenStack VIM connector requires all SFIs of the " |
| 2076 | "same SF to share the same SFC Encapsulation") |
| 2077 | sf_dict = {'name': name, |
| 2078 | 'port_pairs': sfis} |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2079 | new_sf = self.neutron.create_sfc_port_pair_group({ |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 2080 | 'port_pair_group': sf_dict}) |
| 2081 | return new_sf['port_pair_group']['id'] |
| 2082 | except (neExceptions.ConnectionFailed, ksExceptions.ClientException, |
| 2083 | neExceptions.NeutronException, ConnectionError) as e: |
| 2084 | if new_sf: |
| 2085 | try: |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2086 | self.neutron.delete_sfc_port_pair_group( |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 2087 | new_sf['port_pair_group']['id']) |
| 2088 | except Exception: |
| 2089 | self.logger.error( |
| 2090 | 'Creation of Service Function failed, with ' |
| 2091 | 'subsequent deletion failure as well.') |
| 2092 | self._format_exception(e) |
| 2093 | |
| 2094 | def get_sf(self, sf_id): |
| 2095 | self.logger.debug("Getting Service Function %s from VIM", sf_id) |
| 2096 | filter_dict = {"id": sf_id} |
| 2097 | sf_list = self.get_sf_list(filter_dict) |
| 2098 | if len(sf_list) == 0: |
| 2099 | raise vimconn.vimconnNotFoundException( |
| 2100 | "Service Function '{}' not found".format(sf_id)) |
| 2101 | elif len(sf_list) > 1: |
| 2102 | raise vimconn.vimconnConflictException( |
| 2103 | "Found more than one Service Function with this criteria") |
| 2104 | sf = sf_list[0] |
| 2105 | return sf |
| 2106 | |
| 2107 | def get_sf_list(self, filter_dict={}): |
| 2108 | self.logger.debug("Getting Service Function from VIM filter: '%s'", |
| 2109 | str(filter_dict)) |
| 2110 | try: |
| 2111 | self._reload_connection() |
| tierno | 69b590e | 2018-03-13 18:52:23 +0100 | [diff] [blame] | 2112 | filter_dict_os = filter_dict.copy() |
| 2113 | if self.api_version3 and "tenant_id" in filter_dict_os: |
| 2114 | filter_dict_os['project_id'] = filter_dict_os.pop('tenant_id') |
| 2115 | 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] | 2116 | sf_list = sf_dict["port_pair_groups"] |
| 2117 | self.__sf_os2mano(sf_list) |
| 2118 | return sf_list |
| 2119 | except (neExceptions.ConnectionFailed, ksExceptions.ClientException, |
| 2120 | neExceptions.NeutronException, ConnectionError) as e: |
| 2121 | self._format_exception(e) |
| 2122 | |
| 2123 | def delete_sf(self, sf_id): |
| 2124 | self.logger.debug("Deleting Service Function '%s' from VIM", sf_id) |
| 2125 | try: |
| 2126 | self._reload_connection() |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2127 | self.neutron.delete_sfc_port_pair_group(sf_id) |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 2128 | return sf_id |
| 2129 | except (neExceptions.ConnectionFailed, neExceptions.NeutronException, |
| 2130 | ksExceptions.ClientException, neExceptions.NeutronException, |
| 2131 | ConnectionError) as e: |
| 2132 | self._format_exception(e) |
| 2133 | |
| 2134 | def new_sfp(self, name, classifications, sfs, sfc_encap=True, spi=None): |
| 2135 | self.logger.debug("Adding a new Service Function Path to VIM, " |
| 2136 | "named '%s'", name) |
| 2137 | try: |
| 2138 | new_sfp = None |
| 2139 | self._reload_connection() |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2140 | # In networking-sfc the MPLS encapsulation is legacy |
| 2141 | # should be used when no full SFC Encapsulation is intended |
| schillinge | 981df9a | 2019-01-24 09:25:11 +0100 | [diff] [blame] | 2142 | correlation = 'mpls' |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2143 | if sfc_encap: |
| 2144 | correlation = 'nsh' |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 2145 | sfp_dict = {'name': name, |
| 2146 | 'flow_classifiers': classifications, |
| 2147 | 'port_pair_groups': sfs, |
| 2148 | 'chain_parameters': {'correlation': correlation}} |
| 2149 | if spi: |
| 2150 | sfp_dict['chain_id'] = spi |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2151 | new_sfp = self.neutron.create_sfc_port_chain({'port_chain': sfp_dict}) |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 2152 | return new_sfp["port_chain"]["id"] |
| 2153 | except (neExceptions.ConnectionFailed, ksExceptions.ClientException, |
| 2154 | neExceptions.NeutronException, ConnectionError) as e: |
| 2155 | if new_sfp: |
| 2156 | try: |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2157 | self.neutron.delete_sfc_port_chain(new_sfp['port_chain']['id']) |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 2158 | except Exception: |
| 2159 | self.logger.error( |
| 2160 | 'Creation of Service Function Path failed, with ' |
| 2161 | 'subsequent deletion failure as well.') |
| 2162 | self._format_exception(e) |
| 2163 | |
| 2164 | def get_sfp(self, sfp_id): |
| 2165 | self.logger.debug(" Getting Service Function Path %s from VIM", sfp_id) |
| 2166 | filter_dict = {"id": sfp_id} |
| 2167 | sfp_list = self.get_sfp_list(filter_dict) |
| 2168 | if len(sfp_list) == 0: |
| 2169 | raise vimconn.vimconnNotFoundException( |
| 2170 | "Service Function Path '{}' not found".format(sfp_id)) |
| 2171 | elif len(sfp_list) > 1: |
| 2172 | raise vimconn.vimconnConflictException( |
| 2173 | "Found more than one Service Function Path with this criteria") |
| 2174 | sfp = sfp_list[0] |
| 2175 | return sfp |
| 2176 | |
| 2177 | def get_sfp_list(self, filter_dict={}): |
| 2178 | self.logger.debug("Getting Service Function Paths from VIM filter: " |
| 2179 | "'%s'", str(filter_dict)) |
| 2180 | try: |
| 2181 | self._reload_connection() |
| tierno | 69b590e | 2018-03-13 18:52:23 +0100 | [diff] [blame] | 2182 | filter_dict_os = filter_dict.copy() |
| 2183 | if self.api_version3 and "tenant_id" in filter_dict_os: |
| 2184 | filter_dict_os['project_id'] = filter_dict_os.pop('tenant_id') |
| 2185 | sfp_dict = self.neutron.list_sfc_port_chains(**filter_dict_os) |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 2186 | sfp_list = sfp_dict["port_chains"] |
| 2187 | self.__sfp_os2mano(sfp_list) |
| 2188 | return sfp_list |
| 2189 | except (neExceptions.ConnectionFailed, ksExceptions.ClientException, |
| 2190 | neExceptions.NeutronException, ConnectionError) as e: |
| 2191 | self._format_exception(e) |
| 2192 | |
| 2193 | def delete_sfp(self, sfp_id): |
| 2194 | self.logger.debug( |
| 2195 | "Deleting Service Function Path '%s' from VIM", sfp_id) |
| 2196 | try: |
| 2197 | self._reload_connection() |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2198 | self.neutron.delete_sfc_port_chain(sfp_id) |
| Igor Duarte Cardoso | 3cf9bcd | 2017-08-14 16:39:34 +0000 | [diff] [blame] | 2199 | return sfp_id |
| 2200 | except (neExceptions.ConnectionFailed, neExceptions.NeutronException, |
| 2201 | ksExceptions.ClientException, neExceptions.NeutronException, |
| 2202 | ConnectionError) as e: |
| 2203 | self._format_exception(e) |