blob: dba9705b118ad112a935518ab538c6dc78578aa0 [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"
36 " data [-s] -t (rpc | notif) [-O FILE]\n"
37 " [-f FORMAT] [-d DEFAULTS] [-o OUTFILE] <data1> ...\n"
38 " data [-s] -t reply -r <request-file> [-O FILE]\n"
39 " [-f FORMAT] [-d DEFAULTS] [-o OUTFILE] <data1> ...\n"
40 " data [-s] -t reply [-O FILE]\n"
41 " [-f FORMAT] [-d DEFAULTS] [-o OUTFILE] <data1> <request1> ...\n"
42 " data [-es] [-t TYPE] -x XPATH [-o OUTFILE] <data1> ...\n"
43 " Parse, validate and optionaly print data instances\n\n"
44
45 " -t TYPE, --type=TYPE\n"
46 " Specify data tree type in the input data file(s):\n"
47 " data - Complete datastore with status data (default type).\n"
48 " config - Configuration datastore (without status data).\n"
49 " get - Result of the NETCONF <get> operation.\n"
50 " getconfig - Result of the NETCONF <get-config> operation.\n"
51 " edit - Content of the NETCONF <edit-config> operation.\n"
52 " rpc - Content of the NETCONF <rpc> message, defined as YANG's\n"
53 " RPC/Action input statement.\n"
54 " reply - Reply to the RPC/Action. Besides the reply itself,\n"
55 " yanglint(1) requires information about the request for\n"
56 " the reply. The request (RPC/Action) can be provide as\n"
57 " the --request option or as another input data <file>\n"
58 " provided right after the reply data <file> and\n"
59 " containing complete RPC/Action for the reply.\n"
60 " notif - Notification instance (content of the <notification>\n"
61 " element without <eventTime>).\n\n"
62
63 " -e, --present Validate only with the schema modules whose data actually\n"
64 " exist in the provided input data files. Takes effect only\n"
65 " with the 'data' or 'config' TYPEs. Used to avoid requiring\n"
66 " mandatory nodes from modules which data are not present in the\n"
67 " provided input data files.\n"
68 " -m, --merge Merge input data files into a single tree and validate at\n"
69 " once.The option has effect only for 'data' and 'config' TYPEs.\n"
70 " In case of using -x option, the data are always merged.\n"
71 " -s, --strict Strict data parsing (do not skip unknown data), has no effect\n"
72 " for schemas.\n"
73 " -r PATH, --request=PATH\n"
74 " The alternative way of providing request information for the\n"
75 " '--type=reply'. The PATH is the XPath subset described in\n"
76 " documentation as Path format. It is required to point to the\n"
77 " RPC or Action in the schema which is supposed to be a request\n"
78 " for the reply(ies) being parsed from the input data files.\n"
79 " In case of multiple input data files, the 'request' option can\n"
80 " be set once for all the replies or multiple times each for the\n"
81 " respective input data file.\n"
82 " -O FILE, --operational=FILE\n"
83 " Provide optional data to extend validation of the 'rpc',\n"
84 " 'reply' or 'notif' TYPEs. The FILE is supposed to contain\n"
85 " the :running configuration datastore and state data\n"
86 " (operational datastore) referenced from the RPC/Notification.\n\n"
87
88 " -f FORMAT, --format=FORMAT\n"
89 " Print the data in one of the following formats:\n"
90 " xml, json, lyb\n"
91 " Note that the LYB format requires the -o option specified.\n"
92 " -d MODE, --default=MODE\n"
93 " Print data with default values, according to the MODE\n"
94 " (to print attributes, ietf-netconf-with-defaults model\n"
95 " must be loaded):\n"
96 " all - Add missing default nodes.\n"
97 " all-tagged - Add missing default nodes and mark all the default\n"
98 " nodes with the attribute.\n"
99 " trim - Remove all nodes with a default value.\n"
100 " implicit-tagged - Add missing nodes and mark them with the attribute.\n"
101 " -o OUTFILE, --output=OUTFILE\n"
102 " Write the output to OUTFILE instead of stdout.\n\n"
103
104 " -x XPATH, --xpath=XPATH\n"
105 " Evaluate XPATH expression and print the nodes satysfying the.\n"
106 " expression. The output format is specific and the option cannot\n"
107 " be combined with the -f and -d options. Also all the data\n"
108 " inputs are merged into a single data tree where the expression\n"
109 " is evaluated, so the -m option is always set implicitly.\n\n");
110
111}
112
113static int
114prepare_inputs(int argc, char *argv[], struct ly_set *requests, struct ly_set *inputs)
115{
116 struct ly_in *in;
117 uint8_t request_expected = 0;
118
119 for (int i = 0; i < argc - optind; i++) {
120 LYD_FORMAT format = LYD_UNKNOWN;
121 struct cmdline_file *rec;
122
123 if (get_input(argv[optind + i], NULL, &format, &in)) {
124 return -1;
125 }
126
127 if (request_expected) {
128 if (fill_cmdline_file(requests, in, argv[optind + i], format)) {
129 ly_in_free(in, 1);
130 return -1;
131 }
132
133 request_expected = 0;
134 } else {
135 rec = fill_cmdline_file(inputs, in, argv[optind + i], format);
136 if (!rec) {
137 ly_in_free(in, 1);
138 return -1;
139 }
140
141 if (requests) {
142 /* requests for the replies are expected in another input file */
143 if (++i == argc - optind) {
144 /* there is no such file */
145 YLMSG_E("Missing request input file for the reply input file %s.\n", rec->path);
146 return -1;
147 }
148
149 request_expected = 1;
150 }
151 }
152 }
153
154 if (request_expected) {
155 YLMSG_E("Missing request input file for the reply input file %s.\n", argv[argc - optind - 1]);
156 return -1;
157 }
158
159 return 0;
160}
161
162void
163cmd_data(struct ly_ctx **ctx, const char *cmdline)
164{
165 int argc = 0;
166 char **argv = NULL;
167 int opt, opt_index;
168 struct option options[] = {
169 {"defaults", required_argument, NULL, 'd'},
170 {"present", no_argument, NULL, 'e'},
171 {"format", required_argument, NULL, 'f'},
172 {"help", no_argument, NULL, 'h'},
173 {"merge", no_argument, NULL, 'm'},
174 {"output", required_argument, NULL, 'o'},
175 {"operational", required_argument, NULL, 'O'},
176 {"request", required_argument, NULL, 'r'},
177 {"strict", no_argument, NULL, 's'},
178 {"type", required_argument, NULL, 't'},
179 {"xpath", required_argument, NULL, 'x'},
180 {NULL, 0, NULL, 0}
181 };
182
183 uint8_t data_merge = 0;
184 uint32_t options_print = 0;
185 uint32_t options_parse = 0;
186 uint32_t options_validate = 0;
187 uint8_t data_type = 0;
188 uint8_t data_type_set = 0;
189 LYD_FORMAT format = LYD_UNKNOWN;
190 struct ly_out *out = NULL;
191 struct cmdline_file *operational = NULL;
192 struct ly_set inputs = {0};
193 struct ly_set requests = {0};
194 struct ly_set request_paths = {0};
195 struct ly_set xpaths = {0};
196
197 if (parse_cmdline(cmdline, &argc, &argv)) {
198 goto cleanup;
199 }
200
201 while ((opt = getopt_long(argc, argv, "d:ef:hmo:O:r:st:x:", options, &opt_index)) != -1) {
202 switch (opt) {
203 case 'd': /* --default */
204 if (!strcasecmp(optarg, "all")) {
205 options_print = (options_print & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_ALL;
206 } else if (!strcasecmp(optarg, "all-tagged")) {
207 options_print = (options_print & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_ALL_TAG;
208 } else if (!strcasecmp(optarg, "trim")) {
209 options_print = (options_print & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_TRIM;
210 } else if (!strcasecmp(optarg, "implicit-tagged")) {
211 options_print = (options_print & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_IMPL_TAG;
212 } else {
213 YLMSG_E("Unknown default mode %s\n", optarg);
214 cmd_data_help();
215 goto cleanup;
216 }
217 break;
218 case 'f': /* --format */
219 if (!strcasecmp(optarg, "xml")) {
220 format = LYD_XML;
221 } else if (!strcasecmp(optarg, "json")) {
222 format = LYD_JSON;
223 } else if (!strcasecmp(optarg, "lyb")) {
224 format = LYD_LYB;
225 } else {
226 YLMSG_E("Unknown output format %s\n", optarg);
227 cmd_data_help();
228 goto cleanup;
229 }
230 break;
231 case 'o': /* --output */
232 if (out) {
233 YLMSG_E("Only a single output can be specified.\n");
234 goto cleanup;
235 } else {
236 if (ly_out_new_filepath(optarg, &out)) {
237 YLMSG_E("Unable open output file %s (%s)\n", optarg, strerror(errno));
238 goto cleanup;
239 }
240 }
241 break;
242 case 'O': { /* --operational */
243 struct ly_in *in;
244 LYD_FORMAT f;
245
246 if (operational) {
247 YLMSG_E("The operational datastore (-O) cannot be set multiple times.\n");
248 goto cleanup;
249 }
250 if (get_input(optarg, NULL, &f, &in)) {
251 goto cleanup;
252 }
253 operational = fill_cmdline_file(NULL, in, optarg, f);
254 break;
255 } /* case 'O' */
256 case 'r': /* --request */
257 if (ly_set_add(&request_paths, optarg, 0, NULL)) {
258 YLMSG_E("Storing request path \"%s\" failed.\n", optarg);
259 goto cleanup;
260 }
261 break;
262
263 case 'e': /* --present */
264 options_validate |= LYD_VALIDATE_PRESENT;
265 break;
266 case 'm': /* --merge */
267 data_merge = 1;
268 break;
269 case 's': /* --strict */
270 options_parse |= LYD_PARSE_STRICT;
271 break;
272 case 't': /* --type */
273 if (data_type_set) {
274 YLMSG_E("The data type (-t) cannot be set multiple times.\n");
275 goto cleanup;
276 }
277
278 if (!strcasecmp(optarg, "config")) {
279 options_parse |= LYD_PARSE_NO_STATE;
280 } else if (!strcasecmp(optarg, "get")) {
281 options_parse |= LYD_PARSE_ONLY;
282 } else if (!strcasecmp(optarg, "getconfig") || !strcasecmp(optarg, "get-config")) {
283 options_parse |= LYD_PARSE_ONLY | LYD_PARSE_NO_STATE;
284 } else if (!strcasecmp(optarg, "edit")) {
285 options_parse |= LYD_PARSE_ONLY;
286 } else if (!strcasecmp(optarg, "rpc") || !strcasecmp(optarg, "action")) {
287 data_type = LYD_VALIDATE_OP_RPC;
288 } else if (!strcasecmp(optarg, "reply") || !strcasecmp(optarg, "rpcreply")) {
289 data_type = LYD_VALIDATE_OP_REPLY;
290 } else if (!strcasecmp(optarg, "notif") || !strcasecmp(optarg, "notification")) {
291 data_type = LYD_VALIDATE_OP_NOTIF;
292 } else if (!strcasecmp(optarg, "data")) {
293 /* default option */
294 } else {
295 YLMSG_E("Unknown data tree type %s.\n", optarg);
296 cmd_data_help();
297 goto cleanup;
298 }
299
300 data_type_set = 1;
301 break;
302
303 case 'x': /* --xpath */
304 if (ly_set_add(&xpaths, optarg, 0, NULL)) {
305 YLMSG_E("Storing XPath \"%s\" failed.\n", optarg);
306 goto cleanup;
307 }
308 break;
309
310 case 'h': /* --help */
311 cmd_data_help();
312 goto cleanup;
313 default:
314 YLMSG_E("Unknown option.\n");
315 goto cleanup;
316 }
317 }
318
319 if (optind == argc) {
320 YLMSG_E("Missing the data file to process.\n");
321 goto cleanup;
322 }
323
324 if (data_merge) {
325 if (data_type || (options_parse & LYD_PARSE_ONLY)) {
326 /* switch off the option, incompatible input data type */
327 data_merge = 0;
328 } else {
329 /* postpone validation after the merge of all the input data */
330 options_parse |= LYD_PARSE_ONLY;
331 }
332 } else if (xpaths.count) {
333 data_merge = 1;
334 }
335
336 if (xpaths.count && format) {
337 YLMSG_E("The --format option cannot be combined with --xpath option.\n");
338 cmd_data_help();
339 goto cleanup;
340 }
341
342 if (operational && !data_type) {
343 YLMSG_W("Operational datastore takes effect only with RPCs/Actions/Replies/Notifications input data types.\n");
344 free_cmdline_file(operational);
345 operational = NULL;
346 }
347
348 /* process input data files provided as standalone command line arguments */
349 if (prepare_inputs(argc, argv, ((data_type == LYD_VALIDATE_OP_REPLY) && !request_paths.count) ? &requests : NULL,
350 &inputs)) {
351 goto cleanup;
352 }
353
354 if (data_type == LYD_VALIDATE_OP_REPLY) {
355 if (check_request_paths(*ctx, &request_paths, &inputs)) {
356 goto cleanup;
357 }
358 }
359
360 /* default output stream */
361 if (!out) {
362 if (ly_out_new_file(stdout, &out)) {
363 YLMSG_E("Unable to set stdout as output.\n");
364 goto cleanup;
365 }
366 }
367
368 /* parse, validate and print data */
369 if (process_data(*ctx, data_type, data_merge, format, out,
370 options_parse, options_validate, options_print,
371 operational, &inputs, &request_paths, &requests, &xpaths)) {
372 goto cleanup;
373 }
374
375cleanup:
376 ly_out_free(out, NULL, 0);
377 ly_set_erase(&inputs, free_cmdline_file);
378 ly_set_erase(&requests, free_cmdline_file);
379 ly_set_erase(&request_paths, NULL);
380 ly_set_erase(&xpaths, NULL);
381 free_cmdline_file(operational);
382 free_cmdline(argv);
383}