Example: added -m/-M options for testing mount-points

This commit is contained in:
Olof hagsand 2023-08-07 18:06:29 +02:00
parent 27b77b14bc
commit bee30a4ea0
7 changed files with 288 additions and 50 deletions

View file

@ -36,6 +36,8 @@
* The example have the following optional arguments that you can pass as
* argc/argv after -- in clixon_backend:
* -a <..> Register callback for this yang action
* -m <yang> Mount this yang on mountpoint
* -M <namespace> Namespace of mountpoint, note both -m and -M must exist
* -n Notification streams example
* -r enable the reset function
* -s enable the state function
@ -68,7 +70,7 @@
#include <clixon/clixon_backend.h>
/* Command line options to be passed to getopt(3) */
#define BACKEND_EXAMPLE_OPTS "a:nrsS:x:iuUtV:"
#define BACKEND_EXAMPLE_OPTS "a:m:M:nrsS:x:iuUtV:"
/* Enabling this improves performance in tests, but there may trigger the "double XPath"
* problem.
@ -84,6 +86,14 @@
*/
static char *_action_instanceid = NULL;
/*! Yang schema mount
*
* Start backend with -- -m <yang> -M <namespace>
* Mount this yang on mountpoint
*/
static char *_mount_yang = NULL;
static char *_mount_namespace = NULL;
/*! Notification stream
*
* Enable notification streams for netconf/restconf
@ -936,18 +946,19 @@ main_yang_mount(clicon_handle h,
*config = 1;
if (vl)
*vl = VL_FULL;
if (yanglib){
if (yanglib && _mount_yang){
if ((cb = cbuf_new()) == NULL){
clicon_err(OE_UNIX, errno, "cbuf_new");
goto done;
}
cprintf(cb, "<yang-library xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">");
cprintf(cb, "<module-set>");
cprintf(cb, "<name>mount</name>");
cprintf(cb, "<name>mylabel</name>"); // XXX label in test_yang_schema_mount
cprintf(cb, "<module>");
cprintf(cb, "<name>clixon-example</name>");
cprintf(cb, "<revision>2022-11-01</revision>");
cprintf(cb, "<namespace>urn:example:urn</namespace>");
/* In yang name+namespace is mandatory, but not revision */
cprintf(cb, "<name>%s</name>", _mount_yang); // mandatory
cprintf(cb, "<namespace>%s</namespace>", _mount_namespace); // mandatory
// cprintf(cb, "<revision>2022-11-01</revision>");
cprintf(cb, "</module>");
cprintf(cb, "</module-set>");
cprintf(cb, "</yang-library>");
@ -1426,6 +1437,12 @@ clixon_plugin_init(clicon_handle h)
case 'a':
_action_instanceid = optarg;
break;
case 'm':
_mount_yang = optarg;
break;
case 'M':
_mount_namespace = optarg;
break;
case 'n':
_notification_stream = 1;
break;
@ -1457,7 +1474,10 @@ clixon_plugin_init(clicon_handle h)
_validate_fail_xpath = optarg;
break;
}
if ((_mount_yang && !_mount_namespace) || (!_mount_yang && _mount_namespace)){
clicon_err(OE_PLUGIN, EINVAL, "Both -m and -M must be given for mounts");
goto done;
}
if (_state_file){
api.ca_statedata = example_statefile; /* Switch state data callback */
if (_state_xpath){

View file

@ -33,6 +33,10 @@
***** END LICENSE BLOCK *****
*
* The example have the following optional arguments that you can pass as
* argc/argv after -- in clixon_cli:
* -m <yang> Mount this yang on mountpoint
* -M <namespace> Namespace of mountpoint, note both -m and -M must exist
*/
#include <stdio.h>
#include <stdlib.h>
@ -52,6 +56,14 @@
#include <clixon/clixon_cli.h>
#include <clixon/cli_generate.h>
/*! Yang schema mount
*
* Start backend with -- -m <yang> -M <namespace>
* Mount this yang on mountpoint
*/
static char *_mount_yang = NULL;
static char *_mount_namespace = NULL;
/*! Example cli function */
int
mycallback(clicon_handle h, cvec *cvv, cvec *argv)
@ -135,34 +147,6 @@ example_client_rpc(clicon_handle h,
return retval;
}
#ifndef CLIXON_STATIC_PLUGINS
static clixon_plugin_api api = {
"example", /* name */
clixon_plugin_init, /* init */
NULL, /* start */
NULL, /* exit */
.ca_prompt=NULL, /* cli_prompthook_t */
.ca_suspend=NULL, /* cligen_susp_cb_t */
.ca_interrupt=NULL, /* cligen_interrupt_cb_t */
};
/*! CLI plugin initialization
* @param[in] h Clixon handle
* @retval NULL Error with clicon_err set
* @retval api Pointer to API struct
*/
clixon_plugin_api *
clixon_plugin_init(clicon_handle h)
{
struct timeval tv;
gettimeofday(&tv, NULL);
srandom(tv.tv_usec);
return &api;
}
#endif /* CLIXON_STATIC_PLUGINS */
/*! Translate function from an original value to a new.
* In this case, assume string and increment characters, eg HAL->IBM
*/
@ -185,3 +169,111 @@ cli_incstr(cligen_handle h,
str[i]++;
return 0;
}
/*! Example YANG schema mount
*
* Given an XML mount-point xt, return XML yang-lib modules-set
* @param[in] h Clixon handle
* @param[in] xt XML mount-point in XML tree
* @param[out] config If '0' all data nodes in the mounted schema are read-only
* @param[out] validate Do or dont do full RFC 7950 validation
* @param[out] yanglib XML yang-lib module-set tree
* @retval 0 OK
* @retval -1 Error
* XXX hardcoded to clixon-example@2022-11-01.yang regardless of xt
* @see RFC 8528
*/
int
example_cli_yang_mount(clicon_handle h,
cxobj *xt,
int *config,
validate_level *vl,
cxobj **yanglib)
{
int retval = -1;
cbuf *cb = NULL;
if (config)
*config = 1;
if (vl)
*vl = VL_FULL;
if (yanglib && _mount_yang){
if ((cb = cbuf_new()) == NULL){
clicon_err(OE_UNIX, errno, "cbuf_new");
goto done;
}
cprintf(cb, "<yang-library xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">");
cprintf(cb, "<module-set>");
cprintf(cb, "<name>mount</name>");
cprintf(cb, "<module>");
/* In yang name+namespace is mandatory, but not revision */
cprintf(cb, "<name>%s</name>", _mount_yang); // mandatory
cprintf(cb, "<namespace>%s</namespace>", _mount_namespace); // mandatory
// cprintf(cb, "<revision>2022-11-01</revision>");
cprintf(cb, "</module>");
cprintf(cb, "</module-set>");
cprintf(cb, "</yang-library>");
if (clixon_xml_parse_string(cbuf_get(cb), YB_NONE, NULL, yanglib, NULL) < 0)
goto done;
if (xml_rootchild(*yanglib, 0, yanglib) < 0)
goto done;
}
retval = 0;
done:
if (cb)
cbuf_free(cb);
return retval;
}
#ifndef CLIXON_STATIC_PLUGINS
static clixon_plugin_api api = {
"example", /* name */
clixon_plugin_init, /* init */
NULL, /* start */
NULL, /* exit */
.ca_prompt=NULL, /* cli_prompthook_t */
.ca_suspend=NULL, /* cligen_susp_cb_t */
.ca_interrupt=NULL, /* cligen_interrupt_cb_t */
.ca_yang_mount=example_cli_yang_mount /* RFC 8528 schema mount */
};
/*! CLI plugin initialization
* @param[in] h Clixon handle
* @retval NULL Error with clicon_err set
* @retval api Pointer to API struct
*/
clixon_plugin_api *
clixon_plugin_init(clicon_handle h)
{
struct timeval tv;
int c;
int argc; /* command-line options (after --) */
char **argv;
gettimeofday(&tv, NULL);
srandom(tv.tv_usec);
/* Get user command-line options (after --) */
if (clicon_argv_get(h, &argc, &argv) < 0)
goto done;
opterr = 0;
optind = 1;
while ((c = getopt(argc, argv, "m:M:")) != -1)
switch (c) {
case 'm':
_mount_yang = optarg;
break;
case 'M':
_mount_namespace = optarg;
break;
}
if ((_mount_yang && !_mount_namespace) || (!_mount_yang && _mount_namespace)){
clicon_err(OE_PLUGIN, EINVAL, "Both -m and -M must be given for mounts");
goto done;
}
return &api;
done:
return NULL;
}
#endif /* CLIXON_STATIC_PLUGINS */