#!/usr/bin/env bash # Tests for using autocli extension defined in clixon-lib # This is both a test of yang extensions and autocli # The extension is autocli-op and can take the value "hide" (maybe more) # Try both inline and augmented mode # @see https://clixon-docs.readthedocs.io/en/latest/misc.html#extensions # Magic line must be first in script (see README.md) s="$_" ; . ./lib.sh || if [ "$s" = $0 ]; then exit 0; else return 0; fi APPNAME=example fin=$dir/in cfg=$dir/conf_yang.xml fyang=$dir/example.yang fyang2=$dir/$APPNAME-augment.yang clidir=$dir/cli if [ -d $clidir ]; then rm -rf $clidir/* else mkdir $clidir fi # Use yang in example cat < $cfg $cfg ietf-netconf:startup /usr/local/share/clixon $dir $dir /usr/local/lib/$APPNAME/backend $clidir /usr/local/lib/$APPNAME/cli $APPNAME 2 VARS /usr/local/var/$APPNAME/$APPNAME.sock /usr/local/var/$APPNAME/$APPNAME.pidfile $dir false clixon-restconf EOF cat < $clidir/ex.cli CLICON_MODE="example"; CLICON_PROMPT="%U@%H %W> "; # Autocli syntax tree operations edit @datamodel, cli_auto_edit("datamodel"); up, cli_auto_up("datamodel"); top, cli_auto_top("datamodel"); set @datamodel, cli_auto_set(); merge @datamodel, cli_auto_merge(); create @datamodel, cli_auto_create(); delete("Delete a configuration item") { @datamodel, cli_auto_del(); all("Delete whole candidate configuration"), delete_all("candidate"); } show("Show a particular state of the system"){ configuration("Show configuration"), cli_auto_show("datamodel", "candidate", "text", true, false);{ xml("Show configuration as XML"), cli_auto_show("datamodel", "candidate", "xml", false, false); } } EOF # Yang specs must be here first for backend. But then the specs are changed but just for CLI # Annotate original Yang spec example directly # First annotate /table/parameter # Had a problem with unknown in grouping -> test uses uses/grouping cat < $fyang module example { namespace "urn:example:clixon"; prefix ex; import clixon-lib{ prefix cl; } grouping pg { cl:autocli-op hide; /* This is the extension */ leaf value{ description "a value"; type string; } list index{ key i; leaf i{ type string; } leaf iv{ type string; } } } container table{ list parameter{ key name; leaf name{ type string; } uses pg; } } } EOF # Original no annotations for backend cat < $fyang2 module example-augment { namespace "urn:example:augment"; prefix aug; import example{ prefix ex; } import clixon-lib{ prefix cl; } } EOF new "test params: -f $cfg" if [ $BE -ne 0 ]; then new "kill old backend" sudo clixon_backend -z -f $cfg if [ $? -ne 0 ]; then err fi new "start backend -s init -f $cfg" start_backend -s init -f $cfg fi new "wait backend" wait_backend function testparam() { # Try hidden parameter list new "query table parameter hidden" expectpart "$(echo "set table ?" | $clixon_cli -f $cfg 2>&1)" 0 "set table" "" --not-- "parameter" cat < $fin set table parameter x show config xml EOF new "set table parameter hidden" expectpart "$(cat $fin | $clixon_cli -f $cfg 2>&1)" 0 "set table parameter x" "x
" } function testvalue() { # Try not hidden parameter list new "query table parameter hidden" expectpart "$(echo "set table ?" | $clixon_cli -f $cfg 2>&1)" 0 "set table" "" "parameter" # Try hidden value new "query table leaf" expectpart "$(echo "set table parameter x ?" | $clixon_cli -f $cfg 2>&1)" 0 "index" "" --not-- "value" cat < $fin set table parameter x value 42 show config xml EOF new "set table parameter hidden leaf" expectpart "$(cat $fin | $clixon_cli -f $cfg 2>&1)" 0 "x42
" } # INLINE MODE new "Test hidden parameter in table/param inline" testparam # Second annotate /table/parameter/value cat < $fyang module example { namespace "urn:example:clixon"; prefix ex; import clixon-lib{ prefix cl; } container table{ list parameter{ key name; leaf name{ type string; } leaf value{ cl:autocli-op hide; /* Here is the example */ description "a value"; type string; } list index{ key i; leaf i{ type string; } leaf iv{ type string; } } } } } EOF new "Test hidden parameter in table/param/value inline" testvalue # AUGMENT MODE # Here use a new yang module that augments, keep original example intact cat < $fyang module example { namespace "urn:example:clixon"; prefix ex; container table{ list parameter{ key name; leaf name{ type string; } leaf value{ description "a value"; type string; } list index{ key i; leaf i{ type string; } leaf iv{ type string; } } } } } EOF # First annotate /table/parameter cat < $fyang2 module example-augment { namespace "urn:example:augment"; prefix aug; import example{ prefix ex; } import clixon-lib{ prefix cl; } augment "/ex:table/ex:parameter" { cl:autocli-op hide; } } EOF new "Test hidden parameter in table/param augment" testparam # Try hidden specific parameter key (note only cli yang) # Second annotate /table/parameter/value cat < $fyang2 module example-augment { namespace "urn:example:augment"; prefix aug; import example{ prefix ex; } import clixon-lib{ prefix cl; } augment "/ex:table/ex:parameter/ex:value" { cl:autocli-op hide; } } EOF new "Test hidden parameter in table/param/value augment" testvalue # Example using imported module where clixon-lib is NOT declared in main module # see discussion in ys_populate_unknown (y1 vs y2) and # https://github.com/clicon/clixon/issues/282 cat < $fyang module example { yang-version 1.1; namespace "urn:example:clixon"; prefix ex; import example-augment{ prefix aug; } container table{ list parameter{ key name; leaf name{ type string; } uses aug:pg; } } } EOF # Use this as grouping (not annotate) cat < $fyang2 module example-augment { namespace "urn:example:augment"; prefix aug; import clixon-lib{ prefix cl; } grouping pg { cl:autocli-op hide; /* This is the extension */ leaf value{ description "a value"; type string; } list index{ key i; leaf i{ type string; } leaf iv{ type string; } } } } EOF new "Test hidden parameter in imported module" testparam new "Kill backend" # Check if premature kill pid=$(pgrep -u root -f clixon_backend) if [ -z "$pid" ]; then err "backend already dead" fi # kill backend stop_backend -f $cfg rm -rf $dir new "endtest" endtest