Skip to content

Commit

Permalink
[CLI][MPLS][Show] Added multi ASIC support for 'show mpls command'.
Browse files Browse the repository at this point in the history
* Also added unit tests to test show command with various options.
  • Loading branch information
developfast authored Jul 28, 2021
1 parent c2ac2d2 commit e9c73e8
Show file tree
Hide file tree
Showing 7 changed files with 260 additions and 74 deletions.
2 changes: 1 addition & 1 deletion config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4031,7 +4031,7 @@ def add(ctx, interface_name):
config_db.set_entry(table_name, interface_name, {"mpls": "enable"})

#
# 'del' subcommand
# 'remove' subcommand
#

@mpls.command()
Expand Down
75 changes: 52 additions & 23 deletions show/interfaces/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,36 +321,65 @@ def expected(db, interfacename):

click.echo(tabulate(body, header))

# 'mpls' subcommand ("show interfaces mpls")
@interfaces.command()
@click.argument('interfacename', required=False)
@click.option('--namespace', '-n', 'namespace', default=None,
type=str, show_default=True, help='Namespace name or all',
callback=multi_asic_util.multi_asic_namespace_validation_callback)
@click.option('--display', '-d', 'display', default=None, show_default=False,
type=str, help='all|frontend')
@click.pass_context
def mpls(ctx, interfacename):
def mpls(ctx, interfacename, namespace, display):
"""Show Interface MPLS status"""

#Edge case: Force show frontend interfaces on single asic
if not (multi_asic.is_multi_asic()):
if (display == 'frontend' or display == 'all' or display is None):
display = None
else:
print("Error: Invalid display option command for single asic")
return

masic = multi_asic_util.MultiAsic(display_option=display, namespace_option=namespace)
ns_list = masic.get_ns_list_based_on_options()
intfs_data = {}

appl_db = SonicV2Connector()
appl_db.connect(appl_db.APPL_DB)
for ns in ns_list:

if interfacename is not None:
interfacename = try_convert_interfacename_from_alias(ctx, interfacename)
appl_db = multi_asic.connect_to_all_dbs_for_ns(namespace=ns)

# Fetching data from appl_db for intfs
keys = appl_db.keys(appl_db.APPL_DB, "INTF_TABLE:*")
intfs_data = {}
for key in keys if keys else []:
tokens = key.split(":")
# Skip INTF_TABLE entries with address information
if len(tokens) != 2:
continue

if (interfacename is not None) and (interfacename != tokens[1]):
continue

mpls = appl_db.get(appl_db.APPL_DB, key, 'mpls')
if mpls is None or mpls == '':
intfs_data.update({tokens[1]: 'disable'})
else:
intfs_data.update({tokens[1]: mpls})
if interfacename is not None:
interfacename = try_convert_interfacename_from_alias(ctx, interfacename)

# Fetching data from appl_db for intfs
keys = appl_db.keys(appl_db.APPL_DB, "INTF_TABLE:*")
for key in keys if keys else []:
tokens = key.split(":")
ifname = tokens[1]
# Skip INTF_TABLE entries with address information
if len(tokens) != 2:
continue

if (interfacename is not None) and (interfacename != tokens[1]):
continue

if (display != "all"):
if ("Loopback" in tokens[1]):
continue

if ifname.startswith("Ethernet") and multi_asic.is_port_internal(ifname, ns):
continue

if ifname.startswith("PortChannel") and multi_asic.is_port_channel_internal(ifname, ns):
continue


mpls_intf = appl_db.get_all(appl_db.APPL_DB, key)

if 'mpls' not in mpls_intf or mpls_intf['mpls'] == 'disable':
intfs_data.update({tokens[1]: 'disable'})
else:
intfs_data.update({tokens[1]: mpls_intf['mpls']})

header = ['Interface', 'MPLS State']
body = []
Expand Down
18 changes: 18 additions & 0 deletions show/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import click
import utilities_common.cli as clicommon
import utilities_common.multi_asic as multi_asic_util
from importlib import reload
from natsort import natsorted
from sonic_py_common import device_info
from swsscommon.swsscommon import SonicV2Connector, ConfigDBConnector
Expand All @@ -16,6 +17,23 @@
import utilities_common.constants as constants
from utilities_common.general import load_db_config

# mock the redis for unit test purposes #
try:
if os.environ["UTILITIES_UNIT_TESTING"] == "2":
modules_path = os.path.join(os.path.dirname(__file__), "..")
tests_path = os.path.join(modules_path, "tests")
sys.path.insert(0, modules_path)
sys.path.insert(0, tests_path)
import mock_tables.dbconnector
if os.environ["UTILITIES_UNIT_TESTING_TOPOLOGY"] == "multi_asic":
import mock_tables.mock_multi_asic
reload(mock_tables.mock_multi_asic)
reload(mock_tables.dbconnector)
mock_tables.dbconnector.load_namespace_config()

except KeyError:
pass

from . import acl
from . import bgp_common
from . import chassis_modules
Expand Down
14 changes: 13 additions & 1 deletion tests/mock_tables/asic0/appl_db.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,17 @@
"admin_status": "up",
"mtu": "9100",
"oper_status": "up"
},
"INTF_TABLE:Ethernet0": {
"mpls":"enable"
},
"INTF_TABLE:Ethernet4": {
"mpls":"disable"
},
"INTF_TABLE:Ethernet-BP0": {
"mpls":"enable"
},
"INTF_TABLE:Ethernet-BP4": {
"mpls":"disable"
}
}
}
9 changes: 9 additions & 0 deletions tests/mock_tables/asic1/appl_db.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,14 @@
},
"LAG_MEMBER_TABLE:PortChannel4009:Ethernet-BP260": {
"status": "enabled"
},
"INTF_TABLE:Ethernet64": {
"mpls":"enable"
},
"INTF_TABLE:Ethernet-BP256": {
"mpls":"disable"
},
"INTF_TABLE:Ethernet-BP260": {
"mpls":"enable"
}
}
25 changes: 5 additions & 20 deletions tests/mpls_input/appl_db.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
{
"INTF_TABLE:Ethernet16": {
"INTF_TABLE:Ethernet16": {
"NULL": "NULL"
},
"INTF_TABLE:Ethernet16:192.168.16.1/24": {
"NULL": "NULL"
},
"INTF_TABLE:Ethernet2": {
"INTF_TABLE:Ethernet0": {
"mpls": "enable"
},
"INTF_TABLE:Ethernet2:192.168.2.1/24": {
"NULL": "NULL"
},
"INTF_TABLE:Ethernet4": {
"NULL": "NULL"
"mpls": "enable"
},
"INTF_TABLE:Ethernet4:192.168.4.1/24": {
"NULL": "NULL"
Expand All @@ -26,22 +23,10 @@
"INTF_TABLE:Ethernet8:192.168.8.1/24": {
"NULL": "NULL"
},
"INTF_TABLE:Loopback0": {
"NULL": "NULL"
},
"INTF_TABLE:Loopback0:192.168.0.1/24": {
"NULL": "NULL"
},
"INTF_TABLE:PortChannel2": {
"INTF_TABLE:Ethernet12": {
"mpls": "disable"
},
"INTF_TABLE:PortChannel2:10.0.0.56/31": {
"NULL": "NULL"
},
"INTF_TABLE:Vlan2": {
"INTF_TABLE:Ethernet20": {
"mpls": "enable"
},
"INTF_TABLE:Vlan2:192.168.1.1/21": {
"NULL": "NULL"
}
}
Loading

0 comments on commit e9c73e8

Please sign in to comment.