1 # -*- coding: utf-8 -*-
4 # Copyright 2015 Telefonica Investigacion 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.
29 from email
.mime
.multipart
import MIMEMultipart
30 from email
.mime
.text
import MIMEText
31 from http
import HTTPStatus
32 from io
import StringIO
42 __author__
= "Alfonso Tierno, Igor D.C."
43 __date__
= "$14-aug-2017 23:59:59$"
46 def deprecated(message
):
47 def deprecated_decorator(func
):
48 def deprecated_func(*args
, **kwargs
):
50 "{} is a deprecated function. {}".format(func
.__name
__, message
),
51 category
=DeprecationWarning,
54 warnings
.simplefilter("default", DeprecationWarning)
56 return func(*args
, **kwargs
)
58 return deprecated_func
60 return deprecated_decorator
64 HTTP_Bad_Request
= HTTPStatus
.BAD_REQUEST
.value
65 HTTP_Unauthorized
= HTTPStatus
.UNAUTHORIZED
.value
66 HTTP_Not_Found
= HTTPStatus
.NOT_FOUND
.value
67 HTTP_Method_Not_Allowed
= HTTPStatus
.METHOD_NOT_ALLOWED
.value
68 HTTP_Request_Timeout
= HTTPStatus
.REQUEST_TIMEOUT
.value
69 HTTP_Conflict
= HTTPStatus
.CONFLICT
.value
70 HTTP_Not_Implemented
= HTTPStatus
.NOT_IMPLEMENTED
.value
71 HTTP_Service_Unavailable
= HTTPStatus
.SERVICE_UNAVAILABLE
.value
72 HTTP_Internal_Server_Error
= HTTPStatus
.INTERNAL_SERVER_ERROR
.value
75 class VimConnException(Exception):
76 """Common and base class Exception for all VimConnector exceptions"""
78 def __init__(self
, message
, http_code
=HTTP_Bad_Request
):
79 Exception.__init
__(self
, message
)
80 self
.http_code
= http_code
83 class VimConnConnectionException(VimConnException
):
84 """Connectivity error with the VIM"""
86 def __init__(self
, message
, http_code
=HTTP_Service_Unavailable
):
87 VimConnException
.__init
__(self
, message
, http_code
)
90 class VimConnUnexpectedResponse(VimConnException
):
91 """Get an wrong response from VIM"""
93 def __init__(self
, message
, http_code
=HTTP_Service_Unavailable
):
94 VimConnException
.__init
__(self
, message
, http_code
)
97 class VimConnAuthException(VimConnException
):
98 """Invalid credentials or authorization to perform this action over the VIM"""
100 def __init__(self
, message
, http_code
=HTTP_Unauthorized
):
101 VimConnException
.__init
__(self
, message
, http_code
)
104 class VimConnNotFoundException(VimConnException
):
105 """The item is not found at VIM"""
107 def __init__(self
, message
, http_code
=HTTP_Not_Found
):
108 VimConnException
.__init
__(self
, message
, http_code
)
111 class VimConnConflictException(VimConnException
):
112 """There is a conflict, e.g. more item found than one"""
114 def __init__(self
, message
, http_code
=HTTP_Conflict
):
115 VimConnException
.__init
__(self
, message
, http_code
)
118 class VimConnNotSupportedException(VimConnException
):
119 """The request is not supported by connector"""
121 def __init__(self
, message
, http_code
=HTTP_Service_Unavailable
):
122 VimConnException
.__init
__(self
, message
, http_code
)
125 class VimConnNotImplemented(VimConnException
):
126 """The method is not implemented by the connected"""
128 def __init__(self
, message
, http_code
=HTTP_Not_Implemented
):
129 VimConnException
.__init
__(self
, message
, http_code
)
133 """Abstract base class for all the VIM connector plugins
134 These plugins must implement a VimConnector class derived from this
135 and all these privated methods
153 Constructor of VIM. Raise an exception is some needed parameter is missing, but it must not do any connectivity
154 checking against the VIM
155 :param uuid: internal id of this VIM
156 :param name: name assigned to this VIM, can be used for logging
157 :param tenant_id: 'tenant_id': (only one of them is mandatory) VIM tenant to be used
158 :param tenant_name: 'tenant_name': (only one of them is mandatory) VIM tenant to be used
159 :param url: url used for normal operations
160 :param url_admin: (optional), url used for administrative tasks
161 :param user: user to access
162 :param passwd: password
163 :param log_level: provided if it should use a different log_level than the general one
164 :param config: dictionary with extra VIM information. This contains a consolidate version of VIM config
165 at VIM_ACCOUNT (attach)
166 :param persitent_info: dict where the class can store information that will be available among class
167 destroy/creation cycles. This info is unique per VIM/credential. At first call it will contain an
168 empty dict. Useful to store login/tokens information for speed up communication
174 self
.url_admin
= url_admin
175 self
.tenant_id
= tenant_id
176 self
.tenant_name
= tenant_name
179 self
.config
= config
or {}
180 self
.availability_zone
= None
181 self
.logger
= logging
.getLogger("ro.vim")
184 self
.logger
.setLevel(getattr(logging
, log_level
))
186 if not self
.url_admin
: # try to use normal url
187 self
.url_admin
= self
.url
189 def __getitem__(self
, index
):
190 if index
== "tenant_id":
191 return self
.tenant_id
193 if index
== "tenant_name":
194 return self
.tenant_name
197 elif index
== "name":
199 elif index
== "user":
201 elif index
== "passwd":
205 elif index
== "url_admin":
206 return self
.url_admin
207 elif index
== "config":
210 raise KeyError("Invalid key '{}'".format(index
))
212 def __setitem__(self
, index
, value
):
213 if index
== "tenant_id":
214 self
.tenant_id
= value
216 if index
== "tenant_name":
217 self
.tenant_name
= value
220 elif index
== "name":
222 elif index
== "user":
224 elif index
== "passwd":
228 elif index
== "url_admin":
229 self
.url_admin
= value
231 raise KeyError("Invalid key '{}'".format(index
))
234 def _create_mimemultipart(content_list
):
235 """Creates a MIMEmultipart text combining the content_list
236 :param content_list: list of text scripts to be combined
237 :return: str of the created MIMEmultipart. If the list is empty returns None, if the list contains only one
238 element MIMEmultipart is not created and this content is returned
242 elif len(content_list
) == 1:
243 return content_list
[0]
245 combined_message
= MIMEMultipart()
247 for content
in content_list
:
248 if content
.startswith("#include"):
249 mime_format
= "text/x-include-url"
250 elif content
.startswith("#include-once"):
251 mime_format
= "text/x-include-once-url"
252 elif content
.startswith("#!"):
253 mime_format
= "text/x-shellscript"
254 elif content
.startswith("#cloud-config"):
255 mime_format
= "text/cloud-config"
256 elif content
.startswith("#cloud-config-archive"):
257 mime_format
= "text/cloud-config-archive"
258 elif content
.startswith("#upstart-job"):
259 mime_format
= "text/upstart-job"
260 elif content
.startswith("#part-handler"):
261 mime_format
= "text/part-handler"
262 elif content
.startswith("#cloud-boothook"):
263 mime_format
= "text/cloud-boothook"
265 mime_format
= "text/x-shellscript"
267 sub_message
= MIMEText(content
, mime_format
, sys
.getdefaultencoding())
268 combined_message
.attach(sub_message
)
270 return combined_message
.as_string()
272 def _create_user_data(self
, cloud_config
):
274 Creates a script user database on cloud_config info
275 :param cloud_config: dictionary with
276 'key-pairs': (optional) list of strings with the public key to be inserted to the default user
277 'users': (optional) list of users to be inserted, each item is a dict with:
278 'name': (mandatory) user name,
279 'key-pairs': (optional) list of strings with the public key to be inserted to the user
280 'user-data': (optional) can be a string with the text script to be passed directly to cloud-init,
281 or a list of strings, each one contains a script to be passed, usually with a MIMEmultipart file
282 'config-files': (optional). List of files to be transferred. Each item is a dict with:
283 'dest': (mandatory) string with the destination absolute path
284 'encoding': (optional, by default text). Can be one of:
285 'b64', 'base64', 'gz', 'gz+b64', 'gz+base64', 'gzip+b64', 'gzip+base64'
286 'content' (mandatory): string with the content of the file
287 'permissions': (optional) string with file permissions, typically octal notation '0644'
288 'owner': (optional) file owner, string with the format 'owner:group'
289 'boot-data-drive': boolean to indicate if user-data must be passed using a boot drive (hard disk)
290 :return: config_drive, userdata. The first is a boolean or None, the second a string or None
296 if isinstance(cloud_config
, dict):
297 if cloud_config
.get("user-data"):
298 if isinstance(cloud_config
["user-data"], str):
299 userdata_list
.append(cloud_config
["user-data"])
301 for u
in cloud_config
["user-data"]:
302 userdata_list
.append(u
)
304 if cloud_config
.get("boot-data-drive") is not None:
305 config_drive
= cloud_config
["boot-data-drive"]
308 cloud_config
.get("config-files")
309 or cloud_config
.get("users")
310 or cloud_config
.get("key-pairs")
315 if cloud_config
.get("key-pairs"):
316 userdata_dict
["ssh-authorized-keys"] = cloud_config
["key-pairs"]
317 userdata_dict
["users"] = [
320 "ssh-authorized-keys": cloud_config
["key-pairs"],
324 if cloud_config
.get("users"):
325 if "users" not in userdata_dict
:
326 userdata_dict
["users"] = ["default"]
328 for user
in cloud_config
["users"]:
330 "name": user
["name"],
331 "sudo": "ALL = (ALL)NOPASSWD:ALL",
334 if "user-info" in user
:
335 user_info
["gecos"] = user
["user-info"]
337 if user
.get("key-pairs"):
338 user_info
["ssh-authorized-keys"] = user
["key-pairs"]
340 userdata_dict
["users"].append(user_info
)
342 if cloud_config
.get("config-files"):
343 userdata_dict
["write_files"] = []
344 for file in cloud_config
["config-files"]:
345 file_info
= {"path": file["dest"], "content": file["content"]}
347 if file.get("encoding"):
348 file_info
["encoding"] = file["encoding"]
350 if file.get("permissions"):
351 file_info
["permissions"] = file["permissions"]
353 if file.get("owner"):
354 file_info
["owner"] = file["owner"]
356 userdata_dict
["write_files"].append(file_info
)
358 userdata_list
.append(
360 + yaml
.safe_dump(userdata_dict
, indent
=4, default_flow_style
=False)
362 userdata
= self
._create
_mimemultipart
(userdata_list
)
363 self
.logger
.debug("userdata: %s", userdata
)
364 elif isinstance(cloud_config
, str):
365 userdata
= cloud_config
367 return config_drive
, userdata
369 def check_vim_connectivity(self
):
370 """Checks VIM can be reached and user credentials are ok.
371 Returns None if success or raises VimConnConnectionException, VimConnAuthException, ...
373 # by default no checking until each connector implements it
376 def get_tenant_list(self
, filter_dict
={}):
377 """Obtain tenants of VIM
378 filter_dict dictionary that can contain the following keys:
379 name: filter by tenant name
380 id: filter by tenant uuid/id
382 Returns the tenant list of dictionaries, and empty list if no tenant match all the filers:
383 [{'name':'<name>, 'id':'<id>, ...}, ...]
385 raise VimConnNotImplemented("Should have implemented this")
393 provider_network_profile
=None,
395 """Adds a tenant network to VIM
397 'net_name': name of the network
399 'bridge': overlay isolated network
400 'data': underlay E-LAN network for Passthrough and SRIOV interfaces
401 'ptp': underlay E-LINE network for Passthrough and SRIOV interfaces.
402 'ip_profile': is a dict containing the IP parameters of the network
403 'ip_version': can be "IPv4" or "IPv6" (Currently only IPv4 is implemented)
404 'subnet_address': ip_prefix_schema, that is X.X.X.X/Y
405 'gateway_address': (Optional) ip_schema, that is X.X.X.X
406 'dns_address': (Optional) comma separated list of ip_schema, e.g. X.X.X.X[,X,X,X,X]
407 'dhcp_enabled': True or False
408 'dhcp_start_address': ip_schema, first IP to grant
409 'dhcp_count': number of IPs to grant.
410 'shared': if this network can be seen/use by other tenants/organization
411 'provider_network_profile': (optional) contains {segmentation-id: vlan, provider-network: vim_netowrk}
412 Returns a tuple with the network identifier and created_items, or raises an exception on error
413 created_items can be None or a dictionary where this method can include key-values that will be passed to
414 the method delete_network. Can be used to store created segments, created l2gw connections, etc.
415 Format is VimConnector dependent, but do not use nested dictionaries and a value of None should be the same
418 raise VimConnNotImplemented("Should have implemented this")
420 def get_network_list(self
, filter_dict
={}):
421 """Obtain tenant networks of VIM
423 'filter_dict' (optional) contains entries to return only networks that matches ALL entries:
424 name: string => returns only networks with this name
425 id: string => returns networks with this VIM id, this imply returns one network at most
426 shared: boolean >= returns only networks that are (or are not) shared
427 tenant_id: sting => returns only networks that belong to this tenant/project
428 ,#(not used yet) admin_state_up: boolean => returns only networks that are (or are not) in admin state
430 #(not used yet) status: 'ACTIVE','ERROR',... => filter networks that are on this status
431 Returns the network list of dictionaries. each dictionary contains:
432 'id': (mandatory) VIM network id
433 'name': (mandatory) VIM network name
434 'status': (mandatory) can be 'ACTIVE', 'INACTIVE', 'DOWN', 'BUILD', 'ERROR', 'VIM_ERROR', 'OTHER'
435 'network_type': (optional) can be 'vxlan', 'vlan' or 'flat'
436 'segmentation_id': (optional) in case network_type is vlan or vxlan this field contains the segmentation id
437 'error_msg': (optional) text that explains the ERROR status
438 other VIM specific fields: (optional) whenever possible using the same naming of filter_dict param
439 List can be empty if no network map the filter_dict. Raise an exception only upon VIM connectivity,
440 authorization, or some other unspecific error
442 raise VimConnNotImplemented("Should have implemented this")
444 def get_network(self
, net_id
):
445 """Obtain network details from the 'net_id' VIM network
446 Return a dict that contains:
447 'id': (mandatory) VIM network id, that is, net_id
448 'name': (mandatory) VIM network name
449 'status': (mandatory) can be 'ACTIVE', 'INACTIVE', 'DOWN', 'BUILD', 'ERROR', 'VIM_ERROR', 'OTHER'
450 'error_msg': (optional) text that explains the ERROR status
451 other VIM specific fields: (optional) whenever possible using the same naming of filter_dict param
452 Raises an exception upon error or when network is not found
454 raise VimConnNotImplemented("Should have implemented this")
456 def delete_network(self
, net_id
, created_items
=None):
458 Removes a tenant network from VIM and its associated elements
459 :param net_id: VIM identifier of the network, provided by method new_network
460 :param created_items: dictionary with extra items to be deleted. provided by method new_network
461 Returns the network identifier or raises an exception upon error or when network is not found
463 raise VimConnNotImplemented("Should have implemented this")
465 def refresh_nets_status(self
, net_list
):
466 """Get the status of the networks
468 'net_list': a list with the VIM network id to be get the status
469 Returns a dictionary with:
470 'net_id': #VIM id of this network
471 status: #Mandatory. Text with one of:
472 # DELETED (not found at vim)
473 # VIM_ERROR (Cannot connect to VIM, authentication problems, VIM response error, ...)
474 # OTHER (Vim reported other status not understood)
475 # ERROR (VIM indicates an ERROR status)
476 # ACTIVE, INACTIVE, DOWN (admin down),
477 # BUILD (on building process)
478 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
479 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
482 raise VimConnNotImplemented("Should have implemented this")
484 def get_flavor(self
, flavor_id
):
485 """Obtain flavor details from the VIM
486 Returns the flavor dict details {'id':<>, 'name':<>, other vim specific }
487 Raises an exception upon error or if not found
489 raise VimConnNotImplemented("Should have implemented this")
491 def get_flavor_id_from_data(self
, flavor_dict
):
492 """Obtain flavor id that match the flavor description
494 'flavor_dict': dictionary that contains:
495 'disk': main hard disk in GB
497 'vcpus': number of virtual cpus
498 #TODO: complete parameters for EPA
499 Returns the flavor_id or raises a VimConnNotFoundException
501 raise VimConnNotImplemented("Should have implemented this")
503 def new_flavor(self
, flavor_data
):
504 """Adds a tenant flavor to VIM
505 flavor_data contains a dictionary with information, keys:
507 ram: memory (cloud type) in MBytes
508 vpcus: cpus (cloud type)
509 extended: EPA parameters
510 - numas: #items requested in same NUMA
511 memory: number of 1G huge pages memory
512 paired-threads|cores|threads: number of paired hyperthreads, complete cores OR individual
514 interfaces: # passthrough(PT) or SRIOV interfaces attached to this numa
515 - name: interface name
516 dedicated: yes|no|yes:sriov; for PT, SRIOV or only one SRIOV for the physical NIC
517 bandwidth: X Gbps; requested guarantee bandwidth
518 vpci: requested virtual PCI address
522 Returns the flavor identifier
524 raise VimConnNotImplemented("Should have implemented this")
526 def delete_flavor(self
, flavor_id
):
527 """Deletes a tenant flavor from VIM identify by its id
528 Returns the used id or raise an exception
530 raise VimConnNotImplemented("Should have implemented this")
532 def get_affinity_group(self
, affinity_group_id
):
533 """Obtain affinity or anti affinity group details from the VIM
534 Returns the flavor dict details {'id':<>, 'name':<>, other vim specific }
535 Raises an exception upon error or if not found
537 raise VimConnNotImplemented("Should have implemented this")
539 def new_affinity_group(self
, affinity_group_data
):
540 """Adds an affinity or anti affinity group to VIM
541 affinity_group_data contains a dictionary with information, keys:
542 name: name in VIM for the affinity or anti-affinity group
543 type: affinity or anti-affinity
544 scope: Only nfvi-node allowed
545 Returns the affinity or anti affinity group identifier
547 raise VimConnNotImplemented("Should have implemented this")
549 def delete_affinity_group(self
, affinity_group_id
):
550 """Deletes an affinity or anti affinity group from the VIM identified by its id
551 Returns the used id or raise an exception
553 raise VimConnNotImplemented("Should have implemented this")
555 def new_image(self
, image_dict
):
556 """Adds a tenant image to VIM
557 Returns the image id or raises an exception if failed
559 raise VimConnNotImplemented("Should have implemented this")
561 def delete_image(self
, image_id
):
562 """Deletes a tenant image from VIM
563 Returns the image_id if image is deleted or raises an exception on error
565 raise VimConnNotImplemented("Should have implemented this")
567 def get_image_id_from_path(self
, path
):
568 """Get the image id from image path in the VIM database.
569 Returns the image_id or raises a VimConnNotFoundException
571 raise VimConnNotImplemented("Should have implemented this")
573 def get_image_list(self
, filter_dict
={}):
574 """Obtain tenant images from VIM
578 checksum: image checksum
580 Returns the image list of dictionaries:
581 [{<the fields at Filter_dict plus some VIM specific>}, ...]
584 raise VimConnNotImplemented("Should have implemented this")
597 availability_zone_index
=None,
598 availability_zone_list
=None,
600 """Adds a VM instance to VIM
602 'start': (boolean) indicates if VM must start or created in pause mode.
603 'image_id','flavor_id': image and flavor VIM id to use for the VM
604 affinity_group_list: list of affinity groups, each one is a dictionary.
606 'net_list': list of interfaces, each one is a dictionary with:
607 'name': (optional) name for the interface.
608 'net_id': VIM network id where this interface must be connect to. Mandatory for type==virtual
609 'vpci': (optional) virtual vPCI address to assign at the VM. Can be ignored depending on VIM
611 'model': (optional and only have sense for type==virtual) interface model: virtio, e1000, ...
612 'mac_address': (optional) mac address to assign to this interface
613 'ip_address': (optional) IP address to assign to this interface
614 #TODO: CHECK if an optional 'vlan' parameter is needed for VIMs when type if VF and net_id is not
615 provided, the VLAN tag to be used. In case net_id is provided, the internal network vlan is used
617 'type': (mandatory) can be one of:
618 'virtual', in this case always connected to a network of type 'net_type=bridge'
619 'PCI-PASSTHROUGH' or 'PF' (passthrough): depending on VIM capabilities it can be connected to a
620 data/ptp network ot it
621 can created unconnected
622 'SR-IOV' or 'VF' (SRIOV with VLAN tag): same as PF for network connectivity.
623 'VFnotShared'(SRIOV without VLAN tag) same as PF for network connectivity. VF where no other VFs
624 are allocated on the same physical NIC
625 'bw': (optional) only for PF/VF/VFnotShared. Minimal Bandwidth required for the interface in GBPS
626 'port_security': (optional) If False it must avoid any traffic filtering at this interface. If missing
627 or True, it must apply the default VIM behaviour
628 After execution the method will add the key:
629 'vim_id': must be filled/added by this method with the VIM identifier generated by the VIM for this
630 interface. 'net_list' is modified
631 'cloud_config': (optional) dictionary with:
632 'key-pairs': (optional) list of strings with the public key to be inserted to the default user
633 'users': (optional) list of users to be inserted, each item is a dict with:
634 'name': (mandatory) user name,
635 'key-pairs': (optional) list of strings with the public key to be inserted to the user
636 'user-data': (optional) can be a string with the text script to be passed directly to cloud-init,
637 or a list of strings, each one contains a script to be passed, usually with a MIMEmultipart file
638 'config-files': (optional). List of files to be transferred. Each item is a dict with:
639 'dest': (mandatory) string with the destination absolute path
640 'encoding': (optional, by default text). Can be one of:
641 'b64', 'base64', 'gz', 'gz+b64', 'gz+base64', 'gzip+b64', 'gzip+base64'
642 'content' (mandatory): string with the content of the file
643 'permissions': (optional) string with file permissions, typically octal notation '0644'
644 'owner': (optional) file owner, string with the format 'owner:group'
645 'boot-data-drive': boolean to indicate if user-data must be passed using a boot drive (hard disk)
646 'disk_list': (optional) list with additional disks to the VM. Each item is a dict with:
647 'image_id': (optional). VIM id of an existing image. If not provided an empty disk must be mounted
648 'size': (mandatory) string with the size of the disk in GB
649 availability_zone_index: Index of availability_zone_list to use for this this VM. None if not AV required
650 availability_zone_list: list of availability zones given by user in the VNFD descriptor. Ignore if
651 availability_zone_index is None
652 Returns a tuple with the instance identifier and created_items or raises an exception on error
653 created_items can be None or a dictionary where this method can include key-values that will be passed to
654 the method delete_vminstance and action_vminstance. Can be used to store created ports, volumes, etc.
655 Format is VimConnector dependent, but do not use nested dictionaries and a value of None should be the same
658 raise VimConnNotImplemented("Should have implemented this")
660 def get_vminstance(self
, vm_id
):
661 """Returns the VM instance information from VIM"""
662 raise VimConnNotImplemented("Should have implemented this")
664 def delete_vminstance(self
, vm_id
, created_items
=None, volumes_to_hold
=None):
666 Removes a VM instance from VIM and its associated elements
667 :param vm_id: VIM identifier of the VM, provided by method new_vminstance
668 :param created_items: dictionary with extra items to be deleted. provided by method new_vminstance and/or method
670 :return: None or the same vm_id. Raises an exception on fail
672 raise VimConnNotImplemented("Should have implemented this")
674 def refresh_vms_status(self
, vm_list
):
675 """Get the status of the virtual machines and their interfaces/ports
676 Params: the list of VM identifiers
677 Returns a dictionary with:
678 vm_id: #VIM id of this Virtual Machine
679 status: #Mandatory. Text with one of:
680 # DELETED (not found at vim)
681 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
682 # OTHER (Vim reported other status not understood)
683 # ERROR (VIM indicates an ERROR status)
684 # ACTIVE, PAUSED, SUSPENDED, INACTIVE (not running),
685 # BUILD (on building process), ERROR
686 # ACTIVE:NoMgmtIP (Active but any of its interface has an IP address
688 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
689 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
690 interfaces: list with interface info. Each item a dictionary with:
691 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
692 mac_address: #Text format XX:XX:XX:XX:XX:XX
693 vim_net_id: #network id where this interface is connected, if provided at creation
694 vim_interface_id: #interface/port VIM id
695 ip_address: #null, or text with IPv4, IPv6 address
696 compute_node: #identification of compute node where PF,VF interface is allocated
697 pci: #PCI address of the NIC that hosts the PF,VF
698 vlan: #physical VLAN used for VF
700 raise VimConnNotImplemented("Should have implemented this")
702 def action_vminstance(self
, vm_id
, action_dict
, created_items
={}):
704 Send and action over a VM instance. Returns created_items if the action was successfully sent to the VIM.
705 created_items is a dictionary with items that
706 :param vm_id: VIM identifier of the VM, provided by method new_vminstance
707 :param action_dict: dictionary with the action to perform
708 :param created_items: provided by method new_vminstance is a dictionary with key-values that will be passed to
709 the method delete_vminstance. Can be used to store created ports, volumes, etc. Format is VimConnector
710 dependent, but do not use nested dictionaries and a value of None should be the same as not present. This
711 method can modify this value
712 :return: None, or a console dict
714 raise VimConnNotImplemented("Should have implemented this")
716 def get_vminstance_console(self
, vm_id
, console_type
="vnc"):
718 Get a console for the virtual machine
720 vm_id: uuid of the VM
721 console_type, can be:
722 "novnc" (by default), "xvpvnc" for VNC types,
723 "rdp-html5" for RDP types, "spice-html5" for SPICE types
724 Returns dict with the console parameters:
725 protocol: ssh, ftp, http, https, ...
726 server: usually ip address
727 port: the http, ssh, ... port
728 suffix: extra text, e.g. the http path and query string
730 raise VimConnNotImplemented("Should have implemented this")
733 self
, ip_addr
=None, user
=None, key
=None, ro_key
=None, password
=None
736 Inject a ssh public key in a VM
738 ip_addr: ip address of the VM
739 user: username (default-user) to enter in the VM
740 key: public key to be injected in the VM
741 ro_key: private key of the RO, used to enter in the VM if the password is not provided
742 password: password of the user to enter in the VM
743 The function doesn't return a value:
745 if not ip_addr
or not user
:
746 raise VimConnNotSupportedException(
747 "All parameters should be different from 'None'"
749 elif not ro_key
and not password
:
750 raise VimConnNotSupportedException(
751 "All parameters should be different from 'None'"
756 'echo "{}" >> ~/.ssh/authorized_keys'.format(key
),
757 "chmod 644 ~/.ssh/authorized_keys",
762 format
="%(asctime)s %(levelname)s %(name)s %(filename)s:%(lineno)s %(message)s"
764 logging
.getLogger("paramiko").setLevel(logging
.DEBUG
)
765 client
= paramiko
.SSHClient()
769 pkey
= paramiko
.RSAKey
.from_private_key(StringIO(ro_key
))
773 client
.set_missing_host_key_policy(paramiko
.AutoAddPolicy())
784 for command
in commands
:
785 (i
, o
, e
) = client
.exec_command(command
, timeout
=30)
786 returncode
= o
.channel
.recv_exit_status()
790 text
= "run_command='{}' Error='{}'".format(command
, outerror
)
791 self
.logger
.debug(traceback
.format_tb(e
.__traceback
__))
792 raise VimConnUnexpectedResponse(
793 "Cannot inject ssh key in VM: '{}'".format(text
)
798 paramiko
.AuthenticationException
,
799 paramiko
.SSHException
,
801 self
.logger
.debug(traceback
.format_exc())
802 raise VimConnUnexpectedResponse(
803 "Cannot inject ssh key in VM: '{}' - {}".format(
804 ip_addr
, str(message
)
810 def new_tenant(self
, tenant_name
, tenant_description
):
811 """Adds a new tenant to VIM with this name and description, this is done using admin_url if provided
812 "tenant_name": string max lenght 64
813 "tenant_description": string max length 256
814 returns the tenant identifier or raise exception
816 raise VimConnNotImplemented("Should have implemented this")
818 def delete_tenant(self
, tenant_id
):
819 """Delete a tenant from VIM
820 tenant_id: returned VIM tenant_id on "new_tenant"
821 Returns None on success. Raises and exception of failure. If tenant is not found raises VimConnNotFoundException
823 raise VimConnNotImplemented("Should have implemented this")
825 def new_classification(self
, name
, ctype
, definition
):
826 """Creates a traffic classification in the VIM
828 'name': name of this classification
829 'ctype': type of this classification
830 'definition': definition of this classification (type-dependent free-form text)
831 Returns the VIM's classification ID on success or raises an exception on failure
833 raise VimConnNotImplemented("SFC support not implemented")
835 def get_classification(self
, classification_id
):
836 """Obtain classification details of the VIM's classification with ID='classification_id'
837 Return a dict that contains:
838 'id': VIM's classification ID (same as classification_id)
839 'name': VIM's classification name
840 'type': type of this classification
841 'definition': definition of the classification
842 'status': 'ACTIVE', 'INACTIVE', 'DOWN', 'BUILD', 'ERROR', 'VIM_ERROR', 'OTHER'
843 'error_msg': (optional) text that explains the ERROR status
844 other VIM specific fields: (optional) whenever possible
845 Raises an exception upon error or when classification is not found
847 raise VimConnNotImplemented("SFC support not implemented")
849 def get_classification_list(self
, filter_dict
={}):
850 """Obtain classifications from the VIM
852 'filter_dict' (optional): contains the entries to filter the classifications on and only return those that
854 id: string => returns classifications with this VIM's classification ID, which implies a return of one
855 classification at most
856 name: string => returns only classifications with this name
857 type: string => returns classifications of this type
858 definition: string => returns classifications that have this definition
859 tenant_id: string => returns only classifications that belong to this tenant/project
860 Returns a list of classification dictionaries, each dictionary contains:
861 'id': (mandatory) VIM's classification ID
862 'name': (mandatory) VIM's classification name
863 'type': type of this classification
864 'definition': definition of the classification
865 other VIM specific fields: (optional) whenever possible using the same naming of filter_dict param
866 List can be empty if no classification matches the filter_dict. Raise an exception only upon VIM connectivity,
867 authorization, or some other unspecific error
869 raise VimConnNotImplemented("SFC support not implemented")
871 def refresh_classifications_status(self
, classification_list
):
872 """Get the status of the classifications
873 Params: the list of classification identifiers
874 Returns a dictionary with:
875 vm_id: #VIM id of this classifier
876 status: #Mandatory. Text with one of:
877 # DELETED (not found at vim)
878 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
879 # OTHER (Vim reported other status not understood)
880 # ERROR (VIM indicates an ERROR status)
882 # CREATING (on building process)
883 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
884 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
886 raise VimConnNotImplemented("Should have implemented this")
888 def delete_classification(self
, classification_id
):
889 """Deletes a classification from the VIM
890 Returns the classification ID (classification_id) or raises an exception upon error or when classification is
893 raise VimConnNotImplemented("SFC support not implemented")
895 def new_sfi(self
, name
, ingress_ports
, egress_ports
, sfc_encap
=True):
896 """Creates a service function instance in the VIM
898 'name': name of this service function instance
899 'ingress_ports': set of ingress ports (VIM's port IDs)
900 'egress_ports': set of egress ports (VIM's port IDs)
901 'sfc_encap': boolean stating whether this specific instance supports IETF SFC Encapsulation
902 Returns the VIM's service function instance ID on success or raises an exception on failure
904 raise VimConnNotImplemented("SFC support not implemented")
906 def get_sfi(self
, sfi_id
):
907 """Obtain service function instance details of the VIM's service function instance with ID='sfi_id'
908 Return a dict that contains:
909 'id': VIM's sfi ID (same as sfi_id)
910 'name': VIM's sfi name
911 'ingress_ports': set of ingress ports (VIM's port IDs)
912 'egress_ports': set of egress ports (VIM's port IDs)
913 'status': 'ACTIVE', 'INACTIVE', 'DOWN', 'BUILD', 'ERROR', 'VIM_ERROR', 'OTHER'
914 'error_msg': (optional) text that explains the ERROR status
915 other VIM specific fields: (optional) whenever possible
916 Raises an exception upon error or when service function instance is not found
918 raise VimConnNotImplemented("SFC support not implemented")
920 def get_sfi_list(self
, filter_dict
={}):
921 """Obtain service function instances from the VIM
923 'filter_dict' (optional): contains the entries to filter the sfis on and only return those that match ALL:
924 id: string => returns sfis with this VIM's sfi ID, which implies a return of one sfi at most
925 name: string => returns only service function instances with this name
926 tenant_id: string => returns only service function instances that belong to this tenant/project
927 Returns a list of service function instance dictionaries, each dictionary contains:
928 'id': (mandatory) VIM's sfi ID
929 'name': (mandatory) VIM's sfi name
930 'ingress_ports': set of ingress ports (VIM's port IDs)
931 'egress_ports': set of egress ports (VIM's port IDs)
932 other VIM specific fields: (optional) whenever possible using the same naming of filter_dict param
933 List can be empty if no sfi matches the filter_dict. Raise an exception only upon VIM connectivity,
934 authorization, or some other unspecific error
936 raise VimConnNotImplemented("SFC support not implemented")
938 def delete_sfi(self
, sfi_id
):
939 """Deletes a service function instance from the VIM
940 Returns the service function instance ID (sfi_id) or raises an exception upon error or when sfi is not found
942 raise VimConnNotImplemented("SFC support not implemented")
944 def refresh_sfis_status(self
, sfi_list
):
945 """Get the status of the service function instances
946 Params: the list of sfi identifiers
947 Returns a dictionary with:
948 vm_id: #VIM id of this service function instance
949 status: #Mandatory. Text with one of:
950 # DELETED (not found at vim)
951 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
952 # OTHER (Vim reported other status not understood)
953 # ERROR (VIM indicates an ERROR status)
955 # CREATING (on building process)
956 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
957 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
959 raise VimConnNotImplemented("Should have implemented this")
961 def new_sf(self
, name
, sfis
, sfc_encap
=True):
962 """Creates (an abstract) service function in the VIM
964 'name': name of this service function
965 'sfis': set of service function instances of this (abstract) service function
966 'sfc_encap': boolean stating whether this service function supports IETF SFC Encapsulation
967 Returns the VIM's service function ID on success or raises an exception on failure
969 raise VimConnNotImplemented("SFC support not implemented")
971 def get_sf(self
, sf_id
):
972 """Obtain service function details of the VIM's service function with ID='sf_id'
973 Return a dict that contains:
974 'id': VIM's sf ID (same as sf_id)
975 'name': VIM's sf name
976 'sfis': VIM's sf's set of VIM's service function instance IDs
977 'sfc_encap': boolean stating whether this service function supports IETF SFC Encapsulation
978 'status': 'ACTIVE', 'INACTIVE', 'DOWN', 'BUILD', 'ERROR', 'VIM_ERROR', 'OTHER'
979 'error_msg': (optional) text that explains the ERROR status
980 other VIM specific fields: (optional) whenever possible
981 Raises an exception upon error or when sf is not found
984 def get_sf_list(self
, filter_dict
={}):
985 """Obtain service functions from the VIM
987 'filter_dict' (optional): contains the entries to filter the sfs on and only return those that match ALL:
988 id: string => returns sfs with this VIM's sf ID, which implies a return of one sf at most
989 name: string => returns only service functions with this name
990 tenant_id: string => returns only service functions that belong to this tenant/project
991 Returns a list of service function dictionaries, each dictionary contains:
992 'id': (mandatory) VIM's sf ID
993 'name': (mandatory) VIM's sf name
994 'sfis': VIM's sf's set of VIM's service function instance IDs
995 'sfc_encap': boolean stating whether this service function supports IETF SFC Encapsulation
996 other VIM specific fields: (optional) whenever possible using the same naming of filter_dict param
997 List can be empty if no sf matches the filter_dict. Raise an exception only upon VIM connectivity,
998 authorization, or some other unspecific error
1000 raise VimConnNotImplemented("SFC support not implemented")
1002 def delete_sf(self
, sf_id
):
1003 """Deletes (an abstract) service function from the VIM
1004 Returns the service function ID (sf_id) or raises an exception upon error or when sf is not found
1006 raise VimConnNotImplemented("SFC support not implemented")
1008 def refresh_sfs_status(self
, sf_list
):
1009 """Get the status of the service functions
1010 Params: the list of sf identifiers
1011 Returns a dictionary with:
1012 vm_id: #VIM id of this service function
1013 status: #Mandatory. Text with one of:
1014 # DELETED (not found at vim)
1015 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
1016 # OTHER (Vim reported other status not understood)
1017 # ERROR (VIM indicates an ERROR status)
1019 # CREATING (on building process)
1020 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
1021 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
1023 raise VimConnNotImplemented("Should have implemented this")
1025 def new_sfp(self
, name
, classifications
, sfs
, sfc_encap
=True, spi
=None):
1026 """Creates a service function path
1028 'name': name of this service function path
1029 'classifications': set of traffic classifications that should be matched on to get into this sfp
1030 'sfs': list of every service function that constitutes this path , from first to last
1031 'sfc_encap': whether this is an SFC-Encapsulated chain (i.e using NSH), True by default
1032 'spi': (optional) the Service Function Path identifier (SPI: Service Path Identifier) for this path
1033 Returns the VIM's sfp ID on success or raises an exception on failure
1035 raise VimConnNotImplemented("SFC support not implemented")
1037 def get_sfp(self
, sfp_id
):
1038 """Obtain service function path details of the VIM's sfp with ID='sfp_id'
1039 Return a dict that contains:
1040 'id': VIM's sfp ID (same as sfp_id)
1041 'name': VIM's sfp name
1042 'classifications': VIM's sfp's list of VIM's classification IDs
1043 'sfs': VIM's sfp's list of VIM's service function IDs
1044 'status': 'ACTIVE', 'INACTIVE', 'DOWN', 'BUILD', 'ERROR', 'VIM_ERROR', 'OTHER'
1045 'error_msg': (optional) text that explains the ERROR status
1046 other VIM specific fields: (optional) whenever possible
1047 Raises an exception upon error or when sfp is not found
1049 raise VimConnNotImplemented("SFC support not implemented")
1051 def get_sfp_list(self
, filter_dict
={}):
1052 """Obtain service function paths from VIM
1054 'filter_dict' (optional): contains the entries to filter the sfps on, and only return those that match ALL:
1055 id: string => returns sfps with this VIM's sfp ID , which implies a return of one sfp at most
1056 name: string => returns only sfps with this name
1057 tenant_id: string => returns only sfps that belong to this tenant/project
1058 Returns a list of service function path dictionaries, each dictionary contains:
1059 'id': (mandatory) VIM's sfp ID
1060 'name': (mandatory) VIM's sfp name
1061 'classifications': VIM's sfp's list of VIM's classification IDs
1062 'sfs': VIM's sfp's list of VIM's service function IDs
1063 other VIM specific fields: (optional) whenever possible using the same naming of filter_dict param
1064 List can be empty if no sfp matches the filter_dict. Raise an exception only upon VIM connectivity,
1065 authorization, or some other unspecific error
1067 raise VimConnNotImplemented("SFC support not implemented")
1069 def refresh_sfps_status(self
, sfp_list
):
1070 """Get the status of the service function path
1071 Params: the list of sfp identifiers
1072 Returns a dictionary with:
1073 vm_id: #VIM id of this service function path
1074 status: #Mandatory. Text with one of:
1075 # DELETED (not found at vim)
1076 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
1077 # OTHER (Vim reported other status not understood)
1078 # ERROR (VIM indicates an ERROR status)
1080 # CREATING (on building process)
1081 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
1082 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)F
1084 raise VimConnNotImplemented("Should have implemented this")
1086 def delete_sfp(self
, sfp_id
):
1087 """Deletes a service function path from the VIM
1088 Returns the sfp ID (sfp_id) or raises an exception upon error or when sf is not found
1090 raise VimConnNotImplemented("SFC support not implemented")
1092 def migrate_instance(self
, vm_id
, compute_host
=None):
1095 vm_id: ID of an instance
1096 compute_host: Host to migrate the vdu to
1097 Returns the vm state or raises an exception upon error
1099 raise VimConnNotImplemented("Should have implemented this")
1101 def resize_instance(self
, vm_id
, flavor_id
=None):
1105 vm_id: ID of an instance
1106 flavor_id: flavor_id to resize the vdu to
1108 raise VimConnNotImplemented("Should have implemented this")
1110 # NOT USED METHODS in current version. Deprecated
1112 def host_vim2gui(self
, host
, server_dict
):
1113 """Transform host dictionary from VIM format to GUI format,
1114 and append to the server_dict
1116 raise VimConnNotImplemented("Should have implemented this")
1119 def get_hosts_info(self
):
1120 """Get the information of deployed hosts
1121 Returns the hosts content"""
1122 raise VimConnNotImplemented("Should have implemented this")
1125 def get_hosts(self
, vim_tenant
):
1126 """Get the hosts and deployed instances
1127 Returns the hosts content"""
1128 raise VimConnNotImplemented("Should have implemented this")
1131 def get_processor_rankings(self
):
1132 """Get the processor rankings in the VIM database"""
1133 raise VimConnNotImplemented("Should have implemented this")
1136 def new_host(self
, host_data
):
1137 """Adds a new host to VIM"""
1138 """Returns status code of the VIM response"""
1139 raise VimConnNotImplemented("Should have implemented this")
1142 def new_external_port(self
, port_data
):
1143 """Adds a external port to VIM"""
1144 """Returns the port identifier"""
1145 raise VimConnNotImplemented("Should have implemented this")
1148 def new_external_network(self
, net_name
, net_type
):
1149 """Adds a external network to VIM (shared)"""
1150 """Returns the network identifier"""
1151 raise VimConnNotImplemented("Should have implemented this")
1154 def connect_port_network(self
, port_id
, network_id
, admin
=False):
1155 """Connects a external port to a network"""
1156 """Returns status code of the VIM response"""
1157 raise VimConnNotImplemented("Should have implemented this")
1160 def new_vminstancefromJSON(self
, vm_data
):
1161 """Adds a VM instance to VIM"""
1162 """Returns the instance identifier"""
1163 raise VimConnNotImplemented("Should have implemented this")