diff --git a/config/main.py b/config/main.py index 7509628a67..8bab5a161e 100644 --- a/config/main.py +++ b/config/main.py @@ -4191,6 +4191,105 @@ def del_user(db, user): click.echo("Restart service snmp failed with error {}".format(e)) raise click.Abort() + +# +# 'bmp' group ('config bmp ...') +# +@config.group() +@clicommon.pass_db +def bmp(db): + """BMP-related configuration""" + pass + + +# +# common function to update bmp config table +# +@clicommon.pass_db +def update_bmp_table(db, table_name, value): + log.log_info(f"'bmp {value} {table_name}' executing...") + bmp_table = db.cfgdb.get_table('BMP') + if not bmp_table: + bmp_table = {'table': {table_name: value}} + else: + bmp_table['table'][table_name] = value + db.cfgdb.mod_entry('BMP', 'table', bmp_table['table']) + + +# +# 'enable' subgroup ('config bmp enable ...') +# +@bmp.group() +@clicommon.pass_db +def enable(db): + """Enable BMP table dump """ + pass + + +# +# 'bgp-neighbor-table' command ('config bmp enable bgp-neighbor-table') +# +@enable.command('bgp-neighbor-table') +@clicommon.pass_db +def enable_bgp_neighbor_table(db): + update_bmp_table('bgp_neighbor_table', 'true') + + +# +# 'bgp-rib-out-table' command ('config bmp enable bgp-rib-out-table') +# +@enable.command('bgp-rib-out-table') +@clicommon.pass_db +def enable_bgp_rib_out_table(db): + update_bmp_table('bgp_rib_out_table', 'true') + + +# +# 'bgp-rib-in-table' command ('config bmp enable bgp-rib-in-table') +# +@enable.command('bgp-rib-in-table') +@clicommon.pass_db +def enable_bgp_rib_in_table(db): + update_bmp_table('bgp_rib_in_table', 'true') + + +# +# 'disable' subgroup ('config bmp disable ...') +# +@bmp.group() +@clicommon.pass_db +def disable(db): + """Disable BMP table dump """ + pass + + +# +# 'bgp-neighbor-table' command ('config bmp disable bgp-neighbor-table') +# +@disable.command('bgp-neighbor-table') +@clicommon.pass_db +def disable_bgp_neighbor_table(db): + update_bmp_table('bgp_neighbor_table', 'false') + + +# +# 'bgp-rib-out-table' command ('config bmp disable bgp-rib-out-table') +# +@disable.command('bgp-rib-out-table') +@clicommon.pass_db +def diable_bgp_rib_out_table(db): + update_bmp_table('bgp_rib_out_table', 'false') + + +# +# 'bgp-rib-in-table' command ('config bmp disable bgp-rib-in-table') +# +@disable.command('bgp-rib-in-table') +@clicommon.pass_db +def disable_bgp_rib_in_table(db): + update_bmp_table('bgp_rib_in_table', 'false') + + # # 'bgp' group ('config bgp ...') # diff --git a/tests/bmp_input/bmp.json b/tests/bmp_input/bmp.json new file mode 100644 index 0000000000..6f3583f549 --- /dev/null +++ b/tests/bmp_input/bmp.json @@ -0,0 +1,9 @@ +{ + "BMP": { + "table": { + "bgp_neighbor_table": "false", + "bgp_rib_in_table": "false", + "bgp_rib_out_table": "false" + } + } +} diff --git a/tests/bmp_input/bmp_invalid.json b/tests/bmp_input/bmp_invalid.json new file mode 100644 index 0000000000..87a4f937da --- /dev/null +++ b/tests/bmp_input/bmp_invalid.json @@ -0,0 +1,6 @@ +{ + "BMP": { + "table": { + } + } +} diff --git a/tests/config_test.py b/tests/config_test.py index 74bc0e1093..4406a10a5c 100644 --- a/tests/config_test.py +++ b/tests/config_test.py @@ -40,6 +40,9 @@ # Config Reload input Path mock_db_path = os.path.join(test_path, "config_reload_input") +mock_bmp_db_path = os.path.join(test_path, "bmp_input") + + # Load minigraph input Path load_minigraph_input_path = os.path.join(test_path, "load_minigraph_input") load_minigraph_platform_path = os.path.join(load_minigraph_input_path, "platform") @@ -702,6 +705,51 @@ def teardown_class(cls): dbconnector.load_namespace_config() +class TestBMPConfig(object): + @classmethod + def setup_class(cls): + print("SETUP") + os.environ['UTILITIES_UNIT_TESTING'] = "1" + yield + print("TEARDOWN") + os.environ["UTILITIES_UNIT_TESTING"] = "0" + + @pytest.mark.parametrize("table_name", [ + "bgp-neighbor-table", + "bgp-rib-in-table", + "bgp-rib-out-table" + ]) + @pytest.mark.parametrize("enabled", ["true", "false"]) + @pytest.mark.parametrize("filename", ["bmp_invalid.json", "bmp.json"]) + def test_enable_disable_table( + self, + get_cmd_module, + setup_single_broadcom_asic, + table_name, + enabled, + filename): + (config, show) = get_cmd_module + jsonfile_config = os.path.join(mock_bmp_db_path, filename) + config.DEFAULT_CONFIG_DB_FILE = jsonfile_config + runner = CliRunner() + db = Db() + + # Enable table + result = runner.invoke(config.config.commands["bmp"].commands["enable"], + [table_name], obj=db) + assert result.exit_code == 0 + + # Disable table + result = runner.invoke(config.config.commands["bmp"].commands["disable"], + [table_name], obj=db) + assert result.exit_code == 0 + + # Enable table again + result = runner.invoke(config.config.commands["bmp"].commands["enable"], + [table_name], obj=db) + assert result.exit_code == 0 + + class TestConfigReloadMasic(object): @classmethod def setup_class(cls):