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