Skip to content

Commit

Permalink
add new tables yang validation
Browse files Browse the repository at this point in the history
  • Loading branch information
isabelmsft committed Nov 27, 2022
1 parent 94a092d commit 82fd758
Show file tree
Hide file tree
Showing 2 changed files with 192 additions and 79 deletions.
211 changes: 143 additions & 68 deletions config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2881,55 +2881,79 @@ def warm_restart_enable(ctx, module):
@click.argument('seconds', metavar='<seconds>', required=True, type=int)
@click.pass_context
def warm_restart_neighsyncd_timer(ctx, seconds):
db = ctx.obj['db']
if seconds not in range(1, 9999):
ctx.fail("neighsyncd warm restart timer must be in range 1-9999")
db.mod_entry('WARM_RESTART', 'swss', {'neighsyncd_timer': seconds})
db = ValidatedConfigDBConnector(ctx.obj['db'])
if ADHOC_VALIDATION:
if seconds not in range(1, 9999):
ctx.fail("neighsyncd warm restart timer must be in range 1-9999")
try:
db.mod_entry('WARM_RESTART', 'swss', {'neighsyncd_timer': seconds})
except ValueError as e:
ctx.fail("Invalid ConfigDB. Error: {}".format(e))

@warm_restart.command('bgp_timer')
@click.argument('seconds', metavar='<seconds>', required=True, type=int)
@click.pass_context
def warm_restart_bgp_timer(ctx, seconds):
db = ctx.obj['db']
if seconds not in range(1, 3600):
ctx.fail("bgp warm restart timer must be in range 1-3600")
db.mod_entry('WARM_RESTART', 'bgp', {'bgp_timer': seconds})
db = ValidatedConfigDBConnector(ctx.obj['db'])
if ADHOC_VALIDATION:
if seconds not in range(1, 3600):
ctx.fail("bgp warm restart timer must be in range 1-3600")
try:
db.mod_entry('WARM_RESTART', 'bgp', {'bgp_timer': seconds})
except ValueError as e:
ctx.fail("Invalid ConfigDB. Error: {}".format(e))

@warm_restart.command('teamsyncd_timer')
@click.argument('seconds', metavar='<seconds>', required=True, type=int)
@click.pass_context
def warm_restart_teamsyncd_timer(ctx, seconds):
db = ctx.obj['db']
if seconds not in range(1, 3600):
ctx.fail("teamsyncd warm restart timer must be in range 1-3600")
db.mod_entry('WARM_RESTART', 'teamd', {'teamsyncd_timer': seconds})
db = ValidatedConfigDBConnector(ctx.obj['db'])
if ADHOC_VALIDATION:
if seconds not in range(1, 3600):
ctx.fail("teamsyncd warm restart timer must be in range 1-3600")
try:
db.mod_entry('WARM_RESTART', 'teamd', {'teamsyncd_timer': seconds})
except ValueError as e:
ctx.fail("Invalid ConfigDB. Error: {}".format(e))

@warm_restart.command('bgp_eoiu')
@click.argument('enable', metavar='<enable>', default='true', required=False, type=click.Choice(["true", "false"]))
@click.pass_context
def warm_restart_bgp_eoiu(ctx, enable):
db = ctx.obj['db']
db.mod_entry('WARM_RESTART', 'bgp', {'bgp_eoiu': enable})

db = ValidatedConfigDBConnector(ctx.obj['db'])
try:
db.mod_entry('WARM_RESTART', 'bgp', {'bgp_eoiu': enable})
except ValueError as e:
ctx.fail("Invalid ConfigDB. Error: {}".format(e))

def vrf_add_management_vrf(config_db):
"""Enable management vrf in config DB"""

config_db = ValidatedConfigDBConnector(config_db)
entry = config_db.get_entry('MGMT_VRF_CONFIG', "vrf_global")
if entry and entry['mgmtVrfEnabled'] == 'true' :
click.echo("ManagementVRF is already Enabled.")
return None
config_db.mod_entry('MGMT_VRF_CONFIG', "vrf_global", {"mgmtVrfEnabled": "true"})
try:
config_db.mod_entry('MGMT_VRF_CONFIG', "vrf_global", {"mgmtVrfEnabled": "true"})
except ValueError as e:
ctx = click.get_current_context()
ctx.fail("Invalid ConfigDB. Error: {}".format(e))


def vrf_delete_management_vrf(config_db):
"""Disable management vrf in config DB"""

config_db = ValidatedConfigDBConnector(config_db)
entry = config_db.get_entry('MGMT_VRF_CONFIG', "vrf_global")
if not entry or entry['mgmtVrfEnabled'] == 'false' :
click.echo("ManagementVRF is already Disabled.")
return None
config_db.mod_entry('MGMT_VRF_CONFIG', "vrf_global", {"mgmtVrfEnabled": "false"})
try:
config_db.mod_entry('MGMT_VRF_CONFIG', "vrf_global", {"mgmtVrfEnabled": "false"})
except ValueError as e:
ctx = click.get_current_context()
ctx.fail("Invalid ConfigDB. Error: {}".format(e))


@config.group(cls=clicommon.AbbreviationGroup)
Expand Down Expand Up @@ -4707,26 +4731,30 @@ def remove_queue(db, interface_name, queue_map):
@click.pass_context
def cable_length(ctx, interface_name, length):
"""Set interface cable length"""
config_db = ctx.obj["config_db"]
config_db = ValidatedConfigDBConnector(ctx.obj["config_db"])

if not is_dynamic_buffer_enabled(config_db):
ctx.fail("This command can only be supported on a system with dynamic buffer enabled")

if ADHOC_VALIDATION:
# Check whether port is legal
ports = config_db.get_entry("PORT", interface_name)
if not ports:
ctx.fail("Port {} doesn't exist".format(interface_name))

# Check whether port is legal
ports = config_db.get_entry("PORT", interface_name)
if not ports:
ctx.fail("Port {} doesn't exist".format(interface_name))

try:
assert "m" == length[-1]
except Exception:
ctx.fail("Invalid cable length. Should be in format <num>m, like 300m".format(cable_length))
try:
assert "m" == length[-1]
except Exception:
ctx.fail("Invalid cable length. Should be in format <num>m, like 300m".format(cable_length))

keys = config_db.get_keys("CABLE_LENGTH")

cable_length_set = {}
cable_length_set[interface_name] = length
config_db.mod_entry("CABLE_LENGTH", keys[0], cable_length_set)
try:
config_db.mod_entry("CABLE_LENGTH", keys[0], cable_length_set)
except ValueError as e:
ctx.fail("Invalid ConfigDB. Error: {}".format(e))

#
# 'transceiver' subgroup ('config interface transceiver ...')
Expand Down Expand Up @@ -5151,7 +5179,10 @@ def add_vrf(ctx, vrf_name):
if (vrf_name == 'mgmt' or vrf_name == 'management'):
vrf_add_management_vrf(config_db)
else:
config_db.set_entry('VRF', vrf_name, {"NULL": "NULL"})
try:
config_db.set_entry('VRF', vrf_name, {"NULL": "NULL"})
except ValueError as e:
ctx.fail("Invalid ConfigDB. Error: {}".format(e))

@vrf.command('del')
@click.argument('vrf_name', metavar='<vrf_name>', required=True)
Expand All @@ -5173,7 +5204,10 @@ def del_vrf(ctx, vrf_name):
vrf_delete_management_vrf(config_db)
else:
del_interface_bind_to_vrf(config_db, vrf_name)
config_db.set_entry('VRF', vrf_name, None)
try:
config_db.set_entry('VRF', vrf_name, None)
except JsonPatchConflict as e:
ctx.fail("Invalid ConfigDB. Error: {}".format(e))
click.echo("VRF {} deleted and all associated IP addresses removed.".format(vrf_name))

@vrf.command('add_vrf_vni_map')
Expand Down Expand Up @@ -6273,15 +6307,18 @@ def sflow(ctx):
@click.pass_context
def enable(ctx):
"""Enable sFlow"""
config_db = ctx.obj['db']
config_db = ValidatedConfigDBConnector(ctx.obj['db'])
sflow_tbl = config_db.get_table('SFLOW')

if not sflow_tbl:
sflow_tbl = {'global': {'admin_state': 'up'}}
else:
sflow_tbl['global']['admin_state'] = 'up'

config_db.mod_entry('SFLOW', 'global', sflow_tbl['global'])
try:
config_db.mod_entry('SFLOW', 'global', sflow_tbl['global'])
except ValueError as e:
ctx.fail("Invalid ConfigDB. Error: {}".format(e))

try:
proc = subprocess.Popen("systemctl is-active sflow", shell=True, text=True, stdout=subprocess.PIPE)
Expand All @@ -6301,15 +6338,18 @@ def enable(ctx):
@click.pass_context
def disable(ctx):
"""Disable sFlow"""
config_db = ctx.obj['db']
config_db = ValidatedConfigDBConnector(ctx.obj['db'])
sflow_tbl = config_db.get_table('SFLOW')

if not sflow_tbl:
sflow_tbl = {'global': {'admin_state': 'down'}}
else:
sflow_tbl['global']['admin_state'] = 'down'

config_db.mod_entry('SFLOW', 'global', sflow_tbl['global'])
try:
config_db.mod_entry('SFLOW', 'global', sflow_tbl['global'])
except ValueError as e:
ctx.fail("Invalid ConfigDB. Error: {}".format(e))

#
# 'sflow' command ('config sflow polling-interval ...')
Expand All @@ -6320,17 +6360,21 @@ def disable(ctx):
@click.pass_context
def polling_int(ctx, interval):
"""Set polling-interval for counter-sampling (0 to disable)"""
if interval not in range(5, 301) and interval != 0:
click.echo("Polling interval must be between 5-300 (0 to disable)")
if ADHOC_VALIDATION:
if interval not in range(5, 301) and interval != 0:
click.echo("Polling interval must be between 5-300 (0 to disable)")

config_db = ctx.obj['db']
config_db = ValidatedConfigDBConnector(ctx.obj['db'])
sflow_tbl = config_db.get_table('SFLOW')

if not sflow_tbl:
sflow_tbl = {'global': {'admin_state': 'down'}}

sflow_tbl['global']['polling_interval'] = interval
config_db.mod_entry('SFLOW', 'global', sflow_tbl['global'])
try:
config_db.mod_entry('SFLOW', 'global', sflow_tbl['global'])
except ValueError as e:
ctx.fail("Invalid ConfigDB. Error: {}".format(e))

def is_valid_sample_rate(rate):
return rate.isdigit() and int(rate) in range(256, 8388608 + 1)
Expand All @@ -6352,18 +6396,25 @@ def interface(ctx):
@click.argument('ifname', metavar='<interface_name>', required=True, type=str)
@click.pass_context
def enable(ctx, ifname):
config_db = ctx.obj['db']
if not interface_name_is_valid(config_db, ifname) and ifname != 'all':
click.echo("Invalid interface name")
return
config_db = ValidatedConfigDBConnector(ctx.obj['db'])
if ADHOC_VALIDATION:
if not interface_name_is_valid(config_db, ifname) and ifname != 'all':
click.echo("Invalid interface name")
return

intf_dict = config_db.get_table('SFLOW_SESSION')

if intf_dict and ifname in intf_dict:
intf_dict[ifname]['admin_state'] = 'up'
config_db.mod_entry('SFLOW_SESSION', ifname, intf_dict[ifname])
try:
config_db.mod_entry('SFLOW_SESSION', ifname, intf_dict[ifname])
except ValueError as e:
ctx.fail("Invalid ConfigDB. Error: {}".format(e))
else:
config_db.mod_entry('SFLOW_SESSION', ifname, {'admin_state': 'up'})
try:
config_db.mod_entry('SFLOW_SESSION', ifname, {'admin_state': 'up'})
except ValueError as e:
ctx.fail("Invalid ConfigDB. Error: {}".format(e))

#
# 'sflow' command ('config sflow interface disable ...')
Expand All @@ -6372,19 +6423,26 @@ def enable(ctx, ifname):
@click.argument('ifname', metavar='<interface_name>', required=True, type=str)
@click.pass_context
def disable(ctx, ifname):
config_db = ctx.obj['db']
if not interface_name_is_valid(config_db, ifname) and ifname != 'all':
click.echo("Invalid interface name")
return
config_db = ValidatedConfigDBConnector(ctx.obj['db'])
if ADHOC_VALIDATION:
if not interface_name_is_valid(config_db, ifname) and ifname != 'all':
click.echo("Invalid interface name")
return

intf_dict = config_db.get_table('SFLOW_SESSION')

if intf_dict and ifname in intf_dict:
intf_dict[ifname]['admin_state'] = 'down'
config_db.mod_entry('SFLOW_SESSION', ifname, intf_dict[ifname])
try:
config_db.mod_entry('SFLOW_SESSION', ifname, intf_dict[ifname])
except ValueError as e:
ctx.fail("Invalid ConfigDB. Error: {}".format(e))
else:
config_db.mod_entry('SFLOW_SESSION', ifname,
{'admin_state': 'down'})
try:
config_db.mod_entry('SFLOW_SESSION', ifname,
{'admin_state': 'down'})
except ValueError as e:
ctx.fail("Invalid ConfigDB. Error: {}".format(e))

#
# 'sflow' command ('config sflow interface sample-rate ...')
Expand All @@ -6394,13 +6452,14 @@ def disable(ctx, ifname):
@click.argument('rate', metavar='<sample_rate>', required=True, type=str)
@click.pass_context
def sample_rate(ctx, ifname, rate):
config_db = ctx.obj['db']
if not interface_name_is_valid(config_db, ifname) and ifname != 'all':
click.echo('Invalid interface name')
return
if not is_valid_sample_rate(rate) and rate != 'default':
click.echo('Error: Sample rate must be between 256 and 8388608 or default')
return
config_db = ValidatedConfigDBConnector(ctx.obj['db'])
if ADHOC_VALIDATION:
if not interface_name_is_valid(config_db, ifname) and ifname != 'all':
click.echo('Invalid interface name')
return
if not is_valid_sample_rate(rate) and rate != 'default':
click.echo('Error: Sample rate must be between 256 and 8388608 or default')
return

sess_dict = config_db.get_table('SFLOW_SESSION')

Expand All @@ -6409,13 +6468,22 @@ def sample_rate(ctx, ifname, rate):
if 'sample_rate' not in sess_dict[ifname]:
return
del sess_dict[ifname]['sample_rate']
config_db.set_entry('SFLOW_SESSION', ifname, sess_dict[ifname])
try:
config_db.set_entry('SFLOW_SESSION', ifname, sess_dict[ifname])
except ValueError as e:
ctx.fail("Invalid ConfigDB. Error: {}".format(e))
return
sess_dict[ifname]['sample_rate'] = rate
config_db.mod_entry('SFLOW_SESSION', ifname, sess_dict[ifname])
try:
config_db.mod_entry('SFLOW_SESSION', ifname, sess_dict[ifname])
except ValueError as e:
ctx.fail("Invalid ConfigDB. Error: {}".format(e))
else:
if rate != 'default':
config_db.mod_entry('SFLOW_SESSION', ifname, {'sample_rate': rate})
try:
config_db.mod_entry('SFLOW_SESSION', ifname, {'sample_rate': rate})
except ValueError as e:
ctx.fail("Invalid ConfigDB. Error: {}".format(e))


#
Expand Down Expand Up @@ -6510,11 +6578,12 @@ def agent_id(ctx):
@click.pass_context
def add(ctx, ifname):
"""Add sFlow agent information"""
if ifname not in netifaces.interfaces():
click.echo("Invalid interface name")
return
if ADHOC_VALIDATION:
if ifname not in netifaces.interfaces():
click.echo("Invalid interface name")
return

config_db = ctx.obj['db']
config_db = ValidatedConfigDBConnector(ctx.obj['db'])
sflow_tbl = config_db.get_table('SFLOW')

if not sflow_tbl:
Expand All @@ -6525,7 +6594,10 @@ def add(ctx, ifname):
return

sflow_tbl['global']['agent_id'] = ifname
config_db.mod_entry('SFLOW', 'global', sflow_tbl['global'])
try:
config_db.mod_entry('SFLOW', 'global', sflow_tbl['global'])
except ValueError as e:
ctx.fail("Invalid ConfigDB. Error: {}".format(e))

#
# 'sflow' command ('config sflow agent-id del')
Expand All @@ -6534,7 +6606,7 @@ def add(ctx, ifname):
@click.pass_context
def delete(ctx):
"""Delete sFlow agent information"""
config_db = ctx.obj['db']
config_db = ValidatedConfigDBConnector(ctx.obj['db'])
sflow_tbl = config_db.get_table('SFLOW')

if not sflow_tbl:
Expand All @@ -6545,7 +6617,10 @@ def delete(ctx):
return

sflow_tbl['global'].pop('agent_id')
config_db.set_entry('SFLOW', 'global', sflow_tbl['global'])
try:
config_db.set_entry('SFLOW', 'global', sflow_tbl['global'])
except ValueError as e:
ctx.fail("Invalid ConfigDB. Error: {}".format(e))

#
# set ipv6 link local mode on a given interface
Expand Down
Loading

0 comments on commit 82fd758

Please sign in to comment.