blob: b177704d60c5b478b017238e85e693504d4af0ae [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{
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"
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"
aPieceka40764b2023-04-27 15:34:56 +020071 " the operational datastore referenced from the operation.\n"
72 " In case of a nested notification or action, its parent\n"
73 " existence is also checked in these operational data.\n\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +010074 " -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"
Michal Vaskod3b10542021-02-03 11:31:16 +010078 " -F FORMAT, --in-format=FORMAT\n"
79 " Load the data in one of the following formats:\n"
80 " xml, json, lyb\n"
81 " If input format not specified, it is detected from the file extension.\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +010082 " -d MODE, --default=MODE\n"
83 " Print data with default values, according to the MODE\n"
84 " (to print attributes, ietf-netconf-with-defaults model\n"
85 " must be loaded):\n"
86 " all - Add missing default nodes.\n"
87 " all-tagged - Add missing default nodes and mark all the default\n"
88 " nodes with the attribute.\n"
89 " trim - Remove all nodes with a default value.\n"
90 " implicit-tagged - Add missing nodes and mark them with the attribute.\n"
91 " -o OUTFILE, --output=OUTFILE\n"
92 " Write the output to OUTFILE instead of stdout.\n\n"
93
94 " -x XPATH, --xpath=XPATH\n"
95 " Evaluate XPATH expression and print the nodes satysfying the.\n"
96 " expression. The output format is specific and the option cannot\n"
97 " be combined with the -f and -d options. Also all the data\n"
98 " inputs are merged into a single data tree where the expression\n"
99 " is evaluated, so the -m option is always set implicitly.\n\n");
100
101}
102
Radek Krejcie9f13b12020-11-09 17:42:04 +0100103void
104cmd_data(struct ly_ctx **ctx, const char *cmdline)
105{
106 int argc = 0;
107 char **argv = NULL;
108 int opt, opt_index;
109 struct option options[] = {
Michal Vasko667ce6b2021-01-25 15:00:27 +0100110 {"defaults", required_argument, NULL, 'd'},
111 {"present", no_argument, NULL, 'e'},
112 {"format", required_argument, NULL, 'f'},
Michal Vaskod3b10542021-02-03 11:31:16 +0100113 {"in-format", required_argument, NULL, 'F'},
Michal Vasko667ce6b2021-01-25 15:00:27 +0100114 {"help", no_argument, NULL, 'h'},
115 {"merge", no_argument, NULL, 'm'},
116 {"output", required_argument, NULL, 'o'},
Radek Krejcie9f13b12020-11-09 17:42:04 +0100117 {"operational", required_argument, NULL, 'O'},
Michal Vasko667ce6b2021-01-25 15:00:27 +0100118 {"not-strict", no_argument, NULL, 'n'},
119 {"type", required_argument, NULL, 't'},
120 {"xpath", required_argument, NULL, 'x'},
Radek Krejcie9f13b12020-11-09 17:42:04 +0100121 {NULL, 0, NULL, 0}
122 };
123
124 uint8_t data_merge = 0;
125 uint32_t options_print = 0;
Michal Vasko667ce6b2021-01-25 15:00:27 +0100126 uint32_t options_parse = YL_DEFAULT_DATA_PARSE_OPTIONS;
Michal Vasko05eaf832023-02-10 09:21:46 +0100127 uint32_t options_validate = YL_DEFAULT_DATA_VALIDATE_OPTIONS;
Michal Vaskoe0665742021-02-11 11:08:44 +0100128 enum lyd_type data_type = 0;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100129 uint8_t data_type_set = 0;
Michal Vaskod3b10542021-02-03 11:31:16 +0100130 LYD_FORMAT outformat = LYD_UNKNOWN;
131 LYD_FORMAT informat = LYD_UNKNOWN;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100132 struct ly_out *out = NULL;
133 struct cmdline_file *operational = NULL;
134 struct ly_set inputs = {0};
Radek Krejcie9f13b12020-11-09 17:42:04 +0100135 struct ly_set xpaths = {0};
136
137 if (parse_cmdline(cmdline, &argc, &argv)) {
138 goto cleanup;
139 }
140
aPiecek15cc4cf2023-04-17 15:23:21 +0200141 while ((opt = getopt_long(argc, argv, commands[CMD_DATA].optstring, options, &opt_index)) != -1) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100142 switch (opt) {
143 case 'd': /* --default */
144 if (!strcasecmp(optarg, "all")) {
145 options_print = (options_print & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_ALL;
146 } else if (!strcasecmp(optarg, "all-tagged")) {
147 options_print = (options_print & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_ALL_TAG;
148 } else if (!strcasecmp(optarg, "trim")) {
149 options_print = (options_print & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_TRIM;
150 } else if (!strcasecmp(optarg, "implicit-tagged")) {
151 options_print = (options_print & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_IMPL_TAG;
152 } else {
153 YLMSG_E("Unknown default mode %s\n", optarg);
154 cmd_data_help();
155 goto cleanup;
156 }
157 break;
158 case 'f': /* --format */
159 if (!strcasecmp(optarg, "xml")) {
Michal Vaskod3b10542021-02-03 11:31:16 +0100160 outformat = LYD_XML;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100161 } else if (!strcasecmp(optarg, "json")) {
Michal Vaskod3b10542021-02-03 11:31:16 +0100162 outformat = LYD_JSON;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100163 } else if (!strcasecmp(optarg, "lyb")) {
Michal Vaskod3b10542021-02-03 11:31:16 +0100164 outformat = LYD_LYB;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100165 } else {
166 YLMSG_E("Unknown output format %s\n", optarg);
167 cmd_data_help();
168 goto cleanup;
169 }
170 break;
Michal Vaskod3b10542021-02-03 11:31:16 +0100171 case 'F': /* --in-format */
172 if (!strcasecmp(optarg, "xml")) {
173 informat = LYD_XML;
174 } else if (!strcasecmp(optarg, "json")) {
175 informat = LYD_JSON;
176 } else if (!strcasecmp(optarg, "lyb")) {
177 informat = LYD_LYB;
178 } else {
179 YLMSG_E("Unknown input format %s\n", optarg);
180 cmd_data_help();
181 goto cleanup;
182 }
183 break;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100184 case 'o': /* --output */
185 if (out) {
186 YLMSG_E("Only a single output can be specified.\n");
187 goto cleanup;
188 } else {
189 if (ly_out_new_filepath(optarg, &out)) {
190 YLMSG_E("Unable open output file %s (%s)\n", optarg, strerror(errno));
191 goto cleanup;
192 }
193 }
194 break;
195 case 'O': { /* --operational */
196 struct ly_in *in;
aPiecek90c85162023-04-26 14:30:00 +0200197 LYD_FORMAT f = LYD_UNKNOWN;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100198
199 if (operational) {
200 YLMSG_E("The operational datastore (-O) cannot be set multiple times.\n");
201 goto cleanup;
202 }
203 if (get_input(optarg, NULL, &f, &in)) {
204 goto cleanup;
205 }
206 operational = fill_cmdline_file(NULL, in, optarg, f);
207 break;
208 } /* case 'O' */
Radek Krejcie9f13b12020-11-09 17:42:04 +0100209
210 case 'e': /* --present */
211 options_validate |= LYD_VALIDATE_PRESENT;
212 break;
213 case 'm': /* --merge */
214 data_merge = 1;
215 break;
Michal Vasko667ce6b2021-01-25 15:00:27 +0100216 case 'n': /* --not-strict */
217 options_parse &= ~LYD_PARSE_STRICT;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100218 break;
219 case 't': /* --type */
220 if (data_type_set) {
221 YLMSG_E("The data type (-t) cannot be set multiple times.\n");
222 goto cleanup;
223 }
224
225 if (!strcasecmp(optarg, "config")) {
226 options_parse |= LYD_PARSE_NO_STATE;
Michal Vaskof6bda332021-02-01 08:59:03 +0100227 options_validate |= LYD_VALIDATE_NO_STATE;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100228 } else if (!strcasecmp(optarg, "get")) {
229 options_parse |= LYD_PARSE_ONLY;
Michal Vasko6e985382022-07-13 18:34:57 +0200230 } else if (!strcasecmp(optarg, "getconfig") || !strcasecmp(optarg, "get-config") || !strcasecmp(optarg, "edit")) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100231 options_parse |= LYD_PARSE_ONLY | LYD_PARSE_NO_STATE;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100232 } else if (!strcasecmp(optarg, "rpc") || !strcasecmp(optarg, "action")) {
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100233 data_type = LYD_TYPE_RPC_YANG;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100234 } else if (!strcasecmp(optarg, "reply") || !strcasecmp(optarg, "rpcreply")) {
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100235 data_type = LYD_TYPE_REPLY_YANG;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100236 } else if (!strcasecmp(optarg, "notif") || !strcasecmp(optarg, "notification")) {
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100237 data_type = LYD_TYPE_NOTIF_YANG;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100238 } else if (!strcasecmp(optarg, "data")) {
239 /* default option */
240 } else {
241 YLMSG_E("Unknown data tree type %s.\n", optarg);
242 cmd_data_help();
243 goto cleanup;
244 }
245
246 data_type_set = 1;
247 break;
248
249 case 'x': /* --xpath */
250 if (ly_set_add(&xpaths, optarg, 0, NULL)) {
251 YLMSG_E("Storing XPath \"%s\" failed.\n", optarg);
252 goto cleanup;
253 }
254 break;
255
256 case 'h': /* --help */
257 cmd_data_help();
258 goto cleanup;
259 default:
260 YLMSG_E("Unknown option.\n");
261 goto cleanup;
262 }
263 }
264
265 if (optind == argc) {
266 YLMSG_E("Missing the data file to process.\n");
267 goto cleanup;
268 }
269
270 if (data_merge) {
271 if (data_type || (options_parse & LYD_PARSE_ONLY)) {
272 /* switch off the option, incompatible input data type */
273 data_merge = 0;
274 } else {
275 /* postpone validation after the merge of all the input data */
276 options_parse |= LYD_PARSE_ONLY;
277 }
278 } else if (xpaths.count) {
279 data_merge = 1;
280 }
281
Michal Vaskod3b10542021-02-03 11:31:16 +0100282 if (xpaths.count && outformat) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100283 YLMSG_E("The --format option cannot be combined with --xpath option.\n");
284 cmd_data_help();
285 goto cleanup;
286 }
aPiecek72a24c92023-04-12 15:03:05 +0200287 if (xpaths.count && (options_print & LYD_PRINT_WD_MASK)) {
288 YLMSG_E("The --default option cannot be combined with --xpath option.\n");
289 cmd_data_help();
290 goto cleanup;
291 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100292
293 if (operational && !data_type) {
294 YLMSG_W("Operational datastore takes effect only with RPCs/Actions/Replies/Notifications input data types.\n");
295 free_cmdline_file(operational);
296 operational = NULL;
297 }
298
299 /* process input data files provided as standalone command line arguments */
Radek Krejci6784a4e2020-12-09 14:23:05 +0100300 for (int i = 0; i < argc - optind; i++) {
301 struct ly_in *in;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100302
Michal Vaskod3b10542021-02-03 11:31:16 +0100303 if (get_input(argv[optind + i], NULL, &informat, &in)) {
Radek Krejci6784a4e2020-12-09 14:23:05 +0100304 goto cleanup;
305 }
306
Michal Vaskod3b10542021-02-03 11:31:16 +0100307 if (!fill_cmdline_file(&inputs, in, argv[optind + i], informat)) {
Radek Krejci6784a4e2020-12-09 14:23:05 +0100308 ly_in_free(in, 1);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100309 goto cleanup;
310 }
311 }
312
313 /* default output stream */
314 if (!out) {
315 if (ly_out_new_file(stdout, &out)) {
316 YLMSG_E("Unable to set stdout as output.\n");
317 goto cleanup;
318 }
319 }
320
321 /* parse, validate and print data */
Michal Vasko3f08fb92022-04-21 09:52:35 +0200322 if (process_data(*ctx, data_type, data_merge, outformat, out, options_parse, options_validate, options_print,
323 operational, NULL, &inputs, &xpaths)) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100324 goto cleanup;
325 }
326
327cleanup:
328 ly_out_free(out, NULL, 0);
329 ly_set_erase(&inputs, free_cmdline_file);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100330 ly_set_erase(&xpaths, NULL);
331 free_cmdline_file(operational);
332 free_cmdline(argv);
333}