With-defaults CLI support

Extended cli_auto_show() with with-defaults argument, also changing its signature
Example: Added with-defaults argument to clispec
C-API: Added with-defaults argument to clicon_rpc_get_config
Replaced with-defaults prefix/namespace with constants
This commit is contained in:
Olof hagsand 2022-09-09 12:30:57 +02:00
parent 42d5b6fba2
commit 743076b171
23 changed files with 256 additions and 56 deletions

View file

@ -413,7 +413,6 @@ element2value(clicon_handle h,
* @param[in] x XML node
* @param[in] flag Flag to be used
* @retval 0 OK
*/
static int
xml_flag_default_value(cxobj *x,
@ -464,11 +463,11 @@ xml_add_default_tag(cxobj *x,
goto done;
if (xml_value_set(xattr, "true") < 0)
goto done;
if (xml_prefix_set(xattr, "wd") < 0)
if (xml_prefix_set(xattr, IETF_NETCONF_WITH_DEFAULTS_ATTR_PREFIX) < 0)
goto done;
}
retval = 0;
done:
done:
return retval;
}
@ -478,7 +477,6 @@ xml_add_default_tag(cxobj *x,
* @retval 0 OK
* @retval -1 Error
*/
static int
with_defaults(cxobj *xe,
cxobj *xret)
@ -506,7 +504,7 @@ with_defaults(cxobj *xe,
goto done;
goto ok;
}
if (strcmp(mode, "trim") == 0) {
else if (strcmp(mode, "trim") == 0) {
/* Remove default nodes from XML */
if (xml_tree_prune_flags(xret, XML_FLAG_DEFAULT, XML_FLAG_DEFAULT) < 0)
goto done;
@ -521,8 +519,8 @@ with_defaults(cxobj *xe,
goto done;
goto ok;
}
if (strcmp(mode, "report-all-tagged") == 0) {
if (xmlns_set(xret, "wd", "urn:ietf:params:xml:ns:netconf:default:1.0") < 0)
else if (strcmp(mode, "report-all-tagged") == 0) {
if (xmlns_set(xret, IETF_NETCONF_WITH_DEFAULTS_ATTR_PREFIX, IETF_NETCONF_WITH_DEFAULTS_ATTR_NAMESPACE) < 0)
goto done;
/* Mark nodes having default schema values */
if (xml_apply(xret, CX_ELMNT, (xml_applyfn_t*) xml_flag_default_value, (void*) XML_FLAG_MARK) < 0)
@ -532,7 +530,7 @@ with_defaults(cxobj *xe,
goto done;
goto ok;
}
if (strcmp(mode, "report-all") == 0) {
else if (strcmp(mode, "report-all") == 0) {
/* Accept mode, do nothing */
goto ok;
}

View file

@ -347,8 +347,14 @@ cli_auto_top(clicon_handle h,
* <format> "text"|"xml"|"json"|"cli"|"netconf" (see format_enum)
* <pretty> true|false: pretty-print or not
* <state> true|false: pretty-print or not
* <prefix> to print before cli syntax output
* <default> Retrieval default mode: report-all, trim, explicit, report-all-tagged,
* report-all-tagged-default, report-all-tagged-strip
* <prefix> CLI prefix: to print before cli syntax output
* @see cli_show_auto
* @note default mods accorsing to RFC6243 + two extra modes based on report-all-tagged:
* report-all-tagged-default Strip "default" attribute (=report-all xxx)
* report-all-tagged-strip Strip "default" attribute and all nodes tagged with it (=trim)
* XXX Merge with cli_show_auto
*/
int
cli_auto_show(clicon_handle h,
@ -376,9 +382,11 @@ cli_auto_show(clicon_handle h,
char *prefix = NULL;
int state;
cg_var *boolcv = NULL;
char *defaultstr = NULL; /* with extended tagged modes */
char *withdefaultstr = NULL; /* RFC 6243 modes */
if (cvec_len(argv) != 5 && cvec_len(argv) != 6){
clicon_err(OE_PLUGIN, EINVAL, "Usage: <treename> <database> <format> <pretty> <state> [<prefix>].");
if (cvec_len(argv) < 5 || cvec_len(argv) > 7){
clicon_err(OE_PLUGIN, EINVAL, "Usage: <treename> <database> <format> <pretty> <state> [<default> <cli-prefix>].");
goto done;
}
/* First argv argument: treename */
@ -407,10 +415,27 @@ cli_auto_show(clicon_handle h,
goto done;
}
state = cv_bool_get(boolcv);
/* Sixth: prefix */
if (cvec_len(argv) == 6) {
prefix = cv_string_get(cvec_i(argv, 5));
/* Sixth: default */
if (cvec_len(argv) > 5) {
defaultstr = cv_string_get(cvec_i(argv, 5));
/* From extended to RFC6243 withdefault modes */
if (strcmp(defaultstr, "report-all-tagged-strip") == 0)
withdefaultstr = "report-all-tagged";
else if (strcmp(defaultstr, "report-all-tagged-default") == 0)
withdefaultstr = "report-all-tagged";
else if (strcmp(defaultstr, "report-all") != 0 &&
strcmp(defaultstr, "trim") != 0 &&
strcmp(defaultstr, "explicit") != 0 &&
strcmp(defaultstr, "report-all-tagged") != 0){
clicon_err(OE_YANG, EINVAL, "Unexpected with-default option: %s", defaultstr);
goto done;
}
else
withdefaultstr = defaultstr;
}
/* Seventh: cli prefix */
if (cvec_len(argv) > 6) {
prefix = cv_string_get(cvec_i(argv, 6));
}
if ((yspec = clicon_dbspec_yang(h)) == NULL){
clicon_err(OE_FATAL, 0, "No DB_SPEC");
@ -428,22 +453,31 @@ cli_auto_show(clicon_handle h,
if (api_path2xpath(api_path, yspec, &xpath, &nsc, NULL) < 0)
goto done;
skiproot = (xpath != NULL) && (strcmp(xpath,"/") != 0);
if (state == 0){ /* Get configuration-only from database */
if (clicon_rpc_get_config(h, NULL, db, xpath, nsc, &xt) < 0)
if (state && strcmp(db, "running") != 0){
clicon_err(OE_FATAL, 0, "Show state only for running database, not %s", db);
goto done;
}
if (state == 0){ /* Get configuration-only from a database */
if (clicon_rpc_get_config(h, NULL, db, xpath, nsc, withdefaultstr, &xt) < 0)
goto done;
}
else { /* Get configuration and state from database */
if (strcmp(db, "running") != 0){
clicon_err(OE_FATAL, 0, "Show state only for running database, not %s", db);
goto done;
}
if (clicon_rpc_get(h, xpath, nsc, CONTENT_ALL, -1, NULL, &xt) < 0)
else { /* Get configuration and state from running */
if (clicon_rpc_get(h, xpath, nsc, CONTENT_ALL, -1, withdefaultstr, &xt) < 0)
goto done;
}
if ((xerr = xpath_first(xt, NULL, "/rpc-error")) != NULL){
clixon_netconf_error(xerr, "Get configuration", NULL);
goto done;
}
/* Special tagged modes: strip wd:default=true attribute and (optionally) nodes associated with it */
if (defaultstr &&
(strcmp(defaultstr, "report-all-tagged-strip") == 0 ||
strcmp(defaultstr, "report-all-tagged-default") == 0)){
if (purge_tagged_nodes(xt, IETF_NETCONF_WITH_DEFAULTS_ATTR_NAMESPACE, "default", "true",
strcmp(defaultstr, "report-all-tagged-strip")
) < 0)
goto done;
}
if (xpath_vec(xt, nsc, "%s", &vec, &veclen, xpath) < 0)
goto done;

View file

@ -772,13 +772,13 @@ compare_dbs(clicon_handle h,
format = FORMAT_TEXT;
else
format = FORMAT_XML;
if (clicon_rpc_get_config(h, NULL, "running", "/", NULL, &xc1) < 0)
if (clicon_rpc_get_config(h, NULL, "running", "/", NULL, NULL, &xc1) < 0)
goto done;
if ((xerr = xpath_first(xc1, NULL, "/rpc-error")) != NULL){
clixon_netconf_error(xerr, "Get configuration", NULL);
goto done;
}
if (clicon_rpc_get_config(h, NULL, "candidate", "/", NULL, &xc2) < 0)
if (clicon_rpc_get_config(h, NULL, "candidate", "/", NULL, NULL, &xc2) < 0)
goto done;
if ((xerr = xpath_first(xc2, NULL, "/rpc-error")) != NULL){
clixon_netconf_error(xerr, "Get configuration", NULL);
@ -1028,7 +1028,7 @@ save_config_file(clicon_handle h,
goto done;
}
filename = cv_string_get(cv);
if (clicon_rpc_get_config(h, NULL, dbstr,"/", NULL, &xt) < 0)
if (clicon_rpc_get_config(h, NULL, dbstr,"/", NULL, NULL, &xt) < 0)
goto done;
if (xt == NULL){
clicon_err(OE_CFG, 0, "get config: empty tree"); /* Shouldnt happen */
@ -1396,7 +1396,7 @@ cli_copy_config(clicon_handle h,
if ((nsc = xml_nsctx_init(NULL, namespace)) == NULL)
goto done;
/* Get from object configuration and store in x1 */
if (clicon_rpc_get_config(h, NULL, db, cbuf_get(cb), nsc, &x1) < 0)
if (clicon_rpc_get_config(h, NULL, db, cbuf_get(cb), nsc, NULL, &x1) < 0)
goto done;
if ((xerr = xpath_first(x1, NULL, "/rpc-error")) != NULL){
clixon_netconf_error(xerr, "Get configuration", NULL);

View file

@ -315,7 +315,7 @@ expand_dbvar(void *h,
goto done;
}
/* Get configuration based on cbxpath */
if (clicon_rpc_get_config(h, NULL, dbstr, cbuf_get(cbxpath), nsc, &xt) < 0)
if (clicon_rpc_get_config(h, NULL, dbstr, cbuf_get(cbxpath), nsc, NULL, &xt) < 0)
goto done;
if ((xe = xpath_first(xt, NULL, "/rpc-error")) != NULL){
clixon_netconf_error(xe, "Get configuration", NULL);
@ -476,7 +476,7 @@ cli_show_config1(clicon_handle h,
prefix = cv_string_get(cvec_i(argv, 4));
}
if (state == 0){ /* Get configuration-only from database */
if (clicon_rpc_get_config(h, NULL, db, cbuf_get(cbxpath), nsc, &xt) < 0)
if (clicon_rpc_get_config(h, NULL, db, cbuf_get(cbxpath), nsc, NULL, &xt) < 0)
goto done;
}
else { /* Get configuration and state from database */
@ -637,7 +637,7 @@ show_conf_xpath(clicon_handle h,
if (clicon_rpc_get(h, xpath, nsc, CONTENT_ALL, -1, &xt) < 0)
goto done;
#else
if (clicon_rpc_get_config(h, NULL, dbname, xpath, nsc, &xt) < 0)
if (clicon_rpc_get_config(h, NULL, dbname, xpath, nsc, NULL, &xt) < 0)
goto done;
#endif
if ((xerr = xpath_first(xt, NULL, "/rpc-error")) != NULL){
@ -741,7 +741,7 @@ cli_show_auto1(clicon_handle h,
xpath[strlen(xpath)-1] = '\0';
if (state == 0){ /* Get configuration-only from database */
if (clicon_rpc_get_config(h, NULL, db, xpath, nsc, &xt) < 0)
if (clicon_rpc_get_config(h, NULL, db, xpath, nsc, NULL, &xt) < 0)
goto done;
}
else{ /* Get configuration and state from database */

View file

@ -181,7 +181,7 @@ restconf_main_config(clicon_handle h,
clicon_err(OE_UNIX, errno, "getpwuid");
goto done;
}
if (clicon_rpc_get_config(h, pw->pw_name, "running", "/restconf", nsc, &xconfig) < 0)
if (clicon_rpc_get_config(h, pw->pw_name, "running", "/restconf", nsc, NULL, &xconfig) < 0)
goto done;
if ((xerr = xpath_first(xconfig, NULL, "/rpc-error")) != NULL){
clixon_netconf_error(xerr, "Get backend restconf config", NULL);

View file

@ -670,7 +670,7 @@ restconf_clixon_backend(clicon_handle h,
goto done;
}
/* XXX xconfig leaked */
if (clicon_rpc_get_config(h, pw->pw_name, "running", "/restconf", nsc, &xconfig) < 0)
if (clicon_rpc_get_config(h, pw->pw_name, "running", "/restconf", nsc, NULL, &xconfig) < 0)
goto done;
if ((xerr = xpath_first(xconfig, NULL, "/rpc-error")) != NULL){
clixon_netconf_error(xerr, "Get backend restconf config", NULL);