blob: a78f06be1966ec7e6f309f540398f728bdf8f61e [file] [log] [blame]
Radek Krejcie9f13b12020-11-09 17:42:04 +01001/**
2 * @file cmd_data.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @author Radek Krejci <rkrejci@cesnet.cz>
5 * @brief 'data' command of the libyang's yanglint tool.
6 *
7 * Copyright (c) 2015-2020 CESNET, z.s.p.o.
8 *
9 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * https://opensource.org/licenses/BSD-3-Clause
14 */
15
16#define _GNU_SOURCE
17
18#include "cmd.h"
19
20#include <errno.h>
21#include <getopt.h>
22#include <stdint.h>
23#include <stdio.h>
24#include <string.h>
25#include <strings.h>
26
27#include "libyang.h"
28
29#include "common.h"
30
31void
32cmd_data_help(void)
33{
34 printf("Usage: data [-ems] [-t TYPE]\n"
35 " [-f FORMAT] [-d DEFAULTS] [-o OUTFILE] <data1> ...\n"
Radek Krejci6784a4e2020-12-09 14:23:05 +010036 " data [-s] -t (rpc | notif | reply) [-O FILE]\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +010037 " [-f FORMAT] [-d DEFAULTS] [-o OUTFILE] <data1> ...\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +010038 " data [-es] [-t TYPE] -x XPATH [-o OUTFILE] <data1> ...\n"
Radek Krejci6784a4e2020-12-09 14:23:05 +010039 " Parse, validate and optionally print data instances\n\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +010040
41 " -t TYPE, --type=TYPE\n"
42 " Specify data tree type in the input data file(s):\n"
43 " data - Complete datastore with status data (default type).\n"
44 " config - Configuration datastore (without status data).\n"
45 " get - Result of the NETCONF <get> operation.\n"
46 " getconfig - Result of the NETCONF <get-config> operation.\n"
47 " edit - Content of the NETCONF <edit-config> operation.\n"
48 " rpc - Content of the NETCONF <rpc> message, defined as YANG's\n"
49 " RPC/Action input statement.\n"
Radek Krejci6784a4e2020-12-09 14:23:05 +010050 " reply - Reply to the RPC/Action. Note that the reply data are\n"
51 " expected inside a container representing the original\n"
52 " RPC/Action. This is necessary to identify appropriate\n"
53 " data definitions in the schema module.\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +010054 " notif - Notification instance (content of the <notification>\n"
55 " element without <eventTime>).\n\n"
56
57 " -e, --present Validate only with the schema modules whose data actually\n"
58 " exist in the provided input data files. Takes effect only\n"
59 " with the 'data' or 'config' TYPEs. Used to avoid requiring\n"
60 " mandatory nodes from modules which data are not present in the\n"
61 " provided input data files.\n"
62 " -m, --merge Merge input data files into a single tree and validate at\n"
63 " once.The option has effect only for 'data' and 'config' TYPEs.\n"
64 " In case of using -x option, the data are always merged.\n"
Michal Vaskoc431a0a2021-01-25 14:31:58 +010065 " -n, --not-strict\n"
66 " Do not require strict data parsing (silently skip unknown data),\n"
67 " has no effect for schemas.\n\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +010068 " -O FILE, --operational=FILE\n"
69 " Provide optional data to extend validation of the 'rpc',\n"
70 " 'reply' or 'notif' TYPEs. The FILE is supposed to contain\n"
71 " the :running configuration datastore and state data\n"
72 " (operational datastore) referenced from the RPC/Notification.\n\n"
73
74 " -f FORMAT, --format=FORMAT\n"
75 " Print the data in one of the following formats:\n"
76 " xml, json, lyb\n"
77 " Note that the LYB format requires the -o option specified.\n"
78 " -d MODE, --default=MODE\n"
79 " Print data with default values, according to the MODE\n"
80 " (to print attributes, ietf-netconf-with-defaults model\n"
81 " must be loaded):\n"
82 " all - Add missing default nodes.\n"
83 " all-tagged - Add missing default nodes and mark all the default\n"
84 " nodes with the attribute.\n"
85 " trim - Remove all nodes with a default value.\n"
86 " implicit-tagged - Add missing nodes and mark them with the attribute.\n"
87 " -o OUTFILE, --output=OUTFILE\n"
88 " Write the output to OUTFILE instead of stdout.\n\n"
89
90 " -x XPATH, --xpath=XPATH\n"
91 " Evaluate XPATH expression and print the nodes satysfying the.\n"
92 " expression. The output format is specific and the option cannot\n"
93 " be combined with the -f and -d options. Also all the data\n"
94 " inputs are merged into a single data tree where the expression\n"
95 " is evaluated, so the -m option is always set implicitly.\n\n");
96
97}
98
Radek Krejcie9f13b12020-11-09 17:42:04 +010099void
100cmd_data(struct ly_ctx **ctx, const char *cmdline)
101{
102 int argc = 0;
103 char **argv = NULL;
104 int opt, opt_index;
105 struct option options[] = {
106 {"defaults", required_argument, NULL, 'd'},
107 {"present", no_argument, NULL, 'e'},
108 {"format", required_argument, NULL, 'f'},
109 {"help", no_argument, NULL, 'h'},
110 {"merge", no_argument, NULL, 'm'},
111 {"output", required_argument, NULL, 'o'},
112 {"operational", required_argument, NULL, 'O'},
Radek Krejcie9f13b12020-11-09 17:42:04 +0100113 {"strict", no_argument, NULL, 's'},
114 {"type", required_argument, NULL, 't'},
115 {"xpath", required_argument, NULL, 'x'},
116 {NULL, 0, NULL, 0}
117 };
118
119 uint8_t data_merge = 0;
120 uint32_t options_print = 0;
121 uint32_t options_parse = 0;
122 uint32_t options_validate = 0;
123 uint8_t data_type = 0;
124 uint8_t data_type_set = 0;
125 LYD_FORMAT format = LYD_UNKNOWN;
126 struct ly_out *out = NULL;
127 struct cmdline_file *operational = NULL;
128 struct ly_set inputs = {0};
Radek Krejcie9f13b12020-11-09 17:42:04 +0100129 struct ly_set xpaths = {0};
130
131 if (parse_cmdline(cmdline, &argc, &argv)) {
132 goto cleanup;
133 }
134
135 while ((opt = getopt_long(argc, argv, "d:ef:hmo:O:r:st:x:", options, &opt_index)) != -1) {
136 switch (opt) {
137 case 'd': /* --default */
138 if (!strcasecmp(optarg, "all")) {
139 options_print = (options_print & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_ALL;
140 } else if (!strcasecmp(optarg, "all-tagged")) {
141 options_print = (options_print & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_ALL_TAG;
142 } else if (!strcasecmp(optarg, "trim")) {
143 options_print = (options_print & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_TRIM;
144 } else if (!strcasecmp(optarg, "implicit-tagged")) {
145 options_print = (options_print & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_IMPL_TAG;
146 } else {
147 YLMSG_E("Unknown default mode %s\n", optarg);
148 cmd_data_help();
149 goto cleanup;
150 }
151 break;
152 case 'f': /* --format */
153 if (!strcasecmp(optarg, "xml")) {
154 format = LYD_XML;
155 } else if (!strcasecmp(optarg, "json")) {
156 format = LYD_JSON;
157 } else if (!strcasecmp(optarg, "lyb")) {
158 format = LYD_LYB;
159 } else {
160 YLMSG_E("Unknown output format %s\n", optarg);
161 cmd_data_help();
162 goto cleanup;
163 }
164 break;
165 case 'o': /* --output */
166 if (out) {
167 YLMSG_E("Only a single output can be specified.\n");
168 goto cleanup;
169 } else {
170 if (ly_out_new_filepath(optarg, &out)) {
171 YLMSG_E("Unable open output file %s (%s)\n", optarg, strerror(errno));
172 goto cleanup;
173 }
174 }
175 break;
176 case 'O': { /* --operational */
177 struct ly_in *in;
178 LYD_FORMAT f;
179
180 if (operational) {
181 YLMSG_E("The operational datastore (-O) cannot be set multiple times.\n");
182 goto cleanup;
183 }
184 if (get_input(optarg, NULL, &f, &in)) {
185 goto cleanup;
186 }
187 operational = fill_cmdline_file(NULL, in, optarg, f);
188 break;
189 } /* case 'O' */
Radek Krejcie9f13b12020-11-09 17:42:04 +0100190
191 case 'e': /* --present */
192 options_validate |= LYD_VALIDATE_PRESENT;
193 break;
194 case 'm': /* --merge */
195 data_merge = 1;
196 break;
197 case 's': /* --strict */
198 options_parse |= LYD_PARSE_STRICT;
199 break;
200 case 't': /* --type */
201 if (data_type_set) {
202 YLMSG_E("The data type (-t) cannot be set multiple times.\n");
203 goto cleanup;
204 }
205
206 if (!strcasecmp(optarg, "config")) {
207 options_parse |= LYD_PARSE_NO_STATE;
208 } else if (!strcasecmp(optarg, "get")) {
209 options_parse |= LYD_PARSE_ONLY;
210 } else if (!strcasecmp(optarg, "getconfig") || !strcasecmp(optarg, "get-config")) {
211 options_parse |= LYD_PARSE_ONLY | LYD_PARSE_NO_STATE;
212 } else if (!strcasecmp(optarg, "edit")) {
213 options_parse |= LYD_PARSE_ONLY;
214 } else if (!strcasecmp(optarg, "rpc") || !strcasecmp(optarg, "action")) {
215 data_type = LYD_VALIDATE_OP_RPC;
216 } else if (!strcasecmp(optarg, "reply") || !strcasecmp(optarg, "rpcreply")) {
217 data_type = LYD_VALIDATE_OP_REPLY;
218 } else if (!strcasecmp(optarg, "notif") || !strcasecmp(optarg, "notification")) {
219 data_type = LYD_VALIDATE_OP_NOTIF;
220 } else if (!strcasecmp(optarg, "data")) {
221 /* default option */
222 } else {
223 YLMSG_E("Unknown data tree type %s.\n", optarg);
224 cmd_data_help();
225 goto cleanup;
226 }
227
228 data_type_set = 1;
229 break;
230
231 case 'x': /* --xpath */
232 if (ly_set_add(&xpaths, optarg, 0, NULL)) {
233 YLMSG_E("Storing XPath \"%s\" failed.\n", optarg);
234 goto cleanup;
235 }
236 break;
237
238 case 'h': /* --help */
239 cmd_data_help();
240 goto cleanup;
241 default:
242 YLMSG_E("Unknown option.\n");
243 goto cleanup;
244 }
245 }
246
247 if (optind == argc) {
248 YLMSG_E("Missing the data file to process.\n");
249 goto cleanup;
250 }
251
252 if (data_merge) {
253 if (data_type || (options_parse & LYD_PARSE_ONLY)) {
254 /* switch off the option, incompatible input data type */
255 data_merge = 0;
256 } else {
257 /* postpone validation after the merge of all the input data */
258 options_parse |= LYD_PARSE_ONLY;
259 }
260 } else if (xpaths.count) {
261 data_merge = 1;
262 }
263
264 if (xpaths.count && format) {
265 YLMSG_E("The --format option cannot be combined with --xpath option.\n");
266 cmd_data_help();
267 goto cleanup;
268 }
269
270 if (operational && !data_type) {
271 YLMSG_W("Operational datastore takes effect only with RPCs/Actions/Replies/Notifications input data types.\n");
272 free_cmdline_file(operational);
273 operational = NULL;
274 }
275
276 /* process input data files provided as standalone command line arguments */
Radek Krejci6784a4e2020-12-09 14:23:05 +0100277 for (int i = 0; i < argc - optind; i++) {
278 struct ly_in *in;
279 LYD_FORMAT format = LYD_UNKNOWN;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100280
Radek Krejci6784a4e2020-12-09 14:23:05 +0100281 if (get_input(argv[optind + i], NULL, &format, &in)) {
282 goto cleanup;
283 }
284
Michal Vasko48d5a752021-01-25 14:18:25 +0100285 if (!fill_cmdline_file(&inputs, in, argv[optind + i], format)) {
Radek Krejci6784a4e2020-12-09 14:23:05 +0100286 ly_in_free(in, 1);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100287 goto cleanup;
288 }
289 }
290
291 /* default output stream */
292 if (!out) {
293 if (ly_out_new_file(stdout, &out)) {
294 YLMSG_E("Unable to set stdout as output.\n");
295 goto cleanup;
296 }
297 }
298
299 /* parse, validate and print data */
300 if (process_data(*ctx, data_type, data_merge, format, out,
301 options_parse, options_validate, options_print,
Radek Krejci6784a4e2020-12-09 14:23:05 +0100302 operational, &inputs, &xpaths)) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100303 goto cleanup;
304 }
305
306cleanup:
307 ly_out_free(out, NULL, 0);
308 ly_set_erase(&inputs, free_cmdline_file);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100309 ly_set_erase(&xpaths, NULL);
310 free_cmdline_file(operational);
311 free_cmdline(argv);
312}