Skip to content

Commit c7c01e4

Browse files
[show] fix "show interfaces breakout" command (sonic-net#1198)
**- What I did** Fixed `show interface breakout` command **- How I did it** **- How to verify it** From command line type `show interface breakout` **- Previous command output (if the output of a command-line utility has changed)** ``` Traceback (most recent call last): File "/usr/local/bin/show", line 10, in <module> sys.exit(cli()) File "/usr/local/lib/python2.7/dist-packages/click/core.py", line 764, in __call__ return self.main(*args, **kwargs) File "/usr/local/lib/python2.7/dist-packages/click/core.py", line 717, in main rv = self.invoke(ctx) File "/usr/local/lib/python2.7/dist-packages/click/core.py", line 1137, in invoke return _process_result(sub_ctx.command.invoke(sub_ctx)) File "/usr/local/lib/python2.7/dist-packages/click/core.py", line 1137, in invoke return _process_result(sub_ctx.command.invoke(sub_ctx)) File "/usr/local/lib/python2.7/dist-packages/click/core.py", line 1114, in invoke return Command.invoke(self, ctx) File "/usr/local/lib/python2.7/dist-packages/click/core.py", line 956, in invoke return ctx.invoke(self.callback, **ctx.params) File "/usr/local/lib/python2.7/dist-packages/click/core.py", line 555, in invoke return callback(*args, **kwargs) File "/usr/local/lib/python2.7/dist-packages/click/decorators.py", line 17, in new_func return f(get_current_context(), *args, **kwargs) File "/usr/local/lib/python2.7/dist-packages/show/interfaces/__init__.py", line 135, in breakout config_db = ConfigDBConnector() NameError: global name 'ConfigDBConnector' is not defined ``` **- New command output (if the output of a command-line utility has changed)** Related from DPB config. e.g. ``` { "Ethernet0": { "index": "0,0,0,0", "default_brkout_mode": "1x100G[40G]", "child ports": "Ethernet0", "breakout_modes": "1x100G[40G],2x50G,4x25G[10G],2x25G(2)+1x50G(2),1x50G(2)+2x25G(2)", "child port speeds": "100G", "Current Breakout Mode": "1x100G[40G]", "lanes": "25,26,27,28", "alias_at_lanes": "Ethernet0/0,Ethernet0/1,Ethernet0/2,Ethernet0/3" } } ```
1 parent 7a8024a commit c7c01e4

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

show/interfaces/__init__.py

+30-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,36 @@
11
import json
2+
import os
23

34
import click
45
import utilities_common.cli as clicommon
56
import utilities_common.multi_asic as multi_asic_util
67
from natsort import natsorted
78
from tabulate import tabulate
89
from sonic_py_common import multi_asic
10+
from sonic_py_common import device_info
11+
from swsssdk import ConfigDBConnector
12+
from portconfig import get_child_ports
913

1014
from . import portchannel
15+
from collections import OrderedDict
16+
17+
HWSKU_JSON = 'hwsku.json'
18+
19+
# Read given JSON file
20+
def readJsonFile(fileName):
21+
try:
22+
with open(fileName) as f:
23+
result = json.load(f)
24+
except FileNotFoundError as e:
25+
click.echo("{}".format(str(e)), err=True)
26+
raise click.Abort()
27+
except json.decoder.JSONDecodeError as e:
28+
click.echo("Invalid JSON file format('{}')\n{}".format(fileName, str(e)), err=True)
29+
raise click.Abort()
30+
except Exception as e:
31+
click.echo("{}\n{}".format(type(e), str(e)), err=True)
32+
raise click.Abort()
33+
return result
1134

1235
def try_convert_interfacename_from_alias(ctx, interfacename):
1336
"""try to convert interface name from alias"""
@@ -144,13 +167,14 @@ def breakout(ctx):
144167

145168
if ctx.invoked_subcommand is None:
146169
# Get port capability from platform and hwsku related files
147-
platform_path, hwsku_path = device_info.get_paths_to_platform_and_hwsku_dirs()
148-
platform_file = os.path.join(platform_path, PLATFORM_JSON)
170+
hwsku_path = device_info.get_path_to_hwsku_dir()
171+
platform_file = device_info.get_path_to_port_config_file()
149172
platform_dict = readJsonFile(platform_file)['interfaces']
150-
hwsku_dict = readJsonFile(os.path.join(hwsku_path, HWSKU_JSON))['interfaces']
173+
hwsku_file = os.path.join(hwsku_path, HWSKU_JSON)
174+
hwsku_dict = readJsonFile(hwsku_file)['interfaces']
151175

152176
if not platform_dict or not hwsku_dict:
153-
click.echo("Can not load port config from {} or {} file".format(PLATFORM_JSON, HWSKU_JSON))
177+
click.echo("Can not load port config from {} or {} file".format(platform_file, hwsku_file))
154178
raise click.Abort()
155179

156180
for port_name in platform_dict:
@@ -161,9 +185,9 @@ def breakout(ctx):
161185
platform_dict[port_name]["Current Breakout Mode"] = cur_brkout_mode
162186

163187
# List all the child ports if present
164-
child_port_dict = get_child_ports(port_name, cur_brkout_mode, platformFile)
188+
child_port_dict = get_child_ports(port_name, cur_brkout_mode, platform_file)
165189
if not child_port_dict:
166-
click.echo("Cannot find ports from {} file ".format(PLATFORM_JSON))
190+
click.echo("Cannot find ports from {} file ".format(platform_file))
167191
raise click.Abort()
168192

169193
child_ports = natsorted(list(child_port_dict.keys()))

0 commit comments

Comments
 (0)