blob: 999aceedad8835abe797b235b60e5ffa9a0c5237 [file] [log] [blame]
tierno7edb6752016-03-21 17:37:52 +01001# -*- 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'''
25osconnector implements all the methods to interact with openstack using the python-client.
26'''
montesmoreno0c8def02016-12-22 12:16:23 +000027__author__="Alfonso Tierno, Gerardo Garcia, Pablo Montes, xFlow Research"
28__date__ ="$22-jun-2014 11:19:29$"
tierno7edb6752016-03-21 17:37:52 +010029
30import vimconn
31import json
32import yaml
tiernoae4a8d12016-07-08 12:30:39 +020033import logging
garciadeblas9f8456e2016-09-05 05:02:59 +020034import netaddr
montesmoreno0c8def02016-12-22 12:16:23 +000035import time
tierno7edb6752016-03-21 17:37:52 +010036
ahmadsa95baa272016-11-30 09:14:11 +050037from novaclient import client as nClient_v2, exceptions as nvExceptions, api_versions as APIVersion
38import keystoneclient.v2_0.client as ksClient_v2
39from novaclient.v2.client import Client as nClient
40import keystoneclient.v3.client as ksClient
tierno7edb6752016-03-21 17:37:52 +010041import keystoneclient.exceptions as ksExceptions
42import glanceclient.v2.client as glClient
43import glanceclient.client as gl1Client
44import glanceclient.exc as gl1Exceptions
montesmoreno0c8def02016-12-22 12:16:23 +000045import cinderclient.v2.client as cClient_v2
tierno7edb6752016-03-21 17:37:52 +010046from httplib import HTTPException
ahmadsa95baa272016-11-30 09:14:11 +050047from neutronclient.neutron import client as neClient_v2
48from neutronclient.v2_0 import client as neClient
tierno7edb6752016-03-21 17:37:52 +010049from neutronclient.common import exceptions as neExceptions
50from requests.exceptions import ConnectionError
51
52'''contain the openstack virtual machine status to openmano status'''
53vmStatus2manoFormat={'ACTIVE':'ACTIVE',
54 'PAUSED':'PAUSED',
55 'SUSPENDED': 'SUSPENDED',
56 'SHUTOFF':'INACTIVE',
57 'BUILD':'BUILD',
58 'ERROR':'ERROR','DELETED':'DELETED'
59 }
60netStatus2manoFormat={'ACTIVE':'ACTIVE','PAUSED':'PAUSED','INACTIVE':'INACTIVE','BUILD':'BUILD','ERROR':'ERROR','DELETED':'DELETED'
61 }
62
montesmoreno0c8def02016-12-22 12:16:23 +000063#global var to have a timeout creating and deleting volumes
64volume_timeout = 60
65
tierno7edb6752016-03-21 17:37:52 +010066class vimconnector(vimconn.vimconnector):
tiernofe789902016-09-29 14:20:44 +000067 def __init__(self, uuid, name, tenant_id, tenant_name, url, url_admin=None, user=None, passwd=None, log_level=None, config={}):
tierno7edb6752016-03-21 17:37:52 +010068 '''using common constructor parameters. In this case
69 'url' is the keystone authorization url,
70 'url_admin' is not use
71 '''
ahmadsa95baa272016-11-30 09:14:11 +050072 self.osc_api_version = 'v2.0'
73 if config.get('APIversion') == 'v3.3':
74 self.osc_api_version = 'v3.3'
tiernoae4a8d12016-07-08 12:30:39 +020075 vimconn.vimconnector.__init__(self, uuid, name, tenant_id, tenant_name, url, url_admin, user, passwd, log_level, config)
tierno7edb6752016-03-21 17:37:52 +010076
77 self.k_creds={}
78 self.n_creds={}
79 if not url:
80 raise TypeError, 'url param can not be NoneType'
81 self.k_creds['auth_url'] = url
82 self.n_creds['auth_url'] = url
tierno392f2852016-05-13 12:28:55 +020083 if tenant_name:
84 self.k_creds['tenant_name'] = tenant_name
85 self.n_creds['project_id'] = tenant_name
86 if tenant_id:
87 self.k_creds['tenant_id'] = tenant_id
88 self.n_creds['tenant_id'] = tenant_id
tierno7edb6752016-03-21 17:37:52 +010089 if user:
90 self.k_creds['username'] = user
91 self.n_creds['username'] = user
92 if passwd:
93 self.k_creds['password'] = passwd
94 self.n_creds['api_key'] = passwd
ahmadsa95baa272016-11-30 09:14:11 +050095 if self.osc_api_version == 'v3.3':
96 self.k_creds['project_name'] = tenant_name
97 self.k_creds['project_id'] = tenant_id
montesmoreno0c8def02016-12-22 12:16:23 +000098
tierno7edb6752016-03-21 17:37:52 +010099 self.reload_client = True
tierno73ad9e42016-09-12 18:11:11 +0200100 self.logger = logging.getLogger('openmano.vim.openstack')
tiernofe789902016-09-29 14:20:44 +0000101 if log_level:
102 self.logger.setLevel( getattr(logging, log_level) )
tierno7edb6752016-03-21 17:37:52 +0100103
104 def __setitem__(self,index, value):
105 '''Set individuals parameters
106 Throw TypeError, KeyError
107 '''
tierno392f2852016-05-13 12:28:55 +0200108 if index=='tenant_id':
tierno7edb6752016-03-21 17:37:52 +0100109 self.reload_client=True
tierno392f2852016-05-13 12:28:55 +0200110 self.tenant_id = value
ahmadsa95baa272016-11-30 09:14:11 +0500111 if self.osc_api_version == 'v3.3':
112 if value:
113 self.k_creds['project_id'] = value
114 self.n_creds['project_id'] = value
115 else:
116 del self.k_creds['project_id']
117 del self.n_creds['project_id']
tierno392f2852016-05-13 12:28:55 +0200118 else:
ahmadsa95baa272016-11-30 09:14:11 +0500119 if value:
120 self.k_creds['tenant_id'] = value
121 self.n_creds['tenant_id'] = value
122 else:
123 del self.k_creds['tenant_id']
124 del self.n_creds['tenant_id']
tierno392f2852016-05-13 12:28:55 +0200125 elif index=='tenant_name':
126 self.reload_client=True
127 self.tenant_name = value
ahmadsa95baa272016-11-30 09:14:11 +0500128 if self.osc_api_version == 'v3.3':
129 if value:
130 self.k_creds['project_name'] = value
131 self.n_creds['project_name'] = value
132 else:
133 del self.k_creds['project_name']
134 del self.n_creds['project_name']
tierno7edb6752016-03-21 17:37:52 +0100135 else:
ahmadsa95baa272016-11-30 09:14:11 +0500136 if value:
137 self.k_creds['tenant_name'] = value
138 self.n_creds['project_id'] = value
139 else:
140 del self.k_creds['tenant_name']
141 del self.n_creds['project_id']
tierno7edb6752016-03-21 17:37:52 +0100142 elif index=='user':
143 self.reload_client=True
144 self.user = value
145 if value:
146 self.k_creds['username'] = value
147 self.n_creds['username'] = value
148 else:
149 del self.k_creds['username']
150 del self.n_creds['username']
151 elif index=='passwd':
152 self.reload_client=True
153 self.passwd = value
154 if value:
155 self.k_creds['password'] = value
156 self.n_creds['api_key'] = value
157 else:
158 del self.k_creds['password']
159 del self.n_creds['api_key']
160 elif index=='url':
161 self.reload_client=True
162 self.url = value
163 if value:
164 self.k_creds['auth_url'] = value
165 self.n_creds['auth_url'] = value
166 else:
167 raise TypeError, 'url param can not be NoneType'
168 else:
169 vimconn.vimconnector.__setitem__(self,index, value)
170
171 def _reload_connection(self):
172 '''Called before any operation, it check if credentials has changed
173 Throw keystoneclient.apiclient.exceptions.AuthorizationFailure
174 '''
175 #TODO control the timing and possible token timeout, but it seams that python client does this task for us :-)
176 if self.reload_client:
177 #test valid params
178 if len(self.n_creds) <4:
179 raise ksExceptions.ClientException("Not enough parameters to connect to openstack")
ahmadsa95baa272016-11-30 09:14:11 +0500180 if self.osc_api_version == 'v3.3':
181 self.nova = nClient(APIVersion(version_str='2'), **self.n_creds)
montesmoreno0c8def02016-12-22 12:16:23 +0000182 #TODO To be updated for v3
183 #self.cinder = cClient.Client(**self.n_creds)
ahmadsa95baa272016-11-30 09:14:11 +0500184 self.keystone = ksClient.Client(**self.k_creds)
185 self.ne_endpoint=self.keystone.service_catalog.url_for(service_type='network', endpoint_type='publicURL')
186 self.neutron = neClient.Client(APIVersion(version_str='2'), endpoint_url=self.ne_endpoint, token=self.keystone.auth_token, **self.k_creds)
187 else:
188 self.nova = nClient_v2.Client('2', **self.n_creds)
montesmoreno0c8def02016-12-22 12:16:23 +0000189 self.cinder = cClient_v2.Client(**self.n_creds)
ahmadsa95baa272016-11-30 09:14:11 +0500190 self.keystone = ksClient_v2.Client(**self.k_creds)
191 self.ne_endpoint=self.keystone.service_catalog.url_for(service_type='network', endpoint_type='publicURL')
192 self.neutron = neClient_v2.Client('2.0', endpoint_url=self.ne_endpoint, token=self.keystone.auth_token, **self.k_creds)
tierno7edb6752016-03-21 17:37:52 +0100193 self.glance_endpoint = self.keystone.service_catalog.url_for(service_type='image', endpoint_type='publicURL')
194 self.glance = glClient.Client(self.glance_endpoint, token=self.keystone.auth_token, **self.k_creds) #TODO check k_creds vs n_creds
tierno7edb6752016-03-21 17:37:52 +0100195 self.reload_client = False
ahmadsa95baa272016-11-30 09:14:11 +0500196
tierno7edb6752016-03-21 17:37:52 +0100197 def __net_os2mano(self, net_list_dict):
198 '''Transform the net openstack format to mano format
199 net_list_dict can be a list of dict or a single dict'''
200 if type(net_list_dict) is dict:
201 net_list_=(net_list_dict,)
202 elif type(net_list_dict) is list:
203 net_list_=net_list_dict
204 else:
205 raise TypeError("param net_list_dict must be a list or a dictionary")
206 for net in net_list_:
207 if net.get('provider:network_type') == "vlan":
208 net['type']='data'
209 else:
210 net['type']='bridge'
tiernoae4a8d12016-07-08 12:30:39 +0200211
212
213
214 def _format_exception(self, exception):
215 '''Transform a keystone, nova, neutron exception into a vimconn exception'''
216 if isinstance(exception, (HTTPException, gl1Exceptions.HTTPException, gl1Exceptions.CommunicationError,
tierno8e995ce2016-09-22 08:13:00 +0000217 ConnectionError, ksExceptions.ConnectionError, neExceptions.ConnectionFailed
218 )):
tiernoae4a8d12016-07-08 12:30:39 +0200219 raise vimconn.vimconnConnectionException(type(exception).__name__ + ": " + str(exception))
220 elif isinstance(exception, (nvExceptions.ClientException, ksExceptions.ClientException,
221 neExceptions.NeutronException, nvExceptions.BadRequest)):
222 raise vimconn.vimconnUnexpectedResponse(type(exception).__name__ + ": " + str(exception))
223 elif isinstance(exception, (neExceptions.NetworkNotFoundClient, nvExceptions.NotFound)):
224 raise vimconn.vimconnNotFoundException(type(exception).__name__ + ": " + str(exception))
225 elif isinstance(exception, nvExceptions.Conflict):
226 raise vimconn.vimconnConflictException(type(exception).__name__ + ": " + str(exception))
227 else: # ()
228 raise vimconn.vimconnConnectionException(type(exception).__name__ + ": " + str(exception))
229
230 def get_tenant_list(self, filter_dict={}):
231 '''Obtain tenants of VIM
232 filter_dict can contain the following keys:
233 name: filter by tenant name
234 id: filter by tenant uuid/id
235 <other VIM specific>
236 Returns the tenant list of dictionaries: [{'name':'<name>, 'id':'<id>, ...}, ...]
237 '''
ahmadsa95baa272016-11-30 09:14:11 +0500238 self.logger.debug("Getting tenants from VIM filter: '%s'", str(filter_dict))
tiernoae4a8d12016-07-08 12:30:39 +0200239 try:
240 self._reload_connection()
montesmoreno0c8def02016-12-22 12:16:23 +0000241 if self.osc_api_version == 'v3.3':
ahmadsa95baa272016-11-30 09:14:11 +0500242 project_class_list=self.keystone.projects.findall(**filter_dict)
243 else:
244 project_class_list=self.keystone.tenants.findall(**filter_dict)
245 project_list=[]
246 for project in project_class_list:
247 project_list.append(project.to_dict())
248 return project_list
tierno8e995ce2016-09-22 08:13:00 +0000249 except (ksExceptions.ConnectionError, ksExceptions.ClientException, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200250 self._format_exception(e)
251
252 def new_tenant(self, tenant_name, tenant_description):
253 '''Adds a new tenant to openstack VIM. Returns the tenant identifier'''
254 self.logger.debug("Adding a new tenant name: %s", tenant_name)
255 try:
256 self._reload_connection()
ahmadsa95baa272016-11-30 09:14:11 +0500257 if self.osc_api_version == 'v3.3':
258 project=self.keystone.projects.create(tenant_name, tenant_description)
259 else:
260 project=self.keystone.tenants.create(tenant_name, tenant_description)
261 return project.id
tierno8e995ce2016-09-22 08:13:00 +0000262 except (ksExceptions.ConnectionError, ksExceptions.ClientException, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200263 self._format_exception(e)
264
265 def delete_tenant(self, tenant_id):
266 '''Delete a tenant from openstack VIM. Returns the old tenant identifier'''
267 self.logger.debug("Deleting tenant %s from VIM", tenant_id)
268 try:
269 self._reload_connection()
montesmoreno0c8def02016-12-22 12:16:23 +0000270 if self.osc_api_version == 'v3.3':
ahmadsa95baa272016-11-30 09:14:11 +0500271 self.keystone.projects.delete(tenant_id)
272 else:
273 self.keystone.tenants.delete(tenant_id)
tiernoae4a8d12016-07-08 12:30:39 +0200274 return tenant_id
tierno8e995ce2016-09-22 08:13:00 +0000275 except (ksExceptions.ConnectionError, ksExceptions.ClientException, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200276 self._format_exception(e)
ahmadsa95baa272016-11-30 09:14:11 +0500277
garciadeblas9f8456e2016-09-05 05:02:59 +0200278 def new_network(self,net_name, net_type, ip_profile=None, shared=False, vlan=None):
tiernoae4a8d12016-07-08 12:30:39 +0200279 '''Adds a tenant network to VIM. Returns the network identifier'''
280 self.logger.debug("Adding a new network to VIM name '%s', type '%s'", net_name, net_type)
garciadeblasedca7b32016-09-29 14:01:52 +0000281 #self.logger.debug(">>>>>>>>>>>>>>>>>> IP profile %s", str(ip_profile))
tierno7edb6752016-03-21 17:37:52 +0100282 try:
garciadeblasedca7b32016-09-29 14:01:52 +0000283 new_net = None
tierno7edb6752016-03-21 17:37:52 +0100284 self._reload_connection()
285 network_dict = {'name': net_name, 'admin_state_up': True}
286 if net_type=="data" or net_type=="ptp":
287 if self.config.get('dataplane_physical_net') == None:
tiernoae4a8d12016-07-08 12:30:39 +0200288 raise vimconn.vimconnConflictException("You must provide a 'dataplane_physical_net' at config value before creating sriov network")
tierno7edb6752016-03-21 17:37:52 +0100289 network_dict["provider:physical_network"] = self.config['dataplane_physical_net'] #"physnet_sriov" #TODO physical
290 network_dict["provider:network_type"] = "vlan"
291 if vlan!=None:
292 network_dict["provider:network_type"] = vlan
tiernoae4a8d12016-07-08 12:30:39 +0200293 network_dict["shared"]=shared
tierno7edb6752016-03-21 17:37:52 +0100294 new_net=self.neutron.create_network({'network':network_dict})
295 #print new_net
garciadeblas9f8456e2016-09-05 05:02:59 +0200296 #create subnetwork, even if there is no profile
297 if not ip_profile:
298 ip_profile = {}
299 if 'subnet_address' not in ip_profile:
300 #Fake subnet is required
301 ip_profile['subnet_address'] = "192.168.111.0/24"
302 if 'ip_version' not in ip_profile:
303 ip_profile['ip_version'] = "IPv4"
tierno7edb6752016-03-21 17:37:52 +0100304 subnet={"name":net_name+"-subnet",
305 "network_id": new_net["network"]["id"],
garciadeblas9f8456e2016-09-05 05:02:59 +0200306 "ip_version": 4 if ip_profile['ip_version']=="IPv4" else 6,
307 "cidr": ip_profile['subnet_address']
tierno7edb6752016-03-21 17:37:52 +0100308 }
garciadeblas9f8456e2016-09-05 05:02:59 +0200309 if 'gateway_address' in ip_profile:
310 subnet['gateway_ip'] = ip_profile['gateway_address']
garciadeblasedca7b32016-09-29 14:01:52 +0000311 if ip_profile.get('dns_address'):
garciadeblas9f8456e2016-09-05 05:02:59 +0200312 #TODO: manage dns_address as a list of addresses separated by commas
313 subnet['dns_nameservers'] = []
314 subnet['dns_nameservers'].append(ip_profile['dns_address'])
315 if 'dhcp_enabled' in ip_profile:
316 subnet['enable_dhcp'] = False if ip_profile['dhcp_enabled']=="false" else True
317 if 'dhcp_start_address' in ip_profile:
318 subnet['allocation_pools']=[]
319 subnet['allocation_pools'].append(dict())
320 subnet['allocation_pools'][0]['start'] = ip_profile['dhcp_start_address']
321 if 'dhcp_count' in ip_profile:
322 #parts = ip_profile['dhcp_start_address'].split('.')
323 #ip_int = (int(parts[0]) << 24) + (int(parts[1]) << 16) + (int(parts[2]) << 8) + int(parts[3])
324 ip_int = int(netaddr.IPAddress(ip_profile['dhcp_start_address']))
garciadeblas21d795b2016-09-29 17:31:46 +0200325 ip_int += ip_profile['dhcp_count'] - 1
garciadeblas9f8456e2016-09-05 05:02:59 +0200326 ip_str = str(netaddr.IPAddress(ip_int))
327 subnet['allocation_pools'][0]['end'] = ip_str
garciadeblasedca7b32016-09-29 14:01:52 +0000328 #self.logger.debug(">>>>>>>>>>>>>>>>>> Subnet: %s", str(subnet))
tierno7edb6752016-03-21 17:37:52 +0100329 self.neutron.create_subnet({"subnet": subnet} )
tiernoae4a8d12016-07-08 12:30:39 +0200330 return new_net["network"]["id"]
tierno8e995ce2016-09-22 08:13:00 +0000331 except (neExceptions.ConnectionFailed, ksExceptions.ClientException, neExceptions.NeutronException, ConnectionError) as e:
garciadeblasedca7b32016-09-29 14:01:52 +0000332 if new_net:
333 self.neutron.delete_network(new_net['network']['id'])
tiernoae4a8d12016-07-08 12:30:39 +0200334 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100335
336 def get_network_list(self, filter_dict={}):
337 '''Obtain tenant networks of VIM
338 Filter_dict can be:
339 name: network name
340 id: network uuid
341 shared: boolean
342 tenant_id: tenant
343 admin_state_up: boolean
344 status: 'ACTIVE'
345 Returns the network list of dictionaries
346 '''
tiernoae4a8d12016-07-08 12:30:39 +0200347 self.logger.debug("Getting network from VIM filter: '%s'", str(filter_dict))
tierno7edb6752016-03-21 17:37:52 +0100348 try:
349 self._reload_connection()
montesmoreno0c8def02016-12-22 12:16:23 +0000350 if self.osc_api_version == 'v3.3' and "tenant_id" in filter_dict:
ahmadsa95baa272016-11-30 09:14:11 +0500351 filter_dict['project_id'] = filter_dict.pop('tenant_id')
tierno7edb6752016-03-21 17:37:52 +0100352 net_dict=self.neutron.list_networks(**filter_dict)
353 net_list=net_dict["networks"]
354 self.__net_os2mano(net_list)
tiernoae4a8d12016-07-08 12:30:39 +0200355 return net_list
tierno8e995ce2016-09-22 08:13:00 +0000356 except (neExceptions.ConnectionFailed, ksExceptions.ClientException, neExceptions.NeutronException, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200357 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100358
tiernoae4a8d12016-07-08 12:30:39 +0200359 def get_network(self, net_id):
360 '''Obtain details of network from VIM
361 Returns the network information from a network id'''
362 self.logger.debug(" Getting tenant network %s from VIM", net_id)
tierno7edb6752016-03-21 17:37:52 +0100363 filter_dict={"id": net_id}
tiernoae4a8d12016-07-08 12:30:39 +0200364 net_list = self.get_network_list(filter_dict)
tierno7edb6752016-03-21 17:37:52 +0100365 if len(net_list)==0:
tiernoae4a8d12016-07-08 12:30:39 +0200366 raise vimconn.vimconnNotFoundException("Network '{}' not found".format(net_id))
tierno7edb6752016-03-21 17:37:52 +0100367 elif len(net_list)>1:
tiernoae4a8d12016-07-08 12:30:39 +0200368 raise vimconn.vimconnConflictException("Found more than one network with this criteria")
tierno7edb6752016-03-21 17:37:52 +0100369 net = net_list[0]
370 subnets=[]
371 for subnet_id in net.get("subnets", () ):
372 try:
373 subnet = self.neutron.show_subnet(subnet_id)
374 except Exception as e:
tiernoae4a8d12016-07-08 12:30:39 +0200375 self.logger.error("osconnector.get_network(): Error getting subnet %s %s" % (net_id, str(e)))
376 subnet = {"id": subnet_id, "fault": str(e)}
tierno7edb6752016-03-21 17:37:52 +0100377 subnets.append(subnet)
378 net["subnets"] = subnets
tiernoae4a8d12016-07-08 12:30:39 +0200379 return net
tierno7edb6752016-03-21 17:37:52 +0100380
tiernoae4a8d12016-07-08 12:30:39 +0200381 def delete_network(self, net_id):
382 '''Deletes a tenant network from VIM. Returns the old network identifier'''
383 self.logger.debug("Deleting network '%s' from VIM", net_id)
tierno7edb6752016-03-21 17:37:52 +0100384 try:
385 self._reload_connection()
386 #delete VM ports attached to this networks before the network
387 ports = self.neutron.list_ports(network_id=net_id)
388 for p in ports['ports']:
389 try:
390 self.neutron.delete_port(p["id"])
391 except Exception as e:
tiernoae4a8d12016-07-08 12:30:39 +0200392 self.logger.error("Error deleting port %s: %s", p["id"], str(e))
tierno7edb6752016-03-21 17:37:52 +0100393 self.neutron.delete_network(net_id)
tiernoae4a8d12016-07-08 12:30:39 +0200394 return net_id
395 except (neExceptions.ConnectionFailed, neExceptions.NetworkNotFoundClient, neExceptions.NeutronException,
tierno8e995ce2016-09-22 08:13:00 +0000396 ksExceptions.ClientException, neExceptions.NeutronException, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200397 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100398
tiernoae4a8d12016-07-08 12:30:39 +0200399 def refresh_nets_status(self, net_list):
400 '''Get the status of the networks
401 Params: the list of network identifiers
402 Returns a dictionary with:
403 net_id: #VIM id of this network
404 status: #Mandatory. Text with one of:
405 # DELETED (not found at vim)
406 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
407 # OTHER (Vim reported other status not understood)
408 # ERROR (VIM indicates an ERROR status)
409 # ACTIVE, INACTIVE, DOWN (admin down),
410 # BUILD (on building process)
411 #
412 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
413 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
414
415 '''
416 net_dict={}
417 for net_id in net_list:
418 net = {}
419 try:
420 net_vim = self.get_network(net_id)
421 if net_vim['status'] in netStatus2manoFormat:
422 net["status"] = netStatus2manoFormat[ net_vim['status'] ]
423 else:
424 net["status"] = "OTHER"
425 net["error_msg"] = "VIM status reported " + net_vim['status']
426
tierno8e995ce2016-09-22 08:13:00 +0000427 if net['status'] == "ACTIVE" and not net_vim['admin_state_up']:
tiernoae4a8d12016-07-08 12:30:39 +0200428 net['status'] = 'DOWN'
tierno8e995ce2016-09-22 08:13:00 +0000429 try:
430 net['vim_info'] = yaml.safe_dump(net_vim, default_flow_style=True, width=256)
431 except yaml.representer.RepresenterError:
432 net['vim_info'] = str(net_vim)
tiernoae4a8d12016-07-08 12:30:39 +0200433 if net_vim.get('fault'): #TODO
434 net['error_msg'] = str(net_vim['fault'])
435 except vimconn.vimconnNotFoundException as e:
436 self.logger.error("Exception getting net status: %s", str(e))
437 net['status'] = "DELETED"
438 net['error_msg'] = str(e)
439 except vimconn.vimconnException as e:
440 self.logger.error("Exception getting net status: %s", str(e))
441 net['status'] = "VIM_ERROR"
442 net['error_msg'] = str(e)
443 net_dict[net_id] = net
444 return net_dict
445
446 def get_flavor(self, flavor_id):
447 '''Obtain flavor details from the VIM. Returns the flavor dict details'''
448 self.logger.debug("Getting flavor '%s'", flavor_id)
tierno7edb6752016-03-21 17:37:52 +0100449 try:
450 self._reload_connection()
451 flavor = self.nova.flavors.find(id=flavor_id)
452 #TODO parse input and translate to VIM format (openmano_schemas.new_vminstance_response_schema)
tiernoae4a8d12016-07-08 12:30:39 +0200453 return flavor.to_dict()
tierno8e995ce2016-09-22 08:13:00 +0000454 except (nvExceptions.NotFound, nvExceptions.ClientException, ksExceptions.ClientException, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200455 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100456
tiernoae4a8d12016-07-08 12:30:39 +0200457 def new_flavor(self, flavor_data, change_name_if_used=True):
tierno7edb6752016-03-21 17:37:52 +0100458 '''Adds a tenant flavor to openstack VIM
tiernoae4a8d12016-07-08 12:30:39 +0200459 if change_name_if_used is True, it will change name in case of conflict, because it is not supported name repetition
tierno7edb6752016-03-21 17:37:52 +0100460 Returns the flavor identifier
461 '''
tiernoae4a8d12016-07-08 12:30:39 +0200462 self.logger.debug("Adding flavor '%s'", str(flavor_data))
tierno7edb6752016-03-21 17:37:52 +0100463 retry=0
tiernoae4a8d12016-07-08 12:30:39 +0200464 max_retries=3
tierno7edb6752016-03-21 17:37:52 +0100465 name_suffix = 0
tiernoae4a8d12016-07-08 12:30:39 +0200466 name=flavor_data['name']
467 while retry<max_retries:
tierno7edb6752016-03-21 17:37:52 +0100468 retry+=1
469 try:
470 self._reload_connection()
471 if change_name_if_used:
472 #get used names
473 fl_names=[]
474 fl=self.nova.flavors.list()
475 for f in fl:
476 fl_names.append(f.name)
477 while name in fl_names:
478 name_suffix += 1
tiernoae4a8d12016-07-08 12:30:39 +0200479 name = flavor_data['name']+"-" + str(name_suffix)
tierno7edb6752016-03-21 17:37:52 +0100480
tiernoae4a8d12016-07-08 12:30:39 +0200481 ram = flavor_data.get('ram',64)
482 vcpus = flavor_data.get('vcpus',1)
tierno7edb6752016-03-21 17:37:52 +0100483 numa_properties=None
484
tiernoae4a8d12016-07-08 12:30:39 +0200485 extended = flavor_data.get("extended")
tierno7edb6752016-03-21 17:37:52 +0100486 if extended:
487 numas=extended.get("numas")
488 if numas:
489 numa_nodes = len(numas)
490 if numa_nodes > 1:
491 return -1, "Can not add flavor with more than one numa"
492 numa_properties = {"hw:numa_nodes":str(numa_nodes)}
493 numa_properties["hw:mem_page_size"] = "large"
494 numa_properties["hw:cpu_policy"] = "dedicated"
495 numa_properties["hw:numa_mempolicy"] = "strict"
496 for numa in numas:
497 #overwrite ram and vcpus
498 ram = numa['memory']*1024
499 if 'paired-threads' in numa:
500 vcpus = numa['paired-threads']*2
501 numa_properties["hw:cpu_threads_policy"] = "prefer"
502 elif 'cores' in numa:
503 vcpus = numa['cores']
504 #numa_properties["hw:cpu_threads_policy"] = "prefer"
505 elif 'threads' in numa:
506 vcpus = numa['threads']
507 numa_properties["hw:cpu_policy"] = "isolated"
508 for interface in numa.get("interfaces",() ):
509 if interface["dedicated"]=="yes":
tierno809a7802016-07-08 13:31:24 +0200510 raise vimconn.vimconnException("Passthrough interfaces are not supported for the openstack connector", http_code=vimconn.HTTP_Service_Unavailable)
tierno7edb6752016-03-21 17:37:52 +0100511 #TODO, add the key 'pci_passthrough:alias"="<label at config>:<number ifaces>"' when a way to connect it is available
512
513 #create flavor
514 new_flavor=self.nova.flavors.create(name,
515 ram,
516 vcpus,
tiernoae4a8d12016-07-08 12:30:39 +0200517 flavor_data.get('disk',1),
518 is_public=flavor_data.get('is_public', True)
tierno7edb6752016-03-21 17:37:52 +0100519 )
520 #add metadata
521 if numa_properties:
522 new_flavor.set_keys(numa_properties)
tiernoae4a8d12016-07-08 12:30:39 +0200523 return new_flavor.id
tierno7edb6752016-03-21 17:37:52 +0100524 except nvExceptions.Conflict as e:
tiernoae4a8d12016-07-08 12:30:39 +0200525 if change_name_if_used and retry < max_retries:
tierno7edb6752016-03-21 17:37:52 +0100526 continue
tiernoae4a8d12016-07-08 12:30:39 +0200527 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100528 #except nvExceptions.BadRequest as e:
529 except (ksExceptions.ClientException, nvExceptions.ClientException, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200530 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100531
tiernoae4a8d12016-07-08 12:30:39 +0200532 def delete_flavor(self,flavor_id):
533 '''Deletes a tenant flavor from openstack VIM. Returns the old flavor_id
tierno7edb6752016-03-21 17:37:52 +0100534 '''
tiernoae4a8d12016-07-08 12:30:39 +0200535 try:
536 self._reload_connection()
537 self.nova.flavors.delete(flavor_id)
538 return flavor_id
539 #except nvExceptions.BadRequest as e:
tierno8e995ce2016-09-22 08:13:00 +0000540 except (nvExceptions.NotFound, ksExceptions.ClientException, nvExceptions.ClientException, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200541 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100542
tiernoae4a8d12016-07-08 12:30:39 +0200543 def new_image(self,image_dict):
tierno7edb6752016-03-21 17:37:52 +0100544 '''
tiernoae4a8d12016-07-08 12:30:39 +0200545 Adds a tenant image to VIM. imge_dict is a dictionary with:
546 name: name
547 disk_format: qcow2, vhd, vmdk, raw (by default), ...
548 location: path or URI
549 public: "yes" or "no"
550 metadata: metadata of the image
551 Returns the image_id
tierno7edb6752016-03-21 17:37:52 +0100552 '''
tierno7edb6752016-03-21 17:37:52 +0100553 #using version 1 of glance client
554 glancev1 = gl1Client.Client('1',self.glance_endpoint, token=self.keystone.auth_token, **self.k_creds) #TODO check k_creds vs n_creds
tiernoae4a8d12016-07-08 12:30:39 +0200555 retry=0
556 max_retries=3
557 while retry<max_retries:
tierno7edb6752016-03-21 17:37:52 +0100558 retry+=1
559 try:
560 self._reload_connection()
561 #determine format http://docs.openstack.org/developer/glance/formats.html
562 if "disk_format" in image_dict:
563 disk_format=image_dict["disk_format"]
garciadeblas14480452017-01-10 13:08:07 +0100564 else: #autodiscover based on extension
tierno7edb6752016-03-21 17:37:52 +0100565 if image_dict['location'][-6:]==".qcow2":
566 disk_format="qcow2"
567 elif image_dict['location'][-4:]==".vhd":
568 disk_format="vhd"
569 elif image_dict['location'][-5:]==".vmdk":
570 disk_format="vmdk"
571 elif image_dict['location'][-4:]==".vdi":
572 disk_format="vdi"
573 elif image_dict['location'][-4:]==".iso":
574 disk_format="iso"
575 elif image_dict['location'][-4:]==".aki":
576 disk_format="aki"
577 elif image_dict['location'][-4:]==".ari":
578 disk_format="ari"
579 elif image_dict['location'][-4:]==".ami":
580 disk_format="ami"
581 else:
582 disk_format="raw"
tiernoae4a8d12016-07-08 12:30:39 +0200583 self.logger.debug("new_image: '%s' loading from '%s'", image_dict['name'], image_dict['location'])
tierno7edb6752016-03-21 17:37:52 +0100584 if image_dict['location'][0:4]=="http":
585 new_image = glancev1.images.create(name=image_dict['name'], is_public=image_dict.get('public',"yes")=="yes",
586 container_format="bare", location=image_dict['location'], disk_format=disk_format)
587 else: #local path
588 with open(image_dict['location']) as fimage:
589 new_image = glancev1.images.create(name=image_dict['name'], is_public=image_dict.get('public',"yes")=="yes",
590 container_format="bare", data=fimage, disk_format=disk_format)
591 #insert metadata. We cannot use 'new_image.properties.setdefault'
592 #because nova and glance are "INDEPENDENT" and we are using nova for reading metadata
593 new_image_nova=self.nova.images.find(id=new_image.id)
594 new_image_nova.metadata.setdefault('location',image_dict['location'])
595 metadata_to_load = image_dict.get('metadata')
596 if metadata_to_load:
597 for k,v in yaml.load(metadata_to_load).iteritems():
598 new_image_nova.metadata.setdefault(k,v)
tiernoae4a8d12016-07-08 12:30:39 +0200599 return new_image.id
600 except (nvExceptions.Conflict, ksExceptions.ClientException, nvExceptions.ClientException) as e:
601 self._format_exception(e)
tierno8e995ce2016-09-22 08:13:00 +0000602 except (HTTPException, gl1Exceptions.HTTPException, gl1Exceptions.CommunicationError, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200603 if retry==max_retries:
604 continue
605 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100606 except IOError as e: #can not open the file
tiernoae4a8d12016-07-08 12:30:39 +0200607 raise vimconn.vimconnConnectionException(type(e).__name__ + ": " + str(e)+ " for " + image_dict['location'],
608 http_code=vimconn.HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +0100609
tiernoae4a8d12016-07-08 12:30:39 +0200610 def delete_image(self, image_id):
611 '''Deletes a tenant image from openstack VIM. Returns the old id
tierno7edb6752016-03-21 17:37:52 +0100612 '''
tiernoae4a8d12016-07-08 12:30:39 +0200613 try:
614 self._reload_connection()
615 self.nova.images.delete(image_id)
616 return image_id
tierno8e995ce2016-09-22 08:13:00 +0000617 except (nvExceptions.NotFound, ksExceptions.ClientException, nvExceptions.ClientException, gl1Exceptions.CommunicationError, ConnectionError) as e: #TODO remove
tiernoae4a8d12016-07-08 12:30:39 +0200618 self._format_exception(e)
619
620 def get_image_id_from_path(self, path):
garciadeblasb69fa9f2016-09-28 12:04:10 +0200621 '''Get the image id from image path in the VIM database. Returns the image_id'''
tiernoae4a8d12016-07-08 12:30:39 +0200622 try:
623 self._reload_connection()
624 images = self.nova.images.list()
625 for image in images:
626 if image.metadata.get("location")==path:
627 return image.id
628 raise vimconn.vimconnNotFoundException("image with location '{}' not found".format( path))
tierno8e995ce2016-09-22 08:13:00 +0000629 except (ksExceptions.ClientException, nvExceptions.ClientException, gl1Exceptions.CommunicationError, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200630 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100631
garciadeblasb69fa9f2016-09-28 12:04:10 +0200632 def get_image_list(self, filter_dict={}):
633 '''Obtain tenant images from VIM
634 Filter_dict can be:
635 id: image id
636 name: image name
637 checksum: image checksum
638 Returns the image list of dictionaries:
639 [{<the fields at Filter_dict plus some VIM specific>}, ...]
640 List can be empty
641 '''
642 self.logger.debug("Getting image list from VIM filter: '%s'", str(filter_dict))
643 try:
644 self._reload_connection()
645 filter_dict_os=filter_dict.copy()
646 #First we filter by the available filter fields: name, id. The others are removed.
647 filter_dict_os.pop('checksum',None)
648 image_list=self.nova.images.findall(**filter_dict_os)
649 if len(image_list)==0:
650 return []
651 #Then we filter by the rest of filter fields: checksum
652 filtered_list = []
653 for image in image_list:
garciadeblasbb6a1ed2016-09-30 14:02:09 +0000654 image_dict=self.glance.images.get(image.id)
garciadeblas14480452017-01-10 13:08:07 +0100655 if 'checksum' not in filter_dict or image_dict['checksum']==filter_dict.get('checksum'):
656 filtered_list.append(image_dict)
garciadeblasb69fa9f2016-09-28 12:04:10 +0200657 return filtered_list
658 except (ksExceptions.ClientException, nvExceptions.ClientException, gl1Exceptions.CommunicationError, ConnectionError) as e:
659 self._format_exception(e)
660
montesmoreno0c8def02016-12-22 12:16:23 +0000661 def new_vminstance(self,name,description,start,image_id,flavor_id,net_list,cloud_config=None,disk_list=None):
tierno7edb6752016-03-21 17:37:52 +0100662 '''Adds a VM instance to VIM
663 Params:
664 start: indicates if VM must start or boot in pause mode. Ignored
665 image_id,flavor_id: iamge and flavor uuid
666 net_list: list of interfaces, each one is a dictionary with:
667 name:
668 net_id: network uuid to connect
669 vpci: virtual vcpi to assign, ignored because openstack lack #TODO
670 model: interface model, ignored #TODO
671 mac_address: used for SR-IOV ifaces #TODO for other types
672 use: 'data', 'bridge', 'mgmt'
673 type: 'virtual', 'PF', 'VF', 'VFnotShared'
674 vim_id: filled/added by this function
ahmadsaf853d452016-12-22 11:33:47 +0500675 floating_ip: True/False (or it can be None)
tierno7edb6752016-03-21 17:37:52 +0100676 #TODO ip, security groups
tiernoae4a8d12016-07-08 12:30:39 +0200677 Returns the instance identifier
tierno7edb6752016-03-21 17:37:52 +0100678 '''
tiernoae4a8d12016-07-08 12:30:39 +0200679 self.logger.debug("Creating VM image '%s' flavor '%s' nics='%s'",image_id, flavor_id,str(net_list))
tierno7edb6752016-03-21 17:37:52 +0100680 try:
tierno6e116232016-07-18 13:01:40 +0200681 metadata={}
tierno7edb6752016-03-21 17:37:52 +0100682 net_list_vim=[]
ahmadsaf853d452016-12-22 11:33:47 +0500683 external_network=[] #list of external networks to be connected to instance, later on used to create floating_ip
tierno7edb6752016-03-21 17:37:52 +0100684 self._reload_connection()
tiernoae4a8d12016-07-08 12:30:39 +0200685 metadata_vpci={} #For a specific neutron plugin
tierno7edb6752016-03-21 17:37:52 +0100686 for net in net_list:
687 if not net.get("net_id"): #skip non connected iface
688 continue
ahmadsaf853d452016-12-22 11:33:47 +0500689 if net["type"]=="virtual" or net["type"]=="VF":
tierno7edb6752016-03-21 17:37:52 +0100690 port_dict={
ahmadsaf853d452016-12-22 11:33:47 +0500691 "network_id": net["net_id"],
692 "name": net.get("name"),
693 "admin_state_up": True
694 }
695 if net["type"]=="virtual":
696 if "vpci" in net:
697 metadata_vpci[ net["net_id"] ] = [[ net["vpci"], "" ]]
698 else: # for VF
699 if "vpci" in net:
700 if "VF" not in metadata_vpci:
701 metadata_vpci["VF"]=[]
702 metadata_vpci["VF"].append([ net["vpci"], "" ])
703 port_dict["binding:vnic_type"]="direct"
tierno7edb6752016-03-21 17:37:52 +0100704 if not port_dict["name"]:
ahmadsaf853d452016-12-22 11:33:47 +0500705 port_dict["name"]=name
tierno7edb6752016-03-21 17:37:52 +0100706 if net.get("mac_address"):
707 port_dict["mac_address"]=net["mac_address"]
tierno7edb6752016-03-21 17:37:52 +0100708 new_port = self.neutron.create_port({"port": port_dict })
709 net["mac_adress"] = new_port["port"]["mac_address"]
710 net["vim_id"] = new_port["port"]["id"]
ahmadsaf853d452016-12-22 11:33:47 +0500711 net["ip"] = new_port["port"].get("fixed_ips", [{}])[0].get("ip_address")
tierno7edb6752016-03-21 17:37:52 +0100712 net_list_vim.append({"port-id": new_port["port"]["id"]})
ahmadsaf853d452016-12-22 11:33:47 +0500713 else: # for PF
714 self.logger.warn("new_vminstance: Warning, can not connect a passthrough interface ")
715 #TODO insert this when openstack consider passthrough ports as openstack neutron ports
716 if net.get('floating_ip', False):
717 external_network.append(net)
718
tierno7edb6752016-03-21 17:37:52 +0100719 if metadata_vpci:
720 metadata = {"pci_assignement": json.dumps(metadata_vpci)}
tiernoafbced42016-07-23 01:43:53 +0200721 if len(metadata["pci_assignement"]) >255:
tierno6e116232016-07-18 13:01:40 +0200722 #limit the metadata size
723 #metadata["pci_assignement"] = metadata["pci_assignement"][0:255]
724 self.logger.warn("Metadata deleted since it exceeds the expected length (255) ")
725 metadata = {}
tierno7edb6752016-03-21 17:37:52 +0100726
tiernoae4a8d12016-07-08 12:30:39 +0200727 self.logger.debug("name '%s' image_id '%s'flavor_id '%s' net_list_vim '%s' description '%s' metadata %s",
728 name, image_id, flavor_id, str(net_list_vim), description, str(metadata))
tierno7edb6752016-03-21 17:37:52 +0100729
730 security_groups = self.config.get('security_groups')
731 if type(security_groups) is str:
732 security_groups = ( security_groups, )
tiernoa4e1a6e2016-08-31 14:19:40 +0200733 if isinstance(cloud_config, dict):
734 userdata="#cloud-config\nusers:\n"
735 #default user
736 if "key-pairs" in cloud_config:
737 userdata += " - default:\n ssh-authorized-keys:\n"
738 for key in cloud_config["key-pairs"]:
739 userdata += " - '{key}'\n".format(key=key)
740 for user in cloud_config.get("users",[]):
741 userdata += " - name: {name}\n sudo: ALL=(ALL) NOPASSWD:ALL\n".format(name=user["name"])
742 if "user-info" in user:
743 userdata += " gecos: {}'\n".format(user["user-info"])
744 if user.get("key-pairs"):
745 userdata += " ssh-authorized-keys:\n"
746 for key in user["key-pairs"]:
747 userdata += " - '{key}'\n".format(key=key)
748 self.logger.debug("userdata: %s", userdata)
749 elif isinstance(cloud_config, str):
750 userdata = cloud_config
751 else:
montesmoreno0c8def02016-12-22 12:16:23 +0000752 userdata=None
753
754 #Create additional volumes in case these are present in disk_list
755 block_device_mapping = None
756 base_disk_index = ord('b')
757 if disk_list != None:
758 block_device_mapping = dict()
759 for disk in disk_list:
760 if 'image_id' in disk:
761 volume = self.cinder.volumes.create(size = disk['size'],name = name + '_vd' +
762 chr(base_disk_index), imageRef = disk['image_id'])
763 else:
764 volume = self.cinder.volumes.create(size=disk['size'], name=name + '_vd' +
765 chr(base_disk_index))
766 block_device_mapping['_vd' + chr(base_disk_index)] = volume.id
767 base_disk_index += 1
768
769 #wait until volumes are with status available
770 keep_waiting = True
771 elapsed_time = 0
772 while keep_waiting and elapsed_time < volume_timeout:
773 keep_waiting = False
774 for volume_id in block_device_mapping.itervalues():
775 if self.cinder.volumes.get(volume_id).status != 'available':
776 keep_waiting = True
777 if keep_waiting:
778 time.sleep(1)
779 elapsed_time += 1
780
781 #if we exceeded the timeout rollback
782 if elapsed_time >= volume_timeout:
783 #delete the volumes we just created
784 for volume_id in block_device_mapping.itervalues():
785 self.cinder.volumes.delete(volume_id)
786
787 #delete ports we just created
788 for net_item in net_list_vim:
789 if 'port-id' in net_item:
790 self.neutron.delete_port(net_item['port_id'])
791
792 raise vimconn.vimconnException('Timeout creating volumes for instance ' + name,
793 http_code=vimconn.HTTP_Request_Timeout)
794
tierno7edb6752016-03-21 17:37:52 +0100795 server = self.nova.servers.create(name, image_id, flavor_id, nics=net_list_vim, meta=metadata,
montesmoreno0c8def02016-12-22 12:16:23 +0000796 security_groups=security_groups,
797 availability_zone=self.config.get('availability_zone'),
798 key_name=self.config.get('keypair'),
799 userdata=userdata,
800 block_device_mapping = block_device_mapping
801 ) # , description=description)
tiernoae4a8d12016-07-08 12:30:39 +0200802 #print "DONE :-)", server
tierno7edb6752016-03-21 17:37:52 +0100803
ahmadsaf853d452016-12-22 11:33:47 +0500804 pool_id = None
805 floating_ips = self.neutron.list_floatingips().get("floatingips", ())
806 for floating_network in external_network:
807 assigned = False
808 while(assigned == False):
809 if floating_ips:
810 ip = floating_ips.pop(0)
811 if not ip.get("port_id", False):
812 free_floating_ip = ip.get("floating_ip_address")
813 try:
814 fix_ip = floating_network.get('ip')
815 server.add_floating_ip(free_floating_ip, fix_ip)
816 assigned = True
817 except Exception as e:
818 self.delete_vminstance(server.id)
819 raise vimconn.vimconnException(type(e).__name__ + ": Cannot create floating_ip "+ str(e), http_code=vimconn.HTTP_Conflict)
820 else:
821 pool_id = floating_network.get('net_id')
822 param = {'floatingip': {'floating_network_id': pool_id}}
823 try:
824 #self.logger.debug("Creating floating IP")
825 new_floating_ip = self.neutron.create_floatingip(param)
826 free_floating_ip = new_floating_ip['floatingip']['floating_ip_address']
827 fix_ip = floating_network.get('ip')
828 server.add_floating_ip(free_floating_ip, fix_ip)
829 assigned=True
830 except Exception as e:
831 self.delete_vminstance(server.id)
832 raise vimconn.vimconnException(type(e).__name__ + ": Cannot create floating_ip "+ str(e), http_code=vimconn.HTTP_Conflict)
tierno7edb6752016-03-21 17:37:52 +0100833
tiernoae4a8d12016-07-08 12:30:39 +0200834 return server.id
tierno7edb6752016-03-21 17:37:52 +0100835# except nvExceptions.NotFound as e:
836# error_value=-vimconn.HTTP_Not_Found
837# error_text= "vm instance %s not found" % vm_id
tierno8e995ce2016-09-22 08:13:00 +0000838 except (ksExceptions.ClientException, nvExceptions.ClientException, ConnectionError
839 ) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200840 self._format_exception(e)
841 except TypeError as e:
842 raise vimconn.vimconnException(type(e).__name__ + ": "+ str(e), http_code=vimconn.HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +0100843
tiernoae4a8d12016-07-08 12:30:39 +0200844 def get_vminstance(self,vm_id):
tierno7edb6752016-03-21 17:37:52 +0100845 '''Returns the VM instance information from VIM'''
tiernoae4a8d12016-07-08 12:30:39 +0200846 #self.logger.debug("Getting VM from VIM")
tierno7edb6752016-03-21 17:37:52 +0100847 try:
848 self._reload_connection()
849 server = self.nova.servers.find(id=vm_id)
850 #TODO parse input and translate to VIM format (openmano_schemas.new_vminstance_response_schema)
tiernoae4a8d12016-07-08 12:30:39 +0200851 return server.to_dict()
tierno8e995ce2016-09-22 08:13:00 +0000852 except (ksExceptions.ClientException, nvExceptions.ClientException, nvExceptions.NotFound, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200853 self._format_exception(e)
854
855 def get_vminstance_console(self,vm_id, console_type="vnc"):
tierno7edb6752016-03-21 17:37:52 +0100856 '''
857 Get a console for the virtual machine
858 Params:
859 vm_id: uuid of the VM
860 console_type, can be:
861 "novnc" (by default), "xvpvnc" for VNC types,
862 "rdp-html5" for RDP types, "spice-html5" for SPICE types
tiernoae4a8d12016-07-08 12:30:39 +0200863 Returns dict with the console parameters:
864 protocol: ssh, ftp, http, https, ...
865 server: usually ip address
866 port: the http, ssh, ... port
867 suffix: extra text, e.g. the http path and query string
tierno7edb6752016-03-21 17:37:52 +0100868 '''
tiernoae4a8d12016-07-08 12:30:39 +0200869 self.logger.debug("Getting VM CONSOLE from VIM")
tierno7edb6752016-03-21 17:37:52 +0100870 try:
871 self._reload_connection()
872 server = self.nova.servers.find(id=vm_id)
873 if console_type == None or console_type == "novnc":
874 console_dict = server.get_vnc_console("novnc")
875 elif console_type == "xvpvnc":
876 console_dict = server.get_vnc_console(console_type)
877 elif console_type == "rdp-html5":
878 console_dict = server.get_rdp_console(console_type)
879 elif console_type == "spice-html5":
880 console_dict = server.get_spice_console(console_type)
881 else:
tiernoae4a8d12016-07-08 12:30:39 +0200882 raise vimconn.vimconnException("console type '{}' not allowed".format(console_type), http_code=vimconn.HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +0100883
884 console_dict1 = console_dict.get("console")
885 if console_dict1:
886 console_url = console_dict1.get("url")
887 if console_url:
888 #parse console_url
889 protocol_index = console_url.find("//")
890 suffix_index = console_url[protocol_index+2:].find("/") + protocol_index+2
891 port_index = console_url[protocol_index+2:suffix_index].find(":") + protocol_index+2
892 if protocol_index < 0 or port_index<0 or suffix_index<0:
893 return -vimconn.HTTP_Internal_Server_Error, "Unexpected response from VIM"
894 console_dict={"protocol": console_url[0:protocol_index],
895 "server": console_url[protocol_index+2:port_index],
896 "port": console_url[port_index:suffix_index],
897 "suffix": console_url[suffix_index+1:]
898 }
899 protocol_index += 2
tiernoae4a8d12016-07-08 12:30:39 +0200900 return console_dict
901 raise vimconn.vimconnUnexpectedResponse("Unexpected response from VIM")
tierno7edb6752016-03-21 17:37:52 +0100902
tierno8e995ce2016-09-22 08:13:00 +0000903 except (nvExceptions.NotFound, ksExceptions.ClientException, nvExceptions.ClientException, nvExceptions.BadRequest, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200904 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100905
tiernoae4a8d12016-07-08 12:30:39 +0200906 def delete_vminstance(self, vm_id):
907 '''Removes a VM instance from VIM. Returns the old identifier
tierno7edb6752016-03-21 17:37:52 +0100908 '''
tiernoae4a8d12016-07-08 12:30:39 +0200909 #print "osconnector: Getting VM from VIM"
tierno7edb6752016-03-21 17:37:52 +0100910 try:
911 self._reload_connection()
912 #delete VM ports attached to this networks before the virtual machine
913 ports = self.neutron.list_ports(device_id=vm_id)
914 for p in ports['ports']:
915 try:
916 self.neutron.delete_port(p["id"])
917 except Exception as e:
tiernoae4a8d12016-07-08 12:30:39 +0200918 self.logger.error("Error deleting port: " + type(e).__name__ + ": "+ str(e))
montesmoreno0c8def02016-12-22 12:16:23 +0000919
920 #commented because detaching the volumes makes the servers.delete not work properly ?!?
921 #dettach volumes attached
922 server = self.nova.servers.get(vm_id)
923 volumes_attached_dict = server._info['os-extended-volumes:volumes_attached']
924 #for volume in volumes_attached_dict:
925 # self.cinder.volumes.detach(volume['id'])
926
tierno7edb6752016-03-21 17:37:52 +0100927 self.nova.servers.delete(vm_id)
montesmoreno0c8def02016-12-22 12:16:23 +0000928
929 #delete volumes.
930 #Although having detached them should have them in active status
931 #we ensure in this loop
932 keep_waiting = True
933 elapsed_time = 0
934 while keep_waiting and elapsed_time < volume_timeout:
935 keep_waiting = False
936 for volume in volumes_attached_dict:
937 if self.cinder.volumes.get(volume['id']).status != 'available':
938 keep_waiting = True
939 else:
940 self.cinder.volumes.delete(volume['id'])
941 if keep_waiting:
942 time.sleep(1)
943 elapsed_time += 1
944
tiernoae4a8d12016-07-08 12:30:39 +0200945 return vm_id
tierno8e995ce2016-09-22 08:13:00 +0000946 except (nvExceptions.NotFound, ksExceptions.ClientException, nvExceptions.ClientException, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200947 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100948 #TODO insert exception vimconn.HTTP_Unauthorized
949 #if reaching here is because an exception
tierno7edb6752016-03-21 17:37:52 +0100950
tiernoae4a8d12016-07-08 12:30:39 +0200951 def refresh_vms_status(self, vm_list):
952 '''Get the status of the virtual machines and their interfaces/ports
953 Params: the list of VM identifiers
954 Returns a dictionary with:
955 vm_id: #VIM id of this Virtual Machine
956 status: #Mandatory. Text with one of:
957 # DELETED (not found at vim)
958 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
959 # OTHER (Vim reported other status not understood)
960 # ERROR (VIM indicates an ERROR status)
961 # ACTIVE, PAUSED, SUSPENDED, INACTIVE (not running),
962 # CREATING (on building process), ERROR
963 # ACTIVE:NoMgmtIP (Active but any of its interface has an IP address
964 #
965 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
966 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
967 interfaces:
968 - vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
969 mac_address: #Text format XX:XX:XX:XX:XX:XX
970 vim_net_id: #network id where this interface is connected
971 vim_interface_id: #interface/port VIM id
972 ip_address: #null, or text with IPv4, IPv6 address
tierno7edb6752016-03-21 17:37:52 +0100973 '''
tiernoae4a8d12016-07-08 12:30:39 +0200974 vm_dict={}
975 self.logger.debug("refresh_vms status: Getting tenant VM instance information from VIM")
976 for vm_id in vm_list:
977 vm={}
978 try:
979 vm_vim = self.get_vminstance(vm_id)
980 if vm_vim['status'] in vmStatus2manoFormat:
981 vm['status'] = vmStatus2manoFormat[ vm_vim['status'] ]
tierno7edb6752016-03-21 17:37:52 +0100982 else:
tiernoae4a8d12016-07-08 12:30:39 +0200983 vm['status'] = "OTHER"
984 vm['error_msg'] = "VIM status reported " + vm_vim['status']
tierno8e995ce2016-09-22 08:13:00 +0000985 try:
986 vm['vim_info'] = yaml.safe_dump(vm_vim, default_flow_style=True, width=256)
987 except yaml.representer.RepresenterError:
988 vm['vim_info'] = str(vm_vim)
tiernoae4a8d12016-07-08 12:30:39 +0200989 vm["interfaces"] = []
990 if vm_vim.get('fault'):
991 vm['error_msg'] = str(vm_vim['fault'])
992 #get interfaces
tierno7edb6752016-03-21 17:37:52 +0100993 try:
tiernoae4a8d12016-07-08 12:30:39 +0200994 self._reload_connection()
995 port_dict=self.neutron.list_ports(device_id=vm_id)
996 for port in port_dict["ports"]:
997 interface={}
tierno8e995ce2016-09-22 08:13:00 +0000998 try:
999 interface['vim_info'] = yaml.safe_dump(port, default_flow_style=True, width=256)
1000 except yaml.representer.RepresenterError:
1001 interface['vim_info'] = str(port)
tiernoae4a8d12016-07-08 12:30:39 +02001002 interface["mac_address"] = port.get("mac_address")
1003 interface["vim_net_id"] = port["network_id"]
1004 interface["vim_interface_id"] = port["id"]
1005 ips=[]
1006 #look for floating ip address
1007 floating_ip_dict = self.neutron.list_floatingips(port_id=port["id"])
1008 if floating_ip_dict.get("floatingips"):
1009 ips.append(floating_ip_dict["floatingips"][0].get("floating_ip_address") )
tierno7edb6752016-03-21 17:37:52 +01001010
tiernoae4a8d12016-07-08 12:30:39 +02001011 for subnet in port["fixed_ips"]:
1012 ips.append(subnet["ip_address"])
1013 interface["ip_address"] = ";".join(ips)
1014 vm["interfaces"].append(interface)
1015 except Exception as e:
1016 self.logger.error("Error getting vm interface information " + type(e).__name__ + ": "+ str(e))
1017 except vimconn.vimconnNotFoundException as e:
1018 self.logger.error("Exception getting vm status: %s", str(e))
1019 vm['status'] = "DELETED"
1020 vm['error_msg'] = str(e)
1021 except vimconn.vimconnException as e:
1022 self.logger.error("Exception getting vm status: %s", str(e))
1023 vm['status'] = "VIM_ERROR"
1024 vm['error_msg'] = str(e)
1025 vm_dict[vm_id] = vm
1026 return vm_dict
tierno7edb6752016-03-21 17:37:52 +01001027
tiernoae4a8d12016-07-08 12:30:39 +02001028 def action_vminstance(self, vm_id, action_dict):
tierno7edb6752016-03-21 17:37:52 +01001029 '''Send and action over a VM instance from VIM
tiernoae4a8d12016-07-08 12:30:39 +02001030 Returns the vm_id if the action was successfully sent to the VIM'''
1031 self.logger.debug("Action over VM '%s': %s", vm_id, str(action_dict))
tierno7edb6752016-03-21 17:37:52 +01001032 try:
1033 self._reload_connection()
1034 server = self.nova.servers.find(id=vm_id)
1035 if "start" in action_dict:
1036 if action_dict["start"]=="rebuild":
1037 server.rebuild()
1038 else:
1039 if server.status=="PAUSED":
1040 server.unpause()
1041 elif server.status=="SUSPENDED":
1042 server.resume()
1043 elif server.status=="SHUTOFF":
1044 server.start()
1045 elif "pause" in action_dict:
1046 server.pause()
1047 elif "resume" in action_dict:
1048 server.resume()
1049 elif "shutoff" in action_dict or "shutdown" in action_dict:
1050 server.stop()
1051 elif "forceOff" in action_dict:
1052 server.stop() #TODO
1053 elif "terminate" in action_dict:
1054 server.delete()
1055 elif "createImage" in action_dict:
1056 server.create_image()
1057 #"path":path_schema,
1058 #"description":description_schema,
1059 #"name":name_schema,
1060 #"metadata":metadata_schema,
1061 #"imageRef": id_schema,
1062 #"disk": {"oneOf":[{"type": "null"}, {"type":"string"}] },
1063 elif "rebuild" in action_dict:
1064 server.rebuild(server.image['id'])
1065 elif "reboot" in action_dict:
1066 server.reboot() #reboot_type='SOFT'
1067 elif "console" in action_dict:
1068 console_type = action_dict["console"]
1069 if console_type == None or console_type == "novnc":
1070 console_dict = server.get_vnc_console("novnc")
1071 elif console_type == "xvpvnc":
1072 console_dict = server.get_vnc_console(console_type)
1073 elif console_type == "rdp-html5":
1074 console_dict = server.get_rdp_console(console_type)
1075 elif console_type == "spice-html5":
1076 console_dict = server.get_spice_console(console_type)
1077 else:
tiernoae4a8d12016-07-08 12:30:39 +02001078 raise vimconn.vimconnException("console type '{}' not allowed".format(console_type),
1079 http_code=vimconn.HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001080 try:
1081 console_url = console_dict["console"]["url"]
1082 #parse console_url
1083 protocol_index = console_url.find("//")
1084 suffix_index = console_url[protocol_index+2:].find("/") + protocol_index+2
1085 port_index = console_url[protocol_index+2:suffix_index].find(":") + protocol_index+2
1086 if protocol_index < 0 or port_index<0 or suffix_index<0:
tiernoae4a8d12016-07-08 12:30:39 +02001087 raise vimconn.vimconnException("Unexpected response from VIM " + str(console_dict))
tierno7edb6752016-03-21 17:37:52 +01001088 console_dict2={"protocol": console_url[0:protocol_index],
1089 "server": console_url[protocol_index+2 : port_index],
1090 "port": int(console_url[port_index+1 : suffix_index]),
1091 "suffix": console_url[suffix_index+1:]
1092 }
tiernoae4a8d12016-07-08 12:30:39 +02001093 return console_dict2
1094 except Exception as e:
1095 raise vimconn.vimconnException("Unexpected response from VIM " + str(console_dict))
tierno7edb6752016-03-21 17:37:52 +01001096
tiernoae4a8d12016-07-08 12:30:39 +02001097 return vm_id
tierno8e995ce2016-09-22 08:13:00 +00001098 except (ksExceptions.ClientException, nvExceptions.ClientException, nvExceptions.NotFound, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +02001099 self._format_exception(e)
1100 #TODO insert exception vimconn.HTTP_Unauthorized
1101
1102#NOT USED FUNCTIONS
1103
1104 def new_external_port(self, port_data):
1105 #TODO openstack if needed
1106 '''Adds a external port to VIM'''
1107 '''Returns the port identifier'''
1108 return -vimconn.HTTP_Internal_Server_Error, "osconnector.new_external_port() not implemented"
1109
1110 def connect_port_network(self, port_id, network_id, admin=False):
1111 #TODO openstack if needed
1112 '''Connects a external port to a network'''
1113 '''Returns status code of the VIM response'''
1114 return -vimconn.HTTP_Internal_Server_Error, "osconnector.connect_port_network() not implemented"
1115
1116 def new_user(self, user_name, user_passwd, tenant_id=None):
1117 '''Adds a new user to openstack VIM'''
1118 '''Returns the user identifier'''
1119 self.logger.debug("osconnector: Adding a new user to VIM")
1120 try:
1121 self._reload_connection()
1122 user=self.keystone.users.create(user_name, user_passwd, tenant_id=tenant_id)
1123 #self.keystone.tenants.add_user(self.k_creds["username"], #role)
1124 return user.id
1125 except ksExceptions.ConnectionError as e:
1126 error_value=-vimconn.HTTP_Bad_Request
1127 error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0]))
1128 except ksExceptions.ClientException as e: #TODO remove
tierno7edb6752016-03-21 17:37:52 +01001129 error_value=-vimconn.HTTP_Bad_Request
1130 error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0]))
1131 #TODO insert exception vimconn.HTTP_Unauthorized
1132 #if reaching here is because an exception
1133 if self.debug:
tiernoae4a8d12016-07-08 12:30:39 +02001134 self.logger.debug("new_user " + error_text)
tierno7edb6752016-03-21 17:37:52 +01001135 return error_value, error_text
tiernoae4a8d12016-07-08 12:30:39 +02001136
1137 def delete_user(self, user_id):
1138 '''Delete a user from openstack VIM'''
1139 '''Returns the user identifier'''
1140 if self.debug:
1141 print "osconnector: Deleting a user from VIM"
1142 try:
1143 self._reload_connection()
1144 self.keystone.users.delete(user_id)
1145 return 1, user_id
1146 except ksExceptions.ConnectionError as e:
1147 error_value=-vimconn.HTTP_Bad_Request
1148 error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0]))
1149 except ksExceptions.NotFound as e:
1150 error_value=-vimconn.HTTP_Not_Found
1151 error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0]))
1152 except ksExceptions.ClientException as e: #TODO remove
1153 error_value=-vimconn.HTTP_Bad_Request
1154 error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0]))
1155 #TODO insert exception vimconn.HTTP_Unauthorized
1156 #if reaching here is because an exception
1157 if self.debug:
1158 print "delete_tenant " + error_text
1159 return error_value, error_text
1160
tierno7edb6752016-03-21 17:37:52 +01001161 def get_hosts_info(self):
1162 '''Get the information of deployed hosts
1163 Returns the hosts content'''
1164 if self.debug:
1165 print "osconnector: Getting Host info from VIM"
1166 try:
1167 h_list=[]
1168 self._reload_connection()
1169 hypervisors = self.nova.hypervisors.list()
1170 for hype in hypervisors:
1171 h_list.append( hype.to_dict() )
1172 return 1, {"hosts":h_list}
1173 except nvExceptions.NotFound as e:
1174 error_value=-vimconn.HTTP_Not_Found
1175 error_text= (str(e) if len(e.args)==0 else str(e.args[0]))
1176 except (ksExceptions.ClientException, nvExceptions.ClientException) as e:
1177 error_value=-vimconn.HTTP_Bad_Request
1178 error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0]))
1179 #TODO insert exception vimconn.HTTP_Unauthorized
1180 #if reaching here is because an exception
1181 if self.debug:
1182 print "get_hosts_info " + error_text
1183 return error_value, error_text
1184
1185 def get_hosts(self, vim_tenant):
1186 '''Get the hosts and deployed instances
1187 Returns the hosts content'''
1188 r, hype_dict = self.get_hosts_info()
1189 if r<0:
1190 return r, hype_dict
1191 hypervisors = hype_dict["hosts"]
1192 try:
1193 servers = self.nova.servers.list()
1194 for hype in hypervisors:
1195 for server in servers:
1196 if server.to_dict()['OS-EXT-SRV-ATTR:hypervisor_hostname']==hype['hypervisor_hostname']:
1197 if 'vm' in hype:
1198 hype['vm'].append(server.id)
1199 else:
1200 hype['vm'] = [server.id]
1201 return 1, hype_dict
1202 except nvExceptions.NotFound as e:
1203 error_value=-vimconn.HTTP_Not_Found
1204 error_text= (str(e) if len(e.args)==0 else str(e.args[0]))
1205 except (ksExceptions.ClientException, nvExceptions.ClientException) as e:
1206 error_value=-vimconn.HTTP_Bad_Request
1207 error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0]))
1208 #TODO insert exception vimconn.HTTP_Unauthorized
1209 #if reaching here is because an exception
1210 if self.debug:
1211 print "get_hosts " + error_text
1212 return error_value, error_text
1213
tierno7edb6752016-03-21 17:37:52 +01001214