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