blob: 031815689d1af42fe65bf44c49ae54f2917e472e [file] [log] [blame]
tiernoc3dfbfb2019-05-22 09:56:05 +00001# -*- 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"""
20This WIM does nothing and allows using it for testing and when no WIM is needed
21"""
22
sousaedu049cbb12022-01-05 11:39:35 +000023from http import HTTPStatus
tiernoc3dfbfb2019-05-22 09:56:05 +000024import logging
25from uuid import uuid4
sousaedu049cbb12022-01-05 11:39:35 +000026
tierno72774862020-05-04 11:44:15 +000027from osm_ro_plugin.sdnconn import SdnConnectorBase, SdnConnectorError
sousaedu049cbb12022-01-05 11:39:35 +000028
sousaedu80135b92021-02-17 15:05:18 +010029
tiernoc3dfbfb2019-05-22 09:56:05 +000030__author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
31
32
tierno72774862020-05-04 11:44:15 +000033class SdnDummyConnector(SdnConnectorBase):
tiernoc3dfbfb2019-05-22 09:56:05 +000034 """Abstract base class for all the WIM connectors
35
36 Arguments:
37 wim (dict): WIM record, as stored in the database
38 wim_account (dict): WIM account record, as stored in the database
39 config (dict): optional persistent information related to an specific
40 connector. Inside this dict, a special key,
41 ``service_endpoint_mapping`` provides the internal endpoint
42 mapping.
43 logger (logging.Logger): optional logger object. If none is passed
44 ``openmano.wim.wimconn`` is used.
45
46 The arguments of the constructor are converted to object attributes.
47 An extra property, ``service_endpoint_mapping`` is created from ``config``.
48 """
sousaedu80135b92021-02-17 15:05:18 +010049
tiernoc3dfbfb2019-05-22 09:56:05 +000050 def __init__(self, wim, wim_account, config=None, logger=None):
sousaedu80135b92021-02-17 15:05:18 +010051 self.logger = logger or logging.getLogger("ro.sdn.dummy")
tierno72774862020-05-04 11:44:15 +000052 super(SdnDummyConnector, self).__init__(wim, wim_account, config, self.logger)
tiernoc3dfbfb2019-05-22 09:56:05 +000053 self.logger.debug("__init: wim='{}' wim_account='{}'".format(wim, wim_account))
54 self.connections = {}
55 self.counter = 0
56
57 def check_credentials(self):
58 """Check if the connector itself can access the WIM.
59
60 Raises:
tiernoed3e4d42019-10-21 15:31:27 +000061 SdnConnectorError: Issues regarding authorization, access to
tiernoc3dfbfb2019-05-22 09:56:05 +000062 external URLs, etc are detected.
63 """
64 self.logger.debug("check_credentials")
sousaedu80135b92021-02-17 15:05:18 +010065
tiernoc3dfbfb2019-05-22 09:56:05 +000066 return None
67
68 def get_connectivity_service_status(self, service_uuid, conn_info=None):
69 """Monitor the status of the connectivity service established
70
71 Arguments:
72 service_uuid (str): UUID of the connectivity service
73 conn_info (dict or None): Information returned by the connector
74 during the service creation/edition and subsequently stored in
75 the database.
76
77 Returns:
78 dict: JSON/YAML-serializable dict that contains a mandatory key
tiernoed3e4d42019-10-21 15:31:27 +000079 ``sdn_status`` associated with one of the following values::
tiernoc3dfbfb2019-05-22 09:56:05 +000080
tiernoed3e4d42019-10-21 15:31:27 +000081 Additionally ``error_msg``(**str**) and ``sdn_info``(**dict**)
tiernoc3dfbfb2019-05-22 09:56:05 +000082 keys can be used to provide additional status explanation or
83 new information available for the connectivity service.
84 """
sousaedu80135b92021-02-17 15:05:18 +010085 self.logger.debug(
86 "get_connectivity_service_status: service_uuid='{}' conn_info='{}'".format(
87 service_uuid, conn_info
88 )
89 )
tiernoc3dfbfb2019-05-22 09:56:05 +000090
sousaedu80135b92021-02-17 15:05:18 +010091 return {"sdn_status": "ACTIVE", "sdn_info": self.connections.get(service_uuid)}
tiernoc3dfbfb2019-05-22 09:56:05 +000092
sousaedu80135b92021-02-17 15:05:18 +010093 def create_connectivity_service(self, service_type, connection_points, **kwargs):
94 """Establish WAN connectivity between the endpoints"""
95 self.logger.debug(
96 "create_connectivity_service: service_type='{}' connection_points='{}', kwargs='{}'".format(
97 service_type, connection_points, kwargs
98 )
99 )
tiernoc3dfbfb2019-05-22 09:56:05 +0000100 _id = str(uuid4())
tiernoed3e4d42019-10-21 15:31:27 +0000101 self.connections[_id] = connection_points.copy()
tiernoc3dfbfb2019-05-22 09:56:05 +0000102 self.counter += 1
sousaedu80135b92021-02-17 15:05:18 +0100103
tiernoed3e4d42019-10-21 15:31:27 +0000104 return _id, None
tiernoc3dfbfb2019-05-22 09:56:05 +0000105
106 def delete_connectivity_service(self, service_uuid, conn_info=None):
sousaedu80135b92021-02-17 15:05:18 +0100107 """Disconnect multi-site endpoints previously connected"""
108 self.logger.debug(
109 "delete_connectivity_service: service_uuid='{}' conn_info='{}'".format(
110 service_uuid, conn_info
111 )
112 )
tiernoc3dfbfb2019-05-22 09:56:05 +0000113
tiernoed3e4d42019-10-21 15:31:27 +0000114 if service_uuid not in self.connections:
sousaedu80135b92021-02-17 15:05:18 +0100115 raise SdnConnectorError(
116 "connectivity {} not found".format(service_uuid),
117 http_code=HTTPStatus.NOT_FOUND.value,
118 )
119
tiernoed3e4d42019-10-21 15:31:27 +0000120 self.connections.pop(service_uuid, None)
sousaedu80135b92021-02-17 15:05:18 +0100121
tiernoc3dfbfb2019-05-22 09:56:05 +0000122 return None
123
sousaedu80135b92021-02-17 15:05:18 +0100124 def edit_connectivity_service(
125 self, service_uuid, conn_info=None, connection_points=None, **kwargs
126 ):
tiernoc3dfbfb2019-05-22 09:56:05 +0000127 """Change an existing connectivity service.
128
129 This method's arguments and return value follow the same convention as
130 :meth:`~.create_connectivity_service`.
131 """
sousaedu80135b92021-02-17 15:05:18 +0100132 self.logger.debug(
133 "edit_connectivity_service: service_uuid='{}' conn_info='{}', connection_points='{}'"
134 "kwargs='{}'".format(service_uuid, conn_info, connection_points, kwargs)
135 )
136
tiernoed3e4d42019-10-21 15:31:27 +0000137 if service_uuid not in self.connections:
sousaedu80135b92021-02-17 15:05:18 +0100138 raise SdnConnectorError(
139 "connectivity {} not found".format(service_uuid),
140 http_code=HTTPStatus.NOT_FOUND.value,
141 )
142
tiernoed3e4d42019-10-21 15:31:27 +0000143 self.connections[service_uuid] = connection_points.copy()
sousaedu80135b92021-02-17 15:05:18 +0100144
tiernoc3dfbfb2019-05-22 09:56:05 +0000145 return None
146
147 def clear_all_connectivity_services(self):
148 """Delete all WAN Links in a WIM.
149
150 This method is intended for debugging only, and should delete all the
151 connections controlled by the WIM, not only the WIM connections that
152 a specific RO is aware of.
153
154 """
155 self.logger.debug("clear_all_connectivity_services")
tiernoed3e4d42019-10-21 15:31:27 +0000156 self.connections.clear()
sousaedu80135b92021-02-17 15:05:18 +0100157
tiernoc3dfbfb2019-05-22 09:56:05 +0000158 return None
159
160 def get_all_active_connectivity_services(self):
161 """Provide information about all active connections provisioned by a
162 WIM.
163
164 Raises:
tiernoed3e4d42019-10-21 15:31:27 +0000165 SdnConnectorException: In case of error.
tiernoc3dfbfb2019-05-22 09:56:05 +0000166 """
167 self.logger.debug("get_all_active_connectivity_services")
sousaedu80135b92021-02-17 15:05:18 +0100168
tiernoed3e4d42019-10-21 15:31:27 +0000169 return self.connections