Skip to content

Commit

Permalink
Added show command and test case.
Browse files Browse the repository at this point in the history
  • Loading branch information
AkhileshSamineni committed Apr 15, 2021
1 parent a04176d commit 56f74cd
Show file tree
Hide file tree
Showing 4 changed files with 211 additions and 19 deletions.
41 changes: 24 additions & 17 deletions config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3087,10 +3087,14 @@ def disable():
@click.argument('interface_name', metavar='<interface_name>', required=True)
def enable_use_link_local_only(ctx, interface_name):
"""Enable IPv6 link local address on interface"""
config_db = ctx.obj["config_db"]
config_db = ConfigDBConnector()
config_db.connect()
ctx.obj = {}
ctx.obj['config_db'] = config_db
db = ctx.obj["config_db"]

if clicommon.get_interface_naming_mode() == "alias":
interface_name = interface_alias_to_name(config_db, interface_name)
interface_name = interface_alias_to_name(db, interface_name)
if interface_name is None:
ctx.fail("'interface_name' is None!")

Expand All @@ -3104,28 +3108,28 @@ def enable_use_link_local_only(ctx, interface_name):
ctx.fail("'interface_name' is not valid. Valid names [Ethernet/PortChannel/Vlan]")

if (interface_type == "INTERFACE" ) or (interface_type == "PORTCHANNEL_INTERFACE"):
if interface_name_is_valid(config_db, interface_name) is False:
if interface_name_is_valid(db, interface_name) is False:
ctx.fail("Interface name %s is invalid. Please enter a valid interface name!!" %(interface_name))

if (interface_type == "VLAN_INTERFACE"):
vlan = config_db.get_entry('VLAN', interface_name)
vlan = db.get_entry('VLAN', interface_name)
if len(vlan) == 0:
ctx.fail("Interface name %s is invalid. Please enter a valid interface name!!" %(interface_name))

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

if interface_is_in_portchannel(portchannel_member_table, interface_name):
ctx.fail("{} is configured as a member of portchannel. Cannot configure the IPv6 link local mode!"
.format(interface_name))

vlan_member_table = config_db.get_table('VLAN_MEMBER')
vlan_member_table = db.get_table('VLAN_MEMBER')

if interface_is_in_vlan(vlan_member_table, interface_name):
ctx.fail("{} is configured as a member of vlan. Cannot configure the IPv6 link local mode!"
.format(interface_name))

interface_dict = config_db.get_table(interface_type)
set_ipv6_link_local_only_on_interface(config_db, interface_dict, interface_type, interface_name, "enable")
interface_dict = db.get_table(interface_type)
set_ipv6_link_local_only_on_interface(db, interface_dict, interface_type, interface_name, "enable")

#
# 'config interface ipv6 disable use-link-local-only <interface-name>'
Expand All @@ -3136,10 +3140,14 @@ def enable_use_link_local_only(ctx, interface_name):
@click.argument('interface_name', metavar='<interface_name>', required=True)
def disable_use_link_local_only(ctx, interface_name):
"""Disable IPv6 link local address on interface"""
config_db = ctx.obj["config_db"]
config_db = ConfigDBConnector()
config_db.connect()
ctx.obj = {}
ctx.obj['config_db'] = config_db
db = ctx.obj["config_db"]

if clicommon.get_interface_naming_mode() == "alias":
interface_name = interface_alias_to_name(config_db, interface_name)
interface_name = interface_alias_to_name(db, interface_name)
if interface_name is None:
ctx.fail("'interface_name' is None!")

Expand All @@ -3154,27 +3162,27 @@ def disable_use_link_local_only(ctx, interface_name):
ctx.fail("'interface_name' is not valid. Valid names [Ethernet/PortChannel/Vlan]")

if (interface_type == "INTERFACE" ) or (interface_type == "PORTCHANNEL_INTERFACE"):
if interface_name_is_valid(config_db, interface_name) is False:
if interface_name_is_valid(db, interface_name) is False:
ctx.fail("Interface name %s is invalid. Please enter a valid interface name!!" %(interface_name))

if (interface_type == "VLAN_INTERFACE"):
vlan = config_db.get_entry('VLAN', interface_name)
vlan = db.get_entry('VLAN', interface_name)
if len(vlan) == 0:
ctx.fail("Interface name %s is invalid. Please enter a valid interface name!!" %(interface_name))

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

if interface_is_in_portchannel(portchannel_member_table, interface_name):
ctx.fail("{} is configured as a member of portchannel. Cannot configure the IPv6 link local mode!"
.format(interface_name))

vlan_member_table = config_db.get_table('VLAN_MEMBER')
vlan_member_table = db.get_table('VLAN_MEMBER')
if interface_is_in_vlan(vlan_member_table, interface_name):
ctx.fail("{} is configured as a member of vlan. Cannot configure the IPv6 link local mode!"
.format(interface_name))

interface_dict = config_db.get_table(interface_type)
set_ipv6_link_local_only_on_interface(config_db, interface_dict, interface_type, interface_name, "disable")
interface_dict = db.get_table(interface_type)
set_ipv6_link_local_only_on_interface(db, interface_dict, interface_type, interface_name, "disable")

#
# 'vrf' group ('config vrf ...')
Expand Down Expand Up @@ -4541,7 +4549,6 @@ def delete(ctx):
sflow_tbl['global'].pop('agent_id')
config_db.set_entry('SFLOW', 'global', sflow_tbl['global'])


#
# set ipv6 link local mode on a given interface
#
Expand Down
30 changes: 30 additions & 0 deletions show/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,36 @@ def protocol(verbose):
from .bgp_frr_v6 import bgp
ipv6.add_command(bgp)

#
# 'link-local-mode' subcommand ("show ipv6 link-local-mode")
#

@ipv6.command('link-local-mode')
@click.option('--verbose', is_flag=True, help="Enable verbose output")
def link_local_mode(verbose):
"""show ipv6 link-local-mode"""
header = ['Interface Name', 'Mode']
body = []
link_local = []
interfaces = ['INTERFACE', 'PORTCHANNEL_INTERFACE', 'VLAN_INTERFACE']
config_db = ConfigDBConnector()
config_db.connect()

for i in interfaces:
interface_dict = config_db.get_table(i)
link_local_data = {}

if interface_dict:
for interface,value in interface_dict.items():
if 'ipv6_use_link_local_only' in value:
link_local_data[interface] = interface_dict[interface]['ipv6_use_link_local_only']
if link_local_data[interface] == 'enable':
body.append([interface, 'Enabled'])
else:
body.append([interface, 'Disabled'])

click.echo(tabulate(body, header, tablefmt="grid"))

#
# 'lldp' group ("show lldp ...")
#
Expand Down
153 changes: 153 additions & 0 deletions tests/ipv6_link_local_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
import os

from click.testing import CliRunner

import config.main as config
import show.main as show
from utilities_common.db import Db

show_ipv6_link_local_mode_output="""\
+------------------+----------+
| Interface Name | Mode |
+==================+==========+
| Ethernet0 | Disabled |
+------------------+----------+
| PortChannel0001 | Disabled |
+------------------+----------+
"""

class TestIPv6LinkLocal(object):
@classmethod
def setup_class(cls):
os.environ['UTILITIES_UNIT_TESTING'] = "1"
print("SETUP")

def test_show_ipv6_link_local_mode(self):
runner = CliRunner()
db = Db()
obj = {'db':db.cfgdb}

# show ipv6 link-local-mode output
result = runner.invoke(show.cli.commands["ipv6"].commands["link-local-mode"], [], obj=obj)
print(result.output)
assert result.output == show_ipv6_link_local_mode_output

def test_config_enable_disable_ipv6_link_local_on_physical_interface(self):
runner = CliRunner()
db = Db()
obj = {'db':db.cfgdb}

# Enable ipv6 link local on Ethernet0
result = runner.invoke(config.config.commands["interface"].commands["ipv6"].commands["enable"].commands["use-link-local-only"], ["Ethernet0"], obj=obj)
print(result.exit_code)
print(result.output)
assert result.exit_code == 0
assert result.output == ''

# Disable ipv6 link local on Ethernet0
result = runner.invoke(config.config.commands["interface"].commands["ipv6"].commands["disable"].commands["use-link-local-only"], ["Ethernet0"], obj=obj)
print(result.exit_code)
print(result.output)
assert result.exit_code == 0
assert result.output == ''

def test_config_enable_disable_ipv6_link_local_on_portchannel_interface(self):
runner = CliRunner()
db = Db()
obj = {'db':db.cfgdb}

# Enable ipv6 link local on PortChannel0001
result = runner.invoke(config.config.commands["interface"].commands["ipv6"].commands["enable"].commands["use-link-local-only"], ["PortChannel0001"], obj=obj)
print(result.exit_code)
print(result.output)
assert result.exit_code == 0
assert result.output == ''

# Disable ipv6 link local on PortChannel0001
result = runner.invoke(config.config.commands["interface"].commands["ipv6"].commands["disable"].commands["use-link-local-only"], ["PortChannel0001"], obj=obj)
print(result.exit_code)
print(result.output)
assert result.exit_code == 0
assert result.output == ''

def test_config_enable_disable_ipv6_link_local_on_invalid_interface(self):
runner = CliRunner()
db = Db()
obj = {'db':db.cfgdb}

# Enable ipv6 link local on PortChannel1
result = runner.invoke(config.config.commands["interface"].commands["ipv6"].commands["enable"].commands["use-link-local-only"], ["PortChannel1"], obj=obj)
print(result.exit_code)
print(result.output)
assert result.exit_code != 0
assert 'Error: Interface name PortChannel1 is invalid. Please enter a valid interface name!!' in result.output

# Disable ipv6 link local on Ethernet500
result = runner.invoke(config.config.commands["interface"].commands["ipv6"].commands["disable"].commands["use-link-local-only"], ["Ethernet500"], obj=obj)
print(result.exit_code)
print(result.output)
assert result.exit_code != 0
assert 'Error: Interface name Ethernet500 is invalid. Please enter a valid interface name!!' in result.output

def test_config_enable_disable_ipv6_link_local_on_interface_which_is_member_of_vlan(self):
runner = CliRunner()
db = Db()
obj = {'db':db.cfgdb}

# Enable ipv6 link local on Ethernet16
result = runner.invoke(config.config.commands["interface"].commands["ipv6"].commands["enable"].commands["use-link-local-only"], ["Ethernet16"], obj=obj)
print(result.exit_code)
print(result.output)
assert result.exit_code != 0
assert 'Error: Ethernet16 is configured as a member of vlan. Cannot configure the IPv6 link local mode!' in result.output

# Disable ipv6 link local on Ethernet16
result = runner.invoke(config.config.commands["interface"].commands["ipv6"].commands["disable"].commands["use-link-local-only"], ["Ethernet16"], obj=obj)
print(result.exit_code)
print(result.output)
assert result.exit_code != 0
assert 'Error: Ethernet16 is configured as a member of vlan. Cannot configure the IPv6 link local mode!' in result.output

def test_config_enable_disable_ipv6_link_local_on_interface_which_is_member_of_portchannel(self):
runner = CliRunner()
db = Db()
obj = {'db':db.cfgdb}

# Enable ipv6 link local on Ethernet32
result = runner.invoke(config.config.commands["interface"].commands["ipv6"].commands["enable"].commands["use-link-local-only"], ["Ethernet32"], obj=obj)
print(result.exit_code)
print(result.output)
assert result.exit_code != 0
assert 'Error: Ethernet32 is configured as a member of portchannel. Cannot configure the IPv6 link local mode!' in result.output

# Disable ipv6 link local on Ethernet32
result = runner.invoke(config.config.commands["interface"].commands["ipv6"].commands["disable"].commands["use-link-local-only"], ["Ethernet32"], obj=obj)
print(result.exit_code)
print(result.output)
assert result.exit_code != 0
assert 'Error: Ethernet32 is configured as a member of portchannel. Cannot configure the IPv6 link local mode!' in result.output

def test_config_enable_disable_ipv6_link_local_on_all_valid_interfaces(self):
runner = CliRunner()
db = Db()
obj = {'db':db.cfgdb}

# Enable ipv6 link local on all interfaces
result = runner.invoke(config.config.commands["ipv6"].commands["enable"], obj=obj)
print(result.exit_code)
print(result.output)
assert result.exit_code == 0
assert result.output == ''

# Disable ipv6 link local on all interfaces
result = runner.invoke(config.config.commands["ipv6"].commands["disable"], obj=obj)
print(result.exit_code)
print(result.output)
assert result.exit_code == 0
assert result.output == ''

@classmethod
def teardown_class(cls):
os.environ['UTILITIES_UNIT_TESTING'] = "0"
print("TEARDOWN")

6 changes: 4 additions & 2 deletions tests/mock_tables/config_db.json
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,8 @@
"NULL": "NULL"
},
"PORTCHANNEL_INTERFACE|PortChannel0001": {
"NULL": "NULL"
"NULL": "NULL",
"ipv6_use_link_local_only": "disable"
},
"PORTCHANNEL_INTERFACE|PortChannel0002": {
"NULL": "NULL"
Expand Down Expand Up @@ -573,7 +574,8 @@
"NULL": "NULL"
},
"INTERFACE|Ethernet0": {
"NULL": "NULL"
"NULL": "NULL",
"ipv6_use_link_local_only": "disable"
},
"INTERFACE|Ethernet0|14.14.0.1/24": {
"NULL": "NULL"
Expand Down

0 comments on commit 56f74cd

Please sign in to comment.