Autocli optimization feature for generating smaller CLISPECs for large YANGs using treerefs
New `grouping-treeref` option added to clixon-autocli.yang Default is disabled, set to true to generate smaller memory footprint of clixon_cl Add prefix "mtpoint:" to cli api-paths to identify change of yang schemas
This commit is contained in:
parent
d48442960e
commit
f526d5b7a0
14 changed files with 668 additions and 64 deletions
|
|
@ -135,8 +135,8 @@ cli_auto_edit(clicon_handle h,
|
|||
}
|
||||
api_path_fmt = cv_string_get(cvec_i(argv, argc++));
|
||||
str = cv_string_get(cvec_i(argv, argc++));
|
||||
if (str && str[0] == '/'){ /* ad-hoc to see if 2nd arg is mountpoint */
|
||||
mtpoint = str;
|
||||
if (str && strncmp(str, "mtpoint:", strlen("mtpoint:")) == 0){
|
||||
mtpoint = str + strlen("mtpoint:");
|
||||
clicon_debug(1, "%s mtpoint:%s", __FUNCTION__, mtpoint);
|
||||
treename = cv_string_get(cvec_i(argv, argc++));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -384,6 +384,49 @@ autocli_completion(clicon_handle h,
|
|||
return retval;
|
||||
}
|
||||
|
||||
/*! Return autocli grouping treeref option
|
||||
*
|
||||
* When false replaces uses with grouping, when true use tree reference
|
||||
* @param[in] h Clixon handle
|
||||
* @param[out] treeref grouping using treerefs enabled
|
||||
* @retval -1 Error
|
||||
* @retval 0 OK
|
||||
*/
|
||||
int
|
||||
autocli_grouping_treeref(clicon_handle h,
|
||||
int *treeref)
|
||||
{
|
||||
int retval = -1;
|
||||
char *str;
|
||||
uint8_t val;
|
||||
char *reason = NULL;
|
||||
int ret;
|
||||
cxobj *xautocli;
|
||||
|
||||
if (treeref == NULL){
|
||||
clicon_err(OE_YANG, EINVAL, "Argument is NULL");
|
||||
goto done;
|
||||
}
|
||||
if ((xautocli = clicon_conf_autocli(h)) == NULL){
|
||||
clicon_err(OE_YANG, 0, "No clixon-autocli");
|
||||
goto done;
|
||||
}
|
||||
if ((str = xml_find_body(xautocli, "grouping-treeref")) == NULL){
|
||||
clicon_err(OE_XML, EINVAL, "No grouping-treeref rule");
|
||||
goto done;
|
||||
}
|
||||
if ((ret = parse_bool(str, &val, &reason)) < 0){
|
||||
clicon_err(OE_CFG, errno, "parse_bool");
|
||||
goto done;
|
||||
}
|
||||
*treeref = val;
|
||||
retval = 0;
|
||||
done:
|
||||
if (reason)
|
||||
free(reason);
|
||||
return retval;
|
||||
}
|
||||
|
||||
/*! Return default autocli list keyword setting
|
||||
*
|
||||
* Currently only returns list-keyword-default, could be extended to rules
|
||||
|
|
|
|||
|
|
@ -52,6 +52,7 @@ enum autocli_op{
|
|||
*/
|
||||
int autocli_module(clicon_handle h, char *modname, int *enable);
|
||||
int autocli_completion(clicon_handle h, int *completion);
|
||||
int autocli_grouping_treeref(clicon_handle h, int *grouping_treeref);
|
||||
int autocli_list_keyword(clicon_handle h, autocli_listkw_t *listkw);
|
||||
int autocli_compress(clicon_handle h, yang_stmt *ys, int *compress);
|
||||
int autocli_treeref_state(clicon_handle h, int *treeref_state);
|
||||
|
|
|
|||
|
|
@ -348,7 +348,10 @@ mtpoint_paths(yang_stmt *yspec0,
|
|||
*
|
||||
* @param[in] h Clicon handle
|
||||
* @param[in] cvv Vector of cli string and instantiated variables
|
||||
* @param[in] argv Vector: <apipathfmt> [<mointpt>], eg "/aaa/%s"
|
||||
* @param[in] argv Arguments given at the callback:
|
||||
* <api_path_fmt> Generated API PATH (this is added implicitly, not actually given in the cvv)
|
||||
* <api_path_fmt>* Added by merge in treeref_merge_co
|
||||
* [<mt-point>] Optional YANG path-arg/xpath from mount-point
|
||||
* @param[in] op Operation to perform on database
|
||||
* @param[in] nsctx Namespace context for last value added
|
||||
* cvv first contains the complete cli string, and then a set of optional
|
||||
|
|
@ -373,10 +376,10 @@ cli_dbxml(clicon_handle h,
|
|||
cvec *nsctx)
|
||||
{
|
||||
int retval = -1;
|
||||
cbuf *api_path_fmt_cb = NULL; /* xml key format */
|
||||
char *api_path_fmt; /* xml key format */
|
||||
char *api_path_fmt01 = NULL;
|
||||
char *api_path = NULL;
|
||||
cg_var *arg;
|
||||
cbuf *cb = NULL;
|
||||
cxobj *xbot = NULL; /* xpath, NULL if datastore */
|
||||
yang_stmt *y = NULL; /* yang spec of xpath */
|
||||
|
|
@ -384,24 +387,46 @@ cli_dbxml(clicon_handle h,
|
|||
cxobj *xerr = NULL;
|
||||
int ret;
|
||||
cg_var *cv;
|
||||
char *str;
|
||||
int cvvi = 0;
|
||||
char *mtpoint = NULL;
|
||||
yang_stmt *yspec0 = NULL;
|
||||
int argc = 0;
|
||||
int i;
|
||||
|
||||
if (cvec_len(argv) != 1 && cvec_len(argv) != 2){
|
||||
clicon_err(OE_PLUGIN, EINVAL, "Requires one element to be xml key format string");
|
||||
goto done;
|
||||
}
|
||||
/* Top-level yspec */
|
||||
if ((yspec0 = clicon_dbspec_yang(h)) == NULL){
|
||||
clicon_err(OE_FATAL, 0, "No DB_SPEC");
|
||||
goto done;
|
||||
}
|
||||
arg = cvec_i(argv, 0);
|
||||
api_path_fmt = cv_string_get(arg);
|
||||
if (cvec_len(argv) > 1){
|
||||
arg = cvec_i(argv, 1);
|
||||
mtpoint = cv_string_get(arg);
|
||||
if ((api_path_fmt_cb = cbuf_new()) == NULL){
|
||||
clicon_err(OE_UNIX, errno, "cbuf_new");
|
||||
goto done;
|
||||
}
|
||||
/* Iterate through all api_path_fmt:s, assume they start with / */
|
||||
for (argc=0; argc<cvec_len(argv); argc++){
|
||||
cv = cvec_i(argv, argc);
|
||||
str = cv_string_get(cv);
|
||||
if (str[0] != '/')
|
||||
break;
|
||||
}
|
||||
if (argc == 0){
|
||||
clicon_err(OE_PLUGIN, EINVAL, "No <api_path_fmt> in argv");
|
||||
goto done;
|
||||
}
|
||||
/* Append a api_path_fmt from sub-parts */
|
||||
for (i=argc-1; i>=0; i--){
|
||||
cprintf(api_path_fmt_cb, "%s", cv_string_get(cvec_i(argv, i)));
|
||||
}
|
||||
api_path_fmt = cbuf_get(api_path_fmt_cb);
|
||||
if (cvec_len(argv) > argc){
|
||||
cv = cvec_i(argv, argc++);
|
||||
str = cv_string_get(cv);
|
||||
if (strncmp(str, "mtpoint:", strlen("mtpoint:")) != 0){
|
||||
clicon_err(OE_PLUGIN, 0, "mtpoint does not begin with 'mtpoint:'");
|
||||
goto done;
|
||||
}
|
||||
mtpoint = str + strlen("mtpoint:");
|
||||
}
|
||||
/* Remove all keywords */
|
||||
if (cvec_exclude_keys(cvv) < 0)
|
||||
|
|
@ -483,6 +508,8 @@ cli_dbxml(clicon_handle h,
|
|||
goto done;
|
||||
retval = 0;
|
||||
done:
|
||||
if (api_path_fmt_cb)
|
||||
cbuf_free(api_path_fmt_cb);
|
||||
if (api_path_fmt01)
|
||||
free(api_path_fmt01);
|
||||
if (xerr)
|
||||
|
|
|
|||
|
|
@ -103,12 +103,6 @@ You can see which CLISPEC it generates via clixon_cli -D 2:
|
|||
#include "cli_autocli.h"
|
||||
#include "cli_generate.h"
|
||||
|
||||
/*
|
||||
* Constants
|
||||
*/
|
||||
/* variable expand function */
|
||||
#define GENERATE_EXPAND_XMLDB "expand_dbvar"
|
||||
|
||||
/*! Create cligen variable expand entry with api-pathfmt format string as argument
|
||||
*
|
||||
* Add api-fmt as first argument to callback, eg:
|
||||
|
|
@ -160,8 +154,9 @@ cli_expand_var_generate(clicon_handle h,
|
|||
cprintf(cb, " %s(\"candidate\",\"%s\"",
|
||||
GENERATE_EXPAND_XMLDB,
|
||||
api_path_fmt);
|
||||
if (cv) /* Add optional mountpoint */
|
||||
cprintf(cb, ",\"%s\"", cv_string_get(cv));
|
||||
if (cv){ /* Add optional mountpoint */
|
||||
cprintf(cb, ",\"%s%s\"", MTPOINT_PREFIX, cv_string_get(cv));
|
||||
}
|
||||
cprintf(cb, ")>");
|
||||
retval = 0;
|
||||
done:
|
||||
|
|
@ -192,8 +187,9 @@ cli_callback_generate(clicon_handle h,
|
|||
if (yang2api_path_fmt(ys, 0, &api_path_fmt) < 0)
|
||||
goto done;
|
||||
cprintf(cb, ",%s(\"%s\"", GENERATE_CALLBACK, api_path_fmt);
|
||||
if (cv) /* Add optional mountpoint */
|
||||
cprintf(cb, ",\"%s\"", cv_string_get(cv));
|
||||
if (cv){ /* Add optional mountpoint */
|
||||
cprintf(cb, ",\"%s%s\"", MTPOINT_PREFIX, cv_string_get(cv));
|
||||
}
|
||||
cprintf(cb, ")");
|
||||
retval = 0;
|
||||
done:
|
||||
|
|
@ -265,7 +261,7 @@ yang2cli_var_identityref(yang_stmt *ys,
|
|||
if ((ymod = yang_find_module_by_name(yspec, prefix)) != NULL &&
|
||||
(yprefix = yang_find(ymod, Y_PREFIX, NULL)) != NULL){
|
||||
if (i++)
|
||||
cprintf(cb, "|");
|
||||
cprintf(cb, "|");
|
||||
cprintf(cb, "%s:%s", yang_argument_get(yprefix), id);
|
||||
}
|
||||
if (prefix){
|
||||
|
|
@ -461,21 +457,21 @@ yang2cli_var_sub(clicon_handle h,
|
|||
/* enumeration special case completion */
|
||||
if (type){
|
||||
if (strcmp(type, "enumeration") == 0 || strcmp(type, "bits") == 0){
|
||||
cprintf(cb, " choice:");
|
||||
cprintf(cb, " choice:");
|
||||
i = 0;
|
||||
yi = NULL;
|
||||
while ((yi = yn_each(ytype, yi)) != NULL){
|
||||
if (yang_keyword_get(yi) != Y_ENUM && yang_keyword_get(yi) != Y_BIT)
|
||||
continue;
|
||||
if (i)
|
||||
cprintf(cb, "|");
|
||||
cprintf(cb, "|");
|
||||
/* Encode by escaping delimiters */
|
||||
arg = yang_argument_get(yi);
|
||||
len = strlen(arg);
|
||||
for (j=0; j<len; j++){
|
||||
if (index(CLIGEN_DELIMITERS, arg[j]))
|
||||
cprintf(cb, "\\");
|
||||
cprintf(cb, "%c", arg[j]);
|
||||
cprintf(cb, "%c", arg[j]);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
|
@ -524,7 +520,7 @@ yang2cli_var_union_one(clicon_handle h,
|
|||
{
|
||||
int retval = -1;
|
||||
int options = 0;
|
||||
cvec *cvv = NULL;
|
||||
cvec *cvv = NULL;
|
||||
cvec *patterns = NULL;
|
||||
uint8_t fraction_digits = 0;
|
||||
enum cv_type cvtype;
|
||||
|
|
@ -688,7 +684,7 @@ yang2cli_var(clicon_handle h,
|
|||
char *origtype = NULL;
|
||||
yang_stmt *yrestype; /* resolved type */
|
||||
char *restype; /* resolved type */
|
||||
cvec *cvv = NULL;
|
||||
cvec *cvv = NULL;
|
||||
cvec *patterns = NULL;
|
||||
uint8_t fraction_digits = 0;
|
||||
enum cv_type cvtype;
|
||||
|
|
@ -1018,10 +1014,10 @@ yang2cli_list(clicon_handle h,
|
|||
if (cli_callback_generate(h, ys, cb) < 0)
|
||||
goto done;
|
||||
if (keynr == 0)
|
||||
cprintf(cb, ",act-list");
|
||||
cprintf(cb, ",act-list");
|
||||
else
|
||||
cprintf(cb, ",act-prekey");
|
||||
cprintf(cb, ";\n");
|
||||
cprintf(cb, ",act-prekey");
|
||||
cprintf(cb, ";\n");
|
||||
cprintf(cb, "{\n");
|
||||
if (yang2cli_leaf(h, yleaf,
|
||||
level+1,
|
||||
|
|
@ -1105,11 +1101,97 @@ yang2cli_choice(clicon_handle h,
|
|||
return retval;
|
||||
}
|
||||
|
||||
/*! Generate clispec for all modules in a grouping
|
||||
*
|
||||
* Called in cli main function for top-level yangs. But may also be called dynamically for
|
||||
* mountpoints.
|
||||
* @param[in] h Clixon handle
|
||||
* @param[in] ys Top-level Yang statement
|
||||
* @param[in] treename Name of tree
|
||||
* @retval 0 OK
|
||||
* @retval -1 Error
|
||||
* @note Tie-break of same top-level symbol: prefix is NYI
|
||||
* @see yang2cli_yspec for original
|
||||
*/
|
||||
static int yang2cli_grouping(clicon_handle h, yang_stmt *ys, char *treename);
|
||||
|
||||
/*! Generate CLI code for Yang uses statement
|
||||
*
|
||||
* @param[in] h Clixon handle
|
||||
* @param[in] ys Yang statement
|
||||
* @param[in] level Indentation level
|
||||
* @param[out] cb Buffer where cligen code is written
|
||||
* @retval 0 OK
|
||||
* @retval -1 Error
|
||||
*/
|
||||
static int
|
||||
yang2cli_uses(clicon_handle h,
|
||||
yang_stmt *ys,
|
||||
int level,
|
||||
cbuf *cb)
|
||||
{
|
||||
int retval = -1;
|
||||
char *id = NULL;
|
||||
char *prefix = NULL;
|
||||
char *ns;
|
||||
yang_stmt *ygrouping;
|
||||
cbuf *cbtree = NULL;
|
||||
char *api_path_fmt = NULL;
|
||||
yang_stmt *yp;
|
||||
int ret;
|
||||
|
||||
if (nodeid_split(yang_argument_get(ys), &prefix, &id) < 0)
|
||||
goto done;
|
||||
if (ys_grouping_resolve(ys, prefix, id, &ygrouping) < 0)
|
||||
goto done;
|
||||
if (ygrouping == NULL){
|
||||
fprintf(stderr, "%s grouping %s not found\n", __FUNCTION__, id);
|
||||
goto ok;
|
||||
}
|
||||
if ((cbtree = cbuf_new()) == NULL){
|
||||
clicon_err(OE_XML, errno, "cbuf_new");
|
||||
goto done;
|
||||
}
|
||||
/* prefix is not globally unique, need namespace */
|
||||
if ((ns = yang_find_mynamespace(ygrouping)) == NULL)
|
||||
goto done;
|
||||
cprintf(cbtree, "grouping-%s-%s", ns, id);
|
||||
if (cligen_ph_find(cli_cligen(h), cbuf_get(cbtree)) == NULL){
|
||||
/* No such tree, generate it */
|
||||
if ((ret = yang2cli_grouping(h, ygrouping, cbuf_get(cbtree))) < 0)
|
||||
goto done;
|
||||
if (ret == 0) /* tree empty */
|
||||
goto ok;
|
||||
}
|
||||
cprintf(cb, "%*s@%s", level*3, "", cbuf_get(cbtree));
|
||||
/* get api-path to parent, do not include "uses" argument since it is replaced */
|
||||
yp = yang_parent_get(ys);
|
||||
if (yang2api_path_fmt(yp, 0, &api_path_fmt) < 0)
|
||||
goto done;
|
||||
/* This adds a "prepend" callback with decendant argument */
|
||||
cprintf(cb, ",%s(\"%s\")", GROUPING_CALLBACK, api_path_fmt);
|
||||
cprintf(cb, ";\n");
|
||||
ok:
|
||||
retval = 0;
|
||||
done:
|
||||
if (api_path_fmt)
|
||||
free(api_path_fmt);
|
||||
if (prefix)
|
||||
free(prefix);
|
||||
if (id)
|
||||
free(id);
|
||||
if (cbtree)
|
||||
cbuf_free(cbtree);
|
||||
return retval;
|
||||
}
|
||||
|
||||
/*! Generate CLI code for Yang statement
|
||||
* @param[in] h Clixon handle
|
||||
* @param[in] ys Yang statement
|
||||
* @param[in] level Indentation level
|
||||
* @param[out] cb Buffer where cligen code is written
|
||||
* @retval 0 OK
|
||||
* @retval -1 Error
|
||||
*/
|
||||
static int
|
||||
yang2cli_stmt(clicon_handle h,
|
||||
|
|
@ -1117,10 +1199,11 @@ yang2cli_stmt(clicon_handle h,
|
|||
int level,
|
||||
cbuf *cb)
|
||||
{
|
||||
yang_stmt *yc;
|
||||
int retval = -1;
|
||||
yang_stmt *yc;
|
||||
int treeref_state = 0;
|
||||
|
||||
int grouping_treeref;
|
||||
|
||||
if (ys == NULL){
|
||||
clicon_err(OE_YANG, EINVAL, "No yang spec");
|
||||
goto done;
|
||||
|
|
@ -1135,7 +1218,19 @@ yang2cli_stmt(clicon_handle h,
|
|||
/* Only produce autocli for YANG non-config only if autocli-treeref-state is true */
|
||||
if (autocli_treeref_state(h, &treeref_state) < 0)
|
||||
goto done;
|
||||
if (autocli_grouping_treeref(h, &grouping_treeref) < 0)
|
||||
goto done;
|
||||
if (treeref_state || yang_config(ys)){
|
||||
int cornercase = 0;
|
||||
if (grouping_treeref){
|
||||
#ifdef AUTOCLI_GROUPING_TOPLEVEL_SKIP
|
||||
cornercase = yang_keyword_get(yang_parent_get(ys)) == Y_MODULE || yang_keyword_get(yang_parent_get(ys)) == Y_SUBMODULE;
|
||||
#endif
|
||||
if (yang_keyword_get(ys) != Y_USES && yang_flag_get(ys, YANG_FLAG_GROUPING)
|
||||
&& !cornercase
|
||||
)
|
||||
goto ok;
|
||||
}
|
||||
switch (yang_keyword_get(ys)){
|
||||
case Y_CONTAINER:
|
||||
if (yang2cli_container(h, ys, level, cb) < 0)
|
||||
|
|
@ -1157,6 +1252,10 @@ yang2cli_stmt(clicon_handle h,
|
|||
cb) < 0)
|
||||
goto done;
|
||||
break;
|
||||
case Y_USES:
|
||||
if (grouping_treeref && !cornercase && yang2cli_uses(h, ys, level, cb) < 0)
|
||||
goto done;
|
||||
break;
|
||||
case Y_CASE:
|
||||
case Y_SUBMODULE:
|
||||
case Y_MODULE:
|
||||
|
|
@ -1340,6 +1439,143 @@ yang2cli_post(clicon_handle h,
|
|||
return retval;
|
||||
}
|
||||
|
||||
/*! Generate clispec for all modules in a grouping
|
||||
*
|
||||
* Called in cli main function for top-level yangs. But may also be called dynamically for
|
||||
* mountpoints.
|
||||
* @param[in] h Clixon handle
|
||||
* @param[in] ys Top-level Yang statement
|
||||
* @param[in] treename Name of tree
|
||||
* @retval 1 OK
|
||||
* @retval 0 OK but empty clispec, no tree produced
|
||||
* @retval -1 Error
|
||||
* @note Tie-break of same top-level symbol: prefix is NYI
|
||||
* @see yang2cli_yspec and yang2cli_stmt for original
|
||||
* XXX merge with yang2cli_yspec
|
||||
*/
|
||||
static int
|
||||
yang2cli_grouping(clicon_handle h,
|
||||
yang_stmt *ys,
|
||||
char *treename)
|
||||
{
|
||||
int retval = -1;
|
||||
parse_tree *pt0 = NULL;
|
||||
parse_tree *pt = NULL;
|
||||
yang_stmt *yc;
|
||||
pt_head *ph;
|
||||
cbuf *cb = NULL;
|
||||
int treeref_state = 0;
|
||||
char *prefix;
|
||||
cg_obj *co;
|
||||
int config;
|
||||
int i;
|
||||
|
||||
if ((pt0 = pt_new()) == NULL){
|
||||
clicon_err(OE_UNIX, errno, "pt_new");
|
||||
goto done;
|
||||
}
|
||||
if ((cb = cbuf_new()) == NULL){
|
||||
clicon_err(OE_XML, errno, "cbuf_new");
|
||||
goto done;
|
||||
}
|
||||
/* Traverse YANG, loop through all modules and generate CLI, inline of yang2cli_stmt */
|
||||
if (yang_find(ys, Y_STATUS, "obsolete") != NULL){
|
||||
clicon_debug(4, "%s obsolete: %s %s, skipped", __FUNCTION__, yang_argument_get(ys), yang_argument_get(ys_module(ys)));
|
||||
goto empty;
|
||||
}
|
||||
if (yang_find(ys, Y_STATUS, "deprecated") != NULL){
|
||||
clicon_debug(4, "%s deprecated: %s %s", __FUNCTION__, yang_argument_get(ys), yang_argument_get(ys_module(ys)));
|
||||
}
|
||||
/* Only produce autocli for YANG non-config only if autocli-treeref-state is true */
|
||||
if (autocli_treeref_state(h, &treeref_state) < 0)
|
||||
goto done;
|
||||
if (treeref_state || yang_config(ys)){
|
||||
yc = NULL;
|
||||
while ((yc = yn_each(ys, yc)) != NULL)
|
||||
if (yang2cli_stmt(h, yc, 1, cb) < 0)
|
||||
goto done;
|
||||
}
|
||||
if (cbuf_len(cb) == 0)
|
||||
goto empty;
|
||||
/* Note Tie-break of same top-level symbol: prefix is NYI
|
||||
* Needs to move cligen_parse_str() call here instead of later
|
||||
*/
|
||||
if ((prefix = yang_find_myprefix(ys)) == NULL){
|
||||
clicon_err(OE_YANG, 0, "Module %s lacks prefix", yang_argument_get(ys)); /* shouldnt happen */
|
||||
goto done;
|
||||
}
|
||||
if ((pt = pt_new()) == NULL){
|
||||
clicon_err(OE_UNIX, errno, "pt_new");
|
||||
goto done;
|
||||
}
|
||||
/* Parse the buffer using cligen parser. load cli syntax */
|
||||
if (clispec_parse_str(cli_cligen(h), cbuf_get(cb), (char*)__FUNCTION__, NULL, pt, NULL) < 0){
|
||||
fprintf(stderr, "%s\n", cbuf_get(cb));
|
||||
goto done;
|
||||
}
|
||||
clicon_debug(CLIXON_DBG_DEFAULT, "%s Generated auto-cli for grouping:%s",
|
||||
__FUNCTION__, yang_argument_get(ys));
|
||||
/* Add prefix: assume new are appended */
|
||||
for (i=0; i<pt_len_get(pt); i++){
|
||||
if ((co = pt_vec_i_get(pt, i)) != NULL){
|
||||
clicon_debug(CLIXON_DBG_DEFAULT, "%s command: %s",
|
||||
__FUNCTION__, co->co_command);
|
||||
co_prefix_set(co, prefix);
|
||||
}
|
||||
}
|
||||
/* Post-processing, iterate over the generated cligen parse-tree with corresponding yang
|
||||
* Note cannot do it inline in yang2cli above since:
|
||||
* 1. labels cannot be set on "empty"
|
||||
* 2. a; <a>, fn() cannot be set properly
|
||||
*/
|
||||
config = 1;
|
||||
if (yang2cli_post(h, NULL, pt, 0, ys, NULL, &config) < 0){
|
||||
goto done;
|
||||
}
|
||||
if (clicon_data_int_get(h, "autocli-print-debug") == 1)
|
||||
clicon_log(LOG_NOTICE, "%s: Top-level cli-spec %s:\n%s",
|
||||
__FUNCTION__, treename, cbuf_get(cb));
|
||||
else
|
||||
clicon_debug(CLIXON_DBG_DETAIL, "%s: Top-level cli-spec %s:\n%s",
|
||||
__FUNCTION__, treename, cbuf_get(cb));
|
||||
if (cligen_parsetree_merge(pt0, NULL, pt) < 0){
|
||||
clicon_err(OE_YANG, errno, "cligen_parsetree_merge");
|
||||
goto done;
|
||||
}
|
||||
pt_free(pt, 1);
|
||||
pt = NULL;
|
||||
|
||||
/* Resolve the expand callback functions in the generated syntax.
|
||||
* This "should" only be GENERATE_EXPAND_XMLDB
|
||||
* handle=NULL for global namespace, this means expand callbacks must be in
|
||||
* CLICON namespace, not in a cli frontend plugin.
|
||||
*/
|
||||
if (cligen_expandv_str2fn(pt0, (expandv_str2fn_t*)clixon_str2fn, NULL) < 0)
|
||||
goto done;
|
||||
/* Append cligen tree and name it */
|
||||
if ((ph = cligen_ph_add(cli_cligen(h), treename)) == NULL){
|
||||
clicon_err(OE_UNIX, 0, "cligen_ph_add");
|
||||
goto done;
|
||||
}
|
||||
if (cligen_ph_parsetree_set(ph, pt0) < 0){
|
||||
clicon_err(OE_UNIX, 0, "cligen_ph_parsetree_set");
|
||||
goto done;
|
||||
}
|
||||
pt0 = NULL;
|
||||
retval = 1;
|
||||
done:
|
||||
if (pt)
|
||||
pt_free(pt, 1);
|
||||
if (pt0)
|
||||
pt_free(pt0, 1);
|
||||
if (cb)
|
||||
cbuf_free(cb);
|
||||
return retval;
|
||||
empty:
|
||||
retval = 0;
|
||||
goto done;
|
||||
}
|
||||
|
||||
/*! Generate clispec for all modules in yspec (except excluded)
|
||||
*
|
||||
* Called in cli main function for top-level yangs. But may also be called dynamically for
|
||||
|
|
@ -1402,7 +1638,6 @@ yang2cli_yspec(clicon_handle h,
|
|||
clicon_err(OE_UNIX, errno, "pt_new");
|
||||
goto done;
|
||||
}
|
||||
|
||||
/* Parse the buffer using cligen parser. load cli syntax */
|
||||
if (clispec_parse_str(cli_cligen(h), cbuf_get(cb), "yang2cli", NULL, pt, NULL) < 0){
|
||||
fprintf(stderr, "%s\n", cbuf_get(cb));
|
||||
|
|
@ -1446,7 +1681,7 @@ yang2cli_yspec(clicon_handle h,
|
|||
* handle=NULL for global namespace, this means expand callbacks must be in
|
||||
* CLICON namespace, not in a cli frontend plugin.
|
||||
*/
|
||||
if (cligen_expandv_str2fn(pt0, (expandv_str2fn_t*)clixon_str2fn, NULL) < 0)
|
||||
if (cligen_expandv_str2fn(pt0, (expandv_str2fn_t*)clixon_str2fn, NULL) < 0)
|
||||
goto done;
|
||||
/* Append cligen tree and name it */
|
||||
if ((ph = cligen_ph_add(cli_cligen(h), treename)) == NULL){
|
||||
|
|
|
|||
|
|
@ -48,6 +48,11 @@
|
|||
* where the virtual callback (overwrite_me) is overwritten by cli_set.
|
||||
*/
|
||||
#define GENERATE_CALLBACK "overwrite_me"
|
||||
#define GROUPING_CALLBACK "prepend_me"
|
||||
#define MTPOINT_PREFIX "mtpoint:"
|
||||
|
||||
/* variable expand function */
|
||||
#define GENERATE_EXPAND_XMLDB "expand_dbvar"
|
||||
|
||||
/* Name of autocli CLIgen treename
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -173,13 +173,17 @@ xpath_append(cbuf *cb0,
|
|||
* functionality.
|
||||
*
|
||||
* Assume callback given in a cligen spec: a <x:int expand_dbvar("db" "<xmlkeyfmt>")
|
||||
* @param[in] h clicon handle
|
||||
* @param[in] h clicon handle
|
||||
* @param[in] name Name of this function (eg "expand_dbvar")
|
||||
* @param[in] cvv The command so far. Eg: cvec [0]:"a 5 b"; [1]: x=5;
|
||||
* @param[in] argv Arguments given at the callback: <db> <apipathfmt> [<mointpt>]
|
||||
* @param[in] argv Arguments given at the callback:
|
||||
* <db> Name of datastore, such as "running"
|
||||
* <api_path_fmt> Generated API PATH (this is added implicitly, not actually given in the cvv)
|
||||
* [<mt-point>] Optional YANG path-arg/xpath from mount-point
|
||||
* @param[out] commands vector of function pointers to callback functions
|
||||
* @param[out] helptxt vector of pointers to helptexts
|
||||
* @see cli_expand_var_generate This is where arg is generated
|
||||
* @see cli_expand_var_generate where api_path_fmt + mt-point are generated
|
||||
*/
|
||||
int
|
||||
expand_dbvar(void *h,
|
||||
|
|
@ -190,10 +194,10 @@ expand_dbvar(void *h,
|
|||
cvec *helptexts)
|
||||
{
|
||||
int retval = -1;
|
||||
char *api_path_fmt;
|
||||
char *api_path_fmt = NULL;
|
||||
char *api_path = NULL;
|
||||
char *api_path_fmt01 = NULL;
|
||||
char *dbstr;
|
||||
char *dbstr;
|
||||
cxobj *xt = NULL;
|
||||
char *xpath = NULL;
|
||||
cxobj **xvec = NULL;
|
||||
|
|
@ -220,7 +224,10 @@ expand_dbvar(void *h,
|
|||
yang_stmt *yspec;
|
||||
yang_stmt *yu = NULL;
|
||||
cvec *nsc0 = NULL;
|
||||
|
||||
char *str;
|
||||
int grouping_treeref;
|
||||
cbuf *cbprepend;
|
||||
|
||||
if (argv == NULL || (cvec_len(argv) != 2 && cvec_len(argv) != 3)){
|
||||
clicon_err(OE_PLUGIN, EINVAL, "requires arguments: <db> <apipathfmt> [<mountpt>]");
|
||||
goto done;
|
||||
|
|
@ -244,10 +251,33 @@ expand_dbvar(void *h,
|
|||
clicon_err(OE_PLUGIN, 0, "Error when accessing argument <api_path>");
|
||||
goto done;
|
||||
}
|
||||
api_path_fmt = cv_string_get(cv);
|
||||
if (autocli_grouping_treeref(h, &grouping_treeref) < 0)
|
||||
goto done;
|
||||
if (grouping_treeref &&
|
||||
(cbprepend = cligen_expand_prepend_get(cli_cligen(h))) != NULL){
|
||||
cbuf *cb;
|
||||
if ((cb = cbuf_new()) == NULL){
|
||||
clicon_err(OE_UNIX, errno, "cbuf_new");
|
||||
goto done;
|
||||
}
|
||||
cprintf(cb, "%s%s", cbuf_get(cbprepend), cv_string_get(cv));
|
||||
if ((api_path_fmt = strdup(cbuf_get(cb))) == NULL){
|
||||
clicon_err(OE_UNIX, errno, "strdup");
|
||||
goto done;
|
||||
}
|
||||
}
|
||||
else if ((api_path_fmt = strdup(cv_string_get(cv))) == NULL){
|
||||
clicon_err(OE_UNIX, errno, "strdup");
|
||||
goto done;
|
||||
}
|
||||
if (cvec_len(argv) > 2){
|
||||
cv = cvec_i(argv, 2);
|
||||
mtpoint = cv_string_get(cv);
|
||||
str = cv_string_get(cv);
|
||||
if (strncmp(str, "mtpoint:", strlen("mtpoint:")) != 0){
|
||||
clicon_err(OE_PLUGIN, 0, "mtpoint does not begin with 'mtpoint:'");
|
||||
goto done;
|
||||
}
|
||||
mtpoint = str + strlen("mtpoint:");
|
||||
}
|
||||
/* api_path_fmt = /interface/%s/address/%s
|
||||
* api_path: --> /interface/eth0/address/.*
|
||||
|
|
@ -384,6 +414,8 @@ expand_dbvar(void *h,
|
|||
done:
|
||||
if (nsc0)
|
||||
cvec_free(nsc0);
|
||||
if (api_path_fmt)
|
||||
free(api_path_fmt);
|
||||
if (api_path_fmt01)
|
||||
free(api_path_fmt01);
|
||||
if (cbxpath)
|
||||
|
|
@ -847,8 +879,8 @@ int cli_show_version(clicon_handle h,
|
|||
* @param[in] h Clixon handle
|
||||
* @param[in] cvv Vector of variables from CLIgen command-line
|
||||
* @param[in] argv String vector of show options, format:
|
||||
* <api_path_fmt> Generated API PATH (this is added implicitly, not actually given in the cvv)
|
||||
* [<api-path-fmt>] Extra api-path from mount-point
|
||||
* <api_path_fmt> Generated API PATH (this is added implicitly, not actually given in argv)
|
||||
* [<mt-point>] Optional YANG path-arg/xpath from mount-point
|
||||
* <dbname> Name of datastore, such as "running"
|
||||
* -- from here optional:
|
||||
* <format> "text"|"xml"|"json"|"cli"|"netconf" (see format_enum), default: xml
|
||||
|
|
@ -872,6 +904,7 @@ int cli_show_version(clicon_handle h,
|
|||
* @see cli_show_auto_mode autocli with edit menu support
|
||||
*
|
||||
* XXX merge cli_show_auto and cli_show_auto_mode
|
||||
* @see cli_callback_generate where api_path_fmt + mt-point are generated
|
||||
*/
|
||||
int
|
||||
cli_show_auto(clicon_handle h,
|
||||
|
|
@ -903,8 +936,8 @@ cli_show_auto(clicon_handle h,
|
|||
}
|
||||
api_path_fmt = cv_string_get(cvec_i(argv, argc++));
|
||||
str = cv_string_get(cvec_i(argv, argc++));
|
||||
if (str && str[0] == '/'){ /* ad-hoc to see if 2nd arg is mountpoint */
|
||||
mtpoint = str;
|
||||
if (str && strncmp(str, "mtpoint:", strlen("mtpoint:")) == 0){
|
||||
mtpoint = str + strlen("mtpoint:");
|
||||
dbname = cv_string_get(cvec_i(argv, argc++));
|
||||
}
|
||||
else
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue