Skip to content

Commit 330b101

Browse files
Modification of Vlan.py and creation of switchport.py
- What I did Modified config/vlan.py to add new commands with multiple and range options Creation of config/switchport.py which will deal with default Vlan1 Modified the show interface command so it will show the status of port being access or trunk. Before it did not show about access mode only trunk. Modified the Command-Reference.md file to add new commands in it with usage and examples Added new utility functions in utilities_common/cli.py for new Vlan.py and switchport.py commands - How I did Added new commands and modified the existing commands in config/vlan.py for example to accept multiple vids and range of vids in add|del vlan and add|del vlan member as well as except flag in vlan member command Creation of config/switchport.py file to add commands which will deal only with Default Vlan1 which was unable to be defined or assigned in SONiC before New utility functions in utilities_common/cli.py changes in scripts/intfutil. Modified the existing function and now it will display if an interface is in access mode or trunk mode with routed mode. - How to verify it New commands have been added in Command-Reference.md All the syntax and examples have been added there and they can be verified by running the specific command Signed-off-by: Muhammad Umar Asad <[email protected]> < xFlow Research Inc. >
1 parent 414e239 commit 330b101

File tree

7 files changed

+1028
-288
lines changed

7 files changed

+1028
-288
lines changed

config/main.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
from .config_mgmt import ConfigMgmtDPB
5050
from . import mclag
5151
from . import syslog
52+
from . import switchport
5253

5354
# mock masic APIs for unit test
5455
try:
@@ -93,6 +94,8 @@
9394
CFG_PORTCHANNEL_NO="<0-9999>"
9495

9596
PORT_MTU = "mtu"
97+
PORT_MODE= "switchport_mode"
98+
9699
PORT_SPEED = "speed"
97100
PORT_TPID = "tpid"
98101
DEFAULT_TPID = "0x8100"
@@ -1227,6 +1230,7 @@ def config(ctx):
12271230
config.add_command(nat.nat)
12281231
config.add_command(vlan.vlan)
12291232
config.add_command(vxlan.vxlan)
1233+
config.add_command(switchport.switchport)
12301234

12311235
#add mclag commands
12321236
config.add_command(mclag.mclag)

config/switchport.py

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
import click
2+
from .utils import log
3+
import utilities_common.cli as clicommon
4+
5+
# since default vlan id is 1
6+
default_vid = 1
7+
8+
#
9+
# 'switchport' mode ('config switchport ...')
10+
#
11+
12+
13+
@click.group(cls=clicommon.AbbreviationGroup, name='switchport')
14+
def switchport():
15+
"""Switchport mode configuration tasks"""
16+
pass
17+
18+
19+
@switchport.add_command("mode")
20+
@click.argument("type", metavar="<mode_type>", Required=True, type=click.Choice(["access", "trunk", "routed"]))
21+
@click.argument("port", metavar="port", required=True)
22+
@clicommon.pass_db
23+
def access_mode(db, type, port):
24+
"""switchport mode help commands\nmode_type can either be:\n\t access for untagged mode\n\t trunk for tagged mode \n\t routed for non vlan mode"""
25+
26+
ctx = click.get_current_context()
27+
28+
log.log_info(
29+
"'switchport mode {} {}' executing...".format(type, port))
30+
31+
vlan = 'Vlan{}'.format(default_vid)
32+
33+
# checking if port name with alias exists
34+
if clicommon.get_interface_naming_mode() == "alias":
35+
alias = port
36+
iface_alias_converter = clicommon.InterfaceAliasConverter(db)
37+
port = iface_alias_converter.alias_to_name(port)
38+
if port is None:
39+
ctx.fail("cannot find port name for alias {}".format(alias))
40+
41+
# mode type is either access or trunk
42+
if type != "routed":
43+
44+
# checking if default Vlan has been created or not
45+
if not clicommon.check_if_vlanid_exist(db.cfgdb, vlan):
46+
47+
db.cfgdb.set_entry('VLAN', vlan, {'vlanid': default_vid})
48+
49+
log.log_info(
50+
"'vlan add {}' in switchport mode executing...".format(default_vid))
51+
52+
# tagging_mode will be untagged if access and tagged if trunk
53+
if clicommon.is_port_mirror_dst_port(db.cfgdb, port):
54+
ctx.fail("{} is configured as mirror destination port".format(port))
55+
56+
if clicommon.is_port_vlan_member(db.cfgdb, port, vlan):
57+
ctx.fail("{} is already a member of {}".format(port, vlan))
58+
59+
if clicommon.is_valid_port(db.cfgdb, port):
60+
is_port = True
61+
elif clicommon.is_valid_portchannel(db.cfgdb, port):
62+
is_port = False
63+
else:
64+
ctx.fail("{} does not exist".format(port))
65+
66+
if (is_port and clicommon.is_port_router_interface(db.cfgdb, port)) or \
67+
(not is_port and clicommon.is_pc_router_interface(db.cfgdb, port)):
68+
ctx.fail("{} is a router interface!".format(port))
69+
70+
portchannel_member_table = db.cfgdb.get_table('PORTCHANNEL_MEMBER')
71+
72+
if (is_port and clicommon.interface_is_in_portchannel(portchannel_member_table, port)):
73+
ctx.fail("{} is part of portchannel!".format(port))
74+
75+
# checking if it exists in default Vlan1
76+
if clicommon.port_vlan_member_exist(db, vlan, port):
77+
78+
existing_port_vlan_status = clicommon.get_existing_port_vlan_status(
79+
db, vlan, port)
80+
existing_status = "access" if existing_port_vlan_status == "untagged" else "trunk"
81+
82+
if (type == existing_status):
83+
ctx.fail("{} is already in {} mode!".format(port, type))
84+
85+
elif (type != existing_status):
86+
87+
if (type == "trunk" and existing_status == "access"):
88+
db.cfgdb.mod_entry('VLAN_MEMBER', (vlan, port), {
89+
'tagging_mode': "tagged"})
90+
91+
elif (type == "access" and existing_status == "trunk"):
92+
db.cfgdb.mod_entry('VLAN_MEMBER', (vlan, port), {
93+
'tagging_mode': "untagged"})
94+
95+
# switched the mode of already defined switchport
96+
click.echo("{} switched from {} to {} mode".format(
97+
port, existing_status, type))
98+
99+
# if it not exists already set entry
100+
else:
101+
db.cfgdb.set_entry('VLAN_MEMBER', (vlan, port), {
102+
'tagging_mode': "untagged" if type == "access" else "tagged"})
103+
104+
click.echo("{} switched to {} mode. Added to default {}".format(
105+
port, type, vlan))
106+
107+
# if mode type is routed
108+
else:
109+
110+
if clicommon.check_if_vlanid_exist(db.cfgdb, vlan) == False:
111+
ctx.fail("{} is already in routed mode".format(port))
112+
113+
if not clicommon.is_port_vlan_member(db.cfgdb, port, vlan):
114+
ctx.fail("{} is already in routed mode".format(port))
115+
116+
db.cfgdb.set_entry('VLAN_MEMBER', (vlan, port), None)
117+
118+
click.echo("{} is in {} mode. Removed from default {}".format(
119+
port, type, vlan))

0 commit comments

Comments
 (0)