Yang "unique" feature supported according to RFC 7950 7.8.3
This commit is contained in:
parent
a5e955c95f
commit
2fc37d2470
7 changed files with 455 additions and 3 deletions
|
|
@ -966,6 +966,48 @@ netconf_malformed_message_xml(cxobj **xret,
|
|||
return retval;
|
||||
}
|
||||
|
||||
/*! Create Netconf data-not-unique error message according to RFC 7950 15.1
|
||||
*
|
||||
* A NETCONF operation would result in configuration data where a
|
||||
* "unique" constraint is invalidated.
|
||||
* @param[out] cb CLIgen buf. Error XML is written in this buffer
|
||||
* @param[in] x List element containing duplicate
|
||||
* @param[in] cvk List of comonents in x that are non-unique
|
||||
* @see RFC7950 Sec 15.1
|
||||
*/
|
||||
int
|
||||
netconf_data_not_unique(cbuf *cb,
|
||||
cxobj *x,
|
||||
cvec *cvk)
|
||||
{
|
||||
int retval = -1;
|
||||
cg_var *cvi = NULL;
|
||||
cxobj *xi;
|
||||
|
||||
if (cprintf(cb, "<rpc-reply><rpc-error>"
|
||||
"<error-type>protocol</error-type>"
|
||||
"<error-tag>operation-failed</error-tag>"
|
||||
"<error-app-tag>data-not-unique</error-app-tag>"
|
||||
"<error-severity>error</error-severity>"
|
||||
"<error-info>") < 0)
|
||||
goto err;
|
||||
while ((cvi = cvec_each(cvk, cvi)) != NULL){
|
||||
if ((xi = xml_find(x, cv_string_get(cvi))) == NULL)
|
||||
continue; /* ignore, shouldnt happen */
|
||||
cprintf(cb, "<non-unique>");
|
||||
clicon_xml2cbuf(cb, xi, 0, 0);
|
||||
cprintf(cb, "</non-unique>");
|
||||
}
|
||||
if (cprintf(cb, "</error-info></rpc-error></rpc-reply>") <0)
|
||||
goto err;
|
||||
retval = 0;
|
||||
done:
|
||||
return retval;
|
||||
err:
|
||||
clicon_err(OE_XML, errno, "cprintf");
|
||||
goto done;
|
||||
}
|
||||
|
||||
/*! Help function: merge - check yang - if error make netconf errmsg
|
||||
* @param[in] x XML tree
|
||||
* @param[in] yspec Yang spec
|
||||
|
|
@ -1006,6 +1048,7 @@ netconf_trymerge(cxobj *x,
|
|||
return retval;
|
||||
}
|
||||
|
||||
|
||||
/*! Load ietf netconf yang module and set enabled features
|
||||
* The features added are (in order):
|
||||
* candidate (8.3)
|
||||
|
|
|
|||
|
|
@ -633,6 +633,167 @@ check_mandatory(cxobj *xt,
|
|||
goto done;
|
||||
}
|
||||
|
||||
/*! New elemnt last in list, check if already exists if sp return -1
|
||||
* @param[in] vec Vector of existing entries (new is last)
|
||||
* @param[in] i1 The new entry is placed at vec[i1]
|
||||
* @param[in] vlen Lenght of entry
|
||||
* @retval 0 OK, entry is unique
|
||||
* @retval -1 Duplicate detected
|
||||
* @note This is currently linear complexity. It could be improved by inserting new element sorted and binary search.
|
||||
*/
|
||||
static int
|
||||
check_insert_duplicate(char **vec,
|
||||
int i1,
|
||||
int vlen)
|
||||
{
|
||||
int i;
|
||||
int v;
|
||||
char *b;
|
||||
|
||||
for (i=0; i<i1; i++){
|
||||
for (v=0; v<vlen; v++){
|
||||
b = vec[i*vlen+v];
|
||||
if (b == NULL || strcmp(b, vec[i1*vlen+v]))
|
||||
break;
|
||||
}
|
||||
if (v==vlen) /* duplicate */
|
||||
break;
|
||||
}
|
||||
return i==i1?0:-1;
|
||||
}
|
||||
|
||||
/*! Given a list with unique constraint, detect duplicates
|
||||
* @param[in] x The first element in the list (on return the last)
|
||||
* @param[in] xt The parent of x
|
||||
* @param[in] y Its yang spec (Y_LIST)
|
||||
* @param[in] yu A yang unique spec (Y_UNIQUE)
|
||||
* @param[out] cbret Error buffer (set w netconf error if retval == 0)
|
||||
* @retval 1 Validation OK
|
||||
* @retval 0 Validation failed (cbret set)
|
||||
* @retval -1 Error
|
||||
* @note It would be possible to cache the vector built below
|
||||
*/
|
||||
static int
|
||||
check_unique_list(cxobj *x,
|
||||
cxobj *xt,
|
||||
yang_stmt *y,
|
||||
yang_stmt *yu,
|
||||
cbuf *cbret)
|
||||
{
|
||||
int retval = -1;
|
||||
cvec *cvk; /* unique vector */
|
||||
cg_var *cvi; /* unique node name */
|
||||
cxobj *xi;
|
||||
char **vec = NULL; /* 2xmatrix */
|
||||
int vlen;
|
||||
int i;
|
||||
int v;
|
||||
char *bi;
|
||||
|
||||
cvk = yang_cvec_get(yu);
|
||||
vlen = cvec_len(cvk); /* nr of unique elements to check */
|
||||
if ((vec = calloc(vlen*xml_child_nr(xt), sizeof(char*))) == NULL){
|
||||
clicon_err(OE_UNIX, errno, "calloc");
|
||||
goto done;
|
||||
}
|
||||
i = 0; /* x element index */
|
||||
do {
|
||||
cvi = NULL;
|
||||
v = 0; /* index in each tuple */
|
||||
while ((cvi = cvec_each(cvk, cvi)) != NULL){
|
||||
/* RFC7950: Sec 7.8.3.1: entries that do not have value for all
|
||||
* referenced leafs are not taken into account */
|
||||
if ((xi = xml_find(x, cv_string_get(cvi))) ==NULL)
|
||||
break;
|
||||
if ((bi = xml_body(xi)) == NULL)
|
||||
break;
|
||||
vec[i*vlen + v++] = bi;
|
||||
}
|
||||
if (cvi==NULL){
|
||||
/* Last element (i) is newly inserted, see if it is already there */
|
||||
if (check_insert_duplicate(vec, i, vlen) < 0){
|
||||
if (netconf_data_not_unique(cbret, x, cvk) < 0)
|
||||
goto done;
|
||||
goto fail;
|
||||
}
|
||||
}
|
||||
x = xml_child_each(xt, x, CX_ELMNT);
|
||||
i++;
|
||||
} while (x && y == xml_spec(x)); /* stop if list ends, others may follow */
|
||||
/* It would be possible to cache vec here as an optimization */
|
||||
retval = 1;
|
||||
done:
|
||||
if (vec)
|
||||
free(vec);
|
||||
return retval;
|
||||
fail:
|
||||
retval = 0;
|
||||
goto done;
|
||||
}
|
||||
|
||||
/*! Detect unique constraint for duplicates from parent node
|
||||
* @param[in] xt XML parent (may have lists w unique constraints as child)
|
||||
* @param[out] cbret Error buffer (set w netconf error if retval == 0)
|
||||
* @retval 1 Validation OK
|
||||
* @retval 0 Validation failed (cbret set)
|
||||
* @retval -1 Error
|
||||
* Assume xt:s children are sorted and yang populated.
|
||||
* The routine finds the lists: ie xt may have several lists in the children,
|
||||
* example
|
||||
* shows two lists [x1,..] and [x2,..].
|
||||
* xt: {a, b, [x1, x1, x1], d, e, f, [x2, x2, x2], g}
|
||||
* Then call check_unique_list on each list.
|
||||
* The function does this using a single iteration and uses the fact that the
|
||||
* xml symbols share yang symbols: ie [x1..] has yang y1 and d has yd.
|
||||
* Example, x has an associated yang list node with list of unique constraints
|
||||
* y-list->y-unique - "a"
|
||||
* xt->x -> ab
|
||||
* x -> bc
|
||||
* x -> ab
|
||||
*/
|
||||
static int
|
||||
check_unique_parent(cxobj *xt,
|
||||
cbuf *cbret)
|
||||
{
|
||||
int retval = -1;
|
||||
cxobj *x = NULL;
|
||||
yang_stmt *y;
|
||||
yang_stmt *yp = NULL; /* previous in list */
|
||||
yang_stmt *yu;
|
||||
int ret;
|
||||
|
||||
/* */
|
||||
while ((x = xml_child_each(xt, x, CX_ELMNT)) != NULL) {
|
||||
if ((y = xml_spec(x)) == NULL)
|
||||
continue;
|
||||
if (y == yp) /* If same yang as previous x, then skip (eg same list) */
|
||||
continue;
|
||||
yp = y;
|
||||
if (yang_keyword_get(y) != Y_LIST)
|
||||
continue;
|
||||
yu = NULL;
|
||||
while ((yu = yn_each(y, yu)) != NULL) {
|
||||
if (yang_keyword_get(yu) != Y_UNIQUE)
|
||||
continue;
|
||||
/* Here is a list w unique constraints identified by:
|
||||
* its first element x, its yang spec y, its parent xt, and
|
||||
* a unique yang spec yu,
|
||||
*/
|
||||
if ((ret = check_unique_list(x, xt, y, yu, cbret)) < 0)
|
||||
goto done;
|
||||
if (ret == 0)
|
||||
goto fail;
|
||||
}
|
||||
}
|
||||
retval = 1;
|
||||
done:
|
||||
return retval;
|
||||
fail:
|
||||
retval = 0;
|
||||
goto done;
|
||||
}
|
||||
|
||||
|
||||
/*! Validate a single XML node with yang specification for added entry
|
||||
* 1. Check if mandatory leafs present as subs.
|
||||
* 2. Check leaf values, eg int ranges and string regexps.
|
||||
|
|
@ -831,6 +992,10 @@ xml_yang_validate_all(cxobj *xt,
|
|||
default:
|
||||
break;
|
||||
}
|
||||
if ((ret = check_unique_parent(xt, cbret)) < 0)
|
||||
goto done;
|
||||
if (ret == 0)
|
||||
goto fail;
|
||||
/* must sub-node RFC 7950 Sec 7.5.3. Can be several.
|
||||
* XXX. use yang path instead? */
|
||||
yc = NULL;
|
||||
|
|
|
|||
|
|
@ -1213,7 +1213,6 @@ yang_print_cbuf(cbuf *cb,
|
|||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/*! Populate yang leafs after parsing. Create cv and fill it in.
|
||||
*
|
||||
* Populate leaf in 2nd round of yang parsing, now that context is complete:
|
||||
|
|
@ -1308,7 +1307,8 @@ ys_populate_list(yang_stmt *ys,
|
|||
|
||||
if ((ykey = yang_find(ys, Y_KEY, NULL)) == NULL)
|
||||
return 0;
|
||||
cvec_free(ys->ys_cvec);
|
||||
if (ys->ys_cvec)
|
||||
cvec_free(ys->ys_cvec);
|
||||
if ((ys->ys_cvec = yang_arg2cvec(ykey, " ")) == NULL)
|
||||
return -1;
|
||||
return 0;
|
||||
|
|
@ -1681,6 +1681,18 @@ ys_populate_feature(clicon_handle h,
|
|||
return retval;
|
||||
}
|
||||
|
||||
/*! Populate the unique statement with a cvec
|
||||
*/
|
||||
static int
|
||||
ys_populate_unique(yang_stmt *ys)
|
||||
{
|
||||
if (ys->ys_cvec)
|
||||
cvec_free(ys->ys_cvec);
|
||||
if ((ys->ys_cvec = yang_arg2cvec(ys, " ")) == NULL)
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*! Populate unknown node with extension
|
||||
*/
|
||||
static int
|
||||
|
|
@ -1770,6 +1782,10 @@ ys_populate(yang_stmt *ys,
|
|||
if (ys_populate_identity(ys, NULL) < 0)
|
||||
goto done;
|
||||
break;
|
||||
case Y_UNIQUE:
|
||||
if (ys_populate_unique(ys) < 0)
|
||||
goto done;
|
||||
break;
|
||||
case Y_UNKNOWN:
|
||||
if (ys_populate_unknown(ys) < 0)
|
||||
goto done;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue