Skip to content

Commit

Permalink
Modified YANG deviate+derived solution
Browse files Browse the repository at this point in the history
Changed flag to YANG_FLAG_REFINE
More complete test
  • Loading branch information
olofhagsand committed Aug 16, 2024
1 parent 442e039 commit 39fb149
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 85 deletions.
5 changes: 4 additions & 1 deletion lib/clixon/clixon_yang.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@
* augment/grouping */
#define YANG_FLAG_MYMODULE 0x800 /* Use external map to access my-module for
* UNKNOWNS and augment/grouping */
#define YANG_FLAG_NOORIG 0x1000 /* Node different from orig, do not use orig-link */
#define YANG_FLAG_REFINE 0x1000 /* In derived trees from grouping and augments, this node
* may be different from orig, therefore do not use link to
* original. May also be due to deviations of derived trees
*/

/*
* Types
Expand Down
5 changes: 4 additions & 1 deletion lib/src/clixon_yang.c
Original file line number Diff line number Diff line change
Expand Up @@ -2416,6 +2416,8 @@ yang_deviation(yang_stmt *ys,
/* Make a copy of deviate child and insert. */
if ((yc1 = ys_dup(yc)) == NULL)
goto done;
if (yang_apply(yc1, -1, (yang_applyfn_t*)yang_flag_set, -1, (void*)YANG_FLAG_REFINE) < 0)
goto done;
if (yn_insert(ytarget, yc1) < 0)
goto done;
}
Expand Down Expand Up @@ -2449,7 +2451,8 @@ yang_deviation(yang_stmt *ys,
/* Make a copy of deviate child and insert. */
if ((yc1 = ys_dup(yc)) == NULL)
goto done;
yang_flag_set(yc1, YANG_FLAG_NOORIG);
if (yang_apply(yc1, -1, (yang_applyfn_t*)yang_flag_set, -1, (void*)YANG_FLAG_REFINE) < 0)
goto done;
if (yn_insert(ytarget, yc1) < 0)
goto done;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/clixon_yang_type.c
Original file line number Diff line number Diff line change
Expand Up @@ -1486,7 +1486,7 @@ yang_type_get(yang_stmt *ys,
clixon_err(OE_DB, ENOENT, "mandatory type object is not found");
goto done;
}
if ((yorig = yang_orig_get(ys)) != NULL && yang_flag_get(ytype, YANG_FLAG_NOORIG) == 0){
if ((yorig = yang_orig_get(ys)) != NULL && yang_flag_get(ytype, YANG_FLAG_REFINE) == 0){
ys = yorig;
if ((ytype = yang_find(ys, Y_TYPE, NULL)) == NULL){
clixon_err(OE_DB, ENOENT, "mandatory type object is not found");
Expand Down
50 changes: 0 additions & 50 deletions test/test_openconfig.sh
Original file line number Diff line number Diff line change
Expand Up @@ -81,56 +81,6 @@ EOF
fi
done

# The following test is a special case: A deviation of a grouped type did not appear in
# type resolution
f=$dir/my.yang
cat <<EOF > $f
module my {
namespace "urn:example:clixon";
prefix "ex";
import "openconfig-network-instance" {
prefix "oc-netinst";
}
deviation "/oc-netinst:network-instances/oc-netinst:network-instance/oc-netinst:protocols/oc-netinst:protocol/oc-netinst:isis/oc-netinst:interfaces/oc-netinst:interface/oc-netinst:levels/oc-netinst:level/oc-netinst:afi-safi/oc-netinst:af/oc-netinst:config/oc-netinst:metric" {
description "metric range is restricted";
deviate "replace" {
type "union" {
type "uint32";
type "enumeration" {
enum "MYTEXT";
}
}
default "MYTEXT";
}
}
}
EOF

modname=$(basename $f | awk -F "." '{print $1}')
# Generate autocli for these modules
AUTOCLI=$(autocli_config openconfig-* kw-nokey false)

cat <<EOF > $cfg
<clixon-config xmlns="http://clicon.org/config">
<CLICON_CONFIGFILE>$cfg</CLICON_CONFIGFILE>
<CLICON_FEATURE>ietf-netconf:startup</CLICON_FEATURE>
<CLICON_YANG_DIR>${OPENCONFIG}</CLICON_YANG_DIR>
<CLICON_YANG_DIR>${YANG_INSTALLDIR}</CLICON_YANG_DIR>
<CLICON_YANG_AUGMENT_ACCEPT_BROKEN>true</CLICON_YANG_AUGMENT_ACCEPT_BROKEN>
<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/run/$APPNAME.sock</CLICON_SOCK>
<CLICON_BACKEND_PIDFILE>/usr/local/var/run/$APPNAME.pidfile</CLICON_BACKEND_PIDFILE>
<CLICON_XMLDB_DIR>$dir</CLICON_XMLDB_DIR>
${AUTOCLI}
</clixon-config>
EOF

new "deviation: $clixon_cli -D $DBG -1f $cfg -y $f show version"
expectpart "$($clixon_cli -D $DBG -1f $cfg -y $f show version)" 0 "${CLIXON_VERSION}"

rm -rf $dir

new "endtest"
Expand Down
131 changes: 99 additions & 32 deletions test/test_yang_deviation.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,38 +32,42 @@ EOF

cat <<EOF > $fyangbase
module example-base{
yang-version 1.1;
prefix base;
namespace "urn:example:base";
grouping system-top {
container system {
must "daytime or time"; /* deviate delete removes this */
leaf daytime{ /* deviate not-supported removes this */
type string;
}
leaf time{
type string;
}
list name-server {
max-elements 1; /* deviate replace replaces to "max.elements 3" here */
key name;
leaf name {
type string;
}
}
list user {
key name;
leaf name {
type string;
}
leaf type {
type string;
/* deviate add adds "default admin" here */
}
}
}
}
uses system-top;
yang-version 1.1;
prefix base;
namespace "urn:example:base";
grouping system-top {
container system {
must "daytime or time"; /* deviate delete removes this */
leaf daytime{ /* deviate not-supported removes this */
type string;
}
leaf time{
type string;
}
list name-server {
max-elements 1; /* deviate replace replaces to "max.elements 3" here */
key name;
leaf name {
type string;
}
}
list user {
key name;
leaf name {
type string;
}
leaf type {
type string;
/* deviate add adds "default admin" here */
}
}
leaf my {
/* deviate replaces with int and default -42 */
type uint16;
}
}
}
uses system-top;
}
EOF

Expand Down Expand Up @@ -242,6 +246,69 @@ EOF
new "5. deviate delete"
testrun true false false true

# Replace example from RFC 7950 Sec 7.20.3.3
cat <<EOF > $fyangdev
module example-deviations{
yang-version 1.1;
prefix md;
namespace "urn:example:deviations";
import example-base {
prefix base;
}
deviation /base:system/base:my {
deviate replace {
type int8;
}
}
deviation /base:system/base:my {
deviate add {
default -42;
}
}
}
EOF

new "6. deviate replace default"

if [ "$BE" -ne 0 ]; then
new "kill old backend"
sudo clixon_backend -zf "$cfg"
if [ $? -ne 0 ]; then
err
fi
new "start backend -s init -f $cfg"
start_backend -s init -f "$cfg"
fi

new "wait backend"
wait_backend

new "show default value, expect deviated"
expecteof_netconf "$clixon_netconf -qf $cfg" 0 "$DEFAULTHELLO" "<rpc $DEFAULTNS><get-config><with-defaults xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\">report-all</with-defaults><source><running/></source><filter type=\"xpath\" select=\"/base:system/base:my\" xmlns:base=\"urn:example:base\"/></get-config></rpc>" "" "<rpc-reply $DEFAULTNS><data><system xmlns=\"urn:example:base\"><my>-42</my></system></data></rpc-reply>"

new "set negative value"
expecteof_netconf "$clixon_netconf -qf $cfg" 0 "$DEFAULTHELLO" "<rpc $DEFAULTNS><edit-config><target><candidate/></target><config><system xmlns=\"urn:example:base\"><daytime>Sept17</daytime><my>-77</my></system></config></edit-config></rpc>" "" "<rpc-reply $DEFAULTNS><ok/></rpc-reply>"

new "netconf validate ok"
expecteof_netconf "$clixon_netconf -qf $cfg" 0 "$DEFAULTHELLO" "<rpc $DEFAULTNS><validate><source><candidate/></source></validate></rpc>" "" "<rpc-reply $DEFAULTNS><ok/></rpc-reply>"

new "set large value"
expecteof_netconf "$clixon_netconf -qf $cfg" 0 "$DEFAULTHELLO" "<rpc $DEFAULTNS><edit-config><target><candidate/></target><config><system xmlns=\"urn:example:base\"><my>98735</my></system></config></edit-config></rpc>" "" "<rpc-reply $DEFAULTNS><ok/></rpc-reply>"

new "netconf validate expect fail"
expecteof_netconf "$clixon_netconf -qf $cfg" 0 "$DEFAULTHELLO" "<rpc $DEFAULTNS><validate><source><candidate/></source></validate></rpc>" "" "<rpc-reply $DEFAULTNS><rpc-error><error-type>application</error-type><error-tag>bad-element</error-tag><error-info><bad-element>my</bad-element></error-info><error-severity>error</error-severity><error-message>Number 98735 out of range: -128 - 127</error-message></rpc-error></rpc-reply>"

if [ "$BE" -ne 0 ]; then
new "Kill backend"
# Check if premature kill
pid=$(pgrep -u root -f clixon_backend)
if [ -z "$pid" ]; then
err "backend already dead"
fi
# kill backend
stop_backend -f "$cfg"
fi

rm -rf "$dir"

new "endtest"
Expand Down

0 comments on commit 39fb149

Please sign in to comment.