Changed C-API for xml translation/print to other formats.

* Added `skiptop` parameter, if set only apply to children of a node, skip top node
    * default is 0
* Functions are merged, ie removed and replaced with more generic functions
* `xml2json_cbuf()`: Added `skiptop` parameter: `xml2json_cbuf(..., int skiptop)`
* `xml2json()` and `xml2json_cb()` merged into `xml2json_file()` with `skiptop`
    * Replace `xml2json(...)` with `xml2json_file(..., stdout, 0)`
    * Replace `xml2json_cb(...)` with `xml2json_file(..., 0)`
* `clicon_xml2cbuf()`: Added `skiptop` parameter: `clicon_xml2cbuf(..., int skiptop)`
* `xml2cli()`: Added `skiptop` parameter: `xml2cli(..., int skiptop)`
This commit is contained in:
Olof hagsand 2022-06-01 10:48:39 +02:00
parent 724b94137f
commit 43a57dad79
39 changed files with 306 additions and 481 deletions

View file

@ -279,13 +279,13 @@ api_return_err(clicon_handle h,
clicon_debug(1, "%s code:%d", __FUNCTION__, code);
if (pretty){
cprintf(cb, " <errors xmlns=\"urn:ietf:params:xml:ns:yang:ietf-restconf\">\n");
if (clicon_xml2cbuf(cb, xerr, 2, pretty, -1) < 0)
if (clicon_xml2cbuf(cb, xerr, 2, pretty, -1, 0) < 0)
goto done;
cprintf(cb, " </errors>\r\n");
}
else {
cprintf(cb, "<errors xmlns=\"urn:ietf:params:xml:ns:yang:ietf-restconf\">");
if (clicon_xml2cbuf(cb, xerr, 2, pretty, -1) < 0)
if (clicon_xml2cbuf(cb, xerr, 2, pretty, -1, 0) < 0)
goto done;
cprintf(cb, "</errors>\r\n");
}
@ -295,14 +295,14 @@ api_return_err(clicon_handle h,
clicon_debug(1, "%s code:%d", __FUNCTION__, code);
if (pretty){
cprintf(cb, "{\n\"ietf-restconf:errors\" : ");
if (xml2json_cbuf(cb, xerr, pretty) < 0)
if (xml2json_cbuf(cb, xerr, pretty, 0) < 0)
goto done;
cprintf(cb, "\n}\r\n");
}
else{
cprintf(cb, "{");
cprintf(cb, "\"ietf-restconf:errors\":");
if (xml2json_cbuf(cb, xerr, pretty) < 0)
if (xml2json_cbuf(cb, xerr, pretty, 0) < 0)
goto done;
cprintf(cb, "}\r\n");
}

View file

@ -537,7 +537,7 @@ api_data_write(clicon_handle h,
cprintf(cbx, " autocommit=\"true\"");
cprintf(cbx, "><target><candidate /></target>");
cprintf(cbx, "<default-operation>none</default-operation>");
if (clicon_xml2cbuf(cbx, xtop, 0, 0, -1) < 0)
if (clicon_xml2cbuf(cbx, xtop, 0, 0, -1, 0) < 0)
goto done;
cprintf(cbx, "</edit-config></rpc>");
clicon_debug(1, "%s xml: %s api_path:%s",__FUNCTION__, cbuf_get(cbx), api_path);
@ -782,7 +782,7 @@ api_data_delete(clicon_handle h,
cprintf(cbx, " autocommit=\"true\"");
cprintf(cbx, "><target><candidate /></target>");
cprintf(cbx, "<default-operation>none</default-operation>");
if (clicon_xml2cbuf(cbx, xtop, 0, 0, -1) < 0)
if (clicon_xml2cbuf(cbx, xtop, 0, 0, -1, 0) < 0)
goto done;
cprintf(cbx, "</edit-config></rpc>");
if (clicon_rpc_netconf(h, cbuf_get(cbx), &xret, NULL) < 0)

View file

@ -225,11 +225,11 @@ api_data_get2(clicon_handle h,
if (xpath==NULL || strcmp(xpath,"/")==0){ /* Special case: data root */
switch (media_out){
case YANG_DATA_XML:
if (clicon_xml2cbuf(cbx, xret, 0, pretty, -1) < 0) /* Dont print top object? */
if (clicon_xml2cbuf(cbx, xret, 0, pretty, -1, 0) < 0) /* Dont print top object? */
goto done;
break;
case YANG_DATA_JSON:
if (xml2json_cbuf(cbx, xret, pretty) < 0)
if (xml2json_cbuf(cbx, xret, pretty, 0) < 0)
goto done;
break;
default:
@ -270,7 +270,7 @@ api_data_get2(clicon_handle h,
if (namespace && xmlns_set(x, prefix, namespace) < 0)
goto done;
}
if (clicon_xml2cbuf(cbx, x, 0, pretty, -1) < 0) /* Dont print top object? */
if (clicon_xml2cbuf(cbx, x, 0, pretty, -1, 0) < 0) /* Dont print top object? */
goto done;
}
break;
@ -535,7 +535,7 @@ api_data_pagination(clicon_handle h,
if (xml_insert(xpr, xp, INS_LAST, NULL, NULL) < 0)
goto done;
}
if (clicon_xml2cbuf(cbx, xpr, 0, pretty, -1) < 0) /* Dont print top object? */
if (clicon_xml2cbuf(cbx, xpr, 0, pretty, -1, 0) < 0) /* Dont print top object? */
goto done;
break;
case YANG_DATA_JSON:

View file

@ -119,7 +119,7 @@ yang_patch_xml2json_modified_cbuf(cxobj *x_simple_patch)
if (json_simple_patch == NULL)
return NULL;
cb = cbuf_new();
xml2json_cbuf(cb, x_simple_patch, 1);
xml2json_cbuf(cb, x_simple_patch, 0);
// Insert a '[' after the first '{' to get the JSON to match what api_data_post/write() expect
json_simple_patch_tmp = cbuf_get(cb);
@ -266,7 +266,7 @@ yang_patch_do_replace(clicon_handle h,
}
}
// Convert the data to json
xml2json_cbuf(json_simple_patch, x_simple_patch, 1);
xml2json_cbuf(json_simple_patch, x_simple_patch, 0);
// Send the POST request
if (api_data_post(h, req, cbuf_get(simple_patch_request_uri), pi, qvec, cbuf_get(json_simple_patch), pretty, YANG_DATA_JSON, media_out, ds ) < 0)
@ -327,7 +327,7 @@ yang_patch_do_create(clicon_handle h,
xml_addsub(x_simple_patch, value_vec_tmp);
}
}
if (xml2json_cbuf(cb, x_simple_patch, 1) < 0)
if (xml2json_cbuf(cb, x_simple_patch, 0) < 0)
goto done;
if (api_data_post(h, req, cbuf_get(simple_patch_request_uri),
pi, qvec,
@ -477,7 +477,7 @@ yang_patch_do_merge(clicon_handle h,
xml_addsub(x_simple_patch, value_vec_tmp);
}
cbuf_reset(cb); /* reuse cb */
xml2json_cbuf(cb, x_simple_patch, 1);
xml2json_cbuf(cb, x_simple_patch, 0);
if ((json_simple_patch = yang_patch_xml2json_modified_cbuf(x_simple_patch)) == NULL)
goto done;

View file

@ -360,7 +360,7 @@ api_data_post(clicon_handle h,
cprintf(cbx, " autocommit=\"true\"");
cprintf(cbx, "><target><candidate /></target>");
cprintf(cbx, "<default-operation>none</default-operation>");
if (clicon_xml2cbuf(cbx, xtop, 0, 0, -1) < 0)
if (clicon_xml2cbuf(cbx, xtop, 0, 0, -1, 0) < 0)
goto done;
cprintf(cbx, "</edit-config></rpc>");
clicon_debug(1, "%s xml: %s api_path:%s",__FUNCTION__, cbuf_get(cbx), api_path);
@ -874,12 +874,12 @@ api_operations_post(clicon_handle h,
cbuf_reset(cbret);
switch (media_out){
case YANG_DATA_XML:
if (clicon_xml2cbuf(cbret, xoutput, 0, pretty, -1) < 0)
if (clicon_xml2cbuf(cbret, xoutput, 0, pretty, -1, 0) < 0)
goto done;
/* xoutput should now look: <output xmlns="uri"><x>0</x></output> */
break;
case YANG_DATA_JSON:
if (xml2json_cbuf(cbret, xoutput, pretty) < 0)
if (xml2json_cbuf(cbret, xoutput, pretty, 0) < 0)
goto done;
/* xoutput should now look: {"example:output": {"x":0,"y":42}} */
break;

View file

@ -204,12 +204,12 @@ api_root_restconf_exact(clicon_handle h,
switch (media_out){
case YANG_DATA_XML:
case YANG_PATCH_XML:
if (clicon_xml2cbuf(cb, xt, 0, pretty, -1) < 0)
if (clicon_xml2cbuf(cb, xt, 0, pretty, -1, 0) < 0)
goto done;
break;
case YANG_DATA_JSON:
case YANG_PATCH_JSON:
if (xml2json_cbuf(cb, xt, pretty) < 0)
if (xml2json_cbuf(cb, xt, pretty, 0) < 0)
goto done;
break;
default:
@ -288,12 +288,12 @@ api_yang_library_version(clicon_handle h,
switch (media_out){
case YANG_DATA_XML:
case YANG_PATCH_XML:
if (clicon_xml2cbuf(cb, xt, 0, pretty, -1) < 0)
if (clicon_xml2cbuf(cb, xt, 0, pretty, -1, 0) < 0)
goto done;
break;
case YANG_DATA_JSON:
case YANG_PATCH_JSON:
if (xml2json_cbuf(cb, xt, pretty) < 0)
if (xml2json_cbuf(cb, xt, pretty, 0) < 0)
goto done;
break;
default:

View file

@ -254,7 +254,7 @@ restconf_stream_cb(int s,
FCGX_FPrintF(r->out, "M#id: %02d:0\r\n", tv.tv_sec);
}
#endif
if (clicon_xml2cbuf(cb, xn, 0, pretty, -1) < 0)
if (clicon_xml2cbuf(cb, xn, 0, pretty, -1, 0) < 0)
goto done;
FCGX_FPrintF(r->out, "data: %s\r\n", cbuf_get(cb));
FCGX_FPrintF(r->out, "\r\n");