4a897a3603d1521a1397899accec303b82ad7058
[osm/RO.git] / osm_ro / vimconn_openstack.py
1 # -*- coding: utf-8 -*-
2
3 ##
4 # Copyright 2015 Telefonica Investigacion 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 '''
25 osconnector implements all the methods to interact with openstack using the python-neutronclient.
26
27 For the VNF forwarding graph, The OpenStack VIM connector calls the
28 networking-sfc Neutron extension methods, whose resources are mapped
29 to 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)
34 '''
35 __author__ = "Alfonso Tierno, Gerardo Garcia, Pablo Montes, xFlow Research, Igor D.C., Eduardo Sousa"
36 __date__ = "$22-sep-2017 23:59:59$"
37
38 import vimconn
39 # import json
40 import logging
41 import netaddr
42 import time
43 import yaml
44 import random
45 import re
46 import copy
47 from pprint import pformat
48 from types import StringTypes
49
50 from novaclient import client as nClient, exceptions as nvExceptions
51 from keystoneauth1.identity import v2, v3
52 from keystoneauth1 import session
53 import keystoneclient.exceptions as ksExceptions
54 import keystoneclient.v3.client as ksClient_v3
55 import keystoneclient.v2_0.client as ksClient_v2
56 from glanceclient import client as glClient
57 import glanceclient.exc as gl1Exceptions
58 from cinderclient import client as cClient
59 from httplib import HTTPException
60 from neutronclient.neutron import client as neClient
61 from neutronclient.common import exceptions as neExceptions
62 from requests.exceptions import ConnectionError
63
64
65 """contain the openstack virtual machine status to openmano status"""
66 vmStatus2manoFormat={'ACTIVE':'ACTIVE',
67 'PAUSED':'PAUSED',
68 'SUSPENDED': 'SUSPENDED',
69 'SHUTOFF':'INACTIVE',
70 'BUILD':'BUILD',
71 'ERROR':'ERROR','DELETED':'DELETED'
72 }
73 netStatus2manoFormat={'ACTIVE':'ACTIVE','PAUSED':'PAUSED','INACTIVE':'INACTIVE','BUILD':'BUILD','ERROR':'ERROR','DELETED':'DELETED'
74 }
75
76 supportedClassificationTypes = ['legacy_flow_classifier']
77
78 #global var to have a timeout creating and deleting volumes
79 volume_timeout = 600
80 server_timeout = 600
81
82
83 class SafeDumper(yaml.SafeDumper):
84 def represent_data(self, data):
85 # Openstack APIs use custom subclasses of dict and YAML safe dumper
86 # is designed to not handle that (reference issue 142 of pyyaml)
87 if isinstance(data, dict) and data.__class__ != dict:
88 # A simple solution is to convert those items back to dicts
89 data = dict(data.items())
90
91 return super(SafeDumper, self).represent_data(data)
92
93
94 class vimconnector(vimconn.vimconnector):
95 def __init__(self, uuid, name, tenant_id, tenant_name, url, url_admin=None, user=None, passwd=None,
96 log_level=None, config={}, persistent_info={}):
97 '''using common constructor parameters. In this case
98 'url' is the keystone authorization url,
99 'url_admin' is not use
100 '''
101 api_version = config.get('APIversion')
102 if api_version and api_version not in ('v3.3', 'v2.0', '2', '3'):
103 raise vimconn.vimconnException("Invalid value '{}' for config:APIversion. "
104 "Allowed values are 'v3.3', 'v2.0', '2' or '3'".format(api_version))
105 vim_type = config.get('vim_type')
106 if vim_type and vim_type not in ('vio', 'VIO'):
107 raise vimconn.vimconnException("Invalid value '{}' for config:vim_type."
108 "Allowed values are 'vio' or 'VIO'".format(vim_type))
109
110 if config.get('dataplane_net_vlan_range') is not None:
111 #validate vlan ranges provided by user
112 self._validate_vlan_ranges(config.get('dataplane_net_vlan_range'), 'dataplane_net_vlan_range')
113
114 if config.get('multisegment_vlan_range') is not None:
115 #validate vlan ranges provided by user
116 self._validate_vlan_ranges(config.get('multisegment_vlan_range'), 'multisegment_vlan_range')
117
118 vimconn.vimconnector.__init__(self, uuid, name, tenant_id, tenant_name, url, url_admin, user, passwd, log_level,
119 config)
120
121 if self.config.get("insecure") and self.config.get("ca_cert"):
122 raise vimconn.vimconnException("options insecure and ca_cert are mutually exclusive")
123 self.verify = True
124 if self.config.get("insecure"):
125 self.verify = False
126 if self.config.get("ca_cert"):
127 self.verify = self.config.get("ca_cert")
128
129 if not url:
130 raise TypeError('url param can not be NoneType')
131 self.persistent_info = persistent_info
132 self.availability_zone = persistent_info.get('availability_zone', None)
133 self.session = persistent_info.get('session', {'reload_client': True})
134 self.my_tenant_id = self.session.get('my_tenant_id')
135 self.nova = self.session.get('nova')
136 self.neutron = self.session.get('neutron')
137 self.cinder = self.session.get('cinder')
138 self.glance = self.session.get('glance')
139 # self.glancev1 = self.session.get('glancev1')
140 self.keystone = self.session.get('keystone')
141 self.api_version3 = self.session.get('api_version3')
142 self.vim_type = self.config.get("vim_type")
143 if self.vim_type:
144 self.vim_type = self.vim_type.upper()
145 if self.config.get("use_internal_endpoint"):
146 self.endpoint_type = "internalURL"
147 else:
148 self.endpoint_type = None
149
150 self.logger = logging.getLogger('openmano.vim.openstack')
151
152 # allow security_groups to be a list or a single string
153 if isinstance(self.config.get('security_groups'), str):
154 self.config['security_groups'] = [self.config['security_groups']]
155 self.security_groups_id = None
156
157 ####### VIO Specific Changes #########
158 if self.vim_type == "VIO":
159 self.logger = logging.getLogger('openmano.vim.vio')
160
161 if log_level:
162 self.logger.setLevel( getattr(logging, log_level))
163
164 def __getitem__(self, index):
165 """Get individuals parameters.
166 Throw KeyError"""
167 if index == 'project_domain_id':
168 return self.config.get("project_domain_id")
169 elif index == 'user_domain_id':
170 return self.config.get("user_domain_id")
171 else:
172 return vimconn.vimconnector.__getitem__(self, index)
173
174 def __setitem__(self, index, value):
175 """Set individuals parameters and it is marked as dirty so to force connection reload.
176 Throw KeyError"""
177 if index == 'project_domain_id':
178 self.config["project_domain_id"] = value
179 elif index == 'user_domain_id':
180 self.config["user_domain_id"] = value
181 else:
182 vimconn.vimconnector.__setitem__(self, index, value)
183 self.session['reload_client'] = True
184
185 def serialize(self, value):
186 """Serialization of python basic types.
187
188 In the case value is not serializable a message will be logged and a
189 simple representation of the data that cannot be converted back to
190 python is returned.
191 """
192 if isinstance(value, StringTypes):
193 return value
194
195 try:
196 return yaml.dump(value, Dumper=SafeDumper,
197 default_flow_style=True, width=256)
198 except yaml.representer.RepresenterError:
199 self.logger.debug(
200 'The following entity cannot be serialized in YAML:'
201 '\n\n%s\n\n', pformat(value), exc_info=True)
202 return str(value)
203
204 def _reload_connection(self):
205 '''Called before any operation, it check if credentials has changed
206 Throw keystoneclient.apiclient.exceptions.AuthorizationFailure
207 '''
208 #TODO control the timing and possible token timeout, but it seams that python client does this task for us :-)
209 if self.session['reload_client']:
210 if self.config.get('APIversion'):
211 self.api_version3 = self.config['APIversion'] == 'v3.3' or self.config['APIversion'] == '3'
212 else: # get from ending auth_url that end with v3 or with v2.0
213 self.api_version3 = self.url.endswith("/v3") or self.url.endswith("/v3/")
214 self.session['api_version3'] = self.api_version3
215 if self.api_version3:
216 if self.config.get('project_domain_id') or self.config.get('project_domain_name'):
217 project_domain_id_default = None
218 else:
219 project_domain_id_default = 'default'
220 if self.config.get('user_domain_id') or self.config.get('user_domain_name'):
221 user_domain_id_default = None
222 else:
223 user_domain_id_default = 'default'
224 auth = v3.Password(auth_url=self.url,
225 username=self.user,
226 password=self.passwd,
227 project_name=self.tenant_name,
228 project_id=self.tenant_id,
229 project_domain_id=self.config.get('project_domain_id', project_domain_id_default),
230 user_domain_id=self.config.get('user_domain_id', user_domain_id_default),
231 project_domain_name=self.config.get('project_domain_name'),
232 user_domain_name=self.config.get('user_domain_name'))
233 else:
234 auth = v2.Password(auth_url=self.url,
235 username=self.user,
236 password=self.passwd,
237 tenant_name=self.tenant_name,
238 tenant_id=self.tenant_id)
239 sess = session.Session(auth=auth, verify=self.verify)
240 # addedd region_name to keystone, nova, neutron and cinder to support distributed cloud for Wind River Titanium cloud and StarlingX
241 region_name = self.config.get('region_name')
242 if self.api_version3:
243 self.keystone = ksClient_v3.Client(session=sess, endpoint_type=self.endpoint_type, region_name=region_name)
244 else:
245 self.keystone = ksClient_v2.Client(session=sess, endpoint_type=self.endpoint_type)
246 self.session['keystone'] = self.keystone
247 # In order to enable microversion functionality an explicit microversion must be specified in 'config'.
248 # This implementation approach is due to the warning message in
249 # https://developer.openstack.org/api-guide/compute/microversions.html
250 # where it is stated that microversion backwards compatibility is not guaranteed and clients should
251 # always require an specific microversion.
252 # To be able to use 'device role tagging' functionality define 'microversion: 2.32' in datacenter config
253 version = self.config.get("microversion")
254 if not version:
255 version = "2.1"
256 # addedd region_name to keystone, nova, neutron and cinder to support distributed cloud for Wind River Titanium cloud and StarlingX
257 self.nova = self.session['nova'] = nClient.Client(str(version), session=sess, endpoint_type=self.endpoint_type, region_name=region_name)
258 self.neutron = self.session['neutron'] = neClient.Client('2.0', session=sess, endpoint_type=self.endpoint_type, region_name=region_name)
259 self.cinder = self.session['cinder'] = cClient.Client(2, session=sess, endpoint_type=self.endpoint_type, region_name=region_name)
260 try:
261 self.my_tenant_id = self.session['my_tenant_id'] = sess.get_project_id()
262 except Exception as e:
263 self.logger.error("Cannot get project_id from session", exc_info=True)
264 if self.endpoint_type == "internalURL":
265 glance_service_id = self.keystone.services.list(name="glance")[0].id
266 glance_endpoint = self.keystone.endpoints.list(glance_service_id, interface="internal")[0].url
267 else:
268 glance_endpoint = None
269 self.glance = self.session['glance'] = glClient.Client(2, session=sess, endpoint=glance_endpoint)
270 # using version 1 of glance client in new_image()
271 # self.glancev1 = self.session['glancev1'] = glClient.Client('1', session=sess,
272 # endpoint=glance_endpoint)
273 self.session['reload_client'] = False
274 self.persistent_info['session'] = self.session
275 # add availablity zone info inside self.persistent_info
276 self._set_availablity_zones()
277 self.persistent_info['availability_zone'] = self.availability_zone
278 self.security_groups_id = None # force to get again security_groups_ids next time they are needed
279
280 def __net_os2mano(self, net_list_dict):
281 '''Transform the net openstack format to mano format
282 net_list_dict can be a list of dict or a single dict'''
283 if type(net_list_dict) is dict:
284 net_list_=(net_list_dict,)
285 elif type(net_list_dict) is list:
286 net_list_=net_list_dict
287 else:
288 raise TypeError("param net_list_dict must be a list or a dictionary")
289 for net in net_list_:
290 if net.get('provider:network_type') == "vlan":
291 net['type']='data'
292 else:
293 net['type']='bridge'
294
295 def __classification_os2mano(self, class_list_dict):
296 """Transform the openstack format (Flow Classifier) to mano format
297 (Classification) class_list_dict can be a list of dict or a single dict
298 """
299 if isinstance(class_list_dict, dict):
300 class_list_ = [class_list_dict]
301 elif isinstance(class_list_dict, list):
302 class_list_ = class_list_dict
303 else:
304 raise TypeError(
305 "param class_list_dict must be a list or a dictionary")
306 for classification in class_list_:
307 id = classification.pop('id')
308 name = classification.pop('name')
309 description = classification.pop('description')
310 project_id = classification.pop('project_id')
311 tenant_id = classification.pop('tenant_id')
312 original_classification = copy.deepcopy(classification)
313 classification.clear()
314 classification['ctype'] = 'legacy_flow_classifier'
315 classification['definition'] = original_classification
316 classification['id'] = id
317 classification['name'] = name
318 classification['description'] = description
319 classification['project_id'] = project_id
320 classification['tenant_id'] = tenant_id
321
322 def __sfi_os2mano(self, sfi_list_dict):
323 """Transform the openstack format (Port Pair) to mano format (SFI)
324 sfi_list_dict can be a list of dict or a single dict
325 """
326 if isinstance(sfi_list_dict, dict):
327 sfi_list_ = [sfi_list_dict]
328 elif isinstance(sfi_list_dict, list):
329 sfi_list_ = sfi_list_dict
330 else:
331 raise TypeError(
332 "param sfi_list_dict must be a list or a dictionary")
333 for sfi in sfi_list_:
334 sfi['ingress_ports'] = []
335 sfi['egress_ports'] = []
336 if sfi.get('ingress'):
337 sfi['ingress_ports'].append(sfi['ingress'])
338 if sfi.get('egress'):
339 sfi['egress_ports'].append(sfi['egress'])
340 del sfi['ingress']
341 del sfi['egress']
342 params = sfi.get('service_function_parameters')
343 sfc_encap = False
344 if params:
345 correlation = params.get('correlation')
346 if correlation:
347 sfc_encap = True
348 sfi['sfc_encap'] = sfc_encap
349 del sfi['service_function_parameters']
350
351 def __sf_os2mano(self, sf_list_dict):
352 """Transform the openstack format (Port Pair Group) to mano format (SF)
353 sf_list_dict can be a list of dict or a single dict
354 """
355 if isinstance(sf_list_dict, dict):
356 sf_list_ = [sf_list_dict]
357 elif isinstance(sf_list_dict, list):
358 sf_list_ = sf_list_dict
359 else:
360 raise TypeError(
361 "param sf_list_dict must be a list or a dictionary")
362 for sf in sf_list_:
363 del sf['port_pair_group_parameters']
364 sf['sfis'] = sf['port_pairs']
365 del sf['port_pairs']
366
367 def __sfp_os2mano(self, sfp_list_dict):
368 """Transform the openstack format (Port Chain) to mano format (SFP)
369 sfp_list_dict can be a list of dict or a single dict
370 """
371 if isinstance(sfp_list_dict, dict):
372 sfp_list_ = [sfp_list_dict]
373 elif isinstance(sfp_list_dict, list):
374 sfp_list_ = sfp_list_dict
375 else:
376 raise TypeError(
377 "param sfp_list_dict must be a list or a dictionary")
378 for sfp in sfp_list_:
379 params = sfp.pop('chain_parameters')
380 sfc_encap = False
381 if params:
382 correlation = params.get('correlation')
383 if correlation:
384 sfc_encap = True
385 sfp['sfc_encap'] = sfc_encap
386 sfp['spi'] = sfp.pop('chain_id')
387 sfp['classifications'] = sfp.pop('flow_classifiers')
388 sfp['service_functions'] = sfp.pop('port_pair_groups')
389
390 # placeholder for now; read TODO note below
391 def _validate_classification(self, type, definition):
392 # only legacy_flow_classifier Type is supported at this point
393 return True
394 # TODO(igordcard): this method should be an abstract method of an
395 # abstract Classification class to be implemented by the specific
396 # Types. Also, abstract vimconnector should call the validation
397 # method before the implemented VIM connectors are called.
398
399 def _format_exception(self, exception):
400 '''Transform a keystone, nova, neutron exception into a vimconn exception'''
401
402 # Fixing bug 665 https://osm.etsi.org/bugzilla/show_bug.cgi?id=665
403 # There are some openstack versions that message error are unicode with non English
404 message_error = exception.message
405 if isinstance(message_error, unicode):
406 message_error = message_error.encode("utf")
407
408 if isinstance(exception, (neExceptions.NetworkNotFoundClient, nvExceptions.NotFound, ksExceptions.NotFound,
409 gl1Exceptions.HTTPNotFound)):
410 raise vimconn.vimconnNotFoundException(type(exception).__name__ + ": " + message_error)
411 elif isinstance(exception, (HTTPException, gl1Exceptions.HTTPException, gl1Exceptions.CommunicationError,
412 ConnectionError, ksExceptions.ConnectionError, neExceptions.ConnectionFailed)):
413 raise vimconn.vimconnConnectionException(type(exception).__name__ + ": " + message_error)
414 elif isinstance(exception, (KeyError, nvExceptions.BadRequest, ksExceptions.BadRequest)):
415 raise vimconn.vimconnException(type(exception).__name__ + ": " + message_error)
416 elif isinstance(exception, (nvExceptions.ClientException, ksExceptions.ClientException,
417 neExceptions.NeutronException)):
418 raise vimconn.vimconnUnexpectedResponse(type(exception).__name__ + ": " + message_error)
419 elif isinstance(exception, nvExceptions.Conflict):
420 raise vimconn.vimconnConflictException(type(exception).__name__ + ": " + message_error)
421 elif isinstance(exception, vimconn.vimconnException):
422 raise exception
423 else: # ()
424 self.logger.error("General Exception " + message_error, exc_info=True)
425 raise vimconn.vimconnConnectionException(type(exception).__name__ + ": " + message_error)
426
427 def _get_ids_from_name(self):
428 """
429 Obtain ids from name of tenant and security_groups. Store at self .security_groups_id"
430 :return: None
431 """
432 # get tenant_id if only tenant_name is supplied
433 self._reload_connection()
434 if not self.my_tenant_id:
435 raise vimconn.vimconnConnectionException("Error getting tenant information from name={} id={}".
436 format(self.tenant_name, self.tenant_id))
437 if self.config.get('security_groups') and not self.security_groups_id:
438 # convert from name to id
439 neutron_sg_list = self.neutron.list_security_groups(tenant_id=self.my_tenant_id)["security_groups"]
440
441 self.security_groups_id = []
442 for sg in self.config.get('security_groups'):
443 for neutron_sg in neutron_sg_list:
444 if sg in (neutron_sg["id"], neutron_sg["name"]):
445 self.security_groups_id.append(neutron_sg["id"])
446 break
447 else:
448 self.security_groups_id = None
449 raise vimconn.vimconnConnectionException("Not found security group {} for this tenant".format(sg))
450
451 def check_vim_connectivity(self):
452 # just get network list to check connectivity and credentials
453 self.get_network_list(filter_dict={})
454
455 def get_tenant_list(self, filter_dict={}):
456 '''Obtain tenants of VIM
457 filter_dict can contain the following keys:
458 name: filter by tenant name
459 id: filter by tenant uuid/id
460 <other VIM specific>
461 Returns the tenant list of dictionaries: [{'name':'<name>, 'id':'<id>, ...}, ...]
462 '''
463 self.logger.debug("Getting tenants from VIM filter: '%s'", str(filter_dict))
464 try:
465 self._reload_connection()
466 if self.api_version3:
467 project_class_list = self.keystone.projects.list(name=filter_dict.get("name"))
468 else:
469 project_class_list = self.keystone.tenants.findall(**filter_dict)
470 project_list=[]
471 for project in project_class_list:
472 if filter_dict.get('id') and filter_dict["id"] != project.id:
473 continue
474 project_list.append(project.to_dict())
475 return project_list
476 except (ksExceptions.ConnectionError, ksExceptions.ClientException, ConnectionError) as e:
477 self._format_exception(e)
478
479 def new_tenant(self, tenant_name, tenant_description):
480 '''Adds a new tenant to openstack VIM. Returns the tenant identifier'''
481 self.logger.debug("Adding a new tenant name: %s", tenant_name)
482 try:
483 self._reload_connection()
484 if self.api_version3:
485 project = self.keystone.projects.create(tenant_name, self.config.get("project_domain_id", "default"),
486 description=tenant_description, is_domain=False)
487 else:
488 project = self.keystone.tenants.create(tenant_name, tenant_description)
489 return project.id
490 except (ksExceptions.ConnectionError, ksExceptions.ClientException, ksExceptions.BadRequest, ConnectionError) as e:
491 self._format_exception(e)
492
493 def delete_tenant(self, tenant_id):
494 '''Delete a tenant from openstack VIM. Returns the old tenant identifier'''
495 self.logger.debug("Deleting tenant %s from VIM", tenant_id)
496 try:
497 self._reload_connection()
498 if self.api_version3:
499 self.keystone.projects.delete(tenant_id)
500 else:
501 self.keystone.tenants.delete(tenant_id)
502 return tenant_id
503 except (ksExceptions.ConnectionError, ksExceptions.ClientException, ksExceptions.NotFound, ConnectionError) as e:
504 self._format_exception(e)
505
506 def new_network(self,net_name, net_type, ip_profile=None, shared=False, vlan=None):
507 """Adds a tenant network to VIM
508 Params:
509 'net_name': name of the network
510 'net_type': one of:
511 'bridge': overlay isolated network
512 'data': underlay E-LAN network for Passthrough and SRIOV interfaces
513 'ptp': underlay E-LINE network for Passthrough and SRIOV interfaces.
514 'ip_profile': is a dict containing the IP parameters of the network
515 'ip_version': can be "IPv4" or "IPv6" (Currently only IPv4 is implemented)
516 'subnet_address': ip_prefix_schema, that is X.X.X.X/Y
517 'gateway_address': (Optional) ip_schema, that is X.X.X.X
518 'dns_address': (Optional) comma separated list of ip_schema, e.g. X.X.X.X[,X,X,X,X]
519 'dhcp_enabled': True or False
520 'dhcp_start_address': ip_schema, first IP to grant
521 'dhcp_count': number of IPs to grant.
522 'shared': if this network can be seen/use by other tenants/organization
523 'vlan': in case of a data or ptp net_type, the intended vlan tag to be used for the network
524 Returns a tuple with the network identifier and created_items, or raises an exception on error
525 created_items can be None or a dictionary where this method can include key-values that will be passed to
526 the method delete_network. Can be used to store created segments, created l2gw connections, etc.
527 Format is vimconnector dependent, but do not use nested dictionaries and a value of None should be the same
528 as not present.
529 """
530 self.logger.debug("Adding a new network to VIM name '%s', type '%s'", net_name, net_type)
531 # self.logger.debug(">>>>>>>>>>>>>>>>>> IP profile %s", str(ip_profile))
532 try:
533 new_net = None
534 created_items = {}
535 self._reload_connection()
536 network_dict = {'name': net_name, 'admin_state_up': True}
537 if net_type=="data" or net_type=="ptp":
538 if self.config.get('dataplane_physical_net') == None:
539 raise vimconn.vimconnConflictException("You must provide a 'dataplane_physical_net' at config value before creating sriov network")
540 if not self.config.get('multisegment_support'):
541 network_dict["provider:physical_network"] = self.config[
542 'dataplane_physical_net'] # "physnet_sriov" #TODO physical
543 network_dict["provider:network_type"] = "vlan"
544 if vlan!=None:
545 network_dict["provider:network_type"] = vlan
546 else:
547 ###### Multi-segment case ######
548 segment_list = []
549 segment1_dict = {}
550 segment1_dict["provider:physical_network"] = ''
551 segment1_dict["provider:network_type"] = 'vxlan'
552 segment_list.append(segment1_dict)
553 segment2_dict = {}
554 segment2_dict["provider:physical_network"] = self.config['dataplane_physical_net']
555 segment2_dict["provider:network_type"] = "vlan"
556 if self.config.get('multisegment_vlan_range'):
557 vlanID = self._generate_multisegment_vlanID()
558 segment2_dict["provider:segmentation_id"] = vlanID
559 # else
560 # raise vimconn.vimconnConflictException(
561 # "You must provide 'multisegment_vlan_range' at config dict before creating a multisegment network")
562 segment_list.append(segment2_dict)
563 network_dict["segments"] = segment_list
564
565 ####### VIO Specific Changes #########
566 if self.vim_type == "VIO":
567 if vlan is not None:
568 network_dict["provider:segmentation_id"] = vlan
569 else:
570 if self.config.get('dataplane_net_vlan_range') is None:
571 raise vimconn.vimconnConflictException("You must provide "\
572 "'dataplane_net_vlan_range' in format [start_ID - end_ID]"\
573 "at config value before creating sriov network with vlan tag")
574
575 network_dict["provider:segmentation_id"] = self._generate_vlanID()
576
577 network_dict["shared"] = shared
578 if self.config.get("disable_network_port_security"):
579 network_dict["port_security_enabled"] = False
580 new_net = self.neutron.create_network({'network':network_dict})
581 # print new_net
582 # create subnetwork, even if there is no profile
583 if not ip_profile:
584 ip_profile = {}
585 if not ip_profile.get('subnet_address'):
586 #Fake subnet is required
587 subnet_rand = random.randint(0, 255)
588 ip_profile['subnet_address'] = "192.168.{}.0/24".format(subnet_rand)
589 if 'ip_version' not in ip_profile:
590 ip_profile['ip_version'] = "IPv4"
591 subnet = {"name": net_name+"-subnet",
592 "network_id": new_net["network"]["id"],
593 "ip_version": 4 if ip_profile['ip_version']=="IPv4" else 6,
594 "cidr": ip_profile['subnet_address']
595 }
596 # Gateway should be set to None if not needed. Otherwise openstack assigns one by default
597 if ip_profile.get('gateway_address'):
598 subnet['gateway_ip'] = ip_profile['gateway_address']
599 else:
600 subnet['gateway_ip'] = None
601 if ip_profile.get('dns_address'):
602 subnet['dns_nameservers'] = ip_profile['dns_address'].split(";")
603 if 'dhcp_enabled' in ip_profile:
604 subnet['enable_dhcp'] = False if \
605 ip_profile['dhcp_enabled']=="false" or ip_profile['dhcp_enabled']==False else True
606 if ip_profile.get('dhcp_start_address'):
607 subnet['allocation_pools'] = []
608 subnet['allocation_pools'].append(dict())
609 subnet['allocation_pools'][0]['start'] = ip_profile['dhcp_start_address']
610 if ip_profile.get('dhcp_count'):
611 #parts = ip_profile['dhcp_start_address'].split('.')
612 #ip_int = (int(parts[0]) << 24) + (int(parts[1]) << 16) + (int(parts[2]) << 8) + int(parts[3])
613 ip_int = int(netaddr.IPAddress(ip_profile['dhcp_start_address']))
614 ip_int += ip_profile['dhcp_count'] - 1
615 ip_str = str(netaddr.IPAddress(ip_int))
616 subnet['allocation_pools'][0]['end'] = ip_str
617 #self.logger.debug(">>>>>>>>>>>>>>>>>> Subnet: %s", str(subnet))
618 self.neutron.create_subnet({"subnet": subnet} )
619
620 if net_type == "data" and self.config.get('multisegment_support'):
621 if self.config.get('l2gw_support'):
622 l2gw_list = self.neutron.list_l2_gateways().get("l2_gateways", ())
623 for l2gw in l2gw_list:
624 l2gw_conn = {}
625 l2gw_conn["l2_gateway_id"] = l2gw["id"]
626 l2gw_conn["network_id"] = new_net["network"]["id"]
627 l2gw_conn["segmentation_id"] = str(vlanID)
628 new_l2gw_conn = self.neutron.create_l2_gateway_connection({"l2_gateway_connection": l2gw_conn})
629 created_items["l2gwconn:" + str(new_l2gw_conn["l2_gateway_connection"]["id"])] = True
630 return new_net["network"]["id"], created_items
631 except Exception as e:
632 #delete l2gw connections (if any) before deleting the network
633 for k, v in created_items.items():
634 if not v: # skip already deleted
635 continue
636 try:
637 k_item, _, k_id = k.partition(":")
638 if k_item == "l2gwconn":
639 self.neutron.delete_l2_gateway_connection(k_id)
640 except Exception as e2:
641 self.logger.error("Error deleting l2 gateway connection: {}: {}".format(type(e2).__name__, e2))
642 if new_net:
643 self.neutron.delete_network(new_net['network']['id'])
644 self._format_exception(e)
645
646 def get_network_list(self, filter_dict={}):
647 '''Obtain tenant networks of VIM
648 Filter_dict can be:
649 name: network name
650 id: network uuid
651 shared: boolean
652 tenant_id: tenant
653 admin_state_up: boolean
654 status: 'ACTIVE'
655 Returns the network list of dictionaries
656 '''
657 self.logger.debug("Getting network from VIM filter: '%s'", str(filter_dict))
658 try:
659 self._reload_connection()
660 filter_dict_os = filter_dict.copy()
661 if self.api_version3 and "tenant_id" in filter_dict_os:
662 filter_dict_os['project_id'] = filter_dict_os.pop('tenant_id') #T ODO check
663 net_dict = self.neutron.list_networks(**filter_dict_os)
664 net_list = net_dict["networks"]
665 self.__net_os2mano(net_list)
666 return net_list
667 except (neExceptions.ConnectionFailed, ksExceptions.ClientException, neExceptions.NeutronException, ConnectionError) as e:
668 self._format_exception(e)
669
670 def get_network(self, net_id):
671 '''Obtain details of network from VIM
672 Returns the network information from a network id'''
673 self.logger.debug(" Getting tenant network %s from VIM", net_id)
674 filter_dict={"id": net_id}
675 net_list = self.get_network_list(filter_dict)
676 if len(net_list)==0:
677 raise vimconn.vimconnNotFoundException("Network '{}' not found".format(net_id))
678 elif len(net_list)>1:
679 raise vimconn.vimconnConflictException("Found more than one network with this criteria")
680 net = net_list[0]
681 subnets=[]
682 for subnet_id in net.get("subnets", () ):
683 try:
684 subnet = self.neutron.show_subnet(subnet_id)
685 except Exception as e:
686 self.logger.error("osconnector.get_network(): Error getting subnet %s %s" % (net_id, str(e)))
687 subnet = {"id": subnet_id, "fault": str(e)}
688 subnets.append(subnet)
689 net["subnets"] = subnets
690 net["encapsulation"] = net.get('provider:network_type')
691 net["encapsulation_type"] = net.get('provider:network_type')
692 net["segmentation_id"] = net.get('provider:segmentation_id')
693 net["encapsulation_id"] = net.get('provider:segmentation_id')
694 return net
695
696 def delete_network(self, net_id, created_items=None):
697 """
698 Removes a tenant network from VIM and its associated elements
699 :param net_id: VIM identifier of the network, provided by method new_network
700 :param created_items: dictionary with extra items to be deleted. provided by method new_network
701 Returns the network identifier or raises an exception upon error or when network is not found
702 """
703 self.logger.debug("Deleting network '%s' from VIM", net_id)
704 if created_items == None:
705 created_items = {}
706 try:
707 self._reload_connection()
708 #delete l2gw connections (if any) before deleting the network
709 for k, v in created_items.items():
710 if not v: # skip already deleted
711 continue
712 try:
713 k_item, _, k_id = k.partition(":")
714 if k_item == "l2gwconn":
715 self.neutron.delete_l2_gateway_connection(k_id)
716 except Exception as e:
717 self.logger.error("Error deleting l2 gateway connection: {}: {}".format(type(e).__name__, e))
718 #delete VM ports attached to this networks before the network
719 ports = self.neutron.list_ports(network_id=net_id)
720 for p in ports['ports']:
721 try:
722 self.neutron.delete_port(p["id"])
723 except Exception as e:
724 self.logger.error("Error deleting port %s: %s", p["id"], str(e))
725 self.neutron.delete_network(net_id)
726 return net_id
727 except (neExceptions.ConnectionFailed, neExceptions.NetworkNotFoundClient, neExceptions.NeutronException,
728 ksExceptions.ClientException, neExceptions.NeutronException, ConnectionError) as e:
729 self._format_exception(e)
730
731 def refresh_nets_status(self, net_list):
732 '''Get the status of the networks
733 Params: the list of network identifiers
734 Returns a dictionary with:
735 net_id: #VIM id of this network
736 status: #Mandatory. Text with one of:
737 # DELETED (not found at vim)
738 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
739 # OTHER (Vim reported other status not understood)
740 # ERROR (VIM indicates an ERROR status)
741 # ACTIVE, INACTIVE, DOWN (admin down),
742 # BUILD (on building process)
743 #
744 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
745 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
746
747 '''
748 net_dict={}
749 for net_id in net_list:
750 net = {}
751 try:
752 net_vim = self.get_network(net_id)
753 if net_vim['status'] in netStatus2manoFormat:
754 net["status"] = netStatus2manoFormat[ net_vim['status'] ]
755 else:
756 net["status"] = "OTHER"
757 net["error_msg"] = "VIM status reported " + net_vim['status']
758
759 if net['status'] == "ACTIVE" and not net_vim['admin_state_up']:
760 net['status'] = 'DOWN'
761
762 net['vim_info'] = self.serialize(net_vim)
763
764 if net_vim.get('fault'): #TODO
765 net['error_msg'] = str(net_vim['fault'])
766 except vimconn.vimconnNotFoundException as e:
767 self.logger.error("Exception getting net status: %s", str(e))
768 net['status'] = "DELETED"
769 net['error_msg'] = str(e)
770 except vimconn.vimconnException as e:
771 self.logger.error("Exception getting net status: %s", str(e))
772 net['status'] = "VIM_ERROR"
773 net['error_msg'] = str(e)
774 net_dict[net_id] = net
775 return net_dict
776
777 def get_flavor(self, flavor_id):
778 '''Obtain flavor details from the VIM. Returns the flavor dict details'''
779 self.logger.debug("Getting flavor '%s'", flavor_id)
780 try:
781 self._reload_connection()
782 flavor = self.nova.flavors.find(id=flavor_id)
783 #TODO parse input and translate to VIM format (openmano_schemas.new_vminstance_response_schema)
784 return flavor.to_dict()
785 except (nvExceptions.NotFound, nvExceptions.ClientException, ksExceptions.ClientException, ConnectionError) as e:
786 self._format_exception(e)
787
788 def get_flavor_id_from_data(self, flavor_dict):
789 """Obtain flavor id that match the flavor description
790 Returns the flavor_id or raises a vimconnNotFoundException
791 flavor_dict: contains the required ram, vcpus, disk
792 If 'use_existing_flavors' is set to True at config, the closer flavor that provides same or more ram, vcpus
793 and disk is returned. Otherwise a flavor with exactly same ram, vcpus and disk is returned or a
794 vimconnNotFoundException is raised
795 """
796 exact_match = False if self.config.get('use_existing_flavors') else True
797 try:
798 self._reload_connection()
799 flavor_candidate_id = None
800 flavor_candidate_data = (10000, 10000, 10000)
801 flavor_target = (flavor_dict["ram"], flavor_dict["vcpus"], flavor_dict["disk"])
802 # numa=None
803 extended = flavor_dict.get("extended", {})
804 if extended:
805 #TODO
806 raise vimconn.vimconnNotFoundException("Flavor with EPA still not implemented")
807 # if len(numas) > 1:
808 # raise vimconn.vimconnNotFoundException("Cannot find any flavor with more than one numa")
809 # numa=numas[0]
810 # numas = extended.get("numas")
811 for flavor in self.nova.flavors.list():
812 epa = flavor.get_keys()
813 if epa:
814 continue
815 # TODO
816 flavor_data = (flavor.ram, flavor.vcpus, flavor.disk)
817 if flavor_data == flavor_target:
818 return flavor.id
819 elif not exact_match and flavor_target < flavor_data < flavor_candidate_data:
820 flavor_candidate_id = flavor.id
821 flavor_candidate_data = flavor_data
822 if not exact_match and flavor_candidate_id:
823 return flavor_candidate_id
824 raise vimconn.vimconnNotFoundException("Cannot find any flavor matching '{}'".format(str(flavor_dict)))
825 except (nvExceptions.NotFound, nvExceptions.ClientException, ksExceptions.ClientException, ConnectionError) as e:
826 self._format_exception(e)
827
828 def process_resource_quota(self, quota, prefix, extra_specs):
829 """
830 :param prefix:
831 :param extra_specs:
832 :return:
833 """
834 if 'limit' in quota:
835 extra_specs["quota:" + prefix + "_limit"] = quota['limit']
836 if 'reserve' in quota:
837 extra_specs["quota:" + prefix + "_reservation"] = quota['reserve']
838 if 'shares' in quota:
839 extra_specs["quota:" + prefix + "_shares_level"] = "custom"
840 extra_specs["quota:" + prefix + "_shares_share"] = quota['shares']
841
842 def new_flavor(self, flavor_data, change_name_if_used=True):
843 '''Adds a tenant flavor to openstack VIM
844 if change_name_if_used is True, it will change name in case of conflict, because it is not supported name repetition
845 Returns the flavor identifier
846 '''
847 self.logger.debug("Adding flavor '%s'", str(flavor_data))
848 retry=0
849 max_retries=3
850 name_suffix = 0
851 try:
852 name=flavor_data['name']
853 while retry<max_retries:
854 retry+=1
855 try:
856 self._reload_connection()
857 if change_name_if_used:
858 #get used names
859 fl_names=[]
860 fl=self.nova.flavors.list()
861 for f in fl:
862 fl_names.append(f.name)
863 while name in fl_names:
864 name_suffix += 1
865 name = flavor_data['name']+"-" + str(name_suffix)
866
867 ram = flavor_data.get('ram',64)
868 vcpus = flavor_data.get('vcpus',1)
869 extra_specs={}
870
871 extended = flavor_data.get("extended")
872 if extended:
873 numas=extended.get("numas")
874 if numas:
875 numa_nodes = len(numas)
876 if numa_nodes > 1:
877 return -1, "Can not add flavor with more than one numa"
878 extra_specs["hw:numa_nodes"] = str(numa_nodes)
879 extra_specs["hw:mem_page_size"] = "large"
880 extra_specs["hw:cpu_policy"] = "dedicated"
881 extra_specs["hw:numa_mempolicy"] = "strict"
882 if self.vim_type == "VIO":
883 extra_specs["vmware:extra_config"] = '{"numa.nodeAffinity":"0"}'
884 extra_specs["vmware:latency_sensitivity_level"] = "high"
885 for numa in numas:
886 #overwrite ram and vcpus
887 #check if key 'memory' is present in numa else use ram value at flavor
888 if 'memory' in numa:
889 ram = numa['memory']*1024
890 #See for reference: https://specs.openstack.org/openstack/nova-specs/specs/mitaka/implemented/virt-driver-cpu-thread-pinning.html
891 extra_specs["hw:cpu_sockets"] = 1
892 if 'paired-threads' in numa:
893 vcpus = numa['paired-threads']*2
894 #cpu_thread_policy "require" implies that the compute node must have an STM architecture
895 extra_specs["hw:cpu_thread_policy"] = "require"
896 extra_specs["hw:cpu_policy"] = "dedicated"
897 elif 'cores' in numa:
898 vcpus = numa['cores']
899 # cpu_thread_policy "prefer" implies that the host must not have an SMT architecture, or a non-SMT architecture will be emulated
900 extra_specs["hw:cpu_thread_policy"] = "isolate"
901 extra_specs["hw:cpu_policy"] = "dedicated"
902 elif 'threads' in numa:
903 vcpus = numa['threads']
904 # cpu_thread_policy "prefer" implies that the host may or may not have an SMT architecture
905 extra_specs["hw:cpu_thread_policy"] = "prefer"
906 extra_specs["hw:cpu_policy"] = "dedicated"
907 # for interface in numa.get("interfaces",() ):
908 # if interface["dedicated"]=="yes":
909 # raise vimconn.vimconnException("Passthrough interfaces are not supported for the openstack connector", http_code=vimconn.HTTP_Service_Unavailable)
910 # #TODO, add the key 'pci_passthrough:alias"="<label at config>:<number ifaces>"' when a way to connect it is available
911 elif extended.get("cpu-quota"):
912 self.process_resource_quota(extended.get("cpu-quota"), "cpu", extra_specs)
913 if extended.get("mem-quota"):
914 self.process_resource_quota(extended.get("mem-quota"), "memory", extra_specs)
915 if extended.get("vif-quota"):
916 self.process_resource_quota(extended.get("vif-quota"), "vif", extra_specs)
917 if extended.get("disk-io-quota"):
918 self.process_resource_quota(extended.get("disk-io-quota"), "disk_io", extra_specs)
919 #create flavor
920 new_flavor=self.nova.flavors.create(name,
921 ram,
922 vcpus,
923 flavor_data.get('disk',0),
924 is_public=flavor_data.get('is_public', True)
925 )
926 #add metadata
927 if extra_specs:
928 new_flavor.set_keys(extra_specs)
929 return new_flavor.id
930 except nvExceptions.Conflict as e:
931 if change_name_if_used and retry < max_retries:
932 continue
933 self._format_exception(e)
934 #except nvExceptions.BadRequest as e:
935 except (ksExceptions.ClientException, nvExceptions.ClientException, ConnectionError, KeyError) as e:
936 self._format_exception(e)
937
938 def delete_flavor(self,flavor_id):
939 '''Deletes a tenant flavor from openstack VIM. Returns the old flavor_id
940 '''
941 try:
942 self._reload_connection()
943 self.nova.flavors.delete(flavor_id)
944 return flavor_id
945 #except nvExceptions.BadRequest as e:
946 except (nvExceptions.NotFound, ksExceptions.ClientException, nvExceptions.ClientException, ConnectionError) as e:
947 self._format_exception(e)
948
949 def new_image(self,image_dict):
950 '''
951 Adds a tenant image to VIM. imge_dict is a dictionary with:
952 name: name
953 disk_format: qcow2, vhd, vmdk, raw (by default), ...
954 location: path or URI
955 public: "yes" or "no"
956 metadata: metadata of the image
957 Returns the image_id
958 '''
959 retry=0
960 max_retries=3
961 while retry<max_retries:
962 retry+=1
963 try:
964 self._reload_connection()
965 #determine format http://docs.openstack.org/developer/glance/formats.html
966 if "disk_format" in image_dict:
967 disk_format=image_dict["disk_format"]
968 else: #autodiscover based on extension
969 if image_dict['location'].endswith(".qcow2"):
970 disk_format="qcow2"
971 elif image_dict['location'].endswith(".vhd"):
972 disk_format="vhd"
973 elif image_dict['location'].endswith(".vmdk"):
974 disk_format="vmdk"
975 elif image_dict['location'].endswith(".vdi"):
976 disk_format="vdi"
977 elif image_dict['location'].endswith(".iso"):
978 disk_format="iso"
979 elif image_dict['location'].endswith(".aki"):
980 disk_format="aki"
981 elif image_dict['location'].endswith(".ari"):
982 disk_format="ari"
983 elif image_dict['location'].endswith(".ami"):
984 disk_format="ami"
985 else:
986 disk_format="raw"
987 self.logger.debug("new_image: '%s' loading from '%s'", image_dict['name'], image_dict['location'])
988 if self.vim_type == "VIO":
989 container_format = "bare"
990 if 'container_format' in image_dict:
991 container_format = image_dict['container_format']
992 new_image = self.glance.images.create(name=image_dict['name'], container_format=container_format,
993 disk_format=disk_format)
994 else:
995 new_image = self.glance.images.create(name=image_dict['name'])
996 if image_dict['location'].startswith("http"):
997 # TODO there is not a method to direct download. It must be downloaded locally with requests
998 raise vimconn.vimconnNotImplemented("Cannot create image from URL")
999 else: #local path
1000 with open(image_dict['location']) as fimage:
1001 self.glance.images.upload(new_image.id, fimage)
1002 #new_image = self.glancev1.images.create(name=image_dict['name'], is_public=image_dict.get('public',"yes")=="yes",
1003 # container_format="bare", data=fimage, disk_format=disk_format)
1004 metadata_to_load = image_dict.get('metadata')
1005 # TODO location is a reserved word for current openstack versions. fixed for VIO please check for openstack
1006 if self.vim_type == "VIO":
1007 metadata_to_load['upload_location'] = image_dict['location']
1008 else:
1009 metadata_to_load['location'] = image_dict['location']
1010 self.glance.images.update(new_image.id, **metadata_to_load)
1011 return new_image.id
1012 except (nvExceptions.Conflict, ksExceptions.ClientException, nvExceptions.ClientException) as e:
1013 self._format_exception(e)
1014 except (HTTPException, gl1Exceptions.HTTPException, gl1Exceptions.CommunicationError, ConnectionError) as e:
1015 if retry==max_retries:
1016 continue
1017 self._format_exception(e)
1018 except IOError as e: #can not open the file
1019 raise vimconn.vimconnConnectionException(type(e).__name__ + ": " + str(e)+ " for " + image_dict['location'],
1020 http_code=vimconn.HTTP_Bad_Request)
1021
1022 def delete_image(self, image_id):
1023 '''Deletes a tenant image from openstack VIM. Returns the old id
1024 '''
1025 try:
1026 self._reload_connection()
1027 self.glance.images.delete(image_id)
1028 return image_id
1029 except (nvExceptions.NotFound, ksExceptions.ClientException, nvExceptions.ClientException, gl1Exceptions.CommunicationError, gl1Exceptions.HTTPNotFound, ConnectionError) as e: #TODO remove
1030 self._format_exception(e)
1031
1032 def get_image_id_from_path(self, path):
1033 '''Get the image id from image path in the VIM database. Returns the image_id'''
1034 try:
1035 self._reload_connection()
1036 images = self.glance.images.list()
1037 for image in images:
1038 if image.metadata.get("location")==path:
1039 return image.id
1040 raise vimconn.vimconnNotFoundException("image with location '{}' not found".format( path))
1041 except (ksExceptions.ClientException, nvExceptions.ClientException, gl1Exceptions.CommunicationError, ConnectionError) as e:
1042 self._format_exception(e)
1043
1044 def get_image_list(self, filter_dict={}):
1045 '''Obtain tenant images from VIM
1046 Filter_dict can be:
1047 id: image id
1048 name: image name
1049 checksum: image checksum
1050 Returns the image list of dictionaries:
1051 [{<the fields at Filter_dict plus some VIM specific>}, ...]
1052 List can be empty
1053 '''
1054 self.logger.debug("Getting image list from VIM filter: '%s'", str(filter_dict))
1055 try:
1056 self._reload_connection()
1057 filter_dict_os = filter_dict.copy()
1058 #First we filter by the available filter fields: name, id. The others are removed.
1059 image_list = self.glance.images.list()
1060 filtered_list = []
1061 for image in image_list:
1062 try:
1063 if filter_dict.get("name") and image["name"] != filter_dict["name"]:
1064 continue
1065 if filter_dict.get("id") and image["id"] != filter_dict["id"]:
1066 continue
1067 if filter_dict.get("checksum") and image["checksum"] != filter_dict["checksum"]:
1068 continue
1069
1070 filtered_list.append(image.copy())
1071 except gl1Exceptions.HTTPNotFound:
1072 pass
1073 return filtered_list
1074 except (ksExceptions.ClientException, nvExceptions.ClientException, gl1Exceptions.CommunicationError, ConnectionError) as e:
1075 self._format_exception(e)
1076
1077 def __wait_for_vm(self, vm_id, status):
1078 """wait until vm is in the desired status and return True.
1079 If the VM gets in ERROR status, return false.
1080 If the timeout is reached generate an exception"""
1081 elapsed_time = 0
1082 while elapsed_time < server_timeout:
1083 vm_status = self.nova.servers.get(vm_id).status
1084 if vm_status == status:
1085 return True
1086 if vm_status == 'ERROR':
1087 return False
1088 time.sleep(5)
1089 elapsed_time += 5
1090
1091 # if we exceeded the timeout rollback
1092 if elapsed_time >= server_timeout:
1093 raise vimconn.vimconnException('Timeout waiting for instance ' + vm_id + ' to get ' + status,
1094 http_code=vimconn.HTTP_Request_Timeout)
1095
1096 def _get_openstack_availablity_zones(self):
1097 """
1098 Get from openstack availability zones available
1099 :return:
1100 """
1101 try:
1102 openstack_availability_zone = self.nova.availability_zones.list()
1103 openstack_availability_zone = [str(zone.zoneName) for zone in openstack_availability_zone
1104 if zone.zoneName != 'internal']
1105 return openstack_availability_zone
1106 except Exception as e:
1107 return None
1108
1109 def _set_availablity_zones(self):
1110 """
1111 Set vim availablity zone
1112 :return:
1113 """
1114
1115 if 'availability_zone' in self.config:
1116 vim_availability_zones = self.config.get('availability_zone')
1117 if isinstance(vim_availability_zones, str):
1118 self.availability_zone = [vim_availability_zones]
1119 elif isinstance(vim_availability_zones, list):
1120 self.availability_zone = vim_availability_zones
1121 else:
1122 self.availability_zone = self._get_openstack_availablity_zones()
1123
1124 def _get_vm_availability_zone(self, availability_zone_index, availability_zone_list):
1125 """
1126 Return thge availability zone to be used by the created VM.
1127 :return: The VIM availability zone to be used or None
1128 """
1129 if availability_zone_index is None:
1130 if not self.config.get('availability_zone'):
1131 return None
1132 elif isinstance(self.config.get('availability_zone'), str):
1133 return self.config['availability_zone']
1134 else:
1135 # TODO consider using a different parameter at config for default AV and AV list match
1136 return self.config['availability_zone'][0]
1137
1138 vim_availability_zones = self.availability_zone
1139 # check if VIM offer enough availability zones describe in the VNFD
1140 if vim_availability_zones and len(availability_zone_list) <= len(vim_availability_zones):
1141 # check if all the names of NFV AV match VIM AV names
1142 match_by_index = False
1143 for av in availability_zone_list:
1144 if av not in vim_availability_zones:
1145 match_by_index = True
1146 break
1147 if match_by_index:
1148 return vim_availability_zones[availability_zone_index]
1149 else:
1150 return availability_zone_list[availability_zone_index]
1151 else:
1152 raise vimconn.vimconnConflictException("No enough availability zones at VIM for this deployment")
1153
1154 def new_vminstance(self, name, description, start, image_id, flavor_id, net_list, cloud_config=None, disk_list=None,
1155 availability_zone_index=None, availability_zone_list=None):
1156 """Adds a VM instance to VIM
1157 Params:
1158 start: indicates if VM must start or boot in pause mode. Ignored
1159 image_id,flavor_id: iamge and flavor uuid
1160 net_list: list of interfaces, each one is a dictionary with:
1161 name:
1162 net_id: network uuid to connect
1163 vpci: virtual vcpi to assign, ignored because openstack lack #TODO
1164 model: interface model, ignored #TODO
1165 mac_address: used for SR-IOV ifaces #TODO for other types
1166 use: 'data', 'bridge', 'mgmt'
1167 type: 'virtual', 'PCI-PASSTHROUGH'('PF'), 'SR-IOV'('VF'), 'VFnotShared'
1168 vim_id: filled/added by this function
1169 floating_ip: True/False (or it can be None)
1170 'cloud_config': (optional) dictionary with:
1171 'key-pairs': (optional) list of strings with the public key to be inserted to the default user
1172 'users': (optional) list of users to be inserted, each item is a dict with:
1173 'name': (mandatory) user name,
1174 'key-pairs': (optional) list of strings with the public key to be inserted to the user
1175 'user-data': (optional) string is a text script to be passed directly to cloud-init
1176 'config-files': (optional). List of files to be transferred. Each item is a dict with:
1177 'dest': (mandatory) string with the destination absolute path
1178 'encoding': (optional, by default text). Can be one of:
1179 'b64', 'base64', 'gz', 'gz+b64', 'gz+base64', 'gzip+b64', 'gzip+base64'
1180 'content' (mandatory): string with the content of the file
1181 'permissions': (optional) string with file permissions, typically octal notation '0644'
1182 'owner': (optional) file owner, string with the format 'owner:group'
1183 'boot-data-drive': boolean to indicate if user-data must be passed using a boot drive (hard disk)
1184 'disk_list': (optional) list with additional disks to the VM. Each item is a dict with:
1185 'image_id': (optional). VIM id of an existing image. If not provided an empty disk must be mounted
1186 'size': (mandatory) string with the size of the disk in GB
1187 'vim_id' (optional) should use this existing volume id
1188 availability_zone_index: Index of availability_zone_list to use for this this VM. None if not AV required
1189 availability_zone_list: list of availability zones given by user in the VNFD descriptor. Ignore if
1190 availability_zone_index is None
1191 #TODO ip, security groups
1192 Returns a tuple with the instance identifier and created_items or raises an exception on error
1193 created_items can be None or a dictionary where this method can include key-values that will be passed to
1194 the method delete_vminstance and action_vminstance. Can be used to store created ports, volumes, etc.
1195 Format is vimconnector dependent, but do not use nested dictionaries and a value of None should be the same
1196 as not present.
1197 """
1198 self.logger.debug("new_vminstance input: image='%s' flavor='%s' nics='%s'",image_id, flavor_id,str(net_list))
1199 try:
1200 server = None
1201 created_items = {}
1202 # metadata = {}
1203 net_list_vim = []
1204 external_network = [] # list of external networks to be connected to instance, later on used to create floating_ip
1205 no_secured_ports = [] # List of port-is with port-security disabled
1206 self._reload_connection()
1207 # metadata_vpci = {} # For a specific neutron plugin
1208 block_device_mapping = None
1209
1210 for net in net_list:
1211 if not net.get("net_id"): # skip non connected iface
1212 continue
1213
1214 port_dict = {
1215 "network_id": net["net_id"],
1216 "name": net.get("name"),
1217 "admin_state_up": True
1218 }
1219 if self.config.get("security_groups") and net.get("port_security") is not False and \
1220 not self.config.get("no_port_security_extension"):
1221 if not self.security_groups_id:
1222 self._get_ids_from_name()
1223 port_dict["security_groups"] = self.security_groups_id
1224
1225 if net["type"]=="virtual":
1226 pass
1227 # if "vpci" in net:
1228 # metadata_vpci[ net["net_id"] ] = [[ net["vpci"], "" ]]
1229 elif net["type"] == "VF" or net["type"] == "SR-IOV": # for VF
1230 # if "vpci" in net:
1231 # if "VF" not in metadata_vpci:
1232 # metadata_vpci["VF"]=[]
1233 # metadata_vpci["VF"].append([ net["vpci"], "" ])
1234 port_dict["binding:vnic_type"]="direct"
1235 # VIO specific Changes
1236 if self.vim_type == "VIO":
1237 # Need to create port with port_security_enabled = False and no-security-groups
1238 port_dict["port_security_enabled"]=False
1239 port_dict["provider_security_groups"]=[]
1240 port_dict["security_groups"]=[]
1241 else: # For PT PCI-PASSTHROUGH
1242 # VIO specific Changes
1243 # Current VIO release does not support port with type 'direct-physical'
1244 # So no need to create virtual port in case of PCI-device.
1245 # Will update port_dict code when support gets added in next VIO release
1246 if self.vim_type == "VIO":
1247 raise vimconn.vimconnNotSupportedException(
1248 "Current VIO release does not support full passthrough (PT)")
1249 # if "vpci" in net:
1250 # if "PF" not in metadata_vpci:
1251 # metadata_vpci["PF"]=[]
1252 # metadata_vpci["PF"].append([ net["vpci"], "" ])
1253 port_dict["binding:vnic_type"]="direct-physical"
1254 if not port_dict["name"]:
1255 port_dict["name"]=name
1256 if net.get("mac_address"):
1257 port_dict["mac_address"]=net["mac_address"]
1258 if net.get("ip_address"):
1259 port_dict["fixed_ips"] = [{'ip_address': net["ip_address"]}]
1260 # TODO add 'subnet_id': <subnet_id>
1261 new_port = self.neutron.create_port({"port": port_dict })
1262 created_items["port:" + str(new_port["port"]["id"])] = True
1263 net["mac_adress"] = new_port["port"]["mac_address"]
1264 net["vim_id"] = new_port["port"]["id"]
1265 # if try to use a network without subnetwork, it will return a emtpy list
1266 fixed_ips = new_port["port"].get("fixed_ips")
1267 if fixed_ips:
1268 net["ip"] = fixed_ips[0].get("ip_address")
1269 else:
1270 net["ip"] = None
1271
1272 port = {"port-id": new_port["port"]["id"]}
1273 if float(self.nova.api_version.get_string()) >= 2.32:
1274 port["tag"] = new_port["port"]["name"]
1275 net_list_vim.append(port)
1276
1277 if net.get('floating_ip', False):
1278 net['exit_on_floating_ip_error'] = True
1279 external_network.append(net)
1280 elif net['use'] == 'mgmt' and self.config.get('use_floating_ip'):
1281 net['exit_on_floating_ip_error'] = False
1282 external_network.append(net)
1283 net['floating_ip'] = self.config.get('use_floating_ip')
1284
1285 # If port security is disabled when the port has not yet been attached to the VM, then all vm traffic is dropped.
1286 # As a workaround we wait until the VM is active and then disable the port-security
1287 if net.get("port_security") == False and not self.config.get("no_port_security_extension"):
1288 no_secured_ports.append(new_port["port"]["id"])
1289
1290 # if metadata_vpci:
1291 # metadata = {"pci_assignement": json.dumps(metadata_vpci)}
1292 # if len(metadata["pci_assignement"]) >255:
1293 # #limit the metadata size
1294 # #metadata["pci_assignement"] = metadata["pci_assignement"][0:255]
1295 # self.logger.warn("Metadata deleted since it exceeds the expected length (255) ")
1296 # metadata = {}
1297
1298 self.logger.debug("name '%s' image_id '%s'flavor_id '%s' net_list_vim '%s' description '%s'",
1299 name, image_id, flavor_id, str(net_list_vim), description)
1300
1301 # cloud config
1302 config_drive, userdata = self._create_user_data(cloud_config)
1303
1304 # Create additional volumes in case these are present in disk_list
1305 base_disk_index = ord('b')
1306 if disk_list:
1307 block_device_mapping = {}
1308 for disk in disk_list:
1309 if disk.get('vim_id'):
1310 block_device_mapping['_vd' + chr(base_disk_index)] = disk['vim_id']
1311 else:
1312 if 'image_id' in disk:
1313 volume = self.cinder.volumes.create(size=disk['size'], name=name + '_vd' +
1314 chr(base_disk_index), imageRef=disk['image_id'])
1315 else:
1316 volume = self.cinder.volumes.create(size=disk['size'], name=name + '_vd' +
1317 chr(base_disk_index))
1318 created_items["volume:" + str(volume.id)] = True
1319 block_device_mapping['_vd' + chr(base_disk_index)] = volume.id
1320 base_disk_index += 1
1321
1322 # Wait until created volumes are with status available
1323 elapsed_time = 0
1324 while elapsed_time < volume_timeout:
1325 for created_item in created_items:
1326 v, _, volume_id = created_item.partition(":")
1327 if v == 'volume':
1328 if self.cinder.volumes.get(volume_id).status != 'available':
1329 break
1330 else: # all ready: break from while
1331 break
1332 time.sleep(5)
1333 elapsed_time += 5
1334 # If we exceeded the timeout rollback
1335 if elapsed_time >= volume_timeout:
1336 raise vimconn.vimconnException('Timeout creating volumes for instance ' + name,
1337 http_code=vimconn.HTTP_Request_Timeout)
1338 # get availability Zone
1339 vm_av_zone = self._get_vm_availability_zone(availability_zone_index, availability_zone_list)
1340
1341 self.logger.debug("nova.servers.create({}, {}, {}, nics={}, security_groups={}, "
1342 "availability_zone={}, key_name={}, userdata={}, config_drive={}, "
1343 "block_device_mapping={})".format(name, image_id, flavor_id, net_list_vim,
1344 self.config.get("security_groups"), vm_av_zone,
1345 self.config.get('keypair'), userdata, config_drive,
1346 block_device_mapping))
1347 server = self.nova.servers.create(name, image_id, flavor_id, nics=net_list_vim,
1348 security_groups=self.config.get("security_groups"),
1349 # TODO remove security_groups in future versions. Already at neutron port
1350 availability_zone=vm_av_zone,
1351 key_name=self.config.get('keypair'),
1352 userdata=userdata,
1353 config_drive=config_drive,
1354 block_device_mapping=block_device_mapping
1355 ) # , description=description)
1356
1357 vm_start_time = time.time()
1358 # Previously mentioned workaround to wait until the VM is active and then disable the port-security
1359 if no_secured_ports:
1360 self.__wait_for_vm(server.id, 'ACTIVE')
1361
1362 for port_id in no_secured_ports:
1363 try:
1364 self.neutron.update_port(port_id,
1365 {"port": {"port_security_enabled": False, "security_groups": None}})
1366 except Exception as e:
1367 raise vimconn.vimconnException("It was not possible to disable port security for port {}".format(
1368 port_id))
1369 # print "DONE :-)", server
1370
1371 # pool_id = None
1372 if external_network:
1373 floating_ips = self.neutron.list_floatingips().get("floatingips", ())
1374 for floating_network in external_network:
1375 try:
1376 assigned = False
1377 while not assigned:
1378 if floating_ips:
1379 ip = floating_ips.pop(0)
1380 if ip.get("port_id", False) or ip.get('tenant_id') != server.tenant_id:
1381 continue
1382 if isinstance(floating_network['floating_ip'], str):
1383 if ip.get("floating_network_id") != floating_network['floating_ip']:
1384 continue
1385 free_floating_ip = ip.get("floating_ip_address")
1386 else:
1387 if isinstance(floating_network['floating_ip'], str) and \
1388 floating_network['floating_ip'].lower() != "true":
1389 pool_id = floating_network['floating_ip']
1390 else:
1391 # Find the external network
1392 external_nets = list()
1393 for net in self.neutron.list_networks()['networks']:
1394 if net['router:external']:
1395 external_nets.append(net)
1396
1397 if len(external_nets) == 0:
1398 raise vimconn.vimconnException("Cannot create floating_ip automatically since no external "
1399 "network is present",
1400 http_code=vimconn.HTTP_Conflict)
1401 if len(external_nets) > 1:
1402 raise vimconn.vimconnException("Cannot create floating_ip automatically since multiple "
1403 "external networks are present",
1404 http_code=vimconn.HTTP_Conflict)
1405
1406 pool_id = external_nets[0].get('id')
1407 param = {'floatingip': {'floating_network_id': pool_id, 'tenant_id': server.tenant_id}}
1408 try:
1409 # self.logger.debug("Creating floating IP")
1410 new_floating_ip = self.neutron.create_floatingip(param)
1411 free_floating_ip = new_floating_ip['floatingip']['floating_ip_address']
1412 except Exception as e:
1413 raise vimconn.vimconnException(type(e).__name__ + ": Cannot create new floating_ip " +
1414 str(e), http_code=vimconn.HTTP_Conflict)
1415
1416 fix_ip = floating_network.get('ip')
1417 while not assigned:
1418 try:
1419 server.add_floating_ip(free_floating_ip, fix_ip)
1420 assigned = True
1421 except Exception as e:
1422 # openstack need some time after VM creation to asign an IP. So retry if fails
1423 vm_status = self.nova.servers.get(server.id).status
1424 if vm_status != 'ACTIVE' and vm_status != 'ERROR':
1425 if time.time() - vm_start_time < server_timeout:
1426 time.sleep(5)
1427 continue
1428 raise vimconn.vimconnException(
1429 "Cannot create floating_ip: {} {}".format(type(e).__name__, e),
1430 http_code=vimconn.HTTP_Conflict)
1431
1432 except Exception as e:
1433 if not floating_network['exit_on_floating_ip_error']:
1434 self.logger.warn("Cannot create floating_ip. %s", str(e))
1435 continue
1436 raise
1437
1438 return server.id, created_items
1439 # except nvExceptions.NotFound as e:
1440 # error_value=-vimconn.HTTP_Not_Found
1441 # error_text= "vm instance %s not found" % vm_id
1442 # except TypeError as e:
1443 # raise vimconn.vimconnException(type(e).__name__ + ": "+ str(e), http_code=vimconn.HTTP_Bad_Request)
1444
1445 except Exception as e:
1446 server_id = None
1447 if server:
1448 server_id = server.id
1449 try:
1450 self.delete_vminstance(server_id, created_items)
1451 except Exception as e2:
1452 self.logger.error("new_vminstance rollback fail {}".format(e2))
1453
1454 self._format_exception(e)
1455
1456 def get_vminstance(self,vm_id):
1457 '''Returns the VM instance information from VIM'''
1458 #self.logger.debug("Getting VM from VIM")
1459 try:
1460 self._reload_connection()
1461 server = self.nova.servers.find(id=vm_id)
1462 #TODO parse input and translate to VIM format (openmano_schemas.new_vminstance_response_schema)
1463 return server.to_dict()
1464 except (ksExceptions.ClientException, nvExceptions.ClientException, nvExceptions.NotFound, ConnectionError) as e:
1465 self._format_exception(e)
1466
1467 def get_vminstance_console(self,vm_id, console_type="vnc"):
1468 '''
1469 Get a console for the virtual machine
1470 Params:
1471 vm_id: uuid of the VM
1472 console_type, can be:
1473 "novnc" (by default), "xvpvnc" for VNC types,
1474 "rdp-html5" for RDP types, "spice-html5" for SPICE types
1475 Returns dict with the console parameters:
1476 protocol: ssh, ftp, http, https, ...
1477 server: usually ip address
1478 port: the http, ssh, ... port
1479 suffix: extra text, e.g. the http path and query string
1480 '''
1481 self.logger.debug("Getting VM CONSOLE from VIM")
1482 try:
1483 self._reload_connection()
1484 server = self.nova.servers.find(id=vm_id)
1485 if console_type == None or console_type == "novnc":
1486 console_dict = server.get_vnc_console("novnc")
1487 elif console_type == "xvpvnc":
1488 console_dict = server.get_vnc_console(console_type)
1489 elif console_type == "rdp-html5":
1490 console_dict = server.get_rdp_console(console_type)
1491 elif console_type == "spice-html5":
1492 console_dict = server.get_spice_console(console_type)
1493 else:
1494 raise vimconn.vimconnException("console type '{}' not allowed".format(console_type), http_code=vimconn.HTTP_Bad_Request)
1495
1496 console_dict1 = console_dict.get("console")
1497 if console_dict1:
1498 console_url = console_dict1.get("url")
1499 if console_url:
1500 #parse console_url
1501 protocol_index = console_url.find("//")
1502 suffix_index = console_url[protocol_index+2:].find("/") + protocol_index+2
1503 port_index = console_url[protocol_index+2:suffix_index].find(":") + protocol_index+2
1504 if protocol_index < 0 or port_index<0 or suffix_index<0:
1505 return -vimconn.HTTP_Internal_Server_Error, "Unexpected response from VIM"
1506 console_dict={"protocol": console_url[0:protocol_index],
1507 "server": console_url[protocol_index+2:port_index],
1508 "port": console_url[port_index:suffix_index],
1509 "suffix": console_url[suffix_index+1:]
1510 }
1511 protocol_index += 2
1512 return console_dict
1513 raise vimconn.vimconnUnexpectedResponse("Unexpected response from VIM")
1514
1515 except (nvExceptions.NotFound, ksExceptions.ClientException, nvExceptions.ClientException, nvExceptions.BadRequest, ConnectionError) as e:
1516 self._format_exception(e)
1517
1518 def delete_vminstance(self, vm_id, created_items=None):
1519 '''Removes a VM instance from VIM. Returns the old identifier
1520 '''
1521 #print "osconnector: Getting VM from VIM"
1522 if created_items == None:
1523 created_items = {}
1524 try:
1525 self._reload_connection()
1526 # delete VM ports attached to this networks before the virtual machine
1527 for k, v in created_items.items():
1528 if not v: # skip already deleted
1529 continue
1530 try:
1531 k_item, _, k_id = k.partition(":")
1532 if k_item == "port":
1533 self.neutron.delete_port(k_id)
1534 except Exception as e:
1535 self.logger.error("Error deleting port: {}: {}".format(type(e).__name__, e))
1536
1537 # #commented because detaching the volumes makes the servers.delete not work properly ?!?
1538 # #dettach volumes attached
1539 # server = self.nova.servers.get(vm_id)
1540 # volumes_attached_dict = server._info['os-extended-volumes:volumes_attached'] #volume['id']
1541 # #for volume in volumes_attached_dict:
1542 # # self.cinder.volumes.detach(volume['id'])
1543
1544 if vm_id:
1545 self.nova.servers.delete(vm_id)
1546
1547 # delete volumes. Although having detached, they should have in active status before deleting
1548 # we ensure in this loop
1549 keep_waiting = True
1550 elapsed_time = 0
1551 while keep_waiting and elapsed_time < volume_timeout:
1552 keep_waiting = False
1553 for k, v in created_items.items():
1554 if not v: # skip already deleted
1555 continue
1556 try:
1557 k_item, _, k_id = k.partition(":")
1558 if k_item == "volume":
1559 if self.cinder.volumes.get(k_id).status != 'available':
1560 keep_waiting = True
1561 else:
1562 self.cinder.volumes.delete(k_id)
1563 except Exception as e:
1564 self.logger.error("Error deleting volume: {}: {}".format(type(e).__name__, e))
1565 if keep_waiting:
1566 time.sleep(1)
1567 elapsed_time += 1
1568 return None
1569 except (nvExceptions.NotFound, ksExceptions.ClientException, nvExceptions.ClientException, ConnectionError) as e:
1570 self._format_exception(e)
1571
1572 def refresh_vms_status(self, vm_list):
1573 '''Get the status of the virtual machines and their interfaces/ports
1574 Params: the list of VM identifiers
1575 Returns a dictionary with:
1576 vm_id: #VIM id of this Virtual Machine
1577 status: #Mandatory. Text with one of:
1578 # DELETED (not found at vim)
1579 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
1580 # OTHER (Vim reported other status not understood)
1581 # ERROR (VIM indicates an ERROR status)
1582 # ACTIVE, PAUSED, SUSPENDED, INACTIVE (not running),
1583 # CREATING (on building process), ERROR
1584 # ACTIVE:NoMgmtIP (Active but any of its interface has an IP address
1585 #
1586 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
1587 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
1588 interfaces:
1589 - vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
1590 mac_address: #Text format XX:XX:XX:XX:XX:XX
1591 vim_net_id: #network id where this interface is connected
1592 vim_interface_id: #interface/port VIM id
1593 ip_address: #null, or text with IPv4, IPv6 address
1594 compute_node: #identification of compute node where PF,VF interface is allocated
1595 pci: #PCI address of the NIC that hosts the PF,VF
1596 vlan: #physical VLAN used for VF
1597 '''
1598 vm_dict={}
1599 self.logger.debug("refresh_vms status: Getting tenant VM instance information from VIM")
1600 for vm_id in vm_list:
1601 vm={}
1602 try:
1603 vm_vim = self.get_vminstance(vm_id)
1604 if vm_vim['status'] in vmStatus2manoFormat:
1605 vm['status'] = vmStatus2manoFormat[ vm_vim['status'] ]
1606 else:
1607 vm['status'] = "OTHER"
1608 vm['error_msg'] = "VIM status reported " + vm_vim['status']
1609
1610 vm['vim_info'] = self.serialize(vm_vim)
1611
1612 vm["interfaces"] = []
1613 if vm_vim.get('fault'):
1614 vm['error_msg'] = str(vm_vim['fault'])
1615 #get interfaces
1616 try:
1617 self._reload_connection()
1618 port_dict = self.neutron.list_ports(device_id=vm_id)
1619 for port in port_dict["ports"]:
1620 interface={}
1621 interface['vim_info'] = self.serialize(port)
1622 interface["mac_address"] = port.get("mac_address")
1623 interface["vim_net_id"] = port["network_id"]
1624 interface["vim_interface_id"] = port["id"]
1625 # check if OS-EXT-SRV-ATTR:host is there,
1626 # in case of non-admin credentials, it will be missing
1627 if vm_vim.get('OS-EXT-SRV-ATTR:host'):
1628 interface["compute_node"] = vm_vim['OS-EXT-SRV-ATTR:host']
1629 interface["pci"] = None
1630
1631 # check if binding:profile is there,
1632 # in case of non-admin credentials, it will be missing
1633 if port.get('binding:profile'):
1634 if port['binding:profile'].get('pci_slot'):
1635 # TODO: At the moment sr-iov pci addresses are converted to PF pci addresses by setting the slot to 0x00
1636 # TODO: This is just a workaround valid for niantinc. Find a better way to do so
1637 # CHANGE DDDD:BB:SS.F to DDDD:BB:00.(F%2) assuming there are 2 ports per nic
1638 pci = port['binding:profile']['pci_slot']
1639 # interface["pci"] = pci[:-4] + "00." + str(int(pci[-1]) % 2)
1640 interface["pci"] = pci
1641 interface["vlan"] = None
1642 #if network is of type vlan and port is of type direct (sr-iov) then set vlan id
1643 network = self.neutron.show_network(port["network_id"])
1644 if network['network'].get('provider:network_type') == 'vlan' and \
1645 port.get("binding:vnic_type") == "direct":
1646 interface["vlan"] = network['network'].get('provider:segmentation_id')
1647 ips=[]
1648 #look for floating ip address
1649 try:
1650 floating_ip_dict = self.neutron.list_floatingips(port_id=port["id"])
1651 if floating_ip_dict.get("floatingips"):
1652 ips.append(floating_ip_dict["floatingips"][0].get("floating_ip_address") )
1653 except Exception:
1654 pass
1655
1656 for subnet in port["fixed_ips"]:
1657 ips.append(subnet["ip_address"])
1658 interface["ip_address"] = ";".join(ips)
1659 vm["interfaces"].append(interface)
1660 except Exception as e:
1661 self.logger.error("Error getting vm interface information {}: {}".format(type(e).__name__, e),
1662 exc_info=True)
1663 except vimconn.vimconnNotFoundException as e:
1664 self.logger.error("Exception getting vm status: %s", str(e))
1665 vm['status'] = "DELETED"
1666 vm['error_msg'] = str(e)
1667 except vimconn.vimconnException as e:
1668 self.logger.error("Exception getting vm status: %s", str(e))
1669 vm['status'] = "VIM_ERROR"
1670 vm['error_msg'] = str(e)
1671 vm_dict[vm_id] = vm
1672 return vm_dict
1673
1674 def action_vminstance(self, vm_id, action_dict, created_items={}):
1675 '''Send and action over a VM instance from VIM
1676 Returns None or the console dict if the action was successfully sent to the VIM'''
1677 self.logger.debug("Action over VM '%s': %s", vm_id, str(action_dict))
1678 try:
1679 self._reload_connection()
1680 server = self.nova.servers.find(id=vm_id)
1681 if "start" in action_dict:
1682 if action_dict["start"]=="rebuild":
1683 server.rebuild()
1684 else:
1685 if server.status=="PAUSED":
1686 server.unpause()
1687 elif server.status=="SUSPENDED":
1688 server.resume()
1689 elif server.status=="SHUTOFF":
1690 server.start()
1691 elif "pause" in action_dict:
1692 server.pause()
1693 elif "resume" in action_dict:
1694 server.resume()
1695 elif "shutoff" in action_dict or "shutdown" in action_dict:
1696 server.stop()
1697 elif "forceOff" in action_dict:
1698 server.stop() #TODO
1699 elif "terminate" in action_dict:
1700 server.delete()
1701 elif "createImage" in action_dict:
1702 server.create_image()
1703 #"path":path_schema,
1704 #"description":description_schema,
1705 #"name":name_schema,
1706 #"metadata":metadata_schema,
1707 #"imageRef": id_schema,
1708 #"disk": {"oneOf":[{"type": "null"}, {"type":"string"}] },
1709 elif "rebuild" in action_dict:
1710 server.rebuild(server.image['id'])
1711 elif "reboot" in action_dict:
1712 server.reboot() #reboot_type='SOFT'
1713 elif "console" in action_dict:
1714 console_type = action_dict["console"]
1715 if console_type == None or console_type == "novnc":
1716 console_dict = server.get_vnc_console("novnc")
1717 elif console_type == "xvpvnc":
1718 console_dict = server.get_vnc_console(console_type)
1719 elif console_type == "rdp-html5":
1720 console_dict = server.get_rdp_console(console_type)
1721 elif console_type == "spice-html5":
1722 console_dict = server.get_spice_console(console_type)
1723 else:
1724 raise vimconn.vimconnException("console type '{}' not allowed".format(console_type),
1725 http_code=vimconn.HTTP_Bad_Request)
1726 try:
1727 console_url = console_dict["console"]["url"]
1728 #parse console_url
1729 protocol_index = console_url.find("//")
1730 suffix_index = console_url[protocol_index+2:].find("/") + protocol_index+2
1731 port_index = console_url[protocol_index+2:suffix_index].find(":") + protocol_index+2
1732 if protocol_index < 0 or port_index<0 or suffix_index<0:
1733 raise vimconn.vimconnException("Unexpected response from VIM " + str(console_dict))
1734 console_dict2={"protocol": console_url[0:protocol_index],
1735 "server": console_url[protocol_index+2 : port_index],
1736 "port": int(console_url[port_index+1 : suffix_index]),
1737 "suffix": console_url[suffix_index+1:]
1738 }
1739 return console_dict2
1740 except Exception as e:
1741 raise vimconn.vimconnException("Unexpected response from VIM " + str(console_dict))
1742
1743 return None
1744 except (ksExceptions.ClientException, nvExceptions.ClientException, nvExceptions.NotFound, ConnectionError) as e:
1745 self._format_exception(e)
1746 #TODO insert exception vimconn.HTTP_Unauthorized
1747
1748 ####### VIO Specific Changes #########
1749 def _generate_vlanID(self):
1750 """
1751 Method to get unused vlanID
1752 Args:
1753 None
1754 Returns:
1755 vlanID
1756 """
1757 #Get used VLAN IDs
1758 usedVlanIDs = []
1759 networks = self.get_network_list()
1760 for net in networks:
1761 if net.get('provider:segmentation_id'):
1762 usedVlanIDs.append(net.get('provider:segmentation_id'))
1763 used_vlanIDs = set(usedVlanIDs)
1764
1765 #find unused VLAN ID
1766 for vlanID_range in self.config.get('dataplane_net_vlan_range'):
1767 try:
1768 start_vlanid , end_vlanid = map(int, vlanID_range.replace(" ", "").split("-"))
1769 for vlanID in xrange(start_vlanid, end_vlanid + 1):
1770 if vlanID not in used_vlanIDs:
1771 return vlanID
1772 except Exception as exp:
1773 raise vimconn.vimconnException("Exception {} occurred while generating VLAN ID.".format(exp))
1774 else:
1775 raise vimconn.vimconnConflictException("Unable to create the SRIOV VLAN network."\
1776 " All given Vlan IDs {} are in use.".format(self.config.get('dataplane_net_vlan_range')))
1777
1778
1779 def _generate_multisegment_vlanID(self):
1780 """
1781 Method to get unused vlanID
1782 Args:
1783 None
1784 Returns:
1785 vlanID
1786 """
1787 #Get used VLAN IDs
1788 usedVlanIDs = []
1789 networks = self.get_network_list()
1790 for net in networks:
1791 if net.get('provider:network_type') == "vlan" and net.get('provider:segmentation_id'):
1792 usedVlanIDs.append(net.get('provider:segmentation_id'))
1793 elif net.get('segments'):
1794 for segment in net.get('segments'):
1795 if segment.get('provider:network_type') == "vlan" and segment.get('provider:segmentation_id'):
1796 usedVlanIDs.append(segment.get('provider:segmentation_id'))
1797 used_vlanIDs = set(usedVlanIDs)
1798
1799 #find unused VLAN ID
1800 for vlanID_range in self.config.get('multisegment_vlan_range'):
1801 try:
1802 start_vlanid , end_vlanid = map(int, vlanID_range.replace(" ", "").split("-"))
1803 for vlanID in xrange(start_vlanid, end_vlanid + 1):
1804 if vlanID not in used_vlanIDs:
1805 return vlanID
1806 except Exception as exp:
1807 raise vimconn.vimconnException("Exception {} occurred while generating VLAN ID.".format(exp))
1808 else:
1809 raise vimconn.vimconnConflictException("Unable to create the VLAN segment."\
1810 " All VLAN IDs {} are in use.".format(self.config.get('multisegment_vlan_range')))
1811
1812
1813 def _validate_vlan_ranges(self, input_vlan_range, text_vlan_range):
1814 """
1815 Method to validate user given vlanID ranges
1816 Args: None
1817 Returns: None
1818 """
1819 for vlanID_range in input_vlan_range:
1820 vlan_range = vlanID_range.replace(" ", "")
1821 #validate format
1822 vlanID_pattern = r'(\d)*-(\d)*$'
1823 match_obj = re.match(vlanID_pattern, vlan_range)
1824 if not match_obj:
1825 raise vimconn.vimconnConflictException("Invalid VLAN range for {}: {}.You must provide "\
1826 "'{}' in format [start_ID - end_ID].".format(text_vlan_range, vlanID_range, text_vlan_range))
1827
1828 start_vlanid , end_vlanid = map(int,vlan_range.split("-"))
1829 if start_vlanid <= 0 :
1830 raise vimconn.vimconnConflictException("Invalid VLAN range for {}: {}."\
1831 "Start ID can not be zero. For VLAN "\
1832 "networks valid IDs are 1 to 4094 ".format(text_vlan_range, vlanID_range))
1833 if end_vlanid > 4094 :
1834 raise vimconn.vimconnConflictException("Invalid VLAN range for {}: {}."\
1835 "End VLAN ID can not be greater than 4094. For VLAN "\
1836 "networks valid IDs are 1 to 4094 ".format(text_vlan_range, vlanID_range))
1837
1838 if start_vlanid > end_vlanid:
1839 raise vimconn.vimconnConflictException("Invalid VLAN range for {}: {}."\
1840 "You must provide '{}' in format start_ID - end_ID and "\
1841 "start_ID < end_ID ".format(text_vlan_range, vlanID_range, text_vlan_range))
1842
1843 #NOT USED FUNCTIONS
1844
1845 def new_external_port(self, port_data):
1846 #TODO openstack if needed
1847 '''Adds a external port to VIM'''
1848 '''Returns the port identifier'''
1849 return -vimconn.HTTP_Internal_Server_Error, "osconnector.new_external_port() not implemented"
1850
1851 def connect_port_network(self, port_id, network_id, admin=False):
1852 #TODO openstack if needed
1853 '''Connects a external port to a network'''
1854 '''Returns status code of the VIM response'''
1855 return -vimconn.HTTP_Internal_Server_Error, "osconnector.connect_port_network() not implemented"
1856
1857 def new_user(self, user_name, user_passwd, tenant_id=None):
1858 '''Adds a new user to openstack VIM'''
1859 '''Returns the user identifier'''
1860 self.logger.debug("osconnector: Adding a new user to VIM")
1861 try:
1862 self._reload_connection()
1863 user=self.keystone.users.create(user_name, password=user_passwd, default_project=tenant_id)
1864 #self.keystone.tenants.add_user(self.k_creds["username"], #role)
1865 return user.id
1866 except ksExceptions.ConnectionError as e:
1867 error_value=-vimconn.HTTP_Bad_Request
1868 error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0]))
1869 except ksExceptions.ClientException as e: #TODO remove
1870 error_value=-vimconn.HTTP_Bad_Request
1871 error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0]))
1872 #TODO insert exception vimconn.HTTP_Unauthorized
1873 #if reaching here is because an exception
1874 self.logger.debug("new_user " + error_text)
1875 return error_value, error_text
1876
1877 def delete_user(self, user_id):
1878 '''Delete a user from openstack VIM'''
1879 '''Returns the user identifier'''
1880 if self.debug:
1881 print("osconnector: Deleting a user from VIM")
1882 try:
1883 self._reload_connection()
1884 self.keystone.users.delete(user_id)
1885 return 1, user_id
1886 except ksExceptions.ConnectionError as e:
1887 error_value=-vimconn.HTTP_Bad_Request
1888 error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0]))
1889 except ksExceptions.NotFound as e:
1890 error_value=-vimconn.HTTP_Not_Found
1891 error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0]))
1892 except ksExceptions.ClientException as e: #TODO remove
1893 error_value=-vimconn.HTTP_Bad_Request
1894 error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0]))
1895 #TODO insert exception vimconn.HTTP_Unauthorized
1896 #if reaching here is because an exception
1897 self.logger.debug("delete_tenant " + error_text)
1898 return error_value, error_text
1899
1900 def get_hosts_info(self):
1901 '''Get the information of deployed hosts
1902 Returns the hosts content'''
1903 if self.debug:
1904 print("osconnector: Getting Host info from VIM")
1905 try:
1906 h_list=[]
1907 self._reload_connection()
1908 hypervisors = self.nova.hypervisors.list()
1909 for hype in hypervisors:
1910 h_list.append( hype.to_dict() )
1911 return 1, {"hosts":h_list}
1912 except nvExceptions.NotFound as e:
1913 error_value=-vimconn.HTTP_Not_Found
1914 error_text= (str(e) if len(e.args)==0 else str(e.args[0]))
1915 except (ksExceptions.ClientException, nvExceptions.ClientException) as e:
1916 error_value=-vimconn.HTTP_Bad_Request
1917 error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0]))
1918 #TODO insert exception vimconn.HTTP_Unauthorized
1919 #if reaching here is because an exception
1920 self.logger.debug("get_hosts_info " + error_text)
1921 return error_value, error_text
1922
1923 def get_hosts(self, vim_tenant):
1924 '''Get the hosts and deployed instances
1925 Returns the hosts content'''
1926 r, hype_dict = self.get_hosts_info()
1927 if r<0:
1928 return r, hype_dict
1929 hypervisors = hype_dict["hosts"]
1930 try:
1931 servers = self.nova.servers.list()
1932 for hype in hypervisors:
1933 for server in servers:
1934 if server.to_dict()['OS-EXT-SRV-ATTR:hypervisor_hostname']==hype['hypervisor_hostname']:
1935 if 'vm' in hype:
1936 hype['vm'].append(server.id)
1937 else:
1938 hype['vm'] = [server.id]
1939 return 1, hype_dict
1940 except nvExceptions.NotFound as e:
1941 error_value=-vimconn.HTTP_Not_Found
1942 error_text= (str(e) if len(e.args)==0 else str(e.args[0]))
1943 except (ksExceptions.ClientException, nvExceptions.ClientException) as e:
1944 error_value=-vimconn.HTTP_Bad_Request
1945 error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0]))
1946 #TODO insert exception vimconn.HTTP_Unauthorized
1947 #if reaching here is because an exception
1948 self.logger.debug("get_hosts " + error_text)
1949 return error_value, error_text
1950
1951 def new_classification(self, name, ctype, definition):
1952 self.logger.debug(
1953 'Adding a new (Traffic) Classification to VIM, named %s', name)
1954 try:
1955 new_class = None
1956 self._reload_connection()
1957 if ctype not in supportedClassificationTypes:
1958 raise vimconn.vimconnNotSupportedException(
1959 'OpenStack VIM connector doesn\'t support provided '
1960 'Classification Type {}, supported ones are: '
1961 '{}'.format(ctype, supportedClassificationTypes))
1962 if not self._validate_classification(ctype, definition):
1963 raise vimconn.vimconnException(
1964 'Incorrect Classification definition '
1965 'for the type specified.')
1966 classification_dict = definition
1967 classification_dict['name'] = name
1968
1969 new_class = self.neutron.create_sfc_flow_classifier(
1970 {'flow_classifier': classification_dict})
1971 return new_class['flow_classifier']['id']
1972 except (neExceptions.ConnectionFailed, ksExceptions.ClientException,
1973 neExceptions.NeutronException, ConnectionError) as e:
1974 self.logger.error(
1975 'Creation of Classification failed.')
1976 self._format_exception(e)
1977
1978 def get_classification(self, class_id):
1979 self.logger.debug(" Getting Classification %s from VIM", class_id)
1980 filter_dict = {"id": class_id}
1981 class_list = self.get_classification_list(filter_dict)
1982 if len(class_list) == 0:
1983 raise vimconn.vimconnNotFoundException(
1984 "Classification '{}' not found".format(class_id))
1985 elif len(class_list) > 1:
1986 raise vimconn.vimconnConflictException(
1987 "Found more than one Classification with this criteria")
1988 classification = class_list[0]
1989 return classification
1990
1991 def get_classification_list(self, filter_dict={}):
1992 self.logger.debug("Getting Classifications from VIM filter: '%s'",
1993 str(filter_dict))
1994 try:
1995 filter_dict_os = filter_dict.copy()
1996 self._reload_connection()
1997 if self.api_version3 and "tenant_id" in filter_dict_os:
1998 filter_dict_os['project_id'] = filter_dict_os.pop('tenant_id')
1999 classification_dict = self.neutron.list_sfc_flow_classifiers(
2000 **filter_dict_os)
2001 classification_list = classification_dict["flow_classifiers"]
2002 self.__classification_os2mano(classification_list)
2003 return classification_list
2004 except (neExceptions.ConnectionFailed, ksExceptions.ClientException,
2005 neExceptions.NeutronException, ConnectionError) as e:
2006 self._format_exception(e)
2007
2008 def delete_classification(self, class_id):
2009 self.logger.debug("Deleting Classification '%s' from VIM", class_id)
2010 try:
2011 self._reload_connection()
2012 self.neutron.delete_sfc_flow_classifier(class_id)
2013 return class_id
2014 except (neExceptions.ConnectionFailed, neExceptions.NeutronException,
2015 ksExceptions.ClientException, neExceptions.NeutronException,
2016 ConnectionError) as e:
2017 self._format_exception(e)
2018
2019 def new_sfi(self, name, ingress_ports, egress_ports, sfc_encap=True):
2020 self.logger.debug(
2021 "Adding a new Service Function Instance to VIM, named '%s'", name)
2022 try:
2023 new_sfi = None
2024 self._reload_connection()
2025 correlation = None
2026 if sfc_encap:
2027 correlation = 'nsh'
2028 if len(ingress_ports) != 1:
2029 raise vimconn.vimconnNotSupportedException(
2030 "OpenStack VIM connector can only have "
2031 "1 ingress port per SFI")
2032 if len(egress_ports) != 1:
2033 raise vimconn.vimconnNotSupportedException(
2034 "OpenStack VIM connector can only have "
2035 "1 egress port per SFI")
2036 sfi_dict = {'name': name,
2037 'ingress': ingress_ports[0],
2038 'egress': egress_ports[0],
2039 'service_function_parameters': {
2040 'correlation': correlation}}
2041 new_sfi = self.neutron.create_sfc_port_pair({'port_pair': sfi_dict})
2042 return new_sfi['port_pair']['id']
2043 except (neExceptions.ConnectionFailed, ksExceptions.ClientException,
2044 neExceptions.NeutronException, ConnectionError) as e:
2045 if new_sfi:
2046 try:
2047 self.neutron.delete_sfc_port_pair(
2048 new_sfi['port_pair']['id'])
2049 except Exception:
2050 self.logger.error(
2051 'Creation of Service Function Instance failed, with '
2052 'subsequent deletion failure as well.')
2053 self._format_exception(e)
2054
2055 def get_sfi(self, sfi_id):
2056 self.logger.debug(
2057 'Getting Service Function Instance %s from VIM', sfi_id)
2058 filter_dict = {"id": sfi_id}
2059 sfi_list = self.get_sfi_list(filter_dict)
2060 if len(sfi_list) == 0:
2061 raise vimconn.vimconnNotFoundException(
2062 "Service Function Instance '{}' not found".format(sfi_id))
2063 elif len(sfi_list) > 1:
2064 raise vimconn.vimconnConflictException(
2065 'Found more than one Service Function Instance '
2066 'with this criteria')
2067 sfi = sfi_list[0]
2068 return sfi
2069
2070 def get_sfi_list(self, filter_dict={}):
2071 self.logger.debug("Getting Service Function Instances from "
2072 "VIM filter: '%s'", str(filter_dict))
2073 try:
2074 self._reload_connection()
2075 filter_dict_os = filter_dict.copy()
2076 if self.api_version3 and "tenant_id" in filter_dict_os:
2077 filter_dict_os['project_id'] = filter_dict_os.pop('tenant_id')
2078 sfi_dict = self.neutron.list_sfc_port_pairs(**filter_dict_os)
2079 sfi_list = sfi_dict["port_pairs"]
2080 self.__sfi_os2mano(sfi_list)
2081 return sfi_list
2082 except (neExceptions.ConnectionFailed, ksExceptions.ClientException,
2083 neExceptions.NeutronException, ConnectionError) as e:
2084 self._format_exception(e)
2085
2086 def delete_sfi(self, sfi_id):
2087 self.logger.debug("Deleting Service Function Instance '%s' "
2088 "from VIM", sfi_id)
2089 try:
2090 self._reload_connection()
2091 self.neutron.delete_sfc_port_pair(sfi_id)
2092 return sfi_id
2093 except (neExceptions.ConnectionFailed, neExceptions.NeutronException,
2094 ksExceptions.ClientException, neExceptions.NeutronException,
2095 ConnectionError) as e:
2096 self._format_exception(e)
2097
2098 def new_sf(self, name, sfis, sfc_encap=True):
2099 self.logger.debug("Adding a new Service Function to VIM, "
2100 "named '%s'", name)
2101 try:
2102 new_sf = None
2103 self._reload_connection()
2104 # correlation = None
2105 # if sfc_encap:
2106 # correlation = 'nsh'
2107 for instance in sfis:
2108 sfi = self.get_sfi(instance)
2109 if sfi.get('sfc_encap') != sfc_encap:
2110 raise vimconn.vimconnNotSupportedException(
2111 "OpenStack VIM connector requires all SFIs of the "
2112 "same SF to share the same SFC Encapsulation")
2113 sf_dict = {'name': name,
2114 'port_pairs': sfis}
2115 new_sf = self.neutron.create_sfc_port_pair_group({
2116 'port_pair_group': sf_dict})
2117 return new_sf['port_pair_group']['id']
2118 except (neExceptions.ConnectionFailed, ksExceptions.ClientException,
2119 neExceptions.NeutronException, ConnectionError) as e:
2120 if new_sf:
2121 try:
2122 self.neutron.delete_sfc_port_pair_group(
2123 new_sf['port_pair_group']['id'])
2124 except Exception:
2125 self.logger.error(
2126 'Creation of Service Function failed, with '
2127 'subsequent deletion failure as well.')
2128 self._format_exception(e)
2129
2130 def get_sf(self, sf_id):
2131 self.logger.debug("Getting Service Function %s from VIM", sf_id)
2132 filter_dict = {"id": sf_id}
2133 sf_list = self.get_sf_list(filter_dict)
2134 if len(sf_list) == 0:
2135 raise vimconn.vimconnNotFoundException(
2136 "Service Function '{}' not found".format(sf_id))
2137 elif len(sf_list) > 1:
2138 raise vimconn.vimconnConflictException(
2139 "Found more than one Service Function with this criteria")
2140 sf = sf_list[0]
2141 return sf
2142
2143 def get_sf_list(self, filter_dict={}):
2144 self.logger.debug("Getting Service Function from VIM filter: '%s'",
2145 str(filter_dict))
2146 try:
2147 self._reload_connection()
2148 filter_dict_os = filter_dict.copy()
2149 if self.api_version3 and "tenant_id" in filter_dict_os:
2150 filter_dict_os['project_id'] = filter_dict_os.pop('tenant_id')
2151 sf_dict = self.neutron.list_sfc_port_pair_groups(**filter_dict_os)
2152 sf_list = sf_dict["port_pair_groups"]
2153 self.__sf_os2mano(sf_list)
2154 return sf_list
2155 except (neExceptions.ConnectionFailed, ksExceptions.ClientException,
2156 neExceptions.NeutronException, ConnectionError) as e:
2157 self._format_exception(e)
2158
2159 def delete_sf(self, sf_id):
2160 self.logger.debug("Deleting Service Function '%s' from VIM", sf_id)
2161 try:
2162 self._reload_connection()
2163 self.neutron.delete_sfc_port_pair_group(sf_id)
2164 return sf_id
2165 except (neExceptions.ConnectionFailed, neExceptions.NeutronException,
2166 ksExceptions.ClientException, neExceptions.NeutronException,
2167 ConnectionError) as e:
2168 self._format_exception(e)
2169
2170 def new_sfp(self, name, classifications, sfs, sfc_encap=True, spi=None):
2171 self.logger.debug("Adding a new Service Function Path to VIM, "
2172 "named '%s'", name)
2173 try:
2174 new_sfp = None
2175 self._reload_connection()
2176 # In networking-sfc the MPLS encapsulation is legacy
2177 # should be used when no full SFC Encapsulation is intended
2178 correlation = 'mpls'
2179 if sfc_encap:
2180 correlation = 'nsh'
2181 sfp_dict = {'name': name,
2182 'flow_classifiers': classifications,
2183 'port_pair_groups': sfs,
2184 'chain_parameters': {'correlation': correlation}}
2185 if spi:
2186 sfp_dict['chain_id'] = spi
2187 new_sfp = self.neutron.create_sfc_port_chain({'port_chain': sfp_dict})
2188 return new_sfp["port_chain"]["id"]
2189 except (neExceptions.ConnectionFailed, ksExceptions.ClientException,
2190 neExceptions.NeutronException, ConnectionError) as e:
2191 if new_sfp:
2192 try:
2193 self.neutron.delete_sfc_port_chain(new_sfp['port_chain']['id'])
2194 except Exception:
2195 self.logger.error(
2196 'Creation of Service Function Path failed, with '
2197 'subsequent deletion failure as well.')
2198 self._format_exception(e)
2199
2200 def get_sfp(self, sfp_id):
2201 self.logger.debug(" Getting Service Function Path %s from VIM", sfp_id)
2202 filter_dict = {"id": sfp_id}
2203 sfp_list = self.get_sfp_list(filter_dict)
2204 if len(sfp_list) == 0:
2205 raise vimconn.vimconnNotFoundException(
2206 "Service Function Path '{}' not found".format(sfp_id))
2207 elif len(sfp_list) > 1:
2208 raise vimconn.vimconnConflictException(
2209 "Found more than one Service Function Path with this criteria")
2210 sfp = sfp_list[0]
2211 return sfp
2212
2213 def get_sfp_list(self, filter_dict={}):
2214 self.logger.debug("Getting Service Function Paths from VIM filter: "
2215 "'%s'", str(filter_dict))
2216 try:
2217 self._reload_connection()
2218 filter_dict_os = filter_dict.copy()
2219 if self.api_version3 and "tenant_id" in filter_dict_os:
2220 filter_dict_os['project_id'] = filter_dict_os.pop('tenant_id')
2221 sfp_dict = self.neutron.list_sfc_port_chains(**filter_dict_os)
2222 sfp_list = sfp_dict["port_chains"]
2223 self.__sfp_os2mano(sfp_list)
2224 return sfp_list
2225 except (neExceptions.ConnectionFailed, ksExceptions.ClientException,
2226 neExceptions.NeutronException, ConnectionError) as e:
2227 self._format_exception(e)
2228
2229 def delete_sfp(self, sfp_id):
2230 self.logger.debug(
2231 "Deleting Service Function Path '%s' from VIM", sfp_id)
2232 try:
2233 self._reload_connection()
2234 self.neutron.delete_sfc_port_chain(sfp_id)
2235 return sfp_id
2236 except (neExceptions.ConnectionFailed, neExceptions.NeutronException,
2237 ksExceptions.ClientException, neExceptions.NeutronException,
2238 ConnectionError) as e:
2239 self._format_exception(e)