Skip to content

Commit

Permalink
Added range functionality in '-m' flag and removed explicit range com…
Browse files Browse the repository at this point in the history
…mands
  • Loading branch information
MuhammadUmarAsad committed Oct 11, 2022
1 parent 330b101 commit d4ba163
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 132 deletions.
125 changes: 0 additions & 125 deletions config/vlan.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,128 +286,3 @@ def del_vlan_member(db, vid, port, multiple, except_flag):
db.cfgdb.set_entry('VLAN_MEMBER', (vlan, port), None)

click.echo("'vlan member del {} {}' done".format(vid, port))


@vlan.group(cls=clicommon.AbbreviationGroup, name='range')
def vlan_range():
pass


@vlan_range.command("add")
@click.argument('vid1', metavar='<vid1>', required=True, type=int)
@click.argument('vid2', metavar='<vid2>', required=True, type=int)
@clicommon.pass_db
def add_vlan_range(db, vid1, vid2):
"""Add VLAN range vid1 vid2"""

ctx = click.get_current_context()

# it will return a complete list of vlans
vid_list = clicommon.vlan_range_list(ctx, vid1, vid2)

# loop will execute till an exception occurs
for vid in vid_list:

log.log_info("'vlan add {}' executing...".format(vid))

if not clicommon.is_vlanid_in_range(vid):
ctx.fail("Invalid VLAN ID {} (2-4094)".format(vid))

vlan = 'Vlan{}'.format(vid)

if clicommon.check_if_vlanid_exist(db.cfgdb, vlan):
ctx.echo("{} already exists".format(vlan))
log.log_info(
"{} already exists.Continuing vlan range add ...".format(vid))
continue

db.cfgdb.set_entry('VLAN', vlan, {'vlanid': vid})

click.echo("'vlan add {} ' done".format(vid))


@vlan_range.command("del")
@click.argument('vid1', metavar='<vid1>', required=True, type=int)
@click.argument('vid2', metavar='<vid2>', required=True, type=int)
@clicommon.pass_db
def del_vlan_range(db, vid1, vid2):
"""Del VLAN range vid1 vid2"""

ctx = click.get_current_context()

# it will return a complete list of vlans
vid_list = clicommon.vlan_range_list(ctx, vid1, vid2)

# loop will execute till an exception occurs
for vid in vid_list:

log.log_info("'vlan del {}' executing...".format(vid))

vlan = 'Vlan{}'.format(vid)

if clicommon.check_if_vlanid_exist(db.cfgdb, vlan) == False:
ctx.echo("{} does not exist".format(vlan))
log.log_info(
"{} does not exist.Continuing vlan range del ...".format(vid))
continue

intf_table = db.cfgdb.get_table('VLAN_INTERFACE')
for intf_key in intf_table:
if ((type(intf_key) is str and intf_key == 'Vlan{}'.format(vid)) or
(type(intf_key) is tuple and intf_key[0] == 'Vlan{}'.format(vid))):
ctx.fail(
"{} can not be removed. First remove IP addresses assigned to this VLAN".format(vlan))

keys = [(k, v) for k, v in db.cfgdb.get_table(
'VLAN_MEMBER') if k == 'Vlan{}'.format(vid)]

if keys:
ctx.fail(
"VLAN ID {} can not be removed. First remove all members assigned to this VLAN.".format(vid))

db.cfgdb.set_entry('VLAN', 'Vlan{}'.format(vid), None)

click.echo("'vlan del {} ' done".format(vid))


@vlan_member.group(cls=clicommon.AbbreviationGroup, name='range')
def vlan_member_range():
pass


@vlan_member_range.command("add")
@click.argument('vid1', metavar='<vid1>', required=True, type=int)
@click.argument('vid2', metavar='<vid2>', required=True, type=int)
@click.argument('port', metavar='<port>', required=True)
@clicommon.pass_db
def add_vlan_member_range(db, vid1, vid2, port):
"""Del VLAN member range vid1 vid2"""

ctx = click.get_current_context()

# it will return a complete list of vlans
vid_list = clicommon.vlan_range_list(ctx, vid1, vid2)

# loop will execute till an exception occurs
for vid in vid_list:
# adding each member in the range one by one through calling function
add_vlan_member(db, vid, port, False, False, False)


@vlan_member_range.command("del")
@click.argument('vid1', metavar='<vid1>', required=True, type=int)
@click.argument('vid2', metavar='<vid2>', required=True, type=int)
@click.argument('port', metavar='<port>', required=True)
@clicommon.pass_db
def del_vlan_member_range(db, vid1, vid2, port):
"""Del VLAN member range vid1 vid2"""

ctx = click.get_current_context()

# it will return a complete list of vlans
vid_list = clicommon.vlan_range_list(ctx, vid1, vid2)

# loop will execute till an exception occurs
for vid in vid_list:
# adding each member in the range one by one through calling function
del_vlan_member(db, vid, port, False, False)
18 changes: 11 additions & 7 deletions utilities_common/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,9 @@ def is_port_vlan_member(config_db, port, vlan):
return False


def vlan_range_list(ctx, vid1: int, vid2: int) -> list:
def vlan_range_list(ctx, vid_range: str) -> list:

vid1, vid2 = map(int, vid_range.split("-"))

if vid1 == 1 or vid2 == 1:
ctx.fail("Vlan1 is default vlan. Use switchport command.")
Expand All @@ -304,13 +306,15 @@ def vlan_range_list(ctx, vid1: int, vid2: int) -> list:
def multiple_vlan_parser(ctx, s_input: str) -> list:

vlan_list = []
try:
vlan_map = map(int, s_input.replace(" ", "").split(","))
except:
ctx.fail("Vlanid is not an integer.")

vlan_map = map(str, s_input.replace(" ", "").split(","))
for vlan in vlan_map:
if vlan not in vlan_list:
vlan_list.append(vlan)
if "-" in vlan:
vlan_list += vlan_range_list(ctx, vlan)
elif vlan.isdigit() and int(vlan) not in vlan_list:
vlan_list.append(int(vlan))
elif not vlan.isdigit():
ctx.fail(vlan, " is not an integer.")

vlan_list.sort()
return vlan_list
Expand Down

0 comments on commit d4ba163

Please sign in to comment.