blob: 0d2603f69df6c23d636a48f06e3e3d9dcf13a708 [file] [log] [blame]
tierno7edb6752016-03-21 17:37:52 +01001# -*- coding: utf-8 -*-
2
3##
4# Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U.
5# This file is part of openmano
6# All Rights Reserved.
7#
8# Licensed under the Apache License, Version 2.0 (the "License"); you may
9# not use this file except in compliance with the License. You may obtain
10# a copy of the License at
11#
12# http://www.apache.org/licenses/LICENSE-2.0
13#
14# Unless required by applicable law or agreed to in writing, software
15# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17# License for the specific language governing permissions and limitations
18# under the License.
19#
20# For those usages not covered by the Apache License, Version 2.0 please
21# contact with: nfvlabs@tid.es
22##
23
24'''
Igor Duarte 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'''
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +000035__author__ = "Alfonso Tierno, Gerardo Garcia, Pablo Montes, xFlow Research, Igor D.C."
36__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
tierno7edb6752016-03-21 17:37:52 +010047
tiernob5cef372017-06-19 15:52:22 +020048from novaclient import client as nClient, exceptions as nvExceptions
49from keystoneauth1.identity import v2, v3
50from keystoneauth1 import session
tierno7edb6752016-03-21 17:37:52 +010051import keystoneclient.exceptions as ksExceptions
tiernof716aea2017-06-21 18:01:40 +020052import keystoneclient.v3.client as ksClient_v3
53import keystoneclient.v2_0.client as ksClient_v2
tiernob5cef372017-06-19 15:52:22 +020054from glanceclient import client as glClient
tierno7edb6752016-03-21 17:37:52 +010055import glanceclient.client as gl1Client
56import glanceclient.exc as gl1Exceptions
tiernob5cef372017-06-19 15:52:22 +020057from cinderclient import client as cClient
tierno7edb6752016-03-21 17:37:52 +010058from httplib import HTTPException
tiernob5cef372017-06-19 15:52:22 +020059from neutronclient.neutron import client as neClient
tierno7edb6752016-03-21 17:37:52 +010060from neutronclient.common import exceptions as neExceptions
61from requests.exceptions import ConnectionError
62
tierno40e1bce2017-08-09 09:12:04 +020063
64"""contain the openstack virtual machine status to openmano status"""
tierno7edb6752016-03-21 17:37:52 +010065vmStatus2manoFormat={'ACTIVE':'ACTIVE',
66 'PAUSED':'PAUSED',
67 'SUSPENDED': 'SUSPENDED',
68 'SHUTOFF':'INACTIVE',
69 'BUILD':'BUILD',
70 'ERROR':'ERROR','DELETED':'DELETED'
71 }
72netStatus2manoFormat={'ACTIVE':'ACTIVE','PAUSED':'PAUSED','INACTIVE':'INACTIVE','BUILD':'BUILD','ERROR':'ERROR','DELETED':'DELETED'
73 }
74
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +000075supportedClassificationTypes = ['legacy_flow_classifier']
76
montesmoreno0c8def02016-12-22 12:16:23 +000077#global var to have a timeout creating and deleting volumes
tierno00e3df72017-11-29 17:20:13 +010078volume_timeout = 600
79server_timeout = 600
montesmoreno0c8def02016-12-22 12:16:23 +000080
tierno7edb6752016-03-21 17:37:52 +010081class vimconnector(vimconn.vimconnector):
tiernob3d36742017-03-03 23:51:05 +010082 def __init__(self, uuid, name, tenant_id, tenant_name, url, url_admin=None, user=None, passwd=None,
83 log_level=None, config={}, persistent_info={}):
ahmadsa96af9f42017-01-31 16:17:14 +050084 '''using common constructor parameters. In this case
tierno7edb6752016-03-21 17:37:52 +010085 'url' is the keystone authorization url,
86 'url_admin' is not use
87 '''
tiernof716aea2017-06-21 18:01:40 +020088 api_version = config.get('APIversion')
89 if api_version and api_version not in ('v3.3', 'v2.0', '2', '3'):
tiernob5cef372017-06-19 15:52:22 +020090 raise vimconn.vimconnException("Invalid value '{}' for config:APIversion. "
tiernof716aea2017-06-21 18:01:40 +020091 "Allowed values are 'v3.3', 'v2.0', '2' or '3'".format(api_version))
kate721d79b2017-06-24 04:21:38 -070092 vim_type = config.get('vim_type')
93 if vim_type and vim_type not in ('vio', 'VIO'):
94 raise vimconn.vimconnException("Invalid value '{}' for config:vim_type."
95 "Allowed values are 'vio' or 'VIO'".format(vim_type))
96
97 if config.get('dataplane_net_vlan_range') is not None:
98 #validate vlan ranges provided by user
99 self._validate_vlan_ranges(config.get('dataplane_net_vlan_range'))
100
tiernob5cef372017-06-19 15:52:22 +0200101 vimconn.vimconnector.__init__(self, uuid, name, tenant_id, tenant_name, url, url_admin, user, passwd, log_level,
102 config)
tiernob3d36742017-03-03 23:51:05 +0100103
tierno4d1ce222018-04-06 10:41:06 +0200104 if self.config.get("insecure") and self.config.get("ca_cert"):
105 raise vimconn.vimconnException("options insecure and ca_cert are mutually exclusive")
106 self.verify = True
107 if self.config.get("insecure"):
108 self.verify = False
109 if self.config.get("ca_cert"):
110 self.verify = self.config.get("ca_cert")
tierno4d1ce222018-04-06 10:41:06 +0200111
tierno7edb6752016-03-21 17:37:52 +0100112 if not url:
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +0000113 raise TypeError('url param can not be NoneType')
tiernob5cef372017-06-19 15:52:22 +0200114 self.persistent_info = persistent_info
mirabal29356312017-07-27 12:21:22 +0200115 self.availability_zone = persistent_info.get('availability_zone', None)
tiernob5cef372017-06-19 15:52:22 +0200116 self.session = persistent_info.get('session', {'reload_client': True})
117 self.nova = self.session.get('nova')
118 self.neutron = self.session.get('neutron')
119 self.cinder = self.session.get('cinder')
120 self.glance = self.session.get('glance')
tierno1beea862018-07-11 15:47:37 +0200121 # self.glancev1 = self.session.get('glancev1')
tiernof716aea2017-06-21 18:01:40 +0200122 self.keystone = self.session.get('keystone')
123 self.api_version3 = self.session.get('api_version3')
kate721d79b2017-06-24 04:21:38 -0700124 self.vim_type = self.config.get("vim_type")
125 if self.vim_type:
126 self.vim_type = self.vim_type.upper()
127 if self.config.get("use_internal_endpoint"):
128 self.endpoint_type = "internalURL"
129 else:
130 self.endpoint_type = None
montesmoreno0c8def02016-12-22 12:16:23 +0000131
tierno73ad9e42016-09-12 18:11:11 +0200132 self.logger = logging.getLogger('openmano.vim.openstack')
kate721d79b2017-06-24 04:21:38 -0700133
134 ####### VIO Specific Changes #########
135 if self.vim_type == "VIO":
136 self.logger = logging.getLogger('openmano.vim.vio')
137
tiernofe789902016-09-29 14:20:44 +0000138 if log_level:
kate54616752017-09-05 23:26:28 -0700139 self.logger.setLevel( getattr(logging, log_level))
tiernof716aea2017-06-21 18:01:40 +0200140
141 def __getitem__(self, index):
142 """Get individuals parameters.
143 Throw KeyError"""
144 if index == 'project_domain_id':
145 return self.config.get("project_domain_id")
146 elif index == 'user_domain_id':
147 return self.config.get("user_domain_id")
148 else:
tierno76a3c312017-06-29 16:42:15 +0200149 return vimconn.vimconnector.__getitem__(self, index)
tiernof716aea2017-06-21 18:01:40 +0200150
151 def __setitem__(self, index, value):
152 """Set individuals parameters and it is marked as dirty so to force connection reload.
153 Throw KeyError"""
154 if index == 'project_domain_id':
155 self.config["project_domain_id"] = value
156 elif index == 'user_domain_id':
157 self.config["user_domain_id"] = value
158 else:
159 vimconn.vimconnector.__setitem__(self, index, value)
tiernob5cef372017-06-19 15:52:22 +0200160 self.session['reload_client'] = True
tiernof716aea2017-06-21 18:01:40 +0200161
tierno7edb6752016-03-21 17:37:52 +0100162 def _reload_connection(self):
163 '''Called before any operation, it check if credentials has changed
164 Throw keystoneclient.apiclient.exceptions.AuthorizationFailure
165 '''
166 #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 +0200167 if self.session['reload_client']:
tiernof716aea2017-06-21 18:01:40 +0200168 if self.config.get('APIversion'):
169 self.api_version3 = self.config['APIversion'] == 'v3.3' or self.config['APIversion'] == '3'
170 else: # get from ending auth_url that end with v3 or with v2.0
tierno3cb8dc32017-10-24 18:13:19 +0200171 self.api_version3 = self.url.endswith("/v3") or self.url.endswith("/v3/")
tiernof716aea2017-06-21 18:01:40 +0200172 self.session['api_version3'] = self.api_version3
173 if self.api_version3:
tierno3cb8dc32017-10-24 18:13:19 +0200174 if self.config.get('project_domain_id') or self.config.get('project_domain_name'):
175 project_domain_id_default = None
176 else:
177 project_domain_id_default = 'default'
178 if self.config.get('user_domain_id') or self.config.get('user_domain_name'):
179 user_domain_id_default = None
180 else:
181 user_domain_id_default = 'default'
tiernof716aea2017-06-21 18:01:40 +0200182 auth = v3.Password(auth_url=self.url,
tiernob5cef372017-06-19 15:52:22 +0200183 username=self.user,
184 password=self.passwd,
185 project_name=self.tenant_name,
186 project_id=self.tenant_id,
tierno3cb8dc32017-10-24 18:13:19 +0200187 project_domain_id=self.config.get('project_domain_id', project_domain_id_default),
188 user_domain_id=self.config.get('user_domain_id', user_domain_id_default),
189 project_domain_name=self.config.get('project_domain_name'),
190 user_domain_name=self.config.get('user_domain_name'))
ahmadsa95baa272016-11-30 09:14:11 +0500191 else:
tiernof716aea2017-06-21 18:01:40 +0200192 auth = v2.Password(auth_url=self.url,
tiernob5cef372017-06-19 15:52:22 +0200193 username=self.user,
194 password=self.passwd,
195 tenant_name=self.tenant_name,
196 tenant_id=self.tenant_id)
tierno4d1ce222018-04-06 10:41:06 +0200197 sess = session.Session(auth=auth, verify=self.verify)
tiernof716aea2017-06-21 18:01:40 +0200198 if self.api_version3:
kate721d79b2017-06-24 04:21:38 -0700199 self.keystone = ksClient_v3.Client(session=sess, endpoint_type=self.endpoint_type)
tiernof716aea2017-06-21 18:01:40 +0200200 else:
kate721d79b2017-06-24 04:21:38 -0700201 self.keystone = ksClient_v2.Client(session=sess, endpoint_type=self.endpoint_type)
tiernof716aea2017-06-21 18:01:40 +0200202 self.session['keystone'] = self.keystone
montesmoreno9317d302017-08-16 12:48:23 +0200203 # In order to enable microversion functionality an explicit microversion must be specified in 'config'.
204 # This implementation approach is due to the warning message in
205 # https://developer.openstack.org/api-guide/compute/microversions.html
206 # where it is stated that microversion backwards compatibility is not guaranteed and clients should
207 # always require an specific microversion.
208 # To be able to use 'device role tagging' functionality define 'microversion: 2.32' in datacenter config
209 version = self.config.get("microversion")
210 if not version:
211 version = "2.1"
kate54616752017-09-05 23:26:28 -0700212 self.nova = self.session['nova'] = nClient.Client(str(version), session=sess, endpoint_type=self.endpoint_type)
kate721d79b2017-06-24 04:21:38 -0700213 self.neutron = self.session['neutron'] = neClient.Client('2.0', session=sess, endpoint_type=self.endpoint_type)
214 self.cinder = self.session['cinder'] = cClient.Client(2, session=sess, endpoint_type=self.endpoint_type)
215 if self.endpoint_type == "internalURL":
216 glance_service_id = self.keystone.services.list(name="glance")[0].id
217 glance_endpoint = self.keystone.endpoints.list(glance_service_id, interface="internal")[0].url
218 else:
219 glance_endpoint = None
220 self.glance = self.session['glance'] = glClient.Client(2, session=sess, endpoint=glance_endpoint)
221 #using version 1 of glance client in new_image()
tierno1beea862018-07-11 15:47:37 +0200222 # self.glancev1 = self.session['glancev1'] = glClient.Client('1', session=sess,
223 # endpoint=glance_endpoint)
tiernob5cef372017-06-19 15:52:22 +0200224 self.session['reload_client'] = False
225 self.persistent_info['session'] = self.session
mirabal29356312017-07-27 12:21:22 +0200226 # add availablity zone info inside self.persistent_info
227 self._set_availablity_zones()
228 self.persistent_info['availability_zone'] = self.availability_zone
ahmadsa95baa272016-11-30 09:14:11 +0500229
tierno7edb6752016-03-21 17:37:52 +0100230 def __net_os2mano(self, net_list_dict):
231 '''Transform the net openstack format to mano format
232 net_list_dict can be a list of dict or a single dict'''
233 if type(net_list_dict) is dict:
234 net_list_=(net_list_dict,)
235 elif type(net_list_dict) is list:
236 net_list_=net_list_dict
237 else:
238 raise TypeError("param net_list_dict must be a list or a dictionary")
239 for net in net_list_:
240 if net.get('provider:network_type') == "vlan":
241 net['type']='data'
242 else:
243 net['type']='bridge'
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +0200244
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +0000245 def __classification_os2mano(self, class_list_dict):
246 """Transform the openstack format (Flow Classifier) to mano format
247 (Classification) class_list_dict can be a list of dict or a single dict
248 """
249 if isinstance(class_list_dict, dict):
250 class_list_ = [class_list_dict]
251 elif isinstance(class_list_dict, list):
252 class_list_ = class_list_dict
253 else:
254 raise TypeError(
255 "param class_list_dict must be a list or a dictionary")
256 for classification in class_list_:
257 id = classification.pop('id')
258 name = classification.pop('name')
259 description = classification.pop('description')
260 project_id = classification.pop('project_id')
261 tenant_id = classification.pop('tenant_id')
262 original_classification = copy.deepcopy(classification)
263 classification.clear()
264 classification['ctype'] = 'legacy_flow_classifier'
265 classification['definition'] = original_classification
266 classification['id'] = id
267 classification['name'] = name
268 classification['description'] = description
269 classification['project_id'] = project_id
270 classification['tenant_id'] = tenant_id
271
272 def __sfi_os2mano(self, sfi_list_dict):
273 """Transform the openstack format (Port Pair) to mano format (SFI)
274 sfi_list_dict can be a list of dict or a single dict
275 """
276 if isinstance(sfi_list_dict, dict):
277 sfi_list_ = [sfi_list_dict]
278 elif isinstance(sfi_list_dict, list):
279 sfi_list_ = sfi_list_dict
280 else:
281 raise TypeError(
282 "param sfi_list_dict must be a list or a dictionary")
283 for sfi in sfi_list_:
284 sfi['ingress_ports'] = []
285 sfi['egress_ports'] = []
286 if sfi.get('ingress'):
287 sfi['ingress_ports'].append(sfi['ingress'])
288 if sfi.get('egress'):
289 sfi['egress_ports'].append(sfi['egress'])
290 del sfi['ingress']
291 del sfi['egress']
292 params = sfi.get('service_function_parameters')
293 sfc_encap = False
294 if params:
295 correlation = params.get('correlation')
296 if correlation:
297 sfc_encap = True
298 sfi['sfc_encap'] = sfc_encap
299 del sfi['service_function_parameters']
300
301 def __sf_os2mano(self, sf_list_dict):
302 """Transform the openstack format (Port Pair Group) to mano format (SF)
303 sf_list_dict can be a list of dict or a single dict
304 """
305 if isinstance(sf_list_dict, dict):
306 sf_list_ = [sf_list_dict]
307 elif isinstance(sf_list_dict, list):
308 sf_list_ = sf_list_dict
309 else:
310 raise TypeError(
311 "param sf_list_dict must be a list or a dictionary")
312 for sf in sf_list_:
313 del sf['port_pair_group_parameters']
314 sf['sfis'] = sf['port_pairs']
315 del sf['port_pairs']
316
317 def __sfp_os2mano(self, sfp_list_dict):
318 """Transform the openstack format (Port Chain) to mano format (SFP)
319 sfp_list_dict can be a list of dict or a single dict
320 """
321 if isinstance(sfp_list_dict, dict):
322 sfp_list_ = [sfp_list_dict]
323 elif isinstance(sfp_list_dict, list):
324 sfp_list_ = sfp_list_dict
325 else:
326 raise TypeError(
327 "param sfp_list_dict must be a list or a dictionary")
328 for sfp in sfp_list_:
329 params = sfp.pop('chain_parameters')
330 sfc_encap = False
331 if params:
332 correlation = params.get('correlation')
333 if correlation:
334 sfc_encap = True
335 sfp['sfc_encap'] = sfc_encap
336 sfp['spi'] = sfp.pop('chain_id')
337 sfp['classifications'] = sfp.pop('flow_classifiers')
338 sfp['service_functions'] = sfp.pop('port_pair_groups')
339
340 # placeholder for now; read TODO note below
341 def _validate_classification(self, type, definition):
342 # only legacy_flow_classifier Type is supported at this point
343 return True
344 # TODO(igordcard): this method should be an abstract method of an
345 # abstract Classification class to be implemented by the specific
346 # Types. Also, abstract vimconnector should call the validation
347 # method before the implemented VIM connectors are called.
348
tiernoae4a8d12016-07-08 12:30:39 +0200349 def _format_exception(self, exception):
350 '''Transform a keystone, nova, neutron exception into a vimconn exception'''
351 if isinstance(exception, (HTTPException, gl1Exceptions.HTTPException, gl1Exceptions.CommunicationError,
tierno8e995ce2016-09-22 08:13:00 +0000352 ConnectionError, ksExceptions.ConnectionError, neExceptions.ConnectionFailed
353 )):
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +0000354 raise vimconn.vimconnConnectionException(type(exception).__name__ + ": " + str(exception))
355 elif isinstance(exception, (nvExceptions.ClientException, ksExceptions.ClientException,
tiernoae4a8d12016-07-08 12:30:39 +0200356 neExceptions.NeutronException, nvExceptions.BadRequest)):
357 raise vimconn.vimconnUnexpectedResponse(type(exception).__name__ + ": " + str(exception))
358 elif isinstance(exception, (neExceptions.NetworkNotFoundClient, nvExceptions.NotFound)):
359 raise vimconn.vimconnNotFoundException(type(exception).__name__ + ": " + str(exception))
360 elif isinstance(exception, nvExceptions.Conflict):
361 raise vimconn.vimconnConflictException(type(exception).__name__ + ": " + str(exception))
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +0200362 elif isinstance(exception, vimconn.vimconnException):
tierno41a69812018-02-16 14:34:33 +0100363 raise exception
tiernof716aea2017-06-21 18:01:40 +0200364 else: # ()
tiernob84cbdc2017-07-07 14:30:30 +0200365 self.logger.error("General Exception " + str(exception), exc_info=True)
tiernoae4a8d12016-07-08 12:30:39 +0200366 raise vimconn.vimconnConnectionException(type(exception).__name__ + ": " + str(exception))
367
368 def get_tenant_list(self, filter_dict={}):
369 '''Obtain tenants of VIM
370 filter_dict can contain the following keys:
371 name: filter by tenant name
372 id: filter by tenant uuid/id
373 <other VIM specific>
374 Returns the tenant list of dictionaries: [{'name':'<name>, 'id':'<id>, ...}, ...]
375 '''
ahmadsa95baa272016-11-30 09:14:11 +0500376 self.logger.debug("Getting tenants from VIM filter: '%s'", str(filter_dict))
tiernoae4a8d12016-07-08 12:30:39 +0200377 try:
378 self._reload_connection()
tiernof716aea2017-06-21 18:01:40 +0200379 if self.api_version3:
380 project_class_list = self.keystone.projects.list(name=filter_dict.get("name"))
ahmadsa95baa272016-11-30 09:14:11 +0500381 else:
tiernof716aea2017-06-21 18:01:40 +0200382 project_class_list = self.keystone.tenants.findall(**filter_dict)
ahmadsa95baa272016-11-30 09:14:11 +0500383 project_list=[]
384 for project in project_class_list:
tiernof716aea2017-06-21 18:01:40 +0200385 if filter_dict.get('id') and filter_dict["id"] != project.id:
386 continue
ahmadsa95baa272016-11-30 09:14:11 +0500387 project_list.append(project.to_dict())
388 return project_list
tiernof716aea2017-06-21 18:01:40 +0200389 except (ksExceptions.ConnectionError, ksExceptions.ClientException, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200390 self._format_exception(e)
391
392 def new_tenant(self, tenant_name, tenant_description):
393 '''Adds a new tenant to openstack VIM. Returns the tenant identifier'''
394 self.logger.debug("Adding a new tenant name: %s", tenant_name)
395 try:
396 self._reload_connection()
tiernof716aea2017-06-21 18:01:40 +0200397 if self.api_version3:
398 project = self.keystone.projects.create(tenant_name, self.config.get("project_domain_id", "default"),
399 description=tenant_description, is_domain=False)
ahmadsa95baa272016-11-30 09:14:11 +0500400 else:
tiernof716aea2017-06-21 18:01:40 +0200401 project = self.keystone.tenants.create(tenant_name, tenant_description)
ahmadsa95baa272016-11-30 09:14:11 +0500402 return project.id
tierno8e995ce2016-09-22 08:13:00 +0000403 except (ksExceptions.ConnectionError, ksExceptions.ClientException, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200404 self._format_exception(e)
405
406 def delete_tenant(self, tenant_id):
407 '''Delete a tenant from openstack VIM. Returns the old tenant identifier'''
408 self.logger.debug("Deleting tenant %s from VIM", tenant_id)
409 try:
410 self._reload_connection()
tiernof716aea2017-06-21 18:01:40 +0200411 if self.api_version3:
ahmadsa95baa272016-11-30 09:14:11 +0500412 self.keystone.projects.delete(tenant_id)
413 else:
414 self.keystone.tenants.delete(tenant_id)
tiernoae4a8d12016-07-08 12:30:39 +0200415 return tenant_id
tierno8e995ce2016-09-22 08:13:00 +0000416 except (ksExceptions.ConnectionError, ksExceptions.ClientException, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200417 self._format_exception(e)
ahmadsa95baa272016-11-30 09:14:11 +0500418
garciadeblas9f8456e2016-09-05 05:02:59 +0200419 def new_network(self,net_name, net_type, ip_profile=None, shared=False, vlan=None):
tiernoae4a8d12016-07-08 12:30:39 +0200420 '''Adds a tenant network to VIM. Returns the network identifier'''
421 self.logger.debug("Adding a new network to VIM name '%s', type '%s'", net_name, net_type)
garciadeblasedca7b32016-09-29 14:01:52 +0000422 #self.logger.debug(">>>>>>>>>>>>>>>>>> IP profile %s", str(ip_profile))
tierno7edb6752016-03-21 17:37:52 +0100423 try:
garciadeblasedca7b32016-09-29 14:01:52 +0000424 new_net = None
tierno7edb6752016-03-21 17:37:52 +0100425 self._reload_connection()
426 network_dict = {'name': net_name, 'admin_state_up': True}
427 if net_type=="data" or net_type=="ptp":
428 if self.config.get('dataplane_physical_net') == None:
tiernoae4a8d12016-07-08 12:30:39 +0200429 raise vimconn.vimconnConflictException("You must provide a 'dataplane_physical_net' at config value before creating sriov network")
tierno7edb6752016-03-21 17:37:52 +0100430 network_dict["provider:physical_network"] = self.config['dataplane_physical_net'] #"physnet_sriov" #TODO physical
431 network_dict["provider:network_type"] = "vlan"
432 if vlan!=None:
433 network_dict["provider:network_type"] = vlan
kate721d79b2017-06-24 04:21:38 -0700434
435 ####### VIO Specific Changes #########
436 if self.vim_type == "VIO":
437 if vlan is not None:
438 network_dict["provider:segmentation_id"] = vlan
439 else:
440 if self.config.get('dataplane_net_vlan_range') is None:
441 raise vimconn.vimconnConflictException("You must provide "\
442 "'dataplane_net_vlan_range' in format [start_ID - end_ID]"\
443 "at config value before creating sriov network with vlan tag")
444
445 network_dict["provider:segmentation_id"] = self._genrate_vlanID()
446
tiernoae4a8d12016-07-08 12:30:39 +0200447 network_dict["shared"]=shared
tierno7edb6752016-03-21 17:37:52 +0100448 new_net=self.neutron.create_network({'network':network_dict})
449 #print new_net
garciadeblas9f8456e2016-09-05 05:02:59 +0200450 #create subnetwork, even if there is no profile
451 if not ip_profile:
452 ip_profile = {}
tierno41a69812018-02-16 14:34:33 +0100453 if not ip_profile.get('subnet_address'):
garciadeblas2299e3b2017-01-26 14:35:55 +0000454 #Fake subnet is required
455 subnet_rand = random.randint(0, 255)
456 ip_profile['subnet_address'] = "192.168.{}.0/24".format(subnet_rand)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +0000457 if 'ip_version' not in ip_profile:
garciadeblas9f8456e2016-09-05 05:02:59 +0200458 ip_profile['ip_version'] = "IPv4"
tiernoa1fb4462017-06-30 12:25:50 +0200459 subnet = {"name":net_name+"-subnet",
tierno7edb6752016-03-21 17:37:52 +0100460 "network_id": new_net["network"]["id"],
garciadeblas9f8456e2016-09-05 05:02:59 +0200461 "ip_version": 4 if ip_profile['ip_version']=="IPv4" else 6,
462 "cidr": ip_profile['subnet_address']
tierno7edb6752016-03-21 17:37:52 +0100463 }
tiernoa1fb4462017-06-30 12:25:50 +0200464 # Gateway should be set to None if not needed. Otherwise openstack assigns one by default
tierno41a69812018-02-16 14:34:33 +0100465 if ip_profile.get('gateway_address'):
tierno55d234c2018-07-04 18:29:21 +0200466 subnet['gateway_ip'] = ip_profile['gateway_address']
467 else:
468 subnet['gateway_ip'] = None
garciadeblasedca7b32016-09-29 14:01:52 +0000469 if ip_profile.get('dns_address'):
tierno455612d2017-05-30 16:40:10 +0200470 subnet['dns_nameservers'] = ip_profile['dns_address'].split(";")
garciadeblas9f8456e2016-09-05 05:02:59 +0200471 if 'dhcp_enabled' in ip_profile:
tierno41a69812018-02-16 14:34:33 +0100472 subnet['enable_dhcp'] = False if \
473 ip_profile['dhcp_enabled']=="false" or ip_profile['dhcp_enabled']==False else True
474 if ip_profile.get('dhcp_start_address'):
tiernoa1fb4462017-06-30 12:25:50 +0200475 subnet['allocation_pools'] = []
garciadeblas9f8456e2016-09-05 05:02:59 +0200476 subnet['allocation_pools'].append(dict())
477 subnet['allocation_pools'][0]['start'] = ip_profile['dhcp_start_address']
tierno41a69812018-02-16 14:34:33 +0100478 if ip_profile.get('dhcp_count'):
garciadeblas9f8456e2016-09-05 05:02:59 +0200479 #parts = ip_profile['dhcp_start_address'].split('.')
480 #ip_int = (int(parts[0]) << 24) + (int(parts[1]) << 16) + (int(parts[2]) << 8) + int(parts[3])
481 ip_int = int(netaddr.IPAddress(ip_profile['dhcp_start_address']))
garciadeblas21d795b2016-09-29 17:31:46 +0200482 ip_int += ip_profile['dhcp_count'] - 1
garciadeblas9f8456e2016-09-05 05:02:59 +0200483 ip_str = str(netaddr.IPAddress(ip_int))
484 subnet['allocation_pools'][0]['end'] = ip_str
garciadeblasedca7b32016-09-29 14:01:52 +0000485 #self.logger.debug(">>>>>>>>>>>>>>>>>> Subnet: %s", str(subnet))
tierno7edb6752016-03-21 17:37:52 +0100486 self.neutron.create_subnet({"subnet": subnet} )
tiernoae4a8d12016-07-08 12:30:39 +0200487 return new_net["network"]["id"]
tierno41a69812018-02-16 14:34:33 +0100488 except Exception as e:
garciadeblasedca7b32016-09-29 14:01:52 +0000489 if new_net:
490 self.neutron.delete_network(new_net['network']['id'])
tiernoae4a8d12016-07-08 12:30:39 +0200491 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100492
493 def get_network_list(self, filter_dict={}):
494 '''Obtain tenant networks of VIM
495 Filter_dict can be:
496 name: network name
497 id: network uuid
498 shared: boolean
499 tenant_id: tenant
500 admin_state_up: boolean
501 status: 'ACTIVE'
502 Returns the network list of dictionaries
503 '''
tiernoae4a8d12016-07-08 12:30:39 +0200504 self.logger.debug("Getting network from VIM filter: '%s'", str(filter_dict))
tierno7edb6752016-03-21 17:37:52 +0100505 try:
506 self._reload_connection()
tierno69b590e2018-03-13 18:52:23 +0100507 filter_dict_os = filter_dict.copy()
508 if self.api_version3 and "tenant_id" in filter_dict_os:
509 filter_dict_os['project_id'] = filter_dict_os.pop('tenant_id') #T ODO check
510 net_dict = self.neutron.list_networks(**filter_dict_os)
tierno00e3df72017-11-29 17:20:13 +0100511 net_list = net_dict["networks"]
tierno7edb6752016-03-21 17:37:52 +0100512 self.__net_os2mano(net_list)
tiernoae4a8d12016-07-08 12:30:39 +0200513 return net_list
tierno8e995ce2016-09-22 08:13:00 +0000514 except (neExceptions.ConnectionFailed, ksExceptions.ClientException, neExceptions.NeutronException, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200515 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100516
tiernoae4a8d12016-07-08 12:30:39 +0200517 def get_network(self, net_id):
518 '''Obtain details of network from VIM
519 Returns the network information from a network id'''
520 self.logger.debug(" Getting tenant network %s from VIM", net_id)
tierno7edb6752016-03-21 17:37:52 +0100521 filter_dict={"id": net_id}
tiernoae4a8d12016-07-08 12:30:39 +0200522 net_list = self.get_network_list(filter_dict)
tierno7edb6752016-03-21 17:37:52 +0100523 if len(net_list)==0:
tiernoae4a8d12016-07-08 12:30:39 +0200524 raise vimconn.vimconnNotFoundException("Network '{}' not found".format(net_id))
tierno7edb6752016-03-21 17:37:52 +0100525 elif len(net_list)>1:
tiernoae4a8d12016-07-08 12:30:39 +0200526 raise vimconn.vimconnConflictException("Found more than one network with this criteria")
tierno7edb6752016-03-21 17:37:52 +0100527 net = net_list[0]
528 subnets=[]
529 for subnet_id in net.get("subnets", () ):
530 try:
531 subnet = self.neutron.show_subnet(subnet_id)
532 except Exception as e:
tiernoae4a8d12016-07-08 12:30:39 +0200533 self.logger.error("osconnector.get_network(): Error getting subnet %s %s" % (net_id, str(e)))
534 subnet = {"id": subnet_id, "fault": str(e)}
tierno7edb6752016-03-21 17:37:52 +0100535 subnets.append(subnet)
536 net["subnets"] = subnets
Pablo Montes Moreno51e553b2017-03-23 16:39:12 +0100537 net["encapsulation"] = net.get('provider:network_type')
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +0100538 net["segmentation_id"] = net.get('provider:segmentation_id')
tiernoae4a8d12016-07-08 12:30:39 +0200539 return net
tierno7edb6752016-03-21 17:37:52 +0100540
tiernoae4a8d12016-07-08 12:30:39 +0200541 def delete_network(self, net_id):
542 '''Deletes a tenant network from VIM. Returns the old network identifier'''
543 self.logger.debug("Deleting network '%s' from VIM", net_id)
tierno7edb6752016-03-21 17:37:52 +0100544 try:
545 self._reload_connection()
546 #delete VM ports attached to this networks before the network
547 ports = self.neutron.list_ports(network_id=net_id)
548 for p in ports['ports']:
549 try:
550 self.neutron.delete_port(p["id"])
551 except Exception as e:
tiernoae4a8d12016-07-08 12:30:39 +0200552 self.logger.error("Error deleting port %s: %s", p["id"], str(e))
tierno7edb6752016-03-21 17:37:52 +0100553 self.neutron.delete_network(net_id)
tiernoae4a8d12016-07-08 12:30:39 +0200554 return net_id
555 except (neExceptions.ConnectionFailed, neExceptions.NetworkNotFoundClient, neExceptions.NeutronException,
tierno8e995ce2016-09-22 08:13:00 +0000556 ksExceptions.ClientException, neExceptions.NeutronException, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200557 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100558
tiernoae4a8d12016-07-08 12:30:39 +0200559 def refresh_nets_status(self, net_list):
560 '''Get the status of the networks
561 Params: the list of network identifiers
562 Returns a dictionary with:
563 net_id: #VIM id of this network
564 status: #Mandatory. Text with one of:
565 # DELETED (not found at vim)
566 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
567 # OTHER (Vim reported other status not understood)
568 # ERROR (VIM indicates an ERROR status)
569 # ACTIVE, INACTIVE, DOWN (admin down),
570 # BUILD (on building process)
571 #
572 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
573 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
574
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +0000575 '''
tiernoae4a8d12016-07-08 12:30:39 +0200576 net_dict={}
577 for net_id in net_list:
578 net = {}
579 try:
580 net_vim = self.get_network(net_id)
581 if net_vim['status'] in netStatus2manoFormat:
582 net["status"] = netStatus2manoFormat[ net_vim['status'] ]
583 else:
584 net["status"] = "OTHER"
585 net["error_msg"] = "VIM status reported " + net_vim['status']
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +0000586
tierno8e995ce2016-09-22 08:13:00 +0000587 if net['status'] == "ACTIVE" and not net_vim['admin_state_up']:
tiernoae4a8d12016-07-08 12:30:39 +0200588 net['status'] = 'DOWN'
tierno8e995ce2016-09-22 08:13:00 +0000589 try:
590 net['vim_info'] = yaml.safe_dump(net_vim, default_flow_style=True, width=256)
591 except yaml.representer.RepresenterError:
592 net['vim_info'] = str(net_vim)
tiernoae4a8d12016-07-08 12:30:39 +0200593 if net_vim.get('fault'): #TODO
594 net['error_msg'] = str(net_vim['fault'])
595 except vimconn.vimconnNotFoundException as e:
596 self.logger.error("Exception getting net status: %s", str(e))
597 net['status'] = "DELETED"
598 net['error_msg'] = str(e)
599 except vimconn.vimconnException as e:
600 self.logger.error("Exception getting net status: %s", str(e))
601 net['status'] = "VIM_ERROR"
602 net['error_msg'] = str(e)
603 net_dict[net_id] = net
604 return net_dict
605
606 def get_flavor(self, flavor_id):
607 '''Obtain flavor details from the VIM. Returns the flavor dict details'''
608 self.logger.debug("Getting flavor '%s'", flavor_id)
tierno7edb6752016-03-21 17:37:52 +0100609 try:
610 self._reload_connection()
611 flavor = self.nova.flavors.find(id=flavor_id)
612 #TODO parse input and translate to VIM format (openmano_schemas.new_vminstance_response_schema)
tiernoae4a8d12016-07-08 12:30:39 +0200613 return flavor.to_dict()
tierno8e995ce2016-09-22 08:13:00 +0000614 except (nvExceptions.NotFound, nvExceptions.ClientException, ksExceptions.ClientException, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200615 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100616
tiernocf157a82017-01-30 14:07:06 +0100617 def get_flavor_id_from_data(self, flavor_dict):
618 """Obtain flavor id that match the flavor description
619 Returns the flavor_id or raises a vimconnNotFoundException
tiernoe26fc7a2017-05-30 14:43:03 +0200620 flavor_dict: contains the required ram, vcpus, disk
621 If 'use_existing_flavors' is set to True at config, the closer flavor that provides same or more ram, vcpus
622 and disk is returned. Otherwise a flavor with exactly same ram, vcpus and disk is returned or a
623 vimconnNotFoundException is raised
tiernocf157a82017-01-30 14:07:06 +0100624 """
tiernoe26fc7a2017-05-30 14:43:03 +0200625 exact_match = False if self.config.get('use_existing_flavors') else True
tiernocf157a82017-01-30 14:07:06 +0100626 try:
627 self._reload_connection()
tiernoe26fc7a2017-05-30 14:43:03 +0200628 flavor_candidate_id = None
629 flavor_candidate_data = (10000, 10000, 10000)
630 flavor_target = (flavor_dict["ram"], flavor_dict["vcpus"], flavor_dict["disk"])
631 # numa=None
632 numas = flavor_dict.get("extended", {}).get("numas")
tiernocf157a82017-01-30 14:07:06 +0100633 if numas:
634 #TODO
635 raise vimconn.vimconnNotFoundException("Flavor with EPA still not implemted")
636 # if len(numas) > 1:
637 # raise vimconn.vimconnNotFoundException("Cannot find any flavor with more than one numa")
638 # numa=numas[0]
639 # numas = extended.get("numas")
640 for flavor in self.nova.flavors.list():
641 epa = flavor.get_keys()
642 if epa:
643 continue
tiernoe26fc7a2017-05-30 14:43:03 +0200644 # TODO
645 flavor_data = (flavor.ram, flavor.vcpus, flavor.disk)
646 if flavor_data == flavor_target:
647 return flavor.id
648 elif not exact_match and flavor_target < flavor_data < flavor_candidate_data:
649 flavor_candidate_id = flavor.id
650 flavor_candidate_data = flavor_data
651 if not exact_match and flavor_candidate_id:
652 return flavor_candidate_id
tiernocf157a82017-01-30 14:07:06 +0100653 raise vimconn.vimconnNotFoundException("Cannot find any flavor matching '{}'".format(str(flavor_dict)))
654 except (nvExceptions.NotFound, nvExceptions.ClientException, ksExceptions.ClientException, ConnectionError) as e:
655 self._format_exception(e)
656
tiernoae4a8d12016-07-08 12:30:39 +0200657 def new_flavor(self, flavor_data, change_name_if_used=True):
tierno7edb6752016-03-21 17:37:52 +0100658 '''Adds a tenant flavor to openstack VIM
tiernoae4a8d12016-07-08 12:30:39 +0200659 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 +0100660 Returns the flavor identifier
661 '''
tiernoae4a8d12016-07-08 12:30:39 +0200662 self.logger.debug("Adding flavor '%s'", str(flavor_data))
tierno7edb6752016-03-21 17:37:52 +0100663 retry=0
tiernoae4a8d12016-07-08 12:30:39 +0200664 max_retries=3
tierno7edb6752016-03-21 17:37:52 +0100665 name_suffix = 0
tiernoae4a8d12016-07-08 12:30:39 +0200666 name=flavor_data['name']
667 while retry<max_retries:
tierno7edb6752016-03-21 17:37:52 +0100668 retry+=1
669 try:
670 self._reload_connection()
671 if change_name_if_used:
672 #get used names
673 fl_names=[]
674 fl=self.nova.flavors.list()
675 for f in fl:
676 fl_names.append(f.name)
677 while name in fl_names:
678 name_suffix += 1
tiernoae4a8d12016-07-08 12:30:39 +0200679 name = flavor_data['name']+"-" + str(name_suffix)
kate721d79b2017-06-24 04:21:38 -0700680
tiernoae4a8d12016-07-08 12:30:39 +0200681 ram = flavor_data.get('ram',64)
682 vcpus = flavor_data.get('vcpus',1)
tierno7edb6752016-03-21 17:37:52 +0100683 numa_properties=None
684
tiernoae4a8d12016-07-08 12:30:39 +0200685 extended = flavor_data.get("extended")
tierno7edb6752016-03-21 17:37:52 +0100686 if extended:
687 numas=extended.get("numas")
688 if numas:
689 numa_nodes = len(numas)
690 if numa_nodes > 1:
691 return -1, "Can not add flavor with more than one numa"
692 numa_properties = {"hw:numa_nodes":str(numa_nodes)}
693 numa_properties["hw:mem_page_size"] = "large"
694 numa_properties["hw:cpu_policy"] = "dedicated"
695 numa_properties["hw:numa_mempolicy"] = "strict"
kate721d79b2017-06-24 04:21:38 -0700696 if self.vim_type == "VIO":
697 numa_properties["vmware:extra_config"] = '{"numa.nodeAffinity":"0"}'
698 numa_properties["vmware:latency_sensitivity_level"] = "high"
tierno7edb6752016-03-21 17:37:52 +0100699 for numa in numas:
700 #overwrite ram and vcpus
dhumalae3b28d2017-11-22 21:41:41 -0800701 #check if key 'memory' is present in numa else use ram value at flavor
702 if 'memory' in numa:
703 ram = numa['memory']*1024
Pablo Montes Morenoea1d6232017-05-24 11:33:24 +0200704 #See for reference: https://specs.openstack.org/openstack/nova-specs/specs/mitaka/implemented/virt-driver-cpu-thread-pinning.html
tierno7edb6752016-03-21 17:37:52 +0100705 if 'paired-threads' in numa:
706 vcpus = numa['paired-threads']*2
Pablo Montes Morenoea1d6232017-05-24 11:33:24 +0200707 #cpu_thread_policy "require" implies that the compute node must have an STM architecture
708 numa_properties["hw:cpu_thread_policy"] = "require"
709 numa_properties["hw:cpu_policy"] = "dedicated"
tierno7edb6752016-03-21 17:37:52 +0100710 elif 'cores' in numa:
711 vcpus = numa['cores']
Pablo Montes Morenoea1d6232017-05-24 11:33:24 +0200712 # cpu_thread_policy "prefer" implies that the host must not have an SMT architecture, or a non-SMT architecture will be emulated
713 numa_properties["hw:cpu_thread_policy"] = "isolate"
714 numa_properties["hw:cpu_policy"] = "dedicated"
tierno7edb6752016-03-21 17:37:52 +0100715 elif 'threads' in numa:
716 vcpus = numa['threads']
Pablo Montes Morenoea1d6232017-05-24 11:33:24 +0200717 # cpu_thread_policy "prefer" implies that the host may or may not have an SMT architecture
718 numa_properties["hw:cpu_thread_policy"] = "prefer"
719 numa_properties["hw:cpu_policy"] = "dedicated"
Pablo Montes Moreno3be0b2a2017-03-30 13:22:15 +0200720 # for interface in numa.get("interfaces",() ):
721 # if interface["dedicated"]=="yes":
722 # raise vimconn.vimconnException("Passthrough interfaces are not supported for the openstack connector", http_code=vimconn.HTTP_Service_Unavailable)
723 # #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 +0000724
tierno7edb6752016-03-21 17:37:52 +0100725 #create flavor
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +0000726 new_flavor=self.nova.flavors.create(name,
727 ram,
728 vcpus,
garciadeblas79d1a1a2017-12-11 16:07:07 +0100729 flavor_data.get('disk',0),
tiernoae4a8d12016-07-08 12:30:39 +0200730 is_public=flavor_data.get('is_public', True)
kate721d79b2017-06-24 04:21:38 -0700731 )
tierno7edb6752016-03-21 17:37:52 +0100732 #add metadata
733 if numa_properties:
734 new_flavor.set_keys(numa_properties)
tiernoae4a8d12016-07-08 12:30:39 +0200735 return new_flavor.id
tierno7edb6752016-03-21 17:37:52 +0100736 except nvExceptions.Conflict as e:
tiernoae4a8d12016-07-08 12:30:39 +0200737 if change_name_if_used and retry < max_retries:
tierno7edb6752016-03-21 17:37:52 +0100738 continue
tiernoae4a8d12016-07-08 12:30:39 +0200739 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100740 #except nvExceptions.BadRequest as e:
741 except (ksExceptions.ClientException, nvExceptions.ClientException, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200742 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100743
tiernoae4a8d12016-07-08 12:30:39 +0200744 def delete_flavor(self,flavor_id):
745 '''Deletes a tenant flavor from openstack VIM. Returns the old flavor_id
tierno7edb6752016-03-21 17:37:52 +0100746 '''
tiernoae4a8d12016-07-08 12:30:39 +0200747 try:
748 self._reload_connection()
749 self.nova.flavors.delete(flavor_id)
750 return flavor_id
751 #except nvExceptions.BadRequest as e:
tierno8e995ce2016-09-22 08:13:00 +0000752 except (nvExceptions.NotFound, ksExceptions.ClientException, nvExceptions.ClientException, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200753 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100754
tiernoae4a8d12016-07-08 12:30:39 +0200755 def new_image(self,image_dict):
tierno7edb6752016-03-21 17:37:52 +0100756 '''
tiernoae4a8d12016-07-08 12:30:39 +0200757 Adds a tenant image to VIM. imge_dict is a dictionary with:
758 name: name
759 disk_format: qcow2, vhd, vmdk, raw (by default), ...
760 location: path or URI
761 public: "yes" or "no"
762 metadata: metadata of the image
763 Returns the image_id
tierno7edb6752016-03-21 17:37:52 +0100764 '''
tiernoae4a8d12016-07-08 12:30:39 +0200765 retry=0
766 max_retries=3
767 while retry<max_retries:
tierno7edb6752016-03-21 17:37:52 +0100768 retry+=1
769 try:
770 self._reload_connection()
771 #determine format http://docs.openstack.org/developer/glance/formats.html
772 if "disk_format" in image_dict:
773 disk_format=image_dict["disk_format"]
garciadeblas14480452017-01-10 13:08:07 +0100774 else: #autodiscover based on extension
tierno1beea862018-07-11 15:47:37 +0200775 if image_dict['location'].endswith(".qcow2"):
tierno7edb6752016-03-21 17:37:52 +0100776 disk_format="qcow2"
tierno1beea862018-07-11 15:47:37 +0200777 elif image_dict['location'].endswith(".vhd"):
tierno7edb6752016-03-21 17:37:52 +0100778 disk_format="vhd"
tierno1beea862018-07-11 15:47:37 +0200779 elif image_dict['location'].endswith(".vmdk"):
tierno7edb6752016-03-21 17:37:52 +0100780 disk_format="vmdk"
tierno1beea862018-07-11 15:47:37 +0200781 elif image_dict['location'].endswith(".vdi"):
tierno7edb6752016-03-21 17:37:52 +0100782 disk_format="vdi"
tierno1beea862018-07-11 15:47:37 +0200783 elif image_dict['location'].endswith(".iso"):
tierno7edb6752016-03-21 17:37:52 +0100784 disk_format="iso"
tierno1beea862018-07-11 15:47:37 +0200785 elif image_dict['location'].endswith(".aki"):
tierno7edb6752016-03-21 17:37:52 +0100786 disk_format="aki"
tierno1beea862018-07-11 15:47:37 +0200787 elif image_dict['location'].endswith(".ari"):
tierno7edb6752016-03-21 17:37:52 +0100788 disk_format="ari"
tierno1beea862018-07-11 15:47:37 +0200789 elif image_dict['location'].endswith(".ami"):
tierno7edb6752016-03-21 17:37:52 +0100790 disk_format="ami"
791 else:
792 disk_format="raw"
tiernoae4a8d12016-07-08 12:30:39 +0200793 self.logger.debug("new_image: '%s' loading from '%s'", image_dict['name'], image_dict['location'])
tierno1beea862018-07-11 15:47:37 +0200794 new_image = self.glance.images.create(name=image_dict['name'])
795 if image_dict['location'].startswith("http"):
796 # TODO there is not a method to direct download. It must be downloaded locally with requests
797 raise vimconn.vimconnNotImplemented("Cannot create image from URL")
tierno7edb6752016-03-21 17:37:52 +0100798 else: #local path
799 with open(image_dict['location']) as fimage:
tierno1beea862018-07-11 15:47:37 +0200800 self.glance.images.upload(new_image.id, fimage)
801 #new_image = self.glancev1.images.create(name=image_dict['name'], is_public=image_dict.get('public',"yes")=="yes",
802 # container_format="bare", data=fimage, disk_format=disk_format)
tierno7edb6752016-03-21 17:37:52 +0100803 metadata_to_load = image_dict.get('metadata')
tierno1beea862018-07-11 15:47:37 +0200804 #TODO location is a reserved word for current openstack versions. Use another word
805 metadata_to_load['location'] = image_dict['location']
806 self.glance.images.update(new_image.id, **metadata_to_load)
tiernoae4a8d12016-07-08 12:30:39 +0200807 return new_image.id
808 except (nvExceptions.Conflict, ksExceptions.ClientException, nvExceptions.ClientException) as e:
809 self._format_exception(e)
tierno8e995ce2016-09-22 08:13:00 +0000810 except (HTTPException, gl1Exceptions.HTTPException, gl1Exceptions.CommunicationError, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200811 if retry==max_retries:
812 continue
813 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100814 except IOError as e: #can not open the file
tiernoae4a8d12016-07-08 12:30:39 +0200815 raise vimconn.vimconnConnectionException(type(e).__name__ + ": " + str(e)+ " for " + image_dict['location'],
816 http_code=vimconn.HTTP_Bad_Request)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +0000817
tiernoae4a8d12016-07-08 12:30:39 +0200818 def delete_image(self, image_id):
819 '''Deletes a tenant image from openstack VIM. Returns the old id
tierno7edb6752016-03-21 17:37:52 +0100820 '''
tiernoae4a8d12016-07-08 12:30:39 +0200821 try:
822 self._reload_connection()
tierno1beea862018-07-11 15:47:37 +0200823 self.glance.images.delete(image_id)
tiernoae4a8d12016-07-08 12:30:39 +0200824 return image_id
tierno8e995ce2016-09-22 08:13:00 +0000825 except (nvExceptions.NotFound, ksExceptions.ClientException, nvExceptions.ClientException, gl1Exceptions.CommunicationError, ConnectionError) as e: #TODO remove
tiernoae4a8d12016-07-08 12:30:39 +0200826 self._format_exception(e)
827
828 def get_image_id_from_path(self, path):
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +0000829 '''Get the image id from image path in the VIM database. Returns the image_id'''
tiernoae4a8d12016-07-08 12:30:39 +0200830 try:
831 self._reload_connection()
tierno1beea862018-07-11 15:47:37 +0200832 images = self.glance.images.list()
tiernoae4a8d12016-07-08 12:30:39 +0200833 for image in images:
834 if image.metadata.get("location")==path:
835 return image.id
836 raise vimconn.vimconnNotFoundException("image with location '{}' not found".format( path))
tierno8e995ce2016-09-22 08:13:00 +0000837 except (ksExceptions.ClientException, nvExceptions.ClientException, gl1Exceptions.CommunicationError, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200838 self._format_exception(e)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +0000839
garciadeblasb69fa9f2016-09-28 12:04:10 +0200840 def get_image_list(self, filter_dict={}):
841 '''Obtain tenant images from VIM
842 Filter_dict can be:
843 id: image id
844 name: image name
845 checksum: image checksum
846 Returns the image list of dictionaries:
847 [{<the fields at Filter_dict plus some VIM specific>}, ...]
848 List can be empty
849 '''
850 self.logger.debug("Getting image list from VIM filter: '%s'", str(filter_dict))
851 try:
852 self._reload_connection()
tierno69b590e2018-03-13 18:52:23 +0100853 filter_dict_os = filter_dict.copy()
garciadeblasb69fa9f2016-09-28 12:04:10 +0200854 #First we filter by the available filter fields: name, id. The others are removed.
tierno1beea862018-07-11 15:47:37 +0200855 image_list = self.glance.images.list()
garciadeblasb69fa9f2016-09-28 12:04:10 +0200856 filtered_list = []
857 for image in image_list:
tierno3cb8dc32017-10-24 18:13:19 +0200858 try:
tierno1beea862018-07-11 15:47:37 +0200859 if filter_dict.get("name") and image["name"] != filter_dict["name"]:
860 continue
861 if filter_dict.get("id") and image["id"] != filter_dict["id"]:
862 continue
863 if filter_dict.get("checksum") and image["checksum"] != filter_dict["checksum"]:
864 continue
865
866 filtered_list.append(image.copy())
tierno3cb8dc32017-10-24 18:13:19 +0200867 except gl1Exceptions.HTTPNotFound:
868 pass
garciadeblasb69fa9f2016-09-28 12:04:10 +0200869 return filtered_list
870 except (ksExceptions.ClientException, nvExceptions.ClientException, gl1Exceptions.CommunicationError, ConnectionError) as e:
871 self._format_exception(e)
872
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +0200873 def __wait_for_vm(self, vm_id, status):
874 """wait until vm is in the desired status and return True.
875 If the VM gets in ERROR status, return false.
876 If the timeout is reached generate an exception"""
877 elapsed_time = 0
878 while elapsed_time < server_timeout:
879 vm_status = self.nova.servers.get(vm_id).status
880 if vm_status == status:
881 return True
882 if vm_status == 'ERROR':
883 return False
tierno1df468d2018-07-06 14:25:16 +0200884 time.sleep(5)
885 elapsed_time += 5
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +0200886
887 # if we exceeded the timeout rollback
888 if elapsed_time >= server_timeout:
889 raise vimconn.vimconnException('Timeout waiting for instance ' + vm_id + ' to get ' + status,
890 http_code=vimconn.HTTP_Request_Timeout)
891
mirabal29356312017-07-27 12:21:22 +0200892 def _get_openstack_availablity_zones(self):
893 """
894 Get from openstack availability zones available
895 :return:
896 """
897 try:
898 openstack_availability_zone = self.nova.availability_zones.list()
899 openstack_availability_zone = [str(zone.zoneName) for zone in openstack_availability_zone
900 if zone.zoneName != 'internal']
901 return openstack_availability_zone
902 except Exception as e:
903 return None
904
905 def _set_availablity_zones(self):
906 """
907 Set vim availablity zone
908 :return:
909 """
910
911 if 'availability_zone' in self.config:
912 vim_availability_zones = self.config.get('availability_zone')
913 if isinstance(vim_availability_zones, str):
914 self.availability_zone = [vim_availability_zones]
915 elif isinstance(vim_availability_zones, list):
916 self.availability_zone = vim_availability_zones
917 else:
918 self.availability_zone = self._get_openstack_availablity_zones()
919
tierno5a3273c2017-08-29 11:43:46 +0200920 def _get_vm_availability_zone(self, availability_zone_index, availability_zone_list):
mirabal29356312017-07-27 12:21:22 +0200921 """
tierno5a3273c2017-08-29 11:43:46 +0200922 Return thge availability zone to be used by the created VM.
923 :return: The VIM availability zone to be used or None
mirabal29356312017-07-27 12:21:22 +0200924 """
tierno5a3273c2017-08-29 11:43:46 +0200925 if availability_zone_index is None:
926 if not self.config.get('availability_zone'):
927 return None
928 elif isinstance(self.config.get('availability_zone'), str):
929 return self.config['availability_zone']
930 else:
931 # TODO consider using a different parameter at config for default AV and AV list match
932 return self.config['availability_zone'][0]
mirabal29356312017-07-27 12:21:22 +0200933
tierno5a3273c2017-08-29 11:43:46 +0200934 vim_availability_zones = self.availability_zone
935 # check if VIM offer enough availability zones describe in the VNFD
936 if vim_availability_zones and len(availability_zone_list) <= len(vim_availability_zones):
937 # check if all the names of NFV AV match VIM AV names
938 match_by_index = False
939 for av in availability_zone_list:
940 if av not in vim_availability_zones:
941 match_by_index = True
942 break
943 if match_by_index:
944 return vim_availability_zones[availability_zone_index]
945 else:
946 return availability_zone_list[availability_zone_index]
mirabal29356312017-07-27 12:21:22 +0200947 else:
tierno5a3273c2017-08-29 11:43:46 +0200948 raise vimconn.vimconnConflictException("No enough availability zones at VIM for this deployment")
mirabal29356312017-07-27 12:21:22 +0200949
tierno5a3273c2017-08-29 11:43:46 +0200950 def new_vminstance(self, name, description, start, image_id, flavor_id, net_list, cloud_config=None, disk_list=None,
951 availability_zone_index=None, availability_zone_list=None):
tierno98e909c2017-10-14 13:27:03 +0200952 """Adds a VM instance to VIM
tierno7edb6752016-03-21 17:37:52 +0100953 Params:
954 start: indicates if VM must start or boot in pause mode. Ignored
955 image_id,flavor_id: iamge and flavor uuid
956 net_list: list of interfaces, each one is a dictionary with:
957 name:
958 net_id: network uuid to connect
959 vpci: virtual vcpi to assign, ignored because openstack lack #TODO
960 model: interface model, ignored #TODO
961 mac_address: used for SR-IOV ifaces #TODO for other types
962 use: 'data', 'bridge', 'mgmt'
tierno66eba6e2017-11-10 17:09:18 +0100963 type: 'virtual', 'PCI-PASSTHROUGH'('PF'), 'SR-IOV'('VF'), 'VFnotShared'
tierno7edb6752016-03-21 17:37:52 +0100964 vim_id: filled/added by this function
ahmadsaf853d452016-12-22 11:33:47 +0500965 floating_ip: True/False (or it can be None)
tierno41a69812018-02-16 14:34:33 +0100966 'cloud_config': (optional) dictionary with:
967 'key-pairs': (optional) list of strings with the public key to be inserted to the default user
968 'users': (optional) list of users to be inserted, each item is a dict with:
969 'name': (mandatory) user name,
970 'key-pairs': (optional) list of strings with the public key to be inserted to the user
971 'user-data': (optional) string is a text script to be passed directly to cloud-init
972 'config-files': (optional). List of files to be transferred. Each item is a dict with:
973 'dest': (mandatory) string with the destination absolute path
974 'encoding': (optional, by default text). Can be one of:
975 'b64', 'base64', 'gz', 'gz+b64', 'gz+base64', 'gzip+b64', 'gzip+base64'
976 'content' (mandatory): string with the content of the file
977 'permissions': (optional) string with file permissions, typically octal notation '0644'
978 'owner': (optional) file owner, string with the format 'owner:group'
979 'boot-data-drive': boolean to indicate if user-data must be passed using a boot drive (hard disk)
mirabal29356312017-07-27 12:21:22 +0200980 'disk_list': (optional) list with additional disks to the VM. Each item is a dict with:
981 'image_id': (optional). VIM id of an existing image. If not provided an empty disk must be mounted
982 'size': (mandatory) string with the size of the disk in GB
tierno1df468d2018-07-06 14:25:16 +0200983 'vim_id' (optional) should use this existing volume id
tierno5a3273c2017-08-29 11:43:46 +0200984 availability_zone_index: Index of availability_zone_list to use for this this VM. None if not AV required
985 availability_zone_list: list of availability zones given by user in the VNFD descriptor. Ignore if
986 availability_zone_index is None
tierno7edb6752016-03-21 17:37:52 +0100987 #TODO ip, security groups
tierno98e909c2017-10-14 13:27:03 +0200988 Returns a tuple with the instance identifier and created_items or raises an exception on error
989 created_items can be None or a dictionary where this method can include key-values that will be passed to
990 the method delete_vminstance and action_vminstance. Can be used to store created ports, volumes, etc.
991 Format is vimconnector dependent, but do not use nested dictionaries and a value of None should be the same
992 as not present.
993 """
tiernofa51c202017-01-27 14:58:17 +0100994 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 +0100995 try:
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +0200996 server = None
tierno98e909c2017-10-14 13:27:03 +0200997 created_items = {}
tiernob0b9dab2017-10-14 14:25:20 +0200998 # metadata = {}
tierno98e909c2017-10-14 13:27:03 +0200999 net_list_vim = []
1000 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 +02001001 no_secured_ports = [] # List of port-is with port-security disabled
tierno7edb6752016-03-21 17:37:52 +01001002 self._reload_connection()
tiernob0b9dab2017-10-14 14:25:20 +02001003 # metadata_vpci = {} # For a specific neutron plugin
tiernob84cbdc2017-07-07 14:30:30 +02001004 block_device_mapping = None
tierno7edb6752016-03-21 17:37:52 +01001005 for net in net_list:
tierno98e909c2017-10-14 13:27:03 +02001006 if not net.get("net_id"): # skip non connected iface
tierno7edb6752016-03-21 17:37:52 +01001007 continue
Pablo Montes Moreno3be0b2a2017-03-30 13:22:15 +02001008
1009 port_dict={
1010 "network_id": net["net_id"],
1011 "name": net.get("name"),
1012 "admin_state_up": True
1013 }
1014 if net["type"]=="virtual":
tiernob0b9dab2017-10-14 14:25:20 +02001015 pass
1016 # if "vpci" in net:
1017 # metadata_vpci[ net["net_id"] ] = [[ net["vpci"], "" ]]
tierno66eba6e2017-11-10 17:09:18 +01001018 elif net["type"] == "VF" or net["type"] == "SR-IOV": # for VF
tiernob0b9dab2017-10-14 14:25:20 +02001019 # if "vpci" in net:
1020 # if "VF" not in metadata_vpci:
1021 # metadata_vpci["VF"]=[]
1022 # metadata_vpci["VF"].append([ net["vpci"], "" ])
Pablo Montes Moreno3be0b2a2017-03-30 13:22:15 +02001023 port_dict["binding:vnic_type"]="direct"
tiernob0b9dab2017-10-14 14:25:20 +02001024 # VIO specific Changes
kate721d79b2017-06-24 04:21:38 -07001025 if self.vim_type == "VIO":
tiernob0b9dab2017-10-14 14:25:20 +02001026 # Need to create port with port_security_enabled = False and no-security-groups
kate721d79b2017-06-24 04:21:38 -07001027 port_dict["port_security_enabled"]=False
1028 port_dict["provider_security_groups"]=[]
1029 port_dict["security_groups"]=[]
tierno66eba6e2017-11-10 17:09:18 +01001030 else: # For PT PCI-PASSTHROUGH
tiernob0b9dab2017-10-14 14:25:20 +02001031 # VIO specific Changes
1032 # Current VIO release does not support port with type 'direct-physical'
1033 # So no need to create virtual port in case of PCI-device.
1034 # Will update port_dict code when support gets added in next VIO release
kate721d79b2017-06-24 04:21:38 -07001035 if self.vim_type == "VIO":
tiernob0b9dab2017-10-14 14:25:20 +02001036 raise vimconn.vimconnNotSupportedException(
1037 "Current VIO release does not support full passthrough (PT)")
1038 # if "vpci" in net:
1039 # if "PF" not in metadata_vpci:
1040 # metadata_vpci["PF"]=[]
1041 # metadata_vpci["PF"].append([ net["vpci"], "" ])
Pablo Montes Moreno3be0b2a2017-03-30 13:22:15 +02001042 port_dict["binding:vnic_type"]="direct-physical"
1043 if not port_dict["name"]:
1044 port_dict["name"]=name
1045 if net.get("mac_address"):
1046 port_dict["mac_address"]=net["mac_address"]
tierno41a69812018-02-16 14:34:33 +01001047 if net.get("ip_address"):
1048 port_dict["fixed_ips"] = [{'ip_address': net["ip_address"]}]
1049 # TODO add 'subnet_id': <subnet_id>
Pablo Montes Moreno3be0b2a2017-03-30 13:22:15 +02001050 new_port = self.neutron.create_port({"port": port_dict })
tierno00e3df72017-11-29 17:20:13 +01001051 created_items["port:" + str(new_port["port"]["id"])] = True
Pablo Montes Moreno3be0b2a2017-03-30 13:22:15 +02001052 net["mac_adress"] = new_port["port"]["mac_address"]
1053 net["vim_id"] = new_port["port"]["id"]
tiernob84cbdc2017-07-07 14:30:30 +02001054 # if try to use a network without subnetwork, it will return a emtpy list
1055 fixed_ips = new_port["port"].get("fixed_ips")
1056 if fixed_ips:
1057 net["ip"] = fixed_ips[0].get("ip_address")
1058 else:
1059 net["ip"] = None
montesmoreno994a29d2017-08-22 11:23:06 +02001060
1061 port = {"port-id": new_port["port"]["id"]}
1062 if float(self.nova.api_version.get_string()) >= 2.32:
1063 port["tag"] = new_port["port"]["name"]
1064 net_list_vim.append(port)
Pablo Montes Moreno3be0b2a2017-03-30 13:22:15 +02001065
ahmadsaf853d452016-12-22 11:33:47 +05001066 if net.get('floating_ip', False):
tiernof8383b82017-01-18 15:49:48 +01001067 net['exit_on_floating_ip_error'] = True
ahmadsaf853d452016-12-22 11:33:47 +05001068 external_network.append(net)
tiernof8383b82017-01-18 15:49:48 +01001069 elif net['use'] == 'mgmt' and self.config.get('use_floating_ip'):
1070 net['exit_on_floating_ip_error'] = False
1071 external_network.append(net)
tierno326fd5e2018-02-22 11:58:59 +01001072 net['floating_ip'] = self.config.get('use_floating_ip')
tiernof8383b82017-01-18 15:49:48 +01001073
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +02001074 # If port security is disabled when the port has not yet been attached to the VM, then all vm traffic is dropped.
1075 # As a workaround we wait until the VM is active and then disable the port-security
tierno4d1ce222018-04-06 10:41:06 +02001076 if net.get("port_security") == False and not self.config.get("no_port_security_extension"):
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +02001077 no_secured_ports.append(new_port["port"]["id"])
1078
tiernob0b9dab2017-10-14 14:25:20 +02001079 # if metadata_vpci:
1080 # metadata = {"pci_assignement": json.dumps(metadata_vpci)}
1081 # if len(metadata["pci_assignement"]) >255:
1082 # #limit the metadata size
1083 # #metadata["pci_assignement"] = metadata["pci_assignement"][0:255]
1084 # self.logger.warn("Metadata deleted since it exceeds the expected length (255) ")
1085 # metadata = {}
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001086
tiernob0b9dab2017-10-14 14:25:20 +02001087 self.logger.debug("name '%s' image_id '%s'flavor_id '%s' net_list_vim '%s' description '%s'",
1088 name, image_id, flavor_id, str(net_list_vim), description)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001089
tiernob0b9dab2017-10-14 14:25:20 +02001090 security_groups = self.config.get('security_groups')
tierno7edb6752016-03-21 17:37:52 +01001091 if type(security_groups) is str:
1092 security_groups = ( security_groups, )
tierno98e909c2017-10-14 13:27:03 +02001093 # cloud config
tierno0a1437e2017-10-02 00:17:43 +02001094 config_drive, userdata = self._create_user_data(cloud_config)
montesmoreno0c8def02016-12-22 12:16:23 +00001095
tierno98e909c2017-10-14 13:27:03 +02001096 # Create additional volumes in case these are present in disk_list
montesmoreno0c8def02016-12-22 12:16:23 +00001097 base_disk_index = ord('b')
tierno1df468d2018-07-06 14:25:16 +02001098 if disk_list:
tiernob84cbdc2017-07-07 14:30:30 +02001099 block_device_mapping = {}
montesmoreno0c8def02016-12-22 12:16:23 +00001100 for disk in disk_list:
tierno1df468d2018-07-06 14:25:16 +02001101 if disk.get('vim_id'):
1102 block_device_mapping['_vd' + chr(base_disk_index)] = disk['vim_id']
montesmoreno0c8def02016-12-22 12:16:23 +00001103 else:
tierno1df468d2018-07-06 14:25:16 +02001104 if 'image_id' in disk:
1105 volume = self.cinder.volumes.create(size=disk['size'], name=name + '_vd' +
1106 chr(base_disk_index), imageRef=disk['image_id'])
1107 else:
1108 volume = self.cinder.volumes.create(size=disk['size'], name=name + '_vd' +
1109 chr(base_disk_index))
1110 created_items["volume:" + str(volume.id)] = True
1111 block_device_mapping['_vd' + chr(base_disk_index)] = volume.id
montesmoreno0c8def02016-12-22 12:16:23 +00001112 base_disk_index += 1
1113
tierno1df468d2018-07-06 14:25:16 +02001114 # Wait until created volumes are with status available
montesmoreno0c8def02016-12-22 12:16:23 +00001115 elapsed_time = 0
tierno1df468d2018-07-06 14:25:16 +02001116 while elapsed_time < volume_timeout:
1117 for created_item in created_items:
1118 v, _, volume_id = created_item.partition(":")
1119 if v == 'volume':
1120 if self.cinder.volumes.get(volume_id).status != 'available':
1121 break
1122 else: # all ready: break from while
1123 break
1124 time.sleep(5)
1125 elapsed_time += 5
tiernob0b9dab2017-10-14 14:25:20 +02001126 # If we exceeded the timeout rollback
montesmoreno0c8def02016-12-22 12:16:23 +00001127 if elapsed_time >= volume_timeout:
montesmoreno0c8def02016-12-22 12:16:23 +00001128 raise vimconn.vimconnException('Timeout creating volumes for instance ' + name,
1129 http_code=vimconn.HTTP_Request_Timeout)
mirabal29356312017-07-27 12:21:22 +02001130 # get availability Zone
tierno5a3273c2017-08-29 11:43:46 +02001131 vm_av_zone = self._get_vm_availability_zone(availability_zone_index, availability_zone_list)
montesmoreno0c8def02016-12-22 12:16:23 +00001132
tiernob0b9dab2017-10-14 14:25:20 +02001133 self.logger.debug("nova.servers.create({}, {}, {}, nics={}, security_groups={}, "
mirabal29356312017-07-27 12:21:22 +02001134 "availability_zone={}, key_name={}, userdata={}, config_drive={}, "
tiernob0b9dab2017-10-14 14:25:20 +02001135 "block_device_mapping={})".format(name, image_id, flavor_id, net_list_vim,
mirabal29356312017-07-27 12:21:22 +02001136 security_groups, vm_av_zone, self.config.get('keypair'),
tiernob0b9dab2017-10-14 14:25:20 +02001137 userdata, config_drive, block_device_mapping))
1138 server = self.nova.servers.create(name, image_id, flavor_id, nics=net_list_vim,
montesmoreno0c8def02016-12-22 12:16:23 +00001139 security_groups=security_groups,
mirabal29356312017-07-27 12:21:22 +02001140 availability_zone=vm_av_zone,
montesmoreno0c8def02016-12-22 12:16:23 +00001141 key_name=self.config.get('keypair'),
1142 userdata=userdata,
tiernob84cbdc2017-07-07 14:30:30 +02001143 config_drive=config_drive,
1144 block_device_mapping=block_device_mapping
montesmoreno0c8def02016-12-22 12:16:23 +00001145 ) # , description=description)
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +02001146
tierno326fd5e2018-02-22 11:58:59 +01001147 vm_start_time = time.time()
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +02001148 # Previously mentioned workaround to wait until the VM is active and then disable the port-security
1149 if no_secured_ports:
1150 self.__wait_for_vm(server.id, 'ACTIVE')
1151
1152 for port_id in no_secured_ports:
1153 try:
tierno4d1ce222018-04-06 10:41:06 +02001154 self.neutron.update_port(port_id,
1155 {"port": {"port_security_enabled": False, "security_groups": None}})
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +02001156 except Exception as e:
tierno4d1ce222018-04-06 10:41:06 +02001157 raise vimconn.vimconnException("It was not possible to disable port security for port {}".format(
1158 port_id))
tierno98e909c2017-10-14 13:27:03 +02001159 # print "DONE :-)", server
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +02001160
tierno4d1ce222018-04-06 10:41:06 +02001161 # pool_id = None
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +02001162 if external_network:
tierno98e909c2017-10-14 13:27:03 +02001163 floating_ips = self.neutron.list_floatingips().get("floatingips", ())
ahmadsaf853d452016-12-22 11:33:47 +05001164 for floating_network in external_network:
tiernof8383b82017-01-18 15:49:48 +01001165 try:
tiernof8383b82017-01-18 15:49:48 +01001166 assigned = False
tierno98e909c2017-10-14 13:27:03 +02001167 while not assigned:
tiernof8383b82017-01-18 15:49:48 +01001168 if floating_ips:
1169 ip = floating_ips.pop(0)
tierno326fd5e2018-02-22 11:58:59 +01001170 if ip.get("port_id", False) or ip.get('tenant_id') != server.tenant_id:
1171 continue
1172 if isinstance(floating_network['floating_ip'], str):
1173 if ip.get("floating_network_id") != floating_network['floating_ip']:
1174 continue
1175 free_floating_ip = ip.get("floating_ip_address")
tiernof8383b82017-01-18 15:49:48 +01001176 else:
tiernocb3cca22018-05-31 15:08:52 +02001177 if isinstance(floating_network['floating_ip'], str) and \
1178 floating_network['floating_ip'].lower() != "true":
tierno326fd5e2018-02-22 11:58:59 +01001179 pool_id = floating_network['floating_ip']
1180 else:
tierno4d1ce222018-04-06 10:41:06 +02001181 # Find the external network
tierno326fd5e2018-02-22 11:58:59 +01001182 external_nets = list()
1183 for net in self.neutron.list_networks()['networks']:
1184 if net['router:external']:
1185 external_nets.append(net)
tiernof8383b82017-01-18 15:49:48 +01001186
tierno326fd5e2018-02-22 11:58:59 +01001187 if len(external_nets) == 0:
1188 raise vimconn.vimconnException("Cannot create floating_ip automatically since no external "
1189 "network is present",
1190 http_code=vimconn.HTTP_Conflict)
1191 if len(external_nets) > 1:
1192 raise vimconn.vimconnException("Cannot create floating_ip automatically since multiple "
1193 "external networks are present",
1194 http_code=vimconn.HTTP_Conflict)
tiernof8383b82017-01-18 15:49:48 +01001195
tierno326fd5e2018-02-22 11:58:59 +01001196 pool_id = external_nets[0].get('id')
tiernof8383b82017-01-18 15:49:48 +01001197 param = {'floatingip': {'floating_network_id': pool_id, 'tenant_id': server.tenant_id}}
ahmadsaf853d452016-12-22 11:33:47 +05001198 try:
tierno4d1ce222018-04-06 10:41:06 +02001199 # self.logger.debug("Creating floating IP")
tiernof8383b82017-01-18 15:49:48 +01001200 new_floating_ip = self.neutron.create_floatingip(param)
1201 free_floating_ip = new_floating_ip['floatingip']['floating_ip_address']
ahmadsaf853d452016-12-22 11:33:47 +05001202 except Exception as e:
tierno326fd5e2018-02-22 11:58:59 +01001203 raise vimconn.vimconnException(type(e).__name__ + ": Cannot create new floating_ip " +
1204 str(e), http_code=vimconn.HTTP_Conflict)
1205
1206 fix_ip = floating_network.get('ip')
1207 while not assigned:
1208 try:
1209 server.add_floating_ip(free_floating_ip, fix_ip)
1210 assigned = True
1211 except Exception as e:
tierno4d1ce222018-04-06 10:41:06 +02001212 # openstack need some time after VM creation to asign an IP. So retry if fails
tierno326fd5e2018-02-22 11:58:59 +01001213 vm_status = self.nova.servers.get(server.id).status
1214 if vm_status != 'ACTIVE' and vm_status != 'ERROR':
1215 if time.time() - vm_start_time < server_timeout:
1216 time.sleep(5)
1217 continue
tierno4d1ce222018-04-06 10:41:06 +02001218 raise vimconn.vimconnException(
1219 "Cannot create floating_ip: {} {}".format(type(e).__name__, e),
1220 http_code=vimconn.HTTP_Conflict)
tierno326fd5e2018-02-22 11:58:59 +01001221
tiernof8383b82017-01-18 15:49:48 +01001222 except Exception as e:
1223 if not floating_network['exit_on_floating_ip_error']:
1224 self.logger.warn("Cannot create floating_ip. %s", str(e))
1225 continue
tiernof8383b82017-01-18 15:49:48 +01001226 raise
montesmoreno2a1fc4e2017-01-09 16:46:04 +00001227
tierno98e909c2017-10-14 13:27:03 +02001228 return server.id, created_items
tierno7edb6752016-03-21 17:37:52 +01001229# except nvExceptions.NotFound as e:
1230# error_value=-vimconn.HTTP_Not_Found
1231# error_text= "vm instance %s not found" % vm_id
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +02001232# except TypeError as e:
1233# raise vimconn.vimconnException(type(e).__name__ + ": "+ str(e), http_code=vimconn.HTTP_Bad_Request)
1234
1235 except Exception as e:
tierno98e909c2017-10-14 13:27:03 +02001236 server_id = None
1237 if server:
1238 server_id = server.id
1239 try:
1240 self.delete_vminstance(server_id, created_items)
1241 except Exception as e2:
1242 self.logger.error("new_vminstance rollback fail {}".format(e2))
Pablo Montes Moreno6a7785b2017-07-03 10:44:30 +02001243
tiernoae4a8d12016-07-08 12:30:39 +02001244 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +01001245
tiernoae4a8d12016-07-08 12:30:39 +02001246 def get_vminstance(self,vm_id):
tierno7edb6752016-03-21 17:37:52 +01001247 '''Returns the VM instance information from VIM'''
tiernoae4a8d12016-07-08 12:30:39 +02001248 #self.logger.debug("Getting VM from VIM")
tierno7edb6752016-03-21 17:37:52 +01001249 try:
1250 self._reload_connection()
1251 server = self.nova.servers.find(id=vm_id)
1252 #TODO parse input and translate to VIM format (openmano_schemas.new_vminstance_response_schema)
tiernoae4a8d12016-07-08 12:30:39 +02001253 return server.to_dict()
tierno8e995ce2016-09-22 08:13:00 +00001254 except (ksExceptions.ClientException, nvExceptions.ClientException, nvExceptions.NotFound, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +02001255 self._format_exception(e)
1256
1257 def get_vminstance_console(self,vm_id, console_type="vnc"):
tierno7edb6752016-03-21 17:37:52 +01001258 '''
1259 Get a console for the virtual machine
1260 Params:
1261 vm_id: uuid of the VM
1262 console_type, can be:
1263 "novnc" (by default), "xvpvnc" for VNC types,
1264 "rdp-html5" for RDP types, "spice-html5" for SPICE types
tiernoae4a8d12016-07-08 12:30:39 +02001265 Returns dict with the console parameters:
1266 protocol: ssh, ftp, http, https, ...
1267 server: usually ip address
1268 port: the http, ssh, ... port
1269 suffix: extra text, e.g. the http path and query string
tierno7edb6752016-03-21 17:37:52 +01001270 '''
tiernoae4a8d12016-07-08 12:30:39 +02001271 self.logger.debug("Getting VM CONSOLE from VIM")
tierno7edb6752016-03-21 17:37:52 +01001272 try:
1273 self._reload_connection()
1274 server = self.nova.servers.find(id=vm_id)
1275 if console_type == None or console_type == "novnc":
1276 console_dict = server.get_vnc_console("novnc")
1277 elif console_type == "xvpvnc":
1278 console_dict = server.get_vnc_console(console_type)
1279 elif console_type == "rdp-html5":
1280 console_dict = server.get_rdp_console(console_type)
1281 elif console_type == "spice-html5":
1282 console_dict = server.get_spice_console(console_type)
1283 else:
tiernoae4a8d12016-07-08 12:30:39 +02001284 raise vimconn.vimconnException("console type '{}' not allowed".format(console_type), http_code=vimconn.HTTP_Bad_Request)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001285
tierno7edb6752016-03-21 17:37:52 +01001286 console_dict1 = console_dict.get("console")
1287 if console_dict1:
1288 console_url = console_dict1.get("url")
1289 if console_url:
1290 #parse console_url
1291 protocol_index = console_url.find("//")
1292 suffix_index = console_url[protocol_index+2:].find("/") + protocol_index+2
1293 port_index = console_url[protocol_index+2:suffix_index].find(":") + protocol_index+2
1294 if protocol_index < 0 or port_index<0 or suffix_index<0:
1295 return -vimconn.HTTP_Internal_Server_Error, "Unexpected response from VIM"
1296 console_dict={"protocol": console_url[0:protocol_index],
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001297 "server": console_url[protocol_index+2:port_index],
1298 "port": console_url[port_index:suffix_index],
1299 "suffix": console_url[suffix_index+1:]
tierno7edb6752016-03-21 17:37:52 +01001300 }
1301 protocol_index += 2
tiernoae4a8d12016-07-08 12:30:39 +02001302 return console_dict
1303 raise vimconn.vimconnUnexpectedResponse("Unexpected response from VIM")
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001304
tierno8e995ce2016-09-22 08:13:00 +00001305 except (nvExceptions.NotFound, ksExceptions.ClientException, nvExceptions.ClientException, nvExceptions.BadRequest, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +02001306 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +01001307
tierno98e909c2017-10-14 13:27:03 +02001308 def delete_vminstance(self, vm_id, created_items=None):
tiernoae4a8d12016-07-08 12:30:39 +02001309 '''Removes a VM instance from VIM. Returns the old identifier
tierno7edb6752016-03-21 17:37:52 +01001310 '''
tiernoae4a8d12016-07-08 12:30:39 +02001311 #print "osconnector: Getting VM from VIM"
tierno98e909c2017-10-14 13:27:03 +02001312 if created_items == None:
1313 created_items = {}
tierno7edb6752016-03-21 17:37:52 +01001314 try:
1315 self._reload_connection()
tierno98e909c2017-10-14 13:27:03 +02001316 # delete VM ports attached to this networks before the virtual machine
1317 for k, v in created_items.items():
1318 if not v: # skip already deleted
1319 continue
tierno7edb6752016-03-21 17:37:52 +01001320 try:
tiernoad6bdd42018-01-10 10:43:46 +01001321 k_item, _, k_id = k.partition(":")
1322 if k_item == "port":
1323 self.neutron.delete_port(k_id)
tierno7edb6752016-03-21 17:37:52 +01001324 except Exception as e:
tierno00e3df72017-11-29 17:20:13 +01001325 self.logger.error("Error deleting port: {}: {}".format(type(e).__name__, e))
montesmoreno0c8def02016-12-22 12:16:23 +00001326
tierno98e909c2017-10-14 13:27:03 +02001327 # #commented because detaching the volumes makes the servers.delete not work properly ?!?
1328 # #dettach volumes attached
1329 # server = self.nova.servers.get(vm_id)
1330 # volumes_attached_dict = server._info['os-extended-volumes:volumes_attached'] #volume['id']
1331 # #for volume in volumes_attached_dict:
1332 # # self.cinder.volumes.detach(volume['id'])
montesmoreno0c8def02016-12-22 12:16:23 +00001333
tierno98e909c2017-10-14 13:27:03 +02001334 if vm_id:
1335 self.nova.servers.delete(vm_id)
montesmoreno0c8def02016-12-22 12:16:23 +00001336
tierno98e909c2017-10-14 13:27:03 +02001337 # delete volumes. Although having detached, they should have in active status before deleting
1338 # we ensure in this loop
montesmoreno0c8def02016-12-22 12:16:23 +00001339 keep_waiting = True
1340 elapsed_time = 0
1341 while keep_waiting and elapsed_time < volume_timeout:
1342 keep_waiting = False
tierno98e909c2017-10-14 13:27:03 +02001343 for k, v in created_items.items():
1344 if not v: # skip already deleted
1345 continue
1346 try:
tiernoad6bdd42018-01-10 10:43:46 +01001347 k_item, _, k_id = k.partition(":")
1348 if k_item == "volume":
1349 if self.cinder.volumes.get(k_id).status != 'available':
tierno98e909c2017-10-14 13:27:03 +02001350 keep_waiting = True
1351 else:
tiernoad6bdd42018-01-10 10:43:46 +01001352 self.cinder.volumes.delete(k_id)
tierno98e909c2017-10-14 13:27:03 +02001353 except Exception as e:
tierno00e3df72017-11-29 17:20:13 +01001354 self.logger.error("Error deleting volume: {}: {}".format(type(e).__name__, e))
montesmoreno0c8def02016-12-22 12:16:23 +00001355 if keep_waiting:
1356 time.sleep(1)
1357 elapsed_time += 1
tierno98e909c2017-10-14 13:27:03 +02001358 return None
tierno8e995ce2016-09-22 08:13:00 +00001359 except (nvExceptions.NotFound, ksExceptions.ClientException, nvExceptions.ClientException, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +02001360 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +01001361
tiernoae4a8d12016-07-08 12:30:39 +02001362 def refresh_vms_status(self, vm_list):
1363 '''Get the status of the virtual machines and their interfaces/ports
1364 Params: the list of VM identifiers
1365 Returns a dictionary with:
1366 vm_id: #VIM id of this Virtual Machine
1367 status: #Mandatory. Text with one of:
1368 # DELETED (not found at vim)
1369 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
1370 # OTHER (Vim reported other status not understood)
1371 # ERROR (VIM indicates an ERROR status)
1372 # ACTIVE, PAUSED, SUSPENDED, INACTIVE (not running),
1373 # CREATING (on building process), ERROR
1374 # ACTIVE:NoMgmtIP (Active but any of its interface has an IP address
1375 #
1376 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
1377 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
1378 interfaces:
1379 - vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
1380 mac_address: #Text format XX:XX:XX:XX:XX:XX
1381 vim_net_id: #network id where this interface is connected
1382 vim_interface_id: #interface/port VIM id
1383 ip_address: #null, or text with IPv4, IPv6 address
tierno867ffe92017-03-27 12:50:34 +02001384 compute_node: #identification of compute node where PF,VF interface is allocated
1385 pci: #PCI address of the NIC that hosts the PF,VF
1386 vlan: #physical VLAN used for VF
tierno7edb6752016-03-21 17:37:52 +01001387 '''
tiernoae4a8d12016-07-08 12:30:39 +02001388 vm_dict={}
1389 self.logger.debug("refresh_vms status: Getting tenant VM instance information from VIM")
1390 for vm_id in vm_list:
1391 vm={}
1392 try:
1393 vm_vim = self.get_vminstance(vm_id)
1394 if vm_vim['status'] in vmStatus2manoFormat:
1395 vm['status'] = vmStatus2manoFormat[ vm_vim['status'] ]
tierno7edb6752016-03-21 17:37:52 +01001396 else:
tiernoae4a8d12016-07-08 12:30:39 +02001397 vm['status'] = "OTHER"
1398 vm['error_msg'] = "VIM status reported " + vm_vim['status']
tierno8e995ce2016-09-22 08:13:00 +00001399 try:
1400 vm['vim_info'] = yaml.safe_dump(vm_vim, default_flow_style=True, width=256)
1401 except yaml.representer.RepresenterError:
1402 vm['vim_info'] = str(vm_vim)
tiernoae4a8d12016-07-08 12:30:39 +02001403 vm["interfaces"] = []
1404 if vm_vim.get('fault'):
1405 vm['error_msg'] = str(vm_vim['fault'])
1406 #get interfaces
tierno7edb6752016-03-21 17:37:52 +01001407 try:
tiernoae4a8d12016-07-08 12:30:39 +02001408 self._reload_connection()
tiernob42fd9b2018-06-20 10:44:32 +02001409 port_dict = self.neutron.list_ports(device_id=vm_id)
tiernoae4a8d12016-07-08 12:30:39 +02001410 for port in port_dict["ports"]:
1411 interface={}
tierno8e995ce2016-09-22 08:13:00 +00001412 try:
1413 interface['vim_info'] = yaml.safe_dump(port, default_flow_style=True, width=256)
1414 except yaml.representer.RepresenterError:
1415 interface['vim_info'] = str(port)
tiernoae4a8d12016-07-08 12:30:39 +02001416 interface["mac_address"] = port.get("mac_address")
1417 interface["vim_net_id"] = port["network_id"]
1418 interface["vim_interface_id"] = port["id"]
Mike Marchetti5b9da422017-05-02 15:35:47 -04001419 # check if OS-EXT-SRV-ATTR:host is there,
1420 # in case of non-admin credentials, it will be missing
1421 if vm_vim.get('OS-EXT-SRV-ATTR:host'):
1422 interface["compute_node"] = vm_vim['OS-EXT-SRV-ATTR:host']
tierno867ffe92017-03-27 12:50:34 +02001423 interface["pci"] = None
Mike Marchetti5b9da422017-05-02 15:35:47 -04001424
1425 # check if binding:profile is there,
1426 # in case of non-admin credentials, it will be missing
1427 if port.get('binding:profile'):
1428 if port['binding:profile'].get('pci_slot'):
1429 # TODO: At the moment sr-iov pci addresses are converted to PF pci addresses by setting the slot to 0x00
1430 # TODO: This is just a workaround valid for niantinc. Find a better way to do so
1431 # CHANGE DDDD:BB:SS.F to DDDD:BB:00.(F%2) assuming there are 2 ports per nic
1432 pci = port['binding:profile']['pci_slot']
1433 # interface["pci"] = pci[:-4] + "00." + str(int(pci[-1]) % 2)
1434 interface["pci"] = pci
tierno867ffe92017-03-27 12:50:34 +02001435 interface["vlan"] = None
Pablo Montes Moreno3be0b2a2017-03-30 13:22:15 +02001436 #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 +01001437 network = self.neutron.show_network(port["network_id"])
Pablo Montes Moreno3be0b2a2017-03-30 13:22:15 +02001438 if network['network'].get('provider:network_type') == 'vlan' and \
1439 port.get("binding:vnic_type") == "direct":
tierno867ffe92017-03-27 12:50:34 +02001440 interface["vlan"] = network['network'].get('provider:segmentation_id')
tiernoae4a8d12016-07-08 12:30:39 +02001441 ips=[]
1442 #look for floating ip address
tiernob42fd9b2018-06-20 10:44:32 +02001443 try:
1444 floating_ip_dict = self.neutron.list_floatingips(port_id=port["id"])
1445 if floating_ip_dict.get("floatingips"):
1446 ips.append(floating_ip_dict["floatingips"][0].get("floating_ip_address") )
1447 except Exception:
1448 pass
tierno7edb6752016-03-21 17:37:52 +01001449
tiernoae4a8d12016-07-08 12:30:39 +02001450 for subnet in port["fixed_ips"]:
1451 ips.append(subnet["ip_address"])
1452 interface["ip_address"] = ";".join(ips)
1453 vm["interfaces"].append(interface)
1454 except Exception as e:
tiernob42fd9b2018-06-20 10:44:32 +02001455 self.logger.error("Error getting vm interface information {}: {}".format(type(e).__name__, e),
1456 exc_info=True)
tiernoae4a8d12016-07-08 12:30:39 +02001457 except vimconn.vimconnNotFoundException as e:
1458 self.logger.error("Exception getting vm status: %s", str(e))
1459 vm['status'] = "DELETED"
1460 vm['error_msg'] = str(e)
1461 except vimconn.vimconnException as e:
1462 self.logger.error("Exception getting vm status: %s", str(e))
1463 vm['status'] = "VIM_ERROR"
1464 vm['error_msg'] = str(e)
1465 vm_dict[vm_id] = vm
1466 return vm_dict
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001467
tierno98e909c2017-10-14 13:27:03 +02001468 def action_vminstance(self, vm_id, action_dict, created_items={}):
tierno7edb6752016-03-21 17:37:52 +01001469 '''Send and action over a VM instance from VIM
tierno98e909c2017-10-14 13:27:03 +02001470 Returns None or the console dict if the action was successfully sent to the VIM'''
tiernoae4a8d12016-07-08 12:30:39 +02001471 self.logger.debug("Action over VM '%s': %s", vm_id, str(action_dict))
tierno7edb6752016-03-21 17:37:52 +01001472 try:
1473 self._reload_connection()
1474 server = self.nova.servers.find(id=vm_id)
1475 if "start" in action_dict:
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001476 if action_dict["start"]=="rebuild":
tierno7edb6752016-03-21 17:37:52 +01001477 server.rebuild()
1478 else:
1479 if server.status=="PAUSED":
1480 server.unpause()
1481 elif server.status=="SUSPENDED":
1482 server.resume()
1483 elif server.status=="SHUTOFF":
1484 server.start()
1485 elif "pause" in action_dict:
1486 server.pause()
1487 elif "resume" in action_dict:
1488 server.resume()
1489 elif "shutoff" in action_dict or "shutdown" in action_dict:
1490 server.stop()
1491 elif "forceOff" in action_dict:
1492 server.stop() #TODO
1493 elif "terminate" in action_dict:
1494 server.delete()
1495 elif "createImage" in action_dict:
1496 server.create_image()
1497 #"path":path_schema,
1498 #"description":description_schema,
1499 #"name":name_schema,
1500 #"metadata":metadata_schema,
1501 #"imageRef": id_schema,
1502 #"disk": {"oneOf":[{"type": "null"}, {"type":"string"}] },
1503 elif "rebuild" in action_dict:
1504 server.rebuild(server.image['id'])
1505 elif "reboot" in action_dict:
1506 server.reboot() #reboot_type='SOFT'
1507 elif "console" in action_dict:
1508 console_type = action_dict["console"]
1509 if console_type == None or console_type == "novnc":
1510 console_dict = server.get_vnc_console("novnc")
1511 elif console_type == "xvpvnc":
1512 console_dict = server.get_vnc_console(console_type)
1513 elif console_type == "rdp-html5":
1514 console_dict = server.get_rdp_console(console_type)
1515 elif console_type == "spice-html5":
1516 console_dict = server.get_spice_console(console_type)
1517 else:
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001518 raise vimconn.vimconnException("console type '{}' not allowed".format(console_type),
tiernoae4a8d12016-07-08 12:30:39 +02001519 http_code=vimconn.HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001520 try:
1521 console_url = console_dict["console"]["url"]
1522 #parse console_url
1523 protocol_index = console_url.find("//")
1524 suffix_index = console_url[protocol_index+2:].find("/") + protocol_index+2
1525 port_index = console_url[protocol_index+2:suffix_index].find(":") + protocol_index+2
1526 if protocol_index < 0 or port_index<0 or suffix_index<0:
tiernoae4a8d12016-07-08 12:30:39 +02001527 raise vimconn.vimconnException("Unexpected response from VIM " + str(console_dict))
tierno7edb6752016-03-21 17:37:52 +01001528 console_dict2={"protocol": console_url[0:protocol_index],
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001529 "server": console_url[protocol_index+2 : port_index],
1530 "port": int(console_url[port_index+1 : suffix_index]),
1531 "suffix": console_url[suffix_index+1:]
tierno7edb6752016-03-21 17:37:52 +01001532 }
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001533 return console_dict2
tiernoae4a8d12016-07-08 12:30:39 +02001534 except Exception as e:
1535 raise vimconn.vimconnException("Unexpected response from VIM " + str(console_dict))
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001536
tierno98e909c2017-10-14 13:27:03 +02001537 return None
tierno8e995ce2016-09-22 08:13:00 +00001538 except (ksExceptions.ClientException, nvExceptions.ClientException, nvExceptions.NotFound, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +02001539 self._format_exception(e)
1540 #TODO insert exception vimconn.HTTP_Unauthorized
1541
kate721d79b2017-06-24 04:21:38 -07001542 ####### VIO Specific Changes #########
1543 def _genrate_vlanID(self):
1544 """
1545 Method to get unused vlanID
1546 Args:
1547 None
1548 Returns:
1549 vlanID
1550 """
1551 #Get used VLAN IDs
1552 usedVlanIDs = []
1553 networks = self.get_network_list()
1554 for net in networks:
1555 if net.get('provider:segmentation_id'):
1556 usedVlanIDs.append(net.get('provider:segmentation_id'))
1557 used_vlanIDs = set(usedVlanIDs)
1558
1559 #find unused VLAN ID
1560 for vlanID_range in self.config.get('dataplane_net_vlan_range'):
1561 try:
1562 start_vlanid , end_vlanid = map(int, vlanID_range.replace(" ", "").split("-"))
1563 for vlanID in xrange(start_vlanid, end_vlanid + 1):
1564 if vlanID not in used_vlanIDs:
1565 return vlanID
1566 except Exception as exp:
1567 raise vimconn.vimconnException("Exception {} occurred while generating VLAN ID.".format(exp))
1568 else:
1569 raise vimconn.vimconnConflictException("Unable to create the SRIOV VLAN network."\
1570 " All given Vlan IDs {} are in use.".format(self.config.get('dataplane_net_vlan_range')))
1571
1572
1573 def _validate_vlan_ranges(self, dataplane_net_vlan_range):
1574 """
1575 Method to validate user given vlanID ranges
1576 Args: None
1577 Returns: None
1578 """
1579 for vlanID_range in dataplane_net_vlan_range:
1580 vlan_range = vlanID_range.replace(" ", "")
1581 #validate format
1582 vlanID_pattern = r'(\d)*-(\d)*$'
1583 match_obj = re.match(vlanID_pattern, vlan_range)
1584 if not match_obj:
1585 raise vimconn.vimconnConflictException("Invalid dataplane_net_vlan_range {}.You must provide "\
1586 "'dataplane_net_vlan_range' in format [start_ID - end_ID].".format(vlanID_range))
1587
1588 start_vlanid , end_vlanid = map(int,vlan_range.split("-"))
1589 if start_vlanid <= 0 :
1590 raise vimconn.vimconnConflictException("Invalid dataplane_net_vlan_range {}."\
1591 "Start ID can not be zero. For VLAN "\
1592 "networks valid IDs are 1 to 4094 ".format(vlanID_range))
1593 if end_vlanid > 4094 :
1594 raise vimconn.vimconnConflictException("Invalid dataplane_net_vlan_range {}."\
1595 "End VLAN ID can not be greater than 4094. For VLAN "\
1596 "networks valid IDs are 1 to 4094 ".format(vlanID_range))
1597
1598 if start_vlanid > end_vlanid:
1599 raise vimconn.vimconnConflictException("Invalid dataplane_net_vlan_range {}."\
1600 "You must provide a 'dataplane_net_vlan_range' in format start_ID - end_ID and "\
1601 "start_ID < end_ID ".format(vlanID_range))
1602
tiernoae4a8d12016-07-08 12:30:39 +02001603#NOT USED FUNCTIONS
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001604
tiernoae4a8d12016-07-08 12:30:39 +02001605 def new_external_port(self, port_data):
1606 #TODO openstack if needed
1607 '''Adds a external port to VIM'''
1608 '''Returns the port identifier'''
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001609 return -vimconn.HTTP_Internal_Server_Error, "osconnector.new_external_port() not implemented"
1610
tiernoae4a8d12016-07-08 12:30:39 +02001611 def connect_port_network(self, port_id, network_id, admin=False):
1612 #TODO openstack if needed
1613 '''Connects a external port to a network'''
1614 '''Returns status code of the VIM response'''
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001615 return -vimconn.HTTP_Internal_Server_Error, "osconnector.connect_port_network() not implemented"
1616
tiernoae4a8d12016-07-08 12:30:39 +02001617 def new_user(self, user_name, user_passwd, tenant_id=None):
1618 '''Adds a new user to openstack VIM'''
1619 '''Returns the user identifier'''
1620 self.logger.debug("osconnector: Adding a new user to VIM")
1621 try:
1622 self._reload_connection()
1623 user=self.keystone.users.create(user_name, user_passwd, tenant_id=tenant_id)
1624 #self.keystone.tenants.add_user(self.k_creds["username"], #role)
1625 return user.id
1626 except ksExceptions.ConnectionError as e:
1627 error_value=-vimconn.HTTP_Bad_Request
1628 error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0]))
1629 except ksExceptions.ClientException as e: #TODO remove
tierno7edb6752016-03-21 17:37:52 +01001630 error_value=-vimconn.HTTP_Bad_Request
1631 error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0]))
1632 #TODO insert exception vimconn.HTTP_Unauthorized
1633 #if reaching here is because an exception
tierno9c5c8322018-03-23 15:44:03 +01001634 self.logger.debug("new_user " + error_text)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001635 return error_value, error_text
tiernoae4a8d12016-07-08 12:30:39 +02001636
1637 def delete_user(self, user_id):
1638 '''Delete a user from openstack VIM'''
1639 '''Returns the user identifier'''
1640 if self.debug:
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001641 print("osconnector: Deleting a user from VIM")
tiernoae4a8d12016-07-08 12:30:39 +02001642 try:
1643 self._reload_connection()
1644 self.keystone.users.delete(user_id)
1645 return 1, user_id
1646 except ksExceptions.ConnectionError as e:
1647 error_value=-vimconn.HTTP_Bad_Request
1648 error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0]))
1649 except ksExceptions.NotFound as e:
1650 error_value=-vimconn.HTTP_Not_Found
1651 error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0]))
1652 except ksExceptions.ClientException as e: #TODO remove
1653 error_value=-vimconn.HTTP_Bad_Request
1654 error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0]))
1655 #TODO insert exception vimconn.HTTP_Unauthorized
1656 #if reaching here is because an exception
tierno9c5c8322018-03-23 15:44:03 +01001657 self.logger.debug("delete_tenant " + error_text)
tiernoae4a8d12016-07-08 12:30:39 +02001658 return error_value, error_text
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001659
tierno7edb6752016-03-21 17:37:52 +01001660 def get_hosts_info(self):
1661 '''Get the information of deployed hosts
1662 Returns the hosts content'''
1663 if self.debug:
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001664 print("osconnector: Getting Host info from VIM")
tierno7edb6752016-03-21 17:37:52 +01001665 try:
1666 h_list=[]
1667 self._reload_connection()
1668 hypervisors = self.nova.hypervisors.list()
1669 for hype in hypervisors:
1670 h_list.append( hype.to_dict() )
1671 return 1, {"hosts":h_list}
1672 except nvExceptions.NotFound as e:
1673 error_value=-vimconn.HTTP_Not_Found
1674 error_text= (str(e) if len(e.args)==0 else str(e.args[0]))
1675 except (ksExceptions.ClientException, nvExceptions.ClientException) as e:
1676 error_value=-vimconn.HTTP_Bad_Request
1677 error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0]))
1678 #TODO insert exception vimconn.HTTP_Unauthorized
1679 #if reaching here is because an exception
tierno9c5c8322018-03-23 15:44:03 +01001680 self.logger.debug("get_hosts_info " + error_text)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001681 return error_value, error_text
tierno7edb6752016-03-21 17:37:52 +01001682
1683 def get_hosts(self, vim_tenant):
1684 '''Get the hosts and deployed instances
1685 Returns the hosts content'''
1686 r, hype_dict = self.get_hosts_info()
1687 if r<0:
1688 return r, hype_dict
1689 hypervisors = hype_dict["hosts"]
1690 try:
1691 servers = self.nova.servers.list()
1692 for hype in hypervisors:
1693 for server in servers:
1694 if server.to_dict()['OS-EXT-SRV-ATTR:hypervisor_hostname']==hype['hypervisor_hostname']:
1695 if 'vm' in hype:
1696 hype['vm'].append(server.id)
1697 else:
1698 hype['vm'] = [server.id]
1699 return 1, hype_dict
1700 except nvExceptions.NotFound as e:
1701 error_value=-vimconn.HTTP_Not_Found
1702 error_text= (str(e) if len(e.args)==0 else str(e.args[0]))
1703 except (ksExceptions.ClientException, nvExceptions.ClientException) as e:
1704 error_value=-vimconn.HTTP_Bad_Request
1705 error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0]))
1706 #TODO insert exception vimconn.HTTP_Unauthorized
1707 #if reaching here is because an exception
tierno9c5c8322018-03-23 15:44:03 +01001708 self.logger.debug("get_hosts " + error_text)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001709 return error_value, error_text
tierno7edb6752016-03-21 17:37:52 +01001710
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001711 def new_classification(self, name, ctype, definition):
1712 self.logger.debug(
1713 'Adding a new (Traffic) Classification to VIM, named %s', name)
1714 try:
1715 new_class = None
1716 self._reload_connection()
1717 if ctype not in supportedClassificationTypes:
1718 raise vimconn.vimconnNotSupportedException(
1719 'OpenStack VIM connector doesn\'t support provided '
1720 'Classification Type {}, supported ones are: '
1721 '{}'.format(ctype, supportedClassificationTypes))
1722 if not self._validate_classification(ctype, definition):
1723 raise vimconn.vimconnException(
1724 'Incorrect Classification definition '
1725 'for the type specified.')
1726 classification_dict = definition
1727 classification_dict['name'] = name
tierno7edb6752016-03-21 17:37:52 +01001728
Igor D.Ccaadc442017-11-06 12:48:48 +00001729 new_class = self.neutron.create_sfc_flow_classifier(
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001730 {'flow_classifier': classification_dict})
1731 return new_class['flow_classifier']['id']
1732 except (neExceptions.ConnectionFailed, ksExceptions.ClientException,
1733 neExceptions.NeutronException, ConnectionError) as e:
1734 self.logger.error(
1735 'Creation of Classification failed.')
1736 self._format_exception(e)
1737
1738 def get_classification(self, class_id):
1739 self.logger.debug(" Getting Classification %s from VIM", class_id)
1740 filter_dict = {"id": class_id}
1741 class_list = self.get_classification_list(filter_dict)
1742 if len(class_list) == 0:
1743 raise vimconn.vimconnNotFoundException(
1744 "Classification '{}' not found".format(class_id))
1745 elif len(class_list) > 1:
1746 raise vimconn.vimconnConflictException(
1747 "Found more than one Classification with this criteria")
1748 classification = class_list[0]
1749 return classification
1750
1751 def get_classification_list(self, filter_dict={}):
1752 self.logger.debug("Getting Classifications from VIM filter: '%s'",
1753 str(filter_dict))
1754 try:
tierno69b590e2018-03-13 18:52:23 +01001755 filter_dict_os = filter_dict.copy()
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001756 self._reload_connection()
tierno69b590e2018-03-13 18:52:23 +01001757 if self.api_version3 and "tenant_id" in filter_dict_os:
1758 filter_dict_os['project_id'] = filter_dict_os.pop('tenant_id')
Igor D.Ccaadc442017-11-06 12:48:48 +00001759 classification_dict = self.neutron.list_sfc_flow_classifiers(
tierno69b590e2018-03-13 18:52:23 +01001760 **filter_dict_os)
1761 classification_list = classification_dict["flow_classifiers"]
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001762 self.__classification_os2mano(classification_list)
1763 return classification_list
1764 except (neExceptions.ConnectionFailed, ksExceptions.ClientException,
1765 neExceptions.NeutronException, ConnectionError) as e:
1766 self._format_exception(e)
1767
1768 def delete_classification(self, class_id):
1769 self.logger.debug("Deleting Classification '%s' from VIM", class_id)
1770 try:
1771 self._reload_connection()
Igor D.Ccaadc442017-11-06 12:48:48 +00001772 self.neutron.delete_sfc_flow_classifier(class_id)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001773 return class_id
1774 except (neExceptions.ConnectionFailed, neExceptions.NeutronException,
1775 ksExceptions.ClientException, neExceptions.NeutronException,
1776 ConnectionError) as e:
1777 self._format_exception(e)
1778
1779 def new_sfi(self, name, ingress_ports, egress_ports, sfc_encap=True):
1780 self.logger.debug(
1781 "Adding a new Service Function Instance to VIM, named '%s'", name)
1782 try:
1783 new_sfi = None
1784 self._reload_connection()
1785 correlation = None
1786 if sfc_encap:
Igor D.Ccaadc442017-11-06 12:48:48 +00001787 correlation = 'nsh'
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001788 if len(ingress_ports) != 1:
1789 raise vimconn.vimconnNotSupportedException(
1790 "OpenStack VIM connector can only have "
1791 "1 ingress port per SFI")
1792 if len(egress_ports) != 1:
1793 raise vimconn.vimconnNotSupportedException(
1794 "OpenStack VIM connector can only have "
1795 "1 egress port per SFI")
1796 sfi_dict = {'name': name,
1797 'ingress': ingress_ports[0],
1798 'egress': egress_ports[0],
1799 'service_function_parameters': {
1800 'correlation': correlation}}
Igor D.Ccaadc442017-11-06 12:48:48 +00001801 new_sfi = self.neutron.create_sfc_port_pair({'port_pair': sfi_dict})
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001802 return new_sfi['port_pair']['id']
1803 except (neExceptions.ConnectionFailed, ksExceptions.ClientException,
1804 neExceptions.NeutronException, ConnectionError) as e:
1805 if new_sfi:
1806 try:
Igor D.Ccaadc442017-11-06 12:48:48 +00001807 self.neutron.delete_sfc_port_pair(
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001808 new_sfi['port_pair']['id'])
1809 except Exception:
1810 self.logger.error(
1811 'Creation of Service Function Instance failed, with '
1812 'subsequent deletion failure as well.')
1813 self._format_exception(e)
1814
1815 def get_sfi(self, sfi_id):
1816 self.logger.debug(
1817 'Getting Service Function Instance %s from VIM', sfi_id)
1818 filter_dict = {"id": sfi_id}
1819 sfi_list = self.get_sfi_list(filter_dict)
1820 if len(sfi_list) == 0:
1821 raise vimconn.vimconnNotFoundException(
1822 "Service Function Instance '{}' not found".format(sfi_id))
1823 elif len(sfi_list) > 1:
1824 raise vimconn.vimconnConflictException(
1825 'Found more than one Service Function Instance '
1826 'with this criteria')
1827 sfi = sfi_list[0]
1828 return sfi
1829
1830 def get_sfi_list(self, filter_dict={}):
1831 self.logger.debug("Getting Service Function Instances from "
1832 "VIM filter: '%s'", str(filter_dict))
1833 try:
1834 self._reload_connection()
tierno69b590e2018-03-13 18:52:23 +01001835 filter_dict_os = filter_dict.copy()
1836 if self.api_version3 and "tenant_id" in filter_dict_os:
1837 filter_dict_os['project_id'] = filter_dict_os.pop('tenant_id')
1838 sfi_dict = self.neutron.list_sfc_port_pairs(**filter_dict_os)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001839 sfi_list = sfi_dict["port_pairs"]
1840 self.__sfi_os2mano(sfi_list)
1841 return sfi_list
1842 except (neExceptions.ConnectionFailed, ksExceptions.ClientException,
1843 neExceptions.NeutronException, ConnectionError) as e:
1844 self._format_exception(e)
1845
1846 def delete_sfi(self, sfi_id):
1847 self.logger.debug("Deleting Service Function Instance '%s' "
1848 "from VIM", sfi_id)
1849 try:
1850 self._reload_connection()
Igor D.Ccaadc442017-11-06 12:48:48 +00001851 self.neutron.delete_sfc_port_pair(sfi_id)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001852 return sfi_id
1853 except (neExceptions.ConnectionFailed, neExceptions.NeutronException,
1854 ksExceptions.ClientException, neExceptions.NeutronException,
1855 ConnectionError) as e:
1856 self._format_exception(e)
1857
1858 def new_sf(self, name, sfis, sfc_encap=True):
1859 self.logger.debug("Adding a new Service Function to VIM, "
1860 "named '%s'", name)
1861 try:
1862 new_sf = None
1863 self._reload_connection()
tierno9c5c8322018-03-23 15:44:03 +01001864 # correlation = None
1865 # if sfc_encap:
1866 # correlation = 'nsh'
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001867 for instance in sfis:
1868 sfi = self.get_sfi(instance)
Igor D.Ccaadc442017-11-06 12:48:48 +00001869 if sfi.get('sfc_encap') != sfc_encap:
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001870 raise vimconn.vimconnNotSupportedException(
1871 "OpenStack VIM connector requires all SFIs of the "
1872 "same SF to share the same SFC Encapsulation")
1873 sf_dict = {'name': name,
1874 'port_pairs': sfis}
Igor D.Ccaadc442017-11-06 12:48:48 +00001875 new_sf = self.neutron.create_sfc_port_pair_group({
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001876 'port_pair_group': sf_dict})
1877 return new_sf['port_pair_group']['id']
1878 except (neExceptions.ConnectionFailed, ksExceptions.ClientException,
1879 neExceptions.NeutronException, ConnectionError) as e:
1880 if new_sf:
1881 try:
Igor D.Ccaadc442017-11-06 12:48:48 +00001882 self.neutron.delete_sfc_port_pair_group(
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001883 new_sf['port_pair_group']['id'])
1884 except Exception:
1885 self.logger.error(
1886 'Creation of Service Function failed, with '
1887 'subsequent deletion failure as well.')
1888 self._format_exception(e)
1889
1890 def get_sf(self, sf_id):
1891 self.logger.debug("Getting Service Function %s from VIM", sf_id)
1892 filter_dict = {"id": sf_id}
1893 sf_list = self.get_sf_list(filter_dict)
1894 if len(sf_list) == 0:
1895 raise vimconn.vimconnNotFoundException(
1896 "Service Function '{}' not found".format(sf_id))
1897 elif len(sf_list) > 1:
1898 raise vimconn.vimconnConflictException(
1899 "Found more than one Service Function with this criteria")
1900 sf = sf_list[0]
1901 return sf
1902
1903 def get_sf_list(self, filter_dict={}):
1904 self.logger.debug("Getting Service Function from VIM filter: '%s'",
1905 str(filter_dict))
1906 try:
1907 self._reload_connection()
tierno69b590e2018-03-13 18:52:23 +01001908 filter_dict_os = filter_dict.copy()
1909 if self.api_version3 and "tenant_id" in filter_dict_os:
1910 filter_dict_os['project_id'] = filter_dict_os.pop('tenant_id')
1911 sf_dict = self.neutron.list_sfc_port_pair_groups(**filter_dict_os)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001912 sf_list = sf_dict["port_pair_groups"]
1913 self.__sf_os2mano(sf_list)
1914 return sf_list
1915 except (neExceptions.ConnectionFailed, ksExceptions.ClientException,
1916 neExceptions.NeutronException, ConnectionError) as e:
1917 self._format_exception(e)
1918
1919 def delete_sf(self, sf_id):
1920 self.logger.debug("Deleting Service Function '%s' from VIM", sf_id)
1921 try:
1922 self._reload_connection()
Igor D.Ccaadc442017-11-06 12:48:48 +00001923 self.neutron.delete_sfc_port_pair_group(sf_id)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001924 return sf_id
1925 except (neExceptions.ConnectionFailed, neExceptions.NeutronException,
1926 ksExceptions.ClientException, neExceptions.NeutronException,
1927 ConnectionError) as e:
1928 self._format_exception(e)
1929
1930 def new_sfp(self, name, classifications, sfs, sfc_encap=True, spi=None):
1931 self.logger.debug("Adding a new Service Function Path to VIM, "
1932 "named '%s'", name)
1933 try:
1934 new_sfp = None
1935 self._reload_connection()
Igor D.Ccaadc442017-11-06 12:48:48 +00001936 # In networking-sfc the MPLS encapsulation is legacy
1937 # should be used when no full SFC Encapsulation is intended
1938 sfc_encap = 'mpls'
1939 if sfc_encap:
1940 correlation = 'nsh'
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001941 sfp_dict = {'name': name,
1942 'flow_classifiers': classifications,
1943 'port_pair_groups': sfs,
1944 'chain_parameters': {'correlation': correlation}}
1945 if spi:
1946 sfp_dict['chain_id'] = spi
Igor D.Ccaadc442017-11-06 12:48:48 +00001947 new_sfp = self.neutron.create_sfc_port_chain({'port_chain': sfp_dict})
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001948 return new_sfp["port_chain"]["id"]
1949 except (neExceptions.ConnectionFailed, ksExceptions.ClientException,
1950 neExceptions.NeutronException, ConnectionError) as e:
1951 if new_sfp:
1952 try:
Igor D.Ccaadc442017-11-06 12:48:48 +00001953 self.neutron.delete_sfc_port_chain(new_sfp['port_chain']['id'])
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001954 except Exception:
1955 self.logger.error(
1956 'Creation of Service Function Path failed, with '
1957 'subsequent deletion failure as well.')
1958 self._format_exception(e)
1959
1960 def get_sfp(self, sfp_id):
1961 self.logger.debug(" Getting Service Function Path %s from VIM", sfp_id)
1962 filter_dict = {"id": sfp_id}
1963 sfp_list = self.get_sfp_list(filter_dict)
1964 if len(sfp_list) == 0:
1965 raise vimconn.vimconnNotFoundException(
1966 "Service Function Path '{}' not found".format(sfp_id))
1967 elif len(sfp_list) > 1:
1968 raise vimconn.vimconnConflictException(
1969 "Found more than one Service Function Path with this criteria")
1970 sfp = sfp_list[0]
1971 return sfp
1972
1973 def get_sfp_list(self, filter_dict={}):
1974 self.logger.debug("Getting Service Function Paths from VIM filter: "
1975 "'%s'", str(filter_dict))
1976 try:
1977 self._reload_connection()
tierno69b590e2018-03-13 18:52:23 +01001978 filter_dict_os = filter_dict.copy()
1979 if self.api_version3 and "tenant_id" in filter_dict_os:
1980 filter_dict_os['project_id'] = filter_dict_os.pop('tenant_id')
1981 sfp_dict = self.neutron.list_sfc_port_chains(**filter_dict_os)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001982 sfp_list = sfp_dict["port_chains"]
1983 self.__sfp_os2mano(sfp_list)
1984 return sfp_list
1985 except (neExceptions.ConnectionFailed, ksExceptions.ClientException,
1986 neExceptions.NeutronException, ConnectionError) as e:
1987 self._format_exception(e)
1988
1989 def delete_sfp(self, sfp_id):
1990 self.logger.debug(
1991 "Deleting Service Function Path '%s' from VIM", sfp_id)
1992 try:
1993 self._reload_connection()
Igor D.Ccaadc442017-11-06 12:48:48 +00001994 self.neutron.delete_sfc_port_chain(sfp_id)
Igor Duarte Cardoso3cf9bcd2017-08-14 16:39:34 +00001995 return sfp_id
1996 except (neExceptions.ConnectionFailed, neExceptions.NeutronException,
1997 ksExceptions.ClientException, neExceptions.NeutronException,
1998 ConnectionError) as e:
1999 self._format_exception(e)