blob: 5ffe4142e5d159f9021f40fbb0f5d53498c34bde [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"
aPiecek860e34f2023-04-28 10:01:32 +020050 " nc-rpc - Similar to 'rpc' but expect and check also the NETCONF\n"
51 " envelopes <rpc> or <action>.\n"
Radek Krejci6784a4e2020-12-09 14:23:05 +010052 " reply - Reply to the RPC/Action. Note that the reply data are\n"
53 " expected inside a container representing the original\n"
54 " RPC/Action. This is necessary to identify appropriate\n"
55 " data definitions in the schema module.\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +010056 " notif - Notification instance (content of the <notification>\n"
57 " element without <eventTime>).\n\n"
58
59 " -e, --present Validate only with the schema modules whose data actually\n"
60 " exist in the provided input data files. Takes effect only\n"
61 " with the 'data' or 'config' TYPEs. Used to avoid requiring\n"
62 " mandatory nodes from modules which data are not present in the\n"
63 " provided input data files.\n"
64 " -m, --merge Merge input data files into a single tree and validate at\n"
65 " once.The option has effect only for 'data' and 'config' TYPEs.\n"
66 " In case of using -x option, the data are always merged.\n"
Michal Vaskoc431a0a2021-01-25 14:31:58 +010067 " -n, --not-strict\n"
68 " Do not require strict data parsing (silently skip unknown data),\n"
69 " has no effect for schemas.\n\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +010070 " -O FILE, --operational=FILE\n"
71 " Provide optional data to extend validation of the 'rpc',\n"
72 " 'reply' or 'notif' TYPEs. The FILE is supposed to contain\n"
aPieceka40764b2023-04-27 15:34:56 +020073 " the operational datastore referenced from the operation.\n"
74 " In case of a nested notification or action, its parent\n"
75 " existence is also checked in these operational data.\n\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +010076 " -f FORMAT, --format=FORMAT\n"
77 " Print the data in one of the following formats:\n"
78 " xml, json, lyb\n"
79 " Note that the LYB format requires the -o option specified.\n"
Michal Vaskod3b10542021-02-03 11:31:16 +010080 " -F FORMAT, --in-format=FORMAT\n"
81 " Load the data in one of the following formats:\n"
82 " xml, json, lyb\n"
83 " If input format not specified, it is detected from the file extension.\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +010084 " -d MODE, --default=MODE\n"
85 " Print data with default values, according to the MODE\n"
86 " (to print attributes, ietf-netconf-with-defaults model\n"
87 " must be loaded):\n"
88 " all - Add missing default nodes.\n"
89 " all-tagged - Add missing default nodes and mark all the default\n"
90 " nodes with the attribute.\n"
91 " trim - Remove all nodes with a default value.\n"
92 " implicit-tagged - Add missing nodes and mark them with the attribute.\n"
93 " -o OUTFILE, --output=OUTFILE\n"
94 " Write the output to OUTFILE instead of stdout.\n\n"
95
96 " -x XPATH, --xpath=XPATH\n"
97 " Evaluate XPATH expression and print the nodes satysfying the.\n"
98 " expression. The output format is specific and the option cannot\n"
99 " be combined with the -f and -d options. Also all the data\n"
100 " inputs are merged into a single data tree where the expression\n"
101 " is evaluated, so the -m option is always set implicitly.\n\n");
102
103}
104
Radek Krejcie9f13b12020-11-09 17:42:04 +0100105void
106cmd_data(struct ly_ctx **ctx, const char *cmdline)
107{
108 int argc = 0;
109 char **argv = NULL;
110 int opt, opt_index;
111 struct option options[] = {
Michal Vasko667ce6b2021-01-25 15:00:27 +0100112 {"defaults", required_argument, NULL, 'd'},
113 {"present", no_argument, NULL, 'e'},
114 {"format", required_argument, NULL, 'f'},
Michal Vaskod3b10542021-02-03 11:31:16 +0100115 {"in-format", required_argument, NULL, 'F'},
Michal Vasko667ce6b2021-01-25 15:00:27 +0100116 {"help", no_argument, NULL, 'h'},
117 {"merge", no_argument, NULL, 'm'},
118 {"output", required_argument, NULL, 'o'},
Radek Krejcie9f13b12020-11-09 17:42:04 +0100119 {"operational", required_argument, NULL, 'O'},
Michal Vasko667ce6b2021-01-25 15:00:27 +0100120 {"not-strict", no_argument, NULL, 'n'},
121 {"type", required_argument, NULL, 't'},
122 {"xpath", required_argument, NULL, 'x'},
Radek Krejcie9f13b12020-11-09 17:42:04 +0100123 {NULL, 0, NULL, 0}
124 };
125
126 uint8_t data_merge = 0;
127 uint32_t options_print = 0;
Michal Vasko667ce6b2021-01-25 15:00:27 +0100128 uint32_t options_parse = YL_DEFAULT_DATA_PARSE_OPTIONS;
Michal Vasko05eaf832023-02-10 09:21:46 +0100129 uint32_t options_validate = YL_DEFAULT_DATA_VALIDATE_OPTIONS;
Michal Vaskoe0665742021-02-11 11:08:44 +0100130 enum lyd_type data_type = 0;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100131 uint8_t data_type_set = 0;
Michal Vaskod3b10542021-02-03 11:31:16 +0100132 LYD_FORMAT outformat = LYD_UNKNOWN;
133 LYD_FORMAT informat = LYD_UNKNOWN;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100134 struct ly_out *out = NULL;
135 struct cmdline_file *operational = NULL;
136 struct ly_set inputs = {0};
Radek Krejcie9f13b12020-11-09 17:42:04 +0100137 struct ly_set xpaths = {0};
138
139 if (parse_cmdline(cmdline, &argc, &argv)) {
140 goto cleanup;
141 }
142
aPiecek15cc4cf2023-04-17 15:23:21 +0200143 while ((opt = getopt_long(argc, argv, commands[CMD_DATA].optstring, options, &opt_index)) != -1) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100144 switch (opt) {
145 case 'd': /* --default */
146 if (!strcasecmp(optarg, "all")) {
147 options_print = (options_print & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_ALL;
148 } else if (!strcasecmp(optarg, "all-tagged")) {
149 options_print = (options_print & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_ALL_TAG;
150 } else if (!strcasecmp(optarg, "trim")) {
151 options_print = (options_print & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_TRIM;
152 } else if (!strcasecmp(optarg, "implicit-tagged")) {
153 options_print = (options_print & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_IMPL_TAG;
154 } else {
155 YLMSG_E("Unknown default mode %s\n", optarg);
156 cmd_data_help();
157 goto cleanup;
158 }
159 break;
160 case 'f': /* --format */
161 if (!strcasecmp(optarg, "xml")) {
Michal Vaskod3b10542021-02-03 11:31:16 +0100162 outformat = LYD_XML;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100163 } else if (!strcasecmp(optarg, "json")) {
Michal Vaskod3b10542021-02-03 11:31:16 +0100164 outformat = LYD_JSON;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100165 } else if (!strcasecmp(optarg, "lyb")) {
Michal Vaskod3b10542021-02-03 11:31:16 +0100166 outformat = LYD_LYB;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100167 } else {
168 YLMSG_E("Unknown output format %s\n", optarg);
169 cmd_data_help();
170 goto cleanup;
171 }
172 break;
Michal Vaskod3b10542021-02-03 11:31:16 +0100173 case 'F': /* --in-format */
174 if (!strcasecmp(optarg, "xml")) {
175 informat = LYD_XML;
176 } else if (!strcasecmp(optarg, "json")) {
177 informat = LYD_JSON;
178 } else if (!strcasecmp(optarg, "lyb")) {
179 informat = LYD_LYB;
180 } else {
181 YLMSG_E("Unknown input format %s\n", optarg);
182 cmd_data_help();
183 goto cleanup;
184 }
185 break;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100186 case 'o': /* --output */
187 if (out) {
188 YLMSG_E("Only a single output can be specified.\n");
189 goto cleanup;
190 } else {
191 if (ly_out_new_filepath(optarg, &out)) {
192 YLMSG_E("Unable open output file %s (%s)\n", optarg, strerror(errno));
193 goto cleanup;
194 }
195 }
196 break;
197 case 'O': { /* --operational */
198 struct ly_in *in;
aPiecek90c85162023-04-26 14:30:00 +0200199 LYD_FORMAT f = LYD_UNKNOWN;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100200
201 if (operational) {
202 YLMSG_E("The operational datastore (-O) cannot be set multiple times.\n");
203 goto cleanup;
204 }
205 if (get_input(optarg, NULL, &f, &in)) {
206 goto cleanup;
207 }
208 operational = fill_cmdline_file(NULL, in, optarg, f);
209 break;
210 } /* case 'O' */
Radek Krejcie9f13b12020-11-09 17:42:04 +0100211
212 case 'e': /* --present */
213 options_validate |= LYD_VALIDATE_PRESENT;
214 break;
215 case 'm': /* --merge */
216 data_merge = 1;
217 break;
Michal Vasko667ce6b2021-01-25 15:00:27 +0100218 case 'n': /* --not-strict */
219 options_parse &= ~LYD_PARSE_STRICT;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100220 break;
221 case 't': /* --type */
222 if (data_type_set) {
223 YLMSG_E("The data type (-t) cannot be set multiple times.\n");
224 goto cleanup;
225 }
226
227 if (!strcasecmp(optarg, "config")) {
228 options_parse |= LYD_PARSE_NO_STATE;
Michal Vaskof6bda332021-02-01 08:59:03 +0100229 options_validate |= LYD_VALIDATE_NO_STATE;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100230 } else if (!strcasecmp(optarg, "get")) {
231 options_parse |= LYD_PARSE_ONLY;
Michal Vasko6e985382022-07-13 18:34:57 +0200232 } else if (!strcasecmp(optarg, "getconfig") || !strcasecmp(optarg, "get-config") || !strcasecmp(optarg, "edit")) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100233 options_parse |= LYD_PARSE_ONLY | LYD_PARSE_NO_STATE;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100234 } else if (!strcasecmp(optarg, "rpc") || !strcasecmp(optarg, "action")) {
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100235 data_type = LYD_TYPE_RPC_YANG;
aPiecek860e34f2023-04-28 10:01:32 +0200236 } else if (!strcasecmp(optarg, "nc-rpc")) {
237 data_type = LYD_TYPE_RPC_NETCONF;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100238 } else if (!strcasecmp(optarg, "reply") || !strcasecmp(optarg, "rpcreply")) {
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100239 data_type = LYD_TYPE_REPLY_YANG;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100240 } else if (!strcasecmp(optarg, "notif") || !strcasecmp(optarg, "notification")) {
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100241 data_type = LYD_TYPE_NOTIF_YANG;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100242 } else if (!strcasecmp(optarg, "data")) {
243 /* default option */
244 } else {
245 YLMSG_E("Unknown data tree type %s.\n", optarg);
246 cmd_data_help();
247 goto cleanup;
248 }
249
250 data_type_set = 1;
251 break;
252
253 case 'x': /* --xpath */
254 if (ly_set_add(&xpaths, optarg, 0, NULL)) {
255 YLMSG_E("Storing XPath \"%s\" failed.\n", optarg);
256 goto cleanup;
257 }
258 break;
259
260 case 'h': /* --help */
261 cmd_data_help();
262 goto cleanup;
263 default:
264 YLMSG_E("Unknown option.\n");
265 goto cleanup;
266 }
267 }
268
269 if (optind == argc) {
270 YLMSG_E("Missing the data file to process.\n");
271 goto cleanup;
272 }
273
274 if (data_merge) {
275 if (data_type || (options_parse & LYD_PARSE_ONLY)) {
276 /* switch off the option, incompatible input data type */
277 data_merge = 0;
278 } else {
279 /* postpone validation after the merge of all the input data */
280 options_parse |= LYD_PARSE_ONLY;
281 }
282 } else if (xpaths.count) {
283 data_merge = 1;
284 }
285
Michal Vaskod3b10542021-02-03 11:31:16 +0100286 if (xpaths.count && outformat) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100287 YLMSG_E("The --format option cannot be combined with --xpath option.\n");
288 cmd_data_help();
289 goto cleanup;
290 }
aPiecek72a24c92023-04-12 15:03:05 +0200291 if (xpaths.count && (options_print & LYD_PRINT_WD_MASK)) {
292 YLMSG_E("The --default option cannot be combined with --xpath option.\n");
293 cmd_data_help();
294 goto cleanup;
295 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100296
297 if (operational && !data_type) {
298 YLMSG_W("Operational datastore takes effect only with RPCs/Actions/Replies/Notifications input data types.\n");
299 free_cmdline_file(operational);
300 operational = NULL;
301 }
302
303 /* process input data files provided as standalone command line arguments */
Radek Krejci6784a4e2020-12-09 14:23:05 +0100304 for (int i = 0; i < argc - optind; i++) {
305 struct ly_in *in;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100306
Michal Vaskod3b10542021-02-03 11:31:16 +0100307 if (get_input(argv[optind + i], NULL, &informat, &in)) {
Radek Krejci6784a4e2020-12-09 14:23:05 +0100308 goto cleanup;
309 }
310
Michal Vaskod3b10542021-02-03 11:31:16 +0100311 if (!fill_cmdline_file(&inputs, in, argv[optind + i], informat)) {
Radek Krejci6784a4e2020-12-09 14:23:05 +0100312 ly_in_free(in, 1);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100313 goto cleanup;
314 }
315 }
316
317 /* default output stream */
318 if (!out) {
319 if (ly_out_new_file(stdout, &out)) {
320 YLMSG_E("Unable to set stdout as output.\n");
321 goto cleanup;
322 }
323 }
324
325 /* parse, validate and print data */
Michal Vasko3f08fb92022-04-21 09:52:35 +0200326 if (process_data(*ctx, data_type, data_merge, outformat, out, options_parse, options_validate, options_print,
327 operational, NULL, &inputs, &xpaths)) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100328 goto cleanup;
329 }
330
331cleanup:
332 ly_out_free(out, NULL, 0);
333 ly_set_erase(&inputs, free_cmdline_file);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100334 ly_set_erase(&xpaths, NULL);
335 free_cmdline_file(operational);
336 free_cmdline(argv);
337}