blob: cc087f0a38b19453d9e76e6fc0c8ba1603e34259 [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
tiernoae4a8d12016-07-08 12:30:39 +020029import logging
gcalvinoe580c7d2017-09-22 14:09:51 +020030import paramiko
31import socket
tierno7d782ef2019-10-04 12:56:31 +000032from io import StringIO
tierno0a1437e2017-10-02 00:17:43 +020033import yaml
gcalvino51757e92017-10-03 11:31:31 +020034import sys
tierno0a1437e2017-10-02 00:17:43 +020035from email.mime.multipart import MIMEMultipart
36from email.mime.text import MIMEText
tierno72774862020-05-04 11:44:15 +000037from http import HTTPStatus
38import warnings
tierno9228f512019-07-04 16:23:00 +000039
40__author__ = "Alfonso Tierno, Igor D.C."
tierno72774862020-05-04 11:44:15 +000041__date__ = "$14-aug-2017 23:59:59$"
tierno7edb6752016-03-21 17:37:52 +010042
tierno9228f512019-07-04 16:23:00 +000043
tierno72774862020-05-04 11:44:15 +000044def deprecated(message):
45 def deprecated_decorator(func):
46 def deprecated_func(*args, **kwargs):
sousaedu2ad85172021-02-17 15:05:18 +010047 warnings.warn(
48 "{} is a deprecated function. {}".format(func.__name__, message),
49 category=DeprecationWarning,
50 stacklevel=2,
51 )
52 warnings.simplefilter("default", DeprecationWarning)
53
tierno72774862020-05-04 11:44:15 +000054 return func(*args, **kwargs)
sousaedu2ad85172021-02-17 15:05:18 +010055
tierno72774862020-05-04 11:44:15 +000056 return deprecated_func
sousaedu2ad85172021-02-17 15:05:18 +010057
tierno72774862020-05-04 11:44:15 +000058 return deprecated_decorator
59
60
61# Error variables
62HTTP_Bad_Request = HTTPStatus.BAD_REQUEST.value
63HTTP_Unauthorized = HTTPStatus.UNAUTHORIZED.value
64HTTP_Not_Found = HTTPStatus.NOT_FOUND.value
65HTTP_Method_Not_Allowed = HTTPStatus.METHOD_NOT_ALLOWED.value
66HTTP_Request_Timeout = HTTPStatus.REQUEST_TIMEOUT.value
67HTTP_Conflict = HTTPStatus.CONFLICT.value
68HTTP_Not_Implemented = HTTPStatus.NOT_IMPLEMENTED.value
69HTTP_Service_Unavailable = HTTPStatus.SERVICE_UNAVAILABLE.value
70HTTP_Internal_Server_Error = HTTPStatus.INTERNAL_SERVER_ERROR.value
71
72
73class VimConnException(Exception):
74 """Common and base class Exception for all VimConnector exceptions"""
sousaedu2ad85172021-02-17 15:05:18 +010075
tiernoae4a8d12016-07-08 12:30:39 +020076 def __init__(self, message, http_code=HTTP_Bad_Request):
77 Exception.__init__(self, message)
78 self.http_code = http_code
79
tierno9228f512019-07-04 16:23:00 +000080
tierno72774862020-05-04 11:44:15 +000081class VimConnConnectionException(VimConnException):
tiernoa7d34d02017-02-23 14:42:07 +010082 """Connectivity error with the VIM"""
sousaedu2ad85172021-02-17 15:05:18 +010083
tiernoae4a8d12016-07-08 12:30:39 +020084 def __init__(self, message, http_code=HTTP_Service_Unavailable):
tierno72774862020-05-04 11:44:15 +000085 VimConnException.__init__(self, message, http_code)
tierno9228f512019-07-04 16:23:00 +000086
87
tierno72774862020-05-04 11:44:15 +000088class VimConnUnexpectedResponse(VimConnException):
tiernoa7d34d02017-02-23 14:42:07 +010089 """Get an wrong response from VIM"""
sousaedu2ad85172021-02-17 15:05:18 +010090
tiernoae4a8d12016-07-08 12:30:39 +020091 def __init__(self, message, http_code=HTTP_Service_Unavailable):
tierno72774862020-05-04 11:44:15 +000092 VimConnException.__init__(self, message, http_code)
tiernoae4a8d12016-07-08 12:30:39 +020093
tierno9228f512019-07-04 16:23:00 +000094
tierno72774862020-05-04 11:44:15 +000095class VimConnAuthException(VimConnException):
tiernoa7d34d02017-02-23 14:42:07 +010096 """Invalid credentials or authorization to perform this action over the VIM"""
sousaedu2ad85172021-02-17 15:05:18 +010097
tiernoae4a8d12016-07-08 12:30:39 +020098 def __init__(self, message, http_code=HTTP_Unauthorized):
tierno72774862020-05-04 11:44:15 +000099 VimConnException.__init__(self, message, http_code)
tiernoae4a8d12016-07-08 12:30:39 +0200100
tierno9228f512019-07-04 16:23:00 +0000101
tierno72774862020-05-04 11:44:15 +0000102class VimConnNotFoundException(VimConnException):
tiernoa7d34d02017-02-23 14:42:07 +0100103 """The item is not found at VIM"""
sousaedu2ad85172021-02-17 15:05:18 +0100104
tiernoae4a8d12016-07-08 12:30:39 +0200105 def __init__(self, message, http_code=HTTP_Not_Found):
tierno72774862020-05-04 11:44:15 +0000106 VimConnException.__init__(self, message, http_code)
tiernoae4a8d12016-07-08 12:30:39 +0200107
tierno9228f512019-07-04 16:23:00 +0000108
tierno72774862020-05-04 11:44:15 +0000109class VimConnConflictException(VimConnException):
tiernoa7d34d02017-02-23 14:42:07 +0100110 """There is a conflict, e.g. more item found than one"""
sousaedu2ad85172021-02-17 15:05:18 +0100111
tiernoae4a8d12016-07-08 12:30:39 +0200112 def __init__(self, message, http_code=HTTP_Conflict):
tierno72774862020-05-04 11:44:15 +0000113 VimConnException.__init__(self, message, http_code)
tiernoae4a8d12016-07-08 12:30:39 +0200114
tierno9228f512019-07-04 16:23:00 +0000115
tierno72774862020-05-04 11:44:15 +0000116class VimConnNotSupportedException(VimConnException):
tiernoa7d34d02017-02-23 14:42:07 +0100117 """The request is not supported by connector"""
sousaedu2ad85172021-02-17 15:05:18 +0100118
tiernoa7d34d02017-02-23 14:42:07 +0100119 def __init__(self, message, http_code=HTTP_Service_Unavailable):
tierno72774862020-05-04 11:44:15 +0000120 VimConnException.__init__(self, message, http_code)
tiernoa7d34d02017-02-23 14:42:07 +0100121
tierno9228f512019-07-04 16:23:00 +0000122
tierno72774862020-05-04 11:44:15 +0000123class VimConnNotImplemented(VimConnException):
tiernoa7d34d02017-02-23 14:42:07 +0100124 """The method is not implemented by the connected"""
sousaedu2ad85172021-02-17 15:05:18 +0100125
tiernoae4a8d12016-07-08 12:30:39 +0200126 def __init__(self, message, http_code=HTTP_Not_Implemented):
tierno72774862020-05-04 11:44:15 +0000127 VimConnException.__init__(self, message, http_code)
tierno7edb6752016-03-21 17:37:52 +0100128
mirabal29356312017-07-27 12:21:22 +0200129
sousaedu2ad85172021-02-17 15:05:18 +0100130class VimConnector:
tiernoa7d34d02017-02-23 14:42:07 +0100131 """Abstract base class for all the VIM connector plugins
tierno72774862020-05-04 11:44:15 +0000132 These plugins must implement a VimConnector class derived from this
tiernoa7d34d02017-02-23 14:42:07 +0100133 and all these privated methods
borsatti8a2dda32019-12-18 15:08:57 +0000134 """
sousaedu2ad85172021-02-17 15:05:18 +0100135
136 def __init__(
137 self,
138 uuid,
139 name,
140 tenant_id,
141 tenant_name,
142 url,
143 url_admin=None,
144 user=None,
145 passwd=None,
146 log_level=None,
147 config={},
148 persistent_info={},
149 ):
tierno9228f512019-07-04 16:23:00 +0000150 """
151 Constructor of VIM. Raise an exception is some needed parameter is missing, but it must not do any connectivity
152 checking against the VIM
153 :param uuid: internal id of this VIM
154 :param name: name assigned to this VIM, can be used for logging
155 :param tenant_id: 'tenant_id': (only one of them is mandatory) VIM tenant to be used
156 :param tenant_name: 'tenant_name': (only one of them is mandatory) VIM tenant to be used
157 :param url: url used for normal operations
158 :param url_admin: (optional), url used for administrative tasks
159 :param user: user to access
160 :param passwd: password
161 :param log_level: provided if it should use a different log_level than the general one
162 :param config: dictionary with extra VIM information. This contains a consolidate version of VIM config
163 at VIM_ACCOUNT (attach)
164 :param persitent_info: dict where the class can store information that will be available among class
tiernoa7d34d02017-02-23 14:42:07 +0100165 destroy/creation cycles. This info is unique per VIM/credential. At first call it will contain an
166 empty dict. Useful to store login/tokens information for speed up communication
167
tiernofb1987b2016-11-15 17:35:06 +0000168 """
tierno9228f512019-07-04 16:23:00 +0000169 self.id = uuid
170 self.name = name
171 self.url = url
tierno7edb6752016-03-21 17:37:52 +0100172 self.url_admin = url_admin
tierno392f2852016-05-13 12:28:55 +0200173 self.tenant_id = tenant_id
174 self.tenant_name = tenant_name
tierno9228f512019-07-04 16:23:00 +0000175 self.user = user
176 self.passwd = passwd
177 self.config = config or {}
mirabal29356312017-07-27 12:21:22 +0200178 self.availability_zone = None
sousaedu2ad85172021-02-17 15:05:18 +0100179 self.logger = logging.getLogger("ro.vim")
180
tiernofe789902016-09-29 14:20:44 +0000181 if log_level:
tierno9228f512019-07-04 16:23:00 +0000182 self.logger.setLevel(getattr(logging, log_level))
sousaedu2ad85172021-02-17 15:05:18 +0100183
184 if not self.url_admin: # try to use normal url
tiernoae4a8d12016-07-08 12:30:39 +0200185 self.url_admin = self.url
borsatti8a2dda32019-12-18 15:08:57 +0000186
tierno9228f512019-07-04 16:23:00 +0000187 def __getitem__(self, index):
sousaedu2ad85172021-02-17 15:05:18 +0100188 if index == "tenant_id":
tierno392f2852016-05-13 12:28:55 +0200189 return self.tenant_id
sousaedu2ad85172021-02-17 15:05:18 +0100190
191 if index == "tenant_name":
tierno392f2852016-05-13 12:28:55 +0200192 return self.tenant_name
sousaedu2ad85172021-02-17 15:05:18 +0100193 elif index == "id":
tierno7edb6752016-03-21 17:37:52 +0100194 return self.id
sousaedu2ad85172021-02-17 15:05:18 +0100195 elif index == "name":
tierno7edb6752016-03-21 17:37:52 +0100196 return self.name
sousaedu2ad85172021-02-17 15:05:18 +0100197 elif index == "user":
tierno7edb6752016-03-21 17:37:52 +0100198 return self.user
sousaedu2ad85172021-02-17 15:05:18 +0100199 elif index == "passwd":
tierno7edb6752016-03-21 17:37:52 +0100200 return self.passwd
sousaedu2ad85172021-02-17 15:05:18 +0100201 elif index == "url":
tierno7edb6752016-03-21 17:37:52 +0100202 return self.url
sousaedu2ad85172021-02-17 15:05:18 +0100203 elif index == "url_admin":
tierno7edb6752016-03-21 17:37:52 +0100204 return self.url_admin
tierno9228f512019-07-04 16:23:00 +0000205 elif index == "config":
tierno7edb6752016-03-21 17:37:52 +0100206 return self.config
207 else:
tierno9228f512019-07-04 16:23:00 +0000208 raise KeyError("Invalid key '{}'".format(index))
borsatti8a2dda32019-12-18 15:08:57 +0000209
tierno9228f512019-07-04 16:23:00 +0000210 def __setitem__(self, index, value):
sousaedu2ad85172021-02-17 15:05:18 +0100211 if index == "tenant_id":
tierno392f2852016-05-13 12:28:55 +0200212 self.tenant_id = value
sousaedu2ad85172021-02-17 15:05:18 +0100213
214 if index == "tenant_name":
tierno392f2852016-05-13 12:28:55 +0200215 self.tenant_name = value
sousaedu2ad85172021-02-17 15:05:18 +0100216 elif index == "id":
tierno7edb6752016-03-21 17:37:52 +0100217 self.id = value
sousaedu2ad85172021-02-17 15:05:18 +0100218 elif index == "name":
tierno7edb6752016-03-21 17:37:52 +0100219 self.name = value
sousaedu2ad85172021-02-17 15:05:18 +0100220 elif index == "user":
tierno7edb6752016-03-21 17:37:52 +0100221 self.user = value
sousaedu2ad85172021-02-17 15:05:18 +0100222 elif index == "passwd":
tierno7edb6752016-03-21 17:37:52 +0100223 self.passwd = value
sousaedu2ad85172021-02-17 15:05:18 +0100224 elif index == "url":
tierno7edb6752016-03-21 17:37:52 +0100225 self.url = value
sousaedu2ad85172021-02-17 15:05:18 +0100226 elif index == "url_admin":
tierno7edb6752016-03-21 17:37:52 +0100227 self.url_admin = value
228 else:
tierno9228f512019-07-04 16:23:00 +0000229 raise KeyError("Invalid key '{}'".format(index))
tierno0a1437e2017-10-02 00:17:43 +0200230
231 @staticmethod
232 def _create_mimemultipart(content_list):
233 """Creates a MIMEmultipart text combining the content_list
234 :param content_list: list of text scripts to be combined
235 :return: str of the created MIMEmultipart. If the list is empty returns None, if the list contains only one
236 element MIMEmultipart is not created and this content is returned
237 """
238 if not content_list:
239 return None
240 elif len(content_list) == 1:
241 return content_list[0]
sousaedu2ad85172021-02-17 15:05:18 +0100242
tierno0a1437e2017-10-02 00:17:43 +0200243 combined_message = MIMEMultipart()
sousaedu2ad85172021-02-17 15:05:18 +0100244
tierno0a1437e2017-10-02 00:17:43 +0200245 for content in content_list:
sousaedu2ad85172021-02-17 15:05:18 +0100246 if content.startswith("#include"):
247 mime_format = "text/x-include-url"
248 elif content.startswith("#include-once"):
249 mime_format = "text/x-include-once-url"
250 elif content.startswith("#!"):
251 mime_format = "text/x-shellscript"
252 elif content.startswith("#cloud-config"):
253 mime_format = "text/cloud-config"
254 elif content.startswith("#cloud-config-archive"):
255 mime_format = "text/cloud-config-archive"
256 elif content.startswith("#upstart-job"):
257 mime_format = "text/upstart-job"
258 elif content.startswith("#part-handler"):
259 mime_format = "text/part-handler"
260 elif content.startswith("#cloud-boothook"):
261 mime_format = "text/cloud-boothook"
tierno0a1437e2017-10-02 00:17:43 +0200262 else: # by default
sousaedu2ad85172021-02-17 15:05:18 +0100263 mime_format = "text/x-shellscript"
264
tierno9228f512019-07-04 16:23:00 +0000265 sub_message = MIMEText(content, mime_format, sys.getdefaultencoding())
tierno0a1437e2017-10-02 00:17:43 +0200266 combined_message.attach(sub_message)
sousaedu2ad85172021-02-17 15:05:18 +0100267
tierno0a1437e2017-10-02 00:17:43 +0200268 return combined_message.as_string()
269
270 def _create_user_data(self, cloud_config):
271 """
272 Creates a script user database on cloud_config info
273 :param cloud_config: dictionary with
274 'key-pairs': (optional) list of strings with the public key to be inserted to the default user
275 'users': (optional) list of users to be inserted, each item is a dict with:
276 'name': (mandatory) user name,
277 'key-pairs': (optional) list of strings with the public key to be inserted to the user
278 'user-data': (optional) can be a string with the text script to be passed directly to cloud-init,
279 or a list of strings, each one contains a script to be passed, usually with a MIMEmultipart file
280 'config-files': (optional). List of files to be transferred. Each item is a dict with:
281 'dest': (mandatory) string with the destination absolute path
282 'encoding': (optional, by default text). Can be one of:
283 'b64', 'base64', 'gz', 'gz+b64', 'gz+base64', 'gzip+b64', 'gzip+base64'
284 'content' (mandatory): string with the content of the file
285 'permissions': (optional) string with file permissions, typically octal notation '0644'
286 'owner': (optional) file owner, string with the format 'owner:group'
287 'boot-data-drive': boolean to indicate if user-data must be passed using a boot drive (hard disk)
288 :return: config_drive, userdata. The first is a boolean or None, the second a string or None
289 """
290 config_drive = None
291 userdata = None
292 userdata_list = []
sousaedu2ad85172021-02-17 15:05:18 +0100293
tierno0a1437e2017-10-02 00:17:43 +0200294 if isinstance(cloud_config, dict):
295 if cloud_config.get("user-data"):
296 if isinstance(cloud_config["user-data"], str):
297 userdata_list.append(cloud_config["user-data"])
298 else:
299 for u in cloud_config["user-data"]:
300 userdata_list.append(u)
sousaedu2ad85172021-02-17 15:05:18 +0100301
tierno9228f512019-07-04 16:23:00 +0000302 if cloud_config.get("boot-data-drive") is not None:
tierno0a1437e2017-10-02 00:17:43 +0200303 config_drive = cloud_config["boot-data-drive"]
sousaedu2ad85172021-02-17 15:05:18 +0100304
305 if (
306 cloud_config.get("config-files")
307 or cloud_config.get("users")
308 or cloud_config.get("key-pairs")
309 ):
tierno0a1437e2017-10-02 00:17:43 +0200310 userdata_dict = {}
sousaedu2ad85172021-02-17 15:05:18 +0100311
tierno0a1437e2017-10-02 00:17:43 +0200312 # default user
313 if cloud_config.get("key-pairs"):
314 userdata_dict["ssh-authorized-keys"] = cloud_config["key-pairs"]
sousaedu2ad85172021-02-17 15:05:18 +0100315 userdata_dict["users"] = [
316 {
317 "default": None,
318 "ssh-authorized-keys": cloud_config["key-pairs"],
319 }
320 ]
321
tierno0a1437e2017-10-02 00:17:43 +0200322 if cloud_config.get("users"):
323 if "users" not in userdata_dict:
324 userdata_dict["users"] = ["default"]
sousaedu2ad85172021-02-17 15:05:18 +0100325
tierno0a1437e2017-10-02 00:17:43 +0200326 for user in cloud_config["users"]:
327 user_info = {
328 "name": user["name"],
sousaedu2ad85172021-02-17 15:05:18 +0100329 "sudo": "ALL = (ALL)NOPASSWD:ALL",
tierno0a1437e2017-10-02 00:17:43 +0200330 }
sousaedu2ad85172021-02-17 15:05:18 +0100331
tierno0a1437e2017-10-02 00:17:43 +0200332 if "user-info" in user:
333 user_info["gecos"] = user["user-info"]
sousaedu2ad85172021-02-17 15:05:18 +0100334
tierno0a1437e2017-10-02 00:17:43 +0200335 if user.get("key-pairs"):
336 user_info["ssh-authorized-keys"] = user["key-pairs"]
sousaedu2ad85172021-02-17 15:05:18 +0100337
tierno0a1437e2017-10-02 00:17:43 +0200338 userdata_dict["users"].append(user_info)
339
340 if cloud_config.get("config-files"):
341 userdata_dict["write_files"] = []
342 for file in cloud_config["config-files"]:
sousaedu2ad85172021-02-17 15:05:18 +0100343 file_info = {"path": file["dest"], "content": file["content"]}
344
tierno0a1437e2017-10-02 00:17:43 +0200345 if file.get("encoding"):
346 file_info["encoding"] = file["encoding"]
sousaedu2ad85172021-02-17 15:05:18 +0100347
tierno0a1437e2017-10-02 00:17:43 +0200348 if file.get("permissions"):
349 file_info["permissions"] = file["permissions"]
sousaedu2ad85172021-02-17 15:05:18 +0100350
tierno0a1437e2017-10-02 00:17:43 +0200351 if file.get("owner"):
352 file_info["owner"] = file["owner"]
sousaedu2ad85172021-02-17 15:05:18 +0100353
tierno0a1437e2017-10-02 00:17:43 +0200354 userdata_dict["write_files"].append(file_info)
sousaedu2ad85172021-02-17 15:05:18 +0100355
356 userdata_list.append(
357 "#cloud-config\n"
358 + yaml.safe_dump(userdata_dict, indent=4, default_flow_style=False)
359 )
tierno0a1437e2017-10-02 00:17:43 +0200360 userdata = self._create_mimemultipart(userdata_list)
361 self.logger.debug("userdata: %s", userdata)
362 elif isinstance(cloud_config, str):
363 userdata = cloud_config
sousaedu2ad85172021-02-17 15:05:18 +0100364
tierno0a1437e2017-10-02 00:17:43 +0200365 return config_drive, userdata
366
tiernoa7d34d02017-02-23 14:42:07 +0100367 def check_vim_connectivity(self):
368 """Checks VIM can be reached and user credentials are ok.
tierno72774862020-05-04 11:44:15 +0000369 Returns None if success or raises VimConnConnectionException, VimConnAuthException, ...
tiernoa7d34d02017-02-23 14:42:07 +0100370 """
tierno9228f512019-07-04 16:23:00 +0000371 # by default no checking until each connector implements it
372 return None
tiernoa7d34d02017-02-23 14:42:07 +0100373
tiernoae4a8d12016-07-08 12:30:39 +0200374 def get_tenant_list(self, filter_dict={}):
tiernoa7d34d02017-02-23 14:42:07 +0100375 """Obtain tenants of VIM
tiernofb1987b2016-11-15 17:35:06 +0000376 filter_dict dictionary that can contain the following keys:
tiernoae4a8d12016-07-08 12:30:39 +0200377 name: filter by tenant name
378 id: filter by tenant uuid/id
379 <other VIM specific>
tiernofb1987b2016-11-15 17:35:06 +0000380 Returns the tenant list of dictionaries, and empty list if no tenant match all the filers:
tiernoae4a8d12016-07-08 12:30:39 +0200381 [{'name':'<name>, 'id':'<id>, ...}, ...]
tiernoa7d34d02017-02-23 14:42:07 +0100382 """
tierno72774862020-05-04 11:44:15 +0000383 raise VimConnNotImplemented("Should have implemented this")
tiernoae4a8d12016-07-08 12:30:39 +0200384
sousaedu2ad85172021-02-17 15:05:18 +0100385 def new_network(
386 self,
387 net_name,
388 net_type,
389 ip_profile=None,
390 shared=False,
391 provider_network_profile=None,
392 ):
tiernofb1987b2016-11-15 17:35:06 +0000393 """Adds a tenant network to VIM
tiernoa7d34d02017-02-23 14:42:07 +0100394 Params:
395 'net_name': name of the network
396 'net_type': one of:
397 'bridge': overlay isolated network
398 'data': underlay E-LAN network for Passthrough and SRIOV interfaces
399 'ptp': underlay E-LINE network for Passthrough and SRIOV interfaces.
tierno41a69812018-02-16 14:34:33 +0100400 'ip_profile': is a dict containing the IP parameters of the network
401 'ip_version': can be "IPv4" or "IPv6" (Currently only IPv4 is implemented)
402 'subnet_address': ip_prefix_schema, that is X.X.X.X/Y
403 'gateway_address': (Optional) ip_schema, that is X.X.X.X
404 'dns_address': (Optional) comma separated list of ip_schema, e.g. X.X.X.X[,X,X,X,X]
405 'dhcp_enabled': True or False
406 'dhcp_start_address': ip_schema, first IP to grant
407 'dhcp_count': number of IPs to grant.
tiernoa7d34d02017-02-23 14:42:07 +0100408 'shared': if this network can be seen/use by other tenants/organization
kbsuba85c54d2019-10-17 16:30:32 +0000409 'provider_network_profile': (optional) contains {segmentation-id: vlan, provider-network: vim_netowrk}
garciadeblasebd66722019-01-31 16:01:31 +0000410 Returns a tuple with the network identifier and created_items, or raises an exception on error
411 created_items can be None or a dictionary where this method can include key-values that will be passed to
412 the method delete_network. Can be used to store created segments, created l2gw connections, etc.
tierno72774862020-05-04 11:44:15 +0000413 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 +0000414 as not present.
tiernofb1987b2016-11-15 17:35:06 +0000415 """
tierno72774862020-05-04 11:44:15 +0000416 raise VimConnNotImplemented("Should have implemented this")
tierno7edb6752016-03-21 17:37:52 +0100417
418 def get_network_list(self, filter_dict={}):
tiernoa7d34d02017-02-23 14:42:07 +0100419 """Obtain tenant networks of VIM
420 Params:
421 'filter_dict' (optional) contains entries to return only networks that matches ALL entries:
422 name: string => returns only networks with this name
423 id: string => returns networks with this VIM id, this imply returns one network at most
424 shared: boolean >= returns only networks that are (or are not) shared
425 tenant_id: sting => returns only networks that belong to this tenant/project
tierno72774862020-05-04 11:44:15 +0000426 ,#(not used yet) admin_state_up: boolean => returns only networks that are (or are not) in admin state
427 active
tiernoa7d34d02017-02-23 14:42:07 +0100428 #(not used yet) status: 'ACTIVE','ERROR',... => filter networks that are on this status
429 Returns the network list of dictionaries. each dictionary contains:
430 'id': (mandatory) VIM network id
431 'name': (mandatory) VIM network name
432 'status': (mandatory) can be 'ACTIVE', 'INACTIVE', 'DOWN', 'BUILD', 'ERROR', 'VIM_ERROR', 'OTHER'
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +0100433 'network_type': (optional) can be 'vxlan', 'vlan' or 'flat'
434 'segmentation_id': (optional) in case network_type is vlan or vxlan this field contains the segmentation id
tiernoa7d34d02017-02-23 14:42:07 +0100435 'error_msg': (optional) text that explains the ERROR status
436 other VIM specific fields: (optional) whenever possible using the same naming of filter_dict param
437 List can be empty if no network map the filter_dict. Raise an exception only upon VIM connectivity,
438 authorization, or some other unspecific error
439 """
tierno72774862020-05-04 11:44:15 +0000440 raise VimConnNotImplemented("Should have implemented this")
tierno7edb6752016-03-21 17:37:52 +0100441
tiernoae4a8d12016-07-08 12:30:39 +0200442 def get_network(self, net_id):
tiernoa7d34d02017-02-23 14:42:07 +0100443 """Obtain network details from the 'net_id' VIM network
444 Return a dict that contains:
445 'id': (mandatory) VIM network id, that is, net_id
446 'name': (mandatory) VIM network name
447 'status': (mandatory) can be 'ACTIVE', 'INACTIVE', 'DOWN', 'BUILD', 'ERROR', 'VIM_ERROR', 'OTHER'
448 'error_msg': (optional) text that explains the ERROR status
449 other VIM specific fields: (optional) whenever possible using the same naming of filter_dict param
450 Raises an exception upon error or when network is not found
451 """
tierno72774862020-05-04 11:44:15 +0000452 raise VimConnNotImplemented("Should have implemented this")
tierno7edb6752016-03-21 17:37:52 +0100453
garciadeblasebd66722019-01-31 16:01:31 +0000454 def delete_network(self, net_id, created_items=None):
455 """
456 Removes a tenant network from VIM and its associated elements
457 :param net_id: VIM identifier of the network, provided by method new_network
458 :param created_items: dictionary with extra items to be deleted. provided by method new_network
tiernoa7d34d02017-02-23 14:42:07 +0100459 Returns the network identifier or raises an exception upon error or when network is not found
460 """
tierno72774862020-05-04 11:44:15 +0000461 raise VimConnNotImplemented("Should have implemented this")
tierno7edb6752016-03-21 17:37:52 +0100462
tiernoae4a8d12016-07-08 12:30:39 +0200463 def refresh_nets_status(self, net_list):
tiernoa7d34d02017-02-23 14:42:07 +0100464 """Get the status of the networks
465 Params:
466 'net_list': a list with the VIM network id to be get the status
467 Returns a dictionary with:
468 'net_id': #VIM id of this network
469 status: #Mandatory. Text with one of:
470 # DELETED (not found at vim)
471 # VIM_ERROR (Cannot connect to VIM, authentication problems, VIM response error, ...)
472 # OTHER (Vim reported other status not understood)
473 # ERROR (VIM indicates an ERROR status)
474 # ACTIVE, INACTIVE, DOWN (admin down),
475 # BUILD (on building process)
476 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
477 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
478 'net_id2': ...
479 """
tierno72774862020-05-04 11:44:15 +0000480 raise VimConnNotImplemented("Should have implemented this")
tiernoae4a8d12016-07-08 12:30:39 +0200481
482 def get_flavor(self, flavor_id):
tiernoa7d34d02017-02-23 14:42:07 +0100483 """Obtain flavor details from the VIM
484 Returns the flavor dict details {'id':<>, 'name':<>, other vim specific }
485 Raises an exception upon error or if not found
486 """
tierno72774862020-05-04 11:44:15 +0000487 raise VimConnNotImplemented("Should have implemented this")
tiernocf157a82017-01-30 14:07:06 +0100488
489 def get_flavor_id_from_data(self, flavor_dict):
490 """Obtain flavor id that match the flavor description
tiernoa7d34d02017-02-23 14:42:07 +0100491 Params:
492 'flavor_dict': dictionary that contains:
493 'disk': main hard disk in GB
494 'ram': meomry in MB
495 'vcpus': number of virtual cpus
496 #TODO: complete parameters for EPA
tierno72774862020-05-04 11:44:15 +0000497 Returns the flavor_id or raises a VimConnNotFoundException
tiernocf157a82017-01-30 14:07:06 +0100498 """
tierno72774862020-05-04 11:44:15 +0000499 raise VimConnNotImplemented("Should have implemented this")
tiernocf157a82017-01-30 14:07:06 +0100500
tiernoae4a8d12016-07-08 12:30:39 +0200501 def new_flavor(self, flavor_data):
tiernoa7d34d02017-02-23 14:42:07 +0100502 """Adds a tenant flavor to VIM
tiernoae4a8d12016-07-08 12:30:39 +0200503 flavor_data contains a dictionary with information, keys:
504 name: flavor name
505 ram: memory (cloud type) in MBytes
506 vpcus: cpus (cloud type)
507 extended: EPA parameters
508 - numas: #items requested in same NUMA
509 memory: number of 1G huge pages memory
tierno72774862020-05-04 11:44:15 +0000510 paired-threads|cores|threads: number of paired hyperthreads, complete cores OR individual
511 threads
tiernoae4a8d12016-07-08 12:30:39 +0200512 interfaces: # passthrough(PT) or SRIOV interfaces attached to this numa
513 - name: interface name
514 dedicated: yes|no|yes:sriov; for PT, SRIOV or only one SRIOV for the physical NIC
515 bandwidth: X Gbps; requested guarantee bandwidth
borsatti8a2dda32019-12-18 15:08:57 +0000516 vpci: requested virtual PCI address
tiernoae4a8d12016-07-08 12:30:39 +0200517 disk: disk size
518 is_public:
tiernoae4a8d12016-07-08 12:30:39 +0200519 #TODO to concrete
sousaedu2ad85172021-02-17 15:05:18 +0100520 Returns the flavor identifier
521 """
tierno72774862020-05-04 11:44:15 +0000522 raise VimConnNotImplemented("Should have implemented this")
tierno7edb6752016-03-21 17:37:52 +0100523
tiernoae4a8d12016-07-08 12:30:39 +0200524 def delete_flavor(self, flavor_id):
tiernoa7d34d02017-02-23 14:42:07 +0100525 """Deletes a tenant flavor from VIM identify by its id
sousaedu2ad85172021-02-17 15:05:18 +0100526 Returns the used id or raise an exception
527 """
tierno72774862020-05-04 11:44:15 +0000528 raise VimConnNotImplemented("Should have implemented this")
tierno7edb6752016-03-21 17:37:52 +0100529
tiernoa7d34d02017-02-23 14:42:07 +0100530 def new_image(self, image_dict):
sousaedu2ad85172021-02-17 15:05:18 +0100531 """Adds a tenant image to VIM
tiernoa7d34d02017-02-23 14:42:07 +0100532 Returns the image id or raises an exception if failed
533 """
tierno72774862020-05-04 11:44:15 +0000534 raise VimConnNotImplemented("Should have implemented this")
tierno7edb6752016-03-21 17:37:52 +0100535
tiernoae4a8d12016-07-08 12:30:39 +0200536 def delete_image(self, image_id):
tiernoa7d34d02017-02-23 14:42:07 +0100537 """Deletes a tenant image from VIM
sousaedu2ad85172021-02-17 15:05:18 +0100538 Returns the image_id if image is deleted or raises an exception on error
539 """
tierno72774862020-05-04 11:44:15 +0000540 raise VimConnNotImplemented("Should have implemented this")
tierno7edb6752016-03-21 17:37:52 +0100541
tiernoae4a8d12016-07-08 12:30:39 +0200542 def get_image_id_from_path(self, path):
tiernocf157a82017-01-30 14:07:06 +0100543 """Get the image id from image path in the VIM database.
sousaedu2ad85172021-02-17 15:05:18 +0100544 Returns the image_id or raises a VimConnNotFoundException
tiernocf157a82017-01-30 14:07:06 +0100545 """
tierno72774862020-05-04 11:44:15 +0000546 raise VimConnNotImplemented("Should have implemented this")
borsatti8a2dda32019-12-18 15:08:57 +0000547
garciadeblasb69fa9f2016-09-28 12:04:10 +0200548 def get_image_list(self, filter_dict={}):
tiernoa7d34d02017-02-23 14:42:07 +0100549 """Obtain tenant images from VIM
garciadeblasb69fa9f2016-09-28 12:04:10 +0200550 Filter_dict can be:
551 name: image name
552 id: image uuid
553 checksum: image checksum
554 location: image path
555 Returns the image list of dictionaries:
556 [{<the fields at Filter_dict plus some VIM specific>}, ...]
557 List can be empty
tiernoa7d34d02017-02-23 14:42:07 +0100558 """
tierno72774862020-05-04 11:44:15 +0000559 raise VimConnNotImplemented("Should have implemented this")
garciadeblasb69fa9f2016-09-28 12:04:10 +0200560
sousaedu2ad85172021-02-17 15:05:18 +0100561 def new_vminstance(
562 self,
563 name,
564 description,
565 start,
566 image_id,
567 flavor_id,
568 net_list,
569 cloud_config=None,
570 disk_list=None,
571 availability_zone_index=None,
572 availability_zone_list=None,
573 ):
tiernoa7d34d02017-02-23 14:42:07 +0100574 """Adds a VM instance to VIM
tierno7edb6752016-03-21 17:37:52 +0100575 Params:
tiernoa7d34d02017-02-23 14:42:07 +0100576 'start': (boolean) indicates if VM must start or created in pause mode.
577 'image_id','flavor_id': image and flavor VIM id to use for the VM
578 'net_list': list of interfaces, each one is a dictionary with:
579 'name': (optional) name for the interface.
580 'net_id': VIM network id where this interface must be connect to. Mandatory for type==virtual
sousaedu2ad85172021-02-17 15:05:18 +0100581 'vpci': (optional) virtual vPCI address to assign at the VM. Can be ignored depending on VIM
tierno72774862020-05-04 11:44:15 +0000582 capabilities
garciadeblasc4f4d732018-10-25 18:17:24 +0200583 'model': (optional and only have sense for type==virtual) interface model: virtio, e1000, ...
tiernoa7d34d02017-02-23 14:42:07 +0100584 'mac_address': (optional) mac address to assign to this interface
tierno41a69812018-02-16 14:34:33 +0100585 'ip_address': (optional) IP address to assign to this interface
tierno72774862020-05-04 11:44:15 +0000586 #TODO: CHECK if an optional 'vlan' parameter is needed for VIMs when type if VF and net_id is not
sousaedu2ad85172021-02-17 15:05:18 +0100587 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 +0000588 for tagging VF
tiernoa7d34d02017-02-23 14:42:07 +0100589 'type': (mandatory) can be one of:
590 'virtual', in this case always connected to a network of type 'net_type=bridge'
tierno72774862020-05-04 11:44:15 +0000591 'PCI-PASSTHROUGH' or 'PF' (passthrough): depending on VIM capabilities it can be connected to a
592 data/ptp network ot it
tiernoa7d34d02017-02-23 14:42:07 +0100593 can created unconnected
tierno66eba6e2017-11-10 17:09:18 +0100594 'SR-IOV' or 'VF' (SRIOV with VLAN tag): same as PF for network connectivity.
tiernoa7d34d02017-02-23 14:42:07 +0100595 'VFnotShared'(SRIOV without VLAN tag) same as PF for network connectivity. VF where no other VFs
596 are allocated on the same physical NIC
597 'bw': (optional) only for PF/VF/VFnotShared. Minimal Bandwidth required for the interface in GBPS
tiernob3d36742017-03-03 23:51:05 +0100598 'port_security': (optional) If False it must avoid any traffic filtering at this interface. If missing
599 or True, it must apply the default VIM behaviour
tiernoa7d34d02017-02-23 14:42:07 +0100600 After execution the method will add the key:
601 'vim_id': must be filled/added by this method with the VIM identifier generated by the VIM for this
602 interface. 'net_list' is modified
603 'cloud_config': (optional) dictionary with:
604 'key-pairs': (optional) list of strings with the public key to be inserted to the default user
605 'users': (optional) list of users to be inserted, each item is a dict with:
606 'name': (mandatory) user name,
607 'key-pairs': (optional) list of strings with the public key to be inserted to the user
tierno40e1bce2017-08-09 09:12:04 +0200608 'user-data': (optional) can be a string with the text script to be passed directly to cloud-init,
609 or a list of strings, each one contains a script to be passed, usually with a MIMEmultipart file
tiernoa7d34d02017-02-23 14:42:07 +0100610 'config-files': (optional). List of files to be transferred. Each item is a dict with:
611 'dest': (mandatory) string with the destination absolute path
612 'encoding': (optional, by default text). Can be one of:
613 'b64', 'base64', 'gz', 'gz+b64', 'gz+base64', 'gzip+b64', 'gzip+base64'
614 'content' (mandatory): string with the content of the file
615 'permissions': (optional) string with file permissions, typically octal notation '0644'
616 'owner': (optional) file owner, string with the format 'owner:group'
617 'boot-data-drive': boolean to indicate if user-data must be passed using a boot drive (hard disk)
618 'disk_list': (optional) list with additional disks to the VM. Each item is a dict with:
619 'image_id': (optional). VIM id of an existing image. If not provided an empty disk must be mounted
620 'size': (mandatory) string with the size of the disk in GB
tierno5a3273c2017-08-29 11:43:46 +0200621 availability_zone_index: Index of availability_zone_list to use for this this VM. None if not AV required
622 availability_zone_list: list of availability zones given by user in the VNFD descriptor. Ignore if
623 availability_zone_index is None
tierno98e909c2017-10-14 13:27:03 +0200624 Returns a tuple with the instance identifier and created_items or raises an exception on error
625 created_items can be None or a dictionary where this method can include key-values that will be passed to
626 the method delete_vminstance and action_vminstance. Can be used to store created ports, volumes, etc.
tierno72774862020-05-04 11:44:15 +0000627 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 +0200628 as not present.
tiernoa7d34d02017-02-23 14:42:07 +0100629 """
tierno72774862020-05-04 11:44:15 +0000630 raise VimConnNotImplemented("Should have implemented this")
borsatti8a2dda32019-12-18 15:08:57 +0000631
tierno72774862020-05-04 11:44:15 +0000632 def get_vminstance(self, vm_id):
tiernoa7d34d02017-02-23 14:42:07 +0100633 """Returns the VM instance information from VIM"""
tierno72774862020-05-04 11:44:15 +0000634 raise VimConnNotImplemented("Should have implemented this")
borsatti8a2dda32019-12-18 15:08:57 +0000635
tierno98e909c2017-10-14 13:27:03 +0200636 def delete_vminstance(self, vm_id, created_items=None):
637 """
garciadeblasebd66722019-01-31 16:01:31 +0000638 Removes a VM instance from VIM and its associated elements
tierno98e909c2017-10-14 13:27:03 +0200639 :param vm_id: VIM identifier of the VM, provided by method new_vminstance
640 :param created_items: dictionary with extra items to be deleted. provided by method new_vminstance and/or method
641 action_vminstance
642 :return: None or the same vm_id. Raises an exception on fail
643 """
tierno72774862020-05-04 11:44:15 +0000644 raise VimConnNotImplemented("Should have implemented this")
tierno7edb6752016-03-21 17:37:52 +0100645
tiernoae4a8d12016-07-08 12:30:39 +0200646 def refresh_vms_status(self, vm_list):
tiernoa7d34d02017-02-23 14:42:07 +0100647 """Get the status of the virtual machines and their interfaces/ports
sousaedu2ad85172021-02-17 15:05:18 +0100648 Params: the list of VM identifiers
649 Returns a dictionary with:
650 vm_id: #VIM id of this Virtual Machine
651 status: #Mandatory. Text with one of:
652 # DELETED (not found at vim)
653 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
654 # OTHER (Vim reported other status not understood)
655 # ERROR (VIM indicates an ERROR status)
656 # ACTIVE, PAUSED, SUSPENDED, INACTIVE (not running),
657 # BUILD (on building process), ERROR
658 # ACTIVE:NoMgmtIP (Active but any of its interface has an IP address
659 #
660 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
661 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
662 interfaces: list with interface info. Each item a dictionary with:
663 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
664 mac_address: #Text format XX:XX:XX:XX:XX:XX
665 vim_net_id: #network id where this interface is connected, if provided at creation
666 vim_interface_id: #interface/port VIM id
667 ip_address: #null, or text with IPv4, IPv6 address
668 compute_node: #identification of compute node where PF,VF interface is allocated
669 pci: #PCI address of the NIC that hosts the PF,VF
670 vlan: #physical VLAN used for VF
tiernoa7d34d02017-02-23 14:42:07 +0100671 """
tierno72774862020-05-04 11:44:15 +0000672 raise VimConnNotImplemented("Should have implemented this")
borsatti8a2dda32019-12-18 15:08:57 +0000673
tierno98e909c2017-10-14 13:27:03 +0200674 def action_vminstance(self, vm_id, action_dict, created_items={}):
675 """
676 Send and action over a VM instance. Returns created_items if the action was successfully sent to the VIM.
677 created_items is a dictionary with items that
678 :param vm_id: VIM identifier of the VM, provided by method new_vminstance
679 :param action_dict: dictionary with the action to perform
680 :param created_items: provided by method new_vminstance is a dictionary with key-values that will be passed to
tierno72774862020-05-04 11:44:15 +0000681 the method delete_vminstance. Can be used to store created ports, volumes, etc. Format is VimConnector
tierno98e909c2017-10-14 13:27:03 +0200682 dependent, but do not use nested dictionaries and a value of None should be the same as not present. This
683 method can modify this value
684 :return: None, or a console dict
685 """
tierno72774862020-05-04 11:44:15 +0000686 raise VimConnNotImplemented("Should have implemented this")
borsatti8a2dda32019-12-18 15:08:57 +0000687
tiernoa7d34d02017-02-23 14:42:07 +0100688 def get_vminstance_console(self, vm_id, console_type="vnc"):
689 """
tiernoae4a8d12016-07-08 12:30:39 +0200690 Get a console for the virtual machine
691 Params:
692 vm_id: uuid of the VM
693 console_type, can be:
borsatti8a2dda32019-12-18 15:08:57 +0000694 "novnc" (by default), "xvpvnc" for VNC types,
tiernoae4a8d12016-07-08 12:30:39 +0200695 "rdp-html5" for RDP types, "spice-html5" for SPICE types
696 Returns dict with the console parameters:
697 protocol: ssh, ftp, http, https, ...
borsatti8a2dda32019-12-18 15:08:57 +0000698 server: usually ip address
699 port: the http, ssh, ... port
700 suffix: extra text, e.g. the http path and query string
tiernoa7d34d02017-02-23 14:42:07 +0100701 """
tierno72774862020-05-04 11:44:15 +0000702 raise VimConnNotImplemented("Should have implemented this")
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +0000703
sousaedu2ad85172021-02-17 15:05:18 +0100704 def inject_user_key(
705 self, ip_addr=None, user=None, key=None, ro_key=None, password=None
706 ):
tierno9228f512019-07-04 16:23:00 +0000707 """
708 Inject a ssh public key in a VM
709 Params:
710 ip_addr: ip address of the VM
711 user: username (default-user) to enter in the VM
712 key: public key to be injected in the VM
713 ro_key: private key of the RO, used to enter in the VM if the password is not provided
714 password: password of the user to enter in the VM
715 The function doesn't return a value:
716 """
717 if not ip_addr or not user:
sousaedu2ad85172021-02-17 15:05:18 +0100718 raise VimConnNotSupportedException(
719 "All parameters should be different from 'None'"
720 )
tierno9228f512019-07-04 16:23:00 +0000721 elif not ro_key and not password:
sousaedu2ad85172021-02-17 15:05:18 +0100722 raise VimConnNotSupportedException(
723 "All parameters should be different from 'None'"
724 )
tierno9228f512019-07-04 16:23:00 +0000725 else:
sousaedu2ad85172021-02-17 15:05:18 +0100726 commands = {
727 "mkdir -p ~/.ssh/",
728 'echo "{}" >> ~/.ssh/authorized_keys'.format(key),
729 "chmod 644 ~/.ssh/authorized_keys",
730 "chmod 700 ~/.ssh/",
731 }
tierno9228f512019-07-04 16:23:00 +0000732 client = paramiko.SSHClient()
sousaedu2ad85172021-02-17 15:05:18 +0100733
tierno9228f512019-07-04 16:23:00 +0000734 try:
735 if ro_key:
tierno7d782ef2019-10-04 12:56:31 +0000736 pkey = paramiko.RSAKey.from_private_key(StringIO(ro_key))
tierno9228f512019-07-04 16:23:00 +0000737 else:
738 pkey = None
sousaedu2ad85172021-02-17 15:05:18 +0100739
tierno9228f512019-07-04 16:23:00 +0000740 client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
sousaedu2ad85172021-02-17 15:05:18 +0100741 client.connect(
742 ip_addr, username=user, password=password, pkey=pkey, timeout=10
743 )
744
tierno9228f512019-07-04 16:23:00 +0000745 for command in commands:
746 (i, o, e) = client.exec_command(command, timeout=10)
747 returncode = o.channel.recv_exit_status()
tierno9228f512019-07-04 16:23:00 +0000748 outerror = e.read()
sousaedu2ad85172021-02-17 15:05:18 +0100749
tierno9228f512019-07-04 16:23:00 +0000750 if returncode != 0:
751 text = "run_command='{}' Error='{}'".format(command, outerror)
sousaedu2ad85172021-02-17 15:05:18 +0100752 raise VimConnUnexpectedResponse(
753 "Cannot inject ssh key in VM: '{}'".format(text)
754 )
755
tierno9228f512019-07-04 16:23:00 +0000756 return
sousaedu2ad85172021-02-17 15:05:18 +0100757 except (
758 socket.error,
759 paramiko.AuthenticationException,
760 paramiko.SSHException,
761 ) as message:
tierno72774862020-05-04 11:44:15 +0000762 raise VimConnUnexpectedResponse(
sousaedu2ad85172021-02-17 15:05:18 +0100763 "Cannot inject ssh key in VM: '{}' - {}".format(
764 ip_addr, str(message)
765 )
766 )
767
tierno9228f512019-07-04 16:23:00 +0000768 return
769
sousaedu2ad85172021-02-17 15:05:18 +0100770 # Optional methods
tierno72774862020-05-04 11:44:15 +0000771 def new_tenant(self, tenant_name, tenant_description):
tierno9228f512019-07-04 16:23:00 +0000772 """Adds a new tenant to VIM with this name and description, this is done using admin_url if provided
773 "tenant_name": string max lenght 64
774 "tenant_description": string max length 256
775 returns the tenant identifier or raise exception
776 """
tierno72774862020-05-04 11:44:15 +0000777 raise VimConnNotImplemented("Should have implemented this")
tierno9228f512019-07-04 16:23:00 +0000778
sousaedu2ad85172021-02-17 15:05:18 +0100779 def delete_tenant(self, tenant_id):
tierno9228f512019-07-04 16:23:00 +0000780 """Delete a tenant from VIM
781 tenant_id: returned VIM tenant_id on "new_tenant"
tierno72774862020-05-04 11:44:15 +0000782 Returns None on success. Raises and exception of failure. If tenant is not found raises VimConnNotFoundException
tierno9228f512019-07-04 16:23:00 +0000783 """
tierno72774862020-05-04 11:44:15 +0000784 raise VimConnNotImplemented("Should have implemented this")
tierno9228f512019-07-04 16:23:00 +0000785
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +0000786 def new_classification(self, name, ctype, definition):
787 """Creates a traffic classification in the VIM
788 Params:
789 'name': name of this classification
790 'ctype': type of this classification
791 'definition': definition of this classification (type-dependent free-form text)
792 Returns the VIM's classification ID on success or raises an exception on failure
793 """
tierno72774862020-05-04 11:44:15 +0000794 raise VimConnNotImplemented("SFC support not implemented")
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +0000795
796 def get_classification(self, classification_id):
797 """Obtain classification details of the VIM's classification with ID='classification_id'
798 Return a dict that contains:
799 'id': VIM's classification ID (same as classification_id)
800 'name': VIM's classification name
801 'type': type of this classification
802 'definition': definition of the classification
803 'status': 'ACTIVE', 'INACTIVE', 'DOWN', 'BUILD', 'ERROR', 'VIM_ERROR', 'OTHER'
804 'error_msg': (optional) text that explains the ERROR status
805 other VIM specific fields: (optional) whenever possible
806 Raises an exception upon error or when classification is not found
807 """
tierno72774862020-05-04 11:44:15 +0000808 raise VimConnNotImplemented("SFC support not implemented")
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +0000809
810 def get_classification_list(self, filter_dict={}):
811 """Obtain classifications from the VIM
812 Params:
tierno72774862020-05-04 11:44:15 +0000813 'filter_dict' (optional): contains the entries to filter the classifications on and only return those that
814 match ALL:
815 id: string => returns classifications with this VIM's classification ID, which implies a return of one
816 classification at most
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +0000817 name: string => returns only classifications with this name
818 type: string => returns classifications of this type
819 definition: string => returns classifications that have this definition
820 tenant_id: string => returns only classifications that belong to this tenant/project
821 Returns a list of classification dictionaries, each dictionary contains:
822 'id': (mandatory) VIM's classification ID
823 'name': (mandatory) VIM's classification name
824 'type': type of this classification
825 'definition': definition of the classification
826 other VIM specific fields: (optional) whenever possible using the same naming of filter_dict param
827 List can be empty if no classification matches the filter_dict. Raise an exception only upon VIM connectivity,
828 authorization, or some other unspecific error
829 """
tierno72774862020-05-04 11:44:15 +0000830 raise VimConnNotImplemented("SFC support not implemented")
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +0000831
borsatti8a2dda32019-12-18 15:08:57 +0000832 def refresh_classifications_status(self, classification_list):
sousaedu2ad85172021-02-17 15:05:18 +0100833 """Get the status of the classifications
834 Params: the list of classification identifiers
835 Returns a dictionary with:
836 vm_id: #VIM id of this classifier
837 status: #Mandatory. Text with one of:
838 # DELETED (not found at vim)
839 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
840 # OTHER (Vim reported other status not understood)
841 # ERROR (VIM indicates an ERROR status)
842 # ACTIVE,
843 # CREATING (on building process)
844 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
845 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
846 """
tierno72774862020-05-04 11:44:15 +0000847 raise VimConnNotImplemented("Should have implemented this")
borsatti8a2dda32019-12-18 15:08:57 +0000848
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +0000849 def delete_classification(self, classification_id):
850 """Deletes a classification from the VIM
tierno72774862020-05-04 11:44:15 +0000851 Returns the classification ID (classification_id) or raises an exception upon error or when classification is
852 not found
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +0000853 """
tierno72774862020-05-04 11:44:15 +0000854 raise VimConnNotImplemented("SFC support not implemented")
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +0000855
856 def new_sfi(self, name, ingress_ports, egress_ports, sfc_encap=True):
857 """Creates a service function instance in the VIM
858 Params:
859 'name': name of this service function instance
860 'ingress_ports': set of ingress ports (VIM's port IDs)
861 'egress_ports': set of egress ports (VIM's port IDs)
862 'sfc_encap': boolean stating whether this specific instance supports IETF SFC Encapsulation
863 Returns the VIM's service function instance ID on success or raises an exception on failure
864 """
tierno72774862020-05-04 11:44:15 +0000865 raise VimConnNotImplemented("SFC support not implemented")
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +0000866
867 def get_sfi(self, sfi_id):
868 """Obtain service function instance details of the VIM's service function instance with ID='sfi_id'
869 Return a dict that contains:
870 'id': VIM's sfi ID (same as sfi_id)
871 'name': VIM's sfi name
872 'ingress_ports': set of ingress ports (VIM's port IDs)
873 'egress_ports': set of egress ports (VIM's port IDs)
874 'status': 'ACTIVE', 'INACTIVE', 'DOWN', 'BUILD', 'ERROR', 'VIM_ERROR', 'OTHER'
875 'error_msg': (optional) text that explains the ERROR status
876 other VIM specific fields: (optional) whenever possible
877 Raises an exception upon error or when service function instance is not found
878 """
tierno72774862020-05-04 11:44:15 +0000879 raise VimConnNotImplemented("SFC support not implemented")
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +0000880
881 def get_sfi_list(self, filter_dict={}):
882 """Obtain service function instances from the VIM
883 Params:
884 'filter_dict' (optional): contains the entries to filter the sfis on and only return those that match ALL:
885 id: string => returns sfis with this VIM's sfi ID, which implies a return of one sfi at most
886 name: string => returns only service function instances with this name
887 tenant_id: string => returns only service function instances that belong to this tenant/project
888 Returns a list of service function instance dictionaries, each dictionary contains:
889 'id': (mandatory) VIM's sfi ID
890 'name': (mandatory) VIM's sfi name
891 'ingress_ports': set of ingress ports (VIM's port IDs)
892 'egress_ports': set of egress ports (VIM's port IDs)
893 other VIM specific fields: (optional) whenever possible using the same naming of filter_dict param
894 List can be empty if no sfi matches the filter_dict. Raise an exception only upon VIM connectivity,
895 authorization, or some other unspecific error
896 """
tierno72774862020-05-04 11:44:15 +0000897 raise VimConnNotImplemented("SFC support not implemented")
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +0000898
899 def delete_sfi(self, sfi_id):
900 """Deletes a service function instance from the VIM
901 Returns the service function instance ID (sfi_id) or raises an exception upon error or when sfi is not found
902 """
tierno72774862020-05-04 11:44:15 +0000903 raise VimConnNotImplemented("SFC support not implemented")
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +0000904
borsatti8a2dda32019-12-18 15:08:57 +0000905 def refresh_sfis_status(self, sfi_list):
sousaedu2ad85172021-02-17 15:05:18 +0100906 """Get the status of the service function instances
907 Params: the list of sfi identifiers
908 Returns a dictionary with:
909 vm_id: #VIM id of this service function instance
910 status: #Mandatory. Text with one of:
911 # DELETED (not found at vim)
912 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
913 # OTHER (Vim reported other status not understood)
914 # ERROR (VIM indicates an ERROR status)
915 # ACTIVE,
916 # CREATING (on building process)
917 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
918 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
919 """
tierno72774862020-05-04 11:44:15 +0000920 raise VimConnNotImplemented("Should have implemented this")
borsatti8a2dda32019-12-18 15:08:57 +0000921
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +0000922 def new_sf(self, name, sfis, sfc_encap=True):
923 """Creates (an abstract) service function in the VIM
924 Params:
925 'name': name of this service function
926 'sfis': set of service function instances of this (abstract) service function
927 'sfc_encap': boolean stating whether this service function supports IETF SFC Encapsulation
928 Returns the VIM's service function ID on success or raises an exception on failure
929 """
tierno72774862020-05-04 11:44:15 +0000930 raise VimConnNotImplemented("SFC support not implemented")
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +0000931
932 def get_sf(self, sf_id):
933 """Obtain service function details of the VIM's service function with ID='sf_id'
934 Return a dict that contains:
935 'id': VIM's sf ID (same as sf_id)
936 'name': VIM's sf name
937 'sfis': VIM's sf's set of VIM's service function instance IDs
938 'sfc_encap': boolean stating whether this service function supports IETF SFC Encapsulation
939 'status': 'ACTIVE', 'INACTIVE', 'DOWN', 'BUILD', 'ERROR', 'VIM_ERROR', 'OTHER'
940 'error_msg': (optional) text that explains the ERROR status
941 other VIM specific fields: (optional) whenever possible
942 Raises an exception upon error or when sf is not found
943 """
944
945 def get_sf_list(self, filter_dict={}):
946 """Obtain service functions from the VIM
947 Params:
948 'filter_dict' (optional): contains the entries to filter the sfs on and only return those that match ALL:
949 id: string => returns sfs with this VIM's sf ID, which implies a return of one sf at most
950 name: string => returns only service functions with this name
951 tenant_id: string => returns only service functions that belong to this tenant/project
952 Returns a list of service function dictionaries, each dictionary contains:
953 'id': (mandatory) VIM's sf ID
954 'name': (mandatory) VIM's sf name
955 'sfis': VIM's sf's set of VIM's service function instance IDs
956 'sfc_encap': boolean stating whether this service function supports IETF SFC Encapsulation
957 other VIM specific fields: (optional) whenever possible using the same naming of filter_dict param
958 List can be empty if no sf matches the filter_dict. Raise an exception only upon VIM connectivity,
959 authorization, or some other unspecific error
960 """
tierno72774862020-05-04 11:44:15 +0000961 raise VimConnNotImplemented("SFC support not implemented")
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +0000962
963 def delete_sf(self, sf_id):
964 """Deletes (an abstract) service function from the VIM
965 Returns the service function ID (sf_id) or raises an exception upon error or when sf is not found
966 """
tierno72774862020-05-04 11:44:15 +0000967 raise VimConnNotImplemented("SFC support not implemented")
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +0000968
borsatti8a2dda32019-12-18 15:08:57 +0000969 def refresh_sfs_status(self, sf_list):
sousaedu2ad85172021-02-17 15:05:18 +0100970 """Get the status of the service functions
971 Params: the list of sf identifiers
972 Returns a dictionary with:
973 vm_id: #VIM id of this service function
974 status: #Mandatory. Text with one of:
975 # DELETED (not found at vim)
976 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
977 # OTHER (Vim reported other status not understood)
978 # ERROR (VIM indicates an ERROR status)
979 # ACTIVE,
980 # CREATING (on building process)
981 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
982 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
983 """
tierno72774862020-05-04 11:44:15 +0000984 raise VimConnNotImplemented("Should have implemented this")
borsatti8a2dda32019-12-18 15:08:57 +0000985
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +0000986 def new_sfp(self, name, classifications, sfs, sfc_encap=True, spi=None):
987 """Creates a service function path
988 Params:
989 'name': name of this service function path
990 'classifications': set of traffic classifications that should be matched on to get into this sfp
991 'sfs': list of every service function that constitutes this path , from first to last
992 'sfc_encap': whether this is an SFC-Encapsulated chain (i.e using NSH), True by default
993 'spi': (optional) the Service Function Path identifier (SPI: Service Path Identifier) for this path
994 Returns the VIM's sfp ID on success or raises an exception on failure
995 """
tierno72774862020-05-04 11:44:15 +0000996 raise VimConnNotImplemented("SFC support not implemented")
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +0000997
998 def get_sfp(self, sfp_id):
999 """Obtain service function path details of the VIM's sfp with ID='sfp_id'
1000 Return a dict that contains:
1001 'id': VIM's sfp ID (same as sfp_id)
1002 'name': VIM's sfp name
1003 'classifications': VIM's sfp's list of VIM's classification IDs
1004 'sfs': VIM's sfp's list of VIM's service function IDs
1005 'status': 'ACTIVE', 'INACTIVE', 'DOWN', 'BUILD', 'ERROR', 'VIM_ERROR', 'OTHER'
1006 'error_msg': (optional) text that explains the ERROR status
1007 other VIM specific fields: (optional) whenever possible
1008 Raises an exception upon error or when sfp is not found
1009 """
tierno72774862020-05-04 11:44:15 +00001010 raise VimConnNotImplemented("SFC support not implemented")
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +00001011
1012 def get_sfp_list(self, filter_dict={}):
1013 """Obtain service function paths from VIM
1014 Params:
1015 'filter_dict' (optional): contains the entries to filter the sfps on, and only return those that match ALL:
1016 id: string => returns sfps with this VIM's sfp ID , which implies a return of one sfp at most
1017 name: string => returns only sfps with this name
1018 tenant_id: string => returns only sfps that belong to this tenant/project
1019 Returns a list of service function path dictionaries, each dictionary contains:
1020 'id': (mandatory) VIM's sfp ID
1021 'name': (mandatory) VIM's sfp name
1022 'classifications': VIM's sfp's list of VIM's classification IDs
1023 'sfs': VIM's sfp's list of VIM's service function IDs
1024 other VIM specific fields: (optional) whenever possible using the same naming of filter_dict param
1025 List can be empty if no sfp matches the filter_dict. Raise an exception only upon VIM connectivity,
1026 authorization, or some other unspecific error
1027 """
tierno72774862020-05-04 11:44:15 +00001028 raise VimConnNotImplemented("SFC support not implemented")
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +00001029
borsatti8a2dda32019-12-18 15:08:57 +00001030 def refresh_sfps_status(self, sfp_list):
sousaedu2ad85172021-02-17 15:05:18 +01001031 """Get the status of the service function path
1032 Params: the list of sfp identifiers
1033 Returns a dictionary with:
1034 vm_id: #VIM id of this service function path
1035 status: #Mandatory. Text with one of:
1036 # DELETED (not found at vim)
1037 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
1038 # OTHER (Vim reported other status not understood)
1039 # ERROR (VIM indicates an ERROR status)
1040 # ACTIVE,
1041 # CREATING (on building process)
1042 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
1043 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)F
1044 """
tierno72774862020-05-04 11:44:15 +00001045 raise VimConnNotImplemented("Should have implemented this")
borsatti8a2dda32019-12-18 15:08:57 +00001046
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +00001047 def delete_sfp(self, sfp_id):
1048 """Deletes a service function path from the VIM
1049 Returns the sfp ID (sfp_id) or raises an exception upon error or when sf is not found
1050 """
tierno72774862020-05-04 11:44:15 +00001051 raise VimConnNotImplemented("SFC support not implemented")
Igor Duarte Cardoso862a60a2017-08-09 16:07:46 +00001052
sousaedu2ad85172021-02-17 15:05:18 +01001053 # NOT USED METHODS in current version. Deprecated
tierno9228f512019-07-04 16:23:00 +00001054 @deprecated
tierno7edb6752016-03-21 17:37:52 +01001055 def host_vim2gui(self, host, server_dict):
tiernoa7d34d02017-02-23 14:42:07 +01001056 """Transform host dictionary from VIM format to GUI format,
tierno7edb6752016-03-21 17:37:52 +01001057 and append to the server_dict
tiernoa7d34d02017-02-23 14:42:07 +01001058 """
tierno72774862020-05-04 11:44:15 +00001059 raise VimConnNotImplemented("Should have implemented this")
tierno7edb6752016-03-21 17:37:52 +01001060
tierno9228f512019-07-04 16:23:00 +00001061 @deprecated
tierno7edb6752016-03-21 17:37:52 +01001062 def get_hosts_info(self):
tiernoa7d34d02017-02-23 14:42:07 +01001063 """Get the information of deployed hosts
1064 Returns the hosts content"""
tierno72774862020-05-04 11:44:15 +00001065 raise VimConnNotImplemented("Should have implemented this")
tierno7edb6752016-03-21 17:37:52 +01001066
tierno9228f512019-07-04 16:23:00 +00001067 @deprecated
tierno7edb6752016-03-21 17:37:52 +01001068 def get_hosts(self, vim_tenant):
tiernoa7d34d02017-02-23 14:42:07 +01001069 """Get the hosts and deployed instances
1070 Returns the hosts content"""
tierno72774862020-05-04 11:44:15 +00001071 raise VimConnNotImplemented("Should have implemented this")
tierno7edb6752016-03-21 17:37:52 +01001072
tierno9228f512019-07-04 16:23:00 +00001073 @deprecated
tierno7edb6752016-03-21 17:37:52 +01001074 def get_processor_rankings(self):
tiernoa7d34d02017-02-23 14:42:07 +01001075 """Get the processor rankings in the VIM database"""
tierno72774862020-05-04 11:44:15 +00001076 raise VimConnNotImplemented("Should have implemented this")
borsatti8a2dda32019-12-18 15:08:57 +00001077
tierno9228f512019-07-04 16:23:00 +00001078 @deprecated
tiernoae4a8d12016-07-08 12:30:39 +02001079 def new_host(self, host_data):
tiernoa7d34d02017-02-23 14:42:07 +01001080 """Adds a new host to VIM"""
1081 """Returns status code of the VIM response"""
tierno72774862020-05-04 11:44:15 +00001082 raise VimConnNotImplemented("Should have implemented this")
borsatti8a2dda32019-12-18 15:08:57 +00001083
tierno9228f512019-07-04 16:23:00 +00001084 @deprecated
tiernoae4a8d12016-07-08 12:30:39 +02001085 def new_external_port(self, port_data):
tiernoa7d34d02017-02-23 14:42:07 +01001086 """Adds a external port to VIM"""
1087 """Returns the port identifier"""
tierno72774862020-05-04 11:44:15 +00001088 raise VimConnNotImplemented("Should have implemented this")
borsatti8a2dda32019-12-18 15:08:57 +00001089
tierno9228f512019-07-04 16:23:00 +00001090 @deprecated
tierno72774862020-05-04 11:44:15 +00001091 def new_external_network(self, net_name, net_type):
tiernoa7d34d02017-02-23 14:42:07 +01001092 """Adds a external network to VIM (shared)"""
1093 """Returns the network identifier"""
tierno72774862020-05-04 11:44:15 +00001094 raise VimConnNotImplemented("Should have implemented this")
tiernoae4a8d12016-07-08 12:30:39 +02001095
tierno9228f512019-07-04 16:23:00 +00001096 @deprecated
tiernoae4a8d12016-07-08 12:30:39 +02001097 def connect_port_network(self, port_id, network_id, admin=False):
tiernoa7d34d02017-02-23 14:42:07 +01001098 """Connects a external port to a network"""
1099 """Returns status code of the VIM response"""
tierno72774862020-05-04 11:44:15 +00001100 raise VimConnNotImplemented("Should have implemented this")
tiernoae4a8d12016-07-08 12:30:39 +02001101
tierno9228f512019-07-04 16:23:00 +00001102 @deprecated
tiernoae4a8d12016-07-08 12:30:39 +02001103 def new_vminstancefromJSON(self, vm_data):
tiernoa7d34d02017-02-23 14:42:07 +01001104 """Adds a VM instance to VIM"""
1105 """Returns the instance identifier"""
tierno72774862020-05-04 11:44:15 +00001106 raise VimConnNotImplemented("Should have implemented this")