datastore text works with replace
This commit is contained in:
parent
ee9b74d735
commit
d02015f456
15 changed files with 627 additions and 160 deletions
|
|
@ -110,96 +110,6 @@ clicon_debug_xml(int dbglevel,
|
||||||
return retval;
|
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
|
* @param[in] r Fastcgi request handle
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -46,7 +46,6 @@
|
||||||
int notfound(FCGX_Request *r);
|
int notfound(FCGX_Request *r);
|
||||||
int badrequest(FCGX_Request *r);
|
int badrequest(FCGX_Request *r);
|
||||||
int clicon_debug_xml(int dbglevel, char *str, cxobj *cx);
|
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);
|
int test(FCGX_Request *r, int dbg);
|
||||||
cbuf *readdata(FCGX_Request *r);
|
cbuf *readdata(FCGX_Request *r);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -162,7 +162,7 @@ api_data_get_gen(clicon_handle h,
|
||||||
yspec = clicon_dbspec_yang(h);
|
yspec = clicon_dbspec_yang(h);
|
||||||
if ((path = cbuf_new()) == NULL)
|
if ((path = cbuf_new()) == NULL)
|
||||||
goto done;
|
goto done;
|
||||||
if (xml_apipath2xpath(yspec, pcvec, pi, path) < 0){
|
if (api_path2xpath_cvv(yspec, pcvec, pi, path) < 0){
|
||||||
notfound(r);
|
notfound(r);
|
||||||
goto done;
|
goto done;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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>'
|
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
|
#ifdef HAVE_CONFIG_H
|
||||||
|
|
@ -217,7 +216,8 @@ main(int argc, char **argv)
|
||||||
usage(argv0);
|
usage(argv0);
|
||||||
if (xmldb_get(h, db, argv[1], &xt, NULL, 0) < 0)
|
if (xmldb_get(h, db, argv[1], &xt, NULL, 0) < 0)
|
||||||
goto done;
|
goto done;
|
||||||
clicon_xml2file(stdout, xt, 0, 1);
|
clicon_xml2file(stdout, xt, 0, 0);
|
||||||
|
fprintf(stdout, "\n");
|
||||||
}
|
}
|
||||||
else if (strcmp(cmd, "put")==0){
|
else if (strcmp(cmd, "put")==0){
|
||||||
if (argc != 4)
|
if (argc != 4)
|
||||||
|
|
|
||||||
|
|
@ -808,7 +808,6 @@ xmldb_put_xkey(struct kv_handle *kh,
|
||||||
|
|
||||||
{
|
{
|
||||||
int retval = -1;
|
int retval = -1;
|
||||||
cxobj *x = NULL;
|
|
||||||
yang_stmt *y = NULL;
|
yang_stmt *y = NULL;
|
||||||
yang_stmt *ykey;
|
yang_stmt *ykey;
|
||||||
char **vec = NULL;
|
char **vec = NULL;
|
||||||
|
|
@ -839,7 +838,7 @@ xmldb_put_xkey(struct kv_handle *kh,
|
||||||
if (kv_db2file(kh, db, &filename) < 0)
|
if (kv_db2file(kh, db, &filename) < 0)
|
||||||
goto done;
|
goto done;
|
||||||
if (xk == NULL || *xk!='/'){
|
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;
|
goto done;
|
||||||
}
|
}
|
||||||
if ((ckey = cbuf_new()) == NULL){
|
if ((ckey = cbuf_new()) == NULL){
|
||||||
|
|
@ -872,9 +871,8 @@ xmldb_put_xkey(struct kv_handle *kh,
|
||||||
cprintf(ckey, "/");
|
cprintf(ckey, "/");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
else
|
else if ((y = yang_find_topnode(yspec, name)) == NULL){
|
||||||
if ((y = yang_find_topnode(yspec, name)) == NULL){
|
clicon_err(OE_UNIX, errno, "No yang node found: %s", name);
|
||||||
clicon_err(OE_UNIX, errno, "No yang node found: %s", x?xml_name(x):"");
|
|
||||||
goto done;
|
goto done;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -916,7 +914,6 @@ xmldb_put_xkey(struct kv_handle *kh,
|
||||||
free(valvec);
|
free(valvec);
|
||||||
if ((valvec = clicon_strsep(restval, ",", &nvalvec)) == NULL)
|
if ((valvec = clicon_strsep(restval, ",", &nvalvec)) == NULL)
|
||||||
goto done;
|
goto done;
|
||||||
|
|
||||||
if (cvec_len(cvk) != nvalvec){
|
if (cvec_len(cvk) != nvalvec){
|
||||||
clicon_err(OE_XML, errno, "List %s key length mismatch", name);
|
clicon_err(OE_XML, errno, "List %s key length mismatch", name);
|
||||||
goto done;
|
goto done;
|
||||||
|
|
@ -931,7 +928,6 @@ xmldb_put_xkey(struct kv_handle *kh,
|
||||||
else
|
else
|
||||||
cprintf(ckey, "=");
|
cprintf(ckey, "=");
|
||||||
val2 = valvec[j++];
|
val2 = valvec[j++];
|
||||||
|
|
||||||
cprintf(ckey, "%s", val2);
|
cprintf(ckey, "%s", val2);
|
||||||
cbuf_reset(csubkey);
|
cbuf_reset(csubkey);
|
||||||
cprintf(csubkey, "%s/%s", cbuf_get(ckey), keyname);
|
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)
|
if ((exists = db_exists(filename, xk)) < 0)
|
||||||
goto done;
|
goto done;
|
||||||
if (exists == 0){
|
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;
|
goto done;
|
||||||
}
|
}
|
||||||
case OP_REMOVE:
|
case OP_REMOVE:
|
||||||
|
|
@ -1039,7 +1035,7 @@ static int
|
||||||
xmldb_put_restconf_api_path(struct kv_handle *kh,
|
xmldb_put_restconf_api_path(struct kv_handle *kh,
|
||||||
char *db,
|
char *db,
|
||||||
enum operation_type op,
|
enum operation_type op,
|
||||||
char *api_path,
|
char *xk,
|
||||||
cxobj *xt)
|
cxobj *xt)
|
||||||
{
|
{
|
||||||
int retval = -1;
|
int retval = -1;
|
||||||
|
|
@ -1047,6 +1043,12 @@ xmldb_put_restconf_api_path(struct kv_handle *kh,
|
||||||
yang_stmt *ykey;
|
yang_stmt *ykey;
|
||||||
char **vec = NULL;
|
char **vec = NULL;
|
||||||
int nvec;
|
int nvec;
|
||||||
|
#if 0
|
||||||
|
char **valvec = NULL;
|
||||||
|
int nvalvec;
|
||||||
|
int j;
|
||||||
|
char *restval;
|
||||||
|
#endif
|
||||||
int i;
|
int i;
|
||||||
char *name;
|
char *name;
|
||||||
cg_var *cvi;
|
cg_var *cvi;
|
||||||
|
|
@ -1071,8 +1073,8 @@ xmldb_put_restconf_api_path(struct kv_handle *kh,
|
||||||
}
|
}
|
||||||
if (kv_db2file(kh, db, &filename) < 0)
|
if (kv_db2file(kh, db, &filename) < 0)
|
||||||
goto done;
|
goto done;
|
||||||
if (api_path == NULL || *api_path!='/'){
|
if (xk == NULL || *xk!='/'){
|
||||||
clicon_err(OE_DB, 0, "Invalid api path: %s", api_path);
|
clicon_err(OE_DB, 0, "Invalid api path: %s", xk);
|
||||||
goto done;
|
goto done;
|
||||||
}
|
}
|
||||||
if ((ckey = cbuf_new()) == NULL){
|
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");
|
clicon_err(OE_UNIX, errno, "cbuf_new");
|
||||||
goto done;
|
goto done;
|
||||||
}
|
}
|
||||||
if ((vec = clicon_strsep(api_path, "/", &nvec)) == NULL)
|
if ((vec = clicon_strsep(xk, "/", &nvec)) == NULL)
|
||||||
goto done;
|
goto done;
|
||||||
/* Remove trailing '/'. Like in /a/ -> /a */
|
/* Remove trailing '/'. Like in /a/ -> /a */
|
||||||
if (nvec > 1 && !strlen(vec[nvec-1]))
|
if (nvec > 1 && !strlen(vec[nvec-1]))
|
||||||
nvec--;
|
nvec--;
|
||||||
if (nvec < 2){
|
if (nvec < 2){
|
||||||
clicon_err(OE_XML, 0, "Malformed key: %s", api_path);
|
clicon_err(OE_XML, 0, "Malformed key: %s", xk);
|
||||||
goto done;
|
goto done;
|
||||||
}
|
}
|
||||||
i = 1;
|
i = 1;
|
||||||
|
|
@ -1099,14 +1101,19 @@ xmldb_put_restconf_api_path(struct kv_handle *kh,
|
||||||
*keys = '\0';
|
*keys = '\0';
|
||||||
keys++;
|
keys++;
|
||||||
}
|
}
|
||||||
|
#if 0
|
||||||
|
if ((restval = index(name, '=')) != NULL){
|
||||||
|
*restval = '\0';
|
||||||
|
restval++;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
if (i==1){
|
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 "/" */
|
/* Special handling of "/" */
|
||||||
cprintf(ckey, "/%s", name);
|
cprintf(ckey, "/");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
else
|
else if ((y = yang_find_topnode(yspec, name)) == NULL){
|
||||||
if ((y = yang_find_topnode(yspec, name)) == NULL){
|
|
||||||
clicon_err(OE_UNIX, errno, "No yang node found: %s", name);
|
clicon_err(OE_UNIX, errno, "No yang node found: %s", name);
|
||||||
goto done;
|
goto done;
|
||||||
}
|
}
|
||||||
|
|
@ -1139,10 +1146,33 @@ xmldb_put_restconf_api_path(struct kv_handle *kh,
|
||||||
/* The value is a list of keys: <key>[ <key>]* */
|
/* The value is a list of keys: <key>[ <key>]* */
|
||||||
if ((cvk = yang_arg2cvec(ykey, " ")) == NULL)
|
if ((cvk = yang_arg2cvec(ykey, " ")) == NULL)
|
||||||
goto done;
|
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;
|
cvi = NULL;
|
||||||
/* Iterate over individual yang keys */
|
/* Iterate over individual yang keys */
|
||||||
while ((cvi = cvec_each(cvk, cvi)) != NULL) {
|
while ((cvi = cvec_each(cvk, cvi)) != NULL) {
|
||||||
keyname = cv_string_get(cvi);
|
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 = vec[i++]; /* No */
|
||||||
val2 = keys;
|
val2 = keys;
|
||||||
if (i>nvec){ /* XXX >= ? */
|
if (i>nvec){ /* XXX >= ? */
|
||||||
|
|
@ -1150,6 +1180,7 @@ xmldb_put_restconf_api_path(struct kv_handle *kh,
|
||||||
goto done;
|
goto done;
|
||||||
}
|
}
|
||||||
cprintf(ckey, "=%s", val2);
|
cprintf(ckey, "=%s", val2);
|
||||||
|
#endif
|
||||||
cbuf_reset(csubkey);
|
cbuf_reset(csubkey);
|
||||||
cprintf(csubkey, "%s/%s", cbuf_get(ckey), keyname);
|
cprintf(csubkey, "%s/%s", cbuf_get(ckey), keyname);
|
||||||
if (op == OP_MERGE || op == OP_REPLACE || op == OP_CREATE)
|
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);
|
cvec_free(cvk);
|
||||||
if (vec)
|
if (vec)
|
||||||
free(vec);
|
free(vec);
|
||||||
|
#if 0
|
||||||
|
if (valvec)
|
||||||
|
free(valvec);
|
||||||
|
#endif
|
||||||
unchunk_group(__FUNCTION__);
|
unchunk_group(__FUNCTION__);
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -306,10 +306,23 @@ text_get(xmldb_handle xh,
|
||||||
clicon_err(OE_UNIX, errno, "open(%s)", dbfile);
|
clicon_err(OE_UNIX, errno, "open(%s)", dbfile);
|
||||||
goto done;
|
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;
|
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? */
|
/* XXX Maybe the below is general function and should be moved to xmldb? */
|
||||||
|
|
||||||
if (xpath_vec(xt, xpath?xpath:"/", &xvec, &xlen) < 0)
|
if (xpath_vec(xt, xpath?xpath:"/", &xvec, &xlen) < 0)
|
||||||
goto done;
|
goto done;
|
||||||
|
|
||||||
|
|
@ -332,13 +345,15 @@ text_get(xmldb_handle xh,
|
||||||
*xlen0 = xlen;
|
*xlen0 = xlen;
|
||||||
xlen = 0;
|
xlen = 0;
|
||||||
}
|
}
|
||||||
if (xml_apply(xt, CX_ELMNT, xml_default, NULL) < 0)
|
if (0){ /* No xml_spec(xt) */
|
||||||
goto done;
|
if (xml_apply(xt, CX_ELMNT, xml_default, NULL) < 0)
|
||||||
/* XXX does not work for top-level */
|
goto done;
|
||||||
if (xml_apply(xt, CX_ELMNT, xml_order, NULL) < 0)
|
/* XXX does not work for top-level */
|
||||||
goto done;
|
if (xml_apply(xt, CX_ELMNT, xml_order, NULL) < 0)
|
||||||
if (xml_apply(xt, CX_ELMNT, xml_sanity, NULL) < 0)
|
goto done;
|
||||||
goto done;
|
if (xml_apply(xt, CX_ELMNT, xml_sanity, NULL) < 0)
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
if (debug>1)
|
if (debug>1)
|
||||||
clicon_xml2file(stderr, xt, 0, 1);
|
clicon_xml2file(stderr, xt, 0, 1);
|
||||||
*xtop = xt;
|
*xtop = xt;
|
||||||
|
|
@ -350,6 +365,182 @@ text_get(xmldb_handle xh,
|
||||||
return retval;
|
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
|
/*! Modify database provided an xml tree and an operation
|
||||||
*
|
*
|
||||||
* @param[in] xh XMLDB handle
|
* @param[in] xh XMLDB handle
|
||||||
|
|
@ -376,30 +567,105 @@ text_put(xmldb_handle xh,
|
||||||
char *db,
|
char *db,
|
||||||
enum operation_type op,
|
enum operation_type op,
|
||||||
char *api_path,
|
char *api_path,
|
||||||
cxobj *xt)
|
cxobj *xadd)
|
||||||
{
|
{
|
||||||
int retval = -1;
|
int retval = -1;
|
||||||
struct text_handle *th = handle(xh);
|
struct text_handle *th = handle(xh);
|
||||||
char *dbfile = NULL;
|
char *dbfile = NULL;
|
||||||
int fd = -1;
|
int fd = -1;
|
||||||
cbuf *cb = NULL;
|
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)
|
if (text_db2file(th, db, &dbfile) < 0)
|
||||||
goto done;
|
goto done;
|
||||||
if (dbfile==NULL){
|
if (dbfile==NULL){
|
||||||
clicon_err(OE_XML, 0, "dbfile NULL");
|
clicon_err(OE_XML, 0, "dbfile NULL");
|
||||||
goto done;
|
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);
|
clicon_err(OE_UNIX, errno, "open(%s)", dbfile);
|
||||||
goto done;
|
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){
|
if ((cb = cbuf_new()) == NULL){
|
||||||
clicon_err(OE_XML, errno, "cbuf_new");
|
clicon_err(OE_XML, errno, "cbuf_new");
|
||||||
goto done;
|
goto done;
|
||||||
}
|
}
|
||||||
if (clicon_xml2cbuf(cb, xt, 0, 0) < 0)
|
if (clicon_xml2cbuf(cb, xt, 0, 0) < 0)
|
||||||
goto done;
|
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){
|
if (write(fd, cbuf_get(cb), cbuf_len(cb)+1) < 0){
|
||||||
clicon_err(OE_UNIX, errno, "write(%s)", dbfile);
|
clicon_err(OE_UNIX, errno, "write(%s)", dbfile);
|
||||||
goto done;
|
goto done;
|
||||||
|
|
@ -410,6 +676,10 @@ text_put(xmldb_handle xh,
|
||||||
close(fd);
|
close(fd);
|
||||||
if (cb)
|
if (cb)
|
||||||
cbuf_free(cb);
|
cbuf_free(cb);
|
||||||
|
if (xpcb)
|
||||||
|
cbuf_free(xpcb);
|
||||||
|
if (xt)
|
||||||
|
xml_free(xt);
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -426,10 +696,23 @@ text_copy(xmldb_handle xh,
|
||||||
char *to)
|
char *to)
|
||||||
{
|
{
|
||||||
int retval = -1;
|
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;
|
retval = 0;
|
||||||
// done:
|
done:
|
||||||
|
if (fromfile)
|
||||||
|
free(fromfile);
|
||||||
|
if (tofile)
|
||||||
|
free(tofile);
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -563,10 +846,17 @@ text_delete(xmldb_handle xh,
|
||||||
char *db)
|
char *db)
|
||||||
{
|
{
|
||||||
int retval = -1;
|
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;
|
retval = 0;
|
||||||
// done:
|
done:
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -580,11 +870,23 @@ int
|
||||||
text_init(xmldb_handle xh,
|
text_init(xmldb_handle xh,
|
||||||
char *db)
|
char *db)
|
||||||
{
|
{
|
||||||
int retval = -1;
|
int retval = -1;
|
||||||
// struct text_handle *th = handle(xh);
|
struct text_handle *th = handle(xh);
|
||||||
|
char *filename = NULL;
|
||||||
|
int fd = -1;
|
||||||
|
|
||||||
retval = 0;
|
if (text_db2file(th, db, &filename) < 0)
|
||||||
// done:
|
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;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -56,10 +56,11 @@ static inline char * strdup4(char *str)
|
||||||
*/
|
*/
|
||||||
char **clicon_strsep(char *string, char *delim, int *nvec0);
|
char **clicon_strsep(char *string, char *delim, int *nvec0);
|
||||||
char *clicon_strjoin (int argc, char **argv, char *delim);
|
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
|
#ifndef HAVE_STRNDUP
|
||||||
char *clicon_strndup (const char *, size_t);
|
char *clicon_strndup (const char *, size_t);
|
||||||
#endif /* ! HAVE_STRNDUP */
|
#endif /* ! HAVE_STRNDUP */
|
||||||
int percent_encode(char *str, char **escp);
|
|
||||||
int percent_decode(char *esc, char **str);
|
|
||||||
|
|
||||||
#endif /* _CLIXON_STRING_H_ */
|
#endif /* _CLIXON_STRING_H_ */
|
||||||
|
|
|
||||||
|
|
@ -68,6 +68,7 @@ int xml_tree_prune_unmarked(cxobj *xt, int *upmark);
|
||||||
int xml_default(cxobj *x, void *arg);
|
int xml_default(cxobj *x, void *arg);
|
||||||
int xml_order(cxobj *x, void *arg);
|
int xml_order(cxobj *x, void *arg);
|
||||||
int xml_sanity(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_ */
|
#endif /* _CLIXON_XML_MAP_H_ */
|
||||||
|
|
|
||||||
|
|
@ -47,6 +47,8 @@
|
||||||
#include <regex.h>
|
#include <regex.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
|
||||||
|
#include <cligen/cligen.h>
|
||||||
|
|
||||||
/* clicon */
|
/* clicon */
|
||||||
#include "clixon_queue.h"
|
#include "clixon_queue.h"
|
||||||
#include "clixon_string.h"
|
#include "clixon_string.h"
|
||||||
|
|
@ -241,6 +243,97 @@ percent_decode(char *esc,
|
||||||
return retval;
|
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
|
/*! strndup() for systems without it, such as xBSD
|
||||||
*/
|
*/
|
||||||
#ifndef HAVE_STRNDUP
|
#ifndef HAVE_STRNDUP
|
||||||
|
|
@ -265,6 +358,8 @@ clicon_strndup (const char *str,
|
||||||
}
|
}
|
||||||
#endif /* ! HAVE_STRNDUP */
|
#endif /* ! HAVE_STRNDUP */
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Turn this on for uni-test programs
|
* Turn this on for uni-test programs
|
||||||
* Usage: clixon_string join
|
* Usage: clixon_string join
|
||||||
|
|
|
||||||
|
|
@ -843,8 +843,8 @@ clicon_xml2file(FILE *f,
|
||||||
int level,
|
int level,
|
||||||
int prettyprint)
|
int prettyprint)
|
||||||
{
|
{
|
||||||
cbuf *cb = NULL;
|
|
||||||
int retval = -1;
|
int retval = -1;
|
||||||
|
cbuf *cb = NULL;
|
||||||
|
|
||||||
if ((cb = cbuf_new()) == NULL){
|
if ((cb = cbuf_new()) == NULL){
|
||||||
clicon_err(OE_XML, errno, "cbuf_new");
|
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,
|
* x1 should be a created placeholder. If x1 is non-empty,
|
||||||
* the copied tree is appended to the existing tree.
|
* the copied tree is appended to the existing tree.
|
||||||
* @code
|
* @code
|
||||||
* x1 = xml_new("new", xc);
|
* x1 = xml_new("new", xparent);
|
||||||
* xml_copy(x0, x1);
|
* xml_copy(x0, x1);
|
||||||
* @endcode
|
* @endcode
|
||||||
*/
|
*/
|
||||||
|
|
@ -1200,7 +1200,7 @@ int
|
||||||
xml_copy(cxobj *x0,
|
xml_copy(cxobj *x0,
|
||||||
cxobj *x1)
|
cxobj *x1)
|
||||||
{
|
{
|
||||||
int retval = -1;
|
int retval = -1;
|
||||||
cxobj *x;
|
cxobj *x;
|
||||||
cxobj *xcopy;
|
cxobj *xcopy;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -114,7 +114,7 @@ xmldb_plugin_load(clicon_handle h,
|
||||||
/* Add API */
|
/* Add API */
|
||||||
if (clicon_xmldb_api_set(h, xa) < 0)
|
if (clicon_xmldb_api_set(h, xa) < 0)
|
||||||
goto done;
|
goto done;
|
||||||
clicon_log(LOG_WARNING, "xmldb plugin %s loaded", filename);
|
clicon_log(LOG_DEBUG, "xmldb plugin %s loaded", filename);
|
||||||
retval = 0;
|
retval = 0;
|
||||||
done:
|
done:
|
||||||
if (retval < 0 && handle)
|
if (retval < 0 && handle)
|
||||||
|
|
|
||||||
|
|
@ -1125,7 +1125,7 @@ xml_tree_prune_unmarked(cxobj *xt,
|
||||||
*/
|
*/
|
||||||
int
|
int
|
||||||
xml_default(cxobj *xt,
|
xml_default(cxobj *xt,
|
||||||
void *arg)
|
void *arg)
|
||||||
{
|
{
|
||||||
int retval = -1;
|
int retval = -1;
|
||||||
yang_stmt *ys;
|
yang_stmt *ys;
|
||||||
|
|
@ -1135,7 +1135,11 @@ xml_default(cxobj *xt,
|
||||||
cxobj *xb;
|
cxobj *xb;
|
||||||
char *str;
|
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 */
|
/* Check leaf defaults */
|
||||||
if (ys->ys_keyword == Y_CONTAINER || ys->ys_keyword == Y_LIST){
|
if (ys->ys_keyword == Y_CONTAINER || ys->ys_keyword == Y_LIST){
|
||||||
for (i=0; i<ys->ys_len; i++){
|
for (i=0; i<ys->ys_len; i++){
|
||||||
|
|
@ -1184,7 +1188,11 @@ xml_order(cxobj *xt,
|
||||||
char *yname; /* yang child name */
|
char *yname; /* yang child name */
|
||||||
char *xname; /* xml 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;
|
j0 = 0;
|
||||||
/* Go through xml children and ensure they are same order as yspec children */
|
/* Go through xml children and ensure they are same order as yspec children */
|
||||||
for (i=0; i<y->ys_len; i++){
|
for (i=0; i<y->ys_len; i++){
|
||||||
|
|
@ -1217,7 +1225,7 @@ xml_order(cxobj *xt,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
retval = 0;
|
retval = 0;
|
||||||
// done:
|
done:
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1232,7 +1240,11 @@ xml_sanity(cxobj *xt,
|
||||||
yang_stmt *ys;
|
yang_stmt *ys;
|
||||||
char *name;
|
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);
|
name = xml_name(xt);
|
||||||
if (ys==NULL){
|
if (ys==NULL){
|
||||||
clicon_err(OE_XML, 0, "No spec for xml node %s", name);
|
clicon_err(OE_XML, 0, "No spec for xml node %s", name);
|
||||||
|
|
@ -1248,18 +1260,33 @@ xml_sanity(cxobj *xt,
|
||||||
return retval;
|
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]
|
* eg a/b=c -> a/[b=c]
|
||||||
* @param[in] yspec Yang spec
|
* @param[in] yspec Yang spec
|
||||||
* @param[in] pcvec api-path as cvec
|
* @param[in] pcvec api-path as cvec
|
||||||
* @param[in] pi Length of cvec
|
* @param[in] pi Offset of cvec, where api-path starts
|
||||||
* @param[out] path The xpath as cligen bif variable string
|
* @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
|
int
|
||||||
xml_apipath2xpath(yang_spec *yspec,
|
api_path2xpath_cvv(yang_spec *yspec,
|
||||||
cvec *pcvec,
|
cvec *cvv,
|
||||||
int pi,
|
int offset,
|
||||||
cbuf *path)
|
cbuf *xpath)
|
||||||
{
|
{
|
||||||
int retval = -1;
|
int retval = -1;
|
||||||
int i;
|
int i;
|
||||||
|
|
@ -1272,12 +1299,12 @@ xml_apipath2xpath(yang_spec *yspec,
|
||||||
yang_stmt *ykey;
|
yang_stmt *ykey;
|
||||||
cg_var *cvi;
|
cg_var *cvi;
|
||||||
|
|
||||||
for (i=pi; i<cvec_len(pcvec); i++){
|
for (i=offset; i<cvec_len(cvv); i++){
|
||||||
cv = cvec_i(pcvec, i);
|
cv = cvec_i(cvv, i);
|
||||||
name = cv_name_get(cv);
|
name = cv_name_get(cv);
|
||||||
clicon_debug(1, "[%d] cvname:%s", i, name);
|
clicon_debug(1, "[%d] cvname:%s", i, name);
|
||||||
clicon_debug(1, "cv2str%d", cv2str(cv, NULL, 0));
|
clicon_debug(1, "cv2str%d", cv2str(cv, NULL, 0));
|
||||||
if (i == pi){
|
if (i == offset){
|
||||||
if ((y = yang_find_topnode(yspec, name)) == NULL){
|
if ((y = yang_find_topnode(yspec, name)) == NULL){
|
||||||
clicon_err(OE_UNIX, errno, "No yang node found: %s", name);
|
clicon_err(OE_UNIX, errno, "No yang node found: %s", name);
|
||||||
goto done;
|
goto done;
|
||||||
|
|
@ -1313,20 +1340,53 @@ xml_apipath2xpath(yang_spec *yspec,
|
||||||
goto done;
|
goto done;
|
||||||
cvi = NULL;
|
cvi = NULL;
|
||||||
/* Iterate over individual yang keys */
|
/* Iterate over individual yang keys */
|
||||||
cprintf(path, "/%s", name);
|
cprintf(xpath, "/%s", name);
|
||||||
v = val;
|
v = val;
|
||||||
while ((cvi = cvec_each(cvk, cvi)) != NULL){
|
while ((cvi = cvec_each(cvk, cvi)) != NULL){
|
||||||
cprintf(path, "[%s=%s]", cv_string_get(cvi), v);
|
cprintf(xpath, "[%s=%s]", cv_string_get(cvi), v);
|
||||||
v += strlen(v)+1;
|
v += strlen(v)+1;
|
||||||
}
|
}
|
||||||
if (val)
|
if (val)
|
||||||
free(val);
|
free(val);
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
cprintf(path, "%s%s", (i==pi?"":"/"), name);
|
cprintf(xpath, "%s%s", (i==offset?"":"/"), name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
retval = 0;
|
retval = 0;
|
||||||
done:
|
done:
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*! Translate from restconf api-path to xml xpath
|
||||||
|
* eg a/b=c -> 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;
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,9 +17,8 @@ new(){
|
||||||
# sleep 1
|
# 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(){
|
expectfn(){
|
||||||
|
|
||||||
cmd=$1
|
cmd=$1
|
||||||
expect=$2
|
expect=$2
|
||||||
ret=`$cmd`
|
ret=`$cmd`
|
||||||
|
|
@ -40,7 +39,8 @@ expectfn(){
|
||||||
fi
|
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(){
|
expecteof(){
|
||||||
cmd=$1
|
cmd=$1
|
||||||
input=$2
|
input=$2
|
||||||
|
|
@ -61,7 +61,8 @@ EOF
|
||||||
fi
|
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(){
|
expectwait(){
|
||||||
cmd=$1
|
cmd=$1
|
||||||
input=$2
|
input=$2
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
# Test2: backend and netconf basic functionality
|
# Test4: Yang specifics: multi-keys and empty type
|
||||||
|
|
||||||
# include err() and new() functions
|
# include err() and new() functions
|
||||||
. ./lib.sh
|
. ./lib.sh
|
||||||
|
|
|
||||||
63
test/test5.sh
Executable file
63
test/test5.sh
Executable file
|
|
@ -0,0 +1,63 @@
|
||||||
|
#!/bin/bash
|
||||||
|
# Test5: datastore
|
||||||
|
|
||||||
|
# include err() and new() functions
|
||||||
|
. ./lib.sh
|
||||||
|
|
||||||
|
datastore=datastore_client
|
||||||
|
|
||||||
|
cat <<EOF > /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 /" "^<config/>$"
|
||||||
|
|
||||||
|
new "datastore $name put top"
|
||||||
|
expectfn "$datastore $conf put replace / <config><x><y><a>foo</a><b>bar</b><c>fie</c></y></x></config>" ""
|
||||||
|
|
||||||
|
new "datastore $name get config"
|
||||||
|
expectfn "$datastore $conf get /" "^<config><x><y><a>foo</a><b>bar</b><c>fie</c></y></x></config>$"
|
||||||
|
|
||||||
|
new "datastore $name put delete"
|
||||||
|
expectfn "$datastore $conf put delete / <config/>" ""
|
||||||
|
|
||||||
|
new "datastore $name get deleted"
|
||||||
|
expectfn "$datastore $conf get /" "^<config/>$"
|
||||||
|
|
||||||
|
rm -rf $dir
|
||||||
|
}
|
||||||
|
|
||||||
|
#run keyvalue # cant get the put to work
|
||||||
|
run text
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue