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

Validate interface name length #23

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions config/fabric.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,39 @@ def error_threshold(crccells, rxcells, namespace):
config_db.mod_entry("FABRIC_MONITOR", "FABRIC_MONITOR_DATA",
{'monErrThreshCrcCells': crccells, 'monErrThreshRxCells': rxcells})

def setFabricPortMonitorState(state, namespace, ctx):
""" set the fabric port monitor state"""
# Connect to config database
config_db = ConfigDBConnector(use_unix_socket_path=True, namespace=namespace)
config_db.connect()

# Make sure configuration data exists
monitorData = config_db.get_all(config_db.CONFIG_DB, "FABRIC_MONITOR|FABRIC_MONITOR_DATA")
if not bool(monitorData):
ctx.fail("Fabric monitor configuration data not present")

# Update entry
config_db.mod_entry("FABRIC_MONITOR", "FABRIC_MONITOR_DATA",
{'monState': state})

#
# 'config fabric port montior state <enable/disable>'
#
@monitor.command()
@click.argument('state', metavar='<state>', required=True)
@multi_asic_util.multi_asic_click_option_namespace
def state(state, namespace):
"""FABRIC PORT MONITOR STATE configuration tasks"""
ctx = click.get_current_context()

n_asics = multi_asic.get_num_asics()
if n_asics > 1 and namespace is None:
ns_list = multi_asic.get_namespace_list()
for namespace in ns_list:
setFabricPortMonitorState(state, namespace, ctx)
else:
setFabricPortMonitorState(state, namespace, ctx)

#
# 'config fabric port monitor poll ...'
#
Expand Down
74 changes: 30 additions & 44 deletions config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
from utilities_common import bgp_util
import utilities_common.cli as clicommon
from utilities_common.helper import get_port_pbh_binding, get_port_acl_binding, update_config
from utilities_common.helper import validate_interface_name_length
from utilities_common.general import load_db_config, load_module_from_source
from .validated_config_db_connector import ValidatedConfigDBConnector
import utilities_common.multi_asic as multi_asic_util
Expand All @@ -57,7 +58,6 @@
from .config_mgmt import ConfigMgmtDPB, ConfigMgmt
from . import mclag
from . import syslog
from . import switchport
from . import dns

# mock masic APIs for unit test
Expand Down Expand Up @@ -98,15 +98,15 @@

CFG_PORTCHANNEL_PREFIX = "PortChannel"
CFG_PORTCHANNEL_PREFIX_LEN = 11
CFG_PORTCHANNEL_NAME_TOTAL_LEN_MAX = 15
CFG_PORTCHANNEL_MAX_VAL = 9999
CFG_PORTCHANNEL_NO="<0-9999>"

IFNAMSIZ = 16

PORT_MTU = "mtu"
PORT_SPEED = "speed"
PORT_TPID = "tpid"
DEFAULT_TPID = "0x8100"
PORT_MODE= "switchport_mode"

DOM_CONFIG_SUPPORTED_SUBPORTS = ['0', '1']

Expand Down Expand Up @@ -425,7 +425,7 @@ def is_portchannel_name_valid(portchannel_name):
if (portchannel_name[CFG_PORTCHANNEL_PREFIX_LEN:].isdigit() is False or
int(portchannel_name[CFG_PORTCHANNEL_PREFIX_LEN:]) > CFG_PORTCHANNEL_MAX_VAL) :
return False
if len(portchannel_name) > CFG_PORTCHANNEL_NAME_TOTAL_LEN_MAX:
if validate_interface_name_length(portchannel_name) is False:
return False
return True

Expand Down Expand Up @@ -1215,9 +1215,6 @@ def config(ctx):
# DNS module
config.add_command(dns.dns)

# Switchport module
config.add_command(switchport.switchport)

@config.command()
@click.option('-y', '--yes', is_flag=True, callback=_abort_if_false,
expose_value=False, prompt='Existing files will be overwritten, continue?')
Expand Down Expand Up @@ -2173,8 +2170,8 @@ def add_portchannel(ctx, portchannel_name, min_links, fallback, fast_rate):
db = ValidatedConfigDBConnector(ctx.obj['db'])
if ADHOC_VALIDATION:
if is_portchannel_name_valid(portchannel_name) != True:
ctx.fail("{} is invalid!, name should have prefix '{}' and suffix '{}'"
.format(portchannel_name, CFG_PORTCHANNEL_PREFIX, CFG_PORTCHANNEL_NO))
ctx.fail("{} is invalid!, name should have prefix '{}' and suffix '{}' and its length should not exceed {} characters"
.format(portchannel_name, CFG_PORTCHANNEL_PREFIX, CFG_PORTCHANNEL_NO, IFNAMSIZ))
if is_portchannel_present_in_db(db, portchannel_name):
ctx.fail("{} already exists!".format(portchannel_name)) # TODO: MISSING CONSTRAINT IN YANG MODEL

Expand Down Expand Up @@ -2994,7 +2991,14 @@ def _qos_update_ports(ctx, ports, dry_run, json_data):
for table_name in tables_multi_index:
entries = config_db.get_keys(table_name)
for key in entries:
port, _ = key
# Add support for chassis/multi-dut:
# on a single-dut, key = ('Ethernet136', '6')
# while on a chassis, key = ('str2-chassis-lcx-1', 'Asic0', 'Ethernet84', '5')
for element in key:
if element.startswith('Eth'):
port = element
break
assert port is not None, "Port is not found in config DB"
if not port in portset_to_handle:
continue
config_db.set_entry(table_name, '|'.join(key), None)
Expand Down Expand Up @@ -4584,40 +4588,19 @@ def add(ctx, interface_name, ip_addr, gw):
if interface_name is None:
ctx.fail("'interface_name' is None!")

# Add a validation to check this interface is not a member in vlan before
# changing it to a router port
vlan_member_table = config_db.get_table('VLAN_MEMBER')
if (interface_is_in_vlan(vlan_member_table, interface_name)):
click.echo("Interface {} is a member of vlan\nAborting!".format(interface_name))
return

portchannel_member_table = config_db.get_table('PORTCHANNEL_MEMBER')

if interface_is_in_portchannel(portchannel_member_table, interface_name):
ctx.fail("{} is configured as a member of portchannel."
.format(interface_name))


# Add a validation to check this interface is in routed mode before
# assigning an IP address to it

sub_intf = False

if clicommon.is_valid_port(config_db, interface_name):
is_port = True
elif clicommon.is_valid_portchannel(config_db, interface_name):
is_port = False
else:
sub_intf = True

if not sub_intf:
interface_mode = "routed"
if is_port:
interface_data = config_db.get_entry('PORT',interface_name)
elif not is_port:
interface_data = config_db.get_entry('PORTCHANNEL',interface_name)

if "mode" in interface_data:
interface_mode = interface_data["mode"]

if interface_mode != "routed":
ctx.fail("Interface {} is not in routed mode!".format(interface_name))
return


try:
ip_address = ipaddress.ip_interface(ip_addr)
except ValueError as err:
Expand Down Expand Up @@ -5568,8 +5551,8 @@ def add_vrf(ctx, vrf_name):
config_db = ValidatedConfigDBConnector(ctx.obj['config_db'])
if not vrf_name.startswith("Vrf") and not (vrf_name == 'mgmt') and not (vrf_name == 'management'):
ctx.fail("'vrf_name' must begin with 'Vrf' or named 'mgmt'/'management' in case of ManagementVRF.")
if len(vrf_name) > 15:
ctx.fail("'vrf_name' is too long!")
if validate_interface_name_length(vrf_name) is False:
ctx.fail("'vrf_name' length should not exceed {} characters".format(IFNAMSIZ))
if is_vrf_exists(config_db, vrf_name):
ctx.fail("VRF {} already exists!".format(vrf_name))
elif (vrf_name == 'mgmt' or vrf_name == 'management'):
Expand All @@ -5588,8 +5571,8 @@ def del_vrf(ctx, vrf_name):
config_db = ValidatedConfigDBConnector(ctx.obj['config_db'])
if not vrf_name.startswith("Vrf") and not (vrf_name == 'mgmt') and not (vrf_name == 'management'):
ctx.fail("'vrf_name' must begin with 'Vrf' or named 'mgmt'/'management' in case of ManagementVRF.")
if len(vrf_name) > 15:
ctx.fail("'vrf_name' is too long!")
if validate_interface_name_length(vrf_name) is False:
ctx.fail("'vrf_name' length should not exceed {} characters".format(IFNAMSIZ))
syslog_table = config_db.get_table("SYSLOG_SERVER")
syslog_vrf_dev = "mgmt" if vrf_name == "management" else vrf_name
for syslog_entry, syslog_data in syslog_table.items():
Expand Down Expand Up @@ -6593,8 +6576,8 @@ def add_loopback(ctx, loopback_name):
config_db = ValidatedConfigDBConnector(ctx.obj['db'])
if ADHOC_VALIDATION:
if is_loopback_name_valid(loopback_name) is False:
ctx.fail("{} is invalid, name should have prefix '{}' and suffix '{}' "
.format(loopback_name, CFG_LOOPBACK_PREFIX, CFG_LOOPBACK_NO))
ctx.fail("{} is invalid, name should have prefix '{}' and suffix '{}' and should not exceed {} characters"
.format(loopback_name, CFG_LOOPBACK_PREFIX, CFG_LOOPBACK_NO, CFG_LOOPBACK_NAME_TOTAL_LEN_MAX))

lo_intfs = [k for k, v in config_db.get_table('LOOPBACK_INTERFACE').items() if type(k) != tuple]
if loopback_name in lo_intfs:
Expand Down Expand Up @@ -7341,6 +7324,9 @@ def add_subinterface(ctx, subinterface_name, vid):

if interface_alias is None:
ctx.fail("{} invalid subinterface".format(interface_alias))

if validate_interface_name_length(subinterface_name) is False:
ctx.fail("Subinterface name length should not exceed {} characters".format(IFNAMSIZ))

if interface_alias.startswith("Po") is True:
intf_table_name = CFG_PORTCHANNEL_PREFIX
Expand Down
137 changes: 0 additions & 137 deletions config/switchport.py

This file was deleted.

Loading
Loading