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

Adding log for number of flows #194

Merged
merged 7 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ Added
- Added query parameter filter for `cookie_range` to the endpoint ``GET v2/stored_flows``.
- Added query parameter filter for `state` to the endpoint ``GET v2/stored_flows``.

Changed
=======
- Added flow quantity as ``total_length`` in logs for flow deletion and adittion through request and event.

Fixed
=====
- Fixed handling ``OFPT_ERROR`` correctly when OF negotiation fails
Expand Down
10 changes: 9 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -580,9 +580,13 @@ def handle_flows_install_delete(self, event):
try:
dpid = event.content["dpid"]
flow_dict = event.content["flow_dict"]
flows = flow_dict["flows"]
except KeyError as error:
log.error("Error getting fields to install or remove " f"Flows: {error}")
return
except TypeError as err:
log.error(f"{str(err)} for flow_dict {flow_dict}")
return

if event.name.endswith("install"):
command = "add"
Expand All @@ -594,7 +598,7 @@ def handle_flows_install_delete(self, event):
return

try:
validate_cookies_and_masks(flow_dict, command)
validate_cookies_and_masks(flows, command)
except ValueError as exc:
log.error(str(exc))
return
Expand All @@ -603,6 +607,10 @@ def handle_flows_install_delete(self, event):
return

force = bool(event.content.get("force", False))
if not flow_dict["flows"]:
viniarck marked this conversation as resolved.
Show resolved Hide resolved
log.error(f"Error, empty list of flows received. {flow_dict}")
return

viniarck marked this conversation as resolved.
Show resolved Hide resolved
switch = self.controller.get_switch_by_dpid(dpid)
flows_to_log_info(
f"Send FlowMod from KytosEvent dpid: {dpid}, command: {command}, "
Expand Down
6 changes: 4 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ class Test(TestCommand):

def run(self):
"""Run tests."""
cmd = "python3 -m pytest tests/ %s" % self.get_args()
cmd = "python3 -m pytest tests/ --cov-report term-missing"
Alopalao marked this conversation as resolved.
Show resolved Hide resolved
cmd += f" {self.get_args()}"
try:
check_call(cmd, shell=True)
except CalledProcessError as exc:
Expand All @@ -115,7 +116,8 @@ class TestCoverage(Test):

def run(self):
"""Run tests quietly and display coverage report."""
cmd = "python3 -m pytest --cov=. tests/ %s" % self.get_args()
cmd = "python3 -m pytest --cov=. tests/ --cov-report term-missing"
cmd += f" {self.get_args()}"
try:
check_call(cmd, shell=True)
except CalledProcessError as exc:
Expand Down
10 changes: 9 additions & 1 deletion tests/unit/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ def test_handle_flows_install_delete_fail(self, *args):
# missing cookie_mask
event = get_kytos_event_mock(
name="kytos.flow_manager.flows.delete",
content={"dpid": dpid, "flow_dict": [{"cookie": 1}]},
content={"dpid": dpid, "flow_dict": {"cookie": 1, "flows": []}},
)
self.napp.handle_flows_install_delete(event)
assert mock_log.error.call_count == 3
Expand All @@ -663,6 +663,14 @@ def test_handle_flows_install_delete_fail(self, *args):
self.napp.handle_flows_install_delete(event)
assert mock_log.error.call_count == 4

# empty flow list
event = get_kytos_event_mock(
name="kytos.flow_manager.flows.delete",
content={"dpid": dpid, "flow_dict": {"flows": []}},
)
self.napp.handle_flows_install_delete(event)
assert mock_log.error.call_count == 5

# install_flow exceptions
event = get_kytos_event_mock(
name="kytos.flow_manager.flows.install",
Expand Down
5 changes: 4 additions & 1 deletion utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,14 @@ def validate_cookies_del(flows: list[dict]) -> None:

def flows_to_log_info(message: str, flow_dict: dict[str, list]) -> None:
"""Log flows, maximun flows in a log is 200"""
length_msg = f"total_length: {len(flow_dict['flows'])}, "
maximun = 200
flows_n = len(flow_dict["flows"])
i, j = 0, maximun
while flow_dict["flows"][i:j]:
log.info(
f"{message} flows[{i}, {(j if j < flows_n else flows_n)}]: {flow_dict['flows'][i:j]}"
f"{message}{length_msg} flows[{i}, {(j if j < flows_n else flows_n)}]:"
f" {flow_dict['flows'][i:j]}"
)
i, j = i + maximun, j + maximun
length_msg = ""