data tree FEATURE initial implementation of YANG data support
diff --git a/tools/lint/commands.c b/tools/lint/commands.c
index 58ebe96..4b65a76 100644
--- a/tools/lint/commands.c
+++ b/tools/lint/commands.c
@@ -520,7 +520,7 @@
return ret;
}
-#if 0
+
static LYD_FORMAT
detect_data_format(char *filepath)
{
@@ -531,10 +531,12 @@
for (; isspace(filepath[len - 1]); len--, filepath[len] = '\0'); /* remove trailing whitespaces */
if (len >= 5 && !strcmp(&filepath[len - 4], ".xml")) {
return LYD_XML;
+#if 0
} else if (len >= 6 && !strcmp(&filepath[len - 5], ".json")) {
return LYD_JSON;
} else if (len >= 5 && !strcmp(&filepath[len - 4], ".lyb")) {
return LYD_LYB;
+#endif
} else {
return LYD_UNKNOWN;
}
@@ -545,7 +547,6 @@
struct lyd_node **result)
{
LYD_FORMAT informat = LYD_UNKNOWN;
- struct lyxml_elem *xml = NULL;
struct lyd_node *data = NULL, *rpc_act = NULL;
int opts = *options;
@@ -556,8 +557,9 @@
return EXIT_FAILURE;
}
- ly_errno = LY_SUCCESS;
+ ly_err_clean(ctx, NULL);
+#if 0
if ((opts & LYD_OPT_TYPEMASK) == LYD_OPT_TYPEMASK) {
/* automatically detect data type from the data top level */
if (informat != LYD_XML) {
@@ -633,6 +635,7 @@
}
lyxml_free(ctx, xml);
} else {
+#endif
if (opts & LYD_OPT_RPCREPLY) {
if (!rpc_act_file) {
fprintf(stderr, "RPC/action reply data require additional argument (file with the RPC/action).\n");
@@ -659,12 +662,14 @@
}
data = lyd_parse_path(ctx, filepath, informat, opts);
}
+#if 0
}
- lyd_free_withsiblings(rpc_act);
+#endif
+ lyd_free_all(rpc_act);
- if (ly_errno) {
+ if (ly_err_first(ctx)) {
fprintf(stderr, "Failed to parse data.\n");
- lyd_free_withsiblings(data);
+ lyd_free_all(data);
return EXIT_FAILURE;
}
@@ -719,6 +724,7 @@
}
switch (c) {
+#if 0
case 'd':
if (!strcmp(optarg, "all")) {
printopt = (printopt & ~LYP_WD_MASK) | LYP_WD_ALL;
@@ -730,6 +736,7 @@
printopt = (printopt & ~LYP_WD_MASK) | LYP_WD_IMPL_TAG;
}
break;
+#endif
case 'h':
cmd_data_help();
ret = 0;
@@ -737,10 +744,12 @@
case 'f':
if (!strcmp(optarg, "xml")) {
outformat = LYD_XML;
+#if 0
} else if (!strcmp(optarg, "json")) {
outformat = LYD_JSON;
} else if (!strcmp(optarg, "lyb")) {
outformat = LYD_LYB;
+#endif
} else {
fprintf(stderr, "Unknown output format \"%s\".\n", optarg);
goto cleanup;
@@ -827,9 +836,9 @@
if (outformat != LYD_UNKNOWN) {
if (options & LYD_OPT_RPCREPLY) {
- lyd_print_file(output, data->child, outformat, LYP_WITHSIBLINGS | LYP_FORMAT | printopt);
+ lyd_print_file(output, lyd_node_children(data), outformat, LYDP_WITHSIBLINGS | LYDP_FORMAT | printopt);
} else {
- lyd_print_file(output, data, outformat, LYP_WITHSIBLINGS | LYP_FORMAT | printopt);
+ lyd_print_file(output, data, outformat, LYDP_WITHSIBLINGS | LYDP_FORMAT | printopt);
}
}
@@ -843,12 +852,12 @@
fclose(output);
}
- lyd_free_withsiblings(val_tree);
- lyd_free_withsiblings(data);
+ lyd_free_all(val_tree);
+ lyd_free_all(data);
return ret;
}
-
+#if 0
int
cmd_xpath(const char *arg)
{
@@ -1540,8 +1549,8 @@
{"add", cmd_add, cmd_add_help, "Add a new model from a specific file"},
{"load", cmd_load, cmd_load_help, "Load a new model from the searchdirs"},
{"print", cmd_print, cmd_print_help, "Print a model"},
-#if 0
{"data", cmd_data, cmd_data_help, "Load, validate and optionally print instance data"},
+#if 0
{"xpath", cmd_xpath, cmd_xpath_help, "Get data nodes satisfying an XPath expression"},
{"list", cmd_list, cmd_list_help, "List all the loaded models"},
#endif
diff --git a/tools/lint/main_ni.c b/tools/lint/main_ni.c
index e128649..8b72e15 100644
--- a/tools/lint/main_ni.c
+++ b/tools/lint/main_ni.c
@@ -202,26 +202,27 @@
* 2 - data format
*/
static int
-get_fileformat(const char *filename, LYS_INFORMAT *schema/* TODO , LYD_FORMAT *data */)
+get_fileformat(const char *filename, LYS_INFORMAT *schema, LYD_FORMAT *data)
{
char *ptr;
LYS_INFORMAT informat_s;
-#if 0
LYD_FORMAT informat_d;
-#endif
+
/* get the file format */
if ((ptr = strrchr(filename, '.')) != NULL) {
++ptr;
if (!strcmp(ptr, "yang")) {
informat_s = LYS_IN_YANG;
-#if 0
informat_d = 0;
+#if 0
} else if (!strcmp(ptr, "yin")) {
informat_s = LYS_IN_YIN;
informat_d = 0;
+#endif
} else if (!strcmp(ptr, "xml")) {
informat_s = 0;
informat_d = LYD_XML;
+#if 0
} else if (!strcmp(ptr, "json")) {
informat_s = 0;
informat_d = LYD_JSON;
@@ -234,24 +235,20 @@
fprintf(stderr, "yanglint error: input file \"%s\" without file extension - unknown format.\n", filename);
return 0;
}
-#if 0
+
if (data) {
(*data) = informat_d;
}
-#endif
+
if (schema) {
(*schema) = informat_s;
}
-#if 0
if (informat_s) {
return 1;
} else {
return 2;
}
-#else
- return 1;
-#endif
}
int
@@ -289,9 +286,9 @@
#if 0
{"running", required_argument, NULL, 'r'},
{"operational", required_argument, NULL, 'O'},
+#endif
{"strict", no_argument, NULL, 's'},
{"type", required_argument, NULL, 't'},
-#endif
{"version", no_argument, NULL, 'v'},
{"verbose", no_argument, NULL, 'V'},
#ifndef NDEBUG
@@ -305,8 +302,9 @@
const struct lys_module *mod;
LYS_OUTFORMAT outformat_s = 0;
LYS_INFORMAT informat_s;
+ LYD_FORMAT informat_d, outformat_d = 0;
#if 0
- LYD_FORMAT informat_d, outformat_d = 0, ylformat = 0;
+ LYD_FORMAT ylformat = 0;
#endif
struct ly_set *searchpaths = NULL;
const char *outtarget_s = NULL;
@@ -314,21 +312,23 @@
struct stat st;
uint32_t u;
int options_ctx = LY_CTX_NOYANGLIBRARY, list = 0, outoptions_s = 0, outline_length_s = 0;
+ int autodetection = 0, options_parser = 0, merge = 0;
+ const char *oper_file = NULL;
#if 0
- const char *oper_file = NULL, *envelope_s = NULL;
+ const char *envelope_s = NULL;
char *ylpath = NULL;
- int options_dflt = 0, options_parser = 0, envelope = 0, autodetection = 0, merge = 0;
+ int options_dflt = 0, envelope = 0;
+ struct lyxml_elem *iter, *elem;
+ struct *subroot, *next, *node;
+#endif
+ struct lyd_node *oper = NULL;
struct dataitem {
const char *filename;
- struct lyxml_elem *xml;
struct lyd_node *tree;
struct dataitem *next;
LYD_FORMAT format;
int type;
} *data = NULL, *data_item, *data_prev = NULL;
- struct lyxml_elem *iter, *elem;
- struct lyd_node *oper = NULL, *subroot, *next, *node;
-#endif
struct ly_set *mods = NULL;
void *p;
int index = 0;
@@ -367,8 +367,8 @@
case 'f':
if (!strcasecmp(optarg, "yang")) {
outformat_s = LYS_OUT_YANG;
-#if 0
outformat_d = 0;
+#if 0
} else if (!strcasecmp(optarg, "tree")) {
outformat_s = LYS_OUT_TREE;
outformat_d = 0;
@@ -382,9 +382,11 @@
} else if (!strcasecmp(optarg, "jsons")) {
outformat_s = LYS_OUT_JSON;
outformat_d = 0;
+#endif
} else if (!strcasecmp(optarg, "xml")) {
outformat_s = 0;
outformat_d = LYD_XML;
+#if 0
} else if (!strcasecmp(optarg, "json")) {
outformat_s = 0;
outformat_d = LYD_JSON;
@@ -504,6 +506,7 @@
oper_file = optarg;
}
break;
+#endif
case 's':
options_parser |= LYD_OPT_STRICT;
break;
@@ -533,7 +536,6 @@
goto cleanup;
}
break;
-#endif
case 'v':
version();
ret = EXIT_SUCCESS;
@@ -656,11 +658,11 @@
/* ignore operational datastore file */
oper_file = NULL;
}
+#endif
if ((options_parser & LYD_OPT_TYPEMASK) == LYD_OPT_DATA) {
/* add option to ignore ietf-yang-library data for implicit data type */
options_parser |= LYD_OPT_DATA_NO_YANGLIB;
}
-#endif
/* set callback for printing libyang messages */
ly_set_log_clb(libyang_verbclb, 1);
@@ -695,7 +697,7 @@
/* divide input files */
for (i = 0; i < argc - optind; i++) {
/* get the file format */
- if (!get_fileformat(argv[optind + i], &informat_s/* TODO, &informat_d */)) {
+ if (!get_fileformat(argv[optind + i], &informat_s, &informat_d)) {
goto cleanup;
}
@@ -713,7 +715,6 @@
goto cleanup;
}
ly_set_add(mods, (void *)mod, 0);
-#if 0
} else {
if (autodetection && informat_d != LYD_XML) {
/* data file content autodetection is possible only for XML input */
@@ -733,17 +734,13 @@
data_item->format = informat_d;
data_item->type = options_parser & LYD_OPT_TYPEMASK;
data_item->tree = NULL;
- data_item->xml = NULL;
data_item->next = NULL;
-#endif
}
}
-#if 0
if (outformat_d && !data && !list) {
fprintf(stderr, "yanglint error: no input data file for the specified data output format.\n");
goto cleanup;
}
-#endif
/* enable specified features, if not specified, all the module's features are enabled */
u = 4; /* skip internal libyang modules */
@@ -795,9 +792,7 @@
fputs("\n", out);
}
}
-#if 0
} else if (data) {
- ly_errno = 0;
/* prepare operational datastore when specified for RPC/Notification */
if (oper_file) {
@@ -817,6 +812,7 @@
for (data_item = data, data_prev = NULL; data_item; data_prev = data_item, data_item = data_item->next) {
/* parse data file - via LYD_OPT_TRUSTED postpone validation when all data are loaded and merged */
+#if 0
if (autodetection) {
/* erase option not covered by LYD_OPT_TYPEMASK, but used according to the type */
options_parser &= ~LYD_OPT_DATA_NO_YANGLIB;
@@ -974,12 +970,15 @@
continue;
}
} else {
+#else
+ {
+#endif
data_item->tree = lyd_parse_path(ctx, data_item->filename, data_item->format, options_parser, oper);
}
- if (ly_errno) {
+ if (ly_err_first(ctx)) {
goto cleanup;
}
-
+#if 0
if (merge && data != data_item) {
if (!data->tree) {
data->tree = data_item->tree;
@@ -992,8 +991,9 @@
}
data_item->tree = NULL;
}
+#endif
}
-
+#if 0
if (merge) {
/* validate the merged data tree, do not trust the input, invalidate all the data first */
LY_TREE_FOR(data->tree, subroot) {
@@ -1025,13 +1025,14 @@
goto cleanup;
}
}
-
+#endif
/* print only if data output format specified */
if (outformat_d) {
for (data_item = data; data_item; data_item = data_item->next) {
if (!merge && verbose >= 2) {
fprintf(stdout, "File %s:\n", data_item->filename);
}
+#if 0
if (outformat_d == LYD_XML && envelope) {
switch (data_item->type) {
case LYD_OPT_DATA:
@@ -1065,8 +1066,10 @@
fprintf(out, "<action xmlns=\"urn:ietf:params:xml:ns:yang:1\">\n");
}
}
- lyd_print_file(out, (data_item->type == LYD_OPT_RPCREPLY) ? data_item->tree->child : data_item->tree,
- outformat_d, LYP_WITHSIBLINGS | LYP_FORMAT | options_dflt);
+#endif
+ lyd_print_file(out, (data_item->type == LYD_OPT_RPCREPLY) ? lyd_node_children(data_item->tree) : data_item->tree,
+ outformat_d, LYDP_WITHSIBLINGS | LYDP_FORMAT /* TODO defaults | options_dflt */);
+#if 0
if (envelope_s) {
if (data_item->type == LYD_OPT_RPC && data_item->tree->schema->nodetype != LYS_RPC) {
fprintf(out, "</action>\n");
@@ -1077,9 +1080,9 @@
/* stop after first item */
break;
}
+#endif
}
}
-#endif
}
#if 0
if (list) {
@@ -1099,15 +1102,12 @@
free(feat[i]);
}
free(feat);
-#if 0
for (; data; data = data_item) {
data_item = data->next;
- lyxml_free(ctx, data->xml);
- lyd_free_withsiblings(data->tree);
+ lyd_free_all(data->tree);
free(data);
}
- lyd_free_withsiblings(oper);
-#endif
+ lyd_free_all(oper);
ly_ctx_destroy(ctx, NULL);
return ret;