* Made a separate Clixon datastore XML/JSON top-level symbol

* Replaces the hardcoded "config" keyword.
  * Implemented by a compile-time option called `DATASTORE_TOP_SYMBOL` option in clixon_custom.h
* Tests: added endtest to all tests. Removed all premature exits if BE=0
This commit is contained in:
Olof hagsand 2021-03-05 14:15:15 +01:00
parent 2ab90d847b
commit b7991d9b39
132 changed files with 939 additions and 628 deletions

View file

@ -443,7 +443,7 @@ client_get_config_only(clicon_handle h,
if (xret==NULL)
cprintf(cbret, "<data/>");
else{
if (xml_name_set(xret, "data") < 0)
if (xml_name_set(xret, NETCONF_OUTPUT_DATA) < 0)
goto done;
if (clicon_xml2cbuf(cbret, xret, 0, 0, depth>0?depth+1:depth) < 0)
goto done;
@ -638,10 +638,13 @@ from_client_edit_config(clicon_handle h,
}
}
/* Get config element */
if ((xc = xpath_first(xn, nsc, "%s%sconfig", prefix?prefix:"", prefix?":":"")) == NULL){
if ((xc = xpath_first(xn, nsc, "%s%s%s",
prefix?prefix:"",
prefix?":":"",
NETCONF_INPUT_CONFIG)) == NULL){
cprintf(cbx, "Element not found, or mismatching prefix %s for namespace %s",
prefix?prefix:"null", NETCONF_BASE_NAMESPACE);
if (netconf_missing_element(cbret, "protocol", "config", cbuf_get(cbx)) < 0)
if (netconf_missing_element(cbret, "protocol", NETCONF_INPUT_CONFIG, cbuf_get(cbx)) < 0)
goto done;
goto ok;
}
@ -1215,7 +1218,7 @@ from_client_get(clicon_handle h,
if (xret==NULL)
cprintf(cbret, "<data/>");
else{
if (xml_name_set(xret, "data") < 0)
if (xml_name_set(xret, NETCONF_OUTPUT_DATA) < 0)
goto done;
/* Top level is data, so add 1 to depth if significant */
if (clicon_xml2cbuf(cbret, xret, 0, 0, depth>0?depth+1:depth) < 0)

View file

@ -399,32 +399,37 @@ startup_commit(clicon_handle h,
goto done;
if (ret == 0)
goto fail;
/* 8. Call plugin transaction commit callbacks */
if (plugin_transaction_commit_all(h, td) < 0)
goto done;
/* After commit, make a post-commit call (sure that all plugins have committed) */
if (plugin_transaction_commit_done_all(h, td) < 0)
goto done;
/* Clear cached trees from default values and marking */
if (xmldb_get0_clear(h, td->td_target) < 0)
goto done;
/* 8. Call plugin transaction commit callbacks */
if (plugin_transaction_commit_all(h, td) < 0)
goto done;
/* After commit, make a post-commit call (sure that all plugins have committed) */
if (plugin_transaction_commit_done_all(h, td) < 0)
goto done;
/* Clear cached trees from default values and marking */
if (xmldb_get0_clear(h, td->td_target) < 0)
goto done;
/* [Delete and] create running db */
if (xmldb_exists(h, "running") == 1){
/* [Delete and] create running db */
if (xmldb_exists(h, "running") == 1){
if (xmldb_delete(h, "running") != 0 && errno != ENOENT)
goto done;;
}
if (xmldb_create(h, "running") < 0)
goto done;
/* 9, write (potentially modified) tree to running
* XXX note here startup is copied to candidate, which may confuse everything
* XXX default values are overwritten
*/
if ((ret = xmldb_put(h, "running", OP_REPLACE, td->td_target,
clicon_username_get(h), cbret)) < 0)
goto done;
if (ret == 0)
goto fail;
/* 9, write (potentially modified) tree to running
* XXX note here startup is copied to candidate, which may confuse everything
* XXX default values are overwritten
*/
if (td->td_target)
/* target is datastore, but is here transformed to mimic an incoming
* edit-config
*/
xml_name_set(td->td_target, NETCONF_INPUT_CONFIG);
if ((ret = xmldb_put(h, "running", OP_REPLACE, td->td_target,
clicon_username_get(h), cbret)) < 0)
goto done;
if (ret == 0)
goto fail;
/* 10. Call plugin transaction end callbacks */
plugin_transaction_end_all(h, td);
retval = 1;
@ -432,8 +437,8 @@ startup_commit(clicon_handle h,
if (td){
if (retval < 1)
plugin_transaction_abort_all(h, td);
xmldb_get0_free(h, &td->td_target);
transaction_free(td);
xmldb_get0_free(h, &td->td_target);
transaction_free(td);
}
return retval;
fail: /* cbret should be set */

View file

@ -255,7 +255,7 @@ clixon_plugin_statedata_one(clixon_plugin *cp,
cxobj *x = NULL;
if ((fn = cp->cp_api.ca_statedata) != NULL){
if ((x = xml_new("config", NULL, CX_ELMNT)) == NULL)
if ((x = xml_new(XML_TOP_SYMBOL, NULL, CX_ELMNT)) == NULL)
goto done;
if (fn(h, nsc, xpath, x) < 0){
if (clicon_errno < 0)

View file

@ -88,6 +88,7 @@ db_merge(clicon_handle h,
/* Get data as xml from db1 */
if (xmldb_get0(h, (char*)db1, YB_MODULE, NULL, NULL, 0, &xt, NULL) < 0)
goto done;
xml_name_set(xt, NETCONF_INPUT_CONFIG);
/* Merge xml into db2. Without commit */
retval = xmldb_put(h, (char*)db2, OP_MERGE, xt, clicon_username_get(h), cbret);
done:
@ -168,8 +169,10 @@ load_extraxml(clicon_handle h,
{
int retval = -1;
cxobj *xt = NULL;
cxobj *xerr = NULL;
FILE *fp = NULL;
yang_stmt *yspec = NULL;
int ret;
if (filename == NULL)
return 1;
@ -178,11 +181,25 @@ load_extraxml(clicon_handle h,
goto done;
}
yspec = clicon_dbspec_yang(h);
if (clixon_xml_parse_file(fp, YB_MODULE, yspec, NULL, &xt, NULL) < 0)
/* No yang check yet because it has <config> as top symbol, do it later after that is removed */
if (clixon_xml_parse_file(fp, YB_NONE, yspec, NULL, &xt, &xerr) < 0)
goto done;
/* Replace parent w first child */
if (xml_rootchild(xt, 0, &xt) < 0)
goto done;
/* Ensure edit-config "config" statement */
if (xt)
xml_name_set(xt, NETCONF_INPUT_CONFIG);
/* Now we can yang bind */
if ((ret = xml_bind_yang(xt, YB_MODULE, yspec, &xerr)) < 0)
goto done;
if (ret == 0){
if (netconf_err2cb(xerr, cbret) < 0)
goto done;
retval = 0;
goto done;
}
/* Merge user reset state */
retval = xmldb_put(h, (char*)db, OP_MERGE, xt, clicon_username_get(h), cbret);
done:

View file

@ -269,7 +269,7 @@ cli_dbxml(clicon_handle h,
if (api_path_fmt2api_path(api_path_fmt, cvv, &api_path, &cvv_i) < 0)
goto done;
/* Create config top-of-tree */
if ((xtop = xml_new("config", NULL, CX_ELMNT)) == NULL)
if ((xtop = xml_new(NETCONF_INPUT_CONFIG, NULL, CX_ELMNT)) == NULL)
goto done;
xbot = xtop;
if (api_path){
@ -834,8 +834,8 @@ load_config_file(clicon_handle h,
goto done;
x = NULL;
while ((x = xml_child_each(xt, x, -1)) != NULL) {
/* Ensure top-level is "config", maybe this is too rough? */
xml_name_set(x, "config");
/* Read as datastore-top but transformed into an edit-config "config" */
xml_name_set(x, NETCONF_INPUT_CONFIG);
if (clicon_xml2cbuf(cbxml, x, 0, 0, -1) < 0)
goto done;
}
@ -919,7 +919,7 @@ save_config_file(clicon_handle h,
/* get-config returns a <data> tree. Save as <config> tree so it can be used
* as data-store.
*/
if (xml_name_set(xt, "config") < 0)
if (xml_name_set(xt, DATASTORE_TOP_SYMBOL) < 0)
goto done;
if ((f = fopen(filename, "w")) == NULL){
clicon_err(OE_CFG, errno, "Creating file %s", filename);
@ -1166,7 +1166,7 @@ cli_unlock(clicon_handle h,
/*! Copy one configuration object to antother
*
* Works for objects that are items ina yang list with a keyname, eg as:
* Works for objects that are items in a yang list with a keyname, eg as:
* list sender{
* key name;
* leaf name{...
@ -1270,11 +1270,11 @@ cli_copy_config(clicon_handle h,
}
toname = cv_string_get(tocv);
/* Create copy xml tree x2 */
if ((x2 = xml_new("config", NULL, CX_ELMNT)) == NULL)
if ((x2 = xml_new(NETCONF_INPUT_CONFIG, NULL, CX_ELMNT)) == NULL)
goto done;
if (xml_copy(x1, x2) < 0)
goto done;
xml_name_set(x2, "config");
xml_name_set(x2, NETCONF_INPUT_CONFIG);
cprintf(cb, "/%s", keyname);
if ((x = xpath_first(x2, nsc, "%s", cbuf_get(cb))) == NULL){

View file

@ -166,7 +166,7 @@ expand_dbvar(void *h,
xcur = xt; /* default top-of-tree */
xpathcur = xpath;
/* Create config top-of-tree */
if ((xtop = xml_new("config", NULL, CX_ELMNT)) == NULL)
if ((xtop = xml_new(DATASTORE_TOP_SYMBOL, NULL, CX_ELMNT)) == NULL)
goto done;
xbot = xtop;
/* This is primarily to get "y",

View file

@ -716,7 +716,7 @@ usage(clicon_handle h,
fprintf(stderr, "usage:%s [options]\n"
"where options are\n"
"\t-h \t\t Help\n"
"\t-D <level>\t Debug level _ overrides any config debug setting\n"
"\t-D <level>\t Debug level, overrides any config debug setting\n"
"\t-f <file>\t Configuration file (mandatory)\n"
"\t-E <dir> \t Extra configuration file directory\n"
"\t-l <s|f<file>> \t Log on (s)yslog, (f)ile (syslog is default)\n"

View file

@ -305,7 +305,7 @@ api_data_write(clicon_handle h,
else
op = OP_REPLACE; /* OP_CREATE if it does not exist */
/* Create config top-of-tree */
if ((xtop = xml_new("config", NULL, CX_ELMNT)) == NULL)
if ((xtop = xml_new(NETCONF_INPUT_CONFIG, NULL, CX_ELMNT)) == NULL)
goto done;
/* Translate api_path to xml in the form of xtop/xbot */
xbot = xtop;
@ -480,7 +480,7 @@ api_data_write(clicon_handle h,
*/
dname = xml_name(xdata);
if (api_path==NULL) {
if (strcmp(dname, "data")!=0){
if (strcmp(dname, NETCONF_OUTPUT_DATA)!=0){
if (netconf_bad_element_xml(&xerr, "application", dname,
"Data element does not match top-level data") < 0)
goto done;
@ -497,7 +497,7 @@ api_data_write(clicon_handle h,
if (xtop) /* also xbot */
xml_free(xtop);
xtop = xdata;
xml_name_set(xtop, "config");
xml_name_set(xtop, NETCONF_INPUT_CONFIG);
/* remove default namespace */
if ((xa = xml_find_type(xtop, NULL, "xmlns", CX_ATTR)) != NULL){
if (xml_rm(xa) < 0)
@ -818,7 +818,7 @@ api_data_delete(clicon_handle h,
for (i=0; i<pi; i++)
api_path = index(api_path+1, '/');
/* Create config top-of-tree */
if ((xtop = xml_new("config", NULL, CX_ELMNT)) == NULL)
if ((xtop = xml_new(NETCONF_INPUT_CONFIG, NULL, CX_ELMNT)) == NULL)
goto done;
xbot = xtop;
if (api_path){

View file

@ -188,7 +188,7 @@ api_data_post(clicon_handle h,
for (i=0; i<pi; i++)
api_path = index(api_path+1, '/');
/* Create config top-of-tree */
if ((xtop = xml_new("config", NULL, CX_ELMNT)) == NULL)
if ((xtop = xml_new(NETCONF_INPUT_CONFIG, NULL, CX_ELMNT)) == NULL)
goto done;
/* Translate api_path to xtop/xbot */
xbot = xtop;
@ -524,7 +524,7 @@ api_operations_post_input(clicon_handle h,
goto fail;
break;
} /* switch media_in */
xml_name_set(xdata, "data");
xml_name_set(xdata, NETCONF_OUTPUT_DATA);
/* Here xdata is:
* <data><input xmlns="urn:example:clixon">...</input></data>
*/

View file

@ -471,7 +471,7 @@ api_root_restconf(clicon_handle h,
if (api_yang_library_version(h, req, pretty, media_out) < 0)
goto done;
}
else if (strcmp(api_resource, "data") == 0){ /* restconf, skip /api/data */
else if (strcmp(api_resource, NETCONF_OUTPUT_DATA) == 0){ /* restconf, skip /api/data */
if (api_data(h, req, path, pcvec, 2, qvec, indata,
pretty, media_out, IETF_DS_NONE) < 0)
goto done;