Skip to content

Commit

Permalink
Refactoring of PortState
Browse files Browse the repository at this point in the history
  • Loading branch information
vg12345 committed Sep 5, 2024
1 parent 6841d88 commit 0508ad7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,5 @@ class PDRConstants():
ISSUE_INIT = "init"
ISSUE_LINK_DOWN = "link_down"

STATE_NORMAL = "normal"
STATE_ISOLATED = "isolated"
STATE_TREATED = "treated"

# intervals in seconds for testing ber values and corresponding thresholds
BER_THRESHOLDS_INTERVALS = [(125 * 60, 3), (12 * 60, 2.88)]
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
import configparser
import pandas as pd
from exclude_list import ExcludeList
from pdr_algorithm import PortData, PortState, PDRAlgorithm
from pdr_algorithm import PortData, IsolatedPort, PDRAlgorithm

from constants import PDRConstants as Constants
from ufm_communication_mgr import UFMCommunicator
# should actually be persistent and thread safe dictionary pf PortStates
# should actually be persistent and thread safe dictionary pf IsolatedPorts

#pylint: disable=too-many-instance-attributes
class IsolationMgr:
Expand All @@ -29,7 +29,7 @@ class IsolationMgr:

def __init__(self, ufm_client: UFMCommunicator, logger):
self.ufm_client = ufm_client
# {port_name: PortState}
# {port_name: IsolatedPort}
self.ports_states = {}
# {port_name: telemetry_data}
self.ports_data = {}
Expand Down Expand Up @@ -86,8 +86,8 @@ def eval_isolation(self, port_name, cause):
return
port_state = self.ports_states.get(port_name)
if not port_state:
self.ports_states[port_name] = PortState(port_name)
self.ports_states[port_name].update(Constants.STATE_ISOLATED, cause)
self.ports_states[port_name] = IsolatedPort(port_name)
self.ports_states[port_name].update(cause)

log_message = f"Isolated port: {port_name} cause: {cause}. dry_run: {self.dry_run}"
self.logger.warning(log_message)
Expand Down Expand Up @@ -203,21 +203,6 @@ def update_ports_data(self):
ports_updated = True
return ports_updated

def set_ports_as_treated(self, ports_dict):
"""
Sets the state of the specified ports as treated.
Args:
ports_dict (dict): A dictionary containing the ports and their desired state.
Returns:
None
"""
for port, state in ports_dict.items():
port_state = self.ports_states.get(port)
if port_state and state == Constants.STATE_TREATED:
port_state.state = state

def get_isolation_state(self):
"""
Retrieves the isolation state of the ports.
Expand All @@ -237,8 +222,8 @@ def get_isolation_state(self):
self.ufm_latest_isolation_state = isolated_ports
for port in isolated_ports:
if not self.ports_states.get(port):
port_state = PortState(port)
port_state.update(Constants.STATE_ISOLATED, Constants.ISSUE_OONOC)
port_state = IsolatedPort(port)
port_state.update(Constants.ISSUE_OONOC)
self.ports_states[port] = port_state

def get_requested_guids(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

from constants import PDRConstants as Constants
from ufm_communication_mgr import UFMCommunicator
# should actually be persistent and thread safe dictionary pf PortStates
# should actually be persistent and thread safe dictionary pf IsolatedPorts

#pylint: disable=too-many-instance-attributes
class PortData:
Expand Down Expand Up @@ -53,62 +53,50 @@ def __init__(self, port_name=None, port_num=None, peer=None, node_type=None, act



class PortState:
class IsolatedPort:
"""
Represents the state of a port.
Represents the isolated port info.
Attributes:
name (str): The name of the port.
state (str): The current state of the port (isolated or treated).
cause (str): The cause of the state change (oonoc, pdr, ber).
maybe_fixed (bool): Indicates if the port may have been fixed.
change_time (datetime): The time of the last state change.
"""

def __init__(self, name):
"""
Initialize a new instance of the PortState class.
Initialize a new instance of the IsolatedPort class.
:param name: The name of the port.
"""
self.name = name
self.state = Constants.STATE_NORMAL # isolated | treated
self.cause = Constants.ISSUE_INIT # oonoc, pdr, ber
self.maybe_fixed = False
self.change_time = datetime.now()

def update(self, state, cause):
def update(self, cause):
"""
Update the state and cause of the port.
Update the cause of the isolation.
:param state: The new state of the port.
:param cause: The cause of the state change.
:param cause: The cause of the isolation.
"""
self.state = state
self.cause = cause
self.change_time = datetime.now()

def get_cause(self):
"""
Get the cause of the state change.
Get the cause of the isolation.
:return: The cause of the state change.
:return: The cause of the isolation.
"""
return self.cause

def get_state(self):
"""
Get the current state of the port.
:return: The current state of the port.
"""
return self.state

def get_change_time(self):
"""
Get the time of the last state change.
Get the time of the last change.
:return: The time of the last state change.
:return: The time of the last change.
"""
return self.change_time

Expand Down Expand Up @@ -454,12 +442,11 @@ def check_deisolation_conditions(self, port_state):
Function doesn't perform deisolation itself, just checks deisolation conditions only
Return True if given port should be deisolated
"""
state = port_state.get_state()
cause = port_state.get_cause()
# EZ: it is a state that say that some maintenance was done to the link
# so need to re-evaluate if to return it to service
# Deal with ports that with either cause = oonoc or fixed
if not (self.automatic_deisolate or cause == Constants.ISSUE_OONOC or state == Constants.STATE_TREATED):
if not (self.automatic_deisolate or cause == Constants.ISSUE_OONOC):
return False

# We don't deisolate those out of NOC
Expand All @@ -478,7 +465,7 @@ def check_deisolation_conditions(self, port_state):
symbol_ber_rate = self.calc_ber_rates(port_name, port_obj.active_speed, port_obj.port_width, self.max_ber_wait_time + 1)
if symbol_ber_rate and symbol_ber_rate > self.max_ber_threshold:
cause = Constants.ISSUE_BER
port_state.update(Constants.STATE_ISOLATED, cause)
port_state.update(cause)
return False

return True
Expand Down

0 comments on commit 0508ad7

Please sign in to comment.