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

Add sub port interface to config interface ip add/del #651

Merged
merged 9 commits into from
Oct 3, 2019
74 changes: 59 additions & 15 deletions config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

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

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

Expand Down Expand Up @@ -74,16 +75,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 @@ -93,6 +104,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 @@ -108,6 +120,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 @@ -1018,9 +1034,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 @@ -1040,9 +1062,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 @@ -1094,11 +1122,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"})
Copy link
Contributor

Choose a reason for hiding this comment

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

May be it is good now to just set a table_name here and use config_db.set_entry once instead of repeated lines of same code.

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 @@ -1129,11 +1165,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