Skip to content

Commit

Permalink
adjusting code to make easy to understand some EVC attrs and re-intro…
Browse files Browse the repository at this point in the history
…ducing try_setup_failover when EVC gets redeployed
  • Loading branch information
Italo Valcy committed Mar 8, 2024
1 parent 272062d commit 9fb511b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
29 changes: 16 additions & 13 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,7 @@ def execute_consistency(self):
stored_circuits.pop(circuit.id, None)
if self.should_be_checked(circuit):
circuits_to_check.append(circuit)
# setup failover_path whenever possible
if getattr(circuit, "failover_path", None):
continue
affected_at = getattr(circuit, "affected_by_link_at", None)
if (
not affected_at or
(now() - affected_at).seconds >= settings.DEPLOY_EVCS_INTERVAL
):
with circuit.lock:
circuit.setup_failover_path()
circuit.try_setup_failover_path()
circuits_checked = EVCDeploy.check_list_traces(circuits_to_check)
for circuit in circuits_to_check:
is_checked = circuits_checked.get(circuit.id)
Expand Down Expand Up @@ -839,7 +830,7 @@ def handle_link_down(self, event): # pylint: disable=too-many-branches
# if there is no failover path, handles link down the
# tradditional way
if (
not getattr(evc, 'failover_path', None) or
not evc.failover_path or
evc.is_failover_path_affected_by_link(link)
):
evcs_normal.append(evc)
Expand Down Expand Up @@ -937,6 +928,18 @@ def handle_evc_affected_by_link_down(self, event):
emit_event(self.controller, event_name,
content=map_evc_event_content(evc))

@listen_to("kytos/mef_eline.(redeployed_link_(up|down)|deployed)")
def on_evc_deployed(self, event):
"""Handle EVC deployed|redeployed_link_down."""
self.handle_evc_deployed(event)

def handle_evc_deployed(self, event):
"""Setup failover path on evc deployed."""
evc = self.circuits.get(event.content["evc_id"])
if not evc:
return
evc.try_setup_failover_path()

@listen_to("kytos/mef_eline.cleanup_evcs_old_path")
def on_cleanup_evcs_old_path(self, event):
"""Handle cleanup evcs old path."""
Expand All @@ -946,10 +949,10 @@ def handle_cleanup_evcs_old_path(self, event):
"""Handle cleanup evcs old path."""
evcs = event.content.get("evcs", [])
for evc in evcs:
if not hasattr(evc, 'old_path'):
if not evc.old_path:
continue
evc.remove_path_flows(evc.old_path)
delattr(evc, "old_path")
evc.old_path = Path([])

@listen_to("kytos/topology.topology_loaded")
def on_topology_loaded(self, event): # pylint: disable=unused-argument
Expand Down
10 changes: 10 additions & 0 deletions models/evc.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ def __init__(self, controller, **kwargs):
self.current_links_cache = set()
self.primary_links_cache = set()
self.backup_links_cache = set()
self.affected_by_link_at = get_time("0001-01-01T00:00:00")
self.old_path = Path([])

self.lock = Lock()

Expand Down Expand Up @@ -872,6 +874,14 @@ def deploy_to_path(self, path=None): # pylint: disable=too-many-branches
log.info(f"{self} was deployed.")
return True

def try_setup_failover_path(self, wait=settings.DEPLOY_EVCS_INTERVAL):
"""Try setup failover_path whenever possible."""
if self.failover_path:
return
if (now() - self.affected_by_link_at).seconds >= wait:
with self.lock:
self.setup_failover_path()

def setup_failover_path(self):
"""Install flows for the failover path of this EVC.
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 @@ -2088,7 +2088,7 @@ def test_cleanup_evcs_old_path(self):
"""Test handle_cleanup_evcs_old_path method."""
evc1 = create_autospec(EVC, id="1", old_path=["1"])
evc2 = create_autospec(EVC, id="2", old_path=["2"])
evc3 = create_autospec(EVC, id="3")
evc3 = create_autospec(EVC, id="3", old_path=[])

event = KytosEvent(name="e1", content={"evcs": [evc1, evc2, evc3]})
self.napp.handle_cleanup_evcs_old_path(event)
Expand Down

0 comments on commit 9fb511b

Please sign in to comment.