Skip to content

Commit

Permalink
Prevent shutdown/startup commands on invalid interface names "fixes (…
Browse files Browse the repository at this point in the history
…SONiC/issues/268)" (sonic-net#424)

Signed-off-by: [email protected]
  • Loading branch information
kirankella authored and jleveque committed Jan 22, 2019
1 parent 2f4a3b6 commit 9e49466
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

import sonic_platform
from swsssdk import ConfigDBConnector
from natsort import natsorted
from minigraph import parse_device_desc_xml

import aaa
Expand Down Expand Up @@ -77,14 +76,30 @@ def interface_alias_to_name(interface_alias):
if not port_dict:
click.echo("port_dict is None!")
raise click.Abort()
for port_name in natsorted(port_dict.keys()):
for port_name in port_dict.keys():
if interface_alias == port_dict[port_name]['alias']:
return port_name
click.echo("Invalid interface {}".format(interface_alias))

return None


def interface_name_is_valid(interface_name):
"""Check if the interface name is valid
"""
config_db = ConfigDBConnector()
config_db.connect()
port_dict = config_db.get_table('PORT')

if interface_name 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_name == port_name:
return True
return False

def interface_name_to_alias(interface_name):
"""Return alias interface name if default name is given as argument
"""
Expand All @@ -96,10 +111,9 @@ def interface_name_to_alias(interface_name):
if not port_dict:
click.echo("port_dict is None!")
raise click.Abort()
for port_name in natsorted(port_dict.keys()):
for port_name in port_dict.keys():
if interface_name == port_name:
return port_dict[port_name]['alias']
click.echo("Invalid interface {}".format(interface_alias))

return None

Expand Down Expand Up @@ -823,6 +837,9 @@ def startup(ctx):
config_db = ctx.obj['config_db']
interface_name = ctx.obj['interface_name']

if interface_name_is_valid(interface_name) is False:
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"})
elif interface_name.startswith("PortChannel"):
Expand All @@ -838,6 +855,9 @@ def shutdown(ctx):
config_db = ctx.obj['config_db']
interface_name = ctx.obj['interface_name']

if interface_name_is_valid(interface_name) is False:
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"})
elif interface_name.startswith("PortChannel"):
Expand Down

0 comments on commit 9e49466

Please sign in to comment.