* RESTCONF "content" query parameter supported

* New clixon-lib@2019-08-13.yang revision
* Bugfix: If `ietf-netconf.yang` was imported from any yang module, client/backend communication stops working.
This commit is contained in:
Olof hagsand 2019-08-13 13:21:11 +02:00
parent 48022e57b9
commit 8b7b7b0f60
20 changed files with 507 additions and 62 deletions

View file

@ -942,6 +942,94 @@ from_client_get(clicon_handle h,
return retval;
}
/*! Retrieve device state information only
*
* This is a CLIXON specific RPC
* @param[in] h Clicon handle
* @param[in] xe Request: <rpc><xn></rpc>
* @param[out] cbret Return xml tree, eg <rpc-reply>..., <rpc-error..
* @param[in] arg client-entry
* @param[in] regarg User argument given at rpc_callback_register()
* @retval 0 OK
* @retval -1 Error
*
* @see from_client_get
*/
static int
from_client_get_state(clicon_handle h,
cxobj *xe,
cbuf *cbret,
void *arg,
void *regarg)
{
int retval = -1;
cxobj *xfilter;
char *xpath = "/";
cxobj *xret = NULL;
int ret;
cxobj **xvec = NULL;
size_t xlen;
cxobj *xnacm = NULL;
char *username;
cvec *nsc = NULL; /* Create a netconf namespace context from filter */
username = clicon_username_get(h);
if ((xfilter = xml_find(xe, "filter")) != NULL){
if ((xpath = xml_find_value(xfilter, "select"))==NULL)
xpath="/";
/* Create namespace context for xpath from <filter>
* The set of namespace declarations are those in scope on the
* <filter> element.
*/
else
if (xml_nsctx_node(xfilter, &nsc) < 0)
goto done;
}
/* Get state data from plugins as defined by plugin_statedata(), if any */
clicon_err_reset();
if ((ret = client_statedata(h, xpath, nsc, &xret)) < 0)
goto done;
if (ret == 0){ /* Error from callback (error in xret) */
if (clicon_xml2cbuf(cbret, xret, 0, 0) < 0)
goto done;
goto ok;
}
/* Pre-NACM access step */
if ((ret = nacm_access_pre(h, username, NACM_DATA, &xnacm)) < 0)
goto done;
if (ret == 0){ /* Do NACM validation */
if (xpath_vec_nsc(xret, nsc, "%s", &xvec, &xlen, xpath?xpath:"/") < 0)
goto done;
/* NACM datanode/module read validation */
if (nacm_datanode_read(xret, xvec, xlen, username, xnacm) < 0)
goto done;
}
cprintf(cbret, "<rpc-reply>"); /* OK */
if (xret==NULL)
cprintf(cbret, "<data/>");
else{
if (xml_name_set(xret, "data") < 0)
goto done;
if (clicon_xml2cbuf(cbret, xret, 0, 0) < 0)
goto done;
}
cprintf(cbret, "</rpc-reply>");
ok:
retval = 0;
done:
clicon_debug(1, "%s retval:%d", __FUNCTION__, retval);
if (xnacm)
xml_free(xnacm);
if (xvec)
free(xvec);
if (nsc)
xml_nsctx_free(nsc);
if (xret)
xml_free(xret);
return retval;
}
/*! Request graceful termination of a NETCONF session.
* @param[in] h Clicon handle
* @param[in] xe Request: <rpc><xn></rpc>
@ -1283,7 +1371,7 @@ from_client_msg(clicon_handle h,
goto reply; /* Dont quit here on user callbacks */
}
if (ret == 0){ /* not handled by callback */
if (netconf_operation_failed(cbret, "application", "Callback not recognized")< 0)
if (netconf_operation_not_supported(cbret, "application", "RPC operation not supported")< 0)
goto done;
goto reply;
}
@ -1424,6 +1512,9 @@ backend_rpc_init(clicon_handle h)
"urn:ietf:params:xml:ns:netmod:notification", "create-subscription") < 0)
goto done;
/* Clixon RPC */
if (rpc_callback_register(h, from_client_get_state, NULL,
"http://clicon.org/lib", "get-state") < 0)
goto done;
if (rpc_callback_register(h, from_client_debug, NULL,
"http://clicon.org/lib", "debug") < 0)
goto done;

View file

@ -570,6 +570,12 @@ main(int argc,
if (xmldb_connect(h) < 0)
goto done;
/* Add (hardcoded) netconf features in case ietf-netconf loaded here
* Otherwise it is loaded in netconf_module_load below
*/
if (netconf_module_features(h) < 0)
goto done;
/* Create top-level yang spec and store as option */
if ((yspec = yspec_new()) == NULL)
goto done;
@ -601,7 +607,8 @@ main(int argc,
/* Load yang module library, RFC7895 */
if (yang_modules_init(h) < 0)
goto done;
/* Add netconf yang spec, used by netconf client and as internal protocol */
/* Add netconf yang spec, used by netconf client and as internal protocol
*/
if (netconf_module_load(h) < 0)
goto done;
/* Load yang restconf module */

View file

@ -472,6 +472,12 @@ main(int argc, char **argv)
clixon_plugins_load(h, CLIXON_PLUGIN_INIT, dir, NULL) < 0)
goto done;
/* Add (hardcoded) netconf features in case ietf-netconf loaded here
* Otherwise it is loaded in netconf_module_load below
*/
if (netconf_module_features(h) < 0)
goto done;
/* Create top-level and store as option */
if ((yspec = yspec_new()) == NULL)
goto done;

View file

@ -484,6 +484,12 @@ main(int argc,
/* Access the remaining argv/argc options (after --) w clicon-argv_get() */
clicon_argv_set(h, argv0, argc, argv);
/* Add (hardcoded) netconf features in case ietf-netconf loaded here
* Otherwise it is loaded in netconf_module_load below
*/
if (netconf_module_features(h) < 0)
goto done;
/* Create top-level yang spec and store as option */
if ((yspec = yspec_new()) == NULL)
goto done;

View file

@ -143,6 +143,7 @@ static const map_str2int http_reason_phrase_map[] = {
};
/* See RFC 8040
* @see restconf_media_str2int
*/
static const map_str2int http_media_map[] = {
{"application/yang-data+xml", YANG_DATA_XML},
@ -152,6 +153,16 @@ static const map_str2int http_media_map[] = {
{NULL, -1}
};
/* See RFC 8040 4.8.1
* @see query_content_str2int
*/
static const map_str2int query_content_map[] = {
{"config", CONTENT_CONFIG},
{"nonconfig", CONTENT_NONCONFIG},
{"all", CONTENT_ALL},
{NULL, -1}
};
int
restconf_err2code(char *tag)
{
@ -176,6 +187,18 @@ restconf_media_int2str(restconf_media media)
return clicon_int2str(http_media_map, media);
}
const query_content
query_content_str2int(char *str)
{
return clicon_str2int(query_content_map, str);
}
const char *
query_content_int2str(query_content nr)
{
return clicon_int2str(query_content_map, nr);
}
/*! Return media_in from Content-Type, -1 if not found or unrecognized
* @note media-type syntax does not support parameters
* @see RFC7231 Sec 3.1.1.1 for media-type syntax type:

View file

@ -57,6 +57,16 @@ enum restconf_media{
};
typedef enum restconf_media restconf_media;
/*! Content query parameter RFC 8040 Sec 4.8.1
*/
enum query_content{
CONTENT_CONFIG,
CONTENT_NONCONFIG,
CONTENT_ALL /* default */
};
typedef enum query_content query_content;
/*
* Prototypes (also in clixon_restconf.h)
*/
@ -66,6 +76,8 @@ const char *restconf_code2reason(int code);
const restconf_media restconf_media_str2int(char *media);
const char *restconf_media_int2str(restconf_media media);
restconf_media restconf_content_type(FCGX_Request *r);
const query_content query_content_str2int(char *str);
const char *query_content_int2str(query_content nr);
int restconf_badrequest(FCGX_Request *r);
int restconf_unauthorized(FCGX_Request *r);
int restconf_forbidden(FCGX_Request *r);

View file

@ -695,6 +695,12 @@ main(int argc,
/* Access the remaining argv/argc options (after --) w clicon-argv_get() */
clicon_argv_set(h, argv0, argc, argv);
/* Add (hardcoded) netconf features in case ietf-netconf loaded here
* Otherwise it is loaded in netconf_module_load below
*/
if (netconf_module_features(h) < 0)
goto done;
/* Create top-level yang spec and store as option */
if ((yspec = yspec_new()) == NULL)
goto done;

View file

@ -114,12 +114,31 @@ api_data_get2(clicon_handle h,
int ret;
char *namespace = NULL;
cvec *nsc = NULL;
char *str;
query_content content = CONTENT_ALL;
clicon_debug(1, "%s", __FUNCTION__);
if ((yspec = clicon_dbspec_yang(h)) == NULL){
clicon_err(OE_FATAL, 0, "No DB_SPEC");
goto done;
}
/* Check for content attribute */
if ((str = cvec_find_str(qvec, "content")) != NULL){
clicon_debug(1, "%s content=%s", __FUNCTION__, str);
if ((content = query_content_str2int(str)) == -1){
if (netconf_bad_attribute_xml(&xerr, "application",
"<bad-attribute>content</bad-attribute>", "Unrecognized value of content attribute") < 0)
goto done;
if ((xe = xpath_first(xerr, "rpc-error")) == NULL){
clicon_err(OE_XML, EINVAL, "rpc-error not found (internal error)");
goto done;
}
if (api_return_err(h, r, xe, pretty, media_out, 0) < 0)
goto done;
goto ok;
}
}
if ((cbpath = cbuf_new()) == NULL)
goto done;
cprintf(cbpath, "/");
@ -144,7 +163,21 @@ api_data_get2(clicon_handle h,
* xpath expressions */
if ((nsc = xml_nsctx_init(NULL, namespace)) == NULL)
goto done;
if (clicon_rpc_get(h, xpath, namespace, &xret) < 0){
switch (content){
case CONTENT_CONFIG:
ret = clicon_rpc_get_config(h, "running", xpath, namespace, &xret);
break;
case CONTENT_NONCONFIG:
ret = clicon_rpc_get_state(h, xpath, namespace, &xret);
break;
case CONTENT_ALL:
ret = clicon_rpc_get(h, xpath, namespace, &xret);
break;
default:
clicon_err(OE_XML, EINVAL, "Invalid content attribute %d", content);
break;
}
if (ret < 0){
if (netconf_operation_failed_xml(&xerr, "protocol", clicon_err_reason) < 0)
goto done;
if ((xe = xpath_first(xerr, "rpc-error")) == NULL){