1 # -*- coding: utf-8 -*-
4 # Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U.
5 # This file is part of openmano
8 # Licensed under the Apache License, Version 2.0 (the "License"); you may
9 # not use this file except in compliance with the License. You may obtain
10 # a copy of the License at
12 # http://www.apache.org/licenses/LICENSE-2.0
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17 # License for the specific language governing permissions and limitations
20 # For those usages not covered by the Apache License, Version 2.0 please
21 # contact with: nfvlabs@tid.es
25 vimconn implement an Abstract class for the vim connector plugins
26 with the definition of the method to be implemented.
28 __author__
="Alfonso Tierno"
29 __date__
="$16-oct-2015 11:09:29$"
34 HTTP_Bad_Request
= 400
35 HTTP_Unauthorized
= 401
37 HTTP_Method_Not_Allowed
= 405
38 HTTP_Request_Timeout
= 408
40 HTTP_Not_Implemented
= 501
41 HTTP_Service_Unavailable
= 503
42 HTTP_Internal_Server_Error
= 500
44 class vimconnException(Exception):
45 '''Common and base class Exception for all vimconnector exceptions'''
46 def __init__(self
, message
, http_code
=HTTP_Bad_Request
):
47 Exception.__init
__(self
, message
)
48 self
.http_code
= http_code
50 class vimconnConnectionException(vimconnException
):
51 '''Connectivity error with the VIM'''
52 def __init__(self
, message
, http_code
=HTTP_Service_Unavailable
):
53 vimconnException
.__init
__(self
, message
, http_code
)
55 class vimconnUnexpectedResponse(vimconnException
):
56 '''Get an wrong response from VIM'''
57 def __init__(self
, message
, http_code
=HTTP_Service_Unavailable
):
58 vimconnException
.__init
__(self
, message
, http_code
)
60 class vimconnAuthException(vimconnException
):
61 '''Invalid credentials or authorization to perform this action over the VIM'''
62 def __init__(self
, message
, http_code
=HTTP_Unauthorized
):
63 vimconnException
.__init
__(self
, message
, http_code
)
65 class vimconnNotFoundException(vimconnException
):
66 '''The item is not found at VIM'''
67 def __init__(self
, message
, http_code
=HTTP_Not_Found
):
68 vimconnException
.__init
__(self
, message
, http_code
)
70 class vimconnConflictException(vimconnException
):
71 '''There is a conflict, e.g. more item found than one'''
72 def __init__(self
, message
, http_code
=HTTP_Conflict
):
73 vimconnException
.__init
__(self
, message
, http_code
)
75 class vimconnNotImplemented(vimconnException
):
76 '''The method is not implemented by the connected'''
77 def __init__(self
, message
, http_code
=HTTP_Not_Implemented
):
78 vimconnException
.__init
__(self
, message
, http_code
)
81 '''Abstract base class for all the VIM connector plugins
82 These plugins must implement a vimconnector class derived from this
85 def __init__(self
, uuid
, name
, tenant_id
, tenant_name
, url
, url_admin
=None, user
=None, passwd
=None, log_level
=None, config
={}):
89 self
.url_admin
= url_admin
90 self
.tenant_id
= tenant_id
91 self
.tenant_name
= tenant_name
95 self
.logger
= logging
.getLogger('openmano.vim')
97 self
.logger
.setLevel( getattr(logging
, log_level
) )
98 if not self
.url_admin
: #try to use normal url
99 self
.url_admin
= self
.url
101 def __getitem__(self
,index
):
102 if index
=='tenant_id':
103 return self
.tenant_id
104 if index
=='tenant_name':
105 return self
.tenant_name
112 elif index
=='passwd':
116 elif index
=='url_admin':
117 return self
.url_admin
118 elif index
=="config":
121 raise KeyError("Invalid key '%s'" %str
(index
))
123 def __setitem__(self
,index
, value
):
124 if index
=='tenant_id':
125 self
.tenant_id
= value
126 if index
=='tenant_name':
127 self
.tenant_name
= value
134 elif index
=='passwd':
138 elif index
=='url_admin':
139 self
.url_admin
= value
141 raise KeyError("Invalid key '%s'" %str
(index
))
143 def new_tenant(self
,tenant_name
,tenant_description
):
144 '''Adds a new tenant to VIM with this name and description,
145 returns the tenant identifier'''
146 raise vimconnNotImplemented( "Should have implemented this" )
148 def delete_tenant(self
,tenant_id
,):
149 '''Delete a tenant from VIM'''
150 '''Returns the tenant identifier'''
151 raise vimconnNotImplemented( "Should have implemented this" )
153 def get_tenant_list(self
, filter_dict
={}):
154 '''Obtain tenants of VIM
155 filter_dict can contain the following keys:
156 name: filter by tenant name
157 id: filter by tenant uuid/id
159 Returns the tenant list of dictionaries:
160 [{'name':'<name>, 'id':'<id>, ...}, ...]
162 raise vimconnNotImplemented( "Should have implemented this" )
164 def new_network(self
,net_name
, net_type
, ip_profile
=None, shared
=False):
165 '''Adds a tenant network to VIM
167 net_type can be 'bridge','data'.'ptp'. TODO: this need to be revised
168 ip_profile is a dict containing the IP parameters of the network
170 Returns the network identifier'''
171 raise vimconnNotImplemented( "Should have implemented this" )
173 def get_network_list(self
, filter_dict
={}):
174 '''Obtain tenant networks of VIM
180 admin_state_up: boolean
182 Returns the network list of dictionaries:
183 [{<the fields at Filter_dict plus some VIM specific>}, ...]
186 raise vimconnNotImplemented( "Should have implemented this" )
188 def get_network(self
, net_id
):
189 '''Obtain network details of net_id VIM network'
190 Return a dict with the fields at filter_dict (see get_network_list) plus some VIM specific>}, ...]'''
191 raise vimconnNotImplemented( "Should have implemented this" )
193 def delete_network(self
, net_id
):
194 '''Deletes a tenant network from VIM, provide the network id.
195 Returns the network identifier or raise an exception'''
196 raise vimconnNotImplemented( "Should have implemented this" )
198 def refresh_nets_status(self
, net_list
):
199 '''Get the status of the networks
200 Params: the list of network identifiers
201 Returns a dictionary with:
202 net_id: #VIM id of this network
203 status: #Mandatory. Text with one of:
204 # DELETED (not found at vim)
205 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
206 # OTHER (Vim reported other status not understood)
207 # ERROR (VIM indicates an ERROR status)
208 # ACTIVE, INACTIVE, DOWN (admin down),
209 # BUILD (on building process)
211 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
212 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
215 raise vimconnNotImplemented( "Should have implemented this" )
217 def get_flavor(self
, flavor_id
):
218 '''Obtain flavor details from the VIM
219 Returns the flavor dict details {'id':<>, 'name':<>, other vim specific } #TODO to concrete
221 raise vimconnNotImplemented( "Should have implemented this" )
223 def new_flavor(self
, flavor_data
):
224 '''Adds a tenant flavor to VIM
225 flavor_data contains a dictionary with information, keys:
227 ram: memory (cloud type) in MBytes
228 vpcus: cpus (cloud type)
229 extended: EPA parameters
230 - numas: #items requested in same NUMA
231 memory: number of 1G huge pages memory
232 paired-threads|cores|threads: number of paired hyperthreads, complete cores OR individual threads
233 interfaces: # passthrough(PT) or SRIOV interfaces attached to this numa
234 - name: interface name
235 dedicated: yes|no|yes:sriov; for PT, SRIOV or only one SRIOV for the physical NIC
236 bandwidth: X Gbps; requested guarantee bandwidth
237 vpci: requested virtual PCI address
244 Returns the flavor identifier'''
245 raise vimconnNotImplemented( "Should have implemented this" )
247 def delete_flavor(self
, flavor_id
):
248 '''Deletes a tenant flavor from VIM identify by its id
249 Returns the used id or raise an exception'''
250 raise vimconnNotImplemented( "Should have implemented this" )
252 def new_image(self
,image_dict
):
254 Adds a tenant image to VIM
256 200, image-id if the image is created
257 <0, message if there is an error
259 raise vimconnNotImplemented( "Should have implemented this" )
261 def delete_image(self
, image_id
):
262 '''Deletes a tenant image from VIM'''
263 '''Returns the HTTP response code and a message indicating details of the success or fail'''
264 raise vimconnNotImplemented( "Should have implemented this" )
266 def get_image_id_from_path(self
, path
):
267 '''Get the image id from image path in the VIM database. Returns the image_id'''
268 raise vimconnNotImplemented( "Should have implemented this" )
270 def get_image_list(self
, filter_dict
={}):
271 '''Obtain tenant images from VIM
275 checksum: image checksum
277 Returns the image list of dictionaries:
278 [{<the fields at Filter_dict plus some VIM specific>}, ...]
281 raise vimconnNotImplemented( "Should have implemented this" )
283 def new_vminstance(self
,name
,description
,start
,image_id
,flavor_id
,net_list
,cloud_config
=None):
284 '''Adds a VM instance to VIM
286 start: indicates if VM must start or boot in pause mode. Ignored
287 image_id,flavor_id: image and flavor uuid
288 net_list: list of interfaces, each one is a dictionary with:
290 net_id: network uuid to connect
291 vpci: virtual vcpi to assign
292 model: interface model, virtio, e2000, ...
294 use: 'data', 'bridge', 'mgmt'
295 type: 'virtual', 'PF', 'VF', 'VFnotShared'
296 vim_id: filled/added by this function
297 cloud_config: can be a text script to be passed directly to cloud-init,
298 or an object to inject users and ssh keys with format:
299 key-pairs: [] list of keys to install to the default user
300 users: [{ name, key-pairs: []}] list of users to add with their key-pair
301 #TODO ip, security groups
302 Returns >=0, the instance identifier
305 raise vimconnNotImplemented( "Should have implemented this" )
307 def get_vminstance(self
,vm_id
):
308 '''Returns the VM instance information from VIM'''
309 raise vimconnNotImplemented( "Should have implemented this" )
311 def delete_vminstance(self
, vm_id
):
312 '''Removes a VM instance from VIM'''
313 '''Returns the instance identifier'''
314 raise vimconnNotImplemented( "Should have implemented this" )
316 def refresh_vms_status(self
, vm_list
):
317 '''Get the status of the virtual machines and their interfaces/ports
318 Params: the list of VM identifiers
319 Returns a dictionary with:
320 vm_id: #VIM id of this Virtual Machine
321 status: #Mandatory. Text with one of:
322 # DELETED (not found at vim)
323 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
324 # OTHER (Vim reported other status not understood)
325 # ERROR (VIM indicates an ERROR status)
326 # ACTIVE, PAUSED, SUSPENDED, INACTIVE (not running),
327 # CREATING (on building process), ERROR
328 # ACTIVE:NoMgmtIP (Active but any of its interface has an IP address
330 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
331 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
333 - vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
334 mac_address: #Text format XX:XX:XX:XX:XX:XX
335 vim_net_id: #network id where this interface is connected
336 vim_interface_id: #interface/port VIM id
337 ip_address: #null, or text with IPv4, IPv6 address
339 raise vimconnNotImplemented( "Should have implemented this" )
341 def action_vminstance(self
, vm_id
, action_dict
):
342 '''Send and action over a VM instance from VIM
343 Returns the vm_id if the action was successfully sent to the VIM'''
344 raise vimconnNotImplemented( "Should have implemented this" )
346 def get_vminstance_console(self
,vm_id
, console_type
="vnc"):
348 Get a console for the virtual machine
350 vm_id: uuid of the VM
351 console_type, can be:
352 "novnc" (by default), "xvpvnc" for VNC types,
353 "rdp-html5" for RDP types, "spice-html5" for SPICE types
354 Returns dict with the console parameters:
355 protocol: ssh, ftp, http, https, ...
356 server: usually ip address
357 port: the http, ssh, ... port
358 suffix: extra text, e.g. the http path and query string
360 raise vimconnNotImplemented( "Should have implemented this" )
362 #NOT USED METHODS in current version
364 def host_vim2gui(self
, host
, server_dict
):
365 '''Transform host dictionary from VIM format to GUI format,
366 and append to the server_dict
368 raise vimconnNotImplemented( "Should have implemented this" )
370 def get_hosts_info(self
):
371 '''Get the information of deployed hosts
372 Returns the hosts content'''
373 raise vimconnNotImplemented( "Should have implemented this" )
375 def get_hosts(self
, vim_tenant
):
376 '''Get the hosts and deployed instances
377 Returns the hosts content'''
378 raise vimconnNotImplemented( "Should have implemented this" )
380 def get_processor_rankings(self
):
381 '''Get the processor rankings in the VIM database'''
382 raise vimconnNotImplemented( "Should have implemented this" )
384 def new_host(self
, host_data
):
385 '''Adds a new host to VIM'''
386 '''Returns status code of the VIM response'''
387 raise vimconnNotImplemented( "Should have implemented this" )
389 def new_external_port(self
, port_data
):
390 '''Adds a external port to VIM'''
391 '''Returns the port identifier'''
392 raise vimconnNotImplemented( "Should have implemented this" )
394 def new_external_network(self
,net_name
,net_type
):
395 '''Adds a external network to VIM (shared)'''
396 '''Returns the network identifier'''
397 raise vimconnNotImplemented( "Should have implemented this" )
399 def connect_port_network(self
, port_id
, network_id
, admin
=False):
400 '''Connects a external port to a network'''
401 '''Returns status code of the VIM response'''
402 raise vimconnNotImplemented( "Should have implemented this" )
404 def new_vminstancefromJSON(self
, vm_data
):
405 '''Adds a VM instance to VIM'''
406 '''Returns the instance identifier'''
407 raise vimconnNotImplemented( "Should have implemented this" )