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$"
48 from novaclient
import client
as nClient
, exceptions
as nvExceptions
49 from keystoneauth1
.identity
import v2
, v3
50 from keystoneauth1
import session
51 import keystoneclient
.exceptions
as ksExceptions
52 import keystoneclient
.v3
.client
as ksClient_v3
53 import keystoneclient
.v2_0
.client
as ksClient_v2
54 from glanceclient
import client
as glClient
55 import glanceclient
.exc
as gl1Exceptions
56 from cinderclient
import client
as cClient
57 from httplib
import HTTPException
58 from neutronclient
.neutron
import client
as neClient
59 from neutronclient
.common
import exceptions
as neExceptions
60 from requests
.exceptions
import ConnectionError
63 """contain the openstack virtual machine status to openmano status"""
64 vmStatus2manoFormat
={'ACTIVE':'ACTIVE',
66 'SUSPENDED': 'SUSPENDED',
69 'ERROR':'ERROR','DELETED':'DELETED'
71 netStatus2manoFormat
={'ACTIVE':'ACTIVE','PAUSED':'PAUSED','INACTIVE':'INACTIVE','BUILD':'BUILD','ERROR':'ERROR','DELETED':'DELETED'
74 supportedClassificationTypes
= ['legacy_flow_classifier']
76 #global var to have a timeout creating and deleting volumes
80 class vimconnector(vimconn
.vimconnector
):
81 def __init__(self
, uuid
, name
, tenant_id
, tenant_name
, url
, url_admin
=None, user
=None, passwd
=None,
82 log_level
=None, config
={}, persistent_info
={}):
83 '''using common constructor parameters. In this case
84 'url' is the keystone authorization url,
85 'url_admin' is not use
87 api_version
= config
.get('APIversion')
88 if api_version
and api_version
not in ('v3.3', 'v2.0', '2', '3'):
89 raise vimconn
.vimconnException("Invalid value '{}' for config:APIversion. "
90 "Allowed values are 'v3.3', 'v2.0', '2' or '3'".format(api_version
))
91 vim_type
= config
.get('vim_type')
92 if vim_type
and vim_type
not in ('vio', 'VIO'):
93 raise vimconn
.vimconnException("Invalid value '{}' for config:vim_type."
94 "Allowed values are 'vio' or 'VIO'".format(vim_type
))
96 if config
.get('dataplane_net_vlan_range') is not None:
97 #validate vlan ranges provided by user
98 self
._validate
_vlan
_ranges
(config
.get('dataplane_net_vlan_range'))
100 vimconn
.vimconnector
.__init
__(self
, uuid
, name
, tenant_id
, tenant_name
, url
, url_admin
, user
, passwd
, log_level
,
103 if self
.config
.get("insecure") and self
.config
.get("ca_cert"):
104 raise vimconn
.vimconnException("options insecure and ca_cert are mutually exclusive")
106 if self
.config
.get("insecure"):
108 if self
.config
.get("ca_cert"):
109 self
.verify
= self
.config
.get("ca_cert")
112 raise TypeError('url param can not be NoneType')
113 self
.persistent_info
= persistent_info
114 self
.availability_zone
= persistent_info
.get('availability_zone', None)
115 self
.session
= persistent_info
.get('session', {'reload_client': True})
116 self
.nova
= self
.session
.get('nova')
117 self
.neutron
= self
.session
.get('neutron')
118 self
.cinder
= self
.session
.get('cinder')
119 self
.glance
= self
.session
.get('glance')
120 # self.glancev1 = self.session.get('glancev1')
121 self
.keystone
= self
.session
.get('keystone')
122 self
.api_version3
= self
.session
.get('api_version3')
123 self
.vim_type
= self
.config
.get("vim_type")
125 self
.vim_type
= self
.vim_type
.upper()
126 if self
.config
.get("use_internal_endpoint"):
127 self
.endpoint_type
= "internalURL"
129 self
.endpoint_type
= None
131 self
.logger
= logging
.getLogger('openmano.vim.openstack')
133 ####### VIO Specific Changes #########
134 if self
.vim_type
== "VIO":
135 self
.logger
= logging
.getLogger('openmano.vim.vio')
138 self
.logger
.setLevel( getattr(logging
, log_level
))
140 def __getitem__(self
, index
):
141 """Get individuals parameters.
143 if index
== 'project_domain_id':
144 return self
.config
.get("project_domain_id")
145 elif index
== 'user_domain_id':
146 return self
.config
.get("user_domain_id")
148 return vimconn
.vimconnector
.__getitem
__(self
, index
)
150 def __setitem__(self
, index
, value
):
151 """Set individuals parameters and it is marked as dirty so to force connection reload.
153 if index
== 'project_domain_id':
154 self
.config
["project_domain_id"] = value
155 elif index
== 'user_domain_id':
156 self
.config
["user_domain_id"] = value
158 vimconn
.vimconnector
.__setitem
__(self
, index
, value
)
159 self
.session
['reload_client'] = True
161 def _reload_connection(self
):
162 '''Called before any operation, it check if credentials has changed
163 Throw keystoneclient.apiclient.exceptions.AuthorizationFailure
165 #TODO control the timing and possible token timeout, but it seams that python client does this task for us :-)
166 if self
.session
['reload_client']:
167 if self
.config
.get('APIversion'):
168 self
.api_version3
= self
.config
['APIversion'] == 'v3.3' or self
.config
['APIversion'] == '3'
169 else: # get from ending auth_url that end with v3 or with v2.0
170 self
.api_version3
= self
.url
.endswith("/v3") or self
.url
.endswith("/v3/")
171 self
.session
['api_version3'] = self
.api_version3
172 if self
.api_version3
:
173 if self
.config
.get('project_domain_id') or self
.config
.get('project_domain_name'):
174 project_domain_id_default
= None
176 project_domain_id_default
= 'default'
177 if self
.config
.get('user_domain_id') or self
.config
.get('user_domain_name'):
178 user_domain_id_default
= None
180 user_domain_id_default
= 'default'
181 auth
= v3
.Password(auth_url
=self
.url
,
183 password
=self
.passwd
,
184 project_name
=self
.tenant_name
,
185 project_id
=self
.tenant_id
,
186 project_domain_id
=self
.config
.get('project_domain_id', project_domain_id_default
),
187 user_domain_id
=self
.config
.get('user_domain_id', user_domain_id_default
),
188 project_domain_name
=self
.config
.get('project_domain_name'),
189 user_domain_name
=self
.config
.get('user_domain_name'))
191 auth
= v2
.Password(auth_url
=self
.url
,
193 password
=self
.passwd
,
194 tenant_name
=self
.tenant_name
,
195 tenant_id
=self
.tenant_id
)
196 sess
= session
.Session(auth
=auth
, verify
=self
.verify
)
197 if self
.api_version3
:
198 self
.keystone
= ksClient_v3
.Client(session
=sess
, endpoint_type
=self
.endpoint_type
)
200 self
.keystone
= ksClient_v2
.Client(session
=sess
, endpoint_type
=self
.endpoint_type
)
201 self
.session
['keystone'] = self
.keystone
202 # In order to enable microversion functionality an explicit microversion must be specified in 'config'.
203 # This implementation approach is due to the warning message in
204 # https://developer.openstack.org/api-guide/compute/microversions.html
205 # where it is stated that microversion backwards compatibility is not guaranteed and clients should
206 # always require an specific microversion.
207 # To be able to use 'device role tagging' functionality define 'microversion: 2.32' in datacenter config
208 version
= self
.config
.get("microversion")
211 self
.nova
= self
.session
['nova'] = nClient
.Client(str(version
), session
=sess
, endpoint_type
=self
.endpoint_type
)
212 self
.neutron
= self
.session
['neutron'] = neClient
.Client('2.0', session
=sess
, endpoint_type
=self
.endpoint_type
)
213 self
.cinder
= self
.session
['cinder'] = cClient
.Client(2, session
=sess
, endpoint_type
=self
.endpoint_type
)
214 if self
.endpoint_type
== "internalURL":
215 glance_service_id
= self
.keystone
.services
.list(name
="glance")[0].id
216 glance_endpoint
= self
.keystone
.endpoints
.list(glance_service_id
, interface
="internal")[0].url
218 glance_endpoint
= None
219 self
.glance
= self
.session
['glance'] = glClient
.Client(2, session
=sess
, endpoint
=glance_endpoint
)
220 #using version 1 of glance client in new_image()
221 # self.glancev1 = self.session['glancev1'] = glClient.Client('1', session=sess,
222 # endpoint=glance_endpoint)
223 self
.session
['reload_client'] = False
224 self
.persistent_info
['session'] = self
.session
225 # add availablity zone info inside self.persistent_info
226 self
._set
_availablity
_zones
()
227 self
.persistent_info
['availability_zone'] = self
.availability_zone
229 def __net_os2mano(self
, net_list_dict
):
230 '''Transform the net openstack format to mano format
231 net_list_dict can be a list of dict or a single dict'''
232 if type(net_list_dict
) is dict:
233 net_list_
=(net_list_dict
,)
234 elif type(net_list_dict
) is list:
235 net_list_
=net_list_dict
237 raise TypeError("param net_list_dict must be a list or a dictionary")
238 for net
in net_list_
:
239 if net
.get('provider:network_type') == "vlan":
244 def __classification_os2mano(self
, class_list_dict
):
245 """Transform the openstack format (Flow Classifier) to mano format
246 (Classification) class_list_dict can be a list of dict or a single dict
248 if isinstance(class_list_dict
, dict):
249 class_list_
= [class_list_dict
]
250 elif isinstance(class_list_dict
, list):
251 class_list_
= class_list_dict
254 "param class_list_dict must be a list or a dictionary")
255 for classification
in class_list_
:
256 id = classification
.pop('id')
257 name
= classification
.pop('name')
258 description
= classification
.pop('description')
259 project_id
= classification
.pop('project_id')
260 tenant_id
= classification
.pop('tenant_id')
261 original_classification
= copy
.deepcopy(classification
)
262 classification
.clear()
263 classification
['ctype'] = 'legacy_flow_classifier'
264 classification
['definition'] = original_classification
265 classification
['id'] = id
266 classification
['name'] = name
267 classification
['description'] = description
268 classification
['project_id'] = project_id
269 classification
['tenant_id'] = tenant_id
271 def __sfi_os2mano(self
, sfi_list_dict
):
272 """Transform the openstack format (Port Pair) to mano format (SFI)
273 sfi_list_dict can be a list of dict or a single dict
275 if isinstance(sfi_list_dict
, dict):
276 sfi_list_
= [sfi_list_dict
]
277 elif isinstance(sfi_list_dict
, list):
278 sfi_list_
= sfi_list_dict
281 "param sfi_list_dict must be a list or a dictionary")
282 for sfi
in sfi_list_
:
283 sfi
['ingress_ports'] = []
284 sfi
['egress_ports'] = []
285 if sfi
.get('ingress'):
286 sfi
['ingress_ports'].append(sfi
['ingress'])
287 if sfi
.get('egress'):
288 sfi
['egress_ports'].append(sfi
['egress'])
291 params
= sfi
.get('service_function_parameters')
294 correlation
= params
.get('correlation')
297 sfi
['sfc_encap'] = sfc_encap
298 del sfi
['service_function_parameters']
300 def __sf_os2mano(self
, sf_list_dict
):
301 """Transform the openstack format (Port Pair Group) to mano format (SF)
302 sf_list_dict can be a list of dict or a single dict
304 if isinstance(sf_list_dict
, dict):
305 sf_list_
= [sf_list_dict
]
306 elif isinstance(sf_list_dict
, list):
307 sf_list_
= sf_list_dict
310 "param sf_list_dict must be a list or a dictionary")
312 del sf
['port_pair_group_parameters']
313 sf
['sfis'] = sf
['port_pairs']
316 def __sfp_os2mano(self
, sfp_list_dict
):
317 """Transform the openstack format (Port Chain) to mano format (SFP)
318 sfp_list_dict can be a list of dict or a single dict
320 if isinstance(sfp_list_dict
, dict):
321 sfp_list_
= [sfp_list_dict
]
322 elif isinstance(sfp_list_dict
, list):
323 sfp_list_
= sfp_list_dict
326 "param sfp_list_dict must be a list or a dictionary")
327 for sfp
in sfp_list_
:
328 params
= sfp
.pop('chain_parameters')
331 correlation
= params
.get('correlation')
334 sfp
['sfc_encap'] = sfc_encap
335 sfp
['spi'] = sfp
.pop('chain_id')
336 sfp
['classifications'] = sfp
.pop('flow_classifiers')
337 sfp
['service_functions'] = sfp
.pop('port_pair_groups')
339 # placeholder for now; read TODO note below
340 def _validate_classification(self
, type, definition
):
341 # only legacy_flow_classifier Type is supported at this point
343 # TODO(igordcard): this method should be an abstract method of an
344 # abstract Classification class to be implemented by the specific
345 # Types. Also, abstract vimconnector should call the validation
346 # method before the implemented VIM connectors are called.
348 def _format_exception(self
, exception
):
349 '''Transform a keystone, nova, neutron exception into a vimconn exception'''
350 if isinstance(exception
, (HTTPException
, gl1Exceptions
.HTTPException
, gl1Exceptions
.CommunicationError
,
351 ConnectionError
, ksExceptions
.ConnectionError
, neExceptions
.ConnectionFailed
353 raise vimconn
.vimconnConnectionException(type(exception
).__name
__ + ": " + str(exception
))
354 elif isinstance(exception
, (nvExceptions
.ClientException
, ksExceptions
.ClientException
,
355 neExceptions
.NeutronException
, nvExceptions
.BadRequest
)):
356 if "could not be found" in str(exception
):
357 raise vimconn
.vimconnNotFoundException(type(exception
).__name
__ + ": " + str(exception
))
358 raise vimconn
.vimconnUnexpectedResponse(type(exception
).__name
__ + ": " + str(exception
))
359 elif isinstance(exception
, (neExceptions
.NetworkNotFoundClient
, nvExceptions
.NotFound
)):
360 raise vimconn
.vimconnNotFoundException(type(exception
).__name
__ + ": " + str(exception
))
361 elif isinstance(exception
, nvExceptions
.Conflict
):
362 raise vimconn
.vimconnConflictException(type(exception
).__name
__ + ": " + str(exception
))
363 elif isinstance(exception
, vimconn
.vimconnException
):
366 self
.logger
.error("General Exception " + str(exception
), exc_info
=True)
367 raise vimconn
.vimconnConnectionException(type(exception
).__name
__ + ": " + str(exception
))
369 def get_tenant_list(self
, filter_dict
={}):
370 '''Obtain tenants of VIM
371 filter_dict can contain the following keys:
372 name: filter by tenant name
373 id: filter by tenant uuid/id
375 Returns the tenant list of dictionaries: [{'name':'<name>, 'id':'<id>, ...}, ...]
377 self
.logger
.debug("Getting tenants from VIM filter: '%s'", str(filter_dict
))
379 self
._reload
_connection
()
380 if self
.api_version3
:
381 project_class_list
= self
.keystone
.projects
.list(name
=filter_dict
.get("name"))
383 project_class_list
= self
.keystone
.tenants
.findall(**filter_dict
)
385 for project
in project_class_list
:
386 if filter_dict
.get('id') and filter_dict
["id"] != project
.id:
388 project_list
.append(project
.to_dict())
390 except (ksExceptions
.ConnectionError
, ksExceptions
.ClientException
, ConnectionError
) as e
:
391 self
._format
_exception
(e
)
393 def new_tenant(self
, tenant_name
, tenant_description
):
394 '''Adds a new tenant to openstack VIM. Returns the tenant identifier'''
395 self
.logger
.debug("Adding a new tenant name: %s", tenant_name
)
397 self
._reload
_connection
()
398 if self
.api_version3
:
399 project
= self
.keystone
.projects
.create(tenant_name
, self
.config
.get("project_domain_id", "default"),
400 description
=tenant_description
, is_domain
=False)
402 project
= self
.keystone
.tenants
.create(tenant_name
, tenant_description
)
404 except (ksExceptions
.ConnectionError
, ksExceptions
.ClientException
, ConnectionError
) as e
:
405 self
._format
_exception
(e
)
407 def delete_tenant(self
, tenant_id
):
408 '''Delete a tenant from openstack VIM. Returns the old tenant identifier'''
409 self
.logger
.debug("Deleting tenant %s from VIM", tenant_id
)
411 self
._reload
_connection
()
412 if self
.api_version3
:
413 self
.keystone
.projects
.delete(tenant_id
)
415 self
.keystone
.tenants
.delete(tenant_id
)
417 except (ksExceptions
.ConnectionError
, ksExceptions
.ClientException
, ConnectionError
) as e
:
418 self
._format
_exception
(e
)
420 def new_network(self
,net_name
, net_type
, ip_profile
=None, shared
=False, vlan
=None):
421 '''Adds a tenant network to VIM. Returns the network identifier'''
422 self
.logger
.debug("Adding a new network to VIM name '%s', type '%s'", net_name
, net_type
)
423 #self.logger.debug(">>>>>>>>>>>>>>>>>> IP profile %s", str(ip_profile))
426 self
._reload
_connection
()
427 network_dict
= {'name': net_name
, 'admin_state_up': True}
428 if net_type
=="data" or net_type
=="ptp":
429 if self
.config
.get('dataplane_physical_net') == None:
430 raise vimconn
.vimconnConflictException("You must provide a 'dataplane_physical_net' at config value before creating sriov network")
431 network_dict
["provider:physical_network"] = self
.config
['dataplane_physical_net'] #"physnet_sriov" #TODO physical
432 network_dict
["provider:network_type"] = "vlan"
434 network_dict
["provider:network_type"] = vlan
436 ####### VIO Specific Changes #########
437 if self
.vim_type
== "VIO":
439 network_dict
["provider:segmentation_id"] = vlan
441 if self
.config
.get('dataplane_net_vlan_range') is None:
442 raise vimconn
.vimconnConflictException("You must provide "\
443 "'dataplane_net_vlan_range' in format [start_ID - end_ID]"\
444 "at config value before creating sriov network with vlan tag")
446 network_dict
["provider:segmentation_id"] = self
._genrate
_vlanID
()
448 network_dict
["shared"]=shared
449 new_net
=self
.neutron
.create_network({'network':network_dict
})
451 #create subnetwork, even if there is no profile
454 if not ip_profile
.get('subnet_address'):
455 #Fake subnet is required
456 subnet_rand
= random
.randint(0, 255)
457 ip_profile
['subnet_address'] = "192.168.{}.0/24".format(subnet_rand
)
458 if 'ip_version' not in ip_profile
:
459 ip_profile
['ip_version'] = "IPv4"
460 subnet
= {"name":net_name
+"-subnet",
461 "network_id": new_net
["network"]["id"],
462 "ip_version": 4 if ip_profile
['ip_version']=="IPv4" else 6,
463 "cidr": ip_profile
['subnet_address']
465 # Gateway should be set to None if not needed. Otherwise openstack assigns one by default
466 if ip_profile
.get('gateway_address'):
467 subnet
['gateway_ip'] = ip_profile
['gateway_address']
469 subnet
['gateway_ip'] = None
470 if ip_profile
.get('dns_address'):
471 subnet
['dns_nameservers'] = ip_profile
['dns_address'].split(";")
472 if 'dhcp_enabled' in ip_profile
:
473 subnet
['enable_dhcp'] = False if \
474 ip_profile
['dhcp_enabled']=="false" or ip_profile
['dhcp_enabled']==False else True
475 if ip_profile
.get('dhcp_start_address'):
476 subnet
['allocation_pools'] = []
477 subnet
['allocation_pools'].append(dict())
478 subnet
['allocation_pools'][0]['start'] = ip_profile
['dhcp_start_address']
479 if ip_profile
.get('dhcp_count'):
480 #parts = ip_profile['dhcp_start_address'].split('.')
481 #ip_int = (int(parts[0]) << 24) + (int(parts[1]) << 16) + (int(parts[2]) << 8) + int(parts[3])
482 ip_int
= int(netaddr
.IPAddress(ip_profile
['dhcp_start_address']))
483 ip_int
+= ip_profile
['dhcp_count'] - 1
484 ip_str
= str(netaddr
.IPAddress(ip_int
))
485 subnet
['allocation_pools'][0]['end'] = ip_str
486 #self.logger.debug(">>>>>>>>>>>>>>>>>> Subnet: %s", str(subnet))
487 self
.neutron
.create_subnet({"subnet": subnet
} )
488 return new_net
["network"]["id"]
489 except Exception as e
:
491 self
.neutron
.delete_network(new_net
['network']['id'])
492 self
._format
_exception
(e
)
494 def get_network_list(self
, filter_dict
={}):
495 '''Obtain tenant networks of VIM
501 admin_state_up: boolean
503 Returns the network list of dictionaries
505 self
.logger
.debug("Getting network from VIM filter: '%s'", str(filter_dict
))
507 self
._reload
_connection
()
508 filter_dict_os
= filter_dict
.copy()
509 if self
.api_version3
and "tenant_id" in filter_dict_os
:
510 filter_dict_os
['project_id'] = filter_dict_os
.pop('tenant_id') #T ODO check
511 net_dict
= self
.neutron
.list_networks(**filter_dict_os
)
512 net_list
= net_dict
["networks"]
513 self
.__net
_os
2mano
(net_list
)
515 except (neExceptions
.ConnectionFailed
, ksExceptions
.ClientException
, neExceptions
.NeutronException
, ConnectionError
) as e
:
516 self
._format
_exception
(e
)
518 def get_network(self
, net_id
):
519 '''Obtain details of network from VIM
520 Returns the network information from a network id'''
521 self
.logger
.debug(" Getting tenant network %s from VIM", net_id
)
522 filter_dict
={"id": net_id
}
523 net_list
= self
.get_network_list(filter_dict
)
525 raise vimconn
.vimconnNotFoundException("Network '{}' not found".format(net_id
))
526 elif len(net_list
)>1:
527 raise vimconn
.vimconnConflictException("Found more than one network with this criteria")
530 for subnet_id
in net
.get("subnets", () ):
532 subnet
= self
.neutron
.show_subnet(subnet_id
)
533 except Exception as e
:
534 self
.logger
.error("osconnector.get_network(): Error getting subnet %s %s" % (net_id
, str(e
)))
535 subnet
= {"id": subnet_id
, "fault": str(e
)}
536 subnets
.append(subnet
)
537 net
["subnets"] = subnets
538 net
["encapsulation"] = net
.get('provider:network_type')
539 net
["segmentation_id"] = net
.get('provider:segmentation_id')
542 def delete_network(self
, net_id
):
543 '''Deletes a tenant network from VIM. Returns the old network identifier'''
544 self
.logger
.debug("Deleting network '%s' from VIM", net_id
)
546 self
._reload
_connection
()
547 #delete VM ports attached to this networks before the network
548 ports
= self
.neutron
.list_ports(network_id
=net_id
)
549 for p
in ports
['ports']:
551 self
.neutron
.delete_port(p
["id"])
552 except Exception as e
:
553 self
.logger
.error("Error deleting port %s: %s", p
["id"], str(e
))
554 self
.neutron
.delete_network(net_id
)
556 except (neExceptions
.ConnectionFailed
, neExceptions
.NetworkNotFoundClient
, neExceptions
.NeutronException
,
557 ksExceptions
.ClientException
, neExceptions
.NeutronException
, ConnectionError
) as e
:
558 self
._format
_exception
(e
)
560 def refresh_nets_status(self
, net_list
):
561 '''Get the status of the networks
562 Params: the list of network identifiers
563 Returns a dictionary with:
564 net_id: #VIM id of this network
565 status: #Mandatory. Text with one of:
566 # DELETED (not found at vim)
567 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
568 # OTHER (Vim reported other status not understood)
569 # ERROR (VIM indicates an ERROR status)
570 # ACTIVE, INACTIVE, DOWN (admin down),
571 # BUILD (on building process)
573 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
574 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
578 for net_id
in net_list
:
581 net_vim
= self
.get_network(net_id
)
582 if net_vim
['status'] in netStatus2manoFormat
:
583 net
["status"] = netStatus2manoFormat
[ net_vim
['status'] ]
585 net
["status"] = "OTHER"
586 net
["error_msg"] = "VIM status reported " + net_vim
['status']
588 if net
['status'] == "ACTIVE" and not net_vim
['admin_state_up']:
589 net
['status'] = 'DOWN'
591 net
['vim_info'] = yaml
.safe_dump(net_vim
, default_flow_style
=True, width
=256)
592 except yaml
.representer
.RepresenterError
:
593 net
['vim_info'] = str(net_vim
)
594 if net_vim
.get('fault'): #TODO
595 net
['error_msg'] = str(net_vim
['fault'])
596 except vimconn
.vimconnNotFoundException
as e
:
597 self
.logger
.error("Exception getting net status: %s", str(e
))
598 net
['status'] = "DELETED"
599 net
['error_msg'] = str(e
)
600 except vimconn
.vimconnException
as e
:
601 self
.logger
.error("Exception getting net status: %s", str(e
))
602 net
['status'] = "VIM_ERROR"
603 net
['error_msg'] = str(e
)
604 net_dict
[net_id
] = net
607 def get_flavor(self
, flavor_id
):
608 '''Obtain flavor details from the VIM. Returns the flavor dict details'''
609 self
.logger
.debug("Getting flavor '%s'", flavor_id
)
611 self
._reload
_connection
()
612 flavor
= self
.nova
.flavors
.find(id=flavor_id
)
613 #TODO parse input and translate to VIM format (openmano_schemas.new_vminstance_response_schema)
614 return flavor
.to_dict()
615 except (nvExceptions
.NotFound
, nvExceptions
.ClientException
, ksExceptions
.ClientException
, ConnectionError
) as e
:
616 self
._format
_exception
(e
)
618 def get_flavor_id_from_data(self
, flavor_dict
):
619 """Obtain flavor id that match the flavor description
620 Returns the flavor_id or raises a vimconnNotFoundException
621 flavor_dict: contains the required ram, vcpus, disk
622 If 'use_existing_flavors' is set to True at config, the closer flavor that provides same or more ram, vcpus
623 and disk is returned. Otherwise a flavor with exactly same ram, vcpus and disk is returned or a
624 vimconnNotFoundException is raised
626 exact_match
= False if self
.config
.get('use_existing_flavors') else True
628 self
._reload
_connection
()
629 flavor_candidate_id
= None
630 flavor_candidate_data
= (10000, 10000, 10000)
631 flavor_target
= (flavor_dict
["ram"], flavor_dict
["vcpus"], flavor_dict
["disk"])
633 numas
= flavor_dict
.get("extended", {}).get("numas")
636 raise vimconn
.vimconnNotFoundException("Flavor with EPA still not implemted")
638 # raise vimconn.vimconnNotFoundException("Cannot find any flavor with more than one numa")
640 # numas = extended.get("numas")
641 for flavor
in self
.nova
.flavors
.list():
642 epa
= flavor
.get_keys()
646 flavor_data
= (flavor
.ram
, flavor
.vcpus
, flavor
.disk
)
647 if flavor_data
== flavor_target
:
649 elif not exact_match
and flavor_target
< flavor_data
< flavor_candidate_data
:
650 flavor_candidate_id
= flavor
.id
651 flavor_candidate_data
= flavor_data
652 if not exact_match
and flavor_candidate_id
:
653 return flavor_candidate_id
654 raise vimconn
.vimconnNotFoundException("Cannot find any flavor matching '{}'".format(str(flavor_dict
)))
655 except (nvExceptions
.NotFound
, nvExceptions
.ClientException
, ksExceptions
.ClientException
, ConnectionError
) as e
:
656 self
._format
_exception
(e
)
658 def new_flavor(self
, flavor_data
, change_name_if_used
=True):
659 '''Adds a tenant flavor to openstack VIM
660 if change_name_if_used is True, it will change name in case of conflict, because it is not supported name repetition
661 Returns the flavor identifier
663 self
.logger
.debug("Adding flavor '%s'", str(flavor_data
))
667 name
=flavor_data
['name']
668 while retry
<max_retries
:
671 self
._reload
_connection
()
672 if change_name_if_used
:
675 fl
=self
.nova
.flavors
.list()
677 fl_names
.append(f
.name
)
678 while name
in fl_names
:
680 name
= flavor_data
['name']+"-" + str(name_suffix
)
682 ram
= flavor_data
.get('ram',64)
683 vcpus
= flavor_data
.get('vcpus',1)
686 extended
= flavor_data
.get("extended")
688 numas
=extended
.get("numas")
690 numa_nodes
= len(numas
)
692 return -1, "Can not add flavor with more than one numa"
693 numa_properties
= {"hw:numa_nodes":str(numa_nodes
)}
694 numa_properties
["hw:mem_page_size"] = "large"
695 numa_properties
["hw:cpu_policy"] = "dedicated"
696 numa_properties
["hw:numa_mempolicy"] = "strict"
697 if self
.vim_type
== "VIO":
698 numa_properties
["vmware:extra_config"] = '{"numa.nodeAffinity":"0"}'
699 numa_properties
["vmware:latency_sensitivity_level"] = "high"
701 #overwrite ram and vcpus
702 #check if key 'memory' is present in numa else use ram value at flavor
704 ram
= numa
['memory']*1024
705 #See for reference: https://specs.openstack.org/openstack/nova-specs/specs/mitaka/implemented/virt-driver-cpu-thread-pinning.html
706 if 'paired-threads' in numa
:
707 vcpus
= numa
['paired-threads']*2
708 #cpu_thread_policy "require" implies that the compute node must have an STM architecture
709 numa_properties
["hw:cpu_thread_policy"] = "require"
710 numa_properties
["hw:cpu_policy"] = "dedicated"
711 elif 'cores' in numa
:
712 vcpus
= numa
['cores']
713 # cpu_thread_policy "prefer" implies that the host must not have an SMT architecture, or a non-SMT architecture will be emulated
714 numa_properties
["hw:cpu_thread_policy"] = "isolate"
715 numa_properties
["hw:cpu_policy"] = "dedicated"
716 elif 'threads' in numa
:
717 vcpus
= numa
['threads']
718 # cpu_thread_policy "prefer" implies that the host may or may not have an SMT architecture
719 numa_properties
["hw:cpu_thread_policy"] = "prefer"
720 numa_properties
["hw:cpu_policy"] = "dedicated"
721 # for interface in numa.get("interfaces",() ):
722 # if interface["dedicated"]=="yes":
723 # raise vimconn.vimconnException("Passthrough interfaces are not supported for the openstack connector", http_code=vimconn.HTTP_Service_Unavailable)
724 # #TODO, add the key 'pci_passthrough:alias"="<label at config>:<number ifaces>"' when a way to connect it is available
727 new_flavor
=self
.nova
.flavors
.create(name
,
730 flavor_data
.get('disk',0),
731 is_public
=flavor_data
.get('is_public', True)
735 new_flavor
.set_keys(numa_properties
)
737 except nvExceptions
.Conflict
as e
:
738 if change_name_if_used
and retry
< max_retries
:
740 self
._format
_exception
(e
)
741 #except nvExceptions.BadRequest as e:
742 except (ksExceptions
.ClientException
, nvExceptions
.ClientException
, ConnectionError
) as e
:
743 self
._format
_exception
(e
)
745 def delete_flavor(self
,flavor_id
):
746 '''Deletes a tenant flavor from openstack VIM. Returns the old flavor_id
749 self
._reload
_connection
()
750 self
.nova
.flavors
.delete(flavor_id
)
752 #except nvExceptions.BadRequest as e:
753 except (nvExceptions
.NotFound
, ksExceptions
.ClientException
, nvExceptions
.ClientException
, ConnectionError
) as e
:
754 self
._format
_exception
(e
)
756 def new_image(self
,image_dict
):
758 Adds a tenant image to VIM. imge_dict is a dictionary with:
760 disk_format: qcow2, vhd, vmdk, raw (by default), ...
761 location: path or URI
762 public: "yes" or "no"
763 metadata: metadata of the image
768 while retry
<max_retries
:
771 self
._reload
_connection
()
772 #determine format http://docs.openstack.org/developer/glance/formats.html
773 if "disk_format" in image_dict
:
774 disk_format
=image_dict
["disk_format"]
775 else: #autodiscover based on extension
776 if image_dict
['location'].endswith(".qcow2"):
778 elif image_dict
['location'].endswith(".vhd"):
780 elif image_dict
['location'].endswith(".vmdk"):
782 elif image_dict
['location'].endswith(".vdi"):
784 elif image_dict
['location'].endswith(".iso"):
786 elif image_dict
['location'].endswith(".aki"):
788 elif image_dict
['location'].endswith(".ari"):
790 elif image_dict
['location'].endswith(".ami"):
794 self
.logger
.debug("new_image: '%s' loading from '%s'", image_dict
['name'], image_dict
['location'])
795 new_image
= self
.glance
.images
.create(name
=image_dict
['name'])
796 if image_dict
['location'].startswith("http"):
797 # TODO there is not a method to direct download. It must be downloaded locally with requests
798 raise vimconn
.vimconnNotImplemented("Cannot create image from URL")
800 with
open(image_dict
['location']) as fimage
:
801 self
.glance
.images
.upload(new_image
.id, fimage
)
802 #new_image = self.glancev1.images.create(name=image_dict['name'], is_public=image_dict.get('public',"yes")=="yes",
803 # container_format="bare", data=fimage, disk_format=disk_format)
804 metadata_to_load
= image_dict
.get('metadata')
805 #TODO location is a reserved word for current openstack versions. Use another word
806 metadata_to_load
['location'] = image_dict
['location']
807 self
.glance
.images
.update(new_image
.id, **metadata_to_load
)
809 except (nvExceptions
.Conflict
, ksExceptions
.ClientException
, nvExceptions
.ClientException
) as e
:
810 self
._format
_exception
(e
)
811 except (HTTPException
, gl1Exceptions
.HTTPException
, gl1Exceptions
.CommunicationError
, ConnectionError
) as e
:
812 if retry
==max_retries
:
814 self
._format
_exception
(e
)
815 except IOError as e
: #can not open the file
816 raise vimconn
.vimconnConnectionException(type(e
).__name
__ + ": " + str(e
)+ " for " + image_dict
['location'],
817 http_code
=vimconn
.HTTP_Bad_Request
)
819 def delete_image(self
, image_id
):
820 '''Deletes a tenant image from openstack VIM. Returns the old id
823 self
._reload
_connection
()
824 self
.glance
.images
.delete(image_id
)
826 except (nvExceptions
.NotFound
, ksExceptions
.ClientException
, nvExceptions
.ClientException
, gl1Exceptions
.CommunicationError
, ConnectionError
) as e
: #TODO remove
827 self
._format
_exception
(e
)
829 def get_image_id_from_path(self
, path
):
830 '''Get the image id from image path in the VIM database. Returns the image_id'''
832 self
._reload
_connection
()
833 images
= self
.glance
.images
.list()
835 if image
.metadata
.get("location")==path
:
837 raise vimconn
.vimconnNotFoundException("image with location '{}' not found".format( path
))
838 except (ksExceptions
.ClientException
, nvExceptions
.ClientException
, gl1Exceptions
.CommunicationError
, ConnectionError
) as e
:
839 self
._format
_exception
(e
)
841 def get_image_list(self
, filter_dict
={}):
842 '''Obtain tenant images from VIM
846 checksum: image checksum
847 Returns the image list of dictionaries:
848 [{<the fields at Filter_dict plus some VIM specific>}, ...]
851 self
.logger
.debug("Getting image list from VIM filter: '%s'", str(filter_dict
))
853 self
._reload
_connection
()
854 filter_dict_os
= filter_dict
.copy()
855 #First we filter by the available filter fields: name, id. The others are removed.
856 image_list
= self
.glance
.images
.list()
858 for image
in image_list
:
860 if filter_dict
.get("name") and image
["name"] != filter_dict
["name"]:
862 if filter_dict
.get("id") and image
["id"] != filter_dict
["id"]:
864 if filter_dict
.get("checksum") and image
["checksum"] != filter_dict
["checksum"]:
867 filtered_list
.append(image
.copy())
868 except gl1Exceptions
.HTTPNotFound
:
871 except (ksExceptions
.ClientException
, nvExceptions
.ClientException
, gl1Exceptions
.CommunicationError
, ConnectionError
) as e
:
872 self
._format
_exception
(e
)
874 def __wait_for_vm(self
, vm_id
, status
):
875 """wait until vm is in the desired status and return True.
876 If the VM gets in ERROR status, return false.
877 If the timeout is reached generate an exception"""
879 while elapsed_time
< server_timeout
:
880 vm_status
= self
.nova
.servers
.get(vm_id
).status
881 if vm_status
== status
:
883 if vm_status
== 'ERROR':
888 # if we exceeded the timeout rollback
889 if elapsed_time
>= server_timeout
:
890 raise vimconn
.vimconnException('Timeout waiting for instance ' + vm_id
+ ' to get ' + status
,
891 http_code
=vimconn
.HTTP_Request_Timeout
)
893 def _get_openstack_availablity_zones(self
):
895 Get from openstack availability zones available
899 openstack_availability_zone
= self
.nova
.availability_zones
.list()
900 openstack_availability_zone
= [str(zone
.zoneName
) for zone
in openstack_availability_zone
901 if zone
.zoneName
!= 'internal']
902 return openstack_availability_zone
903 except Exception as e
:
906 def _set_availablity_zones(self
):
908 Set vim availablity zone
912 if 'availability_zone' in self
.config
:
913 vim_availability_zones
= self
.config
.get('availability_zone')
914 if isinstance(vim_availability_zones
, str):
915 self
.availability_zone
= [vim_availability_zones
]
916 elif isinstance(vim_availability_zones
, list):
917 self
.availability_zone
= vim_availability_zones
919 self
.availability_zone
= self
._get
_openstack
_availablity
_zones
()
921 def _get_vm_availability_zone(self
, availability_zone_index
, availability_zone_list
):
923 Return thge availability zone to be used by the created VM.
924 :return: The VIM availability zone to be used or None
926 if availability_zone_index
is None:
927 if not self
.config
.get('availability_zone'):
929 elif isinstance(self
.config
.get('availability_zone'), str):
930 return self
.config
['availability_zone']
932 # TODO consider using a different parameter at config for default AV and AV list match
933 return self
.config
['availability_zone'][0]
935 vim_availability_zones
= self
.availability_zone
936 # check if VIM offer enough availability zones describe in the VNFD
937 if vim_availability_zones
and len(availability_zone_list
) <= len(vim_availability_zones
):
938 # check if all the names of NFV AV match VIM AV names
939 match_by_index
= False
940 for av
in availability_zone_list
:
941 if av
not in vim_availability_zones
:
942 match_by_index
= True
945 return vim_availability_zones
[availability_zone_index
]
947 return availability_zone_list
[availability_zone_index
]
949 raise vimconn
.vimconnConflictException("No enough availability zones at VIM for this deployment")
951 def new_vminstance(self
, name
, description
, start
, image_id
, flavor_id
, net_list
, cloud_config
=None, disk_list
=None,
952 availability_zone_index
=None, availability_zone_list
=None):
953 """Adds a VM instance to VIM
955 start: indicates if VM must start or boot in pause mode. Ignored
956 image_id,flavor_id: iamge and flavor uuid
957 net_list: list of interfaces, each one is a dictionary with:
959 net_id: network uuid to connect
960 vpci: virtual vcpi to assign, ignored because openstack lack #TODO
961 model: interface model, ignored #TODO
962 mac_address: used for SR-IOV ifaces #TODO for other types
963 use: 'data', 'bridge', 'mgmt'
964 type: 'virtual', 'PCI-PASSTHROUGH'('PF'), 'SR-IOV'('VF'), 'VFnotShared'
965 vim_id: filled/added by this function
966 floating_ip: True/False (or it can be None)
967 'cloud_config': (optional) dictionary with:
968 'key-pairs': (optional) list of strings with the public key to be inserted to the default user
969 'users': (optional) list of users to be inserted, each item is a dict with:
970 'name': (mandatory) user name,
971 'key-pairs': (optional) list of strings with the public key to be inserted to the user
972 'user-data': (optional) string is a text script to be passed directly to cloud-init
973 'config-files': (optional). List of files to be transferred. Each item is a dict with:
974 'dest': (mandatory) string with the destination absolute path
975 'encoding': (optional, by default text). Can be one of:
976 'b64', 'base64', 'gz', 'gz+b64', 'gz+base64', 'gzip+b64', 'gzip+base64'
977 'content' (mandatory): string with the content of the file
978 'permissions': (optional) string with file permissions, typically octal notation '0644'
979 'owner': (optional) file owner, string with the format 'owner:group'
980 'boot-data-drive': boolean to indicate if user-data must be passed using a boot drive (hard disk)
981 'disk_list': (optional) list with additional disks to the VM. Each item is a dict with:
982 'image_id': (optional). VIM id of an existing image. If not provided an empty disk must be mounted
983 'size': (mandatory) string with the size of the disk in GB
984 'vim_id' (optional) should use this existing volume id
985 availability_zone_index: Index of availability_zone_list to use for this this VM. None if not AV required
986 availability_zone_list: list of availability zones given by user in the VNFD descriptor. Ignore if
987 availability_zone_index is None
988 #TODO ip, security groups
989 Returns a tuple with the instance identifier and created_items or raises an exception on error
990 created_items can be None or a dictionary where this method can include key-values that will be passed to
991 the method delete_vminstance and action_vminstance. Can be used to store created ports, volumes, etc.
992 Format is vimconnector dependent, but do not use nested dictionaries and a value of None should be the same
995 self
.logger
.debug("new_vminstance input: image='%s' flavor='%s' nics='%s'",image_id
, flavor_id
,str(net_list
))
1001 external_network
= [] # list of external networks to be connected to instance, later on used to create floating_ip
1002 no_secured_ports
= [] # List of port-is with port-security disabled
1003 self
._reload
_connection
()
1004 # metadata_vpci = {} # For a specific neutron plugin
1005 block_device_mapping
= None
1006 for net
in net_list
:
1007 if not net
.get("net_id"): # skip non connected iface
1011 "network_id": net
["net_id"],
1012 "name": net
.get("name"),
1013 "admin_state_up": True
1015 if net
["type"]=="virtual":
1018 # metadata_vpci[ net["net_id"] ] = [[ net["vpci"], "" ]]
1019 elif net
["type"] == "VF" or net
["type"] == "SR-IOV": # for VF
1021 # if "VF" not in metadata_vpci:
1022 # metadata_vpci["VF"]=[]
1023 # metadata_vpci["VF"].append([ net["vpci"], "" ])
1024 port_dict
["binding:vnic_type"]="direct"
1025 # VIO specific Changes
1026 if self
.vim_type
== "VIO":
1027 # Need to create port with port_security_enabled = False and no-security-groups
1028 port_dict
["port_security_enabled"]=False
1029 port_dict
["provider_security_groups"]=[]
1030 port_dict
["security_groups"]=[]
1031 else: # For PT PCI-PASSTHROUGH
1032 # VIO specific Changes
1033 # Current VIO release does not support port with type 'direct-physical'
1034 # So no need to create virtual port in case of PCI-device.
1035 # Will update port_dict code when support gets added in next VIO release
1036 if self
.vim_type
== "VIO":
1037 raise vimconn
.vimconnNotSupportedException(
1038 "Current VIO release does not support full passthrough (PT)")
1040 # if "PF" not in metadata_vpci:
1041 # metadata_vpci["PF"]=[]
1042 # metadata_vpci["PF"].append([ net["vpci"], "" ])
1043 port_dict
["binding:vnic_type"]="direct-physical"
1044 if not port_dict
["name"]:
1045 port_dict
["name"]=name
1046 if net
.get("mac_address"):
1047 port_dict
["mac_address"]=net
["mac_address"]
1048 if net
.get("ip_address"):
1049 port_dict
["fixed_ips"] = [{'ip_address': net
["ip_address"]}]
1050 # TODO add 'subnet_id': <subnet_id>
1051 new_port
= self
.neutron
.create_port({"port": port_dict
})
1052 created_items
["port:" + str(new_port
["port"]["id"])] = True
1053 net
["mac_adress"] = new_port
["port"]["mac_address"]
1054 net
["vim_id"] = new_port
["port"]["id"]
1055 # if try to use a network without subnetwork, it will return a emtpy list
1056 fixed_ips
= new_port
["port"].get("fixed_ips")
1058 net
["ip"] = fixed_ips
[0].get("ip_address")
1062 port
= {"port-id": new_port
["port"]["id"]}
1063 if float(self
.nova
.api_version
.get_string()) >= 2.32:
1064 port
["tag"] = new_port
["port"]["name"]
1065 net_list_vim
.append(port
)
1067 if net
.get('floating_ip', False):
1068 net
['exit_on_floating_ip_error'] = True
1069 external_network
.append(net
)
1070 elif net
['use'] == 'mgmt' and self
.config
.get('use_floating_ip'):
1071 net
['exit_on_floating_ip_error'] = False
1072 external_network
.append(net
)
1073 net
['floating_ip'] = self
.config
.get('use_floating_ip')
1075 # If port security is disabled when the port has not yet been attached to the VM, then all vm traffic is dropped.
1076 # As a workaround we wait until the VM is active and then disable the port-security
1077 if net
.get("port_security") == False and not self
.config
.get("no_port_security_extension"):
1078 no_secured_ports
.append(new_port
["port"]["id"])
1081 # metadata = {"pci_assignement": json.dumps(metadata_vpci)}
1082 # if len(metadata["pci_assignement"]) >255:
1083 # #limit the metadata size
1084 # #metadata["pci_assignement"] = metadata["pci_assignement"][0:255]
1085 # self.logger.warn("Metadata deleted since it exceeds the expected length (255) ")
1088 self
.logger
.debug("name '%s' image_id '%s'flavor_id '%s' net_list_vim '%s' description '%s'",
1089 name
, image_id
, flavor_id
, str(net_list_vim
), description
)
1091 security_groups
= self
.config
.get('security_groups')
1092 if type(security_groups
) is str:
1093 security_groups
= ( security_groups
, )
1095 config_drive
, userdata
= self
._create
_user
_data
(cloud_config
)
1097 # Create additional volumes in case these are present in disk_list
1098 base_disk_index
= ord('b')
1100 block_device_mapping
= {}
1101 for disk
in disk_list
:
1102 if disk
.get('vim_id'):
1103 block_device_mapping
['_vd' + chr(base_disk_index
)] = disk
['vim_id']
1105 if 'image_id' in disk
:
1106 volume
= self
.cinder
.volumes
.create(size
=disk
['size'], name
=name
+ '_vd' +
1107 chr(base_disk_index
), imageRef
=disk
['image_id'])
1109 volume
= self
.cinder
.volumes
.create(size
=disk
['size'], name
=name
+ '_vd' +
1110 chr(base_disk_index
))
1111 created_items
["volume:" + str(volume
.id)] = True
1112 block_device_mapping
['_vd' + chr(base_disk_index
)] = volume
.id
1113 base_disk_index
+= 1
1115 # Wait until created volumes are with status available
1117 while elapsed_time
< volume_timeout
:
1118 for created_item
in created_items
:
1119 v
, _
, volume_id
= created_item
.partition(":")
1121 if self
.cinder
.volumes
.get(volume_id
).status
!= 'available':
1123 else: # all ready: break from while
1127 # If we exceeded the timeout rollback
1128 if elapsed_time
>= volume_timeout
:
1129 raise vimconn
.vimconnException('Timeout creating volumes for instance ' + name
,
1130 http_code
=vimconn
.HTTP_Request_Timeout
)
1131 # get availability Zone
1132 vm_av_zone
= self
._get
_vm
_availability
_zone
(availability_zone_index
, availability_zone_list
)
1134 self
.logger
.debug("nova.servers.create({}, {}, {}, nics={}, security_groups={}, "
1135 "availability_zone={}, key_name={}, userdata={}, config_drive={}, "
1136 "block_device_mapping={})".format(name
, image_id
, flavor_id
, net_list_vim
,
1137 security_groups
, vm_av_zone
, self
.config
.get('keypair'),
1138 userdata
, config_drive
, block_device_mapping
))
1139 server
= self
.nova
.servers
.create(name
, image_id
, flavor_id
, nics
=net_list_vim
,
1140 security_groups
=security_groups
,
1141 availability_zone
=vm_av_zone
,
1142 key_name
=self
.config
.get('keypair'),
1144 config_drive
=config_drive
,
1145 block_device_mapping
=block_device_mapping
1146 ) # , description=description)
1148 vm_start_time
= time
.time()
1149 # Previously mentioned workaround to wait until the VM is active and then disable the port-security
1150 if no_secured_ports
:
1151 self
.__wait
_for
_vm
(server
.id, 'ACTIVE')
1153 for port_id
in no_secured_ports
:
1155 self
.neutron
.update_port(port_id
,
1156 {"port": {"port_security_enabled": False, "security_groups": None}})
1157 except Exception as e
:
1158 raise vimconn
.vimconnException("It was not possible to disable port security for port {}".format(
1160 # print "DONE :-)", server
1163 if external_network
:
1164 floating_ips
= self
.neutron
.list_floatingips().get("floatingips", ())
1165 for floating_network
in external_network
:
1170 ip
= floating_ips
.pop(0)
1171 if ip
.get("port_id", False) or ip
.get('tenant_id') != server
.tenant_id
:
1173 if isinstance(floating_network
['floating_ip'], str):
1174 if ip
.get("floating_network_id") != floating_network
['floating_ip']:
1176 free_floating_ip
= ip
.get("floating_ip_address")
1178 if isinstance(floating_network
['floating_ip'], str) and \
1179 floating_network
['floating_ip'].lower() != "true":
1180 pool_id
= floating_network
['floating_ip']
1182 # Find the external network
1183 external_nets
= list()
1184 for net
in self
.neutron
.list_networks()['networks']:
1185 if net
['router:external']:
1186 external_nets
.append(net
)
1188 if len(external_nets
) == 0:
1189 raise vimconn
.vimconnException("Cannot create floating_ip automatically since no external "
1190 "network is present",
1191 http_code
=vimconn
.HTTP_Conflict
)
1192 if len(external_nets
) > 1:
1193 raise vimconn
.vimconnException("Cannot create floating_ip automatically since multiple "
1194 "external networks are present",
1195 http_code
=vimconn
.HTTP_Conflict
)
1197 pool_id
= external_nets
[0].get('id')
1198 param
= {'floatingip': {'floating_network_id': pool_id
, 'tenant_id': server
.tenant_id
}}
1200 # self.logger.debug("Creating floating IP")
1201 new_floating_ip
= self
.neutron
.create_floatingip(param
)
1202 free_floating_ip
= new_floating_ip
['floatingip']['floating_ip_address']
1203 except Exception as e
:
1204 raise vimconn
.vimconnException(type(e
).__name
__ + ": Cannot create new floating_ip " +
1205 str(e
), http_code
=vimconn
.HTTP_Conflict
)
1207 fix_ip
= floating_network
.get('ip')
1210 server
.add_floating_ip(free_floating_ip
, fix_ip
)
1212 except Exception as e
:
1213 # openstack need some time after VM creation to asign an IP. So retry if fails
1214 vm_status
= self
.nova
.servers
.get(server
.id).status
1215 if vm_status
!= 'ACTIVE' and vm_status
!= 'ERROR':
1216 if time
.time() - vm_start_time
< server_timeout
:
1219 raise vimconn
.vimconnException(
1220 "Cannot create floating_ip: {} {}".format(type(e
).__name
__, e
),
1221 http_code
=vimconn
.HTTP_Conflict
)
1223 except Exception as e
:
1224 if not floating_network
['exit_on_floating_ip_error']:
1225 self
.logger
.warn("Cannot create floating_ip. %s", str(e
))
1229 return server
.id, created_items
1230 # except nvExceptions.NotFound as e:
1231 # error_value=-vimconn.HTTP_Not_Found
1232 # error_text= "vm instance %s not found" % vm_id
1233 # except TypeError as e:
1234 # raise vimconn.vimconnException(type(e).__name__ + ": "+ str(e), http_code=vimconn.HTTP_Bad_Request)
1236 except Exception as e
:
1239 server_id
= server
.id
1241 self
.delete_vminstance(server_id
, created_items
)
1242 except Exception as e2
:
1243 self
.logger
.error("new_vminstance rollback fail {}".format(e2
))
1245 self
._format
_exception
(e
)
1247 def get_vminstance(self
,vm_id
):
1248 '''Returns the VM instance information from VIM'''
1249 #self.logger.debug("Getting VM from VIM")
1251 self
._reload
_connection
()
1252 server
= self
.nova
.servers
.find(id=vm_id
)
1253 #TODO parse input and translate to VIM format (openmano_schemas.new_vminstance_response_schema)
1254 return server
.to_dict()
1255 except (ksExceptions
.ClientException
, nvExceptions
.ClientException
, nvExceptions
.NotFound
, ConnectionError
) as e
:
1256 self
._format
_exception
(e
)
1258 def get_vminstance_console(self
,vm_id
, console_type
="vnc"):
1260 Get a console for the virtual machine
1262 vm_id: uuid of the VM
1263 console_type, can be:
1264 "novnc" (by default), "xvpvnc" for VNC types,
1265 "rdp-html5" for RDP types, "spice-html5" for SPICE types
1266 Returns dict with the console parameters:
1267 protocol: ssh, ftp, http, https, ...
1268 server: usually ip address
1269 port: the http, ssh, ... port
1270 suffix: extra text, e.g. the http path and query string
1272 self
.logger
.debug("Getting VM CONSOLE from VIM")
1274 self
._reload
_connection
()
1275 server
= self
.nova
.servers
.find(id=vm_id
)
1276 if console_type
== None or console_type
== "novnc":
1277 console_dict
= server
.get_vnc_console("novnc")
1278 elif console_type
== "xvpvnc":
1279 console_dict
= server
.get_vnc_console(console_type
)
1280 elif console_type
== "rdp-html5":
1281 console_dict
= server
.get_rdp_console(console_type
)
1282 elif console_type
== "spice-html5":
1283 console_dict
= server
.get_spice_console(console_type
)
1285 raise vimconn
.vimconnException("console type '{}' not allowed".format(console_type
), http_code
=vimconn
.HTTP_Bad_Request
)
1287 console_dict1
= console_dict
.get("console")
1289 console_url
= console_dict1
.get("url")
1292 protocol_index
= console_url
.find("//")
1293 suffix_index
= console_url
[protocol_index
+2:].find("/") + protocol_index
+2
1294 port_index
= console_url
[protocol_index
+2:suffix_index
].find(":") + protocol_index
+2
1295 if protocol_index
< 0 or port_index
<0 or suffix_index
<0:
1296 return -vimconn
.HTTP_Internal_Server_Error
, "Unexpected response from VIM"
1297 console_dict
={"protocol": console_url
[0:protocol_index
],
1298 "server": console_url
[protocol_index
+2:port_index
],
1299 "port": console_url
[port_index
:suffix_index
],
1300 "suffix": console_url
[suffix_index
+1:]
1304 raise vimconn
.vimconnUnexpectedResponse("Unexpected response from VIM")
1306 except (nvExceptions
.NotFound
, ksExceptions
.ClientException
, nvExceptions
.ClientException
, nvExceptions
.BadRequest
, ConnectionError
) as e
:
1307 self
._format
_exception
(e
)
1309 def delete_vminstance(self
, vm_id
, created_items
=None):
1310 '''Removes a VM instance from VIM. Returns the old identifier
1312 #print "osconnector: Getting VM from VIM"
1313 if created_items
== None:
1316 self
._reload
_connection
()
1317 # delete VM ports attached to this networks before the virtual machine
1318 for k
, v
in created_items
.items():
1319 if not v
: # skip already deleted
1322 k_item
, _
, k_id
= k
.partition(":")
1323 if k_item
== "port":
1324 self
.neutron
.delete_port(k_id
)
1325 except Exception as e
:
1326 self
.logger
.error("Error deleting port: {}: {}".format(type(e
).__name
__, e
))
1328 # #commented because detaching the volumes makes the servers.delete not work properly ?!?
1329 # #dettach volumes attached
1330 # server = self.nova.servers.get(vm_id)
1331 # volumes_attached_dict = server._info['os-extended-volumes:volumes_attached'] #volume['id']
1332 # #for volume in volumes_attached_dict:
1333 # # self.cinder.volumes.detach(volume['id'])
1336 self
.nova
.servers
.delete(vm_id
)
1338 # delete volumes. Although having detached, they should have in active status before deleting
1339 # we ensure in this loop
1342 while keep_waiting
and elapsed_time
< volume_timeout
:
1343 keep_waiting
= False
1344 for k
, v
in created_items
.items():
1345 if not v
: # skip already deleted
1348 k_item
, _
, k_id
= k
.partition(":")
1349 if k_item
== "volume":
1350 if self
.cinder
.volumes
.get(k_id
).status
!= 'available':
1353 self
.cinder
.volumes
.delete(k_id
)
1354 except Exception as e
:
1355 self
.logger
.error("Error deleting volume: {}: {}".format(type(e
).__name
__, e
))
1360 except (nvExceptions
.NotFound
, ksExceptions
.ClientException
, nvExceptions
.ClientException
, ConnectionError
) as e
:
1361 self
._format
_exception
(e
)
1363 def refresh_vms_status(self
, vm_list
):
1364 '''Get the status of the virtual machines and their interfaces/ports
1365 Params: the list of VM identifiers
1366 Returns a dictionary with:
1367 vm_id: #VIM id of this Virtual Machine
1368 status: #Mandatory. Text with one of:
1369 # DELETED (not found at vim)
1370 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
1371 # OTHER (Vim reported other status not understood)
1372 # ERROR (VIM indicates an ERROR status)
1373 # ACTIVE, PAUSED, SUSPENDED, INACTIVE (not running),
1374 # CREATING (on building process), ERROR
1375 # ACTIVE:NoMgmtIP (Active but any of its interface has an IP address
1377 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
1378 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
1380 - vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
1381 mac_address: #Text format XX:XX:XX:XX:XX:XX
1382 vim_net_id: #network id where this interface is connected
1383 vim_interface_id: #interface/port VIM id
1384 ip_address: #null, or text with IPv4, IPv6 address
1385 compute_node: #identification of compute node where PF,VF interface is allocated
1386 pci: #PCI address of the NIC that hosts the PF,VF
1387 vlan: #physical VLAN used for VF
1390 self
.logger
.debug("refresh_vms status: Getting tenant VM instance information from VIM")
1391 for vm_id
in vm_list
:
1394 vm_vim
= self
.get_vminstance(vm_id
)
1395 if vm_vim
['status'] in vmStatus2manoFormat
:
1396 vm
['status'] = vmStatus2manoFormat
[ vm_vim
['status'] ]
1398 vm
['status'] = "OTHER"
1399 vm
['error_msg'] = "VIM status reported " + vm_vim
['status']
1401 vm
['vim_info'] = yaml
.safe_dump(vm_vim
, default_flow_style
=True, width
=256)
1402 except yaml
.representer
.RepresenterError
:
1403 vm
['vim_info'] = str(vm_vim
)
1404 vm
["interfaces"] = []
1405 if vm_vim
.get('fault'):
1406 vm
['error_msg'] = str(vm_vim
['fault'])
1409 self
._reload
_connection
()
1410 port_dict
= self
.neutron
.list_ports(device_id
=vm_id
)
1411 for port
in port_dict
["ports"]:
1414 interface
['vim_info'] = yaml
.safe_dump(port
, default_flow_style
=True, width
=256)
1415 except yaml
.representer
.RepresenterError
:
1416 interface
['vim_info'] = str(port
)
1417 interface
["mac_address"] = port
.get("mac_address")
1418 interface
["vim_net_id"] = port
["network_id"]
1419 interface
["vim_interface_id"] = port
["id"]
1420 # check if OS-EXT-SRV-ATTR:host is there,
1421 # in case of non-admin credentials, it will be missing
1422 if vm_vim
.get('OS-EXT-SRV-ATTR:host'):
1423 interface
["compute_node"] = vm_vim
['OS-EXT-SRV-ATTR:host']
1424 interface
["pci"] = None
1426 # check if binding:profile is there,
1427 # in case of non-admin credentials, it will be missing
1428 if port
.get('binding:profile'):
1429 if port
['binding:profile'].get('pci_slot'):
1430 # TODO: At the moment sr-iov pci addresses are converted to PF pci addresses by setting the slot to 0x00
1431 # TODO: This is just a workaround valid for niantinc. Find a better way to do so
1432 # CHANGE DDDD:BB:SS.F to DDDD:BB:00.(F%2) assuming there are 2 ports per nic
1433 pci
= port
['binding:profile']['pci_slot']
1434 # interface["pci"] = pci[:-4] + "00." + str(int(pci[-1]) % 2)
1435 interface
["pci"] = pci
1436 interface
["vlan"] = None
1437 #if network is of type vlan and port is of type direct (sr-iov) then set vlan id
1438 network
= self
.neutron
.show_network(port
["network_id"])
1439 if network
['network'].get('provider:network_type') == 'vlan' and \
1440 port
.get("binding:vnic_type") == "direct":
1441 interface
["vlan"] = network
['network'].get('provider:segmentation_id')
1443 #look for floating ip address
1445 floating_ip_dict
= self
.neutron
.list_floatingips(port_id
=port
["id"])
1446 if floating_ip_dict
.get("floatingips"):
1447 ips
.append(floating_ip_dict
["floatingips"][0].get("floating_ip_address") )
1451 for subnet
in port
["fixed_ips"]:
1452 ips
.append(subnet
["ip_address"])
1453 interface
["ip_address"] = ";".join(ips
)
1454 vm
["interfaces"].append(interface
)
1455 except Exception as e
:
1456 self
.logger
.error("Error getting vm interface information {}: {}".format(type(e
).__name
__, e
),
1458 except vimconn
.vimconnNotFoundException
as e
:
1459 self
.logger
.error("Exception getting vm status: %s", str(e
))
1460 vm
['status'] = "DELETED"
1461 vm
['error_msg'] = str(e
)
1462 except vimconn
.vimconnException
as e
:
1463 self
.logger
.error("Exception getting vm status: %s", str(e
))
1464 vm
['status'] = "VIM_ERROR"
1465 vm
['error_msg'] = str(e
)
1469 def action_vminstance(self
, vm_id
, action_dict
, created_items
={}):
1470 '''Send and action over a VM instance from VIM
1471 Returns None or the console dict if the action was successfully sent to the VIM'''
1472 self
.logger
.debug("Action over VM '%s': %s", vm_id
, str(action_dict
))
1474 self
._reload
_connection
()
1475 server
= self
.nova
.servers
.find(id=vm_id
)
1476 if "start" in action_dict
:
1477 if action_dict
["start"]=="rebuild":
1480 if server
.status
=="PAUSED":
1482 elif server
.status
=="SUSPENDED":
1484 elif server
.status
=="SHUTOFF":
1486 elif "pause" in action_dict
:
1488 elif "resume" in action_dict
:
1490 elif "shutoff" in action_dict
or "shutdown" in action_dict
:
1492 elif "forceOff" in action_dict
:
1494 elif "terminate" in action_dict
:
1496 elif "createImage" in action_dict
:
1497 server
.create_image()
1498 #"path":path_schema,
1499 #"description":description_schema,
1500 #"name":name_schema,
1501 #"metadata":metadata_schema,
1502 #"imageRef": id_schema,
1503 #"disk": {"oneOf":[{"type": "null"}, {"type":"string"}] },
1504 elif "rebuild" in action_dict
:
1505 server
.rebuild(server
.image
['id'])
1506 elif "reboot" in action_dict
:
1507 server
.reboot() #reboot_type='SOFT'
1508 elif "console" in action_dict
:
1509 console_type
= action_dict
["console"]
1510 if console_type
== None or console_type
== "novnc":
1511 console_dict
= server
.get_vnc_console("novnc")
1512 elif console_type
== "xvpvnc":
1513 console_dict
= server
.get_vnc_console(console_type
)
1514 elif console_type
== "rdp-html5":
1515 console_dict
= server
.get_rdp_console(console_type
)
1516 elif console_type
== "spice-html5":
1517 console_dict
= server
.get_spice_console(console_type
)
1519 raise vimconn
.vimconnException("console type '{}' not allowed".format(console_type
),
1520 http_code
=vimconn
.HTTP_Bad_Request
)
1522 console_url
= console_dict
["console"]["url"]
1524 protocol_index
= console_url
.find("//")
1525 suffix_index
= console_url
[protocol_index
+2:].find("/") + protocol_index
+2
1526 port_index
= console_url
[protocol_index
+2:suffix_index
].find(":") + protocol_index
+2
1527 if protocol_index
< 0 or port_index
<0 or suffix_index
<0:
1528 raise vimconn
.vimconnException("Unexpected response from VIM " + str(console_dict
))
1529 console_dict2
={"protocol": console_url
[0:protocol_index
],
1530 "server": console_url
[protocol_index
+2 : port_index
],
1531 "port": int(console_url
[port_index
+1 : suffix_index
]),
1532 "suffix": console_url
[suffix_index
+1:]
1534 return console_dict2
1535 except Exception as e
:
1536 raise vimconn
.vimconnException("Unexpected response from VIM " + str(console_dict
))
1539 except (ksExceptions
.ClientException
, nvExceptions
.ClientException
, nvExceptions
.NotFound
, ConnectionError
) as e
:
1540 self
._format
_exception
(e
)
1541 #TODO insert exception vimconn.HTTP_Unauthorized
1543 ####### VIO Specific Changes #########
1544 def _genrate_vlanID(self
):
1546 Method to get unused vlanID
1554 networks
= self
.get_network_list()
1555 for net
in networks
:
1556 if net
.get('provider:segmentation_id'):
1557 usedVlanIDs
.append(net
.get('provider:segmentation_id'))
1558 used_vlanIDs
= set(usedVlanIDs
)
1560 #find unused VLAN ID
1561 for vlanID_range
in self
.config
.get('dataplane_net_vlan_range'):
1563 start_vlanid
, end_vlanid
= map(int, vlanID_range
.replace(" ", "").split("-"))
1564 for vlanID
in xrange(start_vlanid
, end_vlanid
+ 1):
1565 if vlanID
not in used_vlanIDs
:
1567 except Exception as exp
:
1568 raise vimconn
.vimconnException("Exception {} occurred while generating VLAN ID.".format(exp
))
1570 raise vimconn
.vimconnConflictException("Unable to create the SRIOV VLAN network."\
1571 " All given Vlan IDs {} are in use.".format(self
.config
.get('dataplane_net_vlan_range')))
1574 def _validate_vlan_ranges(self
, dataplane_net_vlan_range
):
1576 Method to validate user given vlanID ranges
1580 for vlanID_range
in dataplane_net_vlan_range
:
1581 vlan_range
= vlanID_range
.replace(" ", "")
1583 vlanID_pattern
= r
'(\d)*-(\d)*$'
1584 match_obj
= re
.match(vlanID_pattern
, vlan_range
)
1586 raise vimconn
.vimconnConflictException("Invalid dataplane_net_vlan_range {}.You must provide "\
1587 "'dataplane_net_vlan_range' in format [start_ID - end_ID].".format(vlanID_range
))
1589 start_vlanid
, end_vlanid
= map(int,vlan_range
.split("-"))
1590 if start_vlanid
<= 0 :
1591 raise vimconn
.vimconnConflictException("Invalid dataplane_net_vlan_range {}."\
1592 "Start ID can not be zero. For VLAN "\
1593 "networks valid IDs are 1 to 4094 ".format(vlanID_range
))
1594 if end_vlanid
> 4094 :
1595 raise vimconn
.vimconnConflictException("Invalid dataplane_net_vlan_range {}."\
1596 "End VLAN ID can not be greater than 4094. For VLAN "\
1597 "networks valid IDs are 1 to 4094 ".format(vlanID_range
))
1599 if start_vlanid
> end_vlanid
:
1600 raise vimconn
.vimconnConflictException("Invalid dataplane_net_vlan_range {}."\
1601 "You must provide a 'dataplane_net_vlan_range' in format start_ID - end_ID and "\
1602 "start_ID < end_ID ".format(vlanID_range
))
1606 def new_external_port(self
, port_data
):
1607 #TODO openstack if needed
1608 '''Adds a external port to VIM'''
1609 '''Returns the port identifier'''
1610 return -vimconn
.HTTP_Internal_Server_Error
, "osconnector.new_external_port() not implemented"
1612 def connect_port_network(self
, port_id
, network_id
, admin
=False):
1613 #TODO openstack if needed
1614 '''Connects a external port to a network'''
1615 '''Returns status code of the VIM response'''
1616 return -vimconn
.HTTP_Internal_Server_Error
, "osconnector.connect_port_network() not implemented"
1618 def new_user(self
, user_name
, user_passwd
, tenant_id
=None):
1619 '''Adds a new user to openstack VIM'''
1620 '''Returns the user identifier'''
1621 self
.logger
.debug("osconnector: Adding a new user to VIM")
1623 self
._reload
_connection
()
1624 user
=self
.keystone
.users
.create(user_name
, password
=user_passwd
, default_project
=tenant_id
)
1625 #self.keystone.tenants.add_user(self.k_creds["username"], #role)
1627 except ksExceptions
.ConnectionError
as e
:
1628 error_value
=-vimconn
.HTTP_Bad_Request
1629 error_text
= type(e
).__name
__ + ": "+ (str(e
) if len(e
.args
)==0 else str(e
.args
[0]))
1630 except ksExceptions
.ClientException
as e
: #TODO remove
1631 error_value
=-vimconn
.HTTP_Bad_Request
1632 error_text
= type(e
).__name
__ + ": "+ (str(e
) if len(e
.args
)==0 else str(e
.args
[0]))
1633 #TODO insert exception vimconn.HTTP_Unauthorized
1634 #if reaching here is because an exception
1635 self
.logger
.debug("new_user " + error_text
)
1636 return error_value
, error_text
1638 def delete_user(self
, user_id
):
1639 '''Delete a user from openstack VIM'''
1640 '''Returns the user identifier'''
1642 print("osconnector: Deleting a user from VIM")
1644 self
._reload
_connection
()
1645 self
.keystone
.users
.delete(user_id
)
1647 except ksExceptions
.ConnectionError
as e
:
1648 error_value
=-vimconn
.HTTP_Bad_Request
1649 error_text
= type(e
).__name
__ + ": "+ (str(e
) if len(e
.args
)==0 else str(e
.args
[0]))
1650 except ksExceptions
.NotFound
as e
:
1651 error_value
=-vimconn
.HTTP_Not_Found
1652 error_text
= type(e
).__name
__ + ": "+ (str(e
) if len(e
.args
)==0 else str(e
.args
[0]))
1653 except ksExceptions
.ClientException
as e
: #TODO remove
1654 error_value
=-vimconn
.HTTP_Bad_Request
1655 error_text
= type(e
).__name
__ + ": "+ (str(e
) if len(e
.args
)==0 else str(e
.args
[0]))
1656 #TODO insert exception vimconn.HTTP_Unauthorized
1657 #if reaching here is because an exception
1658 self
.logger
.debug("delete_tenant " + error_text
)
1659 return error_value
, error_text
1661 def get_hosts_info(self
):
1662 '''Get the information of deployed hosts
1663 Returns the hosts content'''
1665 print("osconnector: Getting Host info from VIM")
1668 self
._reload
_connection
()
1669 hypervisors
= self
.nova
.hypervisors
.list()
1670 for hype
in hypervisors
:
1671 h_list
.append( hype
.to_dict() )
1672 return 1, {"hosts":h_list
}
1673 except nvExceptions
.NotFound
as e
:
1674 error_value
=-vimconn
.HTTP_Not_Found
1675 error_text
= (str(e
) if len(e
.args
)==0 else str(e
.args
[0]))
1676 except (ksExceptions
.ClientException
, nvExceptions
.ClientException
) as e
:
1677 error_value
=-vimconn
.HTTP_Bad_Request
1678 error_text
= type(e
).__name
__ + ": "+ (str(e
) if len(e
.args
)==0 else str(e
.args
[0]))
1679 #TODO insert exception vimconn.HTTP_Unauthorized
1680 #if reaching here is because an exception
1681 self
.logger
.debug("get_hosts_info " + error_text
)
1682 return error_value
, error_text
1684 def get_hosts(self
, vim_tenant
):
1685 '''Get the hosts and deployed instances
1686 Returns the hosts content'''
1687 r
, hype_dict
= self
.get_hosts_info()
1690 hypervisors
= hype_dict
["hosts"]
1692 servers
= self
.nova
.servers
.list()
1693 for hype
in hypervisors
:
1694 for server
in servers
:
1695 if server
.to_dict()['OS-EXT-SRV-ATTR:hypervisor_hostname']==hype
['hypervisor_hostname']:
1697 hype
['vm'].append(server
.id)
1699 hype
['vm'] = [server
.id]
1701 except nvExceptions
.NotFound
as e
:
1702 error_value
=-vimconn
.HTTP_Not_Found
1703 error_text
= (str(e
) if len(e
.args
)==0 else str(e
.args
[0]))
1704 except (ksExceptions
.ClientException
, nvExceptions
.ClientException
) as e
:
1705 error_value
=-vimconn
.HTTP_Bad_Request
1706 error_text
= type(e
).__name
__ + ": "+ (str(e
) if len(e
.args
)==0 else str(e
.args
[0]))
1707 #TODO insert exception vimconn.HTTP_Unauthorized
1708 #if reaching here is because an exception
1709 self
.logger
.debug("get_hosts " + error_text
)
1710 return error_value
, error_text
1712 def new_classification(self
, name
, ctype
, definition
):
1714 'Adding a new (Traffic) Classification to VIM, named %s', name
)
1717 self
._reload
_connection
()
1718 if ctype
not in supportedClassificationTypes
:
1719 raise vimconn
.vimconnNotSupportedException(
1720 'OpenStack VIM connector doesn\'t support provided '
1721 'Classification Type {}, supported ones are: '
1722 '{}'.format(ctype
, supportedClassificationTypes
))
1723 if not self
._validate
_classification
(ctype
, definition
):
1724 raise vimconn
.vimconnException(
1725 'Incorrect Classification definition '
1726 'for the type specified.')
1727 classification_dict
= definition
1728 classification_dict
['name'] = name
1730 new_class
= self
.neutron
.create_sfc_flow_classifier(
1731 {'flow_classifier': classification_dict
})
1732 return new_class
['flow_classifier']['id']
1733 except (neExceptions
.ConnectionFailed
, ksExceptions
.ClientException
,
1734 neExceptions
.NeutronException
, ConnectionError
) as e
:
1736 'Creation of Classification failed.')
1737 self
._format
_exception
(e
)
1739 def get_classification(self
, class_id
):
1740 self
.logger
.debug(" Getting Classification %s from VIM", class_id
)
1741 filter_dict
= {"id": class_id
}
1742 class_list
= self
.get_classification_list(filter_dict
)
1743 if len(class_list
) == 0:
1744 raise vimconn
.vimconnNotFoundException(
1745 "Classification '{}' not found".format(class_id
))
1746 elif len(class_list
) > 1:
1747 raise vimconn
.vimconnConflictException(
1748 "Found more than one Classification with this criteria")
1749 classification
= class_list
[0]
1750 return classification
1752 def get_classification_list(self
, filter_dict
={}):
1753 self
.logger
.debug("Getting Classifications from VIM filter: '%s'",
1756 filter_dict_os
= filter_dict
.copy()
1757 self
._reload
_connection
()
1758 if self
.api_version3
and "tenant_id" in filter_dict_os
:
1759 filter_dict_os
['project_id'] = filter_dict_os
.pop('tenant_id')
1760 classification_dict
= self
.neutron
.list_sfc_flow_classifiers(
1762 classification_list
= classification_dict
["flow_classifiers"]
1763 self
.__classification
_os
2mano
(classification_list
)
1764 return classification_list
1765 except (neExceptions
.ConnectionFailed
, ksExceptions
.ClientException
,
1766 neExceptions
.NeutronException
, ConnectionError
) as e
:
1767 self
._format
_exception
(e
)
1769 def delete_classification(self
, class_id
):
1770 self
.logger
.debug("Deleting Classification '%s' from VIM", class_id
)
1772 self
._reload
_connection
()
1773 self
.neutron
.delete_sfc_flow_classifier(class_id
)
1775 except (neExceptions
.ConnectionFailed
, neExceptions
.NeutronException
,
1776 ksExceptions
.ClientException
, neExceptions
.NeutronException
,
1777 ConnectionError
) as e
:
1778 self
._format
_exception
(e
)
1780 def new_sfi(self
, name
, ingress_ports
, egress_ports
, sfc_encap
=True):
1782 "Adding a new Service Function Instance to VIM, named '%s'", name
)
1785 self
._reload
_connection
()
1789 if len(ingress_ports
) != 1:
1790 raise vimconn
.vimconnNotSupportedException(
1791 "OpenStack VIM connector can only have "
1792 "1 ingress port per SFI")
1793 if len(egress_ports
) != 1:
1794 raise vimconn
.vimconnNotSupportedException(
1795 "OpenStack VIM connector can only have "
1796 "1 egress port per SFI")
1797 sfi_dict
= {'name': name
,
1798 'ingress': ingress_ports
[0],
1799 'egress': egress_ports
[0],
1800 'service_function_parameters': {
1801 'correlation': correlation
}}
1802 new_sfi
= self
.neutron
.create_sfc_port_pair({'port_pair': sfi_dict
})
1803 return new_sfi
['port_pair']['id']
1804 except (neExceptions
.ConnectionFailed
, ksExceptions
.ClientException
,
1805 neExceptions
.NeutronException
, ConnectionError
) as e
:
1808 self
.neutron
.delete_sfc_port_pair(
1809 new_sfi
['port_pair']['id'])
1812 'Creation of Service Function Instance failed, with '
1813 'subsequent deletion failure as well.')
1814 self
._format
_exception
(e
)
1816 def get_sfi(self
, sfi_id
):
1818 'Getting Service Function Instance %s from VIM', sfi_id
)
1819 filter_dict
= {"id": sfi_id
}
1820 sfi_list
= self
.get_sfi_list(filter_dict
)
1821 if len(sfi_list
) == 0:
1822 raise vimconn
.vimconnNotFoundException(
1823 "Service Function Instance '{}' not found".format(sfi_id
))
1824 elif len(sfi_list
) > 1:
1825 raise vimconn
.vimconnConflictException(
1826 'Found more than one Service Function Instance '
1827 'with this criteria')
1831 def get_sfi_list(self
, filter_dict
={}):
1832 self
.logger
.debug("Getting Service Function Instances from "
1833 "VIM filter: '%s'", str(filter_dict
))
1835 self
._reload
_connection
()
1836 filter_dict_os
= filter_dict
.copy()
1837 if self
.api_version3
and "tenant_id" in filter_dict_os
:
1838 filter_dict_os
['project_id'] = filter_dict_os
.pop('tenant_id')
1839 sfi_dict
= self
.neutron
.list_sfc_port_pairs(**filter_dict_os
)
1840 sfi_list
= sfi_dict
["port_pairs"]
1841 self
.__sfi
_os
2mano
(sfi_list
)
1843 except (neExceptions
.ConnectionFailed
, ksExceptions
.ClientException
,
1844 neExceptions
.NeutronException
, ConnectionError
) as e
:
1845 self
._format
_exception
(e
)
1847 def delete_sfi(self
, sfi_id
):
1848 self
.logger
.debug("Deleting Service Function Instance '%s' "
1851 self
._reload
_connection
()
1852 self
.neutron
.delete_sfc_port_pair(sfi_id
)
1854 except (neExceptions
.ConnectionFailed
, neExceptions
.NeutronException
,
1855 ksExceptions
.ClientException
, neExceptions
.NeutronException
,
1856 ConnectionError
) as e
:
1857 self
._format
_exception
(e
)
1859 def new_sf(self
, name
, sfis
, sfc_encap
=True):
1860 self
.logger
.debug("Adding a new Service Function to VIM, "
1864 self
._reload
_connection
()
1865 # correlation = None
1867 # correlation = 'nsh'
1868 for instance
in sfis
:
1869 sfi
= self
.get_sfi(instance
)
1870 if sfi
.get('sfc_encap') != sfc_encap
:
1871 raise vimconn
.vimconnNotSupportedException(
1872 "OpenStack VIM connector requires all SFIs of the "
1873 "same SF to share the same SFC Encapsulation")
1874 sf_dict
= {'name': name
,
1876 new_sf
= self
.neutron
.create_sfc_port_pair_group({
1877 'port_pair_group': sf_dict
})
1878 return new_sf
['port_pair_group']['id']
1879 except (neExceptions
.ConnectionFailed
, ksExceptions
.ClientException
,
1880 neExceptions
.NeutronException
, ConnectionError
) as e
:
1883 self
.neutron
.delete_sfc_port_pair_group(
1884 new_sf
['port_pair_group']['id'])
1887 'Creation of Service Function failed, with '
1888 'subsequent deletion failure as well.')
1889 self
._format
_exception
(e
)
1891 def get_sf(self
, sf_id
):
1892 self
.logger
.debug("Getting Service Function %s from VIM", sf_id
)
1893 filter_dict
= {"id": sf_id
}
1894 sf_list
= self
.get_sf_list(filter_dict
)
1895 if len(sf_list
) == 0:
1896 raise vimconn
.vimconnNotFoundException(
1897 "Service Function '{}' not found".format(sf_id
))
1898 elif len(sf_list
) > 1:
1899 raise vimconn
.vimconnConflictException(
1900 "Found more than one Service Function with this criteria")
1904 def get_sf_list(self
, filter_dict
={}):
1905 self
.logger
.debug("Getting Service Function from VIM filter: '%s'",
1908 self
._reload
_connection
()
1909 filter_dict_os
= filter_dict
.copy()
1910 if self
.api_version3
and "tenant_id" in filter_dict_os
:
1911 filter_dict_os
['project_id'] = filter_dict_os
.pop('tenant_id')
1912 sf_dict
= self
.neutron
.list_sfc_port_pair_groups(**filter_dict_os
)
1913 sf_list
= sf_dict
["port_pair_groups"]
1914 self
.__sf
_os
2mano
(sf_list
)
1916 except (neExceptions
.ConnectionFailed
, ksExceptions
.ClientException
,
1917 neExceptions
.NeutronException
, ConnectionError
) as e
:
1918 self
._format
_exception
(e
)
1920 def delete_sf(self
, sf_id
):
1921 self
.logger
.debug("Deleting Service Function '%s' from VIM", sf_id
)
1923 self
._reload
_connection
()
1924 self
.neutron
.delete_sfc_port_pair_group(sf_id
)
1926 except (neExceptions
.ConnectionFailed
, neExceptions
.NeutronException
,
1927 ksExceptions
.ClientException
, neExceptions
.NeutronException
,
1928 ConnectionError
) as e
:
1929 self
._format
_exception
(e
)
1931 def new_sfp(self
, name
, classifications
, sfs
, sfc_encap
=True, spi
=None):
1932 self
.logger
.debug("Adding a new Service Function Path to VIM, "
1936 self
._reload
_connection
()
1937 # In networking-sfc the MPLS encapsulation is legacy
1938 # should be used when no full SFC Encapsulation is intended
1942 sfp_dict
= {'name': name
,
1943 'flow_classifiers': classifications
,
1944 'port_pair_groups': sfs
,
1945 'chain_parameters': {'correlation': correlation
}}
1947 sfp_dict
['chain_id'] = spi
1948 new_sfp
= self
.neutron
.create_sfc_port_chain({'port_chain': sfp_dict
})
1949 return new_sfp
["port_chain"]["id"]
1950 except (neExceptions
.ConnectionFailed
, ksExceptions
.ClientException
,
1951 neExceptions
.NeutronException
, ConnectionError
) as e
:
1954 self
.neutron
.delete_sfc_port_chain(new_sfp
['port_chain']['id'])
1957 'Creation of Service Function Path failed, with '
1958 'subsequent deletion failure as well.')
1959 self
._format
_exception
(e
)
1961 def get_sfp(self
, sfp_id
):
1962 self
.logger
.debug(" Getting Service Function Path %s from VIM", sfp_id
)
1963 filter_dict
= {"id": sfp_id
}
1964 sfp_list
= self
.get_sfp_list(filter_dict
)
1965 if len(sfp_list
) == 0:
1966 raise vimconn
.vimconnNotFoundException(
1967 "Service Function Path '{}' not found".format(sfp_id
))
1968 elif len(sfp_list
) > 1:
1969 raise vimconn
.vimconnConflictException(
1970 "Found more than one Service Function Path with this criteria")
1974 def get_sfp_list(self
, filter_dict
={}):
1975 self
.logger
.debug("Getting Service Function Paths from VIM filter: "
1976 "'%s'", str(filter_dict
))
1978 self
._reload
_connection
()
1979 filter_dict_os
= filter_dict
.copy()
1980 if self
.api_version3
and "tenant_id" in filter_dict_os
:
1981 filter_dict_os
['project_id'] = filter_dict_os
.pop('tenant_id')
1982 sfp_dict
= self
.neutron
.list_sfc_port_chains(**filter_dict_os
)
1983 sfp_list
= sfp_dict
["port_chains"]
1984 self
.__sfp
_os
2mano
(sfp_list
)
1986 except (neExceptions
.ConnectionFailed
, ksExceptions
.ClientException
,
1987 neExceptions
.NeutronException
, ConnectionError
) as e
:
1988 self
._format
_exception
(e
)
1990 def delete_sfp(self
, sfp_id
):
1992 "Deleting Service Function Path '%s' from VIM", sfp_id
)
1994 self
._reload
_connection
()
1995 self
.neutron
.delete_sfc_port_chain(sfp_id
)
1997 except (neExceptions
.ConnectionFailed
, neExceptions
.NeutronException
,
1998 ksExceptions
.ClientException
, neExceptions
.NeutronException
,
1999 ConnectionError
) as e
:
2000 self
._format
_exception
(e
)