blob: 876fa2f4fcdfad324848142da3b4920854507cd8 [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
112 self._validate_vlan_ranges(config.get('dataplane_net_vlan_range'))
113
tiernob5cef372017-06-19 15:52:22 +0200114 vimconn.vimconnector.__init__(self, uuid, name, tenant_id, tenant_name, url, url_admin, user, passwd, log_level,
115 config)
tiernob3d36742017-03-03 23:51:05 +0100116
tierno4d1ce222018-04-06 10:41:06 +0200117 if self.config.get("insecure") and self.config.get("ca_cert"):
118 raise vimconn.vimconnException("options insecure and ca_cert are mutually exclusive")
119 self.verify = True
120 if self.config.get("insecure"):
121 self.verify = False
122 if self.config.get("ca_cert"):
123 self.verify = self.config.get("ca_cert")
tierno4d1ce222018-04-06 10:41:06 +0200124
tierno7edb6752016-03-21 17:37:52 +0100125 if not url:
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +0000126 raise TypeError('url param can not be NoneType')
tiernob5cef372017-06-19 15:52:22 +0200127 self.persistent_info = persistent_info
mirabal29356312017-07-27 12:21:22 +0200128 self.availability_zone = persistent_info.get('availability_zone', None)
tiernob5cef372017-06-19 15:52:22 +0200129 self.session = persistent_info.get('session', {'reload_client': True})
130 self.nova = self.session.get('nova')
131 self.neutron = self.session.get('neutron')
132 self.cinder = self.session.get('cinder')
133 self.glance = self.session.get('glance')
tierno1beea862018-07-11 15:47:37 +0200134 # self.glancev1 = self.session.get('glancev1')
tiernof716aea2017-06-21 18:01:40 +0200135 self.keystone = self.session.get('keystone')
136 self.api_version3 = self.session.get('api_version3')
kate721d79b2017-06-24 04:21:38 -0700137 self.vim_type = self.config.get("vim_type")
138 if self.vim_type:
139 self.vim_type = self.vim_type.upper()
140 if self.config.get("use_internal_endpoint"):
141 self.endpoint_type = "internalURL"
142 else:
143 self.endpoint_type = None
montesmoreno0c8def02016-12-22 12:16:23 +0000144
tierno73ad9e42016-09-12 18:11:11 +0200145 self.logger = logging.getLogger('openmano.vim.openstack')
kate721d79b2017-06-24 04:21:38 -0700146
147 ####### VIO Specific Changes #########
148 if self.vim_type == "VIO":
149 self.logger = logging.getLogger('openmano.vim.vio')
150
tiernofe789902016-09-29 14:20:44 +0000151 if log_level:
kate54616752017-09-05 23:26:28 -0700152 self.logger.setLevel( getattr(logging, log_level))
tiernof716aea2017-06-21 18:01:40 +0200153
154 def __getitem__(self, index):
155 """Get individuals parameters.
156 Throw KeyError"""
157 if index == 'project_domain_id':
158 return self.config.get("project_domain_id")
159 elif index == 'user_domain_id':
160 return self.config.get("user_domain_id")
161 else:
tierno76a3c312017-06-29 16:42:15 +0200162 return vimconn.vimconnector.__getitem__(self, index)
tiernof716aea2017-06-21 18:01:40 +0200163
164 def __setitem__(self, index, value):
165 """Set individuals parameters and it is marked as dirty so to force connection reload.
166 Throw KeyError"""
167 if index == 'project_domain_id':
168 self.config["project_domain_id"] = value
169 elif index == 'user_domain_id':
170 self.config["user_domain_id"] = value
171 else:
172 vimconn.vimconnector.__setitem__(self, index, value)
tiernob5cef372017-06-19 15:52:22 +0200173 self.session['reload_client'] = True
tiernof716aea2017-06-21 18:01:40 +0200174
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100175 def serialize(self, value):
176 """Serialization of python basic types.
177
178 In the case value is not serializable a message will be logged and a
179 simple representation of the data that cannot be converted back to
180 python is returned.
181 """
182 if isinstance(value, StringTypes):
183 return value
184
185 try:
186 return yaml.dump(value, Dumper=SafeDumper,
187 default_flow_style=True, width=256)
188 except yaml.representer.RepresenterError:
189 self.logger.debug(
190 'The following entity cannot be serialized in YAML:'
191 '\n\n%s\n\n', pformat(value), exc_info=True)
192 return str(value)
193
tierno7edb6752016-03-21 17:37:52 +0100194 def _reload_connection(self):
195 '''Called before any operation, it check if credentials has changed
196 Throw keystoneclient.apiclient.exceptions.AuthorizationFailure
197 '''
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100198 #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 +0200199 if self.session['reload_client']:
tiernof716aea2017-06-21 18:01:40 +0200200 if self.config.get('APIversion'):
201 self.api_version3 = self.config['APIversion'] == 'v3.3' or self.config['APIversion'] == '3'
202 else: # get from ending auth_url that end with v3 or with v2.0
tierno3cb8dc32017-10-24 18:13:19 +0200203 self.api_version3 = self.url.endswith("/v3") or self.url.endswith("/v3/")
tiernof716aea2017-06-21 18:01:40 +0200204 self.session['api_version3'] = self.api_version3
205 if self.api_version3:
tierno3cb8dc32017-10-24 18:13:19 +0200206 if self.config.get('project_domain_id') or self.config.get('project_domain_name'):
207 project_domain_id_default = None
208 else:
209 project_domain_id_default = 'default'
210 if self.config.get('user_domain_id') or self.config.get('user_domain_name'):
211 user_domain_id_default = None
212 else:
213 user_domain_id_default = 'default'
tiernof716aea2017-06-21 18:01:40 +0200214 auth = v3.Password(auth_url=self.url,
tiernob5cef372017-06-19 15:52:22 +0200215 username=self.user,
216 password=self.passwd,
217 project_name=self.tenant_name,
218 project_id=self.tenant_id,
tierno3cb8dc32017-10-24 18:13:19 +0200219 project_domain_id=self.config.get('project_domain_id', project_domain_id_default),
220 user_domain_id=self.config.get('user_domain_id', user_domain_id_default),
221 project_domain_name=self.config.get('project_domain_name'),
222 user_domain_name=self.config.get('user_domain_name'))
ahmadsa95baa272016-11-30 09:14:11 +0500223 else:
tiernof716aea2017-06-21 18:01:40 +0200224 auth = v2.Password(auth_url=self.url,
tiernob5cef372017-06-19 15:52:22 +0200225 username=self.user,
226 password=self.passwd,
227 tenant_name=self.tenant_name,
228 tenant_id=self.tenant_id)
tierno4d1ce222018-04-06 10:41:06 +0200229 sess = session.Session(auth=auth, verify=self.verify)
tiernof716aea2017-06-21 18:01:40 +0200230 if self.api_version3:
kate721d79b2017-06-24 04:21:38 -0700231 self.keystone = ksClient_v3.Client(session=sess, endpoint_type=self.endpoint_type)
tiernof716aea2017-06-21 18:01:40 +0200232 else:
kate721d79b2017-06-24 04:21:38 -0700233 self.keystone = ksClient_v2.Client(session=sess, endpoint_type=self.endpoint_type)
tiernof716aea2017-06-21 18:01:40 +0200234 self.session['keystone'] = self.keystone
montesmoreno9317d302017-08-16 12:48:23 +0200235 # In order to enable microversion functionality an explicit microversion must be specified in 'config'.
236 # This implementation approach is due to the warning message in
237 # https://developer.openstack.org/api-guide/compute/microversions.html
238 # where it is stated that microversion backwards compatibility is not guaranteed and clients should
239 # always require an specific microversion.
240 # To be able to use 'device role tagging' functionality define 'microversion: 2.32' in datacenter config
241 version = self.config.get("microversion")
242 if not version:
243 version = "2.1"
kate54616752017-09-05 23:26:28 -0700244 self.nova = self.session['nova'] = nClient.Client(str(version), session=sess, endpoint_type=self.endpoint_type)
kate721d79b2017-06-24 04:21:38 -0700245 self.neutron = self.session['neutron'] = neClient.Client('2.0', session=sess, endpoint_type=self.endpoint_type)
246 self.cinder = self.session['cinder'] = cClient.Client(2, session=sess, endpoint_type=self.endpoint_type)
247 if self.endpoint_type == "internalURL":
248 glance_service_id = self.keystone.services.list(name="glance")[0].id
249 glance_endpoint = self.keystone.endpoints.list(glance_service_id, interface="internal")[0].url
250 else:
251 glance_endpoint = None
252 self.glance = self.session['glance'] = glClient.Client(2, session=sess, endpoint=glance_endpoint)
253 #using version 1 of glance client in new_image()
tierno1beea862018-07-11 15:47:37 +0200254 # self.glancev1 = self.session['glancev1'] = glClient.Client('1', session=sess,
255 # endpoint=glance_endpoint)
tiernob5cef372017-06-19 15:52:22 +0200256 self.session['reload_client'] = False
257 self.persistent_info['session'] = self.session
mirabal29356312017-07-27 12:21:22 +0200258 # add availablity zone info inside self.persistent_info
259 self._set_availablity_zones()
260 self.persistent_info['availability_zone'] = self.availability_zone
ahmadsa95baa272016-11-30 09:14:11 +0500261
tierno7edb6752016-03-21 17:37:52 +0100262 def __net_os2mano(self, net_list_dict):
263 '''Transform the net openstack format to mano format
264 net_list_dict can be a list of dict or a single dict'''
265 if type(net_list_dict) is dict:
266 net_list_=(net_list_dict,)
267 elif type(net_list_dict) is list:
268 net_list_=net_list_dict
269 else:
270 raise TypeError("param net_list_dict must be a list or a dictionary")
271 for net in net_list_:
272 if net.get('provider:network_type') == "vlan":
273 net['type']='data'
274 else:
275 net['type']='bridge'
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +0200276
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +0000277 def __classification_os2mano(self, class_list_dict):
278 """Transform the openstack format (Flow Classifier) to mano format
279 (Classification) class_list_dict can be a list of dict or a single dict
280 """
281 if isinstance(class_list_dict, dict):
282 class_list_ = [class_list_dict]
283 elif isinstance(class_list_dict, list):
284 class_list_ = class_list_dict
285 else:
286 raise TypeError(
287 "param class_list_dict must be a list or a dictionary")
288 for classification in class_list_:
289 id = classification.pop('id')
290 name = classification.pop('name')
291 description = classification.pop('description')
292 project_id = classification.pop('project_id')
293 tenant_id = classification.pop('tenant_id')
294 original_classification = copy.deepcopy(classification)
295 classification.clear()
296 classification['ctype'] = 'legacy_flow_classifier'
297 classification['definition'] = original_classification
298 classification['id'] = id
299 classification['name'] = name
300 classification['description'] = description
301 classification['project_id'] = project_id
302 classification['tenant_id'] = tenant_id
303
304 def __sfi_os2mano(self, sfi_list_dict):
305 """Transform the openstack format (Port Pair) to mano format (SFI)
306 sfi_list_dict can be a list of dict or a single dict
307 """
308 if isinstance(sfi_list_dict, dict):
309 sfi_list_ = [sfi_list_dict]
310 elif isinstance(sfi_list_dict, list):
311 sfi_list_ = sfi_list_dict
312 else:
313 raise TypeError(
314 "param sfi_list_dict must be a list or a dictionary")
315 for sfi in sfi_list_:
316 sfi['ingress_ports'] = []
317 sfi['egress_ports'] = []
318 if sfi.get('ingress'):
319 sfi['ingress_ports'].append(sfi['ingress'])
320 if sfi.get('egress'):
321 sfi['egress_ports'].append(sfi['egress'])
322 del sfi['ingress']
323 del sfi['egress']
324 params = sfi.get('service_function_parameters')
325 sfc_encap = False
326 if params:
327 correlation = params.get('correlation')
328 if correlation:
329 sfc_encap = True
330 sfi['sfc_encap'] = sfc_encap
331 del sfi['service_function_parameters']
332
333 def __sf_os2mano(self, sf_list_dict):
334 """Transform the openstack format (Port Pair Group) to mano format (SF)
335 sf_list_dict can be a list of dict or a single dict
336 """
337 if isinstance(sf_list_dict, dict):
338 sf_list_ = [sf_list_dict]
339 elif isinstance(sf_list_dict, list):
340 sf_list_ = sf_list_dict
341 else:
342 raise TypeError(
343 "param sf_list_dict must be a list or a dictionary")
344 for sf in sf_list_:
345 del sf['port_pair_group_parameters']
346 sf['sfis'] = sf['port_pairs']
347 del sf['port_pairs']
348
349 def __sfp_os2mano(self, sfp_list_dict):
350 """Transform the openstack format (Port Chain) to mano format (SFP)
351 sfp_list_dict can be a list of dict or a single dict
352 """
353 if isinstance(sfp_list_dict, dict):
354 sfp_list_ = [sfp_list_dict]
355 elif isinstance(sfp_list_dict, list):
356 sfp_list_ = sfp_list_dict
357 else:
358 raise TypeError(
359 "param sfp_list_dict must be a list or a dictionary")
360 for sfp in sfp_list_:
361 params = sfp.pop('chain_parameters')
362 sfc_encap = False
363 if params:
364 correlation = params.get('correlation')
365 if correlation:
366 sfc_encap = True
367 sfp['sfc_encap'] = sfc_encap
368 sfp['spi'] = sfp.pop('chain_id')
369 sfp['classifications'] = sfp.pop('flow_classifiers')
370 sfp['service_functions'] = sfp.pop('port_pair_groups')
371
372 # placeholder for now; read TODO note below
373 def _validate_classification(self, type, definition):
374 # only legacy_flow_classifier Type is supported at this point
375 return True
376 # TODO(igordcard): this method should be an abstract method of an
377 # abstract Classification class to be implemented by the specific
378 # Types. Also, abstract vimconnector should call the validation
379 # method before the implemented VIM connectors are called.
380
tiernoae4a8d12016-07-08 12:30:39 +0200381 def _format_exception(self, exception):
382 '''Transform a keystone, nova, neutron exception into a vimconn exception'''
shashankjain3c83a212018-10-04 13:05:46 +0530383 if isinstance(exception, (neExceptions.NetworkNotFoundClient, nvExceptions.NotFound, ksExceptions.NotFound, gl1Exceptions.HTTPNotFound)):
tiernoae4a8d12016-07-08 12:30:39 +0200384 raise vimconn.vimconnNotFoundException(type(exception).__name__ + ": " + str(exception))
shashankjain3c83a212018-10-04 13:05:46 +0530385 elif isinstance(exception, (HTTPException, gl1Exceptions.HTTPException, gl1Exceptions.CommunicationError,
386 ConnectionError, ksExceptions.ConnectionError, neExceptions.ConnectionFailed)):
387 raise vimconn.vimconnConnectionException(type(exception).__name__ + ": " + str(exception))
388 elif isinstance(exception, (KeyError, nvExceptions.BadRequest, ksExceptions.BadRequest)):
anwarsc76a3ee2018-10-04 14:05:32 +0530389 raise vimconn.vimconnException(type(exception).__name__ + ": " + str(exception))
390 elif isinstance(exception, (nvExceptions.ClientException, ksExceptions.ClientException,
391 neExceptions.NeutronException)):
392 raise vimconn.vimconnUnexpectedResponse(type(exception).__name__ + ": " + str(exception))
tiernoae4a8d12016-07-08 12:30:39 +0200393 elif isinstance(exception, nvExceptions.Conflict):
394 raise vimconn.vimconnConflictException(type(exception).__name__ + ": " + str(exception))
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +0200395 elif isinstance(exception, vimconn.vimconnException):
tierno41a69812018-02-16 14:34:33 +0100396 raise exception
tiernof716aea2017-06-21 18:01:40 +0200397 else: # ()
tiernob84cbdc2017-07-07 14:30:30 +0200398 self.logger.error("General Exception " + str(exception), exc_info=True)
tiernoae4a8d12016-07-08 12:30:39 +0200399 raise vimconn.vimconnConnectionException(type(exception).__name__ + ": " + str(exception))
400
401 def get_tenant_list(self, filter_dict={}):
402 '''Obtain tenants of VIM
403 filter_dict can contain the following keys:
404 name: filter by tenant name
405 id: filter by tenant uuid/id
406 <other VIM specific>
407 Returns the tenant list of dictionaries: [{'name':'<name>, 'id':'<id>, ...}, ...]
408 '''
ahmadsa95baa272016-11-30 09:14:11 +0500409 self.logger.debug("Getting tenants from VIM filter: '%s'", str(filter_dict))
tiernoae4a8d12016-07-08 12:30:39 +0200410 try:
411 self._reload_connection()
tiernof716aea2017-06-21 18:01:40 +0200412 if self.api_version3:
413 project_class_list = self.keystone.projects.list(name=filter_dict.get("name"))
ahmadsa95baa272016-11-30 09:14:11 +0500414 else:
tiernof716aea2017-06-21 18:01:40 +0200415 project_class_list = self.keystone.tenants.findall(**filter_dict)
ahmadsa95baa272016-11-30 09:14:11 +0500416 project_list=[]
417 for project in project_class_list:
tiernof716aea2017-06-21 18:01:40 +0200418 if filter_dict.get('id') and filter_dict["id"] != project.id:
419 continue
ahmadsa95baa272016-11-30 09:14:11 +0500420 project_list.append(project.to_dict())
421 return project_list
tiernof716aea2017-06-21 18:01:40 +0200422 except (ksExceptions.ConnectionError, ksExceptions.ClientException, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200423 self._format_exception(e)
424
425 def new_tenant(self, tenant_name, tenant_description):
426 '''Adds a new tenant to openstack VIM. Returns the tenant identifier'''
427 self.logger.debug("Adding a new tenant name: %s", tenant_name)
428 try:
429 self._reload_connection()
tiernof716aea2017-06-21 18:01:40 +0200430 if self.api_version3:
431 project = self.keystone.projects.create(tenant_name, self.config.get("project_domain_id", "default"),
432 description=tenant_description, is_domain=False)
ahmadsa95baa272016-11-30 09:14:11 +0500433 else:
tiernof716aea2017-06-21 18:01:40 +0200434 project = self.keystone.tenants.create(tenant_name, tenant_description)
ahmadsa95baa272016-11-30 09:14:11 +0500435 return project.id
shashankjain3c83a212018-10-04 13:05:46 +0530436 except (ksExceptions.ConnectionError, ksExceptions.ClientException, ksExceptions.BadRequest, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200437 self._format_exception(e)
438
439 def delete_tenant(self, tenant_id):
440 '''Delete a tenant from openstack VIM. Returns the old tenant identifier'''
441 self.logger.debug("Deleting tenant %s from VIM", tenant_id)
442 try:
443 self._reload_connection()
tiernof716aea2017-06-21 18:01:40 +0200444 if self.api_version3:
ahmadsa95baa272016-11-30 09:14:11 +0500445 self.keystone.projects.delete(tenant_id)
446 else:
447 self.keystone.tenants.delete(tenant_id)
tiernoae4a8d12016-07-08 12:30:39 +0200448 return tenant_id
shashankjain3c83a212018-10-04 13:05:46 +0530449 except (ksExceptions.ConnectionError, ksExceptions.ClientException, ksExceptions.NotFound, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200450 self._format_exception(e)
ahmadsa95baa272016-11-30 09:14:11 +0500451
garciadeblas9f8456e2016-09-05 05:02:59 +0200452 def new_network(self,net_name, net_type, ip_profile=None, shared=False, vlan=None):
tiernoae4a8d12016-07-08 12:30:39 +0200453 '''Adds a tenant network to VIM. Returns the network identifier'''
454 self.logger.debug("Adding a new network to VIM name '%s', type '%s'", net_name, net_type)
garciadeblasedca7b32016-09-29 14:01:52 +0000455 #self.logger.debug(">>>>>>>>>>>>>>>>>> IP profile %s", str(ip_profile))
tierno7edb6752016-03-21 17:37:52 +0100456 try:
garciadeblasedca7b32016-09-29 14:01:52 +0000457 new_net = None
tierno7edb6752016-03-21 17:37:52 +0100458 self._reload_connection()
459 network_dict = {'name': net_name, 'admin_state_up': True}
460 if net_type=="data" or net_type=="ptp":
461 if self.config.get('dataplane_physical_net') == None:
tiernoae4a8d12016-07-08 12:30:39 +0200462 raise vimconn.vimconnConflictException("You must provide a 'dataplane_physical_net' at config value before creating sriov network")
tierno7edb6752016-03-21 17:37:52 +0100463 network_dict["provider:physical_network"] = self.config['dataplane_physical_net'] #"physnet_sriov" #TODO physical
464 network_dict["provider:network_type"] = "vlan"
465 if vlan!=None:
466 network_dict["provider:network_type"] = vlan
kate721d79b2017-06-24 04:21:38 -0700467
468 ####### VIO Specific Changes #########
469 if self.vim_type == "VIO":
470 if vlan is not None:
471 network_dict["provider:segmentation_id"] = vlan
472 else:
473 if self.config.get('dataplane_net_vlan_range') is None:
474 raise vimconn.vimconnConflictException("You must provide "\
475 "'dataplane_net_vlan_range' in format [start_ID - end_ID]"\
476 "at config value before creating sriov network with vlan tag")
477
478 network_dict["provider:segmentation_id"] = self._genrate_vlanID()
479
tiernoae4a8d12016-07-08 12:30:39 +0200480 network_dict["shared"]=shared
tierno7edb6752016-03-21 17:37:52 +0100481 new_net=self.neutron.create_network({'network':network_dict})
482 #print new_net
garciadeblas9f8456e2016-09-05 05:02:59 +0200483 #create subnetwork, even if there is no profile
484 if not ip_profile:
485 ip_profile = {}
tierno41a69812018-02-16 14:34:33 +0100486 if not ip_profile.get('subnet_address'):
garciadeblas2299e3b2017-01-26 14:35:55 +0000487 #Fake subnet is required
488 subnet_rand = random.randint(0, 255)
489 ip_profile['subnet_address'] = "192.168.{}.0/24".format(subnet_rand)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +0000490 if 'ip_version' not in ip_profile:
garciadeblas9f8456e2016-09-05 05:02:59 +0200491 ip_profile['ip_version'] = "IPv4"
tiernoa1fb4462017-06-30 12:25:50 +0200492 subnet = {"name":net_name+"-subnet",
tierno7edb6752016-03-21 17:37:52 +0100493 "network_id": new_net["network"]["id"],
garciadeblas9f8456e2016-09-05 05:02:59 +0200494 "ip_version": 4 if ip_profile['ip_version']=="IPv4" else 6,
495 "cidr": ip_profile['subnet_address']
tierno7edb6752016-03-21 17:37:52 +0100496 }
tiernoa1fb4462017-06-30 12:25:50 +0200497 # Gateway should be set to None if not needed. Otherwise openstack assigns one by default
tierno41a69812018-02-16 14:34:33 +0100498 if ip_profile.get('gateway_address'):
tierno55d234c2018-07-04 18:29:21 +0200499 subnet['gateway_ip'] = ip_profile['gateway_address']
500 else:
501 subnet['gateway_ip'] = None
garciadeblasedca7b32016-09-29 14:01:52 +0000502 if ip_profile.get('dns_address'):
tierno455612d2017-05-30 16:40:10 +0200503 subnet['dns_nameservers'] = ip_profile['dns_address'].split(";")
garciadeblas9f8456e2016-09-05 05:02:59 +0200504 if 'dhcp_enabled' in ip_profile:
tierno41a69812018-02-16 14:34:33 +0100505 subnet['enable_dhcp'] = False if \
506 ip_profile['dhcp_enabled']=="false" or ip_profile['dhcp_enabled']==False else True
507 if ip_profile.get('dhcp_start_address'):
tiernoa1fb4462017-06-30 12:25:50 +0200508 subnet['allocation_pools'] = []
garciadeblas9f8456e2016-09-05 05:02:59 +0200509 subnet['allocation_pools'].append(dict())
510 subnet['allocation_pools'][0]['start'] = ip_profile['dhcp_start_address']
tierno41a69812018-02-16 14:34:33 +0100511 if ip_profile.get('dhcp_count'):
garciadeblas9f8456e2016-09-05 05:02:59 +0200512 #parts = ip_profile['dhcp_start_address'].split('.')
513 #ip_int = (int(parts[0]) << 24) + (int(parts[1]) << 16) + (int(parts[2]) << 8) + int(parts[3])
514 ip_int = int(netaddr.IPAddress(ip_profile['dhcp_start_address']))
garciadeblas21d795b2016-09-29 17:31:46 +0200515 ip_int += ip_profile['dhcp_count'] - 1
garciadeblas9f8456e2016-09-05 05:02:59 +0200516 ip_str = str(netaddr.IPAddress(ip_int))
517 subnet['allocation_pools'][0]['end'] = ip_str
garciadeblasedca7b32016-09-29 14:01:52 +0000518 #self.logger.debug(">>>>>>>>>>>>>>>>>> Subnet: %s", str(subnet))
tierno7edb6752016-03-21 17:37:52 +0100519 self.neutron.create_subnet({"subnet": subnet} )
tiernoae4a8d12016-07-08 12:30:39 +0200520 return new_net["network"]["id"]
tierno41a69812018-02-16 14:34:33 +0100521 except Exception as e:
garciadeblasedca7b32016-09-29 14:01:52 +0000522 if new_net:
523 self.neutron.delete_network(new_net['network']['id'])
tiernoae4a8d12016-07-08 12:30:39 +0200524 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100525
526 def get_network_list(self, filter_dict={}):
527 '''Obtain tenant networks of VIM
528 Filter_dict can be:
529 name: network name
530 id: network uuid
531 shared: boolean
532 tenant_id: tenant
533 admin_state_up: boolean
534 status: 'ACTIVE'
535 Returns the network list of dictionaries
536 '''
tiernoae4a8d12016-07-08 12:30:39 +0200537 self.logger.debug("Getting network from VIM filter: '%s'", str(filter_dict))
tierno7edb6752016-03-21 17:37:52 +0100538 try:
539 self._reload_connection()
tierno69b590e2018-03-13 18:52:23 +0100540 filter_dict_os = filter_dict.copy()
541 if self.api_version3 and "tenant_id" in filter_dict_os:
542 filter_dict_os['project_id'] = filter_dict_os.pop('tenant_id') #T ODO check
543 net_dict = self.neutron.list_networks(**filter_dict_os)
tierno00e3df72017-11-29 17:20:13 +0100544 net_list = net_dict["networks"]
tierno7edb6752016-03-21 17:37:52 +0100545 self.__net_os2mano(net_list)
tiernoae4a8d12016-07-08 12:30:39 +0200546 return net_list
tierno8e995ce2016-09-22 08:13:00 +0000547 except (neExceptions.ConnectionFailed, ksExceptions.ClientException, neExceptions.NeutronException, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200548 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100549
tiernoae4a8d12016-07-08 12:30:39 +0200550 def get_network(self, net_id):
551 '''Obtain details of network from VIM
552 Returns the network information from a network id'''
553 self.logger.debug(" Getting tenant network %s from VIM", net_id)
tierno7edb6752016-03-21 17:37:52 +0100554 filter_dict={"id": net_id}
tiernoae4a8d12016-07-08 12:30:39 +0200555 net_list = self.get_network_list(filter_dict)
tierno7edb6752016-03-21 17:37:52 +0100556 if len(net_list)==0:
tiernoae4a8d12016-07-08 12:30:39 +0200557 raise vimconn.vimconnNotFoundException("Network '{}' not found".format(net_id))
tierno7edb6752016-03-21 17:37:52 +0100558 elif len(net_list)>1:
tiernoae4a8d12016-07-08 12:30:39 +0200559 raise vimconn.vimconnConflictException("Found more than one network with this criteria")
tierno7edb6752016-03-21 17:37:52 +0100560 net = net_list[0]
561 subnets=[]
562 for subnet_id in net.get("subnets", () ):
563 try:
564 subnet = self.neutron.show_subnet(subnet_id)
565 except Exception as e:
tiernoae4a8d12016-07-08 12:30:39 +0200566 self.logger.error("osconnector.get_network(): Error getting subnet %s %s" % (net_id, str(e)))
567 subnet = {"id": subnet_id, "fault": str(e)}
tierno7edb6752016-03-21 17:37:52 +0100568 subnets.append(subnet)
569 net["subnets"] = subnets
Pablo Montes Moreno51e553b2017-03-23 16:39:12 +0100570 net["encapsulation"] = net.get('provider:network_type')
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +0100571 net["segmentation_id"] = net.get('provider:segmentation_id')
tiernoae4a8d12016-07-08 12:30:39 +0200572 return net
tierno7edb6752016-03-21 17:37:52 +0100573
tiernoae4a8d12016-07-08 12:30:39 +0200574 def delete_network(self, net_id):
575 '''Deletes a tenant network from VIM. Returns the old network identifier'''
576 self.logger.debug("Deleting network '%s' from VIM", net_id)
tierno7edb6752016-03-21 17:37:52 +0100577 try:
578 self._reload_connection()
579 #delete VM ports attached to this networks before the network
580 ports = self.neutron.list_ports(network_id=net_id)
581 for p in ports['ports']:
582 try:
583 self.neutron.delete_port(p["id"])
584 except Exception as e:
tiernoae4a8d12016-07-08 12:30:39 +0200585 self.logger.error("Error deleting port %s: %s", p["id"], str(e))
tierno7edb6752016-03-21 17:37:52 +0100586 self.neutron.delete_network(net_id)
tiernoae4a8d12016-07-08 12:30:39 +0200587 return net_id
588 except (neExceptions.ConnectionFailed, neExceptions.NetworkNotFoundClient, neExceptions.NeutronException,
tierno8e995ce2016-09-22 08:13:00 +0000589 ksExceptions.ClientException, neExceptions.NeutronException, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200590 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100591
tiernoae4a8d12016-07-08 12:30:39 +0200592 def refresh_nets_status(self, net_list):
593 '''Get the status of the networks
594 Params: the list of network identifiers
595 Returns a dictionary with:
596 net_id: #VIM id of this network
597 status: #Mandatory. Text with one of:
598 # DELETED (not found at vim)
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100599 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
tiernoae4a8d12016-07-08 12:30:39 +0200600 # OTHER (Vim reported other status not understood)
601 # ERROR (VIM indicates an ERROR status)
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100602 # ACTIVE, INACTIVE, DOWN (admin down),
tiernoae4a8d12016-07-08 12:30:39 +0200603 # BUILD (on building process)
604 #
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100605 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
tiernoae4a8d12016-07-08 12:30:39 +0200606 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
607
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +0000608 '''
tiernoae4a8d12016-07-08 12:30:39 +0200609 net_dict={}
610 for net_id in net_list:
611 net = {}
612 try:
613 net_vim = self.get_network(net_id)
614 if net_vim['status'] in netStatus2manoFormat:
615 net["status"] = netStatus2manoFormat[ net_vim['status'] ]
616 else:
617 net["status"] = "OTHER"
618 net["error_msg"] = "VIM status reported " + net_vim['status']
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +0000619
tierno8e995ce2016-09-22 08:13:00 +0000620 if net['status'] == "ACTIVE" and not net_vim['admin_state_up']:
tiernoae4a8d12016-07-08 12:30:39 +0200621 net['status'] = 'DOWN'
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100622
623 net['vim_info'] = self.serialize(net_vim)
624
tiernoae4a8d12016-07-08 12:30:39 +0200625 if net_vim.get('fault'): #TODO
626 net['error_msg'] = str(net_vim['fault'])
627 except vimconn.vimconnNotFoundException as e:
628 self.logger.error("Exception getting net status: %s", str(e))
629 net['status'] = "DELETED"
630 net['error_msg'] = str(e)
631 except vimconn.vimconnException as e:
632 self.logger.error("Exception getting net status: %s", str(e))
633 net['status'] = "VIM_ERROR"
634 net['error_msg'] = str(e)
635 net_dict[net_id] = net
636 return net_dict
637
638 def get_flavor(self, flavor_id):
639 '''Obtain flavor details from the VIM. Returns the flavor dict details'''
640 self.logger.debug("Getting flavor '%s'", flavor_id)
tierno7edb6752016-03-21 17:37:52 +0100641 try:
642 self._reload_connection()
643 flavor = self.nova.flavors.find(id=flavor_id)
644 #TODO parse input and translate to VIM format (openmano_schemas.new_vminstance_response_schema)
tiernoae4a8d12016-07-08 12:30:39 +0200645 return flavor.to_dict()
tierno8e995ce2016-09-22 08:13:00 +0000646 except (nvExceptions.NotFound, nvExceptions.ClientException, ksExceptions.ClientException, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200647 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100648
tiernocf157a82017-01-30 14:07:06 +0100649 def get_flavor_id_from_data(self, flavor_dict):
650 """Obtain flavor id that match the flavor description
651 Returns the flavor_id or raises a vimconnNotFoundException
tiernoe26fc7a2017-05-30 14:43:03 +0200652 flavor_dict: contains the required ram, vcpus, disk
653 If 'use_existing_flavors' is set to True at config, the closer flavor that provides same or more ram, vcpus
654 and disk is returned. Otherwise a flavor with exactly same ram, vcpus and disk is returned or a
655 vimconnNotFoundException is raised
tiernocf157a82017-01-30 14:07:06 +0100656 """
tiernoe26fc7a2017-05-30 14:43:03 +0200657 exact_match = False if self.config.get('use_existing_flavors') else True
tiernocf157a82017-01-30 14:07:06 +0100658 try:
659 self._reload_connection()
tiernoe26fc7a2017-05-30 14:43:03 +0200660 flavor_candidate_id = None
661 flavor_candidate_data = (10000, 10000, 10000)
662 flavor_target = (flavor_dict["ram"], flavor_dict["vcpus"], flavor_dict["disk"])
663 # numa=None
664 numas = flavor_dict.get("extended", {}).get("numas")
tiernocf157a82017-01-30 14:07:06 +0100665 if numas:
666 #TODO
667 raise vimconn.vimconnNotFoundException("Flavor with EPA still not implemted")
668 # if len(numas) > 1:
669 # raise vimconn.vimconnNotFoundException("Cannot find any flavor with more than one numa")
670 # numa=numas[0]
671 # numas = extended.get("numas")
672 for flavor in self.nova.flavors.list():
673 epa = flavor.get_keys()
674 if epa:
675 continue
tiernoe26fc7a2017-05-30 14:43:03 +0200676 # TODO
677 flavor_data = (flavor.ram, flavor.vcpus, flavor.disk)
678 if flavor_data == flavor_target:
679 return flavor.id
680 elif not exact_match and flavor_target < flavor_data < flavor_candidate_data:
681 flavor_candidate_id = flavor.id
682 flavor_candidate_data = flavor_data
683 if not exact_match and flavor_candidate_id:
684 return flavor_candidate_id
tiernocf157a82017-01-30 14:07:06 +0100685 raise vimconn.vimconnNotFoundException("Cannot find any flavor matching '{}'".format(str(flavor_dict)))
686 except (nvExceptions.NotFound, nvExceptions.ClientException, ksExceptions.ClientException, ConnectionError) as e:
687 self._format_exception(e)
688
tiernoae4a8d12016-07-08 12:30:39 +0200689 def new_flavor(self, flavor_data, change_name_if_used=True):
tierno7edb6752016-03-21 17:37:52 +0100690 '''Adds a tenant flavor to openstack VIM
tiernoae4a8d12016-07-08 12:30:39 +0200691 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 +0100692 Returns the flavor identifier
693 '''
tiernoae4a8d12016-07-08 12:30:39 +0200694 self.logger.debug("Adding flavor '%s'", str(flavor_data))
tierno7edb6752016-03-21 17:37:52 +0100695 retry=0
tiernoae4a8d12016-07-08 12:30:39 +0200696 max_retries=3
tierno7edb6752016-03-21 17:37:52 +0100697 name_suffix = 0
anwarsc76a3ee2018-10-04 14:05:32 +0530698 try:
699 name=flavor_data['name']
700 while retry<max_retries:
701 retry+=1
702 try:
703 self._reload_connection()
704 if change_name_if_used:
705 #get used names
706 fl_names=[]
707 fl=self.nova.flavors.list()
708 for f in fl:
709 fl_names.append(f.name)
710 while name in fl_names:
711 name_suffix += 1
712 name = flavor_data['name']+"-" + str(name_suffix)
kate721d79b2017-06-24 04:21:38 -0700713
anwarsc76a3ee2018-10-04 14:05:32 +0530714 ram = flavor_data.get('ram',64)
715 vcpus = flavor_data.get('vcpus',1)
716 numa_properties=None
tierno7edb6752016-03-21 17:37:52 +0100717
anwarsc76a3ee2018-10-04 14:05:32 +0530718 extended = flavor_data.get("extended")
719 if extended:
720 numas=extended.get("numas")
721 if numas:
722 numa_nodes = len(numas)
723 if numa_nodes > 1:
724 return -1, "Can not add flavor with more than one numa"
725 numa_properties = {"hw:numa_nodes":str(numa_nodes)}
726 numa_properties["hw:mem_page_size"] = "large"
727 numa_properties["hw:cpu_policy"] = "dedicated"
728 numa_properties["hw:numa_mempolicy"] = "strict"
729 if self.vim_type == "VIO":
730 numa_properties["vmware:extra_config"] = '{"numa.nodeAffinity":"0"}'
731 numa_properties["vmware:latency_sensitivity_level"] = "high"
732 for numa in numas:
733 #overwrite ram and vcpus
734 #check if key 'memory' is present in numa else use ram value at flavor
735 if 'memory' in numa:
736 ram = numa['memory']*1024
737 #See for reference: https://specs.openstack.org/openstack/nova-specs/specs/mitaka/implemented/virt-driver-cpu-thread-pinning.html
738 if 'paired-threads' in numa:
739 vcpus = numa['paired-threads']*2
740 #cpu_thread_policy "require" implies that the compute node must have an STM architecture
741 numa_properties["hw:cpu_thread_policy"] = "require"
742 numa_properties["hw:cpu_policy"] = "dedicated"
743 elif 'cores' in numa:
744 vcpus = numa['cores']
745 # cpu_thread_policy "prefer" implies that the host must not have an SMT architecture, or a non-SMT architecture will be emulated
746 numa_properties["hw:cpu_thread_policy"] = "isolate"
747 numa_properties["hw:cpu_policy"] = "dedicated"
748 elif 'threads' in numa:
749 vcpus = numa['threads']
750 # cpu_thread_policy "prefer" implies that the host may or may not have an SMT architecture
751 numa_properties["hw:cpu_thread_policy"] = "prefer"
752 numa_properties["hw:cpu_policy"] = "dedicated"
753 # for interface in numa.get("interfaces",() ):
754 # if interface["dedicated"]=="yes":
755 # raise vimconn.vimconnException("Passthrough interfaces are not supported for the openstack connector", http_code=vimconn.HTTP_Service_Unavailable)
756 # #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 +0000757
anwarsc76a3ee2018-10-04 14:05:32 +0530758 #create flavor
759 new_flavor=self.nova.flavors.create(name,
760 ram,
761 vcpus,
762 flavor_data.get('disk',0),
763 is_public=flavor_data.get('is_public', True)
764 )
765 #add metadata
766 if numa_properties:
767 new_flavor.set_keys(numa_properties)
768 return new_flavor.id
769 except nvExceptions.Conflict as e:
770 if change_name_if_used and retry < max_retries:
771 continue
772 self._format_exception(e)
773 #except nvExceptions.BadRequest as e:
774 except (ksExceptions.ClientException, nvExceptions.ClientException, ConnectionError, KeyError) as e:
775 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100776
tiernoae4a8d12016-07-08 12:30:39 +0200777 def delete_flavor(self,flavor_id):
778 '''Deletes a tenant flavor from openstack VIM. Returns the old flavor_id
tierno7edb6752016-03-21 17:37:52 +0100779 '''
tiernoae4a8d12016-07-08 12:30:39 +0200780 try:
781 self._reload_connection()
782 self.nova.flavors.delete(flavor_id)
783 return flavor_id
784 #except nvExceptions.BadRequest as e:
tierno8e995ce2016-09-22 08:13:00 +0000785 except (nvExceptions.NotFound, ksExceptions.ClientException, nvExceptions.ClientException, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200786 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100787
tiernoae4a8d12016-07-08 12:30:39 +0200788 def new_image(self,image_dict):
tierno7edb6752016-03-21 17:37:52 +0100789 '''
tiernoae4a8d12016-07-08 12:30:39 +0200790 Adds a tenant image to VIM. imge_dict is a dictionary with:
791 name: name
792 disk_format: qcow2, vhd, vmdk, raw (by default), ...
793 location: path or URI
794 public: "yes" or "no"
795 metadata: metadata of the image
796 Returns the image_id
tierno7edb6752016-03-21 17:37:52 +0100797 '''
tiernoae4a8d12016-07-08 12:30:39 +0200798 retry=0
799 max_retries=3
800 while retry<max_retries:
tierno7edb6752016-03-21 17:37:52 +0100801 retry+=1
802 try:
803 self._reload_connection()
804 #determine format http://docs.openstack.org/developer/glance/formats.html
805 if "disk_format" in image_dict:
806 disk_format=image_dict["disk_format"]
garciadeblas14480452017-01-10 13:08:07 +0100807 else: #autodiscover based on extension
tierno1beea862018-07-11 15:47:37 +0200808 if image_dict['location'].endswith(".qcow2"):
tierno7edb6752016-03-21 17:37:52 +0100809 disk_format="qcow2"
tierno1beea862018-07-11 15:47:37 +0200810 elif image_dict['location'].endswith(".vhd"):
tierno7edb6752016-03-21 17:37:52 +0100811 disk_format="vhd"
tierno1beea862018-07-11 15:47:37 +0200812 elif image_dict['location'].endswith(".vmdk"):
tierno7edb6752016-03-21 17:37:52 +0100813 disk_format="vmdk"
tierno1beea862018-07-11 15:47:37 +0200814 elif image_dict['location'].endswith(".vdi"):
tierno7edb6752016-03-21 17:37:52 +0100815 disk_format="vdi"
tierno1beea862018-07-11 15:47:37 +0200816 elif image_dict['location'].endswith(".iso"):
tierno7edb6752016-03-21 17:37:52 +0100817 disk_format="iso"
tierno1beea862018-07-11 15:47:37 +0200818 elif image_dict['location'].endswith(".aki"):
tierno7edb6752016-03-21 17:37:52 +0100819 disk_format="aki"
tierno1beea862018-07-11 15:47:37 +0200820 elif image_dict['location'].endswith(".ari"):
tierno7edb6752016-03-21 17:37:52 +0100821 disk_format="ari"
tierno1beea862018-07-11 15:47:37 +0200822 elif image_dict['location'].endswith(".ami"):
tierno7edb6752016-03-21 17:37:52 +0100823 disk_format="ami"
824 else:
825 disk_format="raw"
tiernoae4a8d12016-07-08 12:30:39 +0200826 self.logger.debug("new_image: '%s' loading from '%s'", image_dict['name'], image_dict['location'])
shashankjain3c83a212018-10-04 13:05:46 +0530827 if self.vim_type == "VIO":
828 container_format = "bare"
829 if 'container_format' in image_dict:
830 container_format = image_dict['container_format']
831 new_image = self.glance.images.create(name=image_dict['name'], container_format=container_format,
832 disk_format=disk_format)
833 else:
834 new_image = self.glance.images.create(name=image_dict['name'])
tierno1beea862018-07-11 15:47:37 +0200835 if image_dict['location'].startswith("http"):
836 # TODO there is not a method to direct download. It must be downloaded locally with requests
837 raise vimconn.vimconnNotImplemented("Cannot create image from URL")
tierno7edb6752016-03-21 17:37:52 +0100838 else: #local path
839 with open(image_dict['location']) as fimage:
tierno1beea862018-07-11 15:47:37 +0200840 self.glance.images.upload(new_image.id, fimage)
841 #new_image = self.glancev1.images.create(name=image_dict['name'], is_public=image_dict.get('public',"yes")=="yes",
842 # container_format="bare", data=fimage, disk_format=disk_format)
tierno7edb6752016-03-21 17:37:52 +0100843 metadata_to_load = image_dict.get('metadata')
shashankjain3c83a212018-10-04 13:05:46 +0530844 # TODO location is a reserved word for current openstack versions. fixed for VIO please check for openstack
845 if self.vim_type == "VIO":
846 metadata_to_load['upload_location'] = image_dict['location']
847 else:
848 metadata_to_load['location'] = image_dict['location']
tierno1beea862018-07-11 15:47:37 +0200849 self.glance.images.update(new_image.id, **metadata_to_load)
tiernoae4a8d12016-07-08 12:30:39 +0200850 return new_image.id
851 except (nvExceptions.Conflict, ksExceptions.ClientException, nvExceptions.ClientException) as e:
852 self._format_exception(e)
tierno8e995ce2016-09-22 08:13:00 +0000853 except (HTTPException, gl1Exceptions.HTTPException, gl1Exceptions.CommunicationError, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200854 if retry==max_retries:
855 continue
856 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100857 except IOError as e: #can not open the file
tiernoae4a8d12016-07-08 12:30:39 +0200858 raise vimconn.vimconnConnectionException(type(e).__name__ + ": " + str(e)+ " for " + image_dict['location'],
859 http_code=vimconn.HTTP_Bad_Request)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +0000860
tiernoae4a8d12016-07-08 12:30:39 +0200861 def delete_image(self, image_id):
862 '''Deletes a tenant image from openstack VIM. Returns the old id
tierno7edb6752016-03-21 17:37:52 +0100863 '''
tiernoae4a8d12016-07-08 12:30:39 +0200864 try:
865 self._reload_connection()
tierno1beea862018-07-11 15:47:37 +0200866 self.glance.images.delete(image_id)
tiernoae4a8d12016-07-08 12:30:39 +0200867 return image_id
shashankjain3c83a212018-10-04 13:05:46 +0530868 except (nvExceptions.NotFound, ksExceptions.ClientException, nvExceptions.ClientException, gl1Exceptions.CommunicationError, gl1Exceptions.HTTPNotFound, ConnectionError) as e: #TODO remove
tiernoae4a8d12016-07-08 12:30:39 +0200869 self._format_exception(e)
870
871 def get_image_id_from_path(self, path):
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +0000872 '''Get the image id from image path in the VIM database. Returns the image_id'''
tiernoae4a8d12016-07-08 12:30:39 +0200873 try:
874 self._reload_connection()
tierno1beea862018-07-11 15:47:37 +0200875 images = self.glance.images.list()
tiernoae4a8d12016-07-08 12:30:39 +0200876 for image in images:
877 if image.metadata.get("location")==path:
878 return image.id
879 raise vimconn.vimconnNotFoundException("image with location '{}' not found".format( path))
tierno8e995ce2016-09-22 08:13:00 +0000880 except (ksExceptions.ClientException, nvExceptions.ClientException, gl1Exceptions.CommunicationError, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200881 self._format_exception(e)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +0000882
garciadeblasb69fa9f2016-09-28 12:04:10 +0200883 def get_image_list(self, filter_dict={}):
884 '''Obtain tenant images from VIM
885 Filter_dict can be:
886 id: image id
887 name: image name
888 checksum: image checksum
889 Returns the image list of dictionaries:
890 [{<the fields at Filter_dict plus some VIM specific>}, ...]
891 List can be empty
892 '''
893 self.logger.debug("Getting image list from VIM filter: '%s'", str(filter_dict))
894 try:
895 self._reload_connection()
tierno69b590e2018-03-13 18:52:23 +0100896 filter_dict_os = filter_dict.copy()
garciadeblasb69fa9f2016-09-28 12:04:10 +0200897 #First we filter by the available filter fields: name, id. The others are removed.
tierno1beea862018-07-11 15:47:37 +0200898 image_list = self.glance.images.list()
garciadeblasb69fa9f2016-09-28 12:04:10 +0200899 filtered_list = []
900 for image in image_list:
tierno3cb8dc32017-10-24 18:13:19 +0200901 try:
tierno1beea862018-07-11 15:47:37 +0200902 if filter_dict.get("name") and image["name"] != filter_dict["name"]:
903 continue
904 if filter_dict.get("id") and image["id"] != filter_dict["id"]:
905 continue
906 if filter_dict.get("checksum") and image["checksum"] != filter_dict["checksum"]:
907 continue
908
909 filtered_list.append(image.copy())
tierno3cb8dc32017-10-24 18:13:19 +0200910 except gl1Exceptions.HTTPNotFound:
911 pass
garciadeblasb69fa9f2016-09-28 12:04:10 +0200912 return filtered_list
913 except (ksExceptions.ClientException, nvExceptions.ClientException, gl1Exceptions.CommunicationError, ConnectionError) as e:
914 self._format_exception(e)
915
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +0200916 def __wait_for_vm(self, vm_id, status):
917 """wait until vm is in the desired status and return True.
918 If the VM gets in ERROR status, return false.
919 If the timeout is reached generate an exception"""
920 elapsed_time = 0
921 while elapsed_time < server_timeout:
922 vm_status = self.nova.servers.get(vm_id).status
923 if vm_status == status:
924 return True
925 if vm_status == 'ERROR':
926 return False
tierno1df468d2018-07-06 14:25:16 +0200927 time.sleep(5)
928 elapsed_time += 5
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +0200929
930 # if we exceeded the timeout rollback
931 if elapsed_time >= server_timeout:
932 raise vimconn.vimconnException('Timeout waiting for instance ' + vm_id + ' to get ' + status,
933 http_code=vimconn.HTTP_Request_Timeout)
934
mirabal29356312017-07-27 12:21:22 +0200935 def _get_openstack_availablity_zones(self):
936 """
937 Get from openstack availability zones available
938 :return:
939 """
940 try:
941 openstack_availability_zone = self.nova.availability_zones.list()
942 openstack_availability_zone = [str(zone.zoneName) for zone in openstack_availability_zone
943 if zone.zoneName != 'internal']
944 return openstack_availability_zone
945 except Exception as e:
946 return None
947
948 def _set_availablity_zones(self):
949 """
950 Set vim availablity zone
951 :return:
952 """
953
954 if 'availability_zone' in self.config:
955 vim_availability_zones = self.config.get('availability_zone')
956 if isinstance(vim_availability_zones, str):
957 self.availability_zone = [vim_availability_zones]
958 elif isinstance(vim_availability_zones, list):
959 self.availability_zone = vim_availability_zones
960 else:
961 self.availability_zone = self._get_openstack_availablity_zones()
962
tierno5a3273c2017-08-29 11:43:46 +0200963 def _get_vm_availability_zone(self, availability_zone_index, availability_zone_list):
mirabal29356312017-07-27 12:21:22 +0200964 """
tierno5a3273c2017-08-29 11:43:46 +0200965 Return thge availability zone to be used by the created VM.
966 :return: The VIM availability zone to be used or None
mirabal29356312017-07-27 12:21:22 +0200967 """
tierno5a3273c2017-08-29 11:43:46 +0200968 if availability_zone_index is None:
969 if not self.config.get('availability_zone'):
970 return None
971 elif isinstance(self.config.get('availability_zone'), str):
972 return self.config['availability_zone']
973 else:
974 # TODO consider using a different parameter at config for default AV and AV list match
975 return self.config['availability_zone'][0]
mirabal29356312017-07-27 12:21:22 +0200976
tierno5a3273c2017-08-29 11:43:46 +0200977 vim_availability_zones = self.availability_zone
978 # check if VIM offer enough availability zones describe in the VNFD
979 if vim_availability_zones and len(availability_zone_list) <= len(vim_availability_zones):
980 # check if all the names of NFV AV match VIM AV names
981 match_by_index = False
982 for av in availability_zone_list:
983 if av not in vim_availability_zones:
984 match_by_index = True
985 break
986 if match_by_index:
987 return vim_availability_zones[availability_zone_index]
988 else:
989 return availability_zone_list[availability_zone_index]
mirabal29356312017-07-27 12:21:22 +0200990 else:
tierno5a3273c2017-08-29 11:43:46 +0200991 raise vimconn.vimconnConflictException("No enough availability zones at VIM for this deployment")
mirabal29356312017-07-27 12:21:22 +0200992
tierno5a3273c2017-08-29 11:43:46 +0200993 def new_vminstance(self, name, description, start, image_id, flavor_id, net_list, cloud_config=None, disk_list=None,
994 availability_zone_index=None, availability_zone_list=None):
tierno98e909c2017-10-14 13:27:03 +0200995 """Adds a VM instance to VIM
tierno7edb6752016-03-21 17:37:52 +0100996 Params:
997 start: indicates if VM must start or boot in pause mode. Ignored
998 image_id,flavor_id: iamge and flavor uuid
999 net_list: list of interfaces, each one is a dictionary with:
1000 name:
1001 net_id: network uuid to connect
1002 vpci: virtual vcpi to assign, ignored because openstack lack #TODO
1003 model: interface model, ignored #TODO
1004 mac_address: used for SR-IOV ifaces #TODO for other types
1005 use: 'data', 'bridge', 'mgmt'
tierno66eba6e2017-11-10 17:09:18 +01001006 type: 'virtual', 'PCI-PASSTHROUGH'('PF'), 'SR-IOV'('VF'), 'VFnotShared'
tierno7edb6752016-03-21 17:37:52 +01001007 vim_id: filled/added by this function
ahmadsaf853d452016-12-22 11:33:47 +05001008 floating_ip: True/False (or it can be None)
tierno41a69812018-02-16 14:34:33 +01001009 'cloud_config': (optional) dictionary with:
1010 'key-pairs': (optional) list of strings with the public key to be inserted to the default user
1011 'users': (optional) list of users to be inserted, each item is a dict with:
1012 'name': (mandatory) user name,
1013 'key-pairs': (optional) list of strings with the public key to be inserted to the user
1014 'user-data': (optional) string is a text script to be passed directly to cloud-init
1015 'config-files': (optional). List of files to be transferred. Each item is a dict with:
1016 'dest': (mandatory) string with the destination absolute path
1017 'encoding': (optional, by default text). Can be one of:
1018 'b64', 'base64', 'gz', 'gz+b64', 'gz+base64', 'gzip+b64', 'gzip+base64'
1019 'content' (mandatory): string with the content of the file
1020 'permissions': (optional) string with file permissions, typically octal notation '0644'
1021 'owner': (optional) file owner, string with the format 'owner:group'
1022 'boot-data-drive': boolean to indicate if user-data must be passed using a boot drive (hard disk)
mirabal29356312017-07-27 12:21:22 +02001023 'disk_list': (optional) list with additional disks to the VM. Each item is a dict with:
1024 'image_id': (optional). VIM id of an existing image. If not provided an empty disk must be mounted
1025 'size': (mandatory) string with the size of the disk in GB
tierno1df468d2018-07-06 14:25:16 +02001026 'vim_id' (optional) should use this existing volume id
tierno5a3273c2017-08-29 11:43:46 +02001027 availability_zone_index: Index of availability_zone_list to use for this this VM. None if not AV required
1028 availability_zone_list: list of availability zones given by user in the VNFD descriptor. Ignore if
1029 availability_zone_index is None
tierno7edb6752016-03-21 17:37:52 +01001030 #TODO ip, security groups
tierno98e909c2017-10-14 13:27:03 +02001031 Returns a tuple with the instance identifier and created_items or raises an exception on error
1032 created_items can be None or a dictionary where this method can include key-values that will be passed to
1033 the method delete_vminstance and action_vminstance. Can be used to store created ports, volumes, etc.
1034 Format is vimconnector dependent, but do not use nested dictionaries and a value of None should be the same
1035 as not present.
1036 """
tiernofa51c202017-01-27 14:58:17 +01001037 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 +01001038 try:
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +02001039 server = None
tierno98e909c2017-10-14 13:27:03 +02001040 created_items = {}
tiernob0b9dab2017-10-14 14:25:20 +02001041 # metadata = {}
tierno98e909c2017-10-14 13:27:03 +02001042 net_list_vim = []
1043 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 +02001044 no_secured_ports = [] # List of port-is with port-security disabled
tierno7edb6752016-03-21 17:37:52 +01001045 self._reload_connection()
tiernob0b9dab2017-10-14 14:25:20 +02001046 # metadata_vpci = {} # For a specific neutron plugin
tiernob84cbdc2017-07-07 14:30:30 +02001047 block_device_mapping = None
tierno7edb6752016-03-21 17:37:52 +01001048 for net in net_list:
tierno98e909c2017-10-14 13:27:03 +02001049 if not net.get("net_id"): # skip non connected iface
tierno7edb6752016-03-21 17:37:52 +01001050 continue
Pablo Montes Moreno3be0b2a2017-03-30 13:22:15 +02001051
1052 port_dict={
1053 "network_id": net["net_id"],
1054 "name": net.get("name"),
1055 "admin_state_up": True
1056 }
1057 if net["type"]=="virtual":
tiernob0b9dab2017-10-14 14:25:20 +02001058 pass
1059 # if "vpci" in net:
1060 # metadata_vpci[ net["net_id"] ] = [[ net["vpci"], "" ]]
tierno66eba6e2017-11-10 17:09:18 +01001061 elif net["type"] == "VF" or net["type"] == "SR-IOV": # for VF
tiernob0b9dab2017-10-14 14:25:20 +02001062 # if "vpci" in net:
1063 # if "VF" not in metadata_vpci:
1064 # metadata_vpci["VF"]=[]
1065 # metadata_vpci["VF"].append([ net["vpci"], "" ])
Pablo Montes Moreno3be0b2a2017-03-30 13:22:15 +02001066 port_dict["binding:vnic_type"]="direct"
tiernob0b9dab2017-10-14 14:25:20 +02001067 # VIO specific Changes
kate721d79b2017-06-24 04:21:38 -07001068 if self.vim_type == "VIO":
tiernob0b9dab2017-10-14 14:25:20 +02001069 # Need to create port with port_security_enabled = False and no-security-groups
kate721d79b2017-06-24 04:21:38 -07001070 port_dict["port_security_enabled"]=False
1071 port_dict["provider_security_groups"]=[]
1072 port_dict["security_groups"]=[]
tierno66eba6e2017-11-10 17:09:18 +01001073 else: # For PT PCI-PASSTHROUGH
tiernob0b9dab2017-10-14 14:25:20 +02001074 # VIO specific Changes
1075 # Current VIO release does not support port with type 'direct-physical'
1076 # So no need to create virtual port in case of PCI-device.
1077 # Will update port_dict code when support gets added in next VIO release
kate721d79b2017-06-24 04:21:38 -07001078 if self.vim_type == "VIO":
tiernob0b9dab2017-10-14 14:25:20 +02001079 raise vimconn.vimconnNotSupportedException(
1080 "Current VIO release does not support full passthrough (PT)")
1081 # if "vpci" in net:
1082 # if "PF" not in metadata_vpci:
1083 # metadata_vpci["PF"]=[]
1084 # metadata_vpci["PF"].append([ net["vpci"], "" ])
Pablo Montes Moreno3be0b2a2017-03-30 13:22:15 +02001085 port_dict["binding:vnic_type"]="direct-physical"
1086 if not port_dict["name"]:
1087 port_dict["name"]=name
1088 if net.get("mac_address"):
1089 port_dict["mac_address"]=net["mac_address"]
tierno41a69812018-02-16 14:34:33 +01001090 if net.get("ip_address"):
1091 port_dict["fixed_ips"] = [{'ip_address': net["ip_address"]}]
1092 # TODO add 'subnet_id': <subnet_id>
Pablo Montes Moreno3be0b2a2017-03-30 13:22:15 +02001093 new_port = self.neutron.create_port({"port": port_dict })
tierno00e3df72017-11-29 17:20:13 +01001094 created_items["port:" + str(new_port["port"]["id"])] = True
Pablo Montes Moreno3be0b2a2017-03-30 13:22:15 +02001095 net["mac_adress"] = new_port["port"]["mac_address"]
1096 net["vim_id"] = new_port["port"]["id"]
tiernob84cbdc2017-07-07 14:30:30 +02001097 # if try to use a network without subnetwork, it will return a emtpy list
1098 fixed_ips = new_port["port"].get("fixed_ips")
1099 if fixed_ips:
1100 net["ip"] = fixed_ips[0].get("ip_address")
1101 else:
1102 net["ip"] = None
montesmoreno994a29d2017-08-22 11:23:06 +02001103
1104 port = {"port-id": new_port["port"]["id"]}
1105 if float(self.nova.api_version.get_string()) >= 2.32:
1106 port["tag"] = new_port["port"]["name"]
1107 net_list_vim.append(port)
Pablo Montes Moreno3be0b2a2017-03-30 13:22:15 +02001108
ahmadsaf853d452016-12-22 11:33:47 +05001109 if net.get('floating_ip', False):
tiernof8383b82017-01-18 15:49:48 +01001110 net['exit_on_floating_ip_error'] = True
ahmadsaf853d452016-12-22 11:33:47 +05001111 external_network.append(net)
tiernof8383b82017-01-18 15:49:48 +01001112 elif net['use'] == 'mgmt' and self.config.get('use_floating_ip'):
1113 net['exit_on_floating_ip_error'] = False
1114 external_network.append(net)
tierno326fd5e2018-02-22 11:58:59 +01001115 net['floating_ip'] = self.config.get('use_floating_ip')
tiernof8383b82017-01-18 15:49:48 +01001116
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +02001117 # If port security is disabled when the port has not yet been attached to the VM, then all vm traffic is dropped.
1118 # As a workaround we wait until the VM is active and then disable the port-security
tierno4d1ce222018-04-06 10:41:06 +02001119 if net.get("port_security") == False and not self.config.get("no_port_security_extension"):
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +02001120 no_secured_ports.append(new_port["port"]["id"])
1121
tiernob0b9dab2017-10-14 14:25:20 +02001122 # if metadata_vpci:
1123 # metadata = {"pci_assignement": json.dumps(metadata_vpci)}
1124 # if len(metadata["pci_assignement"]) >255:
1125 # #limit the metadata size
1126 # #metadata["pci_assignement"] = metadata["pci_assignement"][0:255]
1127 # self.logger.warn("Metadata deleted since it exceeds the expected length (255) ")
1128 # metadata = {}
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001129
tiernob0b9dab2017-10-14 14:25:20 +02001130 self.logger.debug("name '%s' image_id '%s'flavor_id '%s' net_list_vim '%s' description '%s'",
1131 name, image_id, flavor_id, str(net_list_vim), description)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001132
tiernob0b9dab2017-10-14 14:25:20 +02001133 security_groups = self.config.get('security_groups')
tierno7edb6752016-03-21 17:37:52 +01001134 if type(security_groups) is str:
1135 security_groups = ( security_groups, )
tierno98e909c2017-10-14 13:27:03 +02001136 # cloud config
tierno0a1437e2017-10-02 00:17:43 +02001137 config_drive, userdata = self._create_user_data(cloud_config)
montesmoreno0c8def02016-12-22 12:16:23 +00001138
tierno98e909c2017-10-14 13:27:03 +02001139 # Create additional volumes in case these are present in disk_list
montesmoreno0c8def02016-12-22 12:16:23 +00001140 base_disk_index = ord('b')
tierno1df468d2018-07-06 14:25:16 +02001141 if disk_list:
tiernob84cbdc2017-07-07 14:30:30 +02001142 block_device_mapping = {}
montesmoreno0c8def02016-12-22 12:16:23 +00001143 for disk in disk_list:
tierno1df468d2018-07-06 14:25:16 +02001144 if disk.get('vim_id'):
1145 block_device_mapping['_vd' + chr(base_disk_index)] = disk['vim_id']
montesmoreno0c8def02016-12-22 12:16:23 +00001146 else:
tierno1df468d2018-07-06 14:25:16 +02001147 if 'image_id' in disk:
1148 volume = self.cinder.volumes.create(size=disk['size'], name=name + '_vd' +
1149 chr(base_disk_index), imageRef=disk['image_id'])
1150 else:
1151 volume = self.cinder.volumes.create(size=disk['size'], name=name + '_vd' +
1152 chr(base_disk_index))
1153 created_items["volume:" + str(volume.id)] = True
1154 block_device_mapping['_vd' + chr(base_disk_index)] = volume.id
montesmoreno0c8def02016-12-22 12:16:23 +00001155 base_disk_index += 1
1156
tierno1df468d2018-07-06 14:25:16 +02001157 # Wait until created volumes are with status available
montesmoreno0c8def02016-12-22 12:16:23 +00001158 elapsed_time = 0
tierno1df468d2018-07-06 14:25:16 +02001159 while elapsed_time < volume_timeout:
1160 for created_item in created_items:
1161 v, _, volume_id = created_item.partition(":")
1162 if v == 'volume':
1163 if self.cinder.volumes.get(volume_id).status != 'available':
1164 break
1165 else: # all ready: break from while
1166 break
1167 time.sleep(5)
1168 elapsed_time += 5
tiernob0b9dab2017-10-14 14:25:20 +02001169 # If we exceeded the timeout rollback
montesmoreno0c8def02016-12-22 12:16:23 +00001170 if elapsed_time >= volume_timeout:
montesmoreno0c8def02016-12-22 12:16:23 +00001171 raise vimconn.vimconnException('Timeout creating volumes for instance ' + name,
1172 http_code=vimconn.HTTP_Request_Timeout)
mirabal29356312017-07-27 12:21:22 +02001173 # get availability Zone
tierno5a3273c2017-08-29 11:43:46 +02001174 vm_av_zone = self._get_vm_availability_zone(availability_zone_index, availability_zone_list)
montesmoreno0c8def02016-12-22 12:16:23 +00001175
tiernob0b9dab2017-10-14 14:25:20 +02001176 self.logger.debug("nova.servers.create({}, {}, {}, nics={}, security_groups={}, "
mirabal29356312017-07-27 12:21:22 +02001177 "availability_zone={}, key_name={}, userdata={}, config_drive={}, "
tiernob0b9dab2017-10-14 14:25:20 +02001178 "block_device_mapping={})".format(name, image_id, flavor_id, net_list_vim,
mirabal29356312017-07-27 12:21:22 +02001179 security_groups, vm_av_zone, self.config.get('keypair'),
tiernob0b9dab2017-10-14 14:25:20 +02001180 userdata, config_drive, block_device_mapping))
1181 server = self.nova.servers.create(name, image_id, flavor_id, nics=net_list_vim,
montesmoreno0c8def02016-12-22 12:16:23 +00001182 security_groups=security_groups,
mirabal29356312017-07-27 12:21:22 +02001183 availability_zone=vm_av_zone,
montesmoreno0c8def02016-12-22 12:16:23 +00001184 key_name=self.config.get('keypair'),
1185 userdata=userdata,
tiernob84cbdc2017-07-07 14:30:30 +02001186 config_drive=config_drive,
1187 block_device_mapping=block_device_mapping
montesmoreno0c8def02016-12-22 12:16:23 +00001188 ) # , description=description)
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +02001189
tierno326fd5e2018-02-22 11:58:59 +01001190 vm_start_time = time.time()
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +02001191 # Previously mentioned workaround to wait until the VM is active and then disable the port-security
1192 if no_secured_ports:
1193 self.__wait_for_vm(server.id, 'ACTIVE')
1194
1195 for port_id in no_secured_ports:
1196 try:
tierno4d1ce222018-04-06 10:41:06 +02001197 self.neutron.update_port(port_id,
1198 {"port": {"port_security_enabled": False, "security_groups": None}})
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +02001199 except Exception as e:
tierno4d1ce222018-04-06 10:41:06 +02001200 raise vimconn.vimconnException("It was not possible to disable port security for port {}".format(
1201 port_id))
tierno98e909c2017-10-14 13:27:03 +02001202 # print "DONE :-)", server
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +02001203
tierno4d1ce222018-04-06 10:41:06 +02001204 # pool_id = None
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +02001205 if external_network:
tierno98e909c2017-10-14 13:27:03 +02001206 floating_ips = self.neutron.list_floatingips().get("floatingips", ())
ahmadsaf853d452016-12-22 11:33:47 +05001207 for floating_network in external_network:
tiernof8383b82017-01-18 15:49:48 +01001208 try:
tiernof8383b82017-01-18 15:49:48 +01001209 assigned = False
tierno98e909c2017-10-14 13:27:03 +02001210 while not assigned:
tiernof8383b82017-01-18 15:49:48 +01001211 if floating_ips:
1212 ip = floating_ips.pop(0)
tierno326fd5e2018-02-22 11:58:59 +01001213 if ip.get("port_id", False) or ip.get('tenant_id') != server.tenant_id:
1214 continue
1215 if isinstance(floating_network['floating_ip'], str):
1216 if ip.get("floating_network_id") != floating_network['floating_ip']:
1217 continue
1218 free_floating_ip = ip.get("floating_ip_address")
tiernof8383b82017-01-18 15:49:48 +01001219 else:
tiernocb3cca22018-05-31 15:08:52 +02001220 if isinstance(floating_network['floating_ip'], str) and \
1221 floating_network['floating_ip'].lower() != "true":
tierno326fd5e2018-02-22 11:58:59 +01001222 pool_id = floating_network['floating_ip']
1223 else:
tierno4d1ce222018-04-06 10:41:06 +02001224 # Find the external network
tierno326fd5e2018-02-22 11:58:59 +01001225 external_nets = list()
1226 for net in self.neutron.list_networks()['networks']:
1227 if net['router:external']:
1228 external_nets.append(net)
tiernof8383b82017-01-18 15:49:48 +01001229
tierno326fd5e2018-02-22 11:58:59 +01001230 if len(external_nets) == 0:
1231 raise vimconn.vimconnException("Cannot create floating_ip automatically since no external "
1232 "network is present",
1233 http_code=vimconn.HTTP_Conflict)
1234 if len(external_nets) > 1:
1235 raise vimconn.vimconnException("Cannot create floating_ip automatically since multiple "
1236 "external networks are present",
1237 http_code=vimconn.HTTP_Conflict)
tiernof8383b82017-01-18 15:49:48 +01001238
tierno326fd5e2018-02-22 11:58:59 +01001239 pool_id = external_nets[0].get('id')
tiernof8383b82017-01-18 15:49:48 +01001240 param = {'floatingip': {'floating_network_id': pool_id, 'tenant_id': server.tenant_id}}
ahmadsaf853d452016-12-22 11:33:47 +05001241 try:
tierno4d1ce222018-04-06 10:41:06 +02001242 # self.logger.debug("Creating floating IP")
tiernof8383b82017-01-18 15:49:48 +01001243 new_floating_ip = self.neutron.create_floatingip(param)
1244 free_floating_ip = new_floating_ip['floatingip']['floating_ip_address']
ahmadsaf853d452016-12-22 11:33:47 +05001245 except Exception as e:
tierno326fd5e2018-02-22 11:58:59 +01001246 raise vimconn.vimconnException(type(e).__name__ + ": Cannot create new floating_ip " +
1247 str(e), http_code=vimconn.HTTP_Conflict)
1248
1249 fix_ip = floating_network.get('ip')
1250 while not assigned:
1251 try:
1252 server.add_floating_ip(free_floating_ip, fix_ip)
1253 assigned = True
1254 except Exception as e:
tierno4d1ce222018-04-06 10:41:06 +02001255 # openstack need some time after VM creation to asign an IP. So retry if fails
tierno326fd5e2018-02-22 11:58:59 +01001256 vm_status = self.nova.servers.get(server.id).status
1257 if vm_status != 'ACTIVE' and vm_status != 'ERROR':
1258 if time.time() - vm_start_time < server_timeout:
1259 time.sleep(5)
1260 continue
tierno4d1ce222018-04-06 10:41:06 +02001261 raise vimconn.vimconnException(
1262 "Cannot create floating_ip: {} {}".format(type(e).__name__, e),
1263 http_code=vimconn.HTTP_Conflict)
tierno326fd5e2018-02-22 11:58:59 +01001264
tiernof8383b82017-01-18 15:49:48 +01001265 except Exception as e:
1266 if not floating_network['exit_on_floating_ip_error']:
1267 self.logger.warn("Cannot create floating_ip. %s", str(e))
1268 continue
tiernof8383b82017-01-18 15:49:48 +01001269 raise
montesmoreno2a1fc4e2017-01-09 16:46:04 +00001270
tierno98e909c2017-10-14 13:27:03 +02001271 return server.id, created_items
tierno7edb6752016-03-21 17:37:52 +01001272# except nvExceptions.NotFound as e:
1273# error_value=-vimconn.HTTP_Not_Found
1274# error_text= "vm instance %s not found" % vm_id
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +02001275# except TypeError as e:
1276# raise vimconn.vimconnException(type(e).__name__ + ": "+ str(e), http_code=vimconn.HTTP_Bad_Request)
1277
1278 except Exception as e:
tierno98e909c2017-10-14 13:27:03 +02001279 server_id = None
1280 if server:
1281 server_id = server.id
1282 try:
1283 self.delete_vminstance(server_id, created_items)
1284 except Exception as e2:
1285 self.logger.error("new_vminstance rollback fail {}".format(e2))
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +02001286
tiernoae4a8d12016-07-08 12:30:39 +02001287 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +01001288
tiernoae4a8d12016-07-08 12:30:39 +02001289 def get_vminstance(self,vm_id):
tierno7edb6752016-03-21 17:37:52 +01001290 '''Returns the VM instance information from VIM'''
tiernoae4a8d12016-07-08 12:30:39 +02001291 #self.logger.debug("Getting VM from VIM")
tierno7edb6752016-03-21 17:37:52 +01001292 try:
1293 self._reload_connection()
1294 server = self.nova.servers.find(id=vm_id)
1295 #TODO parse input and translate to VIM format (openmano_schemas.new_vminstance_response_schema)
tiernoae4a8d12016-07-08 12:30:39 +02001296 return server.to_dict()
tierno8e995ce2016-09-22 08:13:00 +00001297 except (ksExceptions.ClientException, nvExceptions.ClientException, nvExceptions.NotFound, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +02001298 self._format_exception(e)
1299
1300 def get_vminstance_console(self,vm_id, console_type="vnc"):
tierno7edb6752016-03-21 17:37:52 +01001301 '''
1302 Get a console for the virtual machine
1303 Params:
1304 vm_id: uuid of the VM
1305 console_type, can be:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001306 "novnc" (by default), "xvpvnc" for VNC types,
tierno7edb6752016-03-21 17:37:52 +01001307 "rdp-html5" for RDP types, "spice-html5" for SPICE types
tiernoae4a8d12016-07-08 12:30:39 +02001308 Returns dict with the console parameters:
1309 protocol: ssh, ftp, http, https, ...
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001310 server: usually ip address
1311 port: the http, ssh, ... port
1312 suffix: extra text, e.g. the http path and query string
tierno7edb6752016-03-21 17:37:52 +01001313 '''
tiernoae4a8d12016-07-08 12:30:39 +02001314 self.logger.debug("Getting VM CONSOLE from VIM")
tierno7edb6752016-03-21 17:37:52 +01001315 try:
1316 self._reload_connection()
1317 server = self.nova.servers.find(id=vm_id)
1318 if console_type == None or console_type == "novnc":
1319 console_dict = server.get_vnc_console("novnc")
1320 elif console_type == "xvpvnc":
1321 console_dict = server.get_vnc_console(console_type)
1322 elif console_type == "rdp-html5":
1323 console_dict = server.get_rdp_console(console_type)
1324 elif console_type == "spice-html5":
1325 console_dict = server.get_spice_console(console_type)
1326 else:
tiernoae4a8d12016-07-08 12:30:39 +02001327 raise vimconn.vimconnException("console type '{}' not allowed".format(console_type), http_code=vimconn.HTTP_Bad_Request)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001328
tierno7edb6752016-03-21 17:37:52 +01001329 console_dict1 = console_dict.get("console")
1330 if console_dict1:
1331 console_url = console_dict1.get("url")
1332 if console_url:
1333 #parse console_url
1334 protocol_index = console_url.find("//")
1335 suffix_index = console_url[protocol_index+2:].find("/") + protocol_index+2
1336 port_index = console_url[protocol_index+2:suffix_index].find(":") + protocol_index+2
1337 if protocol_index < 0 or port_index<0 or suffix_index<0:
1338 return -vimconn.HTTP_Internal_Server_Error, "Unexpected response from VIM"
1339 console_dict={"protocol": console_url[0:protocol_index],
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001340 "server": console_url[protocol_index+2:port_index],
1341 "port": console_url[port_index:suffix_index],
1342 "suffix": console_url[suffix_index+1:]
tierno7edb6752016-03-21 17:37:52 +01001343 }
1344 protocol_index += 2
tiernoae4a8d12016-07-08 12:30:39 +02001345 return console_dict
1346 raise vimconn.vimconnUnexpectedResponse("Unexpected response from VIM")
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001347
tierno8e995ce2016-09-22 08:13:00 +00001348 except (nvExceptions.NotFound, ksExceptions.ClientException, nvExceptions.ClientException, nvExceptions.BadRequest, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +02001349 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +01001350
tierno98e909c2017-10-14 13:27:03 +02001351 def delete_vminstance(self, vm_id, created_items=None):
tiernoae4a8d12016-07-08 12:30:39 +02001352 '''Removes a VM instance from VIM. Returns the old identifier
tierno7edb6752016-03-21 17:37:52 +01001353 '''
tiernoae4a8d12016-07-08 12:30:39 +02001354 #print "osconnector: Getting VM from VIM"
tierno98e909c2017-10-14 13:27:03 +02001355 if created_items == None:
1356 created_items = {}
tierno7edb6752016-03-21 17:37:52 +01001357 try:
1358 self._reload_connection()
tierno98e909c2017-10-14 13:27:03 +02001359 # delete VM ports attached to this networks before the virtual machine
1360 for k, v in created_items.items():
1361 if not v: # skip already deleted
1362 continue
tierno7edb6752016-03-21 17:37:52 +01001363 try:
tiernoad6bdd42018-01-10 10:43:46 +01001364 k_item, _, k_id = k.partition(":")
1365 if k_item == "port":
1366 self.neutron.delete_port(k_id)
tierno7edb6752016-03-21 17:37:52 +01001367 except Exception as e:
tierno00e3df72017-11-29 17:20:13 +01001368 self.logger.error("Error deleting port: {}: {}".format(type(e).__name__, e))
montesmoreno0c8def02016-12-22 12:16:23 +00001369
tierno98e909c2017-10-14 13:27:03 +02001370 # #commented because detaching the volumes makes the servers.delete not work properly ?!?
1371 # #dettach volumes attached
1372 # server = self.nova.servers.get(vm_id)
1373 # volumes_attached_dict = server._info['os-extended-volumes:volumes_attached'] #volume['id']
1374 # #for volume in volumes_attached_dict:
1375 # # self.cinder.volumes.detach(volume['id'])
montesmoreno0c8def02016-12-22 12:16:23 +00001376
tierno98e909c2017-10-14 13:27:03 +02001377 if vm_id:
1378 self.nova.servers.delete(vm_id)
montesmoreno0c8def02016-12-22 12:16:23 +00001379
tierno98e909c2017-10-14 13:27:03 +02001380 # delete volumes. Although having detached, they should have in active status before deleting
1381 # we ensure in this loop
montesmoreno0c8def02016-12-22 12:16:23 +00001382 keep_waiting = True
1383 elapsed_time = 0
1384 while keep_waiting and elapsed_time < volume_timeout:
1385 keep_waiting = False
tierno98e909c2017-10-14 13:27:03 +02001386 for k, v in created_items.items():
1387 if not v: # skip already deleted
1388 continue
1389 try:
tiernoad6bdd42018-01-10 10:43:46 +01001390 k_item, _, k_id = k.partition(":")
1391 if k_item == "volume":
1392 if self.cinder.volumes.get(k_id).status != 'available':
tierno98e909c2017-10-14 13:27:03 +02001393 keep_waiting = True
1394 else:
tiernoad6bdd42018-01-10 10:43:46 +01001395 self.cinder.volumes.delete(k_id)
tierno98e909c2017-10-14 13:27:03 +02001396 except Exception as e:
tierno00e3df72017-11-29 17:20:13 +01001397 self.logger.error("Error deleting volume: {}: {}".format(type(e).__name__, e))
montesmoreno0c8def02016-12-22 12:16:23 +00001398 if keep_waiting:
1399 time.sleep(1)
1400 elapsed_time += 1
tierno98e909c2017-10-14 13:27:03 +02001401 return None
tierno8e995ce2016-09-22 08:13:00 +00001402 except (nvExceptions.NotFound, ksExceptions.ClientException, nvExceptions.ClientException, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +02001403 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +01001404
tiernoae4a8d12016-07-08 12:30:39 +02001405 def refresh_vms_status(self, vm_list):
1406 '''Get the status of the virtual machines and their interfaces/ports
1407 Params: the list of VM identifiers
1408 Returns a dictionary with:
1409 vm_id: #VIM id of this Virtual Machine
1410 status: #Mandatory. Text with one of:
1411 # DELETED (not found at vim)
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001412 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
tiernoae4a8d12016-07-08 12:30:39 +02001413 # OTHER (Vim reported other status not understood)
1414 # ERROR (VIM indicates an ERROR status)
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001415 # ACTIVE, PAUSED, SUSPENDED, INACTIVE (not running),
tiernoae4a8d12016-07-08 12:30:39 +02001416 # CREATING (on building process), ERROR
1417 # ACTIVE:NoMgmtIP (Active but any of its interface has an IP address
1418 #
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001419 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
tiernoae4a8d12016-07-08 12:30:39 +02001420 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
1421 interfaces:
1422 - vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
1423 mac_address: #Text format XX:XX:XX:XX:XX:XX
1424 vim_net_id: #network id where this interface is connected
1425 vim_interface_id: #interface/port VIM id
1426 ip_address: #null, or text with IPv4, IPv6 address
tierno867ffe92017-03-27 12:50:34 +02001427 compute_node: #identification of compute node where PF,VF interface is allocated
1428 pci: #PCI address of the NIC that hosts the PF,VF
1429 vlan: #physical VLAN used for VF
tierno7edb6752016-03-21 17:37:52 +01001430 '''
tiernoae4a8d12016-07-08 12:30:39 +02001431 vm_dict={}
1432 self.logger.debug("refresh_vms status: Getting tenant VM instance information from VIM")
1433 for vm_id in vm_list:
1434 vm={}
1435 try:
1436 vm_vim = self.get_vminstance(vm_id)
1437 if vm_vim['status'] in vmStatus2manoFormat:
1438 vm['status'] = vmStatus2manoFormat[ vm_vim['status'] ]
tierno7edb6752016-03-21 17:37:52 +01001439 else:
tiernoae4a8d12016-07-08 12:30:39 +02001440 vm['status'] = "OTHER"
1441 vm['error_msg'] = "VIM status reported " + vm_vim['status']
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001442
1443 vm['vim_info'] = self.serialize(vm_vim)
1444
tiernoae4a8d12016-07-08 12:30:39 +02001445 vm["interfaces"] = []
1446 if vm_vim.get('fault'):
1447 vm['error_msg'] = str(vm_vim['fault'])
1448 #get interfaces
tierno7edb6752016-03-21 17:37:52 +01001449 try:
tiernoae4a8d12016-07-08 12:30:39 +02001450 self._reload_connection()
tiernob42fd9b2018-06-20 10:44:32 +02001451 port_dict = self.neutron.list_ports(device_id=vm_id)
tiernoae4a8d12016-07-08 12:30:39 +02001452 for port in port_dict["ports"]:
1453 interface={}
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001454 interface['vim_info'] = self.serialize(port)
tiernoae4a8d12016-07-08 12:30:39 +02001455 interface["mac_address"] = port.get("mac_address")
1456 interface["vim_net_id"] = port["network_id"]
1457 interface["vim_interface_id"] = port["id"]
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001458 # check if OS-EXT-SRV-ATTR:host is there,
Mike Marchetti5b9da422017-05-02 15:35:47 -04001459 # in case of non-admin credentials, it will be missing
1460 if vm_vim.get('OS-EXT-SRV-ATTR:host'):
1461 interface["compute_node"] = vm_vim['OS-EXT-SRV-ATTR:host']
tierno867ffe92017-03-27 12:50:34 +02001462 interface["pci"] = None
Mike Marchetti5b9da422017-05-02 15:35:47 -04001463
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001464 # check if binding:profile is there,
Mike Marchetti5b9da422017-05-02 15:35:47 -04001465 # in case of non-admin credentials, it will be missing
1466 if port.get('binding:profile'):
1467 if port['binding:profile'].get('pci_slot'):
1468 # TODO: At the moment sr-iov pci addresses are converted to PF pci addresses by setting the slot to 0x00
1469 # TODO: This is just a workaround valid for niantinc. Find a better way to do so
1470 # CHANGE DDDD:BB:SS.F to DDDD:BB:00.(F%2) assuming there are 2 ports per nic
1471 pci = port['binding:profile']['pci_slot']
1472 # interface["pci"] = pci[:-4] + "00." + str(int(pci[-1]) % 2)
1473 interface["pci"] = pci
tierno867ffe92017-03-27 12:50:34 +02001474 interface["vlan"] = None
Pablo Montes Moreno3be0b2a2017-03-30 13:22:15 +02001475 #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 +01001476 network = self.neutron.show_network(port["network_id"])
Pablo Montes Moreno3be0b2a2017-03-30 13:22:15 +02001477 if network['network'].get('provider:network_type') == 'vlan' and \
1478 port.get("binding:vnic_type") == "direct":
tierno867ffe92017-03-27 12:50:34 +02001479 interface["vlan"] = network['network'].get('provider:segmentation_id')
tiernoae4a8d12016-07-08 12:30:39 +02001480 ips=[]
1481 #look for floating ip address
tiernob42fd9b2018-06-20 10:44:32 +02001482 try:
1483 floating_ip_dict = self.neutron.list_floatingips(port_id=port["id"])
1484 if floating_ip_dict.get("floatingips"):
1485 ips.append(floating_ip_dict["floatingips"][0].get("floating_ip_address") )
1486 except Exception:
1487 pass
tierno7edb6752016-03-21 17:37:52 +01001488
tiernoae4a8d12016-07-08 12:30:39 +02001489 for subnet in port["fixed_ips"]:
1490 ips.append(subnet["ip_address"])
1491 interface["ip_address"] = ";".join(ips)
1492 vm["interfaces"].append(interface)
1493 except Exception as e:
tiernob42fd9b2018-06-20 10:44:32 +02001494 self.logger.error("Error getting vm interface information {}: {}".format(type(e).__name__, e),
1495 exc_info=True)
tiernoae4a8d12016-07-08 12:30:39 +02001496 except vimconn.vimconnNotFoundException as e:
1497 self.logger.error("Exception getting vm status: %s", str(e))
1498 vm['status'] = "DELETED"
1499 vm['error_msg'] = str(e)
1500 except vimconn.vimconnException as e:
1501 self.logger.error("Exception getting vm status: %s", str(e))
1502 vm['status'] = "VIM_ERROR"
1503 vm['error_msg'] = str(e)
1504 vm_dict[vm_id] = vm
1505 return vm_dict
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001506
tierno98e909c2017-10-14 13:27:03 +02001507 def action_vminstance(self, vm_id, action_dict, created_items={}):
tierno7edb6752016-03-21 17:37:52 +01001508 '''Send and action over a VM instance from VIM
tierno98e909c2017-10-14 13:27:03 +02001509 Returns None or the console dict if the action was successfully sent to the VIM'''
tiernoae4a8d12016-07-08 12:30:39 +02001510 self.logger.debug("Action over VM '%s': %s", vm_id, str(action_dict))
tierno7edb6752016-03-21 17:37:52 +01001511 try:
1512 self._reload_connection()
1513 server = self.nova.servers.find(id=vm_id)
1514 if "start" in action_dict:
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001515 if action_dict["start"]=="rebuild":
tierno7edb6752016-03-21 17:37:52 +01001516 server.rebuild()
1517 else:
1518 if server.status=="PAUSED":
1519 server.unpause()
1520 elif server.status=="SUSPENDED":
1521 server.resume()
1522 elif server.status=="SHUTOFF":
1523 server.start()
1524 elif "pause" in action_dict:
1525 server.pause()
1526 elif "resume" in action_dict:
1527 server.resume()
1528 elif "shutoff" in action_dict or "shutdown" in action_dict:
1529 server.stop()
1530 elif "forceOff" in action_dict:
1531 server.stop() #TODO
1532 elif "terminate" in action_dict:
1533 server.delete()
1534 elif "createImage" in action_dict:
1535 server.create_image()
1536 #"path":path_schema,
1537 #"description":description_schema,
1538 #"name":name_schema,
1539 #"metadata":metadata_schema,
1540 #"imageRef": id_schema,
1541 #"disk": {"oneOf":[{"type": "null"}, {"type":"string"}] },
1542 elif "rebuild" in action_dict:
1543 server.rebuild(server.image['id'])
1544 elif "reboot" in action_dict:
1545 server.reboot() #reboot_type='SOFT'
1546 elif "console" in action_dict:
1547 console_type = action_dict["console"]
1548 if console_type == None or console_type == "novnc":
1549 console_dict = server.get_vnc_console("novnc")
1550 elif console_type == "xvpvnc":
1551 console_dict = server.get_vnc_console(console_type)
1552 elif console_type == "rdp-html5":
1553 console_dict = server.get_rdp_console(console_type)
1554 elif console_type == "spice-html5":
1555 console_dict = server.get_spice_console(console_type)
1556 else:
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001557 raise vimconn.vimconnException("console type '{}' not allowed".format(console_type),
tiernoae4a8d12016-07-08 12:30:39 +02001558 http_code=vimconn.HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001559 try:
1560 console_url = console_dict["console"]["url"]
1561 #parse console_url
1562 protocol_index = console_url.find("//")
1563 suffix_index = console_url[protocol_index+2:].find("/") + protocol_index+2
1564 port_index = console_url[protocol_index+2:suffix_index].find(":") + protocol_index+2
1565 if protocol_index < 0 or port_index<0 or suffix_index<0:
tiernoae4a8d12016-07-08 12:30:39 +02001566 raise vimconn.vimconnException("Unexpected response from VIM " + str(console_dict))
tierno7edb6752016-03-21 17:37:52 +01001567 console_dict2={"protocol": console_url[0:protocol_index],
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001568 "server": console_url[protocol_index+2 : port_index],
1569 "port": int(console_url[port_index+1 : suffix_index]),
1570 "suffix": console_url[suffix_index+1:]
tierno7edb6752016-03-21 17:37:52 +01001571 }
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001572 return console_dict2
tiernoae4a8d12016-07-08 12:30:39 +02001573 except Exception as e:
1574 raise vimconn.vimconnException("Unexpected response from VIM " + str(console_dict))
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001575
tierno98e909c2017-10-14 13:27:03 +02001576 return None
tierno8e995ce2016-09-22 08:13:00 +00001577 except (ksExceptions.ClientException, nvExceptions.ClientException, nvExceptions.NotFound, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +02001578 self._format_exception(e)
1579 #TODO insert exception vimconn.HTTP_Unauthorized
1580
kate721d79b2017-06-24 04:21:38 -07001581 ####### VIO Specific Changes #########
1582 def _genrate_vlanID(self):
1583 """
1584 Method to get unused vlanID
1585 Args:
1586 None
1587 Returns:
1588 vlanID
1589 """
1590 #Get used VLAN IDs
1591 usedVlanIDs = []
1592 networks = self.get_network_list()
1593 for net in networks:
1594 if net.get('provider:segmentation_id'):
1595 usedVlanIDs.append(net.get('provider:segmentation_id'))
1596 used_vlanIDs = set(usedVlanIDs)
1597
1598 #find unused VLAN ID
1599 for vlanID_range in self.config.get('dataplane_net_vlan_range'):
1600 try:
1601 start_vlanid , end_vlanid = map(int, vlanID_range.replace(" ", "").split("-"))
1602 for vlanID in xrange(start_vlanid, end_vlanid + 1):
1603 if vlanID not in used_vlanIDs:
1604 return vlanID
1605 except Exception as exp:
1606 raise vimconn.vimconnException("Exception {} occurred while generating VLAN ID.".format(exp))
1607 else:
1608 raise vimconn.vimconnConflictException("Unable to create the SRIOV VLAN network."\
1609 " All given Vlan IDs {} are in use.".format(self.config.get('dataplane_net_vlan_range')))
1610
1611
1612 def _validate_vlan_ranges(self, dataplane_net_vlan_range):
1613 """
1614 Method to validate user given vlanID ranges
1615 Args: None
1616 Returns: None
1617 """
1618 for vlanID_range in dataplane_net_vlan_range:
1619 vlan_range = vlanID_range.replace(" ", "")
1620 #validate format
1621 vlanID_pattern = r'(\d)*-(\d)*$'
1622 match_obj = re.match(vlanID_pattern, vlan_range)
1623 if not match_obj:
1624 raise vimconn.vimconnConflictException("Invalid dataplane_net_vlan_range {}.You must provide "\
1625 "'dataplane_net_vlan_range' in format [start_ID - end_ID].".format(vlanID_range))
1626
1627 start_vlanid , end_vlanid = map(int,vlan_range.split("-"))
1628 if start_vlanid <= 0 :
1629 raise vimconn.vimconnConflictException("Invalid dataplane_net_vlan_range {}."\
1630 "Start ID can not be zero. For VLAN "\
1631 "networks valid IDs are 1 to 4094 ".format(vlanID_range))
1632 if end_vlanid > 4094 :
1633 raise vimconn.vimconnConflictException("Invalid dataplane_net_vlan_range {}."\
1634 "End VLAN ID can not be greater than 4094. For VLAN "\
1635 "networks valid IDs are 1 to 4094 ".format(vlanID_range))
1636
1637 if start_vlanid > end_vlanid:
1638 raise vimconn.vimconnConflictException("Invalid dataplane_net_vlan_range {}."\
1639 "You must provide a 'dataplane_net_vlan_range' in format start_ID - end_ID and "\
1640 "start_ID < end_ID ".format(vlanID_range))
1641
tiernoae4a8d12016-07-08 12:30:39 +02001642#NOT USED FUNCTIONS
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001643
tiernoae4a8d12016-07-08 12:30:39 +02001644 def new_external_port(self, port_data):
1645 #TODO openstack if needed
1646 '''Adds a external port to VIM'''
1647 '''Returns the port identifier'''
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001648 return -vimconn.HTTP_Internal_Server_Error, "osconnector.new_external_port() not implemented"
1649
tiernoae4a8d12016-07-08 12:30:39 +02001650 def connect_port_network(self, port_id, network_id, admin=False):
1651 #TODO openstack if needed
1652 '''Connects a external port to a network'''
1653 '''Returns status code of the VIM response'''
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001654 return -vimconn.HTTP_Internal_Server_Error, "osconnector.connect_port_network() not implemented"
1655
tiernoae4a8d12016-07-08 12:30:39 +02001656 def new_user(self, user_name, user_passwd, tenant_id=None):
1657 '''Adds a new user to openstack VIM'''
1658 '''Returns the user identifier'''
1659 self.logger.debug("osconnector: Adding a new user to VIM")
1660 try:
1661 self._reload_connection()
Eduardo Sousae3c0dbc2018-09-03 11:56:07 +01001662 user=self.keystone.users.create(user_name, password=user_passwd, default_project=tenant_id)
tiernoae4a8d12016-07-08 12:30:39 +02001663 #self.keystone.tenants.add_user(self.k_creds["username"], #role)
1664 return user.id
1665 except ksExceptions.ConnectionError as e:
1666 error_value=-vimconn.HTTP_Bad_Request
1667 error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0]))
1668 except ksExceptions.ClientException as e: #TODO remove
tierno7edb6752016-03-21 17:37:52 +01001669 error_value=-vimconn.HTTP_Bad_Request
1670 error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0]))
1671 #TODO insert exception vimconn.HTTP_Unauthorized
1672 #if reaching here is because an exception
tierno9c5c8322018-03-23 15:44:03 +01001673 self.logger.debug("new_user " + error_text)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001674 return error_value, error_text
tiernoae4a8d12016-07-08 12:30:39 +02001675
1676 def delete_user(self, user_id):
1677 '''Delete a user from openstack VIM'''
1678 '''Returns the user identifier'''
1679 if self.debug:
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001680 print("osconnector: Deleting a user from VIM")
tiernoae4a8d12016-07-08 12:30:39 +02001681 try:
1682 self._reload_connection()
1683 self.keystone.users.delete(user_id)
1684 return 1, user_id
1685 except ksExceptions.ConnectionError as e:
1686 error_value=-vimconn.HTTP_Bad_Request
1687 error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0]))
1688 except ksExceptions.NotFound as e:
1689 error_value=-vimconn.HTTP_Not_Found
1690 error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0]))
1691 except ksExceptions.ClientException as e: #TODO remove
1692 error_value=-vimconn.HTTP_Bad_Request
1693 error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0]))
1694 #TODO insert exception vimconn.HTTP_Unauthorized
1695 #if reaching here is because an exception
tierno9c5c8322018-03-23 15:44:03 +01001696 self.logger.debug("delete_tenant " + error_text)
tiernoae4a8d12016-07-08 12:30:39 +02001697 return error_value, error_text
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001698
tierno7edb6752016-03-21 17:37:52 +01001699 def get_hosts_info(self):
1700 '''Get the information of deployed hosts
1701 Returns the hosts content'''
1702 if self.debug:
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001703 print("osconnector: Getting Host info from VIM")
tierno7edb6752016-03-21 17:37:52 +01001704 try:
1705 h_list=[]
1706 self._reload_connection()
1707 hypervisors = self.nova.hypervisors.list()
1708 for hype in hypervisors:
1709 h_list.append( hype.to_dict() )
1710 return 1, {"hosts":h_list}
1711 except nvExceptions.NotFound as e:
1712 error_value=-vimconn.HTTP_Not_Found
1713 error_text= (str(e) if len(e.args)==0 else str(e.args[0]))
1714 except (ksExceptions.ClientException, nvExceptions.ClientException) as e:
1715 error_value=-vimconn.HTTP_Bad_Request
1716 error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0]))
1717 #TODO insert exception vimconn.HTTP_Unauthorized
1718 #if reaching here is because an exception
tierno9c5c8322018-03-23 15:44:03 +01001719 self.logger.debug("get_hosts_info " + error_text)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001720 return error_value, error_text
tierno7edb6752016-03-21 17:37:52 +01001721
1722 def get_hosts(self, vim_tenant):
1723 '''Get the hosts and deployed instances
1724 Returns the hosts content'''
1725 r, hype_dict = self.get_hosts_info()
1726 if r<0:
1727 return r, hype_dict
1728 hypervisors = hype_dict["hosts"]
1729 try:
1730 servers = self.nova.servers.list()
1731 for hype in hypervisors:
1732 for server in servers:
1733 if server.to_dict()['OS-EXT-SRV-ATTR:hypervisor_hostname']==hype['hypervisor_hostname']:
1734 if 'vm' in hype:
1735 hype['vm'].append(server.id)
1736 else:
1737 hype['vm'] = [server.id]
1738 return 1, hype_dict
1739 except nvExceptions.NotFound as e:
1740 error_value=-vimconn.HTTP_Not_Found
1741 error_text= (str(e) if len(e.args)==0 else str(e.args[0]))
1742 except (ksExceptions.ClientException, nvExceptions.ClientException) as e:
1743 error_value=-vimconn.HTTP_Bad_Request
1744 error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0]))
1745 #TODO insert exception vimconn.HTTP_Unauthorized
1746 #if reaching here is because an exception
tierno9c5c8322018-03-23 15:44:03 +01001747 self.logger.debug("get_hosts " + error_text)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001748 return error_value, error_text
tierno7edb6752016-03-21 17:37:52 +01001749
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001750 def new_classification(self, name, ctype, definition):
1751 self.logger.debug(
1752 'Adding a new (Traffic) Classification to VIM, named %s', name)
1753 try:
1754 new_class = None
1755 self._reload_connection()
1756 if ctype not in supportedClassificationTypes:
1757 raise vimconn.vimconnNotSupportedException(
1758 'OpenStack VIM connector doesn\'t support provided '
1759 'Classification Type {}, supported ones are: '
1760 '{}'.format(ctype, supportedClassificationTypes))
1761 if not self._validate_classification(ctype, definition):
1762 raise vimconn.vimconnException(
1763 'Incorrect Classification definition '
1764 'for the type specified.')
1765 classification_dict = definition
1766 classification_dict['name'] = name
tierno7edb6752016-03-21 17:37:52 +01001767
Igor D.Ccaadc442017-11-06 12:48:48 +00001768 new_class = self.neutron.create_sfc_flow_classifier(
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001769 {'flow_classifier': classification_dict})
1770 return new_class['flow_classifier']['id']
1771 except (neExceptions.ConnectionFailed, ksExceptions.ClientException,
1772 neExceptions.NeutronException, ConnectionError) as e:
1773 self.logger.error(
1774 'Creation of Classification failed.')
1775 self._format_exception(e)
1776
1777 def get_classification(self, class_id):
1778 self.logger.debug(" Getting Classification %s from VIM", class_id)
1779 filter_dict = {"id": class_id}
1780 class_list = self.get_classification_list(filter_dict)
1781 if len(class_list) == 0:
1782 raise vimconn.vimconnNotFoundException(
1783 "Classification '{}' not found".format(class_id))
1784 elif len(class_list) > 1:
1785 raise vimconn.vimconnConflictException(
1786 "Found more than one Classification with this criteria")
1787 classification = class_list[0]
1788 return classification
1789
1790 def get_classification_list(self, filter_dict={}):
1791 self.logger.debug("Getting Classifications from VIM filter: '%s'",
1792 str(filter_dict))
1793 try:
tierno69b590e2018-03-13 18:52:23 +01001794 filter_dict_os = filter_dict.copy()
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001795 self._reload_connection()
tierno69b590e2018-03-13 18:52:23 +01001796 if self.api_version3 and "tenant_id" in filter_dict_os:
1797 filter_dict_os['project_id'] = filter_dict_os.pop('tenant_id')
Igor D.Ccaadc442017-11-06 12:48:48 +00001798 classification_dict = self.neutron.list_sfc_flow_classifiers(
tierno69b590e2018-03-13 18:52:23 +01001799 **filter_dict_os)
1800 classification_list = classification_dict["flow_classifiers"]
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001801 self.__classification_os2mano(classification_list)
1802 return classification_list
1803 except (neExceptions.ConnectionFailed, ksExceptions.ClientException,
1804 neExceptions.NeutronException, ConnectionError) as e:
1805 self._format_exception(e)
1806
1807 def delete_classification(self, class_id):
1808 self.logger.debug("Deleting Classification '%s' from VIM", class_id)
1809 try:
1810 self._reload_connection()
Igor D.Ccaadc442017-11-06 12:48:48 +00001811 self.neutron.delete_sfc_flow_classifier(class_id)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001812 return class_id
1813 except (neExceptions.ConnectionFailed, neExceptions.NeutronException,
1814 ksExceptions.ClientException, neExceptions.NeutronException,
1815 ConnectionError) as e:
1816 self._format_exception(e)
1817
1818 def new_sfi(self, name, ingress_ports, egress_ports, sfc_encap=True):
1819 self.logger.debug(
1820 "Adding a new Service Function Instance to VIM, named '%s'", name)
1821 try:
1822 new_sfi = None
1823 self._reload_connection()
1824 correlation = None
1825 if sfc_encap:
Igor D.Ccaadc442017-11-06 12:48:48 +00001826 correlation = 'nsh'
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001827 if len(ingress_ports) != 1:
1828 raise vimconn.vimconnNotSupportedException(
1829 "OpenStack VIM connector can only have "
1830 "1 ingress port per SFI")
1831 if len(egress_ports) != 1:
1832 raise vimconn.vimconnNotSupportedException(
1833 "OpenStack VIM connector can only have "
1834 "1 egress port per SFI")
1835 sfi_dict = {'name': name,
1836 'ingress': ingress_ports[0],
1837 'egress': egress_ports[0],
1838 'service_function_parameters': {
1839 'correlation': correlation}}
Igor D.Ccaadc442017-11-06 12:48:48 +00001840 new_sfi = self.neutron.create_sfc_port_pair({'port_pair': sfi_dict})
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001841 return new_sfi['port_pair']['id']
1842 except (neExceptions.ConnectionFailed, ksExceptions.ClientException,
1843 neExceptions.NeutronException, ConnectionError) as e:
1844 if new_sfi:
1845 try:
Igor D.Ccaadc442017-11-06 12:48:48 +00001846 self.neutron.delete_sfc_port_pair(
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001847 new_sfi['port_pair']['id'])
1848 except Exception:
1849 self.logger.error(
1850 'Creation of Service Function Instance failed, with '
1851 'subsequent deletion failure as well.')
1852 self._format_exception(e)
1853
1854 def get_sfi(self, sfi_id):
1855 self.logger.debug(
1856 'Getting Service Function Instance %s from VIM', sfi_id)
1857 filter_dict = {"id": sfi_id}
1858 sfi_list = self.get_sfi_list(filter_dict)
1859 if len(sfi_list) == 0:
1860 raise vimconn.vimconnNotFoundException(
1861 "Service Function Instance '{}' not found".format(sfi_id))
1862 elif len(sfi_list) > 1:
1863 raise vimconn.vimconnConflictException(
1864 'Found more than one Service Function Instance '
1865 'with this criteria')
1866 sfi = sfi_list[0]
1867 return sfi
1868
1869 def get_sfi_list(self, filter_dict={}):
1870 self.logger.debug("Getting Service Function Instances from "
1871 "VIM filter: '%s'", str(filter_dict))
1872 try:
1873 self._reload_connection()
tierno69b590e2018-03-13 18:52:23 +01001874 filter_dict_os = filter_dict.copy()
1875 if self.api_version3 and "tenant_id" in filter_dict_os:
1876 filter_dict_os['project_id'] = filter_dict_os.pop('tenant_id')
1877 sfi_dict = self.neutron.list_sfc_port_pairs(**filter_dict_os)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001878 sfi_list = sfi_dict["port_pairs"]
1879 self.__sfi_os2mano(sfi_list)
1880 return sfi_list
1881 except (neExceptions.ConnectionFailed, ksExceptions.ClientException,
1882 neExceptions.NeutronException, ConnectionError) as e:
1883 self._format_exception(e)
1884
1885 def delete_sfi(self, sfi_id):
1886 self.logger.debug("Deleting Service Function Instance '%s' "
1887 "from VIM", sfi_id)
1888 try:
1889 self._reload_connection()
Igor D.Ccaadc442017-11-06 12:48:48 +00001890 self.neutron.delete_sfc_port_pair(sfi_id)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001891 return sfi_id
1892 except (neExceptions.ConnectionFailed, neExceptions.NeutronException,
1893 ksExceptions.ClientException, neExceptions.NeutronException,
1894 ConnectionError) as e:
1895 self._format_exception(e)
1896
1897 def new_sf(self, name, sfis, sfc_encap=True):
1898 self.logger.debug("Adding a new Service Function to VIM, "
1899 "named '%s'", name)
1900 try:
1901 new_sf = None
1902 self._reload_connection()
tierno9c5c8322018-03-23 15:44:03 +01001903 # correlation = None
1904 # if sfc_encap:
1905 # correlation = 'nsh'
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001906 for instance in sfis:
1907 sfi = self.get_sfi(instance)
Igor D.Ccaadc442017-11-06 12:48:48 +00001908 if sfi.get('sfc_encap') != sfc_encap:
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001909 raise vimconn.vimconnNotSupportedException(
1910 "OpenStack VIM connector requires all SFIs of the "
1911 "same SF to share the same SFC Encapsulation")
1912 sf_dict = {'name': name,
1913 'port_pairs': sfis}
Igor D.Ccaadc442017-11-06 12:48:48 +00001914 new_sf = self.neutron.create_sfc_port_pair_group({
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001915 'port_pair_group': sf_dict})
1916 return new_sf['port_pair_group']['id']
1917 except (neExceptions.ConnectionFailed, ksExceptions.ClientException,
1918 neExceptions.NeutronException, ConnectionError) as e:
1919 if new_sf:
1920 try:
Igor D.Ccaadc442017-11-06 12:48:48 +00001921 self.neutron.delete_sfc_port_pair_group(
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001922 new_sf['port_pair_group']['id'])
1923 except Exception:
1924 self.logger.error(
1925 'Creation of Service Function failed, with '
1926 'subsequent deletion failure as well.')
1927 self._format_exception(e)
1928
1929 def get_sf(self, sf_id):
1930 self.logger.debug("Getting Service Function %s from VIM", sf_id)
1931 filter_dict = {"id": sf_id}
1932 sf_list = self.get_sf_list(filter_dict)
1933 if len(sf_list) == 0:
1934 raise vimconn.vimconnNotFoundException(
1935 "Service Function '{}' not found".format(sf_id))
1936 elif len(sf_list) > 1:
1937 raise vimconn.vimconnConflictException(
1938 "Found more than one Service Function with this criteria")
1939 sf = sf_list[0]
1940 return sf
1941
1942 def get_sf_list(self, filter_dict={}):
1943 self.logger.debug("Getting Service Function from VIM filter: '%s'",
1944 str(filter_dict))
1945 try:
1946 self._reload_connection()
tierno69b590e2018-03-13 18:52:23 +01001947 filter_dict_os = filter_dict.copy()
1948 if self.api_version3 and "tenant_id" in filter_dict_os:
1949 filter_dict_os['project_id'] = filter_dict_os.pop('tenant_id')
1950 sf_dict = self.neutron.list_sfc_port_pair_groups(**filter_dict_os)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001951 sf_list = sf_dict["port_pair_groups"]
1952 self.__sf_os2mano(sf_list)
1953 return sf_list
1954 except (neExceptions.ConnectionFailed, ksExceptions.ClientException,
1955 neExceptions.NeutronException, ConnectionError) as e:
1956 self._format_exception(e)
1957
1958 def delete_sf(self, sf_id):
1959 self.logger.debug("Deleting Service Function '%s' from VIM", sf_id)
1960 try:
1961 self._reload_connection()
Igor D.Ccaadc442017-11-06 12:48:48 +00001962 self.neutron.delete_sfc_port_pair_group(sf_id)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001963 return sf_id
1964 except (neExceptions.ConnectionFailed, neExceptions.NeutronException,
1965 ksExceptions.ClientException, neExceptions.NeutronException,
1966 ConnectionError) as e:
1967 self._format_exception(e)
1968
1969 def new_sfp(self, name, classifications, sfs, sfc_encap=True, spi=None):
1970 self.logger.debug("Adding a new Service Function Path to VIM, "
1971 "named '%s'", name)
1972 try:
1973 new_sfp = None
1974 self._reload_connection()
Igor D.Ccaadc442017-11-06 12:48:48 +00001975 # In networking-sfc the MPLS encapsulation is legacy
1976 # should be used when no full SFC Encapsulation is intended
1977 sfc_encap = 'mpls'
1978 if sfc_encap:
1979 correlation = 'nsh'
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001980 sfp_dict = {'name': name,
1981 'flow_classifiers': classifications,
1982 'port_pair_groups': sfs,
1983 'chain_parameters': {'correlation': correlation}}
1984 if spi:
1985 sfp_dict['chain_id'] = spi
Igor D.Ccaadc442017-11-06 12:48:48 +00001986 new_sfp = self.neutron.create_sfc_port_chain({'port_chain': sfp_dict})
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001987 return new_sfp["port_chain"]["id"]
1988 except (neExceptions.ConnectionFailed, ksExceptions.ClientException,
1989 neExceptions.NeutronException, ConnectionError) as e:
1990 if new_sfp:
1991 try:
Igor D.Ccaadc442017-11-06 12:48:48 +00001992 self.neutron.delete_sfc_port_chain(new_sfp['port_chain']['id'])
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001993 except Exception:
1994 self.logger.error(
1995 'Creation of Service Function Path failed, with '
1996 'subsequent deletion failure as well.')
1997 self._format_exception(e)
1998
1999 def get_sfp(self, sfp_id):
2000 self.logger.debug(" Getting Service Function Path %s from VIM", sfp_id)
2001 filter_dict = {"id": sfp_id}
2002 sfp_list = self.get_sfp_list(filter_dict)
2003 if len(sfp_list) == 0:
2004 raise vimconn.vimconnNotFoundException(
2005 "Service Function Path '{}' not found".format(sfp_id))
2006 elif len(sfp_list) > 1:
2007 raise vimconn.vimconnConflictException(
2008 "Found more than one Service Function Path with this criteria")
2009 sfp = sfp_list[0]
2010 return sfp
2011
2012 def get_sfp_list(self, filter_dict={}):
2013 self.logger.debug("Getting Service Function Paths from VIM filter: "
2014 "'%s'", str(filter_dict))
2015 try:
2016 self._reload_connection()
tierno69b590e2018-03-13 18:52:23 +01002017 filter_dict_os = filter_dict.copy()
2018 if self.api_version3 and "tenant_id" in filter_dict_os:
2019 filter_dict_os['project_id'] = filter_dict_os.pop('tenant_id')
2020 sfp_dict = self.neutron.list_sfc_port_chains(**filter_dict_os)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00002021 sfp_list = sfp_dict["port_chains"]
2022 self.__sfp_os2mano(sfp_list)
2023 return sfp_list
2024 except (neExceptions.ConnectionFailed, ksExceptions.ClientException,
2025 neExceptions.NeutronException, ConnectionError) as e:
2026 self._format_exception(e)
2027
2028 def delete_sfp(self, sfp_id):
2029 self.logger.debug(
2030 "Deleting Service Function Path '%s' from VIM", sfp_id)
2031 try:
2032 self._reload_connection()
Igor D.Ccaadc442017-11-06 12:48:48 +00002033 self.neutron.delete_sfc_port_chain(sfp_id)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00002034 return sfp_id
2035 except (neExceptions.ConnectionFailed, neExceptions.NeutronException,
2036 ksExceptions.ClientException, neExceptions.NeutronException,
2037 ConnectionError) as e:
2038 self._format_exception(e)