SNMP frontend: Reworked types for GET types translation

YANG enum type, split up enum to int into two functions
This commit is contained in:
Olof hagsand 2022-05-23 09:09:02 +02:00
parent 31fea9e6c8
commit aa95ead1cc
6 changed files with 252 additions and 72 deletions

View file

@ -70,6 +70,7 @@ int xml2xpath(cxobj *x, cvec *nsc, char **xpath);
int assign_namespace_element(cxobj *x0, cxobj *x1, cxobj *x1p);
int assign_namespace_body(cxobj *x0, cxobj *x1);
int xml_merge(cxobj *x0, cxobj *x1, yang_stmt *yspec, char **reason);
int yang_enum2intstr(yang_stmt *ytype, char *value, char **intstr);
int yang_enum_int_value(cxobj *node, int32_t *val);
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);

View file

@ -1810,6 +1810,37 @@ xml_merge(cxobj *x0,
goto done;
}
/*! Given a YANG (enum) type node and a value, return the string containing corresponding int str
*
* @param[in] ytype YANG type noden
* @param[in] valstr Value of enum
* @param[out] intstr Corresponding string containing an int (direct pointer, dont free)
*/
int
yang_enum2intstr(yang_stmt *ytype,
char *valstr,
char **intstr)
{
int retval = -1;
yang_stmt *yenum;
yang_stmt *yval;
if (intstr == NULL){
clicon_err(OE_UNIX, EINVAL, "intstr is NULL");
goto done;
}
if ((yenum = yang_find(ytype, Y_ENUM, valstr)) == NULL)
goto done;
/* Should assign value if yval not found */
if ((yval = yang_find(yenum, Y_VALUE, NULL)) == NULL)
goto done;
*intstr = yang_argument_get(yval);
retval = 0;
done:
return retval;
}
/*! Get integer value from xml node from yang enumeration
* @param[in] node XML node in a tree
* @param[out] val Integer value returned
@ -1832,9 +1863,8 @@ yang_enum_int_value(cxobj *node,
yang_stmt *ys;
yang_stmt *ytype;
yang_stmt *yrestype; /* resolved type */
yang_stmt *yenum;
yang_stmt *yval;
char *reason = NULL;
char *intstr = NULL;
if (node == NULL)
goto done;
@ -1853,20 +1883,16 @@ yang_enum_int_value(cxobj *node,
}
if (yrestype==NULL || strcmp(yang_argument_get(yrestype), "enumeration"))
goto done;
if ((yenum = yang_find(yrestype, Y_ENUM, xml_body(node))) == NULL)
goto done;
/* Should assign value if yval not found */
if ((yval = yang_find(yenum, Y_VALUE, NULL)) == NULL)
if (yang_enum2intstr(yrestype, xml_body(node), &intstr) < 0)
goto done;
/* reason is string containing why int could not be parsed */
if (parse_int32(yang_argument_get(yval), val, &reason) < 0)
if (parse_int32(intstr, val, &reason) < 0)
goto done;
retval = 0;
done:
return retval;
}
/*! Given XML tree x0 with marked nodes, copy marked nodes to new tree x1
* Two marks are used: XML_FLAG_MARK and XML_FLAG_CHANGE
*