- New netconf-specific uint32 parse functions

- Added failure handling to xpath traverse_canonical
- Started pagination cli code
This commit is contained in:
Olof hagsand 2021-08-30 14:43:24 +02:00
parent 390b0886ed
commit a046306270
16 changed files with 410 additions and 241 deletions

View file

@ -732,7 +732,7 @@ from_client_kill_session(clicon_handle h,
{
int retval = -1;
uint32_t id; /* session id */
char *str;
char *str = NULL;
struct client_entry *ce;
char *db = "running"; /* XXX */
cxobj *x;
@ -745,15 +745,10 @@ from_client_kill_session(clicon_handle h,
goto done;
goto ok;
}
if ((ret = parse_uint32(str, &id, &reason)) < 0){
clicon_err(OE_XML, errno, "parse_uint32");
goto done;
}
if (ret == 0){
if (netconf_bad_element(cbret, "protocol", "session-id", reason) < 0)
goto done;
goto done;
}
if ((ret = netconf_parse_uint32("session-id", str, NULL, 0, cbret, &id)) < 0)
goto done;
if (ret == 0)
goto ok;
/* may or may not be in active client list, probably not */
if ((ce = ce_find_byid(backend_client_list(h), id)) != NULL){
xmldb_unlock_all(h, id); /* Removes locks on all databases */

View file

@ -273,17 +273,18 @@ client_statedata(clicon_handle h,
}
#ifdef LIST_PAGINATION
/*! Help function for parsing restconf query parameter and setting netconf attribute
*
* If not "unbounded", parse and set a numeric value
* @param[in] h Clixon handle
* @param[in] name Name of attribute
* @param[in] defaultstr Default string which is accepted and sets value to 0
* @param[in,out] cbret Output buffer for internal RPC message
* @param[out] value Value
* @retval -1 Error
* @retval 0 Invalid, cbret set
* @retval 1 OK
* @param[in] h Clixon handle
* @param[in] name Name of attribute
* @param[in] defaultstr Default string which is accepted and sets value to 0
* @param[in,out] cbret Output buffer for internal RPC message if invalid
* @param[out] value Value
* @retval -1 Error
* @retval 0 Invalid, cbret set
* @retval 1 OK
*/
static int
element2value(clicon_handle h,
@ -293,35 +294,15 @@ element2value(clicon_handle h,
cbuf *cbret,
uint32_t *value)
{
int retval = -1;
char *valstr;
int ret;
char *reason = NULL;
cxobj *x;
*value = 0;
if ((x = xml_find_type(xe, NULL, name, CX_ELMNT)) != NULL &&
(valstr = xml_body(x)) != NULL &&
strcmp(valstr, defaultstr) != 0){
if ((ret = parse_uint32(valstr, value, &reason)) < 0){
clicon_err(OE_XML, errno, "parse_uint32");
goto done;
}
if (ret == 0){
if (netconf_bad_element(cbret, "application",
name, "Unrecognized value") < 0)
goto done;
goto fail;
}
(valstr = xml_body(x)) != NULL){
return netconf_parse_uint32(name, valstr, defaultstr, 0, cbret, value);
}
retval = 1;
done:
if (reason)
free(reason);
return retval;
fail:
retval = 0;
goto done;
return 1;
}
#endif
@ -368,8 +349,8 @@ get_common(clicon_handle h,
char *reason = NULL;
cbuf *cbmsg = NULL; /* For error msg */
#ifdef LIST_PAGINATION
uint32_t limit = 0;
uint32_t offset = 0;
uint32_t limit = 0;
uint32_t total = 0;
uint32_t remaining = 0;
char *direction = NULL;
@ -393,6 +374,8 @@ get_common(clicon_handle h,
if ((xfilter = xml_find(xe, "filter")) != NULL){
char *xpath0;
cvec *nsc1 = NULL;
cbuf *cbreason = NULL;
if ((xpath0 = xml_find_value(xfilter, "select"))==NULL)
xpath0 = "/";
/* Create namespace context for xpath from <filter>
@ -402,8 +385,16 @@ get_common(clicon_handle h,
else
if (xml_nsctx_node(xfilter, &nsc) < 0)
goto done;
if (xpath2canonical(xpath0, nsc, yspec, &xpath, &nsc1) < 0)
if ((ret = xpath2canonical(xpath0, nsc, yspec, &xpath, &nsc1, &cbreason)) < 0)
goto done;
if (ret == 0){
if (netconf_bad_attribute(cbret, "application",
"select", cbuf_get(cbreason)) < 0)
goto done;
goto ok;
}
if (cbreason)
cbuf_free(cbreason);
if (nsc)
xml_nsctx_free(nsc);
nsc = nsc1;
@ -435,36 +426,42 @@ get_common(clicon_handle h,
if (yang_path_arg(yspec, xpath, &ylist) < 0)
goto done;
if (ylist == NULL){
if (netconf_invalid_value_xml(&xerr, "application", "list-pagination is enabled but target is not found") < 0)
if ((cbmsg = cbuf_new()) == NULL){
clicon_err(OE_UNIX, errno, "cbuf_new");
goto done;
}
/* error reason should be in clicon_err_reason */
cprintf(cbmsg, "Netconf get list-pagination: \"%s\" not found", xpath);
if (netconf_invalid_value(cbret, "application", cbuf_get(cbmsg)) < 0)
goto done;
goto ok;
}
if (yang_keyword_get(ylist) != Y_LIST &&
yang_keyword_get(ylist) != Y_LEAF_LIST){
if (netconf_invalid_value_xml(&xerr, "application", "list-pagination is enabled but target is not leaf or leaf-list") < 0)
if (netconf_invalid_value(cbret, "application", "list-pagination is enabled but target is not leaf or leaf-list") < 0)
goto done;
goto ok;
}
if ((list_config = yang_config_ancestor(ylist)) != 0){ /* config list */
if (content == CONTENT_NONCONFIG){
if (netconf_invalid_value_xml(&xerr, "application", "list-pagination targets a config list but content request is nonconfig") < 0)
if (netconf_invalid_value(cbret, "application", "list-pagination targets a config list but content request is nonconfig") < 0)
goto done;
goto ok;
}
}
else { /* state list */
if (content == CONTENT_CONFIG){
if (netconf_invalid_value_xml(&xerr, "application", "list-pagination targets a state list but content request is config") < 0)
if (netconf_invalid_value(cbret, "application", "list-pagination targets a state list but content request is config") < 0)
goto done;
goto ok;
}
}
/* limit */
if ((ret = element2value(h, xe, "limit", "unbounded", cbret, &limit)) < 0)
goto done;
/* offset */
if (ret && (ret = element2value(h, xe, "offset", "none", cbret, &offset)) < 0)
goto done;
/* limit */
if ((ret = element2value(h, xe, "limit", "unbounded", cbret, &limit)) < 0)
goto done;
/* direction */
if (ret && (x = xml_find_type(xe, NULL, "direction", CX_ELMNT)) != NULL){
direction = xml_body(x);
@ -517,7 +514,11 @@ get_common(clicon_handle h,
}
}
else{
/* XXX remaining of state list??*/
/* Remaining of state list. Two strategies:
* 1. New api where state callback is registered, lock, iterative, unlock
* 2. Read all here and count (fallback)
*/
}
/* Append predicate to original xpath and replace it */
xpath2 = cbuf_get(cbpath);
@ -525,9 +526,7 @@ get_common(clicon_handle h,
else
xpath2 = xpath;
#endif /* LIST_PAGINATION */
/* Read config
* XXX This seems unnecessary complex
*/
/* Read configuration */
switch (content){
case CONTENT_CONFIG: /* config data only */
/* specific xpath */

View file

@ -1317,28 +1317,38 @@ cli_help(clicon_handle h, cvec *vars, cvec *argv)
* XXX: This is hardcoded only for test_pagination.sh
* @param[in] h Clicon handle
* @param[in] cvv Vector of cli string and instantiated variables
* @param[in] argv Vector. Format: <xpath> <prefix> <namespace>
* @param[in] argv Vector. Format: <xpath> <prefix> <namespace> <format>
* Also, if there is a cligen variable called "xpath" it will override argv xpath arg
*/
int
cli_pagination(clicon_handle h, cvec *vars, cvec *argv)
cli_pagination(clicon_handle h,
cvec *cvv,
cvec *argv)
{
int retval = -1;
cbuf *cb = NULL;
char *xpath = NULL;
char *prefix = NULL;
char *namespace = NULL;
cxobj *xret = NULL;
cxobj *xerr;
cvec *nsc = NULL;
char *formatstr;
int retval = -1;
cbuf *cb = NULL;
char *xpath = NULL;
char *prefix = NULL;
char *namespace = NULL;
cxobj *xret = NULL;
cxobj *xerr;
cvec *nsc = NULL;
char *formatstr;
enum format_enum format;
cxobj *xc;
cxobj *xc;
cg_var *cv;
int i;
const int win = 20;
if (cvec_len(argv) != 4){
clicon_err(OE_PLUGIN, 0, "Expected usage: <xpath> <prefix> <namespace> <format>");
goto done;
}
xpath = cvec_i_str(argv, 0);
/* prefix:variable overrides argv */
if ((cv = cvec_find(cvv, "xpath")) != NULL)
xpath = cv_string_get(cv);
else
xpath = cvec_i_str(argv, 0);
prefix = cvec_i_str(argv, 1);
namespace = cvec_i_str(argv, 2);
formatstr = cv_string_get(cvec_i(argv, 3)); /* Fourthformat: output format */
@ -1348,34 +1358,39 @@ cli_pagination(clicon_handle h, cvec *vars, cvec *argv)
}
if ((nsc = xml_nsctx_init(prefix, namespace)) == NULL)
goto done;
if (clicon_rpc_get_pageable_list(h, "running", xpath, NULL, nsc, CONTENT_CONFIG, -1,
"2", "0",
NULL, NULL, NULL,
&xret) < 0){
goto done;
}
if ((xerr = xpath_first(xret, NULL, "/rpc-error")) != NULL){
clixon_netconf_error(xerr, "Get configuration", NULL);
goto done;
}
xc = NULL;
while ((xc = xml_child_each(xret, xc, CX_ELMNT)) != NULL)
switch (format){
case FORMAT_XML:
clicon_xml2file_cb(stdout, xc, 0, 1, cligen_output);
break;
case FORMAT_JSON:
xml2json_cb(stdout, xc, 1, cligen_output);
break;
case FORMAT_TEXT:
xml2txt_cb(stdout, xc, cligen_output); /* tree-formed text */
break;
case FORMAT_CLI:
xml2cli_cb(stdout, xc, NULL, GT_HIDE, cligen_output); /* cli syntax */
break;
case FORMAT_NETCONF:
break;
for (i = 0; i < 10; i++){
if (clicon_rpc_get_pageable_list(h, "running", xpath, nsc,
CONTENT_NONCONFIG,
-1, /* depth */
win*i, /* offset */
win*(i+1), /* limit */
NULL, NULL, NULL, /* nyi */
&xret) < 0){
goto done;
}
if ((xerr = xpath_first(xret, NULL, "/rpc-error")) != NULL){
clixon_netconf_error(xerr, "Get configuration", NULL);
goto done;
}
xc = NULL;
while ((xc = xml_child_each(xret, xc, CX_ELMNT)) != NULL)
switch (format){
case FORMAT_XML:
clicon_xml2file_cb(stdout, xc, 0, 1, cligen_output);
break;
case FORMAT_JSON:
xml2json_cb(stdout, xc, 1, cligen_output);
break;
case FORMAT_TEXT:
xml2txt_cb(stdout, xc, cligen_output); /* tree-formed text */
break;
case FORMAT_CLI:
xml2cli_cb(stdout, xc, NULL, GT_HIDE, cligen_output); /* cli syntax */
break;
case FORMAT_NETCONF:
break;
}
}
retval = 0;
done:
if (xret)
@ -1386,7 +1401,7 @@ cli_pagination(clicon_handle h, cvec *vars, cvec *argv)
}
#else
int
cli_pagination(clicon_handle h, cvec *vars, cvec *argv)
cli_pagination(clicon_handle h, cvec *cvv, cvec *argv)
{
fprintf(stderr, "Not yet implemented\n");
return 0;

View file

@ -207,7 +207,6 @@ expand_dbvar(void *h,
cxobj *xbot = NULL; /* xpath, NULL if datastore */
yang_stmt *y = NULL; /* yang spec of xpath */
yang_stmt *yp;
char *reason = NULL;
cvec *nsc = NULL;
int ret;
int cvvi = 0;
@ -368,8 +367,6 @@ expand_dbvar(void *h,
xml_free(xerr);
if (nsc)
xml_nsctx_free(nsc);
if (reason)
free(reason);
if (api_path)
free(api_path);
if (xvec)

View file

@ -351,8 +351,8 @@ api_data_collection(clicon_handle h,
yang_stmt *y = NULL;
cbuf *cbrpc = NULL;
int32_t depth = -1; /* Nr of levels to print, -1 is all, 0 is none */
char *limit;
char *offset;
uint32_t limit = 0;
uint32_t offset = 0;
char *direction;
char *sort;
char *where;
@ -446,13 +446,29 @@ api_data_collection(clicon_handle h,
}
}
}
limit = cvec_find_str(qvec, "limit");
offset = cvec_find_str(qvec, "offset");
if ((attr = cvec_find_str(qvec, "limit")) != NULL){ /* 1-uint32 or "unbounded" */
if ((ret = netconf_parse_uint32_xml("limit", attr, "unbounded", 0, &xerr, &limit)) < 0)
goto done;
if (ret == 0){
if (api_return_err0(h, req, xerr, pretty, media_out, 0) < 0)
goto done;
goto ok;
}
}
if ((attr = cvec_find_str(qvec, "offset")) != NULL){ /* 1-uint32 or "none" */
if ((ret = netconf_parse_uint32_xml("offset", attr, "none", 0, &xerr, &offset)) < 0)
goto done;
if (ret == 0){
if (api_return_err0(h, req, xerr, pretty, media_out, 0) < 0)
goto done;
goto ok;
}
}
direction = cvec_find_str(qvec, "direction");
sort = cvec_find_str(qvec, "sort");
where = cvec_find_str(qvec, "where");
if (clicon_rpc_get_pageable_list(h, "running", xpath, y, nsc, content,
depth, limit, offset, direction, sort, where,
if (clicon_rpc_get_pageable_list(h, "running", xpath, nsc, content,
depth, offset, limit, direction, sort, where,
&xret) < 0){
if (netconf_operation_failed_xml(&xerr, "protocol", clicon_err_reason) < 0)
goto done;
@ -483,23 +499,12 @@ api_data_collection(clicon_handle h,
goto done;
if (xpath_vec(xret, nsc, "%s", &xvec, &xlen, xpath) < 0)
goto done;
#if 0
/* Check if not exists */
if (xlen == 0){
/* 4.3: If a retrieval request for a data resource represents an
instance that does not exist, then an error response containing
a "404 Not Found" status-line MUST be returned by the server.
The error-tag value "invalid-value" is used in this case. */
if (netconf_invalid_value_xml(&xerr, "application", "Instance does not exist") < 0)
goto done;
/* override invalid-value default 400 with 404 */
if ((xe = xpath_first(xerr, NULL, "rpc-error")) != NULL){
if (api_return_err(h, req, xe, pretty, media_out, 404) < 0)
goto done;
}
goto ok;
}
#endif
/* Note, the netconf GET pageable list can not distinguish between:
* - not-exists, ie there are no entries
* - no matching entries, eg there are entries, just not that match
* Here we take the latter approach to return an empty list and do not
* handle the non-exist case differently.
*/
for (i=0; i<xlen; i++){
xp = xvec[i];
ns = NULL;