* startup_extraxml triggers unnecessary validation

* Renamed startup_db_reset -> xmldb_db_reset (its a general function)
  * In startup_extraxml(), check if reset callbacks or extraxml file actually makes and changes to the tmp db.
This commit is contained in:
Olof hagsand 2019-06-14 22:01:30 +02:00
parent daa01b3a5e
commit 3bad8bc874
8 changed files with 48 additions and 28 deletions

View file

@ -191,6 +191,9 @@
### Minor changes ### Minor changes
* `startup_extraxml` triggers unnecessary validation
* Renamed startup_db_reset -> xmldb_db_reset (its a general function)
* In startup_extraxml(), check if reset callbacks or extraxml file actually makes and changes to the tmp db.
* Print CLICON_YANG_DIR and CLICON_FEATURE lists on startup with debug flag * Print CLICON_YANG_DIR and CLICON_FEATURE lists on startup with debug flag
* Extended `util/clixon_util_xml` with yang and validate functionality so it can be used as a stand-alone utility for validating XML/JSON files * Extended `util/clixon_util_xml` with yang and validate functionality so it can be used as a stand-alone utility for validating XML/JSON files
* JSON parse and print improvements * JSON parse and print improvements

View file

@ -642,7 +642,7 @@ main(int argc,
switch (startup_mode){ switch (startup_mode){
case SM_INIT: /* Scratch running and start from empty */ case SM_INIT: /* Scratch running and start from empty */
/* [Delete and] create running db */ /* [Delete and] create running db */
if (startup_db_reset(h, "running") < 0) if (xmldb_db_reset(h, "running") < 0)
goto done; goto done;
case SM_NONE: /* Fall through * case SM_NONE: /* Fall through *
* Load plugins and call plugin_init() */ * Load plugins and call plugin_init() */

View file

@ -70,22 +70,6 @@
#include "backend_commit.h" #include "backend_commit.h"
#include "backend_startup.h" #include "backend_startup.h"
/*! Create an XML database. If it exists already, delete it before creating
* @param[in] h Clixon handle
* @param[in] db Symbolic database name, eg "candidate", "running"
*/
int
startup_db_reset(clicon_handle h,
char *db)
{
if (xmldb_exists(h, db) == 1){
if (xmldb_delete(h, db) != 0 && errno != ENOENT)
return -1;
}
if (xmldb_create(h, db) < 0)
return -1;
return 0;
}
/*! Merge db1 into db2 without commit /*! Merge db1 into db2 without commit
* @retval -1 Error * @retval -1 Error
@ -235,34 +219,45 @@ startup_extraxml(clicon_handle h,
cbuf *cbret) cbuf *cbret)
{ {
int retval = -1; int retval = -1;
char *db = "tmp"; char *tmp_db = "tmp";
int ret; int ret;
cxobj *xt = NULL; /* Potentially upgraded XML */ cxobj *xt = NULL; /* Potentially upgraded XML */
/* Clear tmp db */ /* Clear tmp db */
if (startup_db_reset(h, db) < 0) if (xmldb_db_reset(h, tmp_db) < 0)
goto done; goto done;
/* Application may define extra xml in its reset function*/ /* Application may define extra xml in its reset function*/
if (clixon_plugin_reset(h, db) < 0) if (clixon_plugin_reset(h, tmp_db) < 0)
goto done; goto done;
/* Extra XML can also be added via file */ /* Extra XML can also be added via file */
if (file){ if (file){
/* Parse and load file into tmp db */ /* Parse and load file into tmp db */
if ((ret = load_extraxml(h, file, db, cbret)) < 0) if ((ret = load_extraxml(h, file, tmp_db, cbret)) < 0)
goto done; goto done;
if (ret == 0) if (ret == 0)
goto fail; goto fail;
} }
/*
* Check if tmp db is empty.
* It should be empty if extra-xml is null and reset plugins did nothing
* then skip validation.
*/
if (xmldb_get(h, tmp_db, NULL, &xt) < 0)
goto done;
if (xt==NULL || xml_child_nr(xt)==0)
goto ok;
xml_free(xt);
xt = NULL;
/* Validate the tmp db and return possibly upgraded xml in xt /* Validate the tmp db and return possibly upgraded xml in xt
*/ */
if ((ret = startup_validate(h, db, &xt, cbret)) < 0) if ((ret = startup_validate(h, tmp_db, &xt, cbret)) < 0)
goto done; goto done;
if (ret == 0) if (ret == 0)
goto fail; goto fail;
if (xt==NULL || xml_child_nr(xt)==0) if (xt==NULL || xml_child_nr(xt)==0)
goto ok; goto ok;
/* Merge tmp into running (no commit) */ /* Merge tmp into running (no commit) */
if ((ret = db_merge(h, db, "running", cbret)) < 0) if ((ret = db_merge(h, tmp_db, "running", cbret)) < 0)
goto fail; goto fail;
if (ret == 0) if (ret == 0)
goto fail; goto fail;
@ -270,7 +265,7 @@ startup_extraxml(clicon_handle h,
retval = 1; retval = 1;
done: done:
xmldb_get0_free(h, &xt); xmldb_get0_free(h, &xt);
if (xmldb_delete(h, db) != 0 && errno != ENOENT) if (xmldb_delete(h, tmp_db) != 0 && errno != ENOENT)
return -1; return -1;
return retval; return retval;
fail: fail:
@ -306,7 +301,7 @@ startup_failsafe(clicon_handle h)
/* Copy original running to tmp as backup (restore if error) */ /* Copy original running to tmp as backup (restore if error) */
if (xmldb_copy(h, "running", "tmp") < 0) if (xmldb_copy(h, "running", "tmp") < 0)
goto done; goto done;
if (startup_db_reset(h, "running") < 0) if (xmldb_db_reset(h, "running") < 0)
goto done; goto done;
ret = candidate_commit(h, db, cbret); ret = candidate_commit(h, db, cbret);
if (ret != 1) if (ret != 1)

View file

@ -40,7 +40,6 @@
/* /*
* Prototypes * Prototypes
*/ */
int startup_db_reset(clicon_handle h, char *db);
int startup_mode_startup(clicon_handle h, char *db, cbuf *cbret); int startup_mode_startup(clicon_handle h, char *db, cbuf *cbret);
int startup_extraxml(clicon_handle h, char *file, cbuf *cbret); int startup_extraxml(clicon_handle h, char *file, cbuf *cbret);
int startup_failsafe(clicon_handle h); int startup_failsafe(clicon_handle h);

View file

@ -62,5 +62,7 @@ int xmldb_islocked(clicon_handle h, const char *db);
int xmldb_exists(clicon_handle h, const char *db); int xmldb_exists(clicon_handle h, const char *db);
int xmldb_delete(clicon_handle h, const char *db); int xmldb_delete(clicon_handle h, const char *db);
int xmldb_create(clicon_handle h, const char *db); int xmldb_create(clicon_handle h, const char *db);
/* utility functions */
int xmldb_db_reset(clicon_handle h, char *db);
#endif /* _CLIXON_DATASTORE_H */ #endif /* _CLIXON_DATASTORE_H */

View file

@ -78,6 +78,7 @@
#include "clixon_datastore_write.h" #include "clixon_datastore_write.h"
#include "clixon_datastore_read.h" #include "clixon_datastore_read.h"
/*! Translate from symbolic database name to actual filename in file-system /*! Translate from symbolic database name to actual filename in file-system
* @param[in] th text handle handle * @param[in] th text handle handle
* @param[in] db Symbolic database name, eg "candidate", "running" * @param[in] db Symbolic database name, eg "candidate", "running"
@ -446,3 +447,21 @@ xmldb_create(clicon_handle h,
close(fd); close(fd);
return retval; return retval;
} }
/*! Create an XML database. If it exists already, delete it before creating
* Utility function.
* @param[in] h Clixon handle
* @param[in] db Symbolic database name, eg "candidate", "running"
*/
int
xmldb_db_reset(clicon_handle h,
char *db)
{
if (xmldb_exists(h, db) == 1){
if (xmldb_delete(h, db) != 0 && errno != ENOENT)
return -1;
}
if (xmldb_create(h, db) < 0)
return -1;
return 0;
}

View file

@ -641,7 +641,8 @@ xmldb_get_zerocopy(clicon_handle h,
* @param[in] db Name of database to search in (filename including dir path * @param[in] db Name of database to search in (filename including dir path
* @param[in] xpath String with XPATH syntax. or NULL for all * @param[in] xpath String with XPATH syntax. or NULL for all
* @param[out] xret Single return XML tree. Free with xml_free() * @param[out] xret Single return XML tree. Free with xml_free()
* @retval 0 OK
* @retval -1 Error
* @code * @code
* if (xmldb_get(xh, "running", "/interfaces/interface[name="eth"]", &xt) < 0) * if (xmldb_get(xh, "running", "/interfaces/interface[name="eth"]", &xt) < 0)
* err; * err;

View file

@ -87,6 +87,7 @@ checklog(){
s=$1 # statement s=$1 # statement
l0=$2 # linenr l0=$2 # linenr
new "Check $s in log" new "Check $s in log"
# echo "grep \"transaction_log $s\" $flog"
t=$(grep -n "transaction_log $s" $flog) t=$(grep -n "transaction_log $s" $flog)
if [ -z "$t" ]; then if [ -z "$t" ]; then
echo -e "\e[31m\nError in Test$testnr [$testname]:" echo -e "\e[31m\nError in Test$testnr [$testname]:"
@ -124,7 +125,7 @@ if [ $BE -ne 0 ]; then
sleep $RCWAIT sleep $RCWAIT
fi fi
let nr=1 let nr=0
new "Basic transaction to add top-level x" new "Basic transaction to add top-level x"
expecteof "$clixon_netconf -qf $cfg" 0 "<rpc><edit-config><target><candidate/></target><config><x xmlns='urn:example:clixon'><y><a>$nr</a></y></x></config></edit-config></rpc>]]>]]>" '^<rpc-reply><ok/></rpc-reply>]]>]]>$' expecteof "$clixon_netconf -qf $cfg" 0 "<rpc><edit-config><target><candidate/></target><config><x xmlns='urn:example:clixon'><y><a>$nr</a></y></x></config></edit-config></rpc>]]>]]>" '^<rpc-reply><ok/></rpc-reply>]]>]]>$'