* Added experimental binary search API function: xml_binsearch
* Added content parameter to `clicon_rpc_get` (-1 or CONTENT_ALL is default) * Reverted change in clixon-lib.yang
This commit is contained in:
parent
8b7b7b0f60
commit
10a2dbe8ec
17 changed files with 137 additions and 250 deletions
|
|
@ -80,6 +80,7 @@
|
|||
#include <clixon/clixon_yang_module.h>
|
||||
#include <clixon/clixon_stream.h>
|
||||
#include <clixon/clixon_proto.h>
|
||||
#include <clixon/clixon_netconf_lib.h>
|
||||
#include <clixon/clixon_proto_client.h>
|
||||
#include <clixon/clixon_plugin.h>
|
||||
#include <clixon/clixon_options.h>
|
||||
|
|
@ -90,7 +91,6 @@
|
|||
#include <clixon/clixon_xpath_ctx.h>
|
||||
#include <clixon/clixon_xpath.h>
|
||||
#include <clixon/clixon_json.h>
|
||||
#include <clixon/clixon_netconf_lib.h>
|
||||
#include <clixon/clixon_nacm.h>
|
||||
#include <clixon/clixon_xml_changelog.h>
|
||||
#include <clixon/clixon_xml_nsctx.h>
|
||||
|
|
|
|||
|
|
@ -38,6 +38,20 @@
|
|||
#ifndef _CLIXON_NETCONF_LIB_H
|
||||
#define _CLIXON_NETCONF_LIB_H
|
||||
|
||||
/*
|
||||
* Types
|
||||
*/
|
||||
/*! Content query parameter RFC 8040 Sec 4.8.1
|
||||
* Clixon extention: content so that RFC8040 content attribute can be conveyed
|
||||
* internally used in <get>
|
||||
*/
|
||||
enum netconf_content{
|
||||
CONTENT_CONFIG, /* config data only */
|
||||
CONTENT_NONCONFIG, /* state data only */
|
||||
CONTENT_ALL /* default */
|
||||
};
|
||||
typedef enum netconf_content netconf_content;
|
||||
|
||||
/*
|
||||
* Prototypes
|
||||
*/
|
||||
|
|
@ -77,5 +91,7 @@ int netconf_module_features(clicon_handle h);
|
|||
int netconf_module_load(clicon_handle h);
|
||||
char *netconf_db_find(cxobj *xn, char *name);
|
||||
int netconf_err2cb(cxobj *xerr, cbuf **cberr);
|
||||
const netconf_content netconf_content_str2int(char *str);
|
||||
const char *netconf_content_int2str(netconf_content nr);
|
||||
|
||||
#endif /* _CLIXON_NETCONF_LIB_H */
|
||||
|
|
|
|||
|
|
@ -52,8 +52,7 @@ int clicon_rpc_copy_config(clicon_handle h, char *db1, char *db2);
|
|||
int clicon_rpc_delete_config(clicon_handle h, char *db);
|
||||
int clicon_rpc_lock(clicon_handle h, char *db);
|
||||
int clicon_rpc_unlock(clicon_handle h, char *db);
|
||||
int clicon_rpc_get(clicon_handle h, char *xpath, char *namespace, cxobj **xret);
|
||||
int clicon_rpc_get_state(clicon_handle h, char *xpath, char *namespace, cxobj **xret);
|
||||
int clicon_rpc_get(clicon_handle h, char *xpath, char *namespace, netconf_content content, cxobj **xret);
|
||||
int clicon_rpc_close_session(clicon_handle h);
|
||||
int clicon_rpc_kill_session(clicon_handle h, int session_id);
|
||||
int clicon_rpc_validate(clicon_handle h, char *db);
|
||||
|
|
|
|||
|
|
@ -45,5 +45,6 @@ int xml_sort(cxobj *x0, void *arg);
|
|||
int xml_insert(cxobj *xp, cxobj *xc, enum insert_type ins, char *key_val);
|
||||
int xml_sort_verify(cxobj *x, void *arg);
|
||||
int match_base_child(cxobj *x0, cxobj *x1c, yang_stmt *yc, cxobj **x0cp);
|
||||
cxobj *xml_binsearch(cxobj *xp, char *name, char *keyname, char *keyval);
|
||||
|
||||
#endif /* _CLIXON_XML_SORT_H */
|
||||
|
|
|
|||
|
|
@ -216,7 +216,7 @@ clicon_conf_xml_set(clicon_handle h,
|
|||
|
||||
/*! Get authorized user name
|
||||
* @param[in] h Clicon handle
|
||||
* @retval xh XMLDB storage handle. If not connected return NULL
|
||||
* @retval username
|
||||
*/
|
||||
char *
|
||||
clicon_username_get(clicon_handle h)
|
||||
|
|
@ -228,7 +228,7 @@ clicon_username_get(clicon_handle h)
|
|||
|
||||
/*! Set authorized user name
|
||||
* @param[in] h Clicon handle
|
||||
* @param[in] xh XMLDB storage handle. If NULL reset it
|
||||
* @param[in] username
|
||||
* @note Just keep note of it, dont allocate it or so.
|
||||
*/
|
||||
int
|
||||
|
|
|
|||
|
|
@ -1375,3 +1375,25 @@ netconf_err2cb(cxobj *xerr,
|
|||
done:
|
||||
return retval;
|
||||
}
|
||||
|
||||
/* See RFC 8040 4.8.1
|
||||
* @see netconf_content_str2int
|
||||
*/
|
||||
static const map_str2int netconf_content_map[] = {
|
||||
{"config", CONTENT_CONFIG},
|
||||
{"nonconfig", CONTENT_NONCONFIG},
|
||||
{"all", CONTENT_ALL},
|
||||
{NULL, -1}
|
||||
};
|
||||
|
||||
const netconf_content
|
||||
netconf_content_str2int(char *str)
|
||||
{
|
||||
return clicon_str2int(netconf_content_map, str);
|
||||
}
|
||||
|
||||
const char *
|
||||
netconf_content_int2str(netconf_content nr)
|
||||
{
|
||||
return clicon_int2str(netconf_content_map, nr);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -541,6 +541,7 @@ clicon_rpc_unlock(clicon_handle h,
|
|||
* @param[in] h Clicon handle
|
||||
* @param[in] xpath XPath in a filter stmt (or NULL/"" for no filter)
|
||||
* @param[in] namespace Namespace associated w xpath
|
||||
* @param[in] content CLixon extension: all, config, noconfig. -1 means all
|
||||
* @param[out] xt XML tree. Free with xml_free.
|
||||
* Either <config> or <rpc-error>.
|
||||
* @retval 0 OK
|
||||
|
|
@ -549,7 +550,7 @@ clicon_rpc_unlock(clicon_handle h,
|
|||
* namespace will be used which is most probably wrong.
|
||||
* @code
|
||||
* cxobj *xt = NULL;
|
||||
* if (clicon_rpc_get(h, "/hello/world", "urn:example:hello", &xt) < 0)
|
||||
* if (clicon_rpc_get(h, "/hello/world", "urn:example:hello", CONTENT_ALL, &xt) < 0)
|
||||
* err;
|
||||
* if ((xerr = xpath_first(xt, "/rpc-error")) != NULL){
|
||||
* clicon_rpc_generate_error(xerr);
|
||||
|
|
@ -558,13 +559,15 @@ clicon_rpc_unlock(clicon_handle h,
|
|||
* if (xt)
|
||||
* xml_free(xt);
|
||||
* @endcode
|
||||
* @see clicon_rpc_get_config which is almost the same as with content=config, but you can also select dbname
|
||||
* @see clicon_rpc_generate_error
|
||||
*/
|
||||
int
|
||||
clicon_rpc_get(clicon_handle h,
|
||||
char *xpath,
|
||||
char *namespace,
|
||||
cxobj **xt)
|
||||
clicon_rpc_get(clicon_handle h,
|
||||
char *xpath,
|
||||
char *namespace,
|
||||
netconf_content content,
|
||||
cxobj **xt)
|
||||
{
|
||||
int retval = -1;
|
||||
struct clicon_msg *msg = NULL;
|
||||
|
|
@ -580,7 +583,13 @@ clicon_rpc_get(clicon_handle h,
|
|||
cprintf(cb, " username=\"%s\"", username);
|
||||
if (namespace)
|
||||
cprintf(cb, " xmlns:nc=\"%s\"", NETCONF_BASE_NAMESPACE);
|
||||
cprintf(cb, "><get>");
|
||||
cprintf(cb, "><get");
|
||||
#if 1
|
||||
/* Clixon extension, content all, config, nonconfig */
|
||||
if (content != -1)
|
||||
cprintf(cb, " content=\"%s\"", netconf_content_int2str(content));
|
||||
#endif
|
||||
cprintf(cb, ">");
|
||||
if (xpath && strlen(xpath)) {
|
||||
if (namespace)
|
||||
cprintf(cb, "<nc:filter nc:type=\"xpath\" nc:select=\"%s\" xmlns=\"%s\"/>",
|
||||
|
|
@ -615,85 +624,6 @@ clicon_rpc_get(clicon_handle h,
|
|||
return retval;
|
||||
}
|
||||
|
||||
/*! Get database state data, clixon extension
|
||||
* @param[in] h Clicon handle
|
||||
* @param[in] xpath XPath in a filter stmt (or NULL/"" for no filter)
|
||||
* @param[in] namespace Namespace associated w xpath
|
||||
* @param[out] xt XML tree. Free with xml_free.
|
||||
* Either <config> or <rpc-error>.
|
||||
* @retval 0 OK
|
||||
* @retval -1 Error, fatal or xml
|
||||
* @note if xpath is set but namespace is NULL, the default, netconf base
|
||||
* namespace will be used which is most probably wrong.
|
||||
* @code
|
||||
* cxobj *xt = NULL;
|
||||
* if (clicon_rpc_get_state(h, "/hello/world", "urn:example:hello", &xt) < 0)
|
||||
* err;
|
||||
* if ((xerr = xpath_first(xt, "/rpc-error")) != NULL){
|
||||
* clicon_rpc_generate_error(xerr);
|
||||
* err;
|
||||
* }
|
||||
* if (xt)
|
||||
* xml_free(xt);
|
||||
* @endcode
|
||||
* @see clicon_rpc_generate_error
|
||||
*/
|
||||
int
|
||||
clicon_rpc_get_state(clicon_handle h,
|
||||
char *xpath,
|
||||
char *namespace,
|
||||
cxobj **xt)
|
||||
{
|
||||
int retval = -1;
|
||||
struct clicon_msg *msg = NULL;
|
||||
cbuf *cb = NULL;
|
||||
cxobj *xret = NULL;
|
||||
cxobj *xd;
|
||||
char *username;
|
||||
|
||||
if ((cb = cbuf_new()) == NULL)
|
||||
goto done;
|
||||
cprintf(cb, "<rpc");
|
||||
if ((username = clicon_username_get(h)) != NULL)
|
||||
cprintf(cb, " username=\"%s\"", username);
|
||||
if (namespace)
|
||||
cprintf(cb, " xmlns:nc=\"%s\"", NETCONF_BASE_NAMESPACE);
|
||||
cprintf(cb, "><cl:get-state xmlns:cl=\"http://clicon.org/lib\">");
|
||||
if (xpath && strlen(xpath)) {
|
||||
if (namespace)
|
||||
cprintf(cb, "<nc:filter nc:type=\"xpath\" nc:select=\"%s\" xmlns=\"%s\"/>",
|
||||
xpath, namespace);
|
||||
else /* If xpath != /, this will probably yield an error later */
|
||||
cprintf(cb, "<filter type=\"xpath\" select=\"%s\"/>", xpath);
|
||||
}
|
||||
cprintf(cb, "</cl:get-state></rpc>");
|
||||
if ((msg = clicon_msg_encode("%s", cbuf_get(cb))) == NULL)
|
||||
goto done;
|
||||
if (clicon_rpc_msg(h, msg, &xret, NULL) < 0)
|
||||
goto done;
|
||||
/* Send xml error back: first check error, then ok */
|
||||
if ((xd = xpath_first(xret, "/rpc-reply/rpc-error")) != NULL)
|
||||
xd = xml_parent(xd); /* point to rpc-reply */
|
||||
else if ((xd = xpath_first(xret, "/rpc-reply/data")) == NULL)
|
||||
if ((xd = xml_new("data", NULL, NULL)) == NULL)
|
||||
goto done;
|
||||
if (xt){
|
||||
if (xml_rm(xd) < 0)
|
||||
goto done;
|
||||
*xt = xd;
|
||||
}
|
||||
retval = 0;
|
||||
done:
|
||||
if (cb)
|
||||
cbuf_free(cb);
|
||||
if (xret)
|
||||
xml_free(xret);
|
||||
if (msg)
|
||||
free(msg);
|
||||
return retval;
|
||||
}
|
||||
|
||||
|
||||
/*! Close a (user) session
|
||||
* @param[in] h CLICON handle
|
||||
* @retval 0 OK
|
||||
|
|
|
|||
|
|
@ -401,10 +401,9 @@ xml_search_userorder(cxobj *xp,
|
|||
|
||||
/*!
|
||||
* @param[in] xp Parent xml node.
|
||||
* @param[in] x1 Find this object among xp:s children
|
||||
* @param[in] userorder If x1 is ordered by user
|
||||
* @param[in] yangi Yang order
|
||||
* @param[in] keynr Length of keyvec/keyval vector when applicable
|
||||
* @param[in] keyvec Array of of yang key identifiers
|
||||
* @param[in] keyval Array of of yang key values
|
||||
* @param[in] low Lower bound of childvec search interval
|
||||
* @param[in] upper Lower bound of childvec search interval
|
||||
*/
|
||||
|
|
@ -447,10 +446,8 @@ xml_search1(cxobj *xp,
|
|||
|
||||
/*! Find XML child under xp matching x1 using binary search
|
||||
* @param[in] xp Parent xml node.
|
||||
* @param[in] yangi Yang child order
|
||||
* @param[in] keynr Length of keyvec/keyval vector when applicable
|
||||
* @param[in] keyvec Array of of yang key identifiers
|
||||
* @param[in] keyval Array of of yang key values
|
||||
* @param[in] x1 Find this object among xp:s children
|
||||
* @param[in] yc Yang spec of x1
|
||||
*/
|
||||
static cxobj *
|
||||
xml_search(cxobj *xp,
|
||||
|
|
@ -826,3 +823,37 @@ match_base_child(cxobj *x0,
|
|||
return retval;
|
||||
}
|
||||
|
||||
/*! Experimental API for binary search
|
||||
*/
|
||||
cxobj *
|
||||
xml_binsearch(cxobj *xp,
|
||||
char *name,
|
||||
char *keyname,
|
||||
char *keyval)
|
||||
{
|
||||
cxobj *xc = NULL;
|
||||
cxobj *xa = NULL;
|
||||
cxobj *xret = NULL;
|
||||
yang_stmt *yp;
|
||||
yang_stmt *yc;
|
||||
|
||||
if ((yp = xml_spec(xp)) == NULL){
|
||||
clicon_err(OE_YANG, ENOENT, "yang spec not found");
|
||||
goto done;
|
||||
}
|
||||
if ((yc = yang_find(yp, 0, name)) == NULL){
|
||||
clicon_err(OE_YANG, ENOENT, "yang not found");
|
||||
goto done;
|
||||
}
|
||||
if ((xc = xml_new(name, xp, yc)) == NULL)
|
||||
goto done;
|
||||
if ((xa = xml_new(keyname, xc, NULL)) == NULL)
|
||||
goto done;
|
||||
if (xml_value_set(xa, keyval) < 0)
|
||||
goto done;
|
||||
xret = xml_search(xp, xc, yc);
|
||||
done:
|
||||
if (xc)
|
||||
xml_free(xc);
|
||||
return xret;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue