blob: 8e068fe21596a802c99993fe40e29156dc1d56a7 [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
aPiecekb073f6a2023-04-28 15:23:25 +020031static void
32cmd_data_help_header(void)
Radek Krejcie9f13b12020-11-09 17:42:04 +010033{
Michal Vaskod3b10542021-02-03 11:31:16 +010034 printf("Usage: data [-emn] [-t TYPE]\n"
35 " [-F FORMAT] [-f FORMAT] [-d DEFAULTS] [-o OUTFILE] <data1> ...\n"
36 " data [-n] -t (rpc | notif | reply) [-O FILE]\n"
37 " [-F FORMAT] [-f FORMAT] [-d DEFAULTS] [-o OUTFILE] <data1> ...\n"
38 " data [-en] [-t TYPE] [-F FORMAT] -x XPATH [-o OUTFILE] <data1> ...\n"
aPiecekb073f6a2023-04-28 15:23:25 +020039 " Parse, validate and optionally print data instances\n");
40}
Radek Krejcie9f13b12020-11-09 17:42:04 +010041
aPiecekb073f6a2023-04-28 15:23:25 +020042static void
43cmd_data_help_type(void)
44{
45 printf(" -t TYPE, --type=TYPE\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +010046 " 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"
aPiecek860e34f2023-04-28 10:01:32 +020054 " nc-rpc - Similar to 'rpc' but expect and check also the NETCONF\n"
55 " envelopes <rpc> or <action>.\n"
Radek Krejci6784a4e2020-12-09 14:23:05 +010056 " reply - Reply to the RPC/Action. Note that the reply data are\n"
57 " expected inside a container representing the original\n"
58 " RPC/Action. This is necessary to identify appropriate\n"
59 " data definitions in the schema module.\n"
aPiecek079bcde2023-05-05 11:48:25 +020060 " nc-reply - Similar to 'reply' but expect and check also the NETCONF\n"
61 " envelope <rpc-reply> with output data nodes as direct\n"
62 " descendants. The original RPC/action invocation is expected\n"
63 " in a separate parameter '-R' and is parsed as 'nc-rpc'.\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +010064 " notif - Notification instance (content of the <notification>\n"
aPiecek6d2c1e72023-04-28 15:54:48 +020065 " element without <eventTime>).\n"
66 " nc-notif - Similar to 'notif' but expect and check also the NETCONF\n"
67 " envelope <notification> with element <eventTime> and its\n"
68 " sibling as the actual notification.\n");
aPiecekb073f6a2023-04-28 15:23:25 +020069}
Radek Krejcie9f13b12020-11-09 17:42:04 +010070
aPiecekb073f6a2023-04-28 15:23:25 +020071static void
72cmd_data_help_format(void)
73{
74 printf(" -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}
79
80static void
81cmd_data_help_in_format(void)
82{
83 printf(" -F FORMAT, --in-format=FORMAT\n"
84 " Load the data in one of the following formats:\n"
85 " xml, json, lyb\n"
86 " If input format not specified, it is detected from the file extension.\n");
87}
88
89static void
90cmd_data_help_default(void)
91{
92 printf(" -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}
102
103static void
104cmd_data_help_xpath(void)
105{
106 printf(" -x XPATH, --xpath=XPATH\n"
aPiecek41955272023-05-10 16:10:17 +0200107 " Evaluate XPATH expression and print the nodes satisfying the\n"
aPiecekb073f6a2023-04-28 15:23:25 +0200108 " expression. The output format is specific and the option cannot\n"
109 " be combined with the -f and -d options. Also all the data\n"
110 " inputs are merged into a single data tree where the expression\n"
111 " is evaluated, so the -m option is always set implicitly.\n");
112}
113
114void
115cmd_data_help(void)
116{
117 cmd_data_help_header();
118 printf("\n");
119 cmd_data_help_type();
120 printf(" -e, --present Validate only with the schema modules whose data actually\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +0100121 " exist in the provided input data files. Takes effect only\n"
122 " with the 'data' or 'config' TYPEs. Used to avoid requiring\n"
123 " mandatory nodes from modules which data are not present in the\n"
124 " provided input data files.\n"
125 " -m, --merge Merge input data files into a single tree and validate at\n"
126 " once.The option has effect only for 'data' and 'config' TYPEs.\n"
127 " In case of using -x option, the data are always merged.\n"
Michal Vaskoc431a0a2021-01-25 14:31:58 +0100128 " -n, --not-strict\n"
129 " Do not require strict data parsing (silently skip unknown data),\n"
aPiecekb073f6a2023-04-28 15:23:25 +0200130 " has no effect for schemas.\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +0100131 " -O FILE, --operational=FILE\n"
132 " Provide optional data to extend validation of the 'rpc',\n"
133 " 'reply' or 'notif' TYPEs. The FILE is supposed to contain\n"
aPieceka40764b2023-04-27 15:34:56 +0200134 " the operational datastore referenced from the operation.\n"
135 " In case of a nested notification or action, its parent\n"
aPiecek079bcde2023-05-05 11:48:25 +0200136 " existence is also checked in these operational data.\n"
137 " -R FILE, --reply-rpc=FILE\n"
138 " Provide source RPC for parsing of the 'nc-reply' TYPE. The FILE\n"
139 " is supposed to contain the source 'nc-rpc' operation of the reply.\n");
aPiecekb073f6a2023-04-28 15:23:25 +0200140 cmd_data_help_format();
141 cmd_data_help_in_format();
142 printf(" -o OUTFILE, --output=OUTFILE\n"
143 " Write the output to OUTFILE instead of stdout.\n");
144 cmd_data_help_xpath();
145 printf("\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100146}
147
Radek Krejcie9f13b12020-11-09 17:42:04 +0100148void
149cmd_data(struct ly_ctx **ctx, const char *cmdline)
150{
151 int argc = 0;
152 char **argv = NULL;
153 int opt, opt_index;
154 struct option options[] = {
Michal Vasko667ce6b2021-01-25 15:00:27 +0100155 {"defaults", required_argument, NULL, 'd'},
156 {"present", no_argument, NULL, 'e'},
157 {"format", required_argument, NULL, 'f'},
Michal Vaskod3b10542021-02-03 11:31:16 +0100158 {"in-format", required_argument, NULL, 'F'},
Michal Vasko667ce6b2021-01-25 15:00:27 +0100159 {"help", no_argument, NULL, 'h'},
160 {"merge", no_argument, NULL, 'm'},
161 {"output", required_argument, NULL, 'o'},
Radek Krejcie9f13b12020-11-09 17:42:04 +0100162 {"operational", required_argument, NULL, 'O'},
aPiecek079bcde2023-05-05 11:48:25 +0200163 {"reply-rpc", required_argument, NULL, 'R'},
Michal Vasko667ce6b2021-01-25 15:00:27 +0100164 {"not-strict", no_argument, NULL, 'n'},
165 {"type", required_argument, NULL, 't'},
166 {"xpath", required_argument, NULL, 'x'},
Radek Krejcie9f13b12020-11-09 17:42:04 +0100167 {NULL, 0, NULL, 0}
168 };
169
170 uint8_t data_merge = 0;
171 uint32_t options_print = 0;
Michal Vasko667ce6b2021-01-25 15:00:27 +0100172 uint32_t options_parse = YL_DEFAULT_DATA_PARSE_OPTIONS;
Michal Vasko05eaf832023-02-10 09:21:46 +0100173 uint32_t options_validate = YL_DEFAULT_DATA_VALIDATE_OPTIONS;
Michal Vaskoe0665742021-02-11 11:08:44 +0100174 enum lyd_type data_type = 0;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100175 uint8_t data_type_set = 0;
Michal Vaskod3b10542021-02-03 11:31:16 +0100176 LYD_FORMAT outformat = LYD_UNKNOWN;
177 LYD_FORMAT informat = LYD_UNKNOWN;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100178 struct ly_out *out = NULL;
179 struct cmdline_file *operational = NULL;
aPiecek079bcde2023-05-05 11:48:25 +0200180 struct cmdline_file *reply_rpc = NULL;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100181 struct ly_set inputs = {0};
Radek Krejcie9f13b12020-11-09 17:42:04 +0100182 struct ly_set xpaths = {0};
183
184 if (parse_cmdline(cmdline, &argc, &argv)) {
185 goto cleanup;
186 }
187
aPiecek15cc4cf2023-04-17 15:23:21 +0200188 while ((opt = getopt_long(argc, argv, commands[CMD_DATA].optstring, options, &opt_index)) != -1) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100189 switch (opt) {
190 case 'd': /* --default */
191 if (!strcasecmp(optarg, "all")) {
192 options_print = (options_print & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_ALL;
193 } else if (!strcasecmp(optarg, "all-tagged")) {
194 options_print = (options_print & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_ALL_TAG;
195 } else if (!strcasecmp(optarg, "trim")) {
196 options_print = (options_print & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_TRIM;
197 } else if (!strcasecmp(optarg, "implicit-tagged")) {
198 options_print = (options_print & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_IMPL_TAG;
199 } else {
200 YLMSG_E("Unknown default mode %s\n", optarg);
aPiecekb073f6a2023-04-28 15:23:25 +0200201 cmd_data_help_default();
Radek Krejcie9f13b12020-11-09 17:42:04 +0100202 goto cleanup;
203 }
204 break;
205 case 'f': /* --format */
206 if (!strcasecmp(optarg, "xml")) {
Michal Vaskod3b10542021-02-03 11:31:16 +0100207 outformat = LYD_XML;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100208 } else if (!strcasecmp(optarg, "json")) {
Michal Vaskod3b10542021-02-03 11:31:16 +0100209 outformat = LYD_JSON;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100210 } else if (!strcasecmp(optarg, "lyb")) {
Michal Vaskod3b10542021-02-03 11:31:16 +0100211 outformat = LYD_LYB;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100212 } else {
213 YLMSG_E("Unknown output format %s\n", optarg);
aPiecekb073f6a2023-04-28 15:23:25 +0200214 cmd_data_help_format();
Radek Krejcie9f13b12020-11-09 17:42:04 +0100215 goto cleanup;
216 }
217 break;
Michal Vaskod3b10542021-02-03 11:31:16 +0100218 case 'F': /* --in-format */
219 if (!strcasecmp(optarg, "xml")) {
220 informat = LYD_XML;
221 } else if (!strcasecmp(optarg, "json")) {
222 informat = LYD_JSON;
223 } else if (!strcasecmp(optarg, "lyb")) {
224 informat = LYD_LYB;
225 } else {
226 YLMSG_E("Unknown input format %s\n", optarg);
aPiecekb073f6a2023-04-28 15:23:25 +0200227 cmd_data_help_in_format();
Michal Vaskod3b10542021-02-03 11:31:16 +0100228 goto cleanup;
229 }
230 break;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100231 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;
aPiecek90c85162023-04-26 14:30:00 +0200244 LYD_FORMAT f = LYD_UNKNOWN;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100245
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' */
aPiecek079bcde2023-05-05 11:48:25 +0200256 case 'R': { /* --reply-rpc */
257 struct ly_in *in;
258 LYD_FORMAT f = LYD_UNKNOWN;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100259
aPiecek079bcde2023-05-05 11:48:25 +0200260 if (reply_rpc) {
261 YLMSG_E("The PRC of the reply (-R) cannot be set multiple times.\n");
262 goto cleanup;
263 }
264 if (get_input(optarg, NULL, &f, &in)) {
265 goto cleanup;
266 }
267 reply_rpc = fill_cmdline_file(NULL, in, optarg, f);
268 break;
269 } /* case 'R' */
Radek Krejcie9f13b12020-11-09 17:42:04 +0100270 case 'e': /* --present */
271 options_validate |= LYD_VALIDATE_PRESENT;
272 break;
273 case 'm': /* --merge */
274 data_merge = 1;
275 break;
Michal Vasko667ce6b2021-01-25 15:00:27 +0100276 case 'n': /* --not-strict */
277 options_parse &= ~LYD_PARSE_STRICT;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100278 break;
279 case 't': /* --type */
280 if (data_type_set) {
281 YLMSG_E("The data type (-t) cannot be set multiple times.\n");
282 goto cleanup;
283 }
284
285 if (!strcasecmp(optarg, "config")) {
286 options_parse |= LYD_PARSE_NO_STATE;
Michal Vaskof6bda332021-02-01 08:59:03 +0100287 options_validate |= LYD_VALIDATE_NO_STATE;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100288 } else if (!strcasecmp(optarg, "get")) {
289 options_parse |= LYD_PARSE_ONLY;
Michal Vasko6e985382022-07-13 18:34:57 +0200290 } else if (!strcasecmp(optarg, "getconfig") || !strcasecmp(optarg, "get-config") || !strcasecmp(optarg, "edit")) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100291 options_parse |= LYD_PARSE_ONLY | LYD_PARSE_NO_STATE;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100292 } else if (!strcasecmp(optarg, "rpc") || !strcasecmp(optarg, "action")) {
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100293 data_type = LYD_TYPE_RPC_YANG;
aPiecek860e34f2023-04-28 10:01:32 +0200294 } else if (!strcasecmp(optarg, "nc-rpc")) {
295 data_type = LYD_TYPE_RPC_NETCONF;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100296 } else if (!strcasecmp(optarg, "reply") || !strcasecmp(optarg, "rpcreply")) {
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100297 data_type = LYD_TYPE_REPLY_YANG;
aPiecek079bcde2023-05-05 11:48:25 +0200298 } else if (!strcasecmp(optarg, "nc-reply")) {
299 data_type = LYD_TYPE_REPLY_NETCONF;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100300 } else if (!strcasecmp(optarg, "notif") || !strcasecmp(optarg, "notification")) {
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100301 data_type = LYD_TYPE_NOTIF_YANG;
aPiecek6d2c1e72023-04-28 15:54:48 +0200302 } else if (!strcasecmp(optarg, "nc-notif")) {
303 data_type = LYD_TYPE_NOTIF_NETCONF;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100304 } else if (!strcasecmp(optarg, "data")) {
305 /* default option */
306 } else {
307 YLMSG_E("Unknown data tree type %s.\n", optarg);
aPiecekb073f6a2023-04-28 15:23:25 +0200308 cmd_data_help_type();
Radek Krejcie9f13b12020-11-09 17:42:04 +0100309 goto cleanup;
310 }
311
312 data_type_set = 1;
313 break;
314
315 case 'x': /* --xpath */
316 if (ly_set_add(&xpaths, optarg, 0, NULL)) {
317 YLMSG_E("Storing XPath \"%s\" failed.\n", optarg);
318 goto cleanup;
319 }
320 break;
321
322 case 'h': /* --help */
323 cmd_data_help();
324 goto cleanup;
325 default:
326 YLMSG_E("Unknown option.\n");
327 goto cleanup;
328 }
329 }
330
331 if (optind == argc) {
332 YLMSG_E("Missing the data file to process.\n");
333 goto cleanup;
334 }
335
336 if (data_merge) {
337 if (data_type || (options_parse & LYD_PARSE_ONLY)) {
338 /* switch off the option, incompatible input data type */
aPiecek3d46fa92023-05-10 08:30:39 +0200339 YLMSG_W("The --merge option has effect only for 'data' and 'config' TYPEs\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100340 data_merge = 0;
341 } else {
342 /* postpone validation after the merge of all the input data */
343 options_parse |= LYD_PARSE_ONLY;
344 }
345 } else if (xpaths.count) {
346 data_merge = 1;
347 }
348
Michal Vaskod3b10542021-02-03 11:31:16 +0100349 if (xpaths.count && outformat) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100350 YLMSG_E("The --format option cannot be combined with --xpath option.\n");
aPiecekb073f6a2023-04-28 15:23:25 +0200351 cmd_data_help_xpath();
Radek Krejcie9f13b12020-11-09 17:42:04 +0100352 goto cleanup;
353 }
aPiecek72a24c92023-04-12 15:03:05 +0200354 if (xpaths.count && (options_print & LYD_PRINT_WD_MASK)) {
355 YLMSG_E("The --default option cannot be combined with --xpath option.\n");
aPiecekb073f6a2023-04-28 15:23:25 +0200356 cmd_data_help_xpath();
aPiecek72a24c92023-04-12 15:03:05 +0200357 goto cleanup;
358 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100359
360 if (operational && !data_type) {
361 YLMSG_W("Operational datastore takes effect only with RPCs/Actions/Replies/Notifications input data types.\n");
362 free_cmdline_file(operational);
363 operational = NULL;
364 }
365
aPiecek079bcde2023-05-05 11:48:25 +0200366 if (reply_rpc && (data_type != LYD_TYPE_REPLY_NETCONF)) {
367 YLMSG_W("Source RPC is needed only for NETCONF Reply input data type.\n");
368 free_cmdline_file(operational);
369 operational = NULL;
370 } else if (!reply_rpc && (data_type == LYD_TYPE_REPLY_NETCONF)) {
371 YLMSG_E("Missing source RPC (-R) for NETCONF Reply input data type.\n");
372 goto cleanup;
373 }
374
Radek Krejcie9f13b12020-11-09 17:42:04 +0100375 /* process input data files provided as standalone command line arguments */
Radek Krejci6784a4e2020-12-09 14:23:05 +0100376 for (int i = 0; i < argc - optind; i++) {
377 struct ly_in *in;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100378
Michal Vaskod3b10542021-02-03 11:31:16 +0100379 if (get_input(argv[optind + i], NULL, &informat, &in)) {
Radek Krejci6784a4e2020-12-09 14:23:05 +0100380 goto cleanup;
381 }
382
Michal Vaskod3b10542021-02-03 11:31:16 +0100383 if (!fill_cmdline_file(&inputs, in, argv[optind + i], informat)) {
Radek Krejci6784a4e2020-12-09 14:23:05 +0100384 ly_in_free(in, 1);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100385 goto cleanup;
386 }
387 }
388
389 /* default output stream */
390 if (!out) {
391 if (ly_out_new_file(stdout, &out)) {
392 YLMSG_E("Unable to set stdout as output.\n");
393 goto cleanup;
394 }
395 }
396
397 /* parse, validate and print data */
Michal Vasko3f08fb92022-04-21 09:52:35 +0200398 if (process_data(*ctx, data_type, data_merge, outformat, out, options_parse, options_validate, options_print,
aPiecek079bcde2023-05-05 11:48:25 +0200399 operational, reply_rpc, &inputs, &xpaths)) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100400 goto cleanup;
401 }
402
403cleanup:
404 ly_out_free(out, NULL, 0);
405 ly_set_erase(&inputs, free_cmdline_file);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100406 ly_set_erase(&xpaths, NULL);
407 free_cmdline_file(operational);
408 free_cmdline(argv);
409}