* RESTCONF "depth" query parameter supported

* C API change: Added `depth` parameter to function `clicon_xml2cbuf`, default is -1.
This commit is contained in:
Olof hagsand 2019-08-17 10:54:13 +02:00
parent 10a2dbe8ec
commit ee329ee382
31 changed files with 289 additions and 117 deletions

View file

@ -167,7 +167,7 @@ netconf_invalid_value(cbuf *cb,
if (netconf_invalid_value_xml(&xret, type, message) < 0)
goto done;
if (clicon_xml2cbuf(cb, xret, 0, 0) < 0)
if (clicon_xml2cbuf(cb, xret, 0, 0, -1) < 0)
goto done;
retval = 0;
done:
@ -276,7 +276,7 @@ netconf_bad_attribute(cbuf *cb,
if (netconf_bad_attribute_xml(&xret, type, info, message) < 0)
goto done;
if (clicon_xml2cbuf(cb, xret, 0, 0) < 0)
if (clicon_xml2cbuf(cb, xret, 0, 0, -1) < 0)
goto done;
retval = 0;
done:
@ -441,7 +441,7 @@ netconf_missing_element(cbuf *cb,
if (netconf_common_xml(&xret, type, "missing-element",
"bad-element", element, message) < 0)
goto done;
if (clicon_xml2cbuf(cb, xret, 0, 0) < 0)
if (clicon_xml2cbuf(cb, xret, 0, 0, -1) < 0)
goto done;
retval = 0;
done:
@ -487,7 +487,7 @@ netconf_bad_element(cbuf *cb,
if (netconf_common_xml(&xret, type, "bad-element",
"bad-element",element, message) < 0)
goto done;
if (clicon_xml2cbuf(cb, xret, 0, 0) < 0)
if (clicon_xml2cbuf(cb, xret, 0, 0, -1) < 0)
goto done;
retval = 0;
done:
@ -525,7 +525,7 @@ netconf_unknown_element(cbuf *cb,
if (netconf_common_xml(&xret, type, "unknown-element",
"bad-element", element, message) < 0)
goto done;
if (clicon_xml2cbuf(cb, xret, 0, 0) < 0)
if (clicon_xml2cbuf(cb, xret, 0, 0, -1) < 0)
goto done;
retval = 0;
done:
@ -572,7 +572,7 @@ netconf_unknown_namespace(cbuf *cb,
if (netconf_common_xml(&xret, type, "unknown-namespace",
"bad-namespace", namespace, message) < 0)
goto done;
if (clicon_xml2cbuf(cb, xret, 0, 0) < 0)
if (clicon_xml2cbuf(cb, xret, 0, 0, -1) < 0)
goto done;
retval = 0;
done:
@ -610,7 +610,7 @@ netconf_access_denied(cbuf *cb,
if (netconf_access_denied_xml(&xret, type, message) < 0)
goto done;
if (clicon_xml2cbuf(cb, xret, 0, 0) < 0)
if (clicon_xml2cbuf(cb, xret, 0, 0, -1) < 0)
goto done;
retval = 0;
done:
@ -846,7 +846,7 @@ netconf_data_missing(cbuf *cb,
if (netconf_data_missing_xml(&xret, missing_choice, message) < 0)
goto done;
if (clicon_xml2cbuf(cb, xret, 0, 0) < 0)
if (clicon_xml2cbuf(cb, xret, 0, 0, -1) < 0)
goto done;
retval = 0;
done:
@ -967,7 +967,7 @@ netconf_operation_failed(cbuf *cb,
if (netconf_operation_failed_xml(&xret, type, message) < 0)
goto done;
if (clicon_xml2cbuf(cb, xret, 0, 0) < 0)
if (clicon_xml2cbuf(cb, xret, 0, 0, -1) < 0)
goto done;
retval = 0;
done:
@ -1045,7 +1045,7 @@ netconf_malformed_message(cbuf *cb,
if (netconf_malformed_message_xml(&xret, message) < 0)
goto done;
if (clicon_xml2cbuf(cb, xret, 0, 0) < 0)
if (clicon_xml2cbuf(cb, xret, 0, 0, -1) < 0)
goto done;
retval = 0;
done:
@ -1148,7 +1148,7 @@ netconf_data_not_unique_xml(cxobj **xret,
while ((cvi = cvec_each(cvk, cvi)) != NULL){
if ((xi = xml_find(x, cv_string_get(cvi))) == NULL)
continue; /* ignore, shouldnt happen */
clicon_xml2cbuf(cb, xi, 0, 0);
clicon_xml2cbuf(cb, xi, 0, 0, -1);
if (xml_parse_va(&xinfo, NULL, "<non-unique>%s</non-unique>", cbuf_get(cb)) < 0)
goto done;
cbuf_reset(cb);
@ -1369,7 +1369,7 @@ netconf_err2cb(cxobj *xerr,
if ((x=xpath_first(xerr, "error-message"))!=NULL)
cprintf(cb, "%s ", xml_body(x));
if ((x=xpath_first(xerr, "error-info"))!=NULL)
clicon_xml2cbuf(cb, xml_child_i(x,0), 0, 0);
clicon_xml2cbuf(cb, xml_child_i(x,0), 0, 0, -1);
*cberr = cb;
retval = 0;
done:

View file

@ -606,7 +606,7 @@ send_msg_notify_xml(int s,
clicon_err(OE_PLUGIN, errno, "cbuf_new");
goto done;
}
if (clicon_xml2cbuf(cb, xev, 0, 0) < 0)
if (clicon_xml2cbuf(cb, xev, 0, 0, -1) < 0)
goto done;
if (send_msg_notify(s, cbuf_get(cb)) < 0)
goto done;

View file

@ -213,7 +213,7 @@ clicon_rpc_netconf_xml(clicon_handle h,
clicon_err(OE_XML, errno, "cbuf_new");
goto done;
}
if (clicon_xml2cbuf(cb, xml, 0, 0) < 0)
if (clicon_xml2cbuf(cb, xml, 0, 0, -1) < 0)
goto done;
if (clicon_rpc_netconf(h, cbuf_get(cb), xret, sp) < 0)
goto done;
@ -541,7 +541,8 @@ clicon_rpc_unlock(clicon_handle h,
* @param[in] h Clicon handle
* @param[in] xpath XPath in a filter stmt (or NULL/"" for no filter)
* @param[in] namespace Namespace associated w xpath
* @param[in] content CLixon extension: all, config, noconfig. -1 means all
* @param[in] content Clixon extension: all, config, noconfig. -1 means all
* @param[in] depth Nr of XML levels to get, -1 is all, 0 is none
* @param[out] xt XML tree. Free with xml_free.
* Either <config> or <rpc-error>.
* @retval 0 OK
@ -550,7 +551,7 @@ clicon_rpc_unlock(clicon_handle h,
* namespace will be used which is most probably wrong.
* @code
* cxobj *xt = NULL;
* if (clicon_rpc_get(h, "/hello/world", "urn:example:hello", CONTENT_ALL, &xt) < 0)
* if (clicon_rpc_get(h, "/hello/world", "urn:example:hello", CONTENT_ALL, -1, &xt) < 0)
* err;
* if ((xerr = xpath_first(xt, "/rpc-error")) != NULL){
* clicon_rpc_generate_error(xerr);
@ -563,11 +564,12 @@ clicon_rpc_unlock(clicon_handle h,
* @see clicon_rpc_generate_error
*/
int
clicon_rpc_get(clicon_handle h,
char *xpath,
char *namespace,
clicon_rpc_get(clicon_handle h,
char *xpath,
char *namespace,
netconf_content content,
cxobj **xt)
int32_t depth,
cxobj **xt)
{
int retval = -1;
struct clicon_msg *msg = NULL;
@ -584,11 +586,12 @@ clicon_rpc_get(clicon_handle h,
if (namespace)
cprintf(cb, " xmlns:nc=\"%s\"", NETCONF_BASE_NAMESPACE);
cprintf(cb, "><get");
#if 1
/* Clixon extension, content all, config, nonconfig */
/* Clixon extension, content=all,config, or nonconfig */
if (content != -1)
cprintf(cb, " content=\"%s\"", netconf_content_int2str(content));
#endif
/* Clixon extension, depth=<level> */
if (depth != -1)
cprintf(cb, " depth=\"%d\"", depth);
cprintf(cb, ">");
if (xpath && strlen(xpath)) {
if (namespace)

View file

@ -984,7 +984,7 @@ stream_publish_cb(clicon_handle h,
clicon_err(OE_XML, errno, "cbuf_new");
goto done;
}
if (clicon_xml2cbuf(d, event, 0, 0) < 0)
if (clicon_xml2cbuf(d, event, 0, 0, -1) < 0)
goto done;
if (url_post(cbuf_get(u), /* url+stream */
cbuf_get(d), /* postfields */

View file

@ -1651,13 +1651,14 @@ xml_print(FILE *f,
*
* @param[in,out] cb Cligen buffer to write to
* @param[in] xn Clicon xml tree
* @param[in] level Indentation level
* @param[in] level Indentation level for prettyprint
* @param[in] prettyprint insert \n and spaces tomake the xml more readable.
* @param[in] depth Limit levels of child resources: -1 is all, 0 is none, 1 is node itself
*
* @code
* cbuf *cb;
* cb = cbuf_new();
* if (clicon_xml2cbuf(cb, xn, 0, 1) < 0)
* if (clicon_xml2cbuf(cb, xn, 0, 1, -1) < 0)
* goto err;
* fprintf(stderr, "%s", cbuf_get(cb));
* cbuf_free(cb);
@ -1665,10 +1666,11 @@ xml_print(FILE *f,
* @see clicon_xml2file
*/
int
clicon_xml2cbuf(cbuf *cb,
cxobj *x,
int level,
int prettyprint)
clicon_xml2cbuf(cbuf *cb,
cxobj *x,
int level,
int prettyprint,
int32_t depth)
{
int retval = -1;
cxobj *xc;
@ -1679,6 +1681,8 @@ clicon_xml2cbuf(cbuf *cb,
char *encstr = NULL; /* xml encoded string */
char *val;
if (depth == 0)
goto ok;
name = xml_name(x);
namespace = xml_prefix(x);
switch(xml_type(x)){
@ -1707,7 +1711,7 @@ clicon_xml2cbuf(cbuf *cb,
while ((xc = xml_child_each(x, xc, -1)) != NULL)
switch (xc->x_type){
case CX_ATTR:
if (clicon_xml2cbuf(cb, xc, level+1, prettyprint) < 0)
if (clicon_xml2cbuf(cb, xc, level+1, prettyprint, -1) < 0)
goto done;
break;
case CX_BODY:
@ -1729,7 +1733,7 @@ clicon_xml2cbuf(cbuf *cb,
xc = NULL;
while ((xc = xml_child_each(x, xc, -1)) != NULL)
if (xml_type(xc) != CX_ATTR)
if (clicon_xml2cbuf(cb, xc, level+1, prettyprint) < 0)
if (clicon_xml2cbuf(cb, xc, level+1, prettyprint, depth-1) < 0)
goto done;
if (prettyprint && hasbody == 0)
cprintf(cb, "%*s", level*XML_INDENT, "");
@ -1744,6 +1748,7 @@ clicon_xml2cbuf(cbuf *cb,
default:
break;
}/* switch */
ok:
retval = 0;
done:
if (encstr)
@ -2522,7 +2527,7 @@ clicon_log_xml(int level,
/* Print xml as cbuf */
if ((cb = cbuf_new()) == NULL)
goto done;
if (clicon_xml2cbuf(cb, x, 0, 0) < 0)
if (clicon_xml2cbuf(cb, x, 0, 0, -1) < 0)
goto done;
/* first round: compute length of debug message */

View file

@ -148,17 +148,40 @@ xpath_tree_print0(cbuf *cb,
return 0;
}
/*! Print a xpath_tree
/*! Print a xpath_tree to CLIgen buf
* @param[out] cb CLIgen buffer
* @param[in] xs XPATH tree
*/
int
xpath_tree_print(cbuf *cb,
xpath_tree *xs)
xpath_tree_print_cb(cbuf *cb,
xpath_tree *xs)
{
xpath_tree_print0(cb, xs, 0);
return 0;
}
/*! Print a xpath_tree
* @param[in] f UNIX output stream
* @param[in] xs XPATH tree
*/
int
xpath_tree_print(FILE *f,
xpath_tree *xs)
{
int retval = -1;
cbuf *cb = NULL;
if ((cb = cbuf_new()) == NULL){
clicon_err(OE_UNIX, errno, "cbuf_new");
goto done;
}
if (xpath_tree_print0(cb, xs, 0) < 0)
goto done;
fprintf(f, "%s", cbuf_get(cb));
retval = 0;
done:
return retval;
}
/*! Free a xpath_tree
* @param[in] xs XPATH tree
@ -203,7 +226,8 @@ xpath_parse(cvec *nsc,
{
int retval = -1;
struct clicon_xpath_yacc_arg xy = {0,};
cbuf *cb = NULL;
xy.xy_parse_string = xpath;
xy.xy_name = "xpath parser";
xy.xy_linenum = 1;
@ -219,17 +243,20 @@ xpath_parse(cvec *nsc,
goto done;
}
if (debug > 1){
cbuf *cb = cbuf_new();
xpath_tree_print(cb, xy.xy_top);
if ((cb = cbuf_new()) == NULL){
clicon_err(OE_XML, errno, "cbuf_new");
goto done;
}
xpath_tree_print_cb(cb, xy.xy_top);
clicon_debug(2, "xpath parse tree:\n%s", cbuf_get(cb));
cbuf_free(cb);
}
/* done: */
xpath_parse_exit(&xy);
xpath_scan_exit(&xy);
*xptree = xy.xy_top;
retval = 0;
done:
if (cb)
cbuf_free(cb);
return retval;
}