diff --git a/sonic-utilities-data/templates/sonic-cli-gen/config.py.j2 b/sonic-utilities-data/templates/sonic-cli-gen/config.py.j2 index 0906d7c99f..d592a7f9e0 100644 --- a/sonic-utilities-data/templates/sonic-cli-gen/config.py.j2 +++ b/sonic-utilities-data/templates/sonic-cli-gen/config.py.j2 @@ -467,7 +467,7 @@ E.g: {{ gen_click_options(object.attrs) }} @clicommon.pass_db def {{ group }}_update(db, {{ pythonize(object["keys"] + object.attrs) }}): - """ Add object in {{ table.name }}. """ + """ Update object in {{ table.name }}. """ table = "{{ table.name }}" key = {{ pythonize(object["keys"]) }} @@ -531,6 +531,7 @@ def {{ group }}(): {% for table in tables %} +{% if 'config' not in table or table['config'] == 'true' %} @click.group(name="{{ cli_name(table.name) }}", cls=clicommon.AliasedGroup) def {{ table.name }}(): @@ -550,6 +551,7 @@ def {{ table.name }}(): {% endfor %} {% endif %} +{% endif %} {% endfor %} def register(cli): @@ -563,8 +565,10 @@ def register(cli): """ {%- for table in tables %} +{%- if 'config' not in table or table['config'] == 'true' %} cli_node = {{ table.name }} if cli_node.name in cli.commands: raise Exception(f"{cli_node.name} already exists in CLI") cli.add_command({{ table.name }}) +{%- endif %} {%- endfor %} diff --git a/sonic-utilities-data/templates/sonic-cli-gen/show.py.j2 b/sonic-utilities-data/templates/sonic-cli-gen/show.py.j2 index 2a3d065fdf..268893f189 100644 --- a/sonic-utilities-data/templates/sonic-cli-gen/show.py.j2 +++ b/sonic-utilities-data/templates/sonic-cli-gen/show.py.j2 @@ -54,6 +54,34 @@ def format_group_value(entry, attrs): return tabulate.tabulate(data, tablefmt="plain") +def get_db_table(db, db_name, table_name): + """ Retrieve data table from redis DB, support CONFIG_DB|STATE_DB|... + + Args: + db: Db object in utilities_common. + db_name: Redis db name + table_name: Redis table name + + Returns: + dict: data table. + """ + + table = {} + + if db_name == '' or db_name == 'CONFIG_DB': + table = db.cfgdb.get_table(table_name) + else: + db.db.connect(db_name) + seps = db.db.get_db_separator(db_name) + keys = db.db.keys(db_name, table_name + seps + '*') + + for key in keys: + t = db.db.get_all(db_name, key) + table[key[(key.find(seps) + 1):]] = t + + return table + + {# Generates a python list that represents a row in the table view. E.g: Jinja2: @@ -185,7 +213,7 @@ def {{ table.name }}_{{ object.name }}(db): header = {{ gen_header(object.attrs) }} body = [] - table = db.cfgdb.get_table("{{ table.name }}") + table = get_db_table(db, '{{ table['db-name'] }}', '{{ table.name }}') entry = table.get("{{ object.name }}", {}) row = {{ gen_row("entry", object.attrs) }} body.append(row) @@ -222,7 +250,7 @@ def {{ name }}(db): header = {{ gen_header(object["keys"] + object.attrs) }} body = [] - table = db.cfgdb.get_table("{{ table.name }}") + table = get_db_table(db, '{{ table['db-name'] }}', '{{ name }}') for key in natsort.natsorted(table): entry = table[key] if not isinstance(key, tuple): diff --git a/sonic_cli_gen/yang_parser.py b/sonic_cli_gen/yang_parser.py index cfefd9bc6e..302347066c 100644 --- a/sonic_cli_gen/yang_parser.py +++ b/sonic_cli_gen/yang_parser.py @@ -169,6 +169,13 @@ def on_table_container(y_module: OrderedDict, 'description': get_description(tbl_container) } + # add optional configuration for config and db-name, compatible + optional_keys = {'config':'config', 'db-name':'sonic-ext:db-name'} + for k, v in optional_keys.items(): + obj = tbl_container.get(v) + if obj is not None: + y2d_elem[k] = obj.get('@value') + # determine if 'table container' has a 'list' entity if tbl_container.get('list') is None: y2d_elem['static-objects'] = list()