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

[write_standby] update write_standby.py script #11650

Merged
merged 5 commits into from
Aug 9, 2022
Merged
Changes from 4 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
41 changes: 29 additions & 12 deletions files/scripts/write_standby.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python3

import argparse
import time

from sonic_py_common import logger as log
Expand All @@ -19,11 +20,12 @@ class MuxStateWriter(object):
Class used to write standby mux state to APP DB
"""

def __init__(self):
def __init__(self, state):
self.config_db_connector = None
self.appl_db_connector = None
self.state_db_connector = None
self.asic_db_connector = None
self.default_state = state

@property
def config_db(self):
Expand Down Expand Up @@ -96,14 +98,25 @@ def is_warmrestart(self):

return status and value == 'true'

def get_auto_mux_intfs(self):
def get_all_mux_intfs_modes(self):
"""
Returns a list of all mux cable interfaces that are configured to auto-switch
Returns a list of all mux cable interfaces, with suggested modes
Setting mux initial modes is crucial to kick off the statemachines,
have to set the modes for all mux/gRPC ports.
"""
intf_modes = {}
all_intfs = self.config_db.get_table('MUX_CABLE')
auto_intfs = [intf for intf, status in all_intfs.items()
if status['state'].lower() == 'auto']
return auto_intfs
for intf, status in all_intfs.items():
state = status['state'].lower()
if state in ['active', 'standby']:
intf_modes[intf] = state
elif state in ['auto', 'manual']:
if ('soc_ipv4' in status or 'soc_ipv6' in status or
('cable_type' in status and status['cable_type'] == 'active-active')):
intf_modes[intf] = 'active'
else:
intf_modes[intf] = self.default_state
return intf_modes

def tunnel_exists(self):
"""
Expand Down Expand Up @@ -144,19 +157,23 @@ def apply_mux_config(self):
logger.log_warning("Skip setting mux state due to ongoing warmrestart.")
return

intfs = self.get_auto_mux_intfs()
state = 'standby'
modes = self.get_all_mux_intfs_modes()
if self.wait_for_tunnel():
logger.log_warning("Applying {} state to interfaces {}".format(state, intfs))
logger.log_warning("Applying state to interfaces {}".format(modes))
producer_state_table = ProducerStateTable(self.appl_db, 'MUX_CABLE_TABLE')
fvs = create_fvs(state=state)

for intf in intfs:
for intf, state in modes.items():
fvs = create_fvs(state=state)
producer_state_table.set(intf, fvs)
else:
logger.log_error("Timed out waiting for tunnel {}, mux state will not be written".format(self.tunnel_name))


if __name__ == '__main__':
mux_writer = MuxStateWriter()
parser = argparse.ArgumentParser(description='Write initial mux state')
parser.add_argument('-s', '--state',
help='state: intial state for "auto" and/or "manual" config, default "standby"',
type=str, required=False, default='standby')
args = parser.parse_args()
yxieca marked this conversation as resolved.
Show resolved Hide resolved
mux_writer = MuxStateWriter(state=args.state)
mux_writer.apply_mux_config()