First working prototype

This commit is contained in:
Olof hagsand 2020-10-27 09:20:30 +01:00
parent 6b357dc038
commit 2e857bb417
16 changed files with 968 additions and 28 deletions

View file

@ -1526,6 +1526,10 @@ netconf_module_load(clicon_handle h)
if (clicon_option_bool(h, "CLICON_NETCONF_MESSAGE_ID_OPTIONAL") == 1)
xml_bind_netconf_message_id_optional(1);
#endif
/* Load restconf collection */
if (yang_spec_parse_module(h, "ietf-netconf-collection", NULL, yspec)< 0)
goto done;
retval = 0;
done:
return retval;

View file

@ -827,9 +827,10 @@ api_path2xpath_cvv(cvec *api_path,
* @retval 0 Invalid api_path or associated XML, netconf called
* @retval -1 Fatal error, clicon_err called
* @code
* char *xpath = NULL;
* cvec *nsc = NULL;
* if ((ret = api_path2xpath("/module:a/b", yspec, &xpath, &nsc)) < 0)
* char *xpath = NULL;
* cvec *nsc = NULL;
* cxobj *xerr = NULL;
* if ((ret = api_path2xpath("/module:a/b", yspec, &xpath, &nsc, &xerr)) < 0)
* err;
* if (ret == 1)
* ... access xpath as cbuf_get(xpath)
@ -896,6 +897,7 @@ api_path2xpath(char *api_path,
* @param[in] x0 Xpath tree so far
* @param[in] y0 Yang spec for x0
* @param[in] nodeclass Set to schema nodes, data nodes, etc
* @param[in] strict Break if api-path is not "complete" otherwise ignore and continue
* @param[out] xbotp Resulting xml tree
* @param[out] ybotp Yang spec matching xpathp
* @param[out] xerr Netconf error message (if retval=0)
@ -1019,10 +1021,6 @@ api_path2xml_vec(char **vec,
break;
case Y_LIST:
cvk = yang_cvec_get(y); /* Use Y_LIST cache, see ys_populate_list() */
if (valvec){ /* loop, valvec may have been used before */
free(valvec);
valvec = NULL;
}
if (restval==NULL){
if (strict){
cprintf(cberr, "malformed key =%s, expected '=restval'", nodeid);
@ -1111,7 +1109,7 @@ api_path2xml_vec(char **vec,
}
if ((retval = api_path2xml_vec(vec+1, nvec-1,
x, y,
nodeclass, strict,
nodeclass, strict,
xbotp, ybotp, xerr)) < 1)
goto done;
ok:
@ -1142,6 +1140,7 @@ api_path2xml_vec(char **vec,
* @param[in] yspec Yang spec
* @param[in,out] xtop Incoming XML tree
* @param[in] nodeclass Set to schema nodes, data nodes, etc
* @param[in] strict Break if api-path is not "complete" otherwise ignore and continue
* @param[out] xbotp Resulting xml tree (end of xpath)
* @param[out] ybotp Yang spec matching xbotp
* @param[out] xerr Netconf error message (if retval=0)
@ -1163,6 +1162,7 @@ api_path2xml_vec(char **vec,
* @endcode
* @note "api-path" is "URI-encoded path expression" definition in RFC8040 3.5.3
* @see api_path2xpath For api-path to xpath translation (maybe could be combined?)
* @note "Collections" should use strict = 0
*/
int
api_path2xml(char *api_path,

View file

@ -823,6 +823,7 @@ clicon_rpc_get(clicon_handle h,
if (depth != -1)
cprintf(cb, " depth=\"%d\"", depth);
cprintf(cb, ">");
/* If xpath, add a filter */
if (xpath && strlen(xpath)) {
cprintf(cb, "<%s:filter %s:type=\"xpath\" %s:select=\"%s\"",
NETCONF_BASE_PREFIX, NETCONF_BASE_PREFIX, NETCONF_BASE_PREFIX,
@ -877,7 +878,134 @@ clicon_rpc_get(clicon_handle h,
return retval;
}
/*! Get database configuration and state data collection
* @param[in] h Clicon handle
* @param[in] apipath To identify a list/leaf-list
* @param[in] yli Yang-stmt of list/leaf-list of collection
* @param[in] namespace Namespace associated w xpath
* @param[in] nsc Namespace context for filter
* @param[in] content Clixon extension: all, config, noconfig. -1 means all
* @param[in] depth Nr of XML levels to get, -1 is all, 0 is none
* @param[in] count Collection/clixon extension
* @param[in] skip Collection/clixon extension
* @param[in] direction Collection/clixon extension
* @param[in] sort Collection/clixon extension
* @param[in] where Collection/clixon extension
* @param[out] xt XML tree. Free with xml_free.
* Either <config> or <rpc-error>.
* @retval 0 OK
* @retval -1 Error, fatal or xml
* @see clicon_rpc_get
* @see draft-ietf-netconf-restconf-collection-00
* @note the netconf return message is yang populated, as well as the return data
*/
int
clicon_rpc_get_collection(clicon_handle h,
char *apipath,
yang_stmt *yli,
cvec *nsc, /* namespace context for filter */
netconf_content content,
char *depth,
char *count,
char *skip,
char *direction,
char *sort,
char *where,
cxobj **xt)
{
int retval = -1;
struct clicon_msg *msg = NULL;
cbuf *cb = NULL;
cxobj *xret = NULL;
cxobj *xerr = NULL;
cxobj *xr;
char *username;
uint32_t session_id;
int ret;
yang_stmt *yspec;
cxobj *x;
if (session_id_check(h, &session_id) < 0)
goto done;
if ((cb = cbuf_new()) == NULL)
goto done;
cprintf(cb, "<rpc xmlns=\"%s\" ", NETCONF_BASE_NAMESPACE);
if ((username = clicon_username_get(h)) != NULL)
cprintf(cb, " username=\"%s\"", username);
cprintf(cb, " xmlns:%s=\"%s\"",
NETCONF_BASE_PREFIX, NETCONF_BASE_NAMESPACE);
cprintf(cb, "><get-collection xmlns=\"%s\"", NETCONF_COLLECTION_NAMESPACE);
/* Clixon extension, content=all,config, or nonconfig */
if ((int)content != -1)
cprintf(cb, " content=\"%s\"", netconf_content_int2str(content));
if (depth)
cprintf(cb, " depth=\"%s\"", depth);
cprintf(cb, ">");
if (count)
cprintf(cb, "<list-target>%s</list-target>", apipath);
if (count)
cprintf(cb, "<count>%s</count>", count);
if (skip)
cprintf(cb, "<skip>%s</skip>", skip);
if (direction)
cprintf(cb, "<direction>%s</direction>", direction);
if (sort)
cprintf(cb, "<sort>%s</sort>", sort);
if (where)
cprintf(cb, "<where>%s</where>", where);
cprintf(cb, "</get-collection></rpc>");
if ((msg = clicon_msg_encode(session_id, "%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 ((xr = xpath_first(xret, NULL, "/rpc-reply/rpc-error")) != NULL)
xr = xml_parent(xr); /* point to rpc-reply */
else if ((xr = xpath_first(xret, NULL, "/rpc-reply/collection")) == NULL){
if ((xr = xml_new("collection", NULL, CX_ELMNT)) == NULL)
goto done;
}
else{
yspec = clicon_dbspec_yang(h);
/* Populate all children with yco */
x = NULL;
while ((x = xml_child_each(xr, x, CX_ELMNT)) != NULL){
xml_spec_set(x, yli);
if ((ret = xml_bind_yang(x, YB_PARENT, yspec, &xerr)) < 0)
goto done;
if (ret == 0){
if (clixon_netconf_internal_error(xerr,
". Internal error, backend returned invalid XML.",
NULL) < 0)
goto done;
if ((xr = xpath_first(xerr, NULL, "rpc-error")) == NULL){
clicon_err(OE_XML, ENOENT, "Expected rpc-error tag but none found(internal)");
goto done;
}
}
}
}
if (xr){
if (xml_rm(xr) < 0)
goto done;
*xt = xr;
}
retval = 0;
done:
if (cb)
cbuf_free(cb);
if (xerr)
xml_free(xerr);
if (xret)
xml_free(xret);
if (msg)
free(msg);
return retval;
}
/*! Send a close a netconf user session. Socket is also closed if still open
*
* @param[in] h CLICON handle
* @retval 0 OK
* @retval -1 Error and logged to syslog

View file

@ -743,4 +743,3 @@ xml_bind_yang_rpc_reply(cxobj *xrpc,
retval = 0;
goto done;
}