System-only-config

* Fix startup diff
* Add system-only to running on startup
* JSON support:
This commit is contained in:
Olof hagsand 2024-10-16 13:46:47 +02:00
parent 3a656fac07
commit 69b65ad13d
19 changed files with 276 additions and 105 deletions

View file

@ -44,9 +44,9 @@
* Prototypes
*/
int json2xml_decode(cxobj *x, cxobj **xerr);
int clixon_json2cbuf(cbuf *cb, cxobj *x, int pretty, int skiptop, int autocliext);
int clixon_json2cbuf(cbuf *cb, cxobj *x, int pretty, int skiptop, int autocliext, int system_only);
int xml2json_cbuf_vec(cbuf *cb, cxobj **vec, size_t veclen, int pretty, int skiptop);
int clixon_json2file(FILE *f, cxobj *x, int pretty, clicon_output_cb *fn, int skiptop, int autocliext);
int clixon_json2file(FILE *f, cxobj *x, int pretty, clicon_output_cb *fn, int skiptop, int autocliext, int system_only);
int json_print(FILE *f, cxobj *x);
int xml2json_vec(FILE *f, cxobj **vec, size_t veclen, int pretty, clicon_output_cb *fn, int skiptop);
int clixon_json_parse_string(char *str, int rfc7951, yang_bind yb, yang_stmt *yspec, cxobj **xt, cxobj **xret);

View file

@ -887,7 +887,7 @@ xmldb_get_cache(clixon_handle h,
goto done;
}
if (clicon_option_bool(h, "CLICON_XMLDB_SYSTEM_ONLY_CONFIG") &&
strcmp(db, "running") == 0){
(strcmp(db, "candidate") != 0)) {
if (xmldb_system_only_config(h, xpath?xpath:"/", nsc, &x1t) < 0)
goto done;
}

View file

@ -1628,8 +1628,7 @@ xmldb_dump(clixon_handle h,
switch (format){
case FORMAT_XML:
if (clixon_xml2file1(f, xt, 0, pretty, NULL, fprintf, 0, 0, wdef, multi,
clicon_option_bool(h, "CLICON_XMLDB_SYSTEM_ONLY_CONFIG")
) < 0)
clicon_option_bool(h, "CLICON_XMLDB_SYSTEM_ONLY_CONFIG")) < 0)
goto done;
if (multi){
mw.mw_h = h;
@ -1646,7 +1645,8 @@ xmldb_dump(clixon_handle h,
clixon_err(OE_CFG, errno, "JSON+multi not supported");
goto done;
}
if (clixon_json2file(f, xt, pretty, fprintf, 0, 0) < 0)
if (clixon_json2file(f, xt, pretty, fprintf, 0, 0,
clicon_option_bool(h, "CLICON_XMLDB_SYSTEM_ONLY_CONFIG")) < 0)
goto done;
break;
default:

View file

@ -789,6 +789,7 @@ xml2json_encode_attr(cxobj *xa,
* @param[in] level Indentation level
* @param[in] pretty Pretty-print output (2 means debug)
* @param[in] flat Dont print NO_ARRAY object name (for _vec call)
* @param[in] system_only Enable checks for system-only-config extension
* @param[in] modname0
* @param[out] metacbp Meta encoding of attribute
* @retval 0 OK
@ -829,6 +830,7 @@ xml2json1_cbuf(cbuf *cb,
int level,
int pretty,
int flat,
int system_only,
char *modname0,
cbuf *metacbp)
{
@ -838,11 +840,13 @@ xml2json1_cbuf(cbuf *cb,
cxobj *xp;
enum childtype childt;
enum array_element_type xc_arraytype;
yang_stmt *yc;
yang_stmt *ys;
yang_stmt *ymod = NULL; /* yang module */
int commas;
char *modname = NULL;
cbuf *metacbc = NULL;
int exist;
if ((ys = xml_spec(x)) != NULL){
if (ys_real_module(ys, &ymod) < 0)
@ -957,21 +961,29 @@ xml2json1_cbuf(cbuf *cb,
xc_arraytype = array_eval(i?xml_child_i(x,i-1):NULL,
xc,
xml_child_i(x, i+1));
if (xml2json1_cbuf(cb,
xc,
xc_arraytype,
level+1, pretty, 0, modname0,
metacbc) < 0)
goto done;
if (commas > 0) {
cprintf(cb, ",%s", pretty?"\n":"");
--commas;
exist = 0;
if ((yc = xml_spec(xc)) != NULL && system_only){
if (yang_extension_value(yc, "system-only-config", CLIXON_LIB_NS, &exist, NULL) < 0)
goto done;
if (exist && commas)
commas--;
}
if (!exist) {
if (xml2json1_cbuf(cb,
xc,
xc_arraytype,
level+1, pretty, 0, system_only, modname0,
metacbc) < 0)
goto done;
if (commas > 0) {
cprintf(cb, ",%s", pretty?"\n":"");
--commas;
}
}
}
if (cbuf_len(metacbc)){
cprintf(cb, "%s", cbuf_get(metacbc));
}
switch (arraytype){
case BODY_ARRAY:
break;
@ -1041,21 +1053,23 @@ xml2json1_cbuf(cbuf *cb,
* XML-style namespace notation in tree, but RFC7951 in output assume yang
* populated
*
* @param[in,out] cb Cligen buffer to write to
* @param[in] x XML tree to translate from
* @param[in] pretty Set if output is pretty-printed
* @param[in] autocliext How to handle autocli extensions: 0: ignore 1: follow
* @retval 0 OK
* @retval -1 Error
* @param[in,out] cb Cligen buffer to write to
* @param[in] x XML tree to translate from
* @param[in] pretty Set if output is pretty-printed
* @param[in] autocliext How to handle autocli extensions: 0: ignore 1: follow
* @param[in] system_only Enable checks for system-only-config extension
* @retval 0 OK
* @retval -1 Error
*
* @see clixon_xml2cbuf XML corresponding function
* @see xml2json_cbuf_vec Top symbol is list
*/
static int
xml2json_cbuf1(cbuf *cb,
cxobj *x,
int pretty,
int autocliext)
xml2json_cbuf1(cbuf *cb,
cxobj *x,
int pretty,
int autocliext,
int system_only)
{
int retval = 1;
int level = 0;
@ -1070,6 +1084,7 @@ xml2json_cbuf1(cbuf *cb,
if (exist)
goto ok;
}
cprintf(cb, "%*s{%s",
pretty?level*PRETTYPRINT_INDENT:0,"",
pretty?"\n":"");
@ -1090,6 +1105,7 @@ xml2json_cbuf1(cbuf *cb,
level+1,
pretty,
0,
system_only,
NULL, /* ancestor modname / namespace */
NULL) < 0)
goto done;
@ -1108,13 +1124,14 @@ xml2json_cbuf1(cbuf *cb,
* XML-style namespace notation in tree, but RFC7951 in output assume yang
* populated
*
* @param[in,out] cb Cligen buffer to write to
* @param[in] xt Top-level xml object
* @param[in] pretty Set if output is pretty-printed
* @param[in] skiptop 0: Include top object 1: Skip top-object, only children,
* @param[in] autocliext How to handle autocli extensions: 0: ignore 1: follow
* @retval 0 OK
* @retval -1 Error
* @param[in,out] cb Cligen buffer to write to
* @param[in] xt Top-level xml object
* @param[in] pretty Set if output is pretty-printed
* @param[in] skiptop 0: Include top object 1: Skip top-object, only children,
* @param[in] autocliext How to handle autocli extensions: 0: ignore 1: follow
* @param[in] system_only Enable checks for system-only-config extension
* @retval 0 OK
* @retval -1 Error
* @code
* cbuf *cb = cbuf_new();
* if (xml2json_cbuf(cb, xn, 0, 0, 0) < 0)
@ -1128,7 +1145,8 @@ clixon_json2cbuf(cbuf *cb,
cxobj *xt,
int pretty,
int skiptop,
int autocliext)
int autocliext,
int system_only)
{
int retval = -1;
cxobj *xc;
@ -1139,12 +1157,12 @@ clixon_json2cbuf(cbuf *cb,
while ((xc = xml_child_each(xt, xc, CX_ELMNT)) != NULL){
if (i++)
cprintf(cb, ",");
if (xml2json_cbuf1(cb, xc, pretty, autocliext) < 0)
if (xml2json_cbuf1(cb, xc, pretty, autocliext, system_only) < 0)
goto done;
}
}
else {
if (xml2json_cbuf1(cb, xt, pretty, autocliext) < 0)
if (xml2json_cbuf1(cb, xt, pretty, autocliext, system_only) < 0)
goto done;
}
retval = 0;
@ -1217,7 +1235,7 @@ xml2json_cbuf_vec(cbuf *cb,
NO_ARRAY,
level,
pretty,
1, NULL, NULL) < 0)
1, 0, NULL, NULL) < 0)
goto done;
if (0){
@ -1237,19 +1255,20 @@ xml2json_cbuf_vec(cbuf *cb,
/*! Translate from xml tree to JSON and print to file using a callback
*
* @param[in] f File to print to
* @param[in] xn XML tree to translate from
* @param[in] pretty Set if output is pretty-printed
* @param[in] fn File print function
* @param[in] skiptop 0: Include top object 1: Skip top-object, only children,
* @param[in] autocliext How to handle autocli extensions: 0: ignore 1: follow
* @retval 0 OK
* @retval -1 Error
* @param[in] f File to print to
* @param[in] xn XML tree to translate from
* @param[in] pretty Set if output is pretty-printed
* @param[in] fn File print function
* @param[in] skiptop 0: Include top object 1: Skip top-object, only children,
* @param[in] autocliext How to handle autocli extensions: 0: ignore 1: follow
* @param[in] system_only Enable checks for system-only-config extension
* @retval 0 OK
* @retval -1 Error
*
* @note yang is necessary to translate to one-member lists,
* eg if a is a yang LIST <a>0</a> -> {"a":["0"]} and not {"a":"0"}
* @code
* if (clixon_json2file(stderr, xn, 0, fprintf, 0, 0) < 0)
* if (clixon_json2file(stderr, xn, 0, fprintf, 0, 0, 0) < 0)
* goto err;
* @endcode
*/
@ -1259,7 +1278,8 @@ clixon_json2file(FILE *f,
int pretty,
clicon_output_cb *fn,
int skiptop,
int autocliext)
int autocliext,
int system_only)
{
int retval = 1;
cbuf *cb = NULL;
@ -1270,7 +1290,7 @@ clixon_json2file(FILE *f,
clixon_err(OE_XML, errno, "cbuf_new");
goto done;
}
if (clixon_json2cbuf(cb, xn, pretty, skiptop, autocliext) < 0)
if (clixon_json2cbuf(cb, xn, pretty, skiptop, autocliext, system_only) < 0)
goto done;
(*fn)(f, "%s", cbuf_get(cb));
retval = 0;
@ -1289,7 +1309,7 @@ int
json_print(FILE *f,
cxobj *x)
{
return clixon_json2file(f, x, 1, fprintf, 0, 0);
return clixon_json2file(f, x, 1, fprintf, 0, 0, 0);
}
/*! Translate a vector of xml objects to JSON File.

View file

@ -253,7 +253,7 @@ clicon_option_dump1(clixon_handle h,
goto done;
break;
case FORMAT_JSON:
if (clixon_json2file(f, xc, pretty, cligen_output, 0, 0) < 0)
if (clixon_json2file(f, xc, pretty, cligen_output, 0, 0, 0) < 0)
goto done;
break;
case FORMAT_TEXT: