1 # -*- coding: utf-8 -*-
4 # Copyright 2015 Telefonica Investigacion y Desarrollo, S.A.U.
5 # This file is part of openmano
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
12 # http://www.apache.org/licenses/LICENSE-2.0
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
20 # For those usages not covered by the Apache License, Version 2.0 please
21 # contact with: nfvlabs@tid.es
25 osconnector implements all the methods to interact with openstack using the python-neutronclient.
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)
35 __author__
= "Alfonso Tierno, Gerardo Garcia, Pablo Montes, xFlow Research, Igor D.C., Eduardo Sousa"
36 __date__
= "$22-sep-2017 23:59:59$"
47 from pprint
import pformat
48 from types
import StringTypes
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
65 """contain the openstack virtual machine status to openmano status"""
66 vmStatus2manoFormat
={'ACTIVE':'ACTIVE',
68 'SUSPENDED': 'SUSPENDED',
71 'ERROR':'ERROR','DELETED':'DELETED'
73 netStatus2manoFormat
={'ACTIVE':'ACTIVE','PAUSED':'PAUSED','INACTIVE':'INACTIVE','BUILD':'BUILD','ERROR':'ERROR','DELETED':'DELETED'
76 supportedClassificationTypes
= ['legacy_flow_classifier']
78 #global var to have a timeout creating and deleting volumes
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())
91 return super(SafeDumper
, self
).represent_data(data
)
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
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
))
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'))
114 vimconn
.vimconnector
.__init
__(self
, uuid
, name
, tenant_id
, tenant_name
, url
, url_admin
, user
, passwd
, log_level
,
117 if self
.config
.get("insecure") and self
.config
.get("ca_cert"):
118 raise vimconn
.vimconnException("options insecure and ca_cert are mutually exclusive")
120 if self
.config
.get("insecure"):
122 if self
.config
.get("ca_cert"):
123 self
.verify
= self
.config
.get("ca_cert")
126 raise TypeError('url param can not be NoneType')
127 self
.persistent_info
= persistent_info
128 self
.availability_zone
= persistent_info
.get('availability_zone', None)
129 self
.session
= persistent_info
.get('session', {'reload_client': True})
130 self
.my_tenant_id
= self
.session
.get('my_tenant_id')
131 self
.nova
= self
.session
.get('nova')
132 self
.neutron
= self
.session
.get('neutron')
133 self
.cinder
= self
.session
.get('cinder')
134 self
.glance
= self
.session
.get('glance')
135 # self.glancev1 = self.session.get('glancev1')
136 self
.keystone
= self
.session
.get('keystone')
137 self
.api_version3
= self
.session
.get('api_version3')
138 self
.vim_type
= self
.config
.get("vim_type")
140 self
.vim_type
= self
.vim_type
.upper()
141 if self
.config
.get("use_internal_endpoint"):
142 self
.endpoint_type
= "internalURL"
144 self
.endpoint_type
= None
146 self
.logger
= logging
.getLogger('openmano.vim.openstack')
148 # allow security_groups to be a list or a single string
149 if isinstance(self
.config
.get('security_groups'), str):
150 self
.config
['security_groups'] = [self
.config
['security_groups']]
151 self
.security_groups_id
= None
153 ####### VIO Specific Changes #########
154 if self
.vim_type
== "VIO":
155 self
.logger
= logging
.getLogger('openmano.vim.vio')
158 self
.logger
.setLevel( getattr(logging
, log_level
))
160 def __getitem__(self
, index
):
161 """Get individuals parameters.
163 if index
== 'project_domain_id':
164 return self
.config
.get("project_domain_id")
165 elif index
== 'user_domain_id':
166 return self
.config
.get("user_domain_id")
168 return vimconn
.vimconnector
.__getitem
__(self
, index
)
170 def __setitem__(self
, index
, value
):
171 """Set individuals parameters and it is marked as dirty so to force connection reload.
173 if index
== 'project_domain_id':
174 self
.config
["project_domain_id"] = value
175 elif index
== 'user_domain_id':
176 self
.config
["user_domain_id"] = value
178 vimconn
.vimconnector
.__setitem
__(self
, index
, value
)
179 self
.session
['reload_client'] = True
181 def serialize(self
, value
):
182 """Serialization of python basic types.
184 In the case value is not serializable a message will be logged and a
185 simple representation of the data that cannot be converted back to
188 if isinstance(value
, StringTypes
):
192 return yaml
.dump(value
, Dumper
=SafeDumper
,
193 default_flow_style
=True, width
=256)
194 except yaml
.representer
.RepresenterError
:
196 'The following entity cannot be serialized in YAML:'
197 '\n\n%s\n\n', pformat(value
), exc_info
=True)
200 def _reload_connection(self
):
201 '''Called before any operation, it check if credentials has changed
202 Throw keystoneclient.apiclient.exceptions.AuthorizationFailure
204 #TODO control the timing and possible token timeout, but it seams that python client does this task for us :-)
205 if self
.session
['reload_client']:
206 if self
.config
.get('APIversion'):
207 self
.api_version3
= self
.config
['APIversion'] == 'v3.3' or self
.config
['APIversion'] == '3'
208 else: # get from ending auth_url that end with v3 or with v2.0
209 self
.api_version3
= self
.url
.endswith("/v3") or self
.url
.endswith("/v3/")
210 self
.session
['api_version3'] = self
.api_version3
211 if self
.api_version3
:
212 if self
.config
.get('project_domain_id') or self
.config
.get('project_domain_name'):
213 project_domain_id_default
= None
215 project_domain_id_default
= 'default'
216 if self
.config
.get('user_domain_id') or self
.config
.get('user_domain_name'):
217 user_domain_id_default
= None
219 user_domain_id_default
= 'default'
220 auth
= v3
.Password(auth_url
=self
.url
,
222 password
=self
.passwd
,
223 project_name
=self
.tenant_name
,
224 project_id
=self
.tenant_id
,
225 project_domain_id
=self
.config
.get('project_domain_id', project_domain_id_default
),
226 user_domain_id
=self
.config
.get('user_domain_id', user_domain_id_default
),
227 project_domain_name
=self
.config
.get('project_domain_name'),
228 user_domain_name
=self
.config
.get('user_domain_name'))
230 auth
= v2
.Password(auth_url
=self
.url
,
232 password
=self
.passwd
,
233 tenant_name
=self
.tenant_name
,
234 tenant_id
=self
.tenant_id
)
235 sess
= session
.Session(auth
=auth
, verify
=self
.verify
)
236 # addedd region_name to keystone, nova, neutron and cinder to support distributed cloud for Wind River Titanium cloud and StarlingX
237 region_name
= self
.config
.get('region_name')
238 if self
.api_version3
:
239 self
.keystone
= ksClient_v3
.Client(session
=sess
, endpoint_type
=self
.endpoint_type
, region_name
=region_name
)
241 self
.keystone
= ksClient_v2
.Client(session
=sess
, endpoint_type
=self
.endpoint_type
)
242 self
.session
['keystone'] = self
.keystone
243 # In order to enable microversion functionality an explicit microversion must be specified in 'config'.
244 # This implementation approach is due to the warning message in
245 # https://developer.openstack.org/api-guide/compute/microversions.html
246 # where it is stated that microversion backwards compatibility is not guaranteed and clients should
247 # always require an specific microversion.
248 # To be able to use 'device role tagging' functionality define 'microversion: 2.32' in datacenter config
249 version
= self
.config
.get("microversion")
252 # addedd region_name to keystone, nova, neutron and cinder to support distributed cloud for Wind River Titanium cloud and StarlingX
253 self
.nova
= self
.session
['nova'] = nClient
.Client(str(version
), session
=sess
, endpoint_type
=self
.endpoint_type
, region_name
=region_name
)
254 self
.neutron
= self
.session
['neutron'] = neClient
.Client('2.0', session
=sess
, endpoint_type
=self
.endpoint_type
, region_name
=region_name
)
255 self
.cinder
= self
.session
['cinder'] = cClient
.Client(2, session
=sess
, endpoint_type
=self
.endpoint_type
, region_name
=region_name
)
257 self
.my_tenant_id
= self
.session
['my_tenant_id'] = sess
.get_project_id()
258 except Exception as e
:
259 self
.logger
.error("Cannot get project_id from session", exc_info
=True)
260 if self
.endpoint_type
== "internalURL":
261 glance_service_id
= self
.keystone
.services
.list(name
="glance")[0].id
262 glance_endpoint
= self
.keystone
.endpoints
.list(glance_service_id
, interface
="internal")[0].url
264 glance_endpoint
= None
265 self
.glance
= self
.session
['glance'] = glClient
.Client(2, session
=sess
, endpoint
=glance_endpoint
)
266 # using version 1 of glance client in new_image()
267 # self.glancev1 = self.session['glancev1'] = glClient.Client('1', session=sess,
268 # endpoint=glance_endpoint)
269 self
.session
['reload_client'] = False
270 self
.persistent_info
['session'] = self
.session
271 # add availablity zone info inside self.persistent_info
272 self
._set
_availablity
_zones
()
273 self
.persistent_info
['availability_zone'] = self
.availability_zone
274 self
.security_groups_id
= None # force to get again security_groups_ids next time they are needed
276 def __net_os2mano(self
, net_list_dict
):
277 '''Transform the net openstack format to mano format
278 net_list_dict can be a list of dict or a single dict'''
279 if type(net_list_dict
) is dict:
280 net_list_
=(net_list_dict
,)
281 elif type(net_list_dict
) is list:
282 net_list_
=net_list_dict
284 raise TypeError("param net_list_dict must be a list or a dictionary")
285 for net
in net_list_
:
286 if net
.get('provider:network_type') == "vlan":
291 def __classification_os2mano(self
, class_list_dict
):
292 """Transform the openstack format (Flow Classifier) to mano format
293 (Classification) class_list_dict can be a list of dict or a single dict
295 if isinstance(class_list_dict
, dict):
296 class_list_
= [class_list_dict
]
297 elif isinstance(class_list_dict
, list):
298 class_list_
= class_list_dict
301 "param class_list_dict must be a list or a dictionary")
302 for classification
in class_list_
:
303 id = classification
.pop('id')
304 name
= classification
.pop('name')
305 description
= classification
.pop('description')
306 project_id
= classification
.pop('project_id')
307 tenant_id
= classification
.pop('tenant_id')
308 original_classification
= copy
.deepcopy(classification
)
309 classification
.clear()
310 classification
['ctype'] = 'legacy_flow_classifier'
311 classification
['definition'] = original_classification
312 classification
['id'] = id
313 classification
['name'] = name
314 classification
['description'] = description
315 classification
['project_id'] = project_id
316 classification
['tenant_id'] = tenant_id
318 def __sfi_os2mano(self
, sfi_list_dict
):
319 """Transform the openstack format (Port Pair) to mano format (SFI)
320 sfi_list_dict can be a list of dict or a single dict
322 if isinstance(sfi_list_dict
, dict):
323 sfi_list_
= [sfi_list_dict
]
324 elif isinstance(sfi_list_dict
, list):
325 sfi_list_
= sfi_list_dict
328 "param sfi_list_dict must be a list or a dictionary")
329 for sfi
in sfi_list_
:
330 sfi
['ingress_ports'] = []
331 sfi
['egress_ports'] = []
332 if sfi
.get('ingress'):
333 sfi
['ingress_ports'].append(sfi
['ingress'])
334 if sfi
.get('egress'):
335 sfi
['egress_ports'].append(sfi
['egress'])
338 params
= sfi
.get('service_function_parameters')
341 correlation
= params
.get('correlation')
344 sfi
['sfc_encap'] = sfc_encap
345 del sfi
['service_function_parameters']
347 def __sf_os2mano(self
, sf_list_dict
):
348 """Transform the openstack format (Port Pair Group) to mano format (SF)
349 sf_list_dict can be a list of dict or a single dict
351 if isinstance(sf_list_dict
, dict):
352 sf_list_
= [sf_list_dict
]
353 elif isinstance(sf_list_dict
, list):
354 sf_list_
= sf_list_dict
357 "param sf_list_dict must be a list or a dictionary")
359 del sf
['port_pair_group_parameters']
360 sf
['sfis'] = sf
['port_pairs']
363 def __sfp_os2mano(self
, sfp_list_dict
):
364 """Transform the openstack format (Port Chain) to mano format (SFP)
365 sfp_list_dict can be a list of dict or a single dict
367 if isinstance(sfp_list_dict
, dict):
368 sfp_list_
= [sfp_list_dict
]
369 elif isinstance(sfp_list_dict
, list):
370 sfp_list_
= sfp_list_dict
373 "param sfp_list_dict must be a list or a dictionary")
374 for sfp
in sfp_list_
:
375 params
= sfp
.pop('chain_parameters')
378 correlation
= params
.get('correlation')
381 sfp
['sfc_encap'] = sfc_encap
382 sfp
['spi'] = sfp
.pop('chain_id')
383 sfp
['classifications'] = sfp
.pop('flow_classifiers')
384 sfp
['service_functions'] = sfp
.pop('port_pair_groups')
386 # placeholder for now; read TODO note below
387 def _validate_classification(self
, type, definition
):
388 # only legacy_flow_classifier Type is supported at this point
390 # TODO(igordcard): this method should be an abstract method of an
391 # abstract Classification class to be implemented by the specific
392 # Types. Also, abstract vimconnector should call the validation
393 # method before the implemented VIM connectors are called.
395 def _format_exception(self
, exception
):
396 '''Transform a keystone, nova, neutron exception into a vimconn exception'''
397 if isinstance(exception
, (neExceptions
.NetworkNotFoundClient
, nvExceptions
.NotFound
, ksExceptions
.NotFound
, gl1Exceptions
.HTTPNotFound
)):
398 raise vimconn
.vimconnNotFoundException(type(exception
).__name
__ + ": " + str(exception
))
399 elif isinstance(exception
, (HTTPException
, gl1Exceptions
.HTTPException
, gl1Exceptions
.CommunicationError
,
400 ConnectionError
, ksExceptions
.ConnectionError
, neExceptions
.ConnectionFailed
)):
401 raise vimconn
.vimconnConnectionException(type(exception
).__name
__ + ": " + str(exception
))
402 elif isinstance(exception
, (KeyError, nvExceptions
.BadRequest
, ksExceptions
.BadRequest
)):
403 raise vimconn
.vimconnException(type(exception
).__name
__ + ": " + str(exception
))
404 elif isinstance(exception
, (nvExceptions
.ClientException
, ksExceptions
.ClientException
,
405 neExceptions
.NeutronException
)):
406 raise vimconn
.vimconnUnexpectedResponse(type(exception
).__name
__ + ": " + str(exception
))
407 elif isinstance(exception
, nvExceptions
.Conflict
):
408 raise vimconn
.vimconnConflictException(type(exception
).__name
__ + ": " + str(exception
))
409 elif isinstance(exception
, vimconn
.vimconnException
):
412 self
.logger
.error("General Exception " + str(exception
), exc_info
=True)
413 raise vimconn
.vimconnConnectionException(type(exception
).__name
__ + ": " + str(exception
))
415 def _get_ids_from_name(self
):
417 Obtain ids from name of tenant and security_groups. Store at self .security_groups_id"
420 # get tenant_id if only tenant_name is supplied
421 self
._reload
_connection
()
422 if not self
.my_tenant_id
:
423 raise vimconn
.vimconnConnectionException("Error getting tenant information from name={} id={}".
424 format(self
.tenant_name
, self
.tenant_id
))
425 if self
.config
.get('security_groups') and not self
.security_groups_id
:
426 # convert from name to id
427 neutron_sg_list
= self
.neutron
.list_security_groups(tenant_id
=self
.my_tenant_id
)["security_groups"]
429 self
.security_groups_id
= []
430 for sg
in self
.config
.get('security_groups'):
431 for neutron_sg
in neutron_sg_list
:
432 if sg
in (neutron_sg
["id"], neutron_sg
["name"]):
433 self
.security_groups_id
.append(neutron_sg
["id"])
436 self
.security_groups_id
= None
437 raise vimconn
.vimconnConnectionException("Not found security group {} for this tenant".format(sg
))
439 def get_tenant_list(self
, filter_dict
={}):
440 '''Obtain tenants of VIM
441 filter_dict can contain the following keys:
442 name: filter by tenant name
443 id: filter by tenant uuid/id
445 Returns the tenant list of dictionaries: [{'name':'<name>, 'id':'<id>, ...}, ...]
447 self
.logger
.debug("Getting tenants from VIM filter: '%s'", str(filter_dict
))
449 self
._reload
_connection
()
450 if self
.api_version3
:
451 project_class_list
= self
.keystone
.projects
.list(name
=filter_dict
.get("name"))
453 project_class_list
= self
.keystone
.tenants
.findall(**filter_dict
)
455 for project
in project_class_list
:
456 if filter_dict
.get('id') and filter_dict
["id"] != project
.id:
458 project_list
.append(project
.to_dict())
460 except (ksExceptions
.ConnectionError
, ksExceptions
.ClientException
, ConnectionError
) as e
:
461 self
._format
_exception
(e
)
463 def new_tenant(self
, tenant_name
, tenant_description
):
464 '''Adds a new tenant to openstack VIM. Returns the tenant identifier'''
465 self
.logger
.debug("Adding a new tenant name: %s", tenant_name
)
467 self
._reload
_connection
()
468 if self
.api_version3
:
469 project
= self
.keystone
.projects
.create(tenant_name
, self
.config
.get("project_domain_id", "default"),
470 description
=tenant_description
, is_domain
=False)
472 project
= self
.keystone
.tenants
.create(tenant_name
, tenant_description
)
474 except (ksExceptions
.ConnectionError
, ksExceptions
.ClientException
, ksExceptions
.BadRequest
, ConnectionError
) as e
:
475 self
._format
_exception
(e
)
477 def delete_tenant(self
, tenant_id
):
478 '''Delete a tenant from openstack VIM. Returns the old tenant identifier'''
479 self
.logger
.debug("Deleting tenant %s from VIM", tenant_id
)
481 self
._reload
_connection
()
482 if self
.api_version3
:
483 self
.keystone
.projects
.delete(tenant_id
)
485 self
.keystone
.tenants
.delete(tenant_id
)
487 except (ksExceptions
.ConnectionError
, ksExceptions
.ClientException
, ksExceptions
.NotFound
, ConnectionError
) as e
:
488 self
._format
_exception
(e
)
490 def new_network(self
,net_name
, net_type
, ip_profile
=None, shared
=False, vlan
=None):
491 '''Adds a tenant network to VIM. Returns the network identifier'''
492 self
.logger
.debug("Adding a new network to VIM name '%s', type '%s'", net_name
, net_type
)
493 #self.logger.debug(">>>>>>>>>>>>>>>>>> IP profile %s", str(ip_profile))
496 self
._reload
_connection
()
497 network_dict
= {'name': net_name
, 'admin_state_up': True}
498 if net_type
=="data" or net_type
=="ptp":
499 if self
.config
.get('dataplane_physical_net') == None:
500 raise vimconn
.vimconnConflictException("You must provide a 'dataplane_physical_net' at config value before creating sriov network")
501 network_dict
["provider:physical_network"] = self
.config
['dataplane_physical_net'] #"physnet_sriov" #TODO physical
502 network_dict
["provider:network_type"] = "vlan"
504 network_dict
["provider:network_type"] = vlan
506 ####### VIO Specific Changes #########
507 if self
.vim_type
== "VIO":
509 network_dict
["provider:segmentation_id"] = vlan
511 if self
.config
.get('dataplane_net_vlan_range') is None:
512 raise vimconn
.vimconnConflictException("You must provide "\
513 "'dataplane_net_vlan_range' in format [start_ID - end_ID]"\
514 "at config value before creating sriov network with vlan tag")
516 network_dict
["provider:segmentation_id"] = self
._genrate
_vlanID
()
518 network_dict
["shared"]=shared
519 new_net
=self
.neutron
.create_network({'network':network_dict
})
521 #create subnetwork, even if there is no profile
524 if not ip_profile
.get('subnet_address'):
525 #Fake subnet is required
526 subnet_rand
= random
.randint(0, 255)
527 ip_profile
['subnet_address'] = "192.168.{}.0/24".format(subnet_rand
)
528 if 'ip_version' not in ip_profile
:
529 ip_profile
['ip_version'] = "IPv4"
530 subnet
= {"name":net_name
+"-subnet",
531 "network_id": new_net
["network"]["id"],
532 "ip_version": 4 if ip_profile
['ip_version']=="IPv4" else 6,
533 "cidr": ip_profile
['subnet_address']
535 # Gateway should be set to None if not needed. Otherwise openstack assigns one by default
536 if ip_profile
.get('gateway_address'):
537 subnet
['gateway_ip'] = ip_profile
['gateway_address']
539 subnet
['gateway_ip'] = None
540 if ip_profile
.get('dns_address'):
541 subnet
['dns_nameservers'] = ip_profile
['dns_address'].split(";")
542 if 'dhcp_enabled' in ip_profile
:
543 subnet
['enable_dhcp'] = False if \
544 ip_profile
['dhcp_enabled']=="false" or ip_profile
['dhcp_enabled']==False else True
545 if ip_profile
.get('dhcp_start_address'):
546 subnet
['allocation_pools'] = []
547 subnet
['allocation_pools'].append(dict())
548 subnet
['allocation_pools'][0]['start'] = ip_profile
['dhcp_start_address']
549 if ip_profile
.get('dhcp_count'):
550 #parts = ip_profile['dhcp_start_address'].split('.')
551 #ip_int = (int(parts[0]) << 24) + (int(parts[1]) << 16) + (int(parts[2]) << 8) + int(parts[3])
552 ip_int
= int(netaddr
.IPAddress(ip_profile
['dhcp_start_address']))
553 ip_int
+= ip_profile
['dhcp_count'] - 1
554 ip_str
= str(netaddr
.IPAddress(ip_int
))
555 subnet
['allocation_pools'][0]['end'] = ip_str
556 #self.logger.debug(">>>>>>>>>>>>>>>>>> Subnet: %s", str(subnet))
557 self
.neutron
.create_subnet({"subnet": subnet
} )
558 return new_net
["network"]["id"]
559 except Exception as e
:
561 self
.neutron
.delete_network(new_net
['network']['id'])
562 self
._format
_exception
(e
)
564 def get_network_list(self
, filter_dict
={}):
565 '''Obtain tenant networks of VIM
571 admin_state_up: boolean
573 Returns the network list of dictionaries
575 self
.logger
.debug("Getting network from VIM filter: '%s'", str(filter_dict
))
577 self
._reload
_connection
()
578 filter_dict_os
= filter_dict
.copy()
579 if self
.api_version3
and "tenant_id" in filter_dict_os
:
580 filter_dict_os
['project_id'] = filter_dict_os
.pop('tenant_id') #T ODO check
581 net_dict
= self
.neutron
.list_networks(**filter_dict_os
)
582 net_list
= net_dict
["networks"]
583 self
.__net
_os
2mano
(net_list
)
585 except (neExceptions
.ConnectionFailed
, ksExceptions
.ClientException
, neExceptions
.NeutronException
, ConnectionError
) as e
:
586 self
._format
_exception
(e
)
588 def get_network(self
, net_id
):
589 '''Obtain details of network from VIM
590 Returns the network information from a network id'''
591 self
.logger
.debug(" Getting tenant network %s from VIM", net_id
)
592 filter_dict
={"id": net_id
}
593 net_list
= self
.get_network_list(filter_dict
)
595 raise vimconn
.vimconnNotFoundException("Network '{}' not found".format(net_id
))
596 elif len(net_list
)>1:
597 raise vimconn
.vimconnConflictException("Found more than one network with this criteria")
600 for subnet_id
in net
.get("subnets", () ):
602 subnet
= self
.neutron
.show_subnet(subnet_id
)
603 except Exception as e
:
604 self
.logger
.error("osconnector.get_network(): Error getting subnet %s %s" % (net_id
, str(e
)))
605 subnet
= {"id": subnet_id
, "fault": str(e
)}
606 subnets
.append(subnet
)
607 net
["subnets"] = subnets
608 net
["encapsulation"] = net
.get('provider:network_type')
609 net
["encapsulation_type"] = net
.get('provider:network_type')
610 net
["segmentation_id"] = net
.get('provider:segmentation_id')
611 net
["encapsulation_id"] = net
.get('provider:segmentation_id')
614 def delete_network(self
, net_id
):
615 '''Deletes a tenant network from VIM. Returns the old network identifier'''
616 self
.logger
.debug("Deleting network '%s' from VIM", net_id
)
618 self
._reload
_connection
()
619 #delete VM ports attached to this networks before the network
620 ports
= self
.neutron
.list_ports(network_id
=net_id
)
621 for p
in ports
['ports']:
623 self
.neutron
.delete_port(p
["id"])
624 except Exception as e
:
625 self
.logger
.error("Error deleting port %s: %s", p
["id"], str(e
))
626 self
.neutron
.delete_network(net_id
)
628 except (neExceptions
.ConnectionFailed
, neExceptions
.NetworkNotFoundClient
, neExceptions
.NeutronException
,
629 ksExceptions
.ClientException
, neExceptions
.NeutronException
, ConnectionError
) as e
:
630 self
._format
_exception
(e
)
632 def refresh_nets_status(self
, net_list
):
633 '''Get the status of the networks
634 Params: the list of network identifiers
635 Returns a dictionary with:
636 net_id: #VIM id of this network
637 status: #Mandatory. Text with one of:
638 # DELETED (not found at vim)
639 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
640 # OTHER (Vim reported other status not understood)
641 # ERROR (VIM indicates an ERROR status)
642 # ACTIVE, INACTIVE, DOWN (admin down),
643 # BUILD (on building process)
645 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
646 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
650 for net_id
in net_list
:
653 net_vim
= self
.get_network(net_id
)
654 if net_vim
['status'] in netStatus2manoFormat
:
655 net
["status"] = netStatus2manoFormat
[ net_vim
['status'] ]
657 net
["status"] = "OTHER"
658 net
["error_msg"] = "VIM status reported " + net_vim
['status']
660 if net
['status'] == "ACTIVE" and not net_vim
['admin_state_up']:
661 net
['status'] = 'DOWN'
663 net
['vim_info'] = self
.serialize(net_vim
)
665 if net_vim
.get('fault'): #TODO
666 net
['error_msg'] = str(net_vim
['fault'])
667 except vimconn
.vimconnNotFoundException
as e
:
668 self
.logger
.error("Exception getting net status: %s", str(e
))
669 net
['status'] = "DELETED"
670 net
['error_msg'] = str(e
)
671 except vimconn
.vimconnException
as e
:
672 self
.logger
.error("Exception getting net status: %s", str(e
))
673 net
['status'] = "VIM_ERROR"
674 net
['error_msg'] = str(e
)
675 net_dict
[net_id
] = net
678 def get_flavor(self
, flavor_id
):
679 '''Obtain flavor details from the VIM. Returns the flavor dict details'''
680 self
.logger
.debug("Getting flavor '%s'", flavor_id
)
682 self
._reload
_connection
()
683 flavor
= self
.nova
.flavors
.find(id=flavor_id
)
684 #TODO parse input and translate to VIM format (openmano_schemas.new_vminstance_response_schema)
685 return flavor
.to_dict()
686 except (nvExceptions
.NotFound
, nvExceptions
.ClientException
, ksExceptions
.ClientException
, ConnectionError
) as e
:
687 self
._format
_exception
(e
)
689 def get_flavor_id_from_data(self
, flavor_dict
):
690 """Obtain flavor id that match the flavor description
691 Returns the flavor_id or raises a vimconnNotFoundException
692 flavor_dict: contains the required ram, vcpus, disk
693 If 'use_existing_flavors' is set to True at config, the closer flavor that provides same or more ram, vcpus
694 and disk is returned. Otherwise a flavor with exactly same ram, vcpus and disk is returned or a
695 vimconnNotFoundException is raised
697 exact_match
= False if self
.config
.get('use_existing_flavors') else True
699 self
._reload
_connection
()
700 flavor_candidate_id
= None
701 flavor_candidate_data
= (10000, 10000, 10000)
702 flavor_target
= (flavor_dict
["ram"], flavor_dict
["vcpus"], flavor_dict
["disk"])
704 numas
= flavor_dict
.get("extended", {}).get("numas")
707 raise vimconn
.vimconnNotFoundException("Flavor with EPA still not implemted")
709 # raise vimconn.vimconnNotFoundException("Cannot find any flavor with more than one numa")
711 # numas = extended.get("numas")
712 for flavor
in self
.nova
.flavors
.list():
713 epa
= flavor
.get_keys()
717 flavor_data
= (flavor
.ram
, flavor
.vcpus
, flavor
.disk
)
718 if flavor_data
== flavor_target
:
720 elif not exact_match
and flavor_target
< flavor_data
< flavor_candidate_data
:
721 flavor_candidate_id
= flavor
.id
722 flavor_candidate_data
= flavor_data
723 if not exact_match
and flavor_candidate_id
:
724 return flavor_candidate_id
725 raise vimconn
.vimconnNotFoundException("Cannot find any flavor matching '{}'".format(str(flavor_dict
)))
726 except (nvExceptions
.NotFound
, nvExceptions
.ClientException
, ksExceptions
.ClientException
, ConnectionError
) as e
:
727 self
._format
_exception
(e
)
729 def new_flavor(self
, flavor_data
, change_name_if_used
=True):
730 '''Adds a tenant flavor to openstack VIM
731 if change_name_if_used is True, it will change name in case of conflict, because it is not supported name repetition
732 Returns the flavor identifier
734 self
.logger
.debug("Adding flavor '%s'", str(flavor_data
))
739 name
=flavor_data
['name']
740 while retry
<max_retries
:
743 self
._reload
_connection
()
744 if change_name_if_used
:
747 fl
=self
.nova
.flavors
.list()
749 fl_names
.append(f
.name
)
750 while name
in fl_names
:
752 name
= flavor_data
['name']+"-" + str(name_suffix
)
754 ram
= flavor_data
.get('ram',64)
755 vcpus
= flavor_data
.get('vcpus',1)
758 extended
= flavor_data
.get("extended")
760 numas
=extended
.get("numas")
762 numa_nodes
= len(numas
)
764 return -1, "Can not add flavor with more than one numa"
765 numa_properties
= {"hw:numa_nodes":str(numa_nodes
)}
766 numa_properties
["hw:mem_page_size"] = "large"
767 numa_properties
["hw:cpu_policy"] = "dedicated"
768 numa_properties
["hw:numa_mempolicy"] = "strict"
769 if self
.vim_type
== "VIO":
770 numa_properties
["vmware:extra_config"] = '{"numa.nodeAffinity":"0"}'
771 numa_properties
["vmware:latency_sensitivity_level"] = "high"
773 #overwrite ram and vcpus
774 #check if key 'memory' is present in numa else use ram value at flavor
776 ram
= numa
['memory']*1024
777 #See for reference: https://specs.openstack.org/openstack/nova-specs/specs/mitaka/implemented/virt-driver-cpu-thread-pinning.html
778 if 'paired-threads' in numa
:
779 vcpus
= numa
['paired-threads']*2
780 #cpu_thread_policy "require" implies that the compute node must have an STM architecture
781 numa_properties
["hw:cpu_thread_policy"] = "require"
782 numa_properties
["hw:cpu_policy"] = "dedicated"
783 elif 'cores' in numa
:
784 vcpus
= numa
['cores']
785 # cpu_thread_policy "prefer" implies that the host must not have an SMT architecture, or a non-SMT architecture will be emulated
786 numa_properties
["hw:cpu_thread_policy"] = "isolate"
787 numa_properties
["hw:cpu_policy"] = "dedicated"
788 elif 'threads' in numa
:
789 vcpus
= numa
['threads']
790 # cpu_thread_policy "prefer" implies that the host may or may not have an SMT architecture
791 numa_properties
["hw:cpu_thread_policy"] = "prefer"
792 numa_properties
["hw:cpu_policy"] = "dedicated"
793 # for interface in numa.get("interfaces",() ):
794 # if interface["dedicated"]=="yes":
795 # raise vimconn.vimconnException("Passthrough interfaces are not supported for the openstack connector", http_code=vimconn.HTTP_Service_Unavailable)
796 # #TODO, add the key 'pci_passthrough:alias"="<label at config>:<number ifaces>"' when a way to connect it is available
799 new_flavor
=self
.nova
.flavors
.create(name
,
802 flavor_data
.get('disk',0),
803 is_public
=flavor_data
.get('is_public', True)
807 new_flavor
.set_keys(numa_properties
)
809 except nvExceptions
.Conflict
as e
:
810 if change_name_if_used
and retry
< max_retries
:
812 self
._format
_exception
(e
)
813 #except nvExceptions.BadRequest as e:
814 except (ksExceptions
.ClientException
, nvExceptions
.ClientException
, ConnectionError
, KeyError) as e
:
815 self
._format
_exception
(e
)
817 def delete_flavor(self
,flavor_id
):
818 '''Deletes a tenant flavor from openstack VIM. Returns the old flavor_id
821 self
._reload
_connection
()
822 self
.nova
.flavors
.delete(flavor_id
)
824 #except nvExceptions.BadRequest as e:
825 except (nvExceptions
.NotFound
, ksExceptions
.ClientException
, nvExceptions
.ClientException
, ConnectionError
) as e
:
826 self
._format
_exception
(e
)
828 def new_image(self
,image_dict
):
830 Adds a tenant image to VIM. imge_dict is a dictionary with:
832 disk_format: qcow2, vhd, vmdk, raw (by default), ...
833 location: path or URI
834 public: "yes" or "no"
835 metadata: metadata of the image
840 while retry
<max_retries
:
843 self
._reload
_connection
()
844 #determine format http://docs.openstack.org/developer/glance/formats.html
845 if "disk_format" in image_dict
:
846 disk_format
=image_dict
["disk_format"]
847 else: #autodiscover based on extension
848 if image_dict
['location'].endswith(".qcow2"):
850 elif image_dict
['location'].endswith(".vhd"):
852 elif image_dict
['location'].endswith(".vmdk"):
854 elif image_dict
['location'].endswith(".vdi"):
856 elif image_dict
['location'].endswith(".iso"):
858 elif image_dict
['location'].endswith(".aki"):
860 elif image_dict
['location'].endswith(".ari"):
862 elif image_dict
['location'].endswith(".ami"):
866 self
.logger
.debug("new_image: '%s' loading from '%s'", image_dict
['name'], image_dict
['location'])
867 if self
.vim_type
== "VIO":
868 container_format
= "bare"
869 if 'container_format' in image_dict
:
870 container_format
= image_dict
['container_format']
871 new_image
= self
.glance
.images
.create(name
=image_dict
['name'], container_format
=container_format
,
872 disk_format
=disk_format
)
874 new_image
= self
.glance
.images
.create(name
=image_dict
['name'])
875 if image_dict
['location'].startswith("http"):
876 # TODO there is not a method to direct download. It must be downloaded locally with requests
877 raise vimconn
.vimconnNotImplemented("Cannot create image from URL")
879 with
open(image_dict
['location']) as fimage
:
880 self
.glance
.images
.upload(new_image
.id, fimage
)
881 #new_image = self.glancev1.images.create(name=image_dict['name'], is_public=image_dict.get('public',"yes")=="yes",
882 # container_format="bare", data=fimage, disk_format=disk_format)
883 metadata_to_load
= image_dict
.get('metadata')
884 # TODO location is a reserved word for current openstack versions. fixed for VIO please check for openstack
885 if self
.vim_type
== "VIO":
886 metadata_to_load
['upload_location'] = image_dict
['location']
888 metadata_to_load
['location'] = image_dict
['location']
889 self
.glance
.images
.update(new_image
.id, **metadata_to_load
)
891 except (nvExceptions
.Conflict
, ksExceptions
.ClientException
, nvExceptions
.ClientException
) as e
:
892 self
._format
_exception
(e
)
893 except (HTTPException
, gl1Exceptions
.HTTPException
, gl1Exceptions
.CommunicationError
, ConnectionError
) as e
:
894 if retry
==max_retries
:
896 self
._format
_exception
(e
)
897 except IOError as e
: #can not open the file
898 raise vimconn
.vimconnConnectionException(type(e
).__name
__ + ": " + str(e
)+ " for " + image_dict
['location'],
899 http_code
=vimconn
.HTTP_Bad_Request
)
901 def delete_image(self
, image_id
):
902 '''Deletes a tenant image from openstack VIM. Returns the old id
905 self
._reload
_connection
()
906 self
.glance
.images
.delete(image_id
)
908 except (nvExceptions
.NotFound
, ksExceptions
.ClientException
, nvExceptions
.ClientException
, gl1Exceptions
.CommunicationError
, gl1Exceptions
.HTTPNotFound
, ConnectionError
) as e
: #TODO remove
909 self
._format
_exception
(e
)
911 def get_image_id_from_path(self
, path
):
912 '''Get the image id from image path in the VIM database. Returns the image_id'''
914 self
._reload
_connection
()
915 images
= self
.glance
.images
.list()
917 if image
.metadata
.get("location")==path
:
919 raise vimconn
.vimconnNotFoundException("image with location '{}' not found".format( path
))
920 except (ksExceptions
.ClientException
, nvExceptions
.ClientException
, gl1Exceptions
.CommunicationError
, ConnectionError
) as e
:
921 self
._format
_exception
(e
)
923 def get_image_list(self
, filter_dict
={}):
924 '''Obtain tenant images from VIM
928 checksum: image checksum
929 Returns the image list of dictionaries:
930 [{<the fields at Filter_dict plus some VIM specific>}, ...]
933 self
.logger
.debug("Getting image list from VIM filter: '%s'", str(filter_dict
))
935 self
._reload
_connection
()
936 filter_dict_os
= filter_dict
.copy()
937 #First we filter by the available filter fields: name, id. The others are removed.
938 image_list
= self
.glance
.images
.list()
940 for image
in image_list
:
942 if filter_dict
.get("name") and image
["name"] != filter_dict
["name"]:
944 if filter_dict
.get("id") and image
["id"] != filter_dict
["id"]:
946 if filter_dict
.get("checksum") and image
["checksum"] != filter_dict
["checksum"]:
949 filtered_list
.append(image
.copy())
950 except gl1Exceptions
.HTTPNotFound
:
953 except (ksExceptions
.ClientException
, nvExceptions
.ClientException
, gl1Exceptions
.CommunicationError
, ConnectionError
) as e
:
954 self
._format
_exception
(e
)
956 def __wait_for_vm(self
, vm_id
, status
):
957 """wait until vm is in the desired status and return True.
958 If the VM gets in ERROR status, return false.
959 If the timeout is reached generate an exception"""
961 while elapsed_time
< server_timeout
:
962 vm_status
= self
.nova
.servers
.get(vm_id
).status
963 if vm_status
== status
:
965 if vm_status
== 'ERROR':
970 # if we exceeded the timeout rollback
971 if elapsed_time
>= server_timeout
:
972 raise vimconn
.vimconnException('Timeout waiting for instance ' + vm_id
+ ' to get ' + status
,
973 http_code
=vimconn
.HTTP_Request_Timeout
)
975 def _get_openstack_availablity_zones(self
):
977 Get from openstack availability zones available
981 openstack_availability_zone
= self
.nova
.availability_zones
.list()
982 openstack_availability_zone
= [str(zone
.zoneName
) for zone
in openstack_availability_zone
983 if zone
.zoneName
!= 'internal']
984 return openstack_availability_zone
985 except Exception as e
:
988 def _set_availablity_zones(self
):
990 Set vim availablity zone
994 if 'availability_zone' in self
.config
:
995 vim_availability_zones
= self
.config
.get('availability_zone')
996 if isinstance(vim_availability_zones
, str):
997 self
.availability_zone
= [vim_availability_zones
]
998 elif isinstance(vim_availability_zones
, list):
999 self
.availability_zone
= vim_availability_zones
1001 self
.availability_zone
= self
._get
_openstack
_availablity
_zones
()
1003 def _get_vm_availability_zone(self
, availability_zone_index
, availability_zone_list
):
1005 Return thge availability zone to be used by the created VM.
1006 :return: The VIM availability zone to be used or None
1008 if availability_zone_index
is None:
1009 if not self
.config
.get('availability_zone'):
1011 elif isinstance(self
.config
.get('availability_zone'), str):
1012 return self
.config
['availability_zone']
1014 # TODO consider using a different parameter at config for default AV and AV list match
1015 return self
.config
['availability_zone'][0]
1017 vim_availability_zones
= self
.availability_zone
1018 # check if VIM offer enough availability zones describe in the VNFD
1019 if vim_availability_zones
and len(availability_zone_list
) <= len(vim_availability_zones
):
1020 # check if all the names of NFV AV match VIM AV names
1021 match_by_index
= False
1022 for av
in availability_zone_list
:
1023 if av
not in vim_availability_zones
:
1024 match_by_index
= True
1027 return vim_availability_zones
[availability_zone_index
]
1029 return availability_zone_list
[availability_zone_index
]
1031 raise vimconn
.vimconnConflictException("No enough availability zones at VIM for this deployment")
1033 def new_vminstance(self
, name
, description
, start
, image_id
, flavor_id
, net_list
, cloud_config
=None, disk_list
=None,
1034 availability_zone_index
=None, availability_zone_list
=None):
1035 """Adds a VM instance to VIM
1037 start: indicates if VM must start or boot in pause mode. Ignored
1038 image_id,flavor_id: iamge and flavor uuid
1039 net_list: list of interfaces, each one is a dictionary with:
1041 net_id: network uuid to connect
1042 vpci: virtual vcpi to assign, ignored because openstack lack #TODO
1043 model: interface model, ignored #TODO
1044 mac_address: used for SR-IOV ifaces #TODO for other types
1045 use: 'data', 'bridge', 'mgmt'
1046 type: 'virtual', 'PCI-PASSTHROUGH'('PF'), 'SR-IOV'('VF'), 'VFnotShared'
1047 vim_id: filled/added by this function
1048 floating_ip: True/False (or it can be None)
1049 'cloud_config': (optional) dictionary with:
1050 'key-pairs': (optional) list of strings with the public key to be inserted to the default user
1051 'users': (optional) list of users to be inserted, each item is a dict with:
1052 'name': (mandatory) user name,
1053 'key-pairs': (optional) list of strings with the public key to be inserted to the user
1054 'user-data': (optional) string is a text script to be passed directly to cloud-init
1055 'config-files': (optional). List of files to be transferred. Each item is a dict with:
1056 'dest': (mandatory) string with the destination absolute path
1057 'encoding': (optional, by default text). Can be one of:
1058 'b64', 'base64', 'gz', 'gz+b64', 'gz+base64', 'gzip+b64', 'gzip+base64'
1059 'content' (mandatory): string with the content of the file
1060 'permissions': (optional) string with file permissions, typically octal notation '0644'
1061 'owner': (optional) file owner, string with the format 'owner:group'
1062 'boot-data-drive': boolean to indicate if user-data must be passed using a boot drive (hard disk)
1063 'disk_list': (optional) list with additional disks to the VM. Each item is a dict with:
1064 'image_id': (optional). VIM id of an existing image. If not provided an empty disk must be mounted
1065 'size': (mandatory) string with the size of the disk in GB
1066 'vim_id' (optional) should use this existing volume id
1067 availability_zone_index: Index of availability_zone_list to use for this this VM. None if not AV required
1068 availability_zone_list: list of availability zones given by user in the VNFD descriptor. Ignore if
1069 availability_zone_index is None
1070 #TODO ip, security groups
1071 Returns a tuple with the instance identifier and created_items or raises an exception on error
1072 created_items can be None or a dictionary where this method can include key-values that will be passed to
1073 the method delete_vminstance and action_vminstance. Can be used to store created ports, volumes, etc.
1074 Format is vimconnector dependent, but do not use nested dictionaries and a value of None should be the same
1077 self
.logger
.debug("new_vminstance input: image='%s' flavor='%s' nics='%s'",image_id
, flavor_id
,str(net_list
))
1083 external_network
= [] # list of external networks to be connected to instance, later on used to create floating_ip
1084 no_secured_ports
= [] # List of port-is with port-security disabled
1085 self
._reload
_connection
()
1086 # metadata_vpci = {} # For a specific neutron plugin
1087 block_device_mapping
= None
1089 for net
in net_list
:
1090 if not net
.get("net_id"): # skip non connected iface
1094 "network_id": net
["net_id"],
1095 "name": net
.get("name"),
1096 "admin_state_up": True
1098 if self
.config
.get("security_groups") and net
.get("port_security") is not False and \
1099 not self
.config
.get("no_port_security_extension"):
1100 if not self
.security_groups_id
:
1101 self
._get
_ids
_from
_name
()
1102 port_dict
["security_groups"] = self
.security_groups_id
1104 if net
["type"]=="virtual":
1107 # metadata_vpci[ net["net_id"] ] = [[ net["vpci"], "" ]]
1108 elif net
["type"] == "VF" or net
["type"] == "SR-IOV": # for VF
1110 # if "VF" not in metadata_vpci:
1111 # metadata_vpci["VF"]=[]
1112 # metadata_vpci["VF"].append([ net["vpci"], "" ])
1113 port_dict
["binding:vnic_type"]="direct"
1114 # VIO specific Changes
1115 if self
.vim_type
== "VIO":
1116 # Need to create port with port_security_enabled = False and no-security-groups
1117 port_dict
["port_security_enabled"]=False
1118 port_dict
["provider_security_groups"]=[]
1119 port_dict
["security_groups"]=[]
1120 else: # For PT PCI-PASSTHROUGH
1121 # VIO specific Changes
1122 # Current VIO release does not support port with type 'direct-physical'
1123 # So no need to create virtual port in case of PCI-device.
1124 # Will update port_dict code when support gets added in next VIO release
1125 if self
.vim_type
== "VIO":
1126 raise vimconn
.vimconnNotSupportedException(
1127 "Current VIO release does not support full passthrough (PT)")
1129 # if "PF" not in metadata_vpci:
1130 # metadata_vpci["PF"]=[]
1131 # metadata_vpci["PF"].append([ net["vpci"], "" ])
1132 port_dict
["binding:vnic_type"]="direct-physical"
1133 if not port_dict
["name"]:
1134 port_dict
["name"]=name
1135 if net
.get("mac_address"):
1136 port_dict
["mac_address"]=net
["mac_address"]
1137 if net
.get("ip_address"):
1138 port_dict
["fixed_ips"] = [{'ip_address': net
["ip_address"]}]
1139 # TODO add 'subnet_id': <subnet_id>
1140 new_port
= self
.neutron
.create_port({"port": port_dict
})
1141 created_items
["port:" + str(new_port
["port"]["id"])] = True
1142 net
["mac_adress"] = new_port
["port"]["mac_address"]
1143 net
["vim_id"] = new_port
["port"]["id"]
1144 # if try to use a network without subnetwork, it will return a emtpy list
1145 fixed_ips
= new_port
["port"].get("fixed_ips")
1147 net
["ip"] = fixed_ips
[0].get("ip_address")
1151 port
= {"port-id": new_port
["port"]["id"]}
1152 if float(self
.nova
.api_version
.get_string()) >= 2.32:
1153 port
["tag"] = new_port
["port"]["name"]
1154 net_list_vim
.append(port
)
1156 if net
.get('floating_ip', False):
1157 net
['exit_on_floating_ip_error'] = True
1158 external_network
.append(net
)
1159 elif net
['use'] == 'mgmt' and self
.config
.get('use_floating_ip'):
1160 net
['exit_on_floating_ip_error'] = False
1161 external_network
.append(net
)
1162 net
['floating_ip'] = self
.config
.get('use_floating_ip')
1164 # If port security is disabled when the port has not yet been attached to the VM, then all vm traffic is dropped.
1165 # As a workaround we wait until the VM is active and then disable the port-security
1166 if net
.get("port_security") == False and not self
.config
.get("no_port_security_extension"):
1167 no_secured_ports
.append(new_port
["port"]["id"])
1170 # metadata = {"pci_assignement": json.dumps(metadata_vpci)}
1171 # if len(metadata["pci_assignement"]) >255:
1172 # #limit the metadata size
1173 # #metadata["pci_assignement"] = metadata["pci_assignement"][0:255]
1174 # self.logger.warn("Metadata deleted since it exceeds the expected length (255) ")
1177 self
.logger
.debug("name '%s' image_id '%s'flavor_id '%s' net_list_vim '%s' description '%s'",
1178 name
, image_id
, flavor_id
, str(net_list_vim
), description
)
1181 config_drive
, userdata
= self
._create
_user
_data
(cloud_config
)
1183 # Create additional volumes in case these are present in disk_list
1184 base_disk_index
= ord('b')
1186 block_device_mapping
= {}
1187 for disk
in disk_list
:
1188 if disk
.get('vim_id'):
1189 block_device_mapping
['_vd' + chr(base_disk_index
)] = disk
['vim_id']
1191 if 'image_id' in disk
:
1192 volume
= self
.cinder
.volumes
.create(size
=disk
['size'], name
=name
+ '_vd' +
1193 chr(base_disk_index
), imageRef
=disk
['image_id'])
1195 volume
= self
.cinder
.volumes
.create(size
=disk
['size'], name
=name
+ '_vd' +
1196 chr(base_disk_index
))
1197 created_items
["volume:" + str(volume
.id)] = True
1198 block_device_mapping
['_vd' + chr(base_disk_index
)] = volume
.id
1199 base_disk_index
+= 1
1201 # Wait until created volumes are with status available
1203 while elapsed_time
< volume_timeout
:
1204 for created_item
in created_items
:
1205 v
, _
, volume_id
= created_item
.partition(":")
1207 if self
.cinder
.volumes
.get(volume_id
).status
!= 'available':
1209 else: # all ready: break from while
1213 # If we exceeded the timeout rollback
1214 if elapsed_time
>= volume_timeout
:
1215 raise vimconn
.vimconnException('Timeout creating volumes for instance ' + name
,
1216 http_code
=vimconn
.HTTP_Request_Timeout
)
1217 # get availability Zone
1218 vm_av_zone
= self
._get
_vm
_availability
_zone
(availability_zone_index
, availability_zone_list
)
1220 self
.logger
.debug("nova.servers.create({}, {}, {}, nics={}, security_groups={}, "
1221 "availability_zone={}, key_name={}, userdata={}, config_drive={}, "
1222 "block_device_mapping={})".format(name
, image_id
, flavor_id
, net_list_vim
,
1223 self
.config
.get("security_groups"), vm_av_zone
,
1224 self
.config
.get('keypair'), userdata
, config_drive
,
1225 block_device_mapping
))
1226 server
= self
.nova
.servers
.create(name
, image_id
, flavor_id
, nics
=net_list_vim
,
1227 security_groups
=self
.config
.get("security_groups"),
1228 # TODO remove security_groups in future versions. Already at neutron port
1229 availability_zone
=vm_av_zone
,
1230 key_name
=self
.config
.get('keypair'),
1232 config_drive
=config_drive
,
1233 block_device_mapping
=block_device_mapping
1234 ) # , description=description)
1236 vm_start_time
= time
.time()
1237 # Previously mentioned workaround to wait until the VM is active and then disable the port-security
1238 if no_secured_ports
:
1239 self
.__wait
_for
_vm
(server
.id, 'ACTIVE')
1241 for port_id
in no_secured_ports
:
1243 self
.neutron
.update_port(port_id
,
1244 {"port": {"port_security_enabled": False, "security_groups": None}})
1245 except Exception as e
:
1246 raise vimconn
.vimconnException("It was not possible to disable port security for port {}".format(
1248 # print "DONE :-)", server
1251 if external_network
:
1252 floating_ips
= self
.neutron
.list_floatingips().get("floatingips", ())
1253 for floating_network
in external_network
:
1258 ip
= floating_ips
.pop(0)
1259 if ip
.get("port_id", False) or ip
.get('tenant_id') != server
.tenant_id
:
1261 if isinstance(floating_network
['floating_ip'], str):
1262 if ip
.get("floating_network_id") != floating_network
['floating_ip']:
1264 free_floating_ip
= ip
.get("floating_ip_address")
1266 if isinstance(floating_network
['floating_ip'], str) and \
1267 floating_network
['floating_ip'].lower() != "true":
1268 pool_id
= floating_network
['floating_ip']
1270 # Find the external network
1271 external_nets
= list()
1272 for net
in self
.neutron
.list_networks()['networks']:
1273 if net
['router:external']:
1274 external_nets
.append(net
)
1276 if len(external_nets
) == 0:
1277 raise vimconn
.vimconnException("Cannot create floating_ip automatically since no external "
1278 "network is present",
1279 http_code
=vimconn
.HTTP_Conflict
)
1280 if len(external_nets
) > 1:
1281 raise vimconn
.vimconnException("Cannot create floating_ip automatically since multiple "
1282 "external networks are present",
1283 http_code
=vimconn
.HTTP_Conflict
)
1285 pool_id
= external_nets
[0].get('id')
1286 param
= {'floatingip': {'floating_network_id': pool_id
, 'tenant_id': server
.tenant_id
}}
1288 # self.logger.debug("Creating floating IP")
1289 new_floating_ip
= self
.neutron
.create_floatingip(param
)
1290 free_floating_ip
= new_floating_ip
['floatingip']['floating_ip_address']
1291 except Exception as e
:
1292 raise vimconn
.vimconnException(type(e
).__name
__ + ": Cannot create new floating_ip " +
1293 str(e
), http_code
=vimconn
.HTTP_Conflict
)
1295 fix_ip
= floating_network
.get('ip')
1298 server
.add_floating_ip(free_floating_ip
, fix_ip
)
1300 except Exception as e
:
1301 # openstack need some time after VM creation to asign an IP. So retry if fails
1302 vm_status
= self
.nova
.servers
.get(server
.id).status
1303 if vm_status
!= 'ACTIVE' and vm_status
!= 'ERROR':
1304 if time
.time() - vm_start_time
< server_timeout
:
1307 raise vimconn
.vimconnException(
1308 "Cannot create floating_ip: {} {}".format(type(e
).__name
__, e
),
1309 http_code
=vimconn
.HTTP_Conflict
)
1311 except Exception as e
:
1312 if not floating_network
['exit_on_floating_ip_error']:
1313 self
.logger
.warn("Cannot create floating_ip. %s", str(e
))
1317 return server
.id, created_items
1318 # except nvExceptions.NotFound as e:
1319 # error_value=-vimconn.HTTP_Not_Found
1320 # error_text= "vm instance %s not found" % vm_id
1321 # except TypeError as e:
1322 # raise vimconn.vimconnException(type(e).__name__ + ": "+ str(e), http_code=vimconn.HTTP_Bad_Request)
1324 except Exception as e
:
1327 server_id
= server
.id
1329 self
.delete_vminstance(server_id
, created_items
)
1330 except Exception as e2
:
1331 self
.logger
.error("new_vminstance rollback fail {}".format(e2
))
1333 self
._format
_exception
(e
)
1335 def get_vminstance(self
,vm_id
):
1336 '''Returns the VM instance information from VIM'''
1337 #self.logger.debug("Getting VM from VIM")
1339 self
._reload
_connection
()
1340 server
= self
.nova
.servers
.find(id=vm_id
)
1341 #TODO parse input and translate to VIM format (openmano_schemas.new_vminstance_response_schema)
1342 return server
.to_dict()
1343 except (ksExceptions
.ClientException
, nvExceptions
.ClientException
, nvExceptions
.NotFound
, ConnectionError
) as e
:
1344 self
._format
_exception
(e
)
1346 def get_vminstance_console(self
,vm_id
, console_type
="vnc"):
1348 Get a console for the virtual machine
1350 vm_id: uuid of the VM
1351 console_type, can be:
1352 "novnc" (by default), "xvpvnc" for VNC types,
1353 "rdp-html5" for RDP types, "spice-html5" for SPICE types
1354 Returns dict with the console parameters:
1355 protocol: ssh, ftp, http, https, ...
1356 server: usually ip address
1357 port: the http, ssh, ... port
1358 suffix: extra text, e.g. the http path and query string
1360 self
.logger
.debug("Getting VM CONSOLE from VIM")
1362 self
._reload
_connection
()
1363 server
= self
.nova
.servers
.find(id=vm_id
)
1364 if console_type
== None or console_type
== "novnc":
1365 console_dict
= server
.get_vnc_console("novnc")
1366 elif console_type
== "xvpvnc":
1367 console_dict
= server
.get_vnc_console(console_type
)
1368 elif console_type
== "rdp-html5":
1369 console_dict
= server
.get_rdp_console(console_type
)
1370 elif console_type
== "spice-html5":
1371 console_dict
= server
.get_spice_console(console_type
)
1373 raise vimconn
.vimconnException("console type '{}' not allowed".format(console_type
), http_code
=vimconn
.HTTP_Bad_Request
)
1375 console_dict1
= console_dict
.get("console")
1377 console_url
= console_dict1
.get("url")
1380 protocol_index
= console_url
.find("//")
1381 suffix_index
= console_url
[protocol_index
+2:].find("/") + protocol_index
+2
1382 port_index
= console_url
[protocol_index
+2:suffix_index
].find(":") + protocol_index
+2
1383 if protocol_index
< 0 or port_index
<0 or suffix_index
<0:
1384 return -vimconn
.HTTP_Internal_Server_Error
, "Unexpected response from VIM"
1385 console_dict
={"protocol": console_url
[0:protocol_index
],
1386 "server": console_url
[protocol_index
+2:port_index
],
1387 "port": console_url
[port_index
:suffix_index
],
1388 "suffix": console_url
[suffix_index
+1:]
1392 raise vimconn
.vimconnUnexpectedResponse("Unexpected response from VIM")
1394 except (nvExceptions
.NotFound
, ksExceptions
.ClientException
, nvExceptions
.ClientException
, nvExceptions
.BadRequest
, ConnectionError
) as e
:
1395 self
._format
_exception
(e
)
1397 def delete_vminstance(self
, vm_id
, created_items
=None):
1398 '''Removes a VM instance from VIM. Returns the old identifier
1400 #print "osconnector: Getting VM from VIM"
1401 if created_items
== None:
1404 self
._reload
_connection
()
1405 # delete VM ports attached to this networks before the virtual machine
1406 for k
, v
in created_items
.items():
1407 if not v
: # skip already deleted
1410 k_item
, _
, k_id
= k
.partition(":")
1411 if k_item
== "port":
1412 self
.neutron
.delete_port(k_id
)
1413 except Exception as e
:
1414 self
.logger
.error("Error deleting port: {}: {}".format(type(e
).__name
__, e
))
1416 # #commented because detaching the volumes makes the servers.delete not work properly ?!?
1417 # #dettach volumes attached
1418 # server = self.nova.servers.get(vm_id)
1419 # volumes_attached_dict = server._info['os-extended-volumes:volumes_attached'] #volume['id']
1420 # #for volume in volumes_attached_dict:
1421 # # self.cinder.volumes.detach(volume['id'])
1424 self
.nova
.servers
.delete(vm_id
)
1426 # delete volumes. Although having detached, they should have in active status before deleting
1427 # we ensure in this loop
1430 while keep_waiting
and elapsed_time
< volume_timeout
:
1431 keep_waiting
= False
1432 for k
, v
in created_items
.items():
1433 if not v
: # skip already deleted
1436 k_item
, _
, k_id
= k
.partition(":")
1437 if k_item
== "volume":
1438 if self
.cinder
.volumes
.get(k_id
).status
!= 'available':
1441 self
.cinder
.volumes
.delete(k_id
)
1442 except Exception as e
:
1443 self
.logger
.error("Error deleting volume: {}: {}".format(type(e
).__name
__, e
))
1448 except (nvExceptions
.NotFound
, ksExceptions
.ClientException
, nvExceptions
.ClientException
, ConnectionError
) as e
:
1449 self
._format
_exception
(e
)
1451 def refresh_vms_status(self
, vm_list
):
1452 '''Get the status of the virtual machines and their interfaces/ports
1453 Params: the list of VM identifiers
1454 Returns a dictionary with:
1455 vm_id: #VIM id of this Virtual Machine
1456 status: #Mandatory. Text with one of:
1457 # DELETED (not found at vim)
1458 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
1459 # OTHER (Vim reported other status not understood)
1460 # ERROR (VIM indicates an ERROR status)
1461 # ACTIVE, PAUSED, SUSPENDED, INACTIVE (not running),
1462 # CREATING (on building process), ERROR
1463 # ACTIVE:NoMgmtIP (Active but any of its interface has an IP address
1465 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
1466 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
1468 - vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
1469 mac_address: #Text format XX:XX:XX:XX:XX:XX
1470 vim_net_id: #network id where this interface is connected
1471 vim_interface_id: #interface/port VIM id
1472 ip_address: #null, or text with IPv4, IPv6 address
1473 compute_node: #identification of compute node where PF,VF interface is allocated
1474 pci: #PCI address of the NIC that hosts the PF,VF
1475 vlan: #physical VLAN used for VF
1478 self
.logger
.debug("refresh_vms status: Getting tenant VM instance information from VIM")
1479 for vm_id
in vm_list
:
1482 vm_vim
= self
.get_vminstance(vm_id
)
1483 if vm_vim
['status'] in vmStatus2manoFormat
:
1484 vm
['status'] = vmStatus2manoFormat
[ vm_vim
['status'] ]
1486 vm
['status'] = "OTHER"
1487 vm
['error_msg'] = "VIM status reported " + vm_vim
['status']
1489 vm
['vim_info'] = self
.serialize(vm_vim
)
1491 vm
["interfaces"] = []
1492 if vm_vim
.get('fault'):
1493 vm
['error_msg'] = str(vm_vim
['fault'])
1496 self
._reload
_connection
()
1497 port_dict
= self
.neutron
.list_ports(device_id
=vm_id
)
1498 for port
in port_dict
["ports"]:
1500 interface
['vim_info'] = self
.serialize(port
)
1501 interface
["mac_address"] = port
.get("mac_address")
1502 interface
["vim_net_id"] = port
["network_id"]
1503 interface
["vim_interface_id"] = port
["id"]
1504 # check if OS-EXT-SRV-ATTR:host is there,
1505 # in case of non-admin credentials, it will be missing
1506 if vm_vim
.get('OS-EXT-SRV-ATTR:host'):
1507 interface
["compute_node"] = vm_vim
['OS-EXT-SRV-ATTR:host']
1508 interface
["pci"] = None
1510 # check if binding:profile is there,
1511 # in case of non-admin credentials, it will be missing
1512 if port
.get('binding:profile'):
1513 if port
['binding:profile'].get('pci_slot'):
1514 # TODO: At the moment sr-iov pci addresses are converted to PF pci addresses by setting the slot to 0x00
1515 # TODO: This is just a workaround valid for niantinc. Find a better way to do so
1516 # CHANGE DDDD:BB:SS.F to DDDD:BB:00.(F%2) assuming there are 2 ports per nic
1517 pci
= port
['binding:profile']['pci_slot']
1518 # interface["pci"] = pci[:-4] + "00." + str(int(pci[-1]) % 2)
1519 interface
["pci"] = pci
1520 interface
["vlan"] = None
1521 #if network is of type vlan and port is of type direct (sr-iov) then set vlan id
1522 network
= self
.neutron
.show_network(port
["network_id"])
1523 if network
['network'].get('provider:network_type') == 'vlan' and \
1524 port
.get("binding:vnic_type") == "direct":
1525 interface
["vlan"] = network
['network'].get('provider:segmentation_id')
1527 #look for floating ip address
1529 floating_ip_dict
= self
.neutron
.list_floatingips(port_id
=port
["id"])
1530 if floating_ip_dict
.get("floatingips"):
1531 ips
.append(floating_ip_dict
["floatingips"][0].get("floating_ip_address") )
1535 for subnet
in port
["fixed_ips"]:
1536 ips
.append(subnet
["ip_address"])
1537 interface
["ip_address"] = ";".join(ips
)
1538 vm
["interfaces"].append(interface
)
1539 except Exception as e
:
1540 self
.logger
.error("Error getting vm interface information {}: {}".format(type(e
).__name
__, e
),
1542 except vimconn
.vimconnNotFoundException
as e
:
1543 self
.logger
.error("Exception getting vm status: %s", str(e
))
1544 vm
['status'] = "DELETED"
1545 vm
['error_msg'] = str(e
)
1546 except vimconn
.vimconnException
as e
:
1547 self
.logger
.error("Exception getting vm status: %s", str(e
))
1548 vm
['status'] = "VIM_ERROR"
1549 vm
['error_msg'] = str(e
)
1553 def action_vminstance(self
, vm_id
, action_dict
, created_items
={}):
1554 '''Send and action over a VM instance from VIM
1555 Returns None or the console dict if the action was successfully sent to the VIM'''
1556 self
.logger
.debug("Action over VM '%s': %s", vm_id
, str(action_dict
))
1558 self
._reload
_connection
()
1559 server
= self
.nova
.servers
.find(id=vm_id
)
1560 if "start" in action_dict
:
1561 if action_dict
["start"]=="rebuild":
1564 if server
.status
=="PAUSED":
1566 elif server
.status
=="SUSPENDED":
1568 elif server
.status
=="SHUTOFF":
1570 elif "pause" in action_dict
:
1572 elif "resume" in action_dict
:
1574 elif "shutoff" in action_dict
or "shutdown" in action_dict
:
1576 elif "forceOff" in action_dict
:
1578 elif "terminate" in action_dict
:
1580 elif "createImage" in action_dict
:
1581 server
.create_image()
1582 #"path":path_schema,
1583 #"description":description_schema,
1584 #"name":name_schema,
1585 #"metadata":metadata_schema,
1586 #"imageRef": id_schema,
1587 #"disk": {"oneOf":[{"type": "null"}, {"type":"string"}] },
1588 elif "rebuild" in action_dict
:
1589 server
.rebuild(server
.image
['id'])
1590 elif "reboot" in action_dict
:
1591 server
.reboot() #reboot_type='SOFT'
1592 elif "console" in action_dict
:
1593 console_type
= action_dict
["console"]
1594 if console_type
== None or console_type
== "novnc":
1595 console_dict
= server
.get_vnc_console("novnc")
1596 elif console_type
== "xvpvnc":
1597 console_dict
= server
.get_vnc_console(console_type
)
1598 elif console_type
== "rdp-html5":
1599 console_dict
= server
.get_rdp_console(console_type
)
1600 elif console_type
== "spice-html5":
1601 console_dict
= server
.get_spice_console(console_type
)
1603 raise vimconn
.vimconnException("console type '{}' not allowed".format(console_type
),
1604 http_code
=vimconn
.HTTP_Bad_Request
)
1606 console_url
= console_dict
["console"]["url"]
1608 protocol_index
= console_url
.find("//")
1609 suffix_index
= console_url
[protocol_index
+2:].find("/") + protocol_index
+2
1610 port_index
= console_url
[protocol_index
+2:suffix_index
].find(":") + protocol_index
+2
1611 if protocol_index
< 0 or port_index
<0 or suffix_index
<0:
1612 raise vimconn
.vimconnException("Unexpected response from VIM " + str(console_dict
))
1613 console_dict2
={"protocol": console_url
[0:protocol_index
],
1614 "server": console_url
[protocol_index
+2 : port_index
],
1615 "port": int(console_url
[port_index
+1 : suffix_index
]),
1616 "suffix": console_url
[suffix_index
+1:]
1618 return console_dict2
1619 except Exception as e
:
1620 raise vimconn
.vimconnException("Unexpected response from VIM " + str(console_dict
))
1623 except (ksExceptions
.ClientException
, nvExceptions
.ClientException
, nvExceptions
.NotFound
, ConnectionError
) as e
:
1624 self
._format
_exception
(e
)
1625 #TODO insert exception vimconn.HTTP_Unauthorized
1627 ####### VIO Specific Changes #########
1628 def _genrate_vlanID(self
):
1630 Method to get unused vlanID
1638 networks
= self
.get_network_list()
1639 for net
in networks
:
1640 if net
.get('provider:segmentation_id'):
1641 usedVlanIDs
.append(net
.get('provider:segmentation_id'))
1642 used_vlanIDs
= set(usedVlanIDs
)
1644 #find unused VLAN ID
1645 for vlanID_range
in self
.config
.get('dataplane_net_vlan_range'):
1647 start_vlanid
, end_vlanid
= map(int, vlanID_range
.replace(" ", "").split("-"))
1648 for vlanID
in xrange(start_vlanid
, end_vlanid
+ 1):
1649 if vlanID
not in used_vlanIDs
:
1651 except Exception as exp
:
1652 raise vimconn
.vimconnException("Exception {} occurred while generating VLAN ID.".format(exp
))
1654 raise vimconn
.vimconnConflictException("Unable to create the SRIOV VLAN network."\
1655 " All given Vlan IDs {} are in use.".format(self
.config
.get('dataplane_net_vlan_range')))
1658 def _validate_vlan_ranges(self
, dataplane_net_vlan_range
):
1660 Method to validate user given vlanID ranges
1664 for vlanID_range
in dataplane_net_vlan_range
:
1665 vlan_range
= vlanID_range
.replace(" ", "")
1667 vlanID_pattern
= r
'(\d)*-(\d)*$'
1668 match_obj
= re
.match(vlanID_pattern
, vlan_range
)
1670 raise vimconn
.vimconnConflictException("Invalid dataplane_net_vlan_range {}.You must provide "\
1671 "'dataplane_net_vlan_range' in format [start_ID - end_ID].".format(vlanID_range
))
1673 start_vlanid
, end_vlanid
= map(int,vlan_range
.split("-"))
1674 if start_vlanid
<= 0 :
1675 raise vimconn
.vimconnConflictException("Invalid dataplane_net_vlan_range {}."\
1676 "Start ID can not be zero. For VLAN "\
1677 "networks valid IDs are 1 to 4094 ".format(vlanID_range
))
1678 if end_vlanid
> 4094 :
1679 raise vimconn
.vimconnConflictException("Invalid dataplane_net_vlan_range {}."\
1680 "End VLAN ID can not be greater than 4094. For VLAN "\
1681 "networks valid IDs are 1 to 4094 ".format(vlanID_range
))
1683 if start_vlanid
> end_vlanid
:
1684 raise vimconn
.vimconnConflictException("Invalid dataplane_net_vlan_range {}."\
1685 "You must provide a 'dataplane_net_vlan_range' in format start_ID - end_ID and "\
1686 "start_ID < end_ID ".format(vlanID_range
))
1690 def new_external_port(self
, port_data
):
1691 #TODO openstack if needed
1692 '''Adds a external port to VIM'''
1693 '''Returns the port identifier'''
1694 return -vimconn
.HTTP_Internal_Server_Error
, "osconnector.new_external_port() not implemented"
1696 def connect_port_network(self
, port_id
, network_id
, admin
=False):
1697 #TODO openstack if needed
1698 '''Connects a external port to a network'''
1699 '''Returns status code of the VIM response'''
1700 return -vimconn
.HTTP_Internal_Server_Error
, "osconnector.connect_port_network() not implemented"
1702 def new_user(self
, user_name
, user_passwd
, tenant_id
=None):
1703 '''Adds a new user to openstack VIM'''
1704 '''Returns the user identifier'''
1705 self
.logger
.debug("osconnector: Adding a new user to VIM")
1707 self
._reload
_connection
()
1708 user
=self
.keystone
.users
.create(user_name
, password
=user_passwd
, default_project
=tenant_id
)
1709 #self.keystone.tenants.add_user(self.k_creds["username"], #role)
1711 except ksExceptions
.ConnectionError
as e
:
1712 error_value
=-vimconn
.HTTP_Bad_Request
1713 error_text
= type(e
).__name
__ + ": "+ (str(e
) if len(e
.args
)==0 else str(e
.args
[0]))
1714 except ksExceptions
.ClientException
as e
: #TODO remove
1715 error_value
=-vimconn
.HTTP_Bad_Request
1716 error_text
= type(e
).__name
__ + ": "+ (str(e
) if len(e
.args
)==0 else str(e
.args
[0]))
1717 #TODO insert exception vimconn.HTTP_Unauthorized
1718 #if reaching here is because an exception
1719 self
.logger
.debug("new_user " + error_text
)
1720 return error_value
, error_text
1722 def delete_user(self
, user_id
):
1723 '''Delete a user from openstack VIM'''
1724 '''Returns the user identifier'''
1726 print("osconnector: Deleting a user from VIM")
1728 self
._reload
_connection
()
1729 self
.keystone
.users
.delete(user_id
)
1731 except ksExceptions
.ConnectionError
as e
:
1732 error_value
=-vimconn
.HTTP_Bad_Request
1733 error_text
= type(e
).__name
__ + ": "+ (str(e
) if len(e
.args
)==0 else str(e
.args
[0]))
1734 except ksExceptions
.NotFound
as e
:
1735 error_value
=-vimconn
.HTTP_Not_Found
1736 error_text
= type(e
).__name
__ + ": "+ (str(e
) if len(e
.args
)==0 else str(e
.args
[0]))
1737 except ksExceptions
.ClientException
as e
: #TODO remove
1738 error_value
=-vimconn
.HTTP_Bad_Request
1739 error_text
= type(e
).__name
__ + ": "+ (str(e
) if len(e
.args
)==0 else str(e
.args
[0]))
1740 #TODO insert exception vimconn.HTTP_Unauthorized
1741 #if reaching here is because an exception
1742 self
.logger
.debug("delete_tenant " + error_text
)
1743 return error_value
, error_text
1745 def get_hosts_info(self
):
1746 '''Get the information of deployed hosts
1747 Returns the hosts content'''
1749 print("osconnector: Getting Host info from VIM")
1752 self
._reload
_connection
()
1753 hypervisors
= self
.nova
.hypervisors
.list()
1754 for hype
in hypervisors
:
1755 h_list
.append( hype
.to_dict() )
1756 return 1, {"hosts":h_list
}
1757 except nvExceptions
.NotFound
as e
:
1758 error_value
=-vimconn
.HTTP_Not_Found
1759 error_text
= (str(e
) if len(e
.args
)==0 else str(e
.args
[0]))
1760 except (ksExceptions
.ClientException
, nvExceptions
.ClientException
) as e
:
1761 error_value
=-vimconn
.HTTP_Bad_Request
1762 error_text
= type(e
).__name
__ + ": "+ (str(e
) if len(e
.args
)==0 else str(e
.args
[0]))
1763 #TODO insert exception vimconn.HTTP_Unauthorized
1764 #if reaching here is because an exception
1765 self
.logger
.debug("get_hosts_info " + error_text
)
1766 return error_value
, error_text
1768 def get_hosts(self
, vim_tenant
):
1769 '''Get the hosts and deployed instances
1770 Returns the hosts content'''
1771 r
, hype_dict
= self
.get_hosts_info()
1774 hypervisors
= hype_dict
["hosts"]
1776 servers
= self
.nova
.servers
.list()
1777 for hype
in hypervisors
:
1778 for server
in servers
:
1779 if server
.to_dict()['OS-EXT-SRV-ATTR:hypervisor_hostname']==hype
['hypervisor_hostname']:
1781 hype
['vm'].append(server
.id)
1783 hype
['vm'] = [server
.id]
1785 except nvExceptions
.NotFound
as e
:
1786 error_value
=-vimconn
.HTTP_Not_Found
1787 error_text
= (str(e
) if len(e
.args
)==0 else str(e
.args
[0]))
1788 except (ksExceptions
.ClientException
, nvExceptions
.ClientException
) as e
:
1789 error_value
=-vimconn
.HTTP_Bad_Request
1790 error_text
= type(e
).__name
__ + ": "+ (str(e
) if len(e
.args
)==0 else str(e
.args
[0]))
1791 #TODO insert exception vimconn.HTTP_Unauthorized
1792 #if reaching here is because an exception
1793 self
.logger
.debug("get_hosts " + error_text
)
1794 return error_value
, error_text
1796 def new_classification(self
, name
, ctype
, definition
):
1798 'Adding a new (Traffic) Classification to VIM, named %s', name
)
1801 self
._reload
_connection
()
1802 if ctype
not in supportedClassificationTypes
:
1803 raise vimconn
.vimconnNotSupportedException(
1804 'OpenStack VIM connector doesn\'t support provided '
1805 'Classification Type {}, supported ones are: '
1806 '{}'.format(ctype
, supportedClassificationTypes
))
1807 if not self
._validate
_classification
(ctype
, definition
):
1808 raise vimconn
.vimconnException(
1809 'Incorrect Classification definition '
1810 'for the type specified.')
1811 classification_dict
= definition
1812 classification_dict
['name'] = name
1814 new_class
= self
.neutron
.create_sfc_flow_classifier(
1815 {'flow_classifier': classification_dict
})
1816 return new_class
['flow_classifier']['id']
1817 except (neExceptions
.ConnectionFailed
, ksExceptions
.ClientException
,
1818 neExceptions
.NeutronException
, ConnectionError
) as e
:
1820 'Creation of Classification failed.')
1821 self
._format
_exception
(e
)
1823 def get_classification(self
, class_id
):
1824 self
.logger
.debug(" Getting Classification %s from VIM", class_id
)
1825 filter_dict
= {"id": class_id
}
1826 class_list
= self
.get_classification_list(filter_dict
)
1827 if len(class_list
) == 0:
1828 raise vimconn
.vimconnNotFoundException(
1829 "Classification '{}' not found".format(class_id
))
1830 elif len(class_list
) > 1:
1831 raise vimconn
.vimconnConflictException(
1832 "Found more than one Classification with this criteria")
1833 classification
= class_list
[0]
1834 return classification
1836 def get_classification_list(self
, filter_dict
={}):
1837 self
.logger
.debug("Getting Classifications from VIM filter: '%s'",
1840 filter_dict_os
= filter_dict
.copy()
1841 self
._reload
_connection
()
1842 if self
.api_version3
and "tenant_id" in filter_dict_os
:
1843 filter_dict_os
['project_id'] = filter_dict_os
.pop('tenant_id')
1844 classification_dict
= self
.neutron
.list_sfc_flow_classifiers(
1846 classification_list
= classification_dict
["flow_classifiers"]
1847 self
.__classification
_os
2mano
(classification_list
)
1848 return classification_list
1849 except (neExceptions
.ConnectionFailed
, ksExceptions
.ClientException
,
1850 neExceptions
.NeutronException
, ConnectionError
) as e
:
1851 self
._format
_exception
(e
)
1853 def delete_classification(self
, class_id
):
1854 self
.logger
.debug("Deleting Classification '%s' from VIM", class_id
)
1856 self
._reload
_connection
()
1857 self
.neutron
.delete_sfc_flow_classifier(class_id
)
1859 except (neExceptions
.ConnectionFailed
, neExceptions
.NeutronException
,
1860 ksExceptions
.ClientException
, neExceptions
.NeutronException
,
1861 ConnectionError
) as e
:
1862 self
._format
_exception
(e
)
1864 def new_sfi(self
, name
, ingress_ports
, egress_ports
, sfc_encap
=True):
1866 "Adding a new Service Function Instance to VIM, named '%s'", name
)
1869 self
._reload
_connection
()
1873 if len(ingress_ports
) != 1:
1874 raise vimconn
.vimconnNotSupportedException(
1875 "OpenStack VIM connector can only have "
1876 "1 ingress port per SFI")
1877 if len(egress_ports
) != 1:
1878 raise vimconn
.vimconnNotSupportedException(
1879 "OpenStack VIM connector can only have "
1880 "1 egress port per SFI")
1881 sfi_dict
= {'name': name
,
1882 'ingress': ingress_ports
[0],
1883 'egress': egress_ports
[0],
1884 'service_function_parameters': {
1885 'correlation': correlation
}}
1886 new_sfi
= self
.neutron
.create_sfc_port_pair({'port_pair': sfi_dict
})
1887 return new_sfi
['port_pair']['id']
1888 except (neExceptions
.ConnectionFailed
, ksExceptions
.ClientException
,
1889 neExceptions
.NeutronException
, ConnectionError
) as e
:
1892 self
.neutron
.delete_sfc_port_pair(
1893 new_sfi
['port_pair']['id'])
1896 'Creation of Service Function Instance failed, with '
1897 'subsequent deletion failure as well.')
1898 self
._format
_exception
(e
)
1900 def get_sfi(self
, sfi_id
):
1902 'Getting Service Function Instance %s from VIM', sfi_id
)
1903 filter_dict
= {"id": sfi_id
}
1904 sfi_list
= self
.get_sfi_list(filter_dict
)
1905 if len(sfi_list
) == 0:
1906 raise vimconn
.vimconnNotFoundException(
1907 "Service Function Instance '{}' not found".format(sfi_id
))
1908 elif len(sfi_list
) > 1:
1909 raise vimconn
.vimconnConflictException(
1910 'Found more than one Service Function Instance '
1911 'with this criteria')
1915 def get_sfi_list(self
, filter_dict
={}):
1916 self
.logger
.debug("Getting Service Function Instances from "
1917 "VIM filter: '%s'", str(filter_dict
))
1919 self
._reload
_connection
()
1920 filter_dict_os
= filter_dict
.copy()
1921 if self
.api_version3
and "tenant_id" in filter_dict_os
:
1922 filter_dict_os
['project_id'] = filter_dict_os
.pop('tenant_id')
1923 sfi_dict
= self
.neutron
.list_sfc_port_pairs(**filter_dict_os
)
1924 sfi_list
= sfi_dict
["port_pairs"]
1925 self
.__sfi
_os
2mano
(sfi_list
)
1927 except (neExceptions
.ConnectionFailed
, ksExceptions
.ClientException
,
1928 neExceptions
.NeutronException
, ConnectionError
) as e
:
1929 self
._format
_exception
(e
)
1931 def delete_sfi(self
, sfi_id
):
1932 self
.logger
.debug("Deleting Service Function Instance '%s' "
1935 self
._reload
_connection
()
1936 self
.neutron
.delete_sfc_port_pair(sfi_id
)
1938 except (neExceptions
.ConnectionFailed
, neExceptions
.NeutronException
,
1939 ksExceptions
.ClientException
, neExceptions
.NeutronException
,
1940 ConnectionError
) as e
:
1941 self
._format
_exception
(e
)
1943 def new_sf(self
, name
, sfis
, sfc_encap
=True):
1944 self
.logger
.debug("Adding a new Service Function to VIM, "
1948 self
._reload
_connection
()
1949 # correlation = None
1951 # correlation = 'nsh'
1952 for instance
in sfis
:
1953 sfi
= self
.get_sfi(instance
)
1954 if sfi
.get('sfc_encap') != sfc_encap
:
1955 raise vimconn
.vimconnNotSupportedException(
1956 "OpenStack VIM connector requires all SFIs of the "
1957 "same SF to share the same SFC Encapsulation")
1958 sf_dict
= {'name': name
,
1960 new_sf
= self
.neutron
.create_sfc_port_pair_group({
1961 'port_pair_group': sf_dict
})
1962 return new_sf
['port_pair_group']['id']
1963 except (neExceptions
.ConnectionFailed
, ksExceptions
.ClientException
,
1964 neExceptions
.NeutronException
, ConnectionError
) as e
:
1967 self
.neutron
.delete_sfc_port_pair_group(
1968 new_sf
['port_pair_group']['id'])
1971 'Creation of Service Function failed, with '
1972 'subsequent deletion failure as well.')
1973 self
._format
_exception
(e
)
1975 def get_sf(self
, sf_id
):
1976 self
.logger
.debug("Getting Service Function %s from VIM", sf_id
)
1977 filter_dict
= {"id": sf_id
}
1978 sf_list
= self
.get_sf_list(filter_dict
)
1979 if len(sf_list
) == 0:
1980 raise vimconn
.vimconnNotFoundException(
1981 "Service Function '{}' not found".format(sf_id
))
1982 elif len(sf_list
) > 1:
1983 raise vimconn
.vimconnConflictException(
1984 "Found more than one Service Function with this criteria")
1988 def get_sf_list(self
, filter_dict
={}):
1989 self
.logger
.debug("Getting Service Function from VIM filter: '%s'",
1992 self
._reload
_connection
()
1993 filter_dict_os
= filter_dict
.copy()
1994 if self
.api_version3
and "tenant_id" in filter_dict_os
:
1995 filter_dict_os
['project_id'] = filter_dict_os
.pop('tenant_id')
1996 sf_dict
= self
.neutron
.list_sfc_port_pair_groups(**filter_dict_os
)
1997 sf_list
= sf_dict
["port_pair_groups"]
1998 self
.__sf
_os
2mano
(sf_list
)
2000 except (neExceptions
.ConnectionFailed
, ksExceptions
.ClientException
,
2001 neExceptions
.NeutronException
, ConnectionError
) as e
:
2002 self
._format
_exception
(e
)
2004 def delete_sf(self
, sf_id
):
2005 self
.logger
.debug("Deleting Service Function '%s' from VIM", sf_id
)
2007 self
._reload
_connection
()
2008 self
.neutron
.delete_sfc_port_pair_group(sf_id
)
2010 except (neExceptions
.ConnectionFailed
, neExceptions
.NeutronException
,
2011 ksExceptions
.ClientException
, neExceptions
.NeutronException
,
2012 ConnectionError
) as e
:
2013 self
._format
_exception
(e
)
2015 def new_sfp(self
, name
, classifications
, sfs
, sfc_encap
=True, spi
=None):
2016 self
.logger
.debug("Adding a new Service Function Path to VIM, "
2020 self
._reload
_connection
()
2021 # In networking-sfc the MPLS encapsulation is legacy
2022 # should be used when no full SFC Encapsulation is intended
2023 correlation
= 'mpls'
2026 sfp_dict
= {'name': name
,
2027 'flow_classifiers': classifications
,
2028 'port_pair_groups': sfs
,
2029 'chain_parameters': {'correlation': correlation
}}
2031 sfp_dict
['chain_id'] = spi
2032 new_sfp
= self
.neutron
.create_sfc_port_chain({'port_chain': sfp_dict
})
2033 return new_sfp
["port_chain"]["id"]
2034 except (neExceptions
.ConnectionFailed
, ksExceptions
.ClientException
,
2035 neExceptions
.NeutronException
, ConnectionError
) as e
:
2038 self
.neutron
.delete_sfc_port_chain(new_sfp
['port_chain']['id'])
2041 'Creation of Service Function Path failed, with '
2042 'subsequent deletion failure as well.')
2043 self
._format
_exception
(e
)
2045 def get_sfp(self
, sfp_id
):
2046 self
.logger
.debug(" Getting Service Function Path %s from VIM", sfp_id
)
2047 filter_dict
= {"id": sfp_id
}
2048 sfp_list
= self
.get_sfp_list(filter_dict
)
2049 if len(sfp_list
) == 0:
2050 raise vimconn
.vimconnNotFoundException(
2051 "Service Function Path '{}' not found".format(sfp_id
))
2052 elif len(sfp_list
) > 1:
2053 raise vimconn
.vimconnConflictException(
2054 "Found more than one Service Function Path with this criteria")
2058 def get_sfp_list(self
, filter_dict
={}):
2059 self
.logger
.debug("Getting Service Function Paths from VIM filter: "
2060 "'%s'", str(filter_dict
))
2062 self
._reload
_connection
()
2063 filter_dict_os
= filter_dict
.copy()
2064 if self
.api_version3
and "tenant_id" in filter_dict_os
:
2065 filter_dict_os
['project_id'] = filter_dict_os
.pop('tenant_id')
2066 sfp_dict
= self
.neutron
.list_sfc_port_chains(**filter_dict_os
)
2067 sfp_list
= sfp_dict
["port_chains"]
2068 self
.__sfp
_os
2mano
(sfp_list
)
2070 except (neExceptions
.ConnectionFailed
, ksExceptions
.ClientException
,
2071 neExceptions
.NeutronException
, ConnectionError
) as e
:
2072 self
._format
_exception
(e
)
2074 def delete_sfp(self
, sfp_id
):
2076 "Deleting Service Function Path '%s' from VIM", sfp_id
)
2078 self
._reload
_connection
()
2079 self
.neutron
.delete_sfc_port_chain(sfp_id
)
2081 except (neExceptions
.ConnectionFailed
, neExceptions
.NeutronException
,
2082 ksExceptions
.ClientException
, neExceptions
.NeutronException
,
2083 ConnectionError
) as e
:
2084 self
._format
_exception
(e
)