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

@ -651,12 +651,8 @@ cli_auto_show(clicon_handle h,
fprintf(stdout, "\n");
break;
case FORMAT_JSON:
if (isroot)
xml2json_cb(stdout, xp, pretty, cligen_output);
else{
while ((xc = xml_child_each(xp, xc, CX_ELMNT)) != NULL)
xml2json_cb(stdout, xc, pretty, cligen_output);
}
if (xml2json_file(stdout, xp, pretty, cligen_output, !isroot) < 0)
goto done;
fprintf(stdout, "\n");
break;
case FORMAT_TEXT:
@ -667,11 +663,8 @@ cli_auto_show(clicon_handle h,
cli_xml2txt(xc, cligen_output, 0); /* tree-formed text */
break;
case FORMAT_CLI:
if (isroot)
xml2cli(h, stdout, xp, prefix, cligen_output);
else
while ((xc = xml_child_each(xp, xc, CX_ELMNT)) != NULL)
xml2cli(h, stdout, xc, prefix, cligen_output);
if (xml2cli(h, stdout, xp, prefix, cligen_output, !isroot) < 0)
goto done;
break;
case FORMAT_NETCONF:
fprintf(stdout, "<rpc xmlns=\"%s\" %s><edit-config><target><candidate/></target><config>",

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)
if (clicon_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;
@ -925,9 +925,9 @@ load_config_file(clicon_handle h,
while ((x = xml_child_each(xt, x, -1)) != NULL) {
/* Read as datastore-top but transformed into an edit-config "config" */
xml_name_set(x, NETCONF_INPUT_CONFIG);
if (clicon_xml2cbuf(cbxml, x, 0, 0, -1) < 0)
goto done;
}
if (clicon_xml2cbuf(cbxml, xt, 0, 0, -1, 1) < 0)
goto done;
if (clicon_rpc_edit_config(h, "candidate",
replace?OP_REPLACE:OP_MERGE,
cbuf_get(cbxml)) < 0)
@ -973,7 +973,6 @@ save_config_file(clicon_handle h,
char *dbstr;
char *varstr;
cxobj *xt = NULL;
cxobj *x;
cxobj *xerr;
FILE *f = NULL;
char *formatstr;
@ -1031,7 +1030,7 @@ save_config_file(clicon_handle h,
goto done;
break;
case FORMAT_JSON:
if (xml2json(f, xt, pretty) < 0)
if (xml2json_file(f, xt, pretty, fprintf, 0) < 0)
goto done;
break;
case FORMAT_TEXT:
@ -1039,11 +1038,8 @@ save_config_file(clicon_handle h,
goto done;
break;
case FORMAT_CLI:
x = NULL;
while ((x = xml_child_each(xt, x, CX_ELMNT)) != NULL) {
if (xml2cli(h, f, x, prefix, fprintf) < 0)
goto done;
}
if (xml2cli(h, f, xt, prefix, fprintf, 1) < 0)
goto done;
break;
case FORMAT_NETCONF:
fprintf(f, "<rpc xmlns=\"%s\" %s><edit-config><target><candidate/></target>",
@ -1153,6 +1149,13 @@ cli_notification_cb(int s,
clicon_err(OE_NETCONF, EFAULT, "Notification malformed");
goto done;
}
switch (format){
case FORMAT_JSON:
if (xml2json_file(stdout, xt, 1, cligen_output, 1) < 0)
goto done;
default:
break;
}
if ((xe = xpath_first(xt, NULL, "//event")) != NULL){
x = NULL;
while ((x = xml_child_each(xe, x, -1)) != NULL) {
@ -1165,10 +1168,6 @@ cli_notification_cb(int s,
if (xml2txt_cb(stdout, x, cligen_output) < 0)
goto done;
break;
case FORMAT_JSON:
if (xml2json_cb(stdout, x, 1, cligen_output) < 0)
goto done;
break;
default:
break;
}
@ -1417,7 +1416,7 @@ 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);
clicon_xml2cbuf(cb, x2, 0, 0, -1, 0);
if (clicon_rpc_edit_config(h, db, OP_MERGE, cbuf_get(cb)) < 0)
goto done;
retval = 0;

View file

@ -500,7 +500,7 @@ cli_show_config1(clicon_handle h,
cli_xml2file(xc, 0, 1, cligen_output);
break;
case FORMAT_JSON:
xml2json_cb(stdout, xt, 1, cligen_output);
xml2json_file(stdout, xt, 1, cligen_output, 0);
break;
case FORMAT_TEXT:
xc = NULL; /* Dont print xt itself */
@ -508,10 +508,8 @@ cli_show_config1(clicon_handle h,
cli_xml2txt(xc, cligen_output, 0); /* tree-formed text */
break;
case FORMAT_CLI:
xc = NULL; /* Dont print xt itself */
while ((xc = xml_child_each(xt, xc, CX_ELMNT)) != NULL)
if (xml2cli(h, stdout, xc, prefix, cligen_output) < 0)
goto done;
if (xml2cli(h, stdout, xt, prefix, cligen_output, 1) < 0)
goto done;
break;
case FORMAT_NETCONF:
cligen_output(stdout, "<rpc xmlns=\"%s\" %s><edit-config><target><candidate/></target><config>\n",
@ -773,7 +771,7 @@ cli_show_generated(clicon_handle h,
switch (format){
case FORMAT_CLI:
if (xml2cli(h, stdout, xp, prefix, cligen_output) < 0) /* cli syntax */
if (xml2cli(h, stdout, xp, prefix, cligen_output, 0) < 0) /* cli syntax */
goto done;
break;
case FORMAT_NETCONF:
@ -781,15 +779,15 @@ cli_show_generated(clicon_handle h,
cli_xml2file(xp, 2, 1, fprintf);
fprintf(stdout, "</config></edit-config></rpc>]]>]]>\n");
break;
case FORMAT_JSON:
xml2json_file(stdout, xp_helper, 1, cligen_output, 1);
break;
default:
for (; i < xml_child_nr(xml_parent(xp)) ; ++i, xp_helper = xml_child_i(xml_parent(xp), i)) {
switch (format){
case FORMAT_XML:
cli_xml2file(xp_helper, 0, 1, fprintf);
break;
case FORMAT_JSON:
xml2json_cb(stdout, xp_helper, 1, cligen_output);
break;
case FORMAT_TEXT:
cli_xml2txt(xp_helper, cligen_output, 0); /* tree-formed text */
break;
@ -985,14 +983,14 @@ cli_pagination(clicon_handle h,
clicon_xml2file_cb(stdout, xc, 0, 1, cligen_output);
break;
case FORMAT_JSON:
xml2json_cb(stdout, xc, 1, cligen_output);
xml2json_file(stdout, xc, 1, cligen_output, 0);
break;
case FORMAT_TEXT:
xml2txt_cb(stdout, xc, cligen_output); /* tree-formed text */
break;
case FORMAT_CLI:
/* hardcoded to compress and list-keyword = nokey */
xml2cli(h, stdout, xc, NULL, cligen_output);
xml2cli(h, stdout, xc, NULL, cligen_output, 0);
break;
default:
break;
@ -1028,7 +1026,7 @@ cli_pagination(clicon_handle h,
return retval;
}
/*! Translate from XML to CLI commands
/*! Translate from XML to CLI commands, internal
*
* Howto: join strings and pass them down.
* Identify unique/index keywords for correct set syntax.
@ -1037,14 +1035,13 @@ cli_pagination(clicon_handle h,
* @param[in] xn XML Parse-tree (to translate)
* @param[in] prepend Print this text in front of all commands.
* @param[in] fn Callback to make print function
* @see xml2cli XXX should probably use the generic function
*/
int
xml2cli(clicon_handle h,
FILE *f,
cxobj *xn,
char *prepend,
clicon_output_cb *fn)
static int
xml2cli1(clicon_handle h,
FILE *f,
cxobj *xn,
char *prepend,
clicon_output_cb *fn)
{
int retval = -1;
cxobj *xe = NULL;
@ -1139,7 +1136,7 @@ xml2cli(clicon_handle h,
if (match)
continue; /* Not key itself */
}
if (xml2cli(h, f, xe, cbuf_get(cbpre), fn) < 0)
if (xml2cli1(h, f, xe, cbuf_get(cbpre), fn) < 0)
goto done;
}
ok:
@ -1149,3 +1146,41 @@ xml2cli(clicon_handle h,
cbuf_free(cbpre);
return retval;
}
/*! Translate from XML to CLI commands
*
* Howto: join strings and pass them down.
* Identify unique/index keywords for correct set syntax.
* @param[in] h Clicon handle
* @param[in] f Output FILE (eg stdout)
* @param[in] xn XML Parse-tree (to translate)
* @param[in] prepend Print this text in front of all commands.
* @param[in] fn Callback to make print function
* @param[in] skiptop 0: Include top object 1: Skip top-object, only children,
*/
int
xml2cli(clicon_handle h,
FILE *f,
cxobj *xn,
char *prepend,
clicon_output_cb *fn,
int skiptop)
{
int retval = 1;
cxobj *xc;
if (skiptop){
xc = NULL;
while ((xc = xml_child_each(xn, xc, CX_ELMNT)) != NULL)
if (xml2cli1(h, f, xc, prepend, fn) < 0)
goto done;
}
else {
if (xml2cli1(h, f, xn, prepend, fn) < 0)
goto done;
}
retval = 0;
done:
return retval;
}

View file

@ -137,7 +137,7 @@ int expand_dbvar(void *h, char *name, cvec *cvv, cvec *argv,
cvec *commands, cvec *helptexts);
int cli_xml2file (cxobj *xn, int level, int prettyprint, clicon_output_cb *fn);
int cli_xml2txt(cxobj *xn, clicon_output_cb *fn, int level);
int xml2cli(clicon_handle h, FILE *f, cxobj *xn, char *prepend, clicon_output_cb *fn);
int xml2cli(clicon_handle h, FILE *f, cxobj *xn, char *prepend, clicon_output_cb *fn, int skiptop);
/* cli_show.c: CLIgen new vector arg callbacks */
int show_yang(clicon_handle h, cvec *vars, cvec *argv);