Added support for YANG anyxml; Yang union CLI generation and validation; Removed yang string escaping
This commit is contained in:
parent
8fd59d4452
commit
5ae1aeb427
29 changed files with 753 additions and 499 deletions
|
|
@ -97,6 +97,12 @@ clicon_log_init(char *ident,
|
|||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
clicon_get_logflags(void)
|
||||
{
|
||||
return _logflags;
|
||||
}
|
||||
|
||||
/*! Register log callback, return old setting
|
||||
*/
|
||||
clicon_log_notify_t *
|
||||
|
|
@ -153,7 +159,7 @@ slogtime(void)
|
|||
/*! Make a logging call to syslog (or stderr).
|
||||
*
|
||||
* This is the _only_ place the actual syslog (or stderr) logging is made in clicon,..
|
||||
* @note yslog makes itw own filtering, but if log to stderr we do it here
|
||||
* @note syslog makes itw own filtering, but if log to stderr we do it here
|
||||
* @see clicon_debug()
|
||||
*
|
||||
* @param[in] level log level, eg LOG_DEBUG,LOG_INFO,...,LOG_EMERG. Thisis OR:d with facility == LOG_USER
|
||||
|
|
|
|||
|
|
@ -392,7 +392,6 @@ clicon_configfile(clicon_handle h)
|
|||
return clicon_option_str(h, "CLICON_CONFIGFILE");
|
||||
}
|
||||
|
||||
|
||||
/*! YANG database specification directory */
|
||||
char *
|
||||
clicon_yang_dir(clicon_handle h)
|
||||
|
|
@ -400,7 +399,7 @@ clicon_yang_dir(clicon_handle h)
|
|||
return clicon_option_str(h, "CLICON_YANG_DIR");
|
||||
}
|
||||
|
||||
/*! YANG main module */
|
||||
/*! YANG main module or absolute file name */
|
||||
char *
|
||||
clicon_yang_module_main(clicon_handle h)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -492,7 +492,7 @@ clicon_rpc(int s,
|
|||
char **ret)
|
||||
{
|
||||
int retval = -1;
|
||||
struct clicon_msg *reply;
|
||||
struct clicon_msg *reply = NULL;
|
||||
int eof;
|
||||
char *data = NULL;
|
||||
cxobj *cx = NULL;
|
||||
|
|
|
|||
|
|
@ -351,6 +351,23 @@ xml_child_nr(cxobj *xn)
|
|||
return xn->x_childvec_len;
|
||||
}
|
||||
|
||||
/*! Get number of children of specific type
|
||||
* @param[in] xn xml node
|
||||
* @param[in] type XML type or -1 for all
|
||||
* @retval number of typed children in XML tree
|
||||
*/
|
||||
int
|
||||
xml_child_nr_type(cxobj *xn,
|
||||
enum cxobj_type type)
|
||||
{
|
||||
cxobj *x = NULL;
|
||||
int len = 0;
|
||||
|
||||
while ((x = xml_child_each(xn, x, type)) != NULL)
|
||||
len++;
|
||||
return len;
|
||||
}
|
||||
|
||||
/*! Get a specific child
|
||||
* @param[in] xn xml node
|
||||
* @param[in] i the number of the child, eg order in children vector
|
||||
|
|
@ -516,6 +533,9 @@ xml_new_spec(char *name,
|
|||
return x;
|
||||
}
|
||||
|
||||
/*! Return yang spec of node.
|
||||
* Not necessarily set. Either has not been set yet (by xml_spec_set( or anyxml.
|
||||
*/
|
||||
void *
|
||||
xml_spec(cxobj *x)
|
||||
{
|
||||
|
|
@ -939,7 +959,7 @@ clicon_xml2cbuf(cbuf *cb,
|
|||
while ((xc = xml_child_each(x, xc, CX_ATTR)) != NULL)
|
||||
clicon_xml2cbuf(cb, xc, level+1, prettyprint);
|
||||
/* Check for special case <a/> instead of <a></a> */
|
||||
if (xml_body(x)==NULL && xml_child_nr(x)==0)
|
||||
if (xml_body(x)==NULL && xml_child_nr_type(x, CX_ELMNT)==0)
|
||||
cprintf(cb, "/>");
|
||||
else{
|
||||
cprintf(cb, ">");
|
||||
|
|
|
|||
|
|
@ -340,50 +340,51 @@ xml_yang_validate_add(cxobj *xt,
|
|||
char *body;
|
||||
|
||||
/* if not given by argument (overide) use default link */
|
||||
ys = xml_spec(xt);
|
||||
switch (ys->ys_keyword){
|
||||
case Y_LIST:
|
||||
/* fall thru */
|
||||
case Y_CONTAINER:
|
||||
for (i=0; i<ys->ys_len; i++){
|
||||
yc = ys->ys_stmt[i];
|
||||
if (yc->ys_keyword != Y_LEAF)
|
||||
continue;
|
||||
if (yang_mandatory(yc) && xml_find(xt, yc->ys_argument)==NULL){
|
||||
clicon_err(OE_CFG, 0,"Missing mandatory variable: %s",
|
||||
yc->ys_argument);
|
||||
if ((ys = xml_spec(xt)) != NULL){
|
||||
switch (ys->ys_keyword){
|
||||
case Y_LIST:
|
||||
/* fall thru */
|
||||
case Y_CONTAINER:
|
||||
for (i=0; i<ys->ys_len; i++){
|
||||
yc = ys->ys_stmt[i];
|
||||
if (yc->ys_keyword != Y_LEAF)
|
||||
continue;
|
||||
if (yang_mandatory(yc) && xml_find(xt, yc->ys_argument)==NULL){
|
||||
clicon_err(OE_CFG, 0,"Missing mandatory variable: %s",
|
||||
yc->ys_argument);
|
||||
goto done;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case Y_LEAF:
|
||||
/* fall thru */
|
||||
case Y_LEAF_LIST:
|
||||
/* validate value against ranges, etc */
|
||||
if ((cv = cv_dup(ys->ys_cv)) == NULL){
|
||||
clicon_err(OE_UNIX, errno, "cv_dup");
|
||||
goto done;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case Y_LEAF:
|
||||
/* fall thru */
|
||||
case Y_LEAF_LIST:
|
||||
/* validate value against ranges, etc */
|
||||
if ((cv = cv_dup(ys->ys_cv)) == NULL){
|
||||
clicon_err(OE_UNIX, errno, "cv_dup");
|
||||
goto done;
|
||||
}
|
||||
/* In the union case, value is parsed as generic REST type,
|
||||
* needs to be reparsed when concrete type is selected
|
||||
*/
|
||||
if ((body = xml_body(xt)) != NULL){
|
||||
if (cv_parse(body, cv) <0){
|
||||
clicon_err(OE_UNIX, errno, "cv_parse");
|
||||
goto done;
|
||||
}
|
||||
if ((ys_cv_validate(cv, ys, &reason)) != 1){
|
||||
clicon_err(OE_DB, 0,
|
||||
"validation of %s failed %s",
|
||||
ys->ys_argument, reason?reason:"");
|
||||
if (reason)
|
||||
free(reason);
|
||||
goto done;
|
||||
/* In the union case, value is parsed as generic REST type,
|
||||
* needs to be reparsed when concrete type is selected
|
||||
*/
|
||||
if ((body = xml_body(xt)) != NULL){
|
||||
if (cv_parse(body, cv) <0){
|
||||
clicon_err(OE_UNIX, errno, "cv_parse");
|
||||
goto done;
|
||||
}
|
||||
if ((ys_cv_validate(cv, ys, &reason)) != 1){
|
||||
clicon_err(OE_DB, 0,
|
||||
"validation of %s failed %s",
|
||||
ys->ys_argument, reason?reason:"");
|
||||
if (reason)
|
||||
free(reason);
|
||||
goto done;
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
retval = 0;
|
||||
done:
|
||||
|
|
@ -408,21 +409,22 @@ xml_yang_validate_all(cxobj *xt,
|
|||
yang_stmt *ytype;
|
||||
|
||||
/* if not given by argument (overide) use default link */
|
||||
ys = xml_spec(xt);
|
||||
switch (ys->ys_keyword){
|
||||
case Y_LEAF:
|
||||
/* fall thru */
|
||||
case Y_LEAF_LIST:
|
||||
/* Special case if leaf is leafref, then first check against
|
||||
current xml tree
|
||||
*/
|
||||
if ((ytype = yang_find((yang_node*)ys, Y_TYPE, NULL)) != NULL &&
|
||||
strcmp(ytype->ys_argument, "leafref") == 0)
|
||||
if (validate_leafref(xt, ytype) < 0)
|
||||
goto done;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
if ((ys = xml_spec(xt)) != NULL){
|
||||
switch (ys->ys_keyword){
|
||||
case Y_LEAF:
|
||||
/* fall thru */
|
||||
case Y_LEAF_LIST:
|
||||
/* Special case if leaf is leafref, then first check against
|
||||
current xml tree
|
||||
*/
|
||||
if ((ytype = yang_find((yang_node*)ys, Y_TYPE, NULL)) != NULL &&
|
||||
strcmp(ytype->ys_argument, "leafref") == 0)
|
||||
if (validate_leafref(xt, ytype) < 0)
|
||||
goto done;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
retval = 0;
|
||||
done:
|
||||
|
|
@ -477,7 +479,7 @@ xml2cvec(cxobj *xt,
|
|||
/* Go through all children of the xml tree */
|
||||
while ((xc = xml_child_each(xt, xc, CX_ELMNT)) != NULL){
|
||||
name = xml_name(xc);
|
||||
if ((ys = yang_find_syntax((yang_node*)yt, name)) == NULL){
|
||||
if ((ys = yang_find_datanode((yang_node*)yt, name)) == NULL){
|
||||
clicon_debug(0, "%s: yang sanity problem: %s in xml but not present in yang under %s",
|
||||
__FUNCTION__, name, yt->ys_argument);
|
||||
if ((body = xml_body(xc)) != NULL){
|
||||
|
|
@ -592,11 +594,14 @@ xml_is_body(cxobj *xt,
|
|||
char *val)
|
||||
{
|
||||
cxobj *x;
|
||||
char *bx;
|
||||
|
||||
x = NULL;
|
||||
while ((x = xml_child_each(xt, x, CX_ELMNT)) != NULL) {
|
||||
if (strcmp(name, xml_name(x)))
|
||||
continue;
|
||||
if ((bx = xml_body(x)) == NULL)
|
||||
continue;
|
||||
if (strcmp(xml_body(x), val) == 0)
|
||||
return 1;
|
||||
}
|
||||
|
|
@ -641,7 +646,7 @@ xml_diff1(yang_stmt *ys,
|
|||
if (ys->ys_keyword == Y_SPEC)
|
||||
y = yang_find_topnode((yang_spec*)ys, name);
|
||||
else
|
||||
y = yang_find_syntax((yang_node*)ys, name);
|
||||
y = yang_find_datanode((yang_node*)ys, name);
|
||||
if (y == NULL){
|
||||
clicon_err(OE_UNIX, errno, "No yang node found: %s", name);
|
||||
goto done;
|
||||
|
|
@ -719,6 +724,10 @@ xml_diff1(yang_stmt *ys,
|
|||
goto done;
|
||||
break;
|
||||
}
|
||||
body1 = xml_body(x1);
|
||||
body2 = xml_body(x2);
|
||||
if (body1 == NULL || body2 == NULL) /* empty type */
|
||||
break;
|
||||
if (strcmp(xml_body(x1), xml_body(x2))){
|
||||
if (cxvec_append(x1, changed1, changedlen) < 0)
|
||||
goto done;
|
||||
|
|
@ -728,7 +737,8 @@ xml_diff1(yang_stmt *ys,
|
|||
}
|
||||
break;
|
||||
case Y_LEAF_LIST:
|
||||
body1 = xml_body(x1);
|
||||
if ((body1 = xml_body(x1)) == NULL)
|
||||
continue;
|
||||
if (!xml_is_body(xt2, name, body1)) /* where body is */
|
||||
if (cxvec_append(x1, first, firstlen) < 0)
|
||||
goto done;
|
||||
|
|
@ -746,7 +756,7 @@ xml_diff1(yang_stmt *ys,
|
|||
if (ys->ys_keyword == Y_SPEC)
|
||||
y = yang_find_topnode((yang_spec*)ys, name);
|
||||
else
|
||||
y = yang_find_syntax((yang_node*)ys, name);
|
||||
y = yang_find_datanode((yang_node*)ys, name);
|
||||
if (y == NULL){
|
||||
clicon_err(OE_UNIX, errno, "No yang node found: %s", name);
|
||||
goto done;
|
||||
|
|
@ -1197,7 +1207,7 @@ xml_tree_prune_flagged_sub(cxobj *xt,
|
|||
yang_node *yt;
|
||||
|
||||
mark = 0;
|
||||
yt = xml_spec(xt);
|
||||
yt = xml_spec(xt); /* xan be null */
|
||||
x = NULL;
|
||||
xprev = x = NULL;
|
||||
while ((x = xml_child_each(xt, x, CX_ELMNT)) != NULL) {
|
||||
|
|
@ -1306,7 +1316,6 @@ xml_default(cxobj *xt,
|
|||
char *str;
|
||||
|
||||
if ((ys = (yang_stmt*)xml_spec(xt)) == NULL){
|
||||
clicon_log(LOG_WARNING, "%s: no xml_spec(%s)", __FUNCTION__, xml_name(xt));
|
||||
retval = 0;
|
||||
goto done;
|
||||
}
|
||||
|
|
@ -1359,7 +1368,6 @@ xml_order(cxobj *xt,
|
|||
char *xname; /* xml child name */
|
||||
|
||||
if ((y = (yang_stmt*)xml_spec(xt)) == NULL){
|
||||
clicon_log(LOG_WARNING, "%s: no xml_spec(%s)", __FUNCTION__, xml_name(xt));
|
||||
retval = 0;
|
||||
goto done;
|
||||
}
|
||||
|
|
@ -1367,7 +1375,7 @@ xml_order(cxobj *xt,
|
|||
/* Go through xml children and ensure they are same order as yspec children */
|
||||
for (i=0; i<y->ys_len; i++){
|
||||
yc = y->ys_stmt[i];
|
||||
if (!yang_is_syntax(yc))
|
||||
if (!yang_datanode(yc))
|
||||
continue;
|
||||
yname = yc->ys_argument;
|
||||
/* First go thru xml children with same name */
|
||||
|
|
@ -1411,7 +1419,6 @@ xml_sanity(cxobj *xt,
|
|||
char *name;
|
||||
|
||||
if ((ys = (yang_stmt*)xml_spec(xt)) == NULL){
|
||||
clicon_log(LOG_WARNING, "%s: no xml_spec(%s)", __FUNCTION__, xml_name(xt));
|
||||
retval = 0;
|
||||
goto done;
|
||||
}
|
||||
|
|
@ -1438,7 +1445,6 @@ xml_non_config_data(cxobj *xt,
|
|||
yang_stmt *ys;
|
||||
|
||||
if ((ys = (yang_stmt*)xml_spec(xt)) == NULL){
|
||||
clicon_log(LOG_WARNING, "%s: no xml_spec(%s)", __FUNCTION__, xml_name(xt));
|
||||
retval = 0;
|
||||
goto done;
|
||||
}
|
||||
|
|
@ -1455,6 +1461,7 @@ xml_non_config_data(cxobj *xt,
|
|||
/*! Add yang specification backpoint to XML node
|
||||
* @param[in] xt XML tree node
|
||||
* @note This should really be unnecessary since yspec should be set on creation
|
||||
* @note For subs to anyxml nodes will not have spec set
|
||||
* @code
|
||||
* xml_apply(xc, CX_ELMNT, xml_spec_populate, yspec)
|
||||
* @endcode
|
||||
|
|
@ -1466,25 +1473,28 @@ xml_spec_populate(cxobj *x,
|
|||
int retval = -1;
|
||||
yang_spec *yspec = (yang_spec*)arg;
|
||||
char *name;
|
||||
yang_stmt *y; /* yang node */
|
||||
cxobj *xp; /* xml parent */
|
||||
yang_stmt *yp; /* parent yang */
|
||||
yang_stmt *y=NULL; /* yang node */
|
||||
cxobj *xp; /* xml parent */
|
||||
yang_stmt *yp; /* parent yang */
|
||||
|
||||
name = xml_name(x);
|
||||
if ((xp = xml_parent(x)) != NULL &&
|
||||
(yp = xml_spec(xp)) != NULL)
|
||||
y = yang_find_syntax((yang_node*)yp, xml_name(x));
|
||||
y = yang_find_datanode((yang_node*)yp, xml_name(x));
|
||||
else
|
||||
y = yang_find_topnode(yspec, name); /* still NULL for config */
|
||||
#ifdef XXX_OBSOLETE /* Add validate elsewhere */
|
||||
if (y==NULL){
|
||||
clicon_err(OE_XML, EBADF, "yang spec not found for xml node '%s' xml parent name: '%s' yangspec:'%s']",
|
||||
name,
|
||||
xp?xml_name(xp):"", yp?yp->ys_argument:"");
|
||||
goto done;
|
||||
}
|
||||
xml_spec_set(x, y);
|
||||
#endif
|
||||
if (y)
|
||||
xml_spec_set(x, y);
|
||||
retval = 0;
|
||||
done:
|
||||
// done:
|
||||
return retval;
|
||||
}
|
||||
|
||||
|
|
@ -1541,7 +1551,7 @@ api_path2xpath_cvv(yang_spec *yspec,
|
|||
}
|
||||
else{
|
||||
assert(y!=NULL);
|
||||
if ((y = yang_find_syntax((yang_node*)y, name)) == NULL){
|
||||
if ((y = yang_find_datanode((yang_node*)y, name)) == NULL){
|
||||
clicon_err(OE_UNIX, errno, "No yang node found: %s", name);
|
||||
goto done;
|
||||
}
|
||||
|
|
@ -1671,7 +1681,7 @@ api_path2xml_vec(char **vec,
|
|||
if (y0->yn_keyword == Y_SPEC) /* top-node */
|
||||
y = yang_find_topnode((yang_spec*)y0, name);
|
||||
else
|
||||
y = yang_find_syntax((yang_node*)y0, name);
|
||||
y = yang_find_datanode((yang_node*)y0, name);
|
||||
if (y == NULL){
|
||||
clicon_err(OE_YANG, errno, "No yang node found: %s", name);
|
||||
goto done;
|
||||
|
|
@ -1936,7 +1946,7 @@ xml_merge1(cxobj *x0,
|
|||
while ((x1c = xml_child_each(x1, x1c, CX_ELMNT)) != NULL) {
|
||||
x1cname = xml_name(x1c);
|
||||
/* Get yang spec of the child */
|
||||
if ((yc = yang_find_syntax(y0, x1cname)) == NULL){
|
||||
if ((yc = yang_find_datanode(y0, x1cname)) == NULL){
|
||||
clicon_err(OE_YANG, errno, "No yang node found: %s", x1cname);
|
||||
goto done;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -376,7 +376,7 @@ yn_each(yang_node *yn,
|
|||
* @param[in] argument String compare w wrgument. if NULL, match any.
|
||||
* This however means that if you actually want to match only a yang-stmt with
|
||||
* argument==NULL you cannot, but I have not seen any such examples.
|
||||
* @see yang_find_syntax
|
||||
* @see yang_find_datanode
|
||||
*/
|
||||
yang_stmt *
|
||||
yang_find(yang_node *yn,
|
||||
|
|
@ -402,20 +402,17 @@ yang_find(yang_node *yn,
|
|||
return match ? ys : NULL;
|
||||
}
|
||||
|
||||
/*! Find a child syntax-node yang_stmt with matching argument (container, leaf, etc)
|
||||
/*! Find child data node with matching argument (container, leaf, etc)
|
||||
*
|
||||
* @param[in] yn Yang node, current context node.
|
||||
* @param[in] argument if NULL, match any(first) argument.
|
||||
* @param[in] argument if NULL, match any(first) argument. XXX is that really a case?
|
||||
*
|
||||
* @see yang_find But this looks only for the yang specification nodes with
|
||||
* the following keyword: container, leaf, list, leaf-list
|
||||
* That is, basic syntax nodes.
|
||||
* @note check if argument==NULL really required?
|
||||
* Is this yang-stmt a container, list, leaf or leaf-list?
|
||||
* @see yang_find Looks for any node
|
||||
* @note May deviate from RFC since it explores choice/case not just return it.
|
||||
*/
|
||||
yang_stmt *
|
||||
yang_find_syntax(yang_node *yn,
|
||||
char *argument)
|
||||
yang_find_datanode(yang_node *yn,
|
||||
char *argument)
|
||||
{
|
||||
yang_stmt *ys = NULL;
|
||||
yang_stmt *yc = NULL;
|
||||
|
|
@ -428,9 +425,9 @@ yang_find_syntax(yang_node *yn,
|
|||
for (j=0; j<ys->ys_len; j++){
|
||||
yc = ys->ys_stmt[j];
|
||||
if (yc->ys_keyword == Y_CASE) /* Look for its children */
|
||||
ysmatch = yang_find_syntax((yang_node*)yc, argument);
|
||||
ysmatch = yang_find_datanode((yang_node*)yc, argument);
|
||||
else
|
||||
if (yang_is_syntax(yc)){
|
||||
if (yang_datanode(yc)){
|
||||
if (argument == NULL)
|
||||
ysmatch = yc;
|
||||
else
|
||||
|
|
@ -440,9 +437,9 @@ yang_find_syntax(yang_node *yn,
|
|||
if (ysmatch)
|
||||
goto match;
|
||||
}
|
||||
}
|
||||
} /* Y_CHOICE */
|
||||
else
|
||||
if (yang_is_syntax(ys)){
|
||||
if (yang_datanode(ys)){
|
||||
if (argument == NULL)
|
||||
ysmatch = ys;
|
||||
else
|
||||
|
|
@ -456,7 +453,10 @@ yang_find_syntax(yang_node *yn,
|
|||
return ysmatch;
|
||||
}
|
||||
|
||||
/*! Help function to check find 'top-node', eg first 'syntax' node in a spec
|
||||
/*! Find 'top-node', eg first data node in all (sub)modules in a yang spec
|
||||
*
|
||||
* @param[in] ysp Yang specification
|
||||
* @param[in] name if NULL, match any(first) argument. XXX is that really a case?
|
||||
* A yang specification has modules as children which in turn can have
|
||||
* syntax-nodes as children. This function goes through all the modules to
|
||||
* look for syntax-nodes. Note that if a child to a module is a choice,
|
||||
|
|
@ -472,24 +472,23 @@ yang_find_topnode(yang_spec *ysp,
|
|||
|
||||
for (i=0; i<ysp->yp_len; i++){
|
||||
ys = ysp->yp_stmt[i];
|
||||
if ((yc = yang_find_syntax((yang_node*)ys, name)) != NULL)
|
||||
if ((yc = yang_find_datanode((yang_node*)ys, name)) != NULL)
|
||||
return yc;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*! Find a child spec-node yang_stmt with matching argument for xpath
|
||||
/*! Find a child spec-node yang_stmt with matching argument for schema-nodid
|
||||
*
|
||||
* See also yang_find() but this looks only for the yang specification nodes with
|
||||
* the following keyword: container, leaf, list, leaf-list
|
||||
* That is, basic syntax nodes.
|
||||
* @see yang_find_syntax # Maybe this is the same as specnode?
|
||||
* @see yang_find_datanode
|
||||
* @see clicon_dbget_xpath
|
||||
* @see xpath_vec
|
||||
*/
|
||||
static yang_stmt *
|
||||
yang_find_xpath_stmt(yang_node *yn,
|
||||
char *argument)
|
||||
schema_nodeid_stmt(yang_node *yn,
|
||||
char *argument)
|
||||
{
|
||||
yang_stmt *ys = NULL;
|
||||
int i;
|
||||
|
|
@ -497,23 +496,19 @@ yang_find_xpath_stmt(yang_node *yn,
|
|||
|
||||
for (i=0; i<yn->yn_len; i++){
|
||||
ys = yn->yn_stmt[i];
|
||||
if (!yang_schemanode(ys))
|
||||
continue;
|
||||
/* some keys dont have arguments, match on key */
|
||||
if (ys->ys_keyword == Y_INPUT || ys->ys_keyword == Y_OUTPUT){
|
||||
if (strcmp(argument, yang_key2str(ys->ys_keyword)) == 0)
|
||||
if (strcmp(argument, yang_key2str(ys->ys_keyword)) == 0){
|
||||
match++;
|
||||
}
|
||||
else
|
||||
if (ys->ys_keyword == Y_CONTAINER || ys->ys_keyword == Y_LEAF ||
|
||||
ys->ys_keyword == Y_LIST || ys->ys_keyword == Y_LEAF_LIST ||
|
||||
ys->ys_keyword == Y_MODULE || ys->ys_keyword == Y_SUBMODULE ||
|
||||
ys->ys_keyword == Y_RPC || ys->ys_keyword == Y_CHOICE ||
|
||||
ys->ys_keyword == Y_CASE
|
||||
){
|
||||
if (ys->ys_argument && strcmp(argument, ys->ys_argument) == 0)
|
||||
break;
|
||||
}
|
||||
} else
|
||||
if (ys->ys_argument && strcmp(argument, ys->ys_argument) == 0){
|
||||
match++;
|
||||
}
|
||||
if (match)
|
||||
break;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return match ? ys : NULL;
|
||||
}
|
||||
|
|
@ -966,21 +961,6 @@ ys_populate_type(yang_stmt *ys,
|
|||
goto done;
|
||||
}
|
||||
}
|
||||
else
|
||||
if (strcmp(ys->ys_argument, "leafref") == 0){
|
||||
#ifdef notyet /* XXX: Do augment first */
|
||||
yang_stmt *ypath;
|
||||
if ((ypath = yang_find((yang_node*)ys, Y_PATH, NULL)) == NULL){
|
||||
clicon_err(OE_YANG, 0, "leafref type requires path sub-statement");
|
||||
goto done;
|
||||
}
|
||||
if (yang_xpath((yang_node*)ys, ypath->ys_argument) == NULL){
|
||||
clicon_err(OE_YANG, 0, "Leafref path %s not found",
|
||||
ypath->ys_argument);
|
||||
goto done;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
retval = 0;
|
||||
done:
|
||||
return retval;
|
||||
|
|
@ -1101,17 +1081,17 @@ yang_augment_node(yang_stmt *ys,
|
|||
yang_spec *ysp)
|
||||
{
|
||||
int retval = -1;
|
||||
char *path;
|
||||
char *schema_nodeid;
|
||||
yang_node *yn;
|
||||
yang_stmt *yc;
|
||||
int i;
|
||||
|
||||
path = ys->ys_argument;
|
||||
clicon_debug(1, "%s %s", __FUNCTION__, path);
|
||||
schema_nodeid = ys->ys_argument;
|
||||
clicon_debug(1, "%s %s", __FUNCTION__, schema_nodeid);
|
||||
|
||||
/* Find the target */
|
||||
if ((yn = yang_xpath_abs((yang_node*)ys, path)) == NULL){
|
||||
clicon_err(OE_YANG, 0, "Augment path %s not found", path);
|
||||
if ((yn = yang_abs_schema_nodeid((yang_node*)ys, schema_nodeid)) == NULL){
|
||||
clicon_err(OE_YANG, 0, "Augment schema_nodeid %s not found", schema_nodeid);
|
||||
// retval = 0; /* Ignore, continue */
|
||||
goto done;
|
||||
}
|
||||
|
|
@ -1269,8 +1249,7 @@ yang_expand(yang_node *yn)
|
|||
* @retval NULL Error encountered
|
||||
* Calling order:
|
||||
* yang_parse # Parse top-level yang module. Expand and populate yang tree
|
||||
* yang_parse1 # Parse one yang module, go through its (sub)modules and parse them
|
||||
* yang_parse2 # Find file from yang (sub)module
|
||||
* yang_parse_recurse # Parse one yang module, go through its (sub)modules, parse them and then recursively parse them
|
||||
* yang_parse_file # Read yang file into a string
|
||||
* yang_parse_str # Set up yacc parser and call it given a string
|
||||
* clixon_yang_parseparse # Actual yang parsing using yacc
|
||||
|
|
@ -1322,8 +1301,7 @@ yang_parse_str(clicon_handle h,
|
|||
* Similar to clicon_yang_str(), just read a file first
|
||||
* (cloned from cligen)
|
||||
* @param h CLICON handle
|
||||
* @param f Open file handle
|
||||
* @param name Log string, typically filename
|
||||
* @param filename Name of file
|
||||
* @param ysp Yang specification. Should ave been created by caller using yspec_new
|
||||
* @retval ymod Top-level yang (sub)module
|
||||
* @retval NULL Error encountered
|
||||
|
|
@ -1331,26 +1309,35 @@ yang_parse_str(clicon_handle h,
|
|||
* The database symbols are inserted in alphabetical order.
|
||||
* Calling order:
|
||||
* yang_parse # Parse top-level yang module. Expand and populate yang tree
|
||||
* yang_parse1 # Parse one yang module, go through its (sub)modules and parse them
|
||||
* yang_parse2 # Find file from yang (sub)module
|
||||
* yang_parse_recurse # Parse one yang module, go through its (sub)modules, parse them and then recursively parse them
|
||||
* yang_parse_file # Read yang file into a string
|
||||
* yang_parse_str # Set up yacc parser and call it given a string
|
||||
* clixon_yang_parseparse # Actual yang parsing using yacc
|
||||
*/
|
||||
static yang_stmt *
|
||||
yang_parse_file(clicon_handle h,
|
||||
FILE *f,
|
||||
const char *name, /* just for errs */
|
||||
const char *filename,
|
||||
yang_spec *ysp
|
||||
)
|
||||
{
|
||||
char *buf;
|
||||
char *buf = NULL;
|
||||
int i;
|
||||
int c;
|
||||
int len;
|
||||
yang_stmt *ymod = NULL;
|
||||
FILE *f = NULL;
|
||||
struct stat st;
|
||||
|
||||
clicon_debug(1, "Yang parse file: %s", filename);
|
||||
if (stat(filename, &st) < 0){
|
||||
clicon_err(OE_YANG, errno, "%s not found", filename);
|
||||
goto done;
|
||||
}
|
||||
if ((f = fopen(filename, "r")) == NULL){
|
||||
clicon_err(OE_UNIX, errno, "fopen(%s)", filename);
|
||||
goto done;
|
||||
}
|
||||
|
||||
clicon_debug(1, "Yang parse file: %s", name);
|
||||
len = 1024; /* any number is fine */
|
||||
if ((buf = malloc(len)) == NULL){
|
||||
perror("pt_file malloc");
|
||||
|
|
@ -1372,9 +1359,11 @@ yang_parse_file(clicon_handle h,
|
|||
}
|
||||
buf[i++] = (char)(c&0xff);
|
||||
} /* read a line */
|
||||
if ((ymod = yang_parse_str(h, buf, name, ysp)) < 0)
|
||||
if ((ymod = yang_parse_str(h, buf, filename, ysp)) < 0)
|
||||
goto done;
|
||||
done:
|
||||
if (f)
|
||||
fclose(f);
|
||||
if (buf)
|
||||
free(buf);
|
||||
return ymod; /* top-level (sub)module */
|
||||
|
|
@ -1431,105 +1420,62 @@ yang_parse_find_match(clicon_handle h,
|
|||
return retval;
|
||||
}
|
||||
|
||||
/*! Find and open yang file and then parse it
|
||||
*
|
||||
* @param h CLICON handle
|
||||
* @param yang_dir Directory where all YANG module files reside
|
||||
* @param module Name of main YANG module. More modules may be parsed if imported
|
||||
* @param revision Optional module revision date
|
||||
* @param ysp Yang specification. Should ave been created by caller using yspec_new
|
||||
* @retval ymod Top-level yang (sub)module
|
||||
* @retval NULL Error encountered
|
||||
* module-or-submodule-name ['@' revision-date] ( '.yang' / '.yin' )
|
||||
* Calling order:
|
||||
* yang_parse # Parse top-level yang module. Expand and populate yang tree
|
||||
* yang_parse1 # Parse one yang module, go through its (sub)modules and parse them
|
||||
* yang_parse2 # Find file from yang (sub)module
|
||||
* yang_parse_file # Read yang file into a string
|
||||
* yang_parse_str # Set up yacc parser and call it given a string
|
||||
* clixon_yang_parseparse # Actual yang parsing using yacc
|
||||
*/
|
||||
static yang_stmt *
|
||||
yang_parse2(clicon_handle h,
|
||||
const char *yang_dir,
|
||||
const char *module,
|
||||
const char *revision,
|
||||
yang_spec *ysp)
|
||||
{
|
||||
FILE *f = NULL;
|
||||
cbuf *fbuf = NULL;
|
||||
char *filename;
|
||||
yang_stmt *ymod = NULL;
|
||||
struct stat st;
|
||||
int nr;
|
||||
|
||||
if ((fbuf = cbuf_new()) == NULL){
|
||||
clicon_err(OE_YANG, errno, "%s: cbuf_new", __FUNCTION__);
|
||||
goto done;
|
||||
}
|
||||
if (revision)
|
||||
cprintf(fbuf, "%s/%s@%s.yang", yang_dir, module, revision);
|
||||
else{
|
||||
/* No specific revision, Match a yang file */
|
||||
if ((nr = yang_parse_find_match(h, yang_dir, module, fbuf)) < 0)
|
||||
goto done;
|
||||
if (nr == 0){
|
||||
clicon_err(OE_YANG, errno, "No matching %s yang files found", module);
|
||||
goto done;
|
||||
}
|
||||
}
|
||||
filename = cbuf_get(fbuf);
|
||||
if (stat(filename, &st) < 0){
|
||||
clicon_err(OE_YANG, errno, "%s not found", filename);
|
||||
goto done;
|
||||
}
|
||||
if ((f = fopen(filename, "r")) == NULL){
|
||||
clicon_err(OE_UNIX, errno, "fopen(%s)", filename);
|
||||
goto done;
|
||||
}
|
||||
if ((ymod = yang_parse_file(h, f, filename, ysp)) == NULL)
|
||||
goto done;
|
||||
done:
|
||||
if (fbuf)
|
||||
cbuf_free(fbuf);
|
||||
if (f)
|
||||
fclose(f);
|
||||
return ymod; /* top-level (sub)module */
|
||||
}
|
||||
|
||||
/*! Parse one yang module then go through (sub)modules and parse them recursively
|
||||
*
|
||||
* @param h CLICON handle
|
||||
* @param yang_dir Directory where all YANG module files reside
|
||||
* @param module Name of main YANG module. More modules may be parsed if imported
|
||||
* @param revision Optional module revision date
|
||||
* @param ysp Yang specification. Should have been created by caller using yspec_new
|
||||
* @retval ymod Top-level yang (sub)module
|
||||
* @retval NULL Error encountered
|
||||
* @param[in] h CLICON handle
|
||||
* @param[in] yang_dir Directory where all YANG module files reside
|
||||
* @param[in] module Name of main YANG module. Or absolute file name.
|
||||
* @param[in] revision Optional module revision date
|
||||
* @param[in] ysp Yang specification. Should have been created by caller using yspec_new
|
||||
* @retval ymod Top-level yang (sub)module
|
||||
* @retval NULL Error encountered
|
||||
* Find a yang module file, and then recursively parse all its imported modules.
|
||||
* Calling order:
|
||||
* yang_parse # Parse top-level yang module. Expand and populate yang tree
|
||||
* yang_parse1 # Parse one yang module, go through its (sub)modules and parse them
|
||||
* yang_parse2 # Find file from yang (sub)module
|
||||
* yang_parse_recurse # Parse one yang module, go through its (sub)modules, parse them and then recursively parse them
|
||||
* yang_parse_file # Read yang file into a string
|
||||
* yang_parse_str # Set up yacc parser and call it given a string
|
||||
* clixon_yang_parseparse # Actual yang parsing using yacc
|
||||
*/
|
||||
static yang_stmt *
|
||||
yang_parse1(clicon_handle h,
|
||||
const char *yang_dir,
|
||||
const char *module,
|
||||
const char *revision,
|
||||
yang_spec *ysp)
|
||||
yang_parse_recurse(clicon_handle h,
|
||||
const char *yang_dir,
|
||||
const char *module,
|
||||
const char *revision,
|
||||
yang_spec *ysp)
|
||||
{
|
||||
yang_stmt *yi = NULL; /* import */
|
||||
yang_stmt *ymod;
|
||||
yang_stmt *yrev;
|
||||
char *modname;
|
||||
char *subrevision;
|
||||
cbuf *fbuf = NULL;
|
||||
int nr;
|
||||
|
||||
if (module[0] == '/'){
|
||||
if ((ymod = yang_parse_file(h, module, ysp)) == NULL)
|
||||
goto done;
|
||||
}
|
||||
else {
|
||||
if ((fbuf = cbuf_new()) == NULL){
|
||||
clicon_err(OE_YANG, errno, "%s: cbuf_new", __FUNCTION__);
|
||||
goto done;
|
||||
}
|
||||
if (revision)
|
||||
cprintf(fbuf, "%s/%s@%s.yang", yang_dir, module, revision);
|
||||
else{
|
||||
/* No specific revision, Match a yang file */
|
||||
if ((nr = yang_parse_find_match(h, yang_dir, module, fbuf)) < 0)
|
||||
goto done;
|
||||
if (nr == 0){
|
||||
clicon_err(OE_YANG, errno, "No matching %s yang files found", module);
|
||||
goto done;
|
||||
}
|
||||
}
|
||||
if ((ymod = yang_parse_file(h, cbuf_get(fbuf), ysp)) == NULL)
|
||||
goto done;
|
||||
}
|
||||
|
||||
if ((ymod = yang_parse2(h, yang_dir, module, revision, ysp)) == NULL)
|
||||
goto done;
|
||||
/* go through all import statements of ysp (or its module) */
|
||||
while ((yi = yn_each((yang_node*)ymod, yi)) != NULL){
|
||||
if (yi->ys_keyword != Y_IMPORT)
|
||||
|
|
@ -1540,30 +1486,33 @@ yang_parse1(clicon_handle h,
|
|||
else
|
||||
subrevision = NULL;
|
||||
if (yang_find((yang_node*)ysp, Y_MODULE, modname) == NULL)
|
||||
if (yang_parse1(h, yang_dir, modname, subrevision, ysp) == NULL){
|
||||
/* recursive call */
|
||||
if (yang_parse_recurse(h, yang_dir, modname, subrevision, ysp) == NULL){
|
||||
ymod = NULL;
|
||||
goto done;
|
||||
}
|
||||
}
|
||||
done:
|
||||
if (fbuf)
|
||||
cbuf_free(fbuf);
|
||||
return ymod; /* top-level (sub)module */
|
||||
}
|
||||
|
||||
/*! Parse top yang module including all its sub-modules. Expand and populate yang tree
|
||||
*
|
||||
* @param[in] h CLICON handle
|
||||
* @param[in] yang_dir Directory where all YANG module files reside
|
||||
* @param[in] module Name of main YANG module. More modules may be parsed if imported
|
||||
* @param[in] revision Optional module revision date
|
||||
* @param[in] yang_dir Directory where all YANG module files reside (except mainfile)
|
||||
* @param[in] mainmod Name of main YANG module. Or absolute file name.
|
||||
* @param[in] revision Optional main module revision date.
|
||||
* @param[out] ysp Yang specification. Should ave been created by caller using yspec_new
|
||||
* @retval 0 Everything OK
|
||||
* @retval -1 Error encountered
|
||||
* The database symbols are inserted in alphabetical order.
|
||||
* Find a yang module file, and then recursively parse all its imported modules.
|
||||
* @note if mainmod is filename, revision is not considered.
|
||||
* Calling order:
|
||||
* yang_parse # Parse top-level yang module. Expand and populate yang tree
|
||||
* yang_parse1 # Parse one yang module, go through its (sub)modules and parse them
|
||||
* yang_parse2 # Find file from yang (sub)module
|
||||
* yang_parse_recurse # Parse one yang module, go through its (sub)modules, parse them and then recursively parse them
|
||||
* yang_parse_file # Read yang file into a string
|
||||
* yang_parse_str # Set up yacc parser and call it given a string
|
||||
* clixon_yang_parseparse # Actual yang parsing using yacc
|
||||
|
|
@ -1571,7 +1520,7 @@ yang_parse1(clicon_handle h,
|
|||
int
|
||||
yang_parse(clicon_handle h,
|
||||
const char *yang_dir,
|
||||
const char *module,
|
||||
const char *mainmodule,
|
||||
const char *revision,
|
||||
yang_spec *ysp)
|
||||
{
|
||||
|
|
@ -1579,7 +1528,7 @@ yang_parse(clicon_handle h,
|
|||
yang_stmt *ymod; /* Top-level yang (sub)module */
|
||||
|
||||
/* Step 1: parse from text to yang parse-tree. */
|
||||
if ((ymod = yang_parse1(h, yang_dir, module, revision, ysp)) == NULL)
|
||||
if ((ymod = yang_parse_recurse(h, yang_dir, mainmodule, revision, ysp)) == NULL)
|
||||
goto done;
|
||||
/* Add top module name as dbspec-name */
|
||||
clicon_dbspec_name_set(h, ymod->ys_argument);
|
||||
|
|
@ -1661,16 +1610,22 @@ yang_apply(yang_node *yn,
|
|||
return retval;
|
||||
}
|
||||
|
||||
/*! All the work for yang_xpath.
|
||||
Ignore prefixes, see _abs */
|
||||
/*! All the work for schema_nodeid functions both absolute and descendant
|
||||
* Ignore prefixes, see _abs
|
||||
* @param[in] yn Yang node
|
||||
* @param[in] vec Vector of nodeid's in a schema node identifier, eg a/b
|
||||
* @param[in] nvec Length of vec
|
||||
* @retval NULL Error, with clicon_err called
|
||||
* @retval yres First yang node matching schema nodeid
|
||||
*/
|
||||
static yang_node *
|
||||
yang_xpath_vec(yang_node *yn,
|
||||
char **vec,
|
||||
int nvec)
|
||||
schema_nodeid_vec(yang_node *yn,
|
||||
char **vec,
|
||||
int nvec)
|
||||
{
|
||||
char *arg;
|
||||
yang_stmt *ys;
|
||||
yang_node *yret = NULL;
|
||||
yang_node *yres = NULL;
|
||||
char *id;
|
||||
|
||||
if (nvec <= 0)
|
||||
|
|
@ -1687,44 +1642,46 @@ yang_xpath_vec(yang_node *yn,
|
|||
id = arg;
|
||||
else
|
||||
id++;
|
||||
if ((ys = yang_find_xpath_stmt(yn, id)) == NULL){
|
||||
if ((ys = schema_nodeid_stmt(yn, id)) == NULL){
|
||||
clicon_debug(1, "%s %s not found", __FUNCTION__, id);
|
||||
goto done;
|
||||
}
|
||||
}
|
||||
if (nvec == 1){
|
||||
yret = (yang_node*)ys;
|
||||
yres = (yang_node*)ys;
|
||||
goto done;
|
||||
}
|
||||
yret = yang_xpath_vec((yang_node*)ys, vec+1, nvec-1);
|
||||
yres = schema_nodeid_vec((yang_node*)ys, vec+1, nvec-1);
|
||||
done:
|
||||
return yret;
|
||||
return yres;
|
||||
}
|
||||
|
||||
|
||||
/*! Given an absolute xpath (eg /a/b/c) find matching yang specification
|
||||
* @param[in] yn Yang node
|
||||
* @param[in] xpath Absolute xpath, ie /a/b
|
||||
/*! Given an absolute schema-nodeid (eg /a/b/c) find matching yang spec
|
||||
* @param[in] yn Yang node
|
||||
* @param[in] schema_nodeid Absolute schema-node-id, ie /a/b
|
||||
* @retval NULL Error, with clicon_err called
|
||||
* @retval yres First yang node matching schema nodeid
|
||||
* @see yang_schema_nodeid
|
||||
*/
|
||||
yang_node *
|
||||
yang_xpath_abs(yang_node *yn,
|
||||
char *xpath)
|
||||
yang_abs_schema_nodeid(yang_node *yn,
|
||||
char *schema_nodeid)
|
||||
{
|
||||
char **vec = NULL;
|
||||
int nvec;
|
||||
yang_node *ys = NULL;
|
||||
yang_node *yres = NULL;
|
||||
yang_stmt *ymod;
|
||||
char *id;
|
||||
char *prefix = NULL;
|
||||
|
||||
if ((vec = clicon_strsep(xpath, "/", &nvec)) == NULL){
|
||||
if ((vec = clicon_strsep(schema_nodeid, "/", &nvec)) == NULL){
|
||||
clicon_err(OE_YANG, errno, "%s: strsep", __FUNCTION__);
|
||||
return NULL;
|
||||
goto done;
|
||||
}
|
||||
/* Assume path looks like: "/prefix:id[/prefix:id]*" */
|
||||
/* Assume schame nodeid looks like: "/prefix:id[/prefix:id]*" */
|
||||
if (nvec < 2){
|
||||
clicon_err(OE_YANG, 0, "%s: NULL or truncated path: %s",
|
||||
__FUNCTION__, xpath);
|
||||
__FUNCTION__, schema_nodeid);
|
||||
goto done;
|
||||
}
|
||||
|
||||
|
|
@ -1753,53 +1710,77 @@ yang_xpath_abs(yang_node *yn,
|
|||
if ((ymod = yang_find_module_by_prefix((yang_stmt*)yn, prefix)) == NULL)
|
||||
goto done;
|
||||
}
|
||||
ys = yang_xpath_vec((yang_node*)ymod, vec+1, nvec-1);
|
||||
yres = schema_nodeid_vec((yang_node*)ymod, vec+1, nvec-1);
|
||||
done:
|
||||
if (vec)
|
||||
free(vec);
|
||||
if (prefix)
|
||||
free(prefix);
|
||||
return ys;
|
||||
return yres;
|
||||
}
|
||||
|
||||
/*! Given an xpath (eg /a/b/c or a/b/c) find matching yang specification
|
||||
* Note that xpath is defined for xml, and for instances of data, this is
|
||||
* for specifications, sp expect some differences.
|
||||
/*! Given a descendant schema-nodeid (eg a/b/c) find matching yang spec
|
||||
* @param[in] yn Yang node
|
||||
* @param[in] schema_nodeid Descendant schema-node-id, ie a/b
|
||||
* @retval NULL Error, with clicon_err called
|
||||
* @retval yres First yang node matching schema nodeid
|
||||
* @see yang_schema_nodeid
|
||||
*/
|
||||
yang_node *
|
||||
yang_desc_schema_nodeid(yang_node *yn,
|
||||
char *schema_nodeid)
|
||||
{
|
||||
yang_node *yres = NULL;
|
||||
char **vec = NULL;
|
||||
int nvec;
|
||||
|
||||
if (strlen(schema_nodeid) == 0)
|
||||
goto done;
|
||||
/* check absolute schema_nodeid */
|
||||
if (schema_nodeid[0] == '/'){
|
||||
clicon_err(OE_YANG, EINVAL, "descenadant schema nodeid should not start with /");
|
||||
goto done;
|
||||
}
|
||||
if ((vec = clicon_strsep(schema_nodeid, "/", &nvec)) == NULL)
|
||||
goto done;
|
||||
yres = schema_nodeid_vec((yang_node*)yn, vec, nvec);
|
||||
done:
|
||||
if (vec)
|
||||
free(vec);
|
||||
return yres;
|
||||
}
|
||||
|
||||
/*! Given a schema-node-id (eg /a/b/c or a/b/c) find matching yang specification
|
||||
*
|
||||
* They are either absolute (start with /) or descendant (no /)
|
||||
* Compare with XPATH for XML, this is for YANG spec
|
||||
* @param[in] yn Yang node tree
|
||||
* @param[in] xpath A limited xpath expression on the type a/b/c
|
||||
* @param[in] schema_nodeid schema-node-id, ie a/b or /a/b
|
||||
* @retval NULL Error, with clicon_err called
|
||||
* @retval ys First yang node matching xpath
|
||||
* @retval yres First yang node matching schema nodeid
|
||||
* @note: the identifiers in the xpath (eg a, b in a/b) can match the nodes
|
||||
* defined in yang_xpath: container, leaf,list,leaf-list, modules, sub-modules
|
||||
* Example:
|
||||
* yn : module m { prefix b; container b { list c { key d; leaf d; }} }
|
||||
* xpath = m/b/c, returns the list 'c'.
|
||||
* @see xpath_vec
|
||||
* @see clicon_dbget_xpath
|
||||
* schema-node-id = m/b/c, returns the list 'c'.
|
||||
*/
|
||||
yang_node *
|
||||
yang_xpath(yang_node *yn,
|
||||
char *xpath)
|
||||
yang_schema_nodeid(yang_node *yn,
|
||||
char *schema_nodeid)
|
||||
{
|
||||
char **vec = NULL;
|
||||
yang_node *ys = NULL;
|
||||
int nvec;
|
||||
yang_node *yres = NULL;
|
||||
|
||||
if (strlen(xpath) == 0)
|
||||
return NULL;
|
||||
/* check absolute path */
|
||||
if (xpath[0] == '/')
|
||||
return yang_xpath_abs(yn, xpath);
|
||||
if ((vec = clicon_strsep(xpath, "/", &nvec)) == NULL)
|
||||
goto err;
|
||||
ys = yang_xpath_vec((yang_node*)yn, vec, nvec);
|
||||
err:
|
||||
if (vec)
|
||||
free(vec);
|
||||
return ys;
|
||||
if (strlen(schema_nodeid) == 0)
|
||||
goto done;
|
||||
/* check absolute schema_nodeid */
|
||||
if (schema_nodeid[0] == '/')
|
||||
yres = yang_abs_schema_nodeid(yn, schema_nodeid);
|
||||
else
|
||||
yres = yang_desc_schema_nodeid(yn, schema_nodeid);
|
||||
done:
|
||||
return yres;
|
||||
}
|
||||
|
||||
|
||||
/*! Parse argument as CV and save result in yang cv variable
|
||||
*
|
||||
* Note that some CV:s are parsed directly (eg fraction-digits) while others are parsed
|
||||
|
|
@ -1933,6 +1914,7 @@ yang_spec_main(clicon_handle h,
|
|||
clicon_err(OE_FATAL, 0, "CLICON_YANG_DIR option not set");
|
||||
goto done;
|
||||
}
|
||||
/* Yang module is either specific absolute filename, or main module */
|
||||
if ((yang_module = clicon_yang_module_main(h)) == NULL){
|
||||
clicon_err(OE_FATAL, 0, "CLICON_YANG_MODULE_MAIN option not set");
|
||||
goto done;
|
||||
|
|
|
|||
|
|
@ -202,14 +202,12 @@ clixon_yang_parsewrap(void)
|
|||
<STRING0>. { clixon_yang_parselval.string = strdup(yytext);
|
||||
return CHAR;}
|
||||
|
||||
|
||||
<STRING1>\\ { _YY->yy_lex_state = STRING1; BEGIN(ESCAPE); }
|
||||
<STRING1>\" { BEGIN(_YY->yy_lex_string_state); return DQ; }
|
||||
<STRING1>\n { _YY->yy_linenum++; clixon_yang_parselval.string = strdup(yytext); return CHAR;}
|
||||
<STRING1>. { clixon_yang_parselval.string = strdup(yytext);
|
||||
return CHAR;}
|
||||
|
||||
<STRING2>\\ { _YY->yy_lex_state = STRING2; BEGIN(ESCAPE); }
|
||||
<STRING2>\' { BEGIN(_YY->yy_lex_string_state); return DQ; }
|
||||
<STRING2>\n { _YY->yy_linenum++; clixon_yang_parselval.string = strdup(yytext); return CHAR;}
|
||||
<STRING2>. { clixon_yang_parselval.string = strdup(yytext);
|
||||
|
|
|
|||
|
|
@ -221,6 +221,10 @@ ys_resolve_type(yang_stmt *ys,
|
|||
if (yang_type_resolve((yang_stmt*)ys->ys_parent, ys, &resolved,
|
||||
&options, &mincv, &maxcv, &pattern, &fraction) < 0)
|
||||
goto done;
|
||||
/* skip unions since they may have different sets of options, mincv, etc */
|
||||
if (resolved && strcmp(resolved->ys_argument, "union")==0)
|
||||
;
|
||||
else
|
||||
if (yang_type_cache_set(&ys->ys_typecache,
|
||||
resolved, options, mincv, maxcv, pattern, fraction) < 0)
|
||||
goto done;
|
||||
|
|
@ -561,6 +565,91 @@ cv_validate1(cg_var *cv,
|
|||
return retval;
|
||||
}
|
||||
|
||||
/* Forward */
|
||||
static int ys_cv_validate_union(yang_stmt *ys, char **reason, yang_stmt *yrestype,
|
||||
char *type, char *val);
|
||||
|
||||
/*!
|
||||
* @retval -1 Error (fatal), with errno set to indicate error
|
||||
* @retval 0 Validation not OK, malloced reason is returned. Free reason with free()
|
||||
* @retval 1 Validation OK
|
||||
*/
|
||||
static int
|
||||
ys_cv_validate_union_one(yang_stmt *ys,
|
||||
char **reason,
|
||||
yang_stmt *yt,
|
||||
char *type, /* orig type */
|
||||
char *val)
|
||||
{
|
||||
int retval = -1;
|
||||
yang_stmt *yrt; /* union subtype */
|
||||
int options = 0;
|
||||
cg_var *range_min = NULL;
|
||||
cg_var *range_max = NULL;
|
||||
char *pattern = NULL;
|
||||
uint8_t fraction = 0;
|
||||
char *restype;
|
||||
enum cv_type cvtype;
|
||||
cg_var *cvt=NULL;
|
||||
|
||||
if (yang_type_resolve(ys, yt, &yrt,
|
||||
&options, &range_min, &range_max, &pattern,
|
||||
&fraction) < 0)
|
||||
goto done;
|
||||
restype = yrt?yrt->ys_argument:NULL;
|
||||
if (restype && strcmp(restype, "union") == 0){ /* recursive union */
|
||||
if ((retval = ys_cv_validate_union(ys, reason, yrt, type, val)) < 0)
|
||||
goto done;
|
||||
}
|
||||
else {
|
||||
if (clicon_type2cv(type, restype, &cvtype) < 0)
|
||||
goto done;
|
||||
/* reparse value with the new type */
|
||||
if ((cvt = cv_new(cvtype)) == NULL){
|
||||
clicon_err(OE_UNIX, errno, "cv_new");
|
||||
goto done;
|
||||
}
|
||||
if (cv_parse(val, cvt) <0){
|
||||
clicon_err(OE_UNIX, errno, "cv_parse");
|
||||
goto done;
|
||||
}
|
||||
if ((retval = cv_validate1(cvt, cvtype, options, range_min, range_max,
|
||||
pattern, yrt, restype, reason)) < 0)
|
||||
goto done;
|
||||
}
|
||||
done:
|
||||
if (cvt)
|
||||
cv_free(cvt);
|
||||
return retval;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @retval -1 Error (fatal), with errno set to indicate error
|
||||
* @retval 0 Validation not OK, malloced reason is returned. Free reason with free()
|
||||
* @retval 1 Validation OK
|
||||
*/
|
||||
static int
|
||||
ys_cv_validate_union(yang_stmt *ys,
|
||||
char **reason,
|
||||
yang_stmt *yrestype,
|
||||
char *type, /* orig type */
|
||||
char *val)
|
||||
{
|
||||
int retval = 1; /* valid */
|
||||
yang_stmt *yt = NULL;
|
||||
|
||||
while ((yt = yn_each((yang_node*)yrestype, yt)) != NULL){
|
||||
if (yt->ys_keyword != Y_TYPE)
|
||||
continue;
|
||||
if ((retval = ys_cv_validate_union_one(ys, reason, yt, type, val)) < 0)
|
||||
goto done;
|
||||
if (retval == 1) /* Enough that one type validates value */
|
||||
break;
|
||||
}
|
||||
done:
|
||||
return retval;
|
||||
}
|
||||
|
||||
/*! Validate cligen variable cv using yang statement as spec
|
||||
*
|
||||
* @param[in] cv A cligen variable to validate. This is a correctly parsed cv.
|
||||
|
|
@ -579,17 +668,15 @@ ys_cv_validate(cg_var *cv,
|
|||
{
|
||||
int retval = -1;
|
||||
cg_var *ycv; /* cv of yang-statement */
|
||||
int options;
|
||||
cg_var *range_min;
|
||||
cg_var *range_max;
|
||||
char *pattern;
|
||||
int options = 0;
|
||||
cg_var *range_min = NULL;
|
||||
cg_var *range_max = NULL;
|
||||
char *pattern = NULL;
|
||||
enum cv_type cvtype;
|
||||
char *type; /* orig type */
|
||||
yang_stmt *yrestype; /* resolved type */
|
||||
yang_stmt *yrt; /* union subtype */
|
||||
char *restype;
|
||||
uint8_t fraction;
|
||||
yang_stmt *yt = NULL;
|
||||
uint8_t fraction = 0;
|
||||
int retval2;
|
||||
char *val;
|
||||
cg_var *cvt=NULL;
|
||||
|
|
@ -621,38 +708,10 @@ ys_cv_validate(cg_var *cv,
|
|||
}
|
||||
/* Note restype can be NULL here for example with unresolved hardcoded uuid */
|
||||
if (restype && strcmp(restype, "union") == 0){
|
||||
yt = NULL;
|
||||
retval2 = 1; /* valid */
|
||||
assert(cvtype == CGV_REST);
|
||||
val = cv_string_get(cv);
|
||||
while ((yt = yn_each((yang_node*)yrestype, yt)) != NULL){
|
||||
if (yt->ys_keyword != Y_TYPE)
|
||||
continue;
|
||||
if (yang_type_resolve(ys, yt, &yrt,
|
||||
&options, &range_min, &range_max, &pattern,
|
||||
&fraction) < 0)
|
||||
goto done;
|
||||
restype = yrestype?yrt->ys_argument:NULL;
|
||||
if (clicon_type2cv(type, restype, &cvtype) < 0)
|
||||
goto done;
|
||||
/* reparse value with the new type */
|
||||
if ((cvt = cv_new(cvtype)) == NULL){
|
||||
clicon_err(OE_UNIX, errno, "cv_new");
|
||||
goto done;
|
||||
}
|
||||
if (cv_parse(val, cvt) <0){
|
||||
clicon_err(OE_UNIX, errno, "cv_parse");
|
||||
goto done;
|
||||
}
|
||||
if ((retval2 = cv_validate1(cvt, cvtype, options, range_min, range_max,
|
||||
pattern, yrt, restype, reason)) < 0)
|
||||
goto done;
|
||||
if (retval2 == 1) /* done */
|
||||
break;
|
||||
/* Here retval == 0, validation failed */
|
||||
cv_free(cvt);
|
||||
cvt=NULL;
|
||||
}
|
||||
if ((retval2 = ys_cv_validate_union(ys, reason, yrestype, type, val)) < 0)
|
||||
goto done;
|
||||
retval = retval2; /* invalid (0) with latest reason or valid 1 */
|
||||
}
|
||||
else
|
||||
|
|
@ -860,7 +919,7 @@ yang_type_resolve(yang_stmt *ys,
|
|||
/* Not basic type. Now check if prefix which means we look in other module */
|
||||
if (prefix){ /* Go to top and find import that matches */
|
||||
if ((ymod = yang_find_module_by_prefix(ys, prefix)) == NULL){
|
||||
clicon_err(OE_DB, 0, "Module not resolved: %s", prefix);
|
||||
clicon_err(OE_DB, 0, "Type not resolved: %s:%s", prefix, type);
|
||||
goto done;
|
||||
}
|
||||
if ((rytypedef = yang_find((yang_node*)ymod, Y_TYPEDEF, type)) == NULL)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue