New function, clixon_table_create which will clear a table and create any rows, indexes and columns to avoid collissions.

This commit is contained in:
Kristofer Hallin 2022-05-17 15:38:04 +02:00 committed by Olof hagsand
parent 38d91a1305
commit 338be43e6a

View file

@ -93,24 +93,8 @@ struct clixon_snmp_handle {
}; };
typedef struct clixon_snmp_handle clixon_snmp_handle; typedef struct clixon_snmp_handle clixon_snmp_handle;
/*! SNMP table operation handlre int clixon_table_create(netsnmp_table_data_set *table, yang_stmt *ys, clicon_handle h)
* Callorder: 161,160,.... 0, 1,2,3, 160,161,...
* see https://net-snmp.sourceforge.io/dev/agent/data_set_8c-example.html#_a0
*/
int
snmp_table_handler(netsnmp_mib_handler *handler,
netsnmp_handler_registration *nhreg,
netsnmp_agent_request_info *reqinfo,
netsnmp_request_info *requests)
{ {
int retval = -1;
clixon_snmp_handle *sh;
netsnmp_table_data_set *table;
netsnmp_table_row *row;
yang_stmt *ys; /* container */
yang_stmt *ylist;
clicon_handle h;
cvec *nsc = NULL; cvec *nsc = NULL;
cxobj *xt = NULL; cxobj *xt = NULL;
cbuf *cb = NULL; cbuf *cb = NULL;
@ -121,35 +105,35 @@ snmp_table_handler(netsnmp_mib_handler *handler,
cxobj *xleaf; cxobj *xleaf;
int i; int i;
char *valstr; char *valstr;
netsnmp_table_row *row, *tmprow;
clicon_debug(1, "%s %s %s", __FUNCTION__,
handler->handler_name,
snmp_msg_int2str(reqinfo->mode));
sh = (clixon_snmp_handle*)nhreg->my_reg_void;
ys = sh->sh_ys;
h = sh->sh_h;
table = sh->sh_table;
if ((ylist = yang_find(ys, Y_LIST, NULL)) == NULL)
goto ok;
switch(reqinfo->mode){
case MODE_GETNEXT: // 160
if (xml_nsctx_yang(ys, &nsc) < 0) if (xml_nsctx_yang(ys, &nsc) < 0)
goto done; goto done;
/* XXX just for yang2xpath */ /* XXX just for yang2xpath */
if ((cb = cbuf_new()) == NULL){ if ((cb = cbuf_new()) == NULL){
clicon_err(OE_UNIX, errno, "cbuf_new"); clicon_err(OE_UNIX, errno, "cbuf_new");
goto done; goto done;
} }
if (yang2xpath(ys, cb) < 0) if (yang2xpath(ys, cb) < 0)
goto done; goto done;
xpath = cbuf_get(cb); xpath = cbuf_get(cb);
if (clicon_rpc_get(h, xpath, nsc, CONTENT_ALL, -1, &xt) < 0) if (clicon_rpc_get(h, xpath, nsc, CONTENT_ALL, -1, &xt) < 0)
goto done; goto done;
if ((xerr = xpath_first(xt, NULL, "/rpc-error")) != NULL){ if ((xerr = xpath_first(xt, NULL, "/rpc-error")) != NULL){
clixon_netconf_error(xerr, "clicon_rpc_get", NULL); clixon_netconf_error(xerr, "clicon_rpc_get", NULL);
goto done; goto done;
} }
netsnmp_table_dataset_add_index(table, ASN_OCTET_STR);
netsnmp_table_set_multi_add_default_row(table, 2, ASN_OCTET_STR, 1, NULL, 0, 3, ASN_OCTET_STR, 1, NULL, 0, 0);
if ((xtable = xpath_first(xt, nsc, "%s", xpath)) != NULL) { if ((xtable = xpath_first(xt, nsc, "%s", xpath)) != NULL) {
for (tmprow = table->table->first_row; tmprow; tmprow = tmprow->next)
netsnmp_table_dataset_remove_and_delete_row(table, tmprow);
xe = NULL; /* Loop thru entries in table */ xe = NULL; /* Loop thru entries in table */
while ((xe = xml_child_each(xtable, xe, CX_ELMNT)) != NULL) { while ((xe = xml_child_each(xtable, xe, CX_ELMNT)) != NULL) {
@ -166,9 +150,57 @@ snmp_table_handler(netsnmp_mib_handler *handler,
} }
i++; i++;
} }
netsnmp_table_dataset_add_row(table, row); netsnmp_table_dataset_add_row(table, row);
} }
} }
done:
if (xt)
xml_free(xt);
if (nsc)
xml_nsctx_free(nsc);
if (cb)
cbuf_free(cb);
return 0;
}
/*! SNMP table operation handlre
* Callorder: 161,160,.... 0, 1,2,3, 160,161,...
* see https://net-snmp.sourceforge.io/dev/agent/data_set_8c-example.html#_a0
*/
int
snmp_table_handler(netsnmp_mib_handler *handler,
netsnmp_handler_registration *nhreg,
netsnmp_agent_request_info *reqinfo,
netsnmp_request_info *requests)
{
int retval = -1;
clixon_snmp_handle *sh;
netsnmp_table_data_set *table;
yang_stmt *ys;
clicon_handle h;
yang_stmt *ylist;
cvec *nsc = NULL;
cxobj *xt = NULL;
cbuf *cb = NULL;
clicon_debug(1, "%s %s %s", __FUNCTION__,
handler->handler_name,
snmp_msg_int2str(reqinfo->mode));
sh = (clixon_snmp_handle*)nhreg->my_reg_void;
ys = sh->sh_ys;
h = sh->sh_h;
table = sh->sh_table;
if ((ylist = yang_find(ys, Y_LIST, NULL)) == NULL)
goto ok;
clixon_table_create(table, ys, h);
switch(reqinfo->mode){
case MODE_GETNEXT: // 160
break; break;
case MODE_GET: // 160 case MODE_GET: // 160
case MODE_SET_RESERVE1: case MODE_SET_RESERVE1:
@ -381,6 +413,7 @@ mib_yang_table(clicon_handle h,
clixon_snmp_handle *sh; clixon_snmp_handle *sh;
int ret; int ret;
/* Get OID from parent container */ /* Get OID from parent container */
if (yang_extension_value(ys, "oid", IETF_YANG_SMIV2_NS, NULL, &oidstr) < 0) if (yang_extension_value(ys, "oid", IETF_YANG_SMIV2_NS, NULL, &oidstr) < 0)
goto done; goto done;
@ -407,8 +440,7 @@ mib_yang_table(clicon_handle h,
sh->sh_ys = ys; sh->sh_ys = ys;
sh->sh_table = table; sh->sh_table = table;
(void)netsnmp_table_dataset_add_index(table, ASN_OCTET_STR); clixon_table_create(table, ys, h);
(void)netsnmp_table_set_multi_add_default_row(table, 2, ASN_OCTET_STR, 1, NULL, 0, 3, ASN_OCTET_STR, 1, NULL, 0, 0);
if ((nhreg = netsnmp_create_handler_registration(name, if ((nhreg = netsnmp_create_handler_registration(name,
snmp_table_handler, snmp_table_handler,