From 175f9a21360eda42864687258fafd9ddda50f1c6 Mon Sep 17 00:00:00 2001 From: stass Date: Tue, 28 Mar 2023 18:10:21 +0300 Subject: [PATCH 01/15] Support union with are same subtypes with SNMP --- apps/snmp/snmp_lib.c | 61 ++++++++++++- test/test_snmp_union.sh | 184 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 241 insertions(+), 4 deletions(-) create mode 100644 test/test_snmp_union.sh diff --git a/apps/snmp/snmp_lib.c b/apps/snmp/snmp_lib.c index b3f57b12..5043daa3 100644 --- a/apps/snmp/snmp_lib.c +++ b/apps/snmp/snmp_lib.c @@ -155,10 +155,63 @@ static const map_str2str yang_snmp_types[] = { {"uint16", "uint32"}, { NULL, NULL} /* if not found */ }; -char* yang_type_to_snmp(char* yang_type) + + /* A function that checks that all subtypes of the union are the same + * @param[in] ytype Yang resolved type (a union in this case) + * @param[out] cb Buffer where type of subtypes is written + * @retval 1 - true(All subtypes are the same), 0 - false + */ +int is_same_subtypes_union(yang_stmt *ytype, cbuf *cb) { - char* ret = clicon_str2str(yang_snmp_types, yang_type); - return (NULL == ret) ? yang_type : ret; + int retval = 0; + yang_stmt *y_sub_type = NULL; + yang_stmt *y_resolved_type = NULL; /* resolved type */ + char *resolved_type_str; /* resolved type */ + char *type_str = NULL; + + int options = 0; + cvec *cvv = NULL; + cvec *patterns = NULL; + uint8_t fraction_digits = 0; + + /* Loop over all sub-types in the resolved union type, note these are + * not resolved types (unless they are built-in, but the resolve call is + * made in the union_one call. + */ + while ((y_sub_type = yn_each(ytype, y_sub_type)) != NULL) + { + if (yang_keyword_get(y_sub_type) != Y_TYPE) + continue; + + if (yang_type_resolve(ytype, ytype, y_sub_type, + &y_resolved_type, &options, + &cvv, patterns, NULL, &fraction_digits) < 0 || ( NULL == y_resolved_type) ) + break; + if( (NULL == (resolved_type_str = yang_argument_get(y_resolved_type))) ) + break; + if( NULL == type_str || strcmp(type_str, resolved_type_str) == 0) + type_str = resolved_type_str; + else + break; + } + if( NULL == y_sub_type && NULL != type_str ) + { + cbuf_append_str(cb, resolved_type_str); + retval = 1; + } + return retval; +} +char* yang_type_to_snmp(yang_stmt *ytype, char* yang_type_str) +{ + char* type_str = yang_type_str; + if (yang_type_str && strcmp(yang_type_str, "union") == 0) + { + cbuf *cb = cbuf_new(); + if( is_same_subtypes_union(ytype, cb) > 0) + type_str = cbuf_get(cb); + } + char* ret = clicon_str2str(yang_snmp_types, type_str); + return (NULL == ret) ? type_str : ret; } /*! Translate from snmp string to int representation @@ -296,7 +349,7 @@ snmp_yang_type_get(yang_stmt *ys, if (yang_type_get(ys, &origtype, &yrestype, NULL, NULL, NULL, NULL, NULL) < 0) goto done; restype = yrestype?yang_argument_get(yrestype):NULL; - restype = yang_type_to_snmp(restype); + restype = yang_type_to_snmp(restype, restype); if (strcmp(restype, "leafref")==0){ if ((ypath = yang_find(yrestype, Y_PATH, NULL)) == NULL){ clicon_err(OE_YANG, 0, "No path in leafref"); diff --git a/test/test_snmp_union.sh b/test/test_snmp_union.sh new file mode 100644 index 00000000..7eba648a --- /dev/null +++ b/test/test_snmp_union.sh @@ -0,0 +1,184 @@ +#!/usr/bin/env bash +# SNMP test for yang union type with are same types of subtypes + + +# Magic line must be first in script (see README.md) +s="$_" ; . ./lib.sh || if [ "$s" = $0 ]; then exit 0; else return 0; fi + +# Re-use main example backend state callbacks +APPNAME=example + +if [ ${ENABLE_NETSNMP} != "yes" ]; then + echo "Skipping test, Net-SNMP support not enabled." + rm -rf $dir + if [ "$s" = $0 ]; then exit 0; else return 0; fi +fi + +cfg=$dir/conf_startup.xml +fyang=$dir/clixon-example.yang +fstate=$dir/state.xml + +# AgentX unix socket +SOCK=/var/run/snmp.sock + +cat < $cfg + + $cfg + ${YANG_INSTALLDIR} + ${YANG_STANDARD_DIR} + ${MIB_GENERATED_YANG_DIR} + $fyang + $dir/$APPNAME.sock + /usr/local/lib/$APPNAME/backend + /var/tmp/$APPNAME.pidfile + $dir + unix:$SOCK + ENTITY-MIB + true + +EOF + +cat < $fyang +module clixon-example{ + yang-version 1.1; + namespace "urn:example:clixon"; + prefix ex; + + import ietf-yang-smiv2 { + prefix smiv2; + } + typedef first { + type string{ + pattern + "first"; + } + description "first string"; + } + typedef second { + type string{ + pattern + "second"; + } + description "second string"; + } + typedef third { + type string{ + pattern + "third"; + } + description "third string"; + } + + /* Generic config data */ + container table{ + smiv2:oid "1.3.6.1.2.1.47.1.1.1"; + list parameter{ + smiv2:oid "1.3.6.1.2.1.47.1.1.1.1"; + key name; + cx-snmp:table-key "value"; + leaf name{ + type union{ + type ex:first; + type ex:second; + type ex:third; + } + description "name"; + smiv2:oid "1.3.6.1.2.1.47.1.1.1.1.1"; + } + } + } +} +EOF + +# This is state data written to file that backend reads from (on request) + +cat < $fstate + + + first + + + second + + + third + +
+EOF +function testinit(){ + new "test params: -s init -f $cfg -- -sS $fstate" + if [ $BE -ne 0 ]; then + # Kill old backend and start a new one + new "kill old backend" + sudo clixon_backend -zf $cfg + if [ $? -ne 0 ]; then + err "Failed to start backend" + fi + + sudo pkill -f clixon_backend + + new "Starting backend" + start_backend -s init -f $cfg -- -sS $fstate + fi + + new "wait backend" + wait_backend + + if [ $SN -ne 0 ]; then + # Kill old clixon_snmp, if any + new "Terminating any old clixon_snmp processes" + sudo killall -q clixon_snmp + + new "Starting clixon_snmp" + start_snmp $cfg & + fi + + new "wait snmp" + wait_snmp +} + +function testexit(){ + stop_snmp + + 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 +} + +ENTITY_OID=".1.3.6.1.2.1.47.1.1.1" + +# first string, value=first +OID_FIRST="${ENTITY_OID}.1.1.1" +# second string, value=second +OID_SECOND="${ENTITY_OID}.1.1.2" +# third string, value=third +OID_THIRD="${ENTITY_OID}.1.1.3" + +new "SNMP system tests" +testinit + +new "Get index, $OID_FIRST" +validate_oid $OID_FIRST $OID_FIRST "STRING" "first" +new "Get next $OID_FIRST" +validate_oid $OID_FIRST $OID_SECOND "STRING" "second" +new "Get index, $OID_SECOND" +validate_oid $OID_SECOND $OID_SECOND "STRING" "second" +new "Get next $OID_SECOND" +validate_oid $OID_SECOND $OID_THIRD "STRING" "third" +new "Get index, $OID_THIRD" +validate_oid $OID_THIRD $OID_THIRD "STRING" "third" + +new "Cleaning up" +testexit + +rm -rf $dir + +new "endtest" +endtest \ No newline at end of file From c4ca4ed96273efe75311a1b2ddfac6311df8b494 Mon Sep 17 00:00:00 2001 From: stass Date: Tue, 28 Mar 2023 18:35:43 +0300 Subject: [PATCH 02/15] Fix test --- test/test_snmp_union.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_snmp_union.sh b/test/test_snmp_union.sh index 7eba648a..900f51a3 100644 --- a/test/test_snmp_union.sh +++ b/test/test_snmp_union.sh @@ -75,7 +75,7 @@ module clixon-example{ list parameter{ smiv2:oid "1.3.6.1.2.1.47.1.1.1.1"; key name; - cx-snmp:table-key "value"; + leaf name{ type union{ type ex:first; From ba3558bd365fe6b0e9458d1a10c9fc955d913c89 Mon Sep 17 00:00:00 2001 From: stass Date: Tue, 28 Mar 2023 19:17:46 +0300 Subject: [PATCH 03/15] fix test --- test/test_snmp_union.sh | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/test/test_snmp_union.sh b/test/test_snmp_union.sh index 900f51a3..7edab9ae 100644 --- a/test/test_snmp_union.sh +++ b/test/test_snmp_union.sh @@ -1,7 +1,6 @@ #!/usr/bin/env bash # SNMP test for yang union type with are same types of subtypes - # Magic line must be first in script (see README.md) s="$_" ; . ./lib.sh || if [ "$s" = $0 ]; then exit 0; else return 0; fi @@ -33,7 +32,6 @@ cat < $cfg /var/tmp/$APPNAME.pidfile $dir unix:$SOCK - ENTITY-MIB true EOF @@ -75,12 +73,12 @@ module clixon-example{ list parameter{ smiv2:oid "1.3.6.1.2.1.47.1.1.1.1"; key name; - + leaf name{ type union{ - type ex:first; - type ex:second; - type ex:third; + type ex:first; + type ex:second; + type ex:third; } description "name"; smiv2:oid "1.3.6.1.2.1.47.1.1.1.1.1"; From 22b78f0fe6b789f970f488c7d4a5f375f97e0cfa Mon Sep 17 00:00:00 2001 From: stass Date: Wed, 29 Mar 2023 14:25:04 +0300 Subject: [PATCH 04/15] fix test --- test/test_snmp_union.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/test_snmp_union.sh b/test/test_snmp_union.sh index 7edab9ae..0baedf0c 100644 --- a/test/test_snmp_union.sh +++ b/test/test_snmp_union.sh @@ -152,6 +152,8 @@ function testexit(){ ENTITY_OID=".1.3.6.1.2.1.47.1.1.1" +expectpart "$($snmpwalk $ENTITY_OID)" 0 + # first string, value=first OID_FIRST="${ENTITY_OID}.1.1.1" # second string, value=second From 8f240d1edd70600b60ea695a50ea7e948a338b8f Mon Sep 17 00:00:00 2001 From: stass Date: Wed, 29 Mar 2023 14:47:03 +0300 Subject: [PATCH 05/15] fix test --- test/test_snmp_union.sh | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/test/test_snmp_union.sh b/test/test_snmp_union.sh index 0baedf0c..3a90f0c9 100644 --- a/test/test_snmp_union.sh +++ b/test/test_snmp_union.sh @@ -152,7 +152,7 @@ function testexit(){ ENTITY_OID=".1.3.6.1.2.1.47.1.1.1" -expectpart "$($snmpwalk $ENTITY_OID)" 0 +expectpart "$($snmpwalk $ENTITY_OID)" 0 "0.0" # first string, value=first OID_FIRST="${ENTITY_OID}.1.1.1" @@ -164,16 +164,16 @@ OID_THIRD="${ENTITY_OID}.1.1.3" new "SNMP system tests" testinit -new "Get index, $OID_FIRST" -validate_oid $OID_FIRST $OID_FIRST "STRING" "first" -new "Get next $OID_FIRST" -validate_oid $OID_FIRST $OID_SECOND "STRING" "second" -new "Get index, $OID_SECOND" -validate_oid $OID_SECOND $OID_SECOND "STRING" "second" -new "Get next $OID_SECOND" -validate_oid $OID_SECOND $OID_THIRD "STRING" "third" -new "Get index, $OID_THIRD" -validate_oid $OID_THIRD $OID_THIRD "STRING" "third" +# new "Get index, $OID_FIRST" +# validate_oid $OID_FIRST $OID_FIRST "STRING" "first" +# new "Get next $OID_FIRST" +# validate_oid $OID_FIRST $OID_SECOND "STRING" "second" +# new "Get index, $OID_SECOND" +# validate_oid $OID_SECOND $OID_SECOND "STRING" "second" +# new "Get next $OID_SECOND" +# validate_oid $OID_SECOND $OID_THIRD "STRING" "third" +# new "Get index, $OID_THIRD" +# validate_oid $OID_THIRD $OID_THIRD "STRING" "third" new "Cleaning up" testexit From 13ebe0556636dade10a6d2cb28f2690f7dfe058e Mon Sep 17 00:00:00 2001 From: stass Date: Wed, 29 Mar 2023 15:18:11 +0300 Subject: [PATCH 06/15] fix test --- test/test_snmp_union.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_snmp_union.sh b/test/test_snmp_union.sh index 3a90f0c9..11344638 100644 --- a/test/test_snmp_union.sh +++ b/test/test_snmp_union.sh @@ -152,7 +152,7 @@ function testexit(){ ENTITY_OID=".1.3.6.1.2.1.47.1.1.1" -expectpart "$($snmpwalk $ENTITY_OID)" 0 "0.0" +expectpart "$($snmpwalk)" 0 "0.0" # first string, value=first OID_FIRST="${ENTITY_OID}.1.1.1" From efe9f66866e765980c69ed5c431f1a6ca8b5ca55 Mon Sep 17 00:00:00 2001 From: stass Date: Wed, 29 Mar 2023 15:36:34 +0300 Subject: [PATCH 07/15] fix test --- test/test_snmp_union.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_snmp_union.sh b/test/test_snmp_union.sh index 11344638..d076850b 100644 --- a/test/test_snmp_union.sh +++ b/test/test_snmp_union.sh @@ -152,7 +152,7 @@ function testexit(){ ENTITY_OID=".1.3.6.1.2.1.47.1.1.1" -expectpart "$($snmpwalk)" 0 "0.0" +expectpart "$($snmpwalk -Ci .1.3.6)" 0 "0.0" # first string, value=first OID_FIRST="${ENTITY_OID}.1.1.1" From 30db53a2a0793b47a9f03f689c6948476fd74ae2 Mon Sep 17 00:00:00 2001 From: stass Date: Wed, 29 Mar 2023 15:56:38 +0300 Subject: [PATCH 08/15] fix test --- test/test_snmp_union.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_snmp_union.sh b/test/test_snmp_union.sh index d076850b..712e06c0 100644 --- a/test/test_snmp_union.sh +++ b/test/test_snmp_union.sh @@ -152,7 +152,7 @@ function testexit(){ ENTITY_OID=".1.3.6.1.2.1.47.1.1.1" -expectpart "$($snmpwalk -Ci .1.3.6)" 0 "0.0" +expectpart "$($snmpwalk -i .1.3.6)" 0 "0.0" # first string, value=first OID_FIRST="${ENTITY_OID}.1.1.1" From dc14953a1f0300014c12e5e46150249fd1caac2d Mon Sep 17 00:00:00 2001 From: stass Date: Wed, 29 Mar 2023 16:16:36 +0300 Subject: [PATCH 09/15] fix test --- test/test_snmp_union.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/test_snmp_union.sh b/test/test_snmp_union.sh index 712e06c0..449f19d0 100644 --- a/test/test_snmp_union.sh +++ b/test/test_snmp_union.sh @@ -152,7 +152,8 @@ function testexit(){ ENTITY_OID=".1.3.6.1.2.1.47.1.1.1" -expectpart "$($snmpwalk -i .1.3.6)" 0 "0.0" +echo $(snmpwalk -c public -v2c localhost -t 10) +exit 1 # first string, value=first OID_FIRST="${ENTITY_OID}.1.1.1" From 8f3290c41d7c61887d5c73f8e1213b4836954d04 Mon Sep 17 00:00:00 2001 From: stass Date: Wed, 29 Mar 2023 17:26:25 +0300 Subject: [PATCH 10/15] fix test --- test/test_snmp_union.sh | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/test/test_snmp_union.sh b/test/test_snmp_union.sh index 449f19d0..81dfdccb 100644 --- a/test/test_snmp_union.sh +++ b/test/test_snmp_union.sh @@ -152,11 +152,10 @@ function testexit(){ ENTITY_OID=".1.3.6.1.2.1.47.1.1.1" -echo $(snmpwalk -c public -v2c localhost -t 10) -exit 1 +expectpart "$($snmpwalk $ENTITY_OID)" 0 # first string, value=first -OID_FIRST="${ENTITY_OID}.1.1.1" +OID_FIRST="${ENTITY_OID}.1.1" # second string, value=second OID_SECOND="${ENTITY_OID}.1.1.2" # third string, value=third @@ -165,8 +164,8 @@ OID_THIRD="${ENTITY_OID}.1.1.3" new "SNMP system tests" testinit -# new "Get index, $OID_FIRST" -# validate_oid $OID_FIRST $OID_FIRST "STRING" "first" +new "Get index, $OID_FIRST" +validate_oid $OID_FIRST $OID_FIRST "STRING" "1first" # new "Get next $OID_FIRST" # validate_oid $OID_FIRST $OID_SECOND "STRING" "second" # new "Get index, $OID_SECOND" From a5514e051c30229e38cdea26a08b0373f038047c Mon Sep 17 00:00:00 2001 From: stass Date: Wed, 29 Mar 2023 18:05:51 +0300 Subject: [PATCH 11/15] fix test --- test/test_snmp_union.sh | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/test/test_snmp_union.sh b/test/test_snmp_union.sh index 81dfdccb..bb27c01c 100644 --- a/test/test_snmp_union.sh +++ b/test/test_snmp_union.sh @@ -72,8 +72,11 @@ module clixon-example{ smiv2:oid "1.3.6.1.2.1.47.1.1.1"; list parameter{ smiv2:oid "1.3.6.1.2.1.47.1.1.1.1"; - key name; - + key index; + leaf index{ + type int32; + smiv2:oid "1.3.6.1.2.1.47.1.1.1.1.1"; + } leaf name{ type union{ type ex:first; @@ -81,7 +84,7 @@ module clixon-example{ type ex:third; } description "name"; - smiv2:oid "1.3.6.1.2.1.47.1.1.1.1.1"; + smiv2:oid "1.3.6.1.2.1.47.1.1.1.1.2"; } } } @@ -93,13 +96,16 @@ EOF cat < $fstate - first + 1 + first - second + 2 + second - third + 3 + third
EOF @@ -152,10 +158,8 @@ function testexit(){ ENTITY_OID=".1.3.6.1.2.1.47.1.1.1" -expectpart "$($snmpwalk $ENTITY_OID)" 0 - # first string, value=first -OID_FIRST="${ENTITY_OID}.1.1" +OID_FIRST="${ENTITY_OID}.1.1.1" # second string, value=second OID_SECOND="${ENTITY_OID}.1.1.2" # third string, value=third From 7c2ce8de6508a3717c8020ea085215b5353048b1 Mon Sep 17 00:00:00 2001 From: stass Date: Wed, 29 Mar 2023 18:27:19 +0300 Subject: [PATCH 12/15] fix test --- test/test_snmp_union.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/test_snmp_union.sh b/test/test_snmp_union.sh index bb27c01c..688e98ad 100644 --- a/test/test_snmp_union.sh +++ b/test/test_snmp_union.sh @@ -158,6 +158,8 @@ function testexit(){ ENTITY_OID=".1.3.6.1.2.1.47.1.1.1" +echo $($snmpwalk) + # first string, value=first OID_FIRST="${ENTITY_OID}.1.1.1" # second string, value=second From ea40977382f0041675d9ad8de91d1f8b72911909 Mon Sep 17 00:00:00 2001 From: stass Date: Wed, 29 Mar 2023 18:31:50 +0300 Subject: [PATCH 13/15] fix test --- test/test_snmp_union.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_snmp_union.sh b/test/test_snmp_union.sh index 688e98ad..557a3cc4 100644 --- a/test/test_snmp_union.sh +++ b/test/test_snmp_union.sh @@ -158,7 +158,7 @@ function testexit(){ ENTITY_OID=".1.3.6.1.2.1.47.1.1.1" -echo $($snmpwalk) +echo $($snmpwalk) # first string, value=first OID_FIRST="${ENTITY_OID}.1.1.1" From cc951fc3be764ec569c7b1feadc355222bd96d83 Mon Sep 17 00:00:00 2001 From: stass Date: Sun, 2 Apr 2023 14:59:49 +0300 Subject: [PATCH 14/15] Add union test --- apps/snmp/snmp_lib.c | 2 +- test/test_snmp_union.sh | 124 +++++++++++++++++++--------------------- 2 files changed, 60 insertions(+), 66 deletions(-) diff --git a/apps/snmp/snmp_lib.c b/apps/snmp/snmp_lib.c index 5043daa3..fe56b44a 100644 --- a/apps/snmp/snmp_lib.c +++ b/apps/snmp/snmp_lib.c @@ -349,7 +349,7 @@ snmp_yang_type_get(yang_stmt *ys, if (yang_type_get(ys, &origtype, &yrestype, NULL, NULL, NULL, NULL, NULL) < 0) goto done; restype = yrestype?yang_argument_get(yrestype):NULL; - restype = yang_type_to_snmp(restype, restype); + restype = yang_type_to_snmp(yrestype, restype); if (strcmp(restype, "leafref")==0){ if ((ypath = yang_find(yrestype, Y_PATH, NULL)) == NULL){ clicon_err(OE_YANG, 0, "No path in leafref"); diff --git a/test/test_snmp_union.sh b/test/test_snmp_union.sh index 557a3cc4..3aa3abe4 100644 --- a/test/test_snmp_union.sh +++ b/test/test_snmp_union.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -# SNMP test for yang union type with are same types of subtypes +# SNMP test for yang union type with are same types of subtypes. # Magic line must be first in script (see README.md) s="$_" ; . ./lib.sh || if [ "$s" = $0 ]; then exit 0; else return 0; fi @@ -20,6 +20,8 @@ fstate=$dir/state.xml # AgentX unix socket SOCK=/var/run/snmp.sock +# Relies on example_backend.so for $fstate file handling + cat < $cfg $cfg @@ -32,58 +34,38 @@ cat < $cfg /var/tmp/$APPNAME.pidfile $dir unix:$SOCK + clixon-example true EOF cat < $fyang module clixon-example{ - yang-version 1.1; - namespace "urn:example:clixon"; - prefix ex; - - import ietf-yang-smiv2 { - prefix smiv2; - } - typedef first { - type string{ - pattern - "first"; - } - description "first string"; - } - typedef second { - type string{ - pattern - "second"; - } - description "second string"; - } - typedef third { - type string{ - pattern - "third"; - } - description "third string"; - } - - /* Generic config data */ + yang-version 1.1; + namespace "urn:example:clixon"; + prefix ex; + import ietf-yang-smiv2 { + prefix smiv2; + } + /* Generic config data */ container table{ smiv2:oid "1.3.6.1.2.1.47.1.1.1"; list parameter{ smiv2:oid "1.3.6.1.2.1.47.1.1.1.1"; - key index; - leaf index{ + key Index; + + leaf Index{ type int32; smiv2:oid "1.3.6.1.2.1.47.1.1.1.1.1"; } - leaf name{ - type union{ - type ex:first; - type ex:second; - type ex:third; - } - description "name"; + leaf Union_exm{ + description "Union with same subtypes"; + config false; + type union + { + type int32; + type int32; + } smiv2:oid "1.3.6.1.2.1.47.1.1.1.1.2"; } } @@ -92,23 +74,21 @@ module clixon-example{ EOF # This is state data written to file that backend reads from (on request) +# integer and string have values, sleeper does not and uses default (=1) cat < $fstate - 1 - first + 2 + 4 - 2 - second - - - 3 - third + 12 + 14
EOF + function testinit(){ new "test params: -s init -f $cfg -- -sS $fstate" if [ $BE -ne 0 ]; then @@ -158,28 +138,42 @@ function testexit(){ ENTITY_OID=".1.3.6.1.2.1.47.1.1.1" -echo $($snmpwalk) +# name, value=2 +OID1="${ENTITY_OID}.1.1.2" +# name, value=12 +OID2="${ENTITY_OID}.1.1.12" +# value, value=2 +OID3="${ENTITY_OID}.1.2.2" +# value, value=12 +OID4="${ENTITY_OID}.1.2.12" +# stat, value=2 +OIDX="${ENTITY_OID}.1.3.2" +# stat, value=12 +OIDY="${ENTITY_OID}.1.3.12" -# first string, value=first -OID_FIRST="${ENTITY_OID}.1.1.1" -# second string, value=second -OID_SECOND="${ENTITY_OID}.1.1.2" -# third string, value=third -OID_THIRD="${ENTITY_OID}.1.1.3" new "SNMP system tests" testinit + +new "Get index, $OID1" +validate_oid $OID1 $OID1 "INTEGER" "2" -new "Get index, $OID_FIRST" -validate_oid $OID_FIRST $OID_FIRST "STRING" "1first" -# new "Get next $OID_FIRST" -# validate_oid $OID_FIRST $OID_SECOND "STRING" "second" -# new "Get index, $OID_SECOND" -# validate_oid $OID_SECOND $OID_SECOND "STRING" "second" -# new "Get next $OID_SECOND" -# validate_oid $OID_SECOND $OID_THIRD "STRING" "third" -# new "Get index, $OID_THIRD" -# validate_oid $OID_THIRD $OID_THIRD "STRING" "third" +new "Get next $OID1" +validate_oid $OID1 $OID2 "INTEGER" "12" + +new "Get index, $OID2" +validate_oid $OID2 $OID2 "INTEGER" "12" +new "Get next $OID2" +validate_oid $OID2 $OID3 "INTEGER" "4" + +new "Get index, $OID3" +validate_oid $OID3 $OID3 "INTEGER" "4" + +new "Get next $OID4" +validate_oid $OID3 $OID4 "INTEGER" "14" + +new "Get index, $OID4" +validate_oid $OID4 $OID4 "INTEGER" "14" new "Cleaning up" testexit From 2b3dd6e133b3f55989614ac3da41593f1ac30985 Mon Sep 17 00:00:00 2001 From: stass Date: Thu, 27 Apr 2023 15:50:37 +0300 Subject: [PATCH 15/15] Adapted to style guidelines --- apps/snmp/snmp_lib.c | 14 ++++++-------- test/test_snmp_union.sh | 2 ++ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/apps/snmp/snmp_lib.c b/apps/snmp/snmp_lib.c index fe56b44a..c95777fa 100644 --- a/apps/snmp/snmp_lib.c +++ b/apps/snmp/snmp_lib.c @@ -159,7 +159,8 @@ static const map_str2str yang_snmp_types[] = { /* A function that checks that all subtypes of the union are the same * @param[in] ytype Yang resolved type (a union in this case) * @param[out] cb Buffer where type of subtypes is written - * @retval 1 - true(All subtypes are the same), 0 - false + * @retval 1 - true(All subtypes are the same) + * @retval 0 - false */ int is_same_subtypes_union(yang_stmt *ytype, cbuf *cb) { @@ -178,8 +179,7 @@ int is_same_subtypes_union(yang_stmt *ytype, cbuf *cb) * not resolved types (unless they are built-in, but the resolve call is * made in the union_one call. */ - while ((y_sub_type = yn_each(ytype, y_sub_type)) != NULL) - { + while ((y_sub_type = yn_each(ytype, y_sub_type)) != NULL){ if (yang_keyword_get(y_sub_type) != Y_TYPE) continue; @@ -194,8 +194,7 @@ int is_same_subtypes_union(yang_stmt *ytype, cbuf *cb) else break; } - if( NULL == y_sub_type && NULL != type_str ) - { + if (NULL == y_sub_type && NULL != type_str){ cbuf_append_str(cb, resolved_type_str); retval = 1; } @@ -204,10 +203,9 @@ int is_same_subtypes_union(yang_stmt *ytype, cbuf *cb) char* yang_type_to_snmp(yang_stmt *ytype, char* yang_type_str) { char* type_str = yang_type_str; - if (yang_type_str && strcmp(yang_type_str, "union") == 0) - { + if (yang_type_str && strcmp(yang_type_str, "union") == 0){ cbuf *cb = cbuf_new(); - if( is_same_subtypes_union(ytype, cb) > 0) + if (is_same_subtypes_union(ytype, cb) > 0) type_str = cbuf_get(cb); } char* ret = clicon_str2str(yang_snmp_types, type_str); diff --git a/test/test_snmp_union.sh b/test/test_snmp_union.sh index 3aa3abe4..49fab3f4 100644 --- a/test/test_snmp_union.sh +++ b/test/test_snmp_union.sh @@ -57,6 +57,7 @@ module clixon-example{ leaf Index{ type int32; smiv2:oid "1.3.6.1.2.1.47.1.1.1.1.1"; + smiv2:max-access "read-only"; } leaf Union_exm{ description "Union with same subtypes"; @@ -67,6 +68,7 @@ module clixon-example{ type int32; } smiv2:oid "1.3.6.1.2.1.47.1.1.1.1.2"; + smiv2:max-access "read-only"; } } }