Fixed IDENTITYREF_KLUDGE removal #2

This commit is contained in:
Olof hagsand 2022-11-07 09:19:29 +01:00
parent b0f898cf66
commit 4b21a05bcc
5 changed files with 62 additions and 17 deletions

View file

@ -211,6 +211,53 @@ dbxml_body(cxobj *xbot,
return retval;
}
/*! Special handling of identityref:s whose body may be: <namespace prefix>:<id>
* Ensure the namespace is declared if it exists in YANG
*/
static int
identityref_add_ns(cxobj *x,
void *arg)
{
int retval = -1;
yang_stmt *yspec = (yang_stmt *)arg;
yang_stmt *y;
yang_stmt *yrestype; /* resolved type */
char *restype; /* resolved type */
char *origtype = NULL; /* original type */
char *pf = NULL;
yang_stmt *yns;
char *ns = NULL;
if ((y = xml_spec(x)) != NULL &&
yang_keyword_get(y) == Y_LEAF){
if (yang_type_get(y, &origtype, &yrestype, NULL, NULL, NULL, NULL, NULL) < 0)
goto done;
restype = yrestype?yang_argument_get(yrestype):NULL;
if (strcmp(restype, "identityref") == 0){
if (nodeid_split(xml_body(x), &pf, NULL) < 0)
goto done;
// search if already defined
if (pf != NULL){
if (xml2ns(x, pf, &ns) < 0)
goto done;
if (ns == NULL &&
(yns = yang_find_module_by_prefix_yspec(yspec, pf)) != NULL){
if ((ns = yang_find_mynamespace(yns)) != NULL)
if (xmlns_set(x, pf, ns) < 0)
goto done;
}
}
}
}
retval = 0;
done:
if (origtype)
free(origtype);
if (pf)
free(pf);
return retval;
}
/*! Modify xml datastore from a callback using xml key format strings
* @param[in] h Clicon handle
* @param[in] cvv Vector of cli string and instantiated variables
@ -229,7 +276,7 @@ dbxml_body(cxobj *xbot,
* @see cli_callback_generate where arg is generated
* @note The last value may require namespace binding present in nsctx. Note that the nsctx
* cannot normally be supplied by the clispec functions, such as cli_set, but need to be
* generated by afunction such as clixon_instance_id_bind() or other programmatically.
* generated by a function such as clixon_instance_id_bind() or other programmatically.
*/
int
cli_dbxml(clicon_handle h,
@ -270,7 +317,6 @@ cli_dbxml(clicon_handle h,
/* Transform template format string + cvv to actual api-path
* cvv_i indicates if all cvv entries were used
*/
if (api_path_fmt2api_path(api_path_fmt, cvv, &api_path, &cvv_i) < 0)
goto done;
/* Create config top-of-tree */
@ -321,6 +367,11 @@ cli_dbxml(clicon_handle h,
goto done;
}
}
/* Special handling of identityref:s whose body may be: <namespace prefix>:<id>
* Ensure the namespace is declared if it exists in YANG
*/
if ((ret = xml_apply0(xbot, CX_ELMNT, identityref_add_ns, yspec)) < 0)
goto done;
if ((cb = cbuf_new()) == NULL){
clicon_err(OE_XML, errno, "cbuf_new");
goto done;