Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add listening to of_core flow_stats.received to run consistency check #125

Merged
merged 6 commits into from
May 26, 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
35 changes: 15 additions & 20 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
from napps.kytos.of_core.flow import FlowFactory

from .exceptions import InvalidCommandError
from .settings import (CONSISTENCY_COOKIE_IGNORED_RANGE, CONSISTENCY_INTERVAL,
CONSISTENCY_TABLE_ID_IGNORED_RANGE, FLOWS_DICT_MAX_SIZE)
from .settings import (CONSISTENCY_COOKIE_IGNORED_RANGE,
CONSISTENCY_TABLE_ID_IGNORED_RANGE,
ENABLE_CONSISTENCY_CHECK, FLOWS_DICT_MAX_SIZE)


def cast_fields(flow_dict):
Expand Down Expand Up @@ -95,8 +96,6 @@ def setup(self):
# 'flow': {flow_dict}}]}}}
self.stored_flows = {}
self.resent_flows = set()
if CONSISTENCY_INTERVAL > 0:
self.execute_as_loop(CONSISTENCY_INTERVAL)

def execute(self):
"""Run once on NApp 'start' or in a loop.
Expand All @@ -106,9 +105,6 @@ def execute(self):
"""
self._load_flows()

if CONSISTENCY_INTERVAL > 0:
self.consistency_check()

def shutdown(self):
"""Shutdown routine of the NApp."""
log.debug("flow-manager stopping")
Expand All @@ -117,7 +113,7 @@ def shutdown(self):
def resend_stored_flows(self, event):
"""Resend stored Flows."""
# if consistency check is enabled, it should take care of this
if CONSISTENCY_INTERVAL >= 0:
if ENABLE_CONSISTENCY_CHECK:
return
switch = event.content['switch']
dpid = str(switch.dpid)
Expand Down Expand Up @@ -167,17 +163,16 @@ def consistency_ignored_check(self, flow):
return True
return False

def consistency_check(self):
"""Check the consistency of flows in each switch."""
switches = self.controller.switches.values()

for switch in switches:
# Check if a dpid is a key in 'stored_flows' dictionary
if switch.is_enabled():
self.check_storehouse_consistency(switch)

if switch.dpid in self.stored_flows:
self.check_switch_consistency(switch)
@listen_to('kytos/of_core.flow_stats.received')
def on_flow_stats_check_consistency(self, event):
"""Check the consistency of a switch upon receiving flow stats."""
if not ENABLE_CONSISTENCY_CHECK:
return
switch = event.content['switch']
if switch.is_enabled():
self.check_storehouse_consistency(switch)
if switch.dpid in self.stored_flows:
self.check_switch_consistency(switch)

def check_switch_consistency(self, switch):
"""Check consistency of installed flows for a specific switch."""
Expand Down Expand Up @@ -214,7 +209,7 @@ def check_storehouse_consistency(self, switch):

for installed_flow in switch.flows:

# Check if the flow are in the ignored flow list
# Check if the flow is in the ignored flow list
if self.consistency_ignored_check(installed_flow):
continue

Expand Down
2 changes: 1 addition & 1 deletion settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
FLOWS_DICT_MAX_SIZE = 10000
# Time (in seconds) to wait retrieve box from storehouse
BOX_RESTORE_TIMER = 0.1
CONSISTENCY_INTERVAL = 60
ENABLE_CONSISTENCY_CHECK = True

# List of flows ignored by the consistency check
# To filter by a cookie or `table_id` use [value]
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ def test_load_flows(self, mock_storehouse):
self.napp._load_flows()
mock_storehouse.assert_called()

@patch("napps.kytos.flow_manager.main.CONSISTENCY_INTERVAL", -1)
@patch("napps.kytos.flow_manager.main.ENABLE_CONSISTENCY_CHECK", False)
@patch("napps.kytos.flow_manager.main.Main._install_flows")
def test_resend_stored_flows(self, mock_install_flows):
"""Test resend stored flows."""
Expand Down