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> |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 5 | * @author Adam Piecek <piecek@cesnet.cz> |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 6 | * @brief 'data' command of the libyang's yanglint tool. |
| 7 | * |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 8 | * Copyright (c) 2015-2023 CESNET, z.s.p.o. |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 9 | * |
| 10 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 11 | * You may not use this file except in compliance with the License. |
| 12 | * You may obtain a copy of the License at |
| 13 | * |
| 14 | * https://opensource.org/licenses/BSD-3-Clause |
| 15 | */ |
| 16 | |
| 17 | #define _GNU_SOURCE |
| 18 | |
| 19 | #include "cmd.h" |
| 20 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 21 | #include <assert.h> |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 22 | #include <errno.h> |
| 23 | #include <getopt.h> |
| 24 | #include <stdint.h> |
| 25 | #include <stdio.h> |
| 26 | #include <string.h> |
| 27 | #include <strings.h> |
| 28 | |
| 29 | #include "libyang.h" |
| 30 | |
| 31 | #include "common.h" |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 32 | #include "yl_opt.h" |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 33 | |
aPiecek | b073f6a | 2023-04-28 15:23:25 +0200 | [diff] [blame] | 34 | static void |
| 35 | cmd_data_help_header(void) |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 36 | { |
Michal Vasko | d3b1054 | 2021-02-03 11:31:16 +0100 | [diff] [blame] | 37 | printf("Usage: data [-emn] [-t TYPE]\n" |
| 38 | " [-F FORMAT] [-f FORMAT] [-d DEFAULTS] [-o OUTFILE] <data1> ...\n" |
| 39 | " data [-n] -t (rpc | notif | reply) [-O FILE]\n" |
| 40 | " [-F FORMAT] [-f FORMAT] [-d DEFAULTS] [-o OUTFILE] <data1> ...\n" |
| 41 | " data [-en] [-t TYPE] [-F FORMAT] -x XPATH [-o OUTFILE] <data1> ...\n" |
aPiecek | b073f6a | 2023-04-28 15:23:25 +0200 | [diff] [blame] | 42 | " Parse, validate and optionally print data instances\n"); |
| 43 | } |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 44 | |
aPiecek | b073f6a | 2023-04-28 15:23:25 +0200 | [diff] [blame] | 45 | static void |
| 46 | cmd_data_help_type(void) |
| 47 | { |
| 48 | printf(" -t TYPE, --type=TYPE\n" |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 49 | " Specify data tree type in the input data file(s):\n" |
| 50 | " data - Complete datastore with status data (default type).\n" |
| 51 | " config - Configuration datastore (without status data).\n" |
| 52 | " get - Result of the NETCONF <get> operation.\n" |
| 53 | " getconfig - Result of the NETCONF <get-config> operation.\n" |
| 54 | " edit - Content of the NETCONF <edit-config> operation.\n" |
| 55 | " rpc - Content of the NETCONF <rpc> message, defined as YANG's\n" |
| 56 | " RPC/Action input statement.\n" |
aPiecek | 860e34f | 2023-04-28 10:01:32 +0200 | [diff] [blame] | 57 | " nc-rpc - Similar to 'rpc' but expect and check also the NETCONF\n" |
| 58 | " envelopes <rpc> or <action>.\n" |
Radek Krejci | 6784a4e | 2020-12-09 14:23:05 +0100 | [diff] [blame] | 59 | " reply - Reply to the RPC/Action. Note that the reply data are\n" |
| 60 | " expected inside a container representing the original\n" |
| 61 | " RPC/Action. This is necessary to identify appropriate\n" |
| 62 | " data definitions in the schema module.\n" |
aPiecek | 079bcde | 2023-05-05 11:48:25 +0200 | [diff] [blame] | 63 | " nc-reply - Similar to 'reply' but expect and check also the NETCONF\n" |
| 64 | " envelope <rpc-reply> with output data nodes as direct\n" |
| 65 | " descendants. The original RPC/action invocation is expected\n" |
| 66 | " in a separate parameter '-R' and is parsed as 'nc-rpc'.\n" |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 67 | " notif - Notification instance (content of the <notification>\n" |
aPiecek | 6d2c1e7 | 2023-04-28 15:54:48 +0200 | [diff] [blame] | 68 | " element without <eventTime>).\n" |
| 69 | " nc-notif - Similar to 'notif' but expect and check also the NETCONF\n" |
| 70 | " envelope <notification> with element <eventTime> and its\n" |
| 71 | " sibling as the actual notification.\n"); |
aPiecek | b073f6a | 2023-04-28 15:23:25 +0200 | [diff] [blame] | 72 | } |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 73 | |
aPiecek | b073f6a | 2023-04-28 15:23:25 +0200 | [diff] [blame] | 74 | static void |
| 75 | cmd_data_help_format(void) |
| 76 | { |
| 77 | printf(" -f FORMAT, --format=FORMAT\n" |
| 78 | " Print the data in one of the following formats:\n" |
| 79 | " xml, json, lyb\n" |
| 80 | " Note that the LYB format requires the -o option specified.\n"); |
| 81 | } |
| 82 | |
| 83 | static void |
| 84 | cmd_data_help_in_format(void) |
| 85 | { |
| 86 | printf(" -F FORMAT, --in-format=FORMAT\n" |
| 87 | " Load the data in one of the following formats:\n" |
| 88 | " xml, json, lyb\n" |
| 89 | " If input format not specified, it is detected from the file extension.\n"); |
| 90 | } |
| 91 | |
| 92 | static void |
| 93 | cmd_data_help_default(void) |
| 94 | { |
| 95 | printf(" -d MODE, --default=MODE\n" |
| 96 | " Print data with default values, according to the MODE\n" |
| 97 | " (to print attributes, ietf-netconf-with-defaults model\n" |
| 98 | " must be loaded):\n" |
| 99 | " all - Add missing default nodes.\n" |
| 100 | " all-tagged - Add missing default nodes and mark all the default\n" |
| 101 | " nodes with the attribute.\n" |
| 102 | " trim - Remove all nodes with a default value.\n" |
| 103 | " implicit-tagged - Add missing nodes and mark them with the attribute.\n"); |
| 104 | } |
| 105 | |
| 106 | static void |
| 107 | cmd_data_help_xpath(void) |
| 108 | { |
| 109 | printf(" -x XPATH, --xpath=XPATH\n" |
aPiecek | 4195527 | 2023-05-10 16:10:17 +0200 | [diff] [blame] | 110 | " Evaluate XPATH expression and print the nodes satisfying the\n" |
aPiecek | b073f6a | 2023-04-28 15:23:25 +0200 | [diff] [blame] | 111 | " expression. The output format is specific and the option cannot\n" |
| 112 | " be combined with the -f and -d options. Also all the data\n" |
| 113 | " inputs are merged into a single data tree where the expression\n" |
| 114 | " is evaluated, so the -m option is always set implicitly.\n"); |
| 115 | } |
| 116 | |
| 117 | void |
| 118 | cmd_data_help(void) |
| 119 | { |
| 120 | cmd_data_help_header(); |
| 121 | printf("\n"); |
| 122 | cmd_data_help_type(); |
| 123 | printf(" -e, --present Validate only with the schema modules whose data actually\n" |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 124 | " exist in the provided input data files. Takes effect only\n" |
| 125 | " with the 'data' or 'config' TYPEs. Used to avoid requiring\n" |
| 126 | " mandatory nodes from modules which data are not present in the\n" |
| 127 | " provided input data files.\n" |
| 128 | " -m, --merge Merge input data files into a single tree and validate at\n" |
| 129 | " once.The option has effect only for 'data' and 'config' TYPEs.\n" |
| 130 | " In case of using -x option, the data are always merged.\n" |
Michal Vasko | c431a0a | 2021-01-25 14:31:58 +0100 | [diff] [blame] | 131 | " -n, --not-strict\n" |
| 132 | " Do not require strict data parsing (silently skip unknown data),\n" |
aPiecek | b073f6a | 2023-04-28 15:23:25 +0200 | [diff] [blame] | 133 | " has no effect for schemas.\n" |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 134 | " -O FILE, --operational=FILE\n" |
| 135 | " Provide optional data to extend validation of the 'rpc',\n" |
| 136 | " 'reply' or 'notif' TYPEs. The FILE is supposed to contain\n" |
aPiecek | a40764b | 2023-04-27 15:34:56 +0200 | [diff] [blame] | 137 | " the operational datastore referenced from the operation.\n" |
| 138 | " In case of a nested notification or action, its parent\n" |
aPiecek | 079bcde | 2023-05-05 11:48:25 +0200 | [diff] [blame] | 139 | " existence is also checked in these operational data.\n" |
| 140 | " -R FILE, --reply-rpc=FILE\n" |
| 141 | " Provide source RPC for parsing of the 'nc-reply' TYPE. The FILE\n" |
| 142 | " is supposed to contain the source 'nc-rpc' operation of the reply.\n"); |
aPiecek | b073f6a | 2023-04-28 15:23:25 +0200 | [diff] [blame] | 143 | cmd_data_help_format(); |
| 144 | cmd_data_help_in_format(); |
| 145 | printf(" -o OUTFILE, --output=OUTFILE\n" |
| 146 | " Write the output to OUTFILE instead of stdout.\n"); |
| 147 | cmd_data_help_xpath(); |
| 148 | printf("\n"); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 149 | } |
| 150 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 151 | int |
| 152 | cmd_data_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc) |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 153 | { |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 154 | int rc = 0, argc = 0; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 155 | int opt, opt_index; |
| 156 | struct option options[] = { |
Michal Vasko | 667ce6b | 2021-01-25 15:00:27 +0100 | [diff] [blame] | 157 | {"defaults", required_argument, NULL, 'd'}, |
| 158 | {"present", no_argument, NULL, 'e'}, |
| 159 | {"format", required_argument, NULL, 'f'}, |
Michal Vasko | d3b1054 | 2021-02-03 11:31:16 +0100 | [diff] [blame] | 160 | {"in-format", required_argument, NULL, 'F'}, |
Michal Vasko | 667ce6b | 2021-01-25 15:00:27 +0100 | [diff] [blame] | 161 | {"help", no_argument, NULL, 'h'}, |
| 162 | {"merge", no_argument, NULL, 'm'}, |
| 163 | {"output", required_argument, NULL, 'o'}, |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 164 | {"operational", required_argument, NULL, 'O'}, |
aPiecek | 079bcde | 2023-05-05 11:48:25 +0200 | [diff] [blame] | 165 | {"reply-rpc", required_argument, NULL, 'R'}, |
Michal Vasko | 667ce6b | 2021-01-25 15:00:27 +0100 | [diff] [blame] | 166 | {"not-strict", no_argument, NULL, 'n'}, |
| 167 | {"type", required_argument, NULL, 't'}, |
| 168 | {"xpath", required_argument, NULL, 'x'}, |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 169 | {NULL, 0, NULL, 0} |
| 170 | }; |
| 171 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 172 | uint8_t data_type_set = 0; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 173 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 174 | yo->data_parse_options = YL_DEFAULT_DATA_PARSE_OPTIONS; |
| 175 | yo->data_validate_options = YL_DEFAULT_DATA_VALIDATE_OPTIONS; |
| 176 | |
| 177 | if ((rc = parse_cmdline(cmdline, &argc, &yo->argv))) { |
| 178 | return rc; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 179 | } |
| 180 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 181 | while ((opt = getopt_long(argc, yo->argv, commands[CMD_DATA].optstring, options, &opt_index)) != -1) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 182 | switch (opt) { |
| 183 | case 'd': /* --default */ |
aPiecek | 20cc2fe | 2023-06-09 09:32:28 +0200 | [diff] [blame] | 184 | if (yo_opt_update_data_default(optarg, yo)) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 185 | YLMSG_E("Unknown default mode %s.", optarg); |
aPiecek | b073f6a | 2023-04-28 15:23:25 +0200 | [diff] [blame] | 186 | cmd_data_help_default(); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 187 | return 1; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 188 | } |
| 189 | break; |
| 190 | case 'f': /* --format */ |
aPiecek | 113e0f0 | 2023-06-09 08:47:48 +0200 | [diff] [blame] | 191 | if (yl_opt_update_data_out_format(optarg, yo)) { |
aPiecek | b073f6a | 2023-04-28 15:23:25 +0200 | [diff] [blame] | 192 | cmd_data_help_format(); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 193 | return 1; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 194 | } |
| 195 | break; |
Michal Vasko | d3b1054 | 2021-02-03 11:31:16 +0100 | [diff] [blame] | 196 | case 'F': /* --in-format */ |
aPiecek | b5dff49 | 2023-06-09 09:38:08 +0200 | [diff] [blame] | 197 | if (yo_opt_update_data_in_format(optarg, yo)) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 198 | YLMSG_E("Unknown input format %s.", optarg); |
aPiecek | b073f6a | 2023-04-28 15:23:25 +0200 | [diff] [blame] | 199 | cmd_data_help_in_format(); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 200 | return 1; |
Michal Vasko | d3b1054 | 2021-02-03 11:31:16 +0100 | [diff] [blame] | 201 | } |
| 202 | break; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 203 | case 'o': /* --output */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 204 | if (yo->out) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 205 | YLMSG_E("Only a single output can be specified."); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 206 | return 1; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 207 | } else { |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 208 | if (ly_out_new_filepath(optarg, &yo->out)) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 209 | YLMSG_E("Unable open output file %s (%s).", optarg, strerror(errno)); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 210 | return 1; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | break; |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 214 | case 'O': /* --operational */ |
| 215 | if (yo->data_operational.path) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 216 | YLMSG_E("The operational datastore (-O) cannot be set multiple times."); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 217 | return 1; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 218 | } |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 219 | yo->data_operational.path = optarg; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 220 | break; |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 221 | case 'R': /* --reply-rpc */ |
| 222 | if (yo->reply_rpc.path) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 223 | YLMSG_E("The PRC of the reply (-R) cannot be set multiple times."); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 224 | return 1; |
aPiecek | 079bcde | 2023-05-05 11:48:25 +0200 | [diff] [blame] | 225 | } |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 226 | yo->reply_rpc.path = optarg; |
aPiecek | 079bcde | 2023-05-05 11:48:25 +0200 | [diff] [blame] | 227 | break; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 228 | case 'e': /* --present */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 229 | yo->data_validate_options |= LYD_VALIDATE_PRESENT; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 230 | break; |
| 231 | case 'm': /* --merge */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 232 | yo->data_merge = 1; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 233 | break; |
Michal Vasko | 667ce6b | 2021-01-25 15:00:27 +0100 | [diff] [blame] | 234 | case 'n': /* --not-strict */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 235 | yo->data_parse_options &= ~LYD_PARSE_STRICT; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 236 | break; |
| 237 | case 't': /* --type */ |
| 238 | if (data_type_set) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 239 | YLMSG_E("The data type (-t) cannot be set multiple times."); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 240 | return 1; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 241 | } |
| 242 | |
aPiecek | 3167f38 | 2023-06-09 09:23:10 +0200 | [diff] [blame] | 243 | if (yl_opt_update_data_type(optarg, yo)) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 244 | YLMSG_E("Unknown data tree type %s.", optarg); |
aPiecek | b073f6a | 2023-04-28 15:23:25 +0200 | [diff] [blame] | 245 | cmd_data_help_type(); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 246 | return 1; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | data_type_set = 1; |
| 250 | break; |
| 251 | |
| 252 | case 'x': /* --xpath */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 253 | if (ly_set_add(&yo->data_xpath, optarg, 0, NULL)) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 254 | YLMSG_E("Storing XPath \"%s\" failed.", optarg); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 255 | return 1; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 256 | } |
| 257 | break; |
| 258 | |
| 259 | case 'h': /* --help */ |
| 260 | cmd_data_help(); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 261 | return 1; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 262 | default: |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 263 | YLMSG_E("Unknown option."); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 264 | return 1; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 265 | } |
| 266 | } |
| 267 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 268 | *posv = &yo->argv[optind]; |
| 269 | *posc = argc - optind; |
| 270 | |
| 271 | return rc; |
| 272 | } |
| 273 | |
| 274 | int |
| 275 | cmd_data_dep(struct yl_opt *yo, int posc) |
| 276 | { |
| 277 | if (yo->interactive && !posc) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 278 | YLMSG_E("Missing the data file to process."); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 279 | return 1; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 280 | } |
| 281 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 282 | if (yo->data_merge) { |
| 283 | if (yo->data_type || (yo->data_parse_options & LYD_PARSE_ONLY)) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 284 | /* switch off the option, incompatible input data type */ |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 285 | YLMSG_W("The --merge option has effect only for 'data' and 'config' TYPEs."); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 286 | yo->data_merge = 0; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 287 | } else { |
| 288 | /* postpone validation after the merge of all the input data */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 289 | yo->data_parse_options |= LYD_PARSE_ONLY; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 290 | } |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 291 | } else if (yo->data_xpath.count) { |
| 292 | yo->data_merge = 1; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 293 | } |
| 294 | |
aPiecek | bc39de1 | 2023-06-08 08:10:29 +0200 | [diff] [blame] | 295 | if (yo->data_xpath.count && (yo->schema_out_format || yo->data_out_format)) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 296 | YLMSG_E("The --format option cannot be combined with --xpath option."); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 297 | if (yo->interactive) { |
| 298 | cmd_data_help_xpath(); |
| 299 | } |
| 300 | return 1; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 301 | } |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 302 | if (yo->data_xpath.count && (yo->data_print_options & LYD_PRINT_WD_MASK)) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 303 | YLMSG_E("The --default option cannot be combined with --xpath option."); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 304 | if (yo->interactive) { |
| 305 | cmd_data_help_xpath(); |
| 306 | } |
| 307 | return 1; |
aPiecek | 72a24c9 | 2023-04-12 15:03:05 +0200 | [diff] [blame] | 308 | } |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 309 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 310 | if (yo->data_operational.path && !yo->data_type) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 311 | YLMSG_W("Operational datastore takes effect only with RPCs/Actions/Replies/Notification input data types."); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 312 | yo->data_operational.path = NULL; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 313 | } |
| 314 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 315 | if (yo->reply_rpc.path && (yo->data_type != LYD_TYPE_REPLY_NETCONF)) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 316 | YLMSG_W("Source RPC is needed only for NETCONF Reply input data type."); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 317 | yo->data_operational.path = NULL; |
| 318 | } else if (!yo->reply_rpc.path && (yo->data_type == LYD_TYPE_REPLY_NETCONF)) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 319 | YLMSG_E("Missing source RPC (-R) for NETCONF Reply input data type."); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 320 | return 1; |
aPiecek | 079bcde | 2023-05-05 11:48:25 +0200 | [diff] [blame] | 321 | } |
| 322 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 323 | if (!yo->out && (yo->data_out_format == LYD_LYB)) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 324 | YLMSG_E("The LYB format requires the -o option specified."); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 325 | return 1; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | /* default output stream */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 329 | if (!yo->out) { |
| 330 | if (ly_out_new_file(stdout, &yo->out)) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 331 | YLMSG_E("Unable to set stdout as output."); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 332 | return 1; |
| 333 | } |
| 334 | yo->out_stdout = 1; |
| 335 | } |
| 336 | |
| 337 | /* process the operational and/or reply RPC content if any */ |
| 338 | if (yo->data_operational.path) { |
| 339 | if (get_input(yo->data_operational.path, NULL, &yo->data_operational.format, &yo->data_operational.in)) { |
| 340 | return -1; |
| 341 | } |
| 342 | } |
| 343 | if (yo->reply_rpc.path) { |
| 344 | if (get_input(yo->reply_rpc.path, NULL, &yo->reply_rpc.format, &yo->reply_rpc.in)) { |
| 345 | return -1; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 346 | } |
| 347 | } |
| 348 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | int |
aPiecek | cf9e768 | 2023-06-19 14:03:50 +0200 | [diff] [blame] | 353 | cmd_data_store(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv) |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 354 | { |
| 355 | (void) ctx; |
| 356 | struct ly_in *in; |
| 357 | LYD_FORMAT format_data; |
| 358 | |
| 359 | assert(posv); |
| 360 | |
| 361 | format_data = yo->data_in_format; |
| 362 | |
| 363 | /* process input data files provided as standalone command line arguments */ |
| 364 | if (get_input(posv, NULL, &format_data, &in)) { |
| 365 | return 1; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 366 | } |
| 367 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 368 | if (!fill_cmdline_file(&yo->data_inputs, in, posv, format_data)) { |
| 369 | ly_in_free(in, 1); |
| 370 | return 1; |
| 371 | } |
| 372 | |
| 373 | return 0; |
| 374 | } |
| 375 | |
aPiecek | 618d1cc | 2023-06-15 08:31:27 +0200 | [diff] [blame] | 376 | /** |
| 377 | * @brief Evaluate xpath adn print result. |
| 378 | * |
| 379 | * @param[in] tree Data tree. |
| 380 | * @param[in] xpath Xpath to evaluate. |
| 381 | * @return 0 on success. |
| 382 | */ |
| 383 | static int |
| 384 | evaluate_xpath(const struct lyd_node *tree, const char *xpath) |
| 385 | { |
| 386 | struct ly_set *set = NULL; |
| 387 | |
| 388 | if (lyd_find_xpath(tree, xpath, &set)) { |
| 389 | return -1; |
| 390 | } |
| 391 | |
| 392 | /* print result */ |
| 393 | printf("XPath \"%s\" evaluation result:\n", xpath); |
| 394 | if (!set->count) { |
| 395 | printf("\tEmpty\n"); |
| 396 | } else { |
| 397 | for (uint32_t u = 0; u < set->count; ++u) { |
| 398 | struct lyd_node *node = (struct lyd_node *)set->objs[u]; |
| 399 | |
| 400 | printf(" %s \"%s\"", lys_nodetype2str(node->schema->nodetype), node->schema->name); |
| 401 | if (node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 402 | printf(" (value: \"%s\")\n", lyd_get_value(node)); |
| 403 | } else if (node->schema->nodetype == LYS_LIST) { |
| 404 | printf(" ("); |
| 405 | for (struct lyd_node *key = ((struct lyd_node_inner *)node)->child; key && lysc_is_key(key->schema); key = key->next) { |
| 406 | printf("%s\"%s\": \"%s\";", (key != ((struct lyd_node_inner *)node)->child) ? " " : "", |
| 407 | key->schema->name, lyd_get_value(key)); |
| 408 | } |
| 409 | printf(")\n"); |
| 410 | } else { |
| 411 | printf("\n"); |
| 412 | } |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | ly_set_free(set, NULL); |
| 417 | return 0; |
| 418 | } |
| 419 | |
| 420 | /** |
| 421 | * @brief Checking that a parent data node exists in the datastore for the nested-notification and action. |
| 422 | * |
| 423 | * @param[in] op Operation to check. |
| 424 | * @param[in] oper_tree Data from datastore. |
| 425 | * @param[in] operational_f Operational datastore file information. |
| 426 | * @return LY_ERR value. |
| 427 | */ |
| 428 | static LY_ERR |
| 429 | check_operation_parent(struct lyd_node *op, struct lyd_node *oper_tree, struct cmdline_file *operational_f) |
| 430 | { |
| 431 | LY_ERR ret; |
| 432 | struct ly_set *set = NULL; |
| 433 | char *path = NULL; |
| 434 | |
| 435 | if (!op || !lyd_parent(op)) { |
| 436 | /* The function is defined only for nested-notification and action. */ |
| 437 | return LY_SUCCESS; |
| 438 | } |
| 439 | |
| 440 | if (!operational_f || (operational_f && !operational_f->in)) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 441 | YLMSG_E("The --operational parameter needed to validate operation \"%s\" is missing.", LYD_NAME(op)); |
aPiecek | 618d1cc | 2023-06-15 08:31:27 +0200 | [diff] [blame] | 442 | ret = LY_EVALID; |
| 443 | goto cleanup; |
| 444 | } |
| 445 | |
| 446 | path = lyd_path(lyd_parent(op), LYD_PATH_STD, NULL, 0); |
| 447 | if (!path) { |
| 448 | ret = LY_EMEM; |
| 449 | goto cleanup; |
| 450 | } |
| 451 | |
| 452 | if (!oper_tree) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 453 | YLMSG_W("Operational datastore is empty or contains unknown data."); |
| 454 | YLMSG_E("Operation \"%s\" parent \"%s\" not found in the operational data.", LYD_NAME(op), path); |
aPiecek | 618d1cc | 2023-06-15 08:31:27 +0200 | [diff] [blame] | 455 | ret = LY_EVALID; |
| 456 | goto cleanup; |
| 457 | } |
| 458 | if ((ret = lyd_find_xpath(oper_tree, path, &set))) { |
| 459 | goto cleanup; |
| 460 | } |
| 461 | if (!set->count) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 462 | YLMSG_E("Operation \"%s\" parent \"%s\" not found in the operational data.", LYD_NAME(op), path); |
aPiecek | 618d1cc | 2023-06-15 08:31:27 +0200 | [diff] [blame] | 463 | ret = LY_EVALID; |
| 464 | goto cleanup; |
| 465 | } |
| 466 | |
| 467 | cleanup: |
| 468 | ly_set_free(set, NULL); |
| 469 | free(path); |
| 470 | |
| 471 | return ret; |
| 472 | } |
| 473 | |
| 474 | /** |
| 475 | * @brief Process the input data files - parse, validate and print according to provided options. |
| 476 | * |
| 477 | * @param[in] ctx libyang context with schema. |
| 478 | * @param[in] type The type of data in the input files. |
| 479 | * @param[in] merge Flag if the data should be merged before validation. |
| 480 | * @param[in] out_format Data format for printing. |
| 481 | * @param[in] out The output handler for printing. |
| 482 | * @param[in] parse_options Parser options. |
| 483 | * @param[in] validate_options Validation options. |
| 484 | * @param[in] print_options Printer options. |
| 485 | * @param[in] operational Optional operational datastore file information for the case of an extended validation of |
| 486 | * operation(s). |
| 487 | * @param[in] reply_rpc Source RPC operation file information for parsing NETCONF rpc-reply. |
| 488 | * @param[in] inputs Set of file informations of input data files. |
| 489 | * @param[in] xpaths The set of XPaths to be evaluated on the processed data tree, basic information about the resulting set |
| 490 | * is printed. Alternative to data printing. |
| 491 | * @return LY_ERR value. |
| 492 | */ |
| 493 | static LY_ERR |
| 494 | process_data(struct ly_ctx *ctx, enum lyd_type type, uint8_t merge, LYD_FORMAT out_format, |
| 495 | struct ly_out *out, uint32_t parse_options, uint32_t validate_options, uint32_t print_options, |
| 496 | struct cmdline_file *operational, struct cmdline_file *reply_rpc, struct ly_set *inputs, |
| 497 | struct ly_set *xpaths) |
| 498 | { |
| 499 | LY_ERR ret = LY_SUCCESS; |
| 500 | struct lyd_node *tree = NULL, *op = NULL, *envp = NULL, *merged_tree = NULL, *oper_tree = NULL; |
| 501 | const char *xpath; |
| 502 | struct ly_set *set = NULL; |
| 503 | |
| 504 | /* additional operational datastore */ |
| 505 | if (operational && operational->in) { |
| 506 | ret = lyd_parse_data(ctx, NULL, operational->in, operational->format, LYD_PARSE_ONLY, 0, &oper_tree); |
| 507 | if (ret) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 508 | YLMSG_E("Failed to parse operational datastore file \"%s\".", operational->path); |
aPiecek | 618d1cc | 2023-06-15 08:31:27 +0200 | [diff] [blame] | 509 | goto cleanup; |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | for (uint32_t u = 0; u < inputs->count; ++u) { |
| 514 | struct cmdline_file *input_f = (struct cmdline_file *)inputs->objs[u]; |
| 515 | |
| 516 | switch (type) { |
| 517 | case LYD_TYPE_DATA_YANG: |
| 518 | ret = lyd_parse_data(ctx, NULL, input_f->in, input_f->format, parse_options, validate_options, &tree); |
| 519 | break; |
| 520 | case LYD_TYPE_RPC_YANG: |
| 521 | case LYD_TYPE_REPLY_YANG: |
| 522 | case LYD_TYPE_NOTIF_YANG: |
| 523 | ret = lyd_parse_op(ctx, NULL, input_f->in, input_f->format, type, &tree, &op); |
| 524 | break; |
| 525 | case LYD_TYPE_RPC_NETCONF: |
| 526 | case LYD_TYPE_NOTIF_NETCONF: |
| 527 | ret = lyd_parse_op(ctx, NULL, input_f->in, input_f->format, type, &envp, &op); |
| 528 | |
| 529 | /* adjust pointers */ |
| 530 | for (tree = op; lyd_parent(tree); tree = lyd_parent(tree)) {} |
| 531 | break; |
| 532 | case LYD_TYPE_REPLY_NETCONF: |
| 533 | /* parse source RPC operation */ |
| 534 | assert(reply_rpc && reply_rpc->in); |
| 535 | ret = lyd_parse_op(ctx, NULL, reply_rpc->in, reply_rpc->format, LYD_TYPE_RPC_NETCONF, &envp, &op); |
| 536 | if (ret) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 537 | YLMSG_E("Failed to parse source NETCONF RPC operation file \"%s\".", reply_rpc->path); |
aPiecek | 618d1cc | 2023-06-15 08:31:27 +0200 | [diff] [blame] | 538 | goto cleanup; |
| 539 | } |
| 540 | |
| 541 | /* adjust pointers */ |
| 542 | for (tree = op; lyd_parent(tree); tree = lyd_parent(tree)) {} |
| 543 | |
| 544 | /* free input */ |
| 545 | lyd_free_siblings(lyd_child(op)); |
| 546 | |
| 547 | /* we do not care */ |
| 548 | lyd_free_all(envp); |
| 549 | envp = NULL; |
| 550 | |
| 551 | ret = lyd_parse_op(ctx, op, input_f->in, input_f->format, type, &envp, NULL); |
| 552 | break; |
| 553 | default: |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 554 | YLMSG_E("Internal error (%s:%d).", __FILE__, __LINE__); |
aPiecek | 618d1cc | 2023-06-15 08:31:27 +0200 | [diff] [blame] | 555 | goto cleanup; |
| 556 | } |
| 557 | |
| 558 | if (ret) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 559 | YLMSG_E("Failed to parse input data file \"%s\".", input_f->path); |
aPiecek | 618d1cc | 2023-06-15 08:31:27 +0200 | [diff] [blame] | 560 | goto cleanup; |
| 561 | } |
| 562 | |
| 563 | if (merge) { |
| 564 | /* merge the data so far parsed for later validation and print */ |
| 565 | if (!merged_tree) { |
| 566 | merged_tree = tree; |
| 567 | } else { |
| 568 | ret = lyd_merge_siblings(&merged_tree, tree, LYD_MERGE_DESTRUCT); |
| 569 | if (ret) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 570 | YLMSG_E("Merging %s with previous data failed.", input_f->path); |
aPiecek | 618d1cc | 2023-06-15 08:31:27 +0200 | [diff] [blame] | 571 | goto cleanup; |
| 572 | } |
| 573 | } |
| 574 | tree = NULL; |
| 575 | } else if (out_format) { |
| 576 | /* print */ |
| 577 | switch (type) { |
| 578 | case LYD_TYPE_DATA_YANG: |
| 579 | lyd_print_all(out, tree, out_format, print_options); |
| 580 | break; |
| 581 | case LYD_TYPE_RPC_YANG: |
| 582 | case LYD_TYPE_REPLY_YANG: |
| 583 | case LYD_TYPE_NOTIF_YANG: |
| 584 | case LYD_TYPE_RPC_NETCONF: |
| 585 | case LYD_TYPE_NOTIF_NETCONF: |
| 586 | lyd_print_tree(out, tree, out_format, print_options); |
| 587 | break; |
| 588 | case LYD_TYPE_REPLY_NETCONF: |
| 589 | /* just the output */ |
| 590 | lyd_print_tree(out, lyd_child(tree), out_format, print_options); |
| 591 | break; |
| 592 | default: |
| 593 | assert(0); |
| 594 | } |
| 595 | } else { |
| 596 | /* validation of the RPC/Action/reply/Notification with the operational datastore, if any */ |
| 597 | switch (type) { |
| 598 | case LYD_TYPE_DATA_YANG: |
| 599 | /* already validated */ |
| 600 | break; |
| 601 | case LYD_TYPE_RPC_YANG: |
| 602 | case LYD_TYPE_RPC_NETCONF: |
| 603 | ret = lyd_validate_op(tree, oper_tree, LYD_TYPE_RPC_YANG, NULL); |
| 604 | break; |
| 605 | case LYD_TYPE_REPLY_YANG: |
| 606 | case LYD_TYPE_REPLY_NETCONF: |
| 607 | ret = lyd_validate_op(tree, oper_tree, LYD_TYPE_REPLY_YANG, NULL); |
| 608 | break; |
| 609 | case LYD_TYPE_NOTIF_YANG: |
| 610 | case LYD_TYPE_NOTIF_NETCONF: |
| 611 | ret = lyd_validate_op(tree, oper_tree, LYD_TYPE_NOTIF_YANG, NULL); |
| 612 | break; |
| 613 | default: |
| 614 | assert(0); |
| 615 | } |
| 616 | if (ret) { |
| 617 | if (operational->path) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 618 | YLMSG_E("Failed to validate input data file \"%s\" with operational datastore \"%s\".", |
aPiecek | 618d1cc | 2023-06-15 08:31:27 +0200 | [diff] [blame] | 619 | input_f->path, operational->path); |
| 620 | } else { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 621 | YLMSG_E("Failed to validate input data file \"%s\".", input_f->path); |
aPiecek | 618d1cc | 2023-06-15 08:31:27 +0200 | [diff] [blame] | 622 | } |
| 623 | goto cleanup; |
| 624 | } |
| 625 | |
| 626 | if ((ret = check_operation_parent(op, oper_tree, operational))) { |
| 627 | goto cleanup; |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | /* next iter */ |
| 632 | lyd_free_all(tree); |
| 633 | tree = NULL; |
| 634 | lyd_free_all(envp); |
| 635 | envp = NULL; |
| 636 | } |
| 637 | |
| 638 | if (merge) { |
| 639 | /* validate the merged result */ |
| 640 | ret = lyd_validate_all(&merged_tree, ctx, validate_options, NULL); |
| 641 | if (ret) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 642 | YLMSG_E("Merged data are not valid."); |
aPiecek | 618d1cc | 2023-06-15 08:31:27 +0200 | [diff] [blame] | 643 | goto cleanup; |
| 644 | } |
| 645 | |
| 646 | if (out_format) { |
| 647 | /* and print it */ |
| 648 | lyd_print_all(out, merged_tree, out_format, print_options); |
| 649 | } |
| 650 | |
| 651 | for (uint32_t u = 0; xpaths && (u < xpaths->count); ++u) { |
| 652 | xpath = (const char *)xpaths->objs[u]; |
| 653 | ly_set_free(set, NULL); |
| 654 | ret = lys_find_xpath(ctx, NULL, xpath, LYS_FIND_NO_MATCH_ERROR, &set); |
| 655 | if (ret || !set->count) { |
| 656 | ret = (ret == LY_SUCCESS) ? LY_EINVAL : ret; |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 657 | YLMSG_E("The requested xpath failed."); |
aPiecek | 618d1cc | 2023-06-15 08:31:27 +0200 | [diff] [blame] | 658 | goto cleanup; |
| 659 | } |
| 660 | if (evaluate_xpath(merged_tree, xpath)) { |
| 661 | goto cleanup; |
| 662 | } |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | cleanup: |
| 667 | lyd_free_all(tree); |
| 668 | lyd_free_all(envp); |
| 669 | lyd_free_all(merged_tree); |
| 670 | lyd_free_all(oper_tree); |
| 671 | ly_set_free(set, NULL); |
| 672 | return ret; |
| 673 | } |
| 674 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 675 | int |
aPiecek | cf9e768 | 2023-06-19 14:03:50 +0200 | [diff] [blame] | 676 | cmd_data_process(struct ly_ctx *ctx, struct yl_opt *yo) |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 677 | { |
| 678 | /* parse, validate and print data */ |
| 679 | if (process_data(ctx, yo->data_type, yo->data_merge, yo->data_out_format, yo->out, yo->data_parse_options, |
| 680 | yo->data_validate_options, yo->data_print_options, &yo->data_operational, &yo->reply_rpc, |
| 681 | &yo->data_inputs, &yo->data_xpath)) { |
| 682 | return 1; |
| 683 | } |
| 684 | |
| 685 | return 0; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 686 | } |