Skip to content
This repository has been archived by the owner on Apr 22, 2024. It is now read-only.

Create a new event to notify when new flow stats are available #123

Merged
merged 3 commits into from
May 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ def handle_stats_reply(self, event):
if msg.body_type == StatsType.OFPST_FLOW:
switch.flows = [Flow01.from_of_flow_stats(f, switch)
for f in msg.body]
event_raw = KytosEvent(
name='kytos/of_core.flow_stats.received',
content={'switch': switch})
self.controller.buffers.app.put(event_raw)
elif msg.body_type == StatsType.OFPST_PORT:
port_stats = [of_port_stats for of_port_stats in msg.body]
port_stats_event = KytosEvent(
Expand Down Expand Up @@ -121,6 +125,17 @@ def handle_features_reply(self, event):
content={'switch': switch})
self.controller.buffers.app.put(event_raw)

@listen_to('kytos/of_core.handshake.completed')
def on_handshake_completed_request_flow_list(self, event):
"""Request an flow list right after the handshake is completed.

Args:
event (KytosEvent): Event with the switch' handshake completed
"""
switch = event.content['switch']
if switch.is_enabled():
self._request_flow_list(switch)

@listen_to('kytos/of_core.v0x04.messages.in.ofpt_multipart_reply')
def handle_multipart_reply(self, event):
"""Handle multipart replies for v0x04 switches.
Expand Down Expand Up @@ -153,6 +168,10 @@ def _handle_multipart_flow_stats(self, reply, switch):
all_flows.extend(flows)
if reply.flags.value % 2 == 0: # Last bit means more replies
self._update_switch_flows(switch)
event_raw = KytosEvent(
name='kytos/of_core.flow_stats.received',
content={'switch': switch})
self.controller.buffers.app.put(event_raw)

def _handle_multipart_port_stats(self, reply, switch):
"""Emit an event about new port stats."""
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,24 @@ def test_request_flow_list(self, *args):
mock_update_flow_list_v0x04.assert_called_with(self.napp.controller,
self.switch_v0x04)

@patch('napps.kytos.of_core.v0x04.utils.update_flow_list')
@patch('napps.kytos.of_core.v0x01.utils.update_flow_list')
def test_on_handshake_completed_request_flow_list(self, *args):
"""Test request flow list."""
(mock_update_flow_list_v0x01, mock_update_flow_list_v0x04) = args
mock_update_flow_list_v0x04.return_value = "ABC"
name = 'kytos/of_core.handshake.completed'
content = {"switch": self.switch_v0x01}
event = get_kytos_event_mock(name=name, content=content)
self.napp.on_handshake_completed_request_flow_list(event)
mock_update_flow_list_v0x01.assert_called_with(self.napp.controller,
self.switch_v0x01)
content = {"switch": self.switch_v0x04}
event = get_kytos_event_mock(name=name, content=content)
self.napp.on_handshake_completed_request_flow_list(event)
mock_update_flow_list_v0x04.assert_called_with(self.napp.controller,
self.switch_v0x04)

@patch('napps.kytos.of_core.v0x01.flow.Flow.from_of_flow_stats')
def test_handle_stats_reply(self, mock_from_of_flow_stats_v0x01):
"""Test handle stats reply."""
Expand Down