The Clixon API has been extended with namespaces, or namespace contexts in the following cases:

* CLIspec functions have added namespace parameter:
    * `cli_show_config <db> <format> <xpath>` --> `cli_show_config <db> <format> <xpath> <namespace>`
    * `cli_copy_config <db> <xpath> ...` --> `cli_copy_config <db> <xpath> <namespace> ...`
  * Xpath API
    * `xpath_first(x, format, ...)` --> `xpath_first(x, nsc, format, ...)`
    * `xpath_vec(x, format, vec, veclen, ...)` --> `xpath_vec(x, nsc, format, vec, veclen, ...)`
    * `xpath_vec_flag(x, format, flags, vec, veclen, ...)` --> `xpath_vec_flag(x, format, flags, vec, veclen, ...)`
    * `xpath_vec_bool(x, format, ...)` --> `xpath_vec_bool(x, nsc, format, ...)`
    * `xpath_vec_ctx(x, xpath, xp)` --> `xpath_vec_ctx(x, nsc, xpath, xp)`
  * xmldb_get0 has an added `nsc` parameter:
    * `xmldb_get0(h, db, xpath, copy, xret, msd)` --> `xmldb_get0(h, db, nsc, xpath, copy, xret, msd)`
  * The plugin statedata callback (ca_statedata) has been extended with an nsc parameter:
    * `int example_statedata(clicon_handle h, cvec *nsc, char *xpath, cxobj *xstate);`
  * rpc get and get-config api function has an added namespace argument:
    * `clicon_rpc_get_config(clicon_handle h, char *db, char *xpath, char *namespace, cxobj **xt);`
    * `int clicon_rpc_get(clicon_handle h, char *xpath, char *namespace, cxobj **xt);`
This commit is contained in:
Olof hagsand 2019-07-08 10:36:37 +02:00
parent 73d8e97a01
commit 67b8685bab
78 changed files with 1507 additions and 538 deletions

View file

@ -253,15 +253,16 @@ clicon_rpc_generate_error(char *prefix,
* @param[in] h CLICON handle
* @param[in] db Name of database
* @param[in] xpath XPath (or "")
* @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
* @code
* cxobj *xt = NULL;
* if (clicon_rpc_get_config(h, "running", "/", &xt) < 0)
* if (clicon_rpc_get_config(h, "running", "/hello/world", "urn:example:hello", &xt) < 0)
* err;
* if ((xerr = xpath_first(xt, "/rpc-error")) != NULL){
* if ((xerr = xpath_first(xt, NULL, "/rpc-error")) != NULL){
* clicon_rpc_generate_error("", xerr);
* err;
* }
@ -274,6 +275,7 @@ int
clicon_rpc_get_config(clicon_handle h,
char *db,
char *xpath,
char *namespace,
cxobj **xt)
{
int retval = -1;
@ -288,18 +290,25 @@ clicon_rpc_get_config(clicon_handle h,
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, "><get-config><source><%s/></source>", db);
if (xpath && strlen(xpath))
cprintf(cb, "<filter type=\"xpath\" select=\"%s\"/>", xpath);
if (xpath && strlen(xpath)){
if (namespace)
cprintf(cb, "<nc:filter nc:type=\"xpath\" nc:select=\"%s\" xmlns=\"%s\"/>",
xpath, namespace);
else /* XXX shouldnt happen */
cprintf(cb, "<filter type=\"xpath\" select=\"%s\"/>", xpath);
}
cprintf(cb, "</get-config></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)
if ((xd = xpath_first(xret, NULL, "/rpc-reply/rpc-error")) != NULL)
xd = xml_parent(xd); /* point to rpc-reply */
else if ((xd = xpath_first(xret, "/rpc-reply/data")) == NULL)
else if ((xd = xpath_first(xret, NULL, "/rpc-reply/data")) == NULL)
if ((xd = xml_new("data", NULL, NULL)) == NULL)
goto done;
if (xt){
@ -347,7 +356,7 @@ clicon_rpc_edit_config(clicon_handle h,
if ((cb = cbuf_new()) == NULL)
goto done;
cprintf(cb, "<rpc %s", DEFAULT_XMLNS);
cprintf(cb, "<rpc xmlns=\"%s\"", NETCONF_BASE_NAMESPACE);
if ((username = clicon_username_get(h)) != NULL)
cprintf(cb, " username=\"%s\"", username);
cprintf(cb, "><edit-config><target><%s/></target>", db);
@ -360,7 +369,7 @@ clicon_rpc_edit_config(clicon_handle h,
goto done;
if (clicon_rpc_msg(h, msg, &xret, NULL) < 0)
goto done;
if ((xerr = xpath_first(xret, "//rpc-error")) != NULL){
if ((xerr = xpath_first(xret, NULL, "//rpc-error")) != NULL){
clicon_rpc_generate_error("Editing configuration", xerr);
goto done;
}
@ -406,7 +415,7 @@ clicon_rpc_copy_config(clicon_handle h,
goto done;
if (clicon_rpc_msg(h, msg, &xret, NULL) < 0)
goto done;
if ((xerr = xpath_first(xret, "//rpc-error")) != NULL){
if ((xerr = xpath_first(xret, NULL, "//rpc-error")) != NULL){
clicon_rpc_generate_error("Copying configuration", xerr);
goto done;
}
@ -445,7 +454,7 @@ clicon_rpc_delete_config(clicon_handle h,
goto done;
if (clicon_rpc_msg(h, msg, &xret, NULL) < 0)
goto done;
if ((xerr = xpath_first(xret, "//rpc-error")) != NULL){
if ((xerr = xpath_first(xret, NULL, "//rpc-error")) != NULL){
clicon_rpc_generate_error("Deleting configuration", xerr);
goto done;
}
@ -480,7 +489,7 @@ clicon_rpc_lock(clicon_handle h,
goto done;
if (clicon_rpc_msg(h, msg, &xret, NULL) < 0)
goto done;
if ((xerr = xpath_first(xret, "//rpc-error")) != NULL){
if ((xerr = xpath_first(xret, NULL, "//rpc-error")) != NULL){
clicon_rpc_generate_error("Locking configuration", xerr);
goto done;
}
@ -514,7 +523,7 @@ clicon_rpc_unlock(clicon_handle h,
goto done;
if (clicon_rpc_msg(h, msg, &xret, NULL) < 0)
goto done;
if ((xerr = xpath_first(xret, "//rpc-error")) != NULL){
if ((xerr = xpath_first(xret, NULL, "//rpc-error")) != NULL){
clicon_rpc_generate_error("Configuration unlock", xerr);
goto done;
}
@ -528,17 +537,20 @@ clicon_rpc_unlock(clicon_handle h,
}
/*! Get database configuration and state data
* @param[in] h CLICON handle
* @param[in] xpath XPath (or "")
* @param[out] xt XML tree. Free with xml_free.
* Either <config> or <rpc-error>.
* @retval 0 OK
* @retval -1 Error, fatal or xml
* @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(h, "/", &xt) < 0)
* if (clicon_rpc_get(h, "/hello/world", "urn:example:hello", &xt) < 0)
* err;
* if ((xerr = xpath_first(xt, "/rpc-error")) != NULL){
* if ((xerr = xpath_first(xt, NULL, "/rpc-error")) != NULL){
* clicon_rpc_generate_error(xerr);
* err;
* }
@ -550,6 +562,7 @@ clicon_rpc_unlock(clicon_handle h,
int
clicon_rpc_get(clicon_handle h,
char *xpath,
char *namespace,
cxobj **xt)
{
int retval = -1;
@ -564,18 +577,25 @@ clicon_rpc_get(clicon_handle h,
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, "><get>");
if (xpath && strlen(xpath))
cprintf(cb, "<filter type=\"xpath\" select=\"%s\"/>", xpath);
if (xpath && strlen(xpath)) {
if (namespace)
cprintf(cb, "<nc:filter nc:type=\"xpath\" nc:select=\"%s\" xmlns=\"%s\"/>",
xpath, namespace);
else /* XXX shouldnt happen */
cprintf(cb, "<filter type=\"xpath\" select=\"%s\"/>", xpath);
}
cprintf(cb, "</get></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)
if ((xd = xpath_first(xret, NULL, "/rpc-reply/rpc-error")) != NULL)
xd = xml_parent(xd); /* point to rpc-reply */
else if ((xd = xpath_first(xret, "/rpc-reply/data")) == NULL)
else if ((xd = xpath_first(xret, NULL, "/rpc-reply/data")) == NULL)
if ((xd = xml_new("data", NULL, NULL)) == NULL)
goto done;
if (xt){
@ -614,7 +634,7 @@ clicon_rpc_close_session(clicon_handle h)
goto done;
if (clicon_rpc_msg(h, msg, &xret, NULL) < 0)
goto done;
if ((xerr = xpath_first(xret, "//rpc-error")) != NULL){
if ((xerr = xpath_first(xret, NULL, "//rpc-error")) != NULL){
clicon_rpc_generate_error("Close session", xerr);
goto done;
}
@ -649,7 +669,7 @@ clicon_rpc_kill_session(clicon_handle h,
goto done;
if (clicon_rpc_msg(h, msg, &xret, NULL) < 0)
goto done;
if ((xerr = xpath_first(xret, "//rpc-error")) != NULL){
if ((xerr = xpath_first(xret, NULL, "//rpc-error")) != NULL){
clicon_rpc_generate_error("Kill session", xerr);
goto done;
}
@ -683,7 +703,7 @@ clicon_rpc_validate(clicon_handle h,
goto done;
if (clicon_rpc_msg(h, msg, &xret, NULL) < 0)
goto done;
if ((xerr = xpath_first(xret, "//rpc-error")) != NULL){
if ((xerr = xpath_first(xret, NULL, "//rpc-error")) != NULL){
clicon_rpc_generate_error(CLIXON_ERRSTR_VALIDATE_FAILED, xerr);
goto done;
}
@ -715,7 +735,7 @@ clicon_rpc_commit(clicon_handle h)
goto done;
if (clicon_rpc_msg(h, msg, &xret, NULL) < 0)
goto done;
if ((xerr = xpath_first(xret, "//rpc-error")) != NULL){
if ((xerr = xpath_first(xret, NULL, "//rpc-error")) != NULL){
clicon_rpc_generate_error(CLIXON_ERRSTR_COMMIT_FAILED, xerr);
goto done;
}
@ -747,7 +767,7 @@ clicon_rpc_discard_changes(clicon_handle h)
goto done;
if (clicon_rpc_msg(h, msg, &xret, NULL) < 0)
goto done;
if ((xerr = xpath_first(xret, "//rpc-error")) != NULL){
if ((xerr = xpath_first(xret, NULL, "//rpc-error")) != NULL){
clicon_rpc_generate_error("Discard changes", xerr);
goto done;
}
@ -792,7 +812,7 @@ clicon_rpc_create_subscription(clicon_handle h,
goto done;
if (clicon_rpc_msg(h, msg, &xret, s0) < 0)
goto done;
if ((xerr = xpath_first(xret, "//rpc-error")) != NULL){
if ((xerr = xpath_first(xret, NULL, "//rpc-error")) != NULL){
clicon_rpc_generate_error("Create subscription", xerr);
goto done;
}
@ -827,11 +847,11 @@ clicon_rpc_debug(clicon_handle h,
goto done;
if (clicon_rpc_msg(h, msg, &xret, NULL) < 0)
goto done;
if ((xerr = xpath_first(xret, "//rpc-error")) != NULL){
if ((xerr = xpath_first(xret, NULL, "//rpc-error")) != NULL){
clicon_rpc_generate_error("Debug",xerr);
goto done;
}
if (xpath_first(xret, "//rpc-reply/ok") == NULL){
if (xpath_first(xret, NULL, "//rpc-reply/ok") == NULL){
clicon_err(OE_XML, 0, "rpc error"); /* XXX extract info from rpc-error */
goto done;
}