Skip to content

Commit a2da494

Browse files
venkatmahalingamgitsabari
authored andcommitted
Add 'default' option for sFlow. (sonic-net#1606)
* Add 'default' option for sFlow. Signed-off-by: Venkatesan Mahalingam <venkatesan_mahalinga@dell.com>
1 parent 17d28d7 commit a2da494

2 files changed

Lines changed: 52 additions & 6 deletions

File tree

config/main.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5018,7 +5018,7 @@ def polling_int(ctx, interval):
50185018
config_db.mod_entry('SFLOW', 'global', sflow_tbl['global'])
50195019

50205020
def is_valid_sample_rate(rate):
5021-
return rate in range(256, 8388608 + 1)
5021+
return rate.isdigit() and int(rate) in range(256, 8388608 + 1)
50225022

50235023

50245024
#
@@ -5076,24 +5076,31 @@ def disable(ctx, ifname):
50765076
#
50775077
@interface.command('sample-rate')
50785078
@click.argument('ifname', metavar='<interface_name>', required=True, type=str)
5079-
@click.argument('rate', metavar='<sample_rate>', required=True, type=int)
5079+
@click.argument('rate', metavar='<sample_rate>', required=True, type=str)
50805080
@click.pass_context
50815081
def sample_rate(ctx, ifname, rate):
50825082
config_db = ctx.obj['db']
50835083
if not interface_name_is_valid(config_db, ifname) and ifname != 'all':
50845084
click.echo('Invalid interface name')
50855085
return
5086-
if not is_valid_sample_rate(rate):
5087-
click.echo('Error: Sample rate must be between 256 and 8388608')
5086+
if not is_valid_sample_rate(rate) and rate != 'default':
5087+
click.echo('Error: Sample rate must be between 256 and 8388608 or default')
50885088
return
50895089

50905090
sess_dict = config_db.get_table('SFLOW_SESSION')
50915091

5092-
if sess_dict and ifname in sess_dict:
5092+
if sess_dict and ifname in sess_dict.keys():
5093+
if rate == 'default':
5094+
if 'sample_rate' not in sess_dict[ifname]:
5095+
return
5096+
del sess_dict[ifname]['sample_rate']
5097+
config_db.set_entry('SFLOW_SESSION', ifname, sess_dict[ifname])
5098+
return
50935099
sess_dict[ifname]['sample_rate'] = rate
50945100
config_db.mod_entry('SFLOW_SESSION', ifname, sess_dict[ifname])
50955101
else:
5096-
config_db.mod_entry('SFLOW_SESSION', ifname, {'sample_rate': rate})
5102+
if rate != 'default':
5103+
config_db.mod_entry('SFLOW_SESSION', ifname, {'sample_rate': rate})
50975104

50985105

50995106
#

tests/sflow_test.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,45 @@ def test_config_enable_all_intf(self):
290290
sflowSession = db.cfgdb.get_table('SFLOW_SESSION')
291291
assert sflowSession["all"]["admin_state"] == "up"
292292

293+
def test_config_sflow_intf_sample_rate_default(self):
294+
db = Db()
295+
runner = CliRunner()
296+
obj = {'db':db.cfgdb}
297+
298+
# mock interface_name_is_valid
299+
config.interface_name_is_valid = mock.MagicMock(return_value = True)
300+
301+
result_out1 = runner.invoke(show.cli.commands["sflow"].commands["interface"], [], obj=Db())
302+
print(result_out1.exit_code, result_out1.output)
303+
assert result_out1.exit_code == 0
304+
305+
# set sample-rate to 2500
306+
result = runner.invoke(config.config.commands["sflow"].
307+
commands["interface"].commands["sample-rate"],
308+
["Ethernet2", "2500"], obj=obj)
309+
print(result.exit_code, result.output)
310+
assert result.exit_code == 0
311+
312+
# we can not use 'show sflow interface', becasue 'show sflow interface'
313+
# gets data from appDB, we need to fetch data from configDB for verification
314+
sflowSession = db.cfgdb.get_table('SFLOW_SESSION')
315+
assert sflowSession["Ethernet2"]["sample_rate"] == "2500"
316+
317+
# set sample-rate to default
318+
result = runner.invoke(config.config.commands["sflow"].
319+
commands["interface"].commands["sample-rate"],
320+
["Ethernet2", "default"], obj=obj)
321+
print(result.exit_code, result.output)
322+
assert result.exit_code == 0
323+
324+
result_out2 = runner.invoke(show.cli.commands["sflow"].commands["interface"], [], obj=Db())
325+
print(result_out2.exit_code, result_out2.output)
326+
assert result_out2.exit_code == 0
327+
assert result_out1.output == result_out2.output
328+
329+
return
330+
331+
293332
@classmethod
294333
def teardown_class(cls):
295334
print("TEARDOWN")

0 commit comments

Comments
 (0)