| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 1 | # |
| 2 | # Copyright 2020 University of Lancaster - High Performance Networks Research |
| 3 | # Group |
| 4 | # All Rights Reserved. |
| 5 | # |
| 6 | # Contributors: Will Fantom, Paul McCherry |
| 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 | # products derived from this software without specific prior written permission. |
| 21 | # |
| 22 | # This work has been performed in the context of DCMS UK 5G Testbeds |
| 23 | # & Trials Programme and in the framework of the Metro-Haul project - |
| 24 | # funded by the European Commission under Grant number 761727 through the |
| 25 | # Horizon 2020 and 5G-PPP programmes. |
| 26 | ## |
| 27 | |
| 28 | import json |
| 29 | import logging |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 30 | import struct |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 31 | |
| tierno | 7277486 | 2020-05-04 11:44:15 +0000 | [diff] [blame] | 32 | from osm_ro_plugin.sdnconn import SdnConnectorBase, SdnConnectorError |
| sousaedu | 049cbb1 | 2022-01-05 11:39:35 +0000 | [diff] [blame] | 33 | import paramiko |
| 34 | import requests |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 35 | |
| 36 | |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 37 | class DpbSshInterface: |
| garciadeblas | dfad9cd | 2021-05-14 17:22:01 +0200 | [diff] [blame] | 38 | """Communicate with the DPB via SSH""" |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 39 | |
| 40 | __LOGGER_NAME_EXT = ".ssh" |
| 41 | __FUNCTION_MAP_POS = 1 |
| 42 | |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 43 | def __init__( |
| 44 | self, username, password, wim_url, wim_port, network, auth_data, logger_name |
| 45 | ): |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 46 | self.logger = logging.getLogger(logger_name + self.__LOGGER_NAME_EXT) |
| 47 | self.__username = username |
| 48 | self.__password = password |
| 49 | self.__url = wim_url |
| 50 | self.__port = wim_port |
| 51 | self.__network = network |
| 52 | self.__auth_data = auth_data |
| 53 | self.__session_id = 1 |
| 54 | self.__ssh_client = self.__create_client() |
| 55 | self.__stdin = None |
| 56 | self.__stdout = None |
| 57 | self.logger.info("SSH connection to DPB defined") |
| 58 | |
| 59 | def _check_connection(self): |
| 60 | if not (self.__stdin and self.__stdout): |
| 61 | self.__stdin, self.__stdout = self.__connect() |
| 62 | |
| 63 | def post(self, function, url_params="", data=None, get_response=True): |
| 64 | """post request to dpb via ssh |
| 65 | |
| 66 | notes: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 67 | - session_id need only be unique per ssh session, thus is currently safe if |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 68 | ro is restarted |
| 69 | """ |
| 70 | self._check_connection() |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 71 | |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 72 | if data is None: |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 73 | data = {} |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 74 | |
| 75 | url_ext_info = url_params.split("/") |
| 76 | |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 77 | for i in range(0, len(url_ext_info)): |
| 78 | if url_ext_info[i] == "service": |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 79 | data["service-id"] = int(url_ext_info[i + 1]) |
| 80 | |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 81 | data["type"] = function[self.__FUNCTION_MAP_POS] |
| 82 | data = { |
| 83 | "session": self.__session_id, |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 84 | "content": data, |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 85 | } |
| 86 | self.__session_id += 1 |
| 87 | |
| 88 | try: |
| 89 | data = json.dumps(data).encode("utf-8") |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 90 | data_packed = struct.pack(">I" + str(len(data)) + "s", len(data), data) |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 91 | self.__stdin.write(data_packed) |
| 92 | self.logger.debug("Data sent to DPB via SSH") |
| 93 | except Exception as e: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 94 | raise SdnConnectorError("Failed to write via SSH | text: {}".format(e), 500) |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 95 | |
| 96 | try: |
| 97 | data_len = struct.unpack(">I", self.__stdout.read(4))[0] |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 98 | data = struct.unpack(str(data_len) + "s", self.__stdout.read(data_len))[0] |
| 99 | |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 100 | return json.loads(data).get("content", {}) |
| 101 | except Exception as e: |
| 102 | raise SdnConnectorError( |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 103 | "Could not get response from WIM | text: {}".format(e), 500 |
| 104 | ) |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 105 | |
| 106 | def get(self, function, url_params=""): |
| 107 | raise SdnConnectorError("SSH Get not implemented", 500) |
| 108 | |
| 109 | def __create_client(self): |
| 110 | ssh_client = paramiko.SSHClient() |
| garciadeblas | 60cb82f | 2024-09-17 18:27:24 +0200 | [diff] [blame] | 111 | # Load known host keys |
| 112 | ssh_client.load_system_host_keys() |
| 113 | # Reject unknown hosts |
| 114 | ssh_client.set_missing_host_key_policy(paramiko.RejectPolicy()) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 115 | |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 116 | return ssh_client |
| 117 | |
| 118 | def __connect(self): |
| 119 | private_key = None |
| 120 | password = None |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 121 | |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 122 | if self.__auth_data.get("auth_type", "PASS") == "KEY": |
| 123 | private_key = self.__build_private_key_obj() |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 124 | |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 125 | if self.__auth_data.get("auth_type", "PASS") == "PASS": |
| 126 | password = self.__password |
| 127 | |
| 128 | try: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 129 | self.__ssh_client.connect( |
| 130 | hostname=self.__url, |
| 131 | port=self.__port, |
| 132 | username=self.__username, |
| 133 | password=password, |
| 134 | pkey=private_key, |
| 135 | look_for_keys=False, |
| 136 | compress=False, |
| 137 | ) |
| garciadeblas | 60cb82f | 2024-09-17 18:27:24 +0200 | [diff] [blame] | 138 | # TODO: sanitizing commands to be executed |
| 139 | # Whitelist of allowed commands |
| 140 | # valid_commands = ["command1", "command2", "command3"] |
| 141 | # if self.__network not in valid_commands: |
| 142 | # raise SdnConnectorError("Invalid command executed", 400) |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 143 | stdin, stdout, stderr = self.__ssh_client.exec_command( |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 144 | command=self.__network |
| 145 | ) |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 146 | except paramiko.BadHostKeyException as e: |
| 147 | raise SdnConnectorError( |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 148 | "Could not add SSH host key | text: {}".format(e), 500 |
| 149 | ) |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 150 | except paramiko.AuthenticationException as e: |
| 151 | raise SdnConnectorError( |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 152 | "Could not authorize SSH connection | text: {}".format(e), 400 |
| 153 | ) |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 154 | except paramiko.SSHException as e: |
| 155 | raise SdnConnectorError( |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 156 | "Could not establish the SSH connection | text: {}".format(e), 500 |
| 157 | ) |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 158 | except Exception as e: |
| 159 | raise SdnConnectorError( |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 160 | "Unknown error occurred when connecting via SSH | text: {}".format(e), |
| 161 | 500, |
| 162 | ) |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 163 | |
| 164 | try: |
| 165 | data_len = struct.unpack(">I", stdout.read(4))[0] |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 166 | data = json.loads( |
| 167 | struct.unpack(str(data_len) + "s", stdout.read(data_len))[0] |
| 168 | ) |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 169 | except Exception as e: |
| 170 | raise SdnConnectorError( |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 171 | "Failed to get response from DPB | text: {}".format(e), 500 |
| 172 | ) |
| 173 | |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 174 | if "error" in data: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 175 | raise SdnConnectorError(data.get("msg", data.get("error", "ERROR")), 500) |
| 176 | |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 177 | self.logger.info("SSH connection to DPB established OK") |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 178 | |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 179 | return stdin, stdout |
| 180 | |
| 181 | def __build_private_key_obj(self): |
| 182 | try: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 183 | with open(self.__auth_data.get("key_file"), "r") as key_file: |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 184 | if self.__auth_data.get("key_type") == "RSA": |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 185 | return paramiko.RSAKey.from_private_key( |
| 186 | key_file, password=self.__auth_data.get("key_pass", None) |
| 187 | ) |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 188 | elif self.__auth_data.get("key_type") == "ECDSA": |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 189 | return paramiko.ECDSAKey.from_private_key( |
| 190 | key_file, password=self.__auth_data.get("key_pass", None) |
| 191 | ) |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 192 | else: |
| 193 | raise SdnConnectorError("Key type not supported", 400) |
| 194 | except Exception as e: |
| 195 | raise SdnConnectorError( |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 196 | "Could not load private SSH key | text: {}".format(e), 500 |
| 197 | ) |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 198 | |
| 199 | |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 200 | class DpbRestInterface: |
| garciadeblas | dfad9cd | 2021-05-14 17:22:01 +0200 | [diff] [blame] | 201 | """Communicate with the DPB via the REST API""" |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 202 | |
| 203 | __LOGGER_NAME_EXT = ".rest" |
| 204 | __FUNCTION_MAP_POS = 0 |
| 205 | |
| 206 | def __init__(self, wim_url, wim_port, network, logger_name): |
| 207 | self.logger = logging.getLogger(logger_name + self.__LOGGER_NAME_EXT) |
| 208 | self.__base_url = "http://{}:{}/network/{}".format( |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 209 | wim_url, str(wim_port), network |
| 210 | ) |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 211 | self.logger.info("REST defined OK") |
| 212 | |
| 213 | def post(self, function, url_params="", data=None, get_response=True): |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 214 | url = self.__base_url + url_params + "/" + function[self.__FUNCTION_MAP_POS] |
| 215 | |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 216 | try: |
| 217 | self.logger.info(data) |
| 218 | response = requests.post(url, json=data) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 219 | |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 220 | if response.status_code != 200: |
| 221 | raise SdnConnectorError( |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 222 | "REST request failed (status code: {})".format(response.status_code) |
| 223 | ) |
| 224 | |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 225 | if get_response: |
| 226 | return response.json() |
| 227 | except Exception as e: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 228 | raise SdnConnectorError("REST request failed | text: {}".format(e), 500) |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 229 | |
| 230 | def get(self, function, url_params=""): |
| 231 | url = self.__base_url + url_params + function[self.__FUNCTION_MAP_POS] |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 232 | |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 233 | try: |
| 234 | return requests.get(url) |
| 235 | except Exception as e: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 236 | raise SdnConnectorError("REST request failed | text: {}".format(e), 500) |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 237 | |
| 238 | |
| 239 | class DpbConnector(SdnConnectorBase): |
| garciadeblas | dfad9cd | 2021-05-14 17:22:01 +0200 | [diff] [blame] | 240 | """Use the DPB to establish multipoint connections""" |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 241 | |
| sousaedu | e493e9b | 2021-02-09 15:30:01 +0100 | [diff] [blame] | 242 | __LOGGER_NAME = "ro.sdn.dpb" |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 243 | __SUPPORTED_SERV_TYPES = ["ELAN (L2)", "ELINE (L2)"] |
| 244 | __SUPPORTED_CONNECTION_TYPES = ["REST", "SSH"] |
| 245 | __SUPPORTED_SSH_AUTH_TYPES = ["KEY", "PASS"] |
| 246 | __SUPPORTED_SSH_KEY_TYPES = ["ECDSA", "RSA"] |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 247 | __STATUS_MAP = {"ACTIVE": "ACTIVE", "ACTIVATING": "BUILD", "FAILED": "ERROR"} |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 248 | __ACTIONS_MAP = { |
| 249 | "CREATE": ("create-service", "new-service"), |
| 250 | "DEFINE": ("define", "define-service"), |
| 251 | "ACTIVATE": ("activate", "activate-service"), |
| 252 | "RELEASE": ("release", "release-service"), |
| 253 | "DEACTIVATE": ("deactivate", "deactivate-service"), |
| 254 | "CHECK": ("await-status", "await-service-status"), |
| 255 | "GET": ("services", "NOT IMPLEMENTED"), |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 256 | "RESET": ("reset", "NOT IMPLEMENTED"), |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | def __init__(self, wim, wim_account, config): |
| 260 | self.logger = logging.getLogger(self.__LOGGER_NAME) |
| 261 | |
| 262 | self.__wim = wim |
| 263 | self.__account = wim_account |
| 264 | self.__config = config |
| 265 | self.__cli_config = self.__account.pop("config", None) |
| 266 | |
| 267 | self.__url = self.__wim.get("wim_url", "") |
| 268 | self.__password = self.__account.get("passwd", "") |
| 269 | self.__username = self.__account.get("user", "") |
| 270 | self.__network = self.__cli_config.get("network", "") |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 271 | self.__connection_type = self.__cli_config.get("connection_type", "REST") |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 272 | self.__port = self.__cli_config.get( |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 273 | "port", (80 if self.__connection_type == "REST" else 22) |
| 274 | ) |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 275 | self.__ssh_auth = self.__cli_config.get("ssh_auth", None) |
| 276 | |
| 277 | if self.__connection_type == "SSH": |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 278 | interface = DpbSshInterface( |
| 279 | self.__username, |
| 280 | self.__password, |
| 281 | self.__url, |
| 282 | self.__port, |
| 283 | self.__network, |
| 284 | self.__ssh_auth, |
| 285 | self.__LOGGER_NAME, |
| 286 | ) |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 287 | elif self.__connection_type == "REST": |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 288 | interface = DpbRestInterface( |
| 289 | self.__url, self.__port, self.__network, self.__LOGGER_NAME |
| 290 | ) |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 291 | else: |
| 292 | raise SdnConnectorError( |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 293 | "Connection type not supported (must be SSH or REST)", 400 |
| 294 | ) |
| 295 | |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 296 | self.__post = interface.post |
| 297 | self.__get = interface.get |
| 298 | self.logger.info("DPB WimConn Init OK") |
| 299 | |
| 300 | def create_connectivity_service(self, service_type, connection_points, **kwargs): |
| 301 | self.logger.info("Creating a connectivity service") |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 302 | |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 303 | try: |
| 304 | response = self.__post(self.__ACTIONS_MAP.get("CREATE")) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 305 | |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 306 | if "service-id" in response: |
| 307 | service_id = int(response.get("service-id")) |
| 308 | self.logger.debug("created service id {}".format(service_id)) |
| 309 | else: |
| 310 | raise SdnConnectorError( |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 311 | "Invalid create service response (could be an issue with the DPB)", |
| 312 | 500, |
| 313 | ) |
| 314 | |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 315 | data = {"segment": []} |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 316 | |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 317 | for point in connection_points: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 318 | data["segment"].append( |
| 319 | { |
| 320 | "terminal-name": point.get("service_endpoint_id"), |
| 321 | "label": int( |
| 322 | (point.get("service_endpoint_encapsulation_info")).get( |
| 323 | "vlan" |
| 324 | ) |
| 325 | ), |
| 326 | "ingress-bw": 10.0, |
| 327 | "egress-bw": 10.0, |
| 328 | } |
| 329 | ) |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 330 | # "ingress-bw": (bandwidth.get(point.get("service_endpoint_id"))).get("ingress"), |
| 331 | # "egress-bw": (bandwidth.get(point.get("service_endpoint_id"))).get("egress")} |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 332 | |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 333 | self.__post( |
| 334 | self.__ACTIONS_MAP.get("DEFINE"), |
| 335 | "/service/" + str(service_id), |
| 336 | data, |
| 337 | get_response=False, |
| 338 | ) |
| 339 | self.__post( |
| 340 | self.__ACTIONS_MAP.get("ACTIVATE"), |
| 341 | "/service/" + str(service_id), |
| 342 | get_response=False, |
| 343 | ) |
| 344 | self.logger.debug("Created connectivity service id:{}".format(service_id)) |
| 345 | |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 346 | return (str(service_id), None) |
| 347 | except Exception as e: |
| 348 | raise SdnConnectorError( |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 349 | "Connectivity service could not be made | text: {}".format(e), 500 |
| 350 | ) |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 351 | |
| 352 | def get_connectivity_service_status(self, service_uuid, conn_info=None): |
| 353 | self.logger.info( |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 354 | "Checking connectivity service status id:{}".format(service_uuid) |
| 355 | ) |
| 356 | data = {"timeout-millis": 10000, "acceptable": ["ACTIVE", "FAILED"]} |
| 357 | |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 358 | try: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 359 | response = self.__post( |
| 360 | self.__ACTIONS_MAP.get("CHECK"), |
| 361 | "/service/" + service_uuid, |
| 362 | data, |
| 363 | ) |
| 364 | |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 365 | if "status" in response: |
| 366 | status = response.get("status", None) |
| 367 | self.logger.info("CHECKED CONNECTIVITY SERVICE STATUS") |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 368 | |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 369 | return {"wim_status": self.__STATUS_MAP.get(status)} |
| 370 | else: |
| 371 | raise SdnConnectorError( |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 372 | "Invalid status check response (could be an issue with the DPB)", |
| 373 | 500, |
| 374 | ) |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 375 | except Exception as e: |
| 376 | raise SdnConnectorError( |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 377 | "Failed to check service status | text: {}".format(e), 500 |
| 378 | ) |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 379 | |
| 380 | def delete_connectivity_service(self, service_uuid, conn_info=None): |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 381 | self.logger.info("Deleting connectivity service id: {}".format(service_uuid)) |
| 382 | |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 383 | try: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 384 | self.__post( |
| 385 | self.__ACTIONS_MAP.get("RELEASE"), |
| 386 | "/service/" + service_uuid, |
| 387 | get_response=False, |
| 388 | ) |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 389 | except Exception as e: |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 390 | raise SdnConnectorError( |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 391 | "Could not delete service id:{} (could be an issue with the DPB): {}".format( |
| 392 | service_uuid, e |
| 393 | ), |
| 394 | 500, |
| 395 | ) |
| 396 | |
| 397 | self.logger.debug("Deleted connectivity service id:{}".format(service_uuid)) |
| 398 | |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 399 | return None |
| 400 | |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 401 | def edit_connectivity_service( |
| 402 | self, service_uuid, conn_info=None, connection_points=None, **kwargs |
| 403 | ): |
| 404 | self.logger.info("Editing connectivity service id: {}".format(service_uuid)) |
| 405 | data = {"timeout-millis": 10000, "acceptable": ["DORMANT"]} |
| 406 | |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 407 | try: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 408 | self.__post( |
| 409 | self.__ACTIONS_MAP.get("RESET"), |
| 410 | "/service/" + service_uuid, |
| 411 | get_response=False, |
| 412 | ) |
| 413 | response = self.__post( |
| 414 | self.__ACTIONS_MAP.get("CHECK"), |
| 415 | "/service/" + service_uuid, |
| 416 | data, |
| 417 | ) |
| 418 | |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 419 | if "status" in response: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 420 | self.logger.debug("Connectivity service {} reset".format(service_uuid)) |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 421 | else: |
| 422 | raise SdnConnectorError( |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 423 | "Invalid status check response (could be an issue with the DPB)", |
| 424 | 500, |
| 425 | ) |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 426 | except Exception as e: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 427 | raise SdnConnectorError("Failed to reset service | text: {}".format(e), 500) |
| 428 | |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 429 | try: |
| 430 | data = {"segment": []} |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 431 | |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 432 | for point in connection_points: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 433 | data["segment"].append( |
| 434 | { |
| 435 | "terminal-name": point.get("service_endpoint_id"), |
| 436 | "label": int( |
| 437 | (point.get("service_endpoint_encapsulation_info")).get( |
| 438 | "vlan" |
| 439 | ) |
| 440 | ), |
| 441 | "ingress-bw": 10.0, |
| 442 | "egress-bw": 10.0, |
| 443 | } |
| 444 | ) |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 445 | # "ingress-bw": (bandwidth.get(point.get("service_endpoint_id"))).get("ingress"), |
| 446 | # "egress-bw": (bandwidth.get(point.get("service_endpoint_id"))).get("egress")} |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 447 | |
| 448 | self.__post( |
| 449 | self.__ACTIONS_MAP.get("DEFINE"), |
| 450 | "/service/" + str(service_uuid), |
| 451 | data, |
| 452 | get_response=False, |
| 453 | ) |
| 454 | self.__post( |
| 455 | self.__ACTIONS_MAP.get("ACTIVATE"), |
| 456 | "/service/" + str(service_uuid), |
| 457 | get_response=False, |
| 458 | ) |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 459 | except Exception as e: |
| 460 | raise SdnConnectorError( |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 461 | "Failed to edit connectivity service | text: {}".format(e), 500 |
| 462 | ) |
| 463 | |
| 464 | self.logger.debug("Edited connectivity service {}".format(service_uuid)) |
| 465 | |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 466 | return conn_info |
| 467 | |
| 468 | def __check_service(self, serv_type, points, kwargs): |
| tierno | 1ec592d | 2020-06-16 15:29:47 +0000 | [diff] [blame] | 469 | if serv_type not in self.__SUPPORTED_SERV_TYPES: |
| fantom | 36068fd | 2019-11-29 14:18:50 +0000 | [diff] [blame] | 470 | raise SdnConnectorError("Service type no supported", 400) |
| 471 | # Future: BW Checks here |