Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch Port Modes and VLAN CLI Enhancement #2419

Merged
merged 72 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
72 commits
Select commit Hold shift + click to select a range
330b101
Modification of Vlan.py and creation of switchport.py
MuhammadUmarAsad Aug 26, 2022
d4ba163
Added range functionality in '-m' flag and removed explicit range com…
MuhammadUmarAsad Oct 11, 2022
0ddb795
Merge branch 'PR_Saved' of https://github.com/ridahanif96/sonic-utili…
MuhammadUmarAsad Oct 13, 2022
6b3ac13
Merge branch 'PR_Saved' of https://github.com/ridahanif96/sonic-utili…
MuhammadUmarAsad Oct 13, 2022
930630c
Modified switchport.py functionality removing default Vlan from it
MuhammadUmarAsad Oct 14, 2022
fa5d756
Merge branch 'sonic-net:master' into master
MuhammadUmarAsad Oct 23, 2022
02b1e8a
Modification in vlan.py and switchport.py
MuhammadUmarAsad Oct 24, 2022
cd13da3
Added support for mode attribute in IP config commands and updated the
MuhammadUmarAsad Dec 10, 2022
519324d
Added switchport mode support for PORTCHANNEL
MuhammadUmarAsad Dec 13, 2022
0ca0d24
changed echo statement to error statement in switchport
MuhammadUmarAsad Dec 16, 2022
5fea7b8
Merge branch 'master' into master
MuhammadUmarAsad Feb 9, 2023
2fd4c09
updated the merge conflicts
MuhammadUmarAsad Feb 9, 2023
22888f1
Resolved Merge Conflicts with #2669
MuhammadUmarAsad Feb 11, 2023
99609dd
Resolved Merge Conflicts with PR #2669
MuhammadUmarAsad Feb 11, 2023
4176fec
Updated test cases w.r.t to PR#2669
MuhammadUmarAsad Feb 11, 2023
b4e46db
Updated db_migrator.py for switchport mode
MuhammadUmarAsad Feb 15, 2023
6ea6025
updating db_migrator for switchport mode
MuhammadUmarAsad Feb 18, 2023
a007d20
Merge branch 'master' into master
MuhammadUmarAsad Feb 18, 2023
d77508f
fixing db_migrator
MuhammadUmarAsad Feb 18, 2023
d64ddd6
Merge branch 'master' of https://github.com/ridahanif96/sonic-utiliti…
MuhammadUmarAsad Feb 18, 2023
5a18655
db_migrator
MuhammadUmarAsad Feb 18, 2023
499b6d5
changes in db migrator for switchport
MuhammadUmarAsad Feb 18, 2023
63e28aa
fixing migrate_config_db_port_table_for_switchport_mode
MuhammadUmarAsad Feb 18, 2023
b4197b9
updated migrate_config_db_port_table_for_switchport_mode to migrate_c…
MuhammadUmarAsad Feb 18, 2023
0df0f34
plans to handle DHCP restart service after its conflicts ends with ya…
MuhammadUmarAsad Feb 21, 2023
8bc5493
updated config vlan del command for DHCP relay restart services since…
MuhammadUmarAsad Feb 23, 2023
046fc2e
added testcases for switchport mode in db_migrator
MuhammadUmarAsad Feb 23, 2023
92f8a2c
updated input and expected output for switchport db_migrator
MuhammadUmarAsad Feb 23, 2023
dd4828a
updated db_migrator tests for switchport mode with PORTCHANNEL input …
MuhammadUmarAsad Feb 23, 2023
478a3a6
updated test db_migrator and switchport expedted output
MuhammadUmarAsad Feb 24, 2023
c5a1ea1
db_migrator test cases
MuhammadUmarAsad Feb 24, 2023
9547b95
Revert "db_migrator test cases"
MuhammadUmarAsad Feb 24, 2023
f7bc359
testing db_migrator.py
MuhammadUmarAsad Feb 24, 2023
d856425
testing db_migrator.py
MuhammadUmarAsad Feb 24, 2023
6861d63
testing db_migrator.py
MuhammadUmarAsad Feb 24, 2023
1458610
updated db_migrator switchport mode test and inputs/outputs
MuhammadUmarAsad Feb 24, 2023
ae00297
db_migrator switchport test update
MuhammadUmarAsad Feb 24, 2023
5ccc73b
db_migrator updated switchport migration and updated DB_VERSION
MuhammadUmarAsad Feb 24, 2023
ddabf32
db_migrator test
MuhammadUmarAsad Feb 24, 2023
9411196
updated db_migrator tests
MuhammadUmarAsad Feb 24, 2023
c3fab6d
updated db_migrator switchport test input config
MuhammadUmarAsad Feb 24, 2023
da65f41
updated db_migrator switchport mode input and expected output
MuhammadUmarAsad Feb 24, 2023
4c03ff1
updated db_migrator switchport mode test input and output
MuhammadUmarAsad Feb 25, 2023
e347a61
updated migrate_config_db_switchport_mode in db_migrator and updated …
MuhammadUmarAsad Feb 25, 2023
43d7b05
Merge branch 'master' into master
MuhammadUmarAsad Mar 21, 2023
55dbfb3
updated the db_migrator and vlan.py for merge conflicts and removed p…
MuhammadUmarAsad Mar 21, 2023
f4b130c
updated vlan test cases. Removed Assertion of vlan addition|deletion …
MuhammadUmarAsad Mar 22, 2023
e980471
removed print asserrtion vlan member addition statement from test cases
MuhammadUmarAsad Mar 22, 2023
1836cd8
removed print asserrtion vlan member deletion statement from test cases
MuhammadUmarAsad Mar 22, 2023
7998123
removed some leftover print asserrtion vlan member deletion statement…
MuhammadUmarAsad Mar 22, 2023
485b4c8
updating of code with regards to the changed in HLD
MuhammadUmarAsad May 9, 2023
2273103
removed extra error message from vlan.py config
MuhammadUmarAsad May 9, 2023
ba7b72d
Merge branch 'master' into master
MuhammadUmarAsad May 9, 2023
57f1c99
Merge branch 'master' of https://github.com/ridahanif96/sonic-utiliti…
MuhammadUmarAsad May 9, 2023
70cbfe5
fixed indentation errors
MuhammadUmarAsad May 9, 2023
ebc14ca
indentation fix
MuhammadUmarAsad May 9, 2023
5b1063c
updated -e flag for config vlan member add/del in command reference
MuhammadUmarAsad May 22, 2023
3faca88
Merge branch 'master' into master
MuhammadUmarAsad Jun 13, 2023
7177585
Merge branch 'master' into master
MuhammadUmarAsad Jun 16, 2023
2672a99
fixed indentation error in db_migrator.py
MuhammadUmarAsad Jun 16, 2023
f87a7e7
added missing handling log info in verson_4_0_1
MuhammadUmarAsad Jun 16, 2023
7ccb817
running pipelines
MuhammadUmarAsad Jun 16, 2023
43a5e17
added pass statement instead of error when running switchport mode co…
MuhammadUmarAsad Jun 16, 2023
76b4f81
updated test case for switchport mode
MuhammadUmarAsad Jun 17, 2023
a70fd53
Merge branch 'master' into master
MuhammadUmarAsad Jul 6, 2023
cba8e2e
running pipelines
MuhammadUmarAsad Jul 7, 2023
dd11b3c
Merge branch 'sonic-net:master' into master
MuhammadUmarAsad Jul 11, 2023
300b353
Merge branch 'master' into master
MuhammadUmarAsad Jul 20, 2023
95912ba
fixed space/tab indent usage error
MuhammadUmarAsad Jul 21, 2023
f63f960
spacing
MuhammadUmarAsad Jul 26, 2023
7b7fd96
pipeline trigger
MuhammadUmarAsad Jul 26, 2023
6d67b53
pipeline trigger
MuhammadUmarAsad Jul 27, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 31 additions & 7 deletions config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
from .config_mgmt import ConfigMgmtDPB, ConfigMgmt
from . import mclag
from . import syslog
from . import switchport
from . import dns

# mock masic APIs for unit test
Expand Down Expand Up @@ -101,6 +102,8 @@
CFG_PORTCHANNEL_NO="<0-9999>"

PORT_MTU = "mtu"
PORT_MODE= "switchport_mode"

PORT_SPEED = "speed"
PORT_TPID = "tpid"
DEFAULT_TPID = "0x8100"
Expand Down Expand Up @@ -1190,6 +1193,7 @@ def config(ctx):
config.add_command(nat.nat)
config.add_command(vlan.vlan)
config.add_command(vxlan.vxlan)
config.add_command(switchport.switchport)

#add mclag commands
config.add_command(mclag.mclag)
Expand Down Expand Up @@ -4489,19 +4493,39 @@ def add(ctx, interface_name, ip_addr, gw):
if interface_name is None:
ctx.fail("'interface_name' is None!")

# Add a validation to check this interface is not a member in vlan before
# changing it to a router port
vlan_member_table = config_db.get_table('VLAN_MEMBER')
if (interface_is_in_vlan(vlan_member_table, interface_name)):
click.echo("Interface {} is a member of vlan\nAborting!".format(interface_name))
return

portchannel_member_table = config_db.get_table('PORTCHANNEL_MEMBER')

if interface_is_in_portchannel(portchannel_member_table, interface_name):
ctx.fail("{} is configured as a member of portchannel."
.format(interface_name))


# Add a validation to check this interface is in routed mode before
# assigning an IP address to it

sub_intf = False

if clicommon.is_valid_port(config_db, interface_name):
is_port = True
elif clicommon.is_valid_portchannel(config_db, interface_name):
is_port = False
else:
sub_intf = True

if not sub_intf:
interface_mode = "routed"
if is_port:
interface_data = config_db.get_entry('PORT',interface_name)
elif not is_port:
interface_data = config_db.get_entry('PORTCHANNEL',interface_name)

if "mode" in interface_data:
interface_mode = interface_data["mode"]

if interface_mode != "routed":
ctx.fail("Interface {} is not in routed mode!".format(interface_name))
return

try:
ip_address = ipaddress.ip_interface(ip_addr)
except ValueError as err:
Expand Down
138 changes: 138 additions & 0 deletions config/switchport.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import click
from .utils import log
import utilities_common.cli as clicommon

#
# 'switchport' mode ('config switchport ...')
#


@click.group(cls=clicommon.AbbreviationGroup, name='switchport')
def switchport():
"""Switchport mode configuration tasks"""
pass


@switchport.command("mode")
@click.argument("type", metavar="<mode_type>", required=True, type=click.Choice(["access", "trunk", "routed"]))
@click.argument("port", metavar="port", required=True)
@clicommon.pass_db
def switchport_mode(db, type, port):
"""switchport mode help commands.Mode_type can be access or trunk or routed"""

ctx = click.get_current_context()

log.log_info("'switchport mode {} {}' executing...".format(type, port))
mode_exists_status = True

# checking if port name with alias exists
if clicommon.get_interface_naming_mode() == "alias":
alias = port
iface_alias_converter = clicommon.InterfaceAliasConverter(db)
port = iface_alias_converter.alias_to_name(port)
if port is None:
ctx.fail("cannot find port name for alias {}".format(alias))

if clicommon.is_port_mirror_dst_port(db.cfgdb, port):
ctx.fail("{} is configured as mirror destination port".format(port))


if clicommon.is_valid_port(db.cfgdb, port):
is_port = True
elif clicommon.is_valid_portchannel(db.cfgdb, port):
is_port = False
else:
ctx.fail("{} does not exist".format(port))

portchannel_member_table = db.cfgdb.get_table('PORTCHANNEL_MEMBER')

if (is_port and clicommon.interface_is_in_portchannel(portchannel_member_table, port)):
ctx.fail("{} is part of portchannel!".format(port))

if is_port:
port_data = db.cfgdb.get_entry('PORT',port)
else:
port_data = db.cfgdb.get_entry('PORTCHANNEL',port)

# mode type is either access or trunk
if type != "routed":

if "mode" in port_data:
existing_mode = port_data["mode"]
else:
existing_mode = "routed"
mode_exists_status = False
if (is_port and clicommon.is_port_router_interface(db.cfgdb, port)) or \
(not is_port and clicommon.is_pc_router_interface(db.cfgdb, port)):
ctx.fail("Remove IP from {} to change mode!".format(port))

if existing_mode == "routed":
if mode_exists_status:
# if the port in an interface
if is_port:
db.cfgdb.mod_entry("PORT", port, {"mode": "{}".format(type)})
# if not port then is a port channel
elif not is_port:
db.cfgdb.mod_entry("PORTCHANNEL", port, {"mode": "{}".format(type)})

if not mode_exists_status:
port_data["mode"] = type
if is_port:
db.cfgdb.set_entry("PORT", port, port_data)
# if not port then is a port channel
elif not is_port:
db.cfgdb.set_entry("PORTCHANNEL", port, port_data)

if existing_mode == type:
ctx.fail("{} is already in the {} mode".format(port,type))
else:
if existing_mode == "access" and type == "trunk":
pass
if existing_mode == "trunk" and type == "access":
if clicommon.interface_is_tagged_member(db.cfgdb,port):
ctx.fail("{} is in {} mode and have tagged member(s).\nRemove tagged member(s) from {} to switch to {} mode".format(port,existing_mode,port,type))
if is_port:
db.cfgdb.mod_entry("PORT", port, {"mode": "{}".format(type)})
# if not port then is a port channel
elif not is_port:
db.cfgdb.mod_entry("PORTCHANNEL", port, {"mode": "{}".format(type)})

click.echo("{} switched from {} to {} mode".format(port, existing_mode, type))

# if mode type is routed
else:

if clicommon.interface_is_tagged_member(db.cfgdb,port):
ctx.fail("{} has tagged member(s). \nRemove them to change mode to {}".format(port,type))

if clicommon.interface_is_untagged_member(db.cfgdb,port):
ctx.fail("{} has untagged member. \nRemove it to change mode to {}".format(port,type))

if "mode" in port_data:
existing_mode = port_data["mode"]
else:
existing_mode = "routed"
mode_exists_status = False

if not mode_exists_status:
port_data["mode"] = type
if is_port:
db.cfgdb.set_entry("PORT", port, port_data)

# if not port then is a port channel
elif not is_port:
db.cfgdb.set_entry("PORTCHANNEL", port, port_data)
pass

elif mode_exists_status and existing_mode == type:
ctx.fail("{} is already in {} mode".format(port,type))

else:
if is_port:
db.cfgdb.mod_entry("PORT", port, {"mode": "{}".format(type)})
# if not port then is a port channel
elif not is_port:
db.cfgdb.mod_entry("PORTCHANNEL", port, {"mode": "{}".format(type)})

click.echo("{} switched from {} to {} mode".format(port,existing_mode,type))

Loading
Loading