revert yang2cli sub code, use existing yang2cli solution

This commit is contained in:
Olof hagsand 2020-11-27 15:31:27 +01:00
parent dda3244252
commit 75f85e0253
15 changed files with 339 additions and 191 deletions

View file

@ -105,8 +105,6 @@ You can see which CLISPEC it generates via clixon_cli -D 2:
* @param[in] cvtype Type of the cligen variable
* @param[in] options
* @param[in] fraction_digits
* @param[in] yp0 Build the path of ys only to this level not root (optional)
* @param[in] yp0path Use this path if stop at yp0 (not root)
* @param[out] cb The string where the result format string is inserted.
* @see expand_dbvar This is where the expand string is used
@ -118,14 +116,12 @@ cli_expand_var_generate(clicon_handle h,
enum cv_type cvtype,
int options,
uint8_t fraction_digits,
yang_stmt *yp0,
char *yp0_path,
cbuf *cb)
{
int retval = -1;
char *api_path_fmt = NULL;
if (yang2api_path_fmt(ys, 1, yp0, yp0_path, &api_path_fmt) < 0)
if (yang2api_path_fmt(ys, 1, &api_path_fmt) < 0)
goto done;
cprintf(cb, "|<%s:%s", yang_argument_get(ys),
cv_type2str(cvtype));
@ -144,8 +140,6 @@ cli_expand_var_generate(clicon_handle h,
/*! Create callback with api_path format string as argument
* @param[in] h clicon handle
* @param[in] ys yang_stmt of the node at hand
* @param[in] yp0 Build the path of ys only to this level not root (optional)
* @param[in] yp0path Use this path if stop at yp0 (not root)
* @param[out] cb The string where the result format string is inserted.
* @see cli_dbxml This is where the xmlkeyfmt string is used
* @see pt_callback_reference in CLIgen where the actual callback overwrites the template
@ -153,14 +147,12 @@ cli_expand_var_generate(clicon_handle h,
static int
cli_callback_generate(clicon_handle h,
yang_stmt *ys,
yang_stmt *yp0,
char *yp0_path,
cbuf *cb)
{
int retval = -1;
char *api_path_fmt = NULL;
if (yang2api_path_fmt(ys, 0, yp0, yp0_path, &api_path_fmt) < 0)
if (yang2api_path_fmt(ys, 0, &api_path_fmt) < 0)
goto done;
cprintf(cb, ",%s(\"%s\")", GENERATE_CALLBACK,
api_path_fmt);
@ -363,7 +355,7 @@ yang2cli_var_pattern(clicon_handle h,
/* Forward */
static int yang2cli_stmt(clicon_handle h, yang_stmt *ys, enum genmodel_type gt,
int level, int state, int show_tree,
yang_stmt *yp0, char *yp0_path, cbuf *cb);
cbuf *cb);
static int yang2cli_var_union(clicon_handle h, yang_stmt *ys, char *origtype,
yang_stmt *ytype, char *helptext, cbuf *cb);
@ -558,8 +550,6 @@ yang2cli_var_union(clicon_handle h,
* @param[in] h Clixon handle
* @param[in] ys Yang statement
* @param[in] helptext CLI help text
* @param[in] yp0 Build the path of ys only to this level not root (optional)
* @param[in] yp0path Use this path if stop at yp0 (not root)
* @param[out] cb Buffer where cligen code is written
*
@ -573,8 +563,6 @@ static int
yang2cli_var(clicon_handle h,
yang_stmt *ys,
char *helptext,
yang_stmt *yp0,
char *yp0_path,
cbuf *cb)
{
int retval = -1;
@ -613,7 +601,6 @@ yang2cli_var(clicon_handle h,
if (clicon_cli_genmodel_completion(h)){
if (cli_expand_var_generate(h, ys, cvtype,
options, fraction_digits,
yp0, yp0_path,
cb) < 0)
goto done;
yang2cli_helptext(cb, helptext);
@ -637,7 +624,6 @@ yang2cli_var(clicon_handle h,
if (completionp){
if (cli_expand_var_generate(h, ys, cvtype,
options, fraction_digits,
yp0, yp0_path,
cb) < 0)
goto done;
yang2cli_helptext(cb, helptext);
@ -661,8 +647,6 @@ yang2cli_var(clicon_handle h,
* @param[in] callback If set, include a "; cli_set()" callback, otherwise not
* @param[in] show_tree Is tree for show cli command
* @param[in] key_leaf Is leaf in a key in a list module
* @param[in] yp0 Build the path of ys only to this level not root (optional)
* @param[in] yp0path Use this path if stop at yp0 (not root)
* @param[out] cb Buffer where cligen code is written
*/
static int
@ -673,8 +657,6 @@ yang2cli_leaf(clicon_handle h,
int callback,
int show_tree,
int key_leaf,
yang_stmt *yp0,
char *yp0_path,
cbuf *cb)
{
yang_stmt *yd; /* description */
@ -697,18 +679,18 @@ yang2cli_leaf(clicon_handle h,
yang2cli_helptext(cb, helptext);
cprintf(cb, " ");
if ((show_tree == 0) || (key_leaf == 1)) {
if (yang2cli_var(h, ys, helptext, yp0, yp0_path, cb) < 0)
if (yang2cli_var(h, ys, helptext, cb) < 0)
goto done;
}
}
else
if ((show_tree == 0) || (key_leaf == 1)) {
if (yang2cli_var(h, ys, helptext, yp0, yp0_path, cb) < 0)
if (yang2cli_var(h, ys, helptext, cb) < 0)
goto done;
}
if (callback){
if (cli_callback_generate(h, ys, yp0, yp0_path, cb) < 0)
if (cli_callback_generate(h, ys, cb) < 0)
goto done;
cprintf(cb, ";\n");
}
@ -727,8 +709,6 @@ yang2cli_leaf(clicon_handle h,
* @param[in] level Indentation level
* @param[in] state Include syntax for state not only config
* @param[in] show_tree Is tree for show cli command
* @param[in] yp0 Build the path of ys only to this level not root (optional)
* @param[in] yp0path Use this path if stop at yp0 (not root)
* @param[out] cb Buffer where cligen code is written
*/
static int
@ -738,8 +718,6 @@ yang2cli_container(clicon_handle h,
int level,
int state,
int show_tree,
yang_stmt *yp0,
char *yp0_path,
cbuf *cb)
{
yang_stmt *yc;
@ -764,14 +742,14 @@ yang2cli_container(clicon_handle h,
*s = '\0';
yang2cli_helptext(cb, helptext);
}
if (cli_callback_generate(h, ys, yp0, yp0_path, cb) < 0)
if (cli_callback_generate(h, ys, cb) < 0)
goto done;
cprintf(cb, ";{\n");
}
yc = NULL;
while ((yc = yn_each(ys, yc)) != NULL)
if (yang2cli_stmt(h, yc, gt, level+1, state, show_tree, yp0, yp0_path, cb) < 0)
if (yang2cli_stmt(h, yc, gt, level+1, state, show_tree, cb) < 0)
goto done;
if (hide == 0)
cprintf(cb, "%*s}\n", level*3, "");
@ -789,8 +767,6 @@ yang2cli_container(clicon_handle h,
* @param[in] level Indentation level
* @param[in] state Include syntax for state not only config
* @param[in] show_tree Is tree for show cli command
* @param[in] yp0 Build the path of ys only to this level not root (optional)
* @param[in] yp0path Use this path if stop at yp0 (not root)
* @param[out] cb Buffer where cligen code is written
*/
static int
@ -800,8 +776,6 @@ yang2cli_list(clicon_handle h,
int level,
int state,
int show_tree,
yang_stmt *yp0,
char *yp0_path,
cbuf *cb)
{
yang_stmt *yc;
@ -842,7 +816,7 @@ yang2cli_list(clicon_handle h,
list_has_callback = cvec_next(cvk, cvi)?0:1;
if (show_tree == 1) {
if (list_has_callback) {
if (cli_callback_generate(h, ys, yp0, yp0_path, cb) < 0)
if (cli_callback_generate(h, ys, cb) < 0)
goto done;
cprintf(cb, ";\n");
cprintf(cb, "{\n");
@ -852,7 +826,6 @@ yang2cli_list(clicon_handle h,
if (yang2cli_leaf(h, yleaf,
(gt==GT_VARS||gt==GT_HIDE)?GT_NONE:gt, level+1,
list_has_callback, show_tree, 1,
yp0, yp0_path,
cb) < 0)
goto done;
}
@ -871,7 +844,7 @@ yang2cli_list(clicon_handle h,
}
if (cvi != NULL)
continue;
if (yang2cli_stmt(h, yc, gt, level+1, state, show_tree, yp0, yp0_path, cb) < 0)
if (yang2cli_stmt(h, yc, gt, level+1, state, show_tree, cb) < 0)
goto done;
}
cprintf(cb, "%*s}\n", level*3, "");
@ -893,8 +866,6 @@ yang2cli_list(clicon_handle h,
* @param[in] level Indentation level
* @param[in] state Include syntax for state not only config
* @param[in] show_tree Is tree for show cli command
* @param[in] yp0 Build the path of ys only to this level not root (optional)
* @param[in] yp0path Use this path if stop at yp0 (not root)
* @param[out] cb Buffer where cligen code is written
@example
choice interface-type {
@ -912,8 +883,6 @@ yang2cli_choice(clicon_handle h,
int level,
int state,
int show_tree,
yang_stmt *yp0,
char *yp0_path,
cbuf *cb)
{
int retval = -1;
@ -923,7 +892,7 @@ yang2cli_choice(clicon_handle h,
while ((yc = yn_each(ys, yc)) != NULL) {
switch (yang_keyword_get(yc)){
case Y_CASE:
if (yang2cli_stmt(h, yc, gt, level+2, state, show_tree, yp0, yp0_path, cb) < 0)
if (yang2cli_stmt(h, yc, gt, level+2, state, show_tree, cb) < 0)
goto done;
break;
case Y_CONTAINER:
@ -931,7 +900,7 @@ yang2cli_choice(clicon_handle h,
case Y_LEAF_LIST:
case Y_LIST:
default:
if (yang2cli_stmt(h, yc, gt, level+1, state, show_tree, yp0, yp0_path, cb) < 0)
if (yang2cli_stmt(h, yc, gt, level+1, state, show_tree, cb) < 0)
goto done;
break;
}
@ -948,8 +917,6 @@ yang2cli_choice(clicon_handle h,
* @param[in] level Indentation level
* @param[in] state Include syntax for state not only config
* @param[in] show_tree Is tree for show cli command
* @param[in] yp0 Build the path of ys only to this level not root (optional)
* @param[in] yp0path Use this path if stop at yp0 (not root)
* @param[out] cb Buffer where cligen code is written
*/
static int
@ -959,8 +926,6 @@ yang2cli_stmt(clicon_handle h,
int level,
int state,
int show_tree,
yang_stmt *yp0,
char *yp0_path,
cbuf *cb)
{
yang_stmt *yc;
@ -969,24 +934,20 @@ yang2cli_stmt(clicon_handle h,
if (state || yang_config(ys)){
switch (yang_keyword_get(ys)){
case Y_CONTAINER:
if (yang2cli_container(h, ys, gt, level, state, show_tree,
yp0, yp0_path, cb) < 0)
if (yang2cli_container(h, ys, gt, level, state, show_tree, cb) < 0)
goto done;
break;
case Y_LIST:
if (yang2cli_list(h, ys, gt, level, state, show_tree,
yp0, yp0_path, cb) < 0)
if (yang2cli_list(h, ys, gt, level, state, show_tree, cb) < 0)
goto done;
break;
case Y_CHOICE:
if (yang2cli_choice(h, ys, gt, level, state, show_tree,
yp0, yp0_path, cb) < 0)
if (yang2cli_choice(h, ys, gt, level, state, show_tree, cb) < 0)
goto done;
break;
case Y_LEAF_LIST:
case Y_LEAF:
if (yang2cli_leaf(h, ys, gt, level, 1, show_tree, 0,
yp0, yp0_path, cb) < 0)
if (yang2cli_leaf(h, ys, gt, level, 1, show_tree, 0, cb) < 0)
goto done;
break;
case Y_CASE:
@ -994,8 +955,7 @@ yang2cli_stmt(clicon_handle h,
case Y_MODULE:
yc = NULL;
while ((yc = yn_each(ys, yc)) != NULL)
if (yang2cli_stmt(h, yc, gt, level+1, state, show_tree,
yp0, yp0_path, cb) < 0)
if (yang2cli_stmt(h, yc, gt, level+1, state, show_tree, cb) < 0)
goto done;
break;
default: /* skip */
@ -1013,8 +973,6 @@ yang2cli_stmt(clicon_handle h,
* @param[in] printgen Log generated CLIgen syntax
* @param[in] state Set to include state syntax
* @param[in] show_tree Is tree for show cli command
* @param[in] yp0 Build the path of ys only to this level not root (optional)
* @param[in] yp0path Use this path if stop at yp0 (not root)
* @param[out] pt CLIgen parse-tree (must be created on input)
* @retval 0 OK
* @retval -1 Error
@ -1025,8 +983,6 @@ yang2cli(clicon_handle h,
int printgen,
int state,
int show_tree,
yang_stmt *yp0,
char *yp0_path,
parse_tree *pt)
{
int retval = -1;
@ -1047,8 +1003,7 @@ yang2cli(clicon_handle h,
/* Traverse YANG, loop through all modules and generate CLI */
yc = NULL;
while ((yc = yn_each(yn, yc)) != NULL)
if (yang2cli_stmt(h, yc, gt, 0, state, show_tree,
yp0, yp0_path, cb) < 0)
if (yang2cli_stmt(h, yc, gt, 0, state, show_tree, cb) < 0)
goto done;
if (printgen)
clicon_log(LOG_NOTICE, "%s: Generated CLI spec:\n%s", __FUNCTION__, cbuf_get(cb));
@ -1077,50 +1032,3 @@ yang2cli(clicon_handle h,
return retval;
}
/*! Generate CLI code for Yang specification
* @param[in] h Clixon handle
* @param[in] api_path API-path of sub-xml/yang
* @param[in] name Name of tree: use @<name> in clispec
* @param[in] printgen Log generated CLIgen syntax
* @param[in] state Also include state syntax
* @retval -1 Error, with clicon_err called
* @retval 0 OK , with result in yres
*/
int
yang2cli_sub(clicon_handle h,
char *api_path,
char *name,
int printgen,
int state)
{
int retval = -1;
yang_stmt *yspec;
yang_stmt *yn = NULL;
pt_head *ph;
parse_tree *pt = NULL; /* cli parse tree */
/* Get yspec */
yspec = clicon_dbspec_yang(h);
if (api_path2xml(api_path, yspec, NULL, YC_DATANODE, 1, NULL, &yn, NULL) < 0)
goto done;
if (yn == NULL)
goto ok; /* not found */
/* Create empty parse-tree */
if ((pt = pt_new()) == NULL){
clicon_err(OE_UNIX, errno, "pt_new");
goto done;
}
/* Generate tree from yangnode yn */
if (yang2cli(h, yn, printgen, state, 0, yn, api_path, pt) < 0)
goto done;
/* Add a new parse-tree header */
if ((ph = cligen_ph_add(cli_cligen(h), name)) == NULL)
goto done;
/* Add generated parse-tree to header */
if (cligen_ph_parsetree_set(ph, pt) < 0)
goto done;
ok:
retval = 0;
done:
return retval;
}