sort xml children on parsing

This commit is contained in:
Olof hagsand 2017-12-31 18:25:53 +01:00
parent 9681c91681
commit 19301ae240
5 changed files with 35 additions and 23 deletions

View file

@ -403,7 +403,11 @@ from_client_edit_config(clicon_handle h,
"</rpc-error></rpc-reply>");
goto ok;
}
/* Cant do this earlier since we dont have a yang spec to
* the upper part of the tree, until we get the "config" tree.
*/
if (xml_child_sort && xml_apply0(xc, CX_ELMNT, xml_sort, NULL) < 0)
goto done;
if (xmldb_put(h, target, operation, xc) < 0){
cprintf(cbret, "<rpc-reply><rpc-error>"
"<error-tag>operation-failed</error-tag>"

View file

@ -474,9 +474,6 @@ text_get(xmldb_handle xh,
if (singleconfigroot(xt, &xt) < 0)
goto done;
}
/* Sort XML children according to YANG */
if (xml_child_sort && xml_apply0(xt, CX_ELMNT, xml_sort, NULL) < 0)
goto done;
} /* xt == NULL */
/* Here xt looks like: <config>...</config> */
@ -540,11 +537,10 @@ text_get(xmldb_handle xh,
/* Order XML children according to YANG */
if (!xml_child_sort && xml_apply(xt, CX_ELMNT, xml_order, NULL) < 0)
goto done;
/* Again just so default values are placed correctly */
if (xml_child_sort && xml_apply0(xt, CX_ELMNT, xml_sort, NULL) < 0)
goto done;
#if 0 /* debug */
if (xml_child_sort && xml_apply0(xt, -1, xml_sort_verify, NULL) < 0)
clicon_log(LOG_NOTICE, "%s: verify failed #2", __FUNCTION__);
#endif
if (debug>1)
clicon_xml2file(stderr, xt, 0, 1);
*xtop = xt;
@ -730,6 +726,7 @@ text_modify(cxobj *x0,
} /* CONTAINER switch op */
} /* else Y_CONTAINER */
// ok:
xml_sort(x0p, NULL);
retval = 0;
done:
if (x0vec)
@ -916,8 +913,10 @@ text_put(xmldb_handle xh,
/* XXX: where is this created? Add yspec */
if (xml_apply(x1, CX_ELMNT, xml_spec_populate, yspec) < 0)
goto done;
if (xml_child_sort && xml_apply0(x1, CX_ELMNT, xml_sort, NULL) < 0)
goto done;
#if 0 /* debug */
if (xml_child_sort && xml_apply0(x1, -1, xml_sort_verify, NULL) < 0)
clicon_log(LOG_NOTICE, "%s: verify failed #1", __FUNCTION__);
#endif
/*
* Modify base tree x with modification x1. This is where the
* new tree is made.
@ -937,9 +936,10 @@ text_put(xmldb_handle xh,
/* Remove (prune) nodes that are marked (non-presence containers w/o children) */
if (xml_tree_prune_flagged(x0, XML_FLAG_MARK, 1) < 0)
goto done;
if (xml_child_sort && xml_apply0(x0, CX_ELMNT, xml_sort, NULL) < 0)
goto done;
#if 0 /* debug */
if (xml_child_sort && xml_apply0(x0, -1, xml_sort_verify, NULL) < 0)
clicon_log(LOG_NOTICE, "%s: verify failed #3", __FUNCTION__);
#endif
/* Write back to datastore cache if first time */
if (xmltree_cache){
struct db_element de0 = {0,};

View file

@ -534,10 +534,10 @@ xml_childvec_get(cxobj *x)
cxobj *
xml_new(char *name,
cxobj *xp,
yang_stmt *spec)
yang_stmt *yspec)
{
cxobj *x;
if ((x = malloc(sizeof(cxobj))) == NULL){
clicon_err(OE_XML, errno, "%s: malloc", __FUNCTION__);
return NULL;
@ -545,11 +545,12 @@ xml_new(char *name,
memset(x, 0, sizeof(cxobj));
if ((xml_name_set(x, name)) < 0)
return NULL;
xml_parent_set(x, xp);
if (xp && xml_child_append(xp, x) < 0)
return NULL;
x->x_spec = spec; /* Can be NULL */
if (xp){
xml_parent_set(x, xp);
if (xml_child_append(xp, x) < 0)
return NULL;
}
x->x_spec = yspec; /* Can be NULL */
return x;
}
@ -1212,8 +1213,8 @@ xmltree2cbuf(cbuf *cb,
*/
static int
_xml_parse(const char *str,
yang_spec *yspec,
cxobj *xt)
yang_spec *yspec,
cxobj *xt)
{
int retval = -1;
struct xml_parse_yacc_arg ya = {0,};
@ -1233,6 +1234,13 @@ _xml_parse(const char *str,
goto done;
if (clixon_xml_parseparse(&ya) != 0) /* yacc returns 1 on error */
goto done;
/* Sort the complete tree after parsing */
if (yspec){
if (xml_apply0(xt, CX_ELMNT, xml_sort, NULL) < 0)
goto done;
if (xml_apply0(xt, -1, xml_sort_verify, NULL) < 0)
goto done;
}
retval = 0;
done:
clixon_xml_parsel_exit(&ya);

View file

@ -1206,6 +1206,7 @@ xml_default(cxobj *xt,
}
}
}
xml_sort(xt, NULL);
retval = 0;
done:
return retval;

View file

@ -18,7 +18,6 @@ cfg=/tmp/scaling-conf.xml
fyang=/tmp/scaling.yang
fconfig=/tmp/config
# include err() and new() functions
. ./lib.sh
@ -67,7 +66,7 @@ if [ $? -ne 0 ]; then
err
fi
new "start backend"
new "start backend -s init -f $cfg -y $fyang"
# start new backend
sudo clixon_backend -s init -f $cfg -y $fyang
if [ $? -ne 0 ]; then