blob: 15ef71336f4044681941cb5b25ab1a5d22d6d921 [file] [log] [blame]
tierno7edb6752016-03-21 17:37:52 +01001# -*- coding: utf-8 -*-
2
3##
tierno92021022018-09-12 16:29:23 +02004# Copyright 2015 Telefonica Investigacion y Desarrollo, S.A.U.
tierno7edb6752016-03-21 17:37:52 +01005# 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.
tierno7edb6752016-03-21 17:37:52 +010019##
20
21'''
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +000022osconnector implements all the methods to interact with openstack using the python-neutronclient.
23
24For the VNF forwarding graph, The OpenStack VIM connector calls the
25networking-sfc Neutron extension methods, whose resources are mapped
26to the VIM connector's SFC resources as follows:
27- Classification (OSM) -> Flow Classifier (Neutron)
28- Service Function Instance (OSM) -> Port Pair (Neutron)
29- Service Function (OSM) -> Port Pair Group (Neutron)
30- Service Function Path (OSM) -> Port Chain (Neutron)
tierno7edb6752016-03-21 17:37:52 +010031'''
Eduardo Sousae3c0dbc2018-09-03 11:56:07 +010032__author__ = "Alfonso Tierno, Gerardo Garcia, Pablo Montes, xFlow Research, Igor D.C., Eduardo Sousa"
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +000033__date__ = "$22-sep-2017 23:59:59$"
tierno7edb6752016-03-21 17:37:52 +010034
tierno7d782ef2019-10-04 12:56:31 +000035from osm_ro import vimconn
tierno69b590e2018-03-13 18:52:23 +010036# import json
tiernoae4a8d12016-07-08 12:30:39 +020037import logging
garciadeblas9f8456e2016-09-05 05:02:59 +020038import netaddr
montesmoreno0c8def02016-12-22 12:16:23 +000039import time
tierno36c0b172017-01-12 18:32:28 +010040import yaml
garciadeblas2299e3b2017-01-26 14:35:55 +000041import random
kate721d79b2017-06-24 04:21:38 -070042import re
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +000043import copy
Anderson Bravalheri0446cd52018-08-17 15:26:19 +010044from pprint import pformat
tierno7edb6752016-03-21 17:37:52 +010045
tiernob5cef372017-06-19 15:52:22 +020046from novaclient import client as nClient, exceptions as nvExceptions
47from keystoneauth1.identity import v2, v3
48from keystoneauth1 import session
tierno7edb6752016-03-21 17:37:52 +010049import keystoneclient.exceptions as ksExceptions
tiernof716aea2017-06-21 18:01:40 +020050import keystoneclient.v3.client as ksClient_v3
51import keystoneclient.v2_0.client as ksClient_v2
tiernob5cef372017-06-19 15:52:22 +020052from glanceclient import client as glClient
tierno7edb6752016-03-21 17:37:52 +010053import glanceclient.exc as gl1Exceptions
tiernob5cef372017-06-19 15:52:22 +020054from cinderclient import client as cClient
tierno7d782ef2019-10-04 12:56:31 +000055from http.client import HTTPException # TODO py3 check that this base exception matches python2 httplib.HTTPException
tiernob5cef372017-06-19 15:52:22 +020056from neutronclient.neutron import client as neClient
tierno7edb6752016-03-21 17:37:52 +010057from neutronclient.common import exceptions as neExceptions
58from requests.exceptions import ConnectionError
59
tierno40e1bce2017-08-09 09:12:04 +020060
61"""contain the openstack virtual machine status to openmano status"""
tierno7edb6752016-03-21 17:37:52 +010062vmStatus2manoFormat={'ACTIVE':'ACTIVE',
63 'PAUSED':'PAUSED',
64 'SUSPENDED': 'SUSPENDED',
65 'SHUTOFF':'INACTIVE',
66 'BUILD':'BUILD',
67 'ERROR':'ERROR','DELETED':'DELETED'
68 }
69netStatus2manoFormat={'ACTIVE':'ACTIVE','PAUSED':'PAUSED','INACTIVE':'INACTIVE','BUILD':'BUILD','ERROR':'ERROR','DELETED':'DELETED'
70 }
71
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +000072supportedClassificationTypes = ['legacy_flow_classifier']
73
montesmoreno0c8def02016-12-22 12:16:23 +000074#global var to have a timeout creating and deleting volumes
tierno00e3df72017-11-29 17:20:13 +010075volume_timeout = 600
76server_timeout = 600
montesmoreno0c8def02016-12-22 12:16:23 +000077
Anderson Bravalheri0446cd52018-08-17 15:26:19 +010078
79class SafeDumper(yaml.SafeDumper):
80 def represent_data(self, data):
81 # Openstack APIs use custom subclasses of dict and YAML safe dumper
82 # is designed to not handle that (reference issue 142 of pyyaml)
83 if isinstance(data, dict) and data.__class__ != dict:
84 # A simple solution is to convert those items back to dicts
85 data = dict(data.items())
86
87 return super(SafeDumper, self).represent_data(data)
88
89
tierno7edb6752016-03-21 17:37:52 +010090class vimconnector(vimconn.vimconnector):
tiernob3d36742017-03-03 23:51:05 +010091 def __init__(self, uuid, name, tenant_id, tenant_name, url, url_admin=None, user=None, passwd=None,
92 log_level=None, config={}, persistent_info={}):
ahmadsa96af9f42017-01-31 16:17:14 +050093 '''using common constructor parameters. In this case
tierno7edb6752016-03-21 17:37:52 +010094 'url' is the keystone authorization url,
95 'url_admin' is not use
96 '''
tiernof716aea2017-06-21 18:01:40 +020097 api_version = config.get('APIversion')
98 if api_version and api_version not in ('v3.3', 'v2.0', '2', '3'):
tiernob5cef372017-06-19 15:52:22 +020099 raise vimconn.vimconnException("Invalid value '{}' for config:APIversion. "
tiernof716aea2017-06-21 18:01:40 +0200100 "Allowed values are 'v3.3', 'v2.0', '2' or '3'".format(api_version))
kate721d79b2017-06-24 04:21:38 -0700101 vim_type = config.get('vim_type')
102 if vim_type and vim_type not in ('vio', 'VIO'):
103 raise vimconn.vimconnException("Invalid value '{}' for config:vim_type."
104 "Allowed values are 'vio' or 'VIO'".format(vim_type))
105
106 if config.get('dataplane_net_vlan_range') is not None:
107 #validate vlan ranges provided by user
garciadeblasebd66722019-01-31 16:01:31 +0000108 self._validate_vlan_ranges(config.get('dataplane_net_vlan_range'), 'dataplane_net_vlan_range')
109
110 if config.get('multisegment_vlan_range') is not None:
111 #validate vlan ranges provided by user
112 self._validate_vlan_ranges(config.get('multisegment_vlan_range'), 'multisegment_vlan_range')
kate721d79b2017-06-24 04:21:38 -0700113
tiernob5cef372017-06-19 15:52:22 +0200114 vimconn.vimconnector.__init__(self, uuid, name, tenant_id, tenant_name, url, url_admin, user, passwd, log_level,
115 config)
tiernob3d36742017-03-03 23:51:05 +0100116
tierno4d1ce222018-04-06 10:41:06 +0200117 if self.config.get("insecure") and self.config.get("ca_cert"):
118 raise vimconn.vimconnException("options insecure and ca_cert are mutually exclusive")
119 self.verify = True
120 if self.config.get("insecure"):
121 self.verify = False
122 if self.config.get("ca_cert"):
123 self.verify = self.config.get("ca_cert")
tierno4d1ce222018-04-06 10:41:06 +0200124
tierno7edb6752016-03-21 17:37:52 +0100125 if not url:
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +0000126 raise TypeError('url param can not be NoneType')
tiernob5cef372017-06-19 15:52:22 +0200127 self.persistent_info = persistent_info
mirabal29356312017-07-27 12:21:22 +0200128 self.availability_zone = persistent_info.get('availability_zone', None)
tiernob5cef372017-06-19 15:52:22 +0200129 self.session = persistent_info.get('session', {'reload_client': True})
tiernoa05b65a2019-02-01 12:30:27 +0000130 self.my_tenant_id = self.session.get('my_tenant_id')
tiernob5cef372017-06-19 15:52:22 +0200131 self.nova = self.session.get('nova')
132 self.neutron = self.session.get('neutron')
133 self.cinder = self.session.get('cinder')
134 self.glance = self.session.get('glance')
tierno1beea862018-07-11 15:47:37 +0200135 # self.glancev1 = self.session.get('glancev1')
tiernof716aea2017-06-21 18:01:40 +0200136 self.keystone = self.session.get('keystone')
137 self.api_version3 = self.session.get('api_version3')
kate721d79b2017-06-24 04:21:38 -0700138 self.vim_type = self.config.get("vim_type")
139 if self.vim_type:
140 self.vim_type = self.vim_type.upper()
141 if self.config.get("use_internal_endpoint"):
142 self.endpoint_type = "internalURL"
143 else:
144 self.endpoint_type = None
montesmoreno0c8def02016-12-22 12:16:23 +0000145
tierno73ad9e42016-09-12 18:11:11 +0200146 self.logger = logging.getLogger('openmano.vim.openstack')
kate721d79b2017-06-24 04:21:38 -0700147
tiernoa05b65a2019-02-01 12:30:27 +0000148 # allow security_groups to be a list or a single string
149 if isinstance(self.config.get('security_groups'), str):
150 self.config['security_groups'] = [self.config['security_groups']]
151 self.security_groups_id = None
152
kate721d79b2017-06-24 04:21:38 -0700153 ####### VIO Specific Changes #########
154 if self.vim_type == "VIO":
155 self.logger = logging.getLogger('openmano.vim.vio')
156
tiernofe789902016-09-29 14:20:44 +0000157 if log_level:
kate54616752017-09-05 23:26:28 -0700158 self.logger.setLevel( getattr(logging, log_level))
tiernof716aea2017-06-21 18:01:40 +0200159
160 def __getitem__(self, index):
161 """Get individuals parameters.
162 Throw KeyError"""
163 if index == 'project_domain_id':
164 return self.config.get("project_domain_id")
165 elif index == 'user_domain_id':
166 return self.config.get("user_domain_id")
167 else:
tierno76a3c312017-06-29 16:42:15 +0200168 return vimconn.vimconnector.__getitem__(self, index)
tiernof716aea2017-06-21 18:01:40 +0200169
170 def __setitem__(self, index, value):
171 """Set individuals parameters and it is marked as dirty so to force connection reload.
172 Throw KeyError"""
173 if index == 'project_domain_id':
174 self.config["project_domain_id"] = value
175 elif index == 'user_domain_id':
176 self.config["user_domain_id"] = value
177 else:
178 vimconn.vimconnector.__setitem__(self, index, value)
tiernob5cef372017-06-19 15:52:22 +0200179 self.session['reload_client'] = True
tiernof716aea2017-06-21 18:01:40 +0200180
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100181 def serialize(self, value):
182 """Serialization of python basic types.
183
184 In the case value is not serializable a message will be logged and a
185 simple representation of the data that cannot be converted back to
186 python is returned.
187 """
tierno7d782ef2019-10-04 12:56:31 +0000188 if isinstance(value, str):
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100189 return value
190
191 try:
192 return yaml.dump(value, Dumper=SafeDumper,
193 default_flow_style=True, width=256)
194 except yaml.representer.RepresenterError:
tierno7d782ef2019-10-04 12:56:31 +0000195 self.logger.debug('The following entity cannot be serialized in YAML:\n\n%s\n\n', pformat(value),
196 exc_info=True)
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100197 return str(value)
198
tierno7edb6752016-03-21 17:37:52 +0100199 def _reload_connection(self):
200 '''Called before any operation, it check if credentials has changed
201 Throw keystoneclient.apiclient.exceptions.AuthorizationFailure
202 '''
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100203 #TODO control the timing and possible token timeout, but it seams that python client does this task for us :-)
tiernob5cef372017-06-19 15:52:22 +0200204 if self.session['reload_client']:
tiernof716aea2017-06-21 18:01:40 +0200205 if self.config.get('APIversion'):
206 self.api_version3 = self.config['APIversion'] == 'v3.3' or self.config['APIversion'] == '3'
207 else: # get from ending auth_url that end with v3 or with v2.0
tierno3cb8dc32017-10-24 18:13:19 +0200208 self.api_version3 = self.url.endswith("/v3") or self.url.endswith("/v3/")
tiernof716aea2017-06-21 18:01:40 +0200209 self.session['api_version3'] = self.api_version3
210 if self.api_version3:
tierno3cb8dc32017-10-24 18:13:19 +0200211 if self.config.get('project_domain_id') or self.config.get('project_domain_name'):
212 project_domain_id_default = None
213 else:
214 project_domain_id_default = 'default'
215 if self.config.get('user_domain_id') or self.config.get('user_domain_name'):
216 user_domain_id_default = None
217 else:
218 user_domain_id_default = 'default'
tiernof716aea2017-06-21 18:01:40 +0200219 auth = v3.Password(auth_url=self.url,
tiernob5cef372017-06-19 15:52:22 +0200220 username=self.user,
221 password=self.passwd,
222 project_name=self.tenant_name,
223 project_id=self.tenant_id,
tierno3cb8dc32017-10-24 18:13:19 +0200224 project_domain_id=self.config.get('project_domain_id', project_domain_id_default),
225 user_domain_id=self.config.get('user_domain_id', user_domain_id_default),
226 project_domain_name=self.config.get('project_domain_name'),
227 user_domain_name=self.config.get('user_domain_name'))
ahmadsa95baa272016-11-30 09:14:11 +0500228 else:
tiernof716aea2017-06-21 18:01:40 +0200229 auth = v2.Password(auth_url=self.url,
tiernob5cef372017-06-19 15:52:22 +0200230 username=self.user,
231 password=self.passwd,
232 tenant_name=self.tenant_name,
233 tenant_id=self.tenant_id)
tierno4d1ce222018-04-06 10:41:06 +0200234 sess = session.Session(auth=auth, verify=self.verify)
fatollahy40c6a3f2019-02-19 12:53:40 +0000235 # addedd region_name to keystone, nova, neutron and cinder to support distributed cloud for Wind River Titanium cloud and StarlingX
236 region_name = self.config.get('region_name')
tiernof716aea2017-06-21 18:01:40 +0200237 if self.api_version3:
fatollahy40c6a3f2019-02-19 12:53:40 +0000238 self.keystone = ksClient_v3.Client(session=sess, endpoint_type=self.endpoint_type, region_name=region_name)
tiernof716aea2017-06-21 18:01:40 +0200239 else:
kate721d79b2017-06-24 04:21:38 -0700240 self.keystone = ksClient_v2.Client(session=sess, endpoint_type=self.endpoint_type)
tiernof716aea2017-06-21 18:01:40 +0200241 self.session['keystone'] = self.keystone
montesmoreno9317d302017-08-16 12:48:23 +0200242 # In order to enable microversion functionality an explicit microversion must be specified in 'config'.
243 # This implementation approach is due to the warning message in
244 # https://developer.openstack.org/api-guide/compute/microversions.html
245 # where it is stated that microversion backwards compatibility is not guaranteed and clients should
246 # always require an specific microversion.
247 # To be able to use 'device role tagging' functionality define 'microversion: 2.32' in datacenter config
248 version = self.config.get("microversion")
249 if not version:
250 version = "2.1"
fatollahy40c6a3f2019-02-19 12:53:40 +0000251 # addedd region_name to keystone, nova, neutron and cinder to support distributed cloud for Wind River Titanium cloud and StarlingX
252 self.nova = self.session['nova'] = nClient.Client(str(version), session=sess, endpoint_type=self.endpoint_type, region_name=region_name)
253 self.neutron = self.session['neutron'] = neClient.Client('2.0', session=sess, endpoint_type=self.endpoint_type, region_name=region_name)
254 self.cinder = self.session['cinder'] = cClient.Client(2, session=sess, endpoint_type=self.endpoint_type, region_name=region_name)
tiernoa05b65a2019-02-01 12:30:27 +0000255 try:
256 self.my_tenant_id = self.session['my_tenant_id'] = sess.get_project_id()
257 except Exception as e:
258 self.logger.error("Cannot get project_id from session", exc_info=True)
kate721d79b2017-06-24 04:21:38 -0700259 if self.endpoint_type == "internalURL":
260 glance_service_id = self.keystone.services.list(name="glance")[0].id
261 glance_endpoint = self.keystone.endpoints.list(glance_service_id, interface="internal")[0].url
262 else:
263 glance_endpoint = None
264 self.glance = self.session['glance'] = glClient.Client(2, session=sess, endpoint=glance_endpoint)
tiernoa05b65a2019-02-01 12:30:27 +0000265 # using version 1 of glance client in new_image()
tierno1beea862018-07-11 15:47:37 +0200266 # self.glancev1 = self.session['glancev1'] = glClient.Client('1', session=sess,
267 # endpoint=glance_endpoint)
tiernob5cef372017-06-19 15:52:22 +0200268 self.session['reload_client'] = False
269 self.persistent_info['session'] = self.session
mirabal29356312017-07-27 12:21:22 +0200270 # add availablity zone info inside self.persistent_info
271 self._set_availablity_zones()
272 self.persistent_info['availability_zone'] = self.availability_zone
tiernoa05b65a2019-02-01 12:30:27 +0000273 self.security_groups_id = None # force to get again security_groups_ids next time they are needed
ahmadsa95baa272016-11-30 09:14:11 +0500274
tierno7edb6752016-03-21 17:37:52 +0100275 def __net_os2mano(self, net_list_dict):
276 '''Transform the net openstack format to mano format
277 net_list_dict can be a list of dict or a single dict'''
278 if type(net_list_dict) is dict:
279 net_list_=(net_list_dict,)
280 elif type(net_list_dict) is list:
281 net_list_=net_list_dict
282 else:
283 raise TypeError("param net_list_dict must be a list or a dictionary")
284 for net in net_list_:
285 if net.get('provider:network_type') == "vlan":
286 net['type']='data'
287 else:
288 net['type']='bridge'
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +0200289
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +0000290 def __classification_os2mano(self, class_list_dict):
291 """Transform the openstack format (Flow Classifier) to mano format
292 (Classification) class_list_dict can be a list of dict or a single dict
293 """
294 if isinstance(class_list_dict, dict):
295 class_list_ = [class_list_dict]
296 elif isinstance(class_list_dict, list):
297 class_list_ = class_list_dict
298 else:
299 raise TypeError(
300 "param class_list_dict must be a list or a dictionary")
301 for classification in class_list_:
302 id = classification.pop('id')
303 name = classification.pop('name')
304 description = classification.pop('description')
305 project_id = classification.pop('project_id')
306 tenant_id = classification.pop('tenant_id')
307 original_classification = copy.deepcopy(classification)
308 classification.clear()
309 classification['ctype'] = 'legacy_flow_classifier'
310 classification['definition'] = original_classification
311 classification['id'] = id
312 classification['name'] = name
313 classification['description'] = description
314 classification['project_id'] = project_id
315 classification['tenant_id'] = tenant_id
316
317 def __sfi_os2mano(self, sfi_list_dict):
318 """Transform the openstack format (Port Pair) to mano format (SFI)
319 sfi_list_dict can be a list of dict or a single dict
320 """
321 if isinstance(sfi_list_dict, dict):
322 sfi_list_ = [sfi_list_dict]
323 elif isinstance(sfi_list_dict, list):
324 sfi_list_ = sfi_list_dict
325 else:
326 raise TypeError(
327 "param sfi_list_dict must be a list or a dictionary")
328 for sfi in sfi_list_:
329 sfi['ingress_ports'] = []
330 sfi['egress_ports'] = []
331 if sfi.get('ingress'):
332 sfi['ingress_ports'].append(sfi['ingress'])
333 if sfi.get('egress'):
334 sfi['egress_ports'].append(sfi['egress'])
335 del sfi['ingress']
336 del sfi['egress']
337 params = sfi.get('service_function_parameters')
338 sfc_encap = False
339 if params:
340 correlation = params.get('correlation')
341 if correlation:
342 sfc_encap = True
343 sfi['sfc_encap'] = sfc_encap
344 del sfi['service_function_parameters']
345
346 def __sf_os2mano(self, sf_list_dict):
347 """Transform the openstack format (Port Pair Group) to mano format (SF)
348 sf_list_dict can be a list of dict or a single dict
349 """
350 if isinstance(sf_list_dict, dict):
351 sf_list_ = [sf_list_dict]
352 elif isinstance(sf_list_dict, list):
353 sf_list_ = sf_list_dict
354 else:
355 raise TypeError(
356 "param sf_list_dict must be a list or a dictionary")
357 for sf in sf_list_:
358 del sf['port_pair_group_parameters']
359 sf['sfis'] = sf['port_pairs']
360 del sf['port_pairs']
361
362 def __sfp_os2mano(self, sfp_list_dict):
363 """Transform the openstack format (Port Chain) to mano format (SFP)
364 sfp_list_dict can be a list of dict or a single dict
365 """
366 if isinstance(sfp_list_dict, dict):
367 sfp_list_ = [sfp_list_dict]
368 elif isinstance(sfp_list_dict, list):
369 sfp_list_ = sfp_list_dict
370 else:
371 raise TypeError(
372 "param sfp_list_dict must be a list or a dictionary")
373 for sfp in sfp_list_:
374 params = sfp.pop('chain_parameters')
375 sfc_encap = False
376 if params:
377 correlation = params.get('correlation')
378 if correlation:
379 sfc_encap = True
380 sfp['sfc_encap'] = sfc_encap
381 sfp['spi'] = sfp.pop('chain_id')
382 sfp['classifications'] = sfp.pop('flow_classifiers')
383 sfp['service_functions'] = sfp.pop('port_pair_groups')
384
385 # placeholder for now; read TODO note below
386 def _validate_classification(self, type, definition):
387 # only legacy_flow_classifier Type is supported at this point
388 return True
389 # TODO(igordcard): this method should be an abstract method of an
390 # abstract Classification class to be implemented by the specific
391 # Types. Also, abstract vimconnector should call the validation
392 # method before the implemented VIM connectors are called.
393
tiernoae4a8d12016-07-08 12:30:39 +0200394 def _format_exception(self, exception):
395 '''Transform a keystone, nova, neutron exception into a vimconn exception'''
tiernode12f782019-04-05 12:46:42 +0000396
tiernode12f782019-04-05 12:46:42 +0000397 message_error = exception.message
tiernode12f782019-04-05 12:46:42 +0000398
399 if isinstance(exception, (neExceptions.NetworkNotFoundClient, nvExceptions.NotFound, ksExceptions.NotFound,
400 gl1Exceptions.HTTPNotFound)):
401 raise vimconn.vimconnNotFoundException(type(exception).__name__ + ": " + message_error)
shashankjain3c83a212018-10-04 13:05:46 +0530402 elif isinstance(exception, (HTTPException, gl1Exceptions.HTTPException, gl1Exceptions.CommunicationError,
403 ConnectionError, ksExceptions.ConnectionError, neExceptions.ConnectionFailed)):
tiernode12f782019-04-05 12:46:42 +0000404 raise vimconn.vimconnConnectionException(type(exception).__name__ + ": " + message_error)
shashankjain3c83a212018-10-04 13:05:46 +0530405 elif isinstance(exception, (KeyError, nvExceptions.BadRequest, ksExceptions.BadRequest)):
tiernode12f782019-04-05 12:46:42 +0000406 raise vimconn.vimconnException(type(exception).__name__ + ": " + message_error)
anwarsc76a3ee2018-10-04 14:05:32 +0530407 elif isinstance(exception, (nvExceptions.ClientException, ksExceptions.ClientException,
408 neExceptions.NeutronException)):
tiernode12f782019-04-05 12:46:42 +0000409 raise vimconn.vimconnUnexpectedResponse(type(exception).__name__ + ": " + message_error)
tiernoae4a8d12016-07-08 12:30:39 +0200410 elif isinstance(exception, nvExceptions.Conflict):
tiernode12f782019-04-05 12:46:42 +0000411 raise vimconn.vimconnConflictException(type(exception).__name__ + ": " + message_error)
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +0200412 elif isinstance(exception, vimconn.vimconnException):
tierno41a69812018-02-16 14:34:33 +0100413 raise exception
tiernof716aea2017-06-21 18:01:40 +0200414 else: # ()
tiernode12f782019-04-05 12:46:42 +0000415 self.logger.error("General Exception " + message_error, exc_info=True)
416 raise vimconn.vimconnConnectionException(type(exception).__name__ + ": " + message_error)
tiernoae4a8d12016-07-08 12:30:39 +0200417
tiernoa05b65a2019-02-01 12:30:27 +0000418 def _get_ids_from_name(self):
419 """
420 Obtain ids from name of tenant and security_groups. Store at self .security_groups_id"
421 :return: None
422 """
423 # get tenant_id if only tenant_name is supplied
424 self._reload_connection()
425 if not self.my_tenant_id:
426 raise vimconn.vimconnConnectionException("Error getting tenant information from name={} id={}".
427 format(self.tenant_name, self.tenant_id))
428 if self.config.get('security_groups') and not self.security_groups_id:
429 # convert from name to id
430 neutron_sg_list = self.neutron.list_security_groups(tenant_id=self.my_tenant_id)["security_groups"]
431
432 self.security_groups_id = []
433 for sg in self.config.get('security_groups'):
434 for neutron_sg in neutron_sg_list:
435 if sg in (neutron_sg["id"], neutron_sg["name"]):
436 self.security_groups_id.append(neutron_sg["id"])
437 break
438 else:
439 self.security_groups_id = None
440 raise vimconn.vimconnConnectionException("Not found security group {} for this tenant".format(sg))
441
tierno5509c2e2019-07-04 16:23:20 +0000442 def check_vim_connectivity(self):
443 # just get network list to check connectivity and credentials
444 self.get_network_list(filter_dict={})
445
tiernoae4a8d12016-07-08 12:30:39 +0200446 def get_tenant_list(self, filter_dict={}):
447 '''Obtain tenants of VIM
448 filter_dict can contain the following keys:
449 name: filter by tenant name
450 id: filter by tenant uuid/id
451 <other VIM specific>
452 Returns the tenant list of dictionaries: [{'name':'<name>, 'id':'<id>, ...}, ...]
453 '''
ahmadsa95baa272016-11-30 09:14:11 +0500454 self.logger.debug("Getting tenants from VIM filter: '%s'", str(filter_dict))
tiernoae4a8d12016-07-08 12:30:39 +0200455 try:
456 self._reload_connection()
tiernof716aea2017-06-21 18:01:40 +0200457 if self.api_version3:
458 project_class_list = self.keystone.projects.list(name=filter_dict.get("name"))
ahmadsa95baa272016-11-30 09:14:11 +0500459 else:
tiernof716aea2017-06-21 18:01:40 +0200460 project_class_list = self.keystone.tenants.findall(**filter_dict)
ahmadsa95baa272016-11-30 09:14:11 +0500461 project_list=[]
462 for project in project_class_list:
tiernof716aea2017-06-21 18:01:40 +0200463 if filter_dict.get('id') and filter_dict["id"] != project.id:
464 continue
ahmadsa95baa272016-11-30 09:14:11 +0500465 project_list.append(project.to_dict())
466 return project_list
tiernof716aea2017-06-21 18:01:40 +0200467 except (ksExceptions.ConnectionError, ksExceptions.ClientException, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200468 self._format_exception(e)
469
470 def new_tenant(self, tenant_name, tenant_description):
471 '''Adds a new tenant to openstack VIM. Returns the tenant identifier'''
472 self.logger.debug("Adding a new tenant name: %s", tenant_name)
473 try:
474 self._reload_connection()
tiernof716aea2017-06-21 18:01:40 +0200475 if self.api_version3:
476 project = self.keystone.projects.create(tenant_name, self.config.get("project_domain_id", "default"),
477 description=tenant_description, is_domain=False)
ahmadsa95baa272016-11-30 09:14:11 +0500478 else:
tiernof716aea2017-06-21 18:01:40 +0200479 project = self.keystone.tenants.create(tenant_name, tenant_description)
ahmadsa95baa272016-11-30 09:14:11 +0500480 return project.id
shashankjain3c83a212018-10-04 13:05:46 +0530481 except (ksExceptions.ConnectionError, ksExceptions.ClientException, ksExceptions.BadRequest, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200482 self._format_exception(e)
483
484 def delete_tenant(self, tenant_id):
485 '''Delete a tenant from openstack VIM. Returns the old tenant identifier'''
486 self.logger.debug("Deleting tenant %s from VIM", tenant_id)
487 try:
488 self._reload_connection()
tiernof716aea2017-06-21 18:01:40 +0200489 if self.api_version3:
ahmadsa95baa272016-11-30 09:14:11 +0500490 self.keystone.projects.delete(tenant_id)
491 else:
492 self.keystone.tenants.delete(tenant_id)
tiernoae4a8d12016-07-08 12:30:39 +0200493 return tenant_id
shashankjain3c83a212018-10-04 13:05:46 +0530494 except (ksExceptions.ConnectionError, ksExceptions.ClientException, ksExceptions.NotFound, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200495 self._format_exception(e)
ahmadsa95baa272016-11-30 09:14:11 +0500496
kbsuba85c54d2019-10-17 16:30:32 +0000497 def new_network(self,net_name, net_type, ip_profile=None, shared=False, provider_network_profile=None):
garciadeblasebd66722019-01-31 16:01:31 +0000498 """Adds a tenant network to VIM
499 Params:
500 'net_name': name of the network
501 'net_type': one of:
502 'bridge': overlay isolated network
503 'data': underlay E-LAN network for Passthrough and SRIOV interfaces
504 'ptp': underlay E-LINE network for Passthrough and SRIOV interfaces.
505 'ip_profile': is a dict containing the IP parameters of the network
506 'ip_version': can be "IPv4" or "IPv6" (Currently only IPv4 is implemented)
507 'subnet_address': ip_prefix_schema, that is X.X.X.X/Y
508 'gateway_address': (Optional) ip_schema, that is X.X.X.X
509 'dns_address': (Optional) comma separated list of ip_schema, e.g. X.X.X.X[,X,X,X,X]
510 'dhcp_enabled': True or False
511 'dhcp_start_address': ip_schema, first IP to grant
512 'dhcp_count': number of IPs to grant.
513 'shared': if this network can be seen/use by other tenants/organization
kbsuba85c54d2019-10-17 16:30:32 +0000514 'provider_network_profile': (optional) contains {segmentation-id: vlan, provider-network: vim_netowrk}
garciadeblasebd66722019-01-31 16:01:31 +0000515 Returns a tuple with the network identifier and created_items, or raises an exception on error
516 created_items can be None or a dictionary where this method can include key-values that will be passed to
517 the method delete_network. Can be used to store created segments, created l2gw connections, etc.
518 Format is vimconnector dependent, but do not use nested dictionaries and a value of None should be the same
519 as not present.
520 """
tiernoae4a8d12016-07-08 12:30:39 +0200521 self.logger.debug("Adding a new network to VIM name '%s', type '%s'", net_name, net_type)
garciadeblasebd66722019-01-31 16:01:31 +0000522 # self.logger.debug(">>>>>>>>>>>>>>>>>> IP profile %s", str(ip_profile))
kbsuba85c54d2019-10-17 16:30:32 +0000523
tierno7edb6752016-03-21 17:37:52 +0100524 try:
kbsuba85c54d2019-10-17 16:30:32 +0000525 vlan = None
526 if provider_network_profile:
527 vlan = provider_network_profile.get("segmentation-id")
garciadeblasedca7b32016-09-29 14:01:52 +0000528 new_net = None
garciadeblasebd66722019-01-31 16:01:31 +0000529 created_items = {}
tierno7edb6752016-03-21 17:37:52 +0100530 self._reload_connection()
531 network_dict = {'name': net_name, 'admin_state_up': True}
532 if net_type=="data" or net_type=="ptp":
533 if self.config.get('dataplane_physical_net') == None:
tiernoae4a8d12016-07-08 12:30:39 +0200534 raise vimconn.vimconnConflictException("You must provide a 'dataplane_physical_net' at config value before creating sriov network")
garciadeblasebd66722019-01-31 16:01:31 +0000535 if not self.config.get('multisegment_support'):
536 network_dict["provider:physical_network"] = self.config[
537 'dataplane_physical_net'] # "physnet_sriov" #TODO physical
538 network_dict["provider:network_type"] = "vlan"
539 if vlan!=None:
540 network_dict["provider:network_type"] = vlan
541 else:
542 ###### Multi-segment case ######
543 segment_list = []
544 segment1_dict = {}
545 segment1_dict["provider:physical_network"] = ''
546 segment1_dict["provider:network_type"] = 'vxlan'
547 segment_list.append(segment1_dict)
548 segment2_dict = {}
549 segment2_dict["provider:physical_network"] = self.config['dataplane_physical_net']
550 segment2_dict["provider:network_type"] = "vlan"
551 if self.config.get('multisegment_vlan_range'):
552 vlanID = self._generate_multisegment_vlanID()
553 segment2_dict["provider:segmentation_id"] = vlanID
554 # else
555 # raise vimconn.vimconnConflictException(
556 # "You must provide 'multisegment_vlan_range' at config dict before creating a multisegment network")
557 segment_list.append(segment2_dict)
558 network_dict["segments"] = segment_list
kate721d79b2017-06-24 04:21:38 -0700559
560 ####### VIO Specific Changes #########
561 if self.vim_type == "VIO":
562 if vlan is not None:
563 network_dict["provider:segmentation_id"] = vlan
564 else:
565 if self.config.get('dataplane_net_vlan_range') is None:
566 raise vimconn.vimconnConflictException("You must provide "\
567 "'dataplane_net_vlan_range' in format [start_ID - end_ID]"\
568 "at config value before creating sriov network with vlan tag")
569
garciadeblasebd66722019-01-31 16:01:31 +0000570 network_dict["provider:segmentation_id"] = self._generate_vlanID()
kate721d79b2017-06-24 04:21:38 -0700571
garciadeblasebd66722019-01-31 16:01:31 +0000572 network_dict["shared"] = shared
anwarsff168192019-05-06 11:23:07 +0530573 if self.config.get("disable_network_port_security"):
574 network_dict["port_security_enabled"] = False
garciadeblasebd66722019-01-31 16:01:31 +0000575 new_net = self.neutron.create_network({'network':network_dict})
576 # print new_net
577 # create subnetwork, even if there is no profile
garciadeblas9f8456e2016-09-05 05:02:59 +0200578 if not ip_profile:
579 ip_profile = {}
tierno41a69812018-02-16 14:34:33 +0100580 if not ip_profile.get('subnet_address'):
garciadeblas2299e3b2017-01-26 14:35:55 +0000581 #Fake subnet is required
582 subnet_rand = random.randint(0, 255)
583 ip_profile['subnet_address'] = "192.168.{}.0/24".format(subnet_rand)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +0000584 if 'ip_version' not in ip_profile:
garciadeblas9f8456e2016-09-05 05:02:59 +0200585 ip_profile['ip_version'] = "IPv4"
garciadeblasebd66722019-01-31 16:01:31 +0000586 subnet = {"name": net_name+"-subnet",
tierno7edb6752016-03-21 17:37:52 +0100587 "network_id": new_net["network"]["id"],
garciadeblas9f8456e2016-09-05 05:02:59 +0200588 "ip_version": 4 if ip_profile['ip_version']=="IPv4" else 6,
589 "cidr": ip_profile['subnet_address']
tierno7edb6752016-03-21 17:37:52 +0100590 }
tiernoa1fb4462017-06-30 12:25:50 +0200591 # Gateway should be set to None if not needed. Otherwise openstack assigns one by default
tierno41a69812018-02-16 14:34:33 +0100592 if ip_profile.get('gateway_address'):
tierno55d234c2018-07-04 18:29:21 +0200593 subnet['gateway_ip'] = ip_profile['gateway_address']
594 else:
595 subnet['gateway_ip'] = None
garciadeblasedca7b32016-09-29 14:01:52 +0000596 if ip_profile.get('dns_address'):
tierno455612d2017-05-30 16:40:10 +0200597 subnet['dns_nameservers'] = ip_profile['dns_address'].split(";")
garciadeblas9f8456e2016-09-05 05:02:59 +0200598 if 'dhcp_enabled' in ip_profile:
tierno41a69812018-02-16 14:34:33 +0100599 subnet['enable_dhcp'] = False if \
600 ip_profile['dhcp_enabled']=="false" or ip_profile['dhcp_enabled']==False else True
601 if ip_profile.get('dhcp_start_address'):
tiernoa1fb4462017-06-30 12:25:50 +0200602 subnet['allocation_pools'] = []
garciadeblas9f8456e2016-09-05 05:02:59 +0200603 subnet['allocation_pools'].append(dict())
604 subnet['allocation_pools'][0]['start'] = ip_profile['dhcp_start_address']
tierno41a69812018-02-16 14:34:33 +0100605 if ip_profile.get('dhcp_count'):
garciadeblas9f8456e2016-09-05 05:02:59 +0200606 #parts = ip_profile['dhcp_start_address'].split('.')
607 #ip_int = (int(parts[0]) << 24) + (int(parts[1]) << 16) + (int(parts[2]) << 8) + int(parts[3])
608 ip_int = int(netaddr.IPAddress(ip_profile['dhcp_start_address']))
garciadeblas21d795b2016-09-29 17:31:46 +0200609 ip_int += ip_profile['dhcp_count'] - 1
garciadeblas9f8456e2016-09-05 05:02:59 +0200610 ip_str = str(netaddr.IPAddress(ip_int))
611 subnet['allocation_pools'][0]['end'] = ip_str
garciadeblasedca7b32016-09-29 14:01:52 +0000612 #self.logger.debug(">>>>>>>>>>>>>>>>>> Subnet: %s", str(subnet))
tierno7edb6752016-03-21 17:37:52 +0100613 self.neutron.create_subnet({"subnet": subnet} )
garciadeblasebd66722019-01-31 16:01:31 +0000614
615 if net_type == "data" and self.config.get('multisegment_support'):
616 if self.config.get('l2gw_support'):
617 l2gw_list = self.neutron.list_l2_gateways().get("l2_gateways", ())
618 for l2gw in l2gw_list:
619 l2gw_conn = {}
620 l2gw_conn["l2_gateway_id"] = l2gw["id"]
621 l2gw_conn["network_id"] = new_net["network"]["id"]
622 l2gw_conn["segmentation_id"] = str(vlanID)
623 new_l2gw_conn = self.neutron.create_l2_gateway_connection({"l2_gateway_connection": l2gw_conn})
624 created_items["l2gwconn:" + str(new_l2gw_conn["l2_gateway_connection"]["id"])] = True
625 return new_net["network"]["id"], created_items
tierno41a69812018-02-16 14:34:33 +0100626 except Exception as e:
garciadeblasebd66722019-01-31 16:01:31 +0000627 #delete l2gw connections (if any) before deleting the network
628 for k, v in created_items.items():
629 if not v: # skip already deleted
630 continue
631 try:
632 k_item, _, k_id = k.partition(":")
633 if k_item == "l2gwconn":
634 self.neutron.delete_l2_gateway_connection(k_id)
635 except Exception as e2:
636 self.logger.error("Error deleting l2 gateway connection: {}: {}".format(type(e2).__name__, e2))
garciadeblasedca7b32016-09-29 14:01:52 +0000637 if new_net:
638 self.neutron.delete_network(new_net['network']['id'])
tiernoae4a8d12016-07-08 12:30:39 +0200639 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100640
641 def get_network_list(self, filter_dict={}):
642 '''Obtain tenant networks of VIM
643 Filter_dict can be:
644 name: network name
645 id: network uuid
646 shared: boolean
647 tenant_id: tenant
648 admin_state_up: boolean
649 status: 'ACTIVE'
650 Returns the network list of dictionaries
651 '''
tiernoae4a8d12016-07-08 12:30:39 +0200652 self.logger.debug("Getting network from VIM filter: '%s'", str(filter_dict))
tierno7edb6752016-03-21 17:37:52 +0100653 try:
654 self._reload_connection()
tierno69b590e2018-03-13 18:52:23 +0100655 filter_dict_os = filter_dict.copy()
656 if self.api_version3 and "tenant_id" in filter_dict_os:
657 filter_dict_os['project_id'] = filter_dict_os.pop('tenant_id') #T ODO check
658 net_dict = self.neutron.list_networks(**filter_dict_os)
tierno00e3df72017-11-29 17:20:13 +0100659 net_list = net_dict["networks"]
tierno7edb6752016-03-21 17:37:52 +0100660 self.__net_os2mano(net_list)
tiernoae4a8d12016-07-08 12:30:39 +0200661 return net_list
tierno8e995ce2016-09-22 08:13:00 +0000662 except (neExceptions.ConnectionFailed, ksExceptions.ClientException, neExceptions.NeutronException, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200663 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100664
tiernoae4a8d12016-07-08 12:30:39 +0200665 def get_network(self, net_id):
666 '''Obtain details of network from VIM
667 Returns the network information from a network id'''
668 self.logger.debug(" Getting tenant network %s from VIM", net_id)
tierno7edb6752016-03-21 17:37:52 +0100669 filter_dict={"id": net_id}
tiernoae4a8d12016-07-08 12:30:39 +0200670 net_list = self.get_network_list(filter_dict)
tierno7edb6752016-03-21 17:37:52 +0100671 if len(net_list)==0:
tiernoae4a8d12016-07-08 12:30:39 +0200672 raise vimconn.vimconnNotFoundException("Network '{}' not found".format(net_id))
tierno7edb6752016-03-21 17:37:52 +0100673 elif len(net_list)>1:
tiernoae4a8d12016-07-08 12:30:39 +0200674 raise vimconn.vimconnConflictException("Found more than one network with this criteria")
tierno7edb6752016-03-21 17:37:52 +0100675 net = net_list[0]
676 subnets=[]
677 for subnet_id in net.get("subnets", () ):
678 try:
679 subnet = self.neutron.show_subnet(subnet_id)
680 except Exception as e:
tiernoae4a8d12016-07-08 12:30:39 +0200681 self.logger.error("osconnector.get_network(): Error getting subnet %s %s" % (net_id, str(e)))
682 subnet = {"id": subnet_id, "fault": str(e)}
tierno7edb6752016-03-21 17:37:52 +0100683 subnets.append(subnet)
684 net["subnets"] = subnets
Pablo Montes Moreno51e553b2017-03-23 16:39:12 +0100685 net["encapsulation"] = net.get('provider:network_type')
Anderson Bravalheri0fb70282018-12-16 19:28:37 +0000686 net["encapsulation_type"] = net.get('provider:network_type')
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +0100687 net["segmentation_id"] = net.get('provider:segmentation_id')
Anderson Bravalheri0fb70282018-12-16 19:28:37 +0000688 net["encapsulation_id"] = net.get('provider:segmentation_id')
tiernoae4a8d12016-07-08 12:30:39 +0200689 return net
tierno7edb6752016-03-21 17:37:52 +0100690
garciadeblasebd66722019-01-31 16:01:31 +0000691 def delete_network(self, net_id, created_items=None):
692 """
693 Removes a tenant network from VIM and its associated elements
694 :param net_id: VIM identifier of the network, provided by method new_network
695 :param created_items: dictionary with extra items to be deleted. provided by method new_network
696 Returns the network identifier or raises an exception upon error or when network is not found
697 """
tiernoae4a8d12016-07-08 12:30:39 +0200698 self.logger.debug("Deleting network '%s' from VIM", net_id)
garciadeblasebd66722019-01-31 16:01:31 +0000699 if created_items == None:
700 created_items = {}
tierno7edb6752016-03-21 17:37:52 +0100701 try:
702 self._reload_connection()
garciadeblasebd66722019-01-31 16:01:31 +0000703 #delete l2gw connections (if any) before deleting the network
704 for k, v in created_items.items():
705 if not v: # skip already deleted
706 continue
707 try:
708 k_item, _, k_id = k.partition(":")
709 if k_item == "l2gwconn":
710 self.neutron.delete_l2_gateway_connection(k_id)
711 except Exception as e:
712 self.logger.error("Error deleting l2 gateway connection: {}: {}".format(type(e).__name__, e))
tierno7edb6752016-03-21 17:37:52 +0100713 #delete VM ports attached to this networks before the network
714 ports = self.neutron.list_ports(network_id=net_id)
715 for p in ports['ports']:
716 try:
717 self.neutron.delete_port(p["id"])
718 except Exception as e:
tiernoae4a8d12016-07-08 12:30:39 +0200719 self.logger.error("Error deleting port %s: %s", p["id"], str(e))
tierno7edb6752016-03-21 17:37:52 +0100720 self.neutron.delete_network(net_id)
tiernoae4a8d12016-07-08 12:30:39 +0200721 return net_id
722 except (neExceptions.ConnectionFailed, neExceptions.NetworkNotFoundClient, neExceptions.NeutronException,
tierno8e995ce2016-09-22 08:13:00 +0000723 ksExceptions.ClientException, neExceptions.NeutronException, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200724 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100725
tiernoae4a8d12016-07-08 12:30:39 +0200726 def refresh_nets_status(self, net_list):
727 '''Get the status of the networks
728 Params: the list of network identifiers
729 Returns a dictionary with:
730 net_id: #VIM id of this network
731 status: #Mandatory. Text with one of:
732 # DELETED (not found at vim)
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100733 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
tiernoae4a8d12016-07-08 12:30:39 +0200734 # OTHER (Vim reported other status not understood)
735 # ERROR (VIM indicates an ERROR status)
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100736 # ACTIVE, INACTIVE, DOWN (admin down),
tiernoae4a8d12016-07-08 12:30:39 +0200737 # BUILD (on building process)
738 #
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100739 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
tiernoae4a8d12016-07-08 12:30:39 +0200740 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
741
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +0000742 '''
tiernoae4a8d12016-07-08 12:30:39 +0200743 net_dict={}
744 for net_id in net_list:
745 net = {}
746 try:
747 net_vim = self.get_network(net_id)
748 if net_vim['status'] in netStatus2manoFormat:
749 net["status"] = netStatus2manoFormat[ net_vim['status'] ]
750 else:
751 net["status"] = "OTHER"
752 net["error_msg"] = "VIM status reported " + net_vim['status']
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +0000753
tierno8e995ce2016-09-22 08:13:00 +0000754 if net['status'] == "ACTIVE" and not net_vim['admin_state_up']:
tiernoae4a8d12016-07-08 12:30:39 +0200755 net['status'] = 'DOWN'
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100756
757 net['vim_info'] = self.serialize(net_vim)
758
tiernoae4a8d12016-07-08 12:30:39 +0200759 if net_vim.get('fault'): #TODO
760 net['error_msg'] = str(net_vim['fault'])
761 except vimconn.vimconnNotFoundException as e:
762 self.logger.error("Exception getting net status: %s", str(e))
763 net['status'] = "DELETED"
764 net['error_msg'] = str(e)
765 except vimconn.vimconnException as e:
766 self.logger.error("Exception getting net status: %s", str(e))
767 net['status'] = "VIM_ERROR"
768 net['error_msg'] = str(e)
769 net_dict[net_id] = net
770 return net_dict
771
772 def get_flavor(self, flavor_id):
773 '''Obtain flavor details from the VIM. Returns the flavor dict details'''
774 self.logger.debug("Getting flavor '%s'", flavor_id)
tierno7edb6752016-03-21 17:37:52 +0100775 try:
776 self._reload_connection()
777 flavor = self.nova.flavors.find(id=flavor_id)
778 #TODO parse input and translate to VIM format (openmano_schemas.new_vminstance_response_schema)
tiernoae4a8d12016-07-08 12:30:39 +0200779 return flavor.to_dict()
tierno8e995ce2016-09-22 08:13:00 +0000780 except (nvExceptions.NotFound, nvExceptions.ClientException, ksExceptions.ClientException, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200781 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100782
tiernocf157a82017-01-30 14:07:06 +0100783 def get_flavor_id_from_data(self, flavor_dict):
784 """Obtain flavor id that match the flavor description
785 Returns the flavor_id or raises a vimconnNotFoundException
tiernoe26fc7a2017-05-30 14:43:03 +0200786 flavor_dict: contains the required ram, vcpus, disk
787 If 'use_existing_flavors' is set to True at config, the closer flavor that provides same or more ram, vcpus
788 and disk is returned. Otherwise a flavor with exactly same ram, vcpus and disk is returned or a
789 vimconnNotFoundException is raised
tiernocf157a82017-01-30 14:07:06 +0100790 """
tiernoe26fc7a2017-05-30 14:43:03 +0200791 exact_match = False if self.config.get('use_existing_flavors') else True
tiernocf157a82017-01-30 14:07:06 +0100792 try:
793 self._reload_connection()
tiernoe26fc7a2017-05-30 14:43:03 +0200794 flavor_candidate_id = None
795 flavor_candidate_data = (10000, 10000, 10000)
796 flavor_target = (flavor_dict["ram"], flavor_dict["vcpus"], flavor_dict["disk"])
797 # numa=None
anwarsae5f52c2019-04-22 10:35:27 +0530798 extended = flavor_dict.get("extended", {})
799 if extended:
tiernocf157a82017-01-30 14:07:06 +0100800 #TODO
tiernob7aa1bb2019-07-24 15:47:16 +0000801 raise vimconn.vimconnNotFoundException("Flavor with EPA still not implemented")
tiernocf157a82017-01-30 14:07:06 +0100802 # if len(numas) > 1:
803 # raise vimconn.vimconnNotFoundException("Cannot find any flavor with more than one numa")
804 # numa=numas[0]
805 # numas = extended.get("numas")
806 for flavor in self.nova.flavors.list():
807 epa = flavor.get_keys()
808 if epa:
809 continue
tiernoe26fc7a2017-05-30 14:43:03 +0200810 # TODO
811 flavor_data = (flavor.ram, flavor.vcpus, flavor.disk)
812 if flavor_data == flavor_target:
813 return flavor.id
814 elif not exact_match and flavor_target < flavor_data < flavor_candidate_data:
815 flavor_candidate_id = flavor.id
816 flavor_candidate_data = flavor_data
817 if not exact_match and flavor_candidate_id:
818 return flavor_candidate_id
tiernocf157a82017-01-30 14:07:06 +0100819 raise vimconn.vimconnNotFoundException("Cannot find any flavor matching '{}'".format(str(flavor_dict)))
820 except (nvExceptions.NotFound, nvExceptions.ClientException, ksExceptions.ClientException, ConnectionError) as e:
821 self._format_exception(e)
822
anwarsae5f52c2019-04-22 10:35:27 +0530823 def process_resource_quota(self, quota, prefix, extra_specs):
824 """
825 :param prefix:
826 :param extra_specs:
827 :return:
828 """
829 if 'limit' in quota:
830 extra_specs["quota:" + prefix + "_limit"] = quota['limit']
831 if 'reserve' in quota:
832 extra_specs["quota:" + prefix + "_reservation"] = quota['reserve']
833 if 'shares' in quota:
834 extra_specs["quota:" + prefix + "_shares_level"] = "custom"
835 extra_specs["quota:" + prefix + "_shares_share"] = quota['shares']
836
tiernoae4a8d12016-07-08 12:30:39 +0200837 def new_flavor(self, flavor_data, change_name_if_used=True):
tierno7edb6752016-03-21 17:37:52 +0100838 '''Adds a tenant flavor to openstack VIM
tiernoae4a8d12016-07-08 12:30:39 +0200839 if change_name_if_used is True, it will change name in case of conflict, because it is not supported name repetition
tierno7edb6752016-03-21 17:37:52 +0100840 Returns the flavor identifier
841 '''
tiernoae4a8d12016-07-08 12:30:39 +0200842 self.logger.debug("Adding flavor '%s'", str(flavor_data))
tierno7edb6752016-03-21 17:37:52 +0100843 retry=0
tiernoae4a8d12016-07-08 12:30:39 +0200844 max_retries=3
tierno7edb6752016-03-21 17:37:52 +0100845 name_suffix = 0
anwarsc76a3ee2018-10-04 14:05:32 +0530846 try:
847 name=flavor_data['name']
848 while retry<max_retries:
849 retry+=1
850 try:
851 self._reload_connection()
852 if change_name_if_used:
853 #get used names
854 fl_names=[]
855 fl=self.nova.flavors.list()
856 for f in fl:
857 fl_names.append(f.name)
858 while name in fl_names:
859 name_suffix += 1
860 name = flavor_data['name']+"-" + str(name_suffix)
kate721d79b2017-06-24 04:21:38 -0700861
anwarsc76a3ee2018-10-04 14:05:32 +0530862 ram = flavor_data.get('ram',64)
863 vcpus = flavor_data.get('vcpus',1)
anwarsae5f52c2019-04-22 10:35:27 +0530864 extra_specs={}
tierno7edb6752016-03-21 17:37:52 +0100865
anwarsc76a3ee2018-10-04 14:05:32 +0530866 extended = flavor_data.get("extended")
867 if extended:
868 numas=extended.get("numas")
869 if numas:
870 numa_nodes = len(numas)
871 if numa_nodes > 1:
872 return -1, "Can not add flavor with more than one numa"
anwarsae5f52c2019-04-22 10:35:27 +0530873 extra_specs["hw:numa_nodes"] = str(numa_nodes)
874 extra_specs["hw:mem_page_size"] = "large"
875 extra_specs["hw:cpu_policy"] = "dedicated"
876 extra_specs["hw:numa_mempolicy"] = "strict"
anwarsc76a3ee2018-10-04 14:05:32 +0530877 if self.vim_type == "VIO":
anwarsae5f52c2019-04-22 10:35:27 +0530878 extra_specs["vmware:extra_config"] = '{"numa.nodeAffinity":"0"}'
879 extra_specs["vmware:latency_sensitivity_level"] = "high"
anwarsc76a3ee2018-10-04 14:05:32 +0530880 for numa in numas:
881 #overwrite ram and vcpus
882 #check if key 'memory' is present in numa else use ram value at flavor
883 if 'memory' in numa:
884 ram = numa['memory']*1024
885 #See for reference: https://specs.openstack.org/openstack/nova-specs/specs/mitaka/implemented/virt-driver-cpu-thread-pinning.html
garciadeblasfa35a722019-04-11 19:15:49 +0200886 extra_specs["hw:cpu_sockets"] = 1
anwarsc76a3ee2018-10-04 14:05:32 +0530887 if 'paired-threads' in numa:
888 vcpus = numa['paired-threads']*2
889 #cpu_thread_policy "require" implies that the compute node must have an STM architecture
anwarsae5f52c2019-04-22 10:35:27 +0530890 extra_specs["hw:cpu_thread_policy"] = "require"
891 extra_specs["hw:cpu_policy"] = "dedicated"
anwarsc76a3ee2018-10-04 14:05:32 +0530892 elif 'cores' in numa:
893 vcpus = numa['cores']
894 # cpu_thread_policy "prefer" implies that the host must not have an SMT architecture, or a non-SMT architecture will be emulated
anwarsae5f52c2019-04-22 10:35:27 +0530895 extra_specs["hw:cpu_thread_policy"] = "isolate"
896 extra_specs["hw:cpu_policy"] = "dedicated"
anwarsc76a3ee2018-10-04 14:05:32 +0530897 elif 'threads' in numa:
898 vcpus = numa['threads']
899 # cpu_thread_policy "prefer" implies that the host may or may not have an SMT architecture
anwarsae5f52c2019-04-22 10:35:27 +0530900 extra_specs["hw:cpu_thread_policy"] = "prefer"
901 extra_specs["hw:cpu_policy"] = "dedicated"
anwarsc76a3ee2018-10-04 14:05:32 +0530902 # for interface in numa.get("interfaces",() ):
903 # if interface["dedicated"]=="yes":
904 # raise vimconn.vimconnException("Passthrough interfaces are not supported for the openstack connector", http_code=vimconn.HTTP_Service_Unavailable)
905 # #TODO, add the key 'pci_passthrough:alias"="<label at config>:<number ifaces>"' when a way to connect it is available
anwarsae5f52c2019-04-22 10:35:27 +0530906 elif extended.get("cpu-quota"):
907 self.process_resource_quota(extended.get("cpu-quota"), "cpu", extra_specs)
908 if extended.get("mem-quota"):
909 self.process_resource_quota(extended.get("mem-quota"), "memory", extra_specs)
910 if extended.get("vif-quota"):
911 self.process_resource_quota(extended.get("vif-quota"), "vif", extra_specs)
912 if extended.get("disk-io-quota"):
913 self.process_resource_quota(extended.get("disk-io-quota"), "disk_io", extra_specs)
anwarsc76a3ee2018-10-04 14:05:32 +0530914 #create flavor
915 new_flavor=self.nova.flavors.create(name,
916 ram,
917 vcpus,
918 flavor_data.get('disk',0),
919 is_public=flavor_data.get('is_public', True)
920 )
921 #add metadata
anwarsae5f52c2019-04-22 10:35:27 +0530922 if extra_specs:
923 new_flavor.set_keys(extra_specs)
anwarsc76a3ee2018-10-04 14:05:32 +0530924 return new_flavor.id
925 except nvExceptions.Conflict as e:
926 if change_name_if_used and retry < max_retries:
927 continue
928 self._format_exception(e)
929 #except nvExceptions.BadRequest as e:
930 except (ksExceptions.ClientException, nvExceptions.ClientException, ConnectionError, KeyError) as e:
931 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100932
tiernoae4a8d12016-07-08 12:30:39 +0200933 def delete_flavor(self,flavor_id):
934 '''Deletes a tenant flavor from openstack VIM. Returns the old flavor_id
tierno7edb6752016-03-21 17:37:52 +0100935 '''
tiernoae4a8d12016-07-08 12:30:39 +0200936 try:
937 self._reload_connection()
938 self.nova.flavors.delete(flavor_id)
939 return flavor_id
940 #except nvExceptions.BadRequest as e:
tierno8e995ce2016-09-22 08:13:00 +0000941 except (nvExceptions.NotFound, ksExceptions.ClientException, nvExceptions.ClientException, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200942 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100943
tiernoae4a8d12016-07-08 12:30:39 +0200944 def new_image(self,image_dict):
tierno7edb6752016-03-21 17:37:52 +0100945 '''
tiernoae4a8d12016-07-08 12:30:39 +0200946 Adds a tenant image to VIM. imge_dict is a dictionary with:
947 name: name
948 disk_format: qcow2, vhd, vmdk, raw (by default), ...
949 location: path or URI
950 public: "yes" or "no"
951 metadata: metadata of the image
952 Returns the image_id
tierno7edb6752016-03-21 17:37:52 +0100953 '''
tiernoae4a8d12016-07-08 12:30:39 +0200954 retry=0
955 max_retries=3
956 while retry<max_retries:
tierno7edb6752016-03-21 17:37:52 +0100957 retry+=1
958 try:
959 self._reload_connection()
960 #determine format http://docs.openstack.org/developer/glance/formats.html
961 if "disk_format" in image_dict:
962 disk_format=image_dict["disk_format"]
garciadeblas14480452017-01-10 13:08:07 +0100963 else: #autodiscover based on extension
tierno1beea862018-07-11 15:47:37 +0200964 if image_dict['location'].endswith(".qcow2"):
tierno7edb6752016-03-21 17:37:52 +0100965 disk_format="qcow2"
tierno1beea862018-07-11 15:47:37 +0200966 elif image_dict['location'].endswith(".vhd"):
tierno7edb6752016-03-21 17:37:52 +0100967 disk_format="vhd"
tierno1beea862018-07-11 15:47:37 +0200968 elif image_dict['location'].endswith(".vmdk"):
tierno7edb6752016-03-21 17:37:52 +0100969 disk_format="vmdk"
tierno1beea862018-07-11 15:47:37 +0200970 elif image_dict['location'].endswith(".vdi"):
tierno7edb6752016-03-21 17:37:52 +0100971 disk_format="vdi"
tierno1beea862018-07-11 15:47:37 +0200972 elif image_dict['location'].endswith(".iso"):
tierno7edb6752016-03-21 17:37:52 +0100973 disk_format="iso"
tierno1beea862018-07-11 15:47:37 +0200974 elif image_dict['location'].endswith(".aki"):
tierno7edb6752016-03-21 17:37:52 +0100975 disk_format="aki"
tierno1beea862018-07-11 15:47:37 +0200976 elif image_dict['location'].endswith(".ari"):
tierno7edb6752016-03-21 17:37:52 +0100977 disk_format="ari"
tierno1beea862018-07-11 15:47:37 +0200978 elif image_dict['location'].endswith(".ami"):
tierno7edb6752016-03-21 17:37:52 +0100979 disk_format="ami"
980 else:
981 disk_format="raw"
tiernoae4a8d12016-07-08 12:30:39 +0200982 self.logger.debug("new_image: '%s' loading from '%s'", image_dict['name'], image_dict['location'])
shashankjain3c83a212018-10-04 13:05:46 +0530983 if self.vim_type == "VIO":
984 container_format = "bare"
985 if 'container_format' in image_dict:
986 container_format = image_dict['container_format']
987 new_image = self.glance.images.create(name=image_dict['name'], container_format=container_format,
988 disk_format=disk_format)
989 else:
990 new_image = self.glance.images.create(name=image_dict['name'])
tierno1beea862018-07-11 15:47:37 +0200991 if image_dict['location'].startswith("http"):
992 # TODO there is not a method to direct download. It must be downloaded locally with requests
993 raise vimconn.vimconnNotImplemented("Cannot create image from URL")
tierno7edb6752016-03-21 17:37:52 +0100994 else: #local path
995 with open(image_dict['location']) as fimage:
tierno1beea862018-07-11 15:47:37 +0200996 self.glance.images.upload(new_image.id, fimage)
997 #new_image = self.glancev1.images.create(name=image_dict['name'], is_public=image_dict.get('public',"yes")=="yes",
998 # container_format="bare", data=fimage, disk_format=disk_format)
tierno7edb6752016-03-21 17:37:52 +0100999 metadata_to_load = image_dict.get('metadata')
shashankjain3c83a212018-10-04 13:05:46 +05301000 # TODO location is a reserved word for current openstack versions. fixed for VIO please check for openstack
1001 if self.vim_type == "VIO":
1002 metadata_to_load['upload_location'] = image_dict['location']
1003 else:
1004 metadata_to_load['location'] = image_dict['location']
tierno1beea862018-07-11 15:47:37 +02001005 self.glance.images.update(new_image.id, **metadata_to_load)
tiernoae4a8d12016-07-08 12:30:39 +02001006 return new_image.id
1007 except (nvExceptions.Conflict, ksExceptions.ClientException, nvExceptions.ClientException) as e:
1008 self._format_exception(e)
tierno8e995ce2016-09-22 08:13:00 +00001009 except (HTTPException, gl1Exceptions.HTTPException, gl1Exceptions.CommunicationError, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +02001010 if retry==max_retries:
1011 continue
1012 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +01001013 except IOError as e: #can not open the file
tiernoae4a8d12016-07-08 12:30:39 +02001014 raise vimconn.vimconnConnectionException(type(e).__name__ + ": " + str(e)+ " for " + image_dict['location'],
1015 http_code=vimconn.HTTP_Bad_Request)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001016
tiernoae4a8d12016-07-08 12:30:39 +02001017 def delete_image(self, image_id):
1018 '''Deletes a tenant image from openstack VIM. Returns the old id
tierno7edb6752016-03-21 17:37:52 +01001019 '''
tiernoae4a8d12016-07-08 12:30:39 +02001020 try:
1021 self._reload_connection()
tierno1beea862018-07-11 15:47:37 +02001022 self.glance.images.delete(image_id)
tiernoae4a8d12016-07-08 12:30:39 +02001023 return image_id
shashankjain3c83a212018-10-04 13:05:46 +05301024 except (nvExceptions.NotFound, ksExceptions.ClientException, nvExceptions.ClientException, gl1Exceptions.CommunicationError, gl1Exceptions.HTTPNotFound, ConnectionError) as e: #TODO remove
tiernoae4a8d12016-07-08 12:30:39 +02001025 self._format_exception(e)
1026
1027 def get_image_id_from_path(self, path):
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001028 '''Get the image id from image path in the VIM database. Returns the image_id'''
tiernoae4a8d12016-07-08 12:30:39 +02001029 try:
1030 self._reload_connection()
tierno1beea862018-07-11 15:47:37 +02001031 images = self.glance.images.list()
tiernoae4a8d12016-07-08 12:30:39 +02001032 for image in images:
1033 if image.metadata.get("location")==path:
1034 return image.id
1035 raise vimconn.vimconnNotFoundException("image with location '{}' not found".format( path))
tierno8e995ce2016-09-22 08:13:00 +00001036 except (ksExceptions.ClientException, nvExceptions.ClientException, gl1Exceptions.CommunicationError, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +02001037 self._format_exception(e)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001038
garciadeblasb69fa9f2016-09-28 12:04:10 +02001039 def get_image_list(self, filter_dict={}):
1040 '''Obtain tenant images from VIM
1041 Filter_dict can be:
1042 id: image id
1043 name: image name
1044 checksum: image checksum
1045 Returns the image list of dictionaries:
1046 [{<the fields at Filter_dict plus some VIM specific>}, ...]
1047 List can be empty
1048 '''
1049 self.logger.debug("Getting image list from VIM filter: '%s'", str(filter_dict))
1050 try:
1051 self._reload_connection()
tierno69b590e2018-03-13 18:52:23 +01001052 filter_dict_os = filter_dict.copy()
garciadeblasb69fa9f2016-09-28 12:04:10 +02001053 #First we filter by the available filter fields: name, id. The others are removed.
tierno1beea862018-07-11 15:47:37 +02001054 image_list = self.glance.images.list()
garciadeblasb69fa9f2016-09-28 12:04:10 +02001055 filtered_list = []
1056 for image in image_list:
tierno3cb8dc32017-10-24 18:13:19 +02001057 try:
tierno1beea862018-07-11 15:47:37 +02001058 if filter_dict.get("name") and image["name"] != filter_dict["name"]:
1059 continue
1060 if filter_dict.get("id") and image["id"] != filter_dict["id"]:
1061 continue
1062 if filter_dict.get("checksum") and image["checksum"] != filter_dict["checksum"]:
1063 continue
1064
1065 filtered_list.append(image.copy())
tierno3cb8dc32017-10-24 18:13:19 +02001066 except gl1Exceptions.HTTPNotFound:
1067 pass
garciadeblasb69fa9f2016-09-28 12:04:10 +02001068 return filtered_list
1069 except (ksExceptions.ClientException, nvExceptions.ClientException, gl1Exceptions.CommunicationError, ConnectionError) as e:
1070 self._format_exception(e)
1071
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +02001072 def __wait_for_vm(self, vm_id, status):
1073 """wait until vm is in the desired status and return True.
1074 If the VM gets in ERROR status, return false.
1075 If the timeout is reached generate an exception"""
1076 elapsed_time = 0
1077 while elapsed_time < server_timeout:
1078 vm_status = self.nova.servers.get(vm_id).status
1079 if vm_status == status:
1080 return True
1081 if vm_status == 'ERROR':
1082 return False
tierno1df468d2018-07-06 14:25:16 +02001083 time.sleep(5)
1084 elapsed_time += 5
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +02001085
1086 # if we exceeded the timeout rollback
1087 if elapsed_time >= server_timeout:
1088 raise vimconn.vimconnException('Timeout waiting for instance ' + vm_id + ' to get ' + status,
1089 http_code=vimconn.HTTP_Request_Timeout)
1090
mirabal29356312017-07-27 12:21:22 +02001091 def _get_openstack_availablity_zones(self):
1092 """
1093 Get from openstack availability zones available
1094 :return:
1095 """
1096 try:
1097 openstack_availability_zone = self.nova.availability_zones.list()
1098 openstack_availability_zone = [str(zone.zoneName) for zone in openstack_availability_zone
1099 if zone.zoneName != 'internal']
1100 return openstack_availability_zone
1101 except Exception as e:
1102 return None
1103
1104 def _set_availablity_zones(self):
1105 """
1106 Set vim availablity zone
1107 :return:
1108 """
1109
1110 if 'availability_zone' in self.config:
1111 vim_availability_zones = self.config.get('availability_zone')
1112 if isinstance(vim_availability_zones, str):
1113 self.availability_zone = [vim_availability_zones]
1114 elif isinstance(vim_availability_zones, list):
1115 self.availability_zone = vim_availability_zones
1116 else:
1117 self.availability_zone = self._get_openstack_availablity_zones()
1118
tierno5a3273c2017-08-29 11:43:46 +02001119 def _get_vm_availability_zone(self, availability_zone_index, availability_zone_list):
mirabal29356312017-07-27 12:21:22 +02001120 """
tierno5a3273c2017-08-29 11:43:46 +02001121 Return thge availability zone to be used by the created VM.
1122 :return: The VIM availability zone to be used or None
mirabal29356312017-07-27 12:21:22 +02001123 """
tierno5a3273c2017-08-29 11:43:46 +02001124 if availability_zone_index is None:
1125 if not self.config.get('availability_zone'):
1126 return None
1127 elif isinstance(self.config.get('availability_zone'), str):
1128 return self.config['availability_zone']
1129 else:
1130 # TODO consider using a different parameter at config for default AV and AV list match
1131 return self.config['availability_zone'][0]
mirabal29356312017-07-27 12:21:22 +02001132
tierno5a3273c2017-08-29 11:43:46 +02001133 vim_availability_zones = self.availability_zone
1134 # check if VIM offer enough availability zones describe in the VNFD
1135 if vim_availability_zones and len(availability_zone_list) <= len(vim_availability_zones):
1136 # check if all the names of NFV AV match VIM AV names
1137 match_by_index = False
1138 for av in availability_zone_list:
1139 if av not in vim_availability_zones:
1140 match_by_index = True
1141 break
1142 if match_by_index:
1143 return vim_availability_zones[availability_zone_index]
1144 else:
1145 return availability_zone_list[availability_zone_index]
mirabal29356312017-07-27 12:21:22 +02001146 else:
tierno5a3273c2017-08-29 11:43:46 +02001147 raise vimconn.vimconnConflictException("No enough availability zones at VIM for this deployment")
mirabal29356312017-07-27 12:21:22 +02001148
tierno5a3273c2017-08-29 11:43:46 +02001149 def new_vminstance(self, name, description, start, image_id, flavor_id, net_list, cloud_config=None, disk_list=None,
1150 availability_zone_index=None, availability_zone_list=None):
tierno98e909c2017-10-14 13:27:03 +02001151 """Adds a VM instance to VIM
tierno7edb6752016-03-21 17:37:52 +01001152 Params:
1153 start: indicates if VM must start or boot in pause mode. Ignored
1154 image_id,flavor_id: iamge and flavor uuid
1155 net_list: list of interfaces, each one is a dictionary with:
1156 name:
1157 net_id: network uuid to connect
1158 vpci: virtual vcpi to assign, ignored because openstack lack #TODO
1159 model: interface model, ignored #TODO
1160 mac_address: used for SR-IOV ifaces #TODO for other types
1161 use: 'data', 'bridge', 'mgmt'
tierno66eba6e2017-11-10 17:09:18 +01001162 type: 'virtual', 'PCI-PASSTHROUGH'('PF'), 'SR-IOV'('VF'), 'VFnotShared'
tierno7edb6752016-03-21 17:37:52 +01001163 vim_id: filled/added by this function
ahmadsaf853d452016-12-22 11:33:47 +05001164 floating_ip: True/False (or it can be None)
tierno41a69812018-02-16 14:34:33 +01001165 'cloud_config': (optional) dictionary with:
1166 'key-pairs': (optional) list of strings with the public key to be inserted to the default user
1167 'users': (optional) list of users to be inserted, each item is a dict with:
1168 'name': (mandatory) user name,
1169 'key-pairs': (optional) list of strings with the public key to be inserted to the user
1170 'user-data': (optional) string is a text script to be passed directly to cloud-init
1171 'config-files': (optional). List of files to be transferred. Each item is a dict with:
1172 'dest': (mandatory) string with the destination absolute path
1173 'encoding': (optional, by default text). Can be one of:
1174 'b64', 'base64', 'gz', 'gz+b64', 'gz+base64', 'gzip+b64', 'gzip+base64'
1175 'content' (mandatory): string with the content of the file
1176 'permissions': (optional) string with file permissions, typically octal notation '0644'
1177 'owner': (optional) file owner, string with the format 'owner:group'
1178 'boot-data-drive': boolean to indicate if user-data must be passed using a boot drive (hard disk)
mirabal29356312017-07-27 12:21:22 +02001179 'disk_list': (optional) list with additional disks to the VM. Each item is a dict with:
1180 'image_id': (optional). VIM id of an existing image. If not provided an empty disk must be mounted
1181 'size': (mandatory) string with the size of the disk in GB
tierno1df468d2018-07-06 14:25:16 +02001182 'vim_id' (optional) should use this existing volume id
tierno5a3273c2017-08-29 11:43:46 +02001183 availability_zone_index: Index of availability_zone_list to use for this this VM. None if not AV required
1184 availability_zone_list: list of availability zones given by user in the VNFD descriptor. Ignore if
1185 availability_zone_index is None
tierno7edb6752016-03-21 17:37:52 +01001186 #TODO ip, security groups
tierno98e909c2017-10-14 13:27:03 +02001187 Returns a tuple with the instance identifier and created_items or raises an exception on error
1188 created_items can be None or a dictionary where this method can include key-values that will be passed to
1189 the method delete_vminstance and action_vminstance. Can be used to store created ports, volumes, etc.
1190 Format is vimconnector dependent, but do not use nested dictionaries and a value of None should be the same
1191 as not present.
1192 """
tiernofa51c202017-01-27 14:58:17 +01001193 self.logger.debug("new_vminstance input: image='%s' flavor='%s' nics='%s'",image_id, flavor_id,str(net_list))
tierno7edb6752016-03-21 17:37:52 +01001194 try:
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +02001195 server = None
tierno98e909c2017-10-14 13:27:03 +02001196 created_items = {}
tiernob0b9dab2017-10-14 14:25:20 +02001197 # metadata = {}
tierno98e909c2017-10-14 13:27:03 +02001198 net_list_vim = []
1199 external_network = [] # list of external networks to be connected to instance, later on used to create floating_ip
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +02001200 no_secured_ports = [] # List of port-is with port-security disabled
tierno7edb6752016-03-21 17:37:52 +01001201 self._reload_connection()
tiernob0b9dab2017-10-14 14:25:20 +02001202 # metadata_vpci = {} # For a specific neutron plugin
tiernob84cbdc2017-07-07 14:30:30 +02001203 block_device_mapping = None
tiernoa05b65a2019-02-01 12:30:27 +00001204
tierno7edb6752016-03-21 17:37:52 +01001205 for net in net_list:
tierno98e909c2017-10-14 13:27:03 +02001206 if not net.get("net_id"): # skip non connected iface
tierno7edb6752016-03-21 17:37:52 +01001207 continue
Pablo Montes Moreno3be0b2a2017-03-30 13:22:15 +02001208
tiernoa05b65a2019-02-01 12:30:27 +00001209 port_dict = {
Pablo Montes Moreno3be0b2a2017-03-30 13:22:15 +02001210 "network_id": net["net_id"],
1211 "name": net.get("name"),
1212 "admin_state_up": True
1213 }
tiernoa05b65a2019-02-01 12:30:27 +00001214 if self.config.get("security_groups") and net.get("port_security") is not False and \
1215 not self.config.get("no_port_security_extension"):
1216 if not self.security_groups_id:
1217 self._get_ids_from_name()
1218 port_dict["security_groups"] = self.security_groups_id
1219
Pablo Montes Moreno3be0b2a2017-03-30 13:22:15 +02001220 if net["type"]=="virtual":
tiernob0b9dab2017-10-14 14:25:20 +02001221 pass
1222 # if "vpci" in net:
1223 # metadata_vpci[ net["net_id"] ] = [[ net["vpci"], "" ]]
tierno66eba6e2017-11-10 17:09:18 +01001224 elif net["type"] == "VF" or net["type"] == "SR-IOV": # for VF
tiernob0b9dab2017-10-14 14:25:20 +02001225 # if "vpci" in net:
1226 # if "VF" not in metadata_vpci:
1227 # metadata_vpci["VF"]=[]
1228 # metadata_vpci["VF"].append([ net["vpci"], "" ])
Pablo Montes Moreno3be0b2a2017-03-30 13:22:15 +02001229 port_dict["binding:vnic_type"]="direct"
tiernob0b9dab2017-10-14 14:25:20 +02001230 # VIO specific Changes
kate721d79b2017-06-24 04:21:38 -07001231 if self.vim_type == "VIO":
tiernob0b9dab2017-10-14 14:25:20 +02001232 # Need to create port with port_security_enabled = False and no-security-groups
kate721d79b2017-06-24 04:21:38 -07001233 port_dict["port_security_enabled"]=False
1234 port_dict["provider_security_groups"]=[]
1235 port_dict["security_groups"]=[]
tierno66eba6e2017-11-10 17:09:18 +01001236 else: # For PT PCI-PASSTHROUGH
tiernob0b9dab2017-10-14 14:25:20 +02001237 # VIO specific Changes
1238 # Current VIO release does not support port with type 'direct-physical'
1239 # So no need to create virtual port in case of PCI-device.
1240 # Will update port_dict code when support gets added in next VIO release
kate721d79b2017-06-24 04:21:38 -07001241 if self.vim_type == "VIO":
tiernob0b9dab2017-10-14 14:25:20 +02001242 raise vimconn.vimconnNotSupportedException(
1243 "Current VIO release does not support full passthrough (PT)")
1244 # if "vpci" in net:
1245 # if "PF" not in metadata_vpci:
1246 # metadata_vpci["PF"]=[]
1247 # metadata_vpci["PF"].append([ net["vpci"], "" ])
Pablo Montes Moreno3be0b2a2017-03-30 13:22:15 +02001248 port_dict["binding:vnic_type"]="direct-physical"
1249 if not port_dict["name"]:
1250 port_dict["name"]=name
1251 if net.get("mac_address"):
1252 port_dict["mac_address"]=net["mac_address"]
tierno41a69812018-02-16 14:34:33 +01001253 if net.get("ip_address"):
1254 port_dict["fixed_ips"] = [{'ip_address': net["ip_address"]}]
1255 # TODO add 'subnet_id': <subnet_id>
Pablo Montes Moreno3be0b2a2017-03-30 13:22:15 +02001256 new_port = self.neutron.create_port({"port": port_dict })
tierno00e3df72017-11-29 17:20:13 +01001257 created_items["port:" + str(new_port["port"]["id"])] = True
Pablo Montes Moreno3be0b2a2017-03-30 13:22:15 +02001258 net["mac_adress"] = new_port["port"]["mac_address"]
1259 net["vim_id"] = new_port["port"]["id"]
tiernob84cbdc2017-07-07 14:30:30 +02001260 # if try to use a network without subnetwork, it will return a emtpy list
1261 fixed_ips = new_port["port"].get("fixed_ips")
1262 if fixed_ips:
1263 net["ip"] = fixed_ips[0].get("ip_address")
1264 else:
1265 net["ip"] = None
montesmoreno994a29d2017-08-22 11:23:06 +02001266
1267 port = {"port-id": new_port["port"]["id"]}
1268 if float(self.nova.api_version.get_string()) >= 2.32:
1269 port["tag"] = new_port["port"]["name"]
1270 net_list_vim.append(port)
Pablo Montes Moreno3be0b2a2017-03-30 13:22:15 +02001271
ahmadsaf853d452016-12-22 11:33:47 +05001272 if net.get('floating_ip', False):
tiernof8383b82017-01-18 15:49:48 +01001273 net['exit_on_floating_ip_error'] = True
ahmadsaf853d452016-12-22 11:33:47 +05001274 external_network.append(net)
tiernof8383b82017-01-18 15:49:48 +01001275 elif net['use'] == 'mgmt' and self.config.get('use_floating_ip'):
1276 net['exit_on_floating_ip_error'] = False
1277 external_network.append(net)
tierno326fd5e2018-02-22 11:58:59 +01001278 net['floating_ip'] = self.config.get('use_floating_ip')
tiernof8383b82017-01-18 15:49:48 +01001279
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +02001280 # If port security is disabled when the port has not yet been attached to the VM, then all vm traffic is dropped.
1281 # As a workaround we wait until the VM is active and then disable the port-security
tierno4d1ce222018-04-06 10:41:06 +02001282 if net.get("port_security") == False and not self.config.get("no_port_security_extension"):
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +02001283 no_secured_ports.append(new_port["port"]["id"])
1284
tiernob0b9dab2017-10-14 14:25:20 +02001285 # if metadata_vpci:
1286 # metadata = {"pci_assignement": json.dumps(metadata_vpci)}
1287 # if len(metadata["pci_assignement"]) >255:
1288 # #limit the metadata size
1289 # #metadata["pci_assignement"] = metadata["pci_assignement"][0:255]
1290 # self.logger.warn("Metadata deleted since it exceeds the expected length (255) ")
1291 # metadata = {}
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001292
tiernob0b9dab2017-10-14 14:25:20 +02001293 self.logger.debug("name '%s' image_id '%s'flavor_id '%s' net_list_vim '%s' description '%s'",
1294 name, image_id, flavor_id, str(net_list_vim), description)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001295
tierno98e909c2017-10-14 13:27:03 +02001296 # cloud config
tierno0a1437e2017-10-02 00:17:43 +02001297 config_drive, userdata = self._create_user_data(cloud_config)
montesmoreno0c8def02016-12-22 12:16:23 +00001298
tierno98e909c2017-10-14 13:27:03 +02001299 # Create additional volumes in case these are present in disk_list
montesmoreno0c8def02016-12-22 12:16:23 +00001300 base_disk_index = ord('b')
tierno1df468d2018-07-06 14:25:16 +02001301 if disk_list:
tiernob84cbdc2017-07-07 14:30:30 +02001302 block_device_mapping = {}
montesmoreno0c8def02016-12-22 12:16:23 +00001303 for disk in disk_list:
tierno1df468d2018-07-06 14:25:16 +02001304 if disk.get('vim_id'):
1305 block_device_mapping['_vd' + chr(base_disk_index)] = disk['vim_id']
montesmoreno0c8def02016-12-22 12:16:23 +00001306 else:
tierno1df468d2018-07-06 14:25:16 +02001307 if 'image_id' in disk:
1308 volume = self.cinder.volumes.create(size=disk['size'], name=name + '_vd' +
1309 chr(base_disk_index), imageRef=disk['image_id'])
1310 else:
1311 volume = self.cinder.volumes.create(size=disk['size'], name=name + '_vd' +
1312 chr(base_disk_index))
1313 created_items["volume:" + str(volume.id)] = True
1314 block_device_mapping['_vd' + chr(base_disk_index)] = volume.id
montesmoreno0c8def02016-12-22 12:16:23 +00001315 base_disk_index += 1
1316
tierno1df468d2018-07-06 14:25:16 +02001317 # Wait until created volumes are with status available
montesmoreno0c8def02016-12-22 12:16:23 +00001318 elapsed_time = 0
tierno1df468d2018-07-06 14:25:16 +02001319 while elapsed_time < volume_timeout:
1320 for created_item in created_items:
1321 v, _, volume_id = created_item.partition(":")
1322 if v == 'volume':
1323 if self.cinder.volumes.get(volume_id).status != 'available':
1324 break
1325 else: # all ready: break from while
1326 break
1327 time.sleep(5)
1328 elapsed_time += 5
tiernob0b9dab2017-10-14 14:25:20 +02001329 # If we exceeded the timeout rollback
montesmoreno0c8def02016-12-22 12:16:23 +00001330 if elapsed_time >= volume_timeout:
montesmoreno0c8def02016-12-22 12:16:23 +00001331 raise vimconn.vimconnException('Timeout creating volumes for instance ' + name,
1332 http_code=vimconn.HTTP_Request_Timeout)
mirabal29356312017-07-27 12:21:22 +02001333 # get availability Zone
tierno5a3273c2017-08-29 11:43:46 +02001334 vm_av_zone = self._get_vm_availability_zone(availability_zone_index, availability_zone_list)
montesmoreno0c8def02016-12-22 12:16:23 +00001335
tiernob0b9dab2017-10-14 14:25:20 +02001336 self.logger.debug("nova.servers.create({}, {}, {}, nics={}, security_groups={}, "
mirabal29356312017-07-27 12:21:22 +02001337 "availability_zone={}, key_name={}, userdata={}, config_drive={}, "
tiernob0b9dab2017-10-14 14:25:20 +02001338 "block_device_mapping={})".format(name, image_id, flavor_id, net_list_vim,
tiernoa05b65a2019-02-01 12:30:27 +00001339 self.config.get("security_groups"), vm_av_zone,
1340 self.config.get('keypair'), userdata, config_drive,
1341 block_device_mapping))
tiernob0b9dab2017-10-14 14:25:20 +02001342 server = self.nova.servers.create(name, image_id, flavor_id, nics=net_list_vim,
tiernoa05b65a2019-02-01 12:30:27 +00001343 security_groups=self.config.get("security_groups"),
1344 # TODO remove security_groups in future versions. Already at neutron port
mirabal29356312017-07-27 12:21:22 +02001345 availability_zone=vm_av_zone,
montesmoreno0c8def02016-12-22 12:16:23 +00001346 key_name=self.config.get('keypair'),
1347 userdata=userdata,
tiernob84cbdc2017-07-07 14:30:30 +02001348 config_drive=config_drive,
1349 block_device_mapping=block_device_mapping
montesmoreno0c8def02016-12-22 12:16:23 +00001350 ) # , description=description)
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +02001351
tierno326fd5e2018-02-22 11:58:59 +01001352 vm_start_time = time.time()
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +02001353 # Previously mentioned workaround to wait until the VM is active and then disable the port-security
1354 if no_secured_ports:
1355 self.__wait_for_vm(server.id, 'ACTIVE')
1356
1357 for port_id in no_secured_ports:
1358 try:
tierno4d1ce222018-04-06 10:41:06 +02001359 self.neutron.update_port(port_id,
1360 {"port": {"port_security_enabled": False, "security_groups": None}})
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +02001361 except Exception as e:
tierno4d1ce222018-04-06 10:41:06 +02001362 raise vimconn.vimconnException("It was not possible to disable port security for port {}".format(
1363 port_id))
tierno98e909c2017-10-14 13:27:03 +02001364 # print "DONE :-)", server
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +02001365
tierno4d1ce222018-04-06 10:41:06 +02001366 # pool_id = None
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +02001367 if external_network:
tierno98e909c2017-10-14 13:27:03 +02001368 floating_ips = self.neutron.list_floatingips().get("floatingips", ())
ahmadsaf853d452016-12-22 11:33:47 +05001369 for floating_network in external_network:
tiernof8383b82017-01-18 15:49:48 +01001370 try:
tiernof8383b82017-01-18 15:49:48 +01001371 assigned = False
tierno98e909c2017-10-14 13:27:03 +02001372 while not assigned:
tiernof8383b82017-01-18 15:49:48 +01001373 if floating_ips:
1374 ip = floating_ips.pop(0)
tierno326fd5e2018-02-22 11:58:59 +01001375 if ip.get("port_id", False) or ip.get('tenant_id') != server.tenant_id:
1376 continue
1377 if isinstance(floating_network['floating_ip'], str):
1378 if ip.get("floating_network_id") != floating_network['floating_ip']:
1379 continue
tierno7d782ef2019-10-04 12:56:31 +00001380 free_floating_ip = ip["id"]
tiernof8383b82017-01-18 15:49:48 +01001381 else:
tiernocb3cca22018-05-31 15:08:52 +02001382 if isinstance(floating_network['floating_ip'], str) and \
1383 floating_network['floating_ip'].lower() != "true":
tierno326fd5e2018-02-22 11:58:59 +01001384 pool_id = floating_network['floating_ip']
1385 else:
tierno4d1ce222018-04-06 10:41:06 +02001386 # Find the external network
tierno326fd5e2018-02-22 11:58:59 +01001387 external_nets = list()
1388 for net in self.neutron.list_networks()['networks']:
1389 if net['router:external']:
1390 external_nets.append(net)
tiernof8383b82017-01-18 15:49:48 +01001391
tierno326fd5e2018-02-22 11:58:59 +01001392 if len(external_nets) == 0:
1393 raise vimconn.vimconnException("Cannot create floating_ip automatically since no external "
1394 "network is present",
1395 http_code=vimconn.HTTP_Conflict)
1396 if len(external_nets) > 1:
1397 raise vimconn.vimconnException("Cannot create floating_ip automatically since multiple "
1398 "external networks are present",
1399 http_code=vimconn.HTTP_Conflict)
tiernof8383b82017-01-18 15:49:48 +01001400
tierno326fd5e2018-02-22 11:58:59 +01001401 pool_id = external_nets[0].get('id')
tiernof8383b82017-01-18 15:49:48 +01001402 param = {'floatingip': {'floating_network_id': pool_id, 'tenant_id': server.tenant_id}}
ahmadsaf853d452016-12-22 11:33:47 +05001403 try:
tierno4d1ce222018-04-06 10:41:06 +02001404 # self.logger.debug("Creating floating IP")
tiernof8383b82017-01-18 15:49:48 +01001405 new_floating_ip = self.neutron.create_floatingip(param)
tierno7d782ef2019-10-04 12:56:31 +00001406 free_floating_ip = new_floating_ip['floatingip']['id']
ahmadsaf853d452016-12-22 11:33:47 +05001407 except Exception as e:
tierno326fd5e2018-02-22 11:58:59 +01001408 raise vimconn.vimconnException(type(e).__name__ + ": Cannot create new floating_ip " +
1409 str(e), http_code=vimconn.HTTP_Conflict)
1410
tierno326fd5e2018-02-22 11:58:59 +01001411 while not assigned:
1412 try:
tierno7d782ef2019-10-04 12:56:31 +00001413 # the vim_id key contains the neutron.port_id
1414 self.neutron.update_floatingip(free_floating_ip,
1415 {"floatingip": {"port_id": floating_network["vim_id"]}})
1416 # Using nove is deprecated on nova client 10.0
tierno326fd5e2018-02-22 11:58:59 +01001417 assigned = True
1418 except Exception as e:
tierno4d1ce222018-04-06 10:41:06 +02001419 # openstack need some time after VM creation to asign an IP. So retry if fails
tierno326fd5e2018-02-22 11:58:59 +01001420 vm_status = self.nova.servers.get(server.id).status
1421 if vm_status != 'ACTIVE' and vm_status != 'ERROR':
1422 if time.time() - vm_start_time < server_timeout:
1423 time.sleep(5)
1424 continue
tierno4d1ce222018-04-06 10:41:06 +02001425 raise vimconn.vimconnException(
1426 "Cannot create floating_ip: {} {}".format(type(e).__name__, e),
1427 http_code=vimconn.HTTP_Conflict)
tierno326fd5e2018-02-22 11:58:59 +01001428
tiernof8383b82017-01-18 15:49:48 +01001429 except Exception as e:
1430 if not floating_network['exit_on_floating_ip_error']:
tierno7d782ef2019-10-04 12:56:31 +00001431 self.logger.warning("Cannot create floating_ip. %s", str(e))
tiernof8383b82017-01-18 15:49:48 +01001432 continue
tiernof8383b82017-01-18 15:49:48 +01001433 raise
montesmoreno2a1fc4e2017-01-09 16:46:04 +00001434
tierno98e909c2017-10-14 13:27:03 +02001435 return server.id, created_items
tierno7edb6752016-03-21 17:37:52 +01001436# except nvExceptions.NotFound as e:
1437# error_value=-vimconn.HTTP_Not_Found
1438# error_text= "vm instance %s not found" % vm_id
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +02001439# except TypeError as e:
1440# raise vimconn.vimconnException(type(e).__name__ + ": "+ str(e), http_code=vimconn.HTTP_Bad_Request)
1441
1442 except Exception as e:
tierno98e909c2017-10-14 13:27:03 +02001443 server_id = None
1444 if server:
1445 server_id = server.id
1446 try:
1447 self.delete_vminstance(server_id, created_items)
1448 except Exception as e2:
1449 self.logger.error("new_vminstance rollback fail {}".format(e2))
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +02001450
tiernoae4a8d12016-07-08 12:30:39 +02001451 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +01001452
tiernoae4a8d12016-07-08 12:30:39 +02001453 def get_vminstance(self,vm_id):
tierno7edb6752016-03-21 17:37:52 +01001454 '''Returns the VM instance information from VIM'''
tiernoae4a8d12016-07-08 12:30:39 +02001455 #self.logger.debug("Getting VM from VIM")
tierno7edb6752016-03-21 17:37:52 +01001456 try:
1457 self._reload_connection()
1458 server = self.nova.servers.find(id=vm_id)
1459 #TODO parse input and translate to VIM format (openmano_schemas.new_vminstance_response_schema)
tiernoae4a8d12016-07-08 12:30:39 +02001460 return server.to_dict()
tierno8e995ce2016-09-22 08:13:00 +00001461 except (ksExceptions.ClientException, nvExceptions.ClientException, nvExceptions.NotFound, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +02001462 self._format_exception(e)
1463
1464 def get_vminstance_console(self,vm_id, console_type="vnc"):
tierno7edb6752016-03-21 17:37:52 +01001465 '''
1466 Get a console for the virtual machine
1467 Params:
1468 vm_id: uuid of the VM
1469 console_type, can be:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001470 "novnc" (by default), "xvpvnc" for VNC types,
tierno7edb6752016-03-21 17:37:52 +01001471 "rdp-html5" for RDP types, "spice-html5" for SPICE types
tiernoae4a8d12016-07-08 12:30:39 +02001472 Returns dict with the console parameters:
1473 protocol: ssh, ftp, http, https, ...
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001474 server: usually ip address
1475 port: the http, ssh, ... port
1476 suffix: extra text, e.g. the http path and query string
tierno7edb6752016-03-21 17:37:52 +01001477 '''
tiernoae4a8d12016-07-08 12:30:39 +02001478 self.logger.debug("Getting VM CONSOLE from VIM")
tierno7edb6752016-03-21 17:37:52 +01001479 try:
1480 self._reload_connection()
1481 server = self.nova.servers.find(id=vm_id)
1482 if console_type == None or console_type == "novnc":
1483 console_dict = server.get_vnc_console("novnc")
1484 elif console_type == "xvpvnc":
1485 console_dict = server.get_vnc_console(console_type)
1486 elif console_type == "rdp-html5":
1487 console_dict = server.get_rdp_console(console_type)
1488 elif console_type == "spice-html5":
1489 console_dict = server.get_spice_console(console_type)
1490 else:
tiernoae4a8d12016-07-08 12:30:39 +02001491 raise vimconn.vimconnException("console type '{}' not allowed".format(console_type), http_code=vimconn.HTTP_Bad_Request)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001492
tierno7edb6752016-03-21 17:37:52 +01001493 console_dict1 = console_dict.get("console")
1494 if console_dict1:
1495 console_url = console_dict1.get("url")
1496 if console_url:
1497 #parse console_url
1498 protocol_index = console_url.find("//")
1499 suffix_index = console_url[protocol_index+2:].find("/") + protocol_index+2
1500 port_index = console_url[protocol_index+2:suffix_index].find(":") + protocol_index+2
1501 if protocol_index < 0 or port_index<0 or suffix_index<0:
1502 return -vimconn.HTTP_Internal_Server_Error, "Unexpected response from VIM"
1503 console_dict={"protocol": console_url[0:protocol_index],
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001504 "server": console_url[protocol_index+2:port_index],
1505 "port": console_url[port_index:suffix_index],
1506 "suffix": console_url[suffix_index+1:]
tierno7edb6752016-03-21 17:37:52 +01001507 }
1508 protocol_index += 2
tiernoae4a8d12016-07-08 12:30:39 +02001509 return console_dict
1510 raise vimconn.vimconnUnexpectedResponse("Unexpected response from VIM")
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001511
tierno8e995ce2016-09-22 08:13:00 +00001512 except (nvExceptions.NotFound, ksExceptions.ClientException, nvExceptions.ClientException, nvExceptions.BadRequest, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +02001513 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +01001514
tierno98e909c2017-10-14 13:27:03 +02001515 def delete_vminstance(self, vm_id, created_items=None):
tiernoae4a8d12016-07-08 12:30:39 +02001516 '''Removes a VM instance from VIM. Returns the old identifier
tierno7edb6752016-03-21 17:37:52 +01001517 '''
tiernoae4a8d12016-07-08 12:30:39 +02001518 #print "osconnector: Getting VM from VIM"
tierno98e909c2017-10-14 13:27:03 +02001519 if created_items == None:
1520 created_items = {}
tierno7edb6752016-03-21 17:37:52 +01001521 try:
1522 self._reload_connection()
tierno98e909c2017-10-14 13:27:03 +02001523 # delete VM ports attached to this networks before the virtual machine
1524 for k, v in created_items.items():
1525 if not v: # skip already deleted
1526 continue
tierno7edb6752016-03-21 17:37:52 +01001527 try:
tiernoad6bdd42018-01-10 10:43:46 +01001528 k_item, _, k_id = k.partition(":")
1529 if k_item == "port":
1530 self.neutron.delete_port(k_id)
tierno7edb6752016-03-21 17:37:52 +01001531 except Exception as e:
tierno00e3df72017-11-29 17:20:13 +01001532 self.logger.error("Error deleting port: {}: {}".format(type(e).__name__, e))
montesmoreno0c8def02016-12-22 12:16:23 +00001533
tierno98e909c2017-10-14 13:27:03 +02001534 # #commented because detaching the volumes makes the servers.delete not work properly ?!?
1535 # #dettach volumes attached
1536 # server = self.nova.servers.get(vm_id)
1537 # volumes_attached_dict = server._info['os-extended-volumes:volumes_attached'] #volume['id']
1538 # #for volume in volumes_attached_dict:
1539 # # self.cinder.volumes.detach(volume['id'])
montesmoreno0c8def02016-12-22 12:16:23 +00001540
tierno98e909c2017-10-14 13:27:03 +02001541 if vm_id:
1542 self.nova.servers.delete(vm_id)
montesmoreno0c8def02016-12-22 12:16:23 +00001543
tierno98e909c2017-10-14 13:27:03 +02001544 # delete volumes. Although having detached, they should have in active status before deleting
1545 # we ensure in this loop
montesmoreno0c8def02016-12-22 12:16:23 +00001546 keep_waiting = True
1547 elapsed_time = 0
1548 while keep_waiting and elapsed_time < volume_timeout:
1549 keep_waiting = False
tierno98e909c2017-10-14 13:27:03 +02001550 for k, v in created_items.items():
1551 if not v: # skip already deleted
1552 continue
1553 try:
tiernoad6bdd42018-01-10 10:43:46 +01001554 k_item, _, k_id = k.partition(":")
1555 if k_item == "volume":
1556 if self.cinder.volumes.get(k_id).status != 'available':
tierno98e909c2017-10-14 13:27:03 +02001557 keep_waiting = True
1558 else:
tiernoad6bdd42018-01-10 10:43:46 +01001559 self.cinder.volumes.delete(k_id)
tierno98e909c2017-10-14 13:27:03 +02001560 except Exception as e:
tierno00e3df72017-11-29 17:20:13 +01001561 self.logger.error("Error deleting volume: {}: {}".format(type(e).__name__, e))
montesmoreno0c8def02016-12-22 12:16:23 +00001562 if keep_waiting:
1563 time.sleep(1)
1564 elapsed_time += 1
tierno98e909c2017-10-14 13:27:03 +02001565 return None
tierno8e995ce2016-09-22 08:13:00 +00001566 except (nvExceptions.NotFound, ksExceptions.ClientException, nvExceptions.ClientException, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +02001567 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +01001568
tiernoae4a8d12016-07-08 12:30:39 +02001569 def refresh_vms_status(self, vm_list):
1570 '''Get the status of the virtual machines and their interfaces/ports
1571 Params: the list of VM identifiers
1572 Returns a dictionary with:
1573 vm_id: #VIM id of this Virtual Machine
1574 status: #Mandatory. Text with one of:
1575 # DELETED (not found at vim)
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001576 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
tiernoae4a8d12016-07-08 12:30:39 +02001577 # OTHER (Vim reported other status not understood)
1578 # ERROR (VIM indicates an ERROR status)
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001579 # ACTIVE, PAUSED, SUSPENDED, INACTIVE (not running),
tiernoae4a8d12016-07-08 12:30:39 +02001580 # CREATING (on building process), ERROR
1581 # ACTIVE:NoMgmtIP (Active but any of its interface has an IP address
1582 #
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001583 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
tiernoae4a8d12016-07-08 12:30:39 +02001584 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
1585 interfaces:
1586 - vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
1587 mac_address: #Text format XX:XX:XX:XX:XX:XX
1588 vim_net_id: #network id where this interface is connected
1589 vim_interface_id: #interface/port VIM id
1590 ip_address: #null, or text with IPv4, IPv6 address
tierno867ffe92017-03-27 12:50:34 +02001591 compute_node: #identification of compute node where PF,VF interface is allocated
1592 pci: #PCI address of the NIC that hosts the PF,VF
1593 vlan: #physical VLAN used for VF
tierno7edb6752016-03-21 17:37:52 +01001594 '''
tiernoae4a8d12016-07-08 12:30:39 +02001595 vm_dict={}
1596 self.logger.debug("refresh_vms status: Getting tenant VM instance information from VIM")
1597 for vm_id in vm_list:
1598 vm={}
1599 try:
1600 vm_vim = self.get_vminstance(vm_id)
1601 if vm_vim['status'] in vmStatus2manoFormat:
1602 vm['status'] = vmStatus2manoFormat[ vm_vim['status'] ]
tierno7edb6752016-03-21 17:37:52 +01001603 else:
tiernoae4a8d12016-07-08 12:30:39 +02001604 vm['status'] = "OTHER"
1605 vm['error_msg'] = "VIM status reported " + vm_vim['status']
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001606
1607 vm['vim_info'] = self.serialize(vm_vim)
1608
tiernoae4a8d12016-07-08 12:30:39 +02001609 vm["interfaces"] = []
1610 if vm_vim.get('fault'):
1611 vm['error_msg'] = str(vm_vim['fault'])
1612 #get interfaces
tierno7edb6752016-03-21 17:37:52 +01001613 try:
tiernoae4a8d12016-07-08 12:30:39 +02001614 self._reload_connection()
tiernob42fd9b2018-06-20 10:44:32 +02001615 port_dict = self.neutron.list_ports(device_id=vm_id)
tiernoae4a8d12016-07-08 12:30:39 +02001616 for port in port_dict["ports"]:
1617 interface={}
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001618 interface['vim_info'] = self.serialize(port)
tiernoae4a8d12016-07-08 12:30:39 +02001619 interface["mac_address"] = port.get("mac_address")
1620 interface["vim_net_id"] = port["network_id"]
1621 interface["vim_interface_id"] = port["id"]
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001622 # check if OS-EXT-SRV-ATTR:host is there,
Mike Marchetti5b9da422017-05-02 15:35:47 -04001623 # in case of non-admin credentials, it will be missing
1624 if vm_vim.get('OS-EXT-SRV-ATTR:host'):
1625 interface["compute_node"] = vm_vim['OS-EXT-SRV-ATTR:host']
tierno867ffe92017-03-27 12:50:34 +02001626 interface["pci"] = None
Mike Marchetti5b9da422017-05-02 15:35:47 -04001627
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001628 # check if binding:profile is there,
Mike Marchetti5b9da422017-05-02 15:35:47 -04001629 # in case of non-admin credentials, it will be missing
1630 if port.get('binding:profile'):
1631 if port['binding:profile'].get('pci_slot'):
1632 # TODO: At the moment sr-iov pci addresses are converted to PF pci addresses by setting the slot to 0x00
1633 # TODO: This is just a workaround valid for niantinc. Find a better way to do so
1634 # CHANGE DDDD:BB:SS.F to DDDD:BB:00.(F%2) assuming there are 2 ports per nic
1635 pci = port['binding:profile']['pci_slot']
1636 # interface["pci"] = pci[:-4] + "00." + str(int(pci[-1]) % 2)
1637 interface["pci"] = pci
tierno867ffe92017-03-27 12:50:34 +02001638 interface["vlan"] = None
Pablo Montes Moreno3be0b2a2017-03-30 13:22:15 +02001639 #if network is of type vlan and port is of type direct (sr-iov) then set vlan id
Pablo Montes Moreno51e553b2017-03-23 16:39:12 +01001640 network = self.neutron.show_network(port["network_id"])
Pablo Montes Moreno3be0b2a2017-03-30 13:22:15 +02001641 if network['network'].get('provider:network_type') == 'vlan' and \
1642 port.get("binding:vnic_type") == "direct":
tierno867ffe92017-03-27 12:50:34 +02001643 interface["vlan"] = network['network'].get('provider:segmentation_id')
tiernoae4a8d12016-07-08 12:30:39 +02001644 ips=[]
1645 #look for floating ip address
tiernob42fd9b2018-06-20 10:44:32 +02001646 try:
1647 floating_ip_dict = self.neutron.list_floatingips(port_id=port["id"])
1648 if floating_ip_dict.get("floatingips"):
1649 ips.append(floating_ip_dict["floatingips"][0].get("floating_ip_address") )
1650 except Exception:
1651 pass
tierno7edb6752016-03-21 17:37:52 +01001652
tiernoae4a8d12016-07-08 12:30:39 +02001653 for subnet in port["fixed_ips"]:
1654 ips.append(subnet["ip_address"])
1655 interface["ip_address"] = ";".join(ips)
1656 vm["interfaces"].append(interface)
1657 except Exception as e:
tiernob42fd9b2018-06-20 10:44:32 +02001658 self.logger.error("Error getting vm interface information {}: {}".format(type(e).__name__, e),
1659 exc_info=True)
tiernoae4a8d12016-07-08 12:30:39 +02001660 except vimconn.vimconnNotFoundException as e:
1661 self.logger.error("Exception getting vm status: %s", str(e))
1662 vm['status'] = "DELETED"
1663 vm['error_msg'] = str(e)
1664 except vimconn.vimconnException as e:
1665 self.logger.error("Exception getting vm status: %s", str(e))
1666 vm['status'] = "VIM_ERROR"
1667 vm['error_msg'] = str(e)
1668 vm_dict[vm_id] = vm
1669 return vm_dict
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001670
tierno98e909c2017-10-14 13:27:03 +02001671 def action_vminstance(self, vm_id, action_dict, created_items={}):
tierno7edb6752016-03-21 17:37:52 +01001672 '''Send and action over a VM instance from VIM
tierno98e909c2017-10-14 13:27:03 +02001673 Returns None or the console dict if the action was successfully sent to the VIM'''
tiernoae4a8d12016-07-08 12:30:39 +02001674 self.logger.debug("Action over VM '%s': %s", vm_id, str(action_dict))
tierno7edb6752016-03-21 17:37:52 +01001675 try:
1676 self._reload_connection()
1677 server = self.nova.servers.find(id=vm_id)
1678 if "start" in action_dict:
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001679 if action_dict["start"]=="rebuild":
tierno7edb6752016-03-21 17:37:52 +01001680 server.rebuild()
1681 else:
1682 if server.status=="PAUSED":
1683 server.unpause()
1684 elif server.status=="SUSPENDED":
1685 server.resume()
1686 elif server.status=="SHUTOFF":
1687 server.start()
1688 elif "pause" in action_dict:
1689 server.pause()
1690 elif "resume" in action_dict:
1691 server.resume()
1692 elif "shutoff" in action_dict or "shutdown" in action_dict:
1693 server.stop()
1694 elif "forceOff" in action_dict:
1695 server.stop() #TODO
1696 elif "terminate" in action_dict:
1697 server.delete()
1698 elif "createImage" in action_dict:
1699 server.create_image()
1700 #"path":path_schema,
1701 #"description":description_schema,
1702 #"name":name_schema,
1703 #"metadata":metadata_schema,
1704 #"imageRef": id_schema,
1705 #"disk": {"oneOf":[{"type": "null"}, {"type":"string"}] },
1706 elif "rebuild" in action_dict:
1707 server.rebuild(server.image['id'])
1708 elif "reboot" in action_dict:
1709 server.reboot() #reboot_type='SOFT'
1710 elif "console" in action_dict:
1711 console_type = action_dict["console"]
1712 if console_type == None or console_type == "novnc":
1713 console_dict = server.get_vnc_console("novnc")
1714 elif console_type == "xvpvnc":
1715 console_dict = server.get_vnc_console(console_type)
1716 elif console_type == "rdp-html5":
1717 console_dict = server.get_rdp_console(console_type)
1718 elif console_type == "spice-html5":
1719 console_dict = server.get_spice_console(console_type)
1720 else:
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001721 raise vimconn.vimconnException("console type '{}' not allowed".format(console_type),
tiernoae4a8d12016-07-08 12:30:39 +02001722 http_code=vimconn.HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001723 try:
1724 console_url = console_dict["console"]["url"]
1725 #parse console_url
1726 protocol_index = console_url.find("//")
1727 suffix_index = console_url[protocol_index+2:].find("/") + protocol_index+2
1728 port_index = console_url[protocol_index+2:suffix_index].find(":") + protocol_index+2
1729 if protocol_index < 0 or port_index<0 or suffix_index<0:
tiernoae4a8d12016-07-08 12:30:39 +02001730 raise vimconn.vimconnException("Unexpected response from VIM " + str(console_dict))
tierno7edb6752016-03-21 17:37:52 +01001731 console_dict2={"protocol": console_url[0:protocol_index],
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001732 "server": console_url[protocol_index+2 : port_index],
1733 "port": int(console_url[port_index+1 : suffix_index]),
1734 "suffix": console_url[suffix_index+1:]
tierno7edb6752016-03-21 17:37:52 +01001735 }
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001736 return console_dict2
tiernoae4a8d12016-07-08 12:30:39 +02001737 except Exception as e:
1738 raise vimconn.vimconnException("Unexpected response from VIM " + str(console_dict))
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001739
tierno98e909c2017-10-14 13:27:03 +02001740 return None
tierno8e995ce2016-09-22 08:13:00 +00001741 except (ksExceptions.ClientException, nvExceptions.ClientException, nvExceptions.NotFound, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +02001742 self._format_exception(e)
1743 #TODO insert exception vimconn.HTTP_Unauthorized
1744
kate721d79b2017-06-24 04:21:38 -07001745 ####### VIO Specific Changes #########
garciadeblasebd66722019-01-31 16:01:31 +00001746 def _generate_vlanID(self):
kate721d79b2017-06-24 04:21:38 -07001747 """
1748 Method to get unused vlanID
1749 Args:
1750 None
1751 Returns:
1752 vlanID
1753 """
1754 #Get used VLAN IDs
1755 usedVlanIDs = []
1756 networks = self.get_network_list()
1757 for net in networks:
1758 if net.get('provider:segmentation_id'):
1759 usedVlanIDs.append(net.get('provider:segmentation_id'))
1760 used_vlanIDs = set(usedVlanIDs)
1761
1762 #find unused VLAN ID
1763 for vlanID_range in self.config.get('dataplane_net_vlan_range'):
1764 try:
1765 start_vlanid , end_vlanid = map(int, vlanID_range.replace(" ", "").split("-"))
tierno7d782ef2019-10-04 12:56:31 +00001766 for vlanID in range(start_vlanid, end_vlanid + 1):
kate721d79b2017-06-24 04:21:38 -07001767 if vlanID not in used_vlanIDs:
1768 return vlanID
1769 except Exception as exp:
1770 raise vimconn.vimconnException("Exception {} occurred while generating VLAN ID.".format(exp))
1771 else:
1772 raise vimconn.vimconnConflictException("Unable to create the SRIOV VLAN network."\
1773 " All given Vlan IDs {} are in use.".format(self.config.get('dataplane_net_vlan_range')))
1774
1775
garciadeblasebd66722019-01-31 16:01:31 +00001776 def _generate_multisegment_vlanID(self):
1777 """
1778 Method to get unused vlanID
1779 Args:
1780 None
1781 Returns:
1782 vlanID
1783 """
1784 #Get used VLAN IDs
1785 usedVlanIDs = []
1786 networks = self.get_network_list()
1787 for net in networks:
1788 if net.get('provider:network_type') == "vlan" and net.get('provider:segmentation_id'):
1789 usedVlanIDs.append(net.get('provider:segmentation_id'))
1790 elif net.get('segments'):
1791 for segment in net.get('segments'):
1792 if segment.get('provider:network_type') == "vlan" and segment.get('provider:segmentation_id'):
1793 usedVlanIDs.append(segment.get('provider:segmentation_id'))
1794 used_vlanIDs = set(usedVlanIDs)
1795
1796 #find unused VLAN ID
1797 for vlanID_range in self.config.get('multisegment_vlan_range'):
1798 try:
1799 start_vlanid , end_vlanid = map(int, vlanID_range.replace(" ", "").split("-"))
tierno7d782ef2019-10-04 12:56:31 +00001800 for vlanID in range(start_vlanid, end_vlanid + 1):
garciadeblasebd66722019-01-31 16:01:31 +00001801 if vlanID not in used_vlanIDs:
1802 return vlanID
1803 except Exception as exp:
1804 raise vimconn.vimconnException("Exception {} occurred while generating VLAN ID.".format(exp))
1805 else:
1806 raise vimconn.vimconnConflictException("Unable to create the VLAN segment."\
1807 " All VLAN IDs {} are in use.".format(self.config.get('multisegment_vlan_range')))
1808
1809
1810 def _validate_vlan_ranges(self, input_vlan_range, text_vlan_range):
kate721d79b2017-06-24 04:21:38 -07001811 """
1812 Method to validate user given vlanID ranges
1813 Args: None
1814 Returns: None
1815 """
garciadeblasebd66722019-01-31 16:01:31 +00001816 for vlanID_range in input_vlan_range:
kate721d79b2017-06-24 04:21:38 -07001817 vlan_range = vlanID_range.replace(" ", "")
1818 #validate format
1819 vlanID_pattern = r'(\d)*-(\d)*$'
1820 match_obj = re.match(vlanID_pattern, vlan_range)
1821 if not match_obj:
garciadeblasebd66722019-01-31 16:01:31 +00001822 raise vimconn.vimconnConflictException("Invalid VLAN range for {}: {}.You must provide "\
1823 "'{}' in format [start_ID - end_ID].".format(text_vlan_range, vlanID_range, text_vlan_range))
kate721d79b2017-06-24 04:21:38 -07001824
1825 start_vlanid , end_vlanid = map(int,vlan_range.split("-"))
1826 if start_vlanid <= 0 :
garciadeblasebd66722019-01-31 16:01:31 +00001827 raise vimconn.vimconnConflictException("Invalid VLAN range for {}: {}."\
kate721d79b2017-06-24 04:21:38 -07001828 "Start ID can not be zero. For VLAN "\
garciadeblasebd66722019-01-31 16:01:31 +00001829 "networks valid IDs are 1 to 4094 ".format(text_vlan_range, vlanID_range))
kate721d79b2017-06-24 04:21:38 -07001830 if end_vlanid > 4094 :
garciadeblasebd66722019-01-31 16:01:31 +00001831 raise vimconn.vimconnConflictException("Invalid VLAN range for {}: {}."\
kate721d79b2017-06-24 04:21:38 -07001832 "End VLAN ID can not be greater than 4094. For VLAN "\
garciadeblasebd66722019-01-31 16:01:31 +00001833 "networks valid IDs are 1 to 4094 ".format(text_vlan_range, vlanID_range))
kate721d79b2017-06-24 04:21:38 -07001834
1835 if start_vlanid > end_vlanid:
garciadeblasebd66722019-01-31 16:01:31 +00001836 raise vimconn.vimconnConflictException("Invalid VLAN range for {}: {}."\
1837 "You must provide '{}' in format start_ID - end_ID and "\
1838 "start_ID < end_ID ".format(text_vlan_range, vlanID_range, text_vlan_range))
kate721d79b2017-06-24 04:21:38 -07001839
tiernoae4a8d12016-07-08 12:30:39 +02001840#NOT USED FUNCTIONS
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001841
tiernoae4a8d12016-07-08 12:30:39 +02001842 def new_external_port(self, port_data):
1843 #TODO openstack if needed
1844 '''Adds a external port to VIM'''
1845 '''Returns the port identifier'''
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001846 return -vimconn.HTTP_Internal_Server_Error, "osconnector.new_external_port() not implemented"
1847
tiernoae4a8d12016-07-08 12:30:39 +02001848 def connect_port_network(self, port_id, network_id, admin=False):
1849 #TODO openstack if needed
1850 '''Connects a external port to a network'''
1851 '''Returns status code of the VIM response'''
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001852 return -vimconn.HTTP_Internal_Server_Error, "osconnector.connect_port_network() not implemented"
1853
tiernoae4a8d12016-07-08 12:30:39 +02001854 def new_user(self, user_name, user_passwd, tenant_id=None):
1855 '''Adds a new user to openstack VIM'''
1856 '''Returns the user identifier'''
1857 self.logger.debug("osconnector: Adding a new user to VIM")
1858 try:
1859 self._reload_connection()
Eduardo Sousae3c0dbc2018-09-03 11:56:07 +01001860 user=self.keystone.users.create(user_name, password=user_passwd, default_project=tenant_id)
tiernoae4a8d12016-07-08 12:30:39 +02001861 #self.keystone.tenants.add_user(self.k_creds["username"], #role)
1862 return user.id
1863 except ksExceptions.ConnectionError as e:
1864 error_value=-vimconn.HTTP_Bad_Request
1865 error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0]))
1866 except ksExceptions.ClientException as e: #TODO remove
tierno7edb6752016-03-21 17:37:52 +01001867 error_value=-vimconn.HTTP_Bad_Request
1868 error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0]))
1869 #TODO insert exception vimconn.HTTP_Unauthorized
1870 #if reaching here is because an exception
tierno9c5c8322018-03-23 15:44:03 +01001871 self.logger.debug("new_user " + error_text)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001872 return error_value, error_text
tiernoae4a8d12016-07-08 12:30:39 +02001873
1874 def delete_user(self, user_id):
1875 '''Delete a user from openstack VIM'''
1876 '''Returns the user identifier'''
1877 if self.debug:
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001878 print("osconnector: Deleting a user from VIM")
tiernoae4a8d12016-07-08 12:30:39 +02001879 try:
1880 self._reload_connection()
1881 self.keystone.users.delete(user_id)
1882 return 1, user_id
1883 except ksExceptions.ConnectionError as e:
1884 error_value=-vimconn.HTTP_Bad_Request
1885 error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0]))
1886 except ksExceptions.NotFound as e:
1887 error_value=-vimconn.HTTP_Not_Found
1888 error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0]))
1889 except ksExceptions.ClientException as e: #TODO remove
1890 error_value=-vimconn.HTTP_Bad_Request
1891 error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0]))
1892 #TODO insert exception vimconn.HTTP_Unauthorized
1893 #if reaching here is because an exception
tierno9c5c8322018-03-23 15:44:03 +01001894 self.logger.debug("delete_tenant " + error_text)
tiernoae4a8d12016-07-08 12:30:39 +02001895 return error_value, error_text
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001896
tierno7edb6752016-03-21 17:37:52 +01001897 def get_hosts_info(self):
1898 '''Get the information of deployed hosts
1899 Returns the hosts content'''
1900 if self.debug:
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001901 print("osconnector: Getting Host info from VIM")
tierno7edb6752016-03-21 17:37:52 +01001902 try:
1903 h_list=[]
1904 self._reload_connection()
1905 hypervisors = self.nova.hypervisors.list()
1906 for hype in hypervisors:
1907 h_list.append( hype.to_dict() )
1908 return 1, {"hosts":h_list}
1909 except nvExceptions.NotFound as e:
1910 error_value=-vimconn.HTTP_Not_Found
1911 error_text= (str(e) if len(e.args)==0 else str(e.args[0]))
1912 except (ksExceptions.ClientException, nvExceptions.ClientException) as e:
1913 error_value=-vimconn.HTTP_Bad_Request
1914 error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0]))
1915 #TODO insert exception vimconn.HTTP_Unauthorized
1916 #if reaching here is because an exception
tierno9c5c8322018-03-23 15:44:03 +01001917 self.logger.debug("get_hosts_info " + error_text)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001918 return error_value, error_text
tierno7edb6752016-03-21 17:37:52 +01001919
1920 def get_hosts(self, vim_tenant):
1921 '''Get the hosts and deployed instances
1922 Returns the hosts content'''
1923 r, hype_dict = self.get_hosts_info()
1924 if r<0:
1925 return r, hype_dict
1926 hypervisors = hype_dict["hosts"]
1927 try:
1928 servers = self.nova.servers.list()
1929 for hype in hypervisors:
1930 for server in servers:
1931 if server.to_dict()['OS-EXT-SRV-ATTR:hypervisor_hostname']==hype['hypervisor_hostname']:
1932 if 'vm' in hype:
1933 hype['vm'].append(server.id)
1934 else:
1935 hype['vm'] = [server.id]
1936 return 1, hype_dict
1937 except nvExceptions.NotFound as e:
1938 error_value=-vimconn.HTTP_Not_Found
1939 error_text= (str(e) if len(e.args)==0 else str(e.args[0]))
1940 except (ksExceptions.ClientException, nvExceptions.ClientException) as e:
1941 error_value=-vimconn.HTTP_Bad_Request
1942 error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0]))
1943 #TODO insert exception vimconn.HTTP_Unauthorized
1944 #if reaching here is because an exception
tierno9c5c8322018-03-23 15:44:03 +01001945 self.logger.debug("get_hosts " + error_text)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001946 return error_value, error_text
tierno7edb6752016-03-21 17:37:52 +01001947
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001948 def new_classification(self, name, ctype, definition):
tierno7d782ef2019-10-04 12:56:31 +00001949 self.logger.debug('Adding a new (Traffic) Classification to VIM, named %s', name)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001950 try:
1951 new_class = None
1952 self._reload_connection()
1953 if ctype not in supportedClassificationTypes:
1954 raise vimconn.vimconnNotSupportedException(
1955 'OpenStack VIM connector doesn\'t support provided '
1956 'Classification Type {}, supported ones are: '
1957 '{}'.format(ctype, supportedClassificationTypes))
1958 if not self._validate_classification(ctype, definition):
1959 raise vimconn.vimconnException(
1960 'Incorrect Classification definition '
1961 'for the type specified.')
1962 classification_dict = definition
1963 classification_dict['name'] = name
tierno7edb6752016-03-21 17:37:52 +01001964
Igor D.Ccaadc442017-11-06 12:48:48 +00001965 new_class = self.neutron.create_sfc_flow_classifier(
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001966 {'flow_classifier': classification_dict})
1967 return new_class['flow_classifier']['id']
1968 except (neExceptions.ConnectionFailed, ksExceptions.ClientException,
1969 neExceptions.NeutronException, ConnectionError) as e:
1970 self.logger.error(
1971 'Creation of Classification failed.')
1972 self._format_exception(e)
1973
1974 def get_classification(self, class_id):
1975 self.logger.debug(" Getting Classification %s from VIM", class_id)
1976 filter_dict = {"id": class_id}
1977 class_list = self.get_classification_list(filter_dict)
1978 if len(class_list) == 0:
1979 raise vimconn.vimconnNotFoundException(
1980 "Classification '{}' not found".format(class_id))
1981 elif len(class_list) > 1:
1982 raise vimconn.vimconnConflictException(
1983 "Found more than one Classification with this criteria")
1984 classification = class_list[0]
1985 return classification
1986
1987 def get_classification_list(self, filter_dict={}):
1988 self.logger.debug("Getting Classifications from VIM filter: '%s'",
1989 str(filter_dict))
1990 try:
tierno69b590e2018-03-13 18:52:23 +01001991 filter_dict_os = filter_dict.copy()
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001992 self._reload_connection()
tierno69b590e2018-03-13 18:52:23 +01001993 if self.api_version3 and "tenant_id" in filter_dict_os:
1994 filter_dict_os['project_id'] = filter_dict_os.pop('tenant_id')
Igor D.Ccaadc442017-11-06 12:48:48 +00001995 classification_dict = self.neutron.list_sfc_flow_classifiers(
tierno69b590e2018-03-13 18:52:23 +01001996 **filter_dict_os)
1997 classification_list = classification_dict["flow_classifiers"]
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001998 self.__classification_os2mano(classification_list)
1999 return classification_list
2000 except (neExceptions.ConnectionFailed, ksExceptions.ClientException,
2001 neExceptions.NeutronException, ConnectionError) as e:
2002 self._format_exception(e)
2003
2004 def delete_classification(self, class_id):
2005 self.logger.debug("Deleting Classification '%s' from VIM", class_id)
2006 try:
2007 self._reload_connection()
Igor D.Ccaadc442017-11-06 12:48:48 +00002008 self.neutron.delete_sfc_flow_classifier(class_id)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00002009 return class_id
2010 except (neExceptions.ConnectionFailed, neExceptions.NeutronException,
2011 ksExceptions.ClientException, neExceptions.NeutronException,
2012 ConnectionError) as e:
2013 self._format_exception(e)
2014
2015 def new_sfi(self, name, ingress_ports, egress_ports, sfc_encap=True):
tierno7d782ef2019-10-04 12:56:31 +00002016 self.logger.debug("Adding a new Service Function Instance to VIM, named '%s'", name)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00002017 try:
2018 new_sfi = None
2019 self._reload_connection()
2020 correlation = None
2021 if sfc_encap:
Igor D.Ccaadc442017-11-06 12:48:48 +00002022 correlation = 'nsh'
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00002023 if len(ingress_ports) != 1:
2024 raise vimconn.vimconnNotSupportedException(
2025 "OpenStack VIM connector can only have "
2026 "1 ingress port per SFI")
2027 if len(egress_ports) != 1:
2028 raise vimconn.vimconnNotSupportedException(
2029 "OpenStack VIM connector can only have "
2030 "1 egress port per SFI")
2031 sfi_dict = {'name': name,
2032 'ingress': ingress_ports[0],
2033 'egress': egress_ports[0],
2034 'service_function_parameters': {
2035 'correlation': correlation}}
Igor D.Ccaadc442017-11-06 12:48:48 +00002036 new_sfi = self.neutron.create_sfc_port_pair({'port_pair': sfi_dict})
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00002037 return new_sfi['port_pair']['id']
2038 except (neExceptions.ConnectionFailed, ksExceptions.ClientException,
2039 neExceptions.NeutronException, ConnectionError) as e:
2040 if new_sfi:
2041 try:
Igor D.Ccaadc442017-11-06 12:48:48 +00002042 self.neutron.delete_sfc_port_pair(
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00002043 new_sfi['port_pair']['id'])
2044 except Exception:
2045 self.logger.error(
2046 'Creation of Service Function Instance failed, with '
2047 'subsequent deletion failure as well.')
2048 self._format_exception(e)
2049
2050 def get_sfi(self, sfi_id):
tierno7d782ef2019-10-04 12:56:31 +00002051 self.logger.debug('Getting Service Function Instance %s from VIM', sfi_id)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00002052 filter_dict = {"id": sfi_id}
2053 sfi_list = self.get_sfi_list(filter_dict)
2054 if len(sfi_list) == 0:
tierno7d782ef2019-10-04 12:56:31 +00002055 raise vimconn.vimconnNotFoundException("Service Function Instance '{}' not found".format(sfi_id))
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00002056 elif len(sfi_list) > 1:
2057 raise vimconn.vimconnConflictException(
2058 'Found more than one Service Function Instance '
2059 'with this criteria')
2060 sfi = sfi_list[0]
2061 return sfi
2062
2063 def get_sfi_list(self, filter_dict={}):
tierno7d782ef2019-10-04 12:56:31 +00002064 self.logger.debug("Getting Service Function Instances from VIM filter: '%s'", str(filter_dict))
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00002065 try:
2066 self._reload_connection()
tierno69b590e2018-03-13 18:52:23 +01002067 filter_dict_os = filter_dict.copy()
2068 if self.api_version3 and "tenant_id" in filter_dict_os:
2069 filter_dict_os['project_id'] = filter_dict_os.pop('tenant_id')
2070 sfi_dict = self.neutron.list_sfc_port_pairs(**filter_dict_os)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00002071 sfi_list = sfi_dict["port_pairs"]
2072 self.__sfi_os2mano(sfi_list)
2073 return sfi_list
2074 except (neExceptions.ConnectionFailed, ksExceptions.ClientException,
2075 neExceptions.NeutronException, ConnectionError) as e:
2076 self._format_exception(e)
2077
2078 def delete_sfi(self, sfi_id):
2079 self.logger.debug("Deleting Service Function Instance '%s' "
2080 "from VIM", sfi_id)
2081 try:
2082 self._reload_connection()
Igor D.Ccaadc442017-11-06 12:48:48 +00002083 self.neutron.delete_sfc_port_pair(sfi_id)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00002084 return sfi_id
2085 except (neExceptions.ConnectionFailed, neExceptions.NeutronException,
2086 ksExceptions.ClientException, neExceptions.NeutronException,
2087 ConnectionError) as e:
2088 self._format_exception(e)
2089
2090 def new_sf(self, name, sfis, sfc_encap=True):
tierno7d782ef2019-10-04 12:56:31 +00002091 self.logger.debug("Adding a new Service Function to VIM, named '%s'", name)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00002092 try:
2093 new_sf = None
2094 self._reload_connection()
tierno9c5c8322018-03-23 15:44:03 +01002095 # correlation = None
2096 # if sfc_encap:
2097 # correlation = 'nsh'
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00002098 for instance in sfis:
2099 sfi = self.get_sfi(instance)
Igor D.Ccaadc442017-11-06 12:48:48 +00002100 if sfi.get('sfc_encap') != sfc_encap:
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00002101 raise vimconn.vimconnNotSupportedException(
2102 "OpenStack VIM connector requires all SFIs of the "
2103 "same SF to share the same SFC Encapsulation")
2104 sf_dict = {'name': name,
2105 'port_pairs': sfis}
Igor D.Ccaadc442017-11-06 12:48:48 +00002106 new_sf = self.neutron.create_sfc_port_pair_group({
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00002107 'port_pair_group': sf_dict})
2108 return new_sf['port_pair_group']['id']
2109 except (neExceptions.ConnectionFailed, ksExceptions.ClientException,
2110 neExceptions.NeutronException, ConnectionError) as e:
2111 if new_sf:
2112 try:
Igor D.Ccaadc442017-11-06 12:48:48 +00002113 self.neutron.delete_sfc_port_pair_group(
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00002114 new_sf['port_pair_group']['id'])
2115 except Exception:
2116 self.logger.error(
2117 'Creation of Service Function failed, with '
2118 'subsequent deletion failure as well.')
2119 self._format_exception(e)
2120
2121 def get_sf(self, sf_id):
2122 self.logger.debug("Getting Service Function %s from VIM", sf_id)
2123 filter_dict = {"id": sf_id}
2124 sf_list = self.get_sf_list(filter_dict)
2125 if len(sf_list) == 0:
2126 raise vimconn.vimconnNotFoundException(
2127 "Service Function '{}' not found".format(sf_id))
2128 elif len(sf_list) > 1:
2129 raise vimconn.vimconnConflictException(
2130 "Found more than one Service Function with this criteria")
2131 sf = sf_list[0]
2132 return sf
2133
2134 def get_sf_list(self, filter_dict={}):
2135 self.logger.debug("Getting Service Function from VIM filter: '%s'",
2136 str(filter_dict))
2137 try:
2138 self._reload_connection()
tierno69b590e2018-03-13 18:52:23 +01002139 filter_dict_os = filter_dict.copy()
2140 if self.api_version3 and "tenant_id" in filter_dict_os:
2141 filter_dict_os['project_id'] = filter_dict_os.pop('tenant_id')
2142 sf_dict = self.neutron.list_sfc_port_pair_groups(**filter_dict_os)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00002143 sf_list = sf_dict["port_pair_groups"]
2144 self.__sf_os2mano(sf_list)
2145 return sf_list
2146 except (neExceptions.ConnectionFailed, ksExceptions.ClientException,
2147 neExceptions.NeutronException, ConnectionError) as e:
2148 self._format_exception(e)
2149
2150 def delete_sf(self, sf_id):
2151 self.logger.debug("Deleting Service Function '%s' from VIM", sf_id)
2152 try:
2153 self._reload_connection()
Igor D.Ccaadc442017-11-06 12:48:48 +00002154 self.neutron.delete_sfc_port_pair_group(sf_id)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00002155 return sf_id
2156 except (neExceptions.ConnectionFailed, neExceptions.NeutronException,
2157 ksExceptions.ClientException, neExceptions.NeutronException,
2158 ConnectionError) as e:
2159 self._format_exception(e)
2160
2161 def new_sfp(self, name, classifications, sfs, sfc_encap=True, spi=None):
tierno7d782ef2019-10-04 12:56:31 +00002162 self.logger.debug("Adding a new Service Function Path to VIM, named '%s'", name)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00002163 try:
2164 new_sfp = None
2165 self._reload_connection()
Igor D.Ccaadc442017-11-06 12:48:48 +00002166 # In networking-sfc the MPLS encapsulation is legacy
2167 # should be used when no full SFC Encapsulation is intended
schillinge981df9a2019-01-24 09:25:11 +01002168 correlation = 'mpls'
Igor D.Ccaadc442017-11-06 12:48:48 +00002169 if sfc_encap:
2170 correlation = 'nsh'
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00002171 sfp_dict = {'name': name,
2172 'flow_classifiers': classifications,
2173 'port_pair_groups': sfs,
2174 'chain_parameters': {'correlation': correlation}}
2175 if spi:
2176 sfp_dict['chain_id'] = spi
Igor D.Ccaadc442017-11-06 12:48:48 +00002177 new_sfp = self.neutron.create_sfc_port_chain({'port_chain': sfp_dict})
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00002178 return new_sfp["port_chain"]["id"]
2179 except (neExceptions.ConnectionFailed, ksExceptions.ClientException,
2180 neExceptions.NeutronException, ConnectionError) as e:
2181 if new_sfp:
2182 try:
Igor D.Ccaadc442017-11-06 12:48:48 +00002183 self.neutron.delete_sfc_port_chain(new_sfp['port_chain']['id'])
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00002184 except Exception:
2185 self.logger.error(
2186 'Creation of Service Function Path failed, with '
2187 'subsequent deletion failure as well.')
2188 self._format_exception(e)
2189
2190 def get_sfp(self, sfp_id):
2191 self.logger.debug(" Getting Service Function Path %s from VIM", sfp_id)
2192 filter_dict = {"id": sfp_id}
2193 sfp_list = self.get_sfp_list(filter_dict)
2194 if len(sfp_list) == 0:
2195 raise vimconn.vimconnNotFoundException(
2196 "Service Function Path '{}' not found".format(sfp_id))
2197 elif len(sfp_list) > 1:
2198 raise vimconn.vimconnConflictException(
2199 "Found more than one Service Function Path with this criteria")
2200 sfp = sfp_list[0]
2201 return sfp
2202
2203 def get_sfp_list(self, filter_dict={}):
tierno7d782ef2019-10-04 12:56:31 +00002204 self.logger.debug("Getting Service Function Paths from VIM filter: '%s'", str(filter_dict))
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00002205 try:
2206 self._reload_connection()
tierno69b590e2018-03-13 18:52:23 +01002207 filter_dict_os = filter_dict.copy()
2208 if self.api_version3 and "tenant_id" in filter_dict_os:
2209 filter_dict_os['project_id'] = filter_dict_os.pop('tenant_id')
2210 sfp_dict = self.neutron.list_sfc_port_chains(**filter_dict_os)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00002211 sfp_list = sfp_dict["port_chains"]
2212 self.__sfp_os2mano(sfp_list)
2213 return sfp_list
2214 except (neExceptions.ConnectionFailed, ksExceptions.ClientException,
2215 neExceptions.NeutronException, ConnectionError) as e:
2216 self._format_exception(e)
2217
2218 def delete_sfp(self, sfp_id):
tierno7d782ef2019-10-04 12:56:31 +00002219 self.logger.debug("Deleting Service Function Path '%s' from VIM", sfp_id)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00002220 try:
2221 self._reload_connection()
Igor D.Ccaadc442017-11-06 12:48:48 +00002222 self.neutron.delete_sfc_port_chain(sfp_id)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00002223 return sfp_id
2224 except (neExceptions.ConnectionFailed, neExceptions.NeutronException,
2225 ksExceptions.ClientException, neExceptions.NeutronException,
2226 ConnectionError) as e:
2227 self._format_exception(e)