restconf; xmlput extended with api_path; xmldb_put_tree

This commit is contained in:
Olof hagsand 2016-09-20 21:51:08 +02:00
parent ebd53a34ee
commit b744a4ad8a
21 changed files with 732 additions and 1260 deletions

View file

@ -92,13 +92,14 @@
* | <aa><k>17</k></aa>| <------------- | /aa/17 |
* +-------------------+ +-----------------+
*
* ALternative for xmlkeyfmt would be xpath: eg
* Alternative for xmlkeyfmt would be xpath: eg
* instead of /interfaces/interface/%s/ipv4/address/ip/%s
* you can have: /interfaces/interface[name=%s]/ipv4/address/[ip=%s]
*/
/*! Recursive help function */
static int
yang2xmlkeyfmt_1(yang_stmt *ys, cbuf *cb)
yang2xmlkeyfmt_1(yang_stmt *ys,
cbuf *cb)
{
yang_node *yn;
yang_stmt *ykey;
@ -151,7 +152,8 @@ yang2xmlkeyfmt_1(yang_stmt *ys, cbuf *cb)
* @param[out] xpath String, needs to be freed after use
*/
int
yang2xmlkeyfmt(yang_stmt *ys, char **xkfmt)
yang2xmlkeyfmt(yang_stmt *ys,
char **xkfmt)
{
int retval = -1;
cbuf *cb = NULL;
@ -550,8 +552,8 @@ xml_tree_prune_unmarked(cxobj *xt,
}
/*!
* @param[in] xkey xmlkey
* @param[out] xt XML tree as result
* @param[in] xk xmlkey
* @param[out] xt XML tree as result
* XXX cannot handle top-level list
*/
static int
@ -976,14 +978,15 @@ xmldb_get_local(clicon_handle h,
}
/*! Get content of database using xpath.
/*! Get content of database using path.
* The function returns a minimal tree that includes all sub-trees that match
* xpath.
* @param[in] h Clicon handle
* @param[in] db running | candidate
* @param[in] xpath String with XPATH syntax
* @param[in] vector If set, return list of results in xvec, else single tree.
* @param[out] xtop XML tree. Freed by xml_free()p
* @param[out] xtop XML tree. Freed by xml_free()
* @param[out] xvec Vector of xml trees. Free after use
* @param[out] xlen Length of vector.
* @retval 0 OK
@ -1081,6 +1084,11 @@ put(char *dbname,
yang_stmt *y;
int exists;
clicon_debug(1, "%s xk0:%s ys:%s", __FUNCTION__, xk0, ys->ys_argument);
if (debug){
xml_print(stderr, xt);
// yang_print(stderr, (yang_node*)ys, 0);
}
if (get_operation(xt, &op) < 0)
goto done;
body = xml_body(xt);
@ -1146,6 +1154,7 @@ put(char *dbname,
return retval;
}
/*! Local variant of xmldb_put */
static int
xmldb_put_local(clicon_handle h,
@ -1174,10 +1183,10 @@ xmldb_put_local(clicon_handle h,
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 */
x, /* xml root node */
ys, /* yang statement of xml node */
op, /* operation, eg merge/delete */
"" /* aggregate xml key */
) < 0)
goto done;
}
@ -1201,10 +1210,12 @@ xmldb_put_local(clicon_handle h,
* The xml may contain the "operation" attribute which defines the operation.
* @code
* cxobj *xt;
* yang_spec *yspec = clicon_dbspec_yang(h);
* if (xmldb_put(dbname, xt, yspec, OP_MERGE) < 0)
* if (clicon_xml_parse_str("<a>17</a>", &xt) < 0)
* err;
* if (xmldb_put(h, "running", xt, OP_MERGE) < 0)
* err;
* @endcode
* @see xmldb_put_xkey for single key
*/
int
xmldb_put(clicon_handle h,
@ -1218,6 +1229,231 @@ xmldb_put(clicon_handle h,
return xmldb_put_local(h, db, xt, op);
}
/*! XXX: replace xk with xt
* @param[in] api_path According to restconf (Sec 3.5.1.1 in [restconf-draft 13])
* e.g container top {
list list1 {
key "key1 key2 key3";
* is referenced as
/restconf/data/top/list1=a,,foo
*/
static int
xmldb_put_tree_local(clicon_handle h,
char *db,
char *api_path,
cxobj *xt,
enum operation_type op)
{
int retval = -1;
yang_stmt *y = NULL;
yang_stmt *ykey;
char **vec;
int nvec;
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;
clicon_debug(1, "%s db:%s, api_path:%s", __FUNCTION__, db, api_path);
yspec = clicon_dbspec_yang(h);
if (db2file(h, db, &filename) < 0)
goto done;
if (api_path == NULL || *api_path!='/'){
clicon_err(OE_DB, 0, "Invalid api path: %s", api_path);
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_strsplit(api_path, "/", &nvec, __FUNCTION__)) == NULL)
goto done;
if (nvec < 2){
clicon_err(OE_XML, 0, "Malformed key: %s", api_path);
goto done;
}
i = 1;
while (i<nvec){
name = vec[i];
if ((keys = index(name, '=')) != NULL){
*keys = '\0';
keys++;
}
if (i==1){
if (!strlen(name) && (op==OP_DELETE || op == OP_REMOVE)){
/* Special handling of "/" */
cprintf(ckey, "/%s", name);
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:
// val2 = vec[i]; /* No */
val2 = keys;
if (i>=nvec){
clicon_err(OE_XML, errno, "Leaf-list %s without argument", name);
goto done;
}
// i++;
cprintf(ckey, "/%s", val2);
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;
cvi = NULL;
/* Iterate over individual yang keys */
while ((cvi = cvec_each(cvk, cvi)) != NULL) {
keyname = cv_string_get(cvi);
// 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);
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);
unchunk_group(__FUNCTION__);
return retval;
}
/*!
* @param[in] api_path According to restconf (Sec 3.5.1.1 in [restconf-draft 13])
*/
int
xmldb_put_tree(clicon_handle h,
char *db,
char *api_path,
cxobj *xt,
enum operation_type op)
{
if (clicon_xmldb_rpc(h))
return -1; /* XXX */
else
return xmldb_put_tree_local(h, db, api_path, xt, op);
}
/*! Local variant of xmldb_put_xkey */
static int
xmldb_put_xkey_local(clicon_handle h,
@ -1416,6 +1652,7 @@ xmldb_put_xkey_local(clicon_handle h,
* if (xmldb_put_xkey(h, db, "/aa/bb/17/name", "17", OP_MERGE) < 0)
* err;
* @endcode
* @see xmldb_put with xml-tree, no path
*/
int
xmldb_put_xkey(clicon_handle h,