Skip to content

Commit

Permalink
[hash]: Handle review comments.
Browse files Browse the repository at this point in the history
Signed-off-by: Nazarii Hnydyn <[email protected]>
  • Loading branch information
nazariig committed Nov 15, 2023
1 parent a495e3e commit d8401ce
Show file tree
Hide file tree
Showing 3 changed files with 689 additions and 244 deletions.
111 changes: 81 additions & 30 deletions show/plugins/sonic-hash.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import click
import tabulate
import json
import utilities_common.cli as clicommon

from utilities_common.switch_hash import (
Expand Down Expand Up @@ -57,30 +58,50 @@ def SWITCH_HASH():
@SWITCH_HASH.command(
name="global"
)
@click.option(
"-j", "--json", "json_format",
help="Display in JSON format",
is_flag=True,
default=False
)
@clicommon.pass_db
def SWITCH_HASH_GLOBAL(db):
@click.pass_context
def SWITCH_HASH_GLOBAL(ctx, db, json_format):
""" Show switch hash global configuration """

ecmp_header = [
"ECMP HASH",
"ECMP HASH ALGORITHM",
header = [
"Hash",
"Configuration",
]
ecmp_body = []
body = []

lag_header = [
"LAG HASH",
"LAG HASH ALGORITHM",
sub_header = [
"Hash Field",
"Algorithm"
]
ecmp_body = []
lag_body = []

table = db.cfgdb.get_table(CFG_SWITCH_HASH)
entry = table.get(SW_HASH_KEY, {})

if not entry:
click.echo(tabulate.tabulate(ecmp_body, ecmp_header))
click.echo()
click.echo(tabulate.tabulate(lag_body, lag_header))
return
click.echo("No configuration is present in CONFIG DB")
ctx.exit(0)

if json_format:
json_dict = {
"ecmp": {
"hash_field": entry["ecmp_hash"] if "ecmp_hash" in entry else "N/A",
"algorithm": entry["ecmp_hash_algorithm"] if "ecmp_hash_algorithm" in entry else "N/A"
},
"lag": {
"hash_field": entry["lag_hash"] if "lag_hash" in entry else "N/A",
"algorithm": entry["lag_hash_algorithm"] if "lag_hash_algorithm" in entry else "N/A"
}
}
click.echo(json.dumps(json_dict, indent=4))
ctx.exit(0)

ecmp_row = [
format_attr_value(
Expand Down Expand Up @@ -118,37 +139,43 @@ def SWITCH_HASH_GLOBAL(db):
]
lag_body.append(lag_row)

click.echo(tabulate.tabulate(ecmp_body, ecmp_header))
click.echo()
click.echo(tabulate.tabulate(lag_body, lag_header))
body.append(["ECMP", tabulate.tabulate(ecmp_body, sub_header, "psql")])
body.append(["LAG", tabulate.tabulate(lag_body, sub_header, "psql")])

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


@SWITCH_HASH.command(
name="capabilities"
)
@click.option(
"-j", "--json", "json_format",
help="Display in JSON format",
is_flag=True,
default=False
)
@clicommon.pass_db
def SWITCH_HASH_CAPABILITIES(db):
@click.pass_context
def SWITCH_HASH_CAPABILITIES(ctx, db, json_format):
""" Show switch hash capabilities """

ecmp_header = [
"ECMP HASH",
"ECMP HASH ALGORITHM",
header = [
"Hash",
"Capabilities",
]
ecmp_body = []
body = []

lag_header = [
"LAG HASH",
"LAG HASH ALGORITHM",
sub_header = [
"Hash Field",
"Algorithm"
]
ecmp_body = []
lag_body = []

entry = db.db.get_all(db.db.STATE_DB, "{}|{}".format(STATE_SWITCH_CAPABILITY, SW_CAP_KEY))

if not entry:
click.echo(tabulate.tabulate(ecmp_body, ecmp_header))
click.echo()
click.echo(tabulate.tabulate(lag_body, lag_header))
return
ctx.fail("No data is present in STATE DB")

entry.setdefault(SW_CAP_HASH_FIELD_LIST_KEY, 'N/A')
entry.setdefault(SW_CAP_ECMP_HASH_ALGORITHM_KEY, 'N/A')
Expand All @@ -167,6 +194,29 @@ def SWITCH_HASH_CAPABILITIES(db):
if not entry[SW_CAP_LAG_HASH_ALGORITHM_KEY]:
entry[SW_CAP_LAG_HASH_ALGORITHM_KEY] = "no capabilities"

if json_format:
if entry[SW_CAP_HASH_FIELD_LIST_KEY] not in ["N/A", "no capabilities"]:
entry[SW_CAP_HASH_FIELD_LIST_KEY] = entry[SW_CAP_HASH_FIELD_LIST_KEY].split(',')

if entry[SW_CAP_ECMP_HASH_ALGORITHM_KEY] not in ["N/A", "no capabilities"]:
entry[SW_CAP_ECMP_HASH_ALGORITHM_KEY] = entry[SW_CAP_ECMP_HASH_ALGORITHM_KEY].split(',')

if entry[SW_CAP_LAG_HASH_ALGORITHM_KEY] not in ["N/A", "no capabilities"]:
entry[SW_CAP_LAG_HASH_ALGORITHM_KEY] = entry[SW_CAP_LAG_HASH_ALGORITHM_KEY].split(',')

json_dict = {
"ecmp": {
"hash_field": entry[SW_CAP_HASH_FIELD_LIST_KEY] if entry[SW_CAP_ECMP_HASH_CAPABLE_KEY] == 'true' else 'not supported',
"algorithm": entry[SW_CAP_ECMP_HASH_ALGORITHM_KEY] if entry[SW_CAP_ECMP_HASH_ALGORITHM_CAPABLE_KEY] == 'true' else 'not supported'
},
"lag": {
"hash_field": entry[SW_CAP_HASH_FIELD_LIST_KEY] if entry[SW_CAP_LAG_HASH_CAPABLE_KEY] == 'true' else 'not supported',
"algorithm": entry[SW_CAP_LAG_HASH_ALGORITHM_KEY] if entry[SW_CAP_LAG_HASH_ALGORITHM_CAPABLE_KEY] == 'true' else 'not supported'
}
}
click.echo(json.dumps(json_dict, indent=4))
ctx.exit(0)

entry[SW_CAP_HASH_FIELD_LIST_KEY] = entry[SW_CAP_HASH_FIELD_LIST_KEY].split(',')
entry[SW_CAP_ECMP_HASH_ALGORITHM_KEY] = entry[SW_CAP_ECMP_HASH_ALGORITHM_KEY].split(',')
entry[SW_CAP_LAG_HASH_ALGORITHM_KEY] = entry[SW_CAP_LAG_HASH_ALGORITHM_KEY].split(',')
Expand Down Expand Up @@ -207,9 +257,10 @@ def SWITCH_HASH_CAPABILITIES(db):
]
lag_body.append(lag_row)

click.echo(tabulate.tabulate(ecmp_body, ecmp_header))
click.echo()
click.echo(tabulate.tabulate(lag_body, lag_header))
body.append(["ECMP", tabulate.tabulate(ecmp_body, sub_header, "psql")])
body.append(["LAG", tabulate.tabulate(lag_body, sub_header, "psql")])

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


def register(cli):
Expand Down
Loading

0 comments on commit d8401ce

Please sign in to comment.