Skip to content

Commit

Permalink
Reworked exand/completion code for mountpoints
Browse files Browse the repository at this point in the history
Fixed: [Tab completion mounted devices with lists](clicon/clixon-controller#72)
  • Loading branch information
olofhagsand committed Jan 2, 2024
1 parent 0fff9d8 commit 798a9fb
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 86 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ Developers may need to change their code

### 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: [Using the characters '<' and '>' might cause an invalid diff](https://github.com/clicon/clixon-controller/issues/73)

Expand Down
59 changes: 29 additions & 30 deletions apps/cli/cli_show.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
#include <stdarg.h>
#include <time.h>
#include <ctype.h>

#include <unistd.h>
#include <dirent.h>
#include <syslog.h>
Expand Down Expand Up @@ -242,8 +241,6 @@ expand_dbvar(void *h,
yang_stmt *ytype;
char *mtpoint = NULL;
yang_stmt *yspec0 = NULL;
yang_stmt *yspec;
yang_stmt *yu = NULL;
cvec *nsc0 = NULL;
char *str;
int grouping_treeref;
Expand Down Expand Up @@ -280,27 +277,33 @@ expand_dbvar(void *h,
}
if (grouping_treeref &&
(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)
goto done;
}
cprintf(api_path_fmt_cb, "%s", cv_string_get(cv));
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);
str = cv_string_get(cv);
if (strncmp(str, "mtpoint:", strlen("mtpoint:")) != 0){
clixon_err(OE_PLUGIN, 0, "mtpoint does not begin with 'mtpoint:'");
goto done;
}
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
* api_path: --> /interface/eth0/address/.*
* xpath: --> /interface/[name="eth0"]/address
*/
if (api_path_fmt2api_path(api_path_fmt, cvv, &api_path, &cvvi) < 0)
goto done;
else{
if (api_path_fmt2api_path(api_path_fmt, cvv, &api_path, &cvvi) < 0)
goto done;
}
if (api_path == NULL)
goto ok;
/* Create config top-of-tree */
if ((xtop = xml_new(DATASTORE_TOP_SYMBOL, NULL, CX_ELMNT)) == NULL)
goto done;
Expand All @@ -309,32 +312,28 @@ expand_dbvar(void *h,
* xpath2xml would have worked!!
* 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 (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 ((ret = api_path2xml(api_path, yspec0, 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){
clixon_err(OE_YANG, 0, "y is NULL");
goto done;
}
if (y==NULL)
goto ok;
/* Transform api-path to xpath for netconf */
if (api_path2xpath(api_path, yspec, &xpath, &nsc, NULL) < 0)
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){
clixon_err(OE_UNIX, errno, "cbuf_new");
goto done;
}
if (mtpoint){
cprintf(cbxpath, "%s", mtpoint);
if (xml_nsctx_yangspec(yspec0, &nsc0) < 0)
goto done;
cv = NULL; /* Append nsc0 to nsc */
Expand Down Expand Up @@ -383,7 +382,7 @@ expand_dbvar(void *h,
if (clicon_rpc_get_config(h, NULL, dbstr, cbuf_get(cbxpath), nsc, NULL, &xt) < 0)
goto done;
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;
}
if (xpath_vec(xt, nsc, "%s", &xvec, &xlen, cbuf_get(cbxpath)) < 0)
Expand Down
115 changes: 59 additions & 56 deletions lib/src/clixon_path.c
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,6 @@ api_path_fmt2xpath(char *api_path_fmt,
/*! 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] offset Offset of cvec, where api-path starts
* @param[in] yspec Yang spec
* @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)
Expand Down Expand Up @@ -652,12 +651,11 @@ api_path_fmt2xpath(char *api_path_fmt,
* @see api_path2xpath Using strings as parameters
*/
static int
api_path2xpath_cvv(cvec *api_path,
int offset,
yang_stmt *yspec,
cbuf *xpath,
cvec **nscp,
cxobj **xerr)
api_path2xpath_cvv(cvec *api_path,
yang_stmt *yspec,
cbuf *xpath,
cvec **nscp,
cxobj **xerr)
{
int retval = -1;
int i;
Expand All @@ -682,6 +680,7 @@ api_path2xpath_cvv(cvec *api_path,
char *decval;
int ret;
int root;
int ymtpoint; /* y is potential mount-point */

cprintf(xpath, "/");
/* Initialize namespace context */
Expand All @@ -691,8 +690,9 @@ api_path2xpath_cvv(cvec *api_path,
clixon_err(OE_UNIX, errno, "cbuf_new");
goto done;
}
ymtpoint = 0;
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);
nodeid = cv_name_get(cv);
/* api-path: prefix points to module */
Expand All @@ -701,7 +701,7 @@ api_path2xpath_cvv(cvec *api_path,
clixon_debug(CLIXON_DBG_DETAIL, "%s [%d] cvname: %s:%s",
__FUNCTION__, i, prefix?prefix:"", name);
/* top-node must have prefix */
if (i == offset && prefix == NULL){
if (i == 0 && prefix == NULL){
cprintf(cberr, "'%s': Expected prefix:name", nodeid);
if (xerr && netconf_invalid_value_xml(xerr, "application", cbuf_get(cberr)) < 0)
goto done;
Expand All @@ -717,16 +717,46 @@ api_path2xpath_cvv(cvec *api_path,
}
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);
else
}
else{
y = yang_find_datanode(y, name);
root = 0;
}
if (y == NULL){
if (xerr && netconf_unknown_element_xml(xerr, "application", name, "Unknown element") < 0)
goto done;
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.
* note different from api-path prefix
*/
Expand All @@ -740,6 +770,11 @@ api_path2xpath_cvv(cvec *api_path,
if (xml_nsctx_add(nsc, xprefix, namespace) < 0)
goto done;
}
if (i > 0)
cprintf(xpath, "/");
if (xprefix)
cprintf(xpath, "%s:", xprefix);
cprintf(xpath, "%s", name);
/* Check if has value, means '=' */
if (cv_type_get(cv) == CGV_STRING){
/* val is uri percent encoded, eg x%2Cy,z */
Expand All @@ -759,11 +794,6 @@ api_path2xpath_cvv(cvec *api_path,
cvk = yang_cvec_get(y); /* Use Y_LIST cache, see ys_populate_list() */
cvi = NULL;
/* Iterate over individual yang keys */
if (i != offset)
cprintf(xpath, "/");
if (xprefix)
cprintf(xpath, "%s:", xprefix);
cprintf(xpath, "%s", name);
vi = 0;
while ((cvi = cvec_each(cvk, cvi)) != NULL && vi<nvalvec){
cprintf(xpath, "[");
Expand All @@ -781,50 +811,17 @@ api_path2xpath_cvv(cvec *api_path,
}
break;
case Y_LEAF_LIST: /* XXX: LOOP? */
if (i != offset)
cprintf(xpath, "/");
if (xprefix)
cprintf(xpath, "%s:", xprefix);
cprintf(xpath, "%s", name);
if (val)
cprintf(xpath, "[.='%s']", val);
else
cprintf(xpath, "[.='']");
break;
default:
if (i != offset)
cprintf(xpath, "/");
if (xprefix)
cprintf(xpath, "%s:", xprefix);
cprintf(xpath, "%s", name);
break;
}
if (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){
free(prefix);
prefix = NULL;
Expand Down Expand Up @@ -910,7 +907,7 @@ api_path2xpath(char *api_path,
goto done;
if ((xpath = cbuf_new()) == NULL)
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;
if (ret == 0)
goto fail;
Expand Down Expand Up @@ -983,9 +980,9 @@ api_path2xml_vec(char **vec,
char *namespace = NULL;
cbuf *cberr = NULL;
char *val = NULL;
int ret;
char *xpath = NULL;
cvec *nsc = NULL;
int ymtpoint; /* y is mountpoint */

if ((nodeid = vec[0]) == NULL || strlen(nodeid)==0){
if (xbotp)
Expand All @@ -1006,14 +1003,20 @@ api_path2xml_vec(char **vec,
/* Split into prefix and localname */
if (nodeid_split(nodeid, &prefix, &name) < 0)
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){
cprintf(cberr, "api-path element '%s', expected prefix:name", nodeid);
if (xerr &&
netconf_invalid_value_xml(xerr, "application", cbuf_get(cberr)) < 0)
goto done;
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){
cprintf(cberr, "No such yang module prefix");
if (xerr &&
Expand All @@ -1026,7 +1029,7 @@ api_path2xml_vec(char **vec,
}
y = (nodeclass==YC_SCHEMANODE)?
yang_find_schemanode(y0, name):
yang_find_datanode(y0, name);
yang_find_datanode(y0, name); // <--
if (y == NULL){
if (xerr &&
netconf_unknown_element_xml(xerr, "application", name, "Unknown element") < 0)
Expand Down Expand Up @@ -1154,9 +1157,9 @@ api_path2xml_vec(char **vec,
goto done;
}
/* 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;
if (ret == 1){
if (ymtpoint){
y1 = NULL;
if (xml_nsctx_yangspec(ys_spec(y), &nsc) < 0)
goto done;
Expand Down
1 change: 1 addition & 0 deletions lib/src/clixon_yang_schema_mount.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ yang_schema_mount_point(yang_stmt *y)
* @param[in] xpath Key for yspec on y
* @param[out] yspec YANG stmt spec
* @retval 0 OK
* @see yang_mount_get_yspec_any for ANY yspec not for a specific xpath
*/
int
yang_mount_get(yang_stmt *y,
Expand Down

0 comments on commit 798a9fb

Please sign in to comment.