Skip to content

Commit 76386b1

Browse files
committed
STP PR - Fixed lgtm warnings
1 parent 2ab9746 commit 76386b1

File tree

7 files changed

+645
-80
lines changed

7 files changed

+645
-80
lines changed

clear/main.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import click
77

8+
from . import stp
89
from utilities_common import util_base
910

1011
from . import plugins
@@ -127,8 +128,7 @@ def cli():
127128
#
128129
# 'STP'
129130
#
130-
from .stp import spanning_tree
131-
cli.add_command(spanning_tree)
131+
cli.add_command(stp.spanning_tree)
132132

133133
#
134134
# 'ip' group ###

clear/stp.py

+7-12
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,41 @@
11
import click
2-
from clear.main import AliasedGroup,cli
3-
from clear.main import run_command
2+
import utilities_common.cli as clicommon
43

54
#
65
# This group houses Spanning_tree commands and subgroups
76
#
8-
@cli.group(cls=AliasedGroup)
7+
@click.group(cls=clicommon.AliasedGroup)
98
@click.pass_context
109
def spanning_tree(ctx):
1110
'''Clear Spanning-tree counters'''
1211
pass
1312

14-
@spanning_tree.group('statistics', cls=AliasedGroup, invoke_without_command=True)
13+
@spanning_tree.group('statistics', cls=clicommon.AliasedGroup, invoke_without_command=True)
1514
@click.pass_context
1615
def stp_clr_stats(ctx):
1716
if ctx.invoked_subcommand is None:
1817
command = 'sudo stpctl clrstsall'
19-
run_command(command)
20-
pass
18+
clicommon.run_command(command)
2119

2220
@stp_clr_stats.command('interface')
2321
@click.argument('interface_name', metavar='<interface_name>', required=True)
2422
@click.pass_context
2523
def stp_clr_stats_intf(ctx, interface_name):
2624
command = 'sudo stpctl clrstsintf ' + interface_name
27-
run_command(command)
28-
pass
25+
clicommon.run_command(command)
2926

3027
@stp_clr_stats.command('vlan')
3128
@click.argument('vlan_id', metavar='<vlan_id>', required=True)
3229
@click.pass_context
3330
def stp_clr_stats_vlan(ctx, vlan_id):
3431
command = 'sudo stpctl clrstsvlan ' + vlan_id
35-
run_command(command)
36-
pass
32+
clicommon.run_command(command)
3733

3834
@stp_clr_stats.command('vlan-interface')
3935
@click.argument('vlan_id', metavar='<vlan_id>', required=True)
4036
@click.argument('interface_name', metavar='<interface_name>', required=True)
4137
@click.pass_context
4238
def stp_clr_stats_vlan_intf(ctx, vlan_id, interface_name):
4339
command = 'sudo stpctl clrstsvlanintf ' + vlan_id + ' ' + interface_name
44-
run_command(command)
45-
pass
40+
clicommon.run_command(command)
4641

config/stp.py

+10-33
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55

66
import click
77
import utilities_common.cli as clicommon
8-
import netaddr
9-
from swsscommon.swsscommon import SonicV2Connector, ConfigDBConnector
8+
from swsscommon.swsscommon import SonicV2Connector
109
from natsort import natsorted
1110
import logging
1211

@@ -53,33 +52,28 @@ def get_intf_list_in_vlan_member_table(config_db):
5352
def is_valid_root_guard_timeout(ctx, root_guard_timeout):
5453
if root_guard_timeout not in range(STP_MIN_ROOT_GUARD_TIMEOUT, STP_MAX_ROOT_GUARD_TIMEOUT + 1):
5554
ctx.fail("STP root guard timeout must be in range 5-600")
56-
pass
5755

5856

5957
def is_valid_forward_delay(ctx, forward_delay):
6058
if forward_delay not in range(STP_MIN_FORWARD_DELAY, STP_MAX_FORWARD_DELAY + 1):
6159
ctx.fail("STP forward delay value must be in range 4-30")
62-
pass
6360

6461

6562
def is_valid_hello_interval(ctx, hello_interval):
6663
if hello_interval not in range(STP_MIN_HELLO_INTERVAL, STP_MAX_HELLO_INTERVAL + 1):
6764
ctx.fail("STP hello timer must be in range 1-10")
68-
pass
6965

7066

7167
def is_valid_max_age(ctx, max_age):
7268
if max_age not in range(STP_MIN_MAX_AGE, STP_MAX_MAX_AGE + 1):
7369
ctx.fail("STP max age value must be in range 6-40")
74-
pass
7570

7671

7772
def is_valid_bridge_priority(ctx, priority):
7873
if priority not in range(STP_MIN_BRIDGE_PRIORITY, STP_MAX_BRIDGE_PRIORITY + 1):
7974
ctx.fail("STP bridge priority must be in range 0-61440")
8075
if priority % 4096 != 0:
8176
ctx.fail("STP bridge priority must be multiple of 4096")
82-
pass
8377

8478

8579
def validate_params(forward_delay, max_age, hello_time):
@@ -129,15 +123,16 @@ def is_valid_stp_global_parameters(ctx, db, param_type, new_value):
129123
parameter_bridge_priority = 4
130124

131125
def get_max_stp_instances():
132-
state_db = SonicV2Connector(host='127.0.0.1')
133-
state_db.connect(state_db.STATE_DB, False)
134-
max_inst = state_db.get(state_db.STATE_DB, "STP_TABLE|GLOBAL", "max_stp_inst")
135-
#if max_inst == "":
136126
return PVST_MAX_INSTANCES
137-
if max_inst != None and max_inst != 0 and max_inst < PVST_MAX_INSTANCES:
138-
return max_inst
139-
else:
140-
return PVST_MAX_INSTANCES
127+
#state_db = SonicV2Connector(host='127.0.0.1')
128+
#state_db.connect(state_db.STATE_DB, False)
129+
#max_inst = state_db.get(state_db.STATE_DB, "STP_TABLE|GLOBAL", "max_stp_inst")
130+
#if max_inst == "":
131+
# return PVST_MAX_INSTANCES
132+
#if max_inst != None and max_inst != 0 and max_inst < PVST_MAX_INSTANCES:
133+
# return max_inst
134+
#else:
135+
# return PVST_MAX_INSTANCES
141136

142137
def update_stp_vlan_parameter(db, param_type, new_value):
143138
stp_global_entry = db.get_entry('STP', "GLOBAL")
@@ -170,15 +165,13 @@ def update_stp_vlan_parameter(db, param_type, new_value):
170165
current_vlan_value = vlan_entry.get("priority")
171166
if current_global_value == current_vlan_value:
172167
db.mod_entry('STP_VLAN', vlan, {'priority': new_value})
173-
pass
174168

175169

176170
def check_if_vlan_exist_in_db(db, ctx, vid):
177171
vlan_name = 'Vlan{}'.format(vid)
178172
vlan = db.get_entry('VLAN', vlan_name)
179173
if len(vlan) == 0:
180174
ctx.fail("{} doesn't exist".format(vlan_name))
181-
pass
182175

183176

184177
def enable_stp_for_vlans(db):
@@ -197,7 +190,6 @@ def enable_stp_for_vlans(db):
197190
break
198191
db.set_entry('STP_VLAN', vlan_key, fvs)
199192
vlan_count += 1
200-
pass
201193

202194

203195
def get_stp_enabled_vlan_count(db):
@@ -312,7 +304,6 @@ def enable_stp_for_interfaces(db):
312304
for po_ch_key in po_ch_dict:
313305
if po_ch_key in intf_list_in_vlan_member_table:
314306
db.set_entry('STP_PORT', po_ch_key, fvs)
315-
pass
316307

317308

318309
def is_global_stp_enabled(db):
@@ -391,7 +382,6 @@ def spanning_tree_enable(_db, mode):
391382
# Enable STP for VLAN by default
392383
enable_stp_for_interfaces(db)
393384
enable_stp_for_vlans(db)
394-
pass
395385

396386

397387
# cmd: STP disable
@@ -406,7 +396,6 @@ def stp_disable(_db, mode):
406396
db.delete_table('STP_VLAN')
407397
db.delete_table('STP_PORT')
408398
db.delete_table('STP_VLAN_PORT')
409-
pass
410399

411400

412401
# cmd: STP global root guard timeout
@@ -420,7 +409,6 @@ def stp_global_root_guard_timeout(_db, root_guard_timeout):
420409
check_if_global_stp_enabled(db, ctx)
421410
is_valid_root_guard_timeout(ctx, root_guard_timeout)
422411
db.mod_entry('STP', "GLOBAL", {'rootguard_timeout': root_guard_timeout})
423-
pass
424412

425413

426414
# cmd: STP global forward delay
@@ -436,7 +424,6 @@ def stp_global_forward_delay(_db, forward_delay):
436424
is_valid_stp_global_parameters(ctx, db, parameter_forward_delay, forward_delay)
437425
update_stp_vlan_parameter(db, parameter_forward_delay, forward_delay)
438426
db.mod_entry('STP', "GLOBAL", {'forward_delay': forward_delay})
439-
pass
440427

441428

442429
# cmd: STP global hello interval
@@ -452,7 +439,6 @@ def stp_global_hello_interval(_db, hello_interval):
452439
is_valid_stp_global_parameters(ctx, db, parameter_hello_time, hello_interval)
453440
update_stp_vlan_parameter(db, parameter_hello_time, hello_interval)
454441
db.mod_entry('STP', "GLOBAL", {'hello_time': hello_interval})
455-
pass
456442

457443

458444
# cmd: STP global max age
@@ -468,7 +454,6 @@ def stp_global_max_age(_db, max_age):
468454
is_valid_stp_global_parameters(ctx, db, parameter_max_age, max_age)
469455
update_stp_vlan_parameter(db, parameter_max_age, max_age)
470456
db.mod_entry('STP', "GLOBAL", {'max_age': max_age})
471-
pass
472457

473458

474459
# cmd: STP global bridge priority
@@ -483,7 +468,6 @@ def stp_global_priority(_db, priority):
483468
is_valid_bridge_priority(ctx, priority)
484469
update_stp_vlan_parameter(db, parameter_bridge_priority, priority)
485470
db.mod_entry('STP', "GLOBAL", {'priority': priority})
486-
pass
487471

488472

489473
###############################################
@@ -543,7 +527,6 @@ def stp_vlan_enable(_db, vid):
543527
vlan_intf_key = "{}|{}".format(vlan_name, intf)
544528
vlan_intf_entry = db.get_entry('STP_VLAN_PORT', vlan_intf_key)
545529
db.mod_entry('STP_VLAN_PORT', vlan_intf_key, vlan_intf_entry)
546-
pass
547530

548531

549532
@spanning_tree_vlan.command('disable')
@@ -556,7 +539,6 @@ def stp_vlan_disable(_db, vid):
556539
check_if_vlan_exist_in_db(db, ctx, vid)
557540
vlan_name = 'Vlan{}'.format(vid)
558541
db.mod_entry('STP_VLAN', vlan_name, {'enabled': 'false'})
559-
pass
560542

561543

562544
@spanning_tree_vlan.command('forward_delay')
@@ -573,7 +555,6 @@ def stp_vlan_forward_delay(_db, vid, forward_delay):
573555
is_valid_forward_delay(ctx, forward_delay)
574556
is_valid_stp_vlan_parameters(ctx, db, vlan_name, parameter_forward_delay, forward_delay)
575557
db.mod_entry('STP_VLAN', vlan_name, {'forward_delay': forward_delay})
576-
pass
577558

578559

579560
@spanning_tree_vlan.command('hello')
@@ -590,7 +571,6 @@ def stp_vlan_hello_interval(_db, vid, hello_interval):
590571
is_valid_hello_interval(ctx, hello_interval)
591572
is_valid_stp_vlan_parameters(ctx, db, vlan_name, parameter_hello_time, hello_interval)
592573
db.mod_entry('STP_VLAN', vlan_name, {'hello_time': hello_interval})
593-
pass
594574

595575

596576
@spanning_tree_vlan.command('max_age')
@@ -607,7 +587,6 @@ def stp_vlan_max_age(_db, vid, max_age):
607587
is_valid_max_age(ctx, max_age)
608588
is_valid_stp_vlan_parameters(ctx, db, vlan_name, parameter_max_age, max_age)
609589
db.mod_entry('STP_VLAN', vlan_name, {'max_age': max_age})
610-
pass
611590

612591

613592
@spanning_tree_vlan.command('priority')
@@ -623,7 +602,6 @@ def stp_vlan_priority(_db, vid, priority):
623602
check_if_stp_enabled_for_vlan(ctx, db, vlan_name)
624603
is_valid_bridge_priority(ctx, priority)
625604
db.mod_entry('STP_VLAN', vlan_name, {'priority': priority})
626-
pass
627605

628606

629607
###############################################
@@ -658,7 +636,6 @@ def check_if_interface_is_valid(ctx, db, interface_name):
658636
ctx.fail(" {} is a portchannel member port - STP can't be configured".format(interface_name))
659637
if not is_vlan_configured_interface(db, interface_name):
660638
ctx.fail(" {} has no VLAN configured - It's not a L2 interface".format(interface_name))
661-
pass
662639

663640

664641
@spanning_tree.group('interface')

debug/main.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import click
22
import subprocess
33

4+
from . import stp
5+
46
def run_command(command, pager=False):
57
click.echo(click.style("Command: ", fg='cyan') + click.style(command, fg='green'))
68
p = subprocess.Popen(command, shell=True, text=True, stdout=subprocess.PIPE)
@@ -24,9 +26,7 @@ def cli():
2426
#
2527
# STP
2628
#
27-
from .stp import spanning_tree
28-
cli.add_command(spanning_tree)
29-
29+
cli.add_command(stp.spanning_tree)
3030

3131
p = subprocess.check_output(["sudo vtysh -c 'show version'"], shell=True, text=True)
3232
if 'FRRouting' in p:

debug/stp.py

-11
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ def spanning_tree(ctx):
1212
if ctx.invoked_subcommand is None:
1313
command = 'sudo stpctl dbg enable'
1414
run_command(command)
15-
pass
1615

1716
@spanning_tree.group('dump', cls=AliasedGroup, default_if_no_args=False, invoke_without_command=True)
1817
def stp_debug_dump():
@@ -22,34 +21,29 @@ def stp_debug_dump():
2221
def stp_debug_dump_global():
2322
command = 'sudo stpctl global'
2423
run_command(command)
25-
pass
2624

2725
@stp_debug_dump.command('vlan')
2826
@click.argument('vlan_id', metavar='<vlan_id>', required=True)
2927
def stp_debug_dump_vlan(vlan_id):
3028
command = 'sudo stpctl vlan ' + vlan_id
3129
run_command(command)
32-
pass
3330

3431
@stp_debug_dump.command('interface')
3532
@click.argument('vlan_id', metavar='<vlan_id>', required=True)
3633
@click.argument('interface_name', metavar='<interface_name>', required=True)
3734
def stp_debug_dump_vlan_intf(vlan_id, interface_name):
3835
command = 'sudo stpctl port ' + vlan_id + " " + interface_name
3936
run_command(command)
40-
pass
4137

4238
@spanning_tree.command('show')
4339
def stp_debug_show():
4440
command = 'sudo stpctl dbg show'
4541
run_command(command)
46-
pass
4742

4843
@spanning_tree.command('reset')
4944
def stp_debug_reset():
5045
command = 'sudo stpctl dbg disable'
5146
run_command(command)
52-
pass
5347

5448
@spanning_tree.command('bpdu')
5549
@click.argument('mode', metavar='{rx|tx}', required=False)
@@ -70,7 +64,6 @@ def stp_debug_bpdu(mode, disable):
7064
else:
7165
command = 'sudo stpctl dbg bpdu on'
7266
run_command(command)
73-
pass
7467

7568
@spanning_tree.command('verbose')
7669
@click.option('-d', '--disable', is_flag=True)
@@ -80,7 +73,6 @@ def stp_debug_verbose(disable):
8073
else:
8174
command = 'sudo stpctl dbg verbose on'
8275
run_command(command)
83-
pass
8476

8577
@spanning_tree.command('event')
8678
@click.option('-d', '--disable', is_flag=True)
@@ -90,7 +82,6 @@ def stp_debug_event(disable):
9082
else:
9183
command = 'sudo stpctl dbg event on'
9284
run_command(command)
93-
pass
9485

9586
@spanning_tree.command('vlan')
9687
@click.argument('vlan_id', metavar='<vlan_id/all>', required=True)
@@ -101,7 +92,6 @@ def stp_debug_vlan(vlan_id, disable):
10192
else:
10293
command = 'sudo stpctl dbg vlan ' + vlan_id + ' on'
10394
run_command(command)
104-
pass
10595

10696
@spanning_tree.command('interface')
10797
@click.argument('interface_name', metavar='<interface_name/all>', required=True)
@@ -112,6 +102,5 @@ def stp_debug_intf(interface_name, disable):
112102
else:
113103
command = 'sudo stpctl dbg port ' + interface_name + ' on'
114104
run_command(command)
115-
pass
116105

117106

0 commit comments

Comments
 (0)