Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 1 | /** |
| 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 | |
| 31 | void |
| 32 | cmd_data_help(void) |
| 33 | { |
Michal Vasko | d3b1054 | 2021-02-03 11:31:16 +0100 | [diff] [blame] | 34 | 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 Krejci | 6784a4e | 2020-12-09 14:23:05 +0100 | [diff] [blame] | 39 | " Parse, validate and optionally print data instances\n\n" |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 40 | |
| 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 Krejci | 6784a4e | 2020-12-09 14:23:05 +0100 | [diff] [blame] | 50 | " 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 Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 54 | " 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 Vasko | c431a0a | 2021-01-25 14:31:58 +0100 | [diff] [blame] | 65 | " -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 Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 68 | " -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" |
Michal Vasko | d3b1054 | 2021-02-03 11:31:16 +0100 | [diff] [blame] | 78 | " -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 Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 82 | " -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 Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 103 | void |
| 104 | cmd_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 Vasko | 667ce6b | 2021-01-25 15:00:27 +0100 | [diff] [blame] | 110 | {"defaults", required_argument, NULL, 'd'}, |
| 111 | {"present", no_argument, NULL, 'e'}, |
| 112 | {"format", required_argument, NULL, 'f'}, |
Michal Vasko | d3b1054 | 2021-02-03 11:31:16 +0100 | [diff] [blame] | 113 | {"in-format", required_argument, NULL, 'F'}, |
Michal Vasko | 667ce6b | 2021-01-25 15:00:27 +0100 | [diff] [blame] | 114 | {"help", no_argument, NULL, 'h'}, |
| 115 | {"merge", no_argument, NULL, 'm'}, |
| 116 | {"output", required_argument, NULL, 'o'}, |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 117 | {"operational", required_argument, NULL, 'O'}, |
Michal Vasko | 667ce6b | 2021-01-25 15:00:27 +0100 | [diff] [blame] | 118 | {"not-strict", no_argument, NULL, 'n'}, |
| 119 | {"type", required_argument, NULL, 't'}, |
| 120 | {"xpath", required_argument, NULL, 'x'}, |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 121 | {NULL, 0, NULL, 0} |
| 122 | }; |
| 123 | |
| 124 | uint8_t data_merge = 0; |
| 125 | uint32_t options_print = 0; |
Michal Vasko | 667ce6b | 2021-01-25 15:00:27 +0100 | [diff] [blame] | 126 | uint32_t options_parse = YL_DEFAULT_DATA_PARSE_OPTIONS; |
Michal Vasko | 05eaf83 | 2023-02-10 09:21:46 +0100 | [diff] [blame] | 127 | uint32_t options_validate = YL_DEFAULT_DATA_VALIDATE_OPTIONS; |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 128 | enum lyd_type data_type = 0; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 129 | uint8_t data_type_set = 0; |
Michal Vasko | d3b1054 | 2021-02-03 11:31:16 +0100 | [diff] [blame] | 130 | LYD_FORMAT outformat = LYD_UNKNOWN; |
| 131 | LYD_FORMAT informat = LYD_UNKNOWN; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 132 | struct ly_out *out = NULL; |
| 133 | struct cmdline_file *operational = NULL; |
| 134 | struct ly_set inputs = {0}; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 135 | struct ly_set xpaths = {0}; |
| 136 | |
| 137 | if (parse_cmdline(cmdline, &argc, &argv)) { |
| 138 | goto cleanup; |
| 139 | } |
| 140 | |
Michal Vasko | d3b1054 | 2021-02-03 11:31:16 +0100 | [diff] [blame] | 141 | while ((opt = getopt_long(argc, argv, "d:ef:F:hmo:O:r:nt:x:", options, &opt_index)) != -1) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 142 | 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 Vasko | d3b1054 | 2021-02-03 11:31:16 +0100 | [diff] [blame] | 160 | outformat = LYD_XML; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 161 | } else if (!strcasecmp(optarg, "json")) { |
Michal Vasko | d3b1054 | 2021-02-03 11:31:16 +0100 | [diff] [blame] | 162 | outformat = LYD_JSON; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 163 | } else if (!strcasecmp(optarg, "lyb")) { |
Michal Vasko | d3b1054 | 2021-02-03 11:31:16 +0100 | [diff] [blame] | 164 | outformat = LYD_LYB; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 165 | } else { |
| 166 | YLMSG_E("Unknown output format %s\n", optarg); |
| 167 | cmd_data_help(); |
| 168 | goto cleanup; |
| 169 | } |
| 170 | break; |
Michal Vasko | d3b1054 | 2021-02-03 11:31:16 +0100 | [diff] [blame] | 171 | 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 Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 184 | 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; |
| 197 | LYD_FORMAT f; |
| 198 | |
| 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 Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 209 | |
| 210 | case 'e': /* --present */ |
| 211 | options_validate |= LYD_VALIDATE_PRESENT; |
| 212 | break; |
| 213 | case 'm': /* --merge */ |
| 214 | data_merge = 1; |
| 215 | break; |
Michal Vasko | 667ce6b | 2021-01-25 15:00:27 +0100 | [diff] [blame] | 216 | case 'n': /* --not-strict */ |
| 217 | options_parse &= ~LYD_PARSE_STRICT; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 218 | 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 Vasko | f6bda33 | 2021-02-01 08:59:03 +0100 | [diff] [blame] | 227 | options_validate |= LYD_VALIDATE_NO_STATE; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 228 | } else if (!strcasecmp(optarg, "get")) { |
| 229 | options_parse |= LYD_PARSE_ONLY; |
Michal Vasko | 6e98538 | 2022-07-13 18:34:57 +0200 | [diff] [blame] | 230 | } else if (!strcasecmp(optarg, "getconfig") || !strcasecmp(optarg, "get-config") || !strcasecmp(optarg, "edit")) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 231 | options_parse |= LYD_PARSE_ONLY | LYD_PARSE_NO_STATE; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 232 | } else if (!strcasecmp(optarg, "rpc") || !strcasecmp(optarg, "action")) { |
Michal Vasko | 1e4c68e | 2021-02-18 15:03:01 +0100 | [diff] [blame] | 233 | data_type = LYD_TYPE_RPC_YANG; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 234 | } else if (!strcasecmp(optarg, "reply") || !strcasecmp(optarg, "rpcreply")) { |
Michal Vasko | 1e4c68e | 2021-02-18 15:03:01 +0100 | [diff] [blame] | 235 | data_type = LYD_TYPE_REPLY_YANG; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 236 | } else if (!strcasecmp(optarg, "notif") || !strcasecmp(optarg, "notification")) { |
Michal Vasko | 1e4c68e | 2021-02-18 15:03:01 +0100 | [diff] [blame] | 237 | data_type = LYD_TYPE_NOTIF_YANG; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 238 | } 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 Vasko | d3b1054 | 2021-02-03 11:31:16 +0100 | [diff] [blame] | 282 | if (xpaths.count && outformat) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 283 | YLMSG_E("The --format option cannot be combined with --xpath option.\n"); |
| 284 | cmd_data_help(); |
| 285 | goto cleanup; |
| 286 | } |
| 287 | |
| 288 | if (operational && !data_type) { |
| 289 | YLMSG_W("Operational datastore takes effect only with RPCs/Actions/Replies/Notifications input data types.\n"); |
| 290 | free_cmdline_file(operational); |
| 291 | operational = NULL; |
| 292 | } |
| 293 | |
| 294 | /* process input data files provided as standalone command line arguments */ |
Radek Krejci | 6784a4e | 2020-12-09 14:23:05 +0100 | [diff] [blame] | 295 | for (int i = 0; i < argc - optind; i++) { |
| 296 | struct ly_in *in; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 297 | |
Michal Vasko | d3b1054 | 2021-02-03 11:31:16 +0100 | [diff] [blame] | 298 | if (get_input(argv[optind + i], NULL, &informat, &in)) { |
Radek Krejci | 6784a4e | 2020-12-09 14:23:05 +0100 | [diff] [blame] | 299 | goto cleanup; |
| 300 | } |
| 301 | |
Michal Vasko | d3b1054 | 2021-02-03 11:31:16 +0100 | [diff] [blame] | 302 | if (!fill_cmdline_file(&inputs, in, argv[optind + i], informat)) { |
Radek Krejci | 6784a4e | 2020-12-09 14:23:05 +0100 | [diff] [blame] | 303 | ly_in_free(in, 1); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 304 | goto cleanup; |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | /* default output stream */ |
| 309 | if (!out) { |
| 310 | if (ly_out_new_file(stdout, &out)) { |
| 311 | YLMSG_E("Unable to set stdout as output.\n"); |
| 312 | goto cleanup; |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | /* parse, validate and print data */ |
Michal Vasko | 3f08fb9 | 2022-04-21 09:52:35 +0200 | [diff] [blame] | 317 | if (process_data(*ctx, data_type, data_merge, outformat, out, options_parse, options_validate, options_print, |
| 318 | operational, NULL, &inputs, &xpaths)) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 319 | goto cleanup; |
| 320 | } |
| 321 | |
| 322 | cleanup: |
| 323 | ly_out_free(out, NULL, 0); |
| 324 | ly_set_erase(&inputs, free_cmdline_file); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 325 | ly_set_erase(&xpaths, NULL); |
| 326 | free_cmdline_file(operational); |
| 327 | free_cmdline(argv); |
| 328 | } |