Extended purge argument to xml_defaults_nopresence to handle non-config case

Test: with-defaults: added headings to all test cases
This commit is contained in:
Olof hagsand 2022-09-07 15:18:48 +02:00
parent 2d23c9656b
commit 38f501be19
5 changed files with 49 additions and 11 deletions

View file

@ -890,7 +890,7 @@ xmldb_get_cache(clicon_handle h,
goto done;
}
/* Remove global defaults and empty non-presence containers */
if (xml_defaults_nopresence(x0t, 1) < 0)
if (xml_defaults_nopresence(x0t, 2) < 0)
goto done;
if (yb != YB_NONE){
/* Add default global values */
@ -1163,7 +1163,7 @@ xmldb_get0_clear(clicon_handle h,
if (x == NULL)
goto ok;
/* Remove global defaults and empty non-presence containers */
if (xml_defaults_nopresence(x, 1) < 0)
if (xml_defaults_nopresence(x, 2) < 0)
goto done;
/* Clear XML tree of defaults */
if (xml_tree_prune_flagged(x, XML_FLAG_TRANSIENT, 1) < 0)

View file

@ -1258,7 +1258,7 @@ xmldb_put(clicon_handle h,
(void*)(XML_FLAG_NONE|XML_FLAG_MARK)) < 0)
goto done;
/* Remove global defaults and empty non-presence containers */
if (xml_defaults_nopresence(x0, 1) < 0)
if (xml_defaults_nopresence(x0, 2) < 0)
goto done;
#if 0 /* debug */
if (xml_apply0(x0, -1, xml_sort_verify, NULL) < 0)

View file

@ -1103,11 +1103,14 @@ xml_global_defaults(clicon_handle h,
/*! Recursively find empty nopresence containers and default leaves, optionally purge
*
* @param[in] xn XML tree
* @param[in] purge Remove sub-nodes that is empty non-presence container or default leaf
* @param[in] purge 0: Dont remove any nodes
* 1: Remove config sub-nodes that are empty non-presence container or default leaf
* 2: Remove all sub-nodes that are empty non-presence container or default leaf
* @retval 1 Node is an (recursive) empty non-presence container or default leaf
* @retval 0 Other node
* @retval -1 Error
* @note xn is not itself removed if purge
* @note for purge=1 are removed only if config or no yang spec(!)
*/
int
xml_defaults_nopresence(cxobj *xn,
@ -1117,9 +1120,11 @@ xml_defaults_nopresence(cxobj *xn,
cxobj *x;
cxobj *xprev;
yang_stmt *yn;
yang_stmt *y;
int rmx = 0; /* If set, remove this xn */
int ret;
enum rfc_6020 keyw;
int config = 0;
if ((yn = xml_spec(xn)) != NULL){
keyw = yang_keyword_get(yn);
@ -1129,6 +1134,7 @@ xml_defaults_nopresence(cxobj *xn,
else if (keyw == Y_LEAF &&
xml_flag(xn, XML_FLAG_DEFAULT))
rmx = 1;
config = yang_config_ancestor(yn);
}
/* Loop thru children */
x = NULL;
@ -1137,14 +1143,26 @@ xml_defaults_nopresence(cxobj *xn,
if ((ret = xml_defaults_nopresence(x, purge)) < 0)
goto done;
if (ret == 1){
if (purge){
switch (purge){
case 1: /* config nodes only */
if (!config)
break;
if ((y = xml_spec(x)) != NULL &&
!yang_config(y))
break;
/* fall thru */
case 2: /* purge all nodes */
if (xml_purge(x) < 0)
goto done;
x = xprev;
break;
default:
break;
}
}
else if (rmx)
rmx = 0;
/* May switch an empty non-presence container (rmx=1) to non-empty non-presence container (rmx=0) */
rmx = 0;
}
retval = rmx;
done: