Skip to content

Commit

Permalink
IP Address validation check (#530)
Browse files Browse the repository at this point in the history
  • Loading branch information
tapashdas authored and jleveque committed May 31, 2019
1 parent 1f326ad commit 82ef3ec
Showing 1 changed file with 29 additions and 18 deletions.
47 changes: 29 additions & 18 deletions config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import syslog

import sonic_device_util
import ipaddress
from swsssdk import ConfigDBConnector
from swsssdk import SonicV2Connector
from minigraph import parse_device_desc_xml
Expand Down Expand Up @@ -950,15 +951,20 @@ def add(ctx, interface_name, ip_addr):
if interface_name is None:
ctx.fail("'interface_name' is None!")

if interface_name.startswith("Ethernet"):
config_db.set_entry("INTERFACE", (interface_name, ip_addr), {"NULL": "NULL"})
elif interface_name.startswith("PortChannel"):
config_db.set_entry("PORTCHANNEL_INTERFACE", (interface_name, ip_addr), {"NULL": "NULL"})
elif interface_name.startswith("Vlan"):
config_db.set_entry("VLAN_INTERFACE", (interface_name, ip_addr), {"NULL": "NULL"})
elif interface_name.startswith("Loopback"):
config_db.set_entry("LOOPBACK_INTERFACE", (interface_name, ip_addr), {"NULL": "NULL"})

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"})
elif interface_name.startswith("PortChannel"):
config_db.set_entry("PORTCHANNEL_INTERFACE", (interface_name, ip_addr), {"NULL": "NULL"})
elif interface_name.startswith("Vlan"):
config_db.set_entry("VLAN_INTERFACE", (interface_name, ip_addr), {"NULL": "NULL"})
elif interface_name.startswith("Loopback"):
config_db.set_entry("LOOPBACK_INTERFACE", (interface_name, ip_addr), {"NULL": "NULL"})
else:
ctx.fail("'interface_name' is not valid. Valid names [Ethernet/PortChannel/Vlan/Loopback]")
except ValueError:
ctx.fail("'ip_addr' is not valid.")

#
# 'del' subcommand
Expand All @@ -976,15 +982,20 @@ def remove(ctx, interface_name, ip_addr):
if interface_name is None:
ctx.fail("'interface_name' is None!")

if interface_name.startswith("Ethernet"):
config_db.set_entry("INTERFACE", (interface_name, ip_addr), None)
elif interface_name.startswith("PortChannel"):
config_db.set_entry("PORTCHANNEL_INTERFACE", (interface_name, ip_addr), None)
elif interface_name.startswith("Vlan"):
config_db.set_entry("VLAN_INTERFACE", (interface_name, ip_addr), None)
elif interface_name.startswith("Loopback"):
config_db.set_entry("LOOPBACK_INTERFACE", (interface_name, ip_addr), None)

try:
ipaddress.ip_network(unicode(ip_addr), strict=False)
if interface_name.startswith("Ethernet"):
config_db.set_entry("INTERFACE", (interface_name, ip_addr), None)
elif interface_name.startswith("PortChannel"):
config_db.set_entry("PORTCHANNEL_INTERFACE", (interface_name, ip_addr), None)
elif interface_name.startswith("Vlan"):
config_db.set_entry("VLAN_INTERFACE", (interface_name, ip_addr), None)
elif interface_name.startswith("Loopback"):
config_db.set_entry("LOOPBACK_INTERFACE", (interface_name, ip_addr), None)
else:
ctx.fail("'interface_name' is not valid. Valid names [Ethernet/PortChannel/Vlan/Loopback]")
except ValueError:
ctx.fail("'ip_addr' is not valid.")
#
# 'acl' group ('config acl ...')
#
Expand Down

0 comments on commit 82ef3ec

Please sign in to comment.