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