Skip to content

Commit

Permalink
Add sub port interface to config interface ip add/del (#651)
Browse files Browse the repository at this point in the history
* Support sub port interface admin status change

Signed-off-by: Wenda Ni <[email protected]>

* Initialize vlan_id as an empty string

Signed-off-by: Wenda Ni <[email protected]>

* Remove parentheses

Signed-off-by: Wenda Ni <[email protected]>

* Take care of parent port being port channel

Signed-off-by: Wenda Ni <[email protected]>

* Add sub port interface to `config interface ip add/del`

Signed-off-by: Wenda Ni <[email protected]>
  • Loading branch information
wendani authored Oct 3, 2019
1 parent 18df3ae commit 7a68558
Showing 1 changed file with 59 additions and 15 deletions.
74 changes: 59 additions & 15 deletions config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

SONIC_CFGGEN_PATH = '/usr/local/bin/sonic-cfggen'
SYSLOG_IDENTIFIER = "config"
VLAN_SUB_INTERFACE_SEPARATOR = '.'

# ========================== Syslog wrappers ==========================

Expand Down Expand Up @@ -86,16 +87,26 @@ def interface_alias_to_name(interface_alias):
config_db.connect()
port_dict = config_db.get_table('PORT')

vlan_id = ""
sub_intf_sep_idx = -1
if interface_alias is not None:
sub_intf_sep_idx = interface_alias.find(VLAN_SUB_INTERFACE_SEPARATOR)
if sub_intf_sep_idx != -1:
vlan_id = interface_alias[sub_intf_sep_idx + 1:]
# interface_alias holds the parent port name so the subsequent logic still applies
interface_alias = interface_alias[:sub_intf_sep_idx]

if interface_alias is not None:
if not port_dict:
click.echo("port_dict is None!")
raise click.Abort()
for port_name in port_dict.keys():
if interface_alias == port_dict[port_name]['alias']:
return port_name
return port_name if sub_intf_sep_idx == -1 else port_name + VLAN_SUB_INTERFACE_SEPARATOR + vlan_id

# Interface alias not in port_dict, just return interface_alias
return interface_alias
# Interface alias not in port_dict, just return interface_alias, e.g.,
# portchannel is passed in as argument, which does not have an alias
return interface_alias if sub_intf_sep_idx == -1 else interface_alias + VLAN_SUB_INTERFACE_SEPARATOR + vlan_id


def interface_name_is_valid(interface_name):
Expand All @@ -105,6 +116,7 @@ def interface_name_is_valid(interface_name):
config_db.connect()
port_dict = config_db.get_table('PORT')
port_channel_dict = config_db.get_table('PORTCHANNEL')
sub_port_intf_dict = config_db.get_table('VLAN_SUB_INTERFACE')

if get_interface_naming_mode() == "alias":
interface_name = interface_alias_to_name(interface_name)
Expand All @@ -120,6 +132,10 @@ def interface_name_is_valid(interface_name):
for port_channel_name in port_channel_dict.keys():
if interface_name == port_channel_name:
return True
if sub_port_intf_dict:
for sub_port_intf_name in sub_port_intf_dict.keys():
if interface_name == sub_port_intf_name:
return True
return False

def interface_name_to_alias(interface_name):
Expand Down Expand Up @@ -1037,9 +1053,15 @@ def startup(ctx, interface_name):
ctx.fail("Interface name is invalid. Please enter a valid interface name!!")

if interface_name.startswith("Ethernet"):
config_db.mod_entry("PORT", interface_name, {"admin_status": "up"})
if VLAN_SUB_INTERFACE_SEPARATOR in interface_name:
config_db.mod_entry("VLAN_SUB_INTERFACE", interface_name, {"admin_status": "up"})
else:
config_db.mod_entry("PORT", interface_name, {"admin_status": "up"})
elif interface_name.startswith("PortChannel"):
config_db.mod_entry("PORTCHANNEL", interface_name, {"admin_status": "up"})
if VLAN_SUB_INTERFACE_SEPARATOR in interface_name:
config_db.mod_entry("VLAN_SUB_INTERFACE", interface_name, {"admin_status": "up"})
else:
config_db.mod_entry("PORTCHANNEL", interface_name, {"admin_status": "up"})
#
# 'shutdown' subcommand
#
Expand All @@ -1059,9 +1081,15 @@ def shutdown(ctx, interface_name):
ctx.fail("Interface name is invalid. Please enter a valid interface name!!")

if interface_name.startswith("Ethernet"):
config_db.mod_entry("PORT", interface_name, {"admin_status": "down"})
if VLAN_SUB_INTERFACE_SEPARATOR in interface_name:
config_db.mod_entry("VLAN_SUB_INTERFACE", interface_name, {"admin_status": "down"})
else:
config_db.mod_entry("PORT", interface_name, {"admin_status": "down"})
elif interface_name.startswith("PortChannel"):
config_db.mod_entry("PORTCHANNEL", interface_name, {"admin_status": "down"})
if VLAN_SUB_INTERFACE_SEPARATOR in interface_name:
config_db.mod_entry("VLAN_SUB_INTERFACE", interface_name, {"admin_status": "down"})
else:
config_db.mod_entry("PORTCHANNEL", interface_name, {"admin_status": "down"})

#
# 'speed' subcommand
Expand Down Expand Up @@ -1113,11 +1141,19 @@ def add(ctx, interface_name, ip_addr):
try:
ipaddress.ip_network(unicode(ip_addr), strict=False)
if interface_name.startswith("Ethernet"):
config_db.set_entry("INTERFACE", (interface_name, ip_addr), {"NULL": "NULL"})
config_db.set_entry("INTERFACE", interface_name, {"NULL": "NULL"})
if VLAN_SUB_INTERFACE_SEPARATOR in interface_name:
config_db.set_entry("VLAN_SUB_INTERFACE", interface_name, {"admin_status": "up"})
config_db.set_entry("VLAN_SUB_INTERFACE", (interface_name, ip_addr), {"NULL": "NULL"})
else:
config_db.set_entry("INTERFACE", (interface_name, ip_addr), {"NULL": "NULL"})
config_db.set_entry("INTERFACE", interface_name, {"NULL": "NULL"})
elif interface_name.startswith("PortChannel"):
config_db.set_entry("PORTCHANNEL_INTERFACE", (interface_name, ip_addr), {"NULL": "NULL"})
config_db.set_entry("PORTCHANNEL_INTERFACE", interface_name, {"NULL": "NULL"})
if VLAN_SUB_INTERFACE_SEPARATOR in interface_name:
config_db.set_entry("VLAN_SUB_INTERFACE", interface_name, {"admin_status": "up"})
config_db.set_entry("VLAN_SUB_INTERFACE", (interface_name, ip_addr), {"NULL": "NULL"})
else:
config_db.set_entry("PORTCHANNEL_INTERFACE", (interface_name, ip_addr), {"NULL": "NULL"})
config_db.set_entry("PORTCHANNEL_INTERFACE", interface_name, {"NULL": "NULL"})
elif interface_name.startswith("Vlan"):
config_db.set_entry("VLAN_INTERFACE", (interface_name, ip_addr), {"NULL": "NULL"})
config_db.set_entry("VLAN_INTERFACE", interface_name, {"NULL": "NULL"})
Expand Down Expand Up @@ -1148,11 +1184,19 @@ def remove(ctx, interface_name, ip_addr):
try:
ipaddress.ip_network(unicode(ip_addr), strict=False)
if interface_name.startswith("Ethernet"):
config_db.set_entry("INTERFACE", (interface_name, ip_addr), None)
if_table = "INTERFACE"
if VLAN_SUB_INTERFACE_SEPARATOR in interface_name:
config_db.set_entry("VLAN_SUB_INTERFACE", (interface_name, ip_addr), None)
if_table = "VLAN_SUB_INTERFACE"
else:
config_db.set_entry("INTERFACE", (interface_name, ip_addr), None)
if_table = "INTERFACE"
elif interface_name.startswith("PortChannel"):
config_db.set_entry("PORTCHANNEL_INTERFACE", (interface_name, ip_addr), None)
if_table = "PORTCHANNEL_INTERFACE"
if VLAN_SUB_INTERFACE_SEPARATOR in interface_name:
config_db.set_entry("VLAN_SUB_INTERFACE", (interface_name, ip_addr), None)
if_table = "VLAN_SUB_INTERFACE"
else:
config_db.set_entry("PORTCHANNEL_INTERFACE", (interface_name, ip_addr), None)
if_table = "PORTCHANNEL_INTERFACE"
elif interface_name.startswith("Vlan"):
config_db.set_entry("VLAN_INTERFACE", (interface_name, ip_addr), None)
if_table = "VLAN_INTERFACE"
Expand Down

0 comments on commit 7a68558

Please sign in to comment.