- removed api_path extension from internal netconf
- Strings in xmldb_put not properly encoded, eg eth/0 became eth.00000
This commit is contained in:
parent
683376610c
commit
3453dae0db
36 changed files with 1048 additions and 1307 deletions
|
|
@ -89,7 +89,7 @@ usage(char *argv0)
|
|||
"\t-m <module>\tYang module. Mandatory\n"
|
||||
"and command is either:\n"
|
||||
"\tget <xpath>\n"
|
||||
"\tput (merge|replace|create|delete|remove) <api_path> <xml>\n"
|
||||
"\tput (merge|replace|create|delete|remove) <xml>\n"
|
||||
"\tcopy <todb>\n"
|
||||
"\tlock <pid>\n"
|
||||
"\tunlock\n"
|
||||
|
|
@ -220,7 +220,7 @@ main(int argc, char **argv)
|
|||
fprintf(stdout, "\n");
|
||||
}
|
||||
else if (strcmp(cmd, "put")==0){
|
||||
if (argc != 3 && argc != 4){
|
||||
if (argc != 3){
|
||||
clicon_err(OE_DB, 0, "Unexpected nr of args: %d", argc);
|
||||
usage(argv0);
|
||||
}
|
||||
|
|
@ -228,13 +228,11 @@ main(int argc, char **argv)
|
|||
clicon_err(OE_DB, 0, "Unrecognized operation: %s", argv[1]);
|
||||
usage(argv0);
|
||||
}
|
||||
if (argc == 4){
|
||||
if (clicon_xml_parse_str(argv[3], &xt) < 0)
|
||||
goto done;
|
||||
if (xml_rootchild(xt, 0, &xt) < 0)
|
||||
goto done;
|
||||
}
|
||||
if (xmldb_put(h, db, op, argv[2], xt) < 0)
|
||||
if (clicon_xml_parse_str(argv[2], &xt) < 0)
|
||||
goto done;
|
||||
if (xml_rootchild(xt, 0, &xt) < 0)
|
||||
goto done;
|
||||
if (xmldb_put(h, db, op, xt) < 0)
|
||||
goto done;
|
||||
}
|
||||
else if (strcmp(cmd, "copy")==0){
|
||||
|
|
|
|||
|
|
@ -65,7 +65,7 @@ all: $(PLUGIN)
|
|||
-include $(DESTDIR)$(datarootdir)/clixon/clixon.mk
|
||||
|
||||
$(PLUGIN): $(SRC)
|
||||
$(CC) $(CPPFLAGS) $(INCLUDES) $(CFLAGS) -shared -o $@ -lc $^ $(LIBS)
|
||||
$(CC) $(CPPFLAGS) $(INCLUDES) $(CFLAGS) $(LDFLAGS) -shared -o $@ -lc $^ $(LIBS)
|
||||
|
||||
clean:
|
||||
rm -f $(PLUGIN) $(OBJS) *.core
|
||||
|
|
@ -81,12 +81,12 @@ distclean: clean
|
|||
|
||||
install: $(PLUGIN)
|
||||
install -d $(DESTDIR)$(clixon_LIBDIR)/xmldb
|
||||
install $(PLUGIN) $(DESTDIR)$(clixon_LIBDIR)/xmldb;
|
||||
install $(PLUGIN) $(DESTDIR)$(clixon_LIBDIR)/xmldb
|
||||
|
||||
install-include:
|
||||
|
||||
uninstall:
|
||||
rm -rf $(DESTDIR)$(clixon_LIBDIR)/xmldb/$(PLUGIN);
|
||||
rm -rf $(DESTDIR)$(clixon_LIBDIR)/xmldb/$(PLUGIN)
|
||||
|
||||
TAGS:
|
||||
find . -name '*.[chyl]' -print | etags -
|
||||
|
|
|
|||
|
|
@ -50,15 +50,15 @@
|
|||
* The relations between the functions and formats are as follows:
|
||||
*
|
||||
* +-----------------+ +-----------------+
|
||||
* | yang-stmt | yang2xmlkeyfmt | xmlkeyfmt | xmlkeyfmt2xpath
|
||||
* | yang-stmt | yang2api_path_fmt | api_path_fmt | api_path_fmt2xpath
|
||||
* | list aa,leaf k | ----------------->| /aa=%s |---------------->
|
||||
* +-----------------+ +-----------------+
|
||||
* |
|
||||
* | xmlkeyfmt2key
|
||||
* | api_path_fmt2api_path
|
||||
* | k=17
|
||||
* v
|
||||
* +-------------------+ +-----------------+
|
||||
* | xml-tree/cxobj | xmlkey2xml | xmlkey RFC3986|
|
||||
* | xml-tree/cxobj | xmlkey2xml |api_path RFC3986|
|
||||
* | <aa><k>17</k></aa>| <------------- | /aa=17 |
|
||||
* +-------------------+ +-----------------+
|
||||
*
|
||||
|
|
@ -68,7 +68,7 @@
|
|||
*
|
||||
* Paths through the code (for coverage)
|
||||
* cli_callback_generate +----------------+
|
||||
* cli_expand_var_generate | yang2xmlkeyfmt |
|
||||
* cli_expand_var_generate | yang2api_path_fmt |
|
||||
* yang -------------> | |
|
||||
* +----------------+
|
||||
* xmldb_get_tree
|
||||
|
|
@ -785,494 +785,6 @@ put(char *dbfile,
|
|||
return retval;
|
||||
}
|
||||
|
||||
/*! Modify database provided an XML database key and an operation
|
||||
* @param[in] kh Keyvalue handle
|
||||
* @param[in] db Database name
|
||||
* @param[in] op OP_MERGE, OP_REPLACE, OP_REMOVE, etc
|
||||
* @param[in] xk XML Key, eg /aa/bb=17/name
|
||||
* @param[in] val Key value, eg "17"
|
||||
|
||||
* @retval 0 OK
|
||||
* @retval -1 Error
|
||||
* @code
|
||||
* if (xmldb_put_xkey(h, db, OP_MERGE, "/aa/bb=17/name", "17") < 0)
|
||||
* err;
|
||||
* @endcode
|
||||
* @see xmldb_put with xml-tree, no path
|
||||
*/
|
||||
static int
|
||||
xmldb_put_xkey(struct kv_handle *kh,
|
||||
char *db,
|
||||
enum operation_type op,
|
||||
char *xk,
|
||||
char *val)
|
||||
|
||||
{
|
||||
int retval = -1;
|
||||
yang_stmt *y = NULL;
|
||||
yang_stmt *ykey;
|
||||
char **vec = NULL;
|
||||
int nvec;
|
||||
char **valvec = NULL;
|
||||
int nvalvec;
|
||||
int i;
|
||||
int j;
|
||||
char *name;
|
||||
char *restval;
|
||||
cg_var *cvi;
|
||||
cvec *cvk = NULL; /* vector of index keys */
|
||||
char *val2 = NULL;
|
||||
cbuf *ckey=NULL; /* partial keys */
|
||||
cbuf *csubkey=NULL; /* partial keys */
|
||||
cbuf *crx=NULL; /* partial keys */
|
||||
char *keyname;
|
||||
int exists;
|
||||
int npairs;
|
||||
struct db_pair *pairs;
|
||||
yang_spec *yspec;
|
||||
char *filename = NULL;
|
||||
|
||||
// clicon_log(LOG_WARNING, "%s", __FUNCTION__);
|
||||
if ((yspec = kh->kh_yangspec) == NULL){
|
||||
clicon_err(OE_YANG, ENOENT, "No yang spec");
|
||||
goto done;
|
||||
}
|
||||
if (kv_db2file(kh, db, &filename) < 0)
|
||||
goto done;
|
||||
if (xk == NULL || *xk!='/'){
|
||||
clicon_err(OE_DB, 0, "Invalid api_path: %s", xk);
|
||||
goto done;
|
||||
}
|
||||
if ((ckey = cbuf_new()) == NULL){
|
||||
clicon_err(OE_UNIX, errno, "cbuf_new");
|
||||
goto done;
|
||||
}
|
||||
if ((csubkey = cbuf_new()) == NULL){
|
||||
clicon_err(OE_UNIX, errno, "cbuf_new");
|
||||
goto done;
|
||||
}
|
||||
if ((vec = clicon_strsep(xk, "/", &nvec)) == NULL)
|
||||
goto done;
|
||||
/* Remove trailing '/'. Like in /a/ -> /a */
|
||||
if (nvec > 1 && !strlen(vec[nvec-1]))
|
||||
nvec--;
|
||||
if (nvec < 2){
|
||||
clicon_err(OE_XML, 0, "Malformed key: %s", xk);
|
||||
goto done;
|
||||
}
|
||||
i = 1;
|
||||
while (i<nvec){
|
||||
name = vec[i]; /* E.g "x=1,2" -> name:x restval=1,2 */
|
||||
if ((restval = index(name, '=')) != NULL){
|
||||
*restval = '\0';
|
||||
restval++;
|
||||
}
|
||||
if (i==1){
|
||||
if (strlen(name)==0 && (op==OP_DELETE || op == OP_REMOVE)){
|
||||
/* Special handling of "/" */
|
||||
cprintf(ckey, "/");
|
||||
break;
|
||||
}
|
||||
else if ((y = yang_find_topnode(yspec, name)) == NULL){
|
||||
clicon_err(OE_UNIX, errno, "No yang node found: %s", name);
|
||||
goto done;
|
||||
}
|
||||
}
|
||||
else
|
||||
if ((y = yang_find_syntax((yang_node*)y, name)) == NULL){
|
||||
clicon_err(OE_UNIX, errno, "No yang node found: %s", name);
|
||||
goto done;
|
||||
}
|
||||
if ((op==OP_DELETE || op == OP_REMOVE) &&
|
||||
y->ys_keyword == Y_LEAF &&
|
||||
y->ys_parent->yn_keyword == Y_LIST &&
|
||||
yang_key_match(y->ys_parent, y->ys_argument))
|
||||
/* Special rule if key, dont write last key-name, rm whole*/;
|
||||
else
|
||||
cprintf(ckey, "/%s", name);
|
||||
i++;
|
||||
switch (y->ys_keyword){
|
||||
case Y_LEAF_LIST:
|
||||
if (restval==NULL){
|
||||
clicon_err(OE_XML, 0, "malformed key, expected '=<restval>'");
|
||||
goto done;
|
||||
}
|
||||
cprintf(ckey, "=%s", restval);
|
||||
break;
|
||||
case Y_LIST:
|
||||
if ((ykey = yang_find((yang_node*)y, Y_KEY, NULL)) == NULL){
|
||||
clicon_err(OE_XML, errno, "%s: List statement \"%s\" has no key",
|
||||
__FUNCTION__, y->ys_argument);
|
||||
goto done;
|
||||
}
|
||||
/* The value is a list of keys: <key>[ <key>]* */
|
||||
if ((cvk = yang_arg2cvec(ykey, " ")) == NULL)
|
||||
goto done;
|
||||
if (restval==NULL){
|
||||
clicon_err(OE_XML, 0, "malformed key, expected '=<restval>'");
|
||||
goto done;
|
||||
}
|
||||
if (valvec)
|
||||
free(valvec);
|
||||
if ((valvec = clicon_strsep(restval, ",", &nvalvec)) == NULL)
|
||||
goto done;
|
||||
if (cvec_len(cvk) != nvalvec){
|
||||
clicon_err(OE_XML, errno, "List %s key length mismatch", name);
|
||||
goto done;
|
||||
}
|
||||
cvi = NULL;
|
||||
/* Iterate over individual yang keys */
|
||||
j = 0;
|
||||
while ((cvi = cvec_each(cvk, cvi)) != NULL) {
|
||||
keyname = cv_string_get(cvi);
|
||||
if (j)
|
||||
cprintf(ckey, ",");
|
||||
else
|
||||
cprintf(ckey, "=");
|
||||
val2 = valvec[j++];
|
||||
cprintf(ckey, "%s", val2);
|
||||
cbuf_reset(csubkey);
|
||||
cprintf(csubkey, "%s/%s", cbuf_get(ckey), keyname);
|
||||
if (op == OP_MERGE || op == OP_REPLACE || op == OP_CREATE)
|
||||
if (db_set(filename, cbuf_get(csubkey), val2, strlen(val2)+1) < 0)
|
||||
goto done;
|
||||
}
|
||||
if (cvk){
|
||||
cvec_free(cvk);
|
||||
cvk = NULL;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
if (op == OP_MERGE || op == OP_REPLACE || op == OP_CREATE)
|
||||
if (db_set(filename, cbuf_get(ckey), NULL, 0) < 0)
|
||||
goto done;
|
||||
break;
|
||||
}
|
||||
}
|
||||
xk = cbuf_get(ckey);
|
||||
/* final key */
|
||||
switch (op){
|
||||
case OP_CREATE:
|
||||
if ((exists = db_exists(filename, xk)) < 0)
|
||||
goto done;
|
||||
if (exists == 1){
|
||||
clicon_err(OE_DB, 0, "OP_CREATE: %s already exists in database", xk);
|
||||
goto done;
|
||||
}
|
||||
case OP_MERGE:
|
||||
case OP_REPLACE:
|
||||
if (y->ys_keyword == Y_LEAF || y->ys_keyword == Y_LEAF_LIST){
|
||||
if (db_set(filename, xk, val, val?strlen(val)+1:0) < 0)
|
||||
goto done;
|
||||
}
|
||||
else
|
||||
if (db_set(filename, xk, NULL, 0) < 0)
|
||||
goto done;
|
||||
break;
|
||||
case OP_DELETE:
|
||||
if ((exists = db_exists(filename, xk)) < 0)
|
||||
goto done;
|
||||
if (exists == 0){
|
||||
clicon_err(OE_DB, 0, "OP_DELETE: %s does not exist in database", xk);
|
||||
goto done;
|
||||
}
|
||||
case OP_REMOVE:
|
||||
/* Read in complete database (this can be optimized) */
|
||||
if ((crx = cbuf_new()) == NULL){
|
||||
clicon_err(OE_UNIX, errno, "cbuf_new");
|
||||
goto done;
|
||||
}
|
||||
cprintf(crx, "^%s.*$", xk);
|
||||
if ((npairs = db_regexp(filename, cbuf_get(crx), __FUNCTION__, &pairs, 0)) < 0)
|
||||
goto done;
|
||||
for (i = 0; i < npairs; i++) {
|
||||
if (db_del(filename, pairs[i].dp_key) < 0)
|
||||
goto done;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
retval = 0;
|
||||
done:
|
||||
if (filename)
|
||||
free(filename);
|
||||
if (ckey)
|
||||
cbuf_free(ckey);
|
||||
if (csubkey)
|
||||
cbuf_free(csubkey);
|
||||
if (crx)
|
||||
cbuf_free(crx);
|
||||
if (cvk)
|
||||
cvec_free(cvk);
|
||||
if (vec)
|
||||
free(vec);
|
||||
if (valvec)
|
||||
free(valvec);
|
||||
|
||||
unchunk_group(__FUNCTION__);
|
||||
return retval;
|
||||
}
|
||||
|
||||
/*! Modify database provided an xml tree, a restconf api_path and an operation
|
||||
*
|
||||
* @param[in] kh Keyvalue handle
|
||||
* @param[in] db running or candidate
|
||||
* @param[in] op OP_MERGE: just add it.
|
||||
* OP_REPLACE: first delete whole database
|
||||
* OP_NONE: operation attribute in xml determines operation
|
||||
* @param[in] api_path According to restconf (Sec 3.5.1.1 in [restconf-draft 13])
|
||||
* @param[in] xt xml-tree. Top-level symbol is dummy
|
||||
* @retval 0 OK
|
||||
* @retval -1 Error
|
||||
* example:
|
||||
* container top {
|
||||
* list list1 {
|
||||
* key "key1 key2 key3";
|
||||
* is referenced as
|
||||
* /restconf/data/top/list1=a,,foo
|
||||
* @see xmldb_put
|
||||
*/
|
||||
static int
|
||||
xmldb_put_restconf_api_path(struct kv_handle *kh,
|
||||
char *db,
|
||||
enum operation_type op,
|
||||
char *xk,
|
||||
cxobj *xt)
|
||||
{
|
||||
int retval = -1;
|
||||
yang_stmt *y = NULL;
|
||||
yang_stmt *ykey;
|
||||
char **vec = NULL;
|
||||
int nvec;
|
||||
#if 0
|
||||
char **valvec = NULL;
|
||||
int nvalvec;
|
||||
int j;
|
||||
char *restval;
|
||||
#endif
|
||||
int i;
|
||||
char *name;
|
||||
cg_var *cvi;
|
||||
cvec *cvk = NULL; /* vector of index keys */
|
||||
char *val2;
|
||||
cbuf *ckey=NULL; /* partial keys */
|
||||
cbuf *csubkey=NULL; /* partial keys */
|
||||
cbuf *crx=NULL; /* partial keys */
|
||||
char *keyname;
|
||||
int exists;
|
||||
int npairs;
|
||||
struct db_pair *pairs;
|
||||
yang_spec *yspec;
|
||||
yang_stmt *ys;
|
||||
char *filename = NULL;
|
||||
char *key;
|
||||
char *keys;
|
||||
|
||||
if ((yspec = kh->kh_yangspec) == NULL){
|
||||
clicon_err(OE_YANG, ENOENT, "No yang spec");
|
||||
goto done;
|
||||
}
|
||||
if (kv_db2file(kh, db, &filename) < 0)
|
||||
goto done;
|
||||
if (xk == NULL || *xk!='/'){
|
||||
clicon_err(OE_DB, 0, "Invalid api path: %s", xk);
|
||||
goto done;
|
||||
}
|
||||
if ((ckey = cbuf_new()) == NULL){
|
||||
clicon_err(OE_UNIX, errno, "cbuf_new");
|
||||
goto done;
|
||||
}
|
||||
if ((csubkey = cbuf_new()) == NULL){
|
||||
clicon_err(OE_UNIX, errno, "cbuf_new");
|
||||
goto done;
|
||||
}
|
||||
if ((vec = clicon_strsep(xk, "/", &nvec)) == NULL)
|
||||
goto done;
|
||||
/* Remove trailing '/'. Like in /a/ -> /a */
|
||||
if (nvec > 1 && !strlen(vec[nvec-1]))
|
||||
nvec--;
|
||||
if (nvec < 2){
|
||||
clicon_err(OE_XML, 0, "Malformed key: %s", xk);
|
||||
goto done;
|
||||
}
|
||||
i = 1;
|
||||
while (i<nvec){
|
||||
name = vec[i];
|
||||
if ((keys = index(name, '=')) != NULL){
|
||||
*keys = '\0';
|
||||
keys++;
|
||||
}
|
||||
#if 0
|
||||
if ((restval = index(name, '=')) != NULL){
|
||||
*restval = '\0';
|
||||
restval++;
|
||||
}
|
||||
#endif
|
||||
if (i==1){
|
||||
if (strlen(name)==0 && (op==OP_DELETE || op == OP_REMOVE)){
|
||||
/* Special handling of "/" */
|
||||
cprintf(ckey, "/");
|
||||
break;
|
||||
}
|
||||
else if ((y = yang_find_topnode(yspec, name)) == NULL){
|
||||
clicon_err(OE_UNIX, errno, "No yang node found: %s", name);
|
||||
goto done;
|
||||
}
|
||||
}
|
||||
else
|
||||
if ((y = yang_find_syntax((yang_node*)y, name)) == NULL){
|
||||
clicon_err(OE_UNIX, errno, "No yang node found: %s", name);
|
||||
goto done;
|
||||
}
|
||||
if ((op==OP_DELETE || op == OP_REMOVE) &&
|
||||
y->ys_keyword == Y_LEAF &&
|
||||
y->ys_parent->yn_keyword == Y_LIST &&
|
||||
yang_key_match(y->ys_parent, y->ys_argument))
|
||||
/* Special rule if key, dont write last key-name, rm whole*/;
|
||||
else
|
||||
cprintf(ckey, "/%s", name);
|
||||
i++;
|
||||
switch (y->ys_keyword){
|
||||
case Y_LEAF_LIST:
|
||||
/* For leaf-list 'keys' is value, see 3.5.1 in restconf draft */
|
||||
val2 = keys;
|
||||
cprintf(ckey, "/%s", keys);
|
||||
break;
|
||||
case Y_LIST:
|
||||
if ((ykey = yang_find((yang_node*)y, Y_KEY, NULL)) == NULL){
|
||||
clicon_err(OE_XML, errno, "%s: List statement \"%s\" has no key",
|
||||
__FUNCTION__, y->ys_argument);
|
||||
goto done;
|
||||
}
|
||||
/* The value is a list of keys: <key>[ <key>]* */
|
||||
if ((cvk = yang_arg2cvec(ykey, " ")) == NULL)
|
||||
goto done;
|
||||
#if 0
|
||||
if (restval==NULL){
|
||||
clicon_err(OE_XML, 0, "malformed key, expected '=<restval>'");
|
||||
goto done;
|
||||
}
|
||||
if (valvec)
|
||||
free(valvec);
|
||||
if ((valvec = clicon_strsep(restval, ",", &nvalvec)) == NULL)
|
||||
goto done;
|
||||
if (cvec_len(cvk) != nvalvec){
|
||||
clicon_err(OE_XML, errno, "List %s key length mismatch", name);
|
||||
goto done;
|
||||
}
|
||||
j = 0;
|
||||
#endif
|
||||
cvi = NULL;
|
||||
/* Iterate over individual yang keys */
|
||||
while ((cvi = cvec_each(cvk, cvi)) != NULL) {
|
||||
keyname = cv_string_get(cvi);
|
||||
#if 0
|
||||
if (j)
|
||||
cprintf(ckey, ",");
|
||||
else
|
||||
cprintf(ckey, "=");
|
||||
val2 = valvec[j++];
|
||||
cprintf(ckey, "%s", val2);
|
||||
#else
|
||||
// val2 = vec[i++]; /* No */
|
||||
val2 = keys;
|
||||
if (i>nvec){ /* XXX >= ? */
|
||||
clicon_err(OE_XML, errno, "List %s without argument", name);
|
||||
goto done;
|
||||
}
|
||||
cprintf(ckey, "=%s", val2);
|
||||
#endif
|
||||
cbuf_reset(csubkey);
|
||||
cprintf(csubkey, "%s/%s", cbuf_get(ckey), keyname);
|
||||
if (op == OP_MERGE || op == OP_REPLACE || op == OP_CREATE)
|
||||
if (db_set(filename, cbuf_get(csubkey), val2, strlen(val2)+1) < 0)
|
||||
goto done;
|
||||
}
|
||||
if (cvk){
|
||||
cvec_free(cvk);
|
||||
cvk = NULL;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
if (op == OP_MERGE || op == OP_REPLACE || op == OP_CREATE)
|
||||
if (db_set(filename, cbuf_get(ckey), NULL, 0) < 0)
|
||||
goto done;
|
||||
break;
|
||||
}
|
||||
}
|
||||
key = cbuf_get(ckey);
|
||||
/* final key */
|
||||
switch (op){
|
||||
case OP_CREATE:
|
||||
if ((exists = db_exists(filename, key)) < 0)
|
||||
goto done;
|
||||
if (exists == 1){
|
||||
clicon_err(OE_DB, 0, "OP_CREATE: %s already exists in database", key);
|
||||
goto done;
|
||||
}
|
||||
case OP_MERGE:
|
||||
case OP_REPLACE:
|
||||
if (xt==NULL){
|
||||
clicon_err(OE_DB, 0, "%s: no xml when yang node %s required",
|
||||
__FUNCTION__, y->ys_argument);
|
||||
goto done;
|
||||
}
|
||||
if ((ys = yang_find_syntax((yang_node*)y, xml_name(xt))) == NULL){
|
||||
clicon_err(OE_DB, 0, "%s: child %s not found under node %s",
|
||||
__FUNCTION__, xml_name(xt), y->ys_argument);
|
||||
goto done;
|
||||
}
|
||||
y = ys;
|
||||
if (put(filename, xt, y, op, key) < 0)
|
||||
goto done;
|
||||
break;
|
||||
case OP_DELETE:
|
||||
if ((exists = db_exists(filename, key)) < 0)
|
||||
goto done;
|
||||
if (exists == 0){
|
||||
clicon_err(OE_DB, 0, "OP_DELETE: %s does not exists in database", key);
|
||||
goto done;
|
||||
}
|
||||
case OP_REMOVE:
|
||||
/* Read in complete database (this can be optimized) */
|
||||
if ((crx = cbuf_new()) == NULL){
|
||||
clicon_err(OE_UNIX, errno, "cbuf_new");
|
||||
goto done;
|
||||
}
|
||||
cprintf(crx, "^%s.*$", key);
|
||||
if ((npairs = db_regexp(filename, cbuf_get(crx), __FUNCTION__, &pairs, 0)) < 0)
|
||||
goto done;
|
||||
for (i = 0; i < npairs; i++) {
|
||||
if (db_del(filename, pairs[i].dp_key) < 0)
|
||||
goto done;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
retval = 0;
|
||||
done:
|
||||
// clicon_debug(1, "%s retval:%d", __FUNCTION__, retval);
|
||||
if (filename)
|
||||
free(filename);
|
||||
if (ckey)
|
||||
cbuf_free(ckey);
|
||||
if (csubkey)
|
||||
cbuf_free(csubkey);
|
||||
if (crx)
|
||||
cbuf_free(crx);
|
||||
if (cvk)
|
||||
cvec_free(cvk);
|
||||
if (vec)
|
||||
free(vec);
|
||||
#if 0
|
||||
if (valvec)
|
||||
free(valvec);
|
||||
#endif
|
||||
unchunk_group(__FUNCTION__);
|
||||
return retval;
|
||||
}
|
||||
|
||||
/*! Modify database provided an xml tree and an operation
|
||||
*
|
||||
|
|
@ -1299,7 +811,6 @@ int
|
|||
kv_put(xmldb_handle xh,
|
||||
char *db,
|
||||
enum operation_type op,
|
||||
char *api_path,
|
||||
cxobj *xt)
|
||||
{
|
||||
int retval = -1;
|
||||
|
|
@ -1309,9 +820,6 @@ kv_put(xmldb_handle xh,
|
|||
yang_spec *yspec;
|
||||
char *dbfilename = NULL;
|
||||
|
||||
if (xt && (xml_child_nr(xt)==0 || xml_body(xt)!= NULL) &&
|
||||
api_path && strlen(api_path) && strcmp(api_path,"/"))
|
||||
return xmldb_put_xkey(kh, db, op, api_path, xml_body(xt));
|
||||
if ((yspec = kh->kh_yangspec) == NULL){
|
||||
clicon_err(OE_YANG, ENOENT, "No yang spec");
|
||||
goto done;
|
||||
|
|
@ -1324,28 +832,19 @@ kv_put(xmldb_handle xh,
|
|||
if (db_init(dbfilename) < 0)
|
||||
goto done;
|
||||
}
|
||||
if (api_path && strlen(api_path) && strcmp(api_path,"/")){
|
||||
// clicon_log(LOG_WARNING, "xmldb_put_restconf_api_path");
|
||||
while ((x = xml_child_each(xt, x, CX_ELMNT)) != NULL){
|
||||
if (xmldb_put_restconf_api_path(kh, db, op, api_path, x) < 0)
|
||||
goto done;
|
||||
}
|
||||
}
|
||||
else{
|
||||
// clicon_log(LOG_WARNING, "%s", __FUNCTION__);
|
||||
while ((x = xml_child_each(xt, x, CX_ELMNT)) != NULL){
|
||||
if ((ys = yang_find_topnode(yspec, xml_name(x))) == NULL){
|
||||
clicon_err(OE_UNIX, errno, "No yang node found: %s", xml_name(x));
|
||||
goto done;
|
||||
}
|
||||
if (put(dbfilename, /* database name */
|
||||
x, /* xml root node */
|
||||
ys, /* yang statement of xml node */
|
||||
op, /* operation, eg merge/delete */
|
||||
"" /* aggregate xml key */
|
||||
) < 0)
|
||||
goto done;
|
||||
// clicon_log(LOG_WARNING, "%s", __FUNCTION__);
|
||||
while ((x = xml_child_each(xt, x, CX_ELMNT)) != NULL){
|
||||
if ((ys = yang_find_topnode(yspec, xml_name(x))) == NULL){
|
||||
clicon_err(OE_UNIX, errno, "No yang node found: %s", xml_name(x));
|
||||
goto done;
|
||||
}
|
||||
if (put(dbfilename, /* database name */
|
||||
x, /* xml root node */
|
||||
ys, /* yang statement of xml node */
|
||||
op, /* operation, eg merge/delete */
|
||||
"" /* aggregate xml key */
|
||||
) < 0)
|
||||
goto done;
|
||||
}
|
||||
retval = 0;
|
||||
done:
|
||||
|
|
|
|||
|
|
@ -41,8 +41,7 @@
|
|||
*/
|
||||
int kv_get(xmldb_handle h, char *db, char *xpath,
|
||||
cxobj **xtop, cxobj ***xvec, size_t *xlen);
|
||||
int kv_put(xmldb_handle h, char *db, enum operation_type op,
|
||||
char *api_path, cxobj *xt);
|
||||
int kv_put(xmldb_handle h, char *db, enum operation_type op, cxobj *xt);
|
||||
int kv_dump(FILE *f, char *dbfilename, char *rxkey);
|
||||
int kv_copy(xmldb_handle h, char *from, char *to);
|
||||
int kv_lock(xmldb_handle h, char *db, int pid);
|
||||
|
|
|
|||
|
|
@ -64,7 +64,7 @@ all: $(PLUGIN)
|
|||
-include $(DESTDIR)$(datarootdir)/clixon/clixon.mk
|
||||
|
||||
$(PLUGIN): $(SRC)
|
||||
$(CC) $(CPPFLAGS) $(INCLUDES) $(CFLAGS) -shared -o $@ -lc $^ $(LIBS)
|
||||
$(CC) $(CPPFLAGS) $(INCLUDES) $(CFLAGS) $(LDFLAGS) -shared -o $@ -lc $^ $(LIBS)
|
||||
|
||||
clean:
|
||||
rm -f $(PLUGIN) $(OBJS) *.core
|
||||
|
|
@ -80,12 +80,12 @@ distclean: clean
|
|||
|
||||
install: $(PLUGIN)
|
||||
install -d $(DESTDIR)$(clixon_LIBDIR)/xmldb
|
||||
install $(PLUGIN) $(DESTDIR)$(clixon_LIBDIR)/xmldb;
|
||||
install $(PLUGIN) $(DESTDIR)$(clixon_LIBDIR)/xmldb
|
||||
|
||||
install-include:
|
||||
|
||||
uninstall:
|
||||
rm -rf $(DESTDIR)$(clixon_LIBDIR)/xmldb/$(PLUGIN);
|
||||
rm -rf $(DESTDIR)$(clixon_LIBDIR)/xmldb/$(PLUGIN)
|
||||
|
||||
TAGS:
|
||||
find . -name '*.[chyl]' -print | etags -
|
||||
|
|
|
|||
|
|
@ -265,9 +265,52 @@ xml_spec_populate(cxobj *x,
|
|||
y = yang_find_syntax((yang_node*)yp, xml_name(x));
|
||||
else
|
||||
y = yang_find_topnode(yspec, name); /* still NULL for config */
|
||||
if (y==NULL){
|
||||
clicon_err(OE_XML, EBADF, "yang spec not found for xml node '%s' xml parent name: '%s' yangspec:'",
|
||||
name,
|
||||
xp?xml_name(xp):"", yp?yp->ys_argument:"");
|
||||
goto done;
|
||||
}
|
||||
xml_spec_set(x, y);
|
||||
retval = 0;
|
||||
// done:
|
||||
done:
|
||||
return retval;
|
||||
}
|
||||
|
||||
/*! Ensure that xt only has a single sub-element and that is "config"
|
||||
*/
|
||||
static int
|
||||
singleconfigroot(cxobj *xt,
|
||||
cxobj **xp)
|
||||
{
|
||||
int retval = -1;
|
||||
cxobj *x = NULL;
|
||||
int i = 0;
|
||||
|
||||
/* There should only be one element and called config */
|
||||
x = NULL;
|
||||
while ((x = xml_child_each(xt, x, CX_ELMNT)) != NULL){
|
||||
i++;
|
||||
if (strcmp(xml_name(x), "config")){
|
||||
clicon_err(OE_DB, ENOENT, "Wrong top-element %s expected config",
|
||||
xml_name(x));
|
||||
goto done;
|
||||
}
|
||||
}
|
||||
if (i != 1){
|
||||
clicon_err(OE_DB, ENOENT, "Top-element is not unique, expecting single config");
|
||||
goto done;
|
||||
}
|
||||
x = NULL;
|
||||
while ((x = xml_child_each(xt, x, CX_ELMNT)) != NULL){
|
||||
if (xml_rm(x) < 0)
|
||||
goto done;
|
||||
if (xml_free(xt) < 0)
|
||||
goto done;
|
||||
*xp = x;
|
||||
}
|
||||
retval = 0;
|
||||
done:
|
||||
return retval;
|
||||
}
|
||||
|
||||
|
|
@ -344,10 +387,15 @@ text_get(xmldb_handle xh,
|
|||
}
|
||||
/* 2. File is not empty <top><config>...</config></top> -> replace root */
|
||||
else{
|
||||
assert(xml_child_nr(xt)==1);
|
||||
if (xml_rootchild(xt, 0, &xt) < 0)
|
||||
/* There should only be one element and called config */
|
||||
if (singleconfigroot(xt, &xt) < 0)
|
||||
goto done;
|
||||
}
|
||||
/* Here xt looks like: <config>...</config> */
|
||||
/* Validate existing config tree */
|
||||
if (xml_apply(xt, CX_ELMNT, xml_spec_populate, yspec) < 0)
|
||||
goto done;
|
||||
|
||||
/* XXX Maybe the below is general function and should be moved to xmldb? */
|
||||
if (xpath_vec(xt, xpath?xpath:"/", &xvec, &xlen) < 0)
|
||||
goto done;
|
||||
|
|
@ -398,271 +446,19 @@ text_get(xmldb_handle xh,
|
|||
return retval;
|
||||
}
|
||||
|
||||
/*! Check if child with fullmatch exists
|
||||
* param[in] cvk vector of index keys
|
||||
*/
|
||||
static cxobj *
|
||||
find_keys_vec(cxobj *xt,
|
||||
char *name,
|
||||
cvec *cvk,
|
||||
char **valvec)
|
||||
{
|
||||
cxobj *xi = NULL;
|
||||
int j;
|
||||
char *keyname;
|
||||
char *val;
|
||||
cg_var *cvi;
|
||||
char *body;
|
||||
|
||||
while ((xi = xml_child_each(xt, xi, CX_ELMNT)) != NULL)
|
||||
if (strcmp(xml_name(xi), name) == 0){
|
||||
j = 0;
|
||||
cvi = NULL;
|
||||
while ((cvi = cvec_each(cvk, cvi)) != NULL) {
|
||||
keyname = cv_string_get(cvi);
|
||||
val = valvec[j++];
|
||||
if ((body = xml_find_body(xi, keyname)) == NULL)
|
||||
break;
|
||||
if (strcmp(body, val))
|
||||
break;
|
||||
}
|
||||
/* All keys must match: loop terminates. */
|
||||
if (cvi==NULL)
|
||||
return xi;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*! Create 'modification' tree from api-path, ie fill in xml tree from the path
|
||||
* @param[in] api_path api-path expression
|
||||
* @param[in] xt XML tree. Find api-path (or create) in this tree
|
||||
* @param[in] op OP_MERGE, OP_REPLACE, OP_REMOVE, etc
|
||||
* @param[in] yspec Yang spec
|
||||
* @param[out] xp Resulting xml tree corresponding to xt
|
||||
* @param[out] xparp Parent of xp (xp can be NULL)
|
||||
* @param[out] yp Yang spec matching xp
|
||||
* @see xmldb_put_xkey for example
|
||||
*/
|
||||
static int
|
||||
text_apipath_modify(char *api_path,
|
||||
cxobj *xt,
|
||||
enum operation_type op,
|
||||
yang_spec *yspec,
|
||||
cxobj **xp,
|
||||
cxobj **xparp,
|
||||
yang_node **yp)
|
||||
{
|
||||
int retval = -1;
|
||||
char **vec = NULL;
|
||||
int nvec;
|
||||
int i;
|
||||
int j;
|
||||
char *name;
|
||||
char *restval;
|
||||
yang_stmt *y = NULL;
|
||||
yang_stmt *ykey;
|
||||
cxobj *x = NULL;
|
||||
cxobj *xpar = NULL;
|
||||
cxobj *xn = NULL; /* new */
|
||||
cxobj *xb; /* body */
|
||||
cvec *cvk = NULL; /* vector of index keys */
|
||||
char **valvec = NULL;
|
||||
int nvalvec;
|
||||
cg_var *cvi;
|
||||
char *keyname;
|
||||
char *val2;
|
||||
|
||||
x = xt;
|
||||
xpar = xml_parent(xt);
|
||||
if (api_path == NULL || *api_path!='/'){
|
||||
clicon_err(OE_DB, 0, "Invalid key: %s", api_path);
|
||||
goto done;
|
||||
}
|
||||
if ((vec = clicon_strsep(api_path, "/", &nvec)) == NULL)
|
||||
goto done;
|
||||
/* Remove trailing '/'. Like in /a/ -> /a */
|
||||
if (nvec > 1 && !strlen(vec[nvec-1]))
|
||||
nvec--;
|
||||
if (nvec < 1){
|
||||
clicon_err(OE_XML, 0, "Malformed key: %s", api_path);
|
||||
goto done;
|
||||
}
|
||||
i = 1;
|
||||
while (i<nvec){
|
||||
name = vec[i]; /* E.g "x=1,2" -> name:x restval=1,2 */
|
||||
if ((restval = index(name, '=')) != NULL){
|
||||
*restval = '\0';
|
||||
restval++;
|
||||
}
|
||||
if (y == NULL) /* top-node */
|
||||
y = yang_find_topnode(yspec, name);
|
||||
else
|
||||
y = yang_find_syntax((yang_node*)y, name);
|
||||
if (y == NULL){
|
||||
clicon_err(OE_YANG, errno, "No yang node found: %s", name);
|
||||
goto done;
|
||||
}
|
||||
i++;
|
||||
switch (y->ys_keyword){
|
||||
case Y_LEAF_LIST:
|
||||
if (restval==NULL){
|
||||
clicon_err(OE_XML, 0, "malformed key, expected '=<restval>'");
|
||||
goto done;
|
||||
}
|
||||
/* See if it exists */
|
||||
xn = NULL;
|
||||
while ((xn = xml_child_each(x, xn, CX_ELMNT)) != NULL)
|
||||
if (strcmp(name, xml_name(xn)) == 0 &&
|
||||
strcmp(xml_body(xn),restval)==0)
|
||||
break;
|
||||
if (xn == NULL){ /* Not found, does not exist */
|
||||
switch (op){
|
||||
case OP_DELETE: /* not here, should be here */
|
||||
clicon_err(OE_XML, 0, "Object to delete does not exist");
|
||||
goto done;
|
||||
break;
|
||||
case OP_REMOVE:
|
||||
goto ok; /* not here, no need to remove */
|
||||
break;
|
||||
case OP_CREATE:
|
||||
if (i==nvec) /* Last, dont create here */
|
||||
break;
|
||||
default:
|
||||
//XXX create_keyvalues(cxobj *x,
|
||||
if ((xn = xml_new_spec(y->ys_argument, x, y)) == NULL)
|
||||
goto done;
|
||||
// xml_type_set(xn, CX_ELMNT);
|
||||
if ((xb = xml_new("body", xn)) == NULL)
|
||||
goto done;
|
||||
xml_type_set(xb, CX_BODY);
|
||||
if (xml_value_set(xb, restval) < 0)
|
||||
goto done;
|
||||
break;
|
||||
}
|
||||
}
|
||||
xpar = x;
|
||||
x = xn;
|
||||
break;
|
||||
case Y_LIST:
|
||||
/* Get the yang list key */
|
||||
if ((ykey = yang_find((yang_node*)y, Y_KEY, NULL)) == NULL){
|
||||
clicon_err(OE_XML, errno, "%s: List statement \"%s\" has no key",
|
||||
__FUNCTION__, y->ys_argument);
|
||||
goto done;
|
||||
}
|
||||
/* The value is a list of keys: <key>[ <key>]* */
|
||||
if ((cvk = yang_arg2cvec(ykey, " ")) == NULL)
|
||||
goto done;
|
||||
if (restval==NULL){
|
||||
clicon_err(OE_XML, 0, "malformed key, expected '=<restval>'");
|
||||
goto done;
|
||||
}
|
||||
if (valvec)
|
||||
free(valvec);
|
||||
if ((valvec = clicon_strsep(restval, ",", &nvalvec)) == NULL)
|
||||
goto done;
|
||||
|
||||
if (cvec_len(cvk) != nvalvec){
|
||||
clicon_err(OE_XML, errno, "List %s key length mismatch", name);
|
||||
goto done;
|
||||
}
|
||||
cvi = NULL;
|
||||
/* Check if exists, if not, create */
|
||||
if ((xn = find_keys_vec(x, name, cvk, valvec)) == NULL){
|
||||
/* create them, but not if delete op */
|
||||
switch (op){
|
||||
case OP_DELETE: /* not here, should be here */
|
||||
clicon_err(OE_XML, 0, "Object to delete does not exist");
|
||||
goto done;
|
||||
break;
|
||||
case OP_REMOVE:
|
||||
goto ok; /* not here, no need to remove */
|
||||
break;
|
||||
default:
|
||||
if ((xn = xml_new(name, x)) == NULL)
|
||||
goto done;
|
||||
xml_type_set(xn, CX_ELMNT);
|
||||
break;
|
||||
}
|
||||
xpar = x;
|
||||
x = xn;
|
||||
j = 0;
|
||||
while ((cvi = cvec_each(cvk, cvi)) != NULL) {
|
||||
keyname = cv_string_get(cvi);
|
||||
val2 = valvec[j++];
|
||||
if ((xn = xml_new(keyname, x)) == NULL)
|
||||
goto done;
|
||||
xml_type_set(xn, CX_ELMNT);
|
||||
if ((xb = xml_new("body", xn)) == NULL)
|
||||
goto done;
|
||||
xml_type_set(xb, CX_BODY);
|
||||
if (xml_value_set(xb, val2) <0)
|
||||
goto done;
|
||||
}
|
||||
}
|
||||
else{
|
||||
xpar = x;
|
||||
x = xn;
|
||||
}
|
||||
if (cvk){
|
||||
cvec_free(cvk);
|
||||
cvk = NULL;
|
||||
}
|
||||
break;
|
||||
default: /* eg Y_CONTAINER, Y_LEAF */
|
||||
if ((xn = xml_find(x, name)) == NULL){
|
||||
switch (op){
|
||||
case OP_DELETE: /* not here, should be here */
|
||||
clicon_err(OE_XML, 0, "Object to delete does not exist");
|
||||
goto done;
|
||||
break;
|
||||
case OP_REMOVE:
|
||||
goto ok; /* not here, no need to remove */
|
||||
break;
|
||||
case OP_CREATE:
|
||||
if (i==nvec) /* Last, dont create here */
|
||||
break;
|
||||
default:
|
||||
if ((xn = xml_new(name, x)) == NULL)
|
||||
goto done;
|
||||
xml_type_set(xn, CX_ELMNT);
|
||||
break;
|
||||
}
|
||||
}
|
||||
else{
|
||||
if (op==OP_CREATE && i==nvec){ /* here, should not be here */
|
||||
clicon_err(OE_XML, 0, "Object to create already exists");
|
||||
goto done;
|
||||
}
|
||||
}
|
||||
xpar = x;
|
||||
x = xn;
|
||||
break;
|
||||
}
|
||||
}
|
||||
*xp = x;
|
||||
*xparp = xpar;
|
||||
*yp = (yang_node*)y;
|
||||
ok:
|
||||
retval = 0;
|
||||
done:
|
||||
if (vec)
|
||||
free(vec);
|
||||
if (valvec)
|
||||
free(valvec);
|
||||
return retval;
|
||||
}
|
||||
|
||||
/*! Given a modification tree, check existing matching child in the base tree
|
||||
* param[in] x0 Base tree node
|
||||
* param[in] x1c Modification tree child
|
||||
* param[in] yc Yang spec of tree child
|
||||
* param[in] x0 Base tree node
|
||||
* param[in] x1c Modification tree child
|
||||
* param[in] yc Yang spec of tree child
|
||||
* param[out] x0cp Matching base tree child (if any)
|
||||
*/
|
||||
static cxobj *
|
||||
static int
|
||||
match_base_child(cxobj *x0,
|
||||
cxobj *x1c,
|
||||
yang_stmt *yc)
|
||||
yang_stmt *yc,
|
||||
cxobj **x0cp)
|
||||
{
|
||||
int retval = -1;
|
||||
cxobj *x0c = NULL;
|
||||
char *keyname;
|
||||
cvec *cvk = NULL;
|
||||
|
|
@ -719,10 +515,12 @@ match_base_child(cxobj *x0,
|
|||
x0c = xml_find(x0, cname);
|
||||
break;
|
||||
}
|
||||
*x0cp = x0c;
|
||||
retval = 0;
|
||||
done:
|
||||
if (cvk)
|
||||
cvec_free(cvk);
|
||||
return x0c;
|
||||
return retval;
|
||||
}
|
||||
|
||||
/*! Modify a base tree x0 with x1 with yang spec y according to operation op
|
||||
|
|
@ -730,165 +528,211 @@ match_base_child(cxobj *x0,
|
|||
* @param[in] x0p Parent of x0
|
||||
* @param[in] x1 xml tree which modifies base
|
||||
* @param[in] op OP_MERGE, OP_REPLACE, OP_REMOVE, etc
|
||||
* @param[in] y Yang spec corresponding to xml-node x0. NULL if x0 is NULL
|
||||
* @param[in] y0 Yang spec corresponding to xml-node x0. NULL if x0 is NULL
|
||||
* @param[in] yspec Top-level yang spec (if y is NULL)
|
||||
* Assume x0 and x1 are same on entry and that y is the spec
|
||||
* @see put in clixon_keyvalue.c
|
||||
*/
|
||||
static int
|
||||
text_modify(cxobj *x0,
|
||||
yang_node *y0,
|
||||
cxobj *x0p,
|
||||
cxobj *x1,
|
||||
enum operation_type op,
|
||||
yang_node *y,
|
||||
yang_spec *yspec)
|
||||
enum operation_type op)
|
||||
{
|
||||
int retval = -1;
|
||||
char *opstr;
|
||||
char *name;
|
||||
char *cname; /* child name */
|
||||
char *x1name;
|
||||
char *x1cname; /* child name */
|
||||
cxobj *x0c; /* base child */
|
||||
cxobj *x0b; /* base body */
|
||||
cxobj *x1c; /* mod child */
|
||||
char *x1bstr; /* mod body string */
|
||||
yang_stmt *yc; /* yang child */
|
||||
|
||||
clicon_debug(1, "%s %s", __FUNCTION__, x0?xml_name(x0):"");
|
||||
assert(x1 && xml_type(x1) == CX_ELMNT);
|
||||
assert(y0);
|
||||
/* Check for operations embedded in tree according to netconf */
|
||||
if (x1 && (opstr = xml_find_value(x1, "operation")) != NULL)
|
||||
if ((opstr = xml_find_value(x1, "operation")) != NULL)
|
||||
if (xml_operation(opstr, &op) < 0)
|
||||
goto done;
|
||||
if (x1 == NULL){
|
||||
x1name = xml_name(x1);
|
||||
if (y0->yn_keyword == Y_LEAF_LIST || y0->yn_keyword == Y_LEAF){
|
||||
x1bstr = xml_body(x1);
|
||||
switch(op){
|
||||
case OP_REPLACE:
|
||||
if (x0)
|
||||
xml_purge(x0);
|
||||
case OP_CREATE:
|
||||
case OP_MERGE:
|
||||
break;
|
||||
case OP_CREATE:
|
||||
if (x0){
|
||||
clicon_err(OE_XML, 0, "Object to create already exists");
|
||||
goto done;
|
||||
}
|
||||
case OP_NONE: /* fall thru */
|
||||
case OP_MERGE:
|
||||
case OP_REPLACE:
|
||||
if (x0==NULL){
|
||||
// int iamkey=0;
|
||||
if ((x0 = xml_new_spec(x1name, x0p, y0)) == NULL)
|
||||
goto done;
|
||||
#if 0
|
||||
/* If it is key I dont want to mark it */
|
||||
if ((iamkey=yang_key_match(y0->yn_parent, x1name)) < 0)
|
||||
goto done;
|
||||
if (!iamkey && op==OP_NONE)
|
||||
#else
|
||||
if (op==OP_NONE)
|
||||
#endif
|
||||
xml_flag_set(x0, XML_FLAG_NONE); /* Mark for potential deletion */
|
||||
if (x1bstr){ /* empty type does not have body */
|
||||
if ((x0b = xml_new("body", x0)) == NULL)
|
||||
goto done;
|
||||
xml_type_set(x0b, CX_BODY);
|
||||
}
|
||||
}
|
||||
if (x1bstr){
|
||||
if ((x0b = xml_body_get(x0)) == NULL){
|
||||
if ((x0b = xml_new("body", x0)) == NULL)
|
||||
goto done;
|
||||
xml_type_set(x0b, CX_BODY);
|
||||
}
|
||||
if (xml_value_set(x0b, x1bstr) < 0)
|
||||
goto done;
|
||||
}
|
||||
break;
|
||||
case OP_DELETE:
|
||||
if (x0==NULL){
|
||||
clicon_err(OE_XML, 0, "Object to delete does not exist");
|
||||
goto done;
|
||||
}
|
||||
case OP_REMOVE: /* fall thru */
|
||||
if (x0)
|
||||
xml_purge(x0);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
else {
|
||||
assert(xml_type(x1) == CX_ELMNT);
|
||||
name = xml_name(x1);
|
||||
if (y && (y->yn_keyword == Y_LEAF_LIST || y->yn_keyword == Y_LEAF)){
|
||||
x1bstr = xml_body(x1);
|
||||
switch(op){
|
||||
case OP_CREATE:
|
||||
if (x0){
|
||||
clicon_err(OE_XML, 0, "Object to create already exists");
|
||||
} /* switch op */
|
||||
} /* if LEAF|LEAF_LIST */
|
||||
else { /* eg Y_CONTAINER, Y_LIST */
|
||||
switch(op){
|
||||
case OP_CREATE:
|
||||
if (x0){
|
||||
clicon_err(OE_XML, 0, "Object to create already exists");
|
||||
goto done;
|
||||
}
|
||||
case OP_REPLACE: /* fall thru */
|
||||
if (x0){
|
||||
xml_purge(x0);
|
||||
x0 = NULL;
|
||||
}
|
||||
case OP_NONE: /* fall thru */
|
||||
case OP_MERGE:
|
||||
if (x0==NULL){
|
||||
if ((x0 = xml_new_spec(x1name, x0p, y0)) == NULL)
|
||||
goto done;
|
||||
if (op==OP_NONE)
|
||||
xml_flag_set(x0, XML_FLAG_NONE); /* Mark for potential deletion */
|
||||
}
|
||||
/* Loop through children of the modification tree */
|
||||
x1c = NULL;
|
||||
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){
|
||||
clicon_err(OE_YANG, errno, "No yang node found: %s", x1cname);
|
||||
goto done;
|
||||
}
|
||||
/* Fall thru */
|
||||
case OP_NONE: /* XXX */
|
||||
case OP_MERGE:
|
||||
case OP_REPLACE:
|
||||
if (x0==NULL){
|
||||
if ((x0 = xml_new_spec(name, x0p, y)) == NULL)
|
||||
goto done;
|
||||
if (op==OP_NONE)
|
||||
xml_flag_set(x0, XML_FLAG_NONE); /* Mark for potential deletion */
|
||||
if (x1bstr){ /* empty type does not have body */
|
||||
if ((x0b = xml_new("body", x0)) == NULL)
|
||||
goto done;
|
||||
xml_type_set(x0b, CX_BODY);
|
||||
}
|
||||
}
|
||||
if (x1bstr){
|
||||
if ((x0b = xml_body_get(x0)) == NULL){
|
||||
if ((x0b = xml_new("body", x0)) == NULL)
|
||||
goto done;
|
||||
xml_type_set(x0b, CX_BODY);
|
||||
}
|
||||
if (xml_value_set(x0b, x1bstr) < 0)
|
||||
goto done;
|
||||
}
|
||||
break;
|
||||
case OP_DELETE:
|
||||
if (x0==NULL){
|
||||
clicon_err(OE_XML, 0, "Object to delete does not exist");
|
||||
/* See if there is a corresponding node in the base tree */
|
||||
x0c = NULL;
|
||||
if (yc && match_base_child(x0, x1c, yc, &x0c) < 0)
|
||||
goto done;
|
||||
}
|
||||
case OP_REMOVE:
|
||||
if (x0)
|
||||
xml_purge(x0);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
} /* switch op */
|
||||
} /* if LEAF|LEAF_LIST */
|
||||
else { /* eg Y_CONTAINER */
|
||||
switch(op){
|
||||
case OP_CREATE:
|
||||
/* top-level object <config/> is a special case, ie when
|
||||
* x0 parent is NULL
|
||||
* or x1 is empty
|
||||
*/
|
||||
if ((x0p && x0) ||
|
||||
(x0p==NULL && xml_child_nr(x1) == 0)){
|
||||
clicon_err(OE_XML, 0, "Object to create already exists");
|
||||
if (text_modify(x0c, (yang_node*)yc, x0, x1c, op) < 0)
|
||||
goto done;
|
||||
}
|
||||
case OP_REPLACE:
|
||||
/* top-level object <config/> is a special case, ie when
|
||||
* x0 parent is NULL,
|
||||
* or x1 is empty
|
||||
*/
|
||||
if ((x0p && x0) ||
|
||||
(x0p==NULL && xml_child_nr(x1) == 0)){
|
||||
xml_purge(x0);
|
||||
x0 = NULL;
|
||||
}
|
||||
case OP_NONE: /* XXX */
|
||||
case OP_MERGE:
|
||||
if (x0==NULL){
|
||||
if ((x0 = xml_new_spec(name, x0p, y)) == NULL)
|
||||
goto done;
|
||||
if (op==OP_NONE)
|
||||
xml_flag_set(x0, XML_FLAG_NONE); /* Mark for potential deletion */
|
||||
}
|
||||
|
||||
/* Loop through children of the modification tree */
|
||||
x1c = NULL;
|
||||
while ((x1c = xml_child_each(x1, x1c, CX_ELMNT)) != NULL) {
|
||||
cname = xml_name(x1c);
|
||||
/* Get yang spec of the child */
|
||||
if (y == NULL)
|
||||
yc = yang_find_topnode(yspec, cname); /* still NULL for config */
|
||||
else{
|
||||
if ((yc = yang_find_syntax(y, cname)) == NULL){
|
||||
clicon_err(OE_YANG, errno, "No yang node found: %s", cname);
|
||||
goto done;
|
||||
}
|
||||
}
|
||||
/* See if there is a corresponding node in the base tree */
|
||||
x0c = yc?match_base_child(x0, x1c, yc):NULL;
|
||||
if (text_modify(x0c, x0, x1c, op, (yang_node*)yc, yspec) < 0)
|
||||
goto done;
|
||||
}
|
||||
break;
|
||||
case OP_DELETE:
|
||||
if (x0==NULL){
|
||||
clicon_err(OE_XML, 0, "Object to delete does not exist");
|
||||
goto done;
|
||||
}
|
||||
case OP_REMOVE:
|
||||
if (x0)
|
||||
xml_purge(x0);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
} /* CONTAINER switch op */
|
||||
} /* else Y_CONTAINER */
|
||||
} /* x1 != NULL */
|
||||
}
|
||||
break;
|
||||
case OP_DELETE:
|
||||
if (x0==NULL){
|
||||
clicon_err(OE_XML, 0, "Object to delete does not exist");
|
||||
goto done;
|
||||
}
|
||||
case OP_REMOVE: /* fall thru */
|
||||
if (x0)
|
||||
xml_purge(x0);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
} /* CONTAINER switch op */
|
||||
} /* else Y_CONTAINER */
|
||||
// ok:
|
||||
retval = 0;
|
||||
done:
|
||||
return retval;
|
||||
}
|
||||
|
||||
/*! Modify a top-level base tree x0 with modification tree x1
|
||||
* @param[in] x0 Base xml tree (can be NULL in add scenarios)
|
||||
* @param[in] x1 xml tree which modifies base
|
||||
* @param[in] yspec Top-level yang spec (if y is NULL)
|
||||
* @param[in] op OP_MERGE, OP_REPLACE, OP_REMOVE, etc
|
||||
* @see text_modify
|
||||
*/
|
||||
static int
|
||||
text_modify_top(cxobj *x0,
|
||||
cxobj *x1,
|
||||
yang_spec *yspec,
|
||||
enum operation_type op)
|
||||
{
|
||||
int retval = -1;
|
||||
char *x1cname; /* child name */
|
||||
cxobj *x0c; /* base child */
|
||||
cxobj *x1c; /* mod child */
|
||||
yang_stmt *yc; /* yang child */
|
||||
|
||||
/* Assure top-levels are 'config' */
|
||||
assert(x0 && strcmp(xml_name(x0),"config")==0);
|
||||
assert(x1 && strcmp(xml_name(x1),"config")==0);
|
||||
|
||||
/* Special case if x1 is empty, top-level only <config/> */
|
||||
if (!xml_child_nr(x1)){ /* base tree not empty */
|
||||
if (xml_child_nr(x0))
|
||||
switch(op){
|
||||
case OP_DELETE:
|
||||
case OP_REMOVE:
|
||||
case OP_REPLACE:
|
||||
x0c = NULL;
|
||||
while ((x0c = xml_child_each(x0, x0c, CX_ELMNT)) != NULL)
|
||||
xml_purge(x0c);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
else /* base tree empty */
|
||||
switch(op){
|
||||
case OP_DELETE:
|
||||
clicon_err(OE_XML, 0, "Object to delete does not exist");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
/* Loop through children of the modification tree */
|
||||
x1c = NULL;
|
||||
while ((x1c = xml_child_each(x1, x1c, CX_ELMNT)) != NULL) {
|
||||
x1cname = xml_name(x1c);
|
||||
/* Get yang spec of the child */
|
||||
if ((yc = yang_find_topnode(yspec, x1cname)) == NULL){
|
||||
clicon_err(OE_YANG, ENOENT, "No yang spec");
|
||||
goto done;
|
||||
}
|
||||
/* See if there is a corresponding node in the base tree */
|
||||
if (match_base_child(x0, x1c, yc, &x0c) < 0)
|
||||
goto done;
|
||||
if (text_modify(x0c, (yang_node*)yc, x0, x1c, op) < 0)
|
||||
goto done;
|
||||
}
|
||||
retval = 0;
|
||||
done:
|
||||
return retval;
|
||||
}
|
||||
|
||||
|
||||
/*! Modify database provided an xml tree and an operation
|
||||
*
|
||||
* @param[in] xh XMLDB handle
|
||||
|
|
@ -896,9 +740,7 @@ text_modify(cxobj *x0,
|
|||
* @param[in] op OP_MERGE: just add it.
|
||||
* OP_REPLACE: first delete whole database
|
||||
* OP_NONE: operation attribute in xml determines operation
|
||||
* @param[in] api_path According to restconf (Sec 3.5.1.1 in [restconf-draft 13
|
||||
])
|
||||
* @param[in] xadd xml-tree to merge/replace. Top-level symbol is 'config'.
|
||||
* @param[in] x1 xml-tree to merge/replace. Top-level symbol is 'config'.
|
||||
* Should be empty or '<config/>' if delete?
|
||||
* @retval 0 OK
|
||||
* @retval -1 Error
|
||||
|
|
@ -910,34 +752,21 @@ text_modify(cxobj *x0,
|
|||
* if (xmldb_put(h, "running", OP_MERGE, "/", xt) < 0)
|
||||
* err;
|
||||
* @endcode
|
||||
*/
|
||||
y */
|
||||
int
|
||||
text_put(xmldb_handle xh,
|
||||
char *db,
|
||||
enum operation_type op,
|
||||
char *api_path,
|
||||
cxobj *xmod)
|
||||
cxobj *x1)
|
||||
{
|
||||
int retval = -1;
|
||||
struct text_handle *th = handle(xh);
|
||||
char *dbfile = NULL;
|
||||
int fd = -1;
|
||||
cbuf *cb = NULL;
|
||||
cbuf *xpcb = NULL; /* xpath cbuf */
|
||||
yang_spec *yspec;
|
||||
cxobj *xt = NULL;
|
||||
cxobj *xbase = NULL;
|
||||
cxobj *xbasep = NULL; /* parent */
|
||||
cxobj *xc;
|
||||
cxobj *xnew = NULL;
|
||||
yang_node *y = NULL;
|
||||
cxobj *x0 = NULL;
|
||||
|
||||
#if 0 /* Just ignore */
|
||||
if ((op==OP_DELETE || op==OP_REMOVE) && xmod){
|
||||
clicon_err(OE_XML, 0, "xml tree should be NULL for REMOVE/DELETE");
|
||||
goto done;
|
||||
}
|
||||
#endif
|
||||
if (text_db2file(th, db, &dbfile) < 0)
|
||||
goto done;
|
||||
if (dbfile==NULL){
|
||||
|
|
@ -953,56 +782,41 @@ text_put(xmldb_handle xh,
|
|||
goto done;
|
||||
}
|
||||
/* Parse file into XML tree */
|
||||
if ((clicon_xml_parse_file(fd, &xt, "</config>")) < 0)
|
||||
if ((clicon_xml_parse_file(fd, &x0, "</config>")) < 0)
|
||||
goto done;
|
||||
/* Always assert a top-level called "config".
|
||||
To ensure that, deal with two cases:
|
||||
1. File is empty <top/> -> rename top-level to "config" */
|
||||
if (xml_child_nr(xt) == 0){
|
||||
if (xml_name_set(xt, "config") < 0)
|
||||
if (xml_child_nr(x0) == 0){
|
||||
if (xml_name_set(x0, "config") < 0)
|
||||
goto done;
|
||||
}
|
||||
/* 2. File is not empty <top><config>...</config></top> -> replace root */
|
||||
else{
|
||||
assert(xml_child_nr(xt)==1);
|
||||
if (xml_rootchild(xt, 0, &xt) < 0)
|
||||
/* There should only be one element and called config */
|
||||
if (singleconfigroot(x0, &x0) < 0)
|
||||
goto done;
|
||||
}
|
||||
/* here xt looks like: <config>...</config> */
|
||||
/* If xpath find first occurence or api-path (this is where we apply xml) */
|
||||
if (api_path){
|
||||
if (text_apipath_modify(api_path, xt, op, yspec, &xbase, &xbasep, &y) < 0)
|
||||
goto done;
|
||||
}
|
||||
else{
|
||||
xbase = xt; /* defer y since x points to config */
|
||||
xbasep = xml_parent(xt); /* NULL */
|
||||
assert(strcmp(xml_name(xbase),"config")==0);
|
||||
}
|
||||
assert(strcmp(xml_name(x0),"config")==0);
|
||||
/* Here x0 looks like: <config>...</config> */
|
||||
/* Validate existing config tree */
|
||||
if (xml_apply(x0, CX_ELMNT, xml_spec_populate, yspec) < 0)
|
||||
goto done;
|
||||
|
||||
/* Validate modification tree */
|
||||
if (xml_apply(x1, CX_ELMNT, xml_spec_populate, yspec) < 0)
|
||||
goto done;
|
||||
|
||||
/*
|
||||
* Modify base tree x with modification xmod
|
||||
* Modify base tree x with modification x1
|
||||
*/
|
||||
if (op == OP_DELETE || op == OP_REMOVE){
|
||||
/* special case if top-level, dont purge top-level */
|
||||
if (xt == xbase){
|
||||
xc = NULL;
|
||||
while ((xc = xml_child_each(xt, xc, CX_ELMNT)) != NULL){
|
||||
xml_purge(xc);
|
||||
xc = NULL; /* reset iterator */
|
||||
}
|
||||
}
|
||||
else
|
||||
if (xbase)
|
||||
xml_purge(xbase);
|
||||
}
|
||||
else
|
||||
if (text_modify(xbase, xbasep, xmod, op, (yang_node*)y, yspec) < 0)
|
||||
goto done;
|
||||
/* Remove NONE nodes if all subs recursively are also NONE */
|
||||
if (xml_tree_prune_flagged(xt, XML_FLAG_NONE, 0, NULL) <0)
|
||||
if (text_modify_top(x0, x1, yspec, op) < 0)
|
||||
goto done;
|
||||
if (xml_apply(xt, CX_ELMNT, (xml_applyfn_t*)xml_flag_reset,
|
||||
|
||||
/* Remove NONE nodes if all subs recursively are also NONE */
|
||||
if (xml_tree_prune_flagged(x0, XML_FLAG_NONE, 0, NULL) <0)
|
||||
goto done;
|
||||
if (xml_apply(x0, CX_ELMNT, (xml_applyfn_t*)xml_flag_reset,
|
||||
(void*)XML_FLAG_NONE) < 0)
|
||||
goto done;
|
||||
// output:
|
||||
|
|
@ -1011,7 +825,7 @@ text_put(xmldb_handle xh,
|
|||
clicon_err(OE_XML, errno, "cbuf_new");
|
||||
goto done;
|
||||
}
|
||||
if (clicon_xml2cbuf(cb, xt, 0, 0) < 0)
|
||||
if (clicon_xml2cbuf(cb, x0, 0, 1) < 0)
|
||||
goto done;
|
||||
/* Reopen file in write mode */
|
||||
close(fd);
|
||||
|
|
@ -1019,7 +833,7 @@ text_put(xmldb_handle xh,
|
|||
clicon_err(OE_UNIX, errno, "open(%s)", dbfile);
|
||||
goto done;
|
||||
}
|
||||
if (write(fd, cbuf_get(cb), cbuf_len(cb)+1) < 0){
|
||||
if (write(fd, cbuf_get(cb), cbuf_len(cb)) < 0){
|
||||
clicon_err(OE_UNIX, errno, "write(%s)", dbfile);
|
||||
goto done;
|
||||
}
|
||||
|
|
@ -1031,12 +845,8 @@ text_put(xmldb_handle xh,
|
|||
close(fd);
|
||||
if (cb)
|
||||
cbuf_free(cb);
|
||||
if (xpcb)
|
||||
cbuf_free(xpcb);
|
||||
if (xt)
|
||||
xml_free(xt);
|
||||
if (xnew)
|
||||
xml_free(xnew);
|
||||
if (x0)
|
||||
xml_free(x0);
|
||||
return retval;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -41,8 +41,7 @@
|
|||
*/
|
||||
int text_get(xmldb_handle h, char *db, char *xpath,
|
||||
cxobj **xtop, cxobj ***xvec, size_t *xlen);
|
||||
int text_put(xmldb_handle h, char *db, enum operation_type op,
|
||||
char *api_path, cxobj *xt);
|
||||
int text_put(xmldb_handle h, char *db, enum operation_type op, cxobj *xt);
|
||||
int text_dump(FILE *f, char *dbfilename, char *rxkey);
|
||||
int text_copy(xmldb_handle h, char *from, char *to);
|
||||
int text_lock(xmldb_handle h, char *db, int pid);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue