| 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 |
| tierno | c3dfbfb | 2019-05-22 09:56:05 +0000 | [diff] [blame] | 27 | __author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>" |
| 28 | |
| 29 | |
| tierno | 7277486 | 2020-05-04 11:44:15 +0000 | [diff] [blame] | 30 | class SdnDummyConnector(SdnConnectorBase): |
| tierno | c3dfbfb | 2019-05-22 09:56:05 +0000 | [diff] [blame] | 31 | """Abstract base class for all the WIM connectors |
| 32 | |
| 33 | Arguments: |
| 34 | wim (dict): WIM record, as stored in the database |
| 35 | wim_account (dict): WIM account record, as stored in the database |
| 36 | config (dict): optional persistent information related to an specific |
| 37 | connector. Inside this dict, a special key, |
| 38 | ``service_endpoint_mapping`` provides the internal endpoint |
| 39 | mapping. |
| 40 | logger (logging.Logger): optional logger object. If none is passed |
| 41 | ``openmano.wim.wimconn`` is used. |
| 42 | |
| 43 | The arguments of the constructor are converted to object attributes. |
| 44 | An extra property, ``service_endpoint_mapping`` is created from ``config``. |
| 45 | """ |
| 46 | def __init__(self, wim, wim_account, config=None, logger=None): |
| sousaedu | 3f8f2f4 | 2021-02-09 15:30:01 +0100 | [diff] [blame^] | 47 | self.logger = logger or logging.getLogger('ro.sdn.dummy') |
| tierno | 7277486 | 2020-05-04 11:44:15 +0000 | [diff] [blame] | 48 | super(SdnDummyConnector, self).__init__(wim, wim_account, config, self.logger) |
| tierno | c3dfbfb | 2019-05-22 09:56:05 +0000 | [diff] [blame] | 49 | self.logger.debug("__init: wim='{}' wim_account='{}'".format(wim, wim_account)) |
| 50 | self.connections = {} |
| 51 | self.counter = 0 |
| 52 | |
| 53 | def check_credentials(self): |
| 54 | """Check if the connector itself can access the WIM. |
| 55 | |
| 56 | Raises: |
| tierno | ed3e4d4 | 2019-10-21 15:31:27 +0000 | [diff] [blame] | 57 | SdnConnectorError: Issues regarding authorization, access to |
| tierno | c3dfbfb | 2019-05-22 09:56:05 +0000 | [diff] [blame] | 58 | external URLs, etc are detected. |
| 59 | """ |
| 60 | self.logger.debug("check_credentials") |
| 61 | return None |
| 62 | |
| 63 | def get_connectivity_service_status(self, service_uuid, conn_info=None): |
| 64 | """Monitor the status of the connectivity service established |
| 65 | |
| 66 | Arguments: |
| 67 | service_uuid (str): UUID of the connectivity service |
| 68 | conn_info (dict or None): Information returned by the connector |
| 69 | during the service creation/edition and subsequently stored in |
| 70 | the database. |
| 71 | |
| 72 | Returns: |
| 73 | dict: JSON/YAML-serializable dict that contains a mandatory key |
| tierno | ed3e4d4 | 2019-10-21 15:31:27 +0000 | [diff] [blame] | 74 | ``sdn_status`` associated with one of the following values:: |
| tierno | c3dfbfb | 2019-05-22 09:56:05 +0000 | [diff] [blame] | 75 | |
| tierno | ed3e4d4 | 2019-10-21 15:31:27 +0000 | [diff] [blame] | 76 | Additionally ``error_msg``(**str**) and ``sdn_info``(**dict**) |
| tierno | c3dfbfb | 2019-05-22 09:56:05 +0000 | [diff] [blame] | 77 | keys can be used to provide additional status explanation or |
| 78 | new information available for the connectivity service. |
| 79 | """ |
| 80 | self.logger.debug("get_connectivity_service_status: service_uuid='{}' conn_info='{}'".format(service_uuid, |
| 81 | conn_info)) |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 82 | return {'sdn_status': 'ACTIVE', 'sdn_info': self.connections.get(service_uuid)} |
| tierno | c3dfbfb | 2019-05-22 09:56:05 +0000 | [diff] [blame] | 83 | |
| 84 | def create_connectivity_service(self, service_type, connection_points, |
| 85 | **kwargs): |
| 86 | """ |
| 87 | Stablish WAN connectivity between the endpoints |
| 88 | |
| 89 | """ |
| 90 | self.logger.debug("create_connectivity_service: service_type='{}' connection_points='{}', kwargs='{}'". |
| 91 | format(service_type, connection_points, kwargs)) |
| 92 | _id = str(uuid4()) |
| tierno | ed3e4d4 | 2019-10-21 15:31:27 +0000 | [diff] [blame] | 93 | self.connections[_id] = connection_points.copy() |
| tierno | c3dfbfb | 2019-05-22 09:56:05 +0000 | [diff] [blame] | 94 | self.counter += 1 |
| tierno | ed3e4d4 | 2019-10-21 15:31:27 +0000 | [diff] [blame] | 95 | return _id, None |
| tierno | c3dfbfb | 2019-05-22 09:56:05 +0000 | [diff] [blame] | 96 | |
| 97 | def delete_connectivity_service(self, service_uuid, conn_info=None): |
| 98 | """Disconnect multi-site endpoints previously connected |
| 99 | |
| 100 | """ |
| 101 | self.logger.debug("delete_connectivity_service: service_uuid='{}' conn_info='{}'".format(service_uuid, |
| 102 | conn_info)) |
| tierno | ed3e4d4 | 2019-10-21 15:31:27 +0000 | [diff] [blame] | 103 | if service_uuid not in self.connections: |
| 104 | raise SdnConnectorError("connectivity {} not found".format(service_uuid), |
| 105 | http_code=HTTPStatus.NOT_FOUND.value) |
| 106 | self.connections.pop(service_uuid, None) |
| tierno | c3dfbfb | 2019-05-22 09:56:05 +0000 | [diff] [blame] | 107 | return None |
| 108 | |
| 109 | def edit_connectivity_service(self, service_uuid, conn_info=None, |
| 110 | connection_points=None, **kwargs): |
| 111 | """Change an existing connectivity service. |
| 112 | |
| 113 | This method's arguments and return value follow the same convention as |
| 114 | :meth:`~.create_connectivity_service`. |
| 115 | """ |
| 116 | self.logger.debug("edit_connectivity_service: service_uuid='{}' conn_info='{}', connection_points='{}'" |
| 117 | "kwargs='{}'".format(service_uuid, conn_info, connection_points, kwargs)) |
| tierno | ed3e4d4 | 2019-10-21 15:31:27 +0000 | [diff] [blame] | 118 | if service_uuid not in self.connections: |
| 119 | raise SdnConnectorError("connectivity {} not found".format(service_uuid), |
| 120 | http_code=HTTPStatus.NOT_FOUND.value) |
| 121 | self.connections[service_uuid] = connection_points.copy() |
| tierno | c3dfbfb | 2019-05-22 09:56:05 +0000 | [diff] [blame] | 122 | return None |
| 123 | |
| 124 | def clear_all_connectivity_services(self): |
| 125 | """Delete all WAN Links in a WIM. |
| 126 | |
| 127 | This method is intended for debugging only, and should delete all the |
| 128 | connections controlled by the WIM, not only the WIM connections that |
| 129 | a specific RO is aware of. |
| 130 | |
| 131 | """ |
| 132 | self.logger.debug("clear_all_connectivity_services") |
| tierno | ed3e4d4 | 2019-10-21 15:31:27 +0000 | [diff] [blame] | 133 | self.connections.clear() |
| tierno | c3dfbfb | 2019-05-22 09:56:05 +0000 | [diff] [blame] | 134 | return None |
| 135 | |
| 136 | def get_all_active_connectivity_services(self): |
| 137 | """Provide information about all active connections provisioned by a |
| 138 | WIM. |
| 139 | |
| 140 | Raises: |
| tierno | ed3e4d4 | 2019-10-21 15:31:27 +0000 | [diff] [blame] | 141 | SdnConnectorException: In case of error. |
| tierno | c3dfbfb | 2019-05-22 09:56:05 +0000 | [diff] [blame] | 142 | """ |
| 143 | self.logger.debug("get_all_active_connectivity_services") |
| tierno | ed3e4d4 | 2019-10-21 15:31:27 +0000 | [diff] [blame] | 144 | return self.connections |