| tierno | c3dfbfb | 2019-05-22 09:56:05 +0000 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | ## |
| 3 | # Copyright 2018 Telefonica |
| 4 | # All Rights Reserved. |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | # you may not use this file except in compliance with the License. |
| 8 | # You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 15 | # implied. |
| 16 | # See the License for the specific language governing permissions and |
| 17 | # limitations under the License. |
| 18 | |
| 19 | """ |
| 20 | This WIM does nothing and allows using it for testing and when no WIM is needed |
| 21 | """ |
| 22 | |
| 23 | import logging |
| 24 | from uuid import uuid4 |
| tierno | 7277486 | 2020-05-04 11:44:15 +0000 | [diff] [blame] | 25 | from osm_ro_plugin.sdnconn import SdnConnectorBase, SdnConnectorError |
| tierno | ed3e4d4 | 2019-10-21 15:31:27 +0000 | [diff] [blame] | 26 | from http import HTTPStatus |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 27 | |
| tierno | c3dfbfb | 2019-05-22 09:56:05 +0000 | [diff] [blame] | 28 | __author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>" |
| 29 | |
| 30 | |
| tierno | 7277486 | 2020-05-04 11:44:15 +0000 | [diff] [blame] | 31 | class SdnDummyConnector(SdnConnectorBase): |
| tierno | c3dfbfb | 2019-05-22 09:56:05 +0000 | [diff] [blame] | 32 | """Abstract base class for all the WIM connectors |
| 33 | |
| 34 | Arguments: |
| 35 | wim (dict): WIM record, as stored in the database |
| 36 | wim_account (dict): WIM account record, as stored in the database |
| 37 | config (dict): optional persistent information related to an specific |
| 38 | connector. Inside this dict, a special key, |
| 39 | ``service_endpoint_mapping`` provides the internal endpoint |
| 40 | mapping. |
| 41 | logger (logging.Logger): optional logger object. If none is passed |
| 42 | ``openmano.wim.wimconn`` is used. |
| 43 | |
| 44 | The arguments of the constructor are converted to object attributes. |
| 45 | An extra property, ``service_endpoint_mapping`` is created from ``config``. |
| 46 | """ |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 47 | |
| tierno | c3dfbfb | 2019-05-22 09:56:05 +0000 | [diff] [blame] | 48 | def __init__(self, wim, wim_account, config=None, logger=None): |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 49 | self.logger = logger or logging.getLogger("ro.sdn.dummy") |
| tierno | 7277486 | 2020-05-04 11:44:15 +0000 | [diff] [blame] | 50 | super(SdnDummyConnector, self).__init__(wim, wim_account, config, self.logger) |
| tierno | c3dfbfb | 2019-05-22 09:56:05 +0000 | [diff] [blame] | 51 | self.logger.debug("__init: wim='{}' wim_account='{}'".format(wim, wim_account)) |
| 52 | self.connections = {} |
| 53 | self.counter = 0 |
| 54 | |
| 55 | def check_credentials(self): |
| 56 | """Check if the connector itself can access the WIM. |
| 57 | |
| 58 | Raises: |
| tierno | ed3e4d4 | 2019-10-21 15:31:27 +0000 | [diff] [blame] | 59 | SdnConnectorError: Issues regarding authorization, access to |
| tierno | c3dfbfb | 2019-05-22 09:56:05 +0000 | [diff] [blame] | 60 | external URLs, etc are detected. |
| 61 | """ |
| 62 | self.logger.debug("check_credentials") |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 63 | |
| tierno | c3dfbfb | 2019-05-22 09:56:05 +0000 | [diff] [blame] | 64 | return None |
| 65 | |
| 66 | def get_connectivity_service_status(self, service_uuid, conn_info=None): |
| 67 | """Monitor the status of the connectivity service established |
| 68 | |
| 69 | Arguments: |
| 70 | service_uuid (str): UUID of the connectivity service |
| 71 | conn_info (dict or None): Information returned by the connector |
| 72 | during the service creation/edition and subsequently stored in |
| 73 | the database. |
| 74 | |
| 75 | Returns: |
| 76 | dict: JSON/YAML-serializable dict that contains a mandatory key |
| tierno | ed3e4d4 | 2019-10-21 15:31:27 +0000 | [diff] [blame] | 77 | ``sdn_status`` associated with one of the following values:: |
| tierno | c3dfbfb | 2019-05-22 09:56:05 +0000 | [diff] [blame] | 78 | |
| tierno | ed3e4d4 | 2019-10-21 15:31:27 +0000 | [diff] [blame] | 79 | Additionally ``error_msg``(**str**) and ``sdn_info``(**dict**) |
| tierno | c3dfbfb | 2019-05-22 09:56:05 +0000 | [diff] [blame] | 80 | keys can be used to provide additional status explanation or |
| 81 | new information available for the connectivity service. |
| 82 | """ |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 83 | self.logger.debug( |
| 84 | "get_connectivity_service_status: service_uuid='{}' conn_info='{}'".format( |
| 85 | service_uuid, conn_info |
| 86 | ) |
| 87 | ) |
| tierno | c3dfbfb | 2019-05-22 09:56:05 +0000 | [diff] [blame] | 88 | |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 89 | return {"sdn_status": "ACTIVE", "sdn_info": self.connections.get(service_uuid)} |
| tierno | c3dfbfb | 2019-05-22 09:56:05 +0000 | [diff] [blame] | 90 | |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 91 | def create_connectivity_service(self, service_type, connection_points, **kwargs): |
| 92 | """Establish WAN connectivity between the endpoints""" |
| 93 | self.logger.debug( |
| 94 | "create_connectivity_service: service_type='{}' connection_points='{}', kwargs='{}'".format( |
| 95 | service_type, connection_points, kwargs |
| 96 | ) |
| 97 | ) |
| tierno | c3dfbfb | 2019-05-22 09:56:05 +0000 | [diff] [blame] | 98 | _id = str(uuid4()) |
| tierno | ed3e4d4 | 2019-10-21 15:31:27 +0000 | [diff] [blame] | 99 | self.connections[_id] = connection_points.copy() |
| tierno | c3dfbfb | 2019-05-22 09:56:05 +0000 | [diff] [blame] | 100 | self.counter += 1 |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 101 | |
| tierno | ed3e4d4 | 2019-10-21 15:31:27 +0000 | [diff] [blame] | 102 | return _id, None |
| tierno | c3dfbfb | 2019-05-22 09:56:05 +0000 | [diff] [blame] | 103 | |
| 104 | def delete_connectivity_service(self, service_uuid, conn_info=None): |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 105 | """Disconnect multi-site endpoints previously connected""" |
| 106 | self.logger.debug( |
| 107 | "delete_connectivity_service: service_uuid='{}' conn_info='{}'".format( |
| 108 | service_uuid, conn_info |
| 109 | ) |
| 110 | ) |
| tierno | c3dfbfb | 2019-05-22 09:56:05 +0000 | [diff] [blame] | 111 | |
| tierno | ed3e4d4 | 2019-10-21 15:31:27 +0000 | [diff] [blame] | 112 | if service_uuid not in self.connections: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 113 | raise SdnConnectorError( |
| 114 | "connectivity {} not found".format(service_uuid), |
| 115 | http_code=HTTPStatus.NOT_FOUND.value, |
| 116 | ) |
| 117 | |
| tierno | ed3e4d4 | 2019-10-21 15:31:27 +0000 | [diff] [blame] | 118 | self.connections.pop(service_uuid, None) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 119 | |
| tierno | c3dfbfb | 2019-05-22 09:56:05 +0000 | [diff] [blame] | 120 | return None |
| 121 | |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 122 | def edit_connectivity_service( |
| 123 | self, service_uuid, conn_info=None, connection_points=None, **kwargs |
| 124 | ): |
| tierno | c3dfbfb | 2019-05-22 09:56:05 +0000 | [diff] [blame] | 125 | """Change an existing connectivity service. |
| 126 | |
| 127 | This method's arguments and return value follow the same convention as |
| 128 | :meth:`~.create_connectivity_service`. |
| 129 | """ |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 130 | self.logger.debug( |
| 131 | "edit_connectivity_service: service_uuid='{}' conn_info='{}', connection_points='{}'" |
| 132 | "kwargs='{}'".format(service_uuid, conn_info, connection_points, kwargs) |
| 133 | ) |
| 134 | |
| tierno | ed3e4d4 | 2019-10-21 15:31:27 +0000 | [diff] [blame] | 135 | if service_uuid not in self.connections: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 136 | raise SdnConnectorError( |
| 137 | "connectivity {} not found".format(service_uuid), |
| 138 | http_code=HTTPStatus.NOT_FOUND.value, |
| 139 | ) |
| 140 | |
| tierno | ed3e4d4 | 2019-10-21 15:31:27 +0000 | [diff] [blame] | 141 | self.connections[service_uuid] = connection_points.copy() |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 142 | |
| tierno | c3dfbfb | 2019-05-22 09:56:05 +0000 | [diff] [blame] | 143 | return None |
| 144 | |
| 145 | def clear_all_connectivity_services(self): |
| 146 | """Delete all WAN Links in a WIM. |
| 147 | |
| 148 | This method is intended for debugging only, and should delete all the |
| 149 | connections controlled by the WIM, not only the WIM connections that |
| 150 | a specific RO is aware of. |
| 151 | |
| 152 | """ |
| 153 | self.logger.debug("clear_all_connectivity_services") |
| tierno | ed3e4d4 | 2019-10-21 15:31:27 +0000 | [diff] [blame] | 154 | self.connections.clear() |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 155 | |
| tierno | c3dfbfb | 2019-05-22 09:56:05 +0000 | [diff] [blame] | 156 | return None |
| 157 | |
| 158 | def get_all_active_connectivity_services(self): |
| 159 | """Provide information about all active connections provisioned by a |
| 160 | WIM. |
| 161 | |
| 162 | Raises: |
| tierno | ed3e4d4 | 2019-10-21 15:31:27 +0000 | [diff] [blame] | 163 | SdnConnectorException: In case of error. |
| tierno | c3dfbfb | 2019-05-22 09:56:05 +0000 | [diff] [blame] | 164 | """ |
| 165 | self.logger.debug("get_all_active_connectivity_services") |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 166 | |
| tierno | ed3e4d4 | 2019-10-21 15:31:27 +0000 | [diff] [blame] | 167 | return self.connections |