Skip to content

Commit

Permalink
[minigraph][port_config] Consume port_config.json while reloading min…
Browse files Browse the repository at this point in the history
…igraph (#1725)

* [minigraph][port_config] Consume port_config.json while reloading minigraph (#1705)
* Fix build issue for 201911

Signed-off-by: Jing Kan jika@microsoft.com
  • Loading branch information
Blueve authored Jul 26, 2021
1 parent e840c42 commit b566591
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@
config_db = None
multi_asic_cfgdb = None

# Read given JSON file
def read_json_file(fileName):
try:
with open(fileName) as f:
result = json.load(f)
except Exception as e:
raise Exception(str(e))
return result

class AbbreviationGroup(click.Group):
"""This subclass of click.Group supports abbreviated subgroup/subcommand names
"""
Expand Down Expand Up @@ -992,6 +1001,12 @@ def load_minigraph(no_service_restart):
if os.path.isfile('/etc/sonic/acl.json'):
run_command("acl-loader update full /etc/sonic/acl.json", display_cmd=True)

# Load port_config.json
try:
load_port_config(config_db, '/etc/sonic/port_config.json')
except Exception as e:
click.secho("Failed to load port_config.json, Error: {}".format(str(e)), fg='magenta')

# generate QoS and Buffer configs
run_command("config qos reload", display_cmd=True)

Expand All @@ -1014,6 +1029,44 @@ def load_minigraph(no_service_restart):
_restart_services()
click.echo("Please note setting loaded from minigraph will be lost after system reboot. To preserve setting, run `config save`.")

def load_port_config(config_db, port_config_path):
if not os.path.isfile(port_config_path):
return

try:
# Load port_config.json
port_config_input = read_json_file(port_config_path)
except Exception:
raise Exception("Bad format: json file broken")

# Validate if the input is an array
if not isinstance(port_config_input, list):
raise Exception("Bad format: port_config is not an array")

if len(port_config_input) == 0 or 'PORT' not in port_config_input[0]:
raise Exception("Bad format: PORT table not exists")

port_config = port_config_input[0]['PORT']

# Ensure all ports are exist
port_table = {}
for port_name in port_config.keys():
port_entry = config_db.get_entry('PORT', port_name)
if not port_entry:
raise Exception("Port {} is not defined in current device".format(port_name))
port_table[port_name] = port_entry

# Update port state
for port_name in port_config.keys():
if 'admin_status' not in port_config[port_name]:
continue
if 'admin_status' in port_table[port_name]:
if port_table[port_name]['admin_status'] == port_config[port_name]['admin_status']:
continue
run_command('config interface {} {}'.format(
'startup' if port_config[port_name]['admin_status'] == 'up' else 'shutdown',
port_name), display_cmd=True)
return

#
# 'hostname' command
Expand Down

0 comments on commit b566591

Please sign in to comment.