Reworked exand/completion code for mountpoints

Fixed: [Tab completion mounted devices with lists](https://github.com/clicon/clixon-controller/issues/72)
This commit is contained in:
Olof hagsand 2024-01-01 21:56:51 +01:00
parent 0fff9d8ef5
commit 798a9fbfcb
4 changed files with 92 additions and 88 deletions

View file

@ -60,6 +60,7 @@ Developers may need to change their code
### Corrected Bugs ### Corrected Bugs
* Fixed: [Tab completion mounted devices with lists](https://github.com/clicon/clixon-controller/issues/72)
* Fixed: kill-session cleanup when client none existant, and for all db:s * Fixed: kill-session cleanup when client none existant, and for all db:s
* Fixed: [Using the characters '<' and '>' might cause an invalid diff](https://github.com/clicon/clixon-controller/issues/73) * Fixed: [Using the characters '<' and '>' might cause an invalid diff](https://github.com/clicon/clixon-controller/issues/73)

View file

@ -48,7 +48,6 @@
#include <stdarg.h> #include <stdarg.h>
#include <time.h> #include <time.h>
#include <ctype.h> #include <ctype.h>
#include <unistd.h> #include <unistd.h>
#include <dirent.h> #include <dirent.h>
#include <syslog.h> #include <syslog.h>
@ -242,8 +241,6 @@ expand_dbvar(void *h,
yang_stmt *ytype; yang_stmt *ytype;
char *mtpoint = NULL; char *mtpoint = NULL;
yang_stmt *yspec0 = NULL; yang_stmt *yspec0 = NULL;
yang_stmt *yspec;
yang_stmt *yu = NULL;
cvec *nsc0 = NULL; cvec *nsc0 = NULL;
char *str; char *str;
int grouping_treeref; int grouping_treeref;
@ -280,13 +277,14 @@ expand_dbvar(void *h,
} }
if (grouping_treeref && if (grouping_treeref &&
(callback_cvv = cligen_callback_arguments_get(cli_cligen(h))) != NULL){ (callback_cvv = cligen_callback_arguments_get(cli_cligen(h))) != NULL){
/* Concatenate callback arguments to a singel prepend string */ /* Concatenate callback arguments to a single prepend string */
if (cvec_concat_cb(callback_cvv, api_path_fmt_cb) < 0) if (cvec_concat_cb(callback_cvv, api_path_fmt_cb) < 0)
goto done; goto done;
} }
cprintf(api_path_fmt_cb, "%s", cv_string_get(cv)); cprintf(api_path_fmt_cb, "%s", cv_string_get(cv));
api_path_fmt = cbuf_get(api_path_fmt_cb); api_path_fmt = cbuf_get(api_path_fmt_cb);
if (cvec_len(argv) > 2){ if (cvec_len(argv) > 2){ /* mountpoint */
/* api_path_fmt is without top-level */
cv = cvec_i(argv, 2); cv = cvec_i(argv, 2);
str = cv_string_get(cv); str = cv_string_get(cv);
if (strncmp(str, "mtpoint:", strlen("mtpoint:")) != 0){ if (strncmp(str, "mtpoint:", strlen("mtpoint:")) != 0){
@ -294,13 +292,18 @@ expand_dbvar(void *h,
goto done; goto done;
} }
mtpoint = str + strlen("mtpoint:"); mtpoint = str + strlen("mtpoint:");
/* Get and combined api-path01 */
if (mtpoint_paths(yspec0, mtpoint, api_path_fmt, &api_path_fmt01) < 0)
goto done;
if (api_path_fmt2api_path(api_path_fmt01, cvv, &api_path, &cvvi) < 0)
goto done;
} }
/* api_path_fmt = /interface/%s/address/%s else{
* api_path: --> /interface/eth0/address/.* if (api_path_fmt2api_path(api_path_fmt, cvv, &api_path, &cvvi) < 0)
* xpath: --> /interface/[name="eth0"]/address goto done;
*/ }
if (api_path_fmt2api_path(api_path_fmt, cvv, &api_path, &cvvi) < 0) if (api_path == NULL)
goto done; goto ok;
/* Create config top-of-tree */ /* Create config top-of-tree */
if ((xtop = xml_new(DATASTORE_TOP_SYMBOL, NULL, CX_ELMNT)) == NULL) if ((xtop = xml_new(DATASTORE_TOP_SYMBOL, NULL, CX_ELMNT)) == NULL)
goto done; goto done;
@ -309,32 +312,28 @@ expand_dbvar(void *h,
* xpath2xml would have worked!! * xpath2xml would have worked!!
* XXX: but y is just the first in this list, there could be other y:s? * XXX: but y is just the first in this list, there could be other y:s?
*/ */
yspec = yspec0; /* may be reset to mount yspec below */ if ((ret = api_path2xml(api_path, yspec0, xtop, YC_DATANODE, 0, &xbot, &y, &xerr)) < 0)
if (api_path){
if (mtpoint){
if (yang_path_arg(yspec0, mtpoint, &yu) < 0)
goto done;
if (yang_mount_get(yu, mtpoint, &yspec) < 0)
goto done;
}
if ((ret = api_path2xml(api_path, yspec, xtop, YC_DATANODE, 0, &xbot, &y, &xerr)) < 0)
goto done;
if (ret == 0){
clixon_err_netconf(h, OE_NETCONF, 0, xerr, "Expand datastore symbol");
goto done;
}
}
if (y==NULL)
goto ok;
/* Transform api-path to xpath for netconf */
if (api_path2xpath(api_path, yspec, &xpath, &nsc, NULL) < 0)
goto done; goto done;
if (ret == 0){
clixon_err_netconf(h, OE_NETCONF, 0, xerr, "Expand datastore symbol");
goto done;
}
if (y == NULL){
clixon_err(OE_YANG, 0, "y is NULL");
goto done;
}
/* Transform api-path to xpath for netconf */
if ((ret = api_path2xpath(api_path, yspec0, &xpath, &nsc, &xerr)) < 0)
goto done;
if (ret == 0){
clixon_err_netconf(h, OE_NETCONF, 0, xerr, "Expand datastore symbol");
goto done;
}
if ((cbxpath = cbuf_new()) == NULL){ if ((cbxpath = cbuf_new()) == NULL){
clixon_err(OE_UNIX, errno, "cbuf_new"); clixon_err(OE_UNIX, errno, "cbuf_new");
goto done; goto done;
} }
if (mtpoint){ if (mtpoint){
cprintf(cbxpath, "%s", mtpoint);
if (xml_nsctx_yangspec(yspec0, &nsc0) < 0) if (xml_nsctx_yangspec(yspec0, &nsc0) < 0)
goto done; goto done;
cv = NULL; /* Append nsc0 to nsc */ cv = NULL; /* Append nsc0 to nsc */
@ -383,7 +382,7 @@ expand_dbvar(void *h,
if (clicon_rpc_get_config(h, NULL, dbstr, cbuf_get(cbxpath), nsc, NULL, &xt) < 0) if (clicon_rpc_get_config(h, NULL, dbstr, cbuf_get(cbxpath), nsc, NULL, &xt) < 0)
goto done; goto done;
if ((xe = xpath_first(xt, NULL, "/rpc-error")) != NULL){ if ((xe = xpath_first(xt, NULL, "/rpc-error")) != NULL){
clixon_err_netconf(h, OE_NETCONF, 0, xerr, "Get configuration"); clixon_err_netconf(h, OE_NETCONF, 0, xe, "Get configuration");
goto ok; goto ok;
} }
if (xpath_vec(xt, nsc, "%s", &xvec, &xlen, cbuf_get(cbxpath)) < 0) if (xpath_vec(xt, nsc, "%s", &xvec, &xlen, cbuf_get(cbxpath)) < 0)

View file

@ -618,7 +618,6 @@ api_path_fmt2xpath(char *api_path_fmt,
/*! Translate from restconf api-path(cvv) to xml xpath(cbuf) and namespace context /*! Translate from restconf api-path(cvv) to xml xpath(cbuf) and namespace context
* *
* @param[in] api_path URI-encoded path expression" (RFC8040 3.5.3) as cvec * @param[in] api_path URI-encoded path expression" (RFC8040 3.5.3) as cvec
* @param[in] offset Offset of cvec, where api-path starts
* @param[in] yspec Yang spec * @param[in] yspec Yang spec
* @param[in,out] xpath The xpath as cbuf (must be created and may have content) * @param[in,out] xpath The xpath as cbuf (must be created and may have content)
* @param[out] nsc Namespace context of xpath (free w xml_nsctx_free) * @param[out] nsc Namespace context of xpath (free w xml_nsctx_free)
@ -652,12 +651,11 @@ api_path_fmt2xpath(char *api_path_fmt,
* @see api_path2xpath Using strings as parameters * @see api_path2xpath Using strings as parameters
*/ */
static int static int
api_path2xpath_cvv(cvec *api_path, api_path2xpath_cvv(cvec *api_path,
int offset, yang_stmt *yspec,
yang_stmt *yspec, cbuf *xpath,
cbuf *xpath, cvec **nscp,
cvec **nscp, cxobj **xerr)
cxobj **xerr)
{ {
int retval = -1; int retval = -1;
int i; int i;
@ -682,6 +680,7 @@ api_path2xpath_cvv(cvec *api_path,
char *decval; char *decval;
int ret; int ret;
int root; int root;
int ymtpoint; /* y is potential mount-point */
cprintf(xpath, "/"); cprintf(xpath, "/");
/* Initialize namespace context */ /* Initialize namespace context */
@ -691,8 +690,9 @@ api_path2xpath_cvv(cvec *api_path,
clixon_err(OE_UNIX, errno, "cbuf_new"); clixon_err(OE_UNIX, errno, "cbuf_new");
goto done; goto done;
} }
ymtpoint = 0;
root = 1; /* root or mountpoint */ root = 1; /* root or mountpoint */
for (i=offset; i<cvec_len(api_path); i++){ for (i=0; i<cvec_len(api_path); i++){
cv = cvec_i(api_path, i); cv = cvec_i(api_path, i);
nodeid = cv_name_get(cv); nodeid = cv_name_get(cv);
/* api-path: prefix points to module */ /* api-path: prefix points to module */
@ -701,7 +701,7 @@ api_path2xpath_cvv(cvec *api_path,
clixon_debug(CLIXON_DBG_DETAIL, "%s [%d] cvname: %s:%s", clixon_debug(CLIXON_DBG_DETAIL, "%s [%d] cvname: %s:%s",
__FUNCTION__, i, prefix?prefix:"", name); __FUNCTION__, i, prefix?prefix:"", name);
/* top-node must have prefix */ /* top-node must have prefix */
if (i == offset && prefix == NULL){ if (i == 0 && prefix == NULL){
cprintf(cberr, "'%s': Expected prefix:name", nodeid); cprintf(cberr, "'%s': Expected prefix:name", nodeid);
if (xerr && netconf_invalid_value_xml(xerr, "application", cbuf_get(cberr)) < 0) if (xerr && netconf_invalid_value_xml(xerr, "application", cbuf_get(cberr)) < 0)
goto done; goto done;
@ -717,16 +717,46 @@ api_path2xpath_cvv(cvec *api_path,
} }
namespace = yang_find_mynamespace(ymod); /* change namespace */ namespace = yang_find_mynamespace(ymod); /* change namespace */
} }
if (root && ymod) /* root */ if (i == 0 || root || ymtpoint){
if (ymod == NULL){
cprintf(cberr, "'%s': Expected mountpoint prefix:name", nodeid);
if (xerr && netconf_invalid_value_xml(xerr, "application", cbuf_get(cberr)) < 0)
goto done;
goto fail;
}
y = yang_find_datanode(ymod, name); y = yang_find_datanode(ymod, name);
else }
else{
y = yang_find_datanode(y, name); y = yang_find_datanode(y, name);
root = 0; }
if (y == NULL){ if (y == NULL){
if (xerr && netconf_unknown_element_xml(xerr, "application", name, "Unknown element") < 0) if (xerr && netconf_unknown_element_xml(xerr, "application", name, "Unknown element") < 0)
goto done; goto done;
goto fail; goto fail;
} }
root = 0;
/* If x/y is mountpoint, change y to new yspec */
if ((ret = yang_schema_mount_point(y)) < 0)
goto done;
if (ret == 1){
y1 = NULL;
if (xml_nsctx_yangspec(yspec, &nsc) < 0)
goto done;
/* cf xml_bind_yang0_opt/xml_yang_mount_get */
if (yang_mount_get(y, cbuf_get(xpath), &y1) < 0)
goto done;
if (y1 != NULL){
y = y1;
yspec = y1;
root = 1;
}
}
if ((ymtpoint = yang_schema_mount_point(y)) < 0)
goto done;
if (ymtpoint){
if (yang_mount_get_yspec_any(y, &yspec) < 0)
goto done;
}
/* Get XML/xpath prefix given namespace. /* Get XML/xpath prefix given namespace.
* note different from api-path prefix * note different from api-path prefix
*/ */
@ -740,6 +770,11 @@ api_path2xpath_cvv(cvec *api_path,
if (xml_nsctx_add(nsc, xprefix, namespace) < 0) if (xml_nsctx_add(nsc, xprefix, namespace) < 0)
goto done; goto done;
} }
if (i > 0)
cprintf(xpath, "/");
if (xprefix)
cprintf(xpath, "%s:", xprefix);
cprintf(xpath, "%s", name);
/* Check if has value, means '=' */ /* Check if has value, means '=' */
if (cv_type_get(cv) == CGV_STRING){ if (cv_type_get(cv) == CGV_STRING){
/* val is uri percent encoded, eg x%2Cy,z */ /* val is uri percent encoded, eg x%2Cy,z */
@ -759,11 +794,6 @@ api_path2xpath_cvv(cvec *api_path,
cvk = yang_cvec_get(y); /* Use Y_LIST cache, see ys_populate_list() */ cvk = yang_cvec_get(y); /* Use Y_LIST cache, see ys_populate_list() */
cvi = NULL; cvi = NULL;
/* Iterate over individual yang keys */ /* Iterate over individual yang keys */
if (i != offset)
cprintf(xpath, "/");
if (xprefix)
cprintf(xpath, "%s:", xprefix);
cprintf(xpath, "%s", name);
vi = 0; vi = 0;
while ((cvi = cvec_each(cvk, cvi)) != NULL && vi<nvalvec){ while ((cvi = cvec_each(cvk, cvi)) != NULL && vi<nvalvec){
cprintf(xpath, "["); cprintf(xpath, "[");
@ -781,50 +811,17 @@ api_path2xpath_cvv(cvec *api_path,
} }
break; break;
case Y_LEAF_LIST: /* XXX: LOOP? */ case Y_LEAF_LIST: /* XXX: LOOP? */
if (i != offset)
cprintf(xpath, "/");
if (xprefix)
cprintf(xpath, "%s:", xprefix);
cprintf(xpath, "%s", name);
if (val) if (val)
cprintf(xpath, "[.='%s']", val); cprintf(xpath, "[.='%s']", val);
else else
cprintf(xpath, "[.='']"); cprintf(xpath, "[.='']");
break; break;
default: default:
if (i != offset)
cprintf(xpath, "/");
if (xprefix)
cprintf(xpath, "%s:", xprefix);
cprintf(xpath, "%s", name);
break; break;
} }
if (val) if (val)
free(val); free(val);
} }
else{
if (i != offset)
cprintf(xpath, "/");
if (xprefix)
cprintf(xpath, "%s:", xprefix);
cprintf(xpath, "%s", name);
}
/* If x/y is mountpoint, pass mount yspec to children */
if ((ret = yang_schema_mount_point(y)) < 0)
goto done;
if (ret == 1){
y1 = NULL;
if (xml_nsctx_yangspec(yspec, &nsc) < 0)
goto done;
/* cf xml_bind_yang0_opt/xml_yang_mount_get */
if (yang_mount_get(y, cbuf_get(xpath), &y1) < 0)
goto done;
if (y1 != NULL){
y = y1;
yspec = y1;
root = 1;
}
}
if (prefix){ if (prefix){
free(prefix); free(prefix);
prefix = NULL; prefix = NULL;
@ -910,7 +907,7 @@ api_path2xpath(char *api_path,
goto done; goto done;
if ((xpath = cbuf_new()) == NULL) if ((xpath = cbuf_new()) == NULL)
goto done; goto done;
if ((ret = api_path2xpath_cvv(cvv, 0, yspec, xpath, nsc, xerr)) < 0) if ((ret = api_path2xpath_cvv(cvv, yspec, xpath, nsc, xerr)) < 0)
goto done; goto done;
if (ret == 0) if (ret == 0)
goto fail; goto fail;
@ -983,9 +980,9 @@ api_path2xml_vec(char **vec,
char *namespace = NULL; char *namespace = NULL;
cbuf *cberr = NULL; cbuf *cberr = NULL;
char *val = NULL; char *val = NULL;
int ret;
char *xpath = NULL; char *xpath = NULL;
cvec *nsc = NULL; cvec *nsc = NULL;
int ymtpoint; /* y is mountpoint */
if ((nodeid = vec[0]) == NULL || strlen(nodeid)==0){ if ((nodeid = vec[0]) == NULL || strlen(nodeid)==0){
if (xbotp) if (xbotp)
@ -1006,7 +1003,9 @@ api_path2xml_vec(char **vec,
/* Split into prefix and localname */ /* Split into prefix and localname */
if (nodeid_split(nodeid, &prefix, &name) < 0) if (nodeid_split(nodeid, &prefix, &name) < 0)
goto done; goto done;
if (yang_keyword_get(y0) == Y_SPEC){ /* top-node */ if ((ymtpoint = yang_schema_mount_point(y0)) < 0)
goto done;
if (yang_keyword_get(y0) == Y_SPEC || ymtpoint){
if (prefix == NULL){ if (prefix == NULL){
cprintf(cberr, "api-path element '%s', expected prefix:name", nodeid); cprintf(cberr, "api-path element '%s', expected prefix:name", nodeid);
if (xerr && if (xerr &&
@ -1014,6 +1013,10 @@ api_path2xml_vec(char **vec,
goto done; goto done;
goto fail; goto fail;
} }
if (ymtpoint){
if (yang_mount_get_yspec_any(y0, &y0) < 0)
goto done;
}
if ((ymod = yang_find_module_by_name(y0, prefix)) == NULL){ if ((ymod = yang_find_module_by_name(y0, prefix)) == NULL){
cprintf(cberr, "No such yang module prefix"); cprintf(cberr, "No such yang module prefix");
if (xerr && if (xerr &&
@ -1026,7 +1029,7 @@ api_path2xml_vec(char **vec,
} }
y = (nodeclass==YC_SCHEMANODE)? y = (nodeclass==YC_SCHEMANODE)?
yang_find_schemanode(y0, name): yang_find_schemanode(y0, name):
yang_find_datanode(y0, name); yang_find_datanode(y0, name); // <--
if (y == NULL){ if (y == NULL){
if (xerr && if (xerr &&
netconf_unknown_element_xml(xerr, "application", name, "Unknown element") < 0) netconf_unknown_element_xml(xerr, "application", name, "Unknown element") < 0)
@ -1154,9 +1157,9 @@ api_path2xml_vec(char **vec,
goto done; goto done;
} }
/* If x/y is mountpoint, pass mount yspec to children */ /* If x/y is mountpoint, pass mount yspec to children */
if ((ret = yang_schema_mount_point(y)) < 0) if ((ymtpoint = yang_schema_mount_point(y)) < 0)
goto done; goto done;
if (ret == 1){ if (ymtpoint){
y1 = NULL; y1 = NULL;
if (xml_nsctx_yangspec(ys_spec(y), &nsc) < 0) if (xml_nsctx_yangspec(ys_spec(y), &nsc) < 0)
goto done; goto done;

View file

@ -152,6 +152,7 @@ yang_schema_mount_point(yang_stmt *y)
* @param[in] xpath Key for yspec on y * @param[in] xpath Key for yspec on y
* @param[out] yspec YANG stmt spec * @param[out] yspec YANG stmt spec
* @retval 0 OK * @retval 0 OK
* @see yang_mount_get_yspec_any for ANY yspec not for a specific xpath
*/ */
int int
yang_mount_get(yang_stmt *y, yang_mount_get(yang_stmt *y,