RIFT OSM R1 Initial Submission
[osm/SO.git] / rwcal / plugins / vala / rwcal_openmano_vimconnector / rift / rwcal / openmano_vimconnector / vimconn.py
1 ##
2 # Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U.
3 # This file is part of openmano
4 # All Rights Reserved.
5 #
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
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
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
16 # under the License.
17 #
18 # For those usages not covered by the Apache License, Version 2.0 please
19 # contact with: nfvlabs@tid.es
20 ##
21
22 '''
23 vimconn implement an Abstract class for the vim connector plugins
24 with the definition of the method to be implemented.
25 '''
26 __author__="Alfonso Tierno"
27 __date__ ="$16-oct-2015 11:09:29$"
28
29 import logging
30
31 #Error variables
32 HTTP_Bad_Request = 400
33 HTTP_Unauthorized = 401
34 HTTP_Not_Found = 404
35 HTTP_Method_Not_Allowed = 405
36 HTTP_Request_Timeout = 408
37 HTTP_Conflict = 409
38 HTTP_Not_Implemented = 501
39 HTTP_Service_Unavailable = 503
40 HTTP_Internal_Server_Error = 500
41
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
47
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)
52
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)
57
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)
62
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)
67
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)
72
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)
77
78 class vimconnector():
79 '''Abstract base class for all the VIM connector plugins
80 These plugins must implement a vimconnector class derived from this
81 and all these methods
82 '''
83 def __init__(self, uuid, name, tenant_id, tenant_name, url, url_admin=None, user=None, passwd=None, log_level="ERROR", config={}):
84 self.id = uuid
85 self.name = name
86 self.url = url
87 self.url_admin = url_admin
88 self.tenant_id = tenant_id
89 self.tenant_name = tenant_name
90 self.user = user
91 self.passwd = passwd
92 self.config = config
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
97
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
103 elif index=='id':
104 return self.id
105 elif index=='name':
106 return self.name
107 elif index=='user':
108 return self.user
109 elif index=='passwd':
110 return self.passwd
111 elif index=='url':
112 return self.url
113 elif index=='url_admin':
114 return self.url_admin
115 elif index=="config":
116 return self.config
117 else:
118 raise KeyError("Invalid key '%s'" %str(index))
119
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
125 elif index=='id':
126 self.id = value
127 elif index=='name':
128 self.name = value
129 elif index=='user':
130 self.user = value
131 elif index=='passwd':
132 self.passwd = value
133 elif index=='url':
134 self.url = value
135 elif index=='url_admin':
136 self.url_admin = value
137 else:
138 raise KeyError("Invalid key '%s'" %str(index))
139
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" )
144
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" )
149
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
155 <other VIM specific>
156 Returns the tenant list of dictionaries:
157 [{'name':'<name>, 'id':'<id>, ...}, ...]
158 '''
159 raise vimconnNotImplemented( "Should have implemented this" )
160
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
164 shared is a boolean
165 Returns the network identifier'''
166 raise vimconnNotImplemented( "Should have implemented this" )
167
168 def get_network_list(self, filter_dict={}):
169 '''Obtain tenant networks of VIM
170 Filter_dict can be:
171 name: network name
172 id: network uuid
173 shared: boolean
174 tenant_id: tenant
175 admin_state_up: boolean
176 status: 'ACTIVE'
177 Returns the network list of dictionaries:
178 [{<the fields at Filter_dict plus some VIM specific>}, ...]
179 List can be empty
180 '''
181 raise vimconnNotImplemented( "Should have implemented this" )
182
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" )
187
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" )
192
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)
205 #
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)
208
209 '''
210 raise vimconnNotImplemented( "Should have implemented this" )
211
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
215 '''
216 raise vimconnNotImplemented( "Should have implemented this" )
217
218 def new_flavor(self, flavor_data):
219 '''Adds a tenant flavor to VIM
220 flavor_data contains a dictionary with information, keys:
221 name: flavor name
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
233 disk: disk size
234 is_public:
235
236
237
238 #TODO to concrete
239 Returns the flavor identifier'''
240 raise vimconnNotImplemented( "Should have implemented this" )
241
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" )
246
247 def new_image(self,image_dict):
248 '''
249 Adds a tenant image to VIM
250 Returns:
251 200, image-id if the image is created
252 <0, message if there is an error
253 '''
254 raise vimconnNotImplemented( "Should have implemented this" )
255
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" )
260
261 def get_image_id_from_path(self, path):
262 '''Get the image id from image path in the VIM database'''
263 '''Returns:
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.)
267 '''
268 raise vimconnNotImplemented( "Should have implemented this" )
269
270 def new_vminstance(self,name,description,start,image_id,flavor_id,net_list):
271 '''Adds a VM instance to VIM
272 Params:
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:
276 name:
277 net_id: network uuid to connect
278 vpci: virtual vcpi to assign
279 model: interface model, virtio, e2000, ...
280 mac_address:
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
286 <0, error_text
287 '''
288 raise vimconnNotImplemented( "Should have implemented this" )
289
290 def get_vminstance(self,vm_id):
291 '''Returns the VM instance information from VIM'''
292 raise vimconnNotImplemented( "Should have implemented this" )
293
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" )
298
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
312 #
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)
315 interfaces:
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
321 '''
322 raise vimconnNotImplemented( "Should have implemented this" )
323
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" )
328
329 def get_vminstance_console(self,vm_id, console_type="vnc"):
330 '''
331 Get a console for the virtual machine
332 Params:
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
342 '''
343 raise vimconnNotImplemented( "Should have implemented this" )
344
345 #NOT USED METHODS in current version
346
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
350 '''
351 raise vimconnNotImplemented( "Should have implemented this" )
352
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" )
357
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" )
362
363 def get_processor_rankings(self):
364 '''Get the processor rankings in the VIM database'''
365 raise vimconnNotImplemented( "Should have implemented this" )
366
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" )
371
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" )
376
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" )
381
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" )
386
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" )
391