Changed C-API for xml translation/print the internal cxobj tree data structure to other formats

New API is as follows:
  * `clixon_xml2file()` - Print internal tree as XML to file
  * `clixon_xml2cbuf()` - Print internal tree as XML to buffer
  * `clixon_json2file()` - Print internal tree as JSON to file
  * `clixon_json2cbuf()` - Print internal tree as JSON to buffer
  * `clixon_cli2file()` - Print internal tree as CLI format to file
  * `clixon_txt2file()` - Print internal tree as text format to file
This commit is contained in:
Olof hagsand 2022-06-01 20:02:27 +02:00
parent 87719c623c
commit 0c79298e76
49 changed files with 421 additions and 503 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) < 0)
if (clixon_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) < 0)
if (clixon_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) < 0)
if (clixon_json2cbuf(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) < 0)
if (clixon_json2cbuf(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) < 0)
if (clixon_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) < 0)
if (clixon_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) < 0) /* Dont print top object? */
if (clixon_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) < 0)
if (clixon_json2cbuf(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) < 0) /* Dont print top object? */
if (clixon_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) < 0) /* Dont print top object? */
if (clixon_xml2cbuf(cbx, xpr, 0, pretty, -1, 0) < 0) /* Dont print top object? */
goto done;
break;
case YANG_DATA_JSON:

View file

@ -100,9 +100,9 @@ yang_patch_op2int(char *op)
}
/*! Add square brackets after the surrounding curly brackets in JSON
* Needed, in order to modify the result of xml2json_cbuf() to be valid input
* Needed, in order to modify the result of clixon_json2cbuf() to be valid input
* to api_data_post() and api_data_write()
* @param[in] x_simple_patch a cxobj to pass to xml2json_cbuf()
* @param[in] x_simple_patch a cxobj to pass to clixon_json2cbuf()
* @retva cbuf With the modified json
* @retva NULL Error
*/
@ -119,7 +119,8 @@ 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, 0);
if (clixon_json2cbuf(cb, x_simple_patch, 0) < 0)
goto done;
// 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 +267,8 @@ yang_patch_do_replace(clicon_handle h,
}
}
// Convert the data to json
xml2json_cbuf(json_simple_patch, x_simple_patch, 0);
if (clixon_json2cbuf(json_simple_patch, x_simple_patch, 0) < 0)
goto done;
// 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 +329,7 @@ yang_patch_do_create(clicon_handle h,
xml_addsub(x_simple_patch, value_vec_tmp);
}
}
if (xml2json_cbuf(cb, x_simple_patch, 0) < 0)
if (clixon_json2cbuf(cb, x_simple_patch, 0) < 0)
goto done;
if (api_data_post(h, req, cbuf_get(simple_patch_request_uri),
pi, qvec,
@ -477,7 +479,8 @@ 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, 0);
if (clixon_json2cbuf(cb, x_simple_patch, 0) < 0)
goto done;
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) < 0)
if (clixon_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) < 0)
if (clixon_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) < 0)
if (clixon_json2cbuf(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) < 0)
if (clixon_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) < 0)
if (clixon_json2cbuf(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) < 0)
if (clixon_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) < 0)
if (clixon_json2cbuf(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) < 0)
if (clixon_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");