blob: b4e936c1408137d92f36a1e9e78aeaa4359d2f40 [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
tierno72774862020-05-04 11:44:15 +000036import warnings
tierno9228f512019-07-04 16:23:00 +000037
sousaedu049cbb12022-01-05 11:39:35 +000038import paramiko
39import yaml
40
tierno9228f512019-07-04 16:23:00 +000041__author__ = "Alfonso Tierno, Igor D.C."
tierno72774862020-05-04 11:44:15 +000042__date__ = "$14-aug-2017 23:59:59$"
tierno7edb6752016-03-21 17:37:52 +010043
tierno9228f512019-07-04 16:23:00 +000044
tierno72774862020-05-04 11:44:15 +000045def deprecated(message):
46 def deprecated_decorator(func):
47 def deprecated_func(*args, **kwargs):
sousaedu80135b92021-02-17 15:05:18 +010048 warnings.warn(
49 "{} is a deprecated function. {}".format(func.__name__, message),
50 category=DeprecationWarning,
51 stacklevel=2,
52 )
53 warnings.simplefilter("default", DeprecationWarning)
54
tierno72774862020-05-04 11:44:15 +000055 return func(*args, **kwargs)
sousaedu80135b92021-02-17 15:05:18 +010056
tierno72774862020-05-04 11:44:15 +000057 return deprecated_func
sousaedu80135b92021-02-17 15:05:18 +010058
tierno72774862020-05-04 11:44:15 +000059 return deprecated_decorator
60
61
62# Error variables
63HTTP_Bad_Request = HTTPStatus.BAD_REQUEST.value
64HTTP_Unauthorized = HTTPStatus.UNAUTHORIZED.value
65HTTP_Not_Found = HTTPStatus.NOT_FOUND.value
66HTTP_Method_Not_Allowed = HTTPStatus.METHOD_NOT_ALLOWED.value
67HTTP_Request_Timeout = HTTPStatus.REQUEST_TIMEOUT.value
68HTTP_Conflict = HTTPStatus.CONFLICT.value
69HTTP_Not_Implemented = HTTPStatus.NOT_IMPLEMENTED.value
70HTTP_Service_Unavailable = HTTPStatus.SERVICE_UNAVAILABLE.value
71HTTP_Internal_Server_Error = HTTPStatus.INTERNAL_SERVER_ERROR.value
72
73
74class VimConnException(Exception):
75 """Common and base class Exception for all VimConnector exceptions"""
sousaedu80135b92021-02-17 15:05:18 +010076
tiernoae4a8d12016-07-08 12:30:39 +020077 def __init__(self, message, http_code=HTTP_Bad_Request):
78 Exception.__init__(self, message)
79 self.http_code = http_code
80
tierno9228f512019-07-04 16:23:00 +000081
tierno72774862020-05-04 11:44:15 +000082class VimConnConnectionException(VimConnException):
tiernoa7d34d02017-02-23 14:42:07 +010083 """Connectivity error with the VIM"""
sousaedu80135b92021-02-17 15:05:18 +010084
tiernoae4a8d12016-07-08 12:30:39 +020085 def __init__(self, message, http_code=HTTP_Service_Unavailable):
tierno72774862020-05-04 11:44:15 +000086 VimConnException.__init__(self, message, http_code)
tierno9228f512019-07-04 16:23:00 +000087
88
tierno72774862020-05-04 11:44:15 +000089class VimConnUnexpectedResponse(VimConnException):
tiernoa7d34d02017-02-23 14:42:07 +010090 """Get an wrong response from VIM"""
sousaedu80135b92021-02-17 15:05:18 +010091
tiernoae4a8d12016-07-08 12:30:39 +020092 def __init__(self, message, http_code=HTTP_Service_Unavailable):
tierno72774862020-05-04 11:44:15 +000093 VimConnException.__init__(self, message, http_code)
tiernoae4a8d12016-07-08 12:30:39 +020094
tierno9228f512019-07-04 16:23:00 +000095
tierno72774862020-05-04 11:44:15 +000096class VimConnAuthException(VimConnException):
tiernoa7d34d02017-02-23 14:42:07 +010097 """Invalid credentials or authorization to perform this action over the VIM"""
sousaedu80135b92021-02-17 15:05:18 +010098
tiernoae4a8d12016-07-08 12:30:39 +020099 def __init__(self, message, http_code=HTTP_Unauthorized):
tierno72774862020-05-04 11:44:15 +0000100 VimConnException.__init__(self, message, http_code)
tiernoae4a8d12016-07-08 12:30:39 +0200101
tierno9228f512019-07-04 16:23:00 +0000102
tierno72774862020-05-04 11:44:15 +0000103class VimConnNotFoundException(VimConnException):
tiernoa7d34d02017-02-23 14:42:07 +0100104 """The item is not found at VIM"""
sousaedu80135b92021-02-17 15:05:18 +0100105
tiernoae4a8d12016-07-08 12:30:39 +0200106 def __init__(self, message, http_code=HTTP_Not_Found):
tierno72774862020-05-04 11:44:15 +0000107 VimConnException.__init__(self, message, http_code)
tiernoae4a8d12016-07-08 12:30:39 +0200108
tierno9228f512019-07-04 16:23:00 +0000109
tierno72774862020-05-04 11:44:15 +0000110class VimConnConflictException(VimConnException):
tiernoa7d34d02017-02-23 14:42:07 +0100111 """There is a conflict, e.g. more item found than one"""
sousaedu80135b92021-02-17 15:05:18 +0100112
tiernoae4a8d12016-07-08 12:30:39 +0200113 def __init__(self, message, http_code=HTTP_Conflict):
tierno72774862020-05-04 11:44:15 +0000114 VimConnException.__init__(self, message, http_code)
tiernoae4a8d12016-07-08 12:30:39 +0200115
tierno9228f512019-07-04 16:23:00 +0000116
tierno72774862020-05-04 11:44:15 +0000117class VimConnNotSupportedException(VimConnException):
tiernoa7d34d02017-02-23 14:42:07 +0100118 """The request is not supported by connector"""
sousaedu80135b92021-02-17 15:05:18 +0100119
tiernoa7d34d02017-02-23 14:42:07 +0100120 def __init__(self, message, http_code=HTTP_Service_Unavailable):
tierno72774862020-05-04 11:44:15 +0000121 VimConnException.__init__(self, message, http_code)
tiernoa7d34d02017-02-23 14:42:07 +0100122
tierno9228f512019-07-04 16:23:00 +0000123
tierno72774862020-05-04 11:44:15 +0000124class VimConnNotImplemented(VimConnException):
tiernoa7d34d02017-02-23 14:42:07 +0100125 """The method is not implemented by the connected"""
sousaedu80135b92021-02-17 15:05:18 +0100126
tiernoae4a8d12016-07-08 12:30:39 +0200127 def __init__(self, message, http_code=HTTP_Not_Implemented):
tierno72774862020-05-04 11:44:15 +0000128 VimConnException.__init__(self, message, http_code)
tierno7edb6752016-03-21 17:37:52 +0100129
mirabal29356312017-07-27 12:21:22 +0200130
sousaedu80135b92021-02-17 15:05:18 +0100131class VimConnector:
tiernoa7d34d02017-02-23 14:42:07 +0100132 """Abstract base class for all the VIM connector plugins
tierno72774862020-05-04 11:44:15 +0000133 These plugins must implement a VimConnector class derived from this
tiernoa7d34d02017-02-23 14:42:07 +0100134 and all these privated methods
borsatti8a2dda32019-12-18 15:08:57 +0000135 """
sousaedu80135b92021-02-17 15:05:18 +0100136
137 def __init__(
138 self,
139 uuid,
140 name,
141 tenant_id,
142 tenant_name,
143 url,
144 url_admin=None,
145 user=None,
146 passwd=None,
147 log_level=None,
148 config={},
149 persistent_info={},
150 ):
tierno9228f512019-07-04 16:23:00 +0000151 """
152 Constructor of VIM. Raise an exception is some needed parameter is missing, but it must not do any connectivity
153 checking against the VIM
154 :param uuid: internal id of this VIM
155 :param name: name assigned to this VIM, can be used for logging
156 :param tenant_id: 'tenant_id': (only one of them is mandatory) VIM tenant to be used
157 :param tenant_name: 'tenant_name': (only one of them is mandatory) VIM tenant to be used
158 :param url: url used for normal operations
159 :param url_admin: (optional), url used for administrative tasks
160 :param user: user to access
161 :param passwd: password
162 :param log_level: provided if it should use a different log_level than the general one
163 :param config: dictionary with extra VIM information. This contains a consolidate version of VIM config
164 at VIM_ACCOUNT (attach)
165 :param persitent_info: dict where the class can store information that will be available among class
tiernoa7d34d02017-02-23 14:42:07 +0100166 destroy/creation cycles. This info is unique per VIM/credential. At first call it will contain an
167 empty dict. Useful to store login/tokens information for speed up communication
168
tiernofb1987b2016-11-15 17:35:06 +0000169 """
tierno9228f512019-07-04 16:23:00 +0000170 self.id = uuid
171 self.name = name
172 self.url = url
tierno7edb6752016-03-21 17:37:52 +0100173 self.url_admin = url_admin
tierno392f2852016-05-13 12:28:55 +0200174 self.tenant_id = tenant_id
175 self.tenant_name = tenant_name
tierno9228f512019-07-04 16:23:00 +0000176 self.user = user
177 self.passwd = passwd
178 self.config = config or {}
mirabal29356312017-07-27 12:21:22 +0200179 self.availability_zone = None
sousaedu80135b92021-02-17 15:05:18 +0100180 self.logger = logging.getLogger("ro.vim")
181
tiernofe789902016-09-29 14:20:44 +0000182 if log_level:
tierno9228f512019-07-04 16:23:00 +0000183 self.logger.setLevel(getattr(logging, log_level))
sousaedu80135b92021-02-17 15:05:18 +0100184
185 if not self.url_admin: # try to use normal url
tiernoae4a8d12016-07-08 12:30:39 +0200186 self.url_admin = self.url
borsatti8a2dda32019-12-18 15:08:57 +0000187
tierno9228f512019-07-04 16:23:00 +0000188 def __getitem__(self, index):
sousaedu80135b92021-02-17 15:05:18 +0100189 if index == "tenant_id":
tierno392f2852016-05-13 12:28:55 +0200190 return self.tenant_id
sousaedu80135b92021-02-17 15:05:18 +0100191
192 if index == "tenant_name":
tierno392f2852016-05-13 12:28:55 +0200193 return self.tenant_name
sousaedu80135b92021-02-17 15:05:18 +0100194 elif index == "id":
tierno7edb6752016-03-21 17:37:52 +0100195 return self.id
sousaedu80135b92021-02-17 15:05:18 +0100196 elif index == "name":
tierno7edb6752016-03-21 17:37:52 +0100197 return self.name
sousaedu80135b92021-02-17 15:05:18 +0100198 elif index == "user":
tierno7edb6752016-03-21 17:37:52 +0100199 return self.user
sousaedu80135b92021-02-17 15:05:18 +0100200 elif index == "passwd":
tierno7edb6752016-03-21 17:37:52 +0100201 return self.passwd
sousaedu80135b92021-02-17 15:05:18 +0100202 elif index == "url":
tierno7edb6752016-03-21 17:37:52 +0100203 return self.url
sousaedu80135b92021-02-17 15:05:18 +0100204 elif index == "url_admin":
tierno7edb6752016-03-21 17:37:52 +0100205 return self.url_admin
tierno9228f512019-07-04 16:23:00 +0000206 elif index == "config":
tierno7edb6752016-03-21 17:37:52 +0100207 return self.config
208 else:
tierno9228f512019-07-04 16:23:00 +0000209 raise KeyError("Invalid key '{}'".format(index))
borsatti8a2dda32019-12-18 15:08:57 +0000210
tierno9228f512019-07-04 16:23:00 +0000211 def __setitem__(self, index, value):
sousaedu80135b92021-02-17 15:05:18 +0100212 if index == "tenant_id":
tierno392f2852016-05-13 12:28:55 +0200213 self.tenant_id = value
sousaedu80135b92021-02-17 15:05:18 +0100214
215 if index == "tenant_name":
tierno392f2852016-05-13 12:28:55 +0200216 self.tenant_name = value
sousaedu80135b92021-02-17 15:05:18 +0100217 elif index == "id":
tierno7edb6752016-03-21 17:37:52 +0100218 self.id = value
sousaedu80135b92021-02-17 15:05:18 +0100219 elif index == "name":
tierno7edb6752016-03-21 17:37:52 +0100220 self.name = value
sousaedu80135b92021-02-17 15:05:18 +0100221 elif index == "user":
tierno7edb6752016-03-21 17:37:52 +0100222 self.user = value
sousaedu80135b92021-02-17 15:05:18 +0100223 elif index == "passwd":
tierno7edb6752016-03-21 17:37:52 +0100224 self.passwd = value
sousaedu80135b92021-02-17 15:05:18 +0100225 elif index == "url":
tierno7edb6752016-03-21 17:37:52 +0100226 self.url = value
sousaedu80135b92021-02-17 15:05:18 +0100227 elif index == "url_admin":
tierno7edb6752016-03-21 17:37:52 +0100228 self.url_admin = value
229 else:
tierno9228f512019-07-04 16:23:00 +0000230 raise KeyError("Invalid key '{}'".format(index))
tierno0a1437e2017-10-02 00:17:43 +0200231
232 @staticmethod
233 def _create_mimemultipart(content_list):
234 """Creates a MIMEmultipart text combining the content_list
235 :param content_list: list of text scripts to be combined
236 :return: str of the created MIMEmultipart. If the list is empty returns None, if the list contains only one
237 element MIMEmultipart is not created and this content is returned
238 """
239 if not content_list:
240 return None
241 elif len(content_list) == 1:
242 return content_list[0]
sousaedu80135b92021-02-17 15:05:18 +0100243
tierno0a1437e2017-10-02 00:17:43 +0200244 combined_message = MIMEMultipart()
sousaedu80135b92021-02-17 15:05:18 +0100245
tierno0a1437e2017-10-02 00:17:43 +0200246 for content in content_list:
sousaedu80135b92021-02-17 15:05:18 +0100247 if content.startswith("#include"):
248 mime_format = "text/x-include-url"
249 elif content.startswith("#include-once"):
250 mime_format = "text/x-include-once-url"
251 elif content.startswith("#!"):
252 mime_format = "text/x-shellscript"
253 elif content.startswith("#cloud-config"):
254 mime_format = "text/cloud-config"
255 elif content.startswith("#cloud-config-archive"):
256 mime_format = "text/cloud-config-archive"
257 elif content.startswith("#upstart-job"):
258 mime_format = "text/upstart-job"
259 elif content.startswith("#part-handler"):
260 mime_format = "text/part-handler"
261 elif content.startswith("#cloud-boothook"):
262 mime_format = "text/cloud-boothook"
tierno0a1437e2017-10-02 00:17:43 +0200263 else: # by default
sousaedu80135b92021-02-17 15:05:18 +0100264 mime_format = "text/x-shellscript"
265
tierno9228f512019-07-04 16:23:00 +0000266 sub_message = MIMEText(content, mime_format, sys.getdefaultencoding())
tierno0a1437e2017-10-02 00:17:43 +0200267 combined_message.attach(sub_message)
sousaedu80135b92021-02-17 15:05:18 +0100268
tierno0a1437e2017-10-02 00:17:43 +0200269 return combined_message.as_string()
270
271 def _create_user_data(self, cloud_config):
272 """
273 Creates a script user database on cloud_config info
274 :param cloud_config: dictionary with
275 'key-pairs': (optional) list of strings with the public key to be inserted to the default user
276 'users': (optional) list of users to be inserted, each item is a dict with:
277 'name': (mandatory) user name,
278 'key-pairs': (optional) list of strings with the public key to be inserted to the user
279 'user-data': (optional) can be a string with the text script to be passed directly to cloud-init,
280 or a list of strings, each one contains a script to be passed, usually with a MIMEmultipart file
281 'config-files': (optional). List of files to be transferred. Each item is a dict with:
282 'dest': (mandatory) string with the destination absolute path
283 'encoding': (optional, by default text). Can be one of:
284 'b64', 'base64', 'gz', 'gz+b64', 'gz+base64', 'gzip+b64', 'gzip+base64'
285 'content' (mandatory): string with the content of the file
286 'permissions': (optional) string with file permissions, typically octal notation '0644'
287 'owner': (optional) file owner, string with the format 'owner:group'
288 'boot-data-drive': boolean to indicate if user-data must be passed using a boot drive (hard disk)
289 :return: config_drive, userdata. The first is a boolean or None, the second a string or None
290 """
291 config_drive = None
292 userdata = None
293 userdata_list = []
sousaedu80135b92021-02-17 15:05:18 +0100294
tierno0a1437e2017-10-02 00:17:43 +0200295 if isinstance(cloud_config, dict):
296 if cloud_config.get("user-data"):
297 if isinstance(cloud_config["user-data"], str):
298 userdata_list.append(cloud_config["user-data"])
299 else:
300 for u in cloud_config["user-data"]:
301 userdata_list.append(u)
sousaedu80135b92021-02-17 15:05:18 +0100302
tierno9228f512019-07-04 16:23:00 +0000303 if cloud_config.get("boot-data-drive") is not None:
tierno0a1437e2017-10-02 00:17:43 +0200304 config_drive = cloud_config["boot-data-drive"]
sousaedu80135b92021-02-17 15:05:18 +0100305
306 if (
307 cloud_config.get("config-files")
308 or cloud_config.get("users")
309 or cloud_config.get("key-pairs")
310 ):
tierno0a1437e2017-10-02 00:17:43 +0200311 userdata_dict = {}
sousaedu80135b92021-02-17 15:05:18 +0100312
tierno0a1437e2017-10-02 00:17:43 +0200313 # default user
314 if cloud_config.get("key-pairs"):
315 userdata_dict["ssh-authorized-keys"] = cloud_config["key-pairs"]
sousaedu80135b92021-02-17 15:05:18 +0100316 userdata_dict["users"] = [
317 {
318 "default": None,
319 "ssh-authorized-keys": cloud_config["key-pairs"],
320 }
321 ]
322
tierno0a1437e2017-10-02 00:17:43 +0200323 if cloud_config.get("users"):
324 if "users" not in userdata_dict:
325 userdata_dict["users"] = ["default"]
sousaedu80135b92021-02-17 15:05:18 +0100326
tierno0a1437e2017-10-02 00:17:43 +0200327 for user in cloud_config["users"]:
328 user_info = {
329 "name": user["name"],
sousaedu80135b92021-02-17 15:05:18 +0100330 "sudo": "ALL = (ALL)NOPASSWD:ALL",
tierno0a1437e2017-10-02 00:17:43 +0200331 }
sousaedu80135b92021-02-17 15:05:18 +0100332
tierno0a1437e2017-10-02 00:17:43 +0200333 if "user-info" in user:
334 user_info["gecos"] = user["user-info"]
sousaedu80135b92021-02-17 15:05:18 +0100335
tierno0a1437e2017-10-02 00:17:43 +0200336 if user.get("key-pairs"):
337 user_info["ssh-authorized-keys"] = user["key-pairs"]
sousaedu80135b92021-02-17 15:05:18 +0100338
tierno0a1437e2017-10-02 00:17:43 +0200339 userdata_dict["users"].append(user_info)
340
341 if cloud_config.get("config-files"):
342 userdata_dict["write_files"] = []
343 for file in cloud_config["config-files"]:
sousaedu80135b92021-02-17 15:05:18 +0100344 file_info = {"path": file["dest"], "content": file["content"]}
345
tierno0a1437e2017-10-02 00:17:43 +0200346 if file.get("encoding"):
347 file_info["encoding"] = file["encoding"]
sousaedu80135b92021-02-17 15:05:18 +0100348
tierno0a1437e2017-10-02 00:17:43 +0200349 if file.get("permissions"):
350 file_info["permissions"] = file["permissions"]
sousaedu80135b92021-02-17 15:05:18 +0100351
tierno0a1437e2017-10-02 00:17:43 +0200352 if file.get("owner"):
353 file_info["owner"] = file["owner"]
sousaedu80135b92021-02-17 15:05:18 +0100354
tierno0a1437e2017-10-02 00:17:43 +0200355 userdata_dict["write_files"].append(file_info)
sousaedu80135b92021-02-17 15:05:18 +0100356
357 userdata_list.append(
358 "#cloud-config\n"
359 + yaml.safe_dump(userdata_dict, indent=4, default_flow_style=False)
360 )
tierno0a1437e2017-10-02 00:17:43 +0200361 userdata = self._create_mimemultipart(userdata_list)
362 self.logger.debug("userdata: %s", userdata)
363 elif isinstance(cloud_config, str):
364 userdata = cloud_config
sousaedu80135b92021-02-17 15:05:18 +0100365
tierno0a1437e2017-10-02 00:17:43 +0200366 return config_drive, userdata
367
tiernoa7d34d02017-02-23 14:42:07 +0100368 def check_vim_connectivity(self):
369 """Checks VIM can be reached and user credentials are ok.
tierno72774862020-05-04 11:44:15 +0000370 Returns None if success or raises VimConnConnectionException, VimConnAuthException, ...
tiernoa7d34d02017-02-23 14:42:07 +0100371 """
tierno9228f512019-07-04 16:23:00 +0000372 # by default no checking until each connector implements it
373 return None
tiernoa7d34d02017-02-23 14:42:07 +0100374
tiernoae4a8d12016-07-08 12:30:39 +0200375 def get_tenant_list(self, filter_dict={}):
tiernoa7d34d02017-02-23 14:42:07 +0100376 """Obtain tenants of VIM
tiernofb1987b2016-11-15 17:35:06 +0000377 filter_dict dictionary that can contain the following keys:
tiernoae4a8d12016-07-08 12:30:39 +0200378 name: filter by tenant name
379 id: filter by tenant uuid/id
380 <other VIM specific>
tiernofb1987b2016-11-15 17:35:06 +0000381 Returns the tenant list of dictionaries, and empty list if no tenant match all the filers:
tiernoae4a8d12016-07-08 12:30:39 +0200382 [{'name':'<name>, 'id':'<id>, ...}, ...]
tiernoa7d34d02017-02-23 14:42:07 +0100383 """
tierno72774862020-05-04 11:44:15 +0000384 raise VimConnNotImplemented("Should have implemented this")
tiernoae4a8d12016-07-08 12:30:39 +0200385
sousaedu80135b92021-02-17 15:05:18 +0100386 def new_network(
387 self,
388 net_name,
389 net_type,
390 ip_profile=None,
391 shared=False,
392 provider_network_profile=None,
393 ):
tiernofb1987b2016-11-15 17:35:06 +0000394 """Adds a tenant network to VIM
tiernoa7d34d02017-02-23 14:42:07 +0100395 Params:
396 'net_name': name of the network
397 'net_type': one of:
398 'bridge': overlay isolated network
399 'data': underlay E-LAN network for Passthrough and SRIOV interfaces
400 'ptp': underlay E-LINE network for Passthrough and SRIOV interfaces.
tierno41a69812018-02-16 14:34:33 +0100401 'ip_profile': is a dict containing the IP parameters of the network
402 'ip_version': can be "IPv4" or "IPv6" (Currently only IPv4 is implemented)
403 'subnet_address': ip_prefix_schema, that is X.X.X.X/Y
404 'gateway_address': (Optional) ip_schema, that is X.X.X.X
405 'dns_address': (Optional) comma separated list of ip_schema, e.g. X.X.X.X[,X,X,X,X]
406 'dhcp_enabled': True or False
407 'dhcp_start_address': ip_schema, first IP to grant
408 'dhcp_count': number of IPs to grant.
tiernoa7d34d02017-02-23 14:42:07 +0100409 'shared': if this network can be seen/use by other tenants/organization
kbsuba85c54d2019-10-17 16:30:32 +0000410 'provider_network_profile': (optional) contains {segmentation-id: vlan, provider-network: vim_netowrk}
garciadeblasebd66722019-01-31 16:01:31 +0000411 Returns a tuple with the network identifier and created_items, or raises an exception on error
412 created_items can be None or a dictionary where this method can include key-values that will be passed to
413 the method delete_network. Can be used to store created segments, created l2gw connections, etc.
tierno72774862020-05-04 11:44:15 +0000414 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 +0000415 as not present.
tiernofb1987b2016-11-15 17:35:06 +0000416 """
tierno72774862020-05-04 11:44:15 +0000417 raise VimConnNotImplemented("Should have implemented this")
tierno7edb6752016-03-21 17:37:52 +0100418
419 def get_network_list(self, filter_dict={}):
tiernoa7d34d02017-02-23 14:42:07 +0100420 """Obtain tenant networks of VIM
421 Params:
422 'filter_dict' (optional) contains entries to return only networks that matches ALL entries:
423 name: string => returns only networks with this name
424 id: string => returns networks with this VIM id, this imply returns one network at most
425 shared: boolean >= returns only networks that are (or are not) shared
426 tenant_id: sting => returns only networks that belong to this tenant/project
tierno72774862020-05-04 11:44:15 +0000427 ,#(not used yet) admin_state_up: boolean => returns only networks that are (or are not) in admin state
428 active
tiernoa7d34d02017-02-23 14:42:07 +0100429 #(not used yet) status: 'ACTIVE','ERROR',... => filter networks that are on this status
430 Returns the network list of dictionaries. each dictionary contains:
431 'id': (mandatory) VIM network id
432 'name': (mandatory) VIM network name
433 'status': (mandatory) can be 'ACTIVE', 'INACTIVE', 'DOWN', 'BUILD', 'ERROR', 'VIM_ERROR', 'OTHER'
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +0100434 'network_type': (optional) can be 'vxlan', 'vlan' or 'flat'
435 'segmentation_id': (optional) in case network_type is vlan or vxlan this field contains the segmentation id
tiernoa7d34d02017-02-23 14:42:07 +0100436 'error_msg': (optional) text that explains the ERROR status
437 other VIM specific fields: (optional) whenever possible using the same naming of filter_dict param
438 List can be empty if no network map the filter_dict. Raise an exception only upon VIM connectivity,
439 authorization, or some other unspecific error
440 """
tierno72774862020-05-04 11:44:15 +0000441 raise VimConnNotImplemented("Should have implemented this")
tierno7edb6752016-03-21 17:37:52 +0100442
tiernoae4a8d12016-07-08 12:30:39 +0200443 def get_network(self, net_id):
tiernoa7d34d02017-02-23 14:42:07 +0100444 """Obtain network details from the 'net_id' VIM network
445 Return a dict that contains:
446 'id': (mandatory) VIM network id, that is, net_id
447 'name': (mandatory) VIM network name
448 'status': (mandatory) can be 'ACTIVE', 'INACTIVE', 'DOWN', 'BUILD', 'ERROR', 'VIM_ERROR', 'OTHER'
449 'error_msg': (optional) text that explains the ERROR status
450 other VIM specific fields: (optional) whenever possible using the same naming of filter_dict param
451 Raises an exception upon error or when network is not found
452 """
tierno72774862020-05-04 11:44:15 +0000453 raise VimConnNotImplemented("Should have implemented this")
tierno7edb6752016-03-21 17:37:52 +0100454
garciadeblasebd66722019-01-31 16:01:31 +0000455 def delete_network(self, net_id, created_items=None):
456 """
457 Removes a tenant network from VIM and its associated elements
458 :param net_id: VIM identifier of the network, provided by method new_network
459 :param created_items: dictionary with extra items to be deleted. provided by method new_network
tiernoa7d34d02017-02-23 14:42:07 +0100460 Returns the network identifier or raises an exception upon error or when network is not found
461 """
tierno72774862020-05-04 11:44:15 +0000462 raise VimConnNotImplemented("Should have implemented this")
tierno7edb6752016-03-21 17:37:52 +0100463
tiernoae4a8d12016-07-08 12:30:39 +0200464 def refresh_nets_status(self, net_list):
tiernoa7d34d02017-02-23 14:42:07 +0100465 """Get the status of the networks
466 Params:
467 'net_list': a list with the VIM network id to be get the status
468 Returns a dictionary with:
469 'net_id': #VIM id of this network
470 status: #Mandatory. Text with one of:
471 # DELETED (not found at vim)
472 # VIM_ERROR (Cannot connect to VIM, authentication problems, VIM response error, ...)
473 # OTHER (Vim reported other status not understood)
474 # ERROR (VIM indicates an ERROR status)
475 # ACTIVE, INACTIVE, DOWN (admin down),
476 # BUILD (on building process)
477 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
478 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
479 'net_id2': ...
480 """
tierno72774862020-05-04 11:44:15 +0000481 raise VimConnNotImplemented("Should have implemented this")
tiernoae4a8d12016-07-08 12:30:39 +0200482
483 def get_flavor(self, flavor_id):
tiernoa7d34d02017-02-23 14:42:07 +0100484 """Obtain flavor details from the VIM
485 Returns the flavor dict details {'id':<>, 'name':<>, other vim specific }
486 Raises an exception upon error or if not found
487 """
tierno72774862020-05-04 11:44:15 +0000488 raise VimConnNotImplemented("Should have implemented this")
tiernocf157a82017-01-30 14:07:06 +0100489
490 def get_flavor_id_from_data(self, flavor_dict):
491 """Obtain flavor id that match the flavor description
tiernoa7d34d02017-02-23 14:42:07 +0100492 Params:
493 'flavor_dict': dictionary that contains:
494 'disk': main hard disk in GB
495 'ram': meomry in MB
496 'vcpus': number of virtual cpus
497 #TODO: complete parameters for EPA
tierno72774862020-05-04 11:44:15 +0000498 Returns the flavor_id or raises a VimConnNotFoundException
tiernocf157a82017-01-30 14:07:06 +0100499 """
tierno72774862020-05-04 11:44:15 +0000500 raise VimConnNotImplemented("Should have implemented this")
tiernocf157a82017-01-30 14:07:06 +0100501
tiernoae4a8d12016-07-08 12:30:39 +0200502 def new_flavor(self, flavor_data):
tiernoa7d34d02017-02-23 14:42:07 +0100503 """Adds a tenant flavor to VIM
tiernoae4a8d12016-07-08 12:30:39 +0200504 flavor_data contains a dictionary with information, keys:
505 name: flavor name
506 ram: memory (cloud type) in MBytes
507 vpcus: cpus (cloud type)
508 extended: EPA parameters
509 - numas: #items requested in same NUMA
510 memory: number of 1G huge pages memory
tierno72774862020-05-04 11:44:15 +0000511 paired-threads|cores|threads: number of paired hyperthreads, complete cores OR individual
512 threads
tiernoae4a8d12016-07-08 12:30:39 +0200513 interfaces: # passthrough(PT) or SRIOV interfaces attached to this numa
514 - name: interface name
515 dedicated: yes|no|yes:sriov; for PT, SRIOV or only one SRIOV for the physical NIC
516 bandwidth: X Gbps; requested guarantee bandwidth
borsatti8a2dda32019-12-18 15:08:57 +0000517 vpci: requested virtual PCI address
tiernoae4a8d12016-07-08 12:30:39 +0200518 disk: disk size
519 is_public:
tiernoae4a8d12016-07-08 12:30:39 +0200520 #TODO to concrete
sousaedu80135b92021-02-17 15:05:18 +0100521 Returns the flavor identifier
522 """
tierno72774862020-05-04 11:44:15 +0000523 raise VimConnNotImplemented("Should have implemented this")
tierno7edb6752016-03-21 17:37:52 +0100524
tiernoae4a8d12016-07-08 12:30:39 +0200525 def delete_flavor(self, flavor_id):
tiernoa7d34d02017-02-23 14:42:07 +0100526 """Deletes a tenant flavor from VIM identify by its id
sousaedu80135b92021-02-17 15:05:18 +0100527 Returns the used id or raise an exception
528 """
tierno72774862020-05-04 11:44:15 +0000529 raise VimConnNotImplemented("Should have implemented this")
tierno7edb6752016-03-21 17:37:52 +0100530
tiernoa7d34d02017-02-23 14:42:07 +0100531 def new_image(self, image_dict):
sousaedu80135b92021-02-17 15:05:18 +0100532 """Adds a tenant image to VIM
tiernoa7d34d02017-02-23 14:42:07 +0100533 Returns the image id or raises an exception if failed
534 """
tierno72774862020-05-04 11:44:15 +0000535 raise VimConnNotImplemented("Should have implemented this")
tierno7edb6752016-03-21 17:37:52 +0100536
tiernoae4a8d12016-07-08 12:30:39 +0200537 def delete_image(self, image_id):
tiernoa7d34d02017-02-23 14:42:07 +0100538 """Deletes a tenant image from VIM
sousaedu80135b92021-02-17 15:05:18 +0100539 Returns the image_id if image is deleted or raises an exception on error
540 """
tierno72774862020-05-04 11:44:15 +0000541 raise VimConnNotImplemented("Should have implemented this")
tierno7edb6752016-03-21 17:37:52 +0100542
tiernoae4a8d12016-07-08 12:30:39 +0200543 def get_image_id_from_path(self, path):
tiernocf157a82017-01-30 14:07:06 +0100544 """Get the image id from image path in the VIM database.
sousaedu80135b92021-02-17 15:05:18 +0100545 Returns the image_id or raises a VimConnNotFoundException
tiernocf157a82017-01-30 14:07:06 +0100546 """
tierno72774862020-05-04 11:44:15 +0000547 raise VimConnNotImplemented("Should have implemented this")
borsatti8a2dda32019-12-18 15:08:57 +0000548
garciadeblasb69fa9f2016-09-28 12:04:10 +0200549 def get_image_list(self, filter_dict={}):
tiernoa7d34d02017-02-23 14:42:07 +0100550 """Obtain tenant images from VIM
garciadeblasb69fa9f2016-09-28 12:04:10 +0200551 Filter_dict can be:
552 name: image name
553 id: image uuid
554 checksum: image checksum
555 location: image path
556 Returns the image list of dictionaries:
557 [{<the fields at Filter_dict plus some VIM specific>}, ...]
558 List can be empty
tiernoa7d34d02017-02-23 14:42:07 +0100559 """
tierno72774862020-05-04 11:44:15 +0000560 raise VimConnNotImplemented("Should have implemented this")
garciadeblasb69fa9f2016-09-28 12:04:10 +0200561
sousaedu80135b92021-02-17 15:05:18 +0100562 def new_vminstance(
563 self,
564 name,
565 description,
566 start,
567 image_id,
568 flavor_id,
569 net_list,
570 cloud_config=None,
571 disk_list=None,
572 availability_zone_index=None,
573 availability_zone_list=None,
574 ):
tiernoa7d34d02017-02-23 14:42:07 +0100575 """Adds a VM instance to VIM
tierno7edb6752016-03-21 17:37:52 +0100576 Params:
tiernoa7d34d02017-02-23 14:42:07 +0100577 'start': (boolean) indicates if VM must start or created in pause mode.
578 'image_id','flavor_id': image and flavor VIM id to use for the VM
579 'net_list': list of interfaces, each one is a dictionary with:
580 'name': (optional) name for the interface.
581 'net_id': VIM network id where this interface must be connect to. Mandatory for type==virtual
sousaedu80135b92021-02-17 15:05:18 +0100582 'vpci': (optional) virtual vPCI address to assign at the VM. Can be ignored depending on VIM
tierno72774862020-05-04 11:44:15 +0000583 capabilities
garciadeblasc4f4d732018-10-25 18:17:24 +0200584 'model': (optional and only have sense for type==virtual) interface model: virtio, e1000, ...
tiernoa7d34d02017-02-23 14:42:07 +0100585 'mac_address': (optional) mac address to assign to this interface
tierno41a69812018-02-16 14:34:33 +0100586 'ip_address': (optional) IP address to assign to this interface
tierno72774862020-05-04 11:44:15 +0000587 #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 +0100588 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 +0000589 for tagging VF
tiernoa7d34d02017-02-23 14:42:07 +0100590 'type': (mandatory) can be one of:
591 'virtual', in this case always connected to a network of type 'net_type=bridge'
tierno72774862020-05-04 11:44:15 +0000592 'PCI-PASSTHROUGH' or 'PF' (passthrough): depending on VIM capabilities it can be connected to a
593 data/ptp network ot it
tiernoa7d34d02017-02-23 14:42:07 +0100594 can created unconnected
tierno66eba6e2017-11-10 17:09:18 +0100595 'SR-IOV' or 'VF' (SRIOV with VLAN tag): same as PF for network connectivity.
tiernoa7d34d02017-02-23 14:42:07 +0100596 'VFnotShared'(SRIOV without VLAN tag) same as PF for network connectivity. VF where no other VFs
597 are allocated on the same physical NIC
598 'bw': (optional) only for PF/VF/VFnotShared. Minimal Bandwidth required for the interface in GBPS
tiernob3d36742017-03-03 23:51:05 +0100599 'port_security': (optional) If False it must avoid any traffic filtering at this interface. If missing
600 or True, it must apply the default VIM behaviour
tiernoa7d34d02017-02-23 14:42:07 +0100601 After execution the method will add the key:
602 'vim_id': must be filled/added by this method with the VIM identifier generated by the VIM for this
603 interface. 'net_list' is modified
604 'cloud_config': (optional) dictionary with:
605 'key-pairs': (optional) list of strings with the public key to be inserted to the default user
606 'users': (optional) list of users to be inserted, each item is a dict with:
607 'name': (mandatory) user name,
608 'key-pairs': (optional) list of strings with the public key to be inserted to the user
tierno40e1bce2017-08-09 09:12:04 +0200609 'user-data': (optional) can be a string with the text script to be passed directly to cloud-init,
610 or a list of strings, each one contains a script to be passed, usually with a MIMEmultipart file
tiernoa7d34d02017-02-23 14:42:07 +0100611 'config-files': (optional). List of files to be transferred. Each item is a dict with:
612 'dest': (mandatory) string with the destination absolute path
613 'encoding': (optional, by default text). Can be one of:
614 'b64', 'base64', 'gz', 'gz+b64', 'gz+base64', 'gzip+b64', 'gzip+base64'
615 'content' (mandatory): string with the content of the file
616 'permissions': (optional) string with file permissions, typically octal notation '0644'
617 'owner': (optional) file owner, string with the format 'owner:group'
618 'boot-data-drive': boolean to indicate if user-data must be passed using a boot drive (hard disk)
619 'disk_list': (optional) list with additional disks to the VM. Each item is a dict with:
620 'image_id': (optional). VIM id of an existing image. If not provided an empty disk must be mounted
621 'size': (mandatory) string with the size of the disk in GB
tierno5a3273c2017-08-29 11:43:46 +0200622 availability_zone_index: Index of availability_zone_list to use for this this VM. None if not AV required
623 availability_zone_list: list of availability zones given by user in the VNFD descriptor. Ignore if
624 availability_zone_index is None
tierno98e909c2017-10-14 13:27:03 +0200625 Returns a tuple with the instance identifier and created_items or raises an exception on error
626 created_items can be None or a dictionary where this method can include key-values that will be passed to
627 the method delete_vminstance and action_vminstance. Can be used to store created ports, volumes, etc.
tierno72774862020-05-04 11:44:15 +0000628 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 +0200629 as not present.
tiernoa7d34d02017-02-23 14:42:07 +0100630 """
tierno72774862020-05-04 11:44:15 +0000631 raise VimConnNotImplemented("Should have implemented this")
borsatti8a2dda32019-12-18 15:08:57 +0000632
tierno72774862020-05-04 11:44:15 +0000633 def get_vminstance(self, vm_id):
tiernoa7d34d02017-02-23 14:42:07 +0100634 """Returns the VM instance information from VIM"""
tierno72774862020-05-04 11:44:15 +0000635 raise VimConnNotImplemented("Should have implemented this")
borsatti8a2dda32019-12-18 15:08:57 +0000636
tierno98e909c2017-10-14 13:27:03 +0200637 def delete_vminstance(self, vm_id, created_items=None):
638 """
garciadeblasebd66722019-01-31 16:01:31 +0000639 Removes a VM instance from VIM and its associated elements
tierno98e909c2017-10-14 13:27:03 +0200640 :param vm_id: VIM identifier of the VM, provided by method new_vminstance
641 :param created_items: dictionary with extra items to be deleted. provided by method new_vminstance and/or method
642 action_vminstance
643 :return: None or the same vm_id. Raises an exception on fail
644 """
tierno72774862020-05-04 11:44:15 +0000645 raise VimConnNotImplemented("Should have implemented this")
tierno7edb6752016-03-21 17:37:52 +0100646
tiernoae4a8d12016-07-08 12:30:39 +0200647 def refresh_vms_status(self, vm_list):
tiernoa7d34d02017-02-23 14:42:07 +0100648 """Get the status of the virtual machines and their interfaces/ports
sousaedu80135b92021-02-17 15:05:18 +0100649 Params: the list of VM identifiers
650 Returns a dictionary with:
651 vm_id: #VIM id of this Virtual Machine
652 status: #Mandatory. Text with one of:
653 # DELETED (not found at vim)
654 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
655 # OTHER (Vim reported other status not understood)
656 # ERROR (VIM indicates an ERROR status)
657 # ACTIVE, PAUSED, SUSPENDED, INACTIVE (not running),
658 # BUILD (on building process), ERROR
659 # ACTIVE:NoMgmtIP (Active but any of its interface has an IP address
660 #
661 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
662 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
663 interfaces: list with interface info. Each item a dictionary with:
664 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
665 mac_address: #Text format XX:XX:XX:XX:XX:XX
666 vim_net_id: #network id where this interface is connected, if provided at creation
667 vim_interface_id: #interface/port VIM id
668 ip_address: #null, or text with IPv4, IPv6 address
669 compute_node: #identification of compute node where PF,VF interface is allocated
670 pci: #PCI address of the NIC that hosts the PF,VF
671 vlan: #physical VLAN used for VF
tiernoa7d34d02017-02-23 14:42:07 +0100672 """
tierno72774862020-05-04 11:44:15 +0000673 raise VimConnNotImplemented("Should have implemented this")
borsatti8a2dda32019-12-18 15:08:57 +0000674
tierno98e909c2017-10-14 13:27:03 +0200675 def action_vminstance(self, vm_id, action_dict, created_items={}):
676 """
677 Send and action over a VM instance. Returns created_items if the action was successfully sent to the VIM.
678 created_items is a dictionary with items that
679 :param vm_id: VIM identifier of the VM, provided by method new_vminstance
680 :param action_dict: dictionary with the action to perform
681 :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 +0000682 the method delete_vminstance. Can be used to store created ports, volumes, etc. Format is VimConnector
tierno98e909c2017-10-14 13:27:03 +0200683 dependent, but do not use nested dictionaries and a value of None should be the same as not present. This
684 method can modify this value
685 :return: None, or a console dict
686 """
tierno72774862020-05-04 11:44:15 +0000687 raise VimConnNotImplemented("Should have implemented this")
borsatti8a2dda32019-12-18 15:08:57 +0000688
tiernoa7d34d02017-02-23 14:42:07 +0100689 def get_vminstance_console(self, vm_id, console_type="vnc"):
690 """
tiernoae4a8d12016-07-08 12:30:39 +0200691 Get a console for the virtual machine
692 Params:
693 vm_id: uuid of the VM
694 console_type, can be:
borsatti8a2dda32019-12-18 15:08:57 +0000695 "novnc" (by default), "xvpvnc" for VNC types,
tiernoae4a8d12016-07-08 12:30:39 +0200696 "rdp-html5" for RDP types, "spice-html5" for SPICE types
697 Returns dict with the console parameters:
698 protocol: ssh, ftp, http, https, ...
borsatti8a2dda32019-12-18 15:08:57 +0000699 server: usually ip address
700 port: the http, ssh, ... port
701 suffix: extra text, e.g. the http path and query string
tiernoa7d34d02017-02-23 14:42:07 +0100702 """
tierno72774862020-05-04 11:44:15 +0000703 raise VimConnNotImplemented("Should have implemented this")
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +0000704
sousaedu80135b92021-02-17 15:05:18 +0100705 def inject_user_key(
706 self, ip_addr=None, user=None, key=None, ro_key=None, password=None
707 ):
tierno9228f512019-07-04 16:23:00 +0000708 """
709 Inject a ssh public key in a VM
710 Params:
711 ip_addr: ip address of the VM
712 user: username (default-user) to enter in the VM
713 key: public key to be injected in the VM
714 ro_key: private key of the RO, used to enter in the VM if the password is not provided
715 password: password of the user to enter in the VM
716 The function doesn't return a value:
717 """
718 if not ip_addr or not user:
sousaedu80135b92021-02-17 15:05:18 +0100719 raise VimConnNotSupportedException(
720 "All parameters should be different from 'None'"
721 )
tierno9228f512019-07-04 16:23:00 +0000722 elif not ro_key and not password:
sousaedu80135b92021-02-17 15:05:18 +0100723 raise VimConnNotSupportedException(
724 "All parameters should be different from 'None'"
725 )
tierno9228f512019-07-04 16:23:00 +0000726 else:
sousaedu80135b92021-02-17 15:05:18 +0100727 commands = {
728 "mkdir -p ~/.ssh/",
729 'echo "{}" >> ~/.ssh/authorized_keys'.format(key),
730 "chmod 644 ~/.ssh/authorized_keys",
731 "chmod 700 ~/.ssh/",
732 }
tierno9228f512019-07-04 16:23:00 +0000733 client = paramiko.SSHClient()
sousaedu80135b92021-02-17 15:05:18 +0100734
tierno9228f512019-07-04 16:23:00 +0000735 try:
736 if ro_key:
tierno7d782ef2019-10-04 12:56:31 +0000737 pkey = paramiko.RSAKey.from_private_key(StringIO(ro_key))
tierno9228f512019-07-04 16:23:00 +0000738 else:
739 pkey = None
sousaedu80135b92021-02-17 15:05:18 +0100740
tierno9228f512019-07-04 16:23:00 +0000741 client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
sousaedu80135b92021-02-17 15:05:18 +0100742 client.connect(
743 ip_addr, username=user, password=password, pkey=pkey, timeout=10
744 )
745
tierno9228f512019-07-04 16:23:00 +0000746 for command in commands:
747 (i, o, e) = client.exec_command(command, timeout=10)
748 returncode = o.channel.recv_exit_status()
tierno9228f512019-07-04 16:23:00 +0000749 outerror = e.read()
sousaedu80135b92021-02-17 15:05:18 +0100750
tierno9228f512019-07-04 16:23:00 +0000751 if returncode != 0:
752 text = "run_command='{}' Error='{}'".format(command, outerror)
sousaedu80135b92021-02-17 15:05:18 +0100753 raise VimConnUnexpectedResponse(
754 "Cannot inject ssh key in VM: '{}'".format(text)
755 )
756
tierno9228f512019-07-04 16:23:00 +0000757 return
sousaedu80135b92021-02-17 15:05:18 +0100758 except (
759 socket.error,
760 paramiko.AuthenticationException,
761 paramiko.SSHException,
762 ) as message:
tierno72774862020-05-04 11:44:15 +0000763 raise VimConnUnexpectedResponse(
sousaedu80135b92021-02-17 15:05:18 +0100764 "Cannot inject ssh key in VM: '{}' - {}".format(
765 ip_addr, str(message)
766 )
767 )
768
tierno9228f512019-07-04 16:23:00 +0000769 return
770
sousaedu80135b92021-02-17 15:05:18 +0100771 # Optional methods
tierno72774862020-05-04 11:44:15 +0000772 def new_tenant(self, tenant_name, tenant_description):
tierno9228f512019-07-04 16:23:00 +0000773 """Adds a new tenant to VIM with this name and description, this is done using admin_url if provided
774 "tenant_name": string max lenght 64
775 "tenant_description": string max length 256
776 returns the tenant identifier or raise exception
777 """
tierno72774862020-05-04 11:44:15 +0000778 raise VimConnNotImplemented("Should have implemented this")
tierno9228f512019-07-04 16:23:00 +0000779
sousaedu80135b92021-02-17 15:05:18 +0100780 def delete_tenant(self, tenant_id):
tierno9228f512019-07-04 16:23:00 +0000781 """Delete a tenant from VIM
782 tenant_id: returned VIM tenant_id on "new_tenant"
tierno72774862020-05-04 11:44:15 +0000783 Returns None on success. Raises and exception of failure. If tenant is not found raises VimConnNotFoundException
tierno9228f512019-07-04 16:23:00 +0000784 """
tierno72774862020-05-04 11:44:15 +0000785 raise VimConnNotImplemented("Should have implemented this")
tierno9228f512019-07-04 16:23:00 +0000786
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +0000787 def new_classification(self, name, ctype, definition):
788 """Creates a traffic classification in the VIM
789 Params:
790 'name': name of this classification
791 'ctype': type of this classification
792 'definition': definition of this classification (type-dependent free-form text)
793 Returns the VIM's classification ID on success or raises an exception on failure
794 """
tierno72774862020-05-04 11:44:15 +0000795 raise VimConnNotImplemented("SFC support not implemented")
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +0000796
797 def get_classification(self, classification_id):
798 """Obtain classification details of the VIM's classification with ID='classification_id'
799 Return a dict that contains:
800 'id': VIM's classification ID (same as classification_id)
801 'name': VIM's classification name
802 'type': type of this classification
803 'definition': definition of the classification
804 'status': 'ACTIVE', 'INACTIVE', 'DOWN', 'BUILD', 'ERROR', 'VIM_ERROR', 'OTHER'
805 'error_msg': (optional) text that explains the ERROR status
806 other VIM specific fields: (optional) whenever possible
807 Raises an exception upon error or when classification is not found
808 """
tierno72774862020-05-04 11:44:15 +0000809 raise VimConnNotImplemented("SFC support not implemented")
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +0000810
811 def get_classification_list(self, filter_dict={}):
812 """Obtain classifications from the VIM
813 Params:
tierno72774862020-05-04 11:44:15 +0000814 'filter_dict' (optional): contains the entries to filter the classifications on and only return those that
815 match ALL:
816 id: string => returns classifications with this VIM's classification ID, which implies a return of one
817 classification at most
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +0000818 name: string => returns only classifications with this name
819 type: string => returns classifications of this type
820 definition: string => returns classifications that have this definition
821 tenant_id: string => returns only classifications that belong to this tenant/project
822 Returns a list of classification dictionaries, each dictionary contains:
823 'id': (mandatory) VIM's classification ID
824 'name': (mandatory) VIM's classification name
825 'type': type of this classification
826 'definition': definition of the classification
827 other VIM specific fields: (optional) whenever possible using the same naming of filter_dict param
828 List can be empty if no classification matches the filter_dict. Raise an exception only upon VIM connectivity,
829 authorization, or some other unspecific error
830 """
tierno72774862020-05-04 11:44:15 +0000831 raise VimConnNotImplemented("SFC support not implemented")
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +0000832
borsatti8a2dda32019-12-18 15:08:57 +0000833 def refresh_classifications_status(self, classification_list):
sousaedu80135b92021-02-17 15:05:18 +0100834 """Get the status of the classifications
835 Params: the list of classification identifiers
836 Returns a dictionary with:
837 vm_id: #VIM id of this classifier
838 status: #Mandatory. Text with one of:
839 # DELETED (not found at vim)
840 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
841 # OTHER (Vim reported other status not understood)
842 # ERROR (VIM indicates an ERROR status)
843 # ACTIVE,
844 # CREATING (on building process)
845 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
846 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
847 """
tierno72774862020-05-04 11:44:15 +0000848 raise VimConnNotImplemented("Should have implemented this")
borsatti8a2dda32019-12-18 15:08:57 +0000849
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +0000850 def delete_classification(self, classification_id):
851 """Deletes a classification from the VIM
tierno72774862020-05-04 11:44:15 +0000852 Returns the classification ID (classification_id) or raises an exception upon error or when classification is
853 not found
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +0000854 """
tierno72774862020-05-04 11:44:15 +0000855 raise VimConnNotImplemented("SFC support not implemented")
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +0000856
857 def new_sfi(self, name, ingress_ports, egress_ports, sfc_encap=True):
858 """Creates a service function instance in the VIM
859 Params:
860 'name': name of this service function instance
861 'ingress_ports': set of ingress ports (VIM's port IDs)
862 'egress_ports': set of egress ports (VIM's port IDs)
863 'sfc_encap': boolean stating whether this specific instance supports IETF SFC Encapsulation
864 Returns the VIM's service function instance ID on success or raises an exception on failure
865 """
tierno72774862020-05-04 11:44:15 +0000866 raise VimConnNotImplemented("SFC support not implemented")
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +0000867
868 def get_sfi(self, sfi_id):
869 """Obtain service function instance details of the VIM's service function instance with ID='sfi_id'
870 Return a dict that contains:
871 'id': VIM's sfi ID (same as sfi_id)
872 'name': VIM's sfi name
873 'ingress_ports': set of ingress ports (VIM's port IDs)
874 'egress_ports': set of egress ports (VIM's port IDs)
875 'status': 'ACTIVE', 'INACTIVE', 'DOWN', 'BUILD', 'ERROR', 'VIM_ERROR', 'OTHER'
876 'error_msg': (optional) text that explains the ERROR status
877 other VIM specific fields: (optional) whenever possible
878 Raises an exception upon error or when service function instance is not found
879 """
tierno72774862020-05-04 11:44:15 +0000880 raise VimConnNotImplemented("SFC support not implemented")
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +0000881
882 def get_sfi_list(self, filter_dict={}):
883 """Obtain service function instances from the VIM
884 Params:
885 'filter_dict' (optional): contains the entries to filter the sfis on and only return those that match ALL:
886 id: string => returns sfis with this VIM's sfi ID, which implies a return of one sfi at most
887 name: string => returns only service function instances with this name
888 tenant_id: string => returns only service function instances that belong to this tenant/project
889 Returns a list of service function instance dictionaries, each dictionary contains:
890 'id': (mandatory) VIM's sfi ID
891 'name': (mandatory) VIM's sfi name
892 'ingress_ports': set of ingress ports (VIM's port IDs)
893 'egress_ports': set of egress ports (VIM's port IDs)
894 other VIM specific fields: (optional) whenever possible using the same naming of filter_dict param
895 List can be empty if no sfi matches the filter_dict. Raise an exception only upon VIM connectivity,
896 authorization, or some other unspecific error
897 """
tierno72774862020-05-04 11:44:15 +0000898 raise VimConnNotImplemented("SFC support not implemented")
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +0000899
900 def delete_sfi(self, sfi_id):
901 """Deletes a service function instance from the VIM
902 Returns the service function instance ID (sfi_id) or raises an exception upon error or when sfi is not found
903 """
tierno72774862020-05-04 11:44:15 +0000904 raise VimConnNotImplemented("SFC support not implemented")
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +0000905
borsatti8a2dda32019-12-18 15:08:57 +0000906 def refresh_sfis_status(self, sfi_list):
sousaedu80135b92021-02-17 15:05:18 +0100907 """Get the status of the service function instances
908 Params: the list of sfi identifiers
909 Returns a dictionary with:
910 vm_id: #VIM id of this service function instance
911 status: #Mandatory. Text with one of:
912 # DELETED (not found at vim)
913 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
914 # OTHER (Vim reported other status not understood)
915 # ERROR (VIM indicates an ERROR status)
916 # ACTIVE,
917 # CREATING (on building process)
918 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
919 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
920 """
tierno72774862020-05-04 11:44:15 +0000921 raise VimConnNotImplemented("Should have implemented this")
borsatti8a2dda32019-12-18 15:08:57 +0000922
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +0000923 def new_sf(self, name, sfis, sfc_encap=True):
924 """Creates (an abstract) service function in the VIM
925 Params:
926 'name': name of this service function
927 'sfis': set of service function instances of this (abstract) service function
928 'sfc_encap': boolean stating whether this service function supports IETF SFC Encapsulation
929 Returns the VIM's service function ID on success or raises an exception on failure
930 """
tierno72774862020-05-04 11:44:15 +0000931 raise VimConnNotImplemented("SFC support not implemented")
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +0000932
933 def get_sf(self, sf_id):
934 """Obtain service function details of the VIM's service function with ID='sf_id'
935 Return a dict that contains:
936 'id': VIM's sf ID (same as sf_id)
937 'name': VIM's sf name
938 'sfis': VIM's sf's set of VIM's service function instance IDs
939 'sfc_encap': boolean stating whether this service function supports IETF SFC Encapsulation
940 'status': 'ACTIVE', 'INACTIVE', 'DOWN', 'BUILD', 'ERROR', 'VIM_ERROR', 'OTHER'
941 'error_msg': (optional) text that explains the ERROR status
942 other VIM specific fields: (optional) whenever possible
943 Raises an exception upon error or when sf is not found
944 """
945
946 def get_sf_list(self, filter_dict={}):
947 """Obtain service functions from the VIM
948 Params:
949 'filter_dict' (optional): contains the entries to filter the sfs on and only return those that match ALL:
950 id: string => returns sfs with this VIM's sf ID, which implies a return of one sf at most
951 name: string => returns only service functions with this name
952 tenant_id: string => returns only service functions that belong to this tenant/project
953 Returns a list of service function dictionaries, each dictionary contains:
954 'id': (mandatory) VIM's sf ID
955 'name': (mandatory) VIM's sf name
956 'sfis': VIM's sf's set of VIM's service function instance IDs
957 'sfc_encap': boolean stating whether this service function supports IETF SFC Encapsulation
958 other VIM specific fields: (optional) whenever possible using the same naming of filter_dict param
959 List can be empty if no sf matches the filter_dict. Raise an exception only upon VIM connectivity,
960 authorization, or some other unspecific error
961 """
tierno72774862020-05-04 11:44:15 +0000962 raise VimConnNotImplemented("SFC support not implemented")
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +0000963
964 def delete_sf(self, sf_id):
965 """Deletes (an abstract) service function from the VIM
966 Returns the service function ID (sf_id) or raises an exception upon error or when sf is not found
967 """
tierno72774862020-05-04 11:44:15 +0000968 raise VimConnNotImplemented("SFC support not implemented")
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +0000969
borsatti8a2dda32019-12-18 15:08:57 +0000970 def refresh_sfs_status(self, sf_list):
sousaedu80135b92021-02-17 15:05:18 +0100971 """Get the status of the service functions
972 Params: the list of sf identifiers
973 Returns a dictionary with:
974 vm_id: #VIM id of this service function
975 status: #Mandatory. Text with one of:
976 # DELETED (not found at vim)
977 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
978 # OTHER (Vim reported other status not understood)
979 # ERROR (VIM indicates an ERROR status)
980 # ACTIVE,
981 # CREATING (on building process)
982 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
983 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
984 """
tierno72774862020-05-04 11:44:15 +0000985 raise VimConnNotImplemented("Should have implemented this")
borsatti8a2dda32019-12-18 15:08:57 +0000986
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +0000987 def new_sfp(self, name, classifications, sfs, sfc_encap=True, spi=None):
988 """Creates a service function path
989 Params:
990 'name': name of this service function path
991 'classifications': set of traffic classifications that should be matched on to get into this sfp
992 'sfs': list of every service function that constitutes this path , from first to last
993 'sfc_encap': whether this is an SFC-Encapsulated chain (i.e using NSH), True by default
994 'spi': (optional) the Service Function Path identifier (SPI: Service Path Identifier) for this path
995 Returns the VIM's sfp ID on success or raises an exception on failure
996 """
tierno72774862020-05-04 11:44:15 +0000997 raise VimConnNotImplemented("SFC support not implemented")
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +0000998
999 def get_sfp(self, sfp_id):
1000 """Obtain service function path details of the VIM's sfp with ID='sfp_id'
1001 Return a dict that contains:
1002 'id': VIM's sfp ID (same as sfp_id)
1003 'name': VIM's sfp name
1004 'classifications': VIM's sfp's list of VIM's classification IDs
1005 'sfs': VIM's sfp's list of VIM's service function IDs
1006 'status': 'ACTIVE', 'INACTIVE', 'DOWN', 'BUILD', 'ERROR', 'VIM_ERROR', 'OTHER'
1007 'error_msg': (optional) text that explains the ERROR status
1008 other VIM specific fields: (optional) whenever possible
1009 Raises an exception upon error or when sfp is not found
1010 """
tierno72774862020-05-04 11:44:15 +00001011 raise VimConnNotImplemented("SFC support not implemented")
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +00001012
1013 def get_sfp_list(self, filter_dict={}):
1014 """Obtain service function paths from VIM
1015 Params:
1016 'filter_dict' (optional): contains the entries to filter the sfps on, and only return those that match ALL:
1017 id: string => returns sfps with this VIM's sfp ID , which implies a return of one sfp at most
1018 name: string => returns only sfps with this name
1019 tenant_id: string => returns only sfps that belong to this tenant/project
1020 Returns a list of service function path dictionaries, each dictionary contains:
1021 'id': (mandatory) VIM's sfp ID
1022 'name': (mandatory) VIM's sfp name
1023 'classifications': VIM's sfp's list of VIM's classification IDs
1024 'sfs': VIM's sfp's list of VIM's service function IDs
1025 other VIM specific fields: (optional) whenever possible using the same naming of filter_dict param
1026 List can be empty if no sfp matches the filter_dict. Raise an exception only upon VIM connectivity,
1027 authorization, or some other unspecific error
1028 """
tierno72774862020-05-04 11:44:15 +00001029 raise VimConnNotImplemented("SFC support not implemented")
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +00001030
borsatti8a2dda32019-12-18 15:08:57 +00001031 def refresh_sfps_status(self, sfp_list):
sousaedu80135b92021-02-17 15:05:18 +01001032 """Get the status of the service function path
1033 Params: the list of sfp identifiers
1034 Returns a dictionary with:
1035 vm_id: #VIM id of this service function path
1036 status: #Mandatory. Text with one of:
1037 # DELETED (not found at vim)
1038 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
1039 # OTHER (Vim reported other status not understood)
1040 # ERROR (VIM indicates an ERROR status)
1041 # ACTIVE,
1042 # CREATING (on building process)
1043 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
1044 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)F
1045 """
tierno72774862020-05-04 11:44:15 +00001046 raise VimConnNotImplemented("Should have implemented this")
borsatti8a2dda32019-12-18 15:08:57 +00001047
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +00001048 def delete_sfp(self, sfp_id):
1049 """Deletes a service function path from the VIM
1050 Returns the sfp ID (sfp_id) or raises an exception upon error or when sf is not found
1051 """
tierno72774862020-05-04 11:44:15 +00001052 raise VimConnNotImplemented("SFC support not implemented")
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +00001053
sousaedu80135b92021-02-17 15:05:18 +01001054 # NOT USED METHODS in current version. Deprecated
tierno9228f512019-07-04 16:23:00 +00001055 @deprecated
tierno7edb6752016-03-21 17:37:52 +01001056 def host_vim2gui(self, host, server_dict):
tiernoa7d34d02017-02-23 14:42:07 +01001057 """Transform host dictionary from VIM format to GUI format,
tierno7edb6752016-03-21 17:37:52 +01001058 and append to the server_dict
tiernoa7d34d02017-02-23 14:42:07 +01001059 """
tierno72774862020-05-04 11:44:15 +00001060 raise VimConnNotImplemented("Should have implemented this")
tierno7edb6752016-03-21 17:37:52 +01001061
tierno9228f512019-07-04 16:23:00 +00001062 @deprecated
tierno7edb6752016-03-21 17:37:52 +01001063 def get_hosts_info(self):
tiernoa7d34d02017-02-23 14:42:07 +01001064 """Get the information of deployed hosts
1065 Returns the hosts content"""
tierno72774862020-05-04 11:44:15 +00001066 raise VimConnNotImplemented("Should have implemented this")
tierno7edb6752016-03-21 17:37:52 +01001067
tierno9228f512019-07-04 16:23:00 +00001068 @deprecated
tierno7edb6752016-03-21 17:37:52 +01001069 def get_hosts(self, vim_tenant):
tiernoa7d34d02017-02-23 14:42:07 +01001070 """Get the hosts and deployed instances
1071 Returns the hosts content"""
tierno72774862020-05-04 11:44:15 +00001072 raise VimConnNotImplemented("Should have implemented this")
tierno7edb6752016-03-21 17:37:52 +01001073
tierno9228f512019-07-04 16:23:00 +00001074 @deprecated
tierno7edb6752016-03-21 17:37:52 +01001075 def get_processor_rankings(self):
tiernoa7d34d02017-02-23 14:42:07 +01001076 """Get the processor rankings in the VIM database"""
tierno72774862020-05-04 11:44:15 +00001077 raise VimConnNotImplemented("Should have implemented this")
borsatti8a2dda32019-12-18 15:08:57 +00001078
tierno9228f512019-07-04 16:23:00 +00001079 @deprecated
tiernoae4a8d12016-07-08 12:30:39 +02001080 def new_host(self, host_data):
tiernoa7d34d02017-02-23 14:42:07 +01001081 """Adds a new host to VIM"""
1082 """Returns status code of the VIM response"""
tierno72774862020-05-04 11:44:15 +00001083 raise VimConnNotImplemented("Should have implemented this")
borsatti8a2dda32019-12-18 15:08:57 +00001084
tierno9228f512019-07-04 16:23:00 +00001085 @deprecated
tiernoae4a8d12016-07-08 12:30:39 +02001086 def new_external_port(self, port_data):
tiernoa7d34d02017-02-23 14:42:07 +01001087 """Adds a external port to VIM"""
1088 """Returns the port identifier"""
tierno72774862020-05-04 11:44:15 +00001089 raise VimConnNotImplemented("Should have implemented this")
borsatti8a2dda32019-12-18 15:08:57 +00001090
tierno9228f512019-07-04 16:23:00 +00001091 @deprecated
tierno72774862020-05-04 11:44:15 +00001092 def new_external_network(self, net_name, net_type):
tiernoa7d34d02017-02-23 14:42:07 +01001093 """Adds a external network to VIM (shared)"""
1094 """Returns the network identifier"""
tierno72774862020-05-04 11:44:15 +00001095 raise VimConnNotImplemented("Should have implemented this")
tiernoae4a8d12016-07-08 12:30:39 +02001096
tierno9228f512019-07-04 16:23:00 +00001097 @deprecated
tiernoae4a8d12016-07-08 12:30:39 +02001098 def connect_port_network(self, port_id, network_id, admin=False):
tiernoa7d34d02017-02-23 14:42:07 +01001099 """Connects a external port to a network"""
1100 """Returns status code of the VIM response"""
tierno72774862020-05-04 11:44:15 +00001101 raise VimConnNotImplemented("Should have implemented this")
tiernoae4a8d12016-07-08 12:30:39 +02001102
tierno9228f512019-07-04 16:23:00 +00001103 @deprecated
tiernoae4a8d12016-07-08 12:30:39 +02001104 def new_vminstancefromJSON(self, vm_data):
tiernoa7d34d02017-02-23 14:42:07 +01001105 """Adds a VM instance to VIM"""
1106 """Returns the instance identifier"""
tierno72774862020-05-04 11:44:15 +00001107 raise VimConnNotImplemented("Should have implemented this")