Refactor enum2int API

This commit is contained in:
Olof hagsand 2024-01-29 10:59:04 +01:00
parent 99e9b89056
commit 9ecc7e0791
4 changed files with 44 additions and 34 deletions

View file

@ -658,10 +658,7 @@ snmp_table_rowstatus_get(clixon_handle h,
cxobj *xt = NULL; cxobj *xt = NULL;
char *xpath = NULL; char *xpath = NULL;
cxobj *xr; cxobj *xr;
int ret;
char *body; char *body;
char *intstr;
char *reason = NULL;
clixon_debug(CLIXON_DBG_CLIENT, ""); clixon_debug(CLIXON_DBG_CLIENT, "");
/* Prepare backend call by constructing namespace context */ /* Prepare backend call by constructing namespace context */
@ -673,20 +670,8 @@ snmp_table_rowstatus_get(clixon_handle h,
clicon_ptr_get(h, "snmp-rowstatus-tree", (void**)&xt); clicon_ptr_get(h, "snmp-rowstatus-tree", (void**)&xt);
if (xt && (xr = xpath_first(xt, nsc, "%s", xpath)) != NULL && if (xt && (xr = xpath_first(xt, nsc, "%s", xpath)) != NULL &&
(body = xml_body(xr)) != NULL) { (body = xml_body(xr)) != NULL) {
if ((ret = yang_enum2valstr(yrestype, body, &intstr)) < 0) if (yang_enum2int(yrestype, body, rowstatus) < 0)
goto done; goto done;
if (ret == 0){
clixon_debug(CLIXON_DBG_CLIENT, "%s invalid or not found", body);
*rowstatus = 0;
}
else {
if ((ret = parse_int32(intstr, rowstatus, &reason)) < 0)
goto done;
if (ret == 0){
clixon_debug(CLIXON_DBG_CLIENT, "parse_int32: %s", reason);
*rowstatus = 0;
}
}
} }
else else
*rowstatus = 0; *rowstatus = 0;
@ -696,8 +681,6 @@ snmp_table_rowstatus_get(clixon_handle h,
free(xpath); free(xpath);
if (nsc) if (nsc)
xml_nsctx_free(nsc); xml_nsctx_free(nsc);
if (reason)
free(reason);
return retval; return retval;
} }

View file

@ -778,7 +778,7 @@ type_xml2snmp_pre(char *xmlstr0,
{ {
int retval = -1; int retval = -1;
yang_stmt *yrestype; /* resolved type */ yang_stmt *yrestype; /* resolved type */
char *restype = NULL; /* resolved type */ char *restype = NULL; /* resolved type */
char *str = NULL; char *str = NULL;
int ret; int ret;
cbuf *cb = NULL; cbuf *cb = NULL;
@ -809,7 +809,7 @@ type_xml2snmp_pre(char *xmlstr0,
else else
str = "1"; str = "1";
} }
else if( strcmp(restype, "decimal64") == 0 ) { else if (strcmp(restype, "decimal64") == 0 ) {
cg_var* cv = yang_cv_get(ys); cg_var* cv = yang_cv_get(ys);
int64_t num; int64_t num;

View file

@ -69,6 +69,7 @@ int assign_namespace_body(cxobj *x0, cxobj *x1);
int xml_merge(cxobj *x0, cxobj *x1, yang_stmt *yspec, char **reason); int xml_merge(cxobj *x0, cxobj *x1, yang_stmt *yspec, char **reason);
int yang_valstr2enum(yang_stmt *ytype, char *valstr, char **enumstr); int yang_valstr2enum(yang_stmt *ytype, char *valstr, char **enumstr);
int yang_enum2valstr(yang_stmt *ytype, char *enumstr, char **valstr); int yang_enum2valstr(yang_stmt *ytype, char *enumstr, char **valstr);
int yang_enum2int(yang_stmt *ytype, char *enumstr, int32_t *val);
int yang_enum_int_value(cxobj *node, int32_t *val); int yang_enum_int_value(cxobj *node, int32_t *val);
int xml_copy_marked(cxobj *x0, cxobj *x1); int xml_copy_marked(cxobj *x0, cxobj *x1);
int yang_check_when_xpath(cxobj *xn, cxobj *xp, yang_stmt *yn, int *hit, int *nrp, char **xpathp); int yang_check_when_xpath(cxobj *xn, cxobj *xp, yang_stmt *yn, int *hit, int *nrp, char **xpathp);

View file

@ -1511,17 +1511,16 @@ yang_valstr2enum(yang_stmt *ytype,
return retval; return retval;
} }
/*! Given a YANG (enum) type node and a value, return the string containing corresponding int str /*! Given a YANG (enum) type node and an enum name, return the string containing corresponding int str
* *
* Only handles explicit values
* @param[in] ytype YANG type noden * @param[in] ytype YANG type noden
* @param[in] enumstr Value of enum * @param[in] enumstr Value of enum
* @param[out] valstr Corresponding string containing an int (direct pointer, dont free) * @param[out] valstr Corresponding string containing an int (direct pointer, dont free)
* @retval 1 OK, result in valstr * @retval 1 OK, result in valstr
* @retval 0 Invalid, not found * @retval 0 Invalid, not found
* @retval -1 Error * @retval -1 Error
* @note does not handle implicit values
* @see yang_enum2int handles implicit values * @see yang_enum2int handles implicit values
* @note does not handle implicit values, see yang_enum_int_value which does
*/ */
int int
yang_enum2valstr(yang_stmt *ytype, yang_enum2valstr(yang_stmt *ytype,
@ -1550,6 +1549,42 @@ yang_enum2valstr(yang_stmt *ytype,
goto done; goto done;
} }
/*! Given a YANG (enum) type node and an enum name, return the corresponding int
*
* Handles implicit values
* @param[in] ytype YANG type noden
* @param[in] enumstr Value of enum
* @param[out] int32 Int value
* @retval 0 OK, result in val
* @retval -1 Error
*/
int
yang_enum2int(yang_stmt *ytype,
char *enumstr,
int32_t *val)
{
int retval = -1;
yang_stmt *yenum;
cg_var *cv;
if (val == NULL){
clixon_err(OE_UNIX, EINVAL, "val is NULL");
goto done;
}
if ((yenum = yang_find(ytype, Y_ENUM, enumstr)) == NULL){
clixon_err(OE_YANG, 0, "No such enum %s", enumstr);
goto done;
}
if ((cv = yang_cv_get(yenum)) == NULL){
clixon_err(OE_YANG, 0, "Enum lacks cv");
goto done;
}
*val = cv_int32_get(cv);
retval = 0;
done:
return retval;
}
/*! Get integer value from xml node from yang enumeration /*! Get integer value from xml node from yang enumeration
* *
* @param[in] node XML node in a tree * @param[in] node XML node in a tree
@ -1568,13 +1603,11 @@ int
yang_enum_int_value(cxobj *node, yang_enum_int_value(cxobj *node,
int32_t *val) int32_t *val)
{ {
int retval = -1; int retval = -1;
yang_stmt *yspec; yang_stmt *yspec;
yang_stmt *ys; yang_stmt *ys;
yang_stmt *ytype; yang_stmt *ytype;
yang_stmt *yrestype; /* resolved type */ yang_stmt *yrestype; /* resolved type */
yang_stmt *yenum;
cg_var *cv;
if (node == NULL) if (node == NULL)
goto done; goto done;
@ -1593,15 +1626,8 @@ yang_enum_int_value(cxobj *node,
} }
if (strcmp(yang_argument_get(yrestype), "enumeration")) if (strcmp(yang_argument_get(yrestype), "enumeration"))
goto done; goto done;
if ((yenum = yang_find(yrestype, Y_ENUM, xml_body(node))) == NULL){ if (yang_enum2int(yrestype, xml_body(node), val) < 0)
clixon_err(OE_YANG, 0, "No such enum: %s", xml_body(node));
goto done; goto done;
}
if ((cv = yang_cv_get(yenum)) == NULL){
clixon_err(OE_YANG, 0, "Enum lacks cv");
goto done;
}
*val = cv_int32_get(cv);
retval = 0; retval = 0;
done: done:
return retval; return retval;