Skip to content

Commit

Permalink
[show] fix "show interfaces breakout" command (#1198)
Browse files Browse the repository at this point in the history
**- 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"
    }
}
```
  • Loading branch information
dmytroxshevchuk authored and yxieca committed Feb 23, 2021
1 parent 148259b commit cc0bb6e
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions show/interfaces/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,36 @@
import json
import os

import click
import utilities_common.cli as clicommon
import utilities_common.multi_asic as multi_asic_util
from natsort import natsorted
from tabulate import tabulate
from sonic_py_common import multi_asic
from sonic_py_common import device_info
from swsssdk import ConfigDBConnector
from portconfig import get_child_ports

from . import portchannel
from collections import OrderedDict

HWSKU_JSON = 'hwsku.json'

# Read given JSON file
def readJsonFile(fileName):
try:
with open(fileName) as f:
result = json.load(f)
except FileNotFoundError as e:
click.echo("{}".format(str(e)), err=True)
raise click.Abort()
except json.decoder.JSONDecodeError as e:
click.echo("Invalid JSON file format('{}')\n{}".format(fileName, str(e)), err=True)
raise click.Abort()
except Exception as e:
click.echo("{}\n{}".format(type(e), str(e)), err=True)
raise click.Abort()
return result

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

if ctx.invoked_subcommand is None:
# Get port capability from platform and hwsku related files
platform_path, hwsku_path = device_info.get_paths_to_platform_and_hwsku_dirs()
platform_file = os.path.join(platform_path, PLATFORM_JSON)
hwsku_path = device_info.get_path_to_hwsku_dir()
platform_file = device_info.get_path_to_port_config_file()
platform_dict = readJsonFile(platform_file)['interfaces']
hwsku_dict = readJsonFile(os.path.join(hwsku_path, HWSKU_JSON))['interfaces']
hwsku_file = os.path.join(hwsku_path, HWSKU_JSON)
hwsku_dict = readJsonFile(hwsku_file)['interfaces']

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

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

# List all the child ports if present
child_port_dict = get_child_ports(port_name, cur_brkout_mode, platformFile)
child_port_dict = get_child_ports(port_name, cur_brkout_mode, platform_file)
if not child_port_dict:
click.echo("Cannot find ports from {} file ".format(PLATFORM_JSON))
click.echo("Cannot find ports from {} file ".format(platform_file))
raise click.Abort()

child_ports = natsorted(list(child_port_dict.keys()))
Expand Down

0 comments on commit cc0bb6e

Please sign in to comment.