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 | { |
| 34 | printf("Usage: data [-ems] [-t TYPE]\n" |
| 35 | " [-f FORMAT] [-d DEFAULTS] [-o OUTFILE] <data1> ...\n" |
Radek Krejci | 6784a4e | 2020-12-09 14:23:05 +0100 | [diff] [blame] | 36 | " data [-s] -t (rpc | notif | reply) [-O FILE]\n" |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 37 | " [-f FORMAT] [-d DEFAULTS] [-o OUTFILE] <data1> ...\n" |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 38 | " data [-es] [-t TYPE] -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" |
| 65 | " -s, --strict Strict data parsing (do not skip unknown data), has no effect\n" |
| 66 | " for schemas.\n" |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 67 | " -O FILE, --operational=FILE\n" |
| 68 | " Provide optional data to extend validation of the 'rpc',\n" |
| 69 | " 'reply' or 'notif' TYPEs. The FILE is supposed to contain\n" |
| 70 | " the :running configuration datastore and state data\n" |
| 71 | " (operational datastore) referenced from the RPC/Notification.\n\n" |
| 72 | |
| 73 | " -f FORMAT, --format=FORMAT\n" |
| 74 | " Print the data in one of the following formats:\n" |
| 75 | " xml, json, lyb\n" |
| 76 | " Note that the LYB format requires the -o option specified.\n" |
| 77 | " -d MODE, --default=MODE\n" |
| 78 | " Print data with default values, according to the MODE\n" |
| 79 | " (to print attributes, ietf-netconf-with-defaults model\n" |
| 80 | " must be loaded):\n" |
| 81 | " all - Add missing default nodes.\n" |
| 82 | " all-tagged - Add missing default nodes and mark all the default\n" |
| 83 | " nodes with the attribute.\n" |
| 84 | " trim - Remove all nodes with a default value.\n" |
| 85 | " implicit-tagged - Add missing nodes and mark them with the attribute.\n" |
| 86 | " -o OUTFILE, --output=OUTFILE\n" |
| 87 | " Write the output to OUTFILE instead of stdout.\n\n" |
| 88 | |
| 89 | " -x XPATH, --xpath=XPATH\n" |
| 90 | " Evaluate XPATH expression and print the nodes satysfying the.\n" |
| 91 | " expression. The output format is specific and the option cannot\n" |
| 92 | " be combined with the -f and -d options. Also all the data\n" |
| 93 | " inputs are merged into a single data tree where the expression\n" |
| 94 | " is evaluated, so the -m option is always set implicitly.\n\n"); |
| 95 | |
| 96 | } |
| 97 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 98 | void |
| 99 | cmd_data(struct ly_ctx **ctx, const char *cmdline) |
| 100 | { |
| 101 | int argc = 0; |
| 102 | char **argv = NULL; |
| 103 | int opt, opt_index; |
| 104 | struct option options[] = { |
| 105 | {"defaults", required_argument, NULL, 'd'}, |
| 106 | {"present", no_argument, NULL, 'e'}, |
| 107 | {"format", required_argument, NULL, 'f'}, |
| 108 | {"help", no_argument, NULL, 'h'}, |
| 109 | {"merge", no_argument, NULL, 'm'}, |
| 110 | {"output", required_argument, NULL, 'o'}, |
| 111 | {"operational", required_argument, NULL, 'O'}, |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 112 | {"strict", no_argument, NULL, 's'}, |
| 113 | {"type", required_argument, NULL, 't'}, |
| 114 | {"xpath", required_argument, NULL, 'x'}, |
| 115 | {NULL, 0, NULL, 0} |
| 116 | }; |
| 117 | |
| 118 | uint8_t data_merge = 0; |
| 119 | uint32_t options_print = 0; |
| 120 | uint32_t options_parse = 0; |
| 121 | uint32_t options_validate = 0; |
| 122 | uint8_t data_type = 0; |
| 123 | uint8_t data_type_set = 0; |
| 124 | LYD_FORMAT format = LYD_UNKNOWN; |
| 125 | struct ly_out *out = NULL; |
| 126 | struct cmdline_file *operational = NULL; |
| 127 | struct ly_set inputs = {0}; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 128 | struct ly_set xpaths = {0}; |
| 129 | |
| 130 | if (parse_cmdline(cmdline, &argc, &argv)) { |
| 131 | goto cleanup; |
| 132 | } |
| 133 | |
| 134 | while ((opt = getopt_long(argc, argv, "d:ef:hmo:O:r:st:x:", options, &opt_index)) != -1) { |
| 135 | switch (opt) { |
| 136 | case 'd': /* --default */ |
| 137 | if (!strcasecmp(optarg, "all")) { |
| 138 | options_print = (options_print & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_ALL; |
| 139 | } else if (!strcasecmp(optarg, "all-tagged")) { |
| 140 | options_print = (options_print & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_ALL_TAG; |
| 141 | } else if (!strcasecmp(optarg, "trim")) { |
| 142 | options_print = (options_print & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_TRIM; |
| 143 | } else if (!strcasecmp(optarg, "implicit-tagged")) { |
| 144 | options_print = (options_print & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_IMPL_TAG; |
| 145 | } else { |
| 146 | YLMSG_E("Unknown default mode %s\n", optarg); |
| 147 | cmd_data_help(); |
| 148 | goto cleanup; |
| 149 | } |
| 150 | break; |
| 151 | case 'f': /* --format */ |
| 152 | if (!strcasecmp(optarg, "xml")) { |
| 153 | format = LYD_XML; |
| 154 | } else if (!strcasecmp(optarg, "json")) { |
| 155 | format = LYD_JSON; |
| 156 | } else if (!strcasecmp(optarg, "lyb")) { |
| 157 | format = LYD_LYB; |
| 158 | } else { |
| 159 | YLMSG_E("Unknown output format %s\n", optarg); |
| 160 | cmd_data_help(); |
| 161 | goto cleanup; |
| 162 | } |
| 163 | break; |
| 164 | case 'o': /* --output */ |
| 165 | if (out) { |
| 166 | YLMSG_E("Only a single output can be specified.\n"); |
| 167 | goto cleanup; |
| 168 | } else { |
| 169 | if (ly_out_new_filepath(optarg, &out)) { |
| 170 | YLMSG_E("Unable open output file %s (%s)\n", optarg, strerror(errno)); |
| 171 | goto cleanup; |
| 172 | } |
| 173 | } |
| 174 | break; |
| 175 | case 'O': { /* --operational */ |
| 176 | struct ly_in *in; |
| 177 | LYD_FORMAT f; |
| 178 | |
| 179 | if (operational) { |
| 180 | YLMSG_E("The operational datastore (-O) cannot be set multiple times.\n"); |
| 181 | goto cleanup; |
| 182 | } |
| 183 | if (get_input(optarg, NULL, &f, &in)) { |
| 184 | goto cleanup; |
| 185 | } |
| 186 | operational = fill_cmdline_file(NULL, in, optarg, f); |
| 187 | break; |
| 188 | } /* case 'O' */ |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 189 | |
| 190 | case 'e': /* --present */ |
| 191 | options_validate |= LYD_VALIDATE_PRESENT; |
| 192 | break; |
| 193 | case 'm': /* --merge */ |
| 194 | data_merge = 1; |
| 195 | break; |
| 196 | case 's': /* --strict */ |
| 197 | options_parse |= LYD_PARSE_STRICT; |
| 198 | break; |
| 199 | case 't': /* --type */ |
| 200 | if (data_type_set) { |
| 201 | YLMSG_E("The data type (-t) cannot be set multiple times.\n"); |
| 202 | goto cleanup; |
| 203 | } |
| 204 | |
| 205 | if (!strcasecmp(optarg, "config")) { |
| 206 | options_parse |= LYD_PARSE_NO_STATE; |
| 207 | } else if (!strcasecmp(optarg, "get")) { |
| 208 | options_parse |= LYD_PARSE_ONLY; |
| 209 | } else if (!strcasecmp(optarg, "getconfig") || !strcasecmp(optarg, "get-config")) { |
| 210 | options_parse |= LYD_PARSE_ONLY | LYD_PARSE_NO_STATE; |
| 211 | } else if (!strcasecmp(optarg, "edit")) { |
| 212 | options_parse |= LYD_PARSE_ONLY; |
| 213 | } else if (!strcasecmp(optarg, "rpc") || !strcasecmp(optarg, "action")) { |
| 214 | data_type = LYD_VALIDATE_OP_RPC; |
| 215 | } else if (!strcasecmp(optarg, "reply") || !strcasecmp(optarg, "rpcreply")) { |
| 216 | data_type = LYD_VALIDATE_OP_REPLY; |
| 217 | } else if (!strcasecmp(optarg, "notif") || !strcasecmp(optarg, "notification")) { |
| 218 | data_type = LYD_VALIDATE_OP_NOTIF; |
| 219 | } else if (!strcasecmp(optarg, "data")) { |
| 220 | /* default option */ |
| 221 | } else { |
| 222 | YLMSG_E("Unknown data tree type %s.\n", optarg); |
| 223 | cmd_data_help(); |
| 224 | goto cleanup; |
| 225 | } |
| 226 | |
| 227 | data_type_set = 1; |
| 228 | break; |
| 229 | |
| 230 | case 'x': /* --xpath */ |
| 231 | if (ly_set_add(&xpaths, optarg, 0, NULL)) { |
| 232 | YLMSG_E("Storing XPath \"%s\" failed.\n", optarg); |
| 233 | goto cleanup; |
| 234 | } |
| 235 | break; |
| 236 | |
| 237 | case 'h': /* --help */ |
| 238 | cmd_data_help(); |
| 239 | goto cleanup; |
| 240 | default: |
| 241 | YLMSG_E("Unknown option.\n"); |
| 242 | goto cleanup; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | if (optind == argc) { |
| 247 | YLMSG_E("Missing the data file to process.\n"); |
| 248 | goto cleanup; |
| 249 | } |
| 250 | |
| 251 | if (data_merge) { |
| 252 | if (data_type || (options_parse & LYD_PARSE_ONLY)) { |
| 253 | /* switch off the option, incompatible input data type */ |
| 254 | data_merge = 0; |
| 255 | } else { |
| 256 | /* postpone validation after the merge of all the input data */ |
| 257 | options_parse |= LYD_PARSE_ONLY; |
| 258 | } |
| 259 | } else if (xpaths.count) { |
| 260 | data_merge = 1; |
| 261 | } |
| 262 | |
| 263 | if (xpaths.count && format) { |
| 264 | YLMSG_E("The --format option cannot be combined with --xpath option.\n"); |
| 265 | cmd_data_help(); |
| 266 | goto cleanup; |
| 267 | } |
| 268 | |
| 269 | if (operational && !data_type) { |
| 270 | YLMSG_W("Operational datastore takes effect only with RPCs/Actions/Replies/Notifications input data types.\n"); |
| 271 | free_cmdline_file(operational); |
| 272 | operational = NULL; |
| 273 | } |
| 274 | |
| 275 | /* process input data files provided as standalone command line arguments */ |
Radek Krejci | 6784a4e | 2020-12-09 14:23:05 +0100 | [diff] [blame] | 276 | for (int i = 0; i < argc - optind; i++) { |
| 277 | struct ly_in *in; |
| 278 | LYD_FORMAT format = LYD_UNKNOWN; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 279 | |
Radek Krejci | 6784a4e | 2020-12-09 14:23:05 +0100 | [diff] [blame] | 280 | if (get_input(argv[optind + i], NULL, &format, &in)) { |
| 281 | goto cleanup; |
| 282 | } |
| 283 | |
| 284 | if (fill_cmdline_file(&inputs, in, argv[optind + i], format)) { |
| 285 | ly_in_free(in, 1); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 286 | goto cleanup; |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | /* default output stream */ |
| 291 | if (!out) { |
| 292 | if (ly_out_new_file(stdout, &out)) { |
| 293 | YLMSG_E("Unable to set stdout as output.\n"); |
| 294 | goto cleanup; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | /* parse, validate and print data */ |
| 299 | if (process_data(*ctx, data_type, data_merge, format, out, |
| 300 | options_parse, options_validate, options_print, |
Radek Krejci | 6784a4e | 2020-12-09 14:23:05 +0100 | [diff] [blame] | 301 | operational, &inputs, &xpaths)) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 302 | goto cleanup; |
| 303 | } |
| 304 | |
| 305 | cleanup: |
| 306 | ly_out_free(out, NULL, 0); |
| 307 | ly_set_erase(&inputs, free_cmdline_file); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 308 | ly_set_erase(&xpaths, NULL); |
| 309 | free_cmdline_file(operational); |
| 310 | free_cmdline(argv); |
| 311 | } |