Skip to content

Commit

Permalink
add VLAN_MEMBER case
Browse files Browse the repository at this point in the history
  • Loading branch information
isabelmsft committed Sep 7, 2022
1 parent 74b22e5 commit 3ab82c1
Showing 1 changed file with 63 additions and 48 deletions.
111 changes: 63 additions & 48 deletions config/vlan.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,46 +135,54 @@ def add_vlan_member(db, vid, port, untagged):

log.log_info("'vlan member add {} {}' executing...".format(vid, port))

if not clicommon.is_vlanid_in_range(vid):
ctx.fail("Invalid VLAN ID {} (1-4094)".format(vid))

vlan = 'Vlan{}'.format(vid)
if clicommon.check_if_vlanid_exist(db.cfgdb, vlan) == False:
ctx.fail("{} does not exist".format(vlan))

if ADHOC_VALIDATION:
if not clicommon.is_vlanid_in_range(vid):
ctx.fail("Invalid VLAN ID {} (1-4094)".format(vid))

if clicommon.get_interface_naming_mode() == "alias":
alias = port
iface_alias_converter = clicommon.InterfaceAliasConverter(db)
port = iface_alias_converter.alias_to_name(alias)
if port is None:
ctx.fail("cannot find port name for alias {}".format(alias))

if clicommon.is_port_mirror_dst_port(db.cfgdb, port):
ctx.fail("{} is configured as mirror destination port".format(port))

if clicommon.is_port_vlan_member(db.cfgdb, port, vlan):
ctx.fail("{} is already a member of {}".format(port, vlan))

if clicommon.is_valid_port(db.cfgdb, port):
is_port = True
elif clicommon.is_valid_portchannel(db.cfgdb, port):
is_port = False
else:
ctx.fail("{} does not exist".format(port))

if (is_port and clicommon.is_port_router_interface(db.cfgdb, port)) or \
(not is_port and clicommon.is_pc_router_interface(db.cfgdb, port)):
ctx.fail("{} is a router interface!".format(port))
if clicommon.check_if_vlanid_exist(db.cfgdb, vlan) == False:
ctx.fail("{} does not exist".format(vlan))

if clicommon.get_interface_naming_mode() == "alias": # TODO: MISSING CONSTRAINT IN YANG MODEL
alias = port
iface_alias_converter = clicommon.InterfaceAliasConverter(db)
port = iface_alias_converter.alias_to_name(alias)
if port is None:
ctx.fail("cannot find port name for alias {}".format(alias))

if clicommon.is_port_mirror_dst_port(db.cfgdb, port): # TODO: MISSING CONSTRAINT IN YANG MODEL
ctx.fail("{} is configured as mirror destination port".format(port))

if clicommon.is_port_vlan_member(db.cfgdb, port, vlan): # TODO: MISSING CONSTRAINT IN YANG MODEL
ctx.fail("{} is already a member of {}".format(port, vlan))

if clicommon.is_valid_port(db.cfgdb, port):
is_port = True
elif clicommon.is_valid_portchannel(db.cfgdb, port):
is_port = False
else:
ctx.fail("{} does not exist".format(port))

if (is_port and clicommon.is_port_router_interface(db.cfgdb, port)) or \ # TODO: MISSING CONSTRAINT IN YANG MODEL
(not is_port and clicommon.is_pc_router_interface(db.cfgdb, port)):
ctx.fail("{} is a router interface!".format(port))

portchannel_member_table = db.cfgdb.get_table('PORTCHANNEL_MEMBER')
portchannel_member_table = db.cfgdb.get_table('PORTCHANNEL_MEMBER')

if (is_port and clicommon.interface_is_in_portchannel(portchannel_member_table, port)):
ctx.fail("{} is part of portchannel!".format(port))
if (is_port and clicommon.interface_is_in_portchannel(portchannel_member_table, port)): # TODO: MISSING CONSTRAINT IN YANG MODEL
ctx.fail("{} is part of portchannel!".format(port))

if (clicommon.interface_is_untagged_member(db.cfgdb, port) and untagged):
ctx.fail("{} is already untagged member!".format(port))
if (clicommon.interface_is_untagged_member(db.cfgdb, port) and untagged): # TODO: MISSING CONSTRAINT IN YANG MODEL
ctx.fail("{} is already untagged member!".format(port))

db.cfgdb.set_entry('VLAN_MEMBER', (vlan, port), {'tagging_mode': "untagged" if untagged else "tagged" })
config_db = ValidatedConfigDBConnector(db.cfgdb, ADHOC_VALIDATION)
try:
config_db.set_entry('VLAN_MEMBER', (vlan, port), {'tagging_mode': "untagged" if untagged else "tagged" })
except ValueError:
ctx.fail("Invalid VLAN ID {} (1-4094)".format(vid))
except JsonPatchConflict:
ctx.fail("{} does not exist".format(vlan))

@vlan_member.command('del')
@click.argument('vid', metavar='<vid>', required=True, type=int)
Expand All @@ -187,22 +195,29 @@ def del_vlan_member(db, vid, port):

log.log_info("'vlan member del {} {}' executing...".format(vid, port))

if not clicommon.is_vlanid_in_range(vid):
ctx.fail("Invalid VLAN ID {} (1-4094)".format(vid))

vlan = 'Vlan{}'.format(vid)
if clicommon.check_if_vlanid_exist(db.cfgdb, vlan) == False:
ctx.fail("{} does not exist".format(vlan))
if ADHOC_VALIDATION:
if not clicommon.is_vlanid_in_range(vid):
ctx.fail("Invalid VLAN ID {} (1-4094)".format(vid))

if clicommon.get_interface_naming_mode() == "alias":
alias = port
iface_alias_converter = clicommon.InterfaceAliasConverter(db)
port = iface_alias_converter.alias_to_name(alias)
if port is None:
ctx.fail("cannot find port name for alias {}".format(alias))
if clicommon.check_if_vlanid_exist(db.cfgdb, vlan) == False:
ctx.fail("{} does not exist".format(vlan))

if not clicommon.is_port_vlan_member(db.cfgdb, port, vlan):
ctx.fail("{} is not a member of {}".format(port, vlan))
if clicommon.get_interface_naming_mode() == "alias": # TODO: MISSING CONSTRAINT IN YANG MODEL
alias = port
iface_alias_converter = clicommon.InterfaceAliasConverter(db)
port = iface_alias_converter.alias_to_name(alias)
if port is None:
ctx.fail("cannot find port name for alias {}".format(alias))

db.cfgdb.set_entry('VLAN_MEMBER', (vlan, port), None)
if not clicommon.is_port_vlan_member(db.cfgdb, port, vlan): # TODO: MISSING CONSTRAINT IN YANG MODEL
ctx.fail("{} is not a member of {}".format(port, vlan))

config_db = ValidatedConfigDBConnector(db.cfgdb, ADHOC_VALIDATION)
try:
config_db.set_entry('VLAN_MEMBER', (vlan, port), None)
except ValueError:
ctx.fail("Invalid VLAN ID {} (1-4094)".format(vid))
except JsonPatchConflict:
ctx.fail("{} does not exist".format(vlan))

0 comments on commit 3ab82c1

Please sign in to comment.