Skip to content

Commit

Permalink
[config][cbf] Added config commands for CBF (sonic-net#1799)
Browse files Browse the repository at this point in the history
 Added reload and clear commands for CBF.
 Added CBF reload UT
  • Loading branch information
Cosmin-Jinga-MS authored Oct 28, 2021
1 parent 02ce8d6 commit ea4a730
Show file tree
Hide file tree
Showing 12 changed files with 709 additions and 2 deletions.
98 changes: 98 additions & 0 deletions config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,27 @@ def _change_hostname(hostname):
clicommon.run_command(r'sed -i "/\s{}$/d" /etc/hosts'.format(current_hostname), display_cmd=True)
clicommon.run_command('echo "127.0.0.1 {}" >> /etc/hosts'.format(hostname), display_cmd=True)

def _clear_cbf():
CBF_TABLE_NAMES = [
'DSCP_TO_FC_MAP',
'EXP_TO_FC_MAP']

namespace_list = [DEFAULT_NAMESPACE]
if multi_asic.get_num_asics() > 1:
namespace_list = multi_asic.get_namespaces_from_linux()

for ns in namespace_list:
if ns is DEFAULT_NAMESPACE:
config_db = ConfigDBConnector()
else:
config_db = ConfigDBConnector(
use_unix_socket_path=True, namespace=ns
)
config_db.connect()
for cbf_table in CBF_TABLE_NAMES:
config_db.delete_table(cbf_table)


def _clear_qos():
QOS_TABLE_NAMES = [
'TC_TO_PRIORITY_GROUP_MAP',
Expand Down Expand Up @@ -2061,6 +2082,83 @@ def start_default(verbose):

clicommon.run_command(cmd, display_cmd=verbose)

#
# 'cbf' group ('config cbf ...')
#
@config.group(cls=clicommon.AbbreviationGroup)
@click.pass_context
def cbf(ctx):
"""CBF-related configuration tasks"""
pass

@cbf.command('clear')
def clear():
"""Clear CBF configuration"""
log.log_info("'cbf clear' executing...")
_clear_cbf()

@cbf.command('reload')
@click.pass_context
@click.option(
'--json-data', type=click.STRING,
help="json string with additional data, valid with --dry-run option"
)
@click.option(
'--dry_run', type=click.STRING,
help="Dry run, writes config to the given file"
)
def reload(ctx, dry_run, json_data):
"""Reload CBF configuration"""
log.log_info("'cbf reload' executing...")
_clear_cbf()

_, hwsku_path = device_info.get_paths_to_platform_and_hwsku_dirs()
sonic_version_file = device_info.get_sonic_version_file()
from_db = "-d --write-to-db"
if dry_run:
from_db = "--additional-data \'{}\'".format(json_data) if json_data else ""

namespace_list = [DEFAULT_NAMESPACE]
if multi_asic.get_num_asics() > 1:
namespace_list = multi_asic.get_namespaces_from_linux()

for ns in namespace_list:
if ns is DEFAULT_NAMESPACE:
asic_id_suffix = ""
config_db = ConfigDBConnector()
else:
asic_id = multi_asic.get_asic_id_from_name(ns)
if asic_id is None:
click.secho(
"Command 'cbf reload' failed with invalid namespace '{}'".
format(ns),
fg="yellow"
)
raise click.Abort()
asic_id_suffix = str(asic_id)

config_db = ConfigDBConnector(
use_unix_socket_path=True, namespace=ns
)

config_db.connect()

cbf_template_file = os.path.join(hwsku_path, asic_id_suffix, "cbf.json.j2")
if os.path.isfile(cbf_template_file):
cmd_ns = "" if ns is DEFAULT_NAMESPACE else "-n {}".format(ns)
fname = "{}{}".format(dry_run, asic_id_suffix) if dry_run else "config-db"
command = "{} {} {} -t {},{} -y {}".format(
SONIC_CFGGEN_PATH, cmd_ns, from_db,
cbf_template_file, fname, sonic_version_file
)

# Apply the configuration
clicommon.run_command(command, display_cmd=True)
else:
click.secho("CBF definition template not found at {}".format(
cbf_template_file
), fg="yellow")

#
# 'qos' group ('config qos ...')
#
Expand Down
1 change: 1 addition & 0 deletions tests/cbf_config_input/0/cbf.json.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{%- include 'cbf_config.j2' %}
82 changes: 82 additions & 0 deletions tests/cbf_config_input/0/cbf_config.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
{
"DSCP_TO_FC_MAP": {
"AZURE": {
"0" : "1",
"1" : "1",
"2" : "1",
"3" : "3",
"4" : "4",
"5" : "2",
"6" : "1",
"7" : "1",
"8" : "0",
"9" : "1",
"10": "1",
"11": "1",
"12": "1",
"13": "1",
"14": "1",
"15": "1",
"16": "1",
"17": "1",
"18": "1",
"19": "1",
"20": "1",
"21": "1",
"22": "1",
"23": "1",
"24": "1",
"25": "1",
"26": "1",
"27": "1",
"28": "1",
"29": "1",
"30": "1",
"31": "1",
"32": "1",
"33": "1",
"34": "1",
"35": "1",
"36": "1",
"37": "1",
"38": "1",
"39": "1",
"40": "1",
"41": "1",
"42": "1",
"43": "1",
"44": "1",
"45": "1",
"46": "5",
"47": "1",
"48": "6",
"49": "1",
"50": "1",
"51": "1",
"52": "1",
"53": "1",
"54": "1",
"55": "1",
"56": "1",
"57": "1",
"58": "1",
"59": "1",
"60": "1",
"61": "1",
"62": "1",
"63": "1"
}
},
"EXP_TO_FC_MAP": {
"AZURE": {
"0": "0",
"1": "1",
"2": "2",
"3": "3",
"4": "4",
"5": "5",
"6": "6",
"7": "7"
}
}
}
82 changes: 82 additions & 0 deletions tests/cbf_config_input/0/config_cbf.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
{
"DSCP_TO_FC_MAP": {
"AZURE": {
"0" : "1",
"1" : "1",
"2" : "1",
"3" : "3",
"4" : "4",
"5" : "2",
"6" : "1",
"7" : "1",
"8" : "0",
"9" : "1",
"10": "1",
"11": "1",
"12": "1",
"13": "1",
"14": "1",
"15": "1",
"16": "1",
"17": "1",
"18": "1",
"19": "1",
"20": "1",
"21": "1",
"22": "1",
"23": "1",
"24": "1",
"25": "1",
"26": "1",
"27": "1",
"28": "1",
"29": "1",
"30": "1",
"31": "1",
"32": "1",
"33": "1",
"34": "1",
"35": "1",
"36": "1",
"37": "1",
"38": "1",
"39": "1",
"40": "1",
"41": "1",
"42": "1",
"43": "1",
"44": "1",
"45": "1",
"46": "5",
"47": "1",
"48": "6",
"49": "1",
"50": "1",
"51": "1",
"52": "1",
"53": "1",
"54": "1",
"55": "1",
"56": "1",
"57": "1",
"58": "1",
"59": "1",
"60": "1",
"61": "1",
"62": "1",
"63": "1"
}
},
"EXP_TO_FC_MAP": {
"AZURE": {
"0": "0",
"1": "1",
"2": "2",
"3": "3",
"4": "4",
"5": "5",
"6": "6",
"7": "7"
}
}
}
1 change: 1 addition & 0 deletions tests/cbf_config_input/1/cbf.json.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{%- include 'cbf_config.j2' %}
82 changes: 82 additions & 0 deletions tests/cbf_config_input/1/cbf_config.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
{
"DSCP_TO_FC_MAP": {
"AZURE": {
"0" : "1",
"1" : "1",
"2" : "1",
"3" : "3",
"4" : "4",
"5" : "2",
"6" : "1",
"7" : "1",
"8" : "0",
"9" : "1",
"10": "1",
"11": "1",
"12": "1",
"13": "1",
"14": "1",
"15": "1",
"16": "1",
"17": "1",
"18": "1",
"19": "1",
"20": "1",
"21": "1",
"22": "1",
"23": "1",
"24": "1",
"25": "1",
"26": "1",
"27": "1",
"28": "1",
"29": "1",
"30": "1",
"31": "1",
"32": "1",
"33": "1",
"34": "1",
"35": "1",
"36": "1",
"37": "1",
"38": "1",
"39": "1",
"40": "1",
"41": "1",
"42": "1",
"43": "1",
"44": "1",
"45": "1",
"46": "5",
"47": "1",
"48": "6",
"49": "1",
"50": "1",
"51": "1",
"52": "1",
"53": "1",
"54": "1",
"55": "1",
"56": "1",
"57": "1",
"58": "1",
"59": "1",
"60": "1",
"61": "1",
"62": "1",
"63": "1"
}
},
"EXP_TO_FC_MAP": {
"AZURE": {
"0": "0",
"1": "1",
"2": "2",
"3": "3",
"4": "4",
"5": "5",
"6": "6",
"7": "7"
}
}
}
Loading

0 comments on commit ea4a730

Please sign in to comment.