blob: 8b207b18e8f82954f791feda627a05cd6ece6413 [file] [log] [blame]
tierno7edb6752016-03-21 17:37:52 +01001# -*- coding: utf-8 -*-
2
3##
tierno92021022018-09-12 16:29:23 +02004# Copyright 2015 Telefonica Investigacion y Desarrollo, S.A.U.
tierno7edb6752016-03-21 17:37:52 +01005# 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
tiernoa7d34d02017-02-23 14:42:07 +010024"""
tierno7edb6752016-03-21 17:37:52 +010025vimconn implement an Abstract class for the vim connector plugins
26 with the definition of the method to be implemented.
tiernoa7d34d02017-02-23 14:42:07 +010027"""
tierno7edb6752016-03-21 17:37:52 +010028
tierno0a1437e2017-10-02 00:17:43 +020029from email.mime.multipart import MIMEMultipart
30from email.mime.text import MIMEText
tierno72774862020-05-04 11:44:15 +000031from http import HTTPStatus
sousaedu049cbb12022-01-05 11:39:35 +000032from io import StringIO
33import logging
34import socket
35import sys
aticige5d78422022-05-16 23:03:54 +030036import traceback
tierno72774862020-05-04 11:44:15 +000037import warnings
tierno9228f512019-07-04 16:23:00 +000038
sousaedu049cbb12022-01-05 11:39:35 +000039import paramiko
40import yaml
41
tierno9228f512019-07-04 16:23:00 +000042__author__ = "Alfonso Tierno, Igor D.C."
tierno72774862020-05-04 11:44:15 +000043__date__ = "$14-aug-2017 23:59:59$"
tierno7edb6752016-03-21 17:37:52 +010044
tierno9228f512019-07-04 16:23:00 +000045
tierno72774862020-05-04 11:44:15 +000046def deprecated(message):
47 def deprecated_decorator(func):
48 def deprecated_func(*args, **kwargs):
sousaedu80135b92021-02-17 15:05:18 +010049 warnings.warn(
50 "{} is a deprecated function. {}".format(func.__name__, message),
51 category=DeprecationWarning,
52 stacklevel=2,
53 )
54 warnings.simplefilter("default", DeprecationWarning)
55
tierno72774862020-05-04 11:44:15 +000056 return func(*args, **kwargs)
sousaedu80135b92021-02-17 15:05:18 +010057
tierno72774862020-05-04 11:44:15 +000058 return deprecated_func
sousaedu80135b92021-02-17 15:05:18 +010059
tierno72774862020-05-04 11:44:15 +000060 return deprecated_decorator
61
62
63# Error variables
64HTTP_Bad_Request = HTTPStatus.BAD_REQUEST.value
65HTTP_Unauthorized = HTTPStatus.UNAUTHORIZED.value
66HTTP_Not_Found = HTTPStatus.NOT_FOUND.value
67HTTP_Method_Not_Allowed = HTTPStatus.METHOD_NOT_ALLOWED.value
68HTTP_Request_Timeout = HTTPStatus.REQUEST_TIMEOUT.value
69HTTP_Conflict = HTTPStatus.CONFLICT.value
70HTTP_Not_Implemented = HTTPStatus.NOT_IMPLEMENTED.value
71HTTP_Service_Unavailable = HTTPStatus.SERVICE_UNAVAILABLE.value
72HTTP_Internal_Server_Error = HTTPStatus.INTERNAL_SERVER_ERROR.value
73
74
75class VimConnException(Exception):
76 """Common and base class Exception for all VimConnector exceptions"""
sousaedu80135b92021-02-17 15:05:18 +010077
tiernoae4a8d12016-07-08 12:30:39 +020078 def __init__(self, message, http_code=HTTP_Bad_Request):
79 Exception.__init__(self, message)
80 self.http_code = http_code
81
tierno9228f512019-07-04 16:23:00 +000082
tierno72774862020-05-04 11:44:15 +000083class VimConnConnectionException(VimConnException):
tiernoa7d34d02017-02-23 14:42:07 +010084 """Connectivity error with the VIM"""
sousaedu80135b92021-02-17 15:05:18 +010085
tiernoae4a8d12016-07-08 12:30:39 +020086 def __init__(self, message, http_code=HTTP_Service_Unavailable):
tierno72774862020-05-04 11:44:15 +000087 VimConnException.__init__(self, message, http_code)
tierno9228f512019-07-04 16:23:00 +000088
89
tierno72774862020-05-04 11:44:15 +000090class VimConnUnexpectedResponse(VimConnException):
tiernoa7d34d02017-02-23 14:42:07 +010091 """Get an wrong response from VIM"""
sousaedu80135b92021-02-17 15:05:18 +010092
tiernoae4a8d12016-07-08 12:30:39 +020093 def __init__(self, message, http_code=HTTP_Service_Unavailable):
tierno72774862020-05-04 11:44:15 +000094 VimConnException.__init__(self, message, http_code)
tiernoae4a8d12016-07-08 12:30:39 +020095
tierno9228f512019-07-04 16:23:00 +000096
tierno72774862020-05-04 11:44:15 +000097class VimConnAuthException(VimConnException):
tiernoa7d34d02017-02-23 14:42:07 +010098 """Invalid credentials or authorization to perform this action over the VIM"""
sousaedu80135b92021-02-17 15:05:18 +010099
tiernoae4a8d12016-07-08 12:30:39 +0200100 def __init__(self, message, http_code=HTTP_Unauthorized):
tierno72774862020-05-04 11:44:15 +0000101 VimConnException.__init__(self, message, http_code)
tiernoae4a8d12016-07-08 12:30:39 +0200102
tierno9228f512019-07-04 16:23:00 +0000103
tierno72774862020-05-04 11:44:15 +0000104class VimConnNotFoundException(VimConnException):
tiernoa7d34d02017-02-23 14:42:07 +0100105 """The item is not found at VIM"""
sousaedu80135b92021-02-17 15:05:18 +0100106
tiernoae4a8d12016-07-08 12:30:39 +0200107 def __init__(self, message, http_code=HTTP_Not_Found):
tierno72774862020-05-04 11:44:15 +0000108 VimConnException.__init__(self, message, http_code)
tiernoae4a8d12016-07-08 12:30:39 +0200109
tierno9228f512019-07-04 16:23:00 +0000110
tierno72774862020-05-04 11:44:15 +0000111class VimConnConflictException(VimConnException):
tiernoa7d34d02017-02-23 14:42:07 +0100112 """There is a conflict, e.g. more item found than one"""
sousaedu80135b92021-02-17 15:05:18 +0100113
tiernoae4a8d12016-07-08 12:30:39 +0200114 def __init__(self, message, http_code=HTTP_Conflict):
tierno72774862020-05-04 11:44:15 +0000115 VimConnException.__init__(self, message, http_code)
tiernoae4a8d12016-07-08 12:30:39 +0200116
tierno9228f512019-07-04 16:23:00 +0000117
tierno72774862020-05-04 11:44:15 +0000118class VimConnNotSupportedException(VimConnException):
tiernoa7d34d02017-02-23 14:42:07 +0100119 """The request is not supported by connector"""
sousaedu80135b92021-02-17 15:05:18 +0100120
tiernoa7d34d02017-02-23 14:42:07 +0100121 def __init__(self, message, http_code=HTTP_Service_Unavailable):
tierno72774862020-05-04 11:44:15 +0000122 VimConnException.__init__(self, message, http_code)
tiernoa7d34d02017-02-23 14:42:07 +0100123
tierno9228f512019-07-04 16:23:00 +0000124
tierno72774862020-05-04 11:44:15 +0000125class VimConnNotImplemented(VimConnException):
tiernoa7d34d02017-02-23 14:42:07 +0100126 """The method is not implemented by the connected"""
sousaedu80135b92021-02-17 15:05:18 +0100127
tiernoae4a8d12016-07-08 12:30:39 +0200128 def __init__(self, message, http_code=HTTP_Not_Implemented):
tierno72774862020-05-04 11:44:15 +0000129 VimConnException.__init__(self, message, http_code)
tierno7edb6752016-03-21 17:37:52 +0100130
mirabal29356312017-07-27 12:21:22 +0200131
sousaedu80135b92021-02-17 15:05:18 +0100132class VimConnector:
tiernoa7d34d02017-02-23 14:42:07 +0100133 """Abstract base class for all the VIM connector plugins
tierno72774862020-05-04 11:44:15 +0000134 These plugins must implement a VimConnector class derived from this
tiernoa7d34d02017-02-23 14:42:07 +0100135 and all these privated methods
borsatti8a2dda32019-12-18 15:08:57 +0000136 """
sousaedu80135b92021-02-17 15:05:18 +0100137
138 def __init__(
139 self,
140 uuid,
141 name,
142 tenant_id,
143 tenant_name,
144 url,
145 url_admin=None,
146 user=None,
147 passwd=None,
148 log_level=None,
149 config={},
150 persistent_info={},
151 ):
tierno9228f512019-07-04 16:23:00 +0000152 """
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
tiernoa7d34d02017-02-23 14:42:07 +0100167 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
169
tiernofb1987b2016-11-15 17:35:06 +0000170 """
tierno9228f512019-07-04 16:23:00 +0000171 self.id = uuid
172 self.name = name
173 self.url = url
tierno7edb6752016-03-21 17:37:52 +0100174 self.url_admin = url_admin
tierno392f2852016-05-13 12:28:55 +0200175 self.tenant_id = tenant_id
176 self.tenant_name = tenant_name
tierno9228f512019-07-04 16:23:00 +0000177 self.user = user
178 self.passwd = passwd
179 self.config = config or {}
mirabal29356312017-07-27 12:21:22 +0200180 self.availability_zone = None
sousaedu80135b92021-02-17 15:05:18 +0100181 self.logger = logging.getLogger("ro.vim")
182
tiernofe789902016-09-29 14:20:44 +0000183 if log_level:
tierno9228f512019-07-04 16:23:00 +0000184 self.logger.setLevel(getattr(logging, log_level))
sousaedu80135b92021-02-17 15:05:18 +0100185
186 if not self.url_admin: # try to use normal url
tiernoae4a8d12016-07-08 12:30:39 +0200187 self.url_admin = self.url
borsatti8a2dda32019-12-18 15:08:57 +0000188
tierno9228f512019-07-04 16:23:00 +0000189 def __getitem__(self, index):
sousaedu80135b92021-02-17 15:05:18 +0100190 if index == "tenant_id":
tierno392f2852016-05-13 12:28:55 +0200191 return self.tenant_id
sousaedu80135b92021-02-17 15:05:18 +0100192
193 if index == "tenant_name":
tierno392f2852016-05-13 12:28:55 +0200194 return self.tenant_name
sousaedu80135b92021-02-17 15:05:18 +0100195 elif index == "id":
tierno7edb6752016-03-21 17:37:52 +0100196 return self.id
sousaedu80135b92021-02-17 15:05:18 +0100197 elif index == "name":
tierno7edb6752016-03-21 17:37:52 +0100198 return self.name
sousaedu80135b92021-02-17 15:05:18 +0100199 elif index == "user":
tierno7edb6752016-03-21 17:37:52 +0100200 return self.user
sousaedu80135b92021-02-17 15:05:18 +0100201 elif index == "passwd":
tierno7edb6752016-03-21 17:37:52 +0100202 return self.passwd
sousaedu80135b92021-02-17 15:05:18 +0100203 elif index == "url":
tierno7edb6752016-03-21 17:37:52 +0100204 return self.url
sousaedu80135b92021-02-17 15:05:18 +0100205 elif index == "url_admin":
tierno7edb6752016-03-21 17:37:52 +0100206 return self.url_admin
tierno9228f512019-07-04 16:23:00 +0000207 elif index == "config":
tierno7edb6752016-03-21 17:37:52 +0100208 return self.config
209 else:
tierno9228f512019-07-04 16:23:00 +0000210 raise KeyError("Invalid key '{}'".format(index))
borsatti8a2dda32019-12-18 15:08:57 +0000211
tierno9228f512019-07-04 16:23:00 +0000212 def __setitem__(self, index, value):
sousaedu80135b92021-02-17 15:05:18 +0100213 if index == "tenant_id":
tierno392f2852016-05-13 12:28:55 +0200214 self.tenant_id = value
sousaedu80135b92021-02-17 15:05:18 +0100215
216 if index == "tenant_name":
tierno392f2852016-05-13 12:28:55 +0200217 self.tenant_name = value
sousaedu80135b92021-02-17 15:05:18 +0100218 elif index == "id":
tierno7edb6752016-03-21 17:37:52 +0100219 self.id = value
sousaedu80135b92021-02-17 15:05:18 +0100220 elif index == "name":
tierno7edb6752016-03-21 17:37:52 +0100221 self.name = value
sousaedu80135b92021-02-17 15:05:18 +0100222 elif index == "user":
tierno7edb6752016-03-21 17:37:52 +0100223 self.user = value
sousaedu80135b92021-02-17 15:05:18 +0100224 elif index == "passwd":
tierno7edb6752016-03-21 17:37:52 +0100225 self.passwd = value
sousaedu80135b92021-02-17 15:05:18 +0100226 elif index == "url":
tierno7edb6752016-03-21 17:37:52 +0100227 self.url = value
sousaedu80135b92021-02-17 15:05:18 +0100228 elif index == "url_admin":
tierno7edb6752016-03-21 17:37:52 +0100229 self.url_admin = value
230 else:
tierno9228f512019-07-04 16:23:00 +0000231 raise KeyError("Invalid key '{}'".format(index))
tierno0a1437e2017-10-02 00:17:43 +0200232
233 @staticmethod
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
239 """
240 if not content_list:
241 return None
242 elif len(content_list) == 1:
243 return content_list[0]
sousaedu80135b92021-02-17 15:05:18 +0100244
tierno0a1437e2017-10-02 00:17:43 +0200245 combined_message = MIMEMultipart()
sousaedu80135b92021-02-17 15:05:18 +0100246
tierno0a1437e2017-10-02 00:17:43 +0200247 for content in content_list:
sousaedu80135b92021-02-17 15:05:18 +0100248 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"
tierno0a1437e2017-10-02 00:17:43 +0200264 else: # by default
sousaedu80135b92021-02-17 15:05:18 +0100265 mime_format = "text/x-shellscript"
266
tierno9228f512019-07-04 16:23:00 +0000267 sub_message = MIMEText(content, mime_format, sys.getdefaultencoding())
tierno0a1437e2017-10-02 00:17:43 +0200268 combined_message.attach(sub_message)
sousaedu80135b92021-02-17 15:05:18 +0100269
tierno0a1437e2017-10-02 00:17:43 +0200270 return combined_message.as_string()
271
272 def _create_user_data(self, cloud_config):
273 """
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
291 """
292 config_drive = None
293 userdata = None
294 userdata_list = []
sousaedu80135b92021-02-17 15:05:18 +0100295
tierno0a1437e2017-10-02 00:17:43 +0200296 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"])
300 else:
301 for u in cloud_config["user-data"]:
302 userdata_list.append(u)
sousaedu80135b92021-02-17 15:05:18 +0100303
tierno9228f512019-07-04 16:23:00 +0000304 if cloud_config.get("boot-data-drive") is not None:
tierno0a1437e2017-10-02 00:17:43 +0200305 config_drive = cloud_config["boot-data-drive"]
sousaedu80135b92021-02-17 15:05:18 +0100306
307 if (
308 cloud_config.get("config-files")
309 or cloud_config.get("users")
310 or cloud_config.get("key-pairs")
311 ):
tierno0a1437e2017-10-02 00:17:43 +0200312 userdata_dict = {}
sousaedu80135b92021-02-17 15:05:18 +0100313
tierno0a1437e2017-10-02 00:17:43 +0200314 # default user
315 if cloud_config.get("key-pairs"):
316 userdata_dict["ssh-authorized-keys"] = cloud_config["key-pairs"]
sousaedu80135b92021-02-17 15:05:18 +0100317 userdata_dict["users"] = [
318 {
319 "default": None,
320 "ssh-authorized-keys": cloud_config["key-pairs"],
321 }
322 ]
323
tierno0a1437e2017-10-02 00:17:43 +0200324 if cloud_config.get("users"):
325 if "users" not in userdata_dict:
326 userdata_dict["users"] = ["default"]
sousaedu80135b92021-02-17 15:05:18 +0100327
tierno0a1437e2017-10-02 00:17:43 +0200328 for user in cloud_config["users"]:
329 user_info = {
330 "name": user["name"],
sousaedu80135b92021-02-17 15:05:18 +0100331 "sudo": "ALL = (ALL)NOPASSWD:ALL",
tierno0a1437e2017-10-02 00:17:43 +0200332 }
sousaedu80135b92021-02-17 15:05:18 +0100333
tierno0a1437e2017-10-02 00:17:43 +0200334 if "user-info" in user:
335 user_info["gecos"] = user["user-info"]
sousaedu80135b92021-02-17 15:05:18 +0100336
tierno0a1437e2017-10-02 00:17:43 +0200337 if user.get("key-pairs"):
338 user_info["ssh-authorized-keys"] = user["key-pairs"]
sousaedu80135b92021-02-17 15:05:18 +0100339
tierno0a1437e2017-10-02 00:17:43 +0200340 userdata_dict["users"].append(user_info)
341
342 if cloud_config.get("config-files"):
343 userdata_dict["write_files"] = []
344 for file in cloud_config["config-files"]:
sousaedu80135b92021-02-17 15:05:18 +0100345 file_info = {"path": file["dest"], "content": file["content"]}
346
tierno0a1437e2017-10-02 00:17:43 +0200347 if file.get("encoding"):
348 file_info["encoding"] = file["encoding"]
sousaedu80135b92021-02-17 15:05:18 +0100349
tierno0a1437e2017-10-02 00:17:43 +0200350 if file.get("permissions"):
351 file_info["permissions"] = file["permissions"]
sousaedu80135b92021-02-17 15:05:18 +0100352
tierno0a1437e2017-10-02 00:17:43 +0200353 if file.get("owner"):
354 file_info["owner"] = file["owner"]
sousaedu80135b92021-02-17 15:05:18 +0100355
tierno0a1437e2017-10-02 00:17:43 +0200356 userdata_dict["write_files"].append(file_info)
sousaedu80135b92021-02-17 15:05:18 +0100357
358 userdata_list.append(
359 "#cloud-config\n"
360 + yaml.safe_dump(userdata_dict, indent=4, default_flow_style=False)
361 )
tierno0a1437e2017-10-02 00:17:43 +0200362 userdata = self._create_mimemultipart(userdata_list)
363 self.logger.debug("userdata: %s", userdata)
364 elif isinstance(cloud_config, str):
365 userdata = cloud_config
sousaedu80135b92021-02-17 15:05:18 +0100366
tierno0a1437e2017-10-02 00:17:43 +0200367 return config_drive, userdata
368
tiernoa7d34d02017-02-23 14:42:07 +0100369 def check_vim_connectivity(self):
370 """Checks VIM can be reached and user credentials are ok.
tierno72774862020-05-04 11:44:15 +0000371 Returns None if success or raises VimConnConnectionException, VimConnAuthException, ...
tiernoa7d34d02017-02-23 14:42:07 +0100372 """
tierno9228f512019-07-04 16:23:00 +0000373 # by default no checking until each connector implements it
374 return None
tiernoa7d34d02017-02-23 14:42:07 +0100375
tiernoae4a8d12016-07-08 12:30:39 +0200376 def get_tenant_list(self, filter_dict={}):
tiernoa7d34d02017-02-23 14:42:07 +0100377 """Obtain tenants of VIM
tiernofb1987b2016-11-15 17:35:06 +0000378 filter_dict dictionary that can contain the following keys:
tiernoae4a8d12016-07-08 12:30:39 +0200379 name: filter by tenant name
380 id: filter by tenant uuid/id
381 <other VIM specific>
tiernofb1987b2016-11-15 17:35:06 +0000382 Returns the tenant list of dictionaries, and empty list if no tenant match all the filers:
tiernoae4a8d12016-07-08 12:30:39 +0200383 [{'name':'<name>, 'id':'<id>, ...}, ...]
tiernoa7d34d02017-02-23 14:42:07 +0100384 """
tierno72774862020-05-04 11:44:15 +0000385 raise VimConnNotImplemented("Should have implemented this")
tiernoae4a8d12016-07-08 12:30:39 +0200386
sousaedu80135b92021-02-17 15:05:18 +0100387 def new_network(
388 self,
389 net_name,
390 net_type,
391 ip_profile=None,
392 shared=False,
393 provider_network_profile=None,
394 ):
tiernofb1987b2016-11-15 17:35:06 +0000395 """Adds a tenant network to VIM
tiernoa7d34d02017-02-23 14:42:07 +0100396 Params:
397 'net_name': name of the network
398 'net_type': one of:
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.
tierno41a69812018-02-16 14:34:33 +0100402 '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.
tiernoa7d34d02017-02-23 14:42:07 +0100410 'shared': if this network can be seen/use by other tenants/organization
kbsuba85c54d2019-10-17 16:30:32 +0000411 'provider_network_profile': (optional) contains {segmentation-id: vlan, provider-network: vim_netowrk}
garciadeblasebd66722019-01-31 16:01:31 +0000412 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.
tierno72774862020-05-04 11:44:15 +0000415 Format is VimConnector dependent, but do not use nested dictionaries and a value of None should be the same
garciadeblasebd66722019-01-31 16:01:31 +0000416 as not present.
tiernofb1987b2016-11-15 17:35:06 +0000417 """
tierno72774862020-05-04 11:44:15 +0000418 raise VimConnNotImplemented("Should have implemented this")
tierno7edb6752016-03-21 17:37:52 +0100419
420 def get_network_list(self, filter_dict={}):
tiernoa7d34d02017-02-23 14:42:07 +0100421 """Obtain tenant networks of VIM
422 Params:
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
tierno72774862020-05-04 11:44:15 +0000428 ,#(not used yet) admin_state_up: boolean => returns only networks that are (or are not) in admin state
429 active
tiernoa7d34d02017-02-23 14:42:07 +0100430 #(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'
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +0100435 '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
tiernoa7d34d02017-02-23 14:42:07 +0100437 '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
441 """
tierno72774862020-05-04 11:44:15 +0000442 raise VimConnNotImplemented("Should have implemented this")
tierno7edb6752016-03-21 17:37:52 +0100443
tiernoae4a8d12016-07-08 12:30:39 +0200444 def get_network(self, net_id):
tiernoa7d34d02017-02-23 14:42:07 +0100445 """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
453 """
tierno72774862020-05-04 11:44:15 +0000454 raise VimConnNotImplemented("Should have implemented this")
tierno7edb6752016-03-21 17:37:52 +0100455
garciadeblasebd66722019-01-31 16:01:31 +0000456 def delete_network(self, net_id, created_items=None):
457 """
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
tiernoa7d34d02017-02-23 14:42:07 +0100461 Returns the network identifier or raises an exception upon error or when network is not found
462 """
tierno72774862020-05-04 11:44:15 +0000463 raise VimConnNotImplemented("Should have implemented this")
tierno7edb6752016-03-21 17:37:52 +0100464
tiernoae4a8d12016-07-08 12:30:39 +0200465 def refresh_nets_status(self, net_list):
tiernoa7d34d02017-02-23 14:42:07 +0100466 """Get the status of the networks
467 Params:
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)
480 'net_id2': ...
481 """
tierno72774862020-05-04 11:44:15 +0000482 raise VimConnNotImplemented("Should have implemented this")
tiernoae4a8d12016-07-08 12:30:39 +0200483
484 def get_flavor(self, flavor_id):
tiernoa7d34d02017-02-23 14:42:07 +0100485 """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
488 """
tierno72774862020-05-04 11:44:15 +0000489 raise VimConnNotImplemented("Should have implemented this")
tiernocf157a82017-01-30 14:07:06 +0100490
491 def get_flavor_id_from_data(self, flavor_dict):
492 """Obtain flavor id that match the flavor description
tiernoa7d34d02017-02-23 14:42:07 +0100493 Params:
494 'flavor_dict': dictionary that contains:
495 'disk': main hard disk in GB
496 'ram': meomry in MB
497 'vcpus': number of virtual cpus
498 #TODO: complete parameters for EPA
tierno72774862020-05-04 11:44:15 +0000499 Returns the flavor_id or raises a VimConnNotFoundException
tiernocf157a82017-01-30 14:07:06 +0100500 """
tierno72774862020-05-04 11:44:15 +0000501 raise VimConnNotImplemented("Should have implemented this")
tiernocf157a82017-01-30 14:07:06 +0100502
tiernoae4a8d12016-07-08 12:30:39 +0200503 def new_flavor(self, flavor_data):
tiernoa7d34d02017-02-23 14:42:07 +0100504 """Adds a tenant flavor to VIM
tiernoae4a8d12016-07-08 12:30:39 +0200505 flavor_data contains a dictionary with information, keys:
506 name: flavor name
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
tierno72774862020-05-04 11:44:15 +0000512 paired-threads|cores|threads: number of paired hyperthreads, complete cores OR individual
513 threads
tiernoae4a8d12016-07-08 12:30:39 +0200514 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
borsatti8a2dda32019-12-18 15:08:57 +0000518 vpci: requested virtual PCI address
tiernoae4a8d12016-07-08 12:30:39 +0200519 disk: disk size
520 is_public:
tiernoae4a8d12016-07-08 12:30:39 +0200521 #TODO to concrete
sousaedu80135b92021-02-17 15:05:18 +0100522 Returns the flavor identifier
523 """
tierno72774862020-05-04 11:44:15 +0000524 raise VimConnNotImplemented("Should have implemented this")
tierno7edb6752016-03-21 17:37:52 +0100525
tiernoae4a8d12016-07-08 12:30:39 +0200526 def delete_flavor(self, flavor_id):
tiernoa7d34d02017-02-23 14:42:07 +0100527 """Deletes a tenant flavor from VIM identify by its id
sousaedu80135b92021-02-17 15:05:18 +0100528 Returns the used id or raise an exception
529 """
tierno72774862020-05-04 11:44:15 +0000530 raise VimConnNotImplemented("Should have implemented this")
tierno7edb6752016-03-21 17:37:52 +0100531
Alexis Romerob70f4ed2022-03-11 18:00:49 +0100532 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
536 """
537 raise VimConnNotImplemented("Should have implemented this")
538
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
546 """
547 raise VimConnNotImplemented("Should have implemented this")
548
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
552 """
553 raise VimConnNotImplemented("Should have implemented this")
554
tiernoa7d34d02017-02-23 14:42:07 +0100555 def new_image(self, image_dict):
sousaedu80135b92021-02-17 15:05:18 +0100556 """Adds a tenant image to VIM
tiernoa7d34d02017-02-23 14:42:07 +0100557 Returns the image id or raises an exception if failed
558 """
tierno72774862020-05-04 11:44:15 +0000559 raise VimConnNotImplemented("Should have implemented this")
tierno7edb6752016-03-21 17:37:52 +0100560
tiernoae4a8d12016-07-08 12:30:39 +0200561 def delete_image(self, image_id):
tiernoa7d34d02017-02-23 14:42:07 +0100562 """Deletes a tenant image from VIM
sousaedu80135b92021-02-17 15:05:18 +0100563 Returns the image_id if image is deleted or raises an exception on error
564 """
tierno72774862020-05-04 11:44:15 +0000565 raise VimConnNotImplemented("Should have implemented this")
tierno7edb6752016-03-21 17:37:52 +0100566
tiernoae4a8d12016-07-08 12:30:39 +0200567 def get_image_id_from_path(self, path):
tiernocf157a82017-01-30 14:07:06 +0100568 """Get the image id from image path in the VIM database.
sousaedu80135b92021-02-17 15:05:18 +0100569 Returns the image_id or raises a VimConnNotFoundException
tiernocf157a82017-01-30 14:07:06 +0100570 """
tierno72774862020-05-04 11:44:15 +0000571 raise VimConnNotImplemented("Should have implemented this")
borsatti8a2dda32019-12-18 15:08:57 +0000572
garciadeblasb69fa9f2016-09-28 12:04:10 +0200573 def get_image_list(self, filter_dict={}):
tiernoa7d34d02017-02-23 14:42:07 +0100574 """Obtain tenant images from VIM
garciadeblasb69fa9f2016-09-28 12:04:10 +0200575 Filter_dict can be:
576 name: image name
577 id: image uuid
578 checksum: image checksum
579 location: image path
580 Returns the image list of dictionaries:
581 [{<the fields at Filter_dict plus some VIM specific>}, ...]
582 List can be empty
tiernoa7d34d02017-02-23 14:42:07 +0100583 """
tierno72774862020-05-04 11:44:15 +0000584 raise VimConnNotImplemented("Should have implemented this")
garciadeblasb69fa9f2016-09-28 12:04:10 +0200585
sousaedu80135b92021-02-17 15:05:18 +0100586 def new_vminstance(
587 self,
588 name,
589 description,
590 start,
591 image_id,
592 flavor_id,
Alexis Romerob70f4ed2022-03-11 18:00:49 +0100593 affinity_group_list,
sousaedu80135b92021-02-17 15:05:18 +0100594 net_list,
595 cloud_config=None,
596 disk_list=None,
597 availability_zone_index=None,
598 availability_zone_list=None,
599 ):
tiernoa7d34d02017-02-23 14:42:07 +0100600 """Adds a VM instance to VIM
tierno7edb6752016-03-21 17:37:52 +0100601 Params:
tiernoa7d34d02017-02-23 14:42:07 +0100602 '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
Alexis Romerob70f4ed2022-03-11 18:00:49 +0100604 affinity_group_list: list of affinity groups, each one is a dictionary.
605 Ignore if empty.
tiernoa7d34d02017-02-23 14:42:07 +0100606 '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
sousaedu80135b92021-02-17 15:05:18 +0100609 'vpci': (optional) virtual vPCI address to assign at the VM. Can be ignored depending on VIM
tierno72774862020-05-04 11:44:15 +0000610 capabilities
garciadeblasc4f4d732018-10-25 18:17:24 +0200611 'model': (optional and only have sense for type==virtual) interface model: virtio, e1000, ...
tiernoa7d34d02017-02-23 14:42:07 +0100612 'mac_address': (optional) mac address to assign to this interface
tierno41a69812018-02-16 14:34:33 +0100613 'ip_address': (optional) IP address to assign to this interface
tierno72774862020-05-04 11:44:15 +0000614 #TODO: CHECK if an optional 'vlan' parameter is needed for VIMs when type if VF and net_id is not
sousaedu80135b92021-02-17 15:05:18 +0100615 provided, the VLAN tag to be used. In case net_id is provided, the internal network vlan is used
tierno72774862020-05-04 11:44:15 +0000616 for tagging VF
tiernoa7d34d02017-02-23 14:42:07 +0100617 'type': (mandatory) can be one of:
618 'virtual', in this case always connected to a network of type 'net_type=bridge'
tierno72774862020-05-04 11:44:15 +0000619 'PCI-PASSTHROUGH' or 'PF' (passthrough): depending on VIM capabilities it can be connected to a
620 data/ptp network ot it
tiernoa7d34d02017-02-23 14:42:07 +0100621 can created unconnected
tierno66eba6e2017-11-10 17:09:18 +0100622 'SR-IOV' or 'VF' (SRIOV with VLAN tag): same as PF for network connectivity.
tiernoa7d34d02017-02-23 14:42:07 +0100623 '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
tiernob3d36742017-03-03 23:51:05 +0100626 '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
tiernoa7d34d02017-02-23 14:42:07 +0100628 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
tierno40e1bce2017-08-09 09:12:04 +0200636 '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
tiernoa7d34d02017-02-23 14:42:07 +0100638 '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
tierno5a3273c2017-08-29 11:43:46 +0200649 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
tierno98e909c2017-10-14 13:27:03 +0200652 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.
tierno72774862020-05-04 11:44:15 +0000655 Format is VimConnector dependent, but do not use nested dictionaries and a value of None should be the same
tierno98e909c2017-10-14 13:27:03 +0200656 as not present.
tiernoa7d34d02017-02-23 14:42:07 +0100657 """
tierno72774862020-05-04 11:44:15 +0000658 raise VimConnNotImplemented("Should have implemented this")
borsatti8a2dda32019-12-18 15:08:57 +0000659
tierno72774862020-05-04 11:44:15 +0000660 def get_vminstance(self, vm_id):
tiernoa7d34d02017-02-23 14:42:07 +0100661 """Returns the VM instance information from VIM"""
tierno72774862020-05-04 11:44:15 +0000662 raise VimConnNotImplemented("Should have implemented this")
borsatti8a2dda32019-12-18 15:08:57 +0000663
tierno98e909c2017-10-14 13:27:03 +0200664 def delete_vminstance(self, vm_id, created_items=None):
665 """
garciadeblasebd66722019-01-31 16:01:31 +0000666 Removes a VM instance from VIM and its associated elements
tierno98e909c2017-10-14 13:27:03 +0200667 :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
669 action_vminstance
670 :return: None or the same vm_id. Raises an exception on fail
671 """
tierno72774862020-05-04 11:44:15 +0000672 raise VimConnNotImplemented("Should have implemented this")
tierno7edb6752016-03-21 17:37:52 +0100673
tiernoae4a8d12016-07-08 12:30:39 +0200674 def refresh_vms_status(self, vm_list):
tiernoa7d34d02017-02-23 14:42:07 +0100675 """Get the status of the virtual machines and their interfaces/ports
sousaedu80135b92021-02-17 15:05:18 +0100676 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
687 #
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
tiernoa7d34d02017-02-23 14:42:07 +0100699 """
tierno72774862020-05-04 11:44:15 +0000700 raise VimConnNotImplemented("Should have implemented this")
borsatti8a2dda32019-12-18 15:08:57 +0000701
tierno98e909c2017-10-14 13:27:03 +0200702 def action_vminstance(self, vm_id, action_dict, created_items={}):
703 """
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
tierno72774862020-05-04 11:44:15 +0000709 the method delete_vminstance. Can be used to store created ports, volumes, etc. Format is VimConnector
tierno98e909c2017-10-14 13:27:03 +0200710 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
713 """
tierno72774862020-05-04 11:44:15 +0000714 raise VimConnNotImplemented("Should have implemented this")
borsatti8a2dda32019-12-18 15:08:57 +0000715
tiernoa7d34d02017-02-23 14:42:07 +0100716 def get_vminstance_console(self, vm_id, console_type="vnc"):
717 """
tiernoae4a8d12016-07-08 12:30:39 +0200718 Get a console for the virtual machine
719 Params:
720 vm_id: uuid of the VM
721 console_type, can be:
borsatti8a2dda32019-12-18 15:08:57 +0000722 "novnc" (by default), "xvpvnc" for VNC types,
tiernoae4a8d12016-07-08 12:30:39 +0200723 "rdp-html5" for RDP types, "spice-html5" for SPICE types
724 Returns dict with the console parameters:
725 protocol: ssh, ftp, http, https, ...
borsatti8a2dda32019-12-18 15:08:57 +0000726 server: usually ip address
727 port: the http, ssh, ... port
728 suffix: extra text, e.g. the http path and query string
tiernoa7d34d02017-02-23 14:42:07 +0100729 """
tierno72774862020-05-04 11:44:15 +0000730 raise VimConnNotImplemented("Should have implemented this")
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +0000731
sousaedu80135b92021-02-17 15:05:18 +0100732 def inject_user_key(
733 self, ip_addr=None, user=None, key=None, ro_key=None, password=None
734 ):
tierno9228f512019-07-04 16:23:00 +0000735 """
736 Inject a ssh public key in a VM
737 Params:
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:
744 """
745 if not ip_addr or not user:
sousaedu80135b92021-02-17 15:05:18 +0100746 raise VimConnNotSupportedException(
747 "All parameters should be different from 'None'"
748 )
tierno9228f512019-07-04 16:23:00 +0000749 elif not ro_key and not password:
sousaedu80135b92021-02-17 15:05:18 +0100750 raise VimConnNotSupportedException(
751 "All parameters should be different from 'None'"
752 )
tierno9228f512019-07-04 16:23:00 +0000753 else:
sousaedu80135b92021-02-17 15:05:18 +0100754 commands = {
755 "mkdir -p ~/.ssh/",
756 'echo "{}" >> ~/.ssh/authorized_keys'.format(key),
757 "chmod 644 ~/.ssh/authorized_keys",
758 "chmod 700 ~/.ssh/",
759 }
tierno9228f512019-07-04 16:23:00 +0000760 client = paramiko.SSHClient()
sousaedu80135b92021-02-17 15:05:18 +0100761
tierno9228f512019-07-04 16:23:00 +0000762 try:
763 if ro_key:
tierno7d782ef2019-10-04 12:56:31 +0000764 pkey = paramiko.RSAKey.from_private_key(StringIO(ro_key))
tierno9228f512019-07-04 16:23:00 +0000765 else:
766 pkey = None
sousaedu80135b92021-02-17 15:05:18 +0100767
tierno9228f512019-07-04 16:23:00 +0000768 client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
sousaedu80135b92021-02-17 15:05:18 +0100769 client.connect(
aticige5d78422022-05-16 23:03:54 +0300770 ip_addr, username=user, password=password, pkey=pkey, timeout=30
sousaedu80135b92021-02-17 15:05:18 +0100771 )
772
tierno9228f512019-07-04 16:23:00 +0000773 for command in commands:
aticige5d78422022-05-16 23:03:54 +0300774 (i, o, e) = client.exec_command(command, timeout=30)
tierno9228f512019-07-04 16:23:00 +0000775 returncode = o.channel.recv_exit_status()
tierno9228f512019-07-04 16:23:00 +0000776 outerror = e.read()
sousaedu80135b92021-02-17 15:05:18 +0100777
tierno9228f512019-07-04 16:23:00 +0000778 if returncode != 0:
779 text = "run_command='{}' Error='{}'".format(command, outerror)
aticige5d78422022-05-16 23:03:54 +0300780 self.logger.debug(traceback.format_tb(e.__traceback__))
sousaedu80135b92021-02-17 15:05:18 +0100781 raise VimConnUnexpectedResponse(
782 "Cannot inject ssh key in VM: '{}'".format(text)
783 )
tierno9228f512019-07-04 16:23:00 +0000784 return
sousaedu80135b92021-02-17 15:05:18 +0100785 except (
786 socket.error,
787 paramiko.AuthenticationException,
788 paramiko.SSHException,
789 ) as message:
aticige5d78422022-05-16 23:03:54 +0300790 self.logger.debug(traceback.format_exc())
tierno72774862020-05-04 11:44:15 +0000791 raise VimConnUnexpectedResponse(
sousaedu80135b92021-02-17 15:05:18 +0100792 "Cannot inject ssh key in VM: '{}' - {}".format(
793 ip_addr, str(message)
794 )
795 )
tierno9228f512019-07-04 16:23:00 +0000796 return
797
sousaedu80135b92021-02-17 15:05:18 +0100798 # Optional methods
tierno72774862020-05-04 11:44:15 +0000799 def new_tenant(self, tenant_name, tenant_description):
tierno9228f512019-07-04 16:23:00 +0000800 """Adds a new tenant to VIM with this name and description, this is done using admin_url if provided
801 "tenant_name": string max lenght 64
802 "tenant_description": string max length 256
803 returns the tenant identifier or raise exception
804 """
tierno72774862020-05-04 11:44:15 +0000805 raise VimConnNotImplemented("Should have implemented this")
tierno9228f512019-07-04 16:23:00 +0000806
sousaedu80135b92021-02-17 15:05:18 +0100807 def delete_tenant(self, tenant_id):
tierno9228f512019-07-04 16:23:00 +0000808 """Delete a tenant from VIM
809 tenant_id: returned VIM tenant_id on "new_tenant"
tierno72774862020-05-04 11:44:15 +0000810 Returns None on success. Raises and exception of failure. If tenant is not found raises VimConnNotFoundException
tierno9228f512019-07-04 16:23:00 +0000811 """
tierno72774862020-05-04 11:44:15 +0000812 raise VimConnNotImplemented("Should have implemented this")
tierno9228f512019-07-04 16:23:00 +0000813
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +0000814 def new_classification(self, name, ctype, definition):
815 """Creates a traffic classification in the VIM
816 Params:
817 'name': name of this classification
818 'ctype': type of this classification
819 'definition': definition of this classification (type-dependent free-form text)
820 Returns the VIM's classification ID on success or raises an exception on failure
821 """
tierno72774862020-05-04 11:44:15 +0000822 raise VimConnNotImplemented("SFC support not implemented")
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +0000823
824 def get_classification(self, classification_id):
825 """Obtain classification details of the VIM's classification with ID='classification_id'
826 Return a dict that contains:
827 'id': VIM's classification ID (same as classification_id)
828 'name': VIM's classification name
829 'type': type of this classification
830 'definition': definition of the classification
831 'status': 'ACTIVE', 'INACTIVE', 'DOWN', 'BUILD', 'ERROR', 'VIM_ERROR', 'OTHER'
832 'error_msg': (optional) text that explains the ERROR status
833 other VIM specific fields: (optional) whenever possible
834 Raises an exception upon error or when classification is not found
835 """
tierno72774862020-05-04 11:44:15 +0000836 raise VimConnNotImplemented("SFC support not implemented")
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +0000837
838 def get_classification_list(self, filter_dict={}):
839 """Obtain classifications from the VIM
840 Params:
tierno72774862020-05-04 11:44:15 +0000841 'filter_dict' (optional): contains the entries to filter the classifications on and only return those that
842 match ALL:
843 id: string => returns classifications with this VIM's classification ID, which implies a return of one
844 classification at most
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +0000845 name: string => returns only classifications with this name
846 type: string => returns classifications of this type
847 definition: string => returns classifications that have this definition
848 tenant_id: string => returns only classifications that belong to this tenant/project
849 Returns a list of classification dictionaries, each dictionary contains:
850 'id': (mandatory) VIM's classification ID
851 'name': (mandatory) VIM's classification name
852 'type': type of this classification
853 'definition': definition of the classification
854 other VIM specific fields: (optional) whenever possible using the same naming of filter_dict param
855 List can be empty if no classification matches the filter_dict. Raise an exception only upon VIM connectivity,
856 authorization, or some other unspecific error
857 """
tierno72774862020-05-04 11:44:15 +0000858 raise VimConnNotImplemented("SFC support not implemented")
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +0000859
borsatti8a2dda32019-12-18 15:08:57 +0000860 def refresh_classifications_status(self, classification_list):
sousaedu80135b92021-02-17 15:05:18 +0100861 """Get the status of the classifications
862 Params: the list of classification identifiers
863 Returns a dictionary with:
864 vm_id: #VIM id of this classifier
865 status: #Mandatory. Text with one of:
866 # DELETED (not found at vim)
867 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
868 # OTHER (Vim reported other status not understood)
869 # ERROR (VIM indicates an ERROR status)
870 # ACTIVE,
871 # CREATING (on building process)
872 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
873 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
874 """
tierno72774862020-05-04 11:44:15 +0000875 raise VimConnNotImplemented("Should have implemented this")
borsatti8a2dda32019-12-18 15:08:57 +0000876
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +0000877 def delete_classification(self, classification_id):
878 """Deletes a classification from the VIM
tierno72774862020-05-04 11:44:15 +0000879 Returns the classification ID (classification_id) or raises an exception upon error or when classification is
880 not found
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +0000881 """
tierno72774862020-05-04 11:44:15 +0000882 raise VimConnNotImplemented("SFC support not implemented")
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +0000883
884 def new_sfi(self, name, ingress_ports, egress_ports, sfc_encap=True):
885 """Creates a service function instance in the VIM
886 Params:
887 'name': name of this service function instance
888 'ingress_ports': set of ingress ports (VIM's port IDs)
889 'egress_ports': set of egress ports (VIM's port IDs)
890 'sfc_encap': boolean stating whether this specific instance supports IETF SFC Encapsulation
891 Returns the VIM's service function instance ID on success or raises an exception on failure
892 """
tierno72774862020-05-04 11:44:15 +0000893 raise VimConnNotImplemented("SFC support not implemented")
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +0000894
895 def get_sfi(self, sfi_id):
896 """Obtain service function instance details of the VIM's service function instance with ID='sfi_id'
897 Return a dict that contains:
898 'id': VIM's sfi ID (same as sfi_id)
899 'name': VIM's sfi name
900 'ingress_ports': set of ingress ports (VIM's port IDs)
901 'egress_ports': set of egress ports (VIM's port IDs)
902 'status': 'ACTIVE', 'INACTIVE', 'DOWN', 'BUILD', 'ERROR', 'VIM_ERROR', 'OTHER'
903 'error_msg': (optional) text that explains the ERROR status
904 other VIM specific fields: (optional) whenever possible
905 Raises an exception upon error or when service function instance is not found
906 """
tierno72774862020-05-04 11:44:15 +0000907 raise VimConnNotImplemented("SFC support not implemented")
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +0000908
909 def get_sfi_list(self, filter_dict={}):
910 """Obtain service function instances from the VIM
911 Params:
912 'filter_dict' (optional): contains the entries to filter the sfis on and only return those that match ALL:
913 id: string => returns sfis with this VIM's sfi ID, which implies a return of one sfi at most
914 name: string => returns only service function instances with this name
915 tenant_id: string => returns only service function instances that belong to this tenant/project
916 Returns a list of service function instance dictionaries, each dictionary contains:
917 'id': (mandatory) VIM's sfi ID
918 'name': (mandatory) VIM's sfi name
919 'ingress_ports': set of ingress ports (VIM's port IDs)
920 'egress_ports': set of egress ports (VIM's port IDs)
921 other VIM specific fields: (optional) whenever possible using the same naming of filter_dict param
922 List can be empty if no sfi matches the filter_dict. Raise an exception only upon VIM connectivity,
923 authorization, or some other unspecific error
924 """
tierno72774862020-05-04 11:44:15 +0000925 raise VimConnNotImplemented("SFC support not implemented")
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +0000926
927 def delete_sfi(self, sfi_id):
928 """Deletes a service function instance from the VIM
929 Returns the service function instance ID (sfi_id) or raises an exception upon error or when sfi is not found
930 """
tierno72774862020-05-04 11:44:15 +0000931 raise VimConnNotImplemented("SFC support not implemented")
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +0000932
borsatti8a2dda32019-12-18 15:08:57 +0000933 def refresh_sfis_status(self, sfi_list):
sousaedu80135b92021-02-17 15:05:18 +0100934 """Get the status of the service function instances
935 Params: the list of sfi identifiers
936 Returns a dictionary with:
937 vm_id: #VIM id of this service function instance
938 status: #Mandatory. Text with one of:
939 # DELETED (not found at vim)
940 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
941 # OTHER (Vim reported other status not understood)
942 # ERROR (VIM indicates an ERROR status)
943 # ACTIVE,
944 # CREATING (on building process)
945 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
946 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
947 """
tierno72774862020-05-04 11:44:15 +0000948 raise VimConnNotImplemented("Should have implemented this")
borsatti8a2dda32019-12-18 15:08:57 +0000949
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +0000950 def new_sf(self, name, sfis, sfc_encap=True):
951 """Creates (an abstract) service function in the VIM
952 Params:
953 'name': name of this service function
954 'sfis': set of service function instances of this (abstract) service function
955 'sfc_encap': boolean stating whether this service function supports IETF SFC Encapsulation
956 Returns the VIM's service function ID on success or raises an exception on failure
957 """
tierno72774862020-05-04 11:44:15 +0000958 raise VimConnNotImplemented("SFC support not implemented")
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +0000959
960 def get_sf(self, sf_id):
961 """Obtain service function details of the VIM's service function with ID='sf_id'
962 Return a dict that contains:
963 'id': VIM's sf ID (same as sf_id)
964 'name': VIM's sf name
965 'sfis': VIM's sf's set of VIM's service function instance IDs
966 'sfc_encap': boolean stating whether this service function supports IETF SFC Encapsulation
967 'status': 'ACTIVE', 'INACTIVE', 'DOWN', 'BUILD', 'ERROR', 'VIM_ERROR', 'OTHER'
968 'error_msg': (optional) text that explains the ERROR status
969 other VIM specific fields: (optional) whenever possible
970 Raises an exception upon error or when sf is not found
971 """
972
973 def get_sf_list(self, filter_dict={}):
974 """Obtain service functions from the VIM
975 Params:
976 'filter_dict' (optional): contains the entries to filter the sfs on and only return those that match ALL:
977 id: string => returns sfs with this VIM's sf ID, which implies a return of one sf at most
978 name: string => returns only service functions with this name
979 tenant_id: string => returns only service functions that belong to this tenant/project
980 Returns a list of service function dictionaries, each dictionary contains:
981 'id': (mandatory) VIM's sf ID
982 'name': (mandatory) VIM's sf name
983 'sfis': VIM's sf's set of VIM's service function instance IDs
984 'sfc_encap': boolean stating whether this service function supports IETF SFC Encapsulation
985 other VIM specific fields: (optional) whenever possible using the same naming of filter_dict param
986 List can be empty if no sf matches the filter_dict. Raise an exception only upon VIM connectivity,
987 authorization, or some other unspecific error
988 """
tierno72774862020-05-04 11:44:15 +0000989 raise VimConnNotImplemented("SFC support not implemented")
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +0000990
991 def delete_sf(self, sf_id):
992 """Deletes (an abstract) service function from the VIM
993 Returns the service function ID (sf_id) or raises an exception upon error or when sf is not found
994 """
tierno72774862020-05-04 11:44:15 +0000995 raise VimConnNotImplemented("SFC support not implemented")
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +0000996
borsatti8a2dda32019-12-18 15:08:57 +0000997 def refresh_sfs_status(self, sf_list):
sousaedu80135b92021-02-17 15:05:18 +0100998 """Get the status of the service functions
999 Params: the list of sf identifiers
1000 Returns a dictionary with:
1001 vm_id: #VIM id of this service function
1002 status: #Mandatory. Text with one of:
1003 # DELETED (not found at vim)
1004 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
1005 # OTHER (Vim reported other status not understood)
1006 # ERROR (VIM indicates an ERROR status)
1007 # ACTIVE,
1008 # CREATING (on building process)
1009 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
1010 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
1011 """
tierno72774862020-05-04 11:44:15 +00001012 raise VimConnNotImplemented("Should have implemented this")
borsatti8a2dda32019-12-18 15:08:57 +00001013
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +00001014 def new_sfp(self, name, classifications, sfs, sfc_encap=True, spi=None):
1015 """Creates a service function path
1016 Params:
1017 'name': name of this service function path
1018 'classifications': set of traffic classifications that should be matched on to get into this sfp
1019 'sfs': list of every service function that constitutes this path , from first to last
1020 'sfc_encap': whether this is an SFC-Encapsulated chain (i.e using NSH), True by default
1021 'spi': (optional) the Service Function Path identifier (SPI: Service Path Identifier) for this path
1022 Returns the VIM's sfp ID on success or raises an exception on failure
1023 """
tierno72774862020-05-04 11:44:15 +00001024 raise VimConnNotImplemented("SFC support not implemented")
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +00001025
1026 def get_sfp(self, sfp_id):
1027 """Obtain service function path details of the VIM's sfp with ID='sfp_id'
1028 Return a dict that contains:
1029 'id': VIM's sfp ID (same as sfp_id)
1030 'name': VIM's sfp name
1031 'classifications': VIM's sfp's list of VIM's classification IDs
1032 'sfs': VIM's sfp's list of VIM's service function IDs
1033 'status': 'ACTIVE', 'INACTIVE', 'DOWN', 'BUILD', 'ERROR', 'VIM_ERROR', 'OTHER'
1034 'error_msg': (optional) text that explains the ERROR status
1035 other VIM specific fields: (optional) whenever possible
1036 Raises an exception upon error or when sfp is not found
1037 """
tierno72774862020-05-04 11:44:15 +00001038 raise VimConnNotImplemented("SFC support not implemented")
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +00001039
1040 def get_sfp_list(self, filter_dict={}):
1041 """Obtain service function paths from VIM
1042 Params:
1043 'filter_dict' (optional): contains the entries to filter the sfps on, and only return those that match ALL:
1044 id: string => returns sfps with this VIM's sfp ID , which implies a return of one sfp at most
1045 name: string => returns only sfps with this name
1046 tenant_id: string => returns only sfps that belong to this tenant/project
1047 Returns a list of service function path dictionaries, each dictionary contains:
1048 'id': (mandatory) VIM's sfp ID
1049 'name': (mandatory) VIM's sfp name
1050 'classifications': VIM's sfp's list of VIM's classification IDs
1051 'sfs': VIM's sfp's list of VIM's service function IDs
1052 other VIM specific fields: (optional) whenever possible using the same naming of filter_dict param
1053 List can be empty if no sfp matches the filter_dict. Raise an exception only upon VIM connectivity,
1054 authorization, or some other unspecific error
1055 """
tierno72774862020-05-04 11:44:15 +00001056 raise VimConnNotImplemented("SFC support not implemented")
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +00001057
borsatti8a2dda32019-12-18 15:08:57 +00001058 def refresh_sfps_status(self, sfp_list):
sousaedu80135b92021-02-17 15:05:18 +01001059 """Get the status of the service function path
1060 Params: the list of sfp identifiers
1061 Returns a dictionary with:
1062 vm_id: #VIM id of this service function path
1063 status: #Mandatory. Text with one of:
1064 # DELETED (not found at vim)
1065 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
1066 # OTHER (Vim reported other status not understood)
1067 # ERROR (VIM indicates an ERROR status)
1068 # ACTIVE,
1069 # CREATING (on building process)
1070 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
1071 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)F
1072 """
tierno72774862020-05-04 11:44:15 +00001073 raise VimConnNotImplemented("Should have implemented this")
borsatti8a2dda32019-12-18 15:08:57 +00001074
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +00001075 def delete_sfp(self, sfp_id):
1076 """Deletes a service function path from the VIM
1077 Returns the sfp ID (sfp_id) or raises an exception upon error or when sf is not found
1078 """
tierno72774862020-05-04 11:44:15 +00001079 raise VimConnNotImplemented("SFC support not implemented")
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +00001080
sousaedu80135b92021-02-17 15:05:18 +01001081 # NOT USED METHODS in current version. Deprecated
tierno9228f512019-07-04 16:23:00 +00001082 @deprecated
tierno7edb6752016-03-21 17:37:52 +01001083 def host_vim2gui(self, host, server_dict):
tiernoa7d34d02017-02-23 14:42:07 +01001084 """Transform host dictionary from VIM format to GUI format,
tierno7edb6752016-03-21 17:37:52 +01001085 and append to the server_dict
tiernoa7d34d02017-02-23 14:42:07 +01001086 """
tierno72774862020-05-04 11:44:15 +00001087 raise VimConnNotImplemented("Should have implemented this")
tierno7edb6752016-03-21 17:37:52 +01001088
tierno9228f512019-07-04 16:23:00 +00001089 @deprecated
tierno7edb6752016-03-21 17:37:52 +01001090 def get_hosts_info(self):
tiernoa7d34d02017-02-23 14:42:07 +01001091 """Get the information of deployed hosts
1092 Returns the hosts content"""
tierno72774862020-05-04 11:44:15 +00001093 raise VimConnNotImplemented("Should have implemented this")
tierno7edb6752016-03-21 17:37:52 +01001094
tierno9228f512019-07-04 16:23:00 +00001095 @deprecated
tierno7edb6752016-03-21 17:37:52 +01001096 def get_hosts(self, vim_tenant):
tiernoa7d34d02017-02-23 14:42:07 +01001097 """Get the hosts and deployed instances
1098 Returns the hosts content"""
tierno72774862020-05-04 11:44:15 +00001099 raise VimConnNotImplemented("Should have implemented this")
tierno7edb6752016-03-21 17:37:52 +01001100
tierno9228f512019-07-04 16:23:00 +00001101 @deprecated
tierno7edb6752016-03-21 17:37:52 +01001102 def get_processor_rankings(self):
tiernoa7d34d02017-02-23 14:42:07 +01001103 """Get the processor rankings in the VIM database"""
tierno72774862020-05-04 11:44:15 +00001104 raise VimConnNotImplemented("Should have implemented this")
borsatti8a2dda32019-12-18 15:08:57 +00001105
tierno9228f512019-07-04 16:23:00 +00001106 @deprecated
tiernoae4a8d12016-07-08 12:30:39 +02001107 def new_host(self, host_data):
tiernoa7d34d02017-02-23 14:42:07 +01001108 """Adds a new host to VIM"""
1109 """Returns status code of the VIM response"""
tierno72774862020-05-04 11:44:15 +00001110 raise VimConnNotImplemented("Should have implemented this")
borsatti8a2dda32019-12-18 15:08:57 +00001111
tierno9228f512019-07-04 16:23:00 +00001112 @deprecated
tiernoae4a8d12016-07-08 12:30:39 +02001113 def new_external_port(self, port_data):
tiernoa7d34d02017-02-23 14:42:07 +01001114 """Adds a external port to VIM"""
1115 """Returns the port identifier"""
tierno72774862020-05-04 11:44:15 +00001116 raise VimConnNotImplemented("Should have implemented this")
borsatti8a2dda32019-12-18 15:08:57 +00001117
tierno9228f512019-07-04 16:23:00 +00001118 @deprecated
tierno72774862020-05-04 11:44:15 +00001119 def new_external_network(self, net_name, net_type):
tiernoa7d34d02017-02-23 14:42:07 +01001120 """Adds a external network to VIM (shared)"""
1121 """Returns the network identifier"""
tierno72774862020-05-04 11:44:15 +00001122 raise VimConnNotImplemented("Should have implemented this")
tiernoae4a8d12016-07-08 12:30:39 +02001123
tierno9228f512019-07-04 16:23:00 +00001124 @deprecated
tiernoae4a8d12016-07-08 12:30:39 +02001125 def connect_port_network(self, port_id, network_id, admin=False):
tiernoa7d34d02017-02-23 14:42:07 +01001126 """Connects a external port to a network"""
1127 """Returns status code of the VIM response"""
tierno72774862020-05-04 11:44:15 +00001128 raise VimConnNotImplemented("Should have implemented this")
tiernoae4a8d12016-07-08 12:30:39 +02001129
tierno9228f512019-07-04 16:23:00 +00001130 @deprecated
tiernoae4a8d12016-07-08 12:30:39 +02001131 def new_vminstancefromJSON(self, vm_data):
tiernoa7d34d02017-02-23 14:42:07 +01001132 """Adds a VM instance to VIM"""
1133 """Returns the instance identifier"""
tierno72774862020-05-04 11:44:15 +00001134 raise VimConnNotImplemented("Should have implemented this")