From cf3c13e830b24156619807785a140d8751a2820d Mon Sep 17 00:00:00 2001 From: Vinicius Arcanjo Date: Wed, 2 Nov 2022 17:48:59 -0300 Subject: [PATCH 1/3] Publish event kytos/topology.current for topology reconciliation Subscribed to event kytos/topology.get to publish the current topology --- main.py | 11 +++++++++++ tests/integration/test_main.py | 1 + tests/unit/test_main.py | 12 ++++++++++++ 3 files changed, 24 insertions(+) diff --git a/main.py b/main.py index b73e851..c0f2275 100644 --- a/main.py +++ b/main.py @@ -538,6 +538,17 @@ def delete_link_metadata(self, link_id, key): self.notify_metadata_changes(link, 'removed') return jsonify("Operation successful"), 200 + def notify_current_topology(self) -> None: + """Notify current topology.""" + name = "kytos/topology.current" + event = KytosEvent(name=name, content={"topology": + self._get_topology()}) + self.controller.buffers.app.put(event) + + @listen_to("kytos/topology.get") + def on_get_topology(self, event) -> None: + self.notify_current_topology() + @listen_to("kytos/.*.liveness.(up|down)") def on_link_liveness_status(self, event) -> None: """Handle link liveness up|down status event.""" diff --git a/tests/integration/test_main.py b/tests/integration/test_main.py index 15f69b0..5eba8f0 100644 --- a/tests/integration/test_main.py +++ b/tests/integration/test_main.py @@ -143,6 +143,7 @@ def test_get_event_listeners(self): 'kytos/.*.link_available_tags', 'kytos/.*.liveness.(up|down)', 'kytos/.*.liveness.disabled', + 'kytos/topology.get', '.*.topo_controller.upsert_switch', '.*.of_lldp.network_status.updated', '.*.interface.is.nni', diff --git a/tests/unit/test_main.py b/tests/unit/test_main.py index 3199fe2..fad25e4 100644 --- a/tests/unit/test_main.py +++ b/tests/unit/test_main.py @@ -7,6 +7,7 @@ from unittest import TestCase from unittest.mock import MagicMock, create_autospec, patch +from kytos.core import KytosEvent from kytos.core.common import EntityStatus from kytos.core.interface import Interface from kytos.core.link import Link @@ -84,6 +85,7 @@ def test_get_event_listeners(self): '.*.switch.(new|reconnected)', 'kytos/.*.liveness.(up|down)', 'kytos/.*.liveness.disabled', + 'kytos/topology.get', '.*.switch.port.created'] actual_events = self.napp.listeners() self.assertCountEqual(expected_events, actual_events) @@ -1373,6 +1375,16 @@ def test_notify_topology_update(self, *args): mock_event.assert_called() mock_buffers_put.assert_called() + @patch('kytos.core.buffers.KytosEventBuffer.put') + def test_notify_current_topology(self, mock_buffers_put): + """Test notify_current_topology.""" + self.napp.notify_current_topology() + mock_buffers_put.assert_called() + args = mock_buffers_put.call_args + expected_event = args[0][0] + assert expected_event.name == "kytos/topology.current" + assert "topology" in expected_event.content + @patch('napps.kytos.topology.main.KytosEvent') @patch('kytos.core.buffers.KytosEventBuffer.put') def test_notify_link_status_change(self, *args): From 5153d173c2651356fa9dd90920bdb899153489f9 Mon Sep 17 00:00:00 2001 From: Vinicius Arcanjo Date: Wed, 2 Nov 2022 17:50:37 -0300 Subject: [PATCH 2/3] Updated CHANGELOG.rst and README.rst --- CHANGELOG.rst | 3 +++ README.rst | 15 +++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index bbe5f16..199be40 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -6,8 +6,11 @@ All notable changes to the ``topology`` project will be documented in this file. [UNRELEASED] - Under development ******************************** + Added ===== +- Publish event ``kytos/topology.current`` for topology reconciliation +- Subscribed to event ``kytos/topology.get`` to publish the current topology Changed ======= diff --git a/README.rst b/README.rst index 6cdd572..aa16dce 100644 --- a/README.rst +++ b/README.rst @@ -62,6 +62,7 @@ Subscribed - ``kytos/maintenance.end_switch`` - ``kytos/.*.liveness.(up|down)`` - ``kytos/.*.liveness.disabled`` +- ``kytos/topology.get`` Published @@ -91,6 +92,20 @@ topology. Content: +.. code-block:: python3 + + { + 'topology': + } + +kytos/topology.current +~~~~~~~~~~~~~~~~~~~~~~ + +Event reporting the current topology, this is meant as a broadcast response when +a subscriber needs it for reconciliation. + +Content: + .. code-block:: python3 { From 6e0dacd1c69e5ec7f34569ceca132a5031a75fa0 Mon Sep 17 00:00:00 2001 From: Vinicius Arcanjo Date: Wed, 2 Nov 2022 18:28:11 -0300 Subject: [PATCH 3/3] Linter fixes --- main.py | 3 ++- tests/unit/test_main.py | 1 - 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index c0f2275..f2a7d8a 100644 --- a/main.py +++ b/main.py @@ -546,7 +546,8 @@ def notify_current_topology(self) -> None: self.controller.buffers.app.put(event) @listen_to("kytos/topology.get") - def on_get_topology(self, event) -> None: + def on_get_topology(self, _event) -> None: + """Handle kytos/topology.get.""" self.notify_current_topology() @listen_to("kytos/.*.liveness.(up|down)") diff --git a/tests/unit/test_main.py b/tests/unit/test_main.py index fad25e4..cf0bf48 100644 --- a/tests/unit/test_main.py +++ b/tests/unit/test_main.py @@ -7,7 +7,6 @@ from unittest import TestCase from unittest.mock import MagicMock, create_autospec, patch -from kytos.core import KytosEvent from kytos.core.common import EntityStatus from kytos.core.interface import Interface from kytos.core.link import Link