blob: 8d87b7f744341c9dd48ee4abd16166b5409639e6 [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.
19#
20# For those usages not covered by the Apache License, Version 2.0 please
21# contact with: nfvlabs@tid.es
22##
23
24'''
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +000025osconnector implements all the methods to interact with openstack using the python-neutronclient.
26
27For the VNF forwarding graph, The OpenStack VIM connector calls the
28networking-sfc Neutron extension methods, whose resources are mapped
29to the VIM connector's SFC resources as follows:
30- Classification (OSM) -> Flow Classifier (Neutron)
31- Service Function Instance (OSM) -> Port Pair (Neutron)
32- Service Function (OSM) -> Port Pair Group (Neutron)
33- Service Function Path (OSM) -> Port Chain (Neutron)
tierno7edb6752016-03-21 17:37:52 +010034'''
Eduardo Sousae3c0dbc2018-09-03 11:56:07 +010035__author__ = "Alfonso Tierno, Gerardo Garcia, Pablo Montes, xFlow Research, Igor D.C., Eduardo Sousa"
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +000036__date__ = "$22-sep-2017 23:59:59$"
tierno7edb6752016-03-21 17:37:52 +010037
38import vimconn
tierno69b590e2018-03-13 18:52:23 +010039# import json
tiernoae4a8d12016-07-08 12:30:39 +020040import logging
garciadeblas9f8456e2016-09-05 05:02:59 +020041import netaddr
montesmoreno0c8def02016-12-22 12:16:23 +000042import time
tierno36c0b172017-01-12 18:32:28 +010043import yaml
garciadeblas2299e3b2017-01-26 14:35:55 +000044import random
kate721d79b2017-06-24 04:21:38 -070045import re
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +000046import copy
Anderson Bravalheri0446cd52018-08-17 15:26:19 +010047from pprint import pformat
48from types import StringTypes
tierno7edb6752016-03-21 17:37:52 +010049
tiernob5cef372017-06-19 15:52:22 +020050from novaclient import client as nClient, exceptions as nvExceptions
51from keystoneauth1.identity import v2, v3
52from keystoneauth1 import session
tierno7edb6752016-03-21 17:37:52 +010053import keystoneclient.exceptions as ksExceptions
tiernof716aea2017-06-21 18:01:40 +020054import keystoneclient.v3.client as ksClient_v3
55import keystoneclient.v2_0.client as ksClient_v2
tiernob5cef372017-06-19 15:52:22 +020056from glanceclient import client as glClient
tierno7edb6752016-03-21 17:37:52 +010057import glanceclient.exc as gl1Exceptions
tiernob5cef372017-06-19 15:52:22 +020058from cinderclient import client as cClient
tierno7edb6752016-03-21 17:37:52 +010059from httplib import HTTPException
tiernob5cef372017-06-19 15:52:22 +020060from neutronclient.neutron import client as neClient
tierno7edb6752016-03-21 17:37:52 +010061from neutronclient.common import exceptions as neExceptions
62from requests.exceptions import ConnectionError
63
tierno40e1bce2017-08-09 09:12:04 +020064
65"""contain the openstack virtual machine status to openmano status"""
tierno7edb6752016-03-21 17:37:52 +010066vmStatus2manoFormat={'ACTIVE':'ACTIVE',
67 'PAUSED':'PAUSED',
68 'SUSPENDED': 'SUSPENDED',
69 'SHUTOFF':'INACTIVE',
70 'BUILD':'BUILD',
71 'ERROR':'ERROR','DELETED':'DELETED'
72 }
73netStatus2manoFormat={'ACTIVE':'ACTIVE','PAUSED':'PAUSED','INACTIVE':'INACTIVE','BUILD':'BUILD','ERROR':'ERROR','DELETED':'DELETED'
74 }
75
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +000076supportedClassificationTypes = ['legacy_flow_classifier']
77
montesmoreno0c8def02016-12-22 12:16:23 +000078#global var to have a timeout creating and deleting volumes
tierno00e3df72017-11-29 17:20:13 +010079volume_timeout = 600
80server_timeout = 600
montesmoreno0c8def02016-12-22 12:16:23 +000081
Anderson Bravalheri0446cd52018-08-17 15:26:19 +010082
83class SafeDumper(yaml.SafeDumper):
84 def represent_data(self, data):
85 # Openstack APIs use custom subclasses of dict and YAML safe dumper
86 # is designed to not handle that (reference issue 142 of pyyaml)
87 if isinstance(data, dict) and data.__class__ != dict:
88 # A simple solution is to convert those items back to dicts
89 data = dict(data.items())
90
91 return super(SafeDumper, self).represent_data(data)
92
93
tierno7edb6752016-03-21 17:37:52 +010094class vimconnector(vimconn.vimconnector):
tiernob3d36742017-03-03 23:51:05 +010095 def __init__(self, uuid, name, tenant_id, tenant_name, url, url_admin=None, user=None, passwd=None,
96 log_level=None, config={}, persistent_info={}):
ahmadsa96af9f42017-01-31 16:17:14 +050097 '''using common constructor parameters. In this case
tierno7edb6752016-03-21 17:37:52 +010098 'url' is the keystone authorization url,
99 'url_admin' is not use
100 '''
tiernof716aea2017-06-21 18:01:40 +0200101 api_version = config.get('APIversion')
102 if api_version and api_version not in ('v3.3', 'v2.0', '2', '3'):
tiernob5cef372017-06-19 15:52:22 +0200103 raise vimconn.vimconnException("Invalid value '{}' for config:APIversion. "
tiernof716aea2017-06-21 18:01:40 +0200104 "Allowed values are 'v3.3', 'v2.0', '2' or '3'".format(api_version))
kate721d79b2017-06-24 04:21:38 -0700105 vim_type = config.get('vim_type')
106 if vim_type and vim_type not in ('vio', 'VIO'):
107 raise vimconn.vimconnException("Invalid value '{}' for config:vim_type."
108 "Allowed values are 'vio' or 'VIO'".format(vim_type))
109
110 if config.get('dataplane_net_vlan_range') is not None:
111 #validate vlan ranges provided by user
garciadeblasebd66722019-01-31 16:01:31 +0000112 self._validate_vlan_ranges(config.get('dataplane_net_vlan_range'), 'dataplane_net_vlan_range')
113
114 if config.get('multisegment_vlan_range') is not None:
115 #validate vlan ranges provided by user
116 self._validate_vlan_ranges(config.get('multisegment_vlan_range'), 'multisegment_vlan_range')
kate721d79b2017-06-24 04:21:38 -0700117
tiernob5cef372017-06-19 15:52:22 +0200118 vimconn.vimconnector.__init__(self, uuid, name, tenant_id, tenant_name, url, url_admin, user, passwd, log_level,
119 config)
tiernob3d36742017-03-03 23:51:05 +0100120
tierno4d1ce222018-04-06 10:41:06 +0200121 if self.config.get("insecure") and self.config.get("ca_cert"):
122 raise vimconn.vimconnException("options insecure and ca_cert are mutually exclusive")
123 self.verify = True
124 if self.config.get("insecure"):
125 self.verify = False
126 if self.config.get("ca_cert"):
127 self.verify = self.config.get("ca_cert")
tierno4d1ce222018-04-06 10:41:06 +0200128
tierno7edb6752016-03-21 17:37:52 +0100129 if not url:
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +0000130 raise TypeError('url param can not be NoneType')
tiernob5cef372017-06-19 15:52:22 +0200131 self.persistent_info = persistent_info
mirabal29356312017-07-27 12:21:22 +0200132 self.availability_zone = persistent_info.get('availability_zone', None)
tiernob5cef372017-06-19 15:52:22 +0200133 self.session = persistent_info.get('session', {'reload_client': True})
tiernoa05b65a2019-02-01 12:30:27 +0000134 self.my_tenant_id = self.session.get('my_tenant_id')
tiernob5cef372017-06-19 15:52:22 +0200135 self.nova = self.session.get('nova')
136 self.neutron = self.session.get('neutron')
137 self.cinder = self.session.get('cinder')
138 self.glance = self.session.get('glance')
tierno1beea862018-07-11 15:47:37 +0200139 # self.glancev1 = self.session.get('glancev1')
tiernof716aea2017-06-21 18:01:40 +0200140 self.keystone = self.session.get('keystone')
141 self.api_version3 = self.session.get('api_version3')
kate721d79b2017-06-24 04:21:38 -0700142 self.vim_type = self.config.get("vim_type")
143 if self.vim_type:
144 self.vim_type = self.vim_type.upper()
145 if self.config.get("use_internal_endpoint"):
146 self.endpoint_type = "internalURL"
147 else:
148 self.endpoint_type = None
montesmoreno0c8def02016-12-22 12:16:23 +0000149
tierno73ad9e42016-09-12 18:11:11 +0200150 self.logger = logging.getLogger('openmano.vim.openstack')
kate721d79b2017-06-24 04:21:38 -0700151
tiernoa05b65a2019-02-01 12:30:27 +0000152 # allow security_groups to be a list or a single string
153 if isinstance(self.config.get('security_groups'), str):
154 self.config['security_groups'] = [self.config['security_groups']]
155 self.security_groups_id = None
156
kate721d79b2017-06-24 04:21:38 -0700157 ####### VIO Specific Changes #########
158 if self.vim_type == "VIO":
159 self.logger = logging.getLogger('openmano.vim.vio')
160
tiernofe789902016-09-29 14:20:44 +0000161 if log_level:
kate54616752017-09-05 23:26:28 -0700162 self.logger.setLevel( getattr(logging, log_level))
tiernof716aea2017-06-21 18:01:40 +0200163
164 def __getitem__(self, index):
165 """Get individuals parameters.
166 Throw KeyError"""
167 if index == 'project_domain_id':
168 return self.config.get("project_domain_id")
169 elif index == 'user_domain_id':
170 return self.config.get("user_domain_id")
171 else:
tierno76a3c312017-06-29 16:42:15 +0200172 return vimconn.vimconnector.__getitem__(self, index)
tiernof716aea2017-06-21 18:01:40 +0200173
174 def __setitem__(self, index, value):
175 """Set individuals parameters and it is marked as dirty so to force connection reload.
176 Throw KeyError"""
177 if index == 'project_domain_id':
178 self.config["project_domain_id"] = value
179 elif index == 'user_domain_id':
180 self.config["user_domain_id"] = value
181 else:
182 vimconn.vimconnector.__setitem__(self, index, value)
tiernob5cef372017-06-19 15:52:22 +0200183 self.session['reload_client'] = True
tiernof716aea2017-06-21 18:01:40 +0200184
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100185 def serialize(self, value):
186 """Serialization of python basic types.
187
188 In the case value is not serializable a message will be logged and a
189 simple representation of the data that cannot be converted back to
190 python is returned.
191 """
192 if isinstance(value, StringTypes):
193 return value
194
195 try:
196 return yaml.dump(value, Dumper=SafeDumper,
197 default_flow_style=True, width=256)
198 except yaml.representer.RepresenterError:
199 self.logger.debug(
200 'The following entity cannot be serialized in YAML:'
201 '\n\n%s\n\n', pformat(value), exc_info=True)
202 return str(value)
203
tierno7edb6752016-03-21 17:37:52 +0100204 def _reload_connection(self):
205 '''Called before any operation, it check if credentials has changed
206 Throw keystoneclient.apiclient.exceptions.AuthorizationFailure
207 '''
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100208 #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 +0200209 if self.session['reload_client']:
tiernof716aea2017-06-21 18:01:40 +0200210 if self.config.get('APIversion'):
211 self.api_version3 = self.config['APIversion'] == 'v3.3' or self.config['APIversion'] == '3'
212 else: # get from ending auth_url that end with v3 or with v2.0
tierno3cb8dc32017-10-24 18:13:19 +0200213 self.api_version3 = self.url.endswith("/v3") or self.url.endswith("/v3/")
tiernof716aea2017-06-21 18:01:40 +0200214 self.session['api_version3'] = self.api_version3
215 if self.api_version3:
tierno3cb8dc32017-10-24 18:13:19 +0200216 if self.config.get('project_domain_id') or self.config.get('project_domain_name'):
217 project_domain_id_default = None
218 else:
219 project_domain_id_default = 'default'
220 if self.config.get('user_domain_id') or self.config.get('user_domain_name'):
221 user_domain_id_default = None
222 else:
223 user_domain_id_default = 'default'
tiernof716aea2017-06-21 18:01:40 +0200224 auth = v3.Password(auth_url=self.url,
tiernob5cef372017-06-19 15:52:22 +0200225 username=self.user,
226 password=self.passwd,
227 project_name=self.tenant_name,
228 project_id=self.tenant_id,
tierno3cb8dc32017-10-24 18:13:19 +0200229 project_domain_id=self.config.get('project_domain_id', project_domain_id_default),
230 user_domain_id=self.config.get('user_domain_id', user_domain_id_default),
231 project_domain_name=self.config.get('project_domain_name'),
232 user_domain_name=self.config.get('user_domain_name'))
ahmadsa95baa272016-11-30 09:14:11 +0500233 else:
tiernof716aea2017-06-21 18:01:40 +0200234 auth = v2.Password(auth_url=self.url,
tiernob5cef372017-06-19 15:52:22 +0200235 username=self.user,
236 password=self.passwd,
237 tenant_name=self.tenant_name,
238 tenant_id=self.tenant_id)
tierno4d1ce222018-04-06 10:41:06 +0200239 sess = session.Session(auth=auth, verify=self.verify)
fatollahy40c6a3f2019-02-19 12:53:40 +0000240 # addedd region_name to keystone, nova, neutron and cinder to support distributed cloud for Wind River Titanium cloud and StarlingX
241 region_name = self.config.get('region_name')
tiernof716aea2017-06-21 18:01:40 +0200242 if self.api_version3:
fatollahy40c6a3f2019-02-19 12:53:40 +0000243 self.keystone = ksClient_v3.Client(session=sess, endpoint_type=self.endpoint_type, region_name=region_name)
tiernof716aea2017-06-21 18:01:40 +0200244 else:
kate721d79b2017-06-24 04:21:38 -0700245 self.keystone = ksClient_v2.Client(session=sess, endpoint_type=self.endpoint_type)
tiernof716aea2017-06-21 18:01:40 +0200246 self.session['keystone'] = self.keystone
montesmoreno9317d302017-08-16 12:48:23 +0200247 # In order to enable microversion functionality an explicit microversion must be specified in 'config'.
248 # This implementation approach is due to the warning message in
249 # https://developer.openstack.org/api-guide/compute/microversions.html
250 # where it is stated that microversion backwards compatibility is not guaranteed and clients should
251 # always require an specific microversion.
252 # To be able to use 'device role tagging' functionality define 'microversion: 2.32' in datacenter config
253 version = self.config.get("microversion")
254 if not version:
255 version = "2.1"
fatollahy40c6a3f2019-02-19 12:53:40 +0000256 # addedd region_name to keystone, nova, neutron and cinder to support distributed cloud for Wind River Titanium cloud and StarlingX
257 self.nova = self.session['nova'] = nClient.Client(str(version), session=sess, endpoint_type=self.endpoint_type, region_name=region_name)
258 self.neutron = self.session['neutron'] = neClient.Client('2.0', session=sess, endpoint_type=self.endpoint_type, region_name=region_name)
259 self.cinder = self.session['cinder'] = cClient.Client(2, session=sess, endpoint_type=self.endpoint_type, region_name=region_name)
tiernoa05b65a2019-02-01 12:30:27 +0000260 try:
261 self.my_tenant_id = self.session['my_tenant_id'] = sess.get_project_id()
262 except Exception as e:
263 self.logger.error("Cannot get project_id from session", exc_info=True)
kate721d79b2017-06-24 04:21:38 -0700264 if self.endpoint_type == "internalURL":
265 glance_service_id = self.keystone.services.list(name="glance")[0].id
266 glance_endpoint = self.keystone.endpoints.list(glance_service_id, interface="internal")[0].url
267 else:
268 glance_endpoint = None
269 self.glance = self.session['glance'] = glClient.Client(2, session=sess, endpoint=glance_endpoint)
tiernoa05b65a2019-02-01 12:30:27 +0000270 # using version 1 of glance client in new_image()
tierno1beea862018-07-11 15:47:37 +0200271 # self.glancev1 = self.session['glancev1'] = glClient.Client('1', session=sess,
272 # endpoint=glance_endpoint)
tiernob5cef372017-06-19 15:52:22 +0200273 self.session['reload_client'] = False
274 self.persistent_info['session'] = self.session
mirabal29356312017-07-27 12:21:22 +0200275 # add availablity zone info inside self.persistent_info
276 self._set_availablity_zones()
277 self.persistent_info['availability_zone'] = self.availability_zone
tiernoa05b65a2019-02-01 12:30:27 +0000278 self.security_groups_id = None # force to get again security_groups_ids next time they are needed
ahmadsa95baa272016-11-30 09:14:11 +0500279
tierno7edb6752016-03-21 17:37:52 +0100280 def __net_os2mano(self, net_list_dict):
281 '''Transform the net openstack format to mano format
282 net_list_dict can be a list of dict or a single dict'''
283 if type(net_list_dict) is dict:
284 net_list_=(net_list_dict,)
285 elif type(net_list_dict) is list:
286 net_list_=net_list_dict
287 else:
288 raise TypeError("param net_list_dict must be a list or a dictionary")
289 for net in net_list_:
290 if net.get('provider:network_type') == "vlan":
291 net['type']='data'
292 else:
293 net['type']='bridge'
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +0200294
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +0000295 def __classification_os2mano(self, class_list_dict):
296 """Transform the openstack format (Flow Classifier) to mano format
297 (Classification) class_list_dict can be a list of dict or a single dict
298 """
299 if isinstance(class_list_dict, dict):
300 class_list_ = [class_list_dict]
301 elif isinstance(class_list_dict, list):
302 class_list_ = class_list_dict
303 else:
304 raise TypeError(
305 "param class_list_dict must be a list or a dictionary")
306 for classification in class_list_:
307 id = classification.pop('id')
308 name = classification.pop('name')
309 description = classification.pop('description')
310 project_id = classification.pop('project_id')
311 tenant_id = classification.pop('tenant_id')
312 original_classification = copy.deepcopy(classification)
313 classification.clear()
314 classification['ctype'] = 'legacy_flow_classifier'
315 classification['definition'] = original_classification
316 classification['id'] = id
317 classification['name'] = name
318 classification['description'] = description
319 classification['project_id'] = project_id
320 classification['tenant_id'] = tenant_id
321
322 def __sfi_os2mano(self, sfi_list_dict):
323 """Transform the openstack format (Port Pair) to mano format (SFI)
324 sfi_list_dict can be a list of dict or a single dict
325 """
326 if isinstance(sfi_list_dict, dict):
327 sfi_list_ = [sfi_list_dict]
328 elif isinstance(sfi_list_dict, list):
329 sfi_list_ = sfi_list_dict
330 else:
331 raise TypeError(
332 "param sfi_list_dict must be a list or a dictionary")
333 for sfi in sfi_list_:
334 sfi['ingress_ports'] = []
335 sfi['egress_ports'] = []
336 if sfi.get('ingress'):
337 sfi['ingress_ports'].append(sfi['ingress'])
338 if sfi.get('egress'):
339 sfi['egress_ports'].append(sfi['egress'])
340 del sfi['ingress']
341 del sfi['egress']
342 params = sfi.get('service_function_parameters')
343 sfc_encap = False
344 if params:
345 correlation = params.get('correlation')
346 if correlation:
347 sfc_encap = True
348 sfi['sfc_encap'] = sfc_encap
349 del sfi['service_function_parameters']
350
351 def __sf_os2mano(self, sf_list_dict):
352 """Transform the openstack format (Port Pair Group) to mano format (SF)
353 sf_list_dict can be a list of dict or a single dict
354 """
355 if isinstance(sf_list_dict, dict):
356 sf_list_ = [sf_list_dict]
357 elif isinstance(sf_list_dict, list):
358 sf_list_ = sf_list_dict
359 else:
360 raise TypeError(
361 "param sf_list_dict must be a list or a dictionary")
362 for sf in sf_list_:
363 del sf['port_pair_group_parameters']
364 sf['sfis'] = sf['port_pairs']
365 del sf['port_pairs']
366
367 def __sfp_os2mano(self, sfp_list_dict):
368 """Transform the openstack format (Port Chain) to mano format (SFP)
369 sfp_list_dict can be a list of dict or a single dict
370 """
371 if isinstance(sfp_list_dict, dict):
372 sfp_list_ = [sfp_list_dict]
373 elif isinstance(sfp_list_dict, list):
374 sfp_list_ = sfp_list_dict
375 else:
376 raise TypeError(
377 "param sfp_list_dict must be a list or a dictionary")
378 for sfp in sfp_list_:
379 params = sfp.pop('chain_parameters')
380 sfc_encap = False
381 if params:
382 correlation = params.get('correlation')
383 if correlation:
384 sfc_encap = True
385 sfp['sfc_encap'] = sfc_encap
386 sfp['spi'] = sfp.pop('chain_id')
387 sfp['classifications'] = sfp.pop('flow_classifiers')
388 sfp['service_functions'] = sfp.pop('port_pair_groups')
389
390 # placeholder for now; read TODO note below
391 def _validate_classification(self, type, definition):
392 # only legacy_flow_classifier Type is supported at this point
393 return True
394 # TODO(igordcard): this method should be an abstract method of an
395 # abstract Classification class to be implemented by the specific
396 # Types. Also, abstract vimconnector should call the validation
397 # method before the implemented VIM connectors are called.
398
tiernoae4a8d12016-07-08 12:30:39 +0200399 def _format_exception(self, exception):
400 '''Transform a keystone, nova, neutron exception into a vimconn exception'''
tiernode12f782019-04-05 12:46:42 +0000401
402 # Fixing bug 665 https://osm.etsi.org/bugzilla/show_bug.cgi?id=665
403 # There are some openstack versions that message error are unicode with non English
404 message_error = exception.message
405 if isinstance(message_error, unicode):
406 message_error = message_error.encode("utf")
407
408 if isinstance(exception, (neExceptions.NetworkNotFoundClient, nvExceptions.NotFound, ksExceptions.NotFound,
409 gl1Exceptions.HTTPNotFound)):
410 raise vimconn.vimconnNotFoundException(type(exception).__name__ + ": " + message_error)
shashankjain3c83a212018-10-04 13:05:46 +0530411 elif isinstance(exception, (HTTPException, gl1Exceptions.HTTPException, gl1Exceptions.CommunicationError,
412 ConnectionError, ksExceptions.ConnectionError, neExceptions.ConnectionFailed)):
tiernode12f782019-04-05 12:46:42 +0000413 raise vimconn.vimconnConnectionException(type(exception).__name__ + ": " + message_error)
shashankjain3c83a212018-10-04 13:05:46 +0530414 elif isinstance(exception, (KeyError, nvExceptions.BadRequest, ksExceptions.BadRequest)):
tiernode12f782019-04-05 12:46:42 +0000415 raise vimconn.vimconnException(type(exception).__name__ + ": " + message_error)
anwarsc76a3ee2018-10-04 14:05:32 +0530416 elif isinstance(exception, (nvExceptions.ClientException, ksExceptions.ClientException,
417 neExceptions.NeutronException)):
tiernode12f782019-04-05 12:46:42 +0000418 raise vimconn.vimconnUnexpectedResponse(type(exception).__name__ + ": " + message_error)
tiernoae4a8d12016-07-08 12:30:39 +0200419 elif isinstance(exception, nvExceptions.Conflict):
tiernode12f782019-04-05 12:46:42 +0000420 raise vimconn.vimconnConflictException(type(exception).__name__ + ": " + message_error)
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +0200421 elif isinstance(exception, vimconn.vimconnException):
tierno41a69812018-02-16 14:34:33 +0100422 raise exception
tiernof716aea2017-06-21 18:01:40 +0200423 else: # ()
tiernode12f782019-04-05 12:46:42 +0000424 self.logger.error("General Exception " + message_error, exc_info=True)
425 raise vimconn.vimconnConnectionException(type(exception).__name__ + ": " + message_error)
tiernoae4a8d12016-07-08 12:30:39 +0200426
tiernoa05b65a2019-02-01 12:30:27 +0000427 def _get_ids_from_name(self):
428 """
429 Obtain ids from name of tenant and security_groups. Store at self .security_groups_id"
430 :return: None
431 """
432 # get tenant_id if only tenant_name is supplied
433 self._reload_connection()
434 if not self.my_tenant_id:
435 raise vimconn.vimconnConnectionException("Error getting tenant information from name={} id={}".
436 format(self.tenant_name, self.tenant_id))
437 if self.config.get('security_groups') and not self.security_groups_id:
438 # convert from name to id
439 neutron_sg_list = self.neutron.list_security_groups(tenant_id=self.my_tenant_id)["security_groups"]
440
441 self.security_groups_id = []
442 for sg in self.config.get('security_groups'):
443 for neutron_sg in neutron_sg_list:
444 if sg in (neutron_sg["id"], neutron_sg["name"]):
445 self.security_groups_id.append(neutron_sg["id"])
446 break
447 else:
448 self.security_groups_id = None
449 raise vimconn.vimconnConnectionException("Not found security group {} for this tenant".format(sg))
450
tierno5509c2e2019-07-04 16:23:20 +0000451 def check_vim_connectivity(self):
452 # just get network list to check connectivity and credentials
453 self.get_network_list(filter_dict={})
454
tiernoae4a8d12016-07-08 12:30:39 +0200455 def get_tenant_list(self, filter_dict={}):
456 '''Obtain tenants of VIM
457 filter_dict can contain the following keys:
458 name: filter by tenant name
459 id: filter by tenant uuid/id
460 <other VIM specific>
461 Returns the tenant list of dictionaries: [{'name':'<name>, 'id':'<id>, ...}, ...]
462 '''
ahmadsa95baa272016-11-30 09:14:11 +0500463 self.logger.debug("Getting tenants from VIM filter: '%s'", str(filter_dict))
tiernoae4a8d12016-07-08 12:30:39 +0200464 try:
465 self._reload_connection()
tiernof716aea2017-06-21 18:01:40 +0200466 if self.api_version3:
467 project_class_list = self.keystone.projects.list(name=filter_dict.get("name"))
ahmadsa95baa272016-11-30 09:14:11 +0500468 else:
tiernof716aea2017-06-21 18:01:40 +0200469 project_class_list = self.keystone.tenants.findall(**filter_dict)
ahmadsa95baa272016-11-30 09:14:11 +0500470 project_list=[]
471 for project in project_class_list:
tiernof716aea2017-06-21 18:01:40 +0200472 if filter_dict.get('id') and filter_dict["id"] != project.id:
473 continue
ahmadsa95baa272016-11-30 09:14:11 +0500474 project_list.append(project.to_dict())
475 return project_list
tiernof716aea2017-06-21 18:01:40 +0200476 except (ksExceptions.ConnectionError, ksExceptions.ClientException, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200477 self._format_exception(e)
478
479 def new_tenant(self, tenant_name, tenant_description):
480 '''Adds a new tenant to openstack VIM. Returns the tenant identifier'''
481 self.logger.debug("Adding a new tenant name: %s", tenant_name)
482 try:
483 self._reload_connection()
tiernof716aea2017-06-21 18:01:40 +0200484 if self.api_version3:
485 project = self.keystone.projects.create(tenant_name, self.config.get("project_domain_id", "default"),
486 description=tenant_description, is_domain=False)
ahmadsa95baa272016-11-30 09:14:11 +0500487 else:
tiernof716aea2017-06-21 18:01:40 +0200488 project = self.keystone.tenants.create(tenant_name, tenant_description)
ahmadsa95baa272016-11-30 09:14:11 +0500489 return project.id
shashankjain3c83a212018-10-04 13:05:46 +0530490 except (ksExceptions.ConnectionError, ksExceptions.ClientException, ksExceptions.BadRequest, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200491 self._format_exception(e)
492
493 def delete_tenant(self, tenant_id):
494 '''Delete a tenant from openstack VIM. Returns the old tenant identifier'''
495 self.logger.debug("Deleting tenant %s from VIM", tenant_id)
496 try:
497 self._reload_connection()
tiernof716aea2017-06-21 18:01:40 +0200498 if self.api_version3:
ahmadsa95baa272016-11-30 09:14:11 +0500499 self.keystone.projects.delete(tenant_id)
500 else:
501 self.keystone.tenants.delete(tenant_id)
tiernoae4a8d12016-07-08 12:30:39 +0200502 return tenant_id
shashankjain3c83a212018-10-04 13:05:46 +0530503 except (ksExceptions.ConnectionError, ksExceptions.ClientException, ksExceptions.NotFound, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200504 self._format_exception(e)
ahmadsa95baa272016-11-30 09:14:11 +0500505
kbsuba85c54d2019-10-17 16:30:32 +0000506 def new_network(self,net_name, net_type, ip_profile=None, shared=False, provider_network_profile=None):
garciadeblasebd66722019-01-31 16:01:31 +0000507 """Adds a tenant network to VIM
508 Params:
509 'net_name': name of the network
510 'net_type': one of:
511 'bridge': overlay isolated network
512 'data': underlay E-LAN network for Passthrough and SRIOV interfaces
513 'ptp': underlay E-LINE network for Passthrough and SRIOV interfaces.
514 'ip_profile': is a dict containing the IP parameters of the network
515 'ip_version': can be "IPv4" or "IPv6" (Currently only IPv4 is implemented)
516 'subnet_address': ip_prefix_schema, that is X.X.X.X/Y
517 'gateway_address': (Optional) ip_schema, that is X.X.X.X
518 'dns_address': (Optional) comma separated list of ip_schema, e.g. X.X.X.X[,X,X,X,X]
519 'dhcp_enabled': True or False
520 'dhcp_start_address': ip_schema, first IP to grant
521 'dhcp_count': number of IPs to grant.
522 'shared': if this network can be seen/use by other tenants/organization
kbsuba85c54d2019-10-17 16:30:32 +0000523 'provider_network_profile': (optional) contains {segmentation-id: vlan, provider-network: vim_netowrk}
garciadeblasebd66722019-01-31 16:01:31 +0000524 Returns a tuple with the network identifier and created_items, or raises an exception on error
525 created_items can be None or a dictionary where this method can include key-values that will be passed to
526 the method delete_network. Can be used to store created segments, created l2gw connections, etc.
527 Format is vimconnector dependent, but do not use nested dictionaries and a value of None should be the same
528 as not present.
529 """
tiernoae4a8d12016-07-08 12:30:39 +0200530 self.logger.debug("Adding a new network to VIM name '%s', type '%s'", net_name, net_type)
garciadeblasebd66722019-01-31 16:01:31 +0000531 # self.logger.debug(">>>>>>>>>>>>>>>>>> IP profile %s", str(ip_profile))
kbsuba85c54d2019-10-17 16:30:32 +0000532
tierno7edb6752016-03-21 17:37:52 +0100533 try:
kbsuba85c54d2019-10-17 16:30:32 +0000534 vlan = None
535 if provider_network_profile:
536 vlan = provider_network_profile.get("segmentation-id")
garciadeblasedca7b32016-09-29 14:01:52 +0000537 new_net = None
garciadeblasebd66722019-01-31 16:01:31 +0000538 created_items = {}
tierno7edb6752016-03-21 17:37:52 +0100539 self._reload_connection()
540 network_dict = {'name': net_name, 'admin_state_up': True}
541 if net_type=="data" or net_type=="ptp":
542 if self.config.get('dataplane_physical_net') == None:
tiernoae4a8d12016-07-08 12:30:39 +0200543 raise vimconn.vimconnConflictException("You must provide a 'dataplane_physical_net' at config value before creating sriov network")
garciadeblasebd66722019-01-31 16:01:31 +0000544 if not self.config.get('multisegment_support'):
545 network_dict["provider:physical_network"] = self.config[
546 'dataplane_physical_net'] # "physnet_sriov" #TODO physical
547 network_dict["provider:network_type"] = "vlan"
548 if vlan!=None:
549 network_dict["provider:network_type"] = vlan
550 else:
551 ###### Multi-segment case ######
552 segment_list = []
553 segment1_dict = {}
554 segment1_dict["provider:physical_network"] = ''
555 segment1_dict["provider:network_type"] = 'vxlan'
556 segment_list.append(segment1_dict)
557 segment2_dict = {}
558 segment2_dict["provider:physical_network"] = self.config['dataplane_physical_net']
559 segment2_dict["provider:network_type"] = "vlan"
560 if self.config.get('multisegment_vlan_range'):
561 vlanID = self._generate_multisegment_vlanID()
562 segment2_dict["provider:segmentation_id"] = vlanID
563 # else
564 # raise vimconn.vimconnConflictException(
565 # "You must provide 'multisegment_vlan_range' at config dict before creating a multisegment network")
566 segment_list.append(segment2_dict)
567 network_dict["segments"] = segment_list
kate721d79b2017-06-24 04:21:38 -0700568
569 ####### VIO Specific Changes #########
570 if self.vim_type == "VIO":
571 if vlan is not None:
572 network_dict["provider:segmentation_id"] = vlan
573 else:
574 if self.config.get('dataplane_net_vlan_range') is None:
575 raise vimconn.vimconnConflictException("You must provide "\
576 "'dataplane_net_vlan_range' in format [start_ID - end_ID]"\
577 "at config value before creating sriov network with vlan tag")
578
garciadeblasebd66722019-01-31 16:01:31 +0000579 network_dict["provider:segmentation_id"] = self._generate_vlanID()
kate721d79b2017-06-24 04:21:38 -0700580
garciadeblasebd66722019-01-31 16:01:31 +0000581 network_dict["shared"] = shared
anwarsff168192019-05-06 11:23:07 +0530582 if self.config.get("disable_network_port_security"):
583 network_dict["port_security_enabled"] = False
garciadeblasebd66722019-01-31 16:01:31 +0000584 new_net = self.neutron.create_network({'network':network_dict})
585 # print new_net
586 # create subnetwork, even if there is no profile
garciadeblas9f8456e2016-09-05 05:02:59 +0200587 if not ip_profile:
588 ip_profile = {}
tierno41a69812018-02-16 14:34:33 +0100589 if not ip_profile.get('subnet_address'):
garciadeblas2299e3b2017-01-26 14:35:55 +0000590 #Fake subnet is required
591 subnet_rand = random.randint(0, 255)
592 ip_profile['subnet_address'] = "192.168.{}.0/24".format(subnet_rand)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +0000593 if 'ip_version' not in ip_profile:
garciadeblas9f8456e2016-09-05 05:02:59 +0200594 ip_profile['ip_version'] = "IPv4"
garciadeblasebd66722019-01-31 16:01:31 +0000595 subnet = {"name": net_name+"-subnet",
tierno7edb6752016-03-21 17:37:52 +0100596 "network_id": new_net["network"]["id"],
garciadeblas9f8456e2016-09-05 05:02:59 +0200597 "ip_version": 4 if ip_profile['ip_version']=="IPv4" else 6,
598 "cidr": ip_profile['subnet_address']
tierno7edb6752016-03-21 17:37:52 +0100599 }
tiernoa1fb4462017-06-30 12:25:50 +0200600 # Gateway should be set to None if not needed. Otherwise openstack assigns one by default
tierno41a69812018-02-16 14:34:33 +0100601 if ip_profile.get('gateway_address'):
tierno55d234c2018-07-04 18:29:21 +0200602 subnet['gateway_ip'] = ip_profile['gateway_address']
603 else:
604 subnet['gateway_ip'] = None
garciadeblasedca7b32016-09-29 14:01:52 +0000605 if ip_profile.get('dns_address'):
tierno455612d2017-05-30 16:40:10 +0200606 subnet['dns_nameservers'] = ip_profile['dns_address'].split(";")
garciadeblas9f8456e2016-09-05 05:02:59 +0200607 if 'dhcp_enabled' in ip_profile:
tierno41a69812018-02-16 14:34:33 +0100608 subnet['enable_dhcp'] = False if \
609 ip_profile['dhcp_enabled']=="false" or ip_profile['dhcp_enabled']==False else True
610 if ip_profile.get('dhcp_start_address'):
tiernoa1fb4462017-06-30 12:25:50 +0200611 subnet['allocation_pools'] = []
garciadeblas9f8456e2016-09-05 05:02:59 +0200612 subnet['allocation_pools'].append(dict())
613 subnet['allocation_pools'][0]['start'] = ip_profile['dhcp_start_address']
tierno41a69812018-02-16 14:34:33 +0100614 if ip_profile.get('dhcp_count'):
garciadeblas9f8456e2016-09-05 05:02:59 +0200615 #parts = ip_profile['dhcp_start_address'].split('.')
616 #ip_int = (int(parts[0]) << 24) + (int(parts[1]) << 16) + (int(parts[2]) << 8) + int(parts[3])
617 ip_int = int(netaddr.IPAddress(ip_profile['dhcp_start_address']))
garciadeblas21d795b2016-09-29 17:31:46 +0200618 ip_int += ip_profile['dhcp_count'] - 1
garciadeblas9f8456e2016-09-05 05:02:59 +0200619 ip_str = str(netaddr.IPAddress(ip_int))
620 subnet['allocation_pools'][0]['end'] = ip_str
garciadeblasedca7b32016-09-29 14:01:52 +0000621 #self.logger.debug(">>>>>>>>>>>>>>>>>> Subnet: %s", str(subnet))
tierno7edb6752016-03-21 17:37:52 +0100622 self.neutron.create_subnet({"subnet": subnet} )
garciadeblasebd66722019-01-31 16:01:31 +0000623
624 if net_type == "data" and self.config.get('multisegment_support'):
625 if self.config.get('l2gw_support'):
626 l2gw_list = self.neutron.list_l2_gateways().get("l2_gateways", ())
627 for l2gw in l2gw_list:
628 l2gw_conn = {}
629 l2gw_conn["l2_gateway_id"] = l2gw["id"]
630 l2gw_conn["network_id"] = new_net["network"]["id"]
631 l2gw_conn["segmentation_id"] = str(vlanID)
632 new_l2gw_conn = self.neutron.create_l2_gateway_connection({"l2_gateway_connection": l2gw_conn})
633 created_items["l2gwconn:" + str(new_l2gw_conn["l2_gateway_connection"]["id"])] = True
634 return new_net["network"]["id"], created_items
tierno41a69812018-02-16 14:34:33 +0100635 except Exception as e:
garciadeblasebd66722019-01-31 16:01:31 +0000636 #delete l2gw connections (if any) before deleting the network
637 for k, v in created_items.items():
638 if not v: # skip already deleted
639 continue
640 try:
641 k_item, _, k_id = k.partition(":")
642 if k_item == "l2gwconn":
643 self.neutron.delete_l2_gateway_connection(k_id)
644 except Exception as e2:
645 self.logger.error("Error deleting l2 gateway connection: {}: {}".format(type(e2).__name__, e2))
garciadeblasedca7b32016-09-29 14:01:52 +0000646 if new_net:
647 self.neutron.delete_network(new_net['network']['id'])
tiernoae4a8d12016-07-08 12:30:39 +0200648 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100649
650 def get_network_list(self, filter_dict={}):
651 '''Obtain tenant networks of VIM
652 Filter_dict can be:
653 name: network name
654 id: network uuid
655 shared: boolean
656 tenant_id: tenant
657 admin_state_up: boolean
658 status: 'ACTIVE'
659 Returns the network list of dictionaries
660 '''
tiernoae4a8d12016-07-08 12:30:39 +0200661 self.logger.debug("Getting network from VIM filter: '%s'", str(filter_dict))
tierno7edb6752016-03-21 17:37:52 +0100662 try:
663 self._reload_connection()
tierno69b590e2018-03-13 18:52:23 +0100664 filter_dict_os = filter_dict.copy()
665 if self.api_version3 and "tenant_id" in filter_dict_os:
666 filter_dict_os['project_id'] = filter_dict_os.pop('tenant_id') #T ODO check
667 net_dict = self.neutron.list_networks(**filter_dict_os)
tierno00e3df72017-11-29 17:20:13 +0100668 net_list = net_dict["networks"]
tierno7edb6752016-03-21 17:37:52 +0100669 self.__net_os2mano(net_list)
tiernoae4a8d12016-07-08 12:30:39 +0200670 return net_list
tierno8e995ce2016-09-22 08:13:00 +0000671 except (neExceptions.ConnectionFailed, ksExceptions.ClientException, neExceptions.NeutronException, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200672 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100673
tiernoae4a8d12016-07-08 12:30:39 +0200674 def get_network(self, net_id):
675 '''Obtain details of network from VIM
676 Returns the network information from a network id'''
677 self.logger.debug(" Getting tenant network %s from VIM", net_id)
tierno7edb6752016-03-21 17:37:52 +0100678 filter_dict={"id": net_id}
tiernoae4a8d12016-07-08 12:30:39 +0200679 net_list = self.get_network_list(filter_dict)
tierno7edb6752016-03-21 17:37:52 +0100680 if len(net_list)==0:
tiernoae4a8d12016-07-08 12:30:39 +0200681 raise vimconn.vimconnNotFoundException("Network '{}' not found".format(net_id))
tierno7edb6752016-03-21 17:37:52 +0100682 elif len(net_list)>1:
tiernoae4a8d12016-07-08 12:30:39 +0200683 raise vimconn.vimconnConflictException("Found more than one network with this criteria")
tierno7edb6752016-03-21 17:37:52 +0100684 net = net_list[0]
685 subnets=[]
686 for subnet_id in net.get("subnets", () ):
687 try:
688 subnet = self.neutron.show_subnet(subnet_id)
689 except Exception as e:
tiernoae4a8d12016-07-08 12:30:39 +0200690 self.logger.error("osconnector.get_network(): Error getting subnet %s %s" % (net_id, str(e)))
691 subnet = {"id": subnet_id, "fault": str(e)}
tierno7edb6752016-03-21 17:37:52 +0100692 subnets.append(subnet)
693 net["subnets"] = subnets
Pablo Montes Moreno51e553b2017-03-23 16:39:12 +0100694 net["encapsulation"] = net.get('provider:network_type')
Anderson Bravalheri0fb70282018-12-16 19:28:37 +0000695 net["encapsulation_type"] = net.get('provider:network_type')
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +0100696 net["segmentation_id"] = net.get('provider:segmentation_id')
Anderson Bravalheri0fb70282018-12-16 19:28:37 +0000697 net["encapsulation_id"] = net.get('provider:segmentation_id')
tiernoae4a8d12016-07-08 12:30:39 +0200698 return net
tierno7edb6752016-03-21 17:37:52 +0100699
garciadeblasebd66722019-01-31 16:01:31 +0000700 def delete_network(self, net_id, created_items=None):
701 """
702 Removes a tenant network from VIM and its associated elements
703 :param net_id: VIM identifier of the network, provided by method new_network
704 :param created_items: dictionary with extra items to be deleted. provided by method new_network
705 Returns the network identifier or raises an exception upon error or when network is not found
706 """
tiernoae4a8d12016-07-08 12:30:39 +0200707 self.logger.debug("Deleting network '%s' from VIM", net_id)
garciadeblasebd66722019-01-31 16:01:31 +0000708 if created_items == None:
709 created_items = {}
tierno7edb6752016-03-21 17:37:52 +0100710 try:
711 self._reload_connection()
garciadeblasebd66722019-01-31 16:01:31 +0000712 #delete l2gw connections (if any) before deleting the network
713 for k, v in created_items.items():
714 if not v: # skip already deleted
715 continue
716 try:
717 k_item, _, k_id = k.partition(":")
718 if k_item == "l2gwconn":
719 self.neutron.delete_l2_gateway_connection(k_id)
720 except Exception as e:
721 self.logger.error("Error deleting l2 gateway connection: {}: {}".format(type(e).__name__, e))
tierno7edb6752016-03-21 17:37:52 +0100722 #delete VM ports attached to this networks before the network
723 ports = self.neutron.list_ports(network_id=net_id)
724 for p in ports['ports']:
725 try:
726 self.neutron.delete_port(p["id"])
727 except Exception as e:
tiernoae4a8d12016-07-08 12:30:39 +0200728 self.logger.error("Error deleting port %s: %s", p["id"], str(e))
tierno7edb6752016-03-21 17:37:52 +0100729 self.neutron.delete_network(net_id)
tiernoae4a8d12016-07-08 12:30:39 +0200730 return net_id
731 except (neExceptions.ConnectionFailed, neExceptions.NetworkNotFoundClient, neExceptions.NeutronException,
tierno8e995ce2016-09-22 08:13:00 +0000732 ksExceptions.ClientException, neExceptions.NeutronException, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200733 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100734
tiernoae4a8d12016-07-08 12:30:39 +0200735 def refresh_nets_status(self, net_list):
736 '''Get the status of the networks
737 Params: the list of network identifiers
738 Returns a dictionary with:
739 net_id: #VIM id of this network
740 status: #Mandatory. Text with one of:
741 # DELETED (not found at vim)
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100742 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
tiernoae4a8d12016-07-08 12:30:39 +0200743 # OTHER (Vim reported other status not understood)
744 # ERROR (VIM indicates an ERROR status)
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100745 # ACTIVE, INACTIVE, DOWN (admin down),
tiernoae4a8d12016-07-08 12:30:39 +0200746 # BUILD (on building process)
747 #
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100748 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
tiernoae4a8d12016-07-08 12:30:39 +0200749 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
750
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +0000751 '''
tiernoae4a8d12016-07-08 12:30:39 +0200752 net_dict={}
753 for net_id in net_list:
754 net = {}
755 try:
756 net_vim = self.get_network(net_id)
757 if net_vim['status'] in netStatus2manoFormat:
758 net["status"] = netStatus2manoFormat[ net_vim['status'] ]
759 else:
760 net["status"] = "OTHER"
761 net["error_msg"] = "VIM status reported " + net_vim['status']
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +0000762
tierno8e995ce2016-09-22 08:13:00 +0000763 if net['status'] == "ACTIVE" and not net_vim['admin_state_up']:
tiernoae4a8d12016-07-08 12:30:39 +0200764 net['status'] = 'DOWN'
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100765
766 net['vim_info'] = self.serialize(net_vim)
767
tiernoae4a8d12016-07-08 12:30:39 +0200768 if net_vim.get('fault'): #TODO
769 net['error_msg'] = str(net_vim['fault'])
770 except vimconn.vimconnNotFoundException as e:
771 self.logger.error("Exception getting net status: %s", str(e))
772 net['status'] = "DELETED"
773 net['error_msg'] = str(e)
774 except vimconn.vimconnException as e:
775 self.logger.error("Exception getting net status: %s", str(e))
776 net['status'] = "VIM_ERROR"
777 net['error_msg'] = str(e)
778 net_dict[net_id] = net
779 return net_dict
780
781 def get_flavor(self, flavor_id):
782 '''Obtain flavor details from the VIM. Returns the flavor dict details'''
783 self.logger.debug("Getting flavor '%s'", flavor_id)
tierno7edb6752016-03-21 17:37:52 +0100784 try:
785 self._reload_connection()
786 flavor = self.nova.flavors.find(id=flavor_id)
787 #TODO parse input and translate to VIM format (openmano_schemas.new_vminstance_response_schema)
tiernoae4a8d12016-07-08 12:30:39 +0200788 return flavor.to_dict()
tierno8e995ce2016-09-22 08:13:00 +0000789 except (nvExceptions.NotFound, nvExceptions.ClientException, ksExceptions.ClientException, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200790 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100791
tiernocf157a82017-01-30 14:07:06 +0100792 def get_flavor_id_from_data(self, flavor_dict):
793 """Obtain flavor id that match the flavor description
794 Returns the flavor_id or raises a vimconnNotFoundException
tiernoe26fc7a2017-05-30 14:43:03 +0200795 flavor_dict: contains the required ram, vcpus, disk
796 If 'use_existing_flavors' is set to True at config, the closer flavor that provides same or more ram, vcpus
797 and disk is returned. Otherwise a flavor with exactly same ram, vcpus and disk is returned or a
798 vimconnNotFoundException is raised
tiernocf157a82017-01-30 14:07:06 +0100799 """
tiernoe26fc7a2017-05-30 14:43:03 +0200800 exact_match = False if self.config.get('use_existing_flavors') else True
tiernocf157a82017-01-30 14:07:06 +0100801 try:
802 self._reload_connection()
tiernoe26fc7a2017-05-30 14:43:03 +0200803 flavor_candidate_id = None
804 flavor_candidate_data = (10000, 10000, 10000)
805 flavor_target = (flavor_dict["ram"], flavor_dict["vcpus"], flavor_dict["disk"])
806 # numa=None
anwarsae5f52c2019-04-22 10:35:27 +0530807 extended = flavor_dict.get("extended", {})
808 if extended:
tiernocf157a82017-01-30 14:07:06 +0100809 #TODO
tiernob7aa1bb2019-07-24 15:47:16 +0000810 raise vimconn.vimconnNotFoundException("Flavor with EPA still not implemented")
tiernocf157a82017-01-30 14:07:06 +0100811 # if len(numas) > 1:
812 # raise vimconn.vimconnNotFoundException("Cannot find any flavor with more than one numa")
813 # numa=numas[0]
814 # numas = extended.get("numas")
815 for flavor in self.nova.flavors.list():
816 epa = flavor.get_keys()
817 if epa:
818 continue
tiernoe26fc7a2017-05-30 14:43:03 +0200819 # TODO
820 flavor_data = (flavor.ram, flavor.vcpus, flavor.disk)
821 if flavor_data == flavor_target:
822 return flavor.id
823 elif not exact_match and flavor_target < flavor_data < flavor_candidate_data:
824 flavor_candidate_id = flavor.id
825 flavor_candidate_data = flavor_data
826 if not exact_match and flavor_candidate_id:
827 return flavor_candidate_id
tiernocf157a82017-01-30 14:07:06 +0100828 raise vimconn.vimconnNotFoundException("Cannot find any flavor matching '{}'".format(str(flavor_dict)))
829 except (nvExceptions.NotFound, nvExceptions.ClientException, ksExceptions.ClientException, ConnectionError) as e:
830 self._format_exception(e)
831
anwarsae5f52c2019-04-22 10:35:27 +0530832 def process_resource_quota(self, quota, prefix, extra_specs):
833 """
834 :param prefix:
835 :param extra_specs:
836 :return:
837 """
838 if 'limit' in quota:
839 extra_specs["quota:" + prefix + "_limit"] = quota['limit']
840 if 'reserve' in quota:
841 extra_specs["quota:" + prefix + "_reservation"] = quota['reserve']
842 if 'shares' in quota:
843 extra_specs["quota:" + prefix + "_shares_level"] = "custom"
844 extra_specs["quota:" + prefix + "_shares_share"] = quota['shares']
845
tiernoae4a8d12016-07-08 12:30:39 +0200846 def new_flavor(self, flavor_data, change_name_if_used=True):
tierno7edb6752016-03-21 17:37:52 +0100847 '''Adds a tenant flavor to openstack VIM
tiernoae4a8d12016-07-08 12:30:39 +0200848 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 +0100849 Returns the flavor identifier
850 '''
tiernoae4a8d12016-07-08 12:30:39 +0200851 self.logger.debug("Adding flavor '%s'", str(flavor_data))
tierno7edb6752016-03-21 17:37:52 +0100852 retry=0
tiernoae4a8d12016-07-08 12:30:39 +0200853 max_retries=3
tierno7edb6752016-03-21 17:37:52 +0100854 name_suffix = 0
anwarsc76a3ee2018-10-04 14:05:32 +0530855 try:
856 name=flavor_data['name']
857 while retry<max_retries:
858 retry+=1
859 try:
860 self._reload_connection()
861 if change_name_if_used:
862 #get used names
863 fl_names=[]
864 fl=self.nova.flavors.list()
865 for f in fl:
866 fl_names.append(f.name)
867 while name in fl_names:
868 name_suffix += 1
869 name = flavor_data['name']+"-" + str(name_suffix)
kate721d79b2017-06-24 04:21:38 -0700870
anwarsc76a3ee2018-10-04 14:05:32 +0530871 ram = flavor_data.get('ram',64)
872 vcpus = flavor_data.get('vcpus',1)
anwarsae5f52c2019-04-22 10:35:27 +0530873 extra_specs={}
tierno7edb6752016-03-21 17:37:52 +0100874
anwarsc76a3ee2018-10-04 14:05:32 +0530875 extended = flavor_data.get("extended")
876 if extended:
877 numas=extended.get("numas")
878 if numas:
879 numa_nodes = len(numas)
880 if numa_nodes > 1:
881 return -1, "Can not add flavor with more than one numa"
anwarsae5f52c2019-04-22 10:35:27 +0530882 extra_specs["hw:numa_nodes"] = str(numa_nodes)
883 extra_specs["hw:mem_page_size"] = "large"
884 extra_specs["hw:cpu_policy"] = "dedicated"
885 extra_specs["hw:numa_mempolicy"] = "strict"
anwarsc76a3ee2018-10-04 14:05:32 +0530886 if self.vim_type == "VIO":
anwarsae5f52c2019-04-22 10:35:27 +0530887 extra_specs["vmware:extra_config"] = '{"numa.nodeAffinity":"0"}'
888 extra_specs["vmware:latency_sensitivity_level"] = "high"
anwarsc76a3ee2018-10-04 14:05:32 +0530889 for numa in numas:
890 #overwrite ram and vcpus
891 #check if key 'memory' is present in numa else use ram value at flavor
892 if 'memory' in numa:
893 ram = numa['memory']*1024
894 #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 +0200895 extra_specs["hw:cpu_sockets"] = 1
anwarsc76a3ee2018-10-04 14:05:32 +0530896 if 'paired-threads' in numa:
897 vcpus = numa['paired-threads']*2
898 #cpu_thread_policy "require" implies that the compute node must have an STM architecture
anwarsae5f52c2019-04-22 10:35:27 +0530899 extra_specs["hw:cpu_thread_policy"] = "require"
900 extra_specs["hw:cpu_policy"] = "dedicated"
anwarsc76a3ee2018-10-04 14:05:32 +0530901 elif 'cores' in numa:
902 vcpus = numa['cores']
903 # 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 +0530904 extra_specs["hw:cpu_thread_policy"] = "isolate"
905 extra_specs["hw:cpu_policy"] = "dedicated"
anwarsc76a3ee2018-10-04 14:05:32 +0530906 elif 'threads' in numa:
907 vcpus = numa['threads']
908 # cpu_thread_policy "prefer" implies that the host may or may not have an SMT architecture
anwarsae5f52c2019-04-22 10:35:27 +0530909 extra_specs["hw:cpu_thread_policy"] = "prefer"
910 extra_specs["hw:cpu_policy"] = "dedicated"
anwarsc76a3ee2018-10-04 14:05:32 +0530911 # for interface in numa.get("interfaces",() ):
912 # if interface["dedicated"]=="yes":
913 # raise vimconn.vimconnException("Passthrough interfaces are not supported for the openstack connector", http_code=vimconn.HTTP_Service_Unavailable)
914 # #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 +0530915 elif extended.get("cpu-quota"):
916 self.process_resource_quota(extended.get("cpu-quota"), "cpu", extra_specs)
917 if extended.get("mem-quota"):
918 self.process_resource_quota(extended.get("mem-quota"), "memory", extra_specs)
919 if extended.get("vif-quota"):
920 self.process_resource_quota(extended.get("vif-quota"), "vif", extra_specs)
921 if extended.get("disk-io-quota"):
922 self.process_resource_quota(extended.get("disk-io-quota"), "disk_io", extra_specs)
anwarsc76a3ee2018-10-04 14:05:32 +0530923 #create flavor
924 new_flavor=self.nova.flavors.create(name,
925 ram,
926 vcpus,
927 flavor_data.get('disk',0),
928 is_public=flavor_data.get('is_public', True)
929 )
930 #add metadata
anwarsae5f52c2019-04-22 10:35:27 +0530931 if extra_specs:
932 new_flavor.set_keys(extra_specs)
anwarsc76a3ee2018-10-04 14:05:32 +0530933 return new_flavor.id
934 except nvExceptions.Conflict as e:
935 if change_name_if_used and retry < max_retries:
936 continue
937 self._format_exception(e)
938 #except nvExceptions.BadRequest as e:
939 except (ksExceptions.ClientException, nvExceptions.ClientException, ConnectionError, KeyError) as e:
940 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100941
tiernoae4a8d12016-07-08 12:30:39 +0200942 def delete_flavor(self,flavor_id):
943 '''Deletes a tenant flavor from openstack VIM. Returns the old flavor_id
tierno7edb6752016-03-21 17:37:52 +0100944 '''
tiernoae4a8d12016-07-08 12:30:39 +0200945 try:
946 self._reload_connection()
947 self.nova.flavors.delete(flavor_id)
948 return flavor_id
949 #except nvExceptions.BadRequest as e:
tierno8e995ce2016-09-22 08:13:00 +0000950 except (nvExceptions.NotFound, ksExceptions.ClientException, nvExceptions.ClientException, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200951 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100952
tiernoae4a8d12016-07-08 12:30:39 +0200953 def new_image(self,image_dict):
tierno7edb6752016-03-21 17:37:52 +0100954 '''
tiernoae4a8d12016-07-08 12:30:39 +0200955 Adds a tenant image to VIM. imge_dict is a dictionary with:
956 name: name
957 disk_format: qcow2, vhd, vmdk, raw (by default), ...
958 location: path or URI
959 public: "yes" or "no"
960 metadata: metadata of the image
961 Returns the image_id
tierno7edb6752016-03-21 17:37:52 +0100962 '''
tiernoae4a8d12016-07-08 12:30:39 +0200963 retry=0
964 max_retries=3
965 while retry<max_retries:
tierno7edb6752016-03-21 17:37:52 +0100966 retry+=1
967 try:
968 self._reload_connection()
969 #determine format http://docs.openstack.org/developer/glance/formats.html
970 if "disk_format" in image_dict:
971 disk_format=image_dict["disk_format"]
garciadeblas14480452017-01-10 13:08:07 +0100972 else: #autodiscover based on extension
tierno1beea862018-07-11 15:47:37 +0200973 if image_dict['location'].endswith(".qcow2"):
tierno7edb6752016-03-21 17:37:52 +0100974 disk_format="qcow2"
tierno1beea862018-07-11 15:47:37 +0200975 elif image_dict['location'].endswith(".vhd"):
tierno7edb6752016-03-21 17:37:52 +0100976 disk_format="vhd"
tierno1beea862018-07-11 15:47:37 +0200977 elif image_dict['location'].endswith(".vmdk"):
tierno7edb6752016-03-21 17:37:52 +0100978 disk_format="vmdk"
tierno1beea862018-07-11 15:47:37 +0200979 elif image_dict['location'].endswith(".vdi"):
tierno7edb6752016-03-21 17:37:52 +0100980 disk_format="vdi"
tierno1beea862018-07-11 15:47:37 +0200981 elif image_dict['location'].endswith(".iso"):
tierno7edb6752016-03-21 17:37:52 +0100982 disk_format="iso"
tierno1beea862018-07-11 15:47:37 +0200983 elif image_dict['location'].endswith(".aki"):
tierno7edb6752016-03-21 17:37:52 +0100984 disk_format="aki"
tierno1beea862018-07-11 15:47:37 +0200985 elif image_dict['location'].endswith(".ari"):
tierno7edb6752016-03-21 17:37:52 +0100986 disk_format="ari"
tierno1beea862018-07-11 15:47:37 +0200987 elif image_dict['location'].endswith(".ami"):
tierno7edb6752016-03-21 17:37:52 +0100988 disk_format="ami"
989 else:
990 disk_format="raw"
tiernoae4a8d12016-07-08 12:30:39 +0200991 self.logger.debug("new_image: '%s' loading from '%s'", image_dict['name'], image_dict['location'])
shashankjain3c83a212018-10-04 13:05:46 +0530992 if self.vim_type == "VIO":
993 container_format = "bare"
994 if 'container_format' in image_dict:
995 container_format = image_dict['container_format']
996 new_image = self.glance.images.create(name=image_dict['name'], container_format=container_format,
997 disk_format=disk_format)
998 else:
999 new_image = self.glance.images.create(name=image_dict['name'])
tierno1beea862018-07-11 15:47:37 +02001000 if image_dict['location'].startswith("http"):
1001 # TODO there is not a method to direct download. It must be downloaded locally with requests
1002 raise vimconn.vimconnNotImplemented("Cannot create image from URL")
tierno7edb6752016-03-21 17:37:52 +01001003 else: #local path
1004 with open(image_dict['location']) as fimage:
tierno1beea862018-07-11 15:47:37 +02001005 self.glance.images.upload(new_image.id, fimage)
1006 #new_image = self.glancev1.images.create(name=image_dict['name'], is_public=image_dict.get('public',"yes")=="yes",
1007 # container_format="bare", data=fimage, disk_format=disk_format)
tierno7edb6752016-03-21 17:37:52 +01001008 metadata_to_load = image_dict.get('metadata')
shashankjain3c83a212018-10-04 13:05:46 +05301009 # TODO location is a reserved word for current openstack versions. fixed for VIO please check for openstack
1010 if self.vim_type == "VIO":
1011 metadata_to_load['upload_location'] = image_dict['location']
1012 else:
1013 metadata_to_load['location'] = image_dict['location']
tierno1beea862018-07-11 15:47:37 +02001014 self.glance.images.update(new_image.id, **metadata_to_load)
tiernoae4a8d12016-07-08 12:30:39 +02001015 return new_image.id
1016 except (nvExceptions.Conflict, ksExceptions.ClientException, nvExceptions.ClientException) as e:
1017 self._format_exception(e)
tierno8e995ce2016-09-22 08:13:00 +00001018 except (HTTPException, gl1Exceptions.HTTPException, gl1Exceptions.CommunicationError, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +02001019 if retry==max_retries:
1020 continue
1021 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +01001022 except IOError as e: #can not open the file
tiernoae4a8d12016-07-08 12:30:39 +02001023 raise vimconn.vimconnConnectionException(type(e).__name__ + ": " + str(e)+ " for " + image_dict['location'],
1024 http_code=vimconn.HTTP_Bad_Request)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001025
tiernoae4a8d12016-07-08 12:30:39 +02001026 def delete_image(self, image_id):
1027 '''Deletes a tenant image from openstack VIM. Returns the old id
tierno7edb6752016-03-21 17:37:52 +01001028 '''
tiernoae4a8d12016-07-08 12:30:39 +02001029 try:
1030 self._reload_connection()
tierno1beea862018-07-11 15:47:37 +02001031 self.glance.images.delete(image_id)
tiernoae4a8d12016-07-08 12:30:39 +02001032 return image_id
shashankjain3c83a212018-10-04 13:05:46 +05301033 except (nvExceptions.NotFound, ksExceptions.ClientException, nvExceptions.ClientException, gl1Exceptions.CommunicationError, gl1Exceptions.HTTPNotFound, ConnectionError) as e: #TODO remove
tiernoae4a8d12016-07-08 12:30:39 +02001034 self._format_exception(e)
1035
1036 def get_image_id_from_path(self, path):
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001037 '''Get the image id from image path in the VIM database. Returns the image_id'''
tiernoae4a8d12016-07-08 12:30:39 +02001038 try:
1039 self._reload_connection()
tierno1beea862018-07-11 15:47:37 +02001040 images = self.glance.images.list()
tiernoae4a8d12016-07-08 12:30:39 +02001041 for image in images:
1042 if image.metadata.get("location")==path:
1043 return image.id
1044 raise vimconn.vimconnNotFoundException("image with location '{}' not found".format( path))
tierno8e995ce2016-09-22 08:13:00 +00001045 except (ksExceptions.ClientException, nvExceptions.ClientException, gl1Exceptions.CommunicationError, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +02001046 self._format_exception(e)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001047
garciadeblasb69fa9f2016-09-28 12:04:10 +02001048 def get_image_list(self, filter_dict={}):
1049 '''Obtain tenant images from VIM
1050 Filter_dict can be:
1051 id: image id
1052 name: image name
1053 checksum: image checksum
1054 Returns the image list of dictionaries:
1055 [{<the fields at Filter_dict plus some VIM specific>}, ...]
1056 List can be empty
1057 '''
1058 self.logger.debug("Getting image list from VIM filter: '%s'", str(filter_dict))
1059 try:
1060 self._reload_connection()
tierno69b590e2018-03-13 18:52:23 +01001061 filter_dict_os = filter_dict.copy()
garciadeblasb69fa9f2016-09-28 12:04:10 +02001062 #First we filter by the available filter fields: name, id. The others are removed.
tierno1beea862018-07-11 15:47:37 +02001063 image_list = self.glance.images.list()
garciadeblasb69fa9f2016-09-28 12:04:10 +02001064 filtered_list = []
1065 for image in image_list:
tierno3cb8dc32017-10-24 18:13:19 +02001066 try:
tierno1beea862018-07-11 15:47:37 +02001067 if filter_dict.get("name") and image["name"] != filter_dict["name"]:
1068 continue
1069 if filter_dict.get("id") and image["id"] != filter_dict["id"]:
1070 continue
1071 if filter_dict.get("checksum") and image["checksum"] != filter_dict["checksum"]:
1072 continue
1073
1074 filtered_list.append(image.copy())
tierno3cb8dc32017-10-24 18:13:19 +02001075 except gl1Exceptions.HTTPNotFound:
1076 pass
garciadeblasb69fa9f2016-09-28 12:04:10 +02001077 return filtered_list
1078 except (ksExceptions.ClientException, nvExceptions.ClientException, gl1Exceptions.CommunicationError, ConnectionError) as e:
1079 self._format_exception(e)
1080
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +02001081 def __wait_for_vm(self, vm_id, status):
1082 """wait until vm is in the desired status and return True.
1083 If the VM gets in ERROR status, return false.
1084 If the timeout is reached generate an exception"""
1085 elapsed_time = 0
1086 while elapsed_time < server_timeout:
1087 vm_status = self.nova.servers.get(vm_id).status
1088 if vm_status == status:
1089 return True
1090 if vm_status == 'ERROR':
1091 return False
tierno1df468d2018-07-06 14:25:16 +02001092 time.sleep(5)
1093 elapsed_time += 5
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +02001094
1095 # if we exceeded the timeout rollback
1096 if elapsed_time >= server_timeout:
1097 raise vimconn.vimconnException('Timeout waiting for instance ' + vm_id + ' to get ' + status,
1098 http_code=vimconn.HTTP_Request_Timeout)
1099
mirabal29356312017-07-27 12:21:22 +02001100 def _get_openstack_availablity_zones(self):
1101 """
1102 Get from openstack availability zones available
1103 :return:
1104 """
1105 try:
1106 openstack_availability_zone = self.nova.availability_zones.list()
1107 openstack_availability_zone = [str(zone.zoneName) for zone in openstack_availability_zone
1108 if zone.zoneName != 'internal']
1109 return openstack_availability_zone
1110 except Exception as e:
1111 return None
1112
1113 def _set_availablity_zones(self):
1114 """
1115 Set vim availablity zone
1116 :return:
1117 """
1118
1119 if 'availability_zone' in self.config:
1120 vim_availability_zones = self.config.get('availability_zone')
1121 if isinstance(vim_availability_zones, str):
1122 self.availability_zone = [vim_availability_zones]
1123 elif isinstance(vim_availability_zones, list):
1124 self.availability_zone = vim_availability_zones
1125 else:
1126 self.availability_zone = self._get_openstack_availablity_zones()
1127
tierno5a3273c2017-08-29 11:43:46 +02001128 def _get_vm_availability_zone(self, availability_zone_index, availability_zone_list):
mirabal29356312017-07-27 12:21:22 +02001129 """
tierno5a3273c2017-08-29 11:43:46 +02001130 Return thge availability zone to be used by the created VM.
1131 :return: The VIM availability zone to be used or None
mirabal29356312017-07-27 12:21:22 +02001132 """
tierno5a3273c2017-08-29 11:43:46 +02001133 if availability_zone_index is None:
1134 if not self.config.get('availability_zone'):
1135 return None
1136 elif isinstance(self.config.get('availability_zone'), str):
1137 return self.config['availability_zone']
1138 else:
1139 # TODO consider using a different parameter at config for default AV and AV list match
1140 return self.config['availability_zone'][0]
mirabal29356312017-07-27 12:21:22 +02001141
tierno5a3273c2017-08-29 11:43:46 +02001142 vim_availability_zones = self.availability_zone
1143 # check if VIM offer enough availability zones describe in the VNFD
1144 if vim_availability_zones and len(availability_zone_list) <= len(vim_availability_zones):
1145 # check if all the names of NFV AV match VIM AV names
1146 match_by_index = False
1147 for av in availability_zone_list:
1148 if av not in vim_availability_zones:
1149 match_by_index = True
1150 break
1151 if match_by_index:
1152 return vim_availability_zones[availability_zone_index]
1153 else:
1154 return availability_zone_list[availability_zone_index]
mirabal29356312017-07-27 12:21:22 +02001155 else:
tierno5a3273c2017-08-29 11:43:46 +02001156 raise vimconn.vimconnConflictException("No enough availability zones at VIM for this deployment")
mirabal29356312017-07-27 12:21:22 +02001157
tierno5a3273c2017-08-29 11:43:46 +02001158 def new_vminstance(self, name, description, start, image_id, flavor_id, net_list, cloud_config=None, disk_list=None,
1159 availability_zone_index=None, availability_zone_list=None):
tierno98e909c2017-10-14 13:27:03 +02001160 """Adds a VM instance to VIM
tierno7edb6752016-03-21 17:37:52 +01001161 Params:
1162 start: indicates if VM must start or boot in pause mode. Ignored
1163 image_id,flavor_id: iamge and flavor uuid
1164 net_list: list of interfaces, each one is a dictionary with:
1165 name:
1166 net_id: network uuid to connect
1167 vpci: virtual vcpi to assign, ignored because openstack lack #TODO
1168 model: interface model, ignored #TODO
1169 mac_address: used for SR-IOV ifaces #TODO for other types
1170 use: 'data', 'bridge', 'mgmt'
tierno66eba6e2017-11-10 17:09:18 +01001171 type: 'virtual', 'PCI-PASSTHROUGH'('PF'), 'SR-IOV'('VF'), 'VFnotShared'
tierno7edb6752016-03-21 17:37:52 +01001172 vim_id: filled/added by this function
ahmadsaf853d452016-12-22 11:33:47 +05001173 floating_ip: True/False (or it can be None)
tierno41a69812018-02-16 14:34:33 +01001174 'cloud_config': (optional) dictionary with:
1175 'key-pairs': (optional) list of strings with the public key to be inserted to the default user
1176 'users': (optional) list of users to be inserted, each item is a dict with:
1177 'name': (mandatory) user name,
1178 'key-pairs': (optional) list of strings with the public key to be inserted to the user
1179 'user-data': (optional) string is a text script to be passed directly to cloud-init
1180 'config-files': (optional). List of files to be transferred. Each item is a dict with:
1181 'dest': (mandatory) string with the destination absolute path
1182 'encoding': (optional, by default text). Can be one of:
1183 'b64', 'base64', 'gz', 'gz+b64', 'gz+base64', 'gzip+b64', 'gzip+base64'
1184 'content' (mandatory): string with the content of the file
1185 'permissions': (optional) string with file permissions, typically octal notation '0644'
1186 'owner': (optional) file owner, string with the format 'owner:group'
1187 'boot-data-drive': boolean to indicate if user-data must be passed using a boot drive (hard disk)
mirabal29356312017-07-27 12:21:22 +02001188 'disk_list': (optional) list with additional disks to the VM. Each item is a dict with:
1189 'image_id': (optional). VIM id of an existing image. If not provided an empty disk must be mounted
1190 'size': (mandatory) string with the size of the disk in GB
tierno1df468d2018-07-06 14:25:16 +02001191 'vim_id' (optional) should use this existing volume id
tierno5a3273c2017-08-29 11:43:46 +02001192 availability_zone_index: Index of availability_zone_list to use for this this VM. None if not AV required
1193 availability_zone_list: list of availability zones given by user in the VNFD descriptor. Ignore if
1194 availability_zone_index is None
tierno7edb6752016-03-21 17:37:52 +01001195 #TODO ip, security groups
tierno98e909c2017-10-14 13:27:03 +02001196 Returns a tuple with the instance identifier and created_items or raises an exception on error
1197 created_items can be None or a dictionary where this method can include key-values that will be passed to
1198 the method delete_vminstance and action_vminstance. Can be used to store created ports, volumes, etc.
1199 Format is vimconnector dependent, but do not use nested dictionaries and a value of None should be the same
1200 as not present.
1201 """
tiernofa51c202017-01-27 14:58:17 +01001202 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 +01001203 try:
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +02001204 server = None
tierno98e909c2017-10-14 13:27:03 +02001205 created_items = {}
tiernob0b9dab2017-10-14 14:25:20 +02001206 # metadata = {}
tierno98e909c2017-10-14 13:27:03 +02001207 net_list_vim = []
1208 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 +02001209 no_secured_ports = [] # List of port-is with port-security disabled
tierno7edb6752016-03-21 17:37:52 +01001210 self._reload_connection()
tiernob0b9dab2017-10-14 14:25:20 +02001211 # metadata_vpci = {} # For a specific neutron plugin
tiernob84cbdc2017-07-07 14:30:30 +02001212 block_device_mapping = None
tiernoa05b65a2019-02-01 12:30:27 +00001213
tierno7edb6752016-03-21 17:37:52 +01001214 for net in net_list:
tierno98e909c2017-10-14 13:27:03 +02001215 if not net.get("net_id"): # skip non connected iface
tierno7edb6752016-03-21 17:37:52 +01001216 continue
Pablo Montes Moreno3be0b2a2017-03-30 13:22:15 +02001217
tiernoa05b65a2019-02-01 12:30:27 +00001218 port_dict = {
Pablo Montes Moreno3be0b2a2017-03-30 13:22:15 +02001219 "network_id": net["net_id"],
1220 "name": net.get("name"),
1221 "admin_state_up": True
1222 }
tiernoa05b65a2019-02-01 12:30:27 +00001223 if self.config.get("security_groups") and net.get("port_security") is not False and \
1224 not self.config.get("no_port_security_extension"):
1225 if not self.security_groups_id:
1226 self._get_ids_from_name()
1227 port_dict["security_groups"] = self.security_groups_id
1228
Pablo Montes Moreno3be0b2a2017-03-30 13:22:15 +02001229 if net["type"]=="virtual":
tiernob0b9dab2017-10-14 14:25:20 +02001230 pass
1231 # if "vpci" in net:
1232 # metadata_vpci[ net["net_id"] ] = [[ net["vpci"], "" ]]
tierno66eba6e2017-11-10 17:09:18 +01001233 elif net["type"] == "VF" or net["type"] == "SR-IOV": # for VF
tiernob0b9dab2017-10-14 14:25:20 +02001234 # if "vpci" in net:
1235 # if "VF" not in metadata_vpci:
1236 # metadata_vpci["VF"]=[]
1237 # metadata_vpci["VF"].append([ net["vpci"], "" ])
Pablo Montes Moreno3be0b2a2017-03-30 13:22:15 +02001238 port_dict["binding:vnic_type"]="direct"
tiernob0b9dab2017-10-14 14:25:20 +02001239 # VIO specific Changes
kate721d79b2017-06-24 04:21:38 -07001240 if self.vim_type == "VIO":
tiernob0b9dab2017-10-14 14:25:20 +02001241 # Need to create port with port_security_enabled = False and no-security-groups
kate721d79b2017-06-24 04:21:38 -07001242 port_dict["port_security_enabled"]=False
1243 port_dict["provider_security_groups"]=[]
1244 port_dict["security_groups"]=[]
tierno66eba6e2017-11-10 17:09:18 +01001245 else: # For PT PCI-PASSTHROUGH
tiernob0b9dab2017-10-14 14:25:20 +02001246 # VIO specific Changes
1247 # Current VIO release does not support port with type 'direct-physical'
1248 # So no need to create virtual port in case of PCI-device.
1249 # Will update port_dict code when support gets added in next VIO release
kate721d79b2017-06-24 04:21:38 -07001250 if self.vim_type == "VIO":
tiernob0b9dab2017-10-14 14:25:20 +02001251 raise vimconn.vimconnNotSupportedException(
1252 "Current VIO release does not support full passthrough (PT)")
1253 # if "vpci" in net:
1254 # if "PF" not in metadata_vpci:
1255 # metadata_vpci["PF"]=[]
1256 # metadata_vpci["PF"].append([ net["vpci"], "" ])
Pablo Montes Moreno3be0b2a2017-03-30 13:22:15 +02001257 port_dict["binding:vnic_type"]="direct-physical"
1258 if not port_dict["name"]:
1259 port_dict["name"]=name
1260 if net.get("mac_address"):
1261 port_dict["mac_address"]=net["mac_address"]
tierno41a69812018-02-16 14:34:33 +01001262 if net.get("ip_address"):
1263 port_dict["fixed_ips"] = [{'ip_address': net["ip_address"]}]
1264 # TODO add 'subnet_id': <subnet_id>
Pablo Montes Moreno3be0b2a2017-03-30 13:22:15 +02001265 new_port = self.neutron.create_port({"port": port_dict })
tierno00e3df72017-11-29 17:20:13 +01001266 created_items["port:" + str(new_port["port"]["id"])] = True
Pablo Montes Moreno3be0b2a2017-03-30 13:22:15 +02001267 net["mac_adress"] = new_port["port"]["mac_address"]
1268 net["vim_id"] = new_port["port"]["id"]
tiernob84cbdc2017-07-07 14:30:30 +02001269 # if try to use a network without subnetwork, it will return a emtpy list
1270 fixed_ips = new_port["port"].get("fixed_ips")
1271 if fixed_ips:
1272 net["ip"] = fixed_ips[0].get("ip_address")
1273 else:
1274 net["ip"] = None
montesmoreno994a29d2017-08-22 11:23:06 +02001275
1276 port = {"port-id": new_port["port"]["id"]}
1277 if float(self.nova.api_version.get_string()) >= 2.32:
1278 port["tag"] = new_port["port"]["name"]
1279 net_list_vim.append(port)
Pablo Montes Moreno3be0b2a2017-03-30 13:22:15 +02001280
ahmadsaf853d452016-12-22 11:33:47 +05001281 if net.get('floating_ip', False):
tiernof8383b82017-01-18 15:49:48 +01001282 net['exit_on_floating_ip_error'] = True
ahmadsaf853d452016-12-22 11:33:47 +05001283 external_network.append(net)
tiernof8383b82017-01-18 15:49:48 +01001284 elif net['use'] == 'mgmt' and self.config.get('use_floating_ip'):
1285 net['exit_on_floating_ip_error'] = False
1286 external_network.append(net)
tierno326fd5e2018-02-22 11:58:59 +01001287 net['floating_ip'] = self.config.get('use_floating_ip')
tiernof8383b82017-01-18 15:49:48 +01001288
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +02001289 # If port security is disabled when the port has not yet been attached to the VM, then all vm traffic is dropped.
1290 # As a workaround we wait until the VM is active and then disable the port-security
tierno4d1ce222018-04-06 10:41:06 +02001291 if net.get("port_security") == False and not self.config.get("no_port_security_extension"):
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +02001292 no_secured_ports.append(new_port["port"]["id"])
1293
tiernob0b9dab2017-10-14 14:25:20 +02001294 # if metadata_vpci:
1295 # metadata = {"pci_assignement": json.dumps(metadata_vpci)}
1296 # if len(metadata["pci_assignement"]) >255:
1297 # #limit the metadata size
1298 # #metadata["pci_assignement"] = metadata["pci_assignement"][0:255]
1299 # self.logger.warn("Metadata deleted since it exceeds the expected length (255) ")
1300 # metadata = {}
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001301
tiernob0b9dab2017-10-14 14:25:20 +02001302 self.logger.debug("name '%s' image_id '%s'flavor_id '%s' net_list_vim '%s' description '%s'",
1303 name, image_id, flavor_id, str(net_list_vim), description)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001304
tierno98e909c2017-10-14 13:27:03 +02001305 # cloud config
tierno0a1437e2017-10-02 00:17:43 +02001306 config_drive, userdata = self._create_user_data(cloud_config)
montesmoreno0c8def02016-12-22 12:16:23 +00001307
tierno98e909c2017-10-14 13:27:03 +02001308 # Create additional volumes in case these are present in disk_list
montesmoreno0c8def02016-12-22 12:16:23 +00001309 base_disk_index = ord('b')
tierno1df468d2018-07-06 14:25:16 +02001310 if disk_list:
tiernob84cbdc2017-07-07 14:30:30 +02001311 block_device_mapping = {}
montesmoreno0c8def02016-12-22 12:16:23 +00001312 for disk in disk_list:
tierno1df468d2018-07-06 14:25:16 +02001313 if disk.get('vim_id'):
1314 block_device_mapping['_vd' + chr(base_disk_index)] = disk['vim_id']
montesmoreno0c8def02016-12-22 12:16:23 +00001315 else:
tierno1df468d2018-07-06 14:25:16 +02001316 if 'image_id' in disk:
1317 volume = self.cinder.volumes.create(size=disk['size'], name=name + '_vd' +
1318 chr(base_disk_index), imageRef=disk['image_id'])
1319 else:
1320 volume = self.cinder.volumes.create(size=disk['size'], name=name + '_vd' +
1321 chr(base_disk_index))
1322 created_items["volume:" + str(volume.id)] = True
1323 block_device_mapping['_vd' + chr(base_disk_index)] = volume.id
montesmoreno0c8def02016-12-22 12:16:23 +00001324 base_disk_index += 1
1325
tierno1df468d2018-07-06 14:25:16 +02001326 # Wait until created volumes are with status available
montesmoreno0c8def02016-12-22 12:16:23 +00001327 elapsed_time = 0
tierno1df468d2018-07-06 14:25:16 +02001328 while elapsed_time < volume_timeout:
1329 for created_item in created_items:
1330 v, _, volume_id = created_item.partition(":")
1331 if v == 'volume':
1332 if self.cinder.volumes.get(volume_id).status != 'available':
1333 break
1334 else: # all ready: break from while
1335 break
1336 time.sleep(5)
1337 elapsed_time += 5
tiernob0b9dab2017-10-14 14:25:20 +02001338 # If we exceeded the timeout rollback
montesmoreno0c8def02016-12-22 12:16:23 +00001339 if elapsed_time >= volume_timeout:
montesmoreno0c8def02016-12-22 12:16:23 +00001340 raise vimconn.vimconnException('Timeout creating volumes for instance ' + name,
1341 http_code=vimconn.HTTP_Request_Timeout)
mirabal29356312017-07-27 12:21:22 +02001342 # get availability Zone
tierno5a3273c2017-08-29 11:43:46 +02001343 vm_av_zone = self._get_vm_availability_zone(availability_zone_index, availability_zone_list)
montesmoreno0c8def02016-12-22 12:16:23 +00001344
tiernob0b9dab2017-10-14 14:25:20 +02001345 self.logger.debug("nova.servers.create({}, {}, {}, nics={}, security_groups={}, "
mirabal29356312017-07-27 12:21:22 +02001346 "availability_zone={}, key_name={}, userdata={}, config_drive={}, "
tiernob0b9dab2017-10-14 14:25:20 +02001347 "block_device_mapping={})".format(name, image_id, flavor_id, net_list_vim,
tiernoa05b65a2019-02-01 12:30:27 +00001348 self.config.get("security_groups"), vm_av_zone,
1349 self.config.get('keypair'), userdata, config_drive,
1350 block_device_mapping))
tiernob0b9dab2017-10-14 14:25:20 +02001351 server = self.nova.servers.create(name, image_id, flavor_id, nics=net_list_vim,
tiernoa05b65a2019-02-01 12:30:27 +00001352 security_groups=self.config.get("security_groups"),
1353 # TODO remove security_groups in future versions. Already at neutron port
mirabal29356312017-07-27 12:21:22 +02001354 availability_zone=vm_av_zone,
montesmoreno0c8def02016-12-22 12:16:23 +00001355 key_name=self.config.get('keypair'),
1356 userdata=userdata,
tiernob84cbdc2017-07-07 14:30:30 +02001357 config_drive=config_drive,
1358 block_device_mapping=block_device_mapping
montesmoreno0c8def02016-12-22 12:16:23 +00001359 ) # , description=description)
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +02001360
tierno326fd5e2018-02-22 11:58:59 +01001361 vm_start_time = time.time()
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +02001362 # Previously mentioned workaround to wait until the VM is active and then disable the port-security
1363 if no_secured_ports:
1364 self.__wait_for_vm(server.id, 'ACTIVE')
1365
1366 for port_id in no_secured_ports:
1367 try:
tierno4d1ce222018-04-06 10:41:06 +02001368 self.neutron.update_port(port_id,
1369 {"port": {"port_security_enabled": False, "security_groups": None}})
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +02001370 except Exception as e:
tierno4d1ce222018-04-06 10:41:06 +02001371 raise vimconn.vimconnException("It was not possible to disable port security for port {}".format(
1372 port_id))
tierno98e909c2017-10-14 13:27:03 +02001373 # print "DONE :-)", server
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +02001374
tierno4d1ce222018-04-06 10:41:06 +02001375 # pool_id = None
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +02001376 if external_network:
tierno98e909c2017-10-14 13:27:03 +02001377 floating_ips = self.neutron.list_floatingips().get("floatingips", ())
ahmadsaf853d452016-12-22 11:33:47 +05001378 for floating_network in external_network:
tiernof8383b82017-01-18 15:49:48 +01001379 try:
tiernof8383b82017-01-18 15:49:48 +01001380 assigned = False
tierno98e909c2017-10-14 13:27:03 +02001381 while not assigned:
tiernof8383b82017-01-18 15:49:48 +01001382 if floating_ips:
1383 ip = floating_ips.pop(0)
tierno326fd5e2018-02-22 11:58:59 +01001384 if ip.get("port_id", False) or ip.get('tenant_id') != server.tenant_id:
1385 continue
1386 if isinstance(floating_network['floating_ip'], str):
1387 if ip.get("floating_network_id") != floating_network['floating_ip']:
1388 continue
1389 free_floating_ip = ip.get("floating_ip_address")
tiernof8383b82017-01-18 15:49:48 +01001390 else:
tiernocb3cca22018-05-31 15:08:52 +02001391 if isinstance(floating_network['floating_ip'], str) and \
1392 floating_network['floating_ip'].lower() != "true":
tierno326fd5e2018-02-22 11:58:59 +01001393 pool_id = floating_network['floating_ip']
1394 else:
tierno4d1ce222018-04-06 10:41:06 +02001395 # Find the external network
tierno326fd5e2018-02-22 11:58:59 +01001396 external_nets = list()
1397 for net in self.neutron.list_networks()['networks']:
1398 if net['router:external']:
1399 external_nets.append(net)
tiernof8383b82017-01-18 15:49:48 +01001400
tierno326fd5e2018-02-22 11:58:59 +01001401 if len(external_nets) == 0:
1402 raise vimconn.vimconnException("Cannot create floating_ip automatically since no external "
1403 "network is present",
1404 http_code=vimconn.HTTP_Conflict)
1405 if len(external_nets) > 1:
1406 raise vimconn.vimconnException("Cannot create floating_ip automatically since multiple "
1407 "external networks are present",
1408 http_code=vimconn.HTTP_Conflict)
tiernof8383b82017-01-18 15:49:48 +01001409
tierno326fd5e2018-02-22 11:58:59 +01001410 pool_id = external_nets[0].get('id')
tiernof8383b82017-01-18 15:49:48 +01001411 param = {'floatingip': {'floating_network_id': pool_id, 'tenant_id': server.tenant_id}}
ahmadsaf853d452016-12-22 11:33:47 +05001412 try:
tierno4d1ce222018-04-06 10:41:06 +02001413 # self.logger.debug("Creating floating IP")
tiernof8383b82017-01-18 15:49:48 +01001414 new_floating_ip = self.neutron.create_floatingip(param)
1415 free_floating_ip = new_floating_ip['floatingip']['floating_ip_address']
ahmadsaf853d452016-12-22 11:33:47 +05001416 except Exception as e:
tierno326fd5e2018-02-22 11:58:59 +01001417 raise vimconn.vimconnException(type(e).__name__ + ": Cannot create new floating_ip " +
1418 str(e), http_code=vimconn.HTTP_Conflict)
1419
1420 fix_ip = floating_network.get('ip')
1421 while not assigned:
1422 try:
1423 server.add_floating_ip(free_floating_ip, fix_ip)
1424 assigned = True
1425 except Exception as e:
tierno4d1ce222018-04-06 10:41:06 +02001426 # openstack need some time after VM creation to asign an IP. So retry if fails
tierno326fd5e2018-02-22 11:58:59 +01001427 vm_status = self.nova.servers.get(server.id).status
1428 if vm_status != 'ACTIVE' and vm_status != 'ERROR':
1429 if time.time() - vm_start_time < server_timeout:
1430 time.sleep(5)
1431 continue
tierno4d1ce222018-04-06 10:41:06 +02001432 raise vimconn.vimconnException(
1433 "Cannot create floating_ip: {} {}".format(type(e).__name__, e),
1434 http_code=vimconn.HTTP_Conflict)
tierno326fd5e2018-02-22 11:58:59 +01001435
tiernof8383b82017-01-18 15:49:48 +01001436 except Exception as e:
1437 if not floating_network['exit_on_floating_ip_error']:
1438 self.logger.warn("Cannot create floating_ip. %s", str(e))
1439 continue
tiernof8383b82017-01-18 15:49:48 +01001440 raise
montesmoreno2a1fc4e2017-01-09 16:46:04 +00001441
tierno98e909c2017-10-14 13:27:03 +02001442 return server.id, created_items
tierno7edb6752016-03-21 17:37:52 +01001443# except nvExceptions.NotFound as e:
1444# error_value=-vimconn.HTTP_Not_Found
1445# error_text= "vm instance %s not found" % vm_id
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +02001446# except TypeError as e:
1447# raise vimconn.vimconnException(type(e).__name__ + ": "+ str(e), http_code=vimconn.HTTP_Bad_Request)
1448
1449 except Exception as e:
tierno98e909c2017-10-14 13:27:03 +02001450 server_id = None
1451 if server:
1452 server_id = server.id
1453 try:
1454 self.delete_vminstance(server_id, created_items)
1455 except Exception as e2:
1456 self.logger.error("new_vminstance rollback fail {}".format(e2))
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +02001457
tiernoae4a8d12016-07-08 12:30:39 +02001458 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +01001459
tiernoae4a8d12016-07-08 12:30:39 +02001460 def get_vminstance(self,vm_id):
tierno7edb6752016-03-21 17:37:52 +01001461 '''Returns the VM instance information from VIM'''
tiernoae4a8d12016-07-08 12:30:39 +02001462 #self.logger.debug("Getting VM from VIM")
tierno7edb6752016-03-21 17:37:52 +01001463 try:
1464 self._reload_connection()
1465 server = self.nova.servers.find(id=vm_id)
1466 #TODO parse input and translate to VIM format (openmano_schemas.new_vminstance_response_schema)
tiernoae4a8d12016-07-08 12:30:39 +02001467 return server.to_dict()
tierno8e995ce2016-09-22 08:13:00 +00001468 except (ksExceptions.ClientException, nvExceptions.ClientException, nvExceptions.NotFound, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +02001469 self._format_exception(e)
1470
1471 def get_vminstance_console(self,vm_id, console_type="vnc"):
tierno7edb6752016-03-21 17:37:52 +01001472 '''
1473 Get a console for the virtual machine
1474 Params:
1475 vm_id: uuid of the VM
1476 console_type, can be:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001477 "novnc" (by default), "xvpvnc" for VNC types,
tierno7edb6752016-03-21 17:37:52 +01001478 "rdp-html5" for RDP types, "spice-html5" for SPICE types
tiernoae4a8d12016-07-08 12:30:39 +02001479 Returns dict with the console parameters:
1480 protocol: ssh, ftp, http, https, ...
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001481 server: usually ip address
1482 port: the http, ssh, ... port
1483 suffix: extra text, e.g. the http path and query string
tierno7edb6752016-03-21 17:37:52 +01001484 '''
tiernoae4a8d12016-07-08 12:30:39 +02001485 self.logger.debug("Getting VM CONSOLE from VIM")
tierno7edb6752016-03-21 17:37:52 +01001486 try:
1487 self._reload_connection()
1488 server = self.nova.servers.find(id=vm_id)
1489 if console_type == None or console_type == "novnc":
1490 console_dict = server.get_vnc_console("novnc")
1491 elif console_type == "xvpvnc":
1492 console_dict = server.get_vnc_console(console_type)
1493 elif console_type == "rdp-html5":
1494 console_dict = server.get_rdp_console(console_type)
1495 elif console_type == "spice-html5":
1496 console_dict = server.get_spice_console(console_type)
1497 else:
tiernoae4a8d12016-07-08 12:30:39 +02001498 raise vimconn.vimconnException("console type '{}' not allowed".format(console_type), http_code=vimconn.HTTP_Bad_Request)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001499
tierno7edb6752016-03-21 17:37:52 +01001500 console_dict1 = console_dict.get("console")
1501 if console_dict1:
1502 console_url = console_dict1.get("url")
1503 if console_url:
1504 #parse console_url
1505 protocol_index = console_url.find("//")
1506 suffix_index = console_url[protocol_index+2:].find("/") + protocol_index+2
1507 port_index = console_url[protocol_index+2:suffix_index].find(":") + protocol_index+2
1508 if protocol_index < 0 or port_index<0 or suffix_index<0:
1509 return -vimconn.HTTP_Internal_Server_Error, "Unexpected response from VIM"
1510 console_dict={"protocol": console_url[0:protocol_index],
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001511 "server": console_url[protocol_index+2:port_index],
1512 "port": console_url[port_index:suffix_index],
1513 "suffix": console_url[suffix_index+1:]
tierno7edb6752016-03-21 17:37:52 +01001514 }
1515 protocol_index += 2
tiernoae4a8d12016-07-08 12:30:39 +02001516 return console_dict
1517 raise vimconn.vimconnUnexpectedResponse("Unexpected response from VIM")
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001518
tierno8e995ce2016-09-22 08:13:00 +00001519 except (nvExceptions.NotFound, ksExceptions.ClientException, nvExceptions.ClientException, nvExceptions.BadRequest, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +02001520 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +01001521
tierno98e909c2017-10-14 13:27:03 +02001522 def delete_vminstance(self, vm_id, created_items=None):
tiernoae4a8d12016-07-08 12:30:39 +02001523 '''Removes a VM instance from VIM. Returns the old identifier
tierno7edb6752016-03-21 17:37:52 +01001524 '''
tiernoae4a8d12016-07-08 12:30:39 +02001525 #print "osconnector: Getting VM from VIM"
tierno98e909c2017-10-14 13:27:03 +02001526 if created_items == None:
1527 created_items = {}
tierno7edb6752016-03-21 17:37:52 +01001528 try:
1529 self._reload_connection()
tierno98e909c2017-10-14 13:27:03 +02001530 # delete VM ports attached to this networks before the virtual machine
1531 for k, v in created_items.items():
1532 if not v: # skip already deleted
1533 continue
tierno7edb6752016-03-21 17:37:52 +01001534 try:
tiernoad6bdd42018-01-10 10:43:46 +01001535 k_item, _, k_id = k.partition(":")
1536 if k_item == "port":
1537 self.neutron.delete_port(k_id)
tierno7edb6752016-03-21 17:37:52 +01001538 except Exception as e:
tierno00e3df72017-11-29 17:20:13 +01001539 self.logger.error("Error deleting port: {}: {}".format(type(e).__name__, e))
montesmoreno0c8def02016-12-22 12:16:23 +00001540
tierno98e909c2017-10-14 13:27:03 +02001541 # #commented because detaching the volumes makes the servers.delete not work properly ?!?
1542 # #dettach volumes attached
1543 # server = self.nova.servers.get(vm_id)
1544 # volumes_attached_dict = server._info['os-extended-volumes:volumes_attached'] #volume['id']
1545 # #for volume in volumes_attached_dict:
1546 # # self.cinder.volumes.detach(volume['id'])
montesmoreno0c8def02016-12-22 12:16:23 +00001547
tierno98e909c2017-10-14 13:27:03 +02001548 if vm_id:
1549 self.nova.servers.delete(vm_id)
montesmoreno0c8def02016-12-22 12:16:23 +00001550
tierno98e909c2017-10-14 13:27:03 +02001551 # delete volumes. Although having detached, they should have in active status before deleting
1552 # we ensure in this loop
montesmoreno0c8def02016-12-22 12:16:23 +00001553 keep_waiting = True
1554 elapsed_time = 0
1555 while keep_waiting and elapsed_time < volume_timeout:
1556 keep_waiting = False
tierno98e909c2017-10-14 13:27:03 +02001557 for k, v in created_items.items():
1558 if not v: # skip already deleted
1559 continue
1560 try:
tiernoad6bdd42018-01-10 10:43:46 +01001561 k_item, _, k_id = k.partition(":")
1562 if k_item == "volume":
1563 if self.cinder.volumes.get(k_id).status != 'available':
tierno98e909c2017-10-14 13:27:03 +02001564 keep_waiting = True
1565 else:
tiernoad6bdd42018-01-10 10:43:46 +01001566 self.cinder.volumes.delete(k_id)
tierno98e909c2017-10-14 13:27:03 +02001567 except Exception as e:
tierno00e3df72017-11-29 17:20:13 +01001568 self.logger.error("Error deleting volume: {}: {}".format(type(e).__name__, e))
montesmoreno0c8def02016-12-22 12:16:23 +00001569 if keep_waiting:
1570 time.sleep(1)
1571 elapsed_time += 1
tierno98e909c2017-10-14 13:27:03 +02001572 return None
tierno8e995ce2016-09-22 08:13:00 +00001573 except (nvExceptions.NotFound, ksExceptions.ClientException, nvExceptions.ClientException, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +02001574 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +01001575
tiernoae4a8d12016-07-08 12:30:39 +02001576 def refresh_vms_status(self, vm_list):
1577 '''Get the status of the virtual machines and their interfaces/ports
1578 Params: the list of VM identifiers
1579 Returns a dictionary with:
1580 vm_id: #VIM id of this Virtual Machine
1581 status: #Mandatory. Text with one of:
1582 # DELETED (not found at vim)
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001583 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
tiernoae4a8d12016-07-08 12:30:39 +02001584 # OTHER (Vim reported other status not understood)
1585 # ERROR (VIM indicates an ERROR status)
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001586 # ACTIVE, PAUSED, SUSPENDED, INACTIVE (not running),
tiernoae4a8d12016-07-08 12:30:39 +02001587 # CREATING (on building process), ERROR
1588 # ACTIVE:NoMgmtIP (Active but any of its interface has an IP address
1589 #
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001590 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
tiernoae4a8d12016-07-08 12:30:39 +02001591 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
1592 interfaces:
1593 - vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
1594 mac_address: #Text format XX:XX:XX:XX:XX:XX
1595 vim_net_id: #network id where this interface is connected
1596 vim_interface_id: #interface/port VIM id
1597 ip_address: #null, or text with IPv4, IPv6 address
tierno867ffe92017-03-27 12:50:34 +02001598 compute_node: #identification of compute node where PF,VF interface is allocated
1599 pci: #PCI address of the NIC that hosts the PF,VF
1600 vlan: #physical VLAN used for VF
tierno7edb6752016-03-21 17:37:52 +01001601 '''
tiernoae4a8d12016-07-08 12:30:39 +02001602 vm_dict={}
1603 self.logger.debug("refresh_vms status: Getting tenant VM instance information from VIM")
1604 for vm_id in vm_list:
1605 vm={}
1606 try:
1607 vm_vim = self.get_vminstance(vm_id)
1608 if vm_vim['status'] in vmStatus2manoFormat:
1609 vm['status'] = vmStatus2manoFormat[ vm_vim['status'] ]
tierno7edb6752016-03-21 17:37:52 +01001610 else:
tiernoae4a8d12016-07-08 12:30:39 +02001611 vm['status'] = "OTHER"
1612 vm['error_msg'] = "VIM status reported " + vm_vim['status']
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001613
1614 vm['vim_info'] = self.serialize(vm_vim)
1615
tiernoae4a8d12016-07-08 12:30:39 +02001616 vm["interfaces"] = []
1617 if vm_vim.get('fault'):
1618 vm['error_msg'] = str(vm_vim['fault'])
1619 #get interfaces
tierno7edb6752016-03-21 17:37:52 +01001620 try:
tiernoae4a8d12016-07-08 12:30:39 +02001621 self._reload_connection()
tiernob42fd9b2018-06-20 10:44:32 +02001622 port_dict = self.neutron.list_ports(device_id=vm_id)
tiernoae4a8d12016-07-08 12:30:39 +02001623 for port in port_dict["ports"]:
1624 interface={}
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001625 interface['vim_info'] = self.serialize(port)
tiernoae4a8d12016-07-08 12:30:39 +02001626 interface["mac_address"] = port.get("mac_address")
1627 interface["vim_net_id"] = port["network_id"]
1628 interface["vim_interface_id"] = port["id"]
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001629 # check if OS-EXT-SRV-ATTR:host is there,
Mike Marchetti5b9da422017-05-02 15:35:47 -04001630 # in case of non-admin credentials, it will be missing
1631 if vm_vim.get('OS-EXT-SRV-ATTR:host'):
1632 interface["compute_node"] = vm_vim['OS-EXT-SRV-ATTR:host']
tierno867ffe92017-03-27 12:50:34 +02001633 interface["pci"] = None
Mike Marchetti5b9da422017-05-02 15:35:47 -04001634
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001635 # check if binding:profile is there,
Mike Marchetti5b9da422017-05-02 15:35:47 -04001636 # in case of non-admin credentials, it will be missing
1637 if port.get('binding:profile'):
1638 if port['binding:profile'].get('pci_slot'):
1639 # TODO: At the moment sr-iov pci addresses are converted to PF pci addresses by setting the slot to 0x00
1640 # TODO: This is just a workaround valid for niantinc. Find a better way to do so
1641 # CHANGE DDDD:BB:SS.F to DDDD:BB:00.(F%2) assuming there are 2 ports per nic
1642 pci = port['binding:profile']['pci_slot']
1643 # interface["pci"] = pci[:-4] + "00." + str(int(pci[-1]) % 2)
1644 interface["pci"] = pci
tierno867ffe92017-03-27 12:50:34 +02001645 interface["vlan"] = None
Pablo Montes Moreno3be0b2a2017-03-30 13:22:15 +02001646 #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 +01001647 network = self.neutron.show_network(port["network_id"])
Pablo Montes Moreno3be0b2a2017-03-30 13:22:15 +02001648 if network['network'].get('provider:network_type') == 'vlan' and \
1649 port.get("binding:vnic_type") == "direct":
tierno867ffe92017-03-27 12:50:34 +02001650 interface["vlan"] = network['network'].get('provider:segmentation_id')
tiernoae4a8d12016-07-08 12:30:39 +02001651 ips=[]
1652 #look for floating ip address
tiernob42fd9b2018-06-20 10:44:32 +02001653 try:
1654 floating_ip_dict = self.neutron.list_floatingips(port_id=port["id"])
1655 if floating_ip_dict.get("floatingips"):
1656 ips.append(floating_ip_dict["floatingips"][0].get("floating_ip_address") )
1657 except Exception:
1658 pass
tierno7edb6752016-03-21 17:37:52 +01001659
tiernoae4a8d12016-07-08 12:30:39 +02001660 for subnet in port["fixed_ips"]:
1661 ips.append(subnet["ip_address"])
1662 interface["ip_address"] = ";".join(ips)
1663 vm["interfaces"].append(interface)
1664 except Exception as e:
tiernob42fd9b2018-06-20 10:44:32 +02001665 self.logger.error("Error getting vm interface information {}: {}".format(type(e).__name__, e),
1666 exc_info=True)
tiernoae4a8d12016-07-08 12:30:39 +02001667 except vimconn.vimconnNotFoundException as e:
1668 self.logger.error("Exception getting vm status: %s", str(e))
1669 vm['status'] = "DELETED"
1670 vm['error_msg'] = str(e)
1671 except vimconn.vimconnException as e:
1672 self.logger.error("Exception getting vm status: %s", str(e))
1673 vm['status'] = "VIM_ERROR"
1674 vm['error_msg'] = str(e)
1675 vm_dict[vm_id] = vm
1676 return vm_dict
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001677
tierno98e909c2017-10-14 13:27:03 +02001678 def action_vminstance(self, vm_id, action_dict, created_items={}):
tierno7edb6752016-03-21 17:37:52 +01001679 '''Send and action over a VM instance from VIM
tierno98e909c2017-10-14 13:27:03 +02001680 Returns None or the console dict if the action was successfully sent to the VIM'''
tiernoae4a8d12016-07-08 12:30:39 +02001681 self.logger.debug("Action over VM '%s': %s", vm_id, str(action_dict))
tierno7edb6752016-03-21 17:37:52 +01001682 try:
1683 self._reload_connection()
1684 server = self.nova.servers.find(id=vm_id)
1685 if "start" in action_dict:
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001686 if action_dict["start"]=="rebuild":
tierno7edb6752016-03-21 17:37:52 +01001687 server.rebuild()
1688 else:
1689 if server.status=="PAUSED":
1690 server.unpause()
1691 elif server.status=="SUSPENDED":
1692 server.resume()
1693 elif server.status=="SHUTOFF":
1694 server.start()
1695 elif "pause" in action_dict:
1696 server.pause()
1697 elif "resume" in action_dict:
1698 server.resume()
1699 elif "shutoff" in action_dict or "shutdown" in action_dict:
1700 server.stop()
1701 elif "forceOff" in action_dict:
1702 server.stop() #TODO
1703 elif "terminate" in action_dict:
1704 server.delete()
1705 elif "createImage" in action_dict:
1706 server.create_image()
1707 #"path":path_schema,
1708 #"description":description_schema,
1709 #"name":name_schema,
1710 #"metadata":metadata_schema,
1711 #"imageRef": id_schema,
1712 #"disk": {"oneOf":[{"type": "null"}, {"type":"string"}] },
1713 elif "rebuild" in action_dict:
1714 server.rebuild(server.image['id'])
1715 elif "reboot" in action_dict:
1716 server.reboot() #reboot_type='SOFT'
1717 elif "console" in action_dict:
1718 console_type = action_dict["console"]
1719 if console_type == None or console_type == "novnc":
1720 console_dict = server.get_vnc_console("novnc")
1721 elif console_type == "xvpvnc":
1722 console_dict = server.get_vnc_console(console_type)
1723 elif console_type == "rdp-html5":
1724 console_dict = server.get_rdp_console(console_type)
1725 elif console_type == "spice-html5":
1726 console_dict = server.get_spice_console(console_type)
1727 else:
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001728 raise vimconn.vimconnException("console type '{}' not allowed".format(console_type),
tiernoae4a8d12016-07-08 12:30:39 +02001729 http_code=vimconn.HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001730 try:
1731 console_url = console_dict["console"]["url"]
1732 #parse console_url
1733 protocol_index = console_url.find("//")
1734 suffix_index = console_url[protocol_index+2:].find("/") + protocol_index+2
1735 port_index = console_url[protocol_index+2:suffix_index].find(":") + protocol_index+2
1736 if protocol_index < 0 or port_index<0 or suffix_index<0:
tiernoae4a8d12016-07-08 12:30:39 +02001737 raise vimconn.vimconnException("Unexpected response from VIM " + str(console_dict))
tierno7edb6752016-03-21 17:37:52 +01001738 console_dict2={"protocol": console_url[0:protocol_index],
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001739 "server": console_url[protocol_index+2 : port_index],
1740 "port": int(console_url[port_index+1 : suffix_index]),
1741 "suffix": console_url[suffix_index+1:]
tierno7edb6752016-03-21 17:37:52 +01001742 }
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001743 return console_dict2
tiernoae4a8d12016-07-08 12:30:39 +02001744 except Exception as e:
1745 raise vimconn.vimconnException("Unexpected response from VIM " + str(console_dict))
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001746
tierno98e909c2017-10-14 13:27:03 +02001747 return None
tierno8e995ce2016-09-22 08:13:00 +00001748 except (ksExceptions.ClientException, nvExceptions.ClientException, nvExceptions.NotFound, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +02001749 self._format_exception(e)
1750 #TODO insert exception vimconn.HTTP_Unauthorized
1751
kate721d79b2017-06-24 04:21:38 -07001752 ####### VIO Specific Changes #########
garciadeblasebd66722019-01-31 16:01:31 +00001753 def _generate_vlanID(self):
kate721d79b2017-06-24 04:21:38 -07001754 """
1755 Method to get unused vlanID
1756 Args:
1757 None
1758 Returns:
1759 vlanID
1760 """
1761 #Get used VLAN IDs
1762 usedVlanIDs = []
1763 networks = self.get_network_list()
1764 for net in networks:
1765 if net.get('provider:segmentation_id'):
1766 usedVlanIDs.append(net.get('provider:segmentation_id'))
1767 used_vlanIDs = set(usedVlanIDs)
1768
1769 #find unused VLAN ID
1770 for vlanID_range in self.config.get('dataplane_net_vlan_range'):
1771 try:
1772 start_vlanid , end_vlanid = map(int, vlanID_range.replace(" ", "").split("-"))
1773 for vlanID in xrange(start_vlanid, end_vlanid + 1):
1774 if vlanID not in used_vlanIDs:
1775 return vlanID
1776 except Exception as exp:
1777 raise vimconn.vimconnException("Exception {} occurred while generating VLAN ID.".format(exp))
1778 else:
1779 raise vimconn.vimconnConflictException("Unable to create the SRIOV VLAN network."\
1780 " All given Vlan IDs {} are in use.".format(self.config.get('dataplane_net_vlan_range')))
1781
1782
garciadeblasebd66722019-01-31 16:01:31 +00001783 def _generate_multisegment_vlanID(self):
1784 """
1785 Method to get unused vlanID
1786 Args:
1787 None
1788 Returns:
1789 vlanID
1790 """
1791 #Get used VLAN IDs
1792 usedVlanIDs = []
1793 networks = self.get_network_list()
1794 for net in networks:
1795 if net.get('provider:network_type') == "vlan" and net.get('provider:segmentation_id'):
1796 usedVlanIDs.append(net.get('provider:segmentation_id'))
1797 elif net.get('segments'):
1798 for segment in net.get('segments'):
1799 if segment.get('provider:network_type') == "vlan" and segment.get('provider:segmentation_id'):
1800 usedVlanIDs.append(segment.get('provider:segmentation_id'))
1801 used_vlanIDs = set(usedVlanIDs)
1802
1803 #find unused VLAN ID
1804 for vlanID_range in self.config.get('multisegment_vlan_range'):
1805 try:
1806 start_vlanid , end_vlanid = map(int, vlanID_range.replace(" ", "").split("-"))
1807 for vlanID in xrange(start_vlanid, end_vlanid + 1):
1808 if vlanID not in used_vlanIDs:
1809 return vlanID
1810 except Exception as exp:
1811 raise vimconn.vimconnException("Exception {} occurred while generating VLAN ID.".format(exp))
1812 else:
1813 raise vimconn.vimconnConflictException("Unable to create the VLAN segment."\
1814 " All VLAN IDs {} are in use.".format(self.config.get('multisegment_vlan_range')))
1815
1816
1817 def _validate_vlan_ranges(self, input_vlan_range, text_vlan_range):
kate721d79b2017-06-24 04:21:38 -07001818 """
1819 Method to validate user given vlanID ranges
1820 Args: None
1821 Returns: None
1822 """
garciadeblasebd66722019-01-31 16:01:31 +00001823 for vlanID_range in input_vlan_range:
kate721d79b2017-06-24 04:21:38 -07001824 vlan_range = vlanID_range.replace(" ", "")
1825 #validate format
1826 vlanID_pattern = r'(\d)*-(\d)*$'
1827 match_obj = re.match(vlanID_pattern, vlan_range)
1828 if not match_obj:
garciadeblasebd66722019-01-31 16:01:31 +00001829 raise vimconn.vimconnConflictException("Invalid VLAN range for {}: {}.You must provide "\
1830 "'{}' in format [start_ID - end_ID].".format(text_vlan_range, vlanID_range, text_vlan_range))
kate721d79b2017-06-24 04:21:38 -07001831
1832 start_vlanid , end_vlanid = map(int,vlan_range.split("-"))
1833 if start_vlanid <= 0 :
garciadeblasebd66722019-01-31 16:01:31 +00001834 raise vimconn.vimconnConflictException("Invalid VLAN range for {}: {}."\
kate721d79b2017-06-24 04:21:38 -07001835 "Start ID can not be zero. For VLAN "\
garciadeblasebd66722019-01-31 16:01:31 +00001836 "networks valid IDs are 1 to 4094 ".format(text_vlan_range, vlanID_range))
kate721d79b2017-06-24 04:21:38 -07001837 if end_vlanid > 4094 :
garciadeblasebd66722019-01-31 16:01:31 +00001838 raise vimconn.vimconnConflictException("Invalid VLAN range for {}: {}."\
kate721d79b2017-06-24 04:21:38 -07001839 "End VLAN ID can not be greater than 4094. For VLAN "\
garciadeblasebd66722019-01-31 16:01:31 +00001840 "networks valid IDs are 1 to 4094 ".format(text_vlan_range, vlanID_range))
kate721d79b2017-06-24 04:21:38 -07001841
1842 if start_vlanid > end_vlanid:
garciadeblasebd66722019-01-31 16:01:31 +00001843 raise vimconn.vimconnConflictException("Invalid VLAN range for {}: {}."\
1844 "You must provide '{}' in format start_ID - end_ID and "\
1845 "start_ID < end_ID ".format(text_vlan_range, vlanID_range, text_vlan_range))
kate721d79b2017-06-24 04:21:38 -07001846
tiernoae4a8d12016-07-08 12:30:39 +02001847#NOT USED FUNCTIONS
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001848
tiernoae4a8d12016-07-08 12:30:39 +02001849 def new_external_port(self, port_data):
1850 #TODO openstack if needed
1851 '''Adds a external port to VIM'''
1852 '''Returns the port identifier'''
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001853 return -vimconn.HTTP_Internal_Server_Error, "osconnector.new_external_port() not implemented"
1854
tiernoae4a8d12016-07-08 12:30:39 +02001855 def connect_port_network(self, port_id, network_id, admin=False):
1856 #TODO openstack if needed
1857 '''Connects a external port to a network'''
1858 '''Returns status code of the VIM response'''
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001859 return -vimconn.HTTP_Internal_Server_Error, "osconnector.connect_port_network() not implemented"
1860
tiernoae4a8d12016-07-08 12:30:39 +02001861 def new_user(self, user_name, user_passwd, tenant_id=None):
1862 '''Adds a new user to openstack VIM'''
1863 '''Returns the user identifier'''
1864 self.logger.debug("osconnector: Adding a new user to VIM")
1865 try:
1866 self._reload_connection()
Eduardo Sousae3c0dbc2018-09-03 11:56:07 +01001867 user=self.keystone.users.create(user_name, password=user_passwd, default_project=tenant_id)
tiernoae4a8d12016-07-08 12:30:39 +02001868 #self.keystone.tenants.add_user(self.k_creds["username"], #role)
1869 return user.id
1870 except ksExceptions.ConnectionError as e:
1871 error_value=-vimconn.HTTP_Bad_Request
1872 error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0]))
1873 except ksExceptions.ClientException as e: #TODO remove
tierno7edb6752016-03-21 17:37:52 +01001874 error_value=-vimconn.HTTP_Bad_Request
1875 error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0]))
1876 #TODO insert exception vimconn.HTTP_Unauthorized
1877 #if reaching here is because an exception
tierno9c5c8322018-03-23 15:44:03 +01001878 self.logger.debug("new_user " + error_text)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001879 return error_value, error_text
tiernoae4a8d12016-07-08 12:30:39 +02001880
1881 def delete_user(self, user_id):
1882 '''Delete a user from openstack VIM'''
1883 '''Returns the user identifier'''
1884 if self.debug:
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001885 print("osconnector: Deleting a user from VIM")
tiernoae4a8d12016-07-08 12:30:39 +02001886 try:
1887 self._reload_connection()
1888 self.keystone.users.delete(user_id)
1889 return 1, user_id
1890 except ksExceptions.ConnectionError as e:
1891 error_value=-vimconn.HTTP_Bad_Request
1892 error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0]))
1893 except ksExceptions.NotFound as e:
1894 error_value=-vimconn.HTTP_Not_Found
1895 error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0]))
1896 except ksExceptions.ClientException as e: #TODO remove
1897 error_value=-vimconn.HTTP_Bad_Request
1898 error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0]))
1899 #TODO insert exception vimconn.HTTP_Unauthorized
1900 #if reaching here is because an exception
tierno9c5c8322018-03-23 15:44:03 +01001901 self.logger.debug("delete_tenant " + error_text)
tiernoae4a8d12016-07-08 12:30:39 +02001902 return error_value, error_text
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001903
tierno7edb6752016-03-21 17:37:52 +01001904 def get_hosts_info(self):
1905 '''Get the information of deployed hosts
1906 Returns the hosts content'''
1907 if self.debug:
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001908 print("osconnector: Getting Host info from VIM")
tierno7edb6752016-03-21 17:37:52 +01001909 try:
1910 h_list=[]
1911 self._reload_connection()
1912 hypervisors = self.nova.hypervisors.list()
1913 for hype in hypervisors:
1914 h_list.append( hype.to_dict() )
1915 return 1, {"hosts":h_list}
1916 except nvExceptions.NotFound as e:
1917 error_value=-vimconn.HTTP_Not_Found
1918 error_text= (str(e) if len(e.args)==0 else str(e.args[0]))
1919 except (ksExceptions.ClientException, nvExceptions.ClientException) as e:
1920 error_value=-vimconn.HTTP_Bad_Request
1921 error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0]))
1922 #TODO insert exception vimconn.HTTP_Unauthorized
1923 #if reaching here is because an exception
tierno9c5c8322018-03-23 15:44:03 +01001924 self.logger.debug("get_hosts_info " + error_text)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001925 return error_value, error_text
tierno7edb6752016-03-21 17:37:52 +01001926
1927 def get_hosts(self, vim_tenant):
1928 '''Get the hosts and deployed instances
1929 Returns the hosts content'''
1930 r, hype_dict = self.get_hosts_info()
1931 if r<0:
1932 return r, hype_dict
1933 hypervisors = hype_dict["hosts"]
1934 try:
1935 servers = self.nova.servers.list()
1936 for hype in hypervisors:
1937 for server in servers:
1938 if server.to_dict()['OS-EXT-SRV-ATTR:hypervisor_hostname']==hype['hypervisor_hostname']:
1939 if 'vm' in hype:
1940 hype['vm'].append(server.id)
1941 else:
1942 hype['vm'] = [server.id]
1943 return 1, hype_dict
1944 except nvExceptions.NotFound as e:
1945 error_value=-vimconn.HTTP_Not_Found
1946 error_text= (str(e) if len(e.args)==0 else str(e.args[0]))
1947 except (ksExceptions.ClientException, nvExceptions.ClientException) as e:
1948 error_value=-vimconn.HTTP_Bad_Request
1949 error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0]))
1950 #TODO insert exception vimconn.HTTP_Unauthorized
1951 #if reaching here is because an exception
tierno9c5c8322018-03-23 15:44:03 +01001952 self.logger.debug("get_hosts " + error_text)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001953 return error_value, error_text
tierno7edb6752016-03-21 17:37:52 +01001954
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001955 def new_classification(self, name, ctype, definition):
1956 self.logger.debug(
1957 'Adding a new (Traffic) Classification to VIM, named %s', name)
1958 try:
1959 new_class = None
1960 self._reload_connection()
1961 if ctype not in supportedClassificationTypes:
1962 raise vimconn.vimconnNotSupportedException(
1963 'OpenStack VIM connector doesn\'t support provided '
1964 'Classification Type {}, supported ones are: '
1965 '{}'.format(ctype, supportedClassificationTypes))
1966 if not self._validate_classification(ctype, definition):
1967 raise vimconn.vimconnException(
1968 'Incorrect Classification definition '
1969 'for the type specified.')
1970 classification_dict = definition
1971 classification_dict['name'] = name
tierno7edb6752016-03-21 17:37:52 +01001972
Igor D.Ccaadc442017-11-06 12:48:48 +00001973 new_class = self.neutron.create_sfc_flow_classifier(
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001974 {'flow_classifier': classification_dict})
1975 return new_class['flow_classifier']['id']
1976 except (neExceptions.ConnectionFailed, ksExceptions.ClientException,
1977 neExceptions.NeutronException, ConnectionError) as e:
1978 self.logger.error(
1979 'Creation of Classification failed.')
1980 self._format_exception(e)
1981
1982 def get_classification(self, class_id):
1983 self.logger.debug(" Getting Classification %s from VIM", class_id)
1984 filter_dict = {"id": class_id}
1985 class_list = self.get_classification_list(filter_dict)
1986 if len(class_list) == 0:
1987 raise vimconn.vimconnNotFoundException(
1988 "Classification '{}' not found".format(class_id))
1989 elif len(class_list) > 1:
1990 raise vimconn.vimconnConflictException(
1991 "Found more than one Classification with this criteria")
1992 classification = class_list[0]
1993 return classification
1994
1995 def get_classification_list(self, filter_dict={}):
1996 self.logger.debug("Getting Classifications from VIM filter: '%s'",
1997 str(filter_dict))
1998 try:
tierno69b590e2018-03-13 18:52:23 +01001999 filter_dict_os = filter_dict.copy()
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00002000 self._reload_connection()
tierno69b590e2018-03-13 18:52:23 +01002001 if self.api_version3 and "tenant_id" in filter_dict_os:
2002 filter_dict_os['project_id'] = filter_dict_os.pop('tenant_id')
Igor D.Ccaadc442017-11-06 12:48:48 +00002003 classification_dict = self.neutron.list_sfc_flow_classifiers(
tierno69b590e2018-03-13 18:52:23 +01002004 **filter_dict_os)
2005 classification_list = classification_dict["flow_classifiers"]
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00002006 self.__classification_os2mano(classification_list)
2007 return classification_list
2008 except (neExceptions.ConnectionFailed, ksExceptions.ClientException,
2009 neExceptions.NeutronException, ConnectionError) as e:
2010 self._format_exception(e)
2011
2012 def delete_classification(self, class_id):
2013 self.logger.debug("Deleting Classification '%s' from VIM", class_id)
2014 try:
2015 self._reload_connection()
Igor D.Ccaadc442017-11-06 12:48:48 +00002016 self.neutron.delete_sfc_flow_classifier(class_id)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00002017 return class_id
2018 except (neExceptions.ConnectionFailed, neExceptions.NeutronException,
2019 ksExceptions.ClientException, neExceptions.NeutronException,
2020 ConnectionError) as e:
2021 self._format_exception(e)
2022
2023 def new_sfi(self, name, ingress_ports, egress_ports, sfc_encap=True):
2024 self.logger.debug(
2025 "Adding a new Service Function Instance to VIM, named '%s'", name)
2026 try:
2027 new_sfi = None
2028 self._reload_connection()
2029 correlation = None
2030 if sfc_encap:
Igor D.Ccaadc442017-11-06 12:48:48 +00002031 correlation = 'nsh'
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00002032 if len(ingress_ports) != 1:
2033 raise vimconn.vimconnNotSupportedException(
2034 "OpenStack VIM connector can only have "
2035 "1 ingress port per SFI")
2036 if len(egress_ports) != 1:
2037 raise vimconn.vimconnNotSupportedException(
2038 "OpenStack VIM connector can only have "
2039 "1 egress port per SFI")
2040 sfi_dict = {'name': name,
2041 'ingress': ingress_ports[0],
2042 'egress': egress_ports[0],
2043 'service_function_parameters': {
2044 'correlation': correlation}}
Igor D.Ccaadc442017-11-06 12:48:48 +00002045 new_sfi = self.neutron.create_sfc_port_pair({'port_pair': sfi_dict})
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00002046 return new_sfi['port_pair']['id']
2047 except (neExceptions.ConnectionFailed, ksExceptions.ClientException,
2048 neExceptions.NeutronException, ConnectionError) as e:
2049 if new_sfi:
2050 try:
Igor D.Ccaadc442017-11-06 12:48:48 +00002051 self.neutron.delete_sfc_port_pair(
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00002052 new_sfi['port_pair']['id'])
2053 except Exception:
2054 self.logger.error(
2055 'Creation of Service Function Instance failed, with '
2056 'subsequent deletion failure as well.')
2057 self._format_exception(e)
2058
2059 def get_sfi(self, sfi_id):
2060 self.logger.debug(
2061 'Getting Service Function Instance %s from VIM', sfi_id)
2062 filter_dict = {"id": sfi_id}
2063 sfi_list = self.get_sfi_list(filter_dict)
2064 if len(sfi_list) == 0:
2065 raise vimconn.vimconnNotFoundException(
2066 "Service Function Instance '{}' not found".format(sfi_id))
2067 elif len(sfi_list) > 1:
2068 raise vimconn.vimconnConflictException(
2069 'Found more than one Service Function Instance '
2070 'with this criteria')
2071 sfi = sfi_list[0]
2072 return sfi
2073
2074 def get_sfi_list(self, filter_dict={}):
2075 self.logger.debug("Getting Service Function Instances from "
2076 "VIM filter: '%s'", str(filter_dict))
2077 try:
2078 self._reload_connection()
tierno69b590e2018-03-13 18:52:23 +01002079 filter_dict_os = filter_dict.copy()
2080 if self.api_version3 and "tenant_id" in filter_dict_os:
2081 filter_dict_os['project_id'] = filter_dict_os.pop('tenant_id')
2082 sfi_dict = self.neutron.list_sfc_port_pairs(**filter_dict_os)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00002083 sfi_list = sfi_dict["port_pairs"]
2084 self.__sfi_os2mano(sfi_list)
2085 return sfi_list
2086 except (neExceptions.ConnectionFailed, ksExceptions.ClientException,
2087 neExceptions.NeutronException, ConnectionError) as e:
2088 self._format_exception(e)
2089
2090 def delete_sfi(self, sfi_id):
2091 self.logger.debug("Deleting Service Function Instance '%s' "
2092 "from VIM", sfi_id)
2093 try:
2094 self._reload_connection()
Igor D.Ccaadc442017-11-06 12:48:48 +00002095 self.neutron.delete_sfc_port_pair(sfi_id)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00002096 return sfi_id
2097 except (neExceptions.ConnectionFailed, neExceptions.NeutronException,
2098 ksExceptions.ClientException, neExceptions.NeutronException,
2099 ConnectionError) as e:
2100 self._format_exception(e)
2101
2102 def new_sf(self, name, sfis, sfc_encap=True):
2103 self.logger.debug("Adding a new Service Function to VIM, "
2104 "named '%s'", name)
2105 try:
2106 new_sf = None
2107 self._reload_connection()
tierno9c5c8322018-03-23 15:44:03 +01002108 # correlation = None
2109 # if sfc_encap:
2110 # correlation = 'nsh'
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00002111 for instance in sfis:
2112 sfi = self.get_sfi(instance)
Igor D.Ccaadc442017-11-06 12:48:48 +00002113 if sfi.get('sfc_encap') != sfc_encap:
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00002114 raise vimconn.vimconnNotSupportedException(
2115 "OpenStack VIM connector requires all SFIs of the "
2116 "same SF to share the same SFC Encapsulation")
2117 sf_dict = {'name': name,
2118 'port_pairs': sfis}
Igor D.Ccaadc442017-11-06 12:48:48 +00002119 new_sf = self.neutron.create_sfc_port_pair_group({
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00002120 'port_pair_group': sf_dict})
2121 return new_sf['port_pair_group']['id']
2122 except (neExceptions.ConnectionFailed, ksExceptions.ClientException,
2123 neExceptions.NeutronException, ConnectionError) as e:
2124 if new_sf:
2125 try:
Igor D.Ccaadc442017-11-06 12:48:48 +00002126 self.neutron.delete_sfc_port_pair_group(
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00002127 new_sf['port_pair_group']['id'])
2128 except Exception:
2129 self.logger.error(
2130 'Creation of Service Function failed, with '
2131 'subsequent deletion failure as well.')
2132 self._format_exception(e)
2133
2134 def get_sf(self, sf_id):
2135 self.logger.debug("Getting Service Function %s from VIM", sf_id)
2136 filter_dict = {"id": sf_id}
2137 sf_list = self.get_sf_list(filter_dict)
2138 if len(sf_list) == 0:
2139 raise vimconn.vimconnNotFoundException(
2140 "Service Function '{}' not found".format(sf_id))
2141 elif len(sf_list) > 1:
2142 raise vimconn.vimconnConflictException(
2143 "Found more than one Service Function with this criteria")
2144 sf = sf_list[0]
2145 return sf
2146
2147 def get_sf_list(self, filter_dict={}):
2148 self.logger.debug("Getting Service Function from VIM filter: '%s'",
2149 str(filter_dict))
2150 try:
2151 self._reload_connection()
tierno69b590e2018-03-13 18:52:23 +01002152 filter_dict_os = filter_dict.copy()
2153 if self.api_version3 and "tenant_id" in filter_dict_os:
2154 filter_dict_os['project_id'] = filter_dict_os.pop('tenant_id')
2155 sf_dict = self.neutron.list_sfc_port_pair_groups(**filter_dict_os)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00002156 sf_list = sf_dict["port_pair_groups"]
2157 self.__sf_os2mano(sf_list)
2158 return sf_list
2159 except (neExceptions.ConnectionFailed, ksExceptions.ClientException,
2160 neExceptions.NeutronException, ConnectionError) as e:
2161 self._format_exception(e)
2162
2163 def delete_sf(self, sf_id):
2164 self.logger.debug("Deleting Service Function '%s' from VIM", sf_id)
2165 try:
2166 self._reload_connection()
Igor D.Ccaadc442017-11-06 12:48:48 +00002167 self.neutron.delete_sfc_port_pair_group(sf_id)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00002168 return sf_id
2169 except (neExceptions.ConnectionFailed, neExceptions.NeutronException,
2170 ksExceptions.ClientException, neExceptions.NeutronException,
2171 ConnectionError) as e:
2172 self._format_exception(e)
2173
2174 def new_sfp(self, name, classifications, sfs, sfc_encap=True, spi=None):
2175 self.logger.debug("Adding a new Service Function Path to VIM, "
2176 "named '%s'", name)
2177 try:
2178 new_sfp = None
2179 self._reload_connection()
Igor D.Ccaadc442017-11-06 12:48:48 +00002180 # In networking-sfc the MPLS encapsulation is legacy
2181 # should be used when no full SFC Encapsulation is intended
schillinge981df9a2019-01-24 09:25:11 +01002182 correlation = 'mpls'
Igor D.Ccaadc442017-11-06 12:48:48 +00002183 if sfc_encap:
2184 correlation = 'nsh'
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00002185 sfp_dict = {'name': name,
2186 'flow_classifiers': classifications,
2187 'port_pair_groups': sfs,
2188 'chain_parameters': {'correlation': correlation}}
2189 if spi:
2190 sfp_dict['chain_id'] = spi
Igor D.Ccaadc442017-11-06 12:48:48 +00002191 new_sfp = self.neutron.create_sfc_port_chain({'port_chain': sfp_dict})
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00002192 return new_sfp["port_chain"]["id"]
2193 except (neExceptions.ConnectionFailed, ksExceptions.ClientException,
2194 neExceptions.NeutronException, ConnectionError) as e:
2195 if new_sfp:
2196 try:
Igor D.Ccaadc442017-11-06 12:48:48 +00002197 self.neutron.delete_sfc_port_chain(new_sfp['port_chain']['id'])
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00002198 except Exception:
2199 self.logger.error(
2200 'Creation of Service Function Path failed, with '
2201 'subsequent deletion failure as well.')
2202 self._format_exception(e)
2203
2204 def get_sfp(self, sfp_id):
2205 self.logger.debug(" Getting Service Function Path %s from VIM", sfp_id)
2206 filter_dict = {"id": sfp_id}
2207 sfp_list = self.get_sfp_list(filter_dict)
2208 if len(sfp_list) == 0:
2209 raise vimconn.vimconnNotFoundException(
2210 "Service Function Path '{}' not found".format(sfp_id))
2211 elif len(sfp_list) > 1:
2212 raise vimconn.vimconnConflictException(
2213 "Found more than one Service Function Path with this criteria")
2214 sfp = sfp_list[0]
2215 return sfp
2216
2217 def get_sfp_list(self, filter_dict={}):
2218 self.logger.debug("Getting Service Function Paths from VIM filter: "
2219 "'%s'", str(filter_dict))
2220 try:
2221 self._reload_connection()
tierno69b590e2018-03-13 18:52:23 +01002222 filter_dict_os = filter_dict.copy()
2223 if self.api_version3 and "tenant_id" in filter_dict_os:
2224 filter_dict_os['project_id'] = filter_dict_os.pop('tenant_id')
2225 sfp_dict = self.neutron.list_sfc_port_chains(**filter_dict_os)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00002226 sfp_list = sfp_dict["port_chains"]
2227 self.__sfp_os2mano(sfp_list)
2228 return sfp_list
2229 except (neExceptions.ConnectionFailed, ksExceptions.ClientException,
2230 neExceptions.NeutronException, ConnectionError) as e:
2231 self._format_exception(e)
2232
2233 def delete_sfp(self, sfp_id):
2234 self.logger.debug(
2235 "Deleting Service Function Path '%s' from VIM", sfp_id)
2236 try:
2237 self._reload_connection()
Igor D.Ccaadc442017-11-06 12:48:48 +00002238 self.neutron.delete_sfc_port_chain(sfp_id)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00002239 return sfp_id
2240 except (neExceptions.ConnectionFailed, neExceptions.NeutronException,
2241 ksExceptions.ClientException, neExceptions.NeutronException,
2242 ConnectionError) as e:
2243 self._format_exception(e)