blob: 3515ef681ed29653b9eea92185ddbfcfbd643705 [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
tiernoae4a8d12016-07-08 12:30:39 +0200451 def get_tenant_list(self, filter_dict={}):
452 '''Obtain tenants of VIM
453 filter_dict can contain the following keys:
454 name: filter by tenant name
455 id: filter by tenant uuid/id
456 <other VIM specific>
457 Returns the tenant list of dictionaries: [{'name':'<name>, 'id':'<id>, ...}, ...]
458 '''
ahmadsa95baa272016-11-30 09:14:11 +0500459 self.logger.debug("Getting tenants from VIM filter: '%s'", str(filter_dict))
tiernoae4a8d12016-07-08 12:30:39 +0200460 try:
461 self._reload_connection()
tiernof716aea2017-06-21 18:01:40 +0200462 if self.api_version3:
463 project_class_list = self.keystone.projects.list(name=filter_dict.get("name"))
ahmadsa95baa272016-11-30 09:14:11 +0500464 else:
tiernof716aea2017-06-21 18:01:40 +0200465 project_class_list = self.keystone.tenants.findall(**filter_dict)
ahmadsa95baa272016-11-30 09:14:11 +0500466 project_list=[]
467 for project in project_class_list:
tiernof716aea2017-06-21 18:01:40 +0200468 if filter_dict.get('id') and filter_dict["id"] != project.id:
469 continue
ahmadsa95baa272016-11-30 09:14:11 +0500470 project_list.append(project.to_dict())
471 return project_list
tiernof716aea2017-06-21 18:01:40 +0200472 except (ksExceptions.ConnectionError, ksExceptions.ClientException, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200473 self._format_exception(e)
474
475 def new_tenant(self, tenant_name, tenant_description):
476 '''Adds a new tenant to openstack VIM. Returns the tenant identifier'''
477 self.logger.debug("Adding a new tenant name: %s", tenant_name)
478 try:
479 self._reload_connection()
tiernof716aea2017-06-21 18:01:40 +0200480 if self.api_version3:
481 project = self.keystone.projects.create(tenant_name, self.config.get("project_domain_id", "default"),
482 description=tenant_description, is_domain=False)
ahmadsa95baa272016-11-30 09:14:11 +0500483 else:
tiernof716aea2017-06-21 18:01:40 +0200484 project = self.keystone.tenants.create(tenant_name, tenant_description)
ahmadsa95baa272016-11-30 09:14:11 +0500485 return project.id
shashankjain3c83a212018-10-04 13:05:46 +0530486 except (ksExceptions.ConnectionError, ksExceptions.ClientException, ksExceptions.BadRequest, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200487 self._format_exception(e)
488
489 def delete_tenant(self, tenant_id):
490 '''Delete a tenant from openstack VIM. Returns the old tenant identifier'''
491 self.logger.debug("Deleting tenant %s from VIM", tenant_id)
492 try:
493 self._reload_connection()
tiernof716aea2017-06-21 18:01:40 +0200494 if self.api_version3:
ahmadsa95baa272016-11-30 09:14:11 +0500495 self.keystone.projects.delete(tenant_id)
496 else:
497 self.keystone.tenants.delete(tenant_id)
tiernoae4a8d12016-07-08 12:30:39 +0200498 return tenant_id
shashankjain3c83a212018-10-04 13:05:46 +0530499 except (ksExceptions.ConnectionError, ksExceptions.ClientException, ksExceptions.NotFound, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200500 self._format_exception(e)
ahmadsa95baa272016-11-30 09:14:11 +0500501
garciadeblas9f8456e2016-09-05 05:02:59 +0200502 def new_network(self,net_name, net_type, ip_profile=None, shared=False, vlan=None):
garciadeblasebd66722019-01-31 16:01:31 +0000503 """Adds a tenant network to VIM
504 Params:
505 'net_name': name of the network
506 'net_type': one of:
507 'bridge': overlay isolated network
508 'data': underlay E-LAN network for Passthrough and SRIOV interfaces
509 'ptp': underlay E-LINE network for Passthrough and SRIOV interfaces.
510 'ip_profile': is a dict containing the IP parameters of the network
511 'ip_version': can be "IPv4" or "IPv6" (Currently only IPv4 is implemented)
512 'subnet_address': ip_prefix_schema, that is X.X.X.X/Y
513 'gateway_address': (Optional) ip_schema, that is X.X.X.X
514 'dns_address': (Optional) comma separated list of ip_schema, e.g. X.X.X.X[,X,X,X,X]
515 'dhcp_enabled': True or False
516 'dhcp_start_address': ip_schema, first IP to grant
517 'dhcp_count': number of IPs to grant.
518 'shared': if this network can be seen/use by other tenants/organization
519 'vlan': in case of a data or ptp net_type, the intended vlan tag to be used for the network
520 Returns a tuple with the network identifier and created_items, or raises an exception on error
521 created_items can be None or a dictionary where this method can include key-values that will be passed to
522 the method delete_network. Can be used to store created segments, created l2gw connections, etc.
523 Format is vimconnector dependent, but do not use nested dictionaries and a value of None should be the same
524 as not present.
525 """
tiernoae4a8d12016-07-08 12:30:39 +0200526 self.logger.debug("Adding a new network to VIM name '%s', type '%s'", net_name, net_type)
garciadeblasebd66722019-01-31 16:01:31 +0000527 # self.logger.debug(">>>>>>>>>>>>>>>>>> IP profile %s", str(ip_profile))
tierno7edb6752016-03-21 17:37:52 +0100528 try:
garciadeblasedca7b32016-09-29 14:01:52 +0000529 new_net = None
garciadeblasebd66722019-01-31 16:01:31 +0000530 created_items = {}
tierno7edb6752016-03-21 17:37:52 +0100531 self._reload_connection()
532 network_dict = {'name': net_name, 'admin_state_up': True}
533 if net_type=="data" or net_type=="ptp":
534 if self.config.get('dataplane_physical_net') == None:
tiernoae4a8d12016-07-08 12:30:39 +0200535 raise vimconn.vimconnConflictException("You must provide a 'dataplane_physical_net' at config value before creating sriov network")
garciadeblasebd66722019-01-31 16:01:31 +0000536 if not self.config.get('multisegment_support'):
537 network_dict["provider:physical_network"] = self.config[
538 'dataplane_physical_net'] # "physnet_sriov" #TODO physical
539 network_dict["provider:network_type"] = "vlan"
540 if vlan!=None:
541 network_dict["provider:network_type"] = vlan
542 else:
543 ###### Multi-segment case ######
544 segment_list = []
545 segment1_dict = {}
546 segment1_dict["provider:physical_network"] = ''
547 segment1_dict["provider:network_type"] = 'vxlan'
548 segment_list.append(segment1_dict)
549 segment2_dict = {}
550 segment2_dict["provider:physical_network"] = self.config['dataplane_physical_net']
551 segment2_dict["provider:network_type"] = "vlan"
552 if self.config.get('multisegment_vlan_range'):
553 vlanID = self._generate_multisegment_vlanID()
554 segment2_dict["provider:segmentation_id"] = vlanID
555 # else
556 # raise vimconn.vimconnConflictException(
557 # "You must provide 'multisegment_vlan_range' at config dict before creating a multisegment network")
558 segment_list.append(segment2_dict)
559 network_dict["segments"] = segment_list
kate721d79b2017-06-24 04:21:38 -0700560
561 ####### VIO Specific Changes #########
562 if self.vim_type == "VIO":
563 if vlan is not None:
564 network_dict["provider:segmentation_id"] = vlan
565 else:
566 if self.config.get('dataplane_net_vlan_range') is None:
567 raise vimconn.vimconnConflictException("You must provide "\
568 "'dataplane_net_vlan_range' in format [start_ID - end_ID]"\
569 "at config value before creating sriov network with vlan tag")
570
garciadeblasebd66722019-01-31 16:01:31 +0000571 network_dict["provider:segmentation_id"] = self._generate_vlanID()
kate721d79b2017-06-24 04:21:38 -0700572
garciadeblasebd66722019-01-31 16:01:31 +0000573 network_dict["shared"] = shared
574 new_net = self.neutron.create_network({'network':network_dict})
575 # print new_net
576 # create subnetwork, even if there is no profile
garciadeblas9f8456e2016-09-05 05:02:59 +0200577 if not ip_profile:
578 ip_profile = {}
tierno41a69812018-02-16 14:34:33 +0100579 if not ip_profile.get('subnet_address'):
garciadeblas2299e3b2017-01-26 14:35:55 +0000580 #Fake subnet is required
581 subnet_rand = random.randint(0, 255)
582 ip_profile['subnet_address'] = "192.168.{}.0/24".format(subnet_rand)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +0000583 if 'ip_version' not in ip_profile:
garciadeblas9f8456e2016-09-05 05:02:59 +0200584 ip_profile['ip_version'] = "IPv4"
garciadeblasebd66722019-01-31 16:01:31 +0000585 subnet = {"name": net_name+"-subnet",
tierno7edb6752016-03-21 17:37:52 +0100586 "network_id": new_net["network"]["id"],
garciadeblas9f8456e2016-09-05 05:02:59 +0200587 "ip_version": 4 if ip_profile['ip_version']=="IPv4" else 6,
588 "cidr": ip_profile['subnet_address']
tierno7edb6752016-03-21 17:37:52 +0100589 }
tiernoa1fb4462017-06-30 12:25:50 +0200590 # Gateway should be set to None if not needed. Otherwise openstack assigns one by default
tierno41a69812018-02-16 14:34:33 +0100591 if ip_profile.get('gateway_address'):
tierno55d234c2018-07-04 18:29:21 +0200592 subnet['gateway_ip'] = ip_profile['gateway_address']
593 else:
594 subnet['gateway_ip'] = None
garciadeblasedca7b32016-09-29 14:01:52 +0000595 if ip_profile.get('dns_address'):
tierno455612d2017-05-30 16:40:10 +0200596 subnet['dns_nameservers'] = ip_profile['dns_address'].split(";")
garciadeblas9f8456e2016-09-05 05:02:59 +0200597 if 'dhcp_enabled' in ip_profile:
tierno41a69812018-02-16 14:34:33 +0100598 subnet['enable_dhcp'] = False if \
599 ip_profile['dhcp_enabled']=="false" or ip_profile['dhcp_enabled']==False else True
600 if ip_profile.get('dhcp_start_address'):
tiernoa1fb4462017-06-30 12:25:50 +0200601 subnet['allocation_pools'] = []
garciadeblas9f8456e2016-09-05 05:02:59 +0200602 subnet['allocation_pools'].append(dict())
603 subnet['allocation_pools'][0]['start'] = ip_profile['dhcp_start_address']
tierno41a69812018-02-16 14:34:33 +0100604 if ip_profile.get('dhcp_count'):
garciadeblas9f8456e2016-09-05 05:02:59 +0200605 #parts = ip_profile['dhcp_start_address'].split('.')
606 #ip_int = (int(parts[0]) << 24) + (int(parts[1]) << 16) + (int(parts[2]) << 8) + int(parts[3])
607 ip_int = int(netaddr.IPAddress(ip_profile['dhcp_start_address']))
garciadeblas21d795b2016-09-29 17:31:46 +0200608 ip_int += ip_profile['dhcp_count'] - 1
garciadeblas9f8456e2016-09-05 05:02:59 +0200609 ip_str = str(netaddr.IPAddress(ip_int))
610 subnet['allocation_pools'][0]['end'] = ip_str
garciadeblasedca7b32016-09-29 14:01:52 +0000611 #self.logger.debug(">>>>>>>>>>>>>>>>>> Subnet: %s", str(subnet))
tierno7edb6752016-03-21 17:37:52 +0100612 self.neutron.create_subnet({"subnet": subnet} )
garciadeblasebd66722019-01-31 16:01:31 +0000613
614 if net_type == "data" and self.config.get('multisegment_support'):
615 if self.config.get('l2gw_support'):
616 l2gw_list = self.neutron.list_l2_gateways().get("l2_gateways", ())
617 for l2gw in l2gw_list:
618 l2gw_conn = {}
619 l2gw_conn["l2_gateway_id"] = l2gw["id"]
620 l2gw_conn["network_id"] = new_net["network"]["id"]
621 l2gw_conn["segmentation_id"] = str(vlanID)
622 new_l2gw_conn = self.neutron.create_l2_gateway_connection({"l2_gateway_connection": l2gw_conn})
623 created_items["l2gwconn:" + str(new_l2gw_conn["l2_gateway_connection"]["id"])] = True
624 return new_net["network"]["id"], created_items
tierno41a69812018-02-16 14:34:33 +0100625 except Exception as e:
garciadeblasebd66722019-01-31 16:01:31 +0000626 #delete l2gw connections (if any) before deleting the network
627 for k, v in created_items.items():
628 if not v: # skip already deleted
629 continue
630 try:
631 k_item, _, k_id = k.partition(":")
632 if k_item == "l2gwconn":
633 self.neutron.delete_l2_gateway_connection(k_id)
634 except Exception as e2:
635 self.logger.error("Error deleting l2 gateway connection: {}: {}".format(type(e2).__name__, e2))
garciadeblasedca7b32016-09-29 14:01:52 +0000636 if new_net:
637 self.neutron.delete_network(new_net['network']['id'])
tiernoae4a8d12016-07-08 12:30:39 +0200638 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100639
640 def get_network_list(self, filter_dict={}):
641 '''Obtain tenant networks of VIM
642 Filter_dict can be:
643 name: network name
644 id: network uuid
645 shared: boolean
646 tenant_id: tenant
647 admin_state_up: boolean
648 status: 'ACTIVE'
649 Returns the network list of dictionaries
650 '''
tiernoae4a8d12016-07-08 12:30:39 +0200651 self.logger.debug("Getting network from VIM filter: '%s'", str(filter_dict))
tierno7edb6752016-03-21 17:37:52 +0100652 try:
653 self._reload_connection()
tierno69b590e2018-03-13 18:52:23 +0100654 filter_dict_os = filter_dict.copy()
655 if self.api_version3 and "tenant_id" in filter_dict_os:
656 filter_dict_os['project_id'] = filter_dict_os.pop('tenant_id') #T ODO check
657 net_dict = self.neutron.list_networks(**filter_dict_os)
tierno00e3df72017-11-29 17:20:13 +0100658 net_list = net_dict["networks"]
tierno7edb6752016-03-21 17:37:52 +0100659 self.__net_os2mano(net_list)
tiernoae4a8d12016-07-08 12:30:39 +0200660 return net_list
tierno8e995ce2016-09-22 08:13:00 +0000661 except (neExceptions.ConnectionFailed, ksExceptions.ClientException, neExceptions.NeutronException, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200662 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100663
tiernoae4a8d12016-07-08 12:30:39 +0200664 def get_network(self, net_id):
665 '''Obtain details of network from VIM
666 Returns the network information from a network id'''
667 self.logger.debug(" Getting tenant network %s from VIM", net_id)
tierno7edb6752016-03-21 17:37:52 +0100668 filter_dict={"id": net_id}
tiernoae4a8d12016-07-08 12:30:39 +0200669 net_list = self.get_network_list(filter_dict)
tierno7edb6752016-03-21 17:37:52 +0100670 if len(net_list)==0:
tiernoae4a8d12016-07-08 12:30:39 +0200671 raise vimconn.vimconnNotFoundException("Network '{}' not found".format(net_id))
tierno7edb6752016-03-21 17:37:52 +0100672 elif len(net_list)>1:
tiernoae4a8d12016-07-08 12:30:39 +0200673 raise vimconn.vimconnConflictException("Found more than one network with this criteria")
tierno7edb6752016-03-21 17:37:52 +0100674 net = net_list[0]
675 subnets=[]
676 for subnet_id in net.get("subnets", () ):
677 try:
678 subnet = self.neutron.show_subnet(subnet_id)
679 except Exception as e:
tiernoae4a8d12016-07-08 12:30:39 +0200680 self.logger.error("osconnector.get_network(): Error getting subnet %s %s" % (net_id, str(e)))
681 subnet = {"id": subnet_id, "fault": str(e)}
tierno7edb6752016-03-21 17:37:52 +0100682 subnets.append(subnet)
683 net["subnets"] = subnets
Pablo Montes Moreno51e553b2017-03-23 16:39:12 +0100684 net["encapsulation"] = net.get('provider:network_type')
Anderson Bravalheri0fb70282018-12-16 19:28:37 +0000685 net["encapsulation_type"] = net.get('provider:network_type')
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +0100686 net["segmentation_id"] = net.get('provider:segmentation_id')
Anderson Bravalheri0fb70282018-12-16 19:28:37 +0000687 net["encapsulation_id"] = net.get('provider:segmentation_id')
tiernoae4a8d12016-07-08 12:30:39 +0200688 return net
tierno7edb6752016-03-21 17:37:52 +0100689
garciadeblasebd66722019-01-31 16:01:31 +0000690 def delete_network(self, net_id, created_items=None):
691 """
692 Removes a tenant network from VIM and its associated elements
693 :param net_id: VIM identifier of the network, provided by method new_network
694 :param created_items: dictionary with extra items to be deleted. provided by method new_network
695 Returns the network identifier or raises an exception upon error or when network is not found
696 """
tiernoae4a8d12016-07-08 12:30:39 +0200697 self.logger.debug("Deleting network '%s' from VIM", net_id)
garciadeblasebd66722019-01-31 16:01:31 +0000698 if created_items == None:
699 created_items = {}
tierno7edb6752016-03-21 17:37:52 +0100700 try:
701 self._reload_connection()
garciadeblasebd66722019-01-31 16:01:31 +0000702 #delete l2gw connections (if any) before deleting the network
703 for k, v in created_items.items():
704 if not v: # skip already deleted
705 continue
706 try:
707 k_item, _, k_id = k.partition(":")
708 if k_item == "l2gwconn":
709 self.neutron.delete_l2_gateway_connection(k_id)
710 except Exception as e:
711 self.logger.error("Error deleting l2 gateway connection: {}: {}".format(type(e).__name__, e))
tierno7edb6752016-03-21 17:37:52 +0100712 #delete VM ports attached to this networks before the network
713 ports = self.neutron.list_ports(network_id=net_id)
714 for p in ports['ports']:
715 try:
716 self.neutron.delete_port(p["id"])
717 except Exception as e:
tiernoae4a8d12016-07-08 12:30:39 +0200718 self.logger.error("Error deleting port %s: %s", p["id"], str(e))
tierno7edb6752016-03-21 17:37:52 +0100719 self.neutron.delete_network(net_id)
tiernoae4a8d12016-07-08 12:30:39 +0200720 return net_id
721 except (neExceptions.ConnectionFailed, neExceptions.NetworkNotFoundClient, neExceptions.NeutronException,
tierno8e995ce2016-09-22 08:13:00 +0000722 ksExceptions.ClientException, neExceptions.NeutronException, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200723 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100724
tiernoae4a8d12016-07-08 12:30:39 +0200725 def refresh_nets_status(self, net_list):
726 '''Get the status of the networks
727 Params: the list of network identifiers
728 Returns a dictionary with:
729 net_id: #VIM id of this network
730 status: #Mandatory. Text with one of:
731 # DELETED (not found at vim)
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100732 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
tiernoae4a8d12016-07-08 12:30:39 +0200733 # OTHER (Vim reported other status not understood)
734 # ERROR (VIM indicates an ERROR status)
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100735 # ACTIVE, INACTIVE, DOWN (admin down),
tiernoae4a8d12016-07-08 12:30:39 +0200736 # BUILD (on building process)
737 #
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100738 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
tiernoae4a8d12016-07-08 12:30:39 +0200739 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
740
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +0000741 '''
tiernoae4a8d12016-07-08 12:30:39 +0200742 net_dict={}
743 for net_id in net_list:
744 net = {}
745 try:
746 net_vim = self.get_network(net_id)
747 if net_vim['status'] in netStatus2manoFormat:
748 net["status"] = netStatus2manoFormat[ net_vim['status'] ]
749 else:
750 net["status"] = "OTHER"
751 net["error_msg"] = "VIM status reported " + net_vim['status']
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +0000752
tierno8e995ce2016-09-22 08:13:00 +0000753 if net['status'] == "ACTIVE" and not net_vim['admin_state_up']:
tiernoae4a8d12016-07-08 12:30:39 +0200754 net['status'] = 'DOWN'
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100755
756 net['vim_info'] = self.serialize(net_vim)
757
tiernoae4a8d12016-07-08 12:30:39 +0200758 if net_vim.get('fault'): #TODO
759 net['error_msg'] = str(net_vim['fault'])
760 except vimconn.vimconnNotFoundException as e:
761 self.logger.error("Exception getting net status: %s", str(e))
762 net['status'] = "DELETED"
763 net['error_msg'] = str(e)
764 except vimconn.vimconnException as e:
765 self.logger.error("Exception getting net status: %s", str(e))
766 net['status'] = "VIM_ERROR"
767 net['error_msg'] = str(e)
768 net_dict[net_id] = net
769 return net_dict
770
771 def get_flavor(self, flavor_id):
772 '''Obtain flavor details from the VIM. Returns the flavor dict details'''
773 self.logger.debug("Getting flavor '%s'", flavor_id)
tierno7edb6752016-03-21 17:37:52 +0100774 try:
775 self._reload_connection()
776 flavor = self.nova.flavors.find(id=flavor_id)
777 #TODO parse input and translate to VIM format (openmano_schemas.new_vminstance_response_schema)
tiernoae4a8d12016-07-08 12:30:39 +0200778 return flavor.to_dict()
tierno8e995ce2016-09-22 08:13:00 +0000779 except (nvExceptions.NotFound, nvExceptions.ClientException, ksExceptions.ClientException, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200780 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100781
tiernocf157a82017-01-30 14:07:06 +0100782 def get_flavor_id_from_data(self, flavor_dict):
783 """Obtain flavor id that match the flavor description
784 Returns the flavor_id or raises a vimconnNotFoundException
tiernoe26fc7a2017-05-30 14:43:03 +0200785 flavor_dict: contains the required ram, vcpus, disk
786 If 'use_existing_flavors' is set to True at config, the closer flavor that provides same or more ram, vcpus
787 and disk is returned. Otherwise a flavor with exactly same ram, vcpus and disk is returned or a
788 vimconnNotFoundException is raised
tiernocf157a82017-01-30 14:07:06 +0100789 """
tiernoe26fc7a2017-05-30 14:43:03 +0200790 exact_match = False if self.config.get('use_existing_flavors') else True
tiernocf157a82017-01-30 14:07:06 +0100791 try:
792 self._reload_connection()
tiernoe26fc7a2017-05-30 14:43:03 +0200793 flavor_candidate_id = None
794 flavor_candidate_data = (10000, 10000, 10000)
795 flavor_target = (flavor_dict["ram"], flavor_dict["vcpus"], flavor_dict["disk"])
796 # numa=None
797 numas = flavor_dict.get("extended", {}).get("numas")
tiernocf157a82017-01-30 14:07:06 +0100798 if numas:
799 #TODO
800 raise vimconn.vimconnNotFoundException("Flavor with EPA still not implemted")
801 # if len(numas) > 1:
802 # raise vimconn.vimconnNotFoundException("Cannot find any flavor with more than one numa")
803 # numa=numas[0]
804 # numas = extended.get("numas")
805 for flavor in self.nova.flavors.list():
806 epa = flavor.get_keys()
807 if epa:
808 continue
tiernoe26fc7a2017-05-30 14:43:03 +0200809 # TODO
810 flavor_data = (flavor.ram, flavor.vcpus, flavor.disk)
811 if flavor_data == flavor_target:
812 return flavor.id
813 elif not exact_match and flavor_target < flavor_data < flavor_candidate_data:
814 flavor_candidate_id = flavor.id
815 flavor_candidate_data = flavor_data
816 if not exact_match and flavor_candidate_id:
817 return flavor_candidate_id
tiernocf157a82017-01-30 14:07:06 +0100818 raise vimconn.vimconnNotFoundException("Cannot find any flavor matching '{}'".format(str(flavor_dict)))
819 except (nvExceptions.NotFound, nvExceptions.ClientException, ksExceptions.ClientException, ConnectionError) as e:
820 self._format_exception(e)
821
tiernoae4a8d12016-07-08 12:30:39 +0200822 def new_flavor(self, flavor_data, change_name_if_used=True):
tierno7edb6752016-03-21 17:37:52 +0100823 '''Adds a tenant flavor to openstack VIM
tiernoae4a8d12016-07-08 12:30:39 +0200824 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 +0100825 Returns the flavor identifier
826 '''
tiernoae4a8d12016-07-08 12:30:39 +0200827 self.logger.debug("Adding flavor '%s'", str(flavor_data))
tierno7edb6752016-03-21 17:37:52 +0100828 retry=0
tiernoae4a8d12016-07-08 12:30:39 +0200829 max_retries=3
tierno7edb6752016-03-21 17:37:52 +0100830 name_suffix = 0
anwarsc76a3ee2018-10-04 14:05:32 +0530831 try:
832 name=flavor_data['name']
833 while retry<max_retries:
834 retry+=1
835 try:
836 self._reload_connection()
837 if change_name_if_used:
838 #get used names
839 fl_names=[]
840 fl=self.nova.flavors.list()
841 for f in fl:
842 fl_names.append(f.name)
843 while name in fl_names:
844 name_suffix += 1
845 name = flavor_data['name']+"-" + str(name_suffix)
kate721d79b2017-06-24 04:21:38 -0700846
anwarsc76a3ee2018-10-04 14:05:32 +0530847 ram = flavor_data.get('ram',64)
848 vcpus = flavor_data.get('vcpus',1)
849 numa_properties=None
tierno7edb6752016-03-21 17:37:52 +0100850
anwarsc76a3ee2018-10-04 14:05:32 +0530851 extended = flavor_data.get("extended")
852 if extended:
853 numas=extended.get("numas")
854 if numas:
855 numa_nodes = len(numas)
856 if numa_nodes > 1:
857 return -1, "Can not add flavor with more than one numa"
858 numa_properties = {"hw:numa_nodes":str(numa_nodes)}
859 numa_properties["hw:mem_page_size"] = "large"
860 numa_properties["hw:cpu_policy"] = "dedicated"
861 numa_properties["hw:numa_mempolicy"] = "strict"
862 if self.vim_type == "VIO":
863 numa_properties["vmware:extra_config"] = '{"numa.nodeAffinity":"0"}'
864 numa_properties["vmware:latency_sensitivity_level"] = "high"
865 for numa in numas:
866 #overwrite ram and vcpus
867 #check if key 'memory' is present in numa else use ram value at flavor
868 if 'memory' in numa:
869 ram = numa['memory']*1024
870 #See for reference: https://specs.openstack.org/openstack/nova-specs/specs/mitaka/implemented/virt-driver-cpu-thread-pinning.html
871 if 'paired-threads' in numa:
872 vcpus = numa['paired-threads']*2
873 #cpu_thread_policy "require" implies that the compute node must have an STM architecture
874 numa_properties["hw:cpu_thread_policy"] = "require"
875 numa_properties["hw:cpu_policy"] = "dedicated"
876 elif 'cores' in numa:
877 vcpus = numa['cores']
878 # cpu_thread_policy "prefer" implies that the host must not have an SMT architecture, or a non-SMT architecture will be emulated
879 numa_properties["hw:cpu_thread_policy"] = "isolate"
880 numa_properties["hw:cpu_policy"] = "dedicated"
881 elif 'threads' in numa:
882 vcpus = numa['threads']
883 # cpu_thread_policy "prefer" implies that the host may or may not have an SMT architecture
884 numa_properties["hw:cpu_thread_policy"] = "prefer"
885 numa_properties["hw:cpu_policy"] = "dedicated"
886 # for interface in numa.get("interfaces",() ):
887 # if interface["dedicated"]=="yes":
888 # raise vimconn.vimconnException("Passthrough interfaces are not supported for the openstack connector", http_code=vimconn.HTTP_Service_Unavailable)
889 # #TODO, add the key 'pci_passthrough:alias"="<label at config>:<number ifaces>"' when a way to connect it is available
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +0000890
anwarsc76a3ee2018-10-04 14:05:32 +0530891 #create flavor
892 new_flavor=self.nova.flavors.create(name,
893 ram,
894 vcpus,
895 flavor_data.get('disk',0),
896 is_public=flavor_data.get('is_public', True)
897 )
898 #add metadata
899 if numa_properties:
900 new_flavor.set_keys(numa_properties)
901 return new_flavor.id
902 except nvExceptions.Conflict as e:
903 if change_name_if_used and retry < max_retries:
904 continue
905 self._format_exception(e)
906 #except nvExceptions.BadRequest as e:
907 except (ksExceptions.ClientException, nvExceptions.ClientException, ConnectionError, KeyError) as e:
908 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100909
tiernoae4a8d12016-07-08 12:30:39 +0200910 def delete_flavor(self,flavor_id):
911 '''Deletes a tenant flavor from openstack VIM. Returns the old flavor_id
tierno7edb6752016-03-21 17:37:52 +0100912 '''
tiernoae4a8d12016-07-08 12:30:39 +0200913 try:
914 self._reload_connection()
915 self.nova.flavors.delete(flavor_id)
916 return flavor_id
917 #except nvExceptions.BadRequest as e:
tierno8e995ce2016-09-22 08:13:00 +0000918 except (nvExceptions.NotFound, ksExceptions.ClientException, nvExceptions.ClientException, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200919 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100920
tiernoae4a8d12016-07-08 12:30:39 +0200921 def new_image(self,image_dict):
tierno7edb6752016-03-21 17:37:52 +0100922 '''
tiernoae4a8d12016-07-08 12:30:39 +0200923 Adds a tenant image to VIM. imge_dict is a dictionary with:
924 name: name
925 disk_format: qcow2, vhd, vmdk, raw (by default), ...
926 location: path or URI
927 public: "yes" or "no"
928 metadata: metadata of the image
929 Returns the image_id
tierno7edb6752016-03-21 17:37:52 +0100930 '''
tiernoae4a8d12016-07-08 12:30:39 +0200931 retry=0
932 max_retries=3
933 while retry<max_retries:
tierno7edb6752016-03-21 17:37:52 +0100934 retry+=1
935 try:
936 self._reload_connection()
937 #determine format http://docs.openstack.org/developer/glance/formats.html
938 if "disk_format" in image_dict:
939 disk_format=image_dict["disk_format"]
garciadeblas14480452017-01-10 13:08:07 +0100940 else: #autodiscover based on extension
tierno1beea862018-07-11 15:47:37 +0200941 if image_dict['location'].endswith(".qcow2"):
tierno7edb6752016-03-21 17:37:52 +0100942 disk_format="qcow2"
tierno1beea862018-07-11 15:47:37 +0200943 elif image_dict['location'].endswith(".vhd"):
tierno7edb6752016-03-21 17:37:52 +0100944 disk_format="vhd"
tierno1beea862018-07-11 15:47:37 +0200945 elif image_dict['location'].endswith(".vmdk"):
tierno7edb6752016-03-21 17:37:52 +0100946 disk_format="vmdk"
tierno1beea862018-07-11 15:47:37 +0200947 elif image_dict['location'].endswith(".vdi"):
tierno7edb6752016-03-21 17:37:52 +0100948 disk_format="vdi"
tierno1beea862018-07-11 15:47:37 +0200949 elif image_dict['location'].endswith(".iso"):
tierno7edb6752016-03-21 17:37:52 +0100950 disk_format="iso"
tierno1beea862018-07-11 15:47:37 +0200951 elif image_dict['location'].endswith(".aki"):
tierno7edb6752016-03-21 17:37:52 +0100952 disk_format="aki"
tierno1beea862018-07-11 15:47:37 +0200953 elif image_dict['location'].endswith(".ari"):
tierno7edb6752016-03-21 17:37:52 +0100954 disk_format="ari"
tierno1beea862018-07-11 15:47:37 +0200955 elif image_dict['location'].endswith(".ami"):
tierno7edb6752016-03-21 17:37:52 +0100956 disk_format="ami"
957 else:
958 disk_format="raw"
tiernoae4a8d12016-07-08 12:30:39 +0200959 self.logger.debug("new_image: '%s' loading from '%s'", image_dict['name'], image_dict['location'])
shashankjain3c83a212018-10-04 13:05:46 +0530960 if self.vim_type == "VIO":
961 container_format = "bare"
962 if 'container_format' in image_dict:
963 container_format = image_dict['container_format']
964 new_image = self.glance.images.create(name=image_dict['name'], container_format=container_format,
965 disk_format=disk_format)
966 else:
967 new_image = self.glance.images.create(name=image_dict['name'])
tierno1beea862018-07-11 15:47:37 +0200968 if image_dict['location'].startswith("http"):
969 # TODO there is not a method to direct download. It must be downloaded locally with requests
970 raise vimconn.vimconnNotImplemented("Cannot create image from URL")
tierno7edb6752016-03-21 17:37:52 +0100971 else: #local path
972 with open(image_dict['location']) as fimage:
tierno1beea862018-07-11 15:47:37 +0200973 self.glance.images.upload(new_image.id, fimage)
974 #new_image = self.glancev1.images.create(name=image_dict['name'], is_public=image_dict.get('public',"yes")=="yes",
975 # container_format="bare", data=fimage, disk_format=disk_format)
tierno7edb6752016-03-21 17:37:52 +0100976 metadata_to_load = image_dict.get('metadata')
shashankjain3c83a212018-10-04 13:05:46 +0530977 # TODO location is a reserved word for current openstack versions. fixed for VIO please check for openstack
978 if self.vim_type == "VIO":
979 metadata_to_load['upload_location'] = image_dict['location']
980 else:
981 metadata_to_load['location'] = image_dict['location']
tierno1beea862018-07-11 15:47:37 +0200982 self.glance.images.update(new_image.id, **metadata_to_load)
tiernoae4a8d12016-07-08 12:30:39 +0200983 return new_image.id
984 except (nvExceptions.Conflict, ksExceptions.ClientException, nvExceptions.ClientException) as e:
985 self._format_exception(e)
tierno8e995ce2016-09-22 08:13:00 +0000986 except (HTTPException, gl1Exceptions.HTTPException, gl1Exceptions.CommunicationError, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200987 if retry==max_retries:
988 continue
989 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100990 except IOError as e: #can not open the file
tiernoae4a8d12016-07-08 12:30:39 +0200991 raise vimconn.vimconnConnectionException(type(e).__name__ + ": " + str(e)+ " for " + image_dict['location'],
992 http_code=vimconn.HTTP_Bad_Request)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +0000993
tiernoae4a8d12016-07-08 12:30:39 +0200994 def delete_image(self, image_id):
995 '''Deletes a tenant image from openstack VIM. Returns the old id
tierno7edb6752016-03-21 17:37:52 +0100996 '''
tiernoae4a8d12016-07-08 12:30:39 +0200997 try:
998 self._reload_connection()
tierno1beea862018-07-11 15:47:37 +0200999 self.glance.images.delete(image_id)
tiernoae4a8d12016-07-08 12:30:39 +02001000 return image_id
shashankjain3c83a212018-10-04 13:05:46 +05301001 except (nvExceptions.NotFound, ksExceptions.ClientException, nvExceptions.ClientException, gl1Exceptions.CommunicationError, gl1Exceptions.HTTPNotFound, ConnectionError) as e: #TODO remove
tiernoae4a8d12016-07-08 12:30:39 +02001002 self._format_exception(e)
1003
1004 def get_image_id_from_path(self, path):
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001005 '''Get the image id from image path in the VIM database. Returns the image_id'''
tiernoae4a8d12016-07-08 12:30:39 +02001006 try:
1007 self._reload_connection()
tierno1beea862018-07-11 15:47:37 +02001008 images = self.glance.images.list()
tiernoae4a8d12016-07-08 12:30:39 +02001009 for image in images:
1010 if image.metadata.get("location")==path:
1011 return image.id
1012 raise vimconn.vimconnNotFoundException("image with location '{}' not found".format( path))
tierno8e995ce2016-09-22 08:13:00 +00001013 except (ksExceptions.ClientException, nvExceptions.ClientException, gl1Exceptions.CommunicationError, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +02001014 self._format_exception(e)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001015
garciadeblasb69fa9f2016-09-28 12:04:10 +02001016 def get_image_list(self, filter_dict={}):
1017 '''Obtain tenant images from VIM
1018 Filter_dict can be:
1019 id: image id
1020 name: image name
1021 checksum: image checksum
1022 Returns the image list of dictionaries:
1023 [{<the fields at Filter_dict plus some VIM specific>}, ...]
1024 List can be empty
1025 '''
1026 self.logger.debug("Getting image list from VIM filter: '%s'", str(filter_dict))
1027 try:
1028 self._reload_connection()
tierno69b590e2018-03-13 18:52:23 +01001029 filter_dict_os = filter_dict.copy()
garciadeblasb69fa9f2016-09-28 12:04:10 +02001030 #First we filter by the available filter fields: name, id. The others are removed.
tierno1beea862018-07-11 15:47:37 +02001031 image_list = self.glance.images.list()
garciadeblasb69fa9f2016-09-28 12:04:10 +02001032 filtered_list = []
1033 for image in image_list:
tierno3cb8dc32017-10-24 18:13:19 +02001034 try:
tierno1beea862018-07-11 15:47:37 +02001035 if filter_dict.get("name") and image["name"] != filter_dict["name"]:
1036 continue
1037 if filter_dict.get("id") and image["id"] != filter_dict["id"]:
1038 continue
1039 if filter_dict.get("checksum") and image["checksum"] != filter_dict["checksum"]:
1040 continue
1041
1042 filtered_list.append(image.copy())
tierno3cb8dc32017-10-24 18:13:19 +02001043 except gl1Exceptions.HTTPNotFound:
1044 pass
garciadeblasb69fa9f2016-09-28 12:04:10 +02001045 return filtered_list
1046 except (ksExceptions.ClientException, nvExceptions.ClientException, gl1Exceptions.CommunicationError, ConnectionError) as e:
1047 self._format_exception(e)
1048
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +02001049 def __wait_for_vm(self, vm_id, status):
1050 """wait until vm is in the desired status and return True.
1051 If the VM gets in ERROR status, return false.
1052 If the timeout is reached generate an exception"""
1053 elapsed_time = 0
1054 while elapsed_time < server_timeout:
1055 vm_status = self.nova.servers.get(vm_id).status
1056 if vm_status == status:
1057 return True
1058 if vm_status == 'ERROR':
1059 return False
tierno1df468d2018-07-06 14:25:16 +02001060 time.sleep(5)
1061 elapsed_time += 5
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +02001062
1063 # if we exceeded the timeout rollback
1064 if elapsed_time >= server_timeout:
1065 raise vimconn.vimconnException('Timeout waiting for instance ' + vm_id + ' to get ' + status,
1066 http_code=vimconn.HTTP_Request_Timeout)
1067
mirabal29356312017-07-27 12:21:22 +02001068 def _get_openstack_availablity_zones(self):
1069 """
1070 Get from openstack availability zones available
1071 :return:
1072 """
1073 try:
1074 openstack_availability_zone = self.nova.availability_zones.list()
1075 openstack_availability_zone = [str(zone.zoneName) for zone in openstack_availability_zone
1076 if zone.zoneName != 'internal']
1077 return openstack_availability_zone
1078 except Exception as e:
1079 return None
1080
1081 def _set_availablity_zones(self):
1082 """
1083 Set vim availablity zone
1084 :return:
1085 """
1086
1087 if 'availability_zone' in self.config:
1088 vim_availability_zones = self.config.get('availability_zone')
1089 if isinstance(vim_availability_zones, str):
1090 self.availability_zone = [vim_availability_zones]
1091 elif isinstance(vim_availability_zones, list):
1092 self.availability_zone = vim_availability_zones
1093 else:
1094 self.availability_zone = self._get_openstack_availablity_zones()
1095
tierno5a3273c2017-08-29 11:43:46 +02001096 def _get_vm_availability_zone(self, availability_zone_index, availability_zone_list):
mirabal29356312017-07-27 12:21:22 +02001097 """
tierno5a3273c2017-08-29 11:43:46 +02001098 Return thge availability zone to be used by the created VM.
1099 :return: The VIM availability zone to be used or None
mirabal29356312017-07-27 12:21:22 +02001100 """
tierno5a3273c2017-08-29 11:43:46 +02001101 if availability_zone_index is None:
1102 if not self.config.get('availability_zone'):
1103 return None
1104 elif isinstance(self.config.get('availability_zone'), str):
1105 return self.config['availability_zone']
1106 else:
1107 # TODO consider using a different parameter at config for default AV and AV list match
1108 return self.config['availability_zone'][0]
mirabal29356312017-07-27 12:21:22 +02001109
tierno5a3273c2017-08-29 11:43:46 +02001110 vim_availability_zones = self.availability_zone
1111 # check if VIM offer enough availability zones describe in the VNFD
1112 if vim_availability_zones and len(availability_zone_list) <= len(vim_availability_zones):
1113 # check if all the names of NFV AV match VIM AV names
1114 match_by_index = False
1115 for av in availability_zone_list:
1116 if av not in vim_availability_zones:
1117 match_by_index = True
1118 break
1119 if match_by_index:
1120 return vim_availability_zones[availability_zone_index]
1121 else:
1122 return availability_zone_list[availability_zone_index]
mirabal29356312017-07-27 12:21:22 +02001123 else:
tierno5a3273c2017-08-29 11:43:46 +02001124 raise vimconn.vimconnConflictException("No enough availability zones at VIM for this deployment")
mirabal29356312017-07-27 12:21:22 +02001125
tierno5a3273c2017-08-29 11:43:46 +02001126 def new_vminstance(self, name, description, start, image_id, flavor_id, net_list, cloud_config=None, disk_list=None,
1127 availability_zone_index=None, availability_zone_list=None):
tierno98e909c2017-10-14 13:27:03 +02001128 """Adds a VM instance to VIM
tierno7edb6752016-03-21 17:37:52 +01001129 Params:
1130 start: indicates if VM must start or boot in pause mode. Ignored
1131 image_id,flavor_id: iamge and flavor uuid
1132 net_list: list of interfaces, each one is a dictionary with:
1133 name:
1134 net_id: network uuid to connect
1135 vpci: virtual vcpi to assign, ignored because openstack lack #TODO
1136 model: interface model, ignored #TODO
1137 mac_address: used for SR-IOV ifaces #TODO for other types
1138 use: 'data', 'bridge', 'mgmt'
tierno66eba6e2017-11-10 17:09:18 +01001139 type: 'virtual', 'PCI-PASSTHROUGH'('PF'), 'SR-IOV'('VF'), 'VFnotShared'
tierno7edb6752016-03-21 17:37:52 +01001140 vim_id: filled/added by this function
ahmadsaf853d452016-12-22 11:33:47 +05001141 floating_ip: True/False (or it can be None)
tierno41a69812018-02-16 14:34:33 +01001142 'cloud_config': (optional) dictionary with:
1143 'key-pairs': (optional) list of strings with the public key to be inserted to the default user
1144 'users': (optional) list of users to be inserted, each item is a dict with:
1145 'name': (mandatory) user name,
1146 'key-pairs': (optional) list of strings with the public key to be inserted to the user
1147 'user-data': (optional) string is a text script to be passed directly to cloud-init
1148 'config-files': (optional). List of files to be transferred. Each item is a dict with:
1149 'dest': (mandatory) string with the destination absolute path
1150 'encoding': (optional, by default text). Can be one of:
1151 'b64', 'base64', 'gz', 'gz+b64', 'gz+base64', 'gzip+b64', 'gzip+base64'
1152 'content' (mandatory): string with the content of the file
1153 'permissions': (optional) string with file permissions, typically octal notation '0644'
1154 'owner': (optional) file owner, string with the format 'owner:group'
1155 'boot-data-drive': boolean to indicate if user-data must be passed using a boot drive (hard disk)
mirabal29356312017-07-27 12:21:22 +02001156 'disk_list': (optional) list with additional disks to the VM. Each item is a dict with:
1157 'image_id': (optional). VIM id of an existing image. If not provided an empty disk must be mounted
1158 'size': (mandatory) string with the size of the disk in GB
tierno1df468d2018-07-06 14:25:16 +02001159 'vim_id' (optional) should use this existing volume id
tierno5a3273c2017-08-29 11:43:46 +02001160 availability_zone_index: Index of availability_zone_list to use for this this VM. None if not AV required
1161 availability_zone_list: list of availability zones given by user in the VNFD descriptor. Ignore if
1162 availability_zone_index is None
tierno7edb6752016-03-21 17:37:52 +01001163 #TODO ip, security groups
tierno98e909c2017-10-14 13:27:03 +02001164 Returns a tuple with the instance identifier and created_items or raises an exception on error
1165 created_items can be None or a dictionary where this method can include key-values that will be passed to
1166 the method delete_vminstance and action_vminstance. Can be used to store created ports, volumes, etc.
1167 Format is vimconnector dependent, but do not use nested dictionaries and a value of None should be the same
1168 as not present.
1169 """
tiernofa51c202017-01-27 14:58:17 +01001170 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 +01001171 try:
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +02001172 server = None
tierno98e909c2017-10-14 13:27:03 +02001173 created_items = {}
tiernob0b9dab2017-10-14 14:25:20 +02001174 # metadata = {}
tierno98e909c2017-10-14 13:27:03 +02001175 net_list_vim = []
1176 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 +02001177 no_secured_ports = [] # List of port-is with port-security disabled
tierno7edb6752016-03-21 17:37:52 +01001178 self._reload_connection()
tiernob0b9dab2017-10-14 14:25:20 +02001179 # metadata_vpci = {} # For a specific neutron plugin
tiernob84cbdc2017-07-07 14:30:30 +02001180 block_device_mapping = None
tiernoa05b65a2019-02-01 12:30:27 +00001181
tierno7edb6752016-03-21 17:37:52 +01001182 for net in net_list:
tierno98e909c2017-10-14 13:27:03 +02001183 if not net.get("net_id"): # skip non connected iface
tierno7edb6752016-03-21 17:37:52 +01001184 continue
Pablo Montes Moreno3be0b2a2017-03-30 13:22:15 +02001185
tiernoa05b65a2019-02-01 12:30:27 +00001186 port_dict = {
Pablo Montes Moreno3be0b2a2017-03-30 13:22:15 +02001187 "network_id": net["net_id"],
1188 "name": net.get("name"),
1189 "admin_state_up": True
1190 }
tiernoa05b65a2019-02-01 12:30:27 +00001191 if self.config.get("security_groups") and net.get("port_security") is not False and \
1192 not self.config.get("no_port_security_extension"):
1193 if not self.security_groups_id:
1194 self._get_ids_from_name()
1195 port_dict["security_groups"] = self.security_groups_id
1196
Pablo Montes Moreno3be0b2a2017-03-30 13:22:15 +02001197 if net["type"]=="virtual":
tiernob0b9dab2017-10-14 14:25:20 +02001198 pass
1199 # if "vpci" in net:
1200 # metadata_vpci[ net["net_id"] ] = [[ net["vpci"], "" ]]
tierno66eba6e2017-11-10 17:09:18 +01001201 elif net["type"] == "VF" or net["type"] == "SR-IOV": # for VF
tiernob0b9dab2017-10-14 14:25:20 +02001202 # if "vpci" in net:
1203 # if "VF" not in metadata_vpci:
1204 # metadata_vpci["VF"]=[]
1205 # metadata_vpci["VF"].append([ net["vpci"], "" ])
Pablo Montes Moreno3be0b2a2017-03-30 13:22:15 +02001206 port_dict["binding:vnic_type"]="direct"
tiernob0b9dab2017-10-14 14:25:20 +02001207 # VIO specific Changes
kate721d79b2017-06-24 04:21:38 -07001208 if self.vim_type == "VIO":
tiernob0b9dab2017-10-14 14:25:20 +02001209 # Need to create port with port_security_enabled = False and no-security-groups
kate721d79b2017-06-24 04:21:38 -07001210 port_dict["port_security_enabled"]=False
1211 port_dict["provider_security_groups"]=[]
1212 port_dict["security_groups"]=[]
tierno66eba6e2017-11-10 17:09:18 +01001213 else: # For PT PCI-PASSTHROUGH
tiernob0b9dab2017-10-14 14:25:20 +02001214 # VIO specific Changes
1215 # Current VIO release does not support port with type 'direct-physical'
1216 # So no need to create virtual port in case of PCI-device.
1217 # Will update port_dict code when support gets added in next VIO release
kate721d79b2017-06-24 04:21:38 -07001218 if self.vim_type == "VIO":
tiernob0b9dab2017-10-14 14:25:20 +02001219 raise vimconn.vimconnNotSupportedException(
1220 "Current VIO release does not support full passthrough (PT)")
1221 # if "vpci" in net:
1222 # if "PF" not in metadata_vpci:
1223 # metadata_vpci["PF"]=[]
1224 # metadata_vpci["PF"].append([ net["vpci"], "" ])
Pablo Montes Moreno3be0b2a2017-03-30 13:22:15 +02001225 port_dict["binding:vnic_type"]="direct-physical"
1226 if not port_dict["name"]:
1227 port_dict["name"]=name
1228 if net.get("mac_address"):
1229 port_dict["mac_address"]=net["mac_address"]
tierno41a69812018-02-16 14:34:33 +01001230 if net.get("ip_address"):
1231 port_dict["fixed_ips"] = [{'ip_address': net["ip_address"]}]
1232 # TODO add 'subnet_id': <subnet_id>
Pablo Montes Moreno3be0b2a2017-03-30 13:22:15 +02001233 new_port = self.neutron.create_port({"port": port_dict })
tierno00e3df72017-11-29 17:20:13 +01001234 created_items["port:" + str(new_port["port"]["id"])] = True
Pablo Montes Moreno3be0b2a2017-03-30 13:22:15 +02001235 net["mac_adress"] = new_port["port"]["mac_address"]
1236 net["vim_id"] = new_port["port"]["id"]
tiernob84cbdc2017-07-07 14:30:30 +02001237 # if try to use a network without subnetwork, it will return a emtpy list
1238 fixed_ips = new_port["port"].get("fixed_ips")
1239 if fixed_ips:
1240 net["ip"] = fixed_ips[0].get("ip_address")
1241 else:
1242 net["ip"] = None
montesmoreno994a29d2017-08-22 11:23:06 +02001243
1244 port = {"port-id": new_port["port"]["id"]}
1245 if float(self.nova.api_version.get_string()) >= 2.32:
1246 port["tag"] = new_port["port"]["name"]
1247 net_list_vim.append(port)
Pablo Montes Moreno3be0b2a2017-03-30 13:22:15 +02001248
ahmadsaf853d452016-12-22 11:33:47 +05001249 if net.get('floating_ip', False):
tiernof8383b82017-01-18 15:49:48 +01001250 net['exit_on_floating_ip_error'] = True
ahmadsaf853d452016-12-22 11:33:47 +05001251 external_network.append(net)
tiernof8383b82017-01-18 15:49:48 +01001252 elif net['use'] == 'mgmt' and self.config.get('use_floating_ip'):
1253 net['exit_on_floating_ip_error'] = False
1254 external_network.append(net)
tierno326fd5e2018-02-22 11:58:59 +01001255 net['floating_ip'] = self.config.get('use_floating_ip')
tiernof8383b82017-01-18 15:49:48 +01001256
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +02001257 # If port security is disabled when the port has not yet been attached to the VM, then all vm traffic is dropped.
1258 # As a workaround we wait until the VM is active and then disable the port-security
tierno4d1ce222018-04-06 10:41:06 +02001259 if net.get("port_security") == False and not self.config.get("no_port_security_extension"):
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +02001260 no_secured_ports.append(new_port["port"]["id"])
1261
tiernob0b9dab2017-10-14 14:25:20 +02001262 # if metadata_vpci:
1263 # metadata = {"pci_assignement": json.dumps(metadata_vpci)}
1264 # if len(metadata["pci_assignement"]) >255:
1265 # #limit the metadata size
1266 # #metadata["pci_assignement"] = metadata["pci_assignement"][0:255]
1267 # self.logger.warn("Metadata deleted since it exceeds the expected length (255) ")
1268 # metadata = {}
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001269
tiernob0b9dab2017-10-14 14:25:20 +02001270 self.logger.debug("name '%s' image_id '%s'flavor_id '%s' net_list_vim '%s' description '%s'",
1271 name, image_id, flavor_id, str(net_list_vim), description)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001272
tierno98e909c2017-10-14 13:27:03 +02001273 # cloud config
tierno0a1437e2017-10-02 00:17:43 +02001274 config_drive, userdata = self._create_user_data(cloud_config)
montesmoreno0c8def02016-12-22 12:16:23 +00001275
tierno98e909c2017-10-14 13:27:03 +02001276 # Create additional volumes in case these are present in disk_list
montesmoreno0c8def02016-12-22 12:16:23 +00001277 base_disk_index = ord('b')
tierno1df468d2018-07-06 14:25:16 +02001278 if disk_list:
tiernob84cbdc2017-07-07 14:30:30 +02001279 block_device_mapping = {}
montesmoreno0c8def02016-12-22 12:16:23 +00001280 for disk in disk_list:
tierno1df468d2018-07-06 14:25:16 +02001281 if disk.get('vim_id'):
1282 block_device_mapping['_vd' + chr(base_disk_index)] = disk['vim_id']
montesmoreno0c8def02016-12-22 12:16:23 +00001283 else:
tierno1df468d2018-07-06 14:25:16 +02001284 if 'image_id' in disk:
1285 volume = self.cinder.volumes.create(size=disk['size'], name=name + '_vd' +
1286 chr(base_disk_index), imageRef=disk['image_id'])
1287 else:
1288 volume = self.cinder.volumes.create(size=disk['size'], name=name + '_vd' +
1289 chr(base_disk_index))
1290 created_items["volume:" + str(volume.id)] = True
1291 block_device_mapping['_vd' + chr(base_disk_index)] = volume.id
montesmoreno0c8def02016-12-22 12:16:23 +00001292 base_disk_index += 1
1293
tierno1df468d2018-07-06 14:25:16 +02001294 # Wait until created volumes are with status available
montesmoreno0c8def02016-12-22 12:16:23 +00001295 elapsed_time = 0
tierno1df468d2018-07-06 14:25:16 +02001296 while elapsed_time < volume_timeout:
1297 for created_item in created_items:
1298 v, _, volume_id = created_item.partition(":")
1299 if v == 'volume':
1300 if self.cinder.volumes.get(volume_id).status != 'available':
1301 break
1302 else: # all ready: break from while
1303 break
1304 time.sleep(5)
1305 elapsed_time += 5
tiernob0b9dab2017-10-14 14:25:20 +02001306 # If we exceeded the timeout rollback
montesmoreno0c8def02016-12-22 12:16:23 +00001307 if elapsed_time >= volume_timeout:
montesmoreno0c8def02016-12-22 12:16:23 +00001308 raise vimconn.vimconnException('Timeout creating volumes for instance ' + name,
1309 http_code=vimconn.HTTP_Request_Timeout)
mirabal29356312017-07-27 12:21:22 +02001310 # get availability Zone
tierno5a3273c2017-08-29 11:43:46 +02001311 vm_av_zone = self._get_vm_availability_zone(availability_zone_index, availability_zone_list)
montesmoreno0c8def02016-12-22 12:16:23 +00001312
tiernob0b9dab2017-10-14 14:25:20 +02001313 self.logger.debug("nova.servers.create({}, {}, {}, nics={}, security_groups={}, "
mirabal29356312017-07-27 12:21:22 +02001314 "availability_zone={}, key_name={}, userdata={}, config_drive={}, "
tiernob0b9dab2017-10-14 14:25:20 +02001315 "block_device_mapping={})".format(name, image_id, flavor_id, net_list_vim,
tiernoa05b65a2019-02-01 12:30:27 +00001316 self.config.get("security_groups"), vm_av_zone,
1317 self.config.get('keypair'), userdata, config_drive,
1318 block_device_mapping))
tiernob0b9dab2017-10-14 14:25:20 +02001319 server = self.nova.servers.create(name, image_id, flavor_id, nics=net_list_vim,
tiernoa05b65a2019-02-01 12:30:27 +00001320 security_groups=self.config.get("security_groups"),
1321 # TODO remove security_groups in future versions. Already at neutron port
mirabal29356312017-07-27 12:21:22 +02001322 availability_zone=vm_av_zone,
montesmoreno0c8def02016-12-22 12:16:23 +00001323 key_name=self.config.get('keypair'),
1324 userdata=userdata,
tiernob84cbdc2017-07-07 14:30:30 +02001325 config_drive=config_drive,
1326 block_device_mapping=block_device_mapping
montesmoreno0c8def02016-12-22 12:16:23 +00001327 ) # , description=description)
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +02001328
tierno326fd5e2018-02-22 11:58:59 +01001329 vm_start_time = time.time()
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +02001330 # Previously mentioned workaround to wait until the VM is active and then disable the port-security
1331 if no_secured_ports:
1332 self.__wait_for_vm(server.id, 'ACTIVE')
1333
1334 for port_id in no_secured_ports:
1335 try:
tierno4d1ce222018-04-06 10:41:06 +02001336 self.neutron.update_port(port_id,
1337 {"port": {"port_security_enabled": False, "security_groups": None}})
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +02001338 except Exception as e:
tierno4d1ce222018-04-06 10:41:06 +02001339 raise vimconn.vimconnException("It was not possible to disable port security for port {}".format(
1340 port_id))
tierno98e909c2017-10-14 13:27:03 +02001341 # print "DONE :-)", server
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +02001342
tierno4d1ce222018-04-06 10:41:06 +02001343 # pool_id = None
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +02001344 if external_network:
tierno98e909c2017-10-14 13:27:03 +02001345 floating_ips = self.neutron.list_floatingips().get("floatingips", ())
ahmadsaf853d452016-12-22 11:33:47 +05001346 for floating_network in external_network:
tiernof8383b82017-01-18 15:49:48 +01001347 try:
tiernof8383b82017-01-18 15:49:48 +01001348 assigned = False
tierno98e909c2017-10-14 13:27:03 +02001349 while not assigned:
tiernof8383b82017-01-18 15:49:48 +01001350 if floating_ips:
1351 ip = floating_ips.pop(0)
tierno326fd5e2018-02-22 11:58:59 +01001352 if ip.get("port_id", False) or ip.get('tenant_id') != server.tenant_id:
1353 continue
1354 if isinstance(floating_network['floating_ip'], str):
1355 if ip.get("floating_network_id") != floating_network['floating_ip']:
1356 continue
1357 free_floating_ip = ip.get("floating_ip_address")
tiernof8383b82017-01-18 15:49:48 +01001358 else:
tiernocb3cca22018-05-31 15:08:52 +02001359 if isinstance(floating_network['floating_ip'], str) and \
1360 floating_network['floating_ip'].lower() != "true":
tierno326fd5e2018-02-22 11:58:59 +01001361 pool_id = floating_network['floating_ip']
1362 else:
tierno4d1ce222018-04-06 10:41:06 +02001363 # Find the external network
tierno326fd5e2018-02-22 11:58:59 +01001364 external_nets = list()
1365 for net in self.neutron.list_networks()['networks']:
1366 if net['router:external']:
1367 external_nets.append(net)
tiernof8383b82017-01-18 15:49:48 +01001368
tierno326fd5e2018-02-22 11:58:59 +01001369 if len(external_nets) == 0:
1370 raise vimconn.vimconnException("Cannot create floating_ip automatically since no external "
1371 "network is present",
1372 http_code=vimconn.HTTP_Conflict)
1373 if len(external_nets) > 1:
1374 raise vimconn.vimconnException("Cannot create floating_ip automatically since multiple "
1375 "external networks are present",
1376 http_code=vimconn.HTTP_Conflict)
tiernof8383b82017-01-18 15:49:48 +01001377
tierno326fd5e2018-02-22 11:58:59 +01001378 pool_id = external_nets[0].get('id')
tiernof8383b82017-01-18 15:49:48 +01001379 param = {'floatingip': {'floating_network_id': pool_id, 'tenant_id': server.tenant_id}}
ahmadsaf853d452016-12-22 11:33:47 +05001380 try:
tierno4d1ce222018-04-06 10:41:06 +02001381 # self.logger.debug("Creating floating IP")
tiernof8383b82017-01-18 15:49:48 +01001382 new_floating_ip = self.neutron.create_floatingip(param)
1383 free_floating_ip = new_floating_ip['floatingip']['floating_ip_address']
ahmadsaf853d452016-12-22 11:33:47 +05001384 except Exception as e:
tierno326fd5e2018-02-22 11:58:59 +01001385 raise vimconn.vimconnException(type(e).__name__ + ": Cannot create new floating_ip " +
1386 str(e), http_code=vimconn.HTTP_Conflict)
1387
1388 fix_ip = floating_network.get('ip')
1389 while not assigned:
1390 try:
1391 server.add_floating_ip(free_floating_ip, fix_ip)
1392 assigned = True
1393 except Exception as e:
tierno4d1ce222018-04-06 10:41:06 +02001394 # openstack need some time after VM creation to asign an IP. So retry if fails
tierno326fd5e2018-02-22 11:58:59 +01001395 vm_status = self.nova.servers.get(server.id).status
1396 if vm_status != 'ACTIVE' and vm_status != 'ERROR':
1397 if time.time() - vm_start_time < server_timeout:
1398 time.sleep(5)
1399 continue
tierno4d1ce222018-04-06 10:41:06 +02001400 raise vimconn.vimconnException(
1401 "Cannot create floating_ip: {} {}".format(type(e).__name__, e),
1402 http_code=vimconn.HTTP_Conflict)
tierno326fd5e2018-02-22 11:58:59 +01001403
tiernof8383b82017-01-18 15:49:48 +01001404 except Exception as e:
1405 if not floating_network['exit_on_floating_ip_error']:
1406 self.logger.warn("Cannot create floating_ip. %s", str(e))
1407 continue
tiernof8383b82017-01-18 15:49:48 +01001408 raise
montesmoreno2a1fc4e2017-01-09 16:46:04 +00001409
tierno98e909c2017-10-14 13:27:03 +02001410 return server.id, created_items
tierno7edb6752016-03-21 17:37:52 +01001411# except nvExceptions.NotFound as e:
1412# error_value=-vimconn.HTTP_Not_Found
1413# error_text= "vm instance %s not found" % vm_id
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +02001414# except TypeError as e:
1415# raise vimconn.vimconnException(type(e).__name__ + ": "+ str(e), http_code=vimconn.HTTP_Bad_Request)
1416
1417 except Exception as e:
tierno98e909c2017-10-14 13:27:03 +02001418 server_id = None
1419 if server:
1420 server_id = server.id
1421 try:
1422 self.delete_vminstance(server_id, created_items)
1423 except Exception as e2:
1424 self.logger.error("new_vminstance rollback fail {}".format(e2))
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +02001425
tiernoae4a8d12016-07-08 12:30:39 +02001426 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +01001427
tiernoae4a8d12016-07-08 12:30:39 +02001428 def get_vminstance(self,vm_id):
tierno7edb6752016-03-21 17:37:52 +01001429 '''Returns the VM instance information from VIM'''
tiernoae4a8d12016-07-08 12:30:39 +02001430 #self.logger.debug("Getting VM from VIM")
tierno7edb6752016-03-21 17:37:52 +01001431 try:
1432 self._reload_connection()
1433 server = self.nova.servers.find(id=vm_id)
1434 #TODO parse input and translate to VIM format (openmano_schemas.new_vminstance_response_schema)
tiernoae4a8d12016-07-08 12:30:39 +02001435 return server.to_dict()
tierno8e995ce2016-09-22 08:13:00 +00001436 except (ksExceptions.ClientException, nvExceptions.ClientException, nvExceptions.NotFound, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +02001437 self._format_exception(e)
1438
1439 def get_vminstance_console(self,vm_id, console_type="vnc"):
tierno7edb6752016-03-21 17:37:52 +01001440 '''
1441 Get a console for the virtual machine
1442 Params:
1443 vm_id: uuid of the VM
1444 console_type, can be:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001445 "novnc" (by default), "xvpvnc" for VNC types,
tierno7edb6752016-03-21 17:37:52 +01001446 "rdp-html5" for RDP types, "spice-html5" for SPICE types
tiernoae4a8d12016-07-08 12:30:39 +02001447 Returns dict with the console parameters:
1448 protocol: ssh, ftp, http, https, ...
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001449 server: usually ip address
1450 port: the http, ssh, ... port
1451 suffix: extra text, e.g. the http path and query string
tierno7edb6752016-03-21 17:37:52 +01001452 '''
tiernoae4a8d12016-07-08 12:30:39 +02001453 self.logger.debug("Getting VM CONSOLE from VIM")
tierno7edb6752016-03-21 17:37:52 +01001454 try:
1455 self._reload_connection()
1456 server = self.nova.servers.find(id=vm_id)
1457 if console_type == None or console_type == "novnc":
1458 console_dict = server.get_vnc_console("novnc")
1459 elif console_type == "xvpvnc":
1460 console_dict = server.get_vnc_console(console_type)
1461 elif console_type == "rdp-html5":
1462 console_dict = server.get_rdp_console(console_type)
1463 elif console_type == "spice-html5":
1464 console_dict = server.get_spice_console(console_type)
1465 else:
tiernoae4a8d12016-07-08 12:30:39 +02001466 raise vimconn.vimconnException("console type '{}' not allowed".format(console_type), http_code=vimconn.HTTP_Bad_Request)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001467
tierno7edb6752016-03-21 17:37:52 +01001468 console_dict1 = console_dict.get("console")
1469 if console_dict1:
1470 console_url = console_dict1.get("url")
1471 if console_url:
1472 #parse console_url
1473 protocol_index = console_url.find("//")
1474 suffix_index = console_url[protocol_index+2:].find("/") + protocol_index+2
1475 port_index = console_url[protocol_index+2:suffix_index].find(":") + protocol_index+2
1476 if protocol_index < 0 or port_index<0 or suffix_index<0:
1477 return -vimconn.HTTP_Internal_Server_Error, "Unexpected response from VIM"
1478 console_dict={"protocol": console_url[0:protocol_index],
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001479 "server": console_url[protocol_index+2:port_index],
1480 "port": console_url[port_index:suffix_index],
1481 "suffix": console_url[suffix_index+1:]
tierno7edb6752016-03-21 17:37:52 +01001482 }
1483 protocol_index += 2
tiernoae4a8d12016-07-08 12:30:39 +02001484 return console_dict
1485 raise vimconn.vimconnUnexpectedResponse("Unexpected response from VIM")
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001486
tierno8e995ce2016-09-22 08:13:00 +00001487 except (nvExceptions.NotFound, ksExceptions.ClientException, nvExceptions.ClientException, nvExceptions.BadRequest, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +02001488 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +01001489
tierno98e909c2017-10-14 13:27:03 +02001490 def delete_vminstance(self, vm_id, created_items=None):
tiernoae4a8d12016-07-08 12:30:39 +02001491 '''Removes a VM instance from VIM. Returns the old identifier
tierno7edb6752016-03-21 17:37:52 +01001492 '''
tiernoae4a8d12016-07-08 12:30:39 +02001493 #print "osconnector: Getting VM from VIM"
tierno98e909c2017-10-14 13:27:03 +02001494 if created_items == None:
1495 created_items = {}
tierno7edb6752016-03-21 17:37:52 +01001496 try:
1497 self._reload_connection()
tierno98e909c2017-10-14 13:27:03 +02001498 # delete VM ports attached to this networks before the virtual machine
1499 for k, v in created_items.items():
1500 if not v: # skip already deleted
1501 continue
tierno7edb6752016-03-21 17:37:52 +01001502 try:
tiernoad6bdd42018-01-10 10:43:46 +01001503 k_item, _, k_id = k.partition(":")
1504 if k_item == "port":
1505 self.neutron.delete_port(k_id)
tierno7edb6752016-03-21 17:37:52 +01001506 except Exception as e:
tierno00e3df72017-11-29 17:20:13 +01001507 self.logger.error("Error deleting port: {}: {}".format(type(e).__name__, e))
montesmoreno0c8def02016-12-22 12:16:23 +00001508
tierno98e909c2017-10-14 13:27:03 +02001509 # #commented because detaching the volumes makes the servers.delete not work properly ?!?
1510 # #dettach volumes attached
1511 # server = self.nova.servers.get(vm_id)
1512 # volumes_attached_dict = server._info['os-extended-volumes:volumes_attached'] #volume['id']
1513 # #for volume in volumes_attached_dict:
1514 # # self.cinder.volumes.detach(volume['id'])
montesmoreno0c8def02016-12-22 12:16:23 +00001515
tierno98e909c2017-10-14 13:27:03 +02001516 if vm_id:
1517 self.nova.servers.delete(vm_id)
montesmoreno0c8def02016-12-22 12:16:23 +00001518
tierno98e909c2017-10-14 13:27:03 +02001519 # delete volumes. Although having detached, they should have in active status before deleting
1520 # we ensure in this loop
montesmoreno0c8def02016-12-22 12:16:23 +00001521 keep_waiting = True
1522 elapsed_time = 0
1523 while keep_waiting and elapsed_time < volume_timeout:
1524 keep_waiting = False
tierno98e909c2017-10-14 13:27:03 +02001525 for k, v in created_items.items():
1526 if not v: # skip already deleted
1527 continue
1528 try:
tiernoad6bdd42018-01-10 10:43:46 +01001529 k_item, _, k_id = k.partition(":")
1530 if k_item == "volume":
1531 if self.cinder.volumes.get(k_id).status != 'available':
tierno98e909c2017-10-14 13:27:03 +02001532 keep_waiting = True
1533 else:
tiernoad6bdd42018-01-10 10:43:46 +01001534 self.cinder.volumes.delete(k_id)
tierno98e909c2017-10-14 13:27:03 +02001535 except Exception as e:
tierno00e3df72017-11-29 17:20:13 +01001536 self.logger.error("Error deleting volume: {}: {}".format(type(e).__name__, e))
montesmoreno0c8def02016-12-22 12:16:23 +00001537 if keep_waiting:
1538 time.sleep(1)
1539 elapsed_time += 1
tierno98e909c2017-10-14 13:27:03 +02001540 return None
tierno8e995ce2016-09-22 08:13:00 +00001541 except (nvExceptions.NotFound, ksExceptions.ClientException, nvExceptions.ClientException, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +02001542 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +01001543
tiernoae4a8d12016-07-08 12:30:39 +02001544 def refresh_vms_status(self, vm_list):
1545 '''Get the status of the virtual machines and their interfaces/ports
1546 Params: the list of VM identifiers
1547 Returns a dictionary with:
1548 vm_id: #VIM id of this Virtual Machine
1549 status: #Mandatory. Text with one of:
1550 # DELETED (not found at vim)
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001551 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
tiernoae4a8d12016-07-08 12:30:39 +02001552 # OTHER (Vim reported other status not understood)
1553 # ERROR (VIM indicates an ERROR status)
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001554 # ACTIVE, PAUSED, SUSPENDED, INACTIVE (not running),
tiernoae4a8d12016-07-08 12:30:39 +02001555 # CREATING (on building process), ERROR
1556 # ACTIVE:NoMgmtIP (Active but any of its interface has an IP address
1557 #
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001558 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
tiernoae4a8d12016-07-08 12:30:39 +02001559 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
1560 interfaces:
1561 - vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
1562 mac_address: #Text format XX:XX:XX:XX:XX:XX
1563 vim_net_id: #network id where this interface is connected
1564 vim_interface_id: #interface/port VIM id
1565 ip_address: #null, or text with IPv4, IPv6 address
tierno867ffe92017-03-27 12:50:34 +02001566 compute_node: #identification of compute node where PF,VF interface is allocated
1567 pci: #PCI address of the NIC that hosts the PF,VF
1568 vlan: #physical VLAN used for VF
tierno7edb6752016-03-21 17:37:52 +01001569 '''
tiernoae4a8d12016-07-08 12:30:39 +02001570 vm_dict={}
1571 self.logger.debug("refresh_vms status: Getting tenant VM instance information from VIM")
1572 for vm_id in vm_list:
1573 vm={}
1574 try:
1575 vm_vim = self.get_vminstance(vm_id)
1576 if vm_vim['status'] in vmStatus2manoFormat:
1577 vm['status'] = vmStatus2manoFormat[ vm_vim['status'] ]
tierno7edb6752016-03-21 17:37:52 +01001578 else:
tiernoae4a8d12016-07-08 12:30:39 +02001579 vm['status'] = "OTHER"
1580 vm['error_msg'] = "VIM status reported " + vm_vim['status']
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001581
1582 vm['vim_info'] = self.serialize(vm_vim)
1583
tiernoae4a8d12016-07-08 12:30:39 +02001584 vm["interfaces"] = []
1585 if vm_vim.get('fault'):
1586 vm['error_msg'] = str(vm_vim['fault'])
1587 #get interfaces
tierno7edb6752016-03-21 17:37:52 +01001588 try:
tiernoae4a8d12016-07-08 12:30:39 +02001589 self._reload_connection()
tiernob42fd9b2018-06-20 10:44:32 +02001590 port_dict = self.neutron.list_ports(device_id=vm_id)
tiernoae4a8d12016-07-08 12:30:39 +02001591 for port in port_dict["ports"]:
1592 interface={}
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001593 interface['vim_info'] = self.serialize(port)
tiernoae4a8d12016-07-08 12:30:39 +02001594 interface["mac_address"] = port.get("mac_address")
1595 interface["vim_net_id"] = port["network_id"]
1596 interface["vim_interface_id"] = port["id"]
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001597 # check if OS-EXT-SRV-ATTR:host is there,
Mike Marchetti5b9da422017-05-02 15:35:47 -04001598 # in case of non-admin credentials, it will be missing
1599 if vm_vim.get('OS-EXT-SRV-ATTR:host'):
1600 interface["compute_node"] = vm_vim['OS-EXT-SRV-ATTR:host']
tierno867ffe92017-03-27 12:50:34 +02001601 interface["pci"] = None
Mike Marchetti5b9da422017-05-02 15:35:47 -04001602
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001603 # check if binding:profile is there,
Mike Marchetti5b9da422017-05-02 15:35:47 -04001604 # in case of non-admin credentials, it will be missing
1605 if port.get('binding:profile'):
1606 if port['binding:profile'].get('pci_slot'):
1607 # TODO: At the moment sr-iov pci addresses are converted to PF pci addresses by setting the slot to 0x00
1608 # TODO: This is just a workaround valid for niantinc. Find a better way to do so
1609 # CHANGE DDDD:BB:SS.F to DDDD:BB:00.(F%2) assuming there are 2 ports per nic
1610 pci = port['binding:profile']['pci_slot']
1611 # interface["pci"] = pci[:-4] + "00." + str(int(pci[-1]) % 2)
1612 interface["pci"] = pci
tierno867ffe92017-03-27 12:50:34 +02001613 interface["vlan"] = None
Pablo Montes Moreno3be0b2a2017-03-30 13:22:15 +02001614 #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 +01001615 network = self.neutron.show_network(port["network_id"])
Pablo Montes Moreno3be0b2a2017-03-30 13:22:15 +02001616 if network['network'].get('provider:network_type') == 'vlan' and \
1617 port.get("binding:vnic_type") == "direct":
tierno867ffe92017-03-27 12:50:34 +02001618 interface["vlan"] = network['network'].get('provider:segmentation_id')
tiernoae4a8d12016-07-08 12:30:39 +02001619 ips=[]
1620 #look for floating ip address
tiernob42fd9b2018-06-20 10:44:32 +02001621 try:
1622 floating_ip_dict = self.neutron.list_floatingips(port_id=port["id"])
1623 if floating_ip_dict.get("floatingips"):
1624 ips.append(floating_ip_dict["floatingips"][0].get("floating_ip_address") )
1625 except Exception:
1626 pass
tierno7edb6752016-03-21 17:37:52 +01001627
tiernoae4a8d12016-07-08 12:30:39 +02001628 for subnet in port["fixed_ips"]:
1629 ips.append(subnet["ip_address"])
1630 interface["ip_address"] = ";".join(ips)
1631 vm["interfaces"].append(interface)
1632 except Exception as e:
tiernob42fd9b2018-06-20 10:44:32 +02001633 self.logger.error("Error getting vm interface information {}: {}".format(type(e).__name__, e),
1634 exc_info=True)
tiernoae4a8d12016-07-08 12:30:39 +02001635 except vimconn.vimconnNotFoundException as e:
1636 self.logger.error("Exception getting vm status: %s", str(e))
1637 vm['status'] = "DELETED"
1638 vm['error_msg'] = str(e)
1639 except vimconn.vimconnException as e:
1640 self.logger.error("Exception getting vm status: %s", str(e))
1641 vm['status'] = "VIM_ERROR"
1642 vm['error_msg'] = str(e)
1643 vm_dict[vm_id] = vm
1644 return vm_dict
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001645
tierno98e909c2017-10-14 13:27:03 +02001646 def action_vminstance(self, vm_id, action_dict, created_items={}):
tierno7edb6752016-03-21 17:37:52 +01001647 '''Send and action over a VM instance from VIM
tierno98e909c2017-10-14 13:27:03 +02001648 Returns None or the console dict if the action was successfully sent to the VIM'''
tiernoae4a8d12016-07-08 12:30:39 +02001649 self.logger.debug("Action over VM '%s': %s", vm_id, str(action_dict))
tierno7edb6752016-03-21 17:37:52 +01001650 try:
1651 self._reload_connection()
1652 server = self.nova.servers.find(id=vm_id)
1653 if "start" in action_dict:
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001654 if action_dict["start"]=="rebuild":
tierno7edb6752016-03-21 17:37:52 +01001655 server.rebuild()
1656 else:
1657 if server.status=="PAUSED":
1658 server.unpause()
1659 elif server.status=="SUSPENDED":
1660 server.resume()
1661 elif server.status=="SHUTOFF":
1662 server.start()
1663 elif "pause" in action_dict:
1664 server.pause()
1665 elif "resume" in action_dict:
1666 server.resume()
1667 elif "shutoff" in action_dict or "shutdown" in action_dict:
1668 server.stop()
1669 elif "forceOff" in action_dict:
1670 server.stop() #TODO
1671 elif "terminate" in action_dict:
1672 server.delete()
1673 elif "createImage" in action_dict:
1674 server.create_image()
1675 #"path":path_schema,
1676 #"description":description_schema,
1677 #"name":name_schema,
1678 #"metadata":metadata_schema,
1679 #"imageRef": id_schema,
1680 #"disk": {"oneOf":[{"type": "null"}, {"type":"string"}] },
1681 elif "rebuild" in action_dict:
1682 server.rebuild(server.image['id'])
1683 elif "reboot" in action_dict:
1684 server.reboot() #reboot_type='SOFT'
1685 elif "console" in action_dict:
1686 console_type = action_dict["console"]
1687 if console_type == None or console_type == "novnc":
1688 console_dict = server.get_vnc_console("novnc")
1689 elif console_type == "xvpvnc":
1690 console_dict = server.get_vnc_console(console_type)
1691 elif console_type == "rdp-html5":
1692 console_dict = server.get_rdp_console(console_type)
1693 elif console_type == "spice-html5":
1694 console_dict = server.get_spice_console(console_type)
1695 else:
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001696 raise vimconn.vimconnException("console type '{}' not allowed".format(console_type),
tiernoae4a8d12016-07-08 12:30:39 +02001697 http_code=vimconn.HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001698 try:
1699 console_url = console_dict["console"]["url"]
1700 #parse console_url
1701 protocol_index = console_url.find("//")
1702 suffix_index = console_url[protocol_index+2:].find("/") + protocol_index+2
1703 port_index = console_url[protocol_index+2:suffix_index].find(":") + protocol_index+2
1704 if protocol_index < 0 or port_index<0 or suffix_index<0:
tiernoae4a8d12016-07-08 12:30:39 +02001705 raise vimconn.vimconnException("Unexpected response from VIM " + str(console_dict))
tierno7edb6752016-03-21 17:37:52 +01001706 console_dict2={"protocol": console_url[0:protocol_index],
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001707 "server": console_url[protocol_index+2 : port_index],
1708 "port": int(console_url[port_index+1 : suffix_index]),
1709 "suffix": console_url[suffix_index+1:]
tierno7edb6752016-03-21 17:37:52 +01001710 }
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001711 return console_dict2
tiernoae4a8d12016-07-08 12:30:39 +02001712 except Exception as e:
1713 raise vimconn.vimconnException("Unexpected response from VIM " + str(console_dict))
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001714
tierno98e909c2017-10-14 13:27:03 +02001715 return None
tierno8e995ce2016-09-22 08:13:00 +00001716 except (ksExceptions.ClientException, nvExceptions.ClientException, nvExceptions.NotFound, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +02001717 self._format_exception(e)
1718 #TODO insert exception vimconn.HTTP_Unauthorized
1719
kate721d79b2017-06-24 04:21:38 -07001720 ####### VIO Specific Changes #########
garciadeblasebd66722019-01-31 16:01:31 +00001721 def _generate_vlanID(self):
kate721d79b2017-06-24 04:21:38 -07001722 """
1723 Method to get unused vlanID
1724 Args:
1725 None
1726 Returns:
1727 vlanID
1728 """
1729 #Get used VLAN IDs
1730 usedVlanIDs = []
1731 networks = self.get_network_list()
1732 for net in networks:
1733 if net.get('provider:segmentation_id'):
1734 usedVlanIDs.append(net.get('provider:segmentation_id'))
1735 used_vlanIDs = set(usedVlanIDs)
1736
1737 #find unused VLAN ID
1738 for vlanID_range in self.config.get('dataplane_net_vlan_range'):
1739 try:
1740 start_vlanid , end_vlanid = map(int, vlanID_range.replace(" ", "").split("-"))
1741 for vlanID in xrange(start_vlanid, end_vlanid + 1):
1742 if vlanID not in used_vlanIDs:
1743 return vlanID
1744 except Exception as exp:
1745 raise vimconn.vimconnException("Exception {} occurred while generating VLAN ID.".format(exp))
1746 else:
1747 raise vimconn.vimconnConflictException("Unable to create the SRIOV VLAN network."\
1748 " All given Vlan IDs {} are in use.".format(self.config.get('dataplane_net_vlan_range')))
1749
1750
garciadeblasebd66722019-01-31 16:01:31 +00001751 def _generate_multisegment_vlanID(self):
1752 """
1753 Method to get unused vlanID
1754 Args:
1755 None
1756 Returns:
1757 vlanID
1758 """
1759 #Get used VLAN IDs
1760 usedVlanIDs = []
1761 networks = self.get_network_list()
1762 for net in networks:
1763 if net.get('provider:network_type') == "vlan" and net.get('provider:segmentation_id'):
1764 usedVlanIDs.append(net.get('provider:segmentation_id'))
1765 elif net.get('segments'):
1766 for segment in net.get('segments'):
1767 if segment.get('provider:network_type') == "vlan" and segment.get('provider:segmentation_id'):
1768 usedVlanIDs.append(segment.get('provider:segmentation_id'))
1769 used_vlanIDs = set(usedVlanIDs)
1770
1771 #find unused VLAN ID
1772 for vlanID_range in self.config.get('multisegment_vlan_range'):
1773 try:
1774 start_vlanid , end_vlanid = map(int, vlanID_range.replace(" ", "").split("-"))
1775 for vlanID in xrange(start_vlanid, end_vlanid + 1):
1776 if vlanID not in used_vlanIDs:
1777 return vlanID
1778 except Exception as exp:
1779 raise vimconn.vimconnException("Exception {} occurred while generating VLAN ID.".format(exp))
1780 else:
1781 raise vimconn.vimconnConflictException("Unable to create the VLAN segment."\
1782 " All VLAN IDs {} are in use.".format(self.config.get('multisegment_vlan_range')))
1783
1784
1785 def _validate_vlan_ranges(self, input_vlan_range, text_vlan_range):
kate721d79b2017-06-24 04:21:38 -07001786 """
1787 Method to validate user given vlanID ranges
1788 Args: None
1789 Returns: None
1790 """
garciadeblasebd66722019-01-31 16:01:31 +00001791 for vlanID_range in input_vlan_range:
kate721d79b2017-06-24 04:21:38 -07001792 vlan_range = vlanID_range.replace(" ", "")
1793 #validate format
1794 vlanID_pattern = r'(\d)*-(\d)*$'
1795 match_obj = re.match(vlanID_pattern, vlan_range)
1796 if not match_obj:
garciadeblasebd66722019-01-31 16:01:31 +00001797 raise vimconn.vimconnConflictException("Invalid VLAN range for {}: {}.You must provide "\
1798 "'{}' in format [start_ID - end_ID].".format(text_vlan_range, vlanID_range, text_vlan_range))
kate721d79b2017-06-24 04:21:38 -07001799
1800 start_vlanid , end_vlanid = map(int,vlan_range.split("-"))
1801 if start_vlanid <= 0 :
garciadeblasebd66722019-01-31 16:01:31 +00001802 raise vimconn.vimconnConflictException("Invalid VLAN range for {}: {}."\
kate721d79b2017-06-24 04:21:38 -07001803 "Start ID can not be zero. For VLAN "\
garciadeblasebd66722019-01-31 16:01:31 +00001804 "networks valid IDs are 1 to 4094 ".format(text_vlan_range, vlanID_range))
kate721d79b2017-06-24 04:21:38 -07001805 if end_vlanid > 4094 :
garciadeblasebd66722019-01-31 16:01:31 +00001806 raise vimconn.vimconnConflictException("Invalid VLAN range for {}: {}."\
kate721d79b2017-06-24 04:21:38 -07001807 "End VLAN ID can not be greater than 4094. For VLAN "\
garciadeblasebd66722019-01-31 16:01:31 +00001808 "networks valid IDs are 1 to 4094 ".format(text_vlan_range, vlanID_range))
kate721d79b2017-06-24 04:21:38 -07001809
1810 if start_vlanid > end_vlanid:
garciadeblasebd66722019-01-31 16:01:31 +00001811 raise vimconn.vimconnConflictException("Invalid VLAN range for {}: {}."\
1812 "You must provide '{}' in format start_ID - end_ID and "\
1813 "start_ID < end_ID ".format(text_vlan_range, vlanID_range, text_vlan_range))
kate721d79b2017-06-24 04:21:38 -07001814
tiernoae4a8d12016-07-08 12:30:39 +02001815#NOT USED FUNCTIONS
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001816
tiernoae4a8d12016-07-08 12:30:39 +02001817 def new_external_port(self, port_data):
1818 #TODO openstack if needed
1819 '''Adds a external port to VIM'''
1820 '''Returns the port identifier'''
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001821 return -vimconn.HTTP_Internal_Server_Error, "osconnector.new_external_port() not implemented"
1822
tiernoae4a8d12016-07-08 12:30:39 +02001823 def connect_port_network(self, port_id, network_id, admin=False):
1824 #TODO openstack if needed
1825 '''Connects a external port to a network'''
1826 '''Returns status code of the VIM response'''
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001827 return -vimconn.HTTP_Internal_Server_Error, "osconnector.connect_port_network() not implemented"
1828
tiernoae4a8d12016-07-08 12:30:39 +02001829 def new_user(self, user_name, user_passwd, tenant_id=None):
1830 '''Adds a new user to openstack VIM'''
1831 '''Returns the user identifier'''
1832 self.logger.debug("osconnector: Adding a new user to VIM")
1833 try:
1834 self._reload_connection()
Eduardo Sousae3c0dbc2018-09-03 11:56:07 +01001835 user=self.keystone.users.create(user_name, password=user_passwd, default_project=tenant_id)
tiernoae4a8d12016-07-08 12:30:39 +02001836 #self.keystone.tenants.add_user(self.k_creds["username"], #role)
1837 return user.id
1838 except ksExceptions.ConnectionError as e:
1839 error_value=-vimconn.HTTP_Bad_Request
1840 error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0]))
1841 except ksExceptions.ClientException as e: #TODO remove
tierno7edb6752016-03-21 17:37:52 +01001842 error_value=-vimconn.HTTP_Bad_Request
1843 error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0]))
1844 #TODO insert exception vimconn.HTTP_Unauthorized
1845 #if reaching here is because an exception
tierno9c5c8322018-03-23 15:44:03 +01001846 self.logger.debug("new_user " + error_text)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001847 return error_value, error_text
tiernoae4a8d12016-07-08 12:30:39 +02001848
1849 def delete_user(self, user_id):
1850 '''Delete a user from openstack VIM'''
1851 '''Returns the user identifier'''
1852 if self.debug:
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001853 print("osconnector: Deleting a user from VIM")
tiernoae4a8d12016-07-08 12:30:39 +02001854 try:
1855 self._reload_connection()
1856 self.keystone.users.delete(user_id)
1857 return 1, user_id
1858 except ksExceptions.ConnectionError as e:
1859 error_value=-vimconn.HTTP_Bad_Request
1860 error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0]))
1861 except ksExceptions.NotFound as e:
1862 error_value=-vimconn.HTTP_Not_Found
1863 error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0]))
1864 except ksExceptions.ClientException as e: #TODO remove
1865 error_value=-vimconn.HTTP_Bad_Request
1866 error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0]))
1867 #TODO insert exception vimconn.HTTP_Unauthorized
1868 #if reaching here is because an exception
tierno9c5c8322018-03-23 15:44:03 +01001869 self.logger.debug("delete_tenant " + error_text)
tiernoae4a8d12016-07-08 12:30:39 +02001870 return error_value, error_text
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001871
tierno7edb6752016-03-21 17:37:52 +01001872 def get_hosts_info(self):
1873 '''Get the information of deployed hosts
1874 Returns the hosts content'''
1875 if self.debug:
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001876 print("osconnector: Getting Host info from VIM")
tierno7edb6752016-03-21 17:37:52 +01001877 try:
1878 h_list=[]
1879 self._reload_connection()
1880 hypervisors = self.nova.hypervisors.list()
1881 for hype in hypervisors:
1882 h_list.append( hype.to_dict() )
1883 return 1, {"hosts":h_list}
1884 except nvExceptions.NotFound as e:
1885 error_value=-vimconn.HTTP_Not_Found
1886 error_text= (str(e) if len(e.args)==0 else str(e.args[0]))
1887 except (ksExceptions.ClientException, nvExceptions.ClientException) as e:
1888 error_value=-vimconn.HTTP_Bad_Request
1889 error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0]))
1890 #TODO insert exception vimconn.HTTP_Unauthorized
1891 #if reaching here is because an exception
tierno9c5c8322018-03-23 15:44:03 +01001892 self.logger.debug("get_hosts_info " + error_text)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001893 return error_value, error_text
tierno7edb6752016-03-21 17:37:52 +01001894
1895 def get_hosts(self, vim_tenant):
1896 '''Get the hosts and deployed instances
1897 Returns the hosts content'''
1898 r, hype_dict = self.get_hosts_info()
1899 if r<0:
1900 return r, hype_dict
1901 hypervisors = hype_dict["hosts"]
1902 try:
1903 servers = self.nova.servers.list()
1904 for hype in hypervisors:
1905 for server in servers:
1906 if server.to_dict()['OS-EXT-SRV-ATTR:hypervisor_hostname']==hype['hypervisor_hostname']:
1907 if 'vm' in hype:
1908 hype['vm'].append(server.id)
1909 else:
1910 hype['vm'] = [server.id]
1911 return 1, hype_dict
1912 except nvExceptions.NotFound as e:
1913 error_value=-vimconn.HTTP_Not_Found
1914 error_text= (str(e) if len(e.args)==0 else str(e.args[0]))
1915 except (ksExceptions.ClientException, nvExceptions.ClientException) as e:
1916 error_value=-vimconn.HTTP_Bad_Request
1917 error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0]))
1918 #TODO insert exception vimconn.HTTP_Unauthorized
1919 #if reaching here is because an exception
tierno9c5c8322018-03-23 15:44:03 +01001920 self.logger.debug("get_hosts " + error_text)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001921 return error_value, error_text
tierno7edb6752016-03-21 17:37:52 +01001922
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001923 def new_classification(self, name, ctype, definition):
1924 self.logger.debug(
1925 'Adding a new (Traffic) Classification to VIM, named %s', name)
1926 try:
1927 new_class = None
1928 self._reload_connection()
1929 if ctype not in supportedClassificationTypes:
1930 raise vimconn.vimconnNotSupportedException(
1931 'OpenStack VIM connector doesn\'t support provided '
1932 'Classification Type {}, supported ones are: '
1933 '{}'.format(ctype, supportedClassificationTypes))
1934 if not self._validate_classification(ctype, definition):
1935 raise vimconn.vimconnException(
1936 'Incorrect Classification definition '
1937 'for the type specified.')
1938 classification_dict = definition
1939 classification_dict['name'] = name
tierno7edb6752016-03-21 17:37:52 +01001940
Igor D.Ccaadc442017-11-06 12:48:48 +00001941 new_class = self.neutron.create_sfc_flow_classifier(
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001942 {'flow_classifier': classification_dict})
1943 return new_class['flow_classifier']['id']
1944 except (neExceptions.ConnectionFailed, ksExceptions.ClientException,
1945 neExceptions.NeutronException, ConnectionError) as e:
1946 self.logger.error(
1947 'Creation of Classification failed.')
1948 self._format_exception(e)
1949
1950 def get_classification(self, class_id):
1951 self.logger.debug(" Getting Classification %s from VIM", class_id)
1952 filter_dict = {"id": class_id}
1953 class_list = self.get_classification_list(filter_dict)
1954 if len(class_list) == 0:
1955 raise vimconn.vimconnNotFoundException(
1956 "Classification '{}' not found".format(class_id))
1957 elif len(class_list) > 1:
1958 raise vimconn.vimconnConflictException(
1959 "Found more than one Classification with this criteria")
1960 classification = class_list[0]
1961 return classification
1962
1963 def get_classification_list(self, filter_dict={}):
1964 self.logger.debug("Getting Classifications from VIM filter: '%s'",
1965 str(filter_dict))
1966 try:
tierno69b590e2018-03-13 18:52:23 +01001967 filter_dict_os = filter_dict.copy()
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001968 self._reload_connection()
tierno69b590e2018-03-13 18:52:23 +01001969 if self.api_version3 and "tenant_id" in filter_dict_os:
1970 filter_dict_os['project_id'] = filter_dict_os.pop('tenant_id')
Igor D.Ccaadc442017-11-06 12:48:48 +00001971 classification_dict = self.neutron.list_sfc_flow_classifiers(
tierno69b590e2018-03-13 18:52:23 +01001972 **filter_dict_os)
1973 classification_list = classification_dict["flow_classifiers"]
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001974 self.__classification_os2mano(classification_list)
1975 return classification_list
1976 except (neExceptions.ConnectionFailed, ksExceptions.ClientException,
1977 neExceptions.NeutronException, ConnectionError) as e:
1978 self._format_exception(e)
1979
1980 def delete_classification(self, class_id):
1981 self.logger.debug("Deleting Classification '%s' from VIM", class_id)
1982 try:
1983 self._reload_connection()
Igor D.Ccaadc442017-11-06 12:48:48 +00001984 self.neutron.delete_sfc_flow_classifier(class_id)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001985 return class_id
1986 except (neExceptions.ConnectionFailed, neExceptions.NeutronException,
1987 ksExceptions.ClientException, neExceptions.NeutronException,
1988 ConnectionError) as e:
1989 self._format_exception(e)
1990
1991 def new_sfi(self, name, ingress_ports, egress_ports, sfc_encap=True):
1992 self.logger.debug(
1993 "Adding a new Service Function Instance to VIM, named '%s'", name)
1994 try:
1995 new_sfi = None
1996 self._reload_connection()
1997 correlation = None
1998 if sfc_encap:
Igor D.Ccaadc442017-11-06 12:48:48 +00001999 correlation = 'nsh'
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00002000 if len(ingress_ports) != 1:
2001 raise vimconn.vimconnNotSupportedException(
2002 "OpenStack VIM connector can only have "
2003 "1 ingress port per SFI")
2004 if len(egress_ports) != 1:
2005 raise vimconn.vimconnNotSupportedException(
2006 "OpenStack VIM connector can only have "
2007 "1 egress port per SFI")
2008 sfi_dict = {'name': name,
2009 'ingress': ingress_ports[0],
2010 'egress': egress_ports[0],
2011 'service_function_parameters': {
2012 'correlation': correlation}}
Igor D.Ccaadc442017-11-06 12:48:48 +00002013 new_sfi = self.neutron.create_sfc_port_pair({'port_pair': sfi_dict})
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00002014 return new_sfi['port_pair']['id']
2015 except (neExceptions.ConnectionFailed, ksExceptions.ClientException,
2016 neExceptions.NeutronException, ConnectionError) as e:
2017 if new_sfi:
2018 try:
Igor D.Ccaadc442017-11-06 12:48:48 +00002019 self.neutron.delete_sfc_port_pair(
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00002020 new_sfi['port_pair']['id'])
2021 except Exception:
2022 self.logger.error(
2023 'Creation of Service Function Instance failed, with '
2024 'subsequent deletion failure as well.')
2025 self._format_exception(e)
2026
2027 def get_sfi(self, sfi_id):
2028 self.logger.debug(
2029 'Getting Service Function Instance %s from VIM', sfi_id)
2030 filter_dict = {"id": sfi_id}
2031 sfi_list = self.get_sfi_list(filter_dict)
2032 if len(sfi_list) == 0:
2033 raise vimconn.vimconnNotFoundException(
2034 "Service Function Instance '{}' not found".format(sfi_id))
2035 elif len(sfi_list) > 1:
2036 raise vimconn.vimconnConflictException(
2037 'Found more than one Service Function Instance '
2038 'with this criteria')
2039 sfi = sfi_list[0]
2040 return sfi
2041
2042 def get_sfi_list(self, filter_dict={}):
2043 self.logger.debug("Getting Service Function Instances from "
2044 "VIM filter: '%s'", str(filter_dict))
2045 try:
2046 self._reload_connection()
tierno69b590e2018-03-13 18:52:23 +01002047 filter_dict_os = filter_dict.copy()
2048 if self.api_version3 and "tenant_id" in filter_dict_os:
2049 filter_dict_os['project_id'] = filter_dict_os.pop('tenant_id')
2050 sfi_dict = self.neutron.list_sfc_port_pairs(**filter_dict_os)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00002051 sfi_list = sfi_dict["port_pairs"]
2052 self.__sfi_os2mano(sfi_list)
2053 return sfi_list
2054 except (neExceptions.ConnectionFailed, ksExceptions.ClientException,
2055 neExceptions.NeutronException, ConnectionError) as e:
2056 self._format_exception(e)
2057
2058 def delete_sfi(self, sfi_id):
2059 self.logger.debug("Deleting Service Function Instance '%s' "
2060 "from VIM", sfi_id)
2061 try:
2062 self._reload_connection()
Igor D.Ccaadc442017-11-06 12:48:48 +00002063 self.neutron.delete_sfc_port_pair(sfi_id)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00002064 return sfi_id
2065 except (neExceptions.ConnectionFailed, neExceptions.NeutronException,
2066 ksExceptions.ClientException, neExceptions.NeutronException,
2067 ConnectionError) as e:
2068 self._format_exception(e)
2069
2070 def new_sf(self, name, sfis, sfc_encap=True):
2071 self.logger.debug("Adding a new Service Function to VIM, "
2072 "named '%s'", name)
2073 try:
2074 new_sf = None
2075 self._reload_connection()
tierno9c5c8322018-03-23 15:44:03 +01002076 # correlation = None
2077 # if sfc_encap:
2078 # correlation = 'nsh'
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00002079 for instance in sfis:
2080 sfi = self.get_sfi(instance)
Igor D.Ccaadc442017-11-06 12:48:48 +00002081 if sfi.get('sfc_encap') != sfc_encap:
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00002082 raise vimconn.vimconnNotSupportedException(
2083 "OpenStack VIM connector requires all SFIs of the "
2084 "same SF to share the same SFC Encapsulation")
2085 sf_dict = {'name': name,
2086 'port_pairs': sfis}
Igor D.Ccaadc442017-11-06 12:48:48 +00002087 new_sf = self.neutron.create_sfc_port_pair_group({
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00002088 'port_pair_group': sf_dict})
2089 return new_sf['port_pair_group']['id']
2090 except (neExceptions.ConnectionFailed, ksExceptions.ClientException,
2091 neExceptions.NeutronException, ConnectionError) as e:
2092 if new_sf:
2093 try:
Igor D.Ccaadc442017-11-06 12:48:48 +00002094 self.neutron.delete_sfc_port_pair_group(
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00002095 new_sf['port_pair_group']['id'])
2096 except Exception:
2097 self.logger.error(
2098 'Creation of Service Function failed, with '
2099 'subsequent deletion failure as well.')
2100 self._format_exception(e)
2101
2102 def get_sf(self, sf_id):
2103 self.logger.debug("Getting Service Function %s from VIM", sf_id)
2104 filter_dict = {"id": sf_id}
2105 sf_list = self.get_sf_list(filter_dict)
2106 if len(sf_list) == 0:
2107 raise vimconn.vimconnNotFoundException(
2108 "Service Function '{}' not found".format(sf_id))
2109 elif len(sf_list) > 1:
2110 raise vimconn.vimconnConflictException(
2111 "Found more than one Service Function with this criteria")
2112 sf = sf_list[0]
2113 return sf
2114
2115 def get_sf_list(self, filter_dict={}):
2116 self.logger.debug("Getting Service Function from VIM filter: '%s'",
2117 str(filter_dict))
2118 try:
2119 self._reload_connection()
tierno69b590e2018-03-13 18:52:23 +01002120 filter_dict_os = filter_dict.copy()
2121 if self.api_version3 and "tenant_id" in filter_dict_os:
2122 filter_dict_os['project_id'] = filter_dict_os.pop('tenant_id')
2123 sf_dict = self.neutron.list_sfc_port_pair_groups(**filter_dict_os)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00002124 sf_list = sf_dict["port_pair_groups"]
2125 self.__sf_os2mano(sf_list)
2126 return sf_list
2127 except (neExceptions.ConnectionFailed, ksExceptions.ClientException,
2128 neExceptions.NeutronException, ConnectionError) as e:
2129 self._format_exception(e)
2130
2131 def delete_sf(self, sf_id):
2132 self.logger.debug("Deleting Service Function '%s' from VIM", sf_id)
2133 try:
2134 self._reload_connection()
Igor D.Ccaadc442017-11-06 12:48:48 +00002135 self.neutron.delete_sfc_port_pair_group(sf_id)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00002136 return sf_id
2137 except (neExceptions.ConnectionFailed, neExceptions.NeutronException,
2138 ksExceptions.ClientException, neExceptions.NeutronException,
2139 ConnectionError) as e:
2140 self._format_exception(e)
2141
2142 def new_sfp(self, name, classifications, sfs, sfc_encap=True, spi=None):
2143 self.logger.debug("Adding a new Service Function Path to VIM, "
2144 "named '%s'", name)
2145 try:
2146 new_sfp = None
2147 self._reload_connection()
Igor D.Ccaadc442017-11-06 12:48:48 +00002148 # In networking-sfc the MPLS encapsulation is legacy
2149 # should be used when no full SFC Encapsulation is intended
schillinge981df9a2019-01-24 09:25:11 +01002150 correlation = 'mpls'
Igor D.Ccaadc442017-11-06 12:48:48 +00002151 if sfc_encap:
2152 correlation = 'nsh'
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00002153 sfp_dict = {'name': name,
2154 'flow_classifiers': classifications,
2155 'port_pair_groups': sfs,
2156 'chain_parameters': {'correlation': correlation}}
2157 if spi:
2158 sfp_dict['chain_id'] = spi
Igor D.Ccaadc442017-11-06 12:48:48 +00002159 new_sfp = self.neutron.create_sfc_port_chain({'port_chain': sfp_dict})
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00002160 return new_sfp["port_chain"]["id"]
2161 except (neExceptions.ConnectionFailed, ksExceptions.ClientException,
2162 neExceptions.NeutronException, ConnectionError) as e:
2163 if new_sfp:
2164 try:
Igor D.Ccaadc442017-11-06 12:48:48 +00002165 self.neutron.delete_sfc_port_chain(new_sfp['port_chain']['id'])
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00002166 except Exception:
2167 self.logger.error(
2168 'Creation of Service Function Path failed, with '
2169 'subsequent deletion failure as well.')
2170 self._format_exception(e)
2171
2172 def get_sfp(self, sfp_id):
2173 self.logger.debug(" Getting Service Function Path %s from VIM", sfp_id)
2174 filter_dict = {"id": sfp_id}
2175 sfp_list = self.get_sfp_list(filter_dict)
2176 if len(sfp_list) == 0:
2177 raise vimconn.vimconnNotFoundException(
2178 "Service Function Path '{}' not found".format(sfp_id))
2179 elif len(sfp_list) > 1:
2180 raise vimconn.vimconnConflictException(
2181 "Found more than one Service Function Path with this criteria")
2182 sfp = sfp_list[0]
2183 return sfp
2184
2185 def get_sfp_list(self, filter_dict={}):
2186 self.logger.debug("Getting Service Function Paths from VIM filter: "
2187 "'%s'", str(filter_dict))
2188 try:
2189 self._reload_connection()
tierno69b590e2018-03-13 18:52:23 +01002190 filter_dict_os = filter_dict.copy()
2191 if self.api_version3 and "tenant_id" in filter_dict_os:
2192 filter_dict_os['project_id'] = filter_dict_os.pop('tenant_id')
2193 sfp_dict = self.neutron.list_sfc_port_chains(**filter_dict_os)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00002194 sfp_list = sfp_dict["port_chains"]
2195 self.__sfp_os2mano(sfp_list)
2196 return sfp_list
2197 except (neExceptions.ConnectionFailed, ksExceptions.ClientException,
2198 neExceptions.NeutronException, ConnectionError) as e:
2199 self._format_exception(e)
2200
2201 def delete_sfp(self, sfp_id):
2202 self.logger.debug(
2203 "Deleting Service Function Path '%s' from VIM", sfp_id)
2204 try:
2205 self._reload_connection()
Igor D.Ccaadc442017-11-06 12:48:48 +00002206 self.neutron.delete_sfc_port_chain(sfp_id)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00002207 return sfp_id
2208 except (neExceptions.ConnectionFailed, neExceptions.NeutronException,
2209 ksExceptions.ClientException, neExceptions.NeutronException,
2210 ConnectionError) as e:
2211 self._format_exception(e)