Skip to content

Commit 0ae2ec1

Browse files
Add CLI for bmp configdb entity Enable/Disable (sonic-net#3286)
* Add CLI for bmp configdb entity Enable/Disable. * Add CLI for bmp configdb entity Enable/Disable. * Use pytest.mark.parametrize to reduce duplicated code * Use pytest.mark.parametrize to reduce duplicated code
1 parent 5b37ee6 commit 0ae2ec1

File tree

4 files changed

+162
-0
lines changed

4 files changed

+162
-0
lines changed

config/main.py

+99
Original file line numberDiff line numberDiff line change
@@ -4248,6 +4248,105 @@ def del_user(db, user):
42484248
click.echo("Restart service snmp failed with error {}".format(e))
42494249
raise click.Abort()
42504250

4251+
4252+
#
4253+
# 'bmp' group ('config bmp ...')
4254+
#
4255+
@config.group()
4256+
@clicommon.pass_db
4257+
def bmp(db):
4258+
"""BMP-related configuration"""
4259+
pass
4260+
4261+
4262+
#
4263+
# common function to update bmp config table
4264+
#
4265+
@clicommon.pass_db
4266+
def update_bmp_table(db, table_name, value):
4267+
log.log_info(f"'bmp {value} {table_name}' executing...")
4268+
bmp_table = db.cfgdb.get_table('BMP')
4269+
if not bmp_table:
4270+
bmp_table = {'table': {table_name: value}}
4271+
else:
4272+
bmp_table['table'][table_name] = value
4273+
db.cfgdb.mod_entry('BMP', 'table', bmp_table['table'])
4274+
4275+
4276+
#
4277+
# 'enable' subgroup ('config bmp enable ...')
4278+
#
4279+
@bmp.group()
4280+
@clicommon.pass_db
4281+
def enable(db):
4282+
"""Enable BMP table dump """
4283+
pass
4284+
4285+
4286+
#
4287+
# 'bgp-neighbor-table' command ('config bmp enable bgp-neighbor-table')
4288+
#
4289+
@enable.command('bgp-neighbor-table')
4290+
@clicommon.pass_db
4291+
def enable_bgp_neighbor_table(db):
4292+
update_bmp_table('bgp_neighbor_table', 'true')
4293+
4294+
4295+
#
4296+
# 'bgp-rib-out-table' command ('config bmp enable bgp-rib-out-table')
4297+
#
4298+
@enable.command('bgp-rib-out-table')
4299+
@clicommon.pass_db
4300+
def enable_bgp_rib_out_table(db):
4301+
update_bmp_table('bgp_rib_out_table', 'true')
4302+
4303+
4304+
#
4305+
# 'bgp-rib-in-table' command ('config bmp enable bgp-rib-in-table')
4306+
#
4307+
@enable.command('bgp-rib-in-table')
4308+
@clicommon.pass_db
4309+
def enable_bgp_rib_in_table(db):
4310+
update_bmp_table('bgp_rib_in_table', 'true')
4311+
4312+
4313+
#
4314+
# 'disable' subgroup ('config bmp disable ...')
4315+
#
4316+
@bmp.group()
4317+
@clicommon.pass_db
4318+
def disable(db):
4319+
"""Disable BMP table dump """
4320+
pass
4321+
4322+
4323+
#
4324+
# 'bgp-neighbor-table' command ('config bmp disable bgp-neighbor-table')
4325+
#
4326+
@disable.command('bgp-neighbor-table')
4327+
@clicommon.pass_db
4328+
def disable_bgp_neighbor_table(db):
4329+
update_bmp_table('bgp_neighbor_table', 'false')
4330+
4331+
4332+
#
4333+
# 'bgp-rib-out-table' command ('config bmp disable bgp-rib-out-table')
4334+
#
4335+
@disable.command('bgp-rib-out-table')
4336+
@clicommon.pass_db
4337+
def diable_bgp_rib_out_table(db):
4338+
update_bmp_table('bgp_rib_out_table', 'false')
4339+
4340+
4341+
#
4342+
# 'bgp-rib-in-table' command ('config bmp disable bgp-rib-in-table')
4343+
#
4344+
@disable.command('bgp-rib-in-table')
4345+
@clicommon.pass_db
4346+
def disable_bgp_rib_in_table(db):
4347+
update_bmp_table('bgp_rib_in_table', 'false')
4348+
4349+
42514350
#
42524351
# 'bgp' group ('config bgp ...')
42534352
#

tests/bmp_input/bmp.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"BMP": {
3+
"table": {
4+
"bgp_neighbor_table": "false",
5+
"bgp_rib_in_table": "false",
6+
"bgp_rib_out_table": "false"
7+
}
8+
}
9+
}

tests/bmp_input/bmp_invalid.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"BMP": {
3+
"table": {
4+
}
5+
}
6+
}

tests/config_test.py

+48
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
# Config Reload input Path
4141
mock_db_path = os.path.join(test_path, "config_reload_input")
4242

43+
mock_bmp_db_path = os.path.join(test_path, "bmp_input")
44+
45+
4346
# Load minigraph input Path
4447
load_minigraph_input_path = os.path.join(test_path, "load_minigraph_input")
4548
load_minigraph_platform_path = os.path.join(load_minigraph_input_path, "platform")
@@ -702,6 +705,51 @@ def teardown_class(cls):
702705
dbconnector.load_namespace_config()
703706

704707

708+
class TestBMPConfig(object):
709+
@classmethod
710+
def setup_class(cls):
711+
print("SETUP")
712+
os.environ['UTILITIES_UNIT_TESTING'] = "1"
713+
yield
714+
print("TEARDOWN")
715+
os.environ["UTILITIES_UNIT_TESTING"] = "0"
716+
717+
@pytest.mark.parametrize("table_name", [
718+
"bgp-neighbor-table",
719+
"bgp-rib-in-table",
720+
"bgp-rib-out-table"
721+
])
722+
@pytest.mark.parametrize("enabled", ["true", "false"])
723+
@pytest.mark.parametrize("filename", ["bmp_invalid.json", "bmp.json"])
724+
def test_enable_disable_table(
725+
self,
726+
get_cmd_module,
727+
setup_single_broadcom_asic,
728+
table_name,
729+
enabled,
730+
filename):
731+
(config, show) = get_cmd_module
732+
jsonfile_config = os.path.join(mock_bmp_db_path, filename)
733+
config.DEFAULT_CONFIG_DB_FILE = jsonfile_config
734+
runner = CliRunner()
735+
db = Db()
736+
737+
# Enable table
738+
result = runner.invoke(config.config.commands["bmp"].commands["enable"],
739+
[table_name], obj=db)
740+
assert result.exit_code == 0
741+
742+
# Disable table
743+
result = runner.invoke(config.config.commands["bmp"].commands["disable"],
744+
[table_name], obj=db)
745+
assert result.exit_code == 0
746+
747+
# Enable table again
748+
result = runner.invoke(config.config.commands["bmp"].commands["enable"],
749+
[table_name], obj=db)
750+
assert result.exit_code == 0
751+
752+
705753
class TestConfigReloadMasic(object):
706754
@classmethod
707755
def setup_class(cls):

0 commit comments

Comments
 (0)