datastore text works with replace

This commit is contained in:
Olof hagsand 2017-04-17 19:47:32 +02:00
parent ee9b74d735
commit d02015f456
15 changed files with 627 additions and 160 deletions

View file

@ -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 / '<config><interfaces><interface><name>eth0</name><enabled>true</enabled></interface></interfaces></config>'
*
*/
#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)

View file

@ -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: <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 >= ? */
@ -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;
}

View file

@ -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, "</clicon>")) < 0)
/* Parse file into XML tree */
if ((clicon_xml_parse_file(fd, &xt, "</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)
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)
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<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_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 '=<restval>'");
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: <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(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 <config/>");
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, "</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)
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)
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;
}