Skip to content

Commit e9c73e8

Browse files
authored
[CLI][MPLS][Show] Added multi ASIC support for 'show mpls command'.
* Also added unit tests to test show command with various options.
1 parent c2ac2d2 commit e9c73e8

File tree

7 files changed

+260
-74
lines changed

7 files changed

+260
-74
lines changed

config/main.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4031,7 +4031,7 @@ def add(ctx, interface_name):
40314031
config_db.set_entry(table_name, interface_name, {"mpls": "enable"})
40324032

40334033
#
4034-
# 'del' subcommand
4034+
# 'remove' subcommand
40354035
#
40364036

40374037
@mpls.command()

show/interfaces/__init__.py

+52-23
Original file line numberDiff line numberDiff line change
@@ -321,36 +321,65 @@ def expected(db, interfacename):
321321

322322
click.echo(tabulate(body, header))
323323

324-
# 'mpls' subcommand ("show interfaces mpls")
325324
@interfaces.command()
326325
@click.argument('interfacename', required=False)
326+
@click.option('--namespace', '-n', 'namespace', default=None,
327+
type=str, show_default=True, help='Namespace name or all',
328+
callback=multi_asic_util.multi_asic_namespace_validation_callback)
329+
@click.option('--display', '-d', 'display', default=None, show_default=False,
330+
type=str, help='all|frontend')
327331
@click.pass_context
328-
def mpls(ctx, interfacename):
332+
def mpls(ctx, interfacename, namespace, display):
329333
"""Show Interface MPLS status"""
334+
335+
#Edge case: Force show frontend interfaces on single asic
336+
if not (multi_asic.is_multi_asic()):
337+
if (display == 'frontend' or display == 'all' or display is None):
338+
display = None
339+
else:
340+
print("Error: Invalid display option command for single asic")
341+
return
342+
343+
masic = multi_asic_util.MultiAsic(display_option=display, namespace_option=namespace)
344+
ns_list = masic.get_ns_list_based_on_options()
345+
intfs_data = {}
330346

331-
appl_db = SonicV2Connector()
332-
appl_db.connect(appl_db.APPL_DB)
347+
for ns in ns_list:
333348

334-
if interfacename is not None:
335-
interfacename = try_convert_interfacename_from_alias(ctx, interfacename)
349+
appl_db = multi_asic.connect_to_all_dbs_for_ns(namespace=ns)
336350

337-
# Fetching data from appl_db for intfs
338-
keys = appl_db.keys(appl_db.APPL_DB, "INTF_TABLE:*")
339-
intfs_data = {}
340-
for key in keys if keys else []:
341-
tokens = key.split(":")
342-
# Skip INTF_TABLE entries with address information
343-
if len(tokens) != 2:
344-
continue
345-
346-
if (interfacename is not None) and (interfacename != tokens[1]):
347-
continue
348-
349-
mpls = appl_db.get(appl_db.APPL_DB, key, 'mpls')
350-
if mpls is None or mpls == '':
351-
intfs_data.update({tokens[1]: 'disable'})
352-
else:
353-
intfs_data.update({tokens[1]: mpls})
351+
if interfacename is not None:
352+
interfacename = try_convert_interfacename_from_alias(ctx, interfacename)
353+
354+
# Fetching data from appl_db for intfs
355+
keys = appl_db.keys(appl_db.APPL_DB, "INTF_TABLE:*")
356+
for key in keys if keys else []:
357+
tokens = key.split(":")
358+
ifname = tokens[1]
359+
# Skip INTF_TABLE entries with address information
360+
if len(tokens) != 2:
361+
continue
362+
363+
if (interfacename is not None) and (interfacename != tokens[1]):
364+
continue
365+
366+
if (display != "all"):
367+
if ("Loopback" in tokens[1]):
368+
continue
369+
370+
if ifname.startswith("Ethernet") and multi_asic.is_port_internal(ifname, ns):
371+
continue
372+
373+
if ifname.startswith("PortChannel") and multi_asic.is_port_channel_internal(ifname, ns):
374+
continue
375+
376+
377+
mpls_intf = appl_db.get_all(appl_db.APPL_DB, key)
378+
379+
if 'mpls' not in mpls_intf or mpls_intf['mpls'] == 'disable':
380+
intfs_data.update({tokens[1]: 'disable'})
381+
else:
382+
intfs_data.update({tokens[1]: mpls_intf['mpls']})
354383

355384
header = ['Interface', 'MPLS State']
356385
body = []

show/main.py

+18
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import click
88
import utilities_common.cli as clicommon
99
import utilities_common.multi_asic as multi_asic_util
10+
from importlib import reload
1011
from natsort import natsorted
1112
from sonic_py_common import device_info
1213
from swsscommon.swsscommon import SonicV2Connector, ConfigDBConnector
@@ -16,6 +17,23 @@
1617
import utilities_common.constants as constants
1718
from utilities_common.general import load_db_config
1819

20+
# mock the redis for unit test purposes #
21+
try:
22+
if os.environ["UTILITIES_UNIT_TESTING"] == "2":
23+
modules_path = os.path.join(os.path.dirname(__file__), "..")
24+
tests_path = os.path.join(modules_path, "tests")
25+
sys.path.insert(0, modules_path)
26+
sys.path.insert(0, tests_path)
27+
import mock_tables.dbconnector
28+
if os.environ["UTILITIES_UNIT_TESTING_TOPOLOGY"] == "multi_asic":
29+
import mock_tables.mock_multi_asic
30+
reload(mock_tables.mock_multi_asic)
31+
reload(mock_tables.dbconnector)
32+
mock_tables.dbconnector.load_namespace_config()
33+
34+
except KeyError:
35+
pass
36+
1937
from . import acl
2038
from . import bgp_common
2139
from . import chassis_modules

tests/mock_tables/asic0/appl_db.json

+13-1
Original file line numberDiff line numberDiff line change
@@ -80,5 +80,17 @@
8080
"admin_status": "up",
8181
"mtu": "9100",
8282
"oper_status": "up"
83+
},
84+
"INTF_TABLE:Ethernet0": {
85+
"mpls":"enable"
86+
},
87+
"INTF_TABLE:Ethernet4": {
88+
"mpls":"disable"
89+
},
90+
"INTF_TABLE:Ethernet-BP0": {
91+
"mpls":"enable"
92+
},
93+
"INTF_TABLE:Ethernet-BP4": {
94+
"mpls":"disable"
8395
}
84-
}
96+
}

tests/mock_tables/asic1/appl_db.json

+9
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,14 @@
5555
},
5656
"LAG_MEMBER_TABLE:PortChannel4009:Ethernet-BP260": {
5757
"status": "enabled"
58+
},
59+
"INTF_TABLE:Ethernet64": {
60+
"mpls":"enable"
61+
},
62+
"INTF_TABLE:Ethernet-BP256": {
63+
"mpls":"disable"
64+
},
65+
"INTF_TABLE:Ethernet-BP260": {
66+
"mpls":"enable"
5867
}
5968
}

tests/mpls_input/appl_db.json

+5-20
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
{
2-
"INTF_TABLE:Ethernet16": {
2+
"INTF_TABLE:Ethernet16": {
33
"NULL": "NULL"
44
},
55
"INTF_TABLE:Ethernet16:192.168.16.1/24": {
66
"NULL": "NULL"
77
},
8-
"INTF_TABLE:Ethernet2": {
8+
"INTF_TABLE:Ethernet0": {
99
"mpls": "enable"
1010
},
11-
"INTF_TABLE:Ethernet2:192.168.2.1/24": {
12-
"NULL": "NULL"
13-
},
1411
"INTF_TABLE:Ethernet4": {
15-
"NULL": "NULL"
12+
"mpls": "enable"
1613
},
1714
"INTF_TABLE:Ethernet4:192.168.4.1/24": {
1815
"NULL": "NULL"
@@ -26,22 +23,10 @@
2623
"INTF_TABLE:Ethernet8:192.168.8.1/24": {
2724
"NULL": "NULL"
2825
},
29-
"INTF_TABLE:Loopback0": {
30-
"NULL": "NULL"
31-
},
32-
"INTF_TABLE:Loopback0:192.168.0.1/24": {
33-
"NULL": "NULL"
34-
},
35-
"INTF_TABLE:PortChannel2": {
26+
"INTF_TABLE:Ethernet12": {
3627
"mpls": "disable"
3728
},
38-
"INTF_TABLE:PortChannel2:10.0.0.56/31": {
39-
"NULL": "NULL"
40-
},
41-
"INTF_TABLE:Vlan2": {
29+
"INTF_TABLE:Ethernet20": {
4230
"mpls": "enable"
43-
},
44-
"INTF_TABLE:Vlan2:192.168.1.1/21": {
45-
"NULL": "NULL"
4631
}
4732
}

0 commit comments

Comments
 (0)