Skip to content

Commit

Permalink
Fixed: [YANG ordering fails for nested choice and action](YANG orderi…
Browse files Browse the repository at this point in the history
…ng fails for nested choice and action)
  • Loading branch information
olofhagsand committed Aug 24, 2022
1 parent 795ac0c commit a516ee1
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 55 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Expected: September 2022

### Corrected Bugs

* Fixed: [YANG ordering fails for nested choice and action](YANG ordering fails for nested choice and action)
* Fixed: [YANG min-elements within non-presence container does not work](https://github.com/clicon/clixon/issues/355)
* Fixed: [Issues with ietf-snmp modules](https://github.com/clicon/clixon/issues/353)
* Fixed: [Missing/no namespace error in YANG augments with default values](https://github.com/clicon/clixon/issues/354)
Expand Down
64 changes: 43 additions & 21 deletions lib/src/clixon_xml_sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -182,19 +182,18 @@ xml_cv_cache_clear(cxobj *xt)
* Example: x1: <x><k1>1</k1></x> and x2: <x><k1>1</k1><k2>2</k2></x> are considered equal.
* This is useful in searching for indexes in trees, where x1 is a search index and not a
* complete tree.
*
* @note empty value/NULL is smallest value
* @note some error cases return as -1 (qsort cant handle errors)
* @note some error cases return as -1 (qsort cant handle errors)
*
* @note "comparing" x1 and x2 here judges equality from a structural (model)
*
* "Comparing" x1 and x2 here determines equality from a structural (model)
* perspective, ie both have the same yang spec, if they are lists, they have the
* the same keys. NOT that the values are equal!
* In other words, if x is a leaf with the same yang spec, <x>1</x> and <x>2</x> are
* "equal".
* In other words, if x is a leaf with the same yang spec, <x>1</x> and <x>2</x> are "equal".
* If x is a list element (with key "k"),
* <x><k>42</42><y>foo</y></x> and <x><k>42</42><y>bar</y></x> are equal,
* <x><k>42</42><y>foo</y></x> and <x><k>42</42><y>bar</y></x> are equal,
* but is not equal to <x><k>71</42><y>bar</y></x>
*
* @note empty value/NULL is smallest value
* @note some error cases return as -1 (qsort cant handle errors) (which?)
* - yang_order() is one
*/
int
xml_cmp(cxobj *x1,
Expand Down Expand Up @@ -256,6 +255,7 @@ xml_cmp(cxobj *x1,
goto done;
}
if (y1 != y2){
/* XXX handle errors */
yi1 = yang_order(y1);
yi2 = yang_order(y2);
if ((equal = yi1-yi2) != 0)
Expand Down Expand Up @@ -388,7 +388,7 @@ xml_cmp(cxobj *x1,
}

/*!
* @note args are pointer ot pointers, to fit into qsort cmp function
* @note args are pointer to pointers, to fit into qsort cmp function
*/
static int
xml_cmp_qsort(const void* arg1,
Expand Down Expand Up @@ -476,11 +476,14 @@ xml_find_keys_notsorted(cxobj *xp,
int i;
cxobj *xc;
yang_stmt *yc;
int yi;

for (i=mid+1; i<xml_child_nr(xp); i++){ /* First increment */
xc = xml_child_i(xp, i);
yc = xml_spec(xc);
if (yangi != yang_order(yc)) /* wrong yang */
if ((yi = yang_order(yc)) < -1)
goto done;
if (yangi != yi) /* wrong yang */
break;
if (xml_cmp(xc, x1, 0, skip1, NULL) == 0){
if (clixon_xvec_append(xvec, xc) < 0)
Expand All @@ -491,7 +494,9 @@ xml_find_keys_notsorted(cxobj *xp,
for (i=mid-1; i>=0; i--){ /* Then decrement */
xc = xml_child_i(xp, i);
yc = xml_spec(xc);
if (yangi != yang_order(yc)) /* wrong yang */
if ((yi = yang_order(yc)) < -1)
goto done;
if (yangi != yi) /* wrong yang */
break;
if (xml_cmp(xc, x1, 0, skip1, NULL) == 0){
if (clixon_xvec_append(xvec, xc) < 0)
Expand Down Expand Up @@ -528,11 +533,14 @@ search_multi_equals(cxobj **childvec,
int i;
cxobj *xc;
yang_stmt *yc;
int yi;

for (i=mid-1; i>=0; i--){ /* First decrement */
xc = childvec[i];
yc = xml_spec(xc);
if (yangi != yang_order(yc)) /* wrong yang */
if ((yi = yang_order(yc)) < -1)
goto done;
if (yangi != yi) /* wrong yang */
break;
if (xml_cmp(x1, xc, 0, skip1, NULL) != 0)
break;
Expand All @@ -542,7 +550,9 @@ search_multi_equals(cxobj **childvec,
for (i=mid+1; i<childlen; i++){ /* Then increment */
xc = childvec[i];
yc = xml_spec(xc);
if (yangi != yang_order(yc)) /* wrong yang */
if ((yi = yang_order(yc)) < -1)
goto done;
if (yangi != yi) /* wrong yang */
break;
if (xml_cmp(x1, xc, 0, skip1, NULL) != 0)
break;
Expand All @@ -569,11 +579,14 @@ search_multi_equals_xvec(clixon_xvec *childvec,
int i;
cxobj *xc;
yang_stmt *yc;
int yi;

for (i=mid-1; i>=0; i--){ /* First decrement */
xc = clixon_xvec_i(childvec, i);
yc = xml_spec(xc);
if (yangi != yang_order(yc)) /* wrong yang */
if ((yi = yang_order(yc)) < -1)
goto done;
if (yangi != yi) /* wrong yang */
break;
if (xml_cmp(x1, xc, 0, skip1, NULL) != 0)
break;
Expand All @@ -583,7 +596,9 @@ search_multi_equals_xvec(clixon_xvec *childvec,
for (i=mid+1; i<clixon_xvec_len(childvec); i++){ /* Then increment */
xc = clixon_xvec_i(childvec, i);
yc = xml_spec(xc);
if (yangi != yang_order(yc)) /* wrong yang */
if ((yi = yang_order(yc)) < -1)
goto done;
if (yangi != yi) /* wrong yang */
break;
if (xml_cmp(x1, xc, 0, skip1, NULL) != 0)
break;
Expand Down Expand Up @@ -734,6 +749,7 @@ xml_search_binary(cxobj *xp,
int cmp;
cxobj *xc;
yang_stmt *y;
int yi;

if (upper < low)
goto ok;
Expand All @@ -743,7 +759,9 @@ xml_search_binary(cxobj *xp,
xc = xml_child_i(xp, mid);
if ((y = xml_spec(xc)) == NULL)
goto ok;
cmp = yangi-yang_order(y);
if ((yi = yang_order(y)) < -1)
goto done;
cmp = yangi - yi;
/* Here is right yang order == same yang? */
if (cmp == 0){
#ifdef XML_EXPLICIT_INDEX
Expand Down Expand Up @@ -827,8 +845,8 @@ xml_search_yang(cxobj *xp,
#endif
if (yang_keyword_get(yc) == Y_LIST || yang_keyword_get(yc) == Y_LEAF_LIST)
sorted = (yang_find(yc, Y_ORDERED_BY, "user") == NULL);
yangi = yang_order(yc);
if ((yangi = yang_order(yc)) < -1)
goto done;
if (xml_search_binary(xp, x1, sorted, yangi, low, upper, skip1, indexvar, xvec) < 0)
goto done;
retval = 0;
Expand Down Expand Up @@ -962,6 +980,7 @@ xml_insert2(cxobj *xp,
int cmp;
cxobj *xc;
yang_stmt *yc;
int yi;

if (low > upper){ /* beyond range */
clicon_err(OE_XML, 0, "low>upper %d %d", low, upper);
Expand Down Expand Up @@ -994,7 +1013,9 @@ xml_insert2(cxobj *xp,
cmp = xml_cmp(xn, xc, 0, 0, NULL);
}
else{ /* Not equal yang - compute diff */
cmp = yni - yang_order(yc);
if ((yi = yang_order(yc)) < -1)
goto done;
cmp = yni - yi;
/* One case is a choice where
* xc = <tcp/>, xn = <udp/>
* same order but different yang spec
Expand Down Expand Up @@ -1072,7 +1093,8 @@ xml_insert(cxobj *xp,
#endif
if (yang_keyword_get(y) == Y_LIST || yang_keyword_get(y) == Y_LEAF_LIST)
userorder = (yang_find(y, Y_ORDERED_BY, "user") != NULL);
yi = yang_order(y);
if ((yi = yang_order(y)) < -1)
goto done;
if ((i = xml_insert2(xp, xi, y, yi,
userorder, ins, key_val, nsc_key,
low, upper)) < 0)
Expand Down
81 changes: 50 additions & 31 deletions lib/src/clixon_yang.c
Original file line number Diff line number Diff line change
Expand Up @@ -1466,95 +1466,110 @@ yang_choice(yang_stmt *y)
* In (2) we increment with only 1.
*/
static int
order1_choice(yang_stmt *yp,
yang_stmt *y,
int *index)
yang_order1_choice(yang_stmt *yp,
yang_stmt *y,
int *index)
{
yang_stmt *ys;
yang_stmt *yc;
int i;
int j;
int shortcut=0;
int max=0;
int index0;

index0 = *index;
for (i=0; i<yp->ys_len; i++){ /* Loop through choice */
ys = yp->ys_stmt[i];
if (ys->ys_keyword == Y_CASE){ /* Loop through case */
*index = index0;
for (j=0; j<ys->ys_len; j++){
yc = ys->ys_stmt[j];
if (yang_datanode(yc) && yc == y){
*index += j;
return 1;
if (yc->ys_keyword == Y_CHOICE){
if (yang_order1_choice(yc, y, index) == 1) /* If one of the choices is "y" */
return 1;
}
else {
if (yang_datanode(yc) && yc == y){
// *index = index0 + j;
return 1;
}
(*index)++;
}
}
if (j>max)
max = j;
if (*index-index0 > max)
max = *index-index0;
}
else {
shortcut = 1; /* Shortcut, no case */
max = 1; /* Shortcut, no case */
if (yang_datanode(ys) && ys == y)
return 1;
}
}
if (shortcut)
(*index)++;
else
*index += max;
*index += max;
return 0;
}

/*! Find matching y in yp:s children, return "yang order" of y or -1 if not found
* @param[in] yp Parent
* @param[in] y Yang datanode to find
* @param[out] index Index of y in yp:s list of children
* @retval 0 not found (must be datanode)
* @retval 1 found
* @retval 0 OK: found
* @retval -1 Error: not found (must be datanode)
*/
static int
order1(yang_stmt *yp,
yang_stmt *y,
int *index)
yang_order1(yang_stmt *yp,
yang_stmt *y,
int *index)
{
int retval = -1;
yang_stmt *ys;
int i;

for (i=0; i<yp->ys_len; i++){
ys = yp->ys_stmt[i];
if (ys->ys_keyword == Y_CHOICE){
if (order1_choice(ys, y, index) == 1) /* If one of the choices is "y" */
return 1;
if (yang_order1_choice(ys, y, index) == 1) /* If one of the choices is "y" */
break;
}
else {
if (!yang_datanode(ys))
if (!yang_datanode(ys) &&
yang_keyword_get(ys) != Y_ACTION) /* action is special case */
continue;
if (ys == y)
return 1;
break;
(*index)++;
}
}
return 0;
if (i < yp->ys_len) /* break -> found */
retval = 0;
else
assert(0); // XXX
return retval;
}

/*! Return order of yang statement y in parents child vector
* @param[in] y Find position of this data-node
* @param[out] index Index of y in yp:s list of children
* @retval >=0 Order of child with specified argument
* @retval -1 Not found
* @retval -1 No spec, y is NULL, which applies to eg attributes and are placed first
* @retval -2 Error: Not found
* @note special handling if y is child of (sub)module
*/
int
yang_order(yang_stmt *y)
{
int retval = -2;
yang_stmt *yp;
yang_stmt *ypp;
yang_stmt *ym;
int i;
int j=0;
int tot = 0;

if (y == NULL)
return -1;

if (y == NULL){
retval = -1;
goto done;
}
/* Some special handling if yp is choice (or case)
* if so, the real parent (from an xml point of view) is the parents
* parent.
Expand All @@ -1578,9 +1593,13 @@ yang_order(yang_stmt *y)
tot += ym->ys_len;
}
}
if (order1(yp, y, &j) == 1)
return tot + j;
return -1;
if (yang_order1(yp, y, &j) < 0){
clicon_err(OE_YANG, 0, "YANG node %s not ordered: not found", yang_argument_get(y));
goto done;
}
retval = tot + j;
done:
return retval;
}

char *
Expand Down
3 changes: 0 additions & 3 deletions test/test_augment_default.sh
Original file line number Diff line number Diff line change
Expand Up @@ -89,21 +89,18 @@ new "wait backend"
wait_backend

new "cli show config startup"
#expectpart "$($clixon_cli -1 -f $cfg -l o show config xml)" 0 '<table xmlns="urn:example:clixon">' '<map xmlns="urn:example:augment">' '<aug:enable xmlns:aug="urn:example:augment">true</aug:enable>'
expectpart "$($clixon_cli -1 -f $cfg -l o show config xml)" 0 '<table xmlns="urn:example:clixon">' '<map xmlns="urn:example:augment">' '<enable>true</enable>'

new "cli delete map name"
expectpart "$($clixon_cli -1 -f $cfg -l o delete table map name me)" 0 ""

new "cli show config deleted"
#expectpart "$($clixon_cli -1 -f $cfg -l o show config xml)" 0 '<table xmlns="urn:example:clixon">' '<aug:map xmlns:aug="urn:example:augment">' '<aug:enable>true</aug:enable>'
expectpart "$($clixon_cli -1 -f $cfg -l o show config xml)" 0 '<table xmlns="urn:example:clixon">' '<map xmlns="urn:example:augment">' '<enable>true</enable>'

new "cli set map name"
expectpart "$($clixon_cli -1 -f $cfg -l o set table map name x)" 0 ""

new "cli show config set"
#expectpart "$($clixon_cli -1 -f $cfg -l o show config xml)" 0 '<table xmlns="urn:example:clixon">' '<map xmlns="urn:example:augment">' '<aug:enable xmlns:aug="urn:example:augment">true</aug:enable>'
expectpart "$($clixon_cli -1 -f $cfg -l o show config xml)" 0 '<table xmlns="urn:example:clixon">' '<map xmlns="urn:example:augment">' '<enable>true</enable>'

if [ $BE -ne 0 ]; then
Expand Down
Loading

0 comments on commit a516ee1

Please sign in to comment.