diff --git a/CHANGELOG.md b/CHANGELOG.md index fb3748802..838ccf648 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -47,6 +47,7 @@ Expected: December 2023 ### Corrected Bugs +* Fixed: [Does clixon cli support autocompletion for leafrefs pointed to another module?](https://github.com/clicon/clixon/issues/455) * Fixed: [commit diff sometimes includes namespace in output](https://github.com/clicon/clixon-controller/issues/44) ## 6.4.0 diff --git a/apps/cli/cli_show.c b/apps/cli/cli_show.c index 98a085724..d7850d0fd 100644 --- a/apps/cli/cli_show.c +++ b/apps/cli/cli_show.c @@ -74,10 +74,17 @@ #include "cli_autocli.h" #include "cli_common.h" /* internal functions */ -/*! Given an xpath encoded in a cbuf, append a second xpath into the first +/*! Given an xpath encoded in a cbuf, append a second xpath into the first (unless absolute path) * * The method reuses prefixes from xpath1 if they exist, otherwise the module prefix * from y is used. Unless the element is .., . + * @param[in,out] cb0 Result XPath as cbuf + * @param[in] xpath1 Input XPath + * @param[in] y Yang of xpath1 + * @param[in,out] nsc Namespace + * @retval 0 OK + * @retval -1 Error + * * XXX: Predicates not handled * The algorithm is not fool-proof, there are many cases it may not work * To make it more complete, maybe parse the xpath to a tree and put it @@ -86,8 +93,6 @@ goto done; if (xpath_tree2cbuf(xpt, xcb) < 0) goto done; -and -traverse_canonical */ static int xpath_append(cbuf *cb0, @@ -105,6 +110,9 @@ xpath_append(cbuf *cb0, char *prefix = NULL; int initialups = 1; /* If starts with ../../.. */ char *xpath0; + char *ns; + int ret; + int j; if (cb0 == NULL){ clicon_err(OE_XML, EINVAL, "cb0 is NULL"); @@ -130,7 +138,6 @@ xpath_append(cbuf *cb0, else if (strcmp(id, "..") == 0){ if (initialups){ /* Subtract from xpath0 */ - int j; for (j=cbuf_len(cb0); j >= 0; j--){ if (xpath0[j] != '/') continue; @@ -145,6 +152,18 @@ xpath_append(cbuf *cb0, } else{ initialups = 0; + /* If prefix is not in nsc, it needs to be added */ + if (prefix && cvec_find(nsc, prefix) == NULL){ + ns = NULL; + if ((ret = yang_find_namespace_by_prefix(y, prefix, &ns)) < 0) + goto done; + if (ret == 0){ + clicon_err(OE_DB, 0, "Prefix %s does not have an associated namespace", prefix); + goto done; + } + if (xml_nsctx_add(nsc, prefix, ns) < 0) + goto done; + } cprintf(cb0, "/%s:%s", prefix?prefix:myprefix, id); } if (prefix){ diff --git a/test/test_cli_leafref.sh b/test/test_cli_leafref.sh index f0f039162..886e2d557 100755 --- a/test/test_cli_leafref.sh +++ b/test/test_cli_leafref.sh @@ -11,6 +11,7 @@ APPNAME=example cfg=$dir/conf_yang.xml fyang=$dir/example-leafref.yang +fyang2=$dir/example-other.yang clidir=$dir/clidir if [ ! -d $clidir ]; then mkdir $clidir @@ -22,8 +23,7 @@ cat < $cfg $cfg ietf-netconf:startup ${YANG_INSTALLDIR} - $dir - example-leafref + $dir /usr/local/lib/$APPNAME/backend $APPNAME /usr/local/lib/$APPNAME/cli @@ -147,6 +147,30 @@ module example-leafref{ } EOF +cat < $fyang2 +module example-other{ + yang-version 1.1; + namespace "urn:example:other"; + prefix oth; + import example-leafref { + prefix ex; + } + /* leafref to other module */ + container leafrefother { + description "Leafref absolute path to other module"; + list leafref{ + key name; + leaf name { + type leafref{ + path "/ex:table/ex:parameter/ex:name"; + require-instance true; + } + } + } + } +} +EOF + # clispec files 1..6 for submodes AAA and BBB as described in top comment cat < $clidir/cli1.cli @@ -212,8 +236,6 @@ expectpart "$(echo "set identityrefs identityref ?" | $clixon_cli -f $cfg 2> /de # Expected: # # CLI syntax error: "set leafrefs leafref": Incomplete command -echo "set leafrefs leafref ?" | $clixon_cli -f $cfg -o CLICON_CLI_EXPAND_LEAFREF=false - new "expand leafref 1st level" expectpart "$(echo "set leafrefs leafref ?" | $clixon_cli -f $cfg -o CLICON_CLI_EXPAND_LEAFREF=false 2> /dev/null)" 0 "" --not-- "91" "92" "93" @@ -291,6 +313,12 @@ expectpart "$($clixon_cli -1 -f $cfg -l o commit)" 0 "^$" new "show config" expectpart "$($clixon_cli -1 -f $cfg -l o show config cli)" 0 "set table parameter 91" "set table parameter 92" "set table parameter 93" "set leafrefs leafref 91" "set leafrefs leafref 93" "set identityrefs identityref ex:des" "set identityrefs identityref ex:des3" "set leafrefs2 leafref 91" "set identityrefs2 identityref ex:des" --not-- "set identityrefs identityref ex:des2" "set leafrefs leafref 92" +new "set leafrefother aaa" +expectpart "$(echo "set leafrefother leafref ?" | $clixon_cli -f $cfg -o CLICON_CLI_EXPAND_LEAFREF=true 2> /dev/null)" 0 "91" "92" "93" + +new "cli commit" +expectpart "$($clixon_cli -1 -f $cfg -l o commit)" 0 "^$" + if [ $BE -ne 0 ]; then new "Kill backend" # Check if premature kill