started min/max-element

This commit is contained in:
Olof hagsand 2018-07-18 21:45:13 +02:00
parent 0eb9e6c8b2
commit cc6c7ae7a4
3 changed files with 163 additions and 7 deletions

View file

@ -3,9 +3,9 @@
## 3.7.0 (Upcoming) ## 3.7.0 (Upcoming)
### Major changes: ### Major changes:
* Support for YANG conditionals "must" and "when" according to RFC 7950 Sec 7.5.3 and 7.21.5 * Support for YANG conditionals "must" and "when" according to RFC 7950 Sec 7.5.3 and 7.21.5.
* XPATH checked at validation time * XPATH checked at validation time
* Support for YANG identity, identityref, must and when according to RFC 7950 Sec 7.189. * Support for YANG identity, identityref, according to RFC 7950 Sec 7.18 and 9.10.
* Previous support did no validation of values. * Previous support did no validation of values.
* Validation of types and CLI expansion * Validation of types and CLI expansion
* Example extended with inclusion of iana-if-type RFC 7224 interface identities * Example extended with inclusion of iana-if-type RFC 7224 interface identities

View file

@ -430,7 +430,7 @@ xml_yang_validate_all(cxobj *xt,
yang_stmt *yc; /* yang child */ yang_stmt *yc; /* yang child */
yang_stmt *ye; /* yang must error-message */ yang_stmt *ye; /* yang must error-message */
char *xpath; char *xpath;
int b; int nr;
/* if not given by argument (overide) use default link /* if not given by argument (overide) use default link
and !Node has a config sub-statement and it is false */ and !Node has a config sub-statement and it is false */
@ -453,6 +453,33 @@ xml_yang_validate_all(cxobj *xt,
if (validate_identityref(xt, ys, yc) < 0) if (validate_identityref(xt, ys, yc) < 0)
goto done; goto done;
} }
}
if ((yc = yang_find((yang_node*)ys, Y_MIN_ELEMENTS, NULL)) != NULL){
/* The behavior of the constraint depends on the type of the
* leaf-list's or list's closest ancestor node in the schema tree
* that is not a non-presence container (see Section 7.5.1):
* o If no such ancestor exists in the schema tree, the constraint
* is enforced.
* o Otherwise, if this ancestor is a case node, the constraint is
* enforced if any other node from the case exists.
* o Otherwise, it is enforced if the ancestor node exists.
*/
#if 0
cxobj *xp;
cxobj *x;
int i;
if ((xp = xml_parent(xt)) != NULL){
nr = atoi(yc->ys_argument);
x = NULL;
i = 0;
while ((x = xml_child_each(xt, x, CX_ELMNT)) != NULL)
i++;
}
#endif
}
if ((yc = yang_find((yang_node*)ys, Y_MAX_ELEMENTS, NULL)) != NULL){
} }
break; break;
default: default:
@ -464,9 +491,9 @@ xml_yang_validate_all(cxobj *xt,
if (yc->ys_keyword != Y_MUST) if (yc->ys_keyword != Y_MUST)
continue; continue;
xpath = yc->ys_argument; /* "must" has xpath argument */ xpath = yc->ys_argument; /* "must" has xpath argument */
if ((b = xpath_vec_bool(xt, "%s", xpath)) < 0) if ((nr = xpath_vec_bool(xt, "%s", xpath)) < 0)
goto done; goto done;
if (!b){ if (!nr){
if ((ye = yang_find((yang_node*)yc, Y_ERROR_MESSAGE, NULL)) != NULL) if ((ye = yang_find((yang_node*)yc, Y_ERROR_MESSAGE, NULL)) != NULL)
clicon_err(OE_DB, 0, "%s", ye->ys_argument); clicon_err(OE_DB, 0, "%s", ye->ys_argument);
else else
@ -477,9 +504,9 @@ xml_yang_validate_all(cxobj *xt,
/* "when" sub-node RFC 7950 Sec 7.21.5. Can only be one. */ /* "when" sub-node RFC 7950 Sec 7.21.5. Can only be one. */
if ((yc = yang_find((yang_node*)ys, Y_WHEN, NULL)) != NULL){ if ((yc = yang_find((yang_node*)ys, Y_WHEN, NULL)) != NULL){
xpath = yc->ys_argument; /* "when" has xpath argument */ xpath = yc->ys_argument; /* "when" has xpath argument */
if ((b = xpath_vec_bool(xt, "%s", xpath)) < 0) if ((nr = xpath_vec_bool(xt, "%s", xpath)) < 0)
goto done; goto done;
if (!b){ if (!nr){
clicon_err(OE_DB, 0, "xpath %s validation failed", xml_name(xt)); clicon_err(OE_DB, 0, "xpath %s validation failed", xml_name(xt));
goto done; goto done;
} }

129
test/test_list.sh Executable file
View file

@ -0,0 +1,129 @@
#!/bin/bash
# Yang list / leaf-list operations. min/max-elements
APPNAME=example
# include err() and new() functions and creates $dir
. ./lib.sh
exit 0 # NYI
cfg=$dir/conf_yang.xml
fyang=$dir/test.yang
cat <<EOF > $cfg
<config>
<CLICON_CONFIGFILE>$cfg</CLICON_CONFIGFILE>
<CLICON_YANG_DIR>/usr/local/share/$APPNAME/yang</CLICON_YANG_DIR>
<CLICON_YANG_MODULE_MAIN>$APPNAME</CLICON_YANG_MODULE_MAIN>
<CLICON_CLISPEC_DIR>/usr/local/lib/$APPNAME/clispec</CLICON_CLISPEC_DIR>
<CLICON_CLI_DIR>/usr/local/lib/$APPNAME/cli</CLICON_CLI_DIR>
<CLICON_CLI_MODE>$APPNAME</CLICON_CLI_MODE>
<CLICON_SOCK>/usr/local/var/$APPNAME/$APPNAME.sock</CLICON_SOCK>
<CLICON_BACKEND_PIDFILE>/usr/local/var/$APPNAME/$APPNAME.pidfile</CLICON_BACKEND_PIDFILE>
<CLICON_CLI_GENMODEL_COMPLETION>1</CLICON_CLI_GENMODEL_COMPLETION>
<CLICON_XMLDB_DIR>/usr/local/var/$APPNAME</CLICON_XMLDB_DIR>
<CLICON_XMLDB_PLUGIN>/usr/local/lib/xmldb/text.so</CLICON_XMLDB_PLUGIN>
</config>
EOF
cat <<EOF > $fyang
module $APPNAME{
container c{
presence true;
list a0{
key k;
leaf k {
type string;
}
min-elements 0;
max-elements "unbounded";
}
list a1{
key k;
leaf k {
type string;
}
description "There must be at least one of these";
min-elements 1;
max-elements 2;
}
leaf-list b0{
type string;
min-elements 0;
max-elements "unbounded";
}
leaf-list b1{
description "There must be at least one of these";
type string;
min-elements 1;
max-elements 2;
}
}
}
EOF
# kill old backend (if any)
new "kill old backend"
sudo clixon_backend -zf $cfg -y $fyang
if [ $? -ne 0 ]; then
err
fi
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
err
fi
new "minmax: minimal"
expecteof "$clixon_netconf -qf $cfg -y $fyang" 0 "<rpc><edit-config><target><candidate/></target><default-operation>replace</default-operation><config><c><a1><k>0</k></a1><b1>0</b1></c></config></edit-config></rpc>]]>]]>" "^<rpc-reply><ok/></rpc-reply>]]>]]>$"
new "minmax: minimal validate ok"
expecteof "$clixon_netconf -qf $cfg -y $fyang" 0 "<rpc><validate><source><candidate/></source></validate></rpc>]]>]]>" "^<rpc-reply><ok/></rpc-reply>]]>]]>$"
new "minmax: maximal"
expecteof "$clixon_netconf -qf $cfg -y $fyang" 0 "<rpc><edit-config><target><candidate/></target><default-operation>replace</default-operation><config><c><a0><k>0</k></a0><a0><k>1</k></a0><a0><k>unbounded</k></a0><a1><k>0</k></a1><a1><k>1</k></a1><b0>0</b0><b0>1</b0><b0>unbounded</b0><b1>0</b1><b0>1</b0></c></config></edit-config></rpc>]]>]]>" "^<rpc-reply><ok/></rpc-reply>]]>]]>$"
new "minmax: validate ok"
expecteof "$clixon_netconf -qf $cfg -y $fyang" 0 "<rpc><validate><source><candidate/></source></validate></rpc>]]>]]>" "^<rpc-reply><ok/></rpc-reply>]]>]]>$"
new "minmax: empty"
expecteof "$clixon_netconf -qf $cfg -y $fyang" 0 "<rpc><edit-config><target><candidate/></target><default-operation>replace</default-operation><config><c/></config></edit-config></rpc>]]>]]>" "^<rpc-reply><ok/></rpc-reply>]]>]]>$"
new "minmax: validate should fail"
expecteof "$clixon_netconf -qf $cfg -y $fyang" 0 "<rpc><validate><source><candidate/></source></validate></rpc>]]>]]>" "^<rpc-reply><rpc-error/></rpc-reply>]]>]]>$"
new "minmax: no list"
expecteof "$clixon_netconf -qf $cfg -y $fyang" 0 "<rpc><edit-config><target><candidate/></target><default-operation>replace</default-operation><config><c><b1>0</b1></c></config></edit-config></rpc>]]>]]>" "^<rpc-reply><ok/></rpc-reply>]]>]]>$"
new "minmax: validate should fail"
expecteof "$clixon_netconf -qf $cfg -y $fyang" 0 "<rpc><validate><source><candidate/></source></validate></rpc>]]>]]>" "^<rpc-reply><rpc-error/></rpc-reply>]]>]]>$"
new "minmax: no leaf-list"
expecteof "$clixon_netconf -qf $cfg -y $fyang" 0 "<rpc><edit-config><target><candidate/></target><default-operation>replace</default-operation><config><c><a1><k>0</k></a1></c></config></edit-config></rpc>]]>]]>" "^<rpc-reply><ok/></rpc-reply>]]>]]>$"
new "minmax: validate should fail"
expecteof "$clixon_netconf -qf $cfg -y $fyang" 0 "<rpc><validate><source><candidate/></source></validate></rpc>]]>]]>" "^<rpc-reply><rpc-error/></rpc-reply>]]>]]>$"
new "minmax: Too large list"
expecteof "$clixon_netconf -qf $cfg -y $fyang" 0 "<rpc><edit-config><target><candidate/></target><default-operation>replace</default-operation><config><c><a1><k>0</k></a1><a1><k>1</k></a1><a1><k>2</k></a1><b1>0</b1></c></config></edit-config></rpc>]]>]]>" "^<rpc-reply><ok/></rpc-reply>]]>]]>$"
new "minmax: validate should fail"
expecteof "$clixon_netconf -qf $cfg -y $fyang" 0 "<rpc><validate><source><candidate/></source></validate></rpc>]]>]]>" "^<rpc-reply><rpc-error/></rpc-reply>]]>]]>$"
new "minmax: Too large leaf-list"
expecteof "$clixon_netconf -qf $cfg -y $fyang" 0 "<rpc><edit-config><target><candidate/></target><default-operation>replace</default-operation><config><c><a1><k>0</k></a1><b1>0</b1><b1>1</b1><b1>2</b1></c></config></edit-config></rpc>]]>]]>" "^<rpc-reply><ok/></rpc-reply>]]>]]>$"
new "minmax: validate should fail"
expecteof "$clixon_netconf -qf $cfg -y $fyang" 0 "<rpc><validate><source><candidate/></source></validate></rpc>]]>]]>" "^<rpc-reply><rpc-error/></rpc-reply>]]>]]>$"
# kill backend
sudo clixon_backend -zf $cfg
if [ $? -ne 0 ]; then
err "kill backend"
fi
rm -rf $dir