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

Added config commands for CBF #1799

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
98 changes: 98 additions & 0 deletions config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,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',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

elsewhere it's DSCP_TO_TC_MAP. Please update.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other updates were incorrect. CBF has to have its own DSCP mapping, I've reverted the names to DSCP_TO_FC_MAP in the other occurrences too.

'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 @@ -2023,6 +2044,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/cbf.json.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{%- include 'cbf_config.j2' %}
12 changes: 12 additions & 0 deletions tests/cbf_config_input/cbf_config.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
Copy link
Contributor

@smaheshm smaheshm Oct 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no variables here to use a template. In any case the file here should be the one that will be used when generating the config using "sonic_cfggen" command. How will these entries be created in production switch. Is there going to be a new template or are you going to use the existing qos templates.

eg:
https://github.com/Azure/sonic-buildimage/blob/master/files/build_templates/qos_config.j2
https://github.com/Azure/sonic-buildimage/blob/master/files/build_templates/buffers_config.j2

There should be a PR for those changes if not already there. Copy the same template (that's used in prod switches) here for testing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes there is a separate PR for the default CBF template config. It's been updated as well with the new config now found in the UTs too.
sonic-net/sonic-buildimage#8689

"DSCP_TO_FC_MAP": {
"default": {
"0": "0"
}
},
"EXP_TO_FC_MAP": {
"default": {
"0": "0"
}
}
}
12 changes: 12 additions & 0 deletions tests/cbf_config_input/config_cbf.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"DSCP_TO_FC_MAP": {
"default": {
"0": "0"
}
},
"EXP_TO_FC_MAP": {
"default": {
"0": "0"
}
}
}
8 changes: 8 additions & 0 deletions tests/cbf_config_input/sonic_version.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
build_version: 'master.487-a98cf221'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

may be we can reuse the exsiting one.. CBF, QoS are close related anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reused the qos one

debian_version: '10.6'
kernel_version: '4.19.0-9-2-amd64'
asic_type: broadcom
commit_id: 'a98cf221'
build_date: Thu Nov 12 12:21:45 UTC 2020
build_number: 487
built_by: johnar@jenkins-worker-8
40 changes: 40 additions & 0 deletions tests/config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,46 @@ def teardown_class(cls):
print("TEARDOWN")


class TestConfigCbf(object):
@classmethod
def setup_class(cls):
print("SETUP")
os.environ['UTILITIES_UNIT_TESTING'] = "2"
import config.main
importlib.reload(config.main)

def test_cbf_reload_single(
self, get_cmd_module, setup_cbf_mock_apis,
setup_single_broadcom_asic
):
(config, show) = get_cmd_module
runner = CliRunner()
output_file = os.path.join(os.sep, "tmp", "cbf_config_output.json")
print("Saving output in {}".format(output_file))
try:
os.remove(output_file)
except OSError:
pass
result = runner.invoke(
config.config.commands["cbf"],
["reload", "--dry_run", output_file]
)
print(result.exit_code)
print(result.output)
assert result.exit_code == 0

cwd = os.path.dirname(os.path.realpath(__file__))
expected_result = os.path.join(
cwd, "cbf_config_input", "config_cbf.json"
)
assert filecmp.cmp(output_file, expected_result, shallow=False)

@classmethod
def teardown_class(cls):
print("TEARDOWN")
os.environ['UTILITIES_UNIT_TESTING'] = "0"


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this feature will be used in WAN there's a high chance it will be multi ASIC platform. Please add test case for multi ASIC as well. You already added the code to generate, just need to add test cases. It can be the same pattern as QoS tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added the multi asic UT

class TestConfigQos(object):
@classmethod
def setup_class(cls):
Expand Down
12 changes: 12 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@ def set_mock_apis():
config.asic_type = mock.MagicMock(return_value="broadcom")
config._get_device_type = mock.MagicMock(return_value="ToRRouter")

@pytest.fixture
def setup_cbf_mock_apis():
cwd = os.path.dirname(os.path.realpath(__file__))
device_info.get_paths_to_platform_and_hwsku_dirs = mock.MagicMock(
return_value=(
os.path.join(cwd, "."), os.path.join(cwd, "cbf_config_input")
)
)
device_info.get_sonic_version_file = mock.MagicMock(
return_value=os.path.join(cwd, "cbf_config_input/sonic_version.yml")
)

@pytest.fixture
def setup_qos_mock_apis():
cwd = os.path.dirname(os.path.realpath(__file__))
Expand Down