diff --git a/apps/restconf/restconf_lib.c b/apps/restconf/restconf_lib.c
index 7f5f6e4d..150c50c7 100644
--- a/apps/restconf/restconf_lib.c
+++ b/apps/restconf/restconf_lib.c
@@ -110,96 +110,6 @@ clicon_debug_xml(int dbglevel,
return retval;
}
-/*! Split a string into a cligen variable vector using 1st and 2nd delimiter
- * Split a string first into elements delimited by delim1, then into
- * pairs delimited by delim2.
- * @param[in] string String to split
- * @param[in] delim1 First delimiter char that delimits between elements
- * @param[in] delim2 Second delimiter char for pairs within an element
- * @param[out] cvp Created cligen variable vector, NOTE: can be NULL
- * @retval 0 on OK
- * @retval -1 error
- *
- * Example, assuming delim1 = '&' and delim2 = '='
- * a=b&c=d -> [[a,"b"][c="d"]
- * kalle&c=d -> [[c="d"]] # Discard elements with no delim2
- * XXX differentiate between error and null cvec.
- */
-int
-str2cvec(char *string,
- char delim1,
- char delim2,
- cvec **cvp)
-{
- int retval = -1;
- char *s;
- char *s0 = NULL;;
- char *val; /* value */
- char *valu; /* unescaped value */
- char *snext; /* next element in string */
- cvec *cvv = NULL;
- cg_var *cv;
-
- clicon_debug(1, "%s %s", __FUNCTION__, string);
- if ((s0 = strdup(string)) == NULL){
- clicon_debug(1, "error strdup %s", strerror(errno));
- goto err;
- }
- s = s0;
- if ((cvv = cvec_new(0)) ==NULL){
- clicon_debug(1, "error cvec_new %s", strerror(errno));
- goto err;
- }
- while (s != NULL) {
- /*
- * In the pointer algorithm below:
- * name1=val1; name2=val2;
- * ^ ^ ^
- * | | |
- * s val snext
- */
- if ((snext = index(s, delim1)) != NULL)
- *(snext++) = '\0';
- if ((val = index(s, delim2)) != NULL){
- *(val++) = '\0';
- if (percent_decode(val, &valu) < 0)
- goto err;
- if ((cv = cvec_add(cvv, CGV_STRING)) == NULL){
- clicon_debug(1, "error cvec_add %s", strerror(errno));
- goto err;
- }
- while ((strlen(s) > 0) && isblank(*s))
- s++;
- cv_name_set(cv, s);
- cv_string_set(cv, valu);
- free(valu); valu = NULL;
- }
- else{
- if (strlen(s)){
- if ((cv = cvec_add(cvv, CGV_STRING)) == NULL){
- clicon_debug(1, "error cvec_add %s", strerror(errno));
- goto err;
- }
- cv_name_set(cv, s);
- cv_string_set(cv, "");
- }
- }
- s = snext;
- }
- retval = 0;
- done:
- *cvp = cvv;
- if (s0)
- free(s0);
- return retval;
- err:
- if (cvv){
- cvec_free(cvv);
- cvv = NULL;
- }
- goto done;
-}
-
/*!
* @param[in] r Fastcgi request handle
*/
diff --git a/apps/restconf/restconf_lib.h b/apps/restconf/restconf_lib.h
index 8eba97bb..f133b15f 100644
--- a/apps/restconf/restconf_lib.h
+++ b/apps/restconf/restconf_lib.h
@@ -46,7 +46,6 @@
int notfound(FCGX_Request *r);
int badrequest(FCGX_Request *r);
int clicon_debug_xml(int dbglevel, char *str, cxobj *cx);
-int str2cvec(char *string, char delim1, char delim2, cvec **cvp);
int test(FCGX_Request *r, int dbg);
cbuf *readdata(FCGX_Request *r);
diff --git a/apps/restconf/restconf_methods.c b/apps/restconf/restconf_methods.c
index f3e61858..b96b72b6 100644
--- a/apps/restconf/restconf_methods.c
+++ b/apps/restconf/restconf_methods.c
@@ -162,7 +162,7 @@ api_data_get_gen(clicon_handle h,
yspec = clicon_dbspec_yang(h);
if ((path = cbuf_new()) == NULL)
goto done;
- if (xml_apipath2xpath(yspec, pcvec, pi, path) < 0){
+ if (api_path2xpath_cvv(yspec, pcvec, pi, path) < 0){
notfound(r);
goto done;
}
diff --git a/datastore/datastore_client.c b/datastore/datastore_client.c
index 2a094e7b..9ecdf378 100644
--- a/datastore/datastore_client.c
+++ b/datastore/datastore_client.c
@@ -39,7 +39,6 @@ sudo ./datastore_client -d candidate -b /usr/local/var/routing -p /home/olof/src
sudo ./datastore_client -d candidate -b /usr/local/var/routing -p /home/olof/src/clixon/datastore/keyvalue/keyvalue.so -y /usr/local/share/routing/yang -m ietf-ip put merge / 'eth0true'
- *
*/
#ifdef HAVE_CONFIG_H
@@ -217,7 +216,8 @@ main(int argc, char **argv)
usage(argv0);
if (xmldb_get(h, db, argv[1], &xt, NULL, 0) < 0)
goto done;
- clicon_xml2file(stdout, xt, 0, 1);
+ clicon_xml2file(stdout, xt, 0, 0);
+ fprintf(stdout, "\n");
}
else if (strcmp(cmd, "put")==0){
if (argc != 4)
diff --git a/datastore/keyvalue/clixon_keyvalue.c b/datastore/keyvalue/clixon_keyvalue.c
index 65411349..8f73edd4 100644
--- a/datastore/keyvalue/clixon_keyvalue.c
+++ b/datastore/keyvalue/clixon_keyvalue.c
@@ -808,7 +808,6 @@ xmldb_put_xkey(struct kv_handle *kh,
{
int retval = -1;
- cxobj *x = NULL;
yang_stmt *y = NULL;
yang_stmt *ykey;
char **vec = NULL;
@@ -839,7 +838,7 @@ xmldb_put_xkey(struct kv_handle *kh,
if (kv_db2file(kh, db, &filename) < 0)
goto done;
if (xk == NULL || *xk!='/'){
- clicon_err(OE_DB, 0, "Invalid key: %s", xk);
+ clicon_err(OE_DB, 0, "Invalid api_path: %s", xk);
goto done;
}
if ((ckey = cbuf_new()) == NULL){
@@ -872,9 +871,8 @@ xmldb_put_xkey(struct kv_handle *kh,
cprintf(ckey, "/");
break;
}
- else
- if ((y = yang_find_topnode(yspec, name)) == NULL){
- clicon_err(OE_UNIX, errno, "No yang node found: %s", x?xml_name(x):"");
+ else if ((y = yang_find_topnode(yspec, name)) == NULL){
+ clicon_err(OE_UNIX, errno, "No yang node found: %s", name);
goto done;
}
}
@@ -916,7 +914,6 @@ xmldb_put_xkey(struct kv_handle *kh,
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;
@@ -931,7 +928,6 @@ xmldb_put_xkey(struct kv_handle *kh,
else
cprintf(ckey, "=");
val2 = valvec[j++];
-
cprintf(ckey, "%s", val2);
cbuf_reset(csubkey);
cprintf(csubkey, "%s/%s", cbuf_get(ckey), keyname);
@@ -975,7 +971,7 @@ xmldb_put_xkey(struct kv_handle *kh,
if ((exists = db_exists(filename, xk)) < 0)
goto done;
if (exists == 0){
- clicon_err(OE_DB, 0, "OP_DELETE: %s does not exists in database", xk);
+ clicon_err(OE_DB, 0, "OP_DELETE: %s does not exist in database", xk);
goto done;
}
case OP_REMOVE:
@@ -1039,7 +1035,7 @@ static int
xmldb_put_restconf_api_path(struct kv_handle *kh,
char *db,
enum operation_type op,
- char *api_path,
+ char *xk,
cxobj *xt)
{
int retval = -1;
@@ -1047,6 +1043,12 @@ xmldb_put_restconf_api_path(struct kv_handle *kh,
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;
@@ -1071,8 +1073,8 @@ xmldb_put_restconf_api_path(struct kv_handle *kh,
}
if (kv_db2file(kh, db, &filename) < 0)
goto done;
- if (api_path == NULL || *api_path!='/'){
- clicon_err(OE_DB, 0, "Invalid api path: %s", api_path);
+ if (xk == NULL || *xk!='/'){
+ clicon_err(OE_DB, 0, "Invalid api path: %s", xk);
goto done;
}
if ((ckey = cbuf_new()) == NULL){
@@ -1083,13 +1085,13 @@ xmldb_put_restconf_api_path(struct kv_handle *kh,
clicon_err(OE_UNIX, errno, "cbuf_new");
goto done;
}
- if ((vec = clicon_strsep(api_path, "/", &nvec)) == NULL)
+ 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", api_path);
+ clicon_err(OE_XML, 0, "Malformed key: %s", xk);
goto done;
}
i = 1;
@@ -1099,14 +1101,19 @@ xmldb_put_restconf_api_path(struct kv_handle *kh,
*keys = '\0';
keys++;
}
+#if 0
+ if ((restval = index(name, '=')) != NULL){
+ *restval = '\0';
+ restval++;
+ }
+#endif
if (i==1){
- if (!strlen(name) && (op==OP_DELETE || op == OP_REMOVE)){
+ if (strlen(name)==0 && (op==OP_DELETE || op == OP_REMOVE)){
/* Special handling of "/" */
- cprintf(ckey, "/%s", name);
+ cprintf(ckey, "/");
break;
}
- else
- if ((y = yang_find_topnode(yspec, name)) == NULL){
+ else if ((y = yang_find_topnode(yspec, name)) == NULL){
clicon_err(OE_UNIX, errno, "No yang node found: %s", name);
goto done;
}
@@ -1139,10 +1146,33 @@ xmldb_put_restconf_api_path(struct kv_handle *kh,
/* The value is a list of keys: [ ]* */
if ((cvk = yang_arg2cvec(ykey, " ")) == NULL)
goto done;
+#if 0
+ if (restval==NULL){
+ clicon_err(OE_XML, 0, "malformed key, expected '='");
+ 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 >= ? */
@@ -1150,6 +1180,7 @@ xmldb_put_restconf_api_path(struct kv_handle *kh,
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)
@@ -1233,6 +1264,10 @@ xmldb_put_restconf_api_path(struct kv_handle *kh,
cvec_free(cvk);
if (vec)
free(vec);
+#if 0
+ if (valvec)
+ free(valvec);
+#endif
unchunk_group(__FUNCTION__);
return retval;
}
diff --git a/datastore/text/clixon_xmldb_text.c b/datastore/text/clixon_xmldb_text.c
index 4b6b3b91..4ee296b8 100644
--- a/datastore/text/clixon_xmldb_text.c
+++ b/datastore/text/clixon_xmldb_text.c
@@ -306,10 +306,23 @@ text_get(xmldb_handle xh,
clicon_err(OE_UNIX, errno, "open(%s)", dbfile);
goto done;
}
- if ((clicon_xml_parse_file(fd, &xt, "")) < 0)
+ /* Parse file into XML tree */
+ if ((clicon_xml_parse_file(fd, &xt, "")) < 0)
goto done;
+ /* Always assert a top-level called "config".
+ To ensure that, deal with two cases:
+ 1. File is empty -> rename top-level to "config" */
+ if (xml_child_nr(xt) == 0){
+ if (xml_name_set(xt, "config") < 0)
+ goto done;
+ }
+ /* 2. File is not empty ... -> replace root */
+ else{
+ assert(xml_child_nr(xt)==1);
+ if (xml_rootchild(xt, 0, &xt) < 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;
@@ -332,13 +345,15 @@ text_get(xmldb_handle xh,
*xlen0 = xlen;
xlen = 0;
}
- if (xml_apply(xt, CX_ELMNT, xml_default, NULL) < 0)
- goto done;
- /* XXX does not work for top-level */
- if (xml_apply(xt, CX_ELMNT, xml_order, NULL) < 0)
- goto done;
- if (xml_apply(xt, CX_ELMNT, xml_sanity, NULL) < 0)
- goto done;
+ if (0){ /* No xml_spec(xt) */
+ if (xml_apply(xt, CX_ELMNT, xml_default, NULL) < 0)
+ goto done;
+ /* XXX does not work for top-level */
+ if (xml_apply(xt, CX_ELMNT, xml_order, NULL) < 0)
+ goto done;
+ if (xml_apply(xt, CX_ELMNT, xml_sanity, NULL) < 0)
+ goto done;
+ }
if (debug>1)
clicon_xml2file(stderr, xt, 0, 1);
*xtop = xt;
@@ -350,6 +365,182 @@ text_get(xmldb_handle xh,
return retval;
}
+/*! Check if child with fullmatch exists
+ * param[in] cvk vector of index keys
+*/
+static cxobj *
+find_keys(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; /* All keys must match. */
+ 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)
+ continue;
+ if (strcmp(body, val))
+ continue;
+ }
+ return xi;
+ }
+ return NULL;
+}
+
+/*! Create tree from api-path, ie fill in xml tree from the path
+ */
+static int
+text_create_tree(char *xk,
+ cxobj *xt,
+ enum operation_type op,
+ yang_spec *yspec,
+ cxobj **xp)
+{
+ 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 *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;
+ if (xk == NULL || *xk!='/'){
+ clicon_err(OE_DB, 0, "Invalid key: %s", xk);
+ 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 < 1){
+ clicon_err(OE_XML, 0, "Malformed key: %s", xk);
+ goto done;
+ }
+ i = 1;
+ while (i 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_UNIX, 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 '='");
+ goto done;
+ }
+ fprintf(stderr, "XXX create =%s\n", restval);
+ x = xn;
+ 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: [ ]* */
+ if ((cvk = yang_arg2cvec(ykey, " ")) == NULL)
+ goto done;
+ if (restval==NULL){
+ clicon_err(OE_XML, 0, "malformed key, expected '='");
+ 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(x, name, cvk, valvec)) == NULL){
+ /* create them, bit not if delete op */
+ if (op == OP_DELETE || op == OP_REMOVE){
+ retval = 0;
+ goto done;
+ }
+ if ((xn = xml_new(name, x)) == NULL)
+ goto done;
+ xml_type_set(xn, CX_ELMNT);
+ 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(keyname, xn)) == NULL)
+ goto done;
+ xml_type_set(xb, CX_BODY);
+ if (xml_value_set(xb, val2) <0)
+ goto done;
+ }
+ }
+ if (cvk){
+ cvec_free(cvk);
+ cvk = NULL;
+ }
+ break;
+ default: /* eg Y_CONTAINER */
+ if ((xn = xml_find(x, name)) == NULL){
+ /* Already removed */
+ if (op == OP_DELETE || op == OP_REMOVE){
+ retval = 0;
+ goto done;
+ }
+ if ((xn = xml_new(name, x)) == NULL)
+ goto done;
+ xml_type_set(xn, CX_ELMNT);
+ }
+ x = xn;
+ break;
+ }
+
+ }
+ *xp = x;
+ retval = 0;
+ done:
+ return retval;
+}
+
+
/*! Modify database provided an xml tree and an operation
*
* @param[in] xh XMLDB handle
@@ -376,30 +567,105 @@ text_put(xmldb_handle xh,
char *db,
enum operation_type op,
char *api_path,
- cxobj *xt)
+ cxobj *xadd)
{
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 *x = NULL;
+ cxobj *xc;
+ cxobj *xcopy = NULL;
+ if (xadd == NULL || strcmp(xml_name(xadd),"config")){
+ clicon_err(OE_XML, 0, "Misformed xml, should start with ");
+ goto done;
+ }
if (text_db2file(th, db, &dbfile) < 0)
goto done;
if (dbfile==NULL){
clicon_err(OE_XML, 0, "dbfile NULL");
goto done;
}
- if ((fd = open(dbfile, O_WRONLY | O_CREAT)) == -1) {
+ if ((yspec = th->th_yangspec) == NULL){
+ clicon_err(OE_YANG, ENOENT, "No yang spec");
+ goto done;
+ }
+ if ((fd = open(dbfile, O_RDONLY)) < 0) {
clicon_err(OE_UNIX, errno, "open(%s)", dbfile);
goto done;
}
+ /* Parse file into XML tree */
+ if ((clicon_xml_parse_file(fd, &xt, "")) < 0)
+ goto done;
+ /* Always assert a top-level called "config".
+ To ensure that, deal with two cases:
+ 1. File is empty -> rename top-level to "config" */
+ if (xml_child_nr(xt) == 0){
+ if (xml_name_set(xt, "config") < 0)
+ goto done;
+ }
+ /* 2. File is not empty ... -> replace root */
+ else{
+ assert(xml_child_nr(xt)==1);
+ if (xml_rootchild(xt, 0, &xt) < 0)
+ goto done;
+ }
+ /* If xpath find first occurence or api-path (this is where we apply xml) */
+ if (api_path){
+ if (text_create_tree(api_path, xt, op, yspec, &x) < 0)
+ goto done;
+ }
+ else
+ x = xt;
+ assert(x);
+ /* Here point of where xt is applied to xt is 'x' */
+ switch (op){
+ case OP_CREATE:
+ if (x){
+ clicon_err(OE_DB, 0, "OP_CREATE: already exists in database");
+ goto done;
+ }
+ case OP_REPLACE:
+ while ((xc = xml_child_i(x, 0)) != NULL)
+ xml_purge(xc);
+ case OP_MERGE:
+ /* Loop thru xadd's children */
+ xc = NULL;
+ while ((xc = xml_child_each(xadd, xc, CX_ELMNT)) != NULL){
+ if ((xcopy = xml_new("new", x)) == NULL)
+ goto done;
+ xml_copy(xc, xcopy);
+ }
+ break;
+ case OP_DELETE:
+ if (x==NULL){
+ clicon_err(OE_DB, 0, "OP_DELETE: does not exist in database");
+ goto done;
+ }
+ case OP_REMOVE:
+ while ((xc = xml_child_i(x, 0)) != NULL)
+ xml_purge(xc);
+ break;
+ case OP_NONE:
+ break;
+ }
if ((cb = cbuf_new()) == NULL){
clicon_err(OE_XML, errno, "cbuf_new");
goto done;
}
if (clicon_xml2cbuf(cb, xt, 0, 0) < 0)
goto done;
+ /* Reopen file in write mode */
+ close(fd);
+ if ((fd = open(dbfile, O_WRONLY | O_TRUNC, S_IRWXU)) < 0) {
+ clicon_err(OE_UNIX, errno, "open(%s)", dbfile);
+ goto done;
+ }
if (write(fd, cbuf_get(cb), cbuf_len(cb)+1) < 0){
clicon_err(OE_UNIX, errno, "write(%s)", dbfile);
goto done;
@@ -410,6 +676,10 @@ text_put(xmldb_handle xh,
close(fd);
if (cb)
cbuf_free(cb);
+ if (xpcb)
+ cbuf_free(xpcb);
+ if (xt)
+ xml_free(xt);
return retval;
}
@@ -426,10 +696,23 @@ text_copy(xmldb_handle xh,
char *to)
{
int retval = -1;
- // struct text_handle *th = handle(xh);
+ struct text_handle *th = handle(xh);
+ char *fromfile = NULL;
+ char *tofile = NULL;
+ /* XXX lock */
+ if (text_db2file(th, from, &fromfile) < 0)
+ goto done;
+ if (text_db2file(th, to, &tofile) < 0)
+ goto done;
+ if (clicon_file_copy(fromfile, tofile) < 0)
+ goto done;
retval = 0;
- // done:
+ done:
+ if (fromfile)
+ free(fromfile);
+ if (tofile)
+ free(tofile);
return retval;
}
@@ -563,10 +846,17 @@ text_delete(xmldb_handle xh,
char *db)
{
int retval = -1;
-// struct text_handle *th = handle(xh);
+ char *filename = NULL;
+ struct text_handle *th = handle(xh);
+ if (text_db2file(th, db, &filename) < 0)
+ goto done;
+ if (unlink(filename) < 0){
+ clicon_err(OE_DB, errno, "unlink %s", filename);
+ goto done;
+ }
retval = 0;
- // done:
+ done:
return retval;
}
@@ -580,11 +870,23 @@ int
text_init(xmldb_handle xh,
char *db)
{
- int retval = -1;
- // struct text_handle *th = handle(xh);
+ int retval = -1;
+ struct text_handle *th = handle(xh);
+ char *filename = NULL;
+ int fd = -1;
- retval = 0;
- // done:
+ if (text_db2file(th, db, &filename) < 0)
+ goto done;
+ if ((fd = open(filename, O_CREAT|O_WRONLY, S_IRWXU)) == -1) {
+ clicon_err(OE_UNIX, errno, "open(%s)", filename);
+ goto done;
+ }
+ retval = 0;
+ done:
+ if (filename)
+ free(filename);
+ if (fd != -1)
+ close(fd);
return retval;
}
diff --git a/lib/clixon/clixon_string.h b/lib/clixon/clixon_string.h
index d7fd4274..e0c9f1b7 100644
--- a/lib/clixon/clixon_string.h
+++ b/lib/clixon/clixon_string.h
@@ -56,10 +56,11 @@ static inline char * strdup4(char *str)
*/
char **clicon_strsep(char *string, char *delim, int *nvec0);
char *clicon_strjoin (int argc, char **argv, char *delim);
+int str2cvec(char *string, char delim1, char delim2, cvec **cvp);
+int percent_encode(char *str, char **escp);
+int percent_decode(char *esc, char **str);
#ifndef HAVE_STRNDUP
char *clicon_strndup (const char *, size_t);
#endif /* ! HAVE_STRNDUP */
-int percent_encode(char *str, char **escp);
-int percent_decode(char *esc, char **str);
#endif /* _CLIXON_STRING_H_ */
diff --git a/lib/clixon/clixon_xml_map.h b/lib/clixon/clixon_xml_map.h
index 5f816be2..1cabeff6 100644
--- a/lib/clixon/clixon_xml_map.h
+++ b/lib/clixon/clixon_xml_map.h
@@ -68,6 +68,7 @@ int xml_tree_prune_unmarked(cxobj *xt, int *upmark);
int xml_default(cxobj *x, void *arg);
int xml_order(cxobj *x, void *arg);
int xml_sanity(cxobj *x, void *arg);
-int xml_apipath2xpath(yang_spec *yspec, cvec *pcvec, int pi, cbuf *path);
+int api_path2xpath_cvv(yang_spec *yspec, cvec *cvv, int offset, cbuf *xpath);
+int api_path2xpath(yang_spec *yspec, char *api_path, cbuf *xpath);
#endif /* _CLIXON_XML_MAP_H_ */
diff --git a/lib/src/clixon_string.c b/lib/src/clixon_string.c
index 98de980f..06b9fc49 100644
--- a/lib/src/clixon_string.c
+++ b/lib/src/clixon_string.c
@@ -47,6 +47,8 @@
#include
#include
+#include
+
/* clicon */
#include "clixon_queue.h"
#include "clixon_string.h"
@@ -241,6 +243,97 @@ percent_decode(char *esc,
return retval;
}
+/*! Split a string into a cligen variable vector using 1st and 2nd delimiter
+ * Split a string first into elements delimited by delim1, then into
+ * pairs delimited by delim2.
+ * @param[in] string String to split
+ * @param[in] delim1 First delimiter char that delimits between elements
+ * @param[in] delim2 Second delimiter char for pairs within an element
+ * @param[out] cvp Created cligen variable vector, deallocate w cvec_free
+ * @retval 0 on OK
+ * @retval -1 error
+ *
+ * @example,
+ * Assuming delim1 = '&' and delim2 = '='
+ * a=b&c=d -> [[a,"b"][c="d"]
+ * kalle&c=d -> [[c="d"]] # Discard elements with no delim2
+ * XXX differentiate between error and null cvec.
+ */
+int
+str2cvec(char *string,
+ char delim1,
+ char delim2,
+ cvec **cvp)
+{
+ int retval = -1;
+ char *s;
+ char *s0 = NULL;;
+ char *val; /* value */
+ char *valu; /* unescaped value */
+ char *snext; /* next element in string */
+ cvec *cvv = NULL;
+ cg_var *cv;
+
+ if ((s0 = strdup(string)) == NULL){
+ clicon_err(OE_UNIX, errno, "strdup");
+ goto err;
+ }
+ s = s0;
+ if ((cvv = cvec_new(0)) ==NULL){
+ clicon_err(OE_UNIX, errno, "cvec_new");
+ goto err;
+ }
+ while (s != NULL) {
+ /*
+ * In the pointer algorithm below:
+ * name1=val1; name2=val2;
+ * ^ ^ ^
+ * | | |
+ * s val snext
+ */
+ if ((snext = index(s, delim1)) != NULL)
+ *(snext++) = '\0';
+ if ((val = index(s, delim2)) != NULL){
+ *(val++) = '\0';
+ if (percent_decode(val, &valu) < 0)
+ goto err;
+ if ((cv = cvec_add(cvv, CGV_STRING)) == NULL){
+ clicon_err(OE_UNIX, errno, "cvec_add");
+ goto err;
+ }
+ while ((strlen(s) > 0) && isblank(*s))
+ s++;
+ cv_name_set(cv, s);
+ cv_string_set(cv, valu);
+ free(valu); valu = NULL;
+ }
+ else{
+ if (strlen(s)){
+ if ((cv = cvec_add(cvv, CGV_STRING)) == NULL){
+ clicon_err(OE_UNIX, errno, "cvec_add");
+ goto err;
+ }
+ cv_name_set(cv, s);
+ cv_string_set(cv, "");
+ }
+ }
+ s = snext;
+ }
+ retval = 0;
+ done:
+ *cvp = cvv;
+ if (s0)
+ free(s0);
+ return retval;
+ err:
+ if (cvv){
+ cvec_free(cvv);
+ cvv = NULL;
+ }
+ goto done;
+}
+
+
/*! strndup() for systems without it, such as xBSD
*/
#ifndef HAVE_STRNDUP
@@ -265,6 +358,8 @@ clicon_strndup (const char *str,
}
#endif /* ! HAVE_STRNDUP */
+
+
/*
* Turn this on for uni-test programs
* Usage: clixon_string join
diff --git a/lib/src/clixon_xml.c b/lib/src/clixon_xml.c
index 7d1ec8bd..e474cd4f 100644
--- a/lib/src/clixon_xml.c
+++ b/lib/src/clixon_xml.c
@@ -843,8 +843,8 @@ clicon_xml2file(FILE *f,
int level,
int prettyprint)
{
- cbuf *cb = NULL;
int retval = -1;
+ cbuf *cb = NULL;
if ((cb = cbuf_new()) == NULL){
clicon_err(OE_XML, errno, "cbuf_new");
@@ -1192,7 +1192,7 @@ copy_one(cxobj *xn0,
* x1 should be a created placeholder. If x1 is non-empty,
* the copied tree is appended to the existing tree.
* @code
- * x1 = xml_new("new", xc);
+ * x1 = xml_new("new", xparent);
* xml_copy(x0, x1);
* @endcode
*/
@@ -1200,7 +1200,7 @@ int
xml_copy(cxobj *x0,
cxobj *x1)
{
- int retval = -1;
+ int retval = -1;
cxobj *x;
cxobj *xcopy;
diff --git a/lib/src/clixon_xml_db.c b/lib/src/clixon_xml_db.c
index 82a5b1ed..070f93fb 100644
--- a/lib/src/clixon_xml_db.c
+++ b/lib/src/clixon_xml_db.c
@@ -114,7 +114,7 @@ xmldb_plugin_load(clicon_handle h,
/* Add API */
if (clicon_xmldb_api_set(h, xa) < 0)
goto done;
- clicon_log(LOG_WARNING, "xmldb plugin %s loaded", filename);
+ clicon_log(LOG_DEBUG, "xmldb plugin %s loaded", filename);
retval = 0;
done:
if (retval < 0 && handle)
diff --git a/lib/src/clixon_xml_map.c b/lib/src/clixon_xml_map.c
index 2d460f55..4fe808a8 100644
--- a/lib/src/clixon_xml_map.c
+++ b/lib/src/clixon_xml_map.c
@@ -1125,7 +1125,7 @@ xml_tree_prune_unmarked(cxobj *xt,
*/
int
xml_default(cxobj *xt,
- void *arg)
+ void *arg)
{
int retval = -1;
yang_stmt *ys;
@@ -1135,7 +1135,11 @@ xml_default(cxobj *xt,
cxobj *xb;
char *str;
- ys = (yang_stmt*)xml_spec(xt);
+ if ((ys = (yang_stmt*)xml_spec(xt)) == NULL){
+ clicon_log(LOG_WARNING, "%s: no xml_spec(%s)", __FUNCTION__, xml_name(xt));
+ retval = 0;
+ goto done;
+ }
/* Check leaf defaults */
if (ys->ys_keyword == Y_CONTAINER || ys->ys_keyword == Y_LIST){
for (i=0; iys_len; i++){
@@ -1184,7 +1188,11 @@ xml_order(cxobj *xt,
char *yname; /* yang child name */
char *xname; /* xml child name */
- y = (yang_stmt*)xml_spec(xt);
+ if ((y = (yang_stmt*)xml_spec(xt)) == NULL){
+ clicon_log(LOG_WARNING, "%s: no xml_spec(%s)", __FUNCTION__, xml_name(xt));
+ retval = 0;
+ goto done;
+ }
j0 = 0;
/* Go through xml children and ensure they are same order as yspec children */
for (i=0; iys_len; i++){
@@ -1217,7 +1225,7 @@ xml_order(cxobj *xt,
}
}
retval = 0;
- // done:
+ done:
return retval;
}
@@ -1232,7 +1240,11 @@ xml_sanity(cxobj *xt,
yang_stmt *ys;
char *name;
- ys = (yang_stmt*)xml_spec(xt);
+ if ((ys = (yang_stmt*)xml_spec(xt)) == NULL){
+ clicon_log(LOG_WARNING, "%s: no xml_spec(%s)", __FUNCTION__, xml_name(xt));
+ retval = 0;
+ goto done;
+ }
name = xml_name(xt);
if (ys==NULL){
clicon_err(OE_XML, 0, "No spec for xml node %s", name);
@@ -1248,18 +1260,33 @@ xml_sanity(cxobj *xt,
return retval;
}
-/*! Translate from restconf api-path to xml xpath
+/*! Translate from restconf api-path in cvv form to xml xpath
* eg a/b=c -> a/[b=c]
* @param[in] yspec Yang spec
* @param[in] pcvec api-path as cvec
- * @param[in] pi Length of cvec
- * @param[out] path The xpath as cligen bif variable string
+ * @param[in] pi Offset of cvec, where api-path starts
+ * @param[out] path The xpath as cbuf variable string, must be initializeed
+ * The api-path has some wierd encoding, use api_path2xpath() if you are
+ * confused.
+ * It works like this:
+ * Assume origin incoming path is
+ * "www.foo.com/restconf/a/b=c", pi is 2 and pcvec is:
+ * ["www.foo.com" "restconf" "a" "b=c"]
+ * which means the api-path is ["a" "b=c"] corresponding to "a/b=c"
+ * @code
+ * cbuf *xpath = cbuf_new();
+ * if (api_path2xpath_cvv(yspec, cvv, i, xpath)
+ * err;
+ * ... access xpath as cbuf_get(xpath)
+ * cbuf_free(xpath)
+ * @endcode
+ * @see api_path2xpath for string api-path argument
*/
int
-xml_apipath2xpath(yang_spec *yspec,
- cvec *pcvec,
- int pi,
- cbuf *path)
+api_path2xpath_cvv(yang_spec *yspec,
+ cvec *cvv,
+ int offset,
+ cbuf *xpath)
{
int retval = -1;
int i;
@@ -1272,12 +1299,12 @@ xml_apipath2xpath(yang_spec *yspec,
yang_stmt *ykey;
cg_var *cvi;
- for (i=pi; i a/[b=c]
+ * @param[in] yspec Yang spec
+ * @param[in] str API-path as string
+ * @param[out] path The xpath as cbuf variable string, must be initializeed
+ * @code
+ * cbuf *xpath = cbuf_new();
+ * if (api_path2xpath(yspec, "a/b=c", xpath)
+ * err;
+ * ... access xpath as cbuf_get(xpath)
+ * cbuf_free(xpath)
+ * @endcode
+ */
+int
+api_path2xpath(yang_spec *yspec,
+ char *api_path,
+ cbuf *xpath)
+{
+ int retval = -1;
+ cvec *api_path_cvv = NULL;
+
+ /* rest url eg /album=ricky/foo */
+ if (str2cvec(api_path, '/', '=', &api_path_cvv) < 0)
+ goto done;
+ if (api_path2xpath_cvv(yspec, api_path_cvv, 0, xpath) < 0)
+ goto done;
+ retval = 0;
+ done:
+ if (api_path_cvv)
+ cvec_free(api_path_cvv);
+ return retval;
+}
diff --git a/test/lib.sh b/test/lib.sh
index ba9672a4..83353f18 100755
--- a/test/lib.sh
+++ b/test/lib.sh
@@ -17,9 +17,8 @@ new(){
# sleep 1
}
-# clicon_cli tester. First arg is command and second is expected outcome
+# clixon tester. First arg is command and second is expected outcome
expectfn(){
-
cmd=$1
expect=$2
ret=`$cmd`
@@ -40,7 +39,8 @@ expectfn(){
fi
}
-# clicon_cli tester. First arg is command and second is expected outcome
+# clixon tester. First arg is command second is stdin and
+# third is expected outcome
expecteof(){
cmd=$1
input=$2
@@ -61,7 +61,8 @@ EOF
fi
}
-# clicon_cli tester. First arg is command and second is expected outcome
+# clixon tester. First arg is command second is stdin and
+# third is expected outcome, fourth is how long to wait
expectwait(){
cmd=$1
input=$2
diff --git a/test/test4.sh b/test/test4.sh
index 1c472fbb..88fa31d6 100755
--- a/test/test4.sh
+++ b/test/test4.sh
@@ -1,5 +1,5 @@
#!/bin/bash
-# Test2: backend and netconf basic functionality
+# Test4: Yang specifics: multi-keys and empty type
# include err() and new() functions
. ./lib.sh
diff --git a/test/test5.sh b/test/test5.sh
new file mode 100755
index 00000000..80137f01
--- /dev/null
+++ b/test/test5.sh
@@ -0,0 +1,63 @@
+#!/bin/bash
+# Test5: datastore
+
+# include err() and new() functions
+. ./lib.sh
+
+datastore=datastore_client
+
+cat < /tmp/ietf-ip.yang
+module ietf-ip{
+ container x {
+ list y {
+ key "a b";
+ leaf a {
+ type string;
+ }
+ leaf b {
+ type string;
+ }
+ leaf c {
+ type string;
+ }
+ }
+ leaf d {
+ type empty;
+ }
+ }
+}
+EOF
+
+run(){
+ name=$1
+ dir=/tmp/$name
+ if [ ! -d $dir ]; then
+ mkdir $dir
+ fi
+
+ rm -rf $dir/*
+ conf="-d candidate -b $dir -p ../datastore/$name/$name.so -y /tmp -m ietf-ip"
+ new "datastore $name init"
+ expectfn "$datastore $conf init" ""
+
+ new "datastore $name get empty"
+ expectfn "$datastore $conf get /" "^$"
+
+ new "datastore $name put top"
+ expectfn "$datastore $conf put replace / foobarfie" ""
+
+ new "datastore $name get config"
+ expectfn "$datastore $conf get /" "^foobarfie$"
+
+ new "datastore $name put delete"
+ expectfn "$datastore $conf put delete / " ""
+
+ new "datastore $name get deleted"
+ expectfn "$datastore $conf get /" "^$"
+
+ rm -rf $dir
+}
+
+#run keyvalue # cant get the put to work
+run text
+