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; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 127 | uint32_t options_validate = 0; |
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; |
| 230 | } else if (!strcasecmp(optarg, "getconfig") || !strcasecmp(optarg, "get-config")) { |
| 231 | options_parse |= LYD_PARSE_ONLY | LYD_PARSE_NO_STATE; |
| 232 | } else if (!strcasecmp(optarg, "edit")) { |
| 233 | options_parse |= LYD_PARSE_ONLY; |
| 234 | } else if (!strcasecmp(optarg, "rpc") || !strcasecmp(optarg, "action")) { |
Michal Vasko | 1e4c68e | 2021-02-18 15:03:01 +0100 | [diff] [blame^] | 235 | data_type = LYD_TYPE_RPC_YANG; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 236 | } else if (!strcasecmp(optarg, "reply") || !strcasecmp(optarg, "rpcreply")) { |
Michal Vasko | 1e4c68e | 2021-02-18 15:03:01 +0100 | [diff] [blame^] | 237 | data_type = LYD_TYPE_REPLY_YANG; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 238 | } else if (!strcasecmp(optarg, "notif") || !strcasecmp(optarg, "notification")) { |
Michal Vasko | 1e4c68e | 2021-02-18 15:03:01 +0100 | [diff] [blame^] | 239 | data_type = LYD_TYPE_NOTIF_YANG; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 240 | } else if (!strcasecmp(optarg, "data")) { |
| 241 | /* default option */ |
| 242 | } else { |
| 243 | YLMSG_E("Unknown data tree type %s.\n", optarg); |
| 244 | cmd_data_help(); |
| 245 | goto cleanup; |
| 246 | } |
| 247 | |
| 248 | data_type_set = 1; |
| 249 | break; |
| 250 | |
| 251 | case 'x': /* --xpath */ |
| 252 | if (ly_set_add(&xpaths, optarg, 0, NULL)) { |
| 253 | YLMSG_E("Storing XPath \"%s\" failed.\n", optarg); |
| 254 | goto cleanup; |
| 255 | } |
| 256 | break; |
| 257 | |
| 258 | case 'h': /* --help */ |
| 259 | cmd_data_help(); |
| 260 | goto cleanup; |
| 261 | default: |
| 262 | YLMSG_E("Unknown option.\n"); |
| 263 | goto cleanup; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | if (optind == argc) { |
| 268 | YLMSG_E("Missing the data file to process.\n"); |
| 269 | goto cleanup; |
| 270 | } |
| 271 | |
| 272 | if (data_merge) { |
| 273 | if (data_type || (options_parse & LYD_PARSE_ONLY)) { |
| 274 | /* switch off the option, incompatible input data type */ |
| 275 | data_merge = 0; |
| 276 | } else { |
| 277 | /* postpone validation after the merge of all the input data */ |
| 278 | options_parse |= LYD_PARSE_ONLY; |
| 279 | } |
| 280 | } else if (xpaths.count) { |
| 281 | data_merge = 1; |
| 282 | } |
| 283 | |
Michal Vasko | d3b1054 | 2021-02-03 11:31:16 +0100 | [diff] [blame] | 284 | if (xpaths.count && outformat) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 285 | YLMSG_E("The --format option cannot be combined with --xpath option.\n"); |
| 286 | cmd_data_help(); |
| 287 | goto cleanup; |
| 288 | } |
| 289 | |
| 290 | if (operational && !data_type) { |
| 291 | YLMSG_W("Operational datastore takes effect only with RPCs/Actions/Replies/Notifications input data types.\n"); |
| 292 | free_cmdline_file(operational); |
| 293 | operational = NULL; |
| 294 | } |
| 295 | |
| 296 | /* process input data files provided as standalone command line arguments */ |
Radek Krejci | 6784a4e | 2020-12-09 14:23:05 +0100 | [diff] [blame] | 297 | for (int i = 0; i < argc - optind; i++) { |
| 298 | struct ly_in *in; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 299 | |
Michal Vasko | d3b1054 | 2021-02-03 11:31:16 +0100 | [diff] [blame] | 300 | if (get_input(argv[optind + i], NULL, &informat, &in)) { |
Radek Krejci | 6784a4e | 2020-12-09 14:23:05 +0100 | [diff] [blame] | 301 | goto cleanup; |
| 302 | } |
| 303 | |
Michal Vasko | d3b1054 | 2021-02-03 11:31:16 +0100 | [diff] [blame] | 304 | if (!fill_cmdline_file(&inputs, in, argv[optind + i], informat)) { |
Radek Krejci | 6784a4e | 2020-12-09 14:23:05 +0100 | [diff] [blame] | 305 | ly_in_free(in, 1); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 306 | goto cleanup; |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | /* default output stream */ |
| 311 | if (!out) { |
| 312 | if (ly_out_new_file(stdout, &out)) { |
| 313 | YLMSG_E("Unable to set stdout as output.\n"); |
| 314 | goto cleanup; |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | /* parse, validate and print data */ |
Michal Vasko | d3b1054 | 2021-02-03 11:31:16 +0100 | [diff] [blame] | 319 | if (process_data(*ctx, data_type, data_merge, outformat, out, |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 320 | options_parse, options_validate, options_print, |
Radek Krejci | 6784a4e | 2020-12-09 14:23:05 +0100 | [diff] [blame] | 321 | operational, &inputs, &xpaths)) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 322 | goto cleanup; |
| 323 | } |
| 324 | |
| 325 | cleanup: |
| 326 | ly_out_free(out, NULL, 0); |
| 327 | ly_set_erase(&inputs, free_cmdline_file); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 328 | ly_set_erase(&xpaths, NULL); |
| 329 | free_cmdline_file(operational); |
| 330 | free_cmdline(argv); |
| 331 | } |