2 # Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U.
3 # This file is part of openmano
6 # Licensed under the Apache License, Version 2.0 (the "License"); you may
7 # not use this file except in compliance with the License. You may obtain
8 # a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15 # License for the specific language governing permissions and limitations
18 # For those usages not covered by the Apache License, Version 2.0 please
19 # contact with: nfvlabs@tid.es
23 vimconn implement an Abstract class for the vim connector plugins
24 with the definition of the method to be implemented.
26 __author__
="Alfonso Tierno"
27 __date__
="$16-oct-2015 11:09:29$"
32 HTTP_Bad_Request
= 400
33 HTTP_Unauthorized
= 401
35 HTTP_Method_Not_Allowed
= 405
36 HTTP_Request_Timeout
= 408
38 HTTP_Not_Implemented
= 501
39 HTTP_Service_Unavailable
= 503
40 HTTP_Internal_Server_Error
= 500
42 class vimconnException(Exception):
43 '''Common and base class Exception for all vimconnector exceptions'''
44 def __init__(self
, message
, http_code
=HTTP_Bad_Request
):
45 Exception.__init
__(self
, message
)
46 self
.http_code
= http_code
48 class vimconnConnectionException(vimconnException
):
49 '''Connectivity error with the VIM'''
50 def __init__(self
, message
, http_code
=HTTP_Service_Unavailable
):
51 vimconnException
.__init
__(self
, message
, http_code
)
53 class vimconnUnexpectedResponse(vimconnException
):
54 '''Get an wrong response from VIM'''
55 def __init__(self
, message
, http_code
=HTTP_Service_Unavailable
):
56 vimconnException
.__init
__(self
, message
, http_code
)
58 class vimconnAuthException(vimconnException
):
59 '''Invalid credentials or authorization to perform this action over the VIM'''
60 def __init__(self
, message
, http_code
=HTTP_Unauthorized
):
61 vimconnException
.__init
__(self
, message
, http_code
)
63 class vimconnNotFoundException(vimconnException
):
64 '''The item is not found at VIM'''
65 def __init__(self
, message
, http_code
=HTTP_Not_Found
):
66 vimconnException
.__init
__(self
, message
, http_code
)
68 class vimconnConflictException(vimconnException
):
69 '''There is a conflict, e.g. more item found than one'''
70 def __init__(self
, message
, http_code
=HTTP_Conflict
):
71 vimconnException
.__init
__(self
, message
, http_code
)
73 class vimconnNotImplemented(vimconnException
):
74 '''The method is not implemented by the connected'''
75 def __init__(self
, message
, http_code
=HTTP_Not_Implemented
):
76 vimconnException
.__init
__(self
, message
, http_code
)
79 '''Abstract base class for all the VIM connector plugins
80 These plugins must implement a vimconnector class derived from this
83 def __init__(self
, uuid
, name
, tenant_id
, tenant_name
, url
, url_admin
=None, user
=None, passwd
=None, log_level
="ERROR", config
={}):
87 self
.url_admin
= url_admin
88 self
.tenant_id
= tenant_id
89 self
.tenant_name
= tenant_name
93 self
.logger
= logging
.getLogger('mano.vim')
94 self
.logger
.setLevel( getattr(logging
, log_level
) )
95 if not self
.url_admin
: #try to use normal url
96 self
.url_admin
= self
.url
98 def __getitem__(self
,index
):
99 if index
=='tenant_id':
100 return self
.tenant_id
101 if index
=='tenant_name':
102 return self
.tenant_name
109 elif index
=='passwd':
113 elif index
=='url_admin':
114 return self
.url_admin
115 elif index
=="config":
118 raise KeyError("Invalid key '%s'" %str
(index
))
120 def __setitem__(self
,index
, value
):
121 if index
=='tenant_id':
122 self
.tenant_id
= value
123 if index
=='tenant_name':
124 self
.tenant_name
= value
131 elif index
=='passwd':
135 elif index
=='url_admin':
136 self
.url_admin
= value
138 raise KeyError("Invalid key '%s'" %str
(index
))
140 def new_tenant(self
,tenant_name
,tenant_description
):
141 '''Adds a new tenant to VIM with this name and description,
142 returns the tenant identifier'''
143 raise vimconnNotImplemented( "Should have implemented this" )
145 def delete_tenant(self
,tenant_id
,):
146 '''Delete a tenant from VIM'''
147 '''Returns the tenant identifier'''
148 raise vimconnNotImplemented( "Should have implemented this" )
150 def get_tenant_list(self
, filter_dict
={}):
151 '''Obtain tenants of VIM
152 filter_dict can contain the following keys:
153 name: filter by tenant name
154 id: filter by tenant uuid/id
156 Returns the tenant list of dictionaries:
157 [{'name':'<name>, 'id':'<id>, ...}, ...]
159 raise vimconnNotImplemented( "Should have implemented this" )
161 def new_network(self
,net_name
, net_type
, shared
=False):
162 '''Adds a tenant network to VIM
163 net_type can be 'bridge','data'.'ptp'. TODO: this need to be revised
165 Returns the network identifier'''
166 raise vimconnNotImplemented( "Should have implemented this" )
168 def get_network_list(self
, filter_dict
={}):
169 '''Obtain tenant networks of VIM
175 admin_state_up: boolean
177 Returns the network list of dictionaries:
178 [{<the fields at Filter_dict plus some VIM specific>}, ...]
181 raise vimconnNotImplemented( "Should have implemented this" )
183 def get_network(self
, net_id
):
184 '''Obtain network details of net_id VIM network'
185 Return a dict with the fields at filter_dict (see get_network_list) plus some VIM specific>}, ...]'''
186 raise vimconnNotImplemented( "Should have implemented this" )
188 def delete_network(self
, net_id
):
189 '''Deletes a tenant network from VIM, provide the network id.
190 Returns the network identifier or raise an exception'''
191 raise vimconnNotImplemented( "Should have implemented this" )
193 def refresh_nets_status(self
, net_list
):
194 '''Get the status of the networks
195 Params: the list of network identifiers
196 Returns a dictionary with:
197 net_id: #VIM id of this network
198 status: #Mandatory. Text with one of:
199 # DELETED (not found at vim)
200 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
201 # OTHER (Vim reported other status not understood)
202 # ERROR (VIM indicates an ERROR status)
203 # ACTIVE, INACTIVE, DOWN (admin down),
204 # BUILD (on building process)
206 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
207 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
210 raise vimconnNotImplemented( "Should have implemented this" )
212 def get_flavor(self
, flavor_id
):
213 '''Obtain flavor details from the VIM
214 Returns the flavor dict details {'id':<>, 'name':<>, other vim specific } #TODO to concrete
216 raise vimconnNotImplemented( "Should have implemented this" )
218 def new_flavor(self
, flavor_data
):
219 '''Adds a tenant flavor to VIM
220 flavor_data contains a dictionary with information, keys:
222 ram: memory (cloud type) in MBytes
223 vpcus: cpus (cloud type)
224 extended: EPA parameters
225 - numas: #items requested in same NUMA
226 memory: number of 1G huge pages memory
227 paired-threads|cores|threads: number of paired hyperthreads, complete cores OR individual threads
228 interfaces: # passthrough(PT) or SRIOV interfaces attached to this numa
229 - name: interface name
230 dedicated: yes|no|yes:sriov; for PT, SRIOV or only one SRIOV for the physical NIC
231 bandwidth: X Gbps; requested guarantee bandwidth
232 vpci: requested virtual PCI address
239 Returns the flavor identifier'''
240 raise vimconnNotImplemented( "Should have implemented this" )
242 def delete_flavor(self
, flavor_id
):
243 '''Deletes a tenant flavor from VIM identify by its id
244 Returns the used id or raise an exception'''
245 raise vimconnNotImplemented( "Should have implemented this" )
247 def new_image(self
,image_dict
):
249 Adds a tenant image to VIM
251 200, image-id if the image is created
252 <0, message if there is an error
254 raise vimconnNotImplemented( "Should have implemented this" )
256 def delete_image(self
, image_id
):
257 '''Deletes a tenant image from VIM'''
258 '''Returns the HTTP response code and a message indicating details of the success or fail'''
259 raise vimconnNotImplemented( "Should have implemented this" )
261 def get_image_id_from_path(self
, path
):
262 '''Get the image id from image path in the VIM database'''
264 0,"Image not found" if there are no images with that path
265 1,image-id if there is one image with that path
266 <0,message if there was an error (Image not found, error contacting VIM, more than 1 image with that path, etc.)
268 raise vimconnNotImplemented( "Should have implemented this" )
270 def new_vminstance(self
,name
,description
,start
,image_id
,flavor_id
,net_list
):
271 '''Adds a VM instance to VIM
273 start: indicates if VM must start or boot in pause mode. Ignored
274 image_id,flavor_id: image and flavor uuid
275 net_list: list of interfaces, each one is a dictionary with:
277 net_id: network uuid to connect
278 vpci: virtual vcpi to assign
279 model: interface model, virtio, e2000, ...
281 use: 'data', 'bridge', 'mgmt'
282 type: 'virtual', 'PF', 'VF', 'VFnotShared'
283 vim_id: filled/added by this function
284 #TODO ip, security groups
285 Returns >=0, the instance identifier
288 raise vimconnNotImplemented( "Should have implemented this" )
290 def get_vminstance(self
,vm_id
):
291 '''Returns the VM instance information from VIM'''
292 raise vimconnNotImplemented( "Should have implemented this" )
294 def delete_vminstance(self
, vm_id
):
295 '''Removes a VM instance from VIM'''
296 '''Returns the instance identifier'''
297 raise vimconnNotImplemented( "Should have implemented this" )
299 def refresh_vms_status(self
, vm_list
):
300 '''Get the status of the virtual machines and their interfaces/ports
301 Params: the list of VM identifiers
302 Returns a dictionary with:
303 vm_id: #VIM id of this Virtual Machine
304 status: #Mandatory. Text with one of:
305 # DELETED (not found at vim)
306 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
307 # OTHER (Vim reported other status not understood)
308 # ERROR (VIM indicates an ERROR status)
309 # ACTIVE, PAUSED, SUSPENDED, INACTIVE (not running),
310 # CREATING (on building process), ERROR
311 # ACTIVE:NoMgmtIP (Active but any of its interface has an IP address
313 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
314 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
316 - vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
317 mac_address: #Text format XX:XX:XX:XX:XX:XX
318 vim_net_id: #network id where this interface is connected
319 vim_interface_id: #interface/port VIM id
320 ip_address: #null, or text with IPv4, IPv6 address
322 raise vimconnNotImplemented( "Should have implemented this" )
324 def action_vminstance(self
, vm_id
, action_dict
):
325 '''Send and action over a VM instance from VIM
326 Returns the vm_id if the action was successfully sent to the VIM'''
327 raise vimconnNotImplemented( "Should have implemented this" )
329 def get_vminstance_console(self
,vm_id
, console_type
="vnc"):
331 Get a console for the virtual machine
333 vm_id: uuid of the VM
334 console_type, can be:
335 "novnc" (by default), "xvpvnc" for VNC types,
336 "rdp-html5" for RDP types, "spice-html5" for SPICE types
337 Returns dict with the console parameters:
338 protocol: ssh, ftp, http, https, ...
339 server: usually ip address
340 port: the http, ssh, ... port
341 suffix: extra text, e.g. the http path and query string
343 raise vimconnNotImplemented( "Should have implemented this" )
345 #NOT USED METHODS in current version
347 def host_vim2gui(self
, host
, server_dict
):
348 '''Transform host dictionary from VIM format to GUI format,
349 and append to the server_dict
351 raise vimconnNotImplemented( "Should have implemented this" )
353 def get_hosts_info(self
):
354 '''Get the information of deployed hosts
355 Returns the hosts content'''
356 raise vimconnNotImplemented( "Should have implemented this" )
358 def get_hosts(self
, vim_tenant
):
359 '''Get the hosts and deployed instances
360 Returns the hosts content'''
361 raise vimconnNotImplemented( "Should have implemented this" )
363 def get_processor_rankings(self
):
364 '''Get the processor rankings in the VIM database'''
365 raise vimconnNotImplemented( "Should have implemented this" )
367 def new_host(self
, host_data
):
368 '''Adds a new host to VIM'''
369 '''Returns status code of the VIM response'''
370 raise vimconnNotImplemented( "Should have implemented this" )
372 def new_external_port(self
, port_data
):
373 '''Adds a external port to VIM'''
374 '''Returns the port identifier'''
375 raise vimconnNotImplemented( "Should have implemented this" )
377 def new_external_network(self
,net_name
,net_type
):
378 '''Adds a external network to VIM (shared)'''
379 '''Returns the network identifier'''
380 raise vimconnNotImplemented( "Should have implemented this" )
382 def connect_port_network(self
, port_id
, network_id
, admin
=False):
383 '''Connects a external port to a network'''
384 '''Returns status code of the VIM response'''
385 raise vimconnNotImplemented( "Should have implemented this" )
387 def new_vminstancefromJSON(self
, vm_data
):
388 '''Adds a VM instance to VIM'''
389 '''Returns the instance identifier'''
390 raise vimconnNotImplemented( "Should have implemented this" )