Skip to content

Commit e7535ae

Browse files
[sonic-cli-gen] first phase implementation of the SONiC CLI Auto-generation 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.
1 parent a3e34e3 commit e7535ae

39 files changed

+3759
-8
lines changed

clear/main.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -484,8 +484,7 @@ def remap_keys(dict):
484484

485485
# Load plugins and register them
486486
helper = util_base.UtilHelper()
487-
for plugin in helper.load_plugins(plugins):
488-
helper.register_plugin(plugin, cli)
487+
helper.load_and_register_plugins(plugins, cli)
489488

490489

491490
if __name__ == '__main__':

clear/plugins/auto/__init__.py

Whitespace-only changes.

config/main.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -5966,8 +5966,7 @@ def smoothing_interval(interval, rates_type):
59665966

59675967
# Load plugins and register them
59685968
helper = util_base.UtilHelper()
5969-
for plugin in helper.load_plugins(plugins):
5970-
helper.register_plugin(plugin, config)
5969+
helper.load_and_register_plugins(plugins, config)
59715970

59725971

59735972
if __name__ == '__main__':

config/plugins/auto/__init__.py

Whitespace-only changes.

setup.py

+5
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
'acl_loader',
2424
'clear',
2525
'clear.plugins',
26+
'clear.plugins.auto',
2627
'config',
2728
'config.plugins',
29+
'config.plugins.auto',
2830
'connect',
2931
'consutil',
3032
'counterpoll',
@@ -48,6 +50,7 @@
4850
'show',
4951
'show.interfaces',
5052
'show.plugins',
53+
'show.plugins.auto',
5154
'sonic_installer',
5255
'sonic_installer.bootloader',
5356
'sonic_package_manager',
@@ -56,6 +59,7 @@
5659
'undebug',
5760
'utilities_common',
5861
'watchdogutil',
62+
'sonic_cli_gen',
5963
],
6064
package_data={
6165
'generic_config_updater': ['generic_updater_config.conf.json'],
@@ -172,6 +176,7 @@
172176
'spm = sonic_package_manager.main:cli',
173177
'undebug = undebug.main:cli',
174178
'watchdogutil = watchdogutil.main:watchdogutil',
179+
'sonic-cli-gen = sonic_cli_gen.main:cli',
175180
]
176181
},
177182
install_requires=[

show/main.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1701,8 +1701,7 @@ def ztp(status, verbose):
17011701

17021702
# Load plugins and register them
17031703
helper = util_base.UtilHelper()
1704-
for plugin in helper.load_plugins(plugins):
1705-
helper.register_plugin(plugin, cli)
1704+
helper.load_and_register_plugins(plugins, cli)
17061705

17071706
if __name__ == '__main__':
17081707
cli()

show/plugins/auto/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
_sonic_cli_gen_completion() {
2+
COMPREPLY=( $( env COMP_WORDS="${COMP_WORDS[*]}" \
3+
COMP_CWORD=$COMP_CWORD \
4+
_SONIC_CLI_GEN_COMPLETE=complete $1 ) )
5+
return 0
6+
}
7+
8+
complete -F _sonic_cli_gen_completion -o default sonic-cli-gen;

sonic-utilities-data/debian/install

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
bash_completion.d/ /etc/
2-
templates/*.j2 /usr/share/sonic/templates/
1+
bash_completion.d/ /etc/
2+
templates/*.j2 /usr/share/sonic/templates/
3+
templates/sonic-cli-gen/*.j2 /usr/share/sonic/templates/sonic-cli-gen/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{% macro cli_name(name) -%}
2+
{{ name|lower|replace("_", "-") }}
3+
{%- endmacro %}

0 commit comments

Comments
 (0)