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

@ -325,7 +325,7 @@ cli_dbxml(clicon_handle h,
clicon_err(OE_XML, errno, "cbuf_new");
goto done;
}
if (clicon_xml2cbuf(cb, xtop, 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cb, xtop, 0, 0, -1, 0) < 0)
goto done;
if (clicon_rpc_edit_config(h, "candidate", OP_NONE, cbuf_get(cb)) < 0)
goto done;
@ -675,16 +675,15 @@ cli_validate(clicon_handle h,
/*! Compare two dbs using XML. Write to file and run diff
*/
static int
compare_xmls(cxobj *xc1,
cxobj *xc2,
int astext)
compare_xmls(cxobj *xc1,
cxobj *xc2,
enum format_enum format)
{
int fd;
FILE *f;
char filename1[MAXPATHLEN];
char filename2[MAXPATHLEN];
int retval = -1;
cxobj *xc;
cbuf *cb = NULL;
snprintf(filename1, sizeof(filename1), "/tmp/cliconXXXXXX");
@ -695,14 +694,17 @@ compare_xmls(cxobj *xc1,
}
if ((f = fdopen(fd, "w")) == NULL)
goto done;
xc = NULL;
if (astext)
while ((xc = xml_child_each(xc1, xc, -1)) != NULL)
xml2txt(xc, cligen_output, f, 0);
else
while ((xc = xml_child_each(xc1, xc, -1)) != NULL)
clicon_xml2file_cb(f, xc, 0, 1, cligen_output);
switch(format){
case FORMAT_TEXT:
if (clixon_txt2file(f, xc1, 0, cligen_output, 1) < 0)
goto done;
break;
case FORMAT_XML:
default:
if (clixon_xml2file(f, xc1, 0, 1, cligen_output, 1) < 0)
goto done;
break;
}
fclose(f);
close(fd);
@ -712,13 +714,19 @@ compare_xmls(cxobj *xc1,
}
if ((f = fdopen(fd, "w")) == NULL)
goto done;
xc = NULL;
if (astext)
while ((xc = xml_child_each(xc2, xc, -1)) != NULL)
xml2txt(xc, cligen_output, f, 0);
else
while ((xc = xml_child_each(xc2, xc, -1)) != NULL)
clicon_xml2file_cb(f, xc, 0, 1, cligen_output);
switch(format){
case FORMAT_TEXT:
if (clixon_txt2file(f, xc2, 0, cligen_output, 1) < 0)
goto done;
break;
case FORMAT_XML:
default:
if (clixon_xml2file(f, xc2, 0, 1, cligen_output, 1) < 0)
goto done;
break;
}
fclose(f);
close(fd);
@ -754,16 +762,16 @@ compare_dbs(clicon_handle h,
cxobj *xc2 = NULL; /* candidate xml */
cxobj *xerr = NULL;
int retval = -1;
int astext;
enum format_enum format;
if (cvec_len(argv) > 1){
clicon_err(OE_PLUGIN, EINVAL, "Requires 0 or 1 element. If given: astext flag 0|1");
goto done;
}
if (cvec_len(argv))
astext = cv_int32_get(cvec_i(argv, 0));
if (cvec_len(argv) && cv_int32_get(cvec_i(argv, 0)) == 1)
format = FORMAT_TEXT;
else
astext = 0;
format = FORMAT_XML;
if (clicon_rpc_get_config(h, NULL, "running", "/", NULL, &xc1) < 0)
goto done;
if ((xerr = xpath_first(xc1, NULL, "/rpc-error")) != NULL){
@ -776,7 +784,7 @@ compare_dbs(clicon_handle h,
clixon_netconf_error(xerr, "Get configuration", NULL);
goto done;
}
if (compare_xmls(xc1, xc2, astext) < 0) /* astext? */
if (compare_xmls(xc1, xc2, format) < 0) /* astext? */
goto done;
retval = 0;
done:
@ -934,7 +942,7 @@ load_config_file(clicon_handle h,
/* Read as datastore-top but transformed into an edit-config "config" */
xml_name_set(x, NETCONF_INPUT_CONFIG);
}
if (clicon_xml2cbuf(cbxml, xt, 0, 0, -1, 1) < 0)
if (clixon_xml2cbuf(cbxml, xt, 0, 0, -1, 1) < 0)
goto done;
if (clicon_rpc_edit_config(h, "candidate",
replace?OP_REPLACE:OP_MERGE,
@ -1034,26 +1042,26 @@ save_config_file(clicon_handle h,
}
switch (format){
case FORMAT_XML:
if (clicon_xml2file(f, xt, 0, pretty) < 0)
if (clixon_xml2file(f, xt, 0, pretty, fprintf, 0) < 0)
goto done;
break;
case FORMAT_JSON:
if (xml2json_file(f, xt, pretty, fprintf, 0) < 0)
if (clixon_json2file(f, xt, pretty, fprintf, 0) < 0)
goto done;
break;
case FORMAT_TEXT:
if (xml2txt(xt, fprintf, f, 0) < 0)
if (clixon_txt2file(f, xt, 0, fprintf, 0) < 0)
goto done;
break;
case FORMAT_CLI:
if (xml2cli(h, f, xt, prefix, fprintf, 1) < 0)
if (clixon_cli2file(h, f, xt, prefix, fprintf, 1) < 0)
goto done;
break;
case FORMAT_NETCONF:
fprintf(f, "<rpc xmlns=\"%s\" %s><edit-config><target><candidate/></target>",
NETCONF_BASE_NAMESPACE, NETCONF_MESSAGE_ID_ATTR);
fprintf(f, "\n");
if (clicon_xml2file(f, xt, 0, pretty) < 0)
if (clixon_xml2file(f, xt, 0, pretty, fprintf, 0) < 0)
goto done;
fprintf(f, "</edit-config></rpc>]]>]]>\n");
break;
@ -1135,8 +1143,6 @@ cli_notification_cb(int s,
int eof;
int retval = -1;
cxobj *xt = NULL;
cxobj *xe;
cxobj *x;
enum format_enum format = (enum format_enum)arg;
int ret;
@ -1159,30 +1165,19 @@ cli_notification_cb(int s,
}
switch (format){
case FORMAT_JSON:
if (xml2json_file(stdout, xt, 1, cligen_output, 1) < 0)
if (clixon_json2file(stdout, xt, 1, cligen_output, 1) < 0)
goto done;
case FORMAT_TEXT:
if (clixon_txt2file(stdout, xt, 0, cligen_output, 1) < 0)
goto done;
break;
case FORMAT_XML:
if (clixon_xml2file(stdout, xt, 0, 1, cligen_output, 1) < 0)
goto done;
break;
default:
break;
}
if ((xe = xpath_first(xt, NULL, "//event")) != NULL){
x = NULL;
while ((x = xml_child_each(xe, x, -1)) != NULL) {
switch (format){
case FORMAT_XML:
if (clicon_xml2file_cb(stdout, x, 0, 1, cligen_output) < 0)
goto done;
break;
case FORMAT_TEXT:
if (xml2txt(x, cligen_output, stdout, 0) < 0)
goto done;
break;
default:
break;
}
if (cli_output_status() < 0)
break;
}
}
retval = 0;
done:
if (xt)
@ -1424,7 +1419,8 @@ cli_copy_config(clicon_handle h,
/* resuse cb */
cbuf_reset(cb);
/* create xml copy tree and merge it with database configuration */
clicon_xml2cbuf(cb, x2, 0, 0, -1, 0);
if (clixon_xml2cbuf(cb, x2, 0, 0, -1, 0) < 0)
goto done;
if (clicon_rpc_edit_config(h, db, OP_MERGE, cbuf_get(cb)) < 0)
goto done;
retval = 0;