blob: 53045b6843ecc47e469781f2afe1db3507c8cdff [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[] = {
Michal Vasko667ce6b2021-01-25 15:00:27 +0100106 {"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'},
Radek Krejcie9f13b12020-11-09 17:42:04 +0100112 {"operational", required_argument, NULL, 'O'},
Michal Vasko667ce6b2021-01-25 15:00:27 +0100113 {"not-strict", no_argument, NULL, 'n'},
114 {"type", required_argument, NULL, 't'},
115 {"xpath", required_argument, NULL, 'x'},
Radek Krejcie9f13b12020-11-09 17:42:04 +0100116 {NULL, 0, NULL, 0}
117 };
118
119 uint8_t data_merge = 0;
120 uint32_t options_print = 0;
Michal Vasko667ce6b2021-01-25 15:00:27 +0100121 uint32_t options_parse = YL_DEFAULT_DATA_PARSE_OPTIONS;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100122 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
Michal Vasko667ce6b2021-01-25 15:00:27 +0100135 while ((opt = getopt_long(argc, argv, "d:ef:hmo:O:r:nt:x:", options, &opt_index)) != -1) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100136 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;
Michal Vasko667ce6b2021-01-25 15:00:27 +0100197 case 'n': /* --not-strict */
198 options_parse &= ~LYD_PARSE_STRICT;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100199 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;
Michal Vaskof6bda332021-02-01 08:59:03 +0100208 options_validate |= LYD_VALIDATE_NO_STATE;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100209 } else if (!strcasecmp(optarg, "get")) {
210 options_parse |= LYD_PARSE_ONLY;
211 } else if (!strcasecmp(optarg, "getconfig") || !strcasecmp(optarg, "get-config")) {
212 options_parse |= LYD_PARSE_ONLY | LYD_PARSE_NO_STATE;
213 } else if (!strcasecmp(optarg, "edit")) {
214 options_parse |= LYD_PARSE_ONLY;
215 } else if (!strcasecmp(optarg, "rpc") || !strcasecmp(optarg, "action")) {
216 data_type = LYD_VALIDATE_OP_RPC;
217 } else if (!strcasecmp(optarg, "reply") || !strcasecmp(optarg, "rpcreply")) {
218 data_type = LYD_VALIDATE_OP_REPLY;
219 } else if (!strcasecmp(optarg, "notif") || !strcasecmp(optarg, "notification")) {
220 data_type = LYD_VALIDATE_OP_NOTIF;
221 } else if (!strcasecmp(optarg, "data")) {
222 /* default option */
223 } else {
224 YLMSG_E("Unknown data tree type %s.\n", optarg);
225 cmd_data_help();
226 goto cleanup;
227 }
228
229 data_type_set = 1;
230 break;
231
232 case 'x': /* --xpath */
233 if (ly_set_add(&xpaths, optarg, 0, NULL)) {
234 YLMSG_E("Storing XPath \"%s\" failed.\n", optarg);
235 goto cleanup;
236 }
237 break;
238
239 case 'h': /* --help */
240 cmd_data_help();
241 goto cleanup;
242 default:
243 YLMSG_E("Unknown option.\n");
244 goto cleanup;
245 }
246 }
247
248 if (optind == argc) {
249 YLMSG_E("Missing the data file to process.\n");
250 goto cleanup;
251 }
252
253 if (data_merge) {
254 if (data_type || (options_parse & LYD_PARSE_ONLY)) {
255 /* switch off the option, incompatible input data type */
256 data_merge = 0;
257 } else {
258 /* postpone validation after the merge of all the input data */
259 options_parse |= LYD_PARSE_ONLY;
260 }
261 } else if (xpaths.count) {
262 data_merge = 1;
263 }
264
265 if (xpaths.count && format) {
266 YLMSG_E("The --format option cannot be combined with --xpath option.\n");
267 cmd_data_help();
268 goto cleanup;
269 }
270
271 if (operational && !data_type) {
272 YLMSG_W("Operational datastore takes effect only with RPCs/Actions/Replies/Notifications input data types.\n");
273 free_cmdline_file(operational);
274 operational = NULL;
275 }
276
277 /* process input data files provided as standalone command line arguments */
Radek Krejci6784a4e2020-12-09 14:23:05 +0100278 for (int i = 0; i < argc - optind; i++) {
279 struct ly_in *in;
280 LYD_FORMAT format = LYD_UNKNOWN;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100281
Radek Krejci6784a4e2020-12-09 14:23:05 +0100282 if (get_input(argv[optind + i], NULL, &format, &in)) {
283 goto cleanup;
284 }
285
Michal Vasko48d5a752021-01-25 14:18:25 +0100286 if (!fill_cmdline_file(&inputs, in, argv[optind + i], format)) {
Radek Krejci6784a4e2020-12-09 14:23:05 +0100287 ly_in_free(in, 1);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100288 goto cleanup;
289 }
290 }
291
292 /* default output stream */
293 if (!out) {
294 if (ly_out_new_file(stdout, &out)) {
295 YLMSG_E("Unable to set stdout as output.\n");
296 goto cleanup;
297 }
298 }
299
300 /* parse, validate and print data */
301 if (process_data(*ctx, data_type, data_merge, format, out,
302 options_parse, options_validate, options_print,
Radek Krejci6784a4e2020-12-09 14:23:05 +0100303 operational, &inputs, &xpaths)) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100304 goto cleanup;
305 }
306
307cleanup:
308 ly_out_free(out, NULL, 0);
309 ly_set_erase(&inputs, free_cmdline_file);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100310 ly_set_erase(&xpaths, NULL);
311 free_cmdline_file(operational);
312 free_cmdline(argv);
313}