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 { diff --git a/main.py b/main.py index b73e851..f2a7d8a 100644 --- a/main.py +++ b/main.py @@ -538,6 +538,18 @@ 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: + """Handle kytos/topology.get.""" + 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..cf0bf48 100644 --- a/tests/unit/test_main.py +++ b/tests/unit/test_main.py @@ -84,6 +84,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 +1374,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):