Skip to content

Commit

Permalink
[sonic-cli-gen] first phase implementation of the SONiC CLI Auto-gene…
Browse files Browse the repository at this point in the history
…ration tool (#1644)

What I did
Implemented the sonic-cli-gen according to the SONiC CLI Auto-generation HLD

How I did it
How the sonic-cli-gen works:

Parse provided YANG model to the intermediate representation - python dictionary (you can find the structure in the sonic_cli_gen/yang_parser.py file).
Pass the python dictionary to the appropriate j2 template (config or show) and generate the CLI plugin.
Put the auto-generated CLI plugin to an appropriate location, where it can be discovered by SONiC CLI.
How to verify it
Check the file with unit tests - tests/cli_autogen_yang_parser_test.py

Command output
admin@sonic: sonic-cli-gen generate config sonic-acl
Loaded below Yang Models
['sonic-acl', 'sonic-breakout_cfg', 'sonic-crm', 'sonic-device_metadata', 'sonic-device_neighbor', 'sonic-extension', 'sonic-flex_counter', 'sonic-interface', 'sonic-loopback-interface', 'sonic-port', 'sonic-portchannel', 'sonic-types', 'sonic-versions', 'sonic-vlan', 'sonic-vrf']
Note: Below table(s) have no YANG models:
COPP_GROUP, COPP_TRAP, FEATURE, KDUMP, MGMT_INTERFACE, SNMP, SNMP_COMMUNITY, WJH, WJH_CHANNEL, 
INFO:sonic-cli-gen: Auto-generation successful! Location: /usr/local/lib/python3.7/dist-packages/config/plugins/auto/sonic-acl_yang.py
admin@sonic: sonic-cli-gen remove config sonic-acl
/usr/local/lib/python3.7/dist-packages/config/plugins/auto/sonic-acl_yang.py was removed.
  • Loading branch information
vadymhlushko-mlnx committed Nov 17, 2021
1 parent a3e34e3 commit e7535ae
Show file tree
Hide file tree
Showing 39 changed files with 3,759 additions and 8 deletions.
3 changes: 1 addition & 2 deletions clear/main.py
Expand Up @@ -484,8 +484,7 @@ def remap_keys(dict):

# Load plugins and register them
helper = util_base.UtilHelper()
for plugin in helper.load_plugins(plugins):
helper.register_plugin(plugin, cli)
helper.load_and_register_plugins(plugins, cli)


if __name__ == '__main__':
Expand Down
Empty file added clear/plugins/auto/__init__.py
Empty file.
3 changes: 1 addition & 2 deletions config/main.py
Expand Up @@ -5966,8 +5966,7 @@ def smoothing_interval(interval, rates_type):

# Load plugins and register them
helper = util_base.UtilHelper()
for plugin in helper.load_plugins(plugins):
helper.register_plugin(plugin, config)
helper.load_and_register_plugins(plugins, config)


if __name__ == '__main__':
Expand Down
Empty file added config/plugins/auto/__init__.py
Empty file.
5 changes: 5 additions & 0 deletions setup.py
Expand Up @@ -23,8 +23,10 @@
'acl_loader',
'clear',
'clear.plugins',
'clear.plugins.auto',
'config',
'config.plugins',
'config.plugins.auto',
'connect',
'consutil',
'counterpoll',
Expand All @@ -48,6 +50,7 @@
'show',
'show.interfaces',
'show.plugins',
'show.plugins.auto',
'sonic_installer',
'sonic_installer.bootloader',
'sonic_package_manager',
Expand All @@ -56,6 +59,7 @@
'undebug',
'utilities_common',
'watchdogutil',
'sonic_cli_gen',
],
package_data={
'generic_config_updater': ['generic_updater_config.conf.json'],
Expand Down Expand Up @@ -172,6 +176,7 @@
'spm = sonic_package_manager.main:cli',
'undebug = undebug.main:cli',
'watchdogutil = watchdogutil.main:watchdogutil',
'sonic-cli-gen = sonic_cli_gen.main:cli',
]
},
install_requires=[
Expand Down
3 changes: 1 addition & 2 deletions show/main.py
Expand Up @@ -1701,8 +1701,7 @@ def ztp(status, verbose):

# Load plugins and register them
helper = util_base.UtilHelper()
for plugin in helper.load_plugins(plugins):
helper.register_plugin(plugin, cli)
helper.load_and_register_plugins(plugins, cli)

if __name__ == '__main__':
cli()
Empty file added show/plugins/auto/__init__.py
Empty file.
8 changes: 8 additions & 0 deletions sonic-utilities-data/bash_completion.d/sonic-cli-gen
@@ -0,0 +1,8 @@
_sonic_cli_gen_completion() {
COMPREPLY=( $( env COMP_WORDS="${COMP_WORDS[*]}" \
COMP_CWORD=$COMP_CWORD \
_SONIC_CLI_GEN_COMPLETE=complete $1 ) )
return 0
}

complete -F _sonic_cli_gen_completion -o default sonic-cli-gen;
5 changes: 3 additions & 2 deletions sonic-utilities-data/debian/install
@@ -1,2 +1,3 @@
bash_completion.d/ /etc/
templates/*.j2 /usr/share/sonic/templates/
bash_completion.d/ /etc/
templates/*.j2 /usr/share/sonic/templates/
templates/sonic-cli-gen/*.j2 /usr/share/sonic/templates/sonic-cli-gen/
3 changes: 3 additions & 0 deletions sonic-utilities-data/templates/sonic-cli-gen/common.j2
@@ -0,0 +1,3 @@
{% macro cli_name(name) -%}
{{ name|lower|replace("_", "-") }}
{%- endmacro %}

0 comments on commit e7535ae

Please sign in to comment.