bug 425 fix SR-IOV PCI-PASSTHROUGH interfaces
[osm/RO.git] / osm_ro / vimconn.py
1 # -*- coding: utf-8 -*-
2
3 ##
4 # Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U.
5 # This file is part of openmano
6 # All Rights Reserved.
7 #
8 # Licensed under the Apache License, Version 2.0 (the "License"); you may
9 # not use this file except in compliance with the License. You may obtain
10 # a copy of the License at
11 #
12 # http://www.apache.org/licenses/LICENSE-2.0
13 #
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17 # License for the specific language governing permissions and limitations
18 # under the License.
19 #
20 # For those usages not covered by the Apache License, Version 2.0 please
21 # contact with: nfvlabs@tid.es
22 ##
23
24 """
25 vimconn implement an Abstract class for the vim connector plugins
26 with the definition of the method to be implemented.
27 """
28 __author__="Alfonso Tierno, Igor D.C."
29 __date__ ="$14-aug-2017 23:59:59$"
30
31 import logging
32 import paramiko
33 import socket
34 import StringIO
35 import yaml
36 import sys
37 from email.mime.multipart import MIMEMultipart
38 from email.mime.text import MIMEText
39
40 #Error variables
41 HTTP_Bad_Request = 400
42 HTTP_Unauthorized = 401
43 HTTP_Not_Found = 404
44 HTTP_Method_Not_Allowed = 405
45 HTTP_Request_Timeout = 408
46 HTTP_Conflict = 409
47 HTTP_Not_Implemented = 501
48 HTTP_Service_Unavailable = 503
49 HTTP_Internal_Server_Error = 500
50
51 class vimconnException(Exception):
52 """Common and base class Exception for all vimconnector exceptions"""
53 def __init__(self, message, http_code=HTTP_Bad_Request):
54 Exception.__init__(self, message)
55 self.http_code = http_code
56
57 class vimconnConnectionException(vimconnException):
58 """Connectivity error with the VIM"""
59 def __init__(self, message, http_code=HTTP_Service_Unavailable):
60 vimconnException.__init__(self, message, http_code)
61
62 class vimconnUnexpectedResponse(vimconnException):
63 """Get an wrong response from VIM"""
64 def __init__(self, message, http_code=HTTP_Service_Unavailable):
65 vimconnException.__init__(self, message, http_code)
66
67 class vimconnAuthException(vimconnException):
68 """Invalid credentials or authorization to perform this action over the VIM"""
69 def __init__(self, message, http_code=HTTP_Unauthorized):
70 vimconnException.__init__(self, message, http_code)
71
72 class vimconnNotFoundException(vimconnException):
73 """The item is not found at VIM"""
74 def __init__(self, message, http_code=HTTP_Not_Found):
75 vimconnException.__init__(self, message, http_code)
76
77 class vimconnConflictException(vimconnException):
78 """There is a conflict, e.g. more item found than one"""
79 def __init__(self, message, http_code=HTTP_Conflict):
80 vimconnException.__init__(self, message, http_code)
81
82 class vimconnNotSupportedException(vimconnException):
83 """The request is not supported by connector"""
84 def __init__(self, message, http_code=HTTP_Service_Unavailable):
85 vimconnException.__init__(self, message, http_code)
86
87 class vimconnNotImplemented(vimconnException):
88 """The method is not implemented by the connected"""
89 def __init__(self, message, http_code=HTTP_Not_Implemented):
90 vimconnException.__init__(self, message, http_code)
91
92
93 class vimconnector():
94 """Abstract base class for all the VIM connector plugins
95 These plugins must implement a vimconnector class derived from this
96 and all these privated methods
97 """
98 def __init__(self, uuid, name, tenant_id, tenant_name, url, url_admin=None, user=None, passwd=None, log_level=None,
99 config={}, persitent_info={}):
100 """Constructor of VIM
101 Params:
102 'uuid': id asigned to this VIM
103 'name': name assigned to this VIM, can be used for logging
104 'tenant_id', 'tenant_name': (only one of them is mandatory) VIM tenant to be used
105 'url_admin': (optional), url used for administrative tasks
106 'user', 'passwd': credentials of the VIM user
107 'log_level': provider if it should use a different log_level than the general one
108 'config': dictionary with extra VIM information. This contains a consolidate version of general VIM config
109 at creation and particular VIM config at teh attachment
110 'persistent_info': dict where the class can store information that will be available among class
111 destroy/creation cycles. This info is unique per VIM/credential. At first call it will contain an
112 empty dict. Useful to store login/tokens information for speed up communication
113
114 Returns: Raise an exception is some needed parameter is missing, but it must not do any connectivity
115 check against the VIM
116 """
117 self.id = uuid
118 self.name = name
119 self.url = url
120 self.url_admin = url_admin
121 self.tenant_id = tenant_id
122 self.tenant_name = tenant_name
123 self.user = user
124 self.passwd = passwd
125 self.config = config
126 self.availability_zone = None
127 self.logger = logging.getLogger('openmano.vim')
128 if log_level:
129 self.logger.setLevel( getattr(logging, log_level) )
130 if not self.url_admin: #try to use normal url
131 self.url_admin = self.url
132
133 def __getitem__(self,index):
134 if index=='tenant_id':
135 return self.tenant_id
136 if index=='tenant_name':
137 return self.tenant_name
138 elif index=='id':
139 return self.id
140 elif index=='name':
141 return self.name
142 elif index=='user':
143 return self.user
144 elif index=='passwd':
145 return self.passwd
146 elif index=='url':
147 return self.url
148 elif index=='url_admin':
149 return self.url_admin
150 elif index=="config":
151 return self.config
152 else:
153 raise KeyError("Invalid key '%s'" %str(index))
154
155 def __setitem__(self,index, value):
156 if index=='tenant_id':
157 self.tenant_id = value
158 if index=='tenant_name':
159 self.tenant_name = value
160 elif index=='id':
161 self.id = value
162 elif index=='name':
163 self.name = value
164 elif index=='user':
165 self.user = value
166 elif index=='passwd':
167 self.passwd = value
168 elif index=='url':
169 self.url = value
170 elif index=='url_admin':
171 self.url_admin = value
172 else:
173 raise KeyError("Invalid key '%s'" %str(index))
174
175 @staticmethod
176 def _create_mimemultipart(content_list):
177 """Creates a MIMEmultipart text combining the content_list
178 :param content_list: list of text scripts to be combined
179 :return: str of the created MIMEmultipart. If the list is empty returns None, if the list contains only one
180 element MIMEmultipart is not created and this content is returned
181 """
182 if not content_list:
183 return None
184 elif len(content_list) == 1:
185 return content_list[0]
186 combined_message = MIMEMultipart()
187 for content in content_list:
188 if content.startswith('#include'):
189 format = 'text/x-include-url'
190 elif content.startswith('#include-once'):
191 format = 'text/x-include-once-url'
192 elif content.startswith('#!'):
193 format = 'text/x-shellscript'
194 elif content.startswith('#cloud-config'):
195 format = 'text/cloud-config'
196 elif content.startswith('#cloud-config-archive'):
197 format = 'text/cloud-config-archive'
198 elif content.startswith('#upstart-job'):
199 format = 'text/upstart-job'
200 elif content.startswith('#part-handler'):
201 format = 'text/part-handler'
202 elif content.startswith('#cloud-boothook'):
203 format = 'text/cloud-boothook'
204 else: # by default
205 format = 'text/x-shellscript'
206 sub_message = MIMEText(content, format, sys.getdefaultencoding())
207 combined_message.attach(sub_message)
208 return combined_message.as_string()
209
210 def _create_user_data(self, cloud_config):
211 """
212 Creates a script user database on cloud_config info
213 :param cloud_config: dictionary with
214 'key-pairs': (optional) list of strings with the public key to be inserted to the default user
215 'users': (optional) list of users to be inserted, each item is a dict with:
216 'name': (mandatory) user name,
217 'key-pairs': (optional) list of strings with the public key to be inserted to the user
218 'user-data': (optional) can be a string with the text script to be passed directly to cloud-init,
219 or a list of strings, each one contains a script to be passed, usually with a MIMEmultipart file
220 'config-files': (optional). List of files to be transferred. Each item is a dict with:
221 'dest': (mandatory) string with the destination absolute path
222 'encoding': (optional, by default text). Can be one of:
223 'b64', 'base64', 'gz', 'gz+b64', 'gz+base64', 'gzip+b64', 'gzip+base64'
224 'content' (mandatory): string with the content of the file
225 'permissions': (optional) string with file permissions, typically octal notation '0644'
226 'owner': (optional) file owner, string with the format 'owner:group'
227 'boot-data-drive': boolean to indicate if user-data must be passed using a boot drive (hard disk)
228 :return: config_drive, userdata. The first is a boolean or None, the second a string or None
229 """
230 config_drive = None
231 userdata = None
232 userdata_list = []
233 if isinstance(cloud_config, dict):
234 if cloud_config.get("user-data"):
235 if isinstance(cloud_config["user-data"], str):
236 userdata_list.append(cloud_config["user-data"])
237 else:
238 for u in cloud_config["user-data"]:
239 userdata_list.append(u)
240 if cloud_config.get("boot-data-drive") != None:
241 config_drive = cloud_config["boot-data-drive"]
242 if cloud_config.get("config-files") or cloud_config.get("users") or cloud_config.get("key-pairs"):
243 userdata_dict = {}
244 # default user
245 if cloud_config.get("key-pairs"):
246 userdata_dict["ssh-authorized-keys"] = cloud_config["key-pairs"]
247 userdata_dict["users"] = [{"default": None, "ssh-authorized-keys": cloud_config["key-pairs"]}]
248 if cloud_config.get("users"):
249 if "users" not in userdata_dict:
250 userdata_dict["users"] = ["default"]
251 for user in cloud_config["users"]:
252 user_info = {
253 "name": user["name"],
254 "sudo": "ALL = (ALL)NOPASSWD:ALL"
255 }
256 if "user-info" in user:
257 user_info["gecos"] = user["user-info"]
258 if user.get("key-pairs"):
259 user_info["ssh-authorized-keys"] = user["key-pairs"]
260 userdata_dict["users"].append(user_info)
261
262 if cloud_config.get("config-files"):
263 userdata_dict["write_files"] = []
264 for file in cloud_config["config-files"]:
265 file_info = {
266 "path": file["dest"],
267 "content": file["content"]
268 }
269 if file.get("encoding"):
270 file_info["encoding"] = file["encoding"]
271 if file.get("permissions"):
272 file_info["permissions"] = file["permissions"]
273 if file.get("owner"):
274 file_info["owner"] = file["owner"]
275 userdata_dict["write_files"].append(file_info)
276 userdata_list.append("#cloud-config\n" + yaml.safe_dump(userdata_dict, indent=4,
277 default_flow_style=False))
278 userdata = self._create_mimemultipart(userdata_list)
279 self.logger.debug("userdata: %s", userdata)
280 elif isinstance(cloud_config, str):
281 userdata = cloud_config
282 return config_drive, userdata
283
284 def check_vim_connectivity(self):
285 """Checks VIM can be reached and user credentials are ok.
286 Returns None if success or raised vimconnConnectionException, vimconnAuthException, ...
287 """
288 raise vimconnNotImplemented( "Should have implemented this" )
289
290 def new_tenant(self,tenant_name,tenant_description):
291 """Adds a new tenant to VIM with this name and description, this is done using admin_url if provided
292 "tenant_name": string max lenght 64
293 "tenant_description": string max length 256
294 returns the tenant identifier or raise exception
295 """
296 raise vimconnNotImplemented( "Should have implemented this" )
297
298 def delete_tenant(self,tenant_id,):
299 """Delete a tenant from VIM
300 tenant_id: returned VIM tenant_id on "new_tenant"
301 Returns None on success. Raises and exception of failure. If tenant is not found raises vimconnNotFoundException
302 """
303 raise vimconnNotImplemented( "Should have implemented this" )
304
305 def get_tenant_list(self, filter_dict={}):
306 """Obtain tenants of VIM
307 filter_dict dictionary that can contain the following keys:
308 name: filter by tenant name
309 id: filter by tenant uuid/id
310 <other VIM specific>
311 Returns the tenant list of dictionaries, and empty list if no tenant match all the filers:
312 [{'name':'<name>, 'id':'<id>, ...}, ...]
313 """
314 raise vimconnNotImplemented( "Should have implemented this" )
315
316 def new_network(self, net_name, net_type, ip_profile=None, shared=False, vlan=None):
317 """Adds a tenant network to VIM
318 Params:
319 'net_name': name of the network
320 'net_type': one of:
321 'bridge': overlay isolated network
322 'data': underlay E-LAN network for Passthrough and SRIOV interfaces
323 'ptp': underlay E-LINE network for Passthrough and SRIOV interfaces.
324 'ip_profile': is a dict containing the IP parameters of the network (Currently only IPv4 is implemented)
325 'ip-version': can be one of ["IPv4","IPv6"]
326 'subnet-address': ip_prefix_schema, that is X.X.X.X/Y
327 'gateway-address': (Optional) ip_schema, that is X.X.X.X
328 'dns-address': (Optional) ip_schema,
329 'dhcp': (Optional) dict containing
330 'enabled': {"type": "boolean"},
331 'start-address': ip_schema, first IP to grant
332 'count': number of IPs to grant.
333 'shared': if this network can be seen/use by other tenants/organization
334 'vlan': in case of a data or ptp net_type, the intended vlan tag to be used for the network
335 Returns the network identifier on success or raises and exception on failure
336 """
337 raise vimconnNotImplemented( "Should have implemented this" )
338
339 def get_network_list(self, filter_dict={}):
340 """Obtain tenant networks of VIM
341 Params:
342 'filter_dict' (optional) contains entries to return only networks that matches ALL entries:
343 name: string => returns only networks with this name
344 id: string => returns networks with this VIM id, this imply returns one network at most
345 shared: boolean >= returns only networks that are (or are not) shared
346 tenant_id: sting => returns only networks that belong to this tenant/project
347 ,#(not used yet) admin_state_up: boolean => returns only networks that are (or are not) in admin state active
348 #(not used yet) status: 'ACTIVE','ERROR',... => filter networks that are on this status
349 Returns the network list of dictionaries. each dictionary contains:
350 'id': (mandatory) VIM network id
351 'name': (mandatory) VIM network name
352 'status': (mandatory) can be 'ACTIVE', 'INACTIVE', 'DOWN', 'BUILD', 'ERROR', 'VIM_ERROR', 'OTHER'
353 'network_type': (optional) can be 'vxlan', 'vlan' or 'flat'
354 'segmentation_id': (optional) in case network_type is vlan or vxlan this field contains the segmentation id
355 'error_msg': (optional) text that explains the ERROR status
356 other VIM specific fields: (optional) whenever possible using the same naming of filter_dict param
357 List can be empty if no network map the filter_dict. Raise an exception only upon VIM connectivity,
358 authorization, or some other unspecific error
359 """
360 raise vimconnNotImplemented( "Should have implemented this" )
361
362 def get_network(self, net_id):
363 """Obtain network details from the 'net_id' VIM network
364 Return a dict that contains:
365 'id': (mandatory) VIM network id, that is, net_id
366 'name': (mandatory) VIM network name
367 'status': (mandatory) can be 'ACTIVE', 'INACTIVE', 'DOWN', 'BUILD', 'ERROR', 'VIM_ERROR', 'OTHER'
368 'error_msg': (optional) text that explains the ERROR status
369 other VIM specific fields: (optional) whenever possible using the same naming of filter_dict param
370 Raises an exception upon error or when network is not found
371 """
372 raise vimconnNotImplemented( "Should have implemented this" )
373
374 def delete_network(self, net_id):
375 """Deletes a tenant network from VIM
376 Returns the network identifier or raises an exception upon error or when network is not found
377 """
378 raise vimconnNotImplemented( "Should have implemented this" )
379
380 def refresh_nets_status(self, net_list):
381 """Get the status of the networks
382 Params:
383 'net_list': a list with the VIM network id to be get the status
384 Returns a dictionary with:
385 'net_id': #VIM id of this network
386 status: #Mandatory. Text with one of:
387 # DELETED (not found at vim)
388 # VIM_ERROR (Cannot connect to VIM, authentication problems, VIM response error, ...)
389 # OTHER (Vim reported other status not understood)
390 # ERROR (VIM indicates an ERROR status)
391 # ACTIVE, INACTIVE, DOWN (admin down),
392 # BUILD (on building process)
393 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
394 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
395 'net_id2': ...
396 """
397 raise vimconnNotImplemented( "Should have implemented this" )
398
399 def get_flavor(self, flavor_id):
400 """Obtain flavor details from the VIM
401 Returns the flavor dict details {'id':<>, 'name':<>, other vim specific }
402 Raises an exception upon error or if not found
403 """
404 raise vimconnNotImplemented( "Should have implemented this" )
405
406 def get_flavor_id_from_data(self, flavor_dict):
407 """Obtain flavor id that match the flavor description
408 Params:
409 'flavor_dict': dictionary that contains:
410 'disk': main hard disk in GB
411 'ram': meomry in MB
412 'vcpus': number of virtual cpus
413 #TODO: complete parameters for EPA
414 Returns the flavor_id or raises a vimconnNotFoundException
415 """
416 raise vimconnNotImplemented( "Should have implemented this" )
417
418 def new_flavor(self, flavor_data):
419 """Adds a tenant flavor to VIM
420 flavor_data contains a dictionary with information, keys:
421 name: flavor name
422 ram: memory (cloud type) in MBytes
423 vpcus: cpus (cloud type)
424 extended: EPA parameters
425 - numas: #items requested in same NUMA
426 memory: number of 1G huge pages memory
427 paired-threads|cores|threads: number of paired hyperthreads, complete cores OR individual threads
428 interfaces: # passthrough(PT) or SRIOV interfaces attached to this numa
429 - name: interface name
430 dedicated: yes|no|yes:sriov; for PT, SRIOV or only one SRIOV for the physical NIC
431 bandwidth: X Gbps; requested guarantee bandwidth
432 vpci: requested virtual PCI address
433 disk: disk size
434 is_public:
435 #TODO to concrete
436 Returns the flavor identifier"""
437 raise vimconnNotImplemented( "Should have implemented this" )
438
439 def delete_flavor(self, flavor_id):
440 """Deletes a tenant flavor from VIM identify by its id
441 Returns the used id or raise an exception"""
442 raise vimconnNotImplemented( "Should have implemented this" )
443
444 def new_image(self, image_dict):
445 """ Adds a tenant image to VIM
446 Returns the image id or raises an exception if failed
447 """
448 raise vimconnNotImplemented( "Should have implemented this" )
449
450 def delete_image(self, image_id):
451 """Deletes a tenant image from VIM
452 Returns the image_id if image is deleted or raises an exception on error"""
453 raise vimconnNotImplemented( "Should have implemented this" )
454
455 def get_image_id_from_path(self, path):
456 """Get the image id from image path in the VIM database.
457 Returns the image_id or raises a vimconnNotFoundException
458 """
459 raise vimconnNotImplemented( "Should have implemented this" )
460
461 def get_image_list(self, filter_dict={}):
462 """Obtain tenant images from VIM
463 Filter_dict can be:
464 name: image name
465 id: image uuid
466 checksum: image checksum
467 location: image path
468 Returns the image list of dictionaries:
469 [{<the fields at Filter_dict plus some VIM specific>}, ...]
470 List can be empty
471 """
472 raise vimconnNotImplemented( "Should have implemented this" )
473
474 def new_vminstance(self, name, description, start, image_id, flavor_id, net_list, cloud_config=None, disk_list=None,
475 availability_zone_index=None, availability_zone_list=None):
476 """Adds a VM instance to VIM
477 Params:
478 'start': (boolean) indicates if VM must start or created in pause mode.
479 'image_id','flavor_id': image and flavor VIM id to use for the VM
480 'net_list': list of interfaces, each one is a dictionary with:
481 'name': (optional) name for the interface.
482 'net_id': VIM network id where this interface must be connect to. Mandatory for type==virtual
483 'vpci': (optional) virtual vPCI address to assign at the VM. Can be ignored depending on VIM capabilities
484 'model': (optional and only have sense for type==virtual) interface model: virtio, e2000, ...
485 'mac_address': (optional) mac address to assign to this interface
486 #TODO: CHECK if an optional 'vlan' parameter is needed for VIMs when type if VF and net_id is not provided,
487 the VLAN tag to be used. In case net_id is provided, the internal network vlan is used for tagging VF
488 'type': (mandatory) can be one of:
489 'virtual', in this case always connected to a network of type 'net_type=bridge'
490 'PCI-PASSTHROUGH' or 'PF' (passthrough): depending on VIM capabilities it can be connected to a data/ptp network ot it
491 can created unconnected
492 'SR-IOV' or 'VF' (SRIOV with VLAN tag): same as PF for network connectivity.
493 'VFnotShared'(SRIOV without VLAN tag) same as PF for network connectivity. VF where no other VFs
494 are allocated on the same physical NIC
495 'bw': (optional) only for PF/VF/VFnotShared. Minimal Bandwidth required for the interface in GBPS
496 'port_security': (optional) If False it must avoid any traffic filtering at this interface. If missing
497 or True, it must apply the default VIM behaviour
498 After execution the method will add the key:
499 'vim_id': must be filled/added by this method with the VIM identifier generated by the VIM for this
500 interface. 'net_list' is modified
501 'cloud_config': (optional) dictionary with:
502 'key-pairs': (optional) list of strings with the public key to be inserted to the default user
503 'users': (optional) list of users to be inserted, each item is a dict with:
504 'name': (mandatory) user name,
505 'key-pairs': (optional) list of strings with the public key to be inserted to the user
506 'user-data': (optional) can be a string with the text script to be passed directly to cloud-init,
507 or a list of strings, each one contains a script to be passed, usually with a MIMEmultipart file
508 'config-files': (optional). List of files to be transferred. Each item is a dict with:
509 'dest': (mandatory) string with the destination absolute path
510 'encoding': (optional, by default text). Can be one of:
511 'b64', 'base64', 'gz', 'gz+b64', 'gz+base64', 'gzip+b64', 'gzip+base64'
512 'content' (mandatory): string with the content of the file
513 'permissions': (optional) string with file permissions, typically octal notation '0644'
514 'owner': (optional) file owner, string with the format 'owner:group'
515 'boot-data-drive': boolean to indicate if user-data must be passed using a boot drive (hard disk)
516 'disk_list': (optional) list with additional disks to the VM. Each item is a dict with:
517 'image_id': (optional). VIM id of an existing image. If not provided an empty disk must be mounted
518 'size': (mandatory) string with the size of the disk in GB
519 availability_zone_index: Index of availability_zone_list to use for this this VM. None if not AV required
520 availability_zone_list: list of availability zones given by user in the VNFD descriptor. Ignore if
521 availability_zone_index is None
522 Returns a tuple with the instance identifier and created_items or raises an exception on error
523 created_items can be None or a dictionary where this method can include key-values that will be passed to
524 the method delete_vminstance and action_vminstance. Can be used to store created ports, volumes, etc.
525 Format is vimconnector dependent, but do not use nested dictionaries and a value of None should be the same
526 as not present.
527 """
528 raise vimconnNotImplemented( "Should have implemented this" )
529
530 def get_vminstance(self,vm_id):
531 """Returns the VM instance information from VIM"""
532 raise vimconnNotImplemented( "Should have implemented this" )
533
534 def delete_vminstance(self, vm_id, created_items=None):
535 """
536 Removes a VM instance from VIM and each associate elements
537 :param vm_id: VIM identifier of the VM, provided by method new_vminstance
538 :param created_items: dictionary with extra items to be deleted. provided by method new_vminstance and/or method
539 action_vminstance
540 :return: None or the same vm_id. Raises an exception on fail
541 """
542 raise vimconnNotImplemented( "Should have implemented this" )
543
544 def refresh_vms_status(self, vm_list):
545 """Get the status of the virtual machines and their interfaces/ports
546 Params: the list of VM identifiers
547 Returns a dictionary with:
548 vm_id: #VIM id of this Virtual Machine
549 status: #Mandatory. Text with one of:
550 # DELETED (not found at vim)
551 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
552 # OTHER (Vim reported other status not understood)
553 # ERROR (VIM indicates an ERROR status)
554 # ACTIVE, PAUSED, SUSPENDED, INACTIVE (not running),
555 # BUILD (on building process), ERROR
556 # ACTIVE:NoMgmtIP (Active but any of its interface has an IP address
557 #
558 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
559 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
560 interfaces: list with interface info. Each item a dictionary with:
561 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
562 mac_address: #Text format XX:XX:XX:XX:XX:XX
563 vim_net_id: #network id where this interface is connected, if provided at creation
564 vim_interface_id: #interface/port VIM id
565 ip_address: #null, or text with IPv4, IPv6 address
566 compute_node: #identification of compute node where PF,VF interface is allocated
567 pci: #PCI address of the NIC that hosts the PF,VF
568 vlan: #physical VLAN used for VF
569 """
570 raise vimconnNotImplemented( "Should have implemented this" )
571
572 def action_vminstance(self, vm_id, action_dict, created_items={}):
573 """
574 Send and action over a VM instance. Returns created_items if the action was successfully sent to the VIM.
575 created_items is a dictionary with items that
576 :param vm_id: VIM identifier of the VM, provided by method new_vminstance
577 :param action_dict: dictionary with the action to perform
578 :param created_items: provided by method new_vminstance is a dictionary with key-values that will be passed to
579 the method delete_vminstance. Can be used to store created ports, volumes, etc. Format is vimconnector
580 dependent, but do not use nested dictionaries and a value of None should be the same as not present. This
581 method can modify this value
582 :return: None, or a console dict
583 """
584 raise vimconnNotImplemented( "Should have implemented this" )
585
586 def get_vminstance_console(self, vm_id, console_type="vnc"):
587 """
588 Get a console for the virtual machine
589 Params:
590 vm_id: uuid of the VM
591 console_type, can be:
592 "novnc" (by default), "xvpvnc" for VNC types,
593 "rdp-html5" for RDP types, "spice-html5" for SPICE types
594 Returns dict with the console parameters:
595 protocol: ssh, ftp, http, https, ...
596 server: usually ip address
597 port: the http, ssh, ... port
598 suffix: extra text, e.g. the http path and query string
599 """
600 raise vimconnNotImplemented( "Should have implemented this" )
601
602 def new_classification(self, name, ctype, definition):
603 """Creates a traffic classification in the VIM
604 Params:
605 'name': name of this classification
606 'ctype': type of this classification
607 'definition': definition of this classification (type-dependent free-form text)
608 Returns the VIM's classification ID on success or raises an exception on failure
609 """
610 raise vimconnNotImplemented( "SFC support not implemented" )
611
612 def get_classification(self, classification_id):
613 """Obtain classification details of the VIM's classification with ID='classification_id'
614 Return a dict that contains:
615 'id': VIM's classification ID (same as classification_id)
616 'name': VIM's classification name
617 'type': type of this classification
618 'definition': definition of the classification
619 'status': 'ACTIVE', 'INACTIVE', 'DOWN', 'BUILD', 'ERROR', 'VIM_ERROR', 'OTHER'
620 'error_msg': (optional) text that explains the ERROR status
621 other VIM specific fields: (optional) whenever possible
622 Raises an exception upon error or when classification is not found
623 """
624 raise vimconnNotImplemented( "SFC support not implemented" )
625
626 def get_classification_list(self, filter_dict={}):
627 """Obtain classifications from the VIM
628 Params:
629 'filter_dict' (optional): contains the entries to filter the classifications on and only return those that match ALL:
630 id: string => returns classifications with this VIM's classification ID, which implies a return of one classification at most
631 name: string => returns only classifications with this name
632 type: string => returns classifications of this type
633 definition: string => returns classifications that have this definition
634 tenant_id: string => returns only classifications that belong to this tenant/project
635 Returns a list of classification dictionaries, each dictionary contains:
636 'id': (mandatory) VIM's classification ID
637 'name': (mandatory) VIM's classification name
638 'type': type of this classification
639 'definition': definition of the classification
640 other VIM specific fields: (optional) whenever possible using the same naming of filter_dict param
641 List can be empty if no classification matches the filter_dict. Raise an exception only upon VIM connectivity,
642 authorization, or some other unspecific error
643 """
644 raise vimconnNotImplemented( "SFC support not implemented" )
645
646 def delete_classification(self, classification_id):
647 """Deletes a classification from the VIM
648 Returns the classification ID (classification_id) or raises an exception upon error or when classification is not found
649 """
650 raise vimconnNotImplemented( "SFC support not implemented" )
651
652 def new_sfi(self, name, ingress_ports, egress_ports, sfc_encap=True):
653 """Creates a service function instance in the VIM
654 Params:
655 'name': name of this service function instance
656 'ingress_ports': set of ingress ports (VIM's port IDs)
657 'egress_ports': set of egress ports (VIM's port IDs)
658 'sfc_encap': boolean stating whether this specific instance supports IETF SFC Encapsulation
659 Returns the VIM's service function instance ID on success or raises an exception on failure
660 """
661 raise vimconnNotImplemented( "SFC support not implemented" )
662
663 def get_sfi(self, sfi_id):
664 """Obtain service function instance details of the VIM's service function instance with ID='sfi_id'
665 Return a dict that contains:
666 'id': VIM's sfi ID (same as sfi_id)
667 'name': VIM's sfi name
668 'ingress_ports': set of ingress ports (VIM's port IDs)
669 'egress_ports': set of egress ports (VIM's port IDs)
670 'status': 'ACTIVE', 'INACTIVE', 'DOWN', 'BUILD', 'ERROR', 'VIM_ERROR', 'OTHER'
671 'error_msg': (optional) text that explains the ERROR status
672 other VIM specific fields: (optional) whenever possible
673 Raises an exception upon error or when service function instance is not found
674 """
675 raise vimconnNotImplemented( "SFC support not implemented" )
676
677 def get_sfi_list(self, filter_dict={}):
678 """Obtain service function instances from the VIM
679 Params:
680 'filter_dict' (optional): contains the entries to filter the sfis on and only return those that match ALL:
681 id: string => returns sfis with this VIM's sfi ID, which implies a return of one sfi at most
682 name: string => returns only service function instances with this name
683 tenant_id: string => returns only service function instances that belong to this tenant/project
684 Returns a list of service function instance dictionaries, each dictionary contains:
685 'id': (mandatory) VIM's sfi ID
686 'name': (mandatory) VIM's sfi name
687 'ingress_ports': set of ingress ports (VIM's port IDs)
688 'egress_ports': set of egress ports (VIM's port IDs)
689 other VIM specific fields: (optional) whenever possible using the same naming of filter_dict param
690 List can be empty if no sfi matches the filter_dict. Raise an exception only upon VIM connectivity,
691 authorization, or some other unspecific error
692 """
693 raise vimconnNotImplemented( "SFC support not implemented" )
694
695 def delete_sfi(self, sfi_id):
696 """Deletes a service function instance from the VIM
697 Returns the service function instance ID (sfi_id) or raises an exception upon error or when sfi is not found
698 """
699 raise vimconnNotImplemented( "SFC support not implemented" )
700
701 def new_sf(self, name, sfis, sfc_encap=True):
702 """Creates (an abstract) service function in the VIM
703 Params:
704 'name': name of this service function
705 'sfis': set of service function instances of this (abstract) service function
706 'sfc_encap': boolean stating whether this service function supports IETF SFC Encapsulation
707 Returns the VIM's service function ID on success or raises an exception on failure
708 """
709 raise vimconnNotImplemented( "SFC support not implemented" )
710
711 def get_sf(self, sf_id):
712 """Obtain service function details of the VIM's service function with ID='sf_id'
713 Return a dict that contains:
714 'id': VIM's sf ID (same as sf_id)
715 'name': VIM's sf name
716 'sfis': VIM's sf's set of VIM's service function instance IDs
717 'sfc_encap': boolean stating whether this service function supports IETF SFC Encapsulation
718 'status': 'ACTIVE', 'INACTIVE', 'DOWN', 'BUILD', 'ERROR', 'VIM_ERROR', 'OTHER'
719 'error_msg': (optional) text that explains the ERROR status
720 other VIM specific fields: (optional) whenever possible
721 Raises an exception upon error or when sf is not found
722 """
723
724 def get_sf_list(self, filter_dict={}):
725 """Obtain service functions from the VIM
726 Params:
727 'filter_dict' (optional): contains the entries to filter the sfs on and only return those that match ALL:
728 id: string => returns sfs with this VIM's sf ID, which implies a return of one sf at most
729 name: string => returns only service functions with this name
730 tenant_id: string => returns only service functions that belong to this tenant/project
731 Returns a list of service function dictionaries, each dictionary contains:
732 'id': (mandatory) VIM's sf ID
733 'name': (mandatory) VIM's sf name
734 'sfis': VIM's sf's set of VIM's service function instance IDs
735 'sfc_encap': boolean stating whether this service function supports IETF SFC Encapsulation
736 other VIM specific fields: (optional) whenever possible using the same naming of filter_dict param
737 List can be empty if no sf matches the filter_dict. Raise an exception only upon VIM connectivity,
738 authorization, or some other unspecific error
739 """
740 raise vimconnNotImplemented( "SFC support not implemented" )
741
742 def delete_sf(self, sf_id):
743 """Deletes (an abstract) service function from the VIM
744 Returns the service function ID (sf_id) or raises an exception upon error or when sf is not found
745 """
746 raise vimconnNotImplemented( "SFC support not implemented" )
747
748
749 def new_sfp(self, name, classifications, sfs, sfc_encap=True, spi=None):
750 """Creates a service function path
751 Params:
752 'name': name of this service function path
753 'classifications': set of traffic classifications that should be matched on to get into this sfp
754 'sfs': list of every service function that constitutes this path , from first to last
755 'sfc_encap': whether this is an SFC-Encapsulated chain (i.e using NSH), True by default
756 'spi': (optional) the Service Function Path identifier (SPI: Service Path Identifier) for this path
757 Returns the VIM's sfp ID on success or raises an exception on failure
758 """
759 raise vimconnNotImplemented( "SFC support not implemented" )
760
761 def get_sfp(self, sfp_id):
762 """Obtain service function path details of the VIM's sfp with ID='sfp_id'
763 Return a dict that contains:
764 'id': VIM's sfp ID (same as sfp_id)
765 'name': VIM's sfp name
766 'classifications': VIM's sfp's list of VIM's classification IDs
767 'sfs': VIM's sfp's list of VIM's service function IDs
768 'status': 'ACTIVE', 'INACTIVE', 'DOWN', 'BUILD', 'ERROR', 'VIM_ERROR', 'OTHER'
769 'error_msg': (optional) text that explains the ERROR status
770 other VIM specific fields: (optional) whenever possible
771 Raises an exception upon error or when sfp is not found
772 """
773 raise vimconnNotImplemented( "SFC support not implemented" )
774
775 def get_sfp_list(self, filter_dict={}):
776 """Obtain service function paths from VIM
777 Params:
778 'filter_dict' (optional): contains the entries to filter the sfps on, and only return those that match ALL:
779 id: string => returns sfps with this VIM's sfp ID , which implies a return of one sfp at most
780 name: string => returns only sfps with this name
781 tenant_id: string => returns only sfps that belong to this tenant/project
782 Returns a list of service function path dictionaries, each dictionary contains:
783 'id': (mandatory) VIM's sfp ID
784 'name': (mandatory) VIM's sfp name
785 'classifications': VIM's sfp's list of VIM's classification IDs
786 'sfs': VIM's sfp's list of VIM's service function IDs
787 other VIM specific fields: (optional) whenever possible using the same naming of filter_dict param
788 List can be empty if no sfp matches the filter_dict. Raise an exception only upon VIM connectivity,
789 authorization, or some other unspecific error
790 """
791 raise vimconnNotImplemented( "SFC support not implemented" )
792
793 def delete_sfp(self, sfp_id):
794 """Deletes a service function path from the VIM
795 Returns the sfp ID (sfp_id) or raises an exception upon error or when sf is not found
796 """
797 raise vimconnNotImplemented( "SFC support not implemented" )
798
799 def inject_user_key(self, ip_addr=None, user=None, key=None, ro_key=None, password=None):
800 """
801 Inject a ssh public key in a VM
802 Params:
803 ip_addr: ip address of the VM
804 user: username (default-user) to enter in the VM
805 key: public key to be injected in the VM
806 ro_key: private key of the RO, used to enter in the VM if the password is not provided
807 password: password of the user to enter in the VM
808 The function doesn't return a value:
809 """
810 if not ip_addr or not user:
811 raise vimconnNotSupportedException("All parameters should be different from 'None'")
812 elif not ro_key and not password:
813 raise vimconnNotSupportedException("All parameters should be different from 'None'")
814 else:
815 commands = {'mkdir -p ~/.ssh/', 'echo "%s" >> ~/.ssh/authorized_keys' % key,
816 'chmod 644 ~/.ssh/authorized_keys', 'chmod 700 ~/.ssh/'}
817 client = paramiko.SSHClient()
818 try:
819 if ro_key:
820 pkey = paramiko.RSAKey.from_private_key(StringIO.StringIO(ro_key))
821 else:
822 pkey = None
823 client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
824 client.connect(ip_addr, username=user, password=password, pkey=pkey, timeout=10)
825 for command in commands:
826 (i, o, e) = client.exec_command(command, timeout=10)
827 returncode = o.channel.recv_exit_status()
828 output = o.read()
829 outerror = e.read()
830 if returncode != 0:
831 text = "run_command='{}' Error='{}'".format(command, outerror)
832 raise vimconnUnexpectedResponse("Cannot inject ssh key in VM: '{}'".format(text))
833 return
834 except (socket.error, paramiko.AuthenticationException, paramiko.SSHException) as message:
835 raise vimconnUnexpectedResponse(
836 "Cannot inject ssh key in VM: '{}' - {}".format(ip_addr, str(message)))
837 return
838
839
840 #NOT USED METHODS in current version
841
842 def host_vim2gui(self, host, server_dict):
843 """Transform host dictionary from VIM format to GUI format,
844 and append to the server_dict
845 """
846 raise vimconnNotImplemented( "Should have implemented this" )
847
848 def get_hosts_info(self):
849 """Get the information of deployed hosts
850 Returns the hosts content"""
851 raise vimconnNotImplemented( "Should have implemented this" )
852
853 def get_hosts(self, vim_tenant):
854 """Get the hosts and deployed instances
855 Returns the hosts content"""
856 raise vimconnNotImplemented( "Should have implemented this" )
857
858 def get_processor_rankings(self):
859 """Get the processor rankings in the VIM database"""
860 raise vimconnNotImplemented( "Should have implemented this" )
861
862 def new_host(self, host_data):
863 """Adds a new host to VIM"""
864 """Returns status code of the VIM response"""
865 raise vimconnNotImplemented( "Should have implemented this" )
866
867 def new_external_port(self, port_data):
868 """Adds a external port to VIM"""
869 """Returns the port identifier"""
870 raise vimconnNotImplemented( "Should have implemented this" )
871
872 def new_external_network(self,net_name,net_type):
873 """Adds a external network to VIM (shared)"""
874 """Returns the network identifier"""
875 raise vimconnNotImplemented( "Should have implemented this" )
876
877 def connect_port_network(self, port_id, network_id, admin=False):
878 """Connects a external port to a network"""
879 """Returns status code of the VIM response"""
880 raise vimconnNotImplemented( "Should have implemented this" )
881
882 def new_vminstancefromJSON(self, vm_data):
883 """Adds a VM instance to VIM"""
884 """Returns the instance identifier"""
885 raise vimconnNotImplemented( "Should have implemented this" )
886