From 0cf87c75a9615ae27bf3132df9255916014ad0cc Mon Sep 17 00:00:00 2001 From: Olof hagsand Date: Thu, 19 May 2022 15:40:53 +0200 Subject: [PATCH] SNMP frontend: Changed yang2xpath to us string, traverse only datanodes --- apps/snmp/snmp_lib.c | 49 ++++++++++++++++++++++++++++++------ apps/snmp/snmp_lib.h | 2 +- apps/snmp/snmp_mib_yang.c | 52 ++++++++++++++++----------------------- 3 files changed, 63 insertions(+), 40 deletions(-) diff --git a/apps/snmp/snmp_lib.c b/apps/snmp/snmp_lib.c index 9fedccc1..c88c8263 100644 --- a/apps/snmp/snmp_lib.c +++ b/apps/snmp/snmp_lib.c @@ -232,19 +232,17 @@ type_yang2snmp(char *valstr, goto done; } -/*! Construct an xpath from yang statement +/*! Construct an xpath from yang statement, internal fn using cb * Recursively construct it to the top. * @param[in] ys Yang statement * @param[out] cb xpath as cbuf * @retval 0 OK * @retval -1 Error - * @note - * 1. This should really be in a core .c file, like clixon_yang, BUT - * 2. It is far from complete so maybe keep it here as a special case + * @see yang2xpath */ -int -yang2xpath(yang_stmt *ys, - cbuf *cb) +static int +yang2xpath_cb(yang_stmt *ys, + cbuf *cb) { yang_stmt *yp; /* parent */ int i; @@ -259,7 +257,7 @@ yang2xpath(yang_stmt *ys, yang_keyword_get(yp) != Y_MODULE && yang_keyword_get(yp) != Y_SUBMODULE){ - if (yang2xpath(yp, cb) < 0) /* recursive call */ + if (yang2xpath_cb(yp, cb) < 0) /* recursive call */ goto done; if (yang_keyword_get(yp) != Y_CHOICE && yang_keyword_get(yp) != Y_CASE){ cprintf(cb, "/"); @@ -290,3 +288,38 @@ yang2xpath(yang_stmt *ys, done: return retval; } + +/*! Construct an xpath from yang statement + + * Recursively construct it to the top. + * @param[in] ys Yang statement + * @param[out] xpath Malloced xpath string, use free() after use + * @retval 0 OK + * @retval -1 Error + * @note + * 1. This should really be in a core .c file, like clixon_yang, BUT + * 2. It is far from complete so maybe keep it here as a special case + */ +int +yang2xpath(yang_stmt *ys, + char **xpath) +{ + int retval = -1; + cbuf *cb = NULL; + + if ((cb = cbuf_new()) == NULL){ + clicon_err(OE_UNIX, errno, "cbuf_new"); + goto done; + } + if (yang2xpath_cb(ys, cb) < 0) + goto done; + if (xpath && (*xpath = strdup(cbuf_get(cb))) == NULL){ + clicon_err(OE_UNIX, errno, "strdup"); + goto done; + } + retval = 0; + done: + if (cb) + cbuf_free(cb); + return retval; +} diff --git a/apps/snmp/snmp_lib.h b/apps/snmp/snmp_lib.h index a2d3b7c0..e5ed35f2 100644 --- a/apps/snmp/snmp_lib.h +++ b/apps/snmp/snmp_lib.h @@ -49,7 +49,7 @@ int yang2snmp_types(yang_stmt *ys, int *asn1_type, enum cv_type *cvtype); int type_yang2snmp(char *valstr, enum cv_type cvtype, netsnmp_agent_request_info *reqinfo, netsnmp_request_info *requests, u_char **snmpval, size_t *snmplen); -int yang2xpath(yang_stmt *ys, cbuf *cb); +int yang2xpath(yang_stmt *ys, char **xpath); #endif /* _SNMP_LIB_H_ */ diff --git a/apps/snmp/snmp_mib_yang.c b/apps/snmp/snmp_mib_yang.c index 48a3ee2b..60c373f6 100644 --- a/apps/snmp/snmp_mib_yang.c +++ b/apps/snmp/snmp_mib_yang.c @@ -99,7 +99,6 @@ int clixon_table_create(netsnmp_table_data_set *table, yang_stmt *ys, clicon_han { cvec *nsc = NULL; cxobj *xt = NULL; - cbuf *cb = NULL; cxobj *xerr; char *xpath; cxobj *xtable; @@ -113,16 +112,9 @@ int clixon_table_create(netsnmp_table_data_set *table, yang_stmt *ys, clicon_han if (xml_nsctx_yang(ys, &nsc) < 0) goto done; - /* XXX just for yang2xpath */ - if ((cb = cbuf_new()) == NULL){ - clicon_err(OE_UNIX, errno, "cbuf_new"); - goto done; - } - - if (yang2xpath(ys, cb) < 0) + if (yang2xpath(ys, &xpath) < 0) goto done; - xpath = cbuf_get(cb); if (clicon_rpc_get(h, xpath, nsc, CONTENT_ALL, -1, &xt) < 0) goto done; @@ -165,8 +157,6 @@ done: xml_free(xt); if (nsc) xml_nsctx_free(nsc); - if (cb) - cbuf_free(cb); return retval; } @@ -247,13 +237,12 @@ snmp_scalar_handler(netsnmp_mib_handler *handler, clixon_snmp_handle *sh; yang_stmt *ys; clicon_handle h; - cbuf *cb = NULL; cg_var *cv = NULL; cxobj *xt = NULL; cxobj *xerr; cvec *nsc = NULL; cxobj *x; - char *xpath; + char *xpath = NULL; int asn1_type; enum cv_type cvtype; char *valstr; @@ -261,6 +250,7 @@ snmp_scalar_handler(netsnmp_mib_handler *handler, size_t snmplen; int ret; netsnmp_variable_list *requestvb; /* sub of requests */ + cbuf *cb = NULL; /* * can be used to pass information on a per-pdu basis from a @@ -318,14 +308,8 @@ snmp_scalar_handler(netsnmp_mib_handler *handler, if (xml_nsctx_yang(ys, &nsc) < 0) goto done; - /* XXX just for yang2xpath */ - if ((cb = cbuf_new()) == NULL){ - clicon_err(OE_UNIX, errno, "cbuf_new"); + if (yang2xpath(ys, &xpath) < 0) goto done; - } - if (yang2xpath(ys, cb) < 0) - goto done; - xpath = cbuf_get(cb); if (clicon_rpc_get(h, xpath, nsc, CONTENT_ALL, -1, &xt) < 0) goto done; if ((xerr = xpath_first(xt, NULL, "/rpc-error")) != NULL){ @@ -414,14 +398,16 @@ snmp_scalar_handler(netsnmp_mib_handler *handler, done: if (snmpval) free(snmpval); + if (cb) + cbuf_free(cb); + if (xpath) + free(xpath); if (xt) xml_free(xt); if (nsc) xml_nsctx_free(nsc); if (cv) cv_free(cv); - if (cb) - cbuf_free(cb); return retval; } @@ -634,8 +620,10 @@ mib_traverse(clicon_handle h, yang_stmt *ys = NULL; yang_stmt *yp; int ret; - - switch(yang_keyword_get(yn)){ + enum rfc_6020 keyw; + + keyw = yang_keyword_get(yn); + switch(keyw){ case Y_LEAF: if (mib_yang_leaf(h, yn) < 0) goto done; @@ -653,15 +641,17 @@ mib_traverse(clicon_handle h, default: break; } + /* Traverse data nodes in tree (module is special case */ ys = NULL; - while ((ys = yn_each(yn, ys)) != NULL) { - if ((ret = mib_traverse(h, ys)) < 0) - goto done; - if (ret > 0){ - retval = ret; - goto done; + if (yang_schemanode(yn) || keyw == Y_MODULE|| keyw == Y_SUBMODULE) + while ((ys = yn_each(yn, ys)) != NULL) { + if ((ret = mib_traverse(h, ys)) < 0) + goto done; + if (ret > 0){ + retval = ret; + goto done; + } } - } ok: retval = 0; done: