From dc2b3a80ca1818161f9a99380be507c0d2a7435e Mon Sep 17 00:00:00 2001 From: Olof hagsand Date: Tue, 3 Aug 2021 13:38:31 +0200 Subject: [PATCH] * Fixed: YANG when was not properly implemented for LEAF default values --- lib/clixon/clixon_xml_map.h | 2 +- lib/src/clixon_validate.c | 2 +- lib/src/clixon_xml_map.c | 28 +++++++++++++--------------- test/test_leaf_default.sh | 20 ++++++++++++++++++++ 4 files changed, 35 insertions(+), 17 deletions(-) diff --git a/lib/clixon/clixon_xml_map.h b/lib/clixon/clixon_xml_map.h index feba656e..158d2cb8 100644 --- a/lib/clixon/clixon_xml_map.h +++ b/lib/clixon/clixon_xml_map.h @@ -76,6 +76,6 @@ int assign_namespace_body(cxobj *x0, cxobj *x1); int xml_merge(cxobj *x0, cxobj *x1, yang_stmt *yspec, char **reason); int yang_enum_int_value(cxobj *node, int32_t *val); int xml_copy_marked(cxobj *x0, cxobj *x1); -int yang_when_xpath(cxobj *xn, cxobj *xp, yang_stmt *yn, int *hit, int *nrp, char **xpathp); +int yang_check_when_xpath(cxobj *xn, cxobj *xp, yang_stmt *yn, int *hit, int *nrp, char **xpathp); #endif /* _CLIXON_XML_MAP_H_ */ diff --git a/lib/src/clixon_validate.c b/lib/src/clixon_validate.c index 8923dfe6..710d209b 100644 --- a/lib/src/clixon_validate.c +++ b/lib/src/clixon_validate.c @@ -1247,7 +1247,7 @@ xml_yang_validate_all(clicon_handle h, nsc = NULL; } } - if (yang_when_xpath(xt, xml_parent(xt), ys, &hit, &nr, &xpath) < 0) + if (yang_check_when_xpath(xt, xml_parent(xt), ys, &hit, &nr, &xpath) < 0) goto done; if (hit && nr == 0){ if ((cb = cbuf_new()) == NULL){ diff --git a/lib/src/clixon_xml_map.c b/lib/src/clixon_xml_map.c index 5c77c22d..e288e129 100644 --- a/lib/src/clixon_xml_map.c +++ b/lib/src/clixon_xml_map.c @@ -1164,13 +1164,11 @@ xml_default1(yang_stmt *yt, switch (yang_keyword_get(yc)){ case Y_LEAF: if (!cv_flag(yang_cv_get(yc), V_UNSET)){ /* Default value exists */ - /* Check when statement from uses or augment */ - if ((xpath = yang_when_xpath_get(yc)) != NULL){ - if ((nr = xpath_vec_bool(xt, yang_when_nsc_get(yc), "%s", xpath)) < 0) - goto done; - if (nr == 0) - break; /* Do not create default if xpath fails */ - } + /* Check when condition */ + if (yang_check_when_xpath(NULL, xt, yc, &hit, &nr, &xpath) < 0) + goto done; + if (hit && nr == 0) + break; /* Do not create default if xpath fails */ if (xml_find_type(xt, NULL, yang_argument_get(yc), CX_ELMNT) == NULL){ /* No such child exist, create this leaf */ if (xml_default_create(yc, xt, top) < 0) @@ -1181,8 +1179,8 @@ xml_default1(yang_stmt *yt, break; case Y_CONTAINER: if (yang_find(yc, Y_PRESENCE, NULL) == NULL){ - /* Check when statement from uses or augment */ - if (yang_when_xpath(NULL, xt, yc, &hit, &nr, &xpath) < 0) + /* Check when condition */ + if (yang_check_when_xpath(NULL, xt, yc, &hit, &nr, &xpath) < 0) goto done; if (hit && nr == 0) break; /* Do not create default if xpath fails */ @@ -2266,12 +2264,12 @@ xml_copy_marked(cxobj *x0, * Second variant of when, actual "when" sub-node RFC 7950 Sec 7.21.5. Can only be one. */ int -yang_when_xpath(cxobj *xn, - cxobj *xp, - yang_stmt *yn, - int *hit, - int *nrp, - char **xpathp) +yang_check_when_xpath(cxobj *xn, + cxobj *xp, + yang_stmt *yn, + int *hit, + int *nrp, + char **xpathp) { int retval = 1; yang_stmt *yc; diff --git a/test/test_leaf_default.sh b/test/test_leaf_default.sh index 4dbd1032..d36553c0 100755 --- a/test/test_leaf_default.sh +++ b/test/test_leaf_default.sh @@ -67,6 +67,19 @@ module example{ default 31; /* should be set on startup */ } } + /* Extra rules to check when condition */ + leaf npleaf{ + when "../s3 = '99'"; + type uint32; + default 98; + } + container npcont{ + when "../s3 = '99'"; + leaf npext{ + type uint32; + default 99; + } + } } container p4{ presence "A presence container"; @@ -144,6 +157,13 @@ expecteof "$clixon_netconf -qf $cfg" 0 "$DEFAULTHELLO]]>]]>" "^$XMLafalsefalse]]>]]>$" +# Set s3 leaf to 99 triggering when condition for default values +new "Set s3 to 99" +expecteof "$clixon_netconf -qf $cfg" 0 "$DEFAULTHELLO99]]>]]>" "^]]>]]>$" + +new "get config np3 with npleaf and npext" +expecteof "$clixon_netconf -qf $cfg" 0 "$DEFAULTHELLO]]>]]>" "^99319899]]>]]>$" + if [ $BE -ne 0 ]; then new "Kill backend" # Check if premature kill