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

Switchport Mode & CLI Modified Fix #3247

Merged
merged 97 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
97 commits
Select commit Hold shift + click to select a range
3cc50b9
Switchport Modified Code as per suggestion
sabakram Mar 28, 2024
92fdc98
Fix for Vlan.py & Command Reference Supported Commands
sabakram Mar 29, 2024
7607552
Fix for error
sabakram Mar 29, 2024
c7cb85b
MOde fix
sabakram Mar 29, 2024
03952a4
Vlan_test.py fix for fails
sabakram Mar 29, 2024
79ad984
Fix for default mode fails
sabakram Apr 1, 2024
0326712
Fix for errors
sabakram Apr 1, 2024
48d7110
Removed default mode routed
sabakram Apr 2, 2024
6f272ef
Fix for testcase backward compatibility
sabakram Apr 3, 2024
ba29d9d
Fix for typo
sabakram Apr 3, 2024
06694c3
Add testcases for code coverage
sabakram Apr 17, 2024
abc580c
Fix for code coverage
sabakram Apr 17, 2024
ec5d62f
Fix for testcase failures
sabakram Apr 18, 2024
93b64aa
Fix for Portchannel VLAN with swSwitchport modes testcase
sabakram Apr 18, 2024
d1f665e
Fix for Portchannel does not exist error
sabakram Apr 18, 2024
becd26d
Fix for typo
sabakram Apr 18, 2024
a851e9e
Fix for portchannel vlan tagging mode
sabakram Apr 18, 2024
4bc4077
Add testcases for code coverage
sabakram Apr 18, 2024
e7eaef9
Fix for vlan add portchannel member with switchport modes
sabakram Apr 18, 2024
bacc2de
Fix for switchport mode successfully switched message
sabakram Apr 18, 2024
d9e3134
Fix for switchport mode message
sabakram Apr 19, 2024
e35b1e6
Fix for portchannel creation error
sabakram Apr 19, 2024
2347170
Fix for portchannel creation failure
sabakram Apr 19, 2024
a384bd1
Adjusted testcases for code coverage
sabakram Apr 19, 2024
8184b04
Updated PORTCHANNEL cases
sabakram Apr 19, 2024
b4463ef
Updated PORTCHANNEL
sabakram Apr 19, 2024
e48edf5
Add test cases for code coverage
sabakram Apr 24, 2024
0c7ed5e
Fix for test case errors
sabakram Apr 24, 2024
8a0d7c3
Fix for db object error
sabakram Apr 24, 2024
fec4ba4
Fix for portchannel ip add test case failure
sabakram Apr 24, 2024
465b9ca
Fix for alias interface
sabakram Apr 24, 2024
e86a6d9
Fix for etp
sabakram Apr 25, 2024
57a04e8
Fix for alias mode test case failure
sabakram Apr 25, 2024
4fee806
Cover cli-common code coverage
sabakram Apr 25, 2024
554405f
Fix for db object error
sabakram Apr 25, 2024
fa7bd53
Interface Alias Mode
sabakram Apr 25, 2024
4bc59b3
Fixing Alias Issues
sabakram Apr 27, 2024
5ed04f5
Fix for Port name error in ALias mode
sabakram Apr 27, 2024
d1df5b7
Fix for Jsonpatch & Port Alias
sabakram Apr 27, 2024
afafd6a
Fix for patch name error
sabakram Apr 27, 2024
983fdd1
Fix for JSON patch error test case
sabakram Apr 27, 2024
ec2768a
Fix for ALias
sabakram Apr 27, 2024
fde3d19
Fixing JsonPatchConflict
sabakram Apr 27, 2024
3398b5c
Refix above
sabakram Apr 27, 2024
33cb52a
Fix for Asserstion Error
sabakram Apr 27, 2024
ceba62f
Fix for JsonConflict & ValueError
sabakram Apr 27, 2024
0e7de8c
Fix for Conflicts
sabakram Apr 27, 2024
1333873
Merge branch 'master' into Switchport_Modifed
sabakram May 2, 2024
cf6cf3b
Fix for json errors
sabakram May 2, 2024
17a96ee
Fix for pre-commit
sabakram May 2, 2024
26e783a
Fixing pre-commit & logical errors
sabakram May 2, 2024
293df59
Fixing Flake8 error
sabakram May 2, 2024
b262908
Fixing errors
sabakram May 2, 2024
56c25af
Fixing error
sabakram May 3, 2024
be5e31b
fix for add delete interface
sabakram May 3, 2024
cf244e8
fix for unit tests
sabakram May 3, 2024
5083543
fix for classmethod
sabakram May 3, 2024
ea3dd2c
fix classmethod
sabakram May 3, 2024
6ff82cc
fix mpl_test
sabakram May 3, 2024
1afac82
mpl test fix
sabakram May 3, 2024
6a154b0
fix vlan test
sabakram May 3, 2024
ea295ae
modified vlan test
sabakram May 3, 2024
1ba000a
test vlan delete fix
sabakram May 3, 2024
b190faf
fix error message
sabakram May 3, 2024
308ad3f
Code Coverage
sabakram May 3, 2024
d926e85
Fixing Runner
sabakram May 3, 2024
accd560
Fixing port alias
sabakram May 5, 2024
82fe3c0
Alias Fix
sabakram May 5, 2024
5eed6b6
Alias coverage
sabakram May 5, 2024
39eadca
ALias & Json patch
sabakram May 5, 2024
e90ec40
Indent Error Fix
sabakram May 5, 2024
c43ee87
Fix for Port Alias
sabakram May 5, 2024
849b55c
Fixing ALias
sabakram May 5, 2024
0bfe5a7
Fixing ALias
sabakram May 5, 2024
df71024
Fixing ALiass
sabakram May 5, 2024
654bc79
Fixed ALias & Json Patch
sabakram May 5, 2024
763f992
fix error messages
sabakram May 6, 2024
65801e2
fix coverage
sabakram May 6, 2024
08b7d6b
fix unit tests
sabakram May 6, 2024
e5797e3
fixed the unit tests
sabakram May 6, 2024
8f461ba
fixed switchport status
sabakram May 6, 2024
7d4ed50
fix test vlan
sabakram May 6, 2024
f457616
fix yang validation
sabakram May 6, 2024
9cb7ee9
fix add vlan test
sabakram May 6, 2024
d94af23
fix switchport
sabakram May 6, 2024
3f43e37
fixed vlan
sabakram May 6, 2024
bdc536b
fix unit test
sabakram May 6, 2024
f8b510c
Fix for JSON Patch
sabakram May 6, 2024
fb4e581
Json Patch fixes
sabakram May 6, 2024
706a895
json patch
sabakram May 6, 2024
961f896
json patch
sabakram May 6, 2024
55ded95
Fix json
sabakram May 7, 2024
f45232e
vlan validation
sabakram May 7, 2024
f481f1a
undo the argument
sabakram May 7, 2024
990df3f
Revert extras
sabakram May 9, 2024
51e4bfa
Fixing Error
sabakram May 9, 2024
bd7bd55
fixed python static errors
sabakram May 9, 2024
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
39 changes: 32 additions & 7 deletions config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@
from .config_mgmt import ConfigMgmtDPB, ConfigMgmt
from . import mclag
from . import syslog
from . import switchport
from . import dns


# mock masic APIs for unit test
try:
if os.environ["UTILITIES_UNIT_TESTING"] == "1" or os.environ["UTILITIES_UNIT_TESTING"] == "2":
Expand Down Expand Up @@ -105,6 +107,7 @@
PORT_SPEED = "speed"
PORT_TPID = "tpid"
DEFAULT_TPID = "0x8100"
PORT_MODE = "switchport_mode"

DOM_CONFIG_SUPPORTED_SUBPORTS = ['0', '1']

Expand Down Expand Up @@ -1231,6 +1234,9 @@ def config(ctx):
# DNS module
config.add_command(dns.dns)

# Switchport module
config.add_command(switchport.switchport)

@config.command()
@click.option('-y', '--yes', is_flag=True, callback=_abort_if_false,
expose_value=False, prompt='Existing files will be overwritten, continue?')
Expand Down Expand Up @@ -4639,19 +4645,38 @@ 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')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is this checked now?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have added a check for switchport mode routed, a routed port fulfill this behavior that a interface has a vlan membership or not. So this check becomes redundant after addition of new check.

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 = None
if is_port:
interface_data = config_db.get_entry('PORT', interface_name)
else:
interface_data = config_db.get_entry('PORTCHANNEL', interface_name)

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

if interface_mode == "trunk" or interface_mode == "access":
click.echo("Interface {} is in {} mode and needs to be in routed mode!".format(
interface_name, interface_mode))
return
try:
ip_address = ipaddress.ip_interface(ip_addr)
except ValueError as err:
Expand Down
135 changes: 135 additions & 0 deletions config/switchport.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import click
from .utils import log
import utilities_common.cli as clicommon

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


@click.group(cls=clicommon.AbbreviationGroup, name='switchport', invoke_without_command=False)
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(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:
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 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 mode_exists_status:
if existing_mode == "routed":
# 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 existing_mode == type:
ctx.fail("{} is already in {} 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 to {} mode".format(port, 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:
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

if mode_exists_status:
if 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 to {} mode".format(port, type))
Loading
Loading