Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file resolve.c |
| 3 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * @brief libyang resolve functions |
| 5 | * |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6 | * Copyright (c) 2015 - 2018 CESNET, z.s.p.o. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 7 | * |
Radek Krejci | 54f6fb3 | 2016-02-24 12:56:39 +0100 | [diff] [blame] | 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
Michal Vasko | 8de098c | 2016-02-26 10:00:25 +0100 | [diff] [blame] | 11 | * |
Radek Krejci | 54f6fb3 | 2016-02-24 12:56:39 +0100 | [diff] [blame] | 12 | * https://opensource.org/licenses/BSD-3-Clause |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 13 | */ |
| 14 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 15 | #define _GNU_SOURCE |
| 16 | |
| 17 | #include <stdlib.h> |
| 18 | #include <assert.h> |
| 19 | #include <string.h> |
| 20 | #include <ctype.h> |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 21 | #include <limits.h> |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 22 | |
| 23 | #include "libyang.h" |
| 24 | #include "resolve.h" |
| 25 | #include "common.h" |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 26 | #include "xpath.h" |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 27 | #include "parser.h" |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 28 | #include "parser_yang.h" |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 29 | #include "xml_internal.h" |
Michal Vasko | 6c81070 | 2018-03-14 16:23:21 +0100 | [diff] [blame] | 30 | #include "hash_table.h" |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 31 | #include "tree_internal.h" |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 32 | #include "extensions.h" |
Michal Vasko | 185b527 | 2018-09-13 14:26:12 +0200 | [diff] [blame] | 33 | #include "validation.h" |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 34 | |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 35 | /* internal parsed predicate structure */ |
| 36 | struct parsed_pred { |
| 37 | const struct lys_node *schema; |
Michal Vasko | c340b3d | 2020-06-29 10:47:11 +0200 | [diff] [blame] | 38 | const char *pred_str; |
| 39 | int pred_str_len; |
| 40 | |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 41 | int len; |
| 42 | struct { |
| 43 | const char *mod_name; |
| 44 | int mod_name_len; |
| 45 | const char *name; |
| 46 | int nam_len; |
| 47 | const char *value; |
| 48 | int val_len; |
| 49 | } *pred; |
| 50 | }; |
| 51 | |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 52 | int |
| 53 | parse_range_dec64(const char **str_num, uint8_t dig, int64_t *num) |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 54 | { |
| 55 | const char *ptr; |
| 56 | int minus = 0; |
Michal Vasko | e2ea45a | 2017-08-07 13:15:07 +0200 | [diff] [blame] | 57 | int64_t ret = 0, prev_ret; |
Radek Krejci | bf47a82 | 2016-11-04 10:06:08 +0100 | [diff] [blame] | 58 | int8_t str_exp, str_dig = -1, trailing_zeros = 0; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 59 | |
| 60 | ptr = *str_num; |
| 61 | |
| 62 | if (ptr[0] == '-') { |
| 63 | minus = 1; |
| 64 | ++ptr; |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 65 | } else if (ptr[0] == '+') { |
| 66 | ++ptr; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 67 | } |
| 68 | |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 69 | if (!isdigit(ptr[0])) { |
| 70 | /* there must be at least one */ |
| 71 | return 1; |
| 72 | } |
| 73 | |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 74 | for (str_exp = 0; isdigit(ptr[0]) || ((ptr[0] == '.') && (str_dig < 0)); ++ptr) { |
| 75 | if (str_exp > 18) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 76 | return 1; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | if (ptr[0] == '.') { |
| 80 | if (ptr[1] == '.') { |
| 81 | /* it's the next interval */ |
| 82 | break; |
| 83 | } |
| 84 | ++str_dig; |
| 85 | } else { |
Michal Vasko | e2ea45a | 2017-08-07 13:15:07 +0200 | [diff] [blame] | 86 | prev_ret = ret; |
| 87 | if (minus) { |
| 88 | ret = ret * 10 - (ptr[0] - '0'); |
| 89 | if (ret > prev_ret) { |
| 90 | return 1; |
| 91 | } |
| 92 | } else { |
| 93 | ret = ret * 10 + (ptr[0] - '0'); |
| 94 | if (ret < prev_ret) { |
| 95 | return 1; |
| 96 | } |
| 97 | } |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 98 | if (str_dig > -1) { |
| 99 | ++str_dig; |
Radek Krejci | bf47a82 | 2016-11-04 10:06:08 +0100 | [diff] [blame] | 100 | if (ptr[0] == '0') { |
| 101 | /* possibly trailing zero */ |
| 102 | trailing_zeros++; |
| 103 | } else { |
| 104 | trailing_zeros = 0; |
| 105 | } |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 106 | } |
| 107 | ++str_exp; |
| 108 | } |
| 109 | } |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 110 | if (str_dig == 0) { |
| 111 | /* no digits after '.' */ |
| 112 | return 1; |
| 113 | } else if (str_dig == -1) { |
| 114 | /* there are 0 numbers after the floating point */ |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 115 | str_dig = 0; |
| 116 | } |
Radek Krejci | bf47a82 | 2016-11-04 10:06:08 +0100 | [diff] [blame] | 117 | /* remove trailing zeros */ |
| 118 | if (trailing_zeros) { |
Michal Vasko | 6ca5ca7 | 2016-11-28 09:21:51 +0100 | [diff] [blame] | 119 | str_dig -= trailing_zeros; |
| 120 | str_exp -= trailing_zeros; |
Radek Krejci | bf47a82 | 2016-11-04 10:06:08 +0100 | [diff] [blame] | 121 | ret = ret / dec_pow(trailing_zeros); |
| 122 | } |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 123 | |
| 124 | /* it's parsed, now adjust the number based on fraction-digits, if needed */ |
| 125 | if (str_dig < dig) { |
| 126 | if ((str_exp - 1) + (dig - str_dig) > 18) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 127 | return 1; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 128 | } |
Michal Vasko | e2ea45a | 2017-08-07 13:15:07 +0200 | [diff] [blame] | 129 | prev_ret = ret; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 130 | ret *= dec_pow(dig - str_dig); |
Michal Vasko | e2ea45a | 2017-08-07 13:15:07 +0200 | [diff] [blame] | 131 | if ((minus && (ret > prev_ret)) || (!minus && (ret < prev_ret))) { |
| 132 | return 1; |
| 133 | } |
| 134 | |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 135 | } |
| 136 | if (str_dig > dig) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 137 | return 1; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 138 | } |
| 139 | |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 140 | *str_num = ptr; |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 141 | *num = ret; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 142 | |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 143 | return 0; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | /** |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 147 | * @brief Parse an identifier. |
| 148 | * |
| 149 | * ;; An identifier MUST NOT start with (('X'|'x') ('M'|'m') ('L'|'l')) |
| 150 | * identifier = (ALPHA / "_") |
| 151 | * *(ALPHA / DIGIT / "_" / "-" / ".") |
| 152 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 153 | * @param[in] id Identifier to use. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 154 | * |
| 155 | * @return Number of characters successfully parsed. |
| 156 | */ |
Radek Krejci | dce5f97 | 2017-09-12 15:47:49 +0200 | [diff] [blame] | 157 | unsigned int |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 158 | parse_identifier(const char *id) |
| 159 | { |
Radek Krejci | dce5f97 | 2017-09-12 15:47:49 +0200 | [diff] [blame] | 160 | unsigned int parsed = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 161 | |
Michal Vasko | 1ab90bc | 2016-03-15 10:40:22 +0100 | [diff] [blame] | 162 | assert(id); |
| 163 | |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 164 | if (!isalpha(id[0]) && (id[0] != '_')) { |
| 165 | return -parsed; |
| 166 | } |
| 167 | |
| 168 | ++parsed; |
| 169 | ++id; |
| 170 | |
| 171 | while (isalnum(id[0]) || (id[0] == '_') || (id[0] == '-') || (id[0] == '.')) { |
| 172 | ++parsed; |
| 173 | ++id; |
| 174 | } |
| 175 | |
| 176 | return parsed; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * @brief Parse a node-identifier. |
| 181 | * |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 182 | * node-identifier = [module-name ":"] identifier |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 183 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 184 | * @param[in] id Identifier to use. |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 185 | * @param[out] mod_name Points to the module name, NULL if there is not any. |
| 186 | * @param[out] mod_name_len Length of the module name, 0 if there is not any. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 187 | * @param[out] name Points to the node name. |
| 188 | * @param[out] nam_len Length of the node name. |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 189 | * @param[out] all_desc Whether the path starts with '/', only supported in extended paths. |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 190 | * @param[in] extended Whether to accept an extended path (support for [prefix:]*, /[prefix:]*, /[prefix:]., prefix:#identifier). |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 191 | * |
| 192 | * @return Number of characters successfully parsed, |
| 193 | * positive on success, negative on failure. |
| 194 | */ |
| 195 | static int |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 196 | parse_node_identifier(const char *id, const char **mod_name, int *mod_name_len, const char **name, int *nam_len, |
| 197 | int *all_desc, int extended) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 198 | { |
Michal Vasko | 299961d | 2019-02-18 10:20:51 +0100 | [diff] [blame] | 199 | int parsed = 0, ret, all_desc_local = 0, first_id_len; |
| 200 | const char *first_id; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 201 | |
| 202 | assert(id); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 203 | assert((mod_name && mod_name_len) || (!mod_name && !mod_name_len)); |
| 204 | assert((name && nam_len) || (!name && !nam_len)); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 205 | |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 206 | if (mod_name) { |
| 207 | *mod_name = NULL; |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 208 | *mod_name_len = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 209 | } |
| 210 | if (name) { |
| 211 | *name = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 212 | *nam_len = 0; |
| 213 | } |
| 214 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 215 | if (extended) { |
| 216 | /* try to parse only the extended expressions */ |
| 217 | if (id[parsed] == '/') { |
PavolVican | 195cf39 | 2018-02-23 13:24:45 +0100 | [diff] [blame] | 218 | if (all_desc) { |
| 219 | *all_desc = 1; |
| 220 | } |
| 221 | all_desc_local = 1; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 222 | } else { |
PavolVican | 195cf39 | 2018-02-23 13:24:45 +0100 | [diff] [blame] | 223 | if (all_desc) { |
| 224 | *all_desc = 0; |
| 225 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | /* is there a prefix? */ |
PavolVican | 195cf39 | 2018-02-23 13:24:45 +0100 | [diff] [blame] | 229 | ret = parse_identifier(id + all_desc_local); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 230 | if (ret > 0) { |
PavolVican | 195cf39 | 2018-02-23 13:24:45 +0100 | [diff] [blame] | 231 | if (id[all_desc_local + ret] != ':') { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 232 | /* this is not a prefix, so not an extended id */ |
| 233 | goto standard_id; |
| 234 | } |
| 235 | |
| 236 | if (mod_name) { |
PavolVican | 195cf39 | 2018-02-23 13:24:45 +0100 | [diff] [blame] | 237 | *mod_name = id + all_desc_local; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 238 | *mod_name_len = ret; |
| 239 | } |
| 240 | |
| 241 | /* "/" and ":" */ |
PavolVican | 195cf39 | 2018-02-23 13:24:45 +0100 | [diff] [blame] | 242 | ret += all_desc_local + 1; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 243 | } else { |
PavolVican | 195cf39 | 2018-02-23 13:24:45 +0100 | [diff] [blame] | 244 | ret = all_desc_local; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | /* parse either "*" or "." */ |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 248 | if (*(id + ret) == '*') { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 249 | if (name) { |
| 250 | *name = id + ret; |
| 251 | *nam_len = 1; |
| 252 | } |
| 253 | ++ret; |
| 254 | |
| 255 | return ret; |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 256 | } else if (*(id + ret) == '.') { |
PavolVican | 195cf39 | 2018-02-23 13:24:45 +0100 | [diff] [blame] | 257 | if (!all_desc_local) { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 258 | /* /. is redundant expression, we do not accept it */ |
| 259 | return -ret; |
| 260 | } |
| 261 | |
| 262 | if (name) { |
| 263 | *name = id + ret; |
| 264 | *nam_len = 1; |
| 265 | } |
| 266 | ++ret; |
| 267 | |
| 268 | return ret; |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 269 | } else if (*(id + ret) == '#') { |
PavolVican | 195cf39 | 2018-02-23 13:24:45 +0100 | [diff] [blame] | 270 | if (all_desc_local || !ret) { |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 271 | /* no prefix */ |
| 272 | return 0; |
| 273 | } |
| 274 | parsed = ret + 1; |
| 275 | if ((ret = parse_identifier(id + parsed)) < 1) { |
| 276 | return -parsed + ret; |
| 277 | } |
| 278 | *name = id + parsed - 1; |
| 279 | *nam_len = ret + 1; |
| 280 | return parsed + ret; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 281 | } |
| 282 | /* else a standard id, parse it all again */ |
| 283 | } |
| 284 | |
| 285 | standard_id: |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 286 | if ((ret = parse_identifier(id)) < 1) { |
| 287 | return ret; |
| 288 | } |
| 289 | |
Michal Vasko | 299961d | 2019-02-18 10:20:51 +0100 | [diff] [blame] | 290 | first_id = id; |
| 291 | first_id_len = ret; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 292 | |
| 293 | parsed += ret; |
| 294 | id += ret; |
| 295 | |
| 296 | /* there is prefix */ |
| 297 | if (id[0] == ':') { |
| 298 | ++parsed; |
| 299 | ++id; |
| 300 | |
| 301 | /* there isn't */ |
| 302 | } else { |
Michal Vasko | 299961d | 2019-02-18 10:20:51 +0100 | [diff] [blame] | 303 | if (name) { |
| 304 | *name = first_id; |
| 305 | *nam_len = first_id_len; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | return parsed; |
| 309 | } |
| 310 | |
| 311 | /* identifier (node name) */ |
| 312 | if ((ret = parse_identifier(id)) < 1) { |
Michal Vasko | 299961d | 2019-02-18 10:20:51 +0100 | [diff] [blame] | 313 | return -parsed + ret; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 314 | } |
| 315 | |
Michal Vasko | 299961d | 2019-02-18 10:20:51 +0100 | [diff] [blame] | 316 | if (mod_name) { |
| 317 | *mod_name = first_id; |
| 318 | *mod_name_len = first_id_len; |
| 319 | } |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 320 | if (name) { |
| 321 | *name = id; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 322 | *nam_len = ret; |
| 323 | } |
| 324 | |
Michal Vasko | 299961d | 2019-02-18 10:20:51 +0100 | [diff] [blame] | 325 | return parsed + ret; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | /** |
| 329 | * @brief Parse a path-predicate (leafref). |
| 330 | * |
| 331 | * path-predicate = "[" *WSP path-equality-expr *WSP "]" |
| 332 | * path-equality-expr = node-identifier *WSP "=" *WSP path-key-expr |
| 333 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 334 | * @param[in] id Identifier to use. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 335 | * @param[out] prefix Points to the prefix, NULL if there is not any. |
| 336 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 337 | * @param[out] name Points to the node name. |
| 338 | * @param[out] nam_len Length of the node name. |
| 339 | * @param[out] path_key_expr Points to the path-key-expr. |
| 340 | * @param[out] pke_len Length of the path-key-expr. |
| 341 | * @param[out] has_predicate Flag to mark whether there is another predicate following. |
| 342 | * |
| 343 | * @return Number of characters successfully parsed, |
| 344 | * positive on success, negative on failure. |
| 345 | */ |
| 346 | static int |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 347 | parse_path_predicate(const char *id, const char **prefix, int *pref_len, const char **name, int *nam_len, |
| 348 | const char **path_key_expr, int *pke_len, int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 349 | { |
| 350 | const char *ptr; |
| 351 | int parsed = 0, ret; |
| 352 | |
| 353 | assert(id); |
| 354 | if (prefix) { |
| 355 | *prefix = NULL; |
| 356 | } |
| 357 | if (pref_len) { |
| 358 | *pref_len = 0; |
| 359 | } |
| 360 | if (name) { |
| 361 | *name = NULL; |
| 362 | } |
| 363 | if (nam_len) { |
| 364 | *nam_len = 0; |
| 365 | } |
| 366 | if (path_key_expr) { |
| 367 | *path_key_expr = NULL; |
| 368 | } |
| 369 | if (pke_len) { |
| 370 | *pke_len = 0; |
| 371 | } |
| 372 | if (has_predicate) { |
| 373 | *has_predicate = 0; |
| 374 | } |
| 375 | |
| 376 | if (id[0] != '[') { |
| 377 | return -parsed; |
| 378 | } |
| 379 | |
| 380 | ++parsed; |
| 381 | ++id; |
| 382 | |
| 383 | while (isspace(id[0])) { |
| 384 | ++parsed; |
| 385 | ++id; |
| 386 | } |
| 387 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 388 | if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len, NULL, 0)) < 1) { |
Radek Krejci | 29e8528 | 2019-06-10 09:11:10 +0200 | [diff] [blame] | 389 | return -parsed + ret; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | parsed += ret; |
| 393 | id += ret; |
| 394 | |
| 395 | while (isspace(id[0])) { |
| 396 | ++parsed; |
| 397 | ++id; |
| 398 | } |
| 399 | |
| 400 | if (id[0] != '=') { |
| 401 | return -parsed; |
| 402 | } |
| 403 | |
| 404 | ++parsed; |
| 405 | ++id; |
| 406 | |
| 407 | while (isspace(id[0])) { |
| 408 | ++parsed; |
| 409 | ++id; |
| 410 | } |
| 411 | |
| 412 | if ((ptr = strchr(id, ']')) == NULL) { |
| 413 | return -parsed; |
| 414 | } |
| 415 | |
| 416 | --ptr; |
| 417 | while (isspace(ptr[0])) { |
| 418 | --ptr; |
| 419 | } |
| 420 | ++ptr; |
| 421 | |
| 422 | ret = ptr-id; |
| 423 | if (path_key_expr) { |
| 424 | *path_key_expr = id; |
| 425 | } |
| 426 | if (pke_len) { |
| 427 | *pke_len = ret; |
| 428 | } |
| 429 | |
| 430 | parsed += ret; |
| 431 | id += ret; |
| 432 | |
| 433 | while (isspace(id[0])) { |
| 434 | ++parsed; |
| 435 | ++id; |
| 436 | } |
| 437 | |
| 438 | assert(id[0] == ']'); |
| 439 | |
| 440 | if (id[1] == '[') { |
| 441 | *has_predicate = 1; |
| 442 | } |
| 443 | |
| 444 | return parsed+1; |
| 445 | } |
| 446 | |
| 447 | /** |
| 448 | * @brief Parse a path-key-expr (leafref). First call parses "current()", all |
| 449 | * the ".." and the first node-identifier, other calls parse a single |
| 450 | * node-identifier each. |
| 451 | * |
| 452 | * path-key-expr = current-function-invocation *WSP "/" *WSP |
| 453 | * rel-path-keyexpr |
| 454 | * rel-path-keyexpr = 1*(".." *WSP "/" *WSP) |
| 455 | * *(node-identifier *WSP "/" *WSP) |
| 456 | * node-identifier |
| 457 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 458 | * @param[in] id Identifier to use. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 459 | * @param[out] prefix Points to the prefix, NULL if there is not any. |
| 460 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 461 | * @param[out] name Points to the node name. |
| 462 | * @param[out] nam_len Length of the node name. |
| 463 | * @param[out] parent_times Number of ".." in the path. Must be 0 on the first call, |
| 464 | * must not be changed between consecutive calls. |
| 465 | * @return Number of characters successfully parsed, |
| 466 | * positive on success, negative on failure. |
| 467 | */ |
| 468 | static int |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 469 | parse_path_key_expr(const char *id, const char **prefix, int *pref_len, const char **name, int *nam_len, |
| 470 | int *parent_times) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 471 | { |
| 472 | int parsed = 0, ret, par_times = 0; |
| 473 | |
| 474 | assert(id); |
| 475 | assert(parent_times); |
| 476 | if (prefix) { |
| 477 | *prefix = NULL; |
| 478 | } |
| 479 | if (pref_len) { |
| 480 | *pref_len = 0; |
| 481 | } |
| 482 | if (name) { |
| 483 | *name = NULL; |
| 484 | } |
| 485 | if (nam_len) { |
| 486 | *nam_len = 0; |
| 487 | } |
| 488 | |
| 489 | if (!*parent_times) { |
| 490 | /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */ |
| 491 | if (strncmp(id, "current()", 9)) { |
| 492 | return -parsed; |
| 493 | } |
| 494 | |
| 495 | parsed += 9; |
| 496 | id += 9; |
| 497 | |
| 498 | while (isspace(id[0])) { |
| 499 | ++parsed; |
| 500 | ++id; |
| 501 | } |
| 502 | |
| 503 | if (id[0] != '/') { |
| 504 | return -parsed; |
| 505 | } |
| 506 | |
| 507 | ++parsed; |
| 508 | ++id; |
| 509 | |
| 510 | while (isspace(id[0])) { |
| 511 | ++parsed; |
| 512 | ++id; |
| 513 | } |
| 514 | |
| 515 | /* rel-path-keyexpr */ |
| 516 | if (strncmp(id, "..", 2)) { |
| 517 | return -parsed; |
| 518 | } |
| 519 | ++par_times; |
| 520 | |
| 521 | parsed += 2; |
| 522 | id += 2; |
| 523 | |
| 524 | while (isspace(id[0])) { |
| 525 | ++parsed; |
| 526 | ++id; |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | /* 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier |
| 531 | * |
| 532 | * first parent reference with whitespaces already parsed |
| 533 | */ |
| 534 | if (id[0] != '/') { |
| 535 | return -parsed; |
| 536 | } |
| 537 | |
| 538 | ++parsed; |
| 539 | ++id; |
| 540 | |
| 541 | while (isspace(id[0])) { |
| 542 | ++parsed; |
| 543 | ++id; |
| 544 | } |
| 545 | |
| 546 | while (!strncmp(id, "..", 2) && !*parent_times) { |
| 547 | ++par_times; |
| 548 | |
| 549 | parsed += 2; |
| 550 | id += 2; |
| 551 | |
| 552 | while (isspace(id[0])) { |
| 553 | ++parsed; |
| 554 | ++id; |
| 555 | } |
| 556 | |
| 557 | if (id[0] != '/') { |
| 558 | return -parsed; |
| 559 | } |
| 560 | |
| 561 | ++parsed; |
| 562 | ++id; |
| 563 | |
| 564 | while (isspace(id[0])) { |
| 565 | ++parsed; |
| 566 | ++id; |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | if (!*parent_times) { |
| 571 | *parent_times = par_times; |
| 572 | } |
| 573 | |
| 574 | /* all parent references must be parsed at this point */ |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 575 | if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len, NULL, 0)) < 1) { |
| 576 | return -parsed + ret; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | parsed += ret; |
| 580 | id += ret; |
| 581 | |
| 582 | return parsed; |
| 583 | } |
| 584 | |
| 585 | /** |
| 586 | * @brief Parse path-arg (leafref). |
| 587 | * |
| 588 | * path-arg = absolute-path / relative-path |
| 589 | * absolute-path = 1*("/" (node-identifier *path-predicate)) |
| 590 | * relative-path = 1*(".." "/") descendant-path |
| 591 | * |
Radek Krejci | f7ed4c3 | 2016-10-27 16:20:03 +0200 | [diff] [blame] | 592 | * @param[in] mod Module of the context node to get correct prefix in case it is not explicitly specified |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 593 | * @param[in] id Identifier to use. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 594 | * @param[out] prefix Points to the prefix, NULL if there is not any. |
| 595 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 596 | * @param[out] name Points to the node name. |
| 597 | * @param[out] nam_len Length of the node name. |
| 598 | * @param[out] parent_times Number of ".." in the path. Must be 0 on the first call, |
| 599 | * must not be changed between consecutive calls. -1 if the |
| 600 | * path is relative. |
| 601 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 602 | * |
| 603 | * @return Number of characters successfully parsed, |
| 604 | * positive on success, negative on failure. |
| 605 | */ |
| 606 | static int |
Michal Vasko | 3c60cbb | 2017-07-10 11:50:03 +0200 | [diff] [blame] | 607 | parse_path_arg(const struct lys_module *mod, const char *id, const char **prefix, int *pref_len, |
Radek Krejci | f7ed4c3 | 2016-10-27 16:20:03 +0200 | [diff] [blame] | 608 | const char **name, int *nam_len, int *parent_times, int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 609 | { |
| 610 | int parsed = 0, ret, par_times = 0; |
| 611 | |
| 612 | assert(id); |
| 613 | assert(parent_times); |
| 614 | if (prefix) { |
| 615 | *prefix = NULL; |
| 616 | } |
| 617 | if (pref_len) { |
| 618 | *pref_len = 0; |
| 619 | } |
| 620 | if (name) { |
| 621 | *name = NULL; |
| 622 | } |
| 623 | if (nam_len) { |
| 624 | *nam_len = 0; |
| 625 | } |
| 626 | if (has_predicate) { |
| 627 | *has_predicate = 0; |
| 628 | } |
| 629 | |
| 630 | if (!*parent_times && !strncmp(id, "..", 2)) { |
| 631 | ++par_times; |
| 632 | |
| 633 | parsed += 2; |
| 634 | id += 2; |
| 635 | |
| 636 | while (!strncmp(id, "/..", 3)) { |
| 637 | ++par_times; |
| 638 | |
| 639 | parsed += 3; |
| 640 | id += 3; |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | if (!*parent_times) { |
| 645 | if (par_times) { |
| 646 | *parent_times = par_times; |
| 647 | } else { |
| 648 | *parent_times = -1; |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | if (id[0] != '/') { |
| 653 | return -parsed; |
| 654 | } |
| 655 | |
| 656 | /* skip '/' */ |
| 657 | ++parsed; |
| 658 | ++id; |
| 659 | |
| 660 | /* node-identifier ([prefix:]identifier) */ |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 661 | if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len, NULL, 0)) < 1) { |
Radek Krejci | 29e8528 | 2019-06-10 09:11:10 +0200 | [diff] [blame] | 662 | return -parsed + ret; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 663 | } |
Michal Vasko | 3c60cbb | 2017-07-10 11:50:03 +0200 | [diff] [blame] | 664 | if (prefix && !(*prefix)) { |
Radek Krejci | f7ed4c3 | 2016-10-27 16:20:03 +0200 | [diff] [blame] | 665 | /* actually we always need prefix even it is not specified */ |
| 666 | *prefix = lys_main_module(mod)->name; |
| 667 | *pref_len = strlen(*prefix); |
| 668 | } |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 669 | |
| 670 | parsed += ret; |
| 671 | id += ret; |
| 672 | |
| 673 | /* there is no predicate */ |
| 674 | if ((id[0] == '/') || !id[0]) { |
| 675 | return parsed; |
| 676 | } else if (id[0] != '[') { |
| 677 | return -parsed; |
| 678 | } |
| 679 | |
| 680 | if (has_predicate) { |
| 681 | *has_predicate = 1; |
| 682 | } |
| 683 | |
| 684 | return parsed; |
| 685 | } |
| 686 | |
| 687 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 688 | * @brief Parse instance-identifier in JSON data format. That means that prefixes |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 689 | * are actually model names. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 690 | * |
| 691 | * instance-identifier = 1*("/" (node-identifier *predicate)) |
| 692 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 693 | * @param[in] id Identifier to use. |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 694 | * @param[out] model Points to the model name. |
| 695 | * @param[out] mod_len Length of the model name. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 696 | * @param[out] name Points to the node name. |
| 697 | * @param[out] nam_len Length of the node name. |
| 698 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 699 | * |
| 700 | * @return Number of characters successfully parsed, |
| 701 | * positive on success, negative on failure. |
| 702 | */ |
| 703 | static int |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 704 | parse_instance_identifier(const char *id, const char **model, int *mod_len, const char **name, int *nam_len, |
| 705 | int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 706 | { |
| 707 | int parsed = 0, ret; |
| 708 | |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 709 | assert(id && model && mod_len && name && nam_len); |
| 710 | |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 711 | if (has_predicate) { |
| 712 | *has_predicate = 0; |
| 713 | } |
| 714 | |
| 715 | if (id[0] != '/') { |
| 716 | return -parsed; |
| 717 | } |
| 718 | |
| 719 | ++parsed; |
| 720 | ++id; |
| 721 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 722 | if ((ret = parse_identifier(id)) < 1) { |
| 723 | return ret; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 724 | } |
| 725 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 726 | *name = id; |
| 727 | *nam_len = ret; |
| 728 | |
Michal Vasko | 8db018b | 2020-01-09 11:01:02 +0100 | [diff] [blame] | 729 | *model = NULL; |
| 730 | *mod_len = 0; |
| 731 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 732 | parsed += ret; |
| 733 | id += ret; |
| 734 | |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 735 | if (id[0] == ':') { |
| 736 | /* we have prefix */ |
| 737 | *model = *name; |
| 738 | *mod_len = *nam_len; |
| 739 | |
| 740 | ++parsed; |
| 741 | ++id; |
| 742 | |
| 743 | if ((ret = parse_identifier(id)) < 1) { |
| 744 | return ret; |
| 745 | } |
| 746 | |
| 747 | *name = id; |
| 748 | *nam_len = ret; |
| 749 | |
| 750 | parsed += ret; |
| 751 | id += ret; |
| 752 | } |
| 753 | |
Radek Krejci | 4967cb6 | 2016-09-14 16:40:28 +0200 | [diff] [blame] | 754 | if (id[0] == '[' && has_predicate) { |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 755 | *has_predicate = 1; |
| 756 | } |
| 757 | |
| 758 | return parsed; |
| 759 | } |
| 760 | |
| 761 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 762 | * @brief Parse predicate (instance-identifier) in JSON data format. That means that prefixes |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 763 | * (which are mandatory) are actually model names. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 764 | * |
| 765 | * predicate = "[" *WSP (predicate-expr / pos) *WSP "]" |
| 766 | * predicate-expr = (node-identifier / ".") *WSP "=" *WSP |
| 767 | * ((DQUOTE string DQUOTE) / |
| 768 | * (SQUOTE string SQUOTE)) |
| 769 | * pos = non-negative-integer-value |
| 770 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 771 | * @param[in] id Identifier to use. |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 772 | * @param[out] model Points to the model name. |
| 773 | * @param[out] mod_len Length of the model name. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 774 | * @param[out] name Points to the node name. Can be identifier (from node-identifier), "." or pos. |
| 775 | * @param[out] nam_len Length of the node name. |
| 776 | * @param[out] value Value the node-identifier must have (string from the grammar), |
| 777 | * NULL if there is not any. |
| 778 | * @param[out] val_len Length of the value, 0 if there is not any. |
| 779 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 780 | * |
| 781 | * @return Number of characters successfully parsed, |
| 782 | * positive on success, negative on failure. |
| 783 | */ |
| 784 | static int |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 785 | parse_predicate(const char *id, const char **model, int *mod_len, const char **name, int *nam_len, |
| 786 | const char **value, int *val_len, int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 787 | { |
| 788 | const char *ptr; |
| 789 | int parsed = 0, ret; |
| 790 | char quote; |
| 791 | |
| 792 | assert(id); |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 793 | if (model) { |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 794 | assert(mod_len); |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 795 | *model = NULL; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 796 | *mod_len = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 797 | } |
| 798 | if (name) { |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 799 | assert(nam_len); |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 800 | *name = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 801 | *nam_len = 0; |
| 802 | } |
| 803 | if (value) { |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 804 | assert(val_len); |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 805 | *value = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 806 | *val_len = 0; |
| 807 | } |
| 808 | if (has_predicate) { |
| 809 | *has_predicate = 0; |
| 810 | } |
| 811 | |
| 812 | if (id[0] != '[') { |
| 813 | return -parsed; |
| 814 | } |
| 815 | |
| 816 | ++parsed; |
| 817 | ++id; |
| 818 | |
| 819 | while (isspace(id[0])) { |
| 820 | ++parsed; |
| 821 | ++id; |
| 822 | } |
| 823 | |
| 824 | /* pos */ |
| 825 | if (isdigit(id[0])) { |
| 826 | if (name) { |
| 827 | *name = id; |
| 828 | } |
| 829 | |
| 830 | if (id[0] == '0') { |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 831 | return -parsed; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | while (isdigit(id[0])) { |
| 835 | ++parsed; |
| 836 | ++id; |
| 837 | } |
| 838 | |
| 839 | if (nam_len) { |
| 840 | *nam_len = id-(*name); |
| 841 | } |
| 842 | |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 843 | /* "." or node-identifier */ |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 844 | } else { |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 845 | if (id[0] == '.') { |
| 846 | if (name) { |
| 847 | *name = id; |
| 848 | } |
| 849 | if (nam_len) { |
| 850 | *nam_len = 1; |
| 851 | } |
| 852 | |
| 853 | ++parsed; |
| 854 | ++id; |
| 855 | |
| 856 | } else { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 857 | if ((ret = parse_node_identifier(id, model, mod_len, name, nam_len, NULL, 0)) < 1) { |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 858 | return -parsed + ret; |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | parsed += ret; |
| 862 | id += ret; |
| 863 | } |
| 864 | |
| 865 | while (isspace(id[0])) { |
| 866 | ++parsed; |
| 867 | ++id; |
| 868 | } |
| 869 | |
| 870 | if (id[0] != '=') { |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 871 | return -parsed; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 872 | } |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 873 | |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 874 | ++parsed; |
| 875 | ++id; |
| 876 | |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 877 | while (isspace(id[0])) { |
| 878 | ++parsed; |
| 879 | ++id; |
| 880 | } |
| 881 | |
| 882 | /* ((DQUOTE string DQUOTE) / (SQUOTE string SQUOTE)) */ |
| 883 | if ((id[0] == '\"') || (id[0] == '\'')) { |
| 884 | quote = id[0]; |
| 885 | |
| 886 | ++parsed; |
| 887 | ++id; |
| 888 | |
| 889 | if ((ptr = strchr(id, quote)) == NULL) { |
| 890 | return -parsed; |
| 891 | } |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 892 | ret = ptr - id; |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 893 | |
| 894 | if (value) { |
| 895 | *value = id; |
| 896 | } |
| 897 | if (val_len) { |
| 898 | *val_len = ret; |
| 899 | } |
| 900 | |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 901 | parsed += ret + 1; |
| 902 | id += ret + 1; |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 903 | } else { |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 904 | return -parsed; |
| 905 | } |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | while (isspace(id[0])) { |
| 909 | ++parsed; |
| 910 | ++id; |
| 911 | } |
| 912 | |
| 913 | if (id[0] != ']') { |
| 914 | return -parsed; |
| 915 | } |
| 916 | |
| 917 | ++parsed; |
| 918 | ++id; |
| 919 | |
| 920 | if ((id[0] == '[') && has_predicate) { |
| 921 | *has_predicate = 1; |
| 922 | } |
| 923 | |
| 924 | return parsed; |
| 925 | } |
| 926 | |
| 927 | /** |
| 928 | * @brief Parse schema-nodeid. |
| 929 | * |
| 930 | * schema-nodeid = absolute-schema-nodeid / |
| 931 | * descendant-schema-nodeid |
| 932 | * absolute-schema-nodeid = 1*("/" node-identifier) |
Michal Vasko | 4893535 | 2016-03-29 11:52:36 +0200 | [diff] [blame] | 933 | * descendant-schema-nodeid = ["." "/"] |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 934 | * node-identifier |
| 935 | * absolute-schema-nodeid |
| 936 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 937 | * @param[in] id Identifier to use. |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 938 | * @param[out] mod_name Points to the module name, NULL if there is not any. |
| 939 | * @param[out] mod_name_len Length of the module name, 0 if there is not any. |
Michal Vasko | 4893535 | 2016-03-29 11:52:36 +0200 | [diff] [blame] | 940 | * @param[out] name Points to the node name. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 941 | * @param[out] nam_len Length of the node name. |
| 942 | * @param[out] is_relative Flag to mark whether the nodeid is absolute or descendant. Must be -1 |
| 943 | * on the first call, must not be changed between consecutive calls. |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 944 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. It cannot be |
| 945 | * based on the grammar, in those cases use NULL. |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 946 | * @param[in] extended Whether to accept an extended path (support for /[prefix:]*, //[prefix:]*, //[prefix:].). |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 947 | * |
| 948 | * @return Number of characters successfully parsed, |
| 949 | * positive on success, negative on failure. |
| 950 | */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 951 | int |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 952 | parse_schema_nodeid(const char *id, const char **mod_name, int *mod_name_len, const char **name, int *nam_len, |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 953 | int *is_relative, int *has_predicate, int *all_desc, int extended) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 954 | { |
| 955 | int parsed = 0, ret; |
| 956 | |
| 957 | assert(id); |
| 958 | assert(is_relative); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 959 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 960 | if (has_predicate) { |
| 961 | *has_predicate = 0; |
| 962 | } |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 963 | |
| 964 | if (id[0] != '/') { |
| 965 | if (*is_relative != -1) { |
| 966 | return -parsed; |
| 967 | } else { |
| 968 | *is_relative = 1; |
| 969 | } |
Michal Vasko | 4893535 | 2016-03-29 11:52:36 +0200 | [diff] [blame] | 970 | if (!strncmp(id, "./", 2)) { |
| 971 | parsed += 2; |
| 972 | id += 2; |
| 973 | } |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 974 | } else { |
| 975 | if (*is_relative == -1) { |
| 976 | *is_relative = 0; |
| 977 | } |
| 978 | ++parsed; |
| 979 | ++id; |
| 980 | } |
| 981 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 982 | if ((ret = parse_node_identifier(id, mod_name, mod_name_len, name, nam_len, all_desc, extended)) < 1) { |
| 983 | return -parsed + ret; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 984 | } |
| 985 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 986 | parsed += ret; |
| 987 | id += ret; |
| 988 | |
| 989 | if ((id[0] == '[') && has_predicate) { |
| 990 | *has_predicate = 1; |
| 991 | } |
| 992 | |
| 993 | return parsed; |
| 994 | } |
| 995 | |
| 996 | /** |
| 997 | * @brief Parse schema predicate (special format internally used). |
| 998 | * |
| 999 | * predicate = "[" *WSP predicate-expr *WSP "]" |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 1000 | * predicate-expr = "." / [prefix:]identifier / positive-integer / key-with-value |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1001 | * key-with-value = identifier *WSP "=" *WSP |
| 1002 | * ((DQUOTE string DQUOTE) / |
| 1003 | * (SQUOTE string SQUOTE)) |
| 1004 | * |
| 1005 | * @param[in] id Identifier to use. |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 1006 | * @param[out] mod_name Points to the list key module name. |
| 1007 | * @param[out] mod_name_len Length of \p mod_name. |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1008 | * @param[out] name Points to the list key name. |
| 1009 | * @param[out] nam_len Length of \p name. |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1010 | * @param[out] value Points to the key value. If specified, key-with-value is expected. |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1011 | * @param[out] val_len Length of \p value. |
| 1012 | * @param[out] has_predicate Flag to mark whether there is another predicate specified. |
| 1013 | */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1014 | int |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 1015 | parse_schema_json_predicate(const char *id, const char **mod_name, int *mod_name_len, const char **name, int *nam_len, |
| 1016 | const char **value, int *val_len, int *has_predicate) |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1017 | { |
| 1018 | const char *ptr; |
| 1019 | int parsed = 0, ret; |
| 1020 | char quote; |
| 1021 | |
| 1022 | assert(id); |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 1023 | if (mod_name) { |
| 1024 | *mod_name = NULL; |
| 1025 | } |
| 1026 | if (mod_name_len) { |
| 1027 | *mod_name_len = 0; |
| 1028 | } |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1029 | if (name) { |
| 1030 | *name = NULL; |
| 1031 | } |
| 1032 | if (nam_len) { |
| 1033 | *nam_len = 0; |
| 1034 | } |
| 1035 | if (value) { |
| 1036 | *value = NULL; |
| 1037 | } |
| 1038 | if (val_len) { |
| 1039 | *val_len = 0; |
| 1040 | } |
| 1041 | if (has_predicate) { |
| 1042 | *has_predicate = 0; |
| 1043 | } |
| 1044 | |
| 1045 | if (id[0] != '[') { |
| 1046 | return -parsed; |
| 1047 | } |
| 1048 | |
| 1049 | ++parsed; |
| 1050 | ++id; |
| 1051 | |
| 1052 | while (isspace(id[0])) { |
| 1053 | ++parsed; |
| 1054 | ++id; |
| 1055 | } |
| 1056 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1057 | /* identifier */ |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 1058 | if (id[0] == '.') { |
| 1059 | ret = 1; |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 1060 | |
| 1061 | if (name) { |
| 1062 | *name = id; |
| 1063 | } |
| 1064 | if (nam_len) { |
| 1065 | *nam_len = ret; |
| 1066 | } |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 1067 | } else if (isdigit(id[0])) { |
| 1068 | if (id[0] == '0') { |
| 1069 | return -parsed; |
| 1070 | } |
| 1071 | ret = 1; |
| 1072 | while (isdigit(id[ret])) { |
| 1073 | ++ret; |
| 1074 | } |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 1075 | |
| 1076 | if (name) { |
| 1077 | *name = id; |
| 1078 | } |
| 1079 | if (nam_len) { |
| 1080 | *nam_len = ret; |
| 1081 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1082 | } else if ((ret = parse_node_identifier(id, mod_name, mod_name_len, name, nam_len, NULL, 0)) < 1) { |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1083 | return -parsed + ret; |
| 1084 | } |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1085 | |
| 1086 | parsed += ret; |
| 1087 | id += ret; |
| 1088 | |
| 1089 | while (isspace(id[0])) { |
| 1090 | ++parsed; |
| 1091 | ++id; |
| 1092 | } |
| 1093 | |
| 1094 | /* there is value as well */ |
| 1095 | if (id[0] == '=') { |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 1096 | if (name && isdigit(**name)) { |
| 1097 | return -parsed; |
| 1098 | } |
| 1099 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1100 | ++parsed; |
| 1101 | ++id; |
| 1102 | |
| 1103 | while (isspace(id[0])) { |
| 1104 | ++parsed; |
| 1105 | ++id; |
| 1106 | } |
| 1107 | |
| 1108 | /* ((DQUOTE string DQUOTE) / (SQUOTE string SQUOTE)) */ |
| 1109 | if ((id[0] == '\"') || (id[0] == '\'')) { |
| 1110 | quote = id[0]; |
| 1111 | |
| 1112 | ++parsed; |
| 1113 | ++id; |
| 1114 | |
| 1115 | if ((ptr = strchr(id, quote)) == NULL) { |
| 1116 | return -parsed; |
| 1117 | } |
| 1118 | ret = ptr - id; |
| 1119 | |
| 1120 | if (value) { |
| 1121 | *value = id; |
| 1122 | } |
| 1123 | if (val_len) { |
| 1124 | *val_len = ret; |
| 1125 | } |
| 1126 | |
| 1127 | parsed += ret + 1; |
| 1128 | id += ret + 1; |
| 1129 | } else { |
| 1130 | return -parsed; |
| 1131 | } |
| 1132 | |
| 1133 | while (isspace(id[0])) { |
| 1134 | ++parsed; |
| 1135 | ++id; |
| 1136 | } |
| 1137 | } |
| 1138 | |
| 1139 | if (id[0] != ']') { |
| 1140 | return -parsed; |
| 1141 | } |
| 1142 | |
| 1143 | ++parsed; |
| 1144 | ++id; |
| 1145 | |
| 1146 | if ((id[0] == '[') && has_predicate) { |
| 1147 | *has_predicate = 1; |
| 1148 | } |
| 1149 | |
| 1150 | return parsed; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 1151 | } |
| 1152 | |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 1153 | static struct lyd_node * |
Michal Vasko | c340b3d | 2020-06-29 10:47:11 +0200 | [diff] [blame] | 1154 | resolve_json_data_node_hash(struct lyd_node *siblings, struct parsed_pred pp) |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 1155 | { |
Michal Vasko | c340b3d | 2020-06-29 10:47:11 +0200 | [diff] [blame] | 1156 | struct lyd_node *ret = NULL; |
| 1157 | char *key_or_value = NULL; |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 1158 | |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 1159 | if (pp.schema->nodetype == LYS_LEAFLIST) { |
| 1160 | assert((pp.len == 1) && (pp.pred[0].name[0] == '.') && (pp.pred[0].nam_len == 1)); |
Michal Vasko | c340b3d | 2020-06-29 10:47:11 +0200 | [diff] [blame] | 1161 | |
| 1162 | key_or_value = strndup(pp.pred[0].value, pp.pred[0].val_len); |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 1163 | } else if (pp.schema->nodetype == LYS_LIST) { |
Michal Vasko | c340b3d | 2020-06-29 10:47:11 +0200 | [diff] [blame] | 1164 | key_or_value = strndup(pp.pred_str, pp.pred_str_len); |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 1165 | } |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 1166 | |
| 1167 | /* try to find the node */ |
Michal Vasko | c340b3d | 2020-06-29 10:47:11 +0200 | [diff] [blame] | 1168 | lyd_find_sibling_val(siblings, pp.schema, key_or_value, &ret); |
| 1169 | free(key_or_value); |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 1170 | |
Michal Vasko | c340b3d | 2020-06-29 10:47:11 +0200 | [diff] [blame] | 1171 | return ret; |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 1172 | } |
| 1173 | |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 1174 | /** |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1175 | * @brief Resolve (find) a feature definition. Logs directly. |
| 1176 | * |
| 1177 | * @param[in] feat_name Feature name to resolve. |
| 1178 | * @param[in] len Length of \p feat_name. |
| 1179 | * @param[in] node Node with the if-feature expression. |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1180 | * @param[out] feature Pointer to be set to point to the feature definition, if feature not found |
| 1181 | * (return code 1), the pointer is untouched. |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1182 | * |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1183 | * @return 0 on success, 1 on forward reference, -1 on error. |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1184 | */ |
| 1185 | static int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1186 | resolve_feature(const char *feat_name, uint16_t len, const struct lys_node *node, struct lys_feature **feature) |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1187 | { |
| 1188 | char *str; |
| 1189 | const char *mod_name, *name; |
| 1190 | int mod_name_len, nam_len, i, j; |
| 1191 | const struct lys_module *module; |
| 1192 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1193 | assert(feature); |
| 1194 | |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1195 | /* check prefix */ |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1196 | if ((i = parse_node_identifier(feat_name, &mod_name, &mod_name_len, &name, &nam_len, NULL, 0)) < 1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1197 | LOGVAL(node->module->ctx, LYE_INCHAR, LY_VLOG_NONE, NULL, feat_name[-i], &feat_name[-i]); |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1198 | return -1; |
| 1199 | } |
| 1200 | |
Michal Vasko | 921eb6b | 2017-10-13 10:01:39 +0200 | [diff] [blame] | 1201 | module = lyp_get_module(lys_node_module(node), NULL, 0, mod_name, mod_name_len, 0); |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1202 | if (!module) { |
| 1203 | /* identity refers unknown data model */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1204 | LOGVAL(node->module->ctx, LYE_INMOD_LEN, LY_VLOG_NONE, NULL, mod_name_len, mod_name); |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1205 | return -1; |
| 1206 | } |
| 1207 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1208 | if (module != node->module && module == lys_node_module(node)) { |
| 1209 | /* first, try to search directly in submodule where the feature was mentioned */ |
| 1210 | for (j = 0; j < node->module->features_size; j++) { |
| 1211 | if (!strncmp(name, node->module->features[j].name, nam_len) && !node->module->features[j].name[nam_len]) { |
| 1212 | /* check status */ |
| 1213 | if (lyp_check_status(node->flags, lys_node_module(node), node->name, node->module->features[j].flags, |
Pavol Vican | fdab9f9 | 2016-09-07 15:23:27 +0200 | [diff] [blame] | 1214 | node->module->features[j].module, node->module->features[j].name, NULL)) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1215 | return -1; |
| 1216 | } |
| 1217 | *feature = &node->module->features[j]; |
| 1218 | return 0; |
| 1219 | } |
| 1220 | } |
| 1221 | } |
| 1222 | |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1223 | /* search in the identified module ... */ |
| 1224 | for (j = 0; j < module->features_size; j++) { |
Michal Vasko | 3def867 | 2016-07-01 11:43:09 +0200 | [diff] [blame] | 1225 | if (!strncmp(name, module->features[j].name, nam_len) && !module->features[j].name[nam_len]) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1226 | /* check status */ |
| 1227 | if (lyp_check_status(node->flags, lys_node_module(node), node->name, module->features[j].flags, |
Pavol Vican | fdab9f9 | 2016-09-07 15:23:27 +0200 | [diff] [blame] | 1228 | module->features[j].module, module->features[j].name, NULL)) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1229 | return -1; |
| 1230 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1231 | *feature = &module->features[j]; |
| 1232 | return 0; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1233 | } |
| 1234 | } |
| 1235 | /* ... and all its submodules */ |
Radek Krejci | d4c1d0f | 2017-01-19 16:11:38 +0100 | [diff] [blame] | 1236 | for (i = 0; i < module->inc_size && module->inc[i].submodule; i++) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1237 | for (j = 0; j < module->inc[i].submodule->features_size; j++) { |
Michal Vasko | 3def867 | 2016-07-01 11:43:09 +0200 | [diff] [blame] | 1238 | if (!strncmp(name, module->inc[i].submodule->features[j].name, nam_len) |
| 1239 | && !module->inc[i].submodule->features[j].name[nam_len]) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1240 | /* check status */ |
| 1241 | if (lyp_check_status(node->flags, lys_node_module(node), node->name, |
| 1242 | module->inc[i].submodule->features[j].flags, |
| 1243 | module->inc[i].submodule->features[j].module, |
Pavol Vican | fdab9f9 | 2016-09-07 15:23:27 +0200 | [diff] [blame] | 1244 | module->inc[i].submodule->features[j].name, NULL)) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1245 | return -1; |
| 1246 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1247 | *feature = &module->inc[i].submodule->features[j]; |
| 1248 | return 0; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1249 | } |
| 1250 | } |
| 1251 | } |
| 1252 | |
| 1253 | /* not found */ |
| 1254 | str = strndup(feat_name, len); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1255 | LOGVAL(node->module->ctx, LYE_INRESOLV, LY_VLOG_NONE, NULL, "feature", str); |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1256 | free(str); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1257 | return 1; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1258 | } |
| 1259 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1260 | /* |
| 1261 | * @return |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1262 | * - 1 if enabled |
| 1263 | * - 0 if disabled |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1264 | */ |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1265 | static int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1266 | resolve_feature_value(const struct lys_feature *feat) |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1267 | { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1268 | int i; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1269 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1270 | for (i = 0; i < feat->iffeature_size; i++) { |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1271 | if (!resolve_iffeature(&feat->iffeature[i])) { |
Radek Krejci | af56633 | 2017-02-07 15:56:59 +0100 | [diff] [blame] | 1272 | return 0; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1273 | } |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1274 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1275 | |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1276 | return feat->flags & LYS_FENABLED ? 1 : 0; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1277 | } |
| 1278 | |
| 1279 | static int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1280 | resolve_iffeature_recursive(struct lys_iffeature *expr, int *index_e, int *index_f) |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1281 | { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1282 | uint8_t op; |
Radek Krejci | af56633 | 2017-02-07 15:56:59 +0100 | [diff] [blame] | 1283 | int a, b; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1284 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1285 | op = iff_getop(expr->expr, *index_e); |
| 1286 | (*index_e)++; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1287 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1288 | switch (op) { |
| 1289 | case LYS_IFF_F: |
| 1290 | /* resolve feature */ |
| 1291 | return resolve_feature_value(expr->features[(*index_f)++]); |
| 1292 | case LYS_IFF_NOT: |
Radek Krejci | af56633 | 2017-02-07 15:56:59 +0100 | [diff] [blame] | 1293 | /* invert result */ |
| 1294 | return resolve_iffeature_recursive(expr, index_e, index_f) ? 0 : 1; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1295 | case LYS_IFF_AND: |
| 1296 | case LYS_IFF_OR: |
| 1297 | a = resolve_iffeature_recursive(expr, index_e, index_f); |
| 1298 | b = resolve_iffeature_recursive(expr, index_e, index_f); |
Radek Krejci | af56633 | 2017-02-07 15:56:59 +0100 | [diff] [blame] | 1299 | if (op == LYS_IFF_AND) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1300 | return a && b; |
| 1301 | } else { /* LYS_IFF_OR */ |
| 1302 | return a || b; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1303 | } |
| 1304 | } |
| 1305 | |
Radek Krejci | af56633 | 2017-02-07 15:56:59 +0100 | [diff] [blame] | 1306 | return 0; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1307 | } |
| 1308 | |
| 1309 | int |
| 1310 | resolve_iffeature(struct lys_iffeature *expr) |
| 1311 | { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1312 | int index_e = 0, index_f = 0; |
| 1313 | |
Michal Vasko | bdb596d | 2019-04-29 08:59:30 +0200 | [diff] [blame] | 1314 | if (expr->expr && expr->features[0]) { |
Radek Krejci | af56633 | 2017-02-07 15:56:59 +0100 | [diff] [blame] | 1315 | return resolve_iffeature_recursive(expr, &index_e, &index_f); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1316 | } |
Radek Krejci | af56633 | 2017-02-07 15:56:59 +0100 | [diff] [blame] | 1317 | return 0; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1318 | } |
| 1319 | |
| 1320 | struct iff_stack { |
| 1321 | int size; |
| 1322 | int index; /* first empty item */ |
| 1323 | uint8_t *stack; |
| 1324 | }; |
| 1325 | |
| 1326 | static int |
| 1327 | iff_stack_push(struct iff_stack *stack, uint8_t value) |
| 1328 | { |
| 1329 | if (stack->index == stack->size) { |
| 1330 | stack->size += 4; |
| 1331 | stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1332 | LY_CHECK_ERR_RETURN(!stack->stack, LOGMEM(NULL); stack->size = 0, EXIT_FAILURE); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1333 | } |
| 1334 | |
| 1335 | stack->stack[stack->index++] = value; |
| 1336 | return EXIT_SUCCESS; |
| 1337 | } |
| 1338 | |
| 1339 | static uint8_t |
| 1340 | iff_stack_pop(struct iff_stack *stack) |
| 1341 | { |
| 1342 | stack->index--; |
| 1343 | return stack->stack[stack->index]; |
| 1344 | } |
| 1345 | |
| 1346 | static void |
| 1347 | iff_stack_clean(struct iff_stack *stack) |
| 1348 | { |
| 1349 | stack->size = 0; |
| 1350 | free(stack->stack); |
| 1351 | } |
| 1352 | |
| 1353 | static void |
| 1354 | iff_setop(uint8_t *list, uint8_t op, int pos) |
| 1355 | { |
| 1356 | uint8_t *item; |
| 1357 | uint8_t mask = 3; |
| 1358 | |
| 1359 | assert(pos >= 0); |
| 1360 | assert(op <= 3); /* max 2 bits */ |
| 1361 | |
| 1362 | item = &list[pos / 4]; |
| 1363 | mask = mask << 2 * (pos % 4); |
| 1364 | *item = (*item) & ~mask; |
| 1365 | *item = (*item) | (op << 2 * (pos % 4)); |
| 1366 | } |
| 1367 | |
| 1368 | uint8_t |
| 1369 | iff_getop(uint8_t *list, int pos) |
| 1370 | { |
| 1371 | uint8_t *item; |
| 1372 | uint8_t mask = 3, result; |
| 1373 | |
| 1374 | assert(pos >= 0); |
| 1375 | |
| 1376 | item = &list[pos / 4]; |
| 1377 | result = (*item) & (mask << 2 * (pos % 4)); |
| 1378 | return result >> 2 * (pos % 4); |
| 1379 | } |
| 1380 | |
| 1381 | #define LYS_IFF_LP 0x04 /* ( */ |
| 1382 | #define LYS_IFF_RP 0x08 /* ) */ |
| 1383 | |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1384 | /* internal structure for passing data for UNRES_IFFEAT */ |
| 1385 | struct unres_iffeat_data { |
| 1386 | struct lys_node *node; |
| 1387 | const char *fname; |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 1388 | int infeature; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1389 | }; |
| 1390 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1391 | void |
| 1392 | resolve_iffeature_getsizes(struct lys_iffeature *iffeat, unsigned int *expr_size, unsigned int *feat_size) |
| 1393 | { |
| 1394 | unsigned int e = 0, f = 0, r = 0; |
| 1395 | uint8_t op; |
| 1396 | |
| 1397 | assert(iffeat); |
| 1398 | |
| 1399 | if (!iffeat->expr) { |
| 1400 | goto result; |
| 1401 | } |
| 1402 | |
| 1403 | do { |
| 1404 | op = iff_getop(iffeat->expr, e++); |
| 1405 | switch (op) { |
| 1406 | case LYS_IFF_NOT: |
| 1407 | if (!r) { |
| 1408 | r += 1; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1409 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1410 | break; |
| 1411 | case LYS_IFF_AND: |
| 1412 | case LYS_IFF_OR: |
| 1413 | if (!r) { |
| 1414 | r += 2; |
| 1415 | } else { |
| 1416 | r += 1; |
| 1417 | } |
| 1418 | break; |
| 1419 | case LYS_IFF_F: |
| 1420 | f++; |
| 1421 | if (r) { |
| 1422 | r--; |
| 1423 | } |
| 1424 | break; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1425 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1426 | } while(r); |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1427 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1428 | result: |
| 1429 | if (expr_size) { |
| 1430 | *expr_size = e; |
| 1431 | } |
| 1432 | if (feat_size) { |
| 1433 | *feat_size = f; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1434 | } |
| 1435 | } |
| 1436 | |
| 1437 | int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1438 | resolve_iffeature_compile(struct lys_iffeature *iffeat_expr, const char *value, struct lys_node *node, |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 1439 | int infeature, struct unres_schema *unres) |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1440 | { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1441 | const char *c = value; |
| 1442 | int r, rc = EXIT_FAILURE; |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1443 | int i, j, last_not, checkversion = 0; |
| 1444 | unsigned int f_size = 0, expr_size = 0, f_exp = 1; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1445 | uint8_t op; |
| 1446 | struct iff_stack stack = {0, 0, NULL}; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1447 | struct unres_iffeat_data *iff_data; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1448 | struct ly_ctx *ctx = node->module->ctx; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1449 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1450 | assert(c); |
| 1451 | |
| 1452 | if (isspace(c[0])) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1453 | LOGVAL(ctx, LYE_INCHAR, LY_VLOG_NONE, NULL, c[0], c); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1454 | return EXIT_FAILURE; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1455 | } |
| 1456 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1457 | /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */ |
| 1458 | for (i = j = last_not = 0; c[i]; i++) { |
| 1459 | if (c[i] == '(') { |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1460 | checkversion = 1; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1461 | j++; |
| 1462 | continue; |
| 1463 | } else if (c[i] == ')') { |
| 1464 | j--; |
| 1465 | continue; |
| 1466 | } else if (isspace(c[i])) { |
| 1467 | continue; |
| 1468 | } |
| 1469 | |
| 1470 | if (!strncmp(&c[i], "not", r = 3) || !strncmp(&c[i], "and", r = 3) || !strncmp(&c[i], "or", r = 2)) { |
| 1471 | if (c[i + r] == '\0') { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1472 | LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1473 | return EXIT_FAILURE; |
| 1474 | } else if (!isspace(c[i + r])) { |
| 1475 | /* feature name starting with the not/and/or */ |
| 1476 | last_not = 0; |
| 1477 | f_size++; |
| 1478 | } else if (c[i] == 'n') { /* not operation */ |
| 1479 | if (last_not) { |
| 1480 | /* double not */ |
| 1481 | expr_size = expr_size - 2; |
| 1482 | last_not = 0; |
| 1483 | } else { |
| 1484 | last_not = 1; |
| 1485 | } |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1486 | } else { /* and, or */ |
| 1487 | f_exp++; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1488 | /* not a not operation */ |
| 1489 | last_not = 0; |
| 1490 | } |
| 1491 | i += r; |
| 1492 | } else { |
| 1493 | f_size++; |
| 1494 | last_not = 0; |
| 1495 | } |
| 1496 | expr_size++; |
| 1497 | |
| 1498 | while (!isspace(c[i])) { |
Michal Vasko | d9a8366 | 2019-03-14 09:47:11 +0100 | [diff] [blame] | 1499 | if (c[i] == '(') { |
| 1500 | LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
| 1501 | return EXIT_FAILURE; |
| 1502 | } else if (!c[i] || c[i] == ')') { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1503 | i--; |
| 1504 | break; |
| 1505 | } |
| 1506 | i++; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1507 | } |
| 1508 | } |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1509 | if (j || f_exp != f_size) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1510 | /* not matching count of ( and ) */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1511 | LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1512 | return EXIT_FAILURE; |
| 1513 | } |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1514 | |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1515 | if (checkversion || expr_size > 1) { |
| 1516 | /* check that we have 1.1 module */ |
Radek Krejci | 13fde92 | 2018-05-16 10:45:58 +0200 | [diff] [blame] | 1517 | if (node->module->version != LYS_VERSION_1_1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1518 | LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
| 1519 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "YANG 1.1 if-feature expression found in 1.0 module."); |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1520 | return EXIT_FAILURE; |
| 1521 | } |
| 1522 | } |
| 1523 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1524 | /* allocate the memory */ |
| 1525 | iffeat_expr->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iffeat_expr->expr); |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1526 | iffeat_expr->features = calloc(f_size, sizeof *iffeat_expr->features); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1527 | stack.stack = malloc(expr_size * sizeof *stack.stack); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1528 | LY_CHECK_ERR_GOTO(!stack.stack || !iffeat_expr->expr || !iffeat_expr->features, LOGMEM(ctx), error); |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 1529 | stack.size = expr_size; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1530 | f_size--; expr_size--; /* used as indexes from now */ |
| 1531 | |
| 1532 | for (i--; i >= 0; i--) { |
| 1533 | if (c[i] == ')') { |
| 1534 | /* push it on stack */ |
| 1535 | iff_stack_push(&stack, LYS_IFF_RP); |
| 1536 | continue; |
| 1537 | } else if (c[i] == '(') { |
| 1538 | /* pop from the stack into result all operators until ) */ |
| 1539 | while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) { |
| 1540 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1541 | } |
| 1542 | continue; |
| 1543 | } else if (isspace(c[i])) { |
| 1544 | continue; |
| 1545 | } |
| 1546 | |
| 1547 | /* end operator or operand -> find beginning and get what is it */ |
| 1548 | j = i + 1; |
| 1549 | while (i >= 0 && !isspace(c[i]) && c[i] != '(') { |
| 1550 | i--; |
| 1551 | } |
| 1552 | i++; /* get back by one step */ |
| 1553 | |
Michal Vasko | 8801135 | 2018-07-12 08:49:07 +0200 | [diff] [blame] | 1554 | if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1555 | if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) { |
| 1556 | /* double not */ |
| 1557 | iff_stack_pop(&stack); |
| 1558 | } else { |
| 1559 | /* not has the highest priority, so do not pop from the stack |
| 1560 | * as in case of AND and OR */ |
| 1561 | iff_stack_push(&stack, LYS_IFF_NOT); |
| 1562 | } |
Michal Vasko | 8801135 | 2018-07-12 08:49:07 +0200 | [diff] [blame] | 1563 | } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1564 | /* as for OR - pop from the stack all operators with the same or higher |
| 1565 | * priority and store them to the result, then push the AND to the stack */ |
| 1566 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) { |
| 1567 | op = iff_stack_pop(&stack); |
| 1568 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1569 | } |
| 1570 | iff_stack_push(&stack, LYS_IFF_AND); |
Michal Vasko | 8801135 | 2018-07-12 08:49:07 +0200 | [diff] [blame] | 1571 | } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1572 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) { |
| 1573 | op = iff_stack_pop(&stack); |
| 1574 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1575 | } |
| 1576 | iff_stack_push(&stack, LYS_IFF_OR); |
| 1577 | } else { |
| 1578 | /* feature name, length is j - i */ |
| 1579 | |
| 1580 | /* add it to the result */ |
| 1581 | iff_setop(iffeat_expr->expr, LYS_IFF_F, expr_size--); |
| 1582 | |
| 1583 | /* now get the link to the feature definition. Since it can be |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1584 | * forward referenced, we have to keep the feature name in auxiliary |
| 1585 | * structure passed into unres */ |
| 1586 | iff_data = malloc(sizeof *iff_data); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1587 | LY_CHECK_ERR_GOTO(!iff_data, LOGMEM(ctx), error); |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1588 | iff_data->node = node; |
| 1589 | iff_data->fname = lydict_insert(node->module->ctx, &c[i], j - i); |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 1590 | iff_data->infeature = infeature; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1591 | r = unres_schema_add_node(node->module, unres, &iffeat_expr->features[f_size], UNRES_IFFEAT, |
| 1592 | (struct lys_node *)iff_data); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1593 | f_size--; |
| 1594 | |
| 1595 | if (r == -1) { |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 1596 | lydict_remove(node->module->ctx, iff_data->fname); |
Pavol Vican | 4d08451 | 2016-09-29 16:38:12 +0200 | [diff] [blame] | 1597 | free(iff_data); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1598 | goto error; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1599 | } |
| 1600 | } |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1601 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1602 | while (stack.index) { |
| 1603 | op = iff_stack_pop(&stack); |
| 1604 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1605 | } |
| 1606 | |
| 1607 | if (++expr_size || ++f_size) { |
| 1608 | /* not all expected operators and operands found */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1609 | LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1610 | rc = EXIT_FAILURE; |
| 1611 | } else { |
| 1612 | rc = EXIT_SUCCESS; |
| 1613 | } |
| 1614 | |
| 1615 | error: |
| 1616 | /* cleanup */ |
| 1617 | iff_stack_clean(&stack); |
| 1618 | |
| 1619 | return rc; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1620 | } |
| 1621 | |
| 1622 | /** |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1623 | * @brief Resolve (find) a data node based on a schema-nodeid. |
| 1624 | * |
| 1625 | * Used for resolving unique statements - so id is expected to be relative and local (without reference to a different |
| 1626 | * module). |
| 1627 | * |
| 1628 | */ |
| 1629 | struct lyd_node * |
| 1630 | resolve_data_descendant_schema_nodeid(const char *nodeid, struct lyd_node *start) |
| 1631 | { |
| 1632 | char *str, *token, *p; |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1633 | struct lyd_node *result = NULL, *iter; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1634 | const struct lys_node *schema = NULL; |
| 1635 | |
| 1636 | assert(nodeid && start); |
| 1637 | |
| 1638 | if (nodeid[0] == '/') { |
| 1639 | return NULL; |
| 1640 | } |
| 1641 | |
| 1642 | str = p = strdup(nodeid); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1643 | LY_CHECK_ERR_RETURN(!str, LOGMEM(start->schema->module->ctx), NULL); |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1644 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1645 | while (p) { |
| 1646 | token = p; |
| 1647 | p = strchr(p, '/'); |
| 1648 | if (p) { |
| 1649 | *p = '\0'; |
| 1650 | p++; |
| 1651 | } |
| 1652 | |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1653 | if (p) { |
| 1654 | /* inner node */ |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1655 | if (resolve_descendant_schema_nodeid(token, schema ? schema->child : start->schema, |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 1656 | LYS_CONTAINER | LYS_CHOICE | LYS_CASE | LYS_LEAF, 0, &schema) |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1657 | || !schema) { |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1658 | result = NULL; |
| 1659 | break; |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1660 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1661 | |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1662 | if (schema->nodetype & (LYS_CHOICE | LYS_CASE)) { |
| 1663 | continue; |
| 1664 | } |
| 1665 | } else { |
| 1666 | /* final node */ |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 1667 | if (resolve_descendant_schema_nodeid(token, schema ? schema->child : start->schema, LYS_LEAF, 0, &schema) |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1668 | || !schema) { |
| 1669 | result = NULL; |
| 1670 | break; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1671 | } |
| 1672 | } |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1673 | LY_TREE_FOR(result ? result->child : start, iter) { |
| 1674 | if (iter->schema == schema) { |
| 1675 | /* move in data tree according to returned schema */ |
| 1676 | result = iter; |
| 1677 | break; |
| 1678 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1679 | } |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1680 | if (!iter) { |
| 1681 | /* instance not found */ |
| 1682 | result = NULL; |
| 1683 | break; |
| 1684 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1685 | } |
| 1686 | free(str); |
| 1687 | |
| 1688 | return result; |
| 1689 | } |
| 1690 | |
Radek Krejci | 1a9c361 | 2017-04-24 14:49:43 +0200 | [diff] [blame] | 1691 | int |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1692 | schema_nodeid_siblingcheck(const struct lys_node *sibling, const struct lys_module *cur_module, const char *mod_name, |
| 1693 | int mod_name_len, const char *name, int nam_len) |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1694 | { |
| 1695 | const struct lys_module *prefix_mod; |
| 1696 | |
Michal Vasko | cdb3f06 | 2018-02-01 09:55:06 +0100 | [diff] [blame] | 1697 | /* handle special names */ |
| 1698 | if (name[0] == '*') { |
| 1699 | return 2; |
| 1700 | } else if (name[0] == '.') { |
| 1701 | return 3; |
| 1702 | } |
| 1703 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1704 | /* name check */ |
Michal Vasko | cdb3f06 | 2018-02-01 09:55:06 +0100 | [diff] [blame] | 1705 | if (strncmp(name, sibling->name, nam_len) || sibling->name[nam_len]) { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1706 | return 1; |
| 1707 | } |
| 1708 | |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1709 | /* module check */ |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1710 | if (mod_name) { |
Michal Vasko | 921eb6b | 2017-10-13 10:01:39 +0200 | [diff] [blame] | 1711 | prefix_mod = lyp_get_module(cur_module, NULL, 0, mod_name, mod_name_len, 0); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1712 | if (!prefix_mod) { |
| 1713 | return -1; |
| 1714 | } |
| 1715 | } else { |
| 1716 | prefix_mod = cur_module; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1717 | } |
| 1718 | if (prefix_mod != lys_node_module(sibling)) { |
| 1719 | return 1; |
| 1720 | } |
| 1721 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1722 | /* match */ |
Michal Vasko | cdb3f06 | 2018-02-01 09:55:06 +0100 | [diff] [blame] | 1723 | return 0; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1724 | } |
| 1725 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1726 | /* keys do not have to be ordered and do not have to be all of them */ |
| 1727 | static int |
| 1728 | resolve_extended_schema_nodeid_predicate(const char *nodeid, const struct lys_node *node, |
| 1729 | const struct lys_module *cur_module, int *nodeid_end) |
| 1730 | { |
| 1731 | int mod_len, nam_len, has_predicate, r, i; |
| 1732 | const char *model, *name; |
| 1733 | struct lys_node_list *list; |
| 1734 | |
| 1735 | if (!(node->nodetype & (LYS_LIST | LYS_LEAFLIST))) { |
| 1736 | return 1; |
| 1737 | } |
| 1738 | |
| 1739 | list = (struct lys_node_list *)node; |
| 1740 | do { |
| 1741 | r = parse_schema_json_predicate(nodeid, &model, &mod_len, &name, &nam_len, NULL, NULL, &has_predicate); |
| 1742 | if (r < 1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1743 | LOGVAL(cur_module->ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, nodeid[r], &nodeid[r]); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1744 | return -1; |
| 1745 | } |
| 1746 | nodeid += r; |
| 1747 | |
| 1748 | if (node->nodetype == LYS_LEAFLIST) { |
| 1749 | /* just check syntax */ |
| 1750 | if (model || !name || (name[0] != '.') || has_predicate) { |
| 1751 | return 1; |
| 1752 | } |
| 1753 | break; |
| 1754 | } else { |
| 1755 | /* check the key */ |
| 1756 | for (i = 0; i < list->keys_size; ++i) { |
| 1757 | if (strncmp(list->keys[i]->name, name, nam_len) || list->keys[i]->name[nam_len]) { |
| 1758 | continue; |
| 1759 | } |
| 1760 | if (model) { |
| 1761 | if (strncmp(lys_node_module((struct lys_node *)list->keys[i])->name, model, mod_len) |
| 1762 | || lys_node_module((struct lys_node *)list->keys[i])->name[mod_len]) { |
| 1763 | continue; |
| 1764 | } |
| 1765 | } else { |
| 1766 | if (lys_node_module((struct lys_node *)list->keys[i]) != cur_module) { |
| 1767 | continue; |
| 1768 | } |
| 1769 | } |
| 1770 | |
| 1771 | /* match */ |
| 1772 | break; |
| 1773 | } |
| 1774 | |
| 1775 | if (i == list->keys_size) { |
| 1776 | return 1; |
| 1777 | } |
| 1778 | } |
| 1779 | } while (has_predicate); |
| 1780 | |
| 1781 | if (!nodeid[0]) { |
| 1782 | *nodeid_end = 1; |
| 1783 | } |
| 1784 | return 0; |
| 1785 | } |
| 1786 | |
Michal Vasko | 9723426 | 2018-02-01 09:53:01 +0100 | [diff] [blame] | 1787 | /* start_parent - relative, module - absolute, -1 error (logged), EXIT_SUCCESS ok |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 1788 | */ |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1789 | int |
Michal Vasko | 9723426 | 2018-02-01 09:53:01 +0100 | [diff] [blame] | 1790 | resolve_schema_nodeid(const char *nodeid, const struct lys_node *start_parent, const struct lys_module *cur_module, |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1791 | struct ly_set **ret, int extended, int no_node_error) |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1792 | { |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 1793 | const char *name, *mod_name, *id, *backup_mod_name = NULL, *yang_data_name = NULL; |
Michal Vasko | 9723426 | 2018-02-01 09:53:01 +0100 | [diff] [blame] | 1794 | const struct lys_node *sibling, *next, *elem; |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1795 | struct lys_node_augment *last_aug; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1796 | int r, nam_len, mod_name_len = 0, is_relative = -1, all_desc, has_predicate, nodeid_end = 0; |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 1797 | int yang_data_name_len, backup_mod_name_len = 0; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1798 | /* resolved import module from the start module, it must match the next node-name-match sibling */ |
Radek Krejci | daa547a | 2017-09-22 15:56:27 +0200 | [diff] [blame] | 1799 | const struct lys_module *start_mod, *aux_mod = NULL; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1800 | char *str; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1801 | struct ly_ctx *ctx; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1802 | |
Michal Vasko | 9723426 | 2018-02-01 09:53:01 +0100 | [diff] [blame] | 1803 | assert(nodeid && (start_parent || cur_module) && ret); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1804 | *ret = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1805 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1806 | if (!cur_module) { |
Michal Vasko | 9723426 | 2018-02-01 09:53:01 +0100 | [diff] [blame] | 1807 | cur_module = lys_node_module(start_parent); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1808 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1809 | ctx = cur_module->ctx; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1810 | id = nodeid; |
| 1811 | |
PavolVican | 195cf39 | 2018-02-23 13:24:45 +0100 | [diff] [blame] | 1812 | r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, NULL, NULL, 1); |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 1813 | if (r < 1) { |
Radek Krejci | 29e8528 | 2019-06-10 09:11:10 +0200 | [diff] [blame] | 1814 | LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 1815 | return -1; |
| 1816 | } |
| 1817 | |
| 1818 | if (name[0] == '#') { |
| 1819 | if (is_relative) { |
| 1820 | LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, '#', name); |
| 1821 | return -1; |
| 1822 | } |
| 1823 | yang_data_name = name + 1; |
| 1824 | yang_data_name_len = nam_len - 1; |
| 1825 | backup_mod_name = mod_name; |
| 1826 | backup_mod_name_len = mod_name_len; |
| 1827 | id += r; |
| 1828 | } else { |
| 1829 | is_relative = -1; |
| 1830 | } |
| 1831 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1832 | r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate, |
| 1833 | (extended ? &all_desc : NULL), extended); |
| 1834 | if (r < 1) { |
Radek Krejci | 29e8528 | 2019-06-10 09:11:10 +0200 | [diff] [blame] | 1835 | LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1836 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1837 | } |
| 1838 | id += r; |
| 1839 | |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 1840 | if (backup_mod_name) { |
| 1841 | mod_name = backup_mod_name; |
| 1842 | mod_name_len = backup_mod_name_len; |
| 1843 | } |
| 1844 | |
Michal Vasko | 9723426 | 2018-02-01 09:53:01 +0100 | [diff] [blame] | 1845 | if (is_relative && !start_parent) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1846 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_STR, nodeid, "Starting node must be provided for relative paths."); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1847 | return -1; |
| 1848 | } |
| 1849 | |
| 1850 | /* descendant-schema-nodeid */ |
| 1851 | if (is_relative) { |
Michal Vasko | 9723426 | 2018-02-01 09:53:01 +0100 | [diff] [blame] | 1852 | cur_module = start_mod = lys_node_module(start_parent); |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1853 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1854 | /* absolute-schema-nodeid */ |
| 1855 | } else { |
Michal Vasko | 921eb6b | 2017-10-13 10:01:39 +0200 | [diff] [blame] | 1856 | start_mod = lyp_get_module(cur_module, NULL, 0, mod_name, mod_name_len, 0); |
Michal Vasko | e290563 | 2016-02-11 15:42:24 +0100 | [diff] [blame] | 1857 | if (!start_mod) { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1858 | str = strndup(mod_name, mod_name_len); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1859 | LOGVAL(ctx, LYE_PATH_INMOD, LY_VLOG_STR, str); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1860 | free(str); |
Michal Vasko | e290563 | 2016-02-11 15:42:24 +0100 | [diff] [blame] | 1861 | return -1; |
| 1862 | } |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1863 | start_parent = NULL; |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 1864 | if (yang_data_name) { |
| 1865 | start_parent = lyp_get_yang_data_template(start_mod, yang_data_name, yang_data_name_len); |
| 1866 | if (!start_parent) { |
| 1867 | str = strndup(nodeid, (yang_data_name + yang_data_name_len) - nodeid); |
| 1868 | LOGVAL(ctx, LYE_PATH_INNODE, LY_VLOG_STR, str); |
| 1869 | free(str); |
| 1870 | return -1; |
| 1871 | } |
| 1872 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1873 | } |
| 1874 | |
| 1875 | while (1) { |
| 1876 | sibling = NULL; |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1877 | last_aug = NULL; |
| 1878 | |
| 1879 | if (start_parent) { |
Michal Vasko | 1731577 | 2017-07-10 15:15:39 +0200 | [diff] [blame] | 1880 | if (mod_name && (strncmp(mod_name, cur_module->name, mod_name_len) |
| 1881 | || (mod_name_len != (signed)strlen(cur_module->name)))) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1882 | /* we are getting into another module (augment) */ |
Michal Vasko | 921eb6b | 2017-10-13 10:01:39 +0200 | [diff] [blame] | 1883 | aux_mod = lyp_get_module(cur_module, NULL, 0, mod_name, mod_name_len, 0); |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1884 | if (!aux_mod) { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1885 | str = strndup(mod_name, mod_name_len); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1886 | LOGVAL(ctx, LYE_PATH_INMOD, LY_VLOG_STR, str); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1887 | free(str); |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1888 | return -1; |
| 1889 | } |
| 1890 | } else { |
Michal Vasko | 201c339 | 2017-07-10 15:15:39 +0200 | [diff] [blame] | 1891 | /* there is no mod_name, so why are we checking augments again? |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1892 | * because this module may be not implemented and it augments something in another module and |
| 1893 | * there is another augment augmenting that previous one */ |
Michal Vasko | 1731577 | 2017-07-10 15:15:39 +0200 | [diff] [blame] | 1894 | aux_mod = cur_module; |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1895 | } |
| 1896 | |
Michal Vasko | 3b2ad46 | 2018-07-10 15:40:53 +0200 | [diff] [blame] | 1897 | /* look into augments */ |
| 1898 | if (!extended) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1899 | get_next_augment: |
| 1900 | last_aug = lys_getnext_target_aug(last_aug, aux_mod, start_parent); |
| 1901 | } |
| 1902 | } |
| 1903 | |
| 1904 | while ((sibling = lys_getnext(sibling, (last_aug ? (struct lys_node *)last_aug : start_parent), start_mod, |
Michal Vasko | cb45f47 | 2018-02-12 10:47:42 +0100 | [diff] [blame] | 1905 | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT | LYS_GETNEXT_PARENTUSES | LYS_GETNEXT_NOSTATECHECK))) { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1906 | r = schema_nodeid_siblingcheck(sibling, cur_module, mod_name, mod_name_len, name, nam_len); |
| 1907 | |
| 1908 | /* resolve predicate */ |
| 1909 | if (extended && ((r == 0) || (r == 2) || (r == 3)) && has_predicate) { |
| 1910 | r = resolve_extended_schema_nodeid_predicate(id, sibling, cur_module, &nodeid_end); |
| 1911 | if (r == 1) { |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1912 | continue; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1913 | } else if (r == -1) { |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1914 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1915 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1916 | } else if (!id[0]) { |
| 1917 | nodeid_end = 1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1918 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1919 | |
| 1920 | if (r == 0) { |
| 1921 | /* one matching result */ |
| 1922 | if (nodeid_end) { |
| 1923 | *ret = ly_set_new(); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1924 | LY_CHECK_ERR_RETURN(!*ret, LOGMEM(ctx), -1); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1925 | ly_set_add(*ret, (void *)sibling, LY_SET_OPT_USEASLIST); |
| 1926 | } else { |
| 1927 | if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
| 1928 | return -1; |
| 1929 | } |
| 1930 | start_parent = sibling; |
| 1931 | } |
| 1932 | break; |
| 1933 | } else if (r == 1) { |
| 1934 | continue; |
| 1935 | } else if (r == 2) { |
| 1936 | /* "*" */ |
| 1937 | if (!*ret) { |
| 1938 | *ret = ly_set_new(); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1939 | LY_CHECK_ERR_RETURN(!*ret, LOGMEM(ctx), -1); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1940 | } |
| 1941 | ly_set_add(*ret, (void *)sibling, LY_SET_OPT_USEASLIST); |
| 1942 | if (all_desc) { |
| 1943 | LY_TREE_DFS_BEGIN(sibling, next, elem) { |
| 1944 | if (elem != sibling) { |
| 1945 | ly_set_add(*ret, (void *)elem, LY_SET_OPT_USEASLIST); |
| 1946 | } |
| 1947 | |
| 1948 | LY_TREE_DFS_END(sibling, next, elem); |
| 1949 | } |
| 1950 | } |
| 1951 | } else if (r == 3) { |
| 1952 | /* "." */ |
| 1953 | if (!*ret) { |
| 1954 | *ret = ly_set_new(); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1955 | LY_CHECK_ERR_RETURN(!*ret, LOGMEM(ctx), -1); |
Michal Vasko | 8b3a275 | 2019-05-17 15:32:44 +0200 | [diff] [blame] | 1956 | if (start_parent) { |
| 1957 | ly_set_add(*ret, (void *)start_parent, LY_SET_OPT_USEASLIST); |
| 1958 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1959 | } |
| 1960 | ly_set_add(*ret, (void *)sibling, LY_SET_OPT_USEASLIST); |
| 1961 | if (all_desc) { |
| 1962 | LY_TREE_DFS_BEGIN(sibling, next, elem) { |
| 1963 | if (elem != sibling) { |
| 1964 | ly_set_add(*ret, (void *)elem, LY_SET_OPT_USEASLIST); |
| 1965 | } |
| 1966 | |
| 1967 | LY_TREE_DFS_END(sibling, next, elem); |
| 1968 | } |
| 1969 | } |
| 1970 | } else { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1971 | LOGINT(ctx); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1972 | return -1; |
| 1973 | } |
| 1974 | } |
| 1975 | |
| 1976 | /* skip predicate */ |
| 1977 | if (extended && has_predicate) { |
| 1978 | while (id[0] == '[') { |
| 1979 | id = strchr(id, ']'); |
| 1980 | if (!id) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1981 | LOGINT(ctx); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1982 | return -1; |
| 1983 | } |
| 1984 | ++id; |
| 1985 | } |
| 1986 | } |
| 1987 | |
| 1988 | if (nodeid_end && ((r == 0) || (r == 2) || (r == 3))) { |
| 1989 | return EXIT_SUCCESS; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1990 | } |
| 1991 | |
| 1992 | /* no match */ |
| 1993 | if (!sibling) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1994 | if (last_aug) { |
| 1995 | /* it still could be in another augment */ |
| 1996 | goto get_next_augment; |
| 1997 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1998 | if (no_node_error) { |
| 1999 | str = strndup(nodeid, (name - nodeid) + nam_len); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2000 | LOGVAL(ctx, LYE_PATH_INNODE, LY_VLOG_STR, str); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2001 | free(str); |
| 2002 | return -1; |
| 2003 | } |
Michal Vasko | a426fef | 2016-03-07 10:47:31 +0100 | [diff] [blame] | 2004 | *ret = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2005 | return EXIT_SUCCESS; |
| 2006 | } |
| 2007 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2008 | r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate, |
| 2009 | (extended ? &all_desc : NULL), extended); |
| 2010 | if (r < 1) { |
Radek Krejci | 29e8528 | 2019-06-10 09:11:10 +0200 | [diff] [blame] | 2011 | LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2012 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2013 | } |
| 2014 | id += r; |
| 2015 | } |
| 2016 | |
| 2017 | /* cannot get here */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2018 | LOGINT(ctx); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2019 | return -1; |
| 2020 | } |
| 2021 | |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 2022 | /* unique, refine, |
| 2023 | * >0 - unexpected char on position (ret - 1), |
| 2024 | * 0 - ok (but ret can still be NULL), |
| 2025 | * -1 - error, |
| 2026 | * -2 - violated no_innerlist */ |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2027 | int |
| 2028 | resolve_descendant_schema_nodeid(const char *nodeid, const struct lys_node *start, int ret_nodetype, |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 2029 | int no_innerlist, const struct lys_node **ret) |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2030 | { |
| 2031 | const char *name, *mod_name, *id; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 2032 | const struct lys_node *sibling, *start_parent; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2033 | int r, nam_len, mod_name_len, is_relative = -1; |
| 2034 | /* resolved import module from the start module, it must match the next node-name-match sibling */ |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 2035 | const struct lys_module *module; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2036 | |
Radek Krejci | c3f1b6f | 2017-02-15 10:51:10 +0100 | [diff] [blame] | 2037 | assert(nodeid && ret); |
Radek Krejci | e207741 | 2017-01-26 16:03:39 +0100 | [diff] [blame] | 2038 | assert(!(ret_nodetype & (LYS_USES | LYS_AUGMENT | LYS_GROUPING))); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2039 | |
Radek Krejci | c3f1b6f | 2017-02-15 10:51:10 +0100 | [diff] [blame] | 2040 | if (!start) { |
| 2041 | /* leaf not found */ |
| 2042 | return 0; |
| 2043 | } |
| 2044 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2045 | id = nodeid; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2046 | module = lys_node_module(start); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2047 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2048 | if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, NULL, NULL, 0)) < 1) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2049 | return ((id - nodeid) - r) + 1; |
| 2050 | } |
| 2051 | id += r; |
| 2052 | |
| 2053 | if (!is_relative) { |
| 2054 | return -1; |
| 2055 | } |
| 2056 | |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 2057 | start_parent = lys_parent(start); |
Michal Vasko | 74a991b | 2017-03-31 09:17:22 +0200 | [diff] [blame] | 2058 | while ((start_parent->nodetype == LYS_USES) && lys_parent(start_parent)) { |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 2059 | start_parent = lys_parent(start_parent); |
| 2060 | } |
| 2061 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2062 | while (1) { |
| 2063 | sibling = NULL; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 2064 | while ((sibling = lys_getnext(sibling, start_parent, module, |
Michal Vasko | 3223a01 | 2020-03-11 08:46:10 +0100 | [diff] [blame] | 2065 | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT | LYS_GETNEXT_PARENTUSES |
| 2066 | | LYS_GETNEXT_NOSTATECHECK))) { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2067 | r = schema_nodeid_siblingcheck(sibling, module, mod_name, mod_name_len, name, nam_len); |
| 2068 | if (r == 0) { |
| 2069 | if (!id[0]) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2070 | if (!(sibling->nodetype & ret_nodetype)) { |
| 2071 | /* wrong node type, too bad */ |
| 2072 | continue; |
| 2073 | } |
| 2074 | *ret = sibling; |
| 2075 | return EXIT_SUCCESS; |
| 2076 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2077 | start_parent = sibling; |
| 2078 | break; |
| 2079 | } else if (r == 1) { |
| 2080 | continue; |
| 2081 | } else { |
| 2082 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2083 | } |
| 2084 | } |
| 2085 | |
| 2086 | /* no match */ |
| 2087 | if (!sibling) { |
Michal Vasko | a426fef | 2016-03-07 10:47:31 +0100 | [diff] [blame] | 2088 | *ret = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2089 | return EXIT_SUCCESS; |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 2090 | } else if (no_innerlist && sibling->nodetype == LYS_LIST) { |
| 2091 | *ret = NULL; |
| 2092 | return -2; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2093 | } |
| 2094 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2095 | if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, NULL, NULL, 0)) < 1) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2096 | return ((id - nodeid) - r) + 1; |
| 2097 | } |
| 2098 | id += r; |
| 2099 | } |
| 2100 | |
| 2101 | /* cannot get here */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2102 | LOGINT(module->ctx); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2103 | return -1; |
| 2104 | } |
| 2105 | |
| 2106 | /* choice default */ |
| 2107 | int |
| 2108 | resolve_choice_default_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_node **ret) |
| 2109 | { |
| 2110 | /* cannot actually be a path */ |
| 2111 | if (strchr(nodeid, '/')) { |
| 2112 | return -1; |
| 2113 | } |
| 2114 | |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 2115 | return resolve_descendant_schema_nodeid(nodeid, start, LYS_NO_RPC_NOTIF_NODE, 0, ret); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2116 | } |
| 2117 | |
| 2118 | /* uses, -1 error, EXIT_SUCCESS ok (but ret can still be NULL), >0 unexpected char on ret - 1 */ |
| 2119 | static int |
| 2120 | resolve_uses_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_node_grp **ret) |
| 2121 | { |
| 2122 | const struct lys_module *module; |
| 2123 | const char *mod_prefix, *name; |
| 2124 | int i, mod_prefix_len, nam_len; |
| 2125 | |
| 2126 | /* parse the identifier, it must be parsed on one call */ |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2127 | if (((i = parse_node_identifier(nodeid, &mod_prefix, &mod_prefix_len, &name, &nam_len, NULL, 0)) < 1) || nodeid[i]) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2128 | return -i + 1; |
| 2129 | } |
| 2130 | |
Michal Vasko | 921eb6b | 2017-10-13 10:01:39 +0200 | [diff] [blame] | 2131 | module = lyp_get_module(start->module, mod_prefix, mod_prefix_len, NULL, 0, 0); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2132 | if (!module) { |
| 2133 | return -1; |
| 2134 | } |
Radek Krejci | 0a8205d | 2017-03-01 16:25:29 +0100 | [diff] [blame] | 2135 | if (module != lys_main_module(start->module)) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2136 | start = module->data; |
| 2137 | } |
| 2138 | |
| 2139 | *ret = lys_find_grouping_up(name, (struct lys_node *)start); |
| 2140 | |
| 2141 | return EXIT_SUCCESS; |
| 2142 | } |
| 2143 | |
| 2144 | int |
| 2145 | resolve_absolute_schema_nodeid(const char *nodeid, const struct lys_module *module, int ret_nodetype, |
| 2146 | const struct lys_node **ret) |
| 2147 | { |
| 2148 | const char *name, *mod_name, *id; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 2149 | const struct lys_node *sibling, *start_parent; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2150 | int r, nam_len, mod_name_len, is_relative = -1; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 2151 | const struct lys_module *abs_start_mod; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2152 | |
| 2153 | assert(nodeid && module && ret); |
| 2154 | assert(!(ret_nodetype & (LYS_USES | LYS_AUGMENT)) && ((ret_nodetype == LYS_GROUPING) || !(ret_nodetype & LYS_GROUPING))); |
| 2155 | |
| 2156 | id = nodeid; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 2157 | start_parent = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2158 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2159 | if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, NULL, NULL, 0)) < 1) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2160 | return ((id - nodeid) - r) + 1; |
| 2161 | } |
| 2162 | id += r; |
| 2163 | |
| 2164 | if (is_relative) { |
| 2165 | return -1; |
| 2166 | } |
| 2167 | |
Michal Vasko | 921eb6b | 2017-10-13 10:01:39 +0200 | [diff] [blame] | 2168 | abs_start_mod = lyp_get_module(module, NULL, 0, mod_name, mod_name_len, 0); |
Michal Vasko | e290563 | 2016-02-11 15:42:24 +0100 | [diff] [blame] | 2169 | if (!abs_start_mod) { |
| 2170 | return -1; |
| 2171 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2172 | |
| 2173 | while (1) { |
| 2174 | sibling = NULL; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 2175 | while ((sibling = lys_getnext(sibling, start_parent, abs_start_mod, LYS_GETNEXT_WITHCHOICE |
Michal Vasko | cb45f47 | 2018-02-12 10:47:42 +0100 | [diff] [blame] | 2176 | | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT | LYS_GETNEXT_WITHGROUPING | LYS_GETNEXT_NOSTATECHECK))) { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2177 | r = schema_nodeid_siblingcheck(sibling, module, mod_name, mod_name_len, name, nam_len); |
| 2178 | if (r == 0) { |
| 2179 | if (!id[0]) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2180 | if (!(sibling->nodetype & ret_nodetype)) { |
| 2181 | /* wrong node type, too bad */ |
| 2182 | continue; |
| 2183 | } |
| 2184 | *ret = sibling; |
| 2185 | return EXIT_SUCCESS; |
| 2186 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2187 | start_parent = sibling; |
| 2188 | break; |
| 2189 | } else if (r == 1) { |
| 2190 | continue; |
| 2191 | } else { |
| 2192 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2193 | } |
| 2194 | } |
| 2195 | |
| 2196 | /* no match */ |
| 2197 | if (!sibling) { |
Michal Vasko | a426fef | 2016-03-07 10:47:31 +0100 | [diff] [blame] | 2198 | *ret = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2199 | return EXIT_SUCCESS; |
| 2200 | } |
| 2201 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2202 | if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, NULL, NULL, 0)) < 1) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2203 | return ((id - nodeid) - r) + 1; |
| 2204 | } |
| 2205 | id += r; |
| 2206 | } |
| 2207 | |
| 2208 | /* cannot get here */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2209 | LOGINT(module->ctx); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2210 | return -1; |
| 2211 | } |
| 2212 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2213 | static int |
Michal Vasko | f68a49e | 2017-08-14 13:23:37 +0200 | [diff] [blame] | 2214 | resolve_json_schema_list_predicate(const char *predicate, const struct lys_node_list *list, int *parsed) |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2215 | { |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2216 | const char *mod_name, *name; |
| 2217 | int mod_name_len, nam_len, has_predicate, i; |
| 2218 | struct lys_node *key; |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2219 | |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2220 | if (((i = parse_schema_json_predicate(predicate, &mod_name, &mod_name_len, &name, &nam_len, NULL, NULL, &has_predicate)) < 1) |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 2221 | || !strncmp(name, ".", nam_len)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2222 | LOGVAL(list->module->ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-i], &predicate[-i]); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2223 | return -1; |
| 2224 | } |
| 2225 | |
| 2226 | predicate += i; |
| 2227 | *parsed += i; |
| 2228 | |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2229 | if (!isdigit(name[0])) { |
| 2230 | for (i = 0; i < list->keys_size; ++i) { |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2231 | key = (struct lys_node *)list->keys[i]; |
| 2232 | if (!strncmp(key->name, name, nam_len) && !key->name[nam_len]) { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2233 | break; |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2234 | } |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2235 | } |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2236 | |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2237 | if (i == list->keys_size) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2238 | LOGVAL(list->module->ctx, LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name); |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2239 | return -1; |
| 2240 | } |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2241 | } |
| 2242 | |
| 2243 | /* more predicates? */ |
| 2244 | if (has_predicate) { |
Michal Vasko | f68a49e | 2017-08-14 13:23:37 +0200 | [diff] [blame] | 2245 | return resolve_json_schema_list_predicate(predicate, list, parsed); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2246 | } |
| 2247 | |
| 2248 | return 0; |
| 2249 | } |
| 2250 | |
Michal Vasko | 8d26e5c | 2016-09-08 10:03:49 +0200 | [diff] [blame] | 2251 | /* cannot return LYS_GROUPING, LYS_AUGMENT, LYS_USES, logs directly */ |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2252 | const struct lys_node * |
Michal Vasko | 15b1308 | 2019-05-09 10:22:46 +0200 | [diff] [blame] | 2253 | resolve_json_nodeid(const char *nodeid, const struct ly_ctx *ctx, const struct lys_node *start, int output) |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2254 | { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2255 | char *str; |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 2256 | const char *name, *mod_name, *id, *backup_mod_name = NULL, *yang_data_name = NULL; |
Michal Vasko | b374440 | 2017-08-03 14:23:58 +0200 | [diff] [blame] | 2257 | const struct lys_node *sibling, *start_parent, *parent; |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 2258 | int r, nam_len, mod_name_len, is_relative = -1, has_predicate; |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 2259 | int yang_data_name_len, backup_mod_name_len; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2260 | /* resolved import module from the start module, it must match the next node-name-match sibling */ |
Michal Vasko | f68a49e | 2017-08-14 13:23:37 +0200 | [diff] [blame] | 2261 | const struct lys_module *prefix_mod, *module, *prev_mod; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2262 | |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2263 | assert(nodeid && (ctx || start)); |
| 2264 | if (!ctx) { |
| 2265 | ctx = start->module->ctx; |
| 2266 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2267 | |
| 2268 | id = nodeid; |
| 2269 | |
PavolVican | 195cf39 | 2018-02-23 13:24:45 +0100 | [diff] [blame] | 2270 | if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, NULL, NULL, 1)) < 1) { |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 2271 | LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
| 2272 | return NULL; |
| 2273 | } |
| 2274 | |
| 2275 | if (name[0] == '#') { |
| 2276 | if (is_relative) { |
| 2277 | LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, '#', name); |
| 2278 | return NULL; |
| 2279 | } |
| 2280 | yang_data_name = name + 1; |
| 2281 | yang_data_name_len = nam_len - 1; |
| 2282 | backup_mod_name = mod_name; |
| 2283 | backup_mod_name_len = mod_name_len; |
| 2284 | id += r; |
| 2285 | } else { |
| 2286 | is_relative = -1; |
| 2287 | } |
| 2288 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2289 | if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate, NULL, 0)) < 1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2290 | LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2291 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2292 | } |
| 2293 | id += r; |
| 2294 | |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 2295 | if (backup_mod_name) { |
| 2296 | mod_name = backup_mod_name; |
| 2297 | mod_name_len = backup_mod_name_len; |
| 2298 | } |
| 2299 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2300 | if (is_relative) { |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2301 | assert(start); |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 2302 | start_parent = start; |
| 2303 | while (start_parent && (start_parent->nodetype == LYS_USES)) { |
| 2304 | start_parent = lys_parent(start_parent); |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2305 | } |
Michal Vasko | f68a49e | 2017-08-14 13:23:37 +0200 | [diff] [blame] | 2306 | module = start->module; |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2307 | } else { |
| 2308 | if (!mod_name) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2309 | str = strndup(nodeid, (name + nam_len) - nodeid); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2310 | LOGVAL(ctx, LYE_PATH_MISSMOD, LY_VLOG_STR, nodeid); |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2311 | free(str); |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2312 | return NULL; |
| 2313 | } |
| 2314 | |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2315 | str = strndup(mod_name, mod_name_len); |
| 2316 | module = ly_ctx_get_module(ctx, str, NULL, 1); |
| 2317 | free(str); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2318 | |
Michal Vasko | f68a49e | 2017-08-14 13:23:37 +0200 | [diff] [blame] | 2319 | if (!module) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2320 | str = strndup(nodeid, (mod_name + mod_name_len) - nodeid); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2321 | LOGVAL(ctx, LYE_PATH_INMOD, LY_VLOG_STR, str); |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2322 | free(str); |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2323 | return NULL; |
| 2324 | } |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 2325 | start_parent = NULL; |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 2326 | if (yang_data_name) { |
| 2327 | start_parent = lyp_get_yang_data_template(module, yang_data_name, yang_data_name_len); |
| 2328 | if (!start_parent) { |
| 2329 | str = strndup(nodeid, (yang_data_name + yang_data_name_len) - nodeid); |
| 2330 | LOGVAL(ctx, LYE_PATH_INNODE, LY_VLOG_STR, str); |
| 2331 | free(str); |
| 2332 | return NULL; |
| 2333 | } |
| 2334 | } |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2335 | |
| 2336 | /* now it's as if there was no module name */ |
| 2337 | mod_name = NULL; |
| 2338 | mod_name_len = 0; |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2339 | } |
| 2340 | |
Michal Vasko | f68a49e | 2017-08-14 13:23:37 +0200 | [diff] [blame] | 2341 | prev_mod = module; |
| 2342 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2343 | while (1) { |
| 2344 | sibling = NULL; |
Michal Vasko | f68a49e | 2017-08-14 13:23:37 +0200 | [diff] [blame] | 2345 | while ((sibling = lys_getnext(sibling, start_parent, module, 0))) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2346 | /* name match */ |
Michal Vasko | 0a1aaa4 | 2016-04-19 09:48:25 +0200 | [diff] [blame] | 2347 | if (sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len]) { |
Michal Vasko | b374440 | 2017-08-03 14:23:58 +0200 | [diff] [blame] | 2348 | /* output check */ |
| 2349 | for (parent = lys_parent(sibling); parent && !(parent->nodetype & (LYS_INPUT | LYS_OUTPUT)); parent = lys_parent(parent)); |
| 2350 | if (parent) { |
| 2351 | if (output && (parent->nodetype == LYS_INPUT)) { |
| 2352 | continue; |
| 2353 | } else if (!output && (parent->nodetype == LYS_OUTPUT)) { |
| 2354 | continue; |
| 2355 | } |
| 2356 | } |
| 2357 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2358 | /* module check */ |
| 2359 | if (mod_name) { |
Michal Vasko | 8757e7c | 2016-03-15 10:41:30 +0100 | [diff] [blame] | 2360 | /* will also find an augment module */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2361 | prefix_mod = ly_ctx_nget_module(ctx, mod_name, mod_name_len, NULL, 1); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2362 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2363 | if (!prefix_mod) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2364 | str = strndup(nodeid, (mod_name + mod_name_len) - nodeid); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2365 | LOGVAL(ctx, LYE_PATH_INMOD, LY_VLOG_STR, str); |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2366 | free(str); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2367 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2368 | } |
| 2369 | } else { |
Michal Vasko | f68a49e | 2017-08-14 13:23:37 +0200 | [diff] [blame] | 2370 | prefix_mod = prev_mod; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2371 | } |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 2372 | if (prefix_mod != lys_node_module(sibling)) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2373 | continue; |
| 2374 | } |
| 2375 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2376 | /* do we have some predicates on it? */ |
| 2377 | if (has_predicate) { |
| 2378 | r = 0; |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 2379 | if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2380 | if ((r = parse_schema_json_predicate(id, NULL, NULL, NULL, NULL, NULL, NULL, &has_predicate)) < 1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2381 | LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 2382 | return NULL; |
| 2383 | } |
| 2384 | } else if (sibling->nodetype == LYS_LIST) { |
Michal Vasko | f68a49e | 2017-08-14 13:23:37 +0200 | [diff] [blame] | 2385 | if (resolve_json_schema_list_predicate(id, (const struct lys_node_list *)sibling, &r)) { |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 2386 | return NULL; |
| 2387 | } |
| 2388 | } else { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2389 | LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2390 | return NULL; |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2391 | } |
| 2392 | id += r; |
| 2393 | } |
| 2394 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2395 | /* the result node? */ |
| 2396 | if (!id[0]) { |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2397 | return sibling; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2398 | } |
| 2399 | |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 2400 | /* move down the tree, if possible */ |
| 2401 | if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2402 | LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 2403 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2404 | } |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 2405 | start_parent = sibling; |
Michal Vasko | f68a49e | 2017-08-14 13:23:37 +0200 | [diff] [blame] | 2406 | |
| 2407 | /* update prev mod */ |
| 2408 | prev_mod = (start_parent->child ? lys_node_module(start_parent->child) : module); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2409 | break; |
| 2410 | } |
| 2411 | } |
| 2412 | |
| 2413 | /* no match */ |
| 2414 | if (!sibling) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2415 | str = strndup(nodeid, (name + nam_len) - nodeid); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2416 | LOGVAL(ctx, LYE_PATH_INNODE, LY_VLOG_STR, str); |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2417 | free(str); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2418 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2419 | } |
| 2420 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2421 | if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate, NULL, 0)) < 1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2422 | LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2423 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2424 | } |
| 2425 | id += r; |
| 2426 | } |
| 2427 | |
| 2428 | /* cannot get here */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2429 | LOGINT(ctx); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2430 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2431 | } |
| 2432 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2433 | static int |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2434 | resolve_partial_json_data_list_predicate(struct parsed_pred pp, struct lyd_node *node, int position) |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2435 | { |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2436 | uint16_t i; |
Michal Vasko | d51dd42 | 2019-04-23 11:51:32 +0200 | [diff] [blame] | 2437 | char *val_str; |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2438 | struct lyd_node_leaf_list *key; |
Michal Vasko | febd13d | 2018-05-17 10:42:24 +0200 | [diff] [blame] | 2439 | struct lys_node_list *slist; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2440 | struct ly_ctx *ctx; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2441 | |
Radek Krejci | 61a86c6 | 2016-03-24 11:06:44 +0100 | [diff] [blame] | 2442 | assert(node); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2443 | assert(node->schema->nodetype == LYS_LIST); |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2444 | assert(pp.len); |
Michal Vasko | febd13d | 2018-05-17 10:42:24 +0200 | [diff] [blame] | 2445 | |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2446 | ctx = node->schema->module->ctx; |
Michal Vasko | febd13d | 2018-05-17 10:42:24 +0200 | [diff] [blame] | 2447 | slist = (struct lys_node_list *)node->schema; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2448 | |
Michal Vasko | 53adfc7 | 2017-01-06 10:39:10 +0100 | [diff] [blame] | 2449 | /* is the predicate a number? */ |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2450 | if (isdigit(pp.pred[0].name[0])) { |
| 2451 | if (position == atoi(pp.pred[0].name)) { |
Michal Vasko | 53adfc7 | 2017-01-06 10:39:10 +0100 | [diff] [blame] | 2452 | /* match */ |
Michal Vasko | 53adfc7 | 2017-01-06 10:39:10 +0100 | [diff] [blame] | 2453 | return 0; |
| 2454 | } else { |
| 2455 | /* not a match */ |
| 2456 | return 1; |
| 2457 | } |
| 2458 | } |
| 2459 | |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2460 | key = (struct lyd_node_leaf_list *)node->child; |
Michal Vasko | 53adfc7 | 2017-01-06 10:39:10 +0100 | [diff] [blame] | 2461 | if (!key) { |
| 2462 | /* it is not a position, so we need a key for it to be a match */ |
| 2463 | return 1; |
| 2464 | } |
| 2465 | |
| 2466 | /* go through all the keys */ |
Michal Vasko | febd13d | 2018-05-17 10:42:24 +0200 | [diff] [blame] | 2467 | for (i = 0; i < slist->keys_size; ++i) { |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2468 | if (strncmp(key->schema->name, pp.pred[i].name, pp.pred[i].nam_len) || key->schema->name[pp.pred[i].nam_len]) { |
| 2469 | LOGVAL(ctx, LYE_PATH_INKEY, LY_VLOG_NONE, NULL, pp.pred[i].name); |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2470 | return -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2471 | } |
| 2472 | |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2473 | if (pp.pred[i].mod_name) { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2474 | /* specific module, check that the found key is from that module */ |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2475 | if (strncmp(lyd_node_module((struct lyd_node *)key)->name, pp.pred[i].mod_name, pp.pred[i].mod_name_len) |
| 2476 | || lyd_node_module((struct lyd_node *)key)->name[pp.pred[i].mod_name_len]) { |
| 2477 | LOGVAL(ctx, LYE_PATH_INKEY, LY_VLOG_NONE, NULL, pp.pred[i].name); |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2478 | return -1; |
| 2479 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2480 | |
| 2481 | /* but if the module is the same as the parent, it should have been omitted */ |
| 2482 | if (lyd_node_module((struct lyd_node *)key) == lyd_node_module(node)) { |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2483 | LOGVAL(ctx, LYE_PATH_INKEY, LY_VLOG_NONE, NULL, pp.pred[i].name); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2484 | return -1; |
| 2485 | } |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2486 | } else { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2487 | /* no module, so it must be the same as the list (parent) */ |
| 2488 | if (lyd_node_module((struct lyd_node *)key) != lyd_node_module(node)) { |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2489 | LOGVAL(ctx, LYE_PATH_INKEY, LY_VLOG_NONE, NULL, pp.pred[i].name); |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2490 | return -1; |
| 2491 | } |
| 2492 | } |
| 2493 | |
Michal Vasko | d51dd42 | 2019-04-23 11:51:32 +0200 | [diff] [blame] | 2494 | /* get canonical value */ |
| 2495 | val_str = lyd_make_canonical(key->schema, pp.pred[i].value, pp.pred[i].val_len); |
| 2496 | if (!val_str) { |
| 2497 | return -1; |
| 2498 | } |
| 2499 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2500 | /* value does not match */ |
Michal Vasko | d51dd42 | 2019-04-23 11:51:32 +0200 | [diff] [blame] | 2501 | if (strcmp(key->value_str, val_str)) { |
| 2502 | free(val_str); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2503 | return 1; |
| 2504 | } |
Michal Vasko | d51dd42 | 2019-04-23 11:51:32 +0200 | [diff] [blame] | 2505 | free(val_str); |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2506 | |
| 2507 | key = (struct lyd_node_leaf_list *)key->next; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2508 | } |
| 2509 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2510 | return 0; |
| 2511 | } |
| 2512 | |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 2513 | /** |
| 2514 | * @brief get the closest parent of the node (or the node itself) identified by the nodeid (path) |
| 2515 | * |
| 2516 | * @param[in] nodeid Node data path to find |
| 2517 | * @param[in] llist_value If the \p nodeid identifies leaf-list, this is expected value of the leaf-list instance. |
| 2518 | * @param[in] options Bitmask of options flags, see @ref pathoptions. |
| 2519 | * @param[out] parsed Number of characters processed in \p id |
| 2520 | * @return The closes parent (or the node itself) from the path |
| 2521 | */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2522 | struct lyd_node * |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2523 | resolve_partial_json_data_nodeid(const char *nodeid, const char *llist_value, struct lyd_node *start, int options, |
| 2524 | int *parsed) |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2525 | { |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2526 | const char *id, *mod_name, *name, *data_val, *llval; |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2527 | int r, ret, mod_name_len, nam_len, is_relative = -1, list_instance_position; |
Michal Vasko | febd13d | 2018-05-17 10:42:24 +0200 | [diff] [blame] | 2528 | int has_predicate, last_parsed = 0, llval_len; |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2529 | struct lyd_node *sibling, *last_match = NULL; |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2530 | struct lyd_node_leaf_list *llist; |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2531 | const struct lys_module *prev_mod; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2532 | struct ly_ctx *ctx; |
Michal Vasko | 2096478 | 2018-08-01 15:14:30 +0200 | [diff] [blame] | 2533 | const struct lys_node *ssibling, *sparent; |
Michal Vasko | 310bc58 | 2018-05-22 10:47:59 +0200 | [diff] [blame] | 2534 | struct lys_node_list *slist; |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2535 | struct parsed_pred pp; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2536 | |
| 2537 | assert(nodeid && start && parsed); |
| 2538 | |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2539 | memset(&pp, 0, sizeof pp); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2540 | ctx = start->schema->module->ctx; |
| 2541 | id = nodeid; |
| 2542 | |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2543 | /* parse first nodeid in case it is yang-data extension */ |
PavolVican | 195cf39 | 2018-02-23 13:24:45 +0100 | [diff] [blame] | 2544 | if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, NULL, NULL, 1)) < 1) { |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 2545 | LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | febd13d | 2018-05-17 10:42:24 +0200 | [diff] [blame] | 2546 | goto error; |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 2547 | } |
| 2548 | |
| 2549 | if (name[0] == '#') { |
| 2550 | if (is_relative) { |
| 2551 | LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, '#', name); |
Michal Vasko | febd13d | 2018-05-17 10:42:24 +0200 | [diff] [blame] | 2552 | goto error; |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 2553 | } |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 2554 | id += r; |
| 2555 | last_parsed = r; |
| 2556 | } else { |
| 2557 | is_relative = -1; |
| 2558 | } |
| 2559 | |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2560 | /* parse first nodeid */ |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2561 | if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate, NULL, 0)) < 1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2562 | LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | febd13d | 2018-05-17 10:42:24 +0200 | [diff] [blame] | 2563 | goto error; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2564 | } |
| 2565 | id += r; |
| 2566 | /* add it to parsed only after the data node was actually found */ |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 2567 | last_parsed += r; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2568 | |
| 2569 | if (is_relative) { |
Michal Vasko | f68a49e | 2017-08-14 13:23:37 +0200 | [diff] [blame] | 2570 | prev_mod = lyd_node_module(start); |
Michal Vasko | c93bc2d | 2019-02-08 10:37:46 +0100 | [diff] [blame] | 2571 | start = (start->schema->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_RPC | LYS_ACTION | LYS_NOTIF)) ? start->child : NULL; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2572 | } else { |
| 2573 | for (; start->parent; start = start->parent); |
Michal Vasko | f68a49e | 2017-08-14 13:23:37 +0200 | [diff] [blame] | 2574 | prev_mod = lyd_node_module(start); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2575 | } |
Michal Vasko | c93bc2d | 2019-02-08 10:37:46 +0100 | [diff] [blame] | 2576 | if (!start) { |
| 2577 | /* there are no siblings to search */ |
| 2578 | return NULL; |
| 2579 | } |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2580 | |
Michal Vasko | febd13d | 2018-05-17 10:42:24 +0200 | [diff] [blame] | 2581 | /* do not duplicate code, use predicate parsing from the loop */ |
| 2582 | goto parse_predicates; |
| 2583 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2584 | while (1) { |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2585 | /* find the correct schema node first */ |
| 2586 | ssibling = NULL; |
Michal Vasko | 2096478 | 2018-08-01 15:14:30 +0200 | [diff] [blame] | 2587 | sparent = (start && start->parent) ? start->parent->schema : NULL; |
| 2588 | while ((ssibling = lys_getnext(ssibling, sparent, prev_mod, 0))) { |
| 2589 | /* skip invalid input/output nodes */ |
| 2590 | if (sparent && (sparent->nodetype & (LYS_RPC | LYS_ACTION))) { |
| 2591 | if (options & LYD_PATH_OPT_OUTPUT) { |
| 2592 | if (lys_parent(ssibling)->nodetype == LYS_INPUT) { |
| 2593 | continue; |
| 2594 | } |
| 2595 | } else { |
| 2596 | if (lys_parent(ssibling)->nodetype == LYS_OUTPUT) { |
| 2597 | continue; |
| 2598 | } |
| 2599 | } |
| 2600 | } |
| 2601 | |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2602 | if (!schema_nodeid_siblingcheck(ssibling, prev_mod, mod_name, mod_name_len, name, nam_len)) { |
| 2603 | break; |
Michal Vasko | 2411b94 | 2016-03-23 13:50:03 +0100 | [diff] [blame] | 2604 | } |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2605 | } |
| 2606 | if (!ssibling) { |
| 2607 | /* there is not even such a schema node */ |
| 2608 | free(pp.pred); |
| 2609 | return last_match; |
| 2610 | } |
| 2611 | pp.schema = ssibling; |
Michal Vasko | 2411b94 | 2016-03-23 13:50:03 +0100 | [diff] [blame] | 2612 | |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2613 | /* unify leaf-list value - it is possible to specify last-node value as both a predicate or parameter if |
| 2614 | * is a leaf-list, unify both cases and the value will in both cases be in the predicate structure */ |
| 2615 | if (!id[0] && !pp.len && (ssibling->nodetype == LYS_LEAFLIST)) { |
| 2616 | pp.len = 1; |
| 2617 | pp.pred = calloc(1, sizeof *pp.pred); |
| 2618 | LY_CHECK_ERR_GOTO(!pp.pred, LOGMEM(ctx), error); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2619 | |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2620 | pp.pred[0].name = "."; |
| 2621 | pp.pred[0].nam_len = 1; |
| 2622 | pp.pred[0].value = (llist_value ? llist_value : ""); |
| 2623 | pp.pred[0].val_len = strlen(pp.pred[0].value); |
| 2624 | } |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2625 | |
Michal Vasko | 310bc58 | 2018-05-22 10:47:59 +0200 | [diff] [blame] | 2626 | if (ssibling->nodetype & (LYS_LEAFLIST | LYS_LEAF)) { |
| 2627 | /* check leaf/leaf-list predicate */ |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2628 | if (pp.len > 1) { |
| 2629 | LOGVAL(ctx, LYE_PATH_PREDTOOMANY, LY_VLOG_NONE, NULL); |
| 2630 | goto error; |
Michal Vasko | 310bc58 | 2018-05-22 10:47:59 +0200 | [diff] [blame] | 2631 | } else if (pp.len) { |
| 2632 | if ((pp.pred[0].name[0] != '.') || (pp.pred[0].nam_len != 1)) { |
| 2633 | LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, pp.pred[0].name[0], pp.pred[0].name); |
| 2634 | goto error; |
| 2635 | } |
| 2636 | if ((((struct lys_node_leaf *)ssibling)->type.base == LY_TYPE_IDENT) && !strnchr(pp.pred[0].value, ':', pp.pred[0].val_len)) { |
| 2637 | LOGVAL(ctx, LYE_PATH_INIDENTREF, LY_VLOG_LYS, ssibling, pp.pred[0].val_len, pp.pred[0].value); |
| 2638 | goto error; |
| 2639 | } |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2640 | } |
| 2641 | } else if (ssibling->nodetype == LYS_LIST) { |
Michal Vasko | 310bc58 | 2018-05-22 10:47:59 +0200 | [diff] [blame] | 2642 | /* list should have predicates for all the keys or position */ |
| 2643 | slist = (struct lys_node_list *)ssibling; |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2644 | if (!pp.len) { |
| 2645 | /* none match */ |
| 2646 | return last_match; |
Michal Vasko | 310bc58 | 2018-05-22 10:47:59 +0200 | [diff] [blame] | 2647 | } else if (!isdigit(pp.pred[0].name[0])) { |
| 2648 | /* list predicate is not a position, so there must be all the keys */ |
| 2649 | if (pp.len > slist->keys_size) { |
| 2650 | LOGVAL(ctx, LYE_PATH_PREDTOOMANY, LY_VLOG_NONE, NULL); |
| 2651 | goto error; |
| 2652 | } else if (pp.len < slist->keys_size) { |
| 2653 | LOGVAL(ctx, LYE_PATH_MISSKEY, LY_VLOG_NONE, NULL, slist->keys[pp.len]->name); |
| 2654 | goto error; |
| 2655 | } |
| 2656 | /* check that all identityrefs have module name, otherwise the hash of the list instance will never match!! */ |
| 2657 | for (r = 0; r < pp.len; ++r) { |
| 2658 | if ((slist->keys[r]->type.base == LY_TYPE_IDENT) && !strnchr(pp.pred[r].value, ':', pp.pred[r].val_len)) { |
| 2659 | LOGVAL(ctx, LYE_PATH_INIDENTREF, LY_VLOG_LYS, slist->keys[r], pp.pred[r].val_len, pp.pred[r].value); |
| 2660 | goto error; |
| 2661 | } |
| 2662 | } |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2663 | } |
| 2664 | } else if (pp.pred) { |
| 2665 | /* no other nodes allow predicates */ |
| 2666 | LOGVAL(ctx, LYE_PATH_PREDTOOMANY, LY_VLOG_NONE, NULL); |
| 2667 | goto error; |
| 2668 | } |
| 2669 | |
Michal Vasko | c340b3d | 2020-06-29 10:47:11 +0200 | [diff] [blame] | 2670 | /* we will not be matching list position, keyless lists, or state leaf-lists this way */ |
| 2671 | if (((pp.schema->nodetype != LYS_LIST) || (((struct lys_node_list *)pp.schema)->keys_size && !isdigit(pp.pred[0].name[0]))) |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2672 | && ((pp.schema->nodetype != LYS_LEAFLIST) || (pp.schema->flags & LYS_CONFIG_W))) { |
Michal Vasko | c340b3d | 2020-06-29 10:47:11 +0200 | [diff] [blame] | 2673 | sibling = resolve_json_data_node_hash(start, pp); |
| 2674 | } else { |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2675 | list_instance_position = 0; |
| 2676 | LY_TREE_FOR(start, sibling) { |
| 2677 | /* RPC/action data check, return simply invalid argument, because the data tree is invalid */ |
| 2678 | if (lys_parent(sibling->schema)) { |
| 2679 | if (options & LYD_PATH_OPT_OUTPUT) { |
| 2680 | if (lys_parent(sibling->schema)->nodetype == LYS_INPUT) { |
| 2681 | LOGERR(ctx, LY_EINVAL, "Provided data tree includes some RPC input nodes (%s).", sibling->schema->name); |
| 2682 | goto error; |
| 2683 | } |
| 2684 | } else { |
| 2685 | if (lys_parent(sibling->schema)->nodetype == LYS_OUTPUT) { |
| 2686 | LOGERR(ctx, LY_EINVAL, "Provided data tree includes some RPC output nodes (%s).", sibling->schema->name); |
| 2687 | goto error; |
| 2688 | } |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2689 | } |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2690 | } |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2691 | |
| 2692 | if (sibling->schema != ssibling) { |
| 2693 | /* wrong schema node */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2694 | continue; |
| 2695 | } |
| 2696 | |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2697 | /* leaf-list, did we find it with the correct value or not? */ |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2698 | if (ssibling->nodetype == LYS_LEAFLIST) { |
| 2699 | if (ssibling->flags & LYS_CONFIG_R) { |
Michal Vasko | 24affa0 | 2018-04-03 09:06:06 +0200 | [diff] [blame] | 2700 | /* state leaf-lists will never match */ |
| 2701 | continue; |
| 2702 | } |
| 2703 | |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2704 | llist = (struct lyd_node_leaf_list *)sibling; |
| 2705 | |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2706 | /* get the expected leaf-list value */ |
| 2707 | llval = NULL; |
| 2708 | llval_len = 0; |
| 2709 | if (pp.pred) { |
| 2710 | /* it was already checked that it is correct */ |
| 2711 | llval = pp.pred[0].value; |
| 2712 | llval_len = pp.pred[0].val_len; |
Michal Vasko | febd13d | 2018-05-17 10:42:24 +0200 | [diff] [blame] | 2713 | |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2714 | } |
| 2715 | |
Michal Vasko | 21b90ce | 2017-09-19 09:38:27 +0200 | [diff] [blame] | 2716 | /* make value canonical (remove module name prefix) unless it was specified with it */ |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2717 | if (llval && !strchr(llval, ':') && (llist->value_type & LY_TYPE_IDENT) |
Michal Vasko | 6a938d6 | 2016-12-21 09:21:30 +0100 | [diff] [blame] | 2718 | && !strncmp(llist->value_str, lyd_node_module(sibling)->name, strlen(lyd_node_module(sibling)->name)) |
| 2719 | && (llist->value_str[strlen(lyd_node_module(sibling)->name)] == ':')) { |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2720 | data_val = llist->value_str + strlen(lyd_node_module(sibling)->name) + 1; |
| 2721 | } else { |
| 2722 | data_val = llist->value_str; |
| 2723 | } |
| 2724 | |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2725 | if ((!llval && data_val && data_val[0]) || (llval && (strncmp(llval, data_val, llval_len) |
| 2726 | || data_val[llval_len]))) { |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2727 | continue; |
| 2728 | } |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2729 | |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2730 | } else if (ssibling->nodetype == LYS_LIST) { |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2731 | /* list, we likely need predicates'n'stuff then, but if without a predicate, we are always creating it */ |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2732 | ++list_instance_position; |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2733 | ret = resolve_partial_json_data_list_predicate(pp, sibling, list_instance_position); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2734 | if (ret == -1) { |
Michal Vasko | febd13d | 2018-05-17 10:42:24 +0200 | [diff] [blame] | 2735 | goto error; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2736 | } else if (ret == 1) { |
| 2737 | /* this list instance does not match */ |
| 2738 | continue; |
| 2739 | } |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2740 | } |
| 2741 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2742 | break; |
| 2743 | } |
| 2744 | } |
| 2745 | |
| 2746 | /* no match, return last match */ |
| 2747 | if (!sibling) { |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2748 | free(pp.pred); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2749 | return last_match; |
| 2750 | } |
| 2751 | |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2752 | /* we found a next matching node */ |
| 2753 | *parsed += last_parsed; |
Michal Vasko | 586831d | 2018-05-22 11:13:16 +0200 | [diff] [blame] | 2754 | last_match = sibling; |
| 2755 | prev_mod = lyd_node_module(sibling); |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2756 | |
| 2757 | /* the result node? */ |
| 2758 | if (!id[0]) { |
| 2759 | free(pp.pred); |
Michal Vasko | 586831d | 2018-05-22 11:13:16 +0200 | [diff] [blame] | 2760 | return last_match; |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2761 | } |
| 2762 | |
Michal Vasko | 586831d | 2018-05-22 11:13:16 +0200 | [diff] [blame] | 2763 | /* move down the tree, if possible, and continue */ |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2764 | if (ssibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
Michal Vasko | 586831d | 2018-05-22 11:13:16 +0200 | [diff] [blame] | 2765 | /* there can be no children even through expected, error */ |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2766 | LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
| 2767 | goto error; |
Michal Vasko | 586831d | 2018-05-22 11:13:16 +0200 | [diff] [blame] | 2768 | } else if (!sibling->child) { |
| 2769 | /* there could be some children, but are not, return what we found so far */ |
| 2770 | free(pp.pred); |
| 2771 | return last_match; |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2772 | } |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2773 | start = sibling->child; |
| 2774 | |
Michal Vasko | febd13d | 2018-05-17 10:42:24 +0200 | [diff] [blame] | 2775 | /* parse nodeid */ |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2776 | if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate, NULL, 0)) < 1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2777 | LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | febd13d | 2018-05-17 10:42:24 +0200 | [diff] [blame] | 2778 | goto error; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2779 | } |
| 2780 | id += r; |
| 2781 | last_parsed = r; |
Michal Vasko | febd13d | 2018-05-17 10:42:24 +0200 | [diff] [blame] | 2782 | |
| 2783 | parse_predicates: |
| 2784 | /* parse all the predicates */ |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2785 | free(pp.pred); |
| 2786 | pp.schema = NULL; |
Michal Vasko | c340b3d | 2020-06-29 10:47:11 +0200 | [diff] [blame] | 2787 | pp.pred_str = NULL; |
| 2788 | pp.pred_str_len = 0; |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2789 | pp.len = 0; |
| 2790 | pp.pred = NULL; |
Michal Vasko | febd13d | 2018-05-17 10:42:24 +0200 | [diff] [blame] | 2791 | while (has_predicate) { |
Michal Vasko | c340b3d | 2020-06-29 10:47:11 +0200 | [diff] [blame] | 2792 | if (!pp.pred_str) { |
| 2793 | pp.pred_str = id; |
| 2794 | } |
| 2795 | |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2796 | ++pp.len; |
| 2797 | pp.pred = ly_realloc(pp.pred, pp.len * sizeof *pp.pred); |
| 2798 | LY_CHECK_ERR_GOTO(!pp.pred, LOGMEM(ctx), error); |
| 2799 | if ((r = parse_schema_json_predicate(id, &pp.pred[pp.len - 1].mod_name, &pp.pred[pp.len - 1].mod_name_len, |
| 2800 | &pp.pred[pp.len - 1].name, &pp.pred[pp.len - 1].nam_len, &pp.pred[pp.len - 1].value, |
| 2801 | &pp.pred[pp.len - 1].val_len, &has_predicate)) < 1) { |
Michal Vasko | febd13d | 2018-05-17 10:42:24 +0200 | [diff] [blame] | 2802 | LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
| 2803 | goto error; |
| 2804 | } |
| 2805 | |
| 2806 | id += r; |
| 2807 | last_parsed += r; |
Michal Vasko | c340b3d | 2020-06-29 10:47:11 +0200 | [diff] [blame] | 2808 | pp.pred_str_len += r; |
Michal Vasko | febd13d | 2018-05-17 10:42:24 +0200 | [diff] [blame] | 2809 | } |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2810 | } |
| 2811 | |
Michal Vasko | febd13d | 2018-05-17 10:42:24 +0200 | [diff] [blame] | 2812 | error: |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2813 | *parsed = -1; |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2814 | free(pp.pred); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2815 | return NULL; |
| 2816 | } |
| 2817 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2818 | /** |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2819 | * @brief Resolves length or range intervals. Does not log. |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2820 | * Syntax is assumed to be correct, *ret MUST be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2821 | * |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2822 | * @param[in] ctx Context for errors. |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2823 | * @param[in] str_restr Restriction as a string. |
| 2824 | * @param[in] type Type of the restriction. |
| 2825 | * @param[out] ret Final interval structure that starts with |
| 2826 | * the interval of the initial type, continues with intervals |
| 2827 | * of any superior types derived from the initial one, and |
| 2828 | * finishes with intervals from our \p type. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2829 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2830 | * @return EXIT_SUCCESS on succes, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2831 | */ |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2832 | int |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2833 | resolve_len_ran_interval(struct ly_ctx *ctx, const char *str_restr, struct lys_type *type, struct len_ran_intv **ret) |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2834 | { |
| 2835 | /* 0 - unsigned, 1 - signed, 2 - floating point */ |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2836 | int kind; |
Michal Vasko | f75b277 | 2018-03-14 09:55:33 +0100 | [diff] [blame] | 2837 | int64_t local_smin = 0, local_smax = 0, local_fmin, local_fmax; |
| 2838 | uint64_t local_umin, local_umax = 0; |
| 2839 | uint8_t local_fdig = 0; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2840 | const char *seg_ptr, *ptr; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2841 | struct len_ran_intv *local_intv = NULL, *tmp_local_intv = NULL, *tmp_intv, *intv = NULL; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2842 | |
| 2843 | switch (type->base) { |
| 2844 | case LY_TYPE_BINARY: |
| 2845 | kind = 0; |
| 2846 | local_umin = 0; |
| 2847 | local_umax = 18446744073709551615UL; |
| 2848 | |
| 2849 | if (!str_restr && type->info.binary.length) { |
| 2850 | str_restr = type->info.binary.length->expr; |
| 2851 | } |
| 2852 | break; |
| 2853 | case LY_TYPE_DEC64: |
| 2854 | kind = 2; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2855 | local_fmin = __INT64_C(-9223372036854775807) - __INT64_C(1); |
| 2856 | local_fmax = __INT64_C(9223372036854775807); |
| 2857 | local_fdig = type->info.dec64.dig; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2858 | |
| 2859 | if (!str_restr && type->info.dec64.range) { |
| 2860 | str_restr = type->info.dec64.range->expr; |
| 2861 | } |
| 2862 | break; |
| 2863 | case LY_TYPE_INT8: |
| 2864 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2865 | local_smin = __INT64_C(-128); |
| 2866 | local_smax = __INT64_C(127); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2867 | |
| 2868 | if (!str_restr && type->info.num.range) { |
| 2869 | str_restr = type->info.num.range->expr; |
| 2870 | } |
| 2871 | break; |
| 2872 | case LY_TYPE_INT16: |
| 2873 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2874 | local_smin = __INT64_C(-32768); |
| 2875 | local_smax = __INT64_C(32767); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2876 | |
| 2877 | if (!str_restr && type->info.num.range) { |
| 2878 | str_restr = type->info.num.range->expr; |
| 2879 | } |
| 2880 | break; |
| 2881 | case LY_TYPE_INT32: |
| 2882 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2883 | local_smin = __INT64_C(-2147483648); |
| 2884 | local_smax = __INT64_C(2147483647); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2885 | |
| 2886 | if (!str_restr && type->info.num.range) { |
| 2887 | str_restr = type->info.num.range->expr; |
| 2888 | } |
| 2889 | break; |
| 2890 | case LY_TYPE_INT64: |
| 2891 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2892 | local_smin = __INT64_C(-9223372036854775807) - __INT64_C(1); |
| 2893 | local_smax = __INT64_C(9223372036854775807); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2894 | |
| 2895 | if (!str_restr && type->info.num.range) { |
| 2896 | str_restr = type->info.num.range->expr; |
| 2897 | } |
| 2898 | break; |
| 2899 | case LY_TYPE_UINT8: |
| 2900 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2901 | local_umin = __UINT64_C(0); |
| 2902 | local_umax = __UINT64_C(255); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2903 | |
| 2904 | if (!str_restr && type->info.num.range) { |
| 2905 | str_restr = type->info.num.range->expr; |
| 2906 | } |
| 2907 | break; |
| 2908 | case LY_TYPE_UINT16: |
| 2909 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2910 | local_umin = __UINT64_C(0); |
| 2911 | local_umax = __UINT64_C(65535); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2912 | |
| 2913 | if (!str_restr && type->info.num.range) { |
| 2914 | str_restr = type->info.num.range->expr; |
| 2915 | } |
| 2916 | break; |
| 2917 | case LY_TYPE_UINT32: |
| 2918 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2919 | local_umin = __UINT64_C(0); |
| 2920 | local_umax = __UINT64_C(4294967295); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2921 | |
| 2922 | if (!str_restr && type->info.num.range) { |
| 2923 | str_restr = type->info.num.range->expr; |
| 2924 | } |
| 2925 | break; |
| 2926 | case LY_TYPE_UINT64: |
| 2927 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2928 | local_umin = __UINT64_C(0); |
| 2929 | local_umax = __UINT64_C(18446744073709551615); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2930 | |
| 2931 | if (!str_restr && type->info.num.range) { |
| 2932 | str_restr = type->info.num.range->expr; |
| 2933 | } |
| 2934 | break; |
| 2935 | case LY_TYPE_STRING: |
| 2936 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2937 | local_umin = __UINT64_C(0); |
| 2938 | local_umax = __UINT64_C(18446744073709551615); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2939 | |
| 2940 | if (!str_restr && type->info.str.length) { |
| 2941 | str_restr = type->info.str.length->expr; |
| 2942 | } |
| 2943 | break; |
| 2944 | default: |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2945 | return -1; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2946 | } |
| 2947 | |
| 2948 | /* process superior types */ |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2949 | if (type->der) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2950 | if (resolve_len_ran_interval(ctx, NULL, &type->der->type, &intv)) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2951 | return -1; |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2952 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2953 | assert(!intv || (intv->kind == kind)); |
| 2954 | } |
| 2955 | |
| 2956 | if (!str_restr) { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2957 | /* we do not have any restriction, return superior ones */ |
| 2958 | *ret = intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2959 | return EXIT_SUCCESS; |
| 2960 | } |
| 2961 | |
| 2962 | /* adjust local min and max */ |
| 2963 | if (intv) { |
| 2964 | tmp_intv = intv; |
| 2965 | |
| 2966 | if (kind == 0) { |
| 2967 | local_umin = tmp_intv->value.uval.min; |
| 2968 | } else if (kind == 1) { |
| 2969 | local_smin = tmp_intv->value.sval.min; |
| 2970 | } else if (kind == 2) { |
| 2971 | local_fmin = tmp_intv->value.fval.min; |
| 2972 | } |
| 2973 | |
| 2974 | while (tmp_intv->next) { |
| 2975 | tmp_intv = tmp_intv->next; |
| 2976 | } |
| 2977 | |
| 2978 | if (kind == 0) { |
| 2979 | local_umax = tmp_intv->value.uval.max; |
| 2980 | } else if (kind == 1) { |
| 2981 | local_smax = tmp_intv->value.sval.max; |
| 2982 | } else if (kind == 2) { |
| 2983 | local_fmax = tmp_intv->value.fval.max; |
| 2984 | } |
| 2985 | } |
| 2986 | |
| 2987 | /* finally parse our restriction */ |
| 2988 | seg_ptr = str_restr; |
Pavol Vican | 9e7c01d | 2016-08-29 09:36:17 +0200 | [diff] [blame] | 2989 | tmp_intv = NULL; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2990 | while (1) { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2991 | if (!tmp_local_intv) { |
| 2992 | assert(!local_intv); |
| 2993 | local_intv = malloc(sizeof *local_intv); |
| 2994 | tmp_local_intv = local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2995 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2996 | tmp_local_intv->next = malloc(sizeof *tmp_local_intv); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2997 | tmp_local_intv = tmp_local_intv->next; |
| 2998 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2999 | LY_CHECK_ERR_GOTO(!tmp_local_intv, LOGMEM(ctx), error); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3000 | |
| 3001 | tmp_local_intv->kind = kind; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3002 | tmp_local_intv->type = type; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3003 | tmp_local_intv->next = NULL; |
| 3004 | |
| 3005 | /* min */ |
| 3006 | ptr = seg_ptr; |
| 3007 | while (isspace(ptr[0])) { |
| 3008 | ++ptr; |
| 3009 | } |
| 3010 | if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) { |
| 3011 | if (kind == 0) { |
Michal Vasko | 05d8313 | 2019-04-02 15:50:40 +0200 | [diff] [blame] | 3012 | tmp_local_intv->value.uval.min = strtoull(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3013 | } else if (kind == 1) { |
Radek Krejci | 2589441 | 2017-07-11 10:53:16 +0200 | [diff] [blame] | 3014 | tmp_local_intv->value.sval.min = strtoll(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3015 | } else if (kind == 2) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 3016 | if (parse_range_dec64(&ptr, local_fdig, &tmp_local_intv->value.fval.min)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3017 | LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, ptr, "range"); |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 3018 | goto error; |
| 3019 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3020 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3021 | } else if (!strncmp(ptr, "min", 3)) { |
| 3022 | if (kind == 0) { |
| 3023 | tmp_local_intv->value.uval.min = local_umin; |
| 3024 | } else if (kind == 1) { |
| 3025 | tmp_local_intv->value.sval.min = local_smin; |
| 3026 | } else if (kind == 2) { |
| 3027 | tmp_local_intv->value.fval.min = local_fmin; |
| 3028 | } |
| 3029 | |
| 3030 | ptr += 3; |
| 3031 | } else if (!strncmp(ptr, "max", 3)) { |
| 3032 | if (kind == 0) { |
| 3033 | tmp_local_intv->value.uval.min = local_umax; |
| 3034 | } else if (kind == 1) { |
| 3035 | tmp_local_intv->value.sval.min = local_smax; |
| 3036 | } else if (kind == 2) { |
| 3037 | tmp_local_intv->value.fval.min = local_fmax; |
| 3038 | } |
| 3039 | |
| 3040 | ptr += 3; |
| 3041 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3042 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3043 | } |
| 3044 | |
| 3045 | while (isspace(ptr[0])) { |
| 3046 | ptr++; |
| 3047 | } |
| 3048 | |
| 3049 | /* no interval or interval */ |
| 3050 | if ((ptr[0] == '|') || !ptr[0]) { |
| 3051 | if (kind == 0) { |
| 3052 | tmp_local_intv->value.uval.max = tmp_local_intv->value.uval.min; |
| 3053 | } else if (kind == 1) { |
| 3054 | tmp_local_intv->value.sval.max = tmp_local_intv->value.sval.min; |
| 3055 | } else if (kind == 2) { |
| 3056 | tmp_local_intv->value.fval.max = tmp_local_intv->value.fval.min; |
| 3057 | } |
| 3058 | } else if (!strncmp(ptr, "..", 2)) { |
| 3059 | /* skip ".." */ |
| 3060 | ptr += 2; |
| 3061 | while (isspace(ptr[0])) { |
| 3062 | ++ptr; |
| 3063 | } |
| 3064 | |
| 3065 | /* max */ |
| 3066 | if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) { |
| 3067 | if (kind == 0) { |
Michal Vasko | 05d8313 | 2019-04-02 15:50:40 +0200 | [diff] [blame] | 3068 | tmp_local_intv->value.uval.max = strtoull(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3069 | } else if (kind == 1) { |
Radek Krejci | 2589441 | 2017-07-11 10:53:16 +0200 | [diff] [blame] | 3070 | tmp_local_intv->value.sval.max = strtoll(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3071 | } else if (kind == 2) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 3072 | if (parse_range_dec64(&ptr, local_fdig, &tmp_local_intv->value.fval.max)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3073 | LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, ptr, "range"); |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 3074 | goto error; |
| 3075 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3076 | } |
| 3077 | } else if (!strncmp(ptr, "max", 3)) { |
| 3078 | if (kind == 0) { |
| 3079 | tmp_local_intv->value.uval.max = local_umax; |
| 3080 | } else if (kind == 1) { |
| 3081 | tmp_local_intv->value.sval.max = local_smax; |
| 3082 | } else if (kind == 2) { |
| 3083 | tmp_local_intv->value.fval.max = local_fmax; |
| 3084 | } |
| 3085 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3086 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3087 | } |
| 3088 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3089 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3090 | } |
| 3091 | |
Michal Vasko | 05d8313 | 2019-04-02 15:50:40 +0200 | [diff] [blame] | 3092 | /* check min and max in correct order */ |
Pavol Vican | 9e7c01d | 2016-08-29 09:36:17 +0200 | [diff] [blame] | 3093 | if (kind == 0) { |
| 3094 | /* current segment */ |
| 3095 | if (tmp_local_intv->value.uval.min > tmp_local_intv->value.uval.max) { |
| 3096 | goto error; |
| 3097 | } |
| 3098 | if (tmp_local_intv->value.uval.min < local_umin || tmp_local_intv->value.uval.max > local_umax) { |
| 3099 | goto error; |
| 3100 | } |
| 3101 | /* segments sholud be ascending order */ |
Pavol Vican | 69f62c9 | 2016-08-30 09:06:25 +0200 | [diff] [blame] | 3102 | if (tmp_intv && (tmp_intv->value.uval.max >= tmp_local_intv->value.uval.min)) { |
Pavol Vican | 9e7c01d | 2016-08-29 09:36:17 +0200 | [diff] [blame] | 3103 | goto error; |
| 3104 | } |
| 3105 | } else if (kind == 1) { |
| 3106 | if (tmp_local_intv->value.sval.min > tmp_local_intv->value.sval.max) { |
| 3107 | goto error; |
| 3108 | } |
| 3109 | if (tmp_local_intv->value.sval.min < local_smin || tmp_local_intv->value.sval.max > local_smax) { |
| 3110 | goto error; |
| 3111 | } |
Pavol Vican | 69f62c9 | 2016-08-30 09:06:25 +0200 | [diff] [blame] | 3112 | if (tmp_intv && (tmp_intv->value.sval.max >= tmp_local_intv->value.sval.min)) { |
Pavol Vican | 9e7c01d | 2016-08-29 09:36:17 +0200 | [diff] [blame] | 3113 | goto error; |
| 3114 | } |
| 3115 | } else if (kind == 2) { |
| 3116 | if (tmp_local_intv->value.fval.min > tmp_local_intv->value.fval.max) { |
| 3117 | goto error; |
| 3118 | } |
| 3119 | if (tmp_local_intv->value.fval.min < local_fmin || tmp_local_intv->value.fval.max > local_fmax) { |
| 3120 | goto error; |
| 3121 | } |
Pavol Vican | 69f62c9 | 2016-08-30 09:06:25 +0200 | [diff] [blame] | 3122 | if (tmp_intv && (tmp_intv->value.fval.max >= tmp_local_intv->value.fval.min)) { |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 3123 | /* fraction-digits value is always the same (it cannot be changed in derived types) */ |
Pavol Vican | 9e7c01d | 2016-08-29 09:36:17 +0200 | [diff] [blame] | 3124 | goto error; |
| 3125 | } |
| 3126 | } |
| 3127 | |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3128 | /* next segment (next OR) */ |
| 3129 | seg_ptr = strchr(seg_ptr, '|'); |
| 3130 | if (!seg_ptr) { |
| 3131 | break; |
| 3132 | } |
| 3133 | seg_ptr++; |
Pavol Vican | 9e7c01d | 2016-08-29 09:36:17 +0200 | [diff] [blame] | 3134 | tmp_intv = tmp_local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3135 | } |
| 3136 | |
| 3137 | /* check local restrictions against superior ones */ |
| 3138 | if (intv) { |
| 3139 | tmp_intv = intv; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3140 | tmp_local_intv = local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3141 | |
| 3142 | while (tmp_local_intv && tmp_intv) { |
| 3143 | /* reuse local variables */ |
| 3144 | if (kind == 0) { |
| 3145 | local_umin = tmp_local_intv->value.uval.min; |
| 3146 | local_umax = tmp_local_intv->value.uval.max; |
| 3147 | |
| 3148 | /* it must be in this interval */ |
| 3149 | if ((local_umin >= tmp_intv->value.uval.min) && (local_umin <= tmp_intv->value.uval.max)) { |
| 3150 | /* this interval is covered, next one */ |
| 3151 | if (local_umax <= tmp_intv->value.uval.max) { |
| 3152 | tmp_local_intv = tmp_local_intv->next; |
| 3153 | continue; |
| 3154 | /* ascending order of restrictions -> fail */ |
| 3155 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3156 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3157 | } |
| 3158 | } |
| 3159 | } else if (kind == 1) { |
| 3160 | local_smin = tmp_local_intv->value.sval.min; |
| 3161 | local_smax = tmp_local_intv->value.sval.max; |
| 3162 | |
| 3163 | if ((local_smin >= tmp_intv->value.sval.min) && (local_smin <= tmp_intv->value.sval.max)) { |
| 3164 | if (local_smax <= tmp_intv->value.sval.max) { |
| 3165 | tmp_local_intv = tmp_local_intv->next; |
| 3166 | continue; |
| 3167 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3168 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3169 | } |
| 3170 | } |
| 3171 | } else if (kind == 2) { |
| 3172 | local_fmin = tmp_local_intv->value.fval.min; |
| 3173 | local_fmax = tmp_local_intv->value.fval.max; |
| 3174 | |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 3175 | if ((dec64cmp(local_fmin, local_fdig, tmp_intv->value.fval.min, local_fdig) > -1) |
Pavol Vican | 3c8ee2b | 2016-09-29 13:18:13 +0200 | [diff] [blame] | 3176 | && (dec64cmp(local_fmin, local_fdig, tmp_intv->value.fval.max, local_fdig) < 1)) { |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 3177 | if (dec64cmp(local_fmax, local_fdig, tmp_intv->value.fval.max, local_fdig) < 1) { |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3178 | tmp_local_intv = tmp_local_intv->next; |
| 3179 | continue; |
| 3180 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3181 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3182 | } |
| 3183 | } |
| 3184 | } |
| 3185 | |
| 3186 | tmp_intv = tmp_intv->next; |
| 3187 | } |
| 3188 | |
| 3189 | /* some interval left uncovered -> fail */ |
| 3190 | if (tmp_local_intv) { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3191 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3192 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3193 | } |
| 3194 | |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3195 | /* append the local intervals to all the intervals of the superior types, return it all */ |
| 3196 | if (intv) { |
| 3197 | for (tmp_intv = intv; tmp_intv->next; tmp_intv = tmp_intv->next); |
| 3198 | tmp_intv->next = local_intv; |
| 3199 | } else { |
| 3200 | intv = local_intv; |
| 3201 | } |
| 3202 | *ret = intv; |
| 3203 | |
| 3204 | return EXIT_SUCCESS; |
| 3205 | |
| 3206 | error: |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3207 | while (intv) { |
| 3208 | tmp_intv = intv->next; |
| 3209 | free(intv); |
| 3210 | intv = tmp_intv; |
| 3211 | } |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3212 | while (local_intv) { |
| 3213 | tmp_local_intv = local_intv->next; |
| 3214 | free(local_intv); |
| 3215 | local_intv = tmp_local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3216 | } |
| 3217 | |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3218 | return -1; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3219 | } |
| 3220 | |
Michal Vasko | 4e610cc | 2019-03-08 13:21:27 +0100 | [diff] [blame] | 3221 | static int |
| 3222 | resolve_superior_type_check(struct lys_type *type) |
| 3223 | { |
| 3224 | uint32_t i; |
| 3225 | |
| 3226 | if (type->base == LY_TYPE_DER) { |
| 3227 | /* check that the referenced typedef is resolved */ |
| 3228 | return EXIT_FAILURE; |
| 3229 | } else if (type->base == LY_TYPE_UNION) { |
| 3230 | /* check that all union types are resolved */ |
| 3231 | for (i = 0; i < type->info.uni.count; ++i) { |
| 3232 | if (resolve_superior_type_check(&type->info.uni.types[i])) { |
| 3233 | return EXIT_FAILURE; |
| 3234 | } |
| 3235 | } |
| 3236 | } else if (type->base == LY_TYPE_LEAFREF) { |
| 3237 | /* check there is path in some derived type */ |
| 3238 | while (!type->info.lref.path) { |
| 3239 | assert(type->der); |
| 3240 | type = &type->der->type; |
| 3241 | } |
| 3242 | } |
| 3243 | |
| 3244 | return EXIT_SUCCESS; |
| 3245 | } |
| 3246 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3247 | /** |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 3248 | * @brief Resolve a typedef, return only resolved typedefs if derived. If leafref, it must be |
| 3249 | * resolved for this function to return it. Does not log. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3250 | * |
| 3251 | * @param[in] name Typedef name. |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3252 | * @param[in] mod_name Typedef name module name. |
| 3253 | * @param[in] module Main module. |
| 3254 | * @param[in] parent Parent of the resolved type definition. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3255 | * @param[out] ret Pointer to the resolved typedef. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3256 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3257 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3258 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3259 | int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3260 | resolve_superior_type(const char *name, const char *mod_name, const struct lys_module *module, |
| 3261 | const struct lys_node *parent, struct lys_tpdf **ret) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3262 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3263 | int i, j; |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 3264 | struct lys_tpdf *tpdf, *match; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3265 | int tpdf_size; |
| 3266 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3267 | if (!mod_name) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3268 | /* no prefix, try built-in types */ |
| 3269 | for (i = 1; i < LY_DATA_TYPE_COUNT; i++) { |
Radek Krejci | a68ddeb | 2017-02-24 12:49:44 +0100 | [diff] [blame] | 3270 | if (!strcmp(ly_types[i]->name, name)) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3271 | if (ret) { |
Radek Krejci | a68ddeb | 2017-02-24 12:49:44 +0100 | [diff] [blame] | 3272 | *ret = ly_types[i]; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3273 | } |
| 3274 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3275 | } |
| 3276 | } |
| 3277 | } else { |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3278 | if (!strcmp(mod_name, module->name)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3279 | /* prefix refers to the current module, ignore it */ |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3280 | mod_name = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3281 | } |
| 3282 | } |
| 3283 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3284 | if (!mod_name && parent) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3285 | /* search in local typedefs */ |
| 3286 | while (parent) { |
| 3287 | switch (parent->nodetype) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3288 | case LYS_CONTAINER: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 3289 | tpdf_size = ((struct lys_node_container *)parent)->tpdf_size; |
| 3290 | tpdf = ((struct lys_node_container *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3291 | break; |
| 3292 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3293 | case LYS_LIST: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 3294 | tpdf_size = ((struct lys_node_list *)parent)->tpdf_size; |
| 3295 | tpdf = ((struct lys_node_list *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3296 | break; |
| 3297 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3298 | case LYS_GROUPING: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 3299 | tpdf_size = ((struct lys_node_grp *)parent)->tpdf_size; |
| 3300 | tpdf = ((struct lys_node_grp *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3301 | break; |
| 3302 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3303 | case LYS_RPC: |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 3304 | case LYS_ACTION: |
| 3305 | tpdf_size = ((struct lys_node_rpc_action *)parent)->tpdf_size; |
| 3306 | tpdf = ((struct lys_node_rpc_action *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3307 | break; |
| 3308 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3309 | case LYS_NOTIF: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 3310 | tpdf_size = ((struct lys_node_notif *)parent)->tpdf_size; |
| 3311 | tpdf = ((struct lys_node_notif *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3312 | break; |
| 3313 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3314 | case LYS_INPUT: |
| 3315 | case LYS_OUTPUT: |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 3316 | tpdf_size = ((struct lys_node_inout *)parent)->tpdf_size; |
| 3317 | tpdf = ((struct lys_node_inout *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3318 | break; |
| 3319 | |
| 3320 | default: |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 3321 | parent = lys_parent(parent); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3322 | continue; |
| 3323 | } |
| 3324 | |
| 3325 | for (i = 0; i < tpdf_size; i++) { |
Michal Vasko | 4e610cc | 2019-03-08 13:21:27 +0100 | [diff] [blame] | 3326 | if (!strcmp(tpdf[i].name, name)) { |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 3327 | match = &tpdf[i]; |
Michal Vasko | 4e610cc | 2019-03-08 13:21:27 +0100 | [diff] [blame] | 3328 | goto check_typedef; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3329 | } |
| 3330 | } |
| 3331 | |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 3332 | parent = lys_parent(parent); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3333 | } |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 3334 | } else { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3335 | /* get module where to search */ |
Michal Vasko | 921eb6b | 2017-10-13 10:01:39 +0200 | [diff] [blame] | 3336 | module = lyp_get_module(module, NULL, 0, mod_name, 0, 0); |
Michal Vasko | c935fff | 2015-08-17 14:02:06 +0200 | [diff] [blame] | 3337 | if (!module) { |
| 3338 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3339 | } |
| 3340 | } |
| 3341 | |
| 3342 | /* search in top level typedefs */ |
| 3343 | for (i = 0; i < module->tpdf_size; i++) { |
Michal Vasko | 4e610cc | 2019-03-08 13:21:27 +0100 | [diff] [blame] | 3344 | if (!strcmp(module->tpdf[i].name, name)) { |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 3345 | match = &module->tpdf[i]; |
Michal Vasko | 4e610cc | 2019-03-08 13:21:27 +0100 | [diff] [blame] | 3346 | goto check_typedef; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3347 | } |
| 3348 | } |
| 3349 | |
| 3350 | /* search in submodules */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 3351 | for (i = 0; i < module->inc_size && module->inc[i].submodule; i++) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3352 | for (j = 0; j < module->inc[i].submodule->tpdf_size; j++) { |
Michal Vasko | 4e610cc | 2019-03-08 13:21:27 +0100 | [diff] [blame] | 3353 | if (!strcmp(module->inc[i].submodule->tpdf[j].name, name)) { |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 3354 | match = &module->inc[i].submodule->tpdf[j]; |
Michal Vasko | 4e610cc | 2019-03-08 13:21:27 +0100 | [diff] [blame] | 3355 | goto check_typedef; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3356 | } |
| 3357 | } |
| 3358 | } |
| 3359 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3360 | return EXIT_FAILURE; |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 3361 | |
Michal Vasko | 4e610cc | 2019-03-08 13:21:27 +0100 | [diff] [blame] | 3362 | check_typedef: |
| 3363 | if (resolve_superior_type_check(&match->type)) { |
| 3364 | return EXIT_FAILURE; |
| 3365 | } |
| 3366 | |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 3367 | if (ret) { |
| 3368 | *ret = match; |
| 3369 | } |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 3370 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3371 | } |
| 3372 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3373 | /** |
| 3374 | * @brief Check the default \p value of the \p type. Logs directly. |
| 3375 | * |
| 3376 | * @param[in] type Type definition to use. |
| 3377 | * @param[in] value Default value to check. |
Michal Vasko | 5682640 | 2016-03-02 11:11:37 +0100 | [diff] [blame] | 3378 | * @param[in] module Type module. |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3379 | * |
| 3380 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 3381 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3382 | static int |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 3383 | check_default(struct lys_type *type, const char **value, struct lys_module *module, int tpdf) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3384 | { |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3385 | struct lys_tpdf *base_tpdf = NULL; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3386 | struct lyd_node_leaf_list node; |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3387 | const char *dflt = NULL; |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 3388 | char *s; |
Michal Vasko | d1bf7c4 | 2018-02-15 08:38:49 +0100 | [diff] [blame] | 3389 | int ret = EXIT_SUCCESS, r; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3390 | struct ly_ctx *ctx = module->ctx; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3391 | |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3392 | assert(value); |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3393 | memset(&node, 0, sizeof node); |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3394 | |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 3395 | if (type->base <= LY_TYPE_DER) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3396 | /* the type was not resolved yet, nothing to do for now */ |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3397 | ret = EXIT_FAILURE; |
| 3398 | goto cleanup; |
Michal Vasko | aa1b0f3 | 2019-12-12 13:03:12 +0100 | [diff] [blame] | 3399 | } else if (!module->implemented && ((type->base == LY_TYPE_IDENT) || (type->base == LY_TYPE_INST))) { |
| 3400 | /* /instidsidentityrefs are checked when instantiated in data instead of typedef, |
| 3401 | * but the value has to be modified to include the prefix */ |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 3402 | if (*value) { |
| 3403 | if (strchr(*value, ':')) { |
| 3404 | dflt = transform_schema2json(module, *value); |
| 3405 | } else { |
| 3406 | /* default prefix of the module where the typedef is defined */ |
Michal Vasko | d1bf7c4 | 2018-02-15 08:38:49 +0100 | [diff] [blame] | 3407 | if (asprintf(&s, "%s:%s", lys_main_module(module)->name, *value) == -1) { |
| 3408 | LOGMEM(ctx); |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3409 | ret = -1; |
| 3410 | goto cleanup; |
Michal Vasko | d1bf7c4 | 2018-02-15 08:38:49 +0100 | [diff] [blame] | 3411 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3412 | dflt = lydict_insert_zc(ctx, s); |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 3413 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3414 | lydict_remove(ctx, *value); |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 3415 | *value = dflt; |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3416 | dflt = NULL; |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 3417 | } |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3418 | goto cleanup; |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 3419 | } else if (type->base == LY_TYPE_LEAFREF && tpdf) { |
| 3420 | /* leafref in typedef cannot be checked */ |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3421 | goto cleanup; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3422 | } |
| 3423 | |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3424 | dflt = lydict_insert(ctx, *value, 0); |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3425 | if (!dflt) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3426 | /* we do not have a new default value, so is there any to check even, in some base type? */ |
| 3427 | for (base_tpdf = type->der; base_tpdf->type.der; base_tpdf = base_tpdf->type.der) { |
| 3428 | if (base_tpdf->dflt) { |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3429 | dflt = lydict_insert(ctx, base_tpdf->dflt, 0); |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3430 | break; |
| 3431 | } |
| 3432 | } |
| 3433 | |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3434 | if (!dflt) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3435 | /* no default value, nothing to check, all is well */ |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3436 | goto cleanup; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3437 | } |
| 3438 | |
| 3439 | /* so there is a default value in a base type, but can the default value be no longer valid (did we define some new restrictions)? */ |
| 3440 | switch (type->base) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3441 | case LY_TYPE_IDENT: |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 3442 | if (lys_main_module(base_tpdf->type.parent->module)->implemented) { |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3443 | goto cleanup; |
Michal Vasko | 9301fdf | 2020-03-09 08:58:43 +0100 | [diff] [blame] | 3444 | } else if ((type->base == LY_TYPE_IDENT) || (type->base == LY_TYPE_INST)) { |
| 3445 | /* impossible to check with a non-implemented module */ |
| 3446 | goto cleanup; |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 3447 | } else { |
| 3448 | /* check the default value from typedef, but use also the typedef's module |
| 3449 | * due to possible searching in imported modules which is expected in |
| 3450 | * typedef's module instead of module where the typedef is used */ |
| 3451 | module = base_tpdf->module; |
| 3452 | } |
| 3453 | break; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3454 | case LY_TYPE_INST: |
| 3455 | case LY_TYPE_LEAFREF: |
| 3456 | case LY_TYPE_BOOL: |
| 3457 | case LY_TYPE_EMPTY: |
| 3458 | /* these have no restrictions, so we would do the exact same work as the unres in the base typedef */ |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3459 | goto cleanup; |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3460 | case LY_TYPE_BITS: |
| 3461 | /* the default value must match the restricted list of values, if the type was restricted */ |
| 3462 | if (type->info.bits.count) { |
| 3463 | break; |
| 3464 | } |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3465 | goto cleanup; |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3466 | case LY_TYPE_ENUM: |
| 3467 | /* the default value must match the restricted list of values, if the type was restricted */ |
| 3468 | if (type->info.enums.count) { |
| 3469 | break; |
| 3470 | } |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3471 | goto cleanup; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3472 | case LY_TYPE_DEC64: |
| 3473 | if (type->info.dec64.range) { |
| 3474 | break; |
| 3475 | } |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3476 | goto cleanup; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3477 | case LY_TYPE_BINARY: |
| 3478 | if (type->info.binary.length) { |
| 3479 | break; |
| 3480 | } |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3481 | goto cleanup; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3482 | case LY_TYPE_INT8: |
| 3483 | case LY_TYPE_INT16: |
| 3484 | case LY_TYPE_INT32: |
| 3485 | case LY_TYPE_INT64: |
| 3486 | case LY_TYPE_UINT8: |
| 3487 | case LY_TYPE_UINT16: |
| 3488 | case LY_TYPE_UINT32: |
| 3489 | case LY_TYPE_UINT64: |
| 3490 | if (type->info.num.range) { |
| 3491 | break; |
| 3492 | } |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3493 | goto cleanup; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3494 | case LY_TYPE_STRING: |
| 3495 | if (type->info.str.length || type->info.str.patterns) { |
| 3496 | break; |
| 3497 | } |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3498 | goto cleanup; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3499 | case LY_TYPE_UNION: |
| 3500 | /* way too much trouble learning whether we need to check the default again, so just do it */ |
| 3501 | break; |
| 3502 | default: |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3503 | LOGINT(ctx); |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3504 | ret = -1; |
| 3505 | goto cleanup; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3506 | } |
Radek Krejci | 55a161c | 2016-09-05 17:13:25 +0200 | [diff] [blame] | 3507 | } else if (type->base == LY_TYPE_EMPTY) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3508 | LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "default", type->parent->name); |
| 3509 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "The \"empty\" data type cannot have a default value."); |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3510 | ret = -1; |
| 3511 | goto cleanup; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3512 | } |
| 3513 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3514 | /* dummy leaf */ |
Radek Krejci | 4fe36bd | 2016-03-17 16:47:16 +0100 | [diff] [blame] | 3515 | memset(&node, 0, sizeof node); |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3516 | node.value_str = lydict_insert(ctx, dflt, 0); |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3517 | node.value_type = type->base; |
Michal Vasko | 31a2d32 | 2018-01-12 13:36:12 +0100 | [diff] [blame] | 3518 | |
| 3519 | if (tpdf) { |
| 3520 | node.schema = calloc(1, sizeof (struct lys_node_leaf)); |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3521 | if (!node.schema) { |
| 3522 | LOGMEM(ctx); |
| 3523 | ret = -1; |
| 3524 | goto cleanup; |
| 3525 | } |
Michal Vasko | d1bf7c4 | 2018-02-15 08:38:49 +0100 | [diff] [blame] | 3526 | r = asprintf((char **)&node.schema->name, "typedef-%s-default", ((struct lys_tpdf *)type->parent)->name); |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3527 | if (r == -1) { |
| 3528 | LOGMEM(ctx); |
| 3529 | ret = -1; |
| 3530 | goto cleanup; |
| 3531 | } |
Michal Vasko | 31a2d32 | 2018-01-12 13:36:12 +0100 | [diff] [blame] | 3532 | node.schema->module = module; |
| 3533 | memcpy(&((struct lys_node_leaf *)node.schema)->type, type, sizeof *type); |
| 3534 | } else { |
| 3535 | node.schema = (struct lys_node *)type->parent; |
| 3536 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3537 | |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 3538 | if (type->base == LY_TYPE_LEAFREF) { |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3539 | if (!type->info.lref.target) { |
| 3540 | ret = EXIT_FAILURE; |
Michal Vasko | 00f28a7 | 2018-11-12 11:55:36 +0100 | [diff] [blame] | 3541 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Default value \"%s\" cannot be checked in an unresolved leafref.", |
| 3542 | dflt); |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3543 | goto cleanup; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3544 | } |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 3545 | ret = check_default(&type->info.lref.target->type, &dflt, module, 0); |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3546 | if (!ret) { |
| 3547 | /* adopt possibly changed default value to its canonical form */ |
| 3548 | if (*value) { |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3549 | lydict_remove(ctx, *value); |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3550 | *value = dflt; |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3551 | dflt = NULL; |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3552 | } |
| 3553 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3554 | } else { |
Michal Vasko | 0abb1bc | 2020-02-25 15:47:10 +0100 | [diff] [blame] | 3555 | if (!lyp_parse_value(type, &node.value_str, NULL, &node, NULL, module, 1, 1)) { |
Radek Krejci | 5dca593 | 2016-11-04 14:30:47 +0100 | [diff] [blame] | 3556 | /* possible forward reference */ |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3557 | ret = EXIT_FAILURE; |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3558 | if (base_tpdf) { |
Radek Krejci | 9ad23f4 | 2016-10-31 15:46:52 +0100 | [diff] [blame] | 3559 | /* default value is defined in some base typedef */ |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3560 | if ((type->base == LY_TYPE_BITS && type->der->type.der) || |
| 3561 | (type->base == LY_TYPE_ENUM && type->der->type.der)) { |
| 3562 | /* we have refined bits/enums */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3563 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3564 | "Invalid value \"%s\" of the default statement inherited to \"%s\" from \"%s\" base type.", |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3565 | dflt, type->parent->name, base_tpdf->name); |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3566 | } |
| 3567 | } |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3568 | } else { |
| 3569 | /* success - adopt canonical form from the node into the default value */ |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3570 | if (!ly_strequal(dflt, node.value_str, 1)) { |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3571 | /* this can happen only if we have non-inherited default value, |
| 3572 | * inherited default values are already in canonical form */ |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3573 | assert(ly_strequal(dflt, *value, 1)); |
| 3574 | |
| 3575 | lydict_remove(ctx, *value); |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3576 | *value = node.value_str; |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3577 | node.value_str = NULL; |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3578 | } |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 3579 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3580 | } |
| 3581 | |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3582 | cleanup: |
Michal Vasko | d3dd15d | 2019-01-23 14:06:58 +0100 | [diff] [blame] | 3583 | lyd_free_value(node.value, node.value_type, node.value_flags, type, node.value_str, NULL, NULL, NULL); |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3584 | lydict_remove(ctx, node.value_str); |
| 3585 | if (tpdf && node.schema) { |
Michal Vasko | 31a2d32 | 2018-01-12 13:36:12 +0100 | [diff] [blame] | 3586 | free((char *)node.schema->name); |
| 3587 | free(node.schema); |
| 3588 | } |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3589 | lydict_remove(ctx, dflt); |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3590 | |
| 3591 | return ret; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3592 | } |
| 3593 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3594 | /** |
| 3595 | * @brief Check a key for mandatory attributes. Logs directly. |
| 3596 | * |
| 3597 | * @param[in] key The key to check. |
| 3598 | * @param[in] flags What flags to check. |
| 3599 | * @param[in] list The list of all the keys. |
| 3600 | * @param[in] index Index of the key in the key list. |
| 3601 | * @param[in] name The name of the keys. |
| 3602 | * @param[in] len The name length. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3603 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3604 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3605 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3606 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3607 | check_key(struct lys_node_list *list, int index, const char *name, int len) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3608 | { |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3609 | struct lys_node_leaf *key = list->keys[index]; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3610 | char *dup = NULL; |
| 3611 | int j; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3612 | struct ly_ctx *ctx = list->module->ctx; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3613 | |
| 3614 | /* existence */ |
| 3615 | if (!key) { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3616 | if (name[len] != '\0') { |
| 3617 | dup = strdup(name); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3618 | LY_CHECK_ERR_RETURN(!dup, LOGMEM(ctx), -1); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3619 | dup[len] = '\0'; |
| 3620 | name = dup; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3621 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3622 | LOGVAL(ctx, LYE_KEY_MISS, LY_VLOG_LYS, list, name); |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 3623 | free(dup); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3624 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3625 | } |
| 3626 | |
| 3627 | /* uniqueness */ |
| 3628 | for (j = index - 1; j >= 0; j--) { |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3629 | if (key == list->keys[j]) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3630 | LOGVAL(ctx, LYE_KEY_DUP, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3631 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3632 | } |
| 3633 | } |
| 3634 | |
| 3635 | /* key is a leaf */ |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3636 | if (key->nodetype != LYS_LEAF) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3637 | LOGVAL(ctx, LYE_KEY_NLEAF, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3638 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3639 | } |
| 3640 | |
| 3641 | /* type of the leaf is not built-in empty */ |
Radek Krejci | 13fde92 | 2018-05-16 10:45:58 +0200 | [diff] [blame] | 3642 | if (key->type.base == LY_TYPE_EMPTY && key->module->version < LYS_VERSION_1_1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3643 | LOGVAL(ctx, LYE_KEY_TYPE, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3644 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3645 | } |
| 3646 | |
| 3647 | /* config attribute is the same as of the list */ |
Michal Vasko | 627016e | 2018-10-24 09:25:55 +0200 | [diff] [blame] | 3648 | if ((key->flags & LYS_CONFIG_MASK) && (list->flags & LYS_CONFIG_MASK) |
| 3649 | && ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK))) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3650 | LOGVAL(ctx, LYE_KEY_CONFIG, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3651 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3652 | } |
| 3653 | |
Radek Krejci | 55e2cdc | 2016-03-11 13:51:09 +0100 | [diff] [blame] | 3654 | /* key is not placed from augment */ |
| 3655 | if (key->parent->nodetype == LYS_AUGMENT) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3656 | LOGVAL(ctx, LYE_KEY_MISS, LY_VLOG_LYS, key, key->name); |
| 3657 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Key inserted from augment."); |
Radek Krejci | 55e2cdc | 2016-03-11 13:51:09 +0100 | [diff] [blame] | 3658 | return -1; |
| 3659 | } |
| 3660 | |
Radek Krejci | 404acb6 | 2019-09-19 14:33:56 +0200 | [diff] [blame] | 3661 | /* key is not when/if-feature -conditional |
| 3662 | * note that j is really assigned in condition to distinguish when and if-feature, it is not supposed to be a comparison */ |
Radek Krejci | 3f21ada | 2016-08-01 13:34:31 +0200 | [diff] [blame] | 3663 | j = 0; |
| 3664 | if (key->when || (key->iffeature_size && (j = 1))) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3665 | LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, key, j ? "if-feature" : "when", "leaf"); |
| 3666 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Key definition cannot depend on a \"%s\" condition.", |
Radek Krejci | 3f21ada | 2016-08-01 13:34:31 +0200 | [diff] [blame] | 3667 | j ? "if-feature" : "when"); |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3668 | return -1; |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3669 | } |
| 3670 | |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3671 | return EXIT_SUCCESS; |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3672 | } |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3673 | |
| 3674 | /** |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3675 | * @brief Resolve (test the target exists) unique. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3676 | * |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3677 | * @param[in] parent The parent node of the unique structure. |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3678 | * @param[in] uniq_str_path One path from the unique string. |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3679 | * |
| 3680 | * @return EXIT_SUCCESS on succes, EXIT_FAILURE on forward reference, -1 on error. |
| 3681 | */ |
| 3682 | int |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3683 | resolve_unique(struct lys_node *parent, const char *uniq_str_path, uint8_t *trg_type) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3684 | { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3685 | int rc; |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3686 | const struct lys_node *leaf = NULL; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3687 | struct ly_ctx *ctx = parent->module->ctx; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3688 | |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 3689 | rc = resolve_descendant_schema_nodeid(uniq_str_path, *lys_child(parent, LYS_LEAF), LYS_LEAF, 1, &leaf); |
Michal Vasko | 9bb061b | 2016-02-12 11:00:19 +0100 | [diff] [blame] | 3690 | if (rc || !leaf) { |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3691 | if (rc) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3692 | LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3693 | if (rc > 0) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3694 | LOGVAL(ctx, LYE_INCHAR, LY_VLOG_PREV, NULL, uniq_str_path[rc - 1], &uniq_str_path[rc - 1]); |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 3695 | } else if (rc == -2) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3696 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Unique argument references list."); |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3697 | } |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3698 | rc = -1; |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3699 | } else { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3700 | LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
| 3701 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Target leaf not found."); |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3702 | rc = EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3703 | } |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3704 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3705 | } |
Michal Vasko | 9bb061b | 2016-02-12 11:00:19 +0100 | [diff] [blame] | 3706 | if (leaf->nodetype != LYS_LEAF) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3707 | LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
| 3708 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Target is not a leaf."); |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3709 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3710 | } |
| 3711 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3712 | /* check status */ |
Radek Krejci | c3f1b6f | 2017-02-15 10:51:10 +0100 | [diff] [blame] | 3713 | if (parent->nodetype != LYS_EXT && lyp_check_status(parent->flags, parent->module, parent->name, |
| 3714 | leaf->flags, leaf->module, leaf->name, leaf)) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3715 | return -1; |
| 3716 | } |
| 3717 | |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3718 | /* check that all unique's targets are of the same config type */ |
| 3719 | if (*trg_type) { |
| 3720 | if (((*trg_type == 1) && (leaf->flags & LYS_CONFIG_R)) || ((*trg_type == 2) && (leaf->flags & LYS_CONFIG_W))) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3721 | LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
| 3722 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3723 | "Leaf \"%s\" referenced in unique statement is config %s, but previous referenced leaf is config %s.", |
| 3724 | uniq_str_path, *trg_type == 1 ? "false" : "true", *trg_type == 1 ? "true" : "false"); |
| 3725 | return -1; |
| 3726 | } |
| 3727 | } else { |
| 3728 | /* first unique */ |
| 3729 | if (leaf->flags & LYS_CONFIG_W) { |
| 3730 | *trg_type = 1; |
| 3731 | } else { |
| 3732 | *trg_type = 2; |
| 3733 | } |
| 3734 | } |
| 3735 | |
Radek Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame] | 3736 | /* set leaf's unique flag */ |
| 3737 | ((struct lys_node_leaf *)leaf)->flags |= LYS_UNIQUE; |
| 3738 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3739 | return EXIT_SUCCESS; |
| 3740 | |
| 3741 | error: |
| 3742 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3743 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3744 | } |
| 3745 | |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 3746 | void |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3747 | unres_data_del(struct unres_data *unres, uint32_t i) |
| 3748 | { |
| 3749 | /* there are items after the one deleted */ |
| 3750 | if (i+1 < unres->count) { |
| 3751 | /* we only move the data, memory is left allocated, why bother */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3752 | memmove(&unres->node[i], &unres->node[i+1], (unres->count-(i+1)) * sizeof *unres->node); |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3753 | |
| 3754 | /* deleting the last item */ |
| 3755 | } else if (i == 0) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3756 | free(unres->node); |
| 3757 | unres->node = NULL; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3758 | } |
| 3759 | |
| 3760 | /* if there are no items after and it is not the last one, just move the counter */ |
| 3761 | --unres->count; |
| 3762 | } |
| 3763 | |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3764 | /** |
| 3765 | * @brief Resolve (find) a data node from a specific module. Does not log. |
| 3766 | * |
| 3767 | * @param[in] mod Module to search in. |
| 3768 | * @param[in] name Name of the data node. |
| 3769 | * @param[in] nam_len Length of the name. |
| 3770 | * @param[in] start Data node to start the search from. |
| 3771 | * @param[in,out] parents Resolved nodes. If there are some parents, |
| 3772 | * they are replaced (!!) with the resolvents. |
| 3773 | * |
Michal Vasko | 2471e7f | 2016-04-11 11:00:15 +0200 | [diff] [blame] | 3774 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3775 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3776 | static int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3777 | resolve_data(const struct lys_module *mod, const char *name, int nam_len, struct lyd_node *start, struct unres_data *parents) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3778 | { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3779 | struct lyd_node *node; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3780 | int flag; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3781 | uint32_t i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3782 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3783 | if (!parents->count) { |
| 3784 | parents->count = 1; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3785 | parents->node = malloc(sizeof *parents->node); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3786 | LY_CHECK_ERR_RETURN(!parents->node, LOGMEM(mod->ctx), -1); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3787 | parents->node[0] = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3788 | } |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3789 | for (i = 0; i < parents->count;) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 3790 | if (parents->node[i] && (parents->node[i]->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3791 | /* skip */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3792 | ++i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3793 | continue; |
| 3794 | } |
| 3795 | flag = 0; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3796 | LY_TREE_FOR(parents->node[i] ? parents->node[i]->child : start, node) { |
Michal Vasko | 3960835 | 2017-05-11 10:37:10 +0200 | [diff] [blame] | 3797 | if (lyd_node_module(node) == mod && !strncmp(node->schema->name, name, nam_len) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3798 | && node->schema->name[nam_len] == '\0') { |
| 3799 | /* matching target */ |
| 3800 | if (!flag) { |
Michal Vasko | 9a47e12 | 2015-09-03 14:26:32 +0200 | [diff] [blame] | 3801 | /* put node instead of the current parent */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3802 | parents->node[i] = node; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3803 | flag = 1; |
| 3804 | } else { |
Michal Vasko | 9a47e12 | 2015-09-03 14:26:32 +0200 | [diff] [blame] | 3805 | /* multiple matching, so create a new node */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3806 | ++parents->count; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3807 | parents->node = ly_realloc(parents->node, parents->count * sizeof *parents->node); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3808 | LY_CHECK_ERR_RETURN(!parents->node, LOGMEM(mod->ctx), EXIT_FAILURE); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3809 | parents->node[parents->count-1] = node; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3810 | ++i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3811 | } |
| 3812 | } |
| 3813 | } |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3814 | |
| 3815 | if (!flag) { |
| 3816 | /* remove item from the parents list */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3817 | unres_data_del(parents, i); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3818 | } else { |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3819 | ++i; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3820 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3821 | } |
| 3822 | |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3823 | return parents->count ? EXIT_SUCCESS : EXIT_FAILURE; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3824 | } |
| 3825 | |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3826 | static int |
Michal Vasko | 299aef1 | 2018-12-18 13:13:55 +0100 | [diff] [blame] | 3827 | resolve_schema_leafref_valid_dep_flag(const struct lys_node *op_node, const struct lys_module *local_mod, |
| 3828 | const struct lys_node *first_node, int abs_path) |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3829 | { |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3830 | const struct lys_node *node; |
| 3831 | |
Michal Vasko | 299aef1 | 2018-12-18 13:13:55 +0100 | [diff] [blame] | 3832 | if (!op_node) { |
| 3833 | /* leafref pointing to a different module */ |
| 3834 | if (local_mod != lys_node_module(first_node)) { |
| 3835 | return 1; |
| 3836 | } |
| 3837 | } else if (lys_parent(op_node)) { |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3838 | /* inner operation (notif/action) */ |
| 3839 | if (abs_path) { |
| 3840 | return 1; |
| 3841 | } else { |
Michal Vasko | 4757980 | 2019-09-24 13:56:47 +0200 | [diff] [blame] | 3842 | /* if first_node parent is not op_node, it is external */ |
| 3843 | for (node = first_node; node && (node != op_node); node = lys_parent(node)); |
| 3844 | if (!node) { |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3845 | return 1; |
| 3846 | } |
| 3847 | } |
| 3848 | } else { |
| 3849 | /* top-level operation (notif/rpc) */ |
| 3850 | if (op_node != first_node) { |
| 3851 | return 1; |
| 3852 | } |
| 3853 | } |
| 3854 | |
| 3855 | return 0; |
| 3856 | } |
| 3857 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3858 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3859 | * @brief Resolve a path (leafref) predicate in JSON schema context. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3860 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3861 | * @param[in] path Path to use. |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3862 | * @param[in] context_node Predicate context node (where the predicate is placed). |
| 3863 | * @param[in] parent Path context node (where the path begins/is placed). |
Michal Vasko | ba8fd2b | 2019-04-25 11:17:36 +0200 | [diff] [blame] | 3864 | * @param[in] node_set Set where to add nodes whose parent chain must be implemented. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3865 | * |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3866 | * @return 0 on forward reference, otherwise the number |
| 3867 | * of characters successfully parsed, |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3868 | * positive on success, negative on failure. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3869 | */ |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3870 | static int |
Michal Vasko | ba8fd2b | 2019-04-25 11:17:36 +0200 | [diff] [blame] | 3871 | resolve_schema_leafref_predicate(const char *path, const struct lys_node *context_node, struct lys_node *parent, |
| 3872 | struct ly_set *node_set) |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3873 | { |
Michal Vasko | 0f99d3e | 2017-01-10 10:50:40 +0100 | [diff] [blame] | 3874 | const struct lys_module *trg_mod; |
Michal Vasko | ba8fd2b | 2019-04-25 11:17:36 +0200 | [diff] [blame] | 3875 | const struct lys_node *src_node, *dst_node, *tmp_parent; |
| 3876 | struct lys_node_augment *last_aug; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3877 | const char *path_key_expr, *source, *sour_pref, *dest, *dest_pref; |
Michal Vasko | f9b35d9 | 2016-10-21 15:19:30 +0200 | [diff] [blame] | 3878 | int pke_len, sour_len, sour_pref_len, dest_len, dest_pref_len, pke_parsed, parsed = 0; |
Michal Vasko | 299aef1 | 2018-12-18 13:13:55 +0100 | [diff] [blame] | 3879 | int has_predicate, dest_parent_times, i, rc; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3880 | struct ly_ctx *ctx = context_node->module->ctx; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3881 | |
| 3882 | do { |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3883 | if ((i = parse_path_predicate(path, &sour_pref, &sour_pref_len, &source, &sour_len, &path_key_expr, |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3884 | &pke_len, &has_predicate)) < 1) { |
Michal Vasko | ba8fd2b | 2019-04-25 11:17:36 +0200 | [diff] [blame] | 3885 | LOGVAL(ctx, LYE_INCHAR, LY_VLOG_LYS, parent, path[-i], path - i); |
| 3886 | return -parsed + i; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3887 | } |
| 3888 | parsed += i; |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3889 | path += i; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3890 | |
Michal Vasko | 5809090 | 2015-08-13 14:04:15 +0200 | [diff] [blame] | 3891 | /* source (must be leaf) */ |
Michal Vasko | 0f99d3e | 2017-01-10 10:50:40 +0100 | [diff] [blame] | 3892 | if (sour_pref) { |
Michal Vasko | 921eb6b | 2017-10-13 10:01:39 +0200 | [diff] [blame] | 3893 | trg_mod = lyp_get_module(lys_node_module(parent), NULL, 0, sour_pref, sour_pref_len, 0); |
Michal Vasko | 0f99d3e | 2017-01-10 10:50:40 +0100 | [diff] [blame] | 3894 | } else { |
Michal Vasko | a50e60c | 2018-08-22 12:07:21 +0200 | [diff] [blame] | 3895 | trg_mod = lys_node_module(parent); |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3896 | } |
Michal Vasko | 32fb499 | 2019-03-08 08:55:16 +0100 | [diff] [blame] | 3897 | rc = lys_getnext_data(trg_mod, context_node, source, sour_len, LYS_LEAF | LYS_LEAFLIST, LYS_GETNEXT_NOSTATECHECK, |
| 3898 | &src_node); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3899 | if (rc) { |
Michal Vasko | af8ec36 | 2018-03-28 09:08:09 +0200 | [diff] [blame] | 3900 | LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref predicate", path-parsed); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3901 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3902 | } |
| 3903 | |
| 3904 | /* destination */ |
Michal Vasko | f9b35d9 | 2016-10-21 15:19:30 +0200 | [diff] [blame] | 3905 | dest_parent_times = 0; |
| 3906 | pke_parsed = 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3907 | if ((i = parse_path_key_expr(path_key_expr, &dest_pref, &dest_pref_len, &dest, &dest_len, |
| 3908 | &dest_parent_times)) < 1) { |
Michal Vasko | af8ec36 | 2018-03-28 09:08:09 +0200 | [diff] [blame] | 3909 | LOGVAL(ctx, LYE_INCHAR, LY_VLOG_LYS, parent, path_key_expr[-i], path_key_expr-i); |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3910 | return -parsed; |
| 3911 | } |
| 3912 | pke_parsed += i; |
| 3913 | |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3914 | for (i = 0, dst_node = parent; i < dest_parent_times; ++i) { |
Michal Vasko | ec06ed4 | 2018-12-07 08:33:55 +0100 | [diff] [blame] | 3915 | if (!dst_node) { |
| 3916 | /* we went too much into parents, there is no parent anymore */ |
| 3917 | LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref predicate", path_key_expr); |
| 3918 | return 0; |
| 3919 | } |
| 3920 | |
Michal Vasko | 3ba2d79 | 2017-07-10 15:14:43 +0200 | [diff] [blame] | 3921 | if (dst_node->parent && (dst_node->parent->nodetype == LYS_AUGMENT) |
| 3922 | && !((struct lys_node_augment *)dst_node->parent)->target) { |
| 3923 | /* we are in an unresolved augment, cannot evaluate */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3924 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, dst_node->parent, |
Michal Vasko | 3ba2d79 | 2017-07-10 15:14:43 +0200 | [diff] [blame] | 3925 | "Cannot resolve leafref predicate \"%s\" because it is in an unresolved augment.", path_key_expr); |
| 3926 | return 0; |
| 3927 | } |
| 3928 | |
Michal Vasko | fbaead7 | 2016-10-07 10:54:48 +0200 | [diff] [blame] | 3929 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 3930 | * all schema nodes that cannot be instantiated in data tree */ |
| 3931 | for (dst_node = lys_parent(dst_node); |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3932 | dst_node && !(dst_node->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_ACTION | LYS_NOTIF | LYS_RPC)); |
Michal Vasko | fbaead7 | 2016-10-07 10:54:48 +0200 | [diff] [blame] | 3933 | dst_node = lys_parent(dst_node)); |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3934 | } |
| 3935 | while (1) { |
Michal Vasko | ba8fd2b | 2019-04-25 11:17:36 +0200 | [diff] [blame] | 3936 | last_aug = NULL; |
| 3937 | |
Michal Vasko | 0f99d3e | 2017-01-10 10:50:40 +0100 | [diff] [blame] | 3938 | if (dest_pref) { |
Michal Vasko | 921eb6b | 2017-10-13 10:01:39 +0200 | [diff] [blame] | 3939 | trg_mod = lyp_get_module(lys_node_module(parent), NULL, 0, dest_pref, dest_pref_len, 0); |
Michal Vasko | 0f99d3e | 2017-01-10 10:50:40 +0100 | [diff] [blame] | 3940 | } else { |
Michal Vasko | a50e60c | 2018-08-22 12:07:21 +0200 | [diff] [blame] | 3941 | trg_mod = lys_node_module(parent); |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3942 | } |
Michal Vasko | ba8fd2b | 2019-04-25 11:17:36 +0200 | [diff] [blame] | 3943 | |
| 3944 | if (!trg_mod->implemented && dst_node) { |
| 3945 | get_next_augment: |
| 3946 | last_aug = lys_getnext_target_aug(last_aug, trg_mod, dst_node); |
| 3947 | } |
| 3948 | |
| 3949 | tmp_parent = (last_aug ? (struct lys_node *)last_aug : dst_node); |
| 3950 | rc = lys_getnext_data(trg_mod, tmp_parent, dest, dest_len, LYS_CONTAINER | LYS_LIST | LYS_LEAF, |
Michal Vasko | 32fb499 | 2019-03-08 08:55:16 +0100 | [diff] [blame] | 3951 | LYS_GETNEXT_NOSTATECHECK, &dst_node); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3952 | if (rc) { |
Michal Vasko | ba8fd2b | 2019-04-25 11:17:36 +0200 | [diff] [blame] | 3953 | if (last_aug) { |
| 3954 | /* restore the correct augment target */ |
| 3955 | dst_node = last_aug->target; |
| 3956 | goto get_next_augment; |
| 3957 | } |
Michal Vasko | af8ec36 | 2018-03-28 09:08:09 +0200 | [diff] [blame] | 3958 | LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref predicate", path_key_expr); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3959 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3960 | } |
| 3961 | |
| 3962 | if (pke_len == pke_parsed) { |
| 3963 | break; |
| 3964 | } |
| 3965 | |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3966 | if ((i = parse_path_key_expr(path_key_expr + pke_parsed, &dest_pref, &dest_pref_len, &dest, &dest_len, |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3967 | &dest_parent_times)) < 1) { |
Michal Vasko | af8ec36 | 2018-03-28 09:08:09 +0200 | [diff] [blame] | 3968 | LOGVAL(ctx, LYE_INCHAR, LY_VLOG_LYS, parent, |
Michal Vasko | ba8fd2b | 2019-04-25 11:17:36 +0200 | [diff] [blame] | 3969 | (path_key_expr + pke_parsed)[-i], (path_key_expr + pke_parsed) - i); |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3970 | return -parsed; |
| 3971 | } |
| 3972 | pke_parsed += i; |
| 3973 | } |
| 3974 | |
| 3975 | /* check source - dest match */ |
Michal Vasko | 59ad458 | 2016-09-16 13:15:41 +0200 | [diff] [blame] | 3976 | if (dst_node->nodetype != src_node->nodetype) { |
Michal Vasko | af8ec36 | 2018-03-28 09:08:09 +0200 | [diff] [blame] | 3977 | LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref predicate", path - parsed); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3978 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Destination node is not a %s, but a %s.", |
Michal Vasko | 59ad458 | 2016-09-16 13:15:41 +0200 | [diff] [blame] | 3979 | strnodetype(src_node->nodetype), strnodetype(dst_node->nodetype)); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3980 | return -parsed; |
| 3981 | } |
Michal Vasko | ba8fd2b | 2019-04-25 11:17:36 +0200 | [diff] [blame] | 3982 | |
| 3983 | /* add both nodes into node set */ |
| 3984 | ly_set_add(node_set, (void *)dst_node, 0); |
| 3985 | ly_set_add(node_set, (void *)src_node, 0); |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3986 | } while (has_predicate); |
| 3987 | |
| 3988 | return parsed; |
| 3989 | } |
| 3990 | |
Michal Vasko | 74083ec | 2018-06-15 10:00:12 +0200 | [diff] [blame] | 3991 | static int |
| 3992 | check_leafref_features(struct lys_type *type) |
| 3993 | { |
| 3994 | struct lys_node *iter; |
| 3995 | struct ly_set *src_parents, *trg_parents, *features; |
| 3996 | struct lys_node_augment *aug; |
| 3997 | struct ly_ctx *ctx = ((struct lys_tpdf *)type->parent)->module->ctx; |
| 3998 | unsigned int i, j, size, x; |
| 3999 | int ret = EXIT_SUCCESS; |
| 4000 | |
| 4001 | assert(type->parent); |
| 4002 | |
| 4003 | src_parents = ly_set_new(); |
| 4004 | trg_parents = ly_set_new(); |
| 4005 | features = ly_set_new(); |
| 4006 | |
| 4007 | /* get parents chain of source (leafref) */ |
| 4008 | for (iter = (struct lys_node *)type->parent; iter; iter = lys_parent(iter)) { |
| 4009 | if (iter->nodetype & (LYS_INPUT | LYS_OUTPUT)) { |
| 4010 | continue; |
| 4011 | } |
| 4012 | if (iter->parent && (iter->parent->nodetype == LYS_AUGMENT)) { |
| 4013 | aug = (struct lys_node_augment *)iter->parent; |
| 4014 | if ((aug->module->implemented && (aug->flags & LYS_NOTAPPLIED)) || !aug->target) { |
| 4015 | /* unresolved augment, wait until it's resolved */ |
| 4016 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, aug, |
| 4017 | "Cannot check leafref \"%s\" if-feature consistency because of an unresolved augment.", type->info.lref.path); |
| 4018 | ret = EXIT_FAILURE; |
| 4019 | goto cleanup; |
| 4020 | } |
Michal Vasko | d42871e | 2018-07-12 09:06:53 +0200 | [diff] [blame] | 4021 | /* also add this augment */ |
| 4022 | ly_set_add(src_parents, aug, LY_SET_OPT_USEASLIST); |
Michal Vasko | 74083ec | 2018-06-15 10:00:12 +0200 | [diff] [blame] | 4023 | } |
| 4024 | ly_set_add(src_parents, iter, LY_SET_OPT_USEASLIST); |
| 4025 | } |
| 4026 | /* get parents chain of target */ |
| 4027 | for (iter = (struct lys_node *)type->info.lref.target; iter; iter = lys_parent(iter)) { |
| 4028 | if (iter->nodetype & (LYS_INPUT | LYS_OUTPUT)) { |
| 4029 | continue; |
| 4030 | } |
| 4031 | if (iter->parent && (iter->parent->nodetype == LYS_AUGMENT)) { |
| 4032 | aug = (struct lys_node_augment *)iter->parent; |
| 4033 | if ((aug->module->implemented && (aug->flags & LYS_NOTAPPLIED)) || !aug->target) { |
| 4034 | /* unresolved augment, wait until it's resolved */ |
| 4035 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, aug, |
| 4036 | "Cannot check leafref \"%s\" if-feature consistency because of an unresolved augment.", type->info.lref.path); |
| 4037 | ret = EXIT_FAILURE; |
| 4038 | goto cleanup; |
| 4039 | } |
| 4040 | } |
| 4041 | ly_set_add(trg_parents, iter, LY_SET_OPT_USEASLIST); |
| 4042 | } |
| 4043 | |
| 4044 | /* compare the features used in if-feature statements in the rest of both |
| 4045 | * chains of parents. The set of features used for target must be a subset |
| 4046 | * of features used for the leafref. This is not a perfect, we should compare |
| 4047 | * the truth tables but it could require too much resources, so we simplify that */ |
| 4048 | for (i = 0; i < src_parents->number; i++) { |
| 4049 | iter = src_parents->set.s[i]; /* shortcut */ |
| 4050 | if (!iter->iffeature_size) { |
| 4051 | continue; |
| 4052 | } |
| 4053 | for (j = 0; j < iter->iffeature_size; j++) { |
| 4054 | resolve_iffeature_getsizes(&iter->iffeature[j], NULL, &size); |
| 4055 | for (; size; size--) { |
| 4056 | if (!iter->iffeature[j].features[size - 1]) { |
| 4057 | /* not yet resolved feature, postpone this check */ |
| 4058 | ret = EXIT_FAILURE; |
| 4059 | goto cleanup; |
| 4060 | } |
| 4061 | ly_set_add(features, iter->iffeature[j].features[size - 1], 0); |
| 4062 | } |
| 4063 | } |
| 4064 | } |
| 4065 | x = features->number; |
| 4066 | for (i = 0; i < trg_parents->number; i++) { |
| 4067 | iter = trg_parents->set.s[i]; /* shortcut */ |
| 4068 | if (!iter->iffeature_size) { |
| 4069 | continue; |
| 4070 | } |
| 4071 | for (j = 0; j < iter->iffeature_size; j++) { |
| 4072 | resolve_iffeature_getsizes(&iter->iffeature[j], NULL, &size); |
| 4073 | for (; size; size--) { |
| 4074 | if (!iter->iffeature[j].features[size - 1]) { |
| 4075 | /* not yet resolved feature, postpone this check */ |
| 4076 | ret = EXIT_FAILURE; |
| 4077 | goto cleanup; |
| 4078 | } |
| 4079 | if ((unsigned)ly_set_add(features, iter->iffeature[j].features[size - 1], 0) >= x) { |
| 4080 | /* the feature is not present in features set of target's parents chain */ |
Michal Vasko | 3367250 | 2020-01-09 10:22:52 +0100 | [diff] [blame] | 4081 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, type->parent, |
Michal Vasko | 74083ec | 2018-06-15 10:00:12 +0200 | [diff] [blame] | 4082 | "Leafref is not conditional based on \"%s\" feature as its target.", |
| 4083 | iter->iffeature[j].features[size - 1]->name); |
Michal Vasko | 3367250 | 2020-01-09 10:22:52 +0100 | [diff] [blame] | 4084 | for (iter = type->info.lref.target->parent; iter && (iter->nodetype != LYS_USES); iter = lys_parent(iter)); |
| 4085 | if (iter) { |
| 4086 | /* we are in a uses so there can still be a refine that will add an if-feature */ |
| 4087 | ret = EXIT_FAILURE; |
| 4088 | } else { |
| 4089 | ret = -1; |
| 4090 | } |
Michal Vasko | 74083ec | 2018-06-15 10:00:12 +0200 | [diff] [blame] | 4091 | goto cleanup; |
| 4092 | } |
| 4093 | } |
| 4094 | } |
| 4095 | } |
| 4096 | |
| 4097 | cleanup: |
| 4098 | ly_set_free(features); |
| 4099 | ly_set_free(src_parents); |
| 4100 | ly_set_free(trg_parents); |
| 4101 | |
| 4102 | return ret; |
| 4103 | } |
| 4104 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4105 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 4106 | * @brief Resolve a path (leafref) in JSON schema context. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4107 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4108 | * @param[in] path Path to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4109 | * @param[in] parent_node Parent of the leafref. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4110 | * @param[out] ret Pointer to the resolved schema node. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4111 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4112 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4113 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4114 | static int |
Michal Vasko | 74083ec | 2018-06-15 10:00:12 +0200 | [diff] [blame] | 4115 | resolve_schema_leafref(struct lys_type *type, struct lys_node *parent, struct unres_schema *unres) |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 4116 | { |
Michal Vasko | cb45f47 | 2018-02-12 10:47:42 +0100 | [diff] [blame] | 4117 | const struct lys_node *node, *op_node = NULL, *tmp_parent; |
Michal Vasko | ba8fd2b | 2019-04-25 11:17:36 +0200 | [diff] [blame] | 4118 | struct ly_set *node_set; |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 4119 | struct lys_node_augment *last_aug; |
Michal Vasko | 3c60cbb | 2017-07-10 11:50:03 +0200 | [diff] [blame] | 4120 | const struct lys_module *tmp_mod, *cur_module; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 4121 | const char *id, *prefix, *name; |
| 4122 | int pref_len, nam_len, parent_times, has_predicate; |
Michal Vasko | cb45f47 | 2018-02-12 10:47:42 +0100 | [diff] [blame] | 4123 | int i, first_iter; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4124 | struct ly_ctx *ctx = parent->module->ctx; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 4125 | |
Michal Vasko | 9e451f9 | 2019-07-19 11:57:53 +0200 | [diff] [blame] | 4126 | /* first check that we are not in an unresolved augment */ |
| 4127 | for (node = parent; lys_parent(node); node = lys_parent(node)); |
| 4128 | if (node->parent && (node->parent->nodetype == LYS_AUGMENT)) { |
| 4129 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, node->parent, |
| 4130 | "Cannot resolve leafref \"%s\" because it is in an unresolved augment.", type->info.lref.path); |
| 4131 | return EXIT_FAILURE; |
| 4132 | } |
| 4133 | |
Michal Vasko | ba8fd2b | 2019-04-25 11:17:36 +0200 | [diff] [blame] | 4134 | first_iter = 1; |
| 4135 | parent_times = 0; |
| 4136 | id = type->info.lref.path; |
| 4137 | node_set = ly_set_new(); |
| 4138 | if (!node_set) { |
| 4139 | LOGMEM(ctx); |
| 4140 | return -1; |
| 4141 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 4142 | |
Michal Vasko | ba8fd2b | 2019-04-25 11:17:36 +0200 | [diff] [blame] | 4143 | /* find operation schema we are in */ |
| 4144 | for (op_node = lys_parent(parent); |
| 4145 | op_node && !(op_node->nodetype & (LYS_ACTION | LYS_NOTIF | LYS_RPC)); |
| 4146 | op_node = lys_parent(op_node)); |
Michal Vasko | e9914d1 | 2016-10-07 14:32:37 +0200 | [diff] [blame] | 4147 | |
Michal Vasko | 2a33c21 | 2020-05-07 09:08:13 +0200 | [diff] [blame] | 4148 | if (type->der->module) { |
| 4149 | /* typedef, take its local module */ |
| 4150 | cur_module = type->der->module; |
| 4151 | } else { |
| 4152 | cur_module = lys_node_module(parent); |
| 4153 | } |
Michal Vasko | ba8fd2b | 2019-04-25 11:17:36 +0200 | [diff] [blame] | 4154 | do { |
| 4155 | if ((i = parse_path_arg(cur_module, id, &prefix, &pref_len, &name, &nam_len, &parent_times, &has_predicate)) < 1) { |
| 4156 | LOGVAL(ctx, LYE_INCHAR, LY_VLOG_LYS, parent, id[-i], &id[-i]); |
| 4157 | ly_set_free(node_set); |
| 4158 | return -1; |
| 4159 | } |
| 4160 | id += i; |
| 4161 | |
| 4162 | /* get the current module */ |
| 4163 | tmp_mod = prefix ? lyp_get_module(cur_module, NULL, 0, prefix, pref_len, 0) : cur_module; |
| 4164 | if (!tmp_mod) { |
| 4165 | LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", type->info.lref.path); |
| 4166 | ly_set_free(node_set); |
| 4167 | return EXIT_FAILURE; |
| 4168 | } |
| 4169 | last_aug = NULL; |
| 4170 | |
| 4171 | if (first_iter) { |
| 4172 | if (parent_times == -1) { |
| 4173 | /* use module data */ |
| 4174 | node = NULL; |
| 4175 | |
| 4176 | } else if (parent_times > 0) { |
| 4177 | /* we are looking for the right parent */ |
| 4178 | for (i = 0, node = parent; i < parent_times; i++) { |
Michal Vasko | ba8fd2b | 2019-04-25 11:17:36 +0200 | [diff] [blame] | 4179 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 4180 | * all schema nodes that cannot be instantiated in data tree */ |
| 4181 | for (node = lys_parent(node); |
| 4182 | node && !(node->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_ACTION | LYS_NOTIF | LYS_RPC)); |
| 4183 | node = lys_parent(node)); |
| 4184 | |
| 4185 | if (!node) { |
| 4186 | if (i == parent_times - 1) { |
| 4187 | /* top-level */ |
| 4188 | break; |
| 4189 | } |
| 4190 | |
| 4191 | /* higher than top-level */ |
| 4192 | LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", type->info.lref.path); |
| 4193 | ly_set_free(node_set); |
| 4194 | return EXIT_FAILURE; |
| 4195 | } |
| 4196 | } |
| 4197 | } else { |
| 4198 | LOGINT(ctx); |
| 4199 | ly_set_free(node_set); |
| 4200 | return -1; |
| 4201 | } |
| 4202 | } |
| 4203 | |
| 4204 | /* find the next node (either in unconnected augment or as a schema sibling, node is NULL for top-level node - |
| 4205 | * - useless to search for that in augments) */ |
| 4206 | if (!tmp_mod->implemented && node) { |
| 4207 | get_next_augment: |
| 4208 | last_aug = lys_getnext_target_aug(last_aug, tmp_mod, node); |
| 4209 | } |
| 4210 | |
| 4211 | tmp_parent = (last_aug ? (struct lys_node *)last_aug : node); |
| 4212 | node = NULL; |
| 4213 | while ((node = lys_getnext(node, tmp_parent, tmp_mod, LYS_GETNEXT_NOSTATECHECK))) { |
| 4214 | if (lys_node_module(node) != lys_main_module(tmp_mod)) { |
| 4215 | continue; |
| 4216 | } |
| 4217 | if (strncmp(node->name, name, nam_len) || node->name[nam_len]) { |
| 4218 | continue; |
| 4219 | } |
| 4220 | /* match */ |
| 4221 | break; |
| 4222 | } |
| 4223 | if (!node) { |
| 4224 | if (last_aug) { |
| 4225 | /* restore the correct augment target */ |
| 4226 | node = last_aug->target; |
| 4227 | goto get_next_augment; |
| 4228 | } |
| 4229 | LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", type->info.lref.path); |
| 4230 | ly_set_free(node_set); |
| 4231 | return EXIT_FAILURE; |
| 4232 | } |
| 4233 | |
| 4234 | if (first_iter) { |
Michal Vasko | c17f9e1 | 2019-09-05 13:29:26 +0200 | [diff] [blame] | 4235 | /* find module whose data will actually contain this leafref */ |
| 4236 | for (tmp_parent = parent; lys_parent(tmp_parent); tmp_parent = lys_parent(tmp_parent)); |
| 4237 | |
Michal Vasko | ba8fd2b | 2019-04-25 11:17:36 +0200 | [diff] [blame] | 4238 | /* set external dependency flag, we can decide based on the first found node */ |
Michal Vasko | c17f9e1 | 2019-09-05 13:29:26 +0200 | [diff] [blame] | 4239 | if (resolve_schema_leafref_valid_dep_flag(op_node, lys_node_module(tmp_parent), node, (parent_times == -1 ? 1 : 0))) { |
Michal Vasko | ba8fd2b | 2019-04-25 11:17:36 +0200 | [diff] [blame] | 4240 | parent->flags |= LYS_LEAFREF_DEP; |
| 4241 | } |
| 4242 | first_iter = 0; |
| 4243 | } |
| 4244 | |
| 4245 | if (has_predicate) { |
| 4246 | /* we have predicate, so the current result must be list */ |
| 4247 | if (node->nodetype != LYS_LIST) { |
| 4248 | LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", type->info.lref.path); |
| 4249 | ly_set_free(node_set); |
| 4250 | return -1; |
| 4251 | } |
| 4252 | |
| 4253 | i = resolve_schema_leafref_predicate(id, node, parent, node_set); |
| 4254 | if (!i) { |
| 4255 | ly_set_free(node_set); |
| 4256 | return EXIT_FAILURE; |
| 4257 | } else if (i < 0) { |
| 4258 | ly_set_free(node_set); |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 4259 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 4260 | } |
| 4261 | id += i; |
Michal Vasko | ba8fd2b | 2019-04-25 11:17:36 +0200 | [diff] [blame] | 4262 | has_predicate = 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 4263 | } |
Michal Vasko | ba8fd2b | 2019-04-25 11:17:36 +0200 | [diff] [blame] | 4264 | } while (id[0]); |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 4265 | |
Michal Vasko | ba8fd2b | 2019-04-25 11:17:36 +0200 | [diff] [blame] | 4266 | /* the target must be leaf or leaf-list (in YANG 1.1 only) */ |
| 4267 | if ((node->nodetype != LYS_LEAF) && (node->nodetype != LYS_LEAFLIST)) { |
| 4268 | LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", type->info.lref.path); |
| 4269 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Leafref target \"%s\" is not a leaf nor a leaf-list.", type->info.lref.path); |
| 4270 | ly_set_free(node_set); |
| 4271 | return -1; |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 4272 | } |
| 4273 | |
Michal Vasko | ba8fd2b | 2019-04-25 11:17:36 +0200 | [diff] [blame] | 4274 | /* check status */ |
| 4275 | if (lyp_check_status(parent->flags, parent->module, parent->name, |
| 4276 | node->flags, node->module, node->name, node)) { |
| 4277 | ly_set_free(node_set); |
| 4278 | return -1; |
| 4279 | } |
| 4280 | |
| 4281 | /* assign */ |
| 4282 | type->info.lref.target = (struct lys_node_leaf *)node; |
| 4283 | |
| 4284 | /* add the target node into a set so its parent chain modules can be implemented */ |
| 4285 | ly_set_add(node_set, (void *)node, 0); |
| 4286 | |
Michal Vasko | 9e451f9 | 2019-07-19 11:57:53 +0200 | [diff] [blame] | 4287 | /* find the actual module of this leafref, it can be in a foreign augment */ |
| 4288 | for (node = parent; lys_parent(node); node = lys_parent(node)); |
| 4289 | if (lys_node_module(node)->implemented) { |
Michal Vasko | 74083ec | 2018-06-15 10:00:12 +0200 | [diff] [blame] | 4290 | /* make all the modules in the path implemented */ |
Michal Vasko | ba8fd2b | 2019-04-25 11:17:36 +0200 | [diff] [blame] | 4291 | for (i = 0; (unsigned)i < node_set->number; ++i) { |
| 4292 | for (node = node_set->set.s[i]; node; node = lys_parent(node)) { |
| 4293 | if (!lys_node_module(node)->implemented) { |
| 4294 | lys_node_module(node)->implemented = 1; |
| 4295 | if (unres_schema_add_node(lys_node_module(node), unres, NULL, UNRES_MOD_IMPLEMENT, NULL) == -1) { |
| 4296 | ly_set_free(node_set); |
| 4297 | return -1; |
| 4298 | } |
Michal Vasko | 74083ec | 2018-06-15 10:00:12 +0200 | [diff] [blame] | 4299 | } |
| 4300 | } |
Michal Vasko | 74083ec | 2018-06-15 10:00:12 +0200 | [diff] [blame] | 4301 | |
Michal Vasko | 1076550 | 2020-01-09 10:22:27 +0100 | [diff] [blame] | 4302 | /* check leafref that it is allowed */ |
| 4303 | if (lys_leaf_check_leafref((struct lys_node_leaf *)node_set->set.s[i], (struct lys_node *)type->parent)) { |
Michal Vasko | 113cae5 | 2019-10-14 09:28:54 +0200 | [diff] [blame] | 4304 | ly_set_free(node_set); |
| 4305 | return -1; |
| 4306 | } |
Michal Vasko | 74083ec | 2018-06-15 10:00:12 +0200 | [diff] [blame] | 4307 | } |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 4308 | } |
Michal Vasko | ba8fd2b | 2019-04-25 11:17:36 +0200 | [diff] [blame] | 4309 | ly_set_free(node_set); |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 4310 | |
Michal Vasko | 74083ec | 2018-06-15 10:00:12 +0200 | [diff] [blame] | 4311 | /* check if leafref and its target are under common if-features */ |
| 4312 | return check_leafref_features(type); |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 4313 | } |
| 4314 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4315 | /** |
Michal Vasko | 718ecdd | 2017-10-03 14:12:39 +0200 | [diff] [blame] | 4316 | * @brief Compare 2 data node values. |
| 4317 | * |
| 4318 | * Comparison performed on canonical forms, the first value |
| 4319 | * is first transformed into canonical form. |
| 4320 | * |
| 4321 | * @param[in] node Leaf/leaf-list with these values. |
| 4322 | * @param[in] noncan_val Non-canonical value. |
| 4323 | * @param[in] noncan_val_len Length of \p noncal_val. |
| 4324 | * @param[in] can_val Canonical value. |
| 4325 | * @return 1 if equal, 0 if not, -1 on error (logged). |
| 4326 | */ |
| 4327 | static int |
| 4328 | valequal(struct lys_node *node, const char *noncan_val, int noncan_val_len, const char *can_val) |
| 4329 | { |
| 4330 | int ret; |
| 4331 | struct lyd_node_leaf_list leaf; |
| 4332 | struct lys_node_leaf *sleaf = (struct lys_node_leaf*)node; |
| 4333 | |
| 4334 | /* dummy leaf */ |
| 4335 | memset(&leaf, 0, sizeof leaf); |
| 4336 | leaf.value_str = lydict_insert(node->module->ctx, noncan_val, noncan_val_len); |
| 4337 | |
| 4338 | repeat: |
| 4339 | leaf.value_type = sleaf->type.base; |
| 4340 | leaf.schema = node; |
| 4341 | |
| 4342 | if (leaf.value_type == LY_TYPE_LEAFREF) { |
| 4343 | if (!sleaf->type.info.lref.target) { |
| 4344 | /* it should either be unresolved leafref (leaf.value_type are ORed flags) or it will be resolved */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4345 | LOGINT(node->module->ctx); |
Michal Vasko | 718ecdd | 2017-10-03 14:12:39 +0200 | [diff] [blame] | 4346 | ret = -1; |
| 4347 | goto finish; |
| 4348 | } |
| 4349 | sleaf = sleaf->type.info.lref.target; |
| 4350 | goto repeat; |
| 4351 | } else { |
Michal Vasko | 0abb1bc | 2020-02-25 15:47:10 +0100 | [diff] [blame] | 4352 | if (!lyp_parse_value(&sleaf->type, &leaf.value_str, NULL, &leaf, NULL, NULL, 0, 0)) { |
Michal Vasko | 718ecdd | 2017-10-03 14:12:39 +0200 | [diff] [blame] | 4353 | ret = -1; |
| 4354 | goto finish; |
| 4355 | } |
| 4356 | } |
| 4357 | |
| 4358 | if (!strcmp(leaf.value_str, can_val)) { |
| 4359 | ret = 1; |
| 4360 | } else { |
| 4361 | ret = 0; |
| 4362 | } |
| 4363 | |
| 4364 | finish: |
| 4365 | lydict_remove(node->module->ctx, leaf.value_str); |
| 4366 | return ret; |
| 4367 | } |
| 4368 | |
| 4369 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 4370 | * @brief Resolve instance-identifier predicate in JSON data format. |
| 4371 | * Does not log. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4372 | * |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 4373 | * @param[in] prev_mod Previous module to use in case there is no prefix. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4374 | * @param[in] pred Predicate to use. |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4375 | * @param[in,out] node Node matching the restriction without |
| 4376 | * the predicate. If it does not satisfy the predicate, |
| 4377 | * it is set to NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4378 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4379 | * @return Number of characters successfully parsed, |
| 4380 | * positive on success, negative on failure. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4381 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4382 | static int |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4383 | resolve_instid_predicate(const struct lys_module *prev_mod, const char *pred, struct lyd_node **node, int cur_idx) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4384 | { |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4385 | /* ... /node[key=value] ... */ |
| 4386 | struct lyd_node_leaf_list *key; |
| 4387 | struct lys_node_leaf **list_keys = NULL; |
Michal Vasko | ab8adcd | 2017-10-02 13:32:24 +0200 | [diff] [blame] | 4388 | struct lys_node_list *slist = NULL; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4389 | const char *model, *name, *value; |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4390 | int mod_len, nam_len, val_len, i, has_predicate, parsed; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4391 | struct ly_ctx *ctx = prev_mod->ctx; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4392 | |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4393 | assert(pred && node && *node); |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4394 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4395 | parsed = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4396 | do { |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4397 | if ((i = parse_predicate(pred + parsed, &model, &mod_len, &name, &nam_len, &value, &val_len, &has_predicate)) < 1) { |
| 4398 | return -parsed + i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4399 | } |
| 4400 | parsed += i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4401 | |
Michal Vasko | 88850b7 | 2017-10-02 13:13:21 +0200 | [diff] [blame] | 4402 | if (!(*node)) { |
| 4403 | /* just parse it all */ |
| 4404 | continue; |
| 4405 | } |
| 4406 | |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4407 | /* target */ |
| 4408 | if (name[0] == '.') { |
| 4409 | /* leaf-list value */ |
| 4410 | if ((*node)->schema->nodetype != LYS_LEAFLIST) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4411 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier expects leaf-list, but have %s \"%s\".", |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4412 | strnodetype((*node)->schema->nodetype), (*node)->schema->name); |
| 4413 | parsed = -1; |
| 4414 | goto cleanup; |
| 4415 | } |
| 4416 | |
| 4417 | /* check the value */ |
Michal Vasko | 718ecdd | 2017-10-03 14:12:39 +0200 | [diff] [blame] | 4418 | if (!valequal((*node)->schema, value, val_len, ((struct lyd_node_leaf_list *)*node)->value_str)) { |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4419 | *node = NULL; |
| 4420 | goto cleanup; |
| 4421 | } |
| 4422 | |
| 4423 | } else if (isdigit(name[0])) { |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4424 | assert(!value); |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4425 | |
| 4426 | /* keyless list position */ |
| 4427 | if ((*node)->schema->nodetype != LYS_LIST) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4428 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier expects list, but have %s \"%s\".", |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4429 | strnodetype((*node)->schema->nodetype), (*node)->schema->name); |
| 4430 | parsed = -1; |
| 4431 | goto cleanup; |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4432 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4433 | |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4434 | if (((struct lys_node_list *)(*node)->schema)->keys) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4435 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier expects list without keys, but have list \"%s\".", |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4436 | (*node)->schema->name); |
| 4437 | parsed = -1; |
| 4438 | goto cleanup; |
| 4439 | } |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4440 | |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4441 | /* check the index */ |
| 4442 | if (atoi(name) != cur_idx) { |
| 4443 | *node = NULL; |
| 4444 | goto cleanup; |
| 4445 | } |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4446 | |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4447 | } else { |
| 4448 | /* list key value */ |
| 4449 | if ((*node)->schema->nodetype != LYS_LIST) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4450 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier expects list, but have %s \"%s\".", |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4451 | strnodetype((*node)->schema->nodetype), (*node)->schema->name); |
| 4452 | parsed = -1; |
| 4453 | goto cleanup; |
| 4454 | } |
| 4455 | slist = (struct lys_node_list *)(*node)->schema; |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4456 | |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4457 | /* prepare key array */ |
| 4458 | if (!list_keys) { |
| 4459 | list_keys = malloc(slist->keys_size * sizeof *list_keys); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4460 | LY_CHECK_ERR_RETURN(!list_keys, LOGMEM(ctx), -1); |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4461 | for (i = 0; i < slist->keys_size; ++i) { |
| 4462 | list_keys[i] = slist->keys[i]; |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4463 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4464 | } |
| 4465 | |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4466 | /* find the schema key leaf */ |
| 4467 | for (i = 0; i < slist->keys_size; ++i) { |
| 4468 | if (list_keys[i] && !strncmp(list_keys[i]->name, name, nam_len) && !list_keys[i]->name[nam_len]) { |
| 4469 | break; |
| 4470 | } |
| 4471 | } |
| 4472 | if (i == slist->keys_size) { |
| 4473 | /* this list has no such key */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4474 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier expects list with the key \"%.*s\"," |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4475 | " but list \"%s\" does not define it.", nam_len, name, slist->name); |
| 4476 | parsed = -1; |
| 4477 | goto cleanup; |
| 4478 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4479 | |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4480 | /* check module */ |
| 4481 | if (model) { |
| 4482 | if (strncmp(list_keys[i]->module->name, model, mod_len) || list_keys[i]->module->name[mod_len]) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4483 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier expects key \"%s\" from module \"%.*s\", not \"%s\".", |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4484 | list_keys[i]->name, model, mod_len, list_keys[i]->module->name); |
| 4485 | parsed = -1; |
| 4486 | goto cleanup; |
| 4487 | } |
| 4488 | } else { |
| 4489 | if (list_keys[i]->module != prev_mod) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4490 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier expects key \"%s\" from module \"%s\", not \"%s\".", |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4491 | list_keys[i]->name, prev_mod->name, list_keys[i]->module->name); |
| 4492 | parsed = -1; |
| 4493 | goto cleanup; |
| 4494 | } |
| 4495 | } |
| 4496 | |
| 4497 | /* find the actual data key */ |
| 4498 | for (key = (struct lyd_node_leaf_list *)(*node)->child; key; key = (struct lyd_node_leaf_list *)key->next) { |
| 4499 | if (key->schema == (struct lys_node *)list_keys[i]) { |
| 4500 | break; |
| 4501 | } |
| 4502 | } |
| 4503 | if (!key) { |
| 4504 | /* list instance is missing a key? definitely should not happen */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4505 | LOGINT(ctx); |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4506 | parsed = -1; |
| 4507 | goto cleanup; |
| 4508 | } |
| 4509 | |
| 4510 | /* check the value */ |
Michal Vasko | 718ecdd | 2017-10-03 14:12:39 +0200 | [diff] [blame] | 4511 | if (!valequal(key->schema, value, val_len, key->value_str)) { |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4512 | *node = NULL; |
Michal Vasko | 88850b7 | 2017-10-02 13:13:21 +0200 | [diff] [blame] | 4513 | /* we still want to parse the whole predicate */ |
| 4514 | continue; |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4515 | } |
| 4516 | |
| 4517 | /* everything is fine, mark this key as resolved */ |
| 4518 | list_keys[i] = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4519 | } |
| 4520 | } while (has_predicate); |
| 4521 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4522 | /* check that all list keys were specified */ |
Michal Vasko | 88850b7 | 2017-10-02 13:13:21 +0200 | [diff] [blame] | 4523 | if (*node && list_keys) { |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4524 | for (i = 0; i < slist->keys_size; ++i) { |
| 4525 | if (list_keys[i]) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4526 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier is missing list key \"%s\".", list_keys[i]->name); |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4527 | parsed = -1; |
| 4528 | goto cleanup; |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4529 | } |
| 4530 | } |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4531 | } |
| 4532 | |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4533 | cleanup: |
| 4534 | free(list_keys); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4535 | return parsed; |
| 4536 | } |
| 4537 | |
Michal Vasko | 895c11f | 2018-03-12 11:35:58 +0100 | [diff] [blame] | 4538 | static int |
| 4539 | check_xpath(struct lys_node *node, int check_place) |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4540 | { |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 4541 | struct lys_node *parent; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4542 | struct lyxp_set set; |
Michal Vasko | 895c11f | 2018-03-12 11:35:58 +0100 | [diff] [blame] | 4543 | enum int_log_opts prev_ilo; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4544 | |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4545 | if (check_place) { |
| 4546 | parent = node; |
| 4547 | while (parent) { |
| 4548 | if (parent->nodetype == LYS_GROUPING) { |
| 4549 | /* unresolved grouping, skip for now (will be checked later) */ |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4550 | return EXIT_SUCCESS; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4551 | } |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4552 | if (parent->nodetype == LYS_AUGMENT) { |
| 4553 | if (!((struct lys_node_augment *)parent)->target) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4554 | /* unresolved augment, skip for now (will be checked later) */ |
| 4555 | return EXIT_FAILURE; |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4556 | } else { |
| 4557 | parent = ((struct lys_node_augment *)parent)->target; |
| 4558 | continue; |
| 4559 | } |
| 4560 | } |
| 4561 | parent = parent->parent; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4562 | } |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4563 | } |
| 4564 | |
Michal Vasko | 895c11f | 2018-03-12 11:35:58 +0100 | [diff] [blame] | 4565 | memset(&set, 0, sizeof set); |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4566 | |
Michal Vasko | 895c11f | 2018-03-12 11:35:58 +0100 | [diff] [blame] | 4567 | /* produce just warnings */ |
| 4568 | ly_ilo_change(NULL, ILO_ERR2WRN, &prev_ilo, NULL); |
| 4569 | lyxp_node_atomize(node, &set, 1); |
| 4570 | ly_ilo_restore(NULL, prev_ilo, NULL, 0); |
| 4571 | |
| 4572 | if (set.val.snodes) { |
| 4573 | free(set.val.snodes); |
| 4574 | } |
| 4575 | return EXIT_SUCCESS; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4576 | } |
| 4577 | |
Radek Krejci | f71f48f | 2016-10-25 16:37:24 +0200 | [diff] [blame] | 4578 | static int |
| 4579 | check_leafref_config(struct lys_node_leaf *leaf, struct lys_type *type) |
| 4580 | { |
Radek Krejci | dce5f97 | 2017-09-12 15:47:49 +0200 | [diff] [blame] | 4581 | unsigned int i; |
Radek Krejci | f71f48f | 2016-10-25 16:37:24 +0200 | [diff] [blame] | 4582 | |
| 4583 | if (type->base == LY_TYPE_LEAFREF) { |
Radek Krejci | c688ca0 | 2017-03-20 12:54:39 +0100 | [diff] [blame] | 4584 | if ((leaf->flags & LYS_CONFIG_W) && type->info.lref.target && type->info.lref.req != -1 && |
| 4585 | (type->info.lref.target->flags & LYS_CONFIG_R)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4586 | LOGVAL(leaf->module->ctx, LYE_SPEC, LY_VLOG_LYS, leaf, "The leafref %s is config but refers to a non-config %s.", |
Radek Krejci | f71f48f | 2016-10-25 16:37:24 +0200 | [diff] [blame] | 4587 | strnodetype(leaf->nodetype), strnodetype(type->info.lref.target->nodetype)); |
| 4588 | return -1; |
| 4589 | } |
| 4590 | /* we can skip the test in case the leafref is not yet resolved. In that case the test is done in the time |
| 4591 | * of leafref resolving (lys_leaf_add_leafref_target()) */ |
| 4592 | } else if (type->base == LY_TYPE_UNION) { |
| 4593 | for (i = 0; i < type->info.uni.count; i++) { |
| 4594 | if (check_leafref_config(leaf, &type->info.uni.types[i])) { |
| 4595 | return -1; |
| 4596 | } |
| 4597 | } |
| 4598 | } |
| 4599 | return 0; |
| 4600 | } |
| 4601 | |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4602 | /** |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4603 | * @brief Passes config flag down to children, skips nodes without config flags. |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4604 | * Logs. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4605 | * |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4606 | * @param[in] node Siblings and their children to have flags changed. |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4607 | * @param[in] clear Flag to clear all config flags if parent is LYS_NOTIF, LYS_INPUT, LYS_OUTPUT, LYS_RPC. |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4608 | * @param[in] flags Flags to assign to all the nodes. |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 4609 | * @param[in,out] unres List of unresolved items. |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4610 | * |
| 4611 | * @return 0 on success, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4612 | */ |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4613 | int |
| 4614 | inherit_config_flag(struct lys_node *node, int flags, int clear) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4615 | { |
Radek Krejci | f71f48f | 2016-10-25 16:37:24 +0200 | [diff] [blame] | 4616 | struct lys_node_leaf *leaf; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4617 | struct ly_ctx *ctx; |
| 4618 | |
| 4619 | if (!node) { |
| 4620 | return 0; |
| 4621 | } |
Radek Krejci | f71f48f | 2016-10-25 16:37:24 +0200 | [diff] [blame] | 4622 | |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4623 | assert(!(flags ^ (flags & LYS_CONFIG_MASK))); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4624 | ctx = node->module->ctx; |
| 4625 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4626 | LY_TREE_FOR(node, node) { |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4627 | if (clear) { |
| 4628 | node->flags &= ~LYS_CONFIG_MASK; |
Michal Vasko | c2a8d36 | 2016-09-29 08:50:13 +0200 | [diff] [blame] | 4629 | node->flags &= ~LYS_CONFIG_SET; |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4630 | } else { |
| 4631 | if (node->flags & LYS_CONFIG_SET) { |
| 4632 | /* skip nodes with an explicit config value */ |
| 4633 | if ((flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4634 | LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, node, "true", "config"); |
| 4635 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "State nodes cannot have configuration nodes as children."); |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4636 | return -1; |
| 4637 | } |
| 4638 | continue; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4639 | } |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4640 | |
| 4641 | if (!(node->nodetype & (LYS_USES | LYS_GROUPING))) { |
| 4642 | node->flags = (node->flags & ~LYS_CONFIG_MASK) | flags; |
| 4643 | /* check that configuration lists have keys */ |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4644 | if ((node->nodetype == LYS_LIST) && (node->flags & LYS_CONFIG_W) |
| 4645 | && !((struct lys_node_list *)node)->keys_size) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4646 | LOGVAL(ctx, LYE_MISSCHILDSTMT, LY_VLOG_LYS, node, "key", "list"); |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4647 | return -1; |
| 4648 | } |
| 4649 | } |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4650 | } |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4651 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4652 | if (inherit_config_flag(node->child, flags, clear)) { |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4653 | return -1; |
| 4654 | } |
Radek Krejci | f71f48f | 2016-10-25 16:37:24 +0200 | [diff] [blame] | 4655 | } else if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 4656 | leaf = (struct lys_node_leaf *)node; |
| 4657 | if (check_leafref_config(leaf, &leaf->type)) { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 4658 | return -1; |
| 4659 | } |
| 4660 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4661 | } |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4662 | |
| 4663 | return 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4664 | } |
| 4665 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4666 | /** |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 4667 | * @brief Resolve augment target. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4668 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4669 | * @param[in] aug Augment to use. |
Michal Vasko | 9723426 | 2018-02-01 09:53:01 +0100 | [diff] [blame] | 4670 | * @param[in] uses Parent where to start the search in. If set, uses augment, if not, standalone augment. |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 4671 | * @param[in,out] unres List of unresolved items. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4672 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4673 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4674 | */ |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 4675 | static int |
Michal Vasko | 9723426 | 2018-02-01 09:53:01 +0100 | [diff] [blame] | 4676 | resolve_augment(struct lys_node_augment *aug, struct lys_node *uses, struct unres_schema *unres) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4677 | { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4678 | int rc; |
Radek Krejci | 63495df | 2019-10-10 09:06:27 +0200 | [diff] [blame] | 4679 | struct lys_node *sub, *next; |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4680 | struct lys_module *mod; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4681 | struct ly_set *set; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4682 | struct ly_ctx *ctx; |
Radek Krejci | 63495df | 2019-10-10 09:06:27 +0200 | [diff] [blame] | 4683 | struct lys_node_case *c; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4684 | |
Michal Vasko | 2ef7db6 | 2017-06-12 09:24:02 +0200 | [diff] [blame] | 4685 | assert(aug); |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 4686 | mod = lys_main_module(aug->module); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4687 | ctx = mod->ctx; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4688 | |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 4689 | /* set it as not applied for now */ |
| 4690 | aug->flags |= LYS_NOTAPPLIED; |
| 4691 | |
Michal Vasko | 2ef7db6 | 2017-06-12 09:24:02 +0200 | [diff] [blame] | 4692 | /* it can already be resolved in case we returned EXIT_FAILURE from if block below */ |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4693 | if (!aug->target) { |
Michal Vasko | 2ef7db6 | 2017-06-12 09:24:02 +0200 | [diff] [blame] | 4694 | /* resolve target node */ |
Michal Vasko | 9723426 | 2018-02-01 09:53:01 +0100 | [diff] [blame] | 4695 | rc = resolve_schema_nodeid(aug->target_name, uses, (uses ? NULL : lys_node_module((struct lys_node *)aug)), &set, 0, 0); |
Michal Vasko | 2ef7db6 | 2017-06-12 09:24:02 +0200 | [diff] [blame] | 4696 | if (rc == -1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4697 | LOGVAL(ctx, LYE_PATH, LY_VLOG_LYS, aug); |
Michal Vasko | 2ef7db6 | 2017-06-12 09:24:02 +0200 | [diff] [blame] | 4698 | return -1; |
| 4699 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4700 | if (!set) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4701 | LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_LYS, aug, "augment", aug->target_name); |
Michal Vasko | 2ef7db6 | 2017-06-12 09:24:02 +0200 | [diff] [blame] | 4702 | return EXIT_FAILURE; |
| 4703 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4704 | aug->target = set->set.s[0]; |
| 4705 | ly_set_free(set); |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4706 | } |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4707 | |
Michal Vasko | 74083ec | 2018-06-15 10:00:12 +0200 | [diff] [blame] | 4708 | /* make this module implemented if the target module is (if the target is in an unimplemented module, |
| 4709 | * it is fine because when we will be making that module implemented, its augment will be applied |
| 4710 | * and that augment target module made implemented, recursively) */ |
| 4711 | if (mod->implemented && !lys_node_module(aug->target)->implemented) { |
| 4712 | lys_node_module(aug->target)->implemented = 1; |
| 4713 | if (unres_schema_add_node(lys_node_module(aug->target), unres, NULL, UNRES_MOD_IMPLEMENT, NULL) == -1) { |
| 4714 | return -1; |
| 4715 | } |
| 4716 | } |
| 4717 | |
Michal Vasko | d58d596 | 2016-03-02 14:29:41 +0100 | [diff] [blame] | 4718 | /* check for mandatory nodes - if the target node is in another module |
| 4719 | * the added nodes cannot be mandatory |
| 4720 | */ |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4721 | if (!aug->parent && (lys_node_module((struct lys_node *)aug) != lys_node_module(aug->target)) |
| 4722 | && (rc = lyp_check_mandatory_augment(aug, aug->target))) { |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 4723 | return rc; |
Michal Vasko | d58d596 | 2016-03-02 14:29:41 +0100 | [diff] [blame] | 4724 | } |
| 4725 | |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4726 | /* check augment target type and then augment nodes type */ |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4727 | if (aug->target->nodetype & (LYS_CONTAINER | LYS_LIST)) { |
Michal Vasko | db01726 | 2017-01-24 13:10:04 +0100 | [diff] [blame] | 4728 | LY_TREE_FOR(aug->child, sub) { |
| 4729 | if (!(sub->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_LEAF | LYS_LIST | LYS_LEAFLIST | LYS_USES |
| 4730 | | LYS_CHOICE | LYS_ACTION | LYS_NOTIF))) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4731 | LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment"); |
| 4732 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Cannot augment \"%s\" with a \"%s\".", |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4733 | strnodetype(aug->target->nodetype), strnodetype(sub->nodetype)); |
Michal Vasko | db01726 | 2017-01-24 13:10:04 +0100 | [diff] [blame] | 4734 | return -1; |
| 4735 | } |
| 4736 | } |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4737 | } else if (aug->target->nodetype & (LYS_CASE | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF)) { |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4738 | LY_TREE_FOR(aug->child, sub) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4739 | if (!(sub->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_LEAF | LYS_LIST | LYS_LEAFLIST | LYS_USES | LYS_CHOICE))) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4740 | LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment"); |
| 4741 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Cannot augment \"%s\" with a \"%s\".", |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4742 | strnodetype(aug->target->nodetype), strnodetype(sub->nodetype)); |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4743 | return -1; |
| 4744 | } |
| 4745 | } |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4746 | } else if (aug->target->nodetype == LYS_CHOICE) { |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4747 | LY_TREE_FOR(aug->child, sub) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4748 | if (!(sub->nodetype & (LYS_CASE | LYS_ANYDATA | LYS_CONTAINER | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4749 | LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment"); |
| 4750 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Cannot augment \"%s\" with a \"%s\".", |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4751 | strnodetype(aug->target->nodetype), strnodetype(sub->nodetype)); |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4752 | return -1; |
| 4753 | } |
| 4754 | } |
| 4755 | } else { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4756 | LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, aug, aug->target_name, "target-node"); |
| 4757 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Invalid augment target node type \"%s\".", strnodetype(aug->target->nodetype)); |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4758 | return -1; |
| 4759 | } |
| 4760 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 4761 | /* check identifier uniqueness as in lys_node_addchild() */ |
Michal Vasko | 1d87a92 | 2015-08-21 12:57:16 +0200 | [diff] [blame] | 4762 | LY_TREE_FOR(aug->child, sub) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4763 | if (lys_check_id(sub, aug->target, NULL)) { |
Michal Vasko | 3e6665f | 2015-08-17 14:00:38 +0200 | [diff] [blame] | 4764 | return -1; |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 4765 | } |
| 4766 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4767 | |
Radek Krejci | 63495df | 2019-10-10 09:06:27 +0200 | [diff] [blame] | 4768 | if (aug->target->nodetype == LYS_CHOICE) { |
| 4769 | /* in the case of implicit cases, we have to create the nodes representing them |
| 4770 | * to serve as a target of the augments, they won't be printed, but they are expected in the tree */ |
| 4771 | LY_TREE_FOR_SAFE(aug->child, next, sub) { |
| 4772 | if (sub->nodetype == LYS_CASE) { |
| 4773 | /* explicit case */ |
| 4774 | continue; |
| 4775 | } |
| 4776 | |
| 4777 | c = calloc(1, sizeof *c); |
| 4778 | LY_CHECK_ERR_RETURN(!c, LOGMEM(ctx), EXIT_FAILURE); |
| 4779 | c->name = lydict_insert(ctx, sub->name, 0); |
| 4780 | c->flags = LYS_IMPLICIT | (sub->flags & LYS_CONFIG_MASK); |
| 4781 | c->module = sub->module; |
| 4782 | c->nodetype = LYS_CASE; |
| 4783 | c->parent = sub->parent; |
| 4784 | c->prev = sub->prev != sub ? sub->prev : (struct lys_node*)c; |
| 4785 | if (c->prev->next) { |
| 4786 | c->prev->next = (struct lys_node*)c; |
| 4787 | } else { |
| 4788 | c->parent->child = (struct lys_node*)c; |
| 4789 | } |
| 4790 | c->next = sub->next; |
| 4791 | if (c->next) { |
| 4792 | c->next->prev = (struct lys_node*)c; |
| 4793 | } else { |
| 4794 | c->parent->child->prev = (struct lys_node*)c; |
| 4795 | } |
| 4796 | c->child = sub; |
| 4797 | sub->prev = sub; |
| 4798 | sub->next = NULL; |
| 4799 | sub->parent = (struct lys_node*)c; |
| 4800 | } |
| 4801 | } |
| 4802 | |
Michal Vasko | 8fa6ec2 | 2020-03-05 15:29:26 +0100 | [diff] [blame] | 4803 | LY_TREE_DFS_BEGIN(aug->child, next, sub) { |
| 4804 | if (sub->nodetype == LYS_ACTION) { |
| 4805 | /* we need to check parents */ |
| 4806 | for (sub = aug->target; sub; sub = lys_parent(sub)) { |
| 4807 | if ((sub->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) |
| 4808 | || ((sub->nodetype == LYS_LIST) && !((struct lys_node_list *)sub)->keys)) { |
| 4809 | LOGVAL(ctx, LYE_INPAR, LY_VLOG_LYS, aug->target, strnodetype(sub->nodetype), "action"); |
| 4810 | LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_LYS, aug, "augment", aug->target_name); |
| 4811 | return -1; |
| 4812 | } |
| 4813 | } |
| 4814 | break; |
| 4815 | } |
| 4816 | LY_TREE_DFS_END(aug->child, next, sub); |
| 4817 | } |
| 4818 | |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4819 | if (!aug->child) { |
| 4820 | /* empty augment, nothing to connect, but it is techincally applied */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4821 | LOGWRN(ctx, "Augment \"%s\" without children.", aug->target_name); |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4822 | aug->flags &= ~LYS_NOTAPPLIED; |
Radek Krejci | aa6b2a1 | 2017-10-26 15:52:39 +0200 | [diff] [blame] | 4823 | } else if ((aug->parent || mod->implemented) && apply_aug(aug, unres)) { |
| 4824 | /* we try to connect the augment only in case the module is implemented or |
| 4825 | * the augment applies on the used grouping, anyway we failed here */ |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4826 | return -1; |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4827 | } |
| 4828 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4829 | return EXIT_SUCCESS; |
| 4830 | } |
| 4831 | |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4832 | static int |
Radek Krejci | a7db970 | 2017-01-20 12:55:14 +0100 | [diff] [blame] | 4833 | resolve_extension(struct unres_ext *info, struct lys_ext_instance **ext, struct unres_schema *unres) |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4834 | { |
| 4835 | enum LY_VLOG_ELEM vlog_type; |
| 4836 | void *vlog_node; |
| 4837 | unsigned int i, j; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4838 | struct lys_ext *e; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4839 | char *ext_name, *ext_prefix, *tmp; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4840 | struct lyxml_elem *next_yin, *yin; |
Radek Krejci | a7db970 | 2017-01-20 12:55:14 +0100 | [diff] [blame] | 4841 | const struct lys_module *mod; |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4842 | struct lys_ext_instance *tmp_ext; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4843 | struct ly_ctx *ctx = NULL; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4844 | LYEXT_TYPE etype; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4845 | |
| 4846 | switch (info->parent_type) { |
Radek Krejci | 0aa821a | 2016-12-08 11:21:35 +0100 | [diff] [blame] | 4847 | case LYEXT_PAR_NODE: |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4848 | vlog_node = info->parent; |
| 4849 | vlog_type = LY_VLOG_LYS; |
| 4850 | break; |
Radek Krejci | 0aa821a | 2016-12-08 11:21:35 +0100 | [diff] [blame] | 4851 | case LYEXT_PAR_MODULE: |
| 4852 | case LYEXT_PAR_IMPORT: |
| 4853 | case LYEXT_PAR_INCLUDE: |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4854 | vlog_node = NULL; |
| 4855 | vlog_type = LY_VLOG_LYS; |
| 4856 | break; |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 4857 | default: |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4858 | vlog_node = NULL; |
Radek Krejci | 6a7fedf | 2017-02-10 12:38:06 +0100 | [diff] [blame] | 4859 | vlog_type = LY_VLOG_NONE; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4860 | break; |
| 4861 | } |
| 4862 | |
| 4863 | if (info->datatype == LYS_IN_YIN) { |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4864 | /* YIN */ |
| 4865 | |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4866 | /* get the module where the extension is supposed to be defined */ |
Michal Vasko | 921eb6b | 2017-10-13 10:01:39 +0200 | [diff] [blame] | 4867 | mod = lyp_get_import_module_ns(info->mod, info->data.yin->ns->value); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4868 | if (!mod) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4869 | LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, info->data.yin->name); |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 4870 | return EXIT_FAILURE; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4871 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4872 | ctx = mod->ctx; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4873 | |
| 4874 | /* find the extension definition */ |
| 4875 | e = NULL; |
| 4876 | for (i = 0; i < mod->extensions_size; i++) { |
| 4877 | if (ly_strequal(mod->extensions[i].name, info->data.yin->name, 1)) { |
| 4878 | e = &mod->extensions[i]; |
| 4879 | break; |
| 4880 | } |
| 4881 | } |
| 4882 | /* try submodules */ |
| 4883 | for (j = 0; !e && j < mod->inc_size; j++) { |
| 4884 | for (i = 0; i < mod->inc[j].submodule->extensions_size; i++) { |
| 4885 | if (ly_strequal(mod->inc[j].submodule->extensions[i].name, info->data.yin->name, 1)) { |
| 4886 | e = &mod->inc[j].submodule->extensions[i]; |
| 4887 | break; |
| 4888 | } |
| 4889 | } |
| 4890 | } |
| 4891 | if (!e) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4892 | LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, info->data.yin->name); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4893 | return EXIT_FAILURE; |
| 4894 | } |
| 4895 | |
| 4896 | /* we have the extension definition, so now it cannot be forward referenced and error is always fatal */ |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4897 | |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4898 | if (e->plugin && e->plugin->check_position) { |
| 4899 | /* common part - we have plugin with position checking function, use it first */ |
| 4900 | if ((*e->plugin->check_position)(info->parent, info->parent_type, info->substmt)) { |
| 4901 | /* extension is not allowed here */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4902 | LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, e->name); |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4903 | return -1; |
| 4904 | } |
| 4905 | } |
| 4906 | |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4907 | /* extension type-specific part - allocation */ |
| 4908 | if (e->plugin) { |
| 4909 | etype = e->plugin->type; |
| 4910 | } else { |
| 4911 | /* default type */ |
| 4912 | etype = LYEXT_FLAG; |
| 4913 | } |
| 4914 | switch (etype) { |
| 4915 | case LYEXT_FLAG: |
| 4916 | (*ext) = calloc(1, sizeof(struct lys_ext_instance)); |
| 4917 | break; |
| 4918 | case LYEXT_COMPLEX: |
| 4919 | (*ext) = calloc(1, ((struct lyext_plugin_complex*)e->plugin)->instance_size); |
| 4920 | break; |
| 4921 | case LYEXT_ERR: |
| 4922 | /* we never should be here */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4923 | LOGINT(ctx); |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4924 | return -1; |
| 4925 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4926 | LY_CHECK_ERR_RETURN(!*ext, LOGMEM(ctx), -1); |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4927 | |
| 4928 | /* common part for all extension types */ |
| 4929 | (*ext)->def = e; |
| 4930 | (*ext)->parent = info->parent; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4931 | (*ext)->parent_type = info->parent_type; |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 4932 | (*ext)->insubstmt = info->substmt; |
| 4933 | (*ext)->insubstmt_index = info->substmt_index; |
Radek Krejci | 8de8f61 | 2017-02-16 15:03:32 +0100 | [diff] [blame] | 4934 | (*ext)->ext_type = e->plugin ? e->plugin->type : LYEXT_FLAG; |
PavolVican | 92f2362 | 2017-12-12 13:35:56 +0100 | [diff] [blame] | 4935 | (*ext)->flags |= e->plugin ? e->plugin->flags : 0; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4936 | |
PavolVican | d86b0d6 | 2017-09-01 11:01:39 +0200 | [diff] [blame] | 4937 | if (e->argument) { |
| 4938 | if (!(e->flags & LYS_YINELEM)) { |
| 4939 | (*ext)->arg_value = lyxml_get_attr(info->data.yin, e->argument, NULL); |
| 4940 | if (!(*ext)->arg_value) { |
PavolVican | e7a1fc6 | 2018-02-19 16:50:27 +0100 | [diff] [blame] | 4941 | LOGVAL(ctx, LYE_MISSARG, LY_VLOG_NONE, NULL, e->argument, info->data.yin->name); |
PavolVican | d86b0d6 | 2017-09-01 11:01:39 +0200 | [diff] [blame] | 4942 | return -1; |
| 4943 | } |
| 4944 | |
| 4945 | (*ext)->arg_value = lydict_insert(mod->ctx, (*ext)->arg_value, 0); |
| 4946 | } else { |
| 4947 | LY_TREE_FOR_SAFE(info->data.yin->child, next_yin, yin) { |
| 4948 | if (ly_strequal(yin->name, e->argument, 1)) { |
| 4949 | (*ext)->arg_value = lydict_insert(mod->ctx, yin->content, 0); |
| 4950 | lyxml_free(mod->ctx, yin); |
| 4951 | break; |
| 4952 | } |
| 4953 | } |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4954 | } |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4955 | } |
| 4956 | |
PavolVican | 92f2362 | 2017-12-12 13:35:56 +0100 | [diff] [blame] | 4957 | if ((*ext)->flags & LYEXT_OPT_VALID && |
| 4958 | (info->parent_type == LYEXT_PAR_NODE || info->parent_type == LYEXT_PAR_TPDF)) { |
Michal Vasko | 1bdfd43 | 2018-03-09 09:30:19 +0100 | [diff] [blame] | 4959 | ((struct lys_node *)info->parent)->flags |= LYS_VALID_EXT; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4960 | } |
| 4961 | |
Radek Krejci | 7f1d47e | 2017-04-12 15:29:02 +0200 | [diff] [blame] | 4962 | (*ext)->nodetype = LYS_EXT; |
| 4963 | (*ext)->module = info->mod; |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 4964 | |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4965 | /* extension type-specific part - parsing content */ |
| 4966 | switch (etype) { |
| 4967 | case LYEXT_FLAG: |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4968 | LY_TREE_FOR_SAFE(info->data.yin->child, next_yin, yin) { |
| 4969 | if (!yin->ns) { |
| 4970 | /* garbage */ |
| 4971 | lyxml_free(mod->ctx, yin); |
| 4972 | continue; |
| 4973 | } else if (!strcmp(yin->ns->value, LY_NSYIN)) { |
| 4974 | /* standard YANG statements are not expected here */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4975 | LOGVAL(ctx, LYE_INCHILDSTMT, vlog_type, vlog_node, yin->name, info->data.yin->name); |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4976 | return -1; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4977 | } else if (yin->ns == info->data.yin->ns && |
| 4978 | (e->flags & LYS_YINELEM) && ly_strequal(yin->name, e->argument, 1)) { |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4979 | /* we have the extension's argument */ |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4980 | if ((*ext)->arg_value) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4981 | LOGVAL(ctx, LYE_TOOMANY, vlog_type, vlog_node, yin->name, info->data.yin->name); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4982 | return -1; |
| 4983 | } |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4984 | (*ext)->arg_value = yin->content; |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4985 | yin->content = NULL; |
| 4986 | lyxml_free(mod->ctx, yin); |
| 4987 | } else { |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4988 | /* extension instance */ |
| 4989 | if (lyp_yin_parse_subnode_ext(info->mod, *ext, LYEXT_PAR_EXTINST, yin, |
| 4990 | LYEXT_SUBSTMT_SELF, 0, unres)) { |
| 4991 | return -1; |
| 4992 | } |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4993 | |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4994 | continue; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4995 | } |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4996 | } |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4997 | break; |
| 4998 | case LYEXT_COMPLEX: |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 4999 | ((struct lys_ext_instance_complex*)(*ext))->substmt = ((struct lyext_plugin_complex*)e->plugin)->substmt; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 5000 | if (lyp_yin_parse_complex_ext(info->mod, (struct lys_ext_instance_complex*)(*ext), info->data.yin, unres)) { |
| 5001 | /* TODO memory cleanup */ |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 5002 | return -1; |
| 5003 | } |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 5004 | break; |
| 5005 | default: |
| 5006 | break; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 5007 | } |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 5008 | |
| 5009 | /* TODO - lyext_check_result_clb, other than LYEXT_FLAG plugins */ |
| 5010 | |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 5011 | } else { |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 5012 | /* YANG */ |
| 5013 | |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 5014 | ext_prefix = (char *)(*ext)->def; |
| 5015 | tmp = strchr(ext_prefix, ':'); |
| 5016 | if (!tmp) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5017 | LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, ext_prefix); |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 5018 | goto error; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 5019 | } |
| 5020 | ext_name = tmp + 1; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 5021 | |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 5022 | /* get the module where the extension is supposed to be defined */ |
Michal Vasko | 921eb6b | 2017-10-13 10:01:39 +0200 | [diff] [blame] | 5023 | mod = lyp_get_module(info->mod, ext_prefix, tmp - ext_prefix, NULL, 0, 0); |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 5024 | if (!mod) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5025 | LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, ext_prefix); |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 5026 | return EXIT_FAILURE; |
| 5027 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5028 | ctx = mod->ctx; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 5029 | |
| 5030 | /* find the extension definition */ |
| 5031 | e = NULL; |
| 5032 | for (i = 0; i < mod->extensions_size; i++) { |
| 5033 | if (ly_strequal(mod->extensions[i].name, ext_name, 0)) { |
| 5034 | e = &mod->extensions[i]; |
| 5035 | break; |
| 5036 | } |
| 5037 | } |
| 5038 | /* try submodules */ |
| 5039 | for (j = 0; !e && j < mod->inc_size; j++) { |
| 5040 | for (i = 0; i < mod->inc[j].submodule->extensions_size; i++) { |
| 5041 | if (ly_strequal(mod->inc[j].submodule->extensions[i].name, ext_name, 0)) { |
| 5042 | e = &mod->inc[j].submodule->extensions[i]; |
| 5043 | break; |
| 5044 | } |
| 5045 | } |
| 5046 | } |
| 5047 | if (!e) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5048 | LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, ext_prefix); |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 5049 | return EXIT_FAILURE; |
| 5050 | } |
| 5051 | |
PavolVican | fcc9876 | 2017-09-01 15:51:39 +0200 | [diff] [blame] | 5052 | (*ext)->flags &= ~LYEXT_OPT_YANG; |
| 5053 | (*ext)->def = NULL; |
| 5054 | |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 5055 | /* we have the extension definition, so now it cannot be forward referenced and error is always fatal */ |
| 5056 | |
| 5057 | if (e->plugin && e->plugin->check_position) { |
| 5058 | /* common part - we have plugin with position checking function, use it first */ |
| 5059 | if ((*e->plugin->check_position)(info->parent, info->parent_type, info->substmt)) { |
| 5060 | /* extension is not allowed here */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5061 | LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, e->name); |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 5062 | goto error; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 5063 | } |
| 5064 | } |
| 5065 | |
Michal Vasko | 3676bfb | 2019-03-12 08:51:54 +0100 | [diff] [blame] | 5066 | if (e->argument && !(*ext)->arg_value) { |
| 5067 | LOGVAL(ctx, LYE_MISSARG, LY_VLOG_NONE, NULL, e->argument, ext_name); |
| 5068 | goto error; |
| 5069 | } |
| 5070 | |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 5071 | /* extension common part */ |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 5072 | (*ext)->def = e; |
| 5073 | (*ext)->parent = info->parent; |
Radek Krejci | 8de8f61 | 2017-02-16 15:03:32 +0100 | [diff] [blame] | 5074 | (*ext)->ext_type = e->plugin ? e->plugin->type : LYEXT_FLAG; |
PavolVican | 92f2362 | 2017-12-12 13:35:56 +0100 | [diff] [blame] | 5075 | (*ext)->flags |= e->plugin ? e->plugin->flags : 0; |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 5076 | |
PavolVican | 92f2362 | 2017-12-12 13:35:56 +0100 | [diff] [blame] | 5077 | if ((*ext)->flags & LYEXT_OPT_VALID && |
| 5078 | (info->parent_type == LYEXT_PAR_NODE || info->parent_type == LYEXT_PAR_TPDF)) { |
Michal Vasko | 1bdfd43 | 2018-03-09 09:30:19 +0100 | [diff] [blame] | 5079 | ((struct lys_node *)info->parent)->flags |= LYS_VALID_EXT; |
PavolVican | 92f2362 | 2017-12-12 13:35:56 +0100 | [diff] [blame] | 5080 | } |
| 5081 | |
Radek Krejci | 7f1d47e | 2017-04-12 15:29:02 +0200 | [diff] [blame] | 5082 | (*ext)->module = info->mod; |
| 5083 | (*ext)->nodetype = LYS_EXT; |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 5084 | |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 5085 | /* extension type-specific part */ |
| 5086 | if (e->plugin) { |
| 5087 | etype = e->plugin->type; |
| 5088 | } else { |
| 5089 | /* default type */ |
| 5090 | etype = LYEXT_FLAG; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 5091 | } |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 5092 | switch (etype) { |
| 5093 | case LYEXT_FLAG: |
| 5094 | /* nothing change */ |
| 5095 | break; |
| 5096 | case LYEXT_COMPLEX: |
Radek Krejci | b728c9f | 2018-12-06 09:42:17 +0100 | [diff] [blame] | 5097 | tmp_ext = realloc(*ext, ((struct lyext_plugin_complex*)e->plugin)->instance_size); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5098 | LY_CHECK_ERR_GOTO(!tmp_ext, LOGMEM(ctx), error); |
Radek Krejci | b728c9f | 2018-12-06 09:42:17 +0100 | [diff] [blame] | 5099 | memset((char *)tmp_ext + offsetof(struct lys_ext_instance_complex, content), 0, |
| 5100 | ((struct lyext_plugin_complex*)e->plugin)->instance_size - offsetof(struct lys_ext_instance_complex, content)); |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 5101 | (*ext) = tmp_ext; |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 5102 | ((struct lys_ext_instance_complex*)(*ext))->substmt = ((struct lyext_plugin_complex*)e->plugin)->substmt; |
PavolVican | a1e291f | 2017-02-19 16:07:12 +0100 | [diff] [blame] | 5103 | if (info->data.yang) { |
| 5104 | *tmp = ':'; |
PavolVican | db0e817 | 2017-02-20 00:46:09 +0100 | [diff] [blame] | 5105 | if (yang_parse_ext_substatement(info->mod, unres, info->data.yang->ext_substmt, ext_prefix, |
| 5106 | (struct lys_ext_instance_complex*)(*ext))) { |
| 5107 | goto error; |
| 5108 | } |
| 5109 | if (yang_fill_extcomplex_module(info->mod->ctx, (struct lys_ext_instance_complex*)(*ext), ext_prefix, |
| 5110 | info->data.yang->ext_modules, info->mod->implemented)) { |
PavolVican | a1e291f | 2017-02-19 16:07:12 +0100 | [diff] [blame] | 5111 | goto error; |
| 5112 | } |
PavolVican | a387667 | 2017-02-21 15:49:51 +0100 | [diff] [blame] | 5113 | } |
| 5114 | if (lyp_mand_check_ext((struct lys_ext_instance_complex*)(*ext), ext_prefix)) { |
| 5115 | goto error; |
PavolVican | a1e291f | 2017-02-19 16:07:12 +0100 | [diff] [blame] | 5116 | } |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 5117 | break; |
| 5118 | case LYEXT_ERR: |
| 5119 | /* we never should be here */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5120 | LOGINT(ctx); |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 5121 | goto error; |
| 5122 | } |
| 5123 | |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 5124 | if (yang_check_ext_instance(info->mod, &(*ext)->ext, (*ext)->ext_size, *ext, unres)) { |
| 5125 | goto error; |
| 5126 | } |
| 5127 | free(ext_prefix); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 5128 | } |
| 5129 | |
| 5130 | return EXIT_SUCCESS; |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 5131 | error: |
| 5132 | free(ext_prefix); |
| 5133 | return -1; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 5134 | } |
| 5135 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5136 | /** |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5137 | * @brief Resolve (find) choice default case. Does not log. |
| 5138 | * |
| 5139 | * @param[in] choic Choice to use. |
| 5140 | * @param[in] dflt Name of the default case. |
| 5141 | * |
| 5142 | * @return Pointer to the default node or NULL. |
| 5143 | */ |
| 5144 | static struct lys_node * |
| 5145 | resolve_choice_dflt(struct lys_node_choice *choic, const char *dflt) |
| 5146 | { |
| 5147 | struct lys_node *child, *ret; |
| 5148 | |
| 5149 | LY_TREE_FOR(choic->child, child) { |
| 5150 | if (child->nodetype == LYS_USES) { |
| 5151 | ret = resolve_choice_dflt((struct lys_node_choice *)child, dflt); |
| 5152 | if (ret) { |
| 5153 | return ret; |
| 5154 | } |
| 5155 | } |
| 5156 | |
| 5157 | if (ly_strequal(child->name, dflt, 1) && (child->nodetype & (LYS_ANYDATA | LYS_CASE |
Radek Krejci | 2f792db | 2016-09-12 10:52:33 +0200 | [diff] [blame] | 5158 | | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CHOICE))) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5159 | return child; |
| 5160 | } |
| 5161 | } |
| 5162 | |
| 5163 | return NULL; |
| 5164 | } |
| 5165 | |
| 5166 | /** |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5167 | * @brief Resolve uses, apply augments, refines. Logs directly. |
| 5168 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5169 | * @param[in] uses Uses to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5170 | * @param[in,out] unres List of unresolved items. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5171 | * |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 5172 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5173 | */ |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 5174 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5175 | resolve_uses(struct lys_node_uses *uses, struct unres_schema *unres) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5176 | { |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 5177 | struct ly_ctx *ctx = uses->module->ctx; /* shortcut */ |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5178 | struct lys_node *node = NULL, *next, *iter, **refine_nodes = NULL; |
Pavol Vican | 55abd33 | 2016-07-12 15:54:49 +0200 | [diff] [blame] | 5179 | struct lys_node *node_aux, *parent, *tmp; |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 5180 | struct lys_node_leaflist *llist; |
| 5181 | struct lys_node_leaf *leaf; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 5182 | struct lys_refine *rfn; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 5183 | struct lys_restr *must, **old_must; |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 5184 | struct lys_iffeature *iff, **old_iff; |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 5185 | int i, j, k, rc; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 5186 | uint8_t size, *old_size; |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 5187 | unsigned int usize, usize1, usize2; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5188 | |
Michal Vasko | 71e1aa8 | 2015-08-12 12:17:51 +0200 | [diff] [blame] | 5189 | assert(uses->grp); |
Radek Krejci | 6ff885d | 2017-01-03 14:06:22 +0100 | [diff] [blame] | 5190 | |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 5191 | /* check that the grouping is resolved (no unresolved uses inside) */ |
| 5192 | assert(!uses->grp->unres_count); |
Michal Vasko | 71e1aa8 | 2015-08-12 12:17:51 +0200 | [diff] [blame] | 5193 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5194 | /* copy the data nodes from grouping into the uses context */ |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 5195 | LY_TREE_FOR(uses->grp->child, node_aux) { |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 5196 | if (node_aux->nodetype & LYS_GROUPING) { |
| 5197 | /* do not instantiate groupings from groupings */ |
| 5198 | continue; |
| 5199 | } |
Radek Krejci | 6ff885d | 2017-01-03 14:06:22 +0100 | [diff] [blame] | 5200 | node = lys_node_dup(uses->module, (struct lys_node *)uses, node_aux, unres, 0); |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 5201 | if (!node) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5202 | LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, uses, uses->grp->name, "uses"); |
| 5203 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Copying data from grouping failed."); |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 5204 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5205 | } |
Pavol Vican | 55abd33 | 2016-07-12 15:54:49 +0200 | [diff] [blame] | 5206 | /* test the name of siblings */ |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 5207 | LY_TREE_FOR((uses->parent) ? *lys_child(uses->parent, LYS_USES) : lys_main_module(uses->module)->data, tmp) { |
Pavol Vican | 2510ddc | 2016-07-18 16:23:44 +0200 | [diff] [blame] | 5208 | if (!(tmp->nodetype & (LYS_USES | LYS_GROUPING | LYS_CASE)) && ly_strequal(tmp->name, node_aux->name, 1)) { |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 5209 | goto fail; |
Pavol Vican | 55abd33 | 2016-07-12 15:54:49 +0200 | [diff] [blame] | 5210 | } |
| 5211 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5212 | } |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 5213 | |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 5214 | /* we managed to copy the grouping, the rest must be possible to resolve */ |
| 5215 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5216 | if (uses->refine_size) { |
| 5217 | refine_nodes = malloc(uses->refine_size * sizeof *refine_nodes); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5218 | LY_CHECK_ERR_GOTO(!refine_nodes, LOGMEM(ctx), fail); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5219 | } |
| 5220 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5221 | /* apply refines */ |
| 5222 | for (i = 0; i < uses->refine_size; i++) { |
| 5223 | rfn = &uses->refine[i]; |
Radek Krejci | e207741 | 2017-01-26 16:03:39 +0100 | [diff] [blame] | 5224 | rc = resolve_descendant_schema_nodeid(rfn->target_name, uses->child, |
| 5225 | LYS_NO_RPC_NOTIF_NODE | LYS_ACTION | LYS_NOTIF, |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 5226 | 0, (const struct lys_node **)&node); |
Michal Vasko | 9bb061b | 2016-02-12 11:00:19 +0100 | [diff] [blame] | 5227 | if (rc || !node) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5228 | LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine"); |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 5229 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5230 | } |
| 5231 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 5232 | if (rfn->target_type && !(node->nodetype & rfn->target_type)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5233 | LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine"); |
| 5234 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Refine substatements not applicable to the target-node."); |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 5235 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5236 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5237 | refine_nodes[i] = node; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5238 | |
| 5239 | /* description on any nodetype */ |
| 5240 | if (rfn->dsc) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 5241 | lydict_remove(ctx, node->dsc); |
| 5242 | node->dsc = lydict_insert(ctx, rfn->dsc, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5243 | } |
| 5244 | |
| 5245 | /* reference on any nodetype */ |
| 5246 | if (rfn->ref) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 5247 | lydict_remove(ctx, node->ref); |
| 5248 | node->ref = lydict_insert(ctx, rfn->ref, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5249 | } |
| 5250 | |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 5251 | /* config on any nodetype, |
| 5252 | * in case of notification or rpc/action, the config is not applicable (there is no config status) */ |
| 5253 | if ((rfn->flags & LYS_CONFIG_MASK) && (node->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 5254 | node->flags &= ~LYS_CONFIG_MASK; |
| 5255 | node->flags |= (rfn->flags & LYS_CONFIG_MASK); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5256 | } |
| 5257 | |
| 5258 | /* default value ... */ |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 5259 | if (rfn->dflt_size) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 5260 | if (node->nodetype == LYS_LEAF) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5261 | /* leaf */ |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 5262 | leaf = (struct lys_node_leaf *)node; |
| 5263 | |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 5264 | /* replace default value */ |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 5265 | lydict_remove(ctx, leaf->dflt); |
| 5266 | leaf->dflt = lydict_insert(ctx, rfn->dflt[0], 0); |
| 5267 | |
| 5268 | /* check the default value */ |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 5269 | if (unres_schema_add_node(leaf->module, unres, &leaf->type, UNRES_TYPE_DFLT, |
| 5270 | (struct lys_node *)(&leaf->dflt)) == -1) { |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 5271 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5272 | } |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 5273 | } else if (node->nodetype == LYS_LEAFLIST) { |
| 5274 | /* leaf-list */ |
| 5275 | llist = (struct lys_node_leaflist *)node; |
| 5276 | |
| 5277 | /* remove complete set of defaults in target */ |
Radek Krejci | 542ab14 | 2017-01-23 15:57:08 +0100 | [diff] [blame] | 5278 | for (j = 0; j < llist->dflt_size; j++) { |
| 5279 | lydict_remove(ctx, llist->dflt[j]); |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 5280 | } |
| 5281 | free(llist->dflt); |
| 5282 | |
| 5283 | /* copy the default set from refine */ |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 5284 | llist->dflt = malloc(rfn->dflt_size * sizeof *llist->dflt); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5285 | LY_CHECK_ERR_GOTO(!llist->dflt, LOGMEM(ctx), fail); |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 5286 | llist->dflt_size = rfn->dflt_size; |
Radek Krejci | 542ab14 | 2017-01-23 15:57:08 +0100 | [diff] [blame] | 5287 | for (j = 0; j < llist->dflt_size; j++) { |
| 5288 | llist->dflt[j] = lydict_insert(ctx, rfn->dflt[j], 0); |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 5289 | } |
| 5290 | |
| 5291 | /* check default value */ |
Radek Krejci | 542ab14 | 2017-01-23 15:57:08 +0100 | [diff] [blame] | 5292 | for (j = 0; j < llist->dflt_size; j++) { |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 5293 | if (unres_schema_add_node(llist->module, unres, &llist->type, UNRES_TYPE_DFLT, |
Radek Krejci | 542ab14 | 2017-01-23 15:57:08 +0100 | [diff] [blame] | 5294 | (struct lys_node *)(&llist->dflt[j])) == -1) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5295 | goto fail; |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 5296 | } |
| 5297 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5298 | } |
| 5299 | } |
| 5300 | |
| 5301 | /* mandatory on leaf, anyxml or choice */ |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 5302 | if (rfn->flags & LYS_MAND_MASK) { |
Radek Krejci | bf28583 | 2017-01-26 16:05:41 +0100 | [diff] [blame] | 5303 | /* remove current value */ |
| 5304 | node->flags &= ~LYS_MAND_MASK; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5305 | |
Radek Krejci | bf28583 | 2017-01-26 16:05:41 +0100 | [diff] [blame] | 5306 | /* set new value */ |
| 5307 | node->flags |= (rfn->flags & LYS_MAND_MASK); |
| 5308 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5309 | if (rfn->flags & LYS_MAND_TRUE) { |
| 5310 | /* check if node has default value */ |
| 5311 | if ((node->nodetype & LYS_LEAF) && ((struct lys_node_leaf *)node)->dflt) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5312 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, uses, |
Radek Krejci | bdcaf24 | 2017-04-19 10:29:47 +0200 | [diff] [blame] | 5313 | "The \"mandatory\" statement is forbidden on leaf with \"default\"."); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5314 | goto fail; |
| 5315 | } |
| 5316 | if ((node->nodetype & LYS_CHOICE) && ((struct lys_node_choice *)node)->dflt) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5317 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, uses, |
Radek Krejci | bdcaf24 | 2017-04-19 10:29:47 +0200 | [diff] [blame] | 5318 | "The \"mandatory\" statement is forbidden on choices with \"default\"."); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5319 | goto fail; |
| 5320 | } |
| 5321 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5322 | } |
| 5323 | |
| 5324 | /* presence on container */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 5325 | if ((node->nodetype & LYS_CONTAINER) && rfn->mod.presence) { |
| 5326 | lydict_remove(ctx, ((struct lys_node_container *)node)->presence); |
| 5327 | ((struct lys_node_container *)node)->presence = lydict_insert(ctx, rfn->mod.presence, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5328 | } |
| 5329 | |
| 5330 | /* min/max-elements on list or leaf-list */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 5331 | if (node->nodetype == LYS_LIST) { |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 5332 | if (rfn->flags & LYS_RFN_MINSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 5333 | ((struct lys_node_list *)node)->min = rfn->mod.list.min; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5334 | } |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 5335 | if (rfn->flags & LYS_RFN_MAXSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 5336 | ((struct lys_node_list *)node)->max = rfn->mod.list.max; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5337 | } |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 5338 | } else if (node->nodetype == LYS_LEAFLIST) { |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 5339 | if (rfn->flags & LYS_RFN_MINSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 5340 | ((struct lys_node_leaflist *)node)->min = rfn->mod.list.min; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5341 | } |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 5342 | if (rfn->flags & LYS_RFN_MAXSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 5343 | ((struct lys_node_leaflist *)node)->max = rfn->mod.list.max; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5344 | } |
| 5345 | } |
| 5346 | |
| 5347 | /* must in leaf, leaf-list, list, container or anyxml */ |
| 5348 | if (rfn->must_size) { |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 5349 | switch (node->nodetype) { |
| 5350 | case LYS_LEAF: |
| 5351 | old_size = &((struct lys_node_leaf *)node)->must_size; |
| 5352 | old_must = &((struct lys_node_leaf *)node)->must; |
| 5353 | break; |
| 5354 | case LYS_LEAFLIST: |
| 5355 | old_size = &((struct lys_node_leaflist *)node)->must_size; |
| 5356 | old_must = &((struct lys_node_leaflist *)node)->must; |
| 5357 | break; |
| 5358 | case LYS_LIST: |
| 5359 | old_size = &((struct lys_node_list *)node)->must_size; |
| 5360 | old_must = &((struct lys_node_list *)node)->must; |
| 5361 | break; |
| 5362 | case LYS_CONTAINER: |
| 5363 | old_size = &((struct lys_node_container *)node)->must_size; |
| 5364 | old_must = &((struct lys_node_container *)node)->must; |
| 5365 | break; |
| 5366 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 5367 | case LYS_ANYDATA: |
| 5368 | old_size = &((struct lys_node_anydata *)node)->must_size; |
| 5369 | old_must = &((struct lys_node_anydata *)node)->must; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 5370 | break; |
| 5371 | default: |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5372 | LOGINT(ctx); |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 5373 | goto fail; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 5374 | } |
| 5375 | |
| 5376 | size = *old_size + rfn->must_size; |
| 5377 | must = realloc(*old_must, size * sizeof *rfn->must); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5378 | LY_CHECK_ERR_GOTO(!must, LOGMEM(ctx), fail); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5379 | for (k = 0, j = *old_size; k < rfn->must_size; k++, j++) { |
Radek Krejci | 7f0164a | 2017-01-25 17:04:06 +0100 | [diff] [blame] | 5380 | must[j].ext_size = rfn->must[k].ext_size; |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 5381 | lys_ext_dup(ctx, rfn->module, rfn->must[k].ext, rfn->must[k].ext_size, &rfn->must[k], LYEXT_PAR_RESTR, |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 5382 | &must[j].ext, 0, unres); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5383 | must[j].expr = lydict_insert(ctx, rfn->must[k].expr, 0); |
| 5384 | must[j].dsc = lydict_insert(ctx, rfn->must[k].dsc, 0); |
| 5385 | must[j].ref = lydict_insert(ctx, rfn->must[k].ref, 0); |
| 5386 | must[j].eapptag = lydict_insert(ctx, rfn->must[k].eapptag, 0); |
| 5387 | must[j].emsg = lydict_insert(ctx, rfn->must[k].emsg, 0); |
Radek Krejci | cfcd8a5 | 2017-09-04 13:19:57 +0200 | [diff] [blame] | 5388 | must[j].flags = rfn->must[k].flags; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5389 | } |
| 5390 | |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 5391 | *old_must = must; |
| 5392 | *old_size = size; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5393 | |
| 5394 | /* check XPath dependencies again */ |
| 5395 | if (unres_schema_add_node(node->module, unres, node, UNRES_XPATH, NULL) == -1) { |
| 5396 | goto fail; |
| 5397 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5398 | } |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 5399 | |
| 5400 | /* if-feature in leaf, leaf-list, list, container or anyxml */ |
| 5401 | if (rfn->iffeature_size) { |
| 5402 | old_size = &node->iffeature_size; |
| 5403 | old_iff = &node->iffeature; |
| 5404 | |
| 5405 | size = *old_size + rfn->iffeature_size; |
| 5406 | iff = realloc(*old_iff, size * sizeof *rfn->iffeature); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5407 | LY_CHECK_ERR_GOTO(!iff, LOGMEM(ctx), fail); |
Radek Krejci | 3a3b200 | 2017-09-13 16:39:02 +0200 | [diff] [blame] | 5408 | *old_iff = iff; |
| 5409 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5410 | for (k = 0, j = *old_size; k < rfn->iffeature_size; k++, j++) { |
| 5411 | resolve_iffeature_getsizes(&rfn->iffeature[k], &usize1, &usize2); |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 5412 | if (usize1) { |
| 5413 | /* there is something to duplicate */ |
| 5414 | /* duplicate compiled expression */ |
Radek Krejci | 4e7ea7a | 2019-12-05 09:46:39 +0100 | [diff] [blame] | 5415 | usize = (usize1 / 4) + ((usize1 % 4) ? 1 : 0); |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 5416 | iff[j].expr = malloc(usize * sizeof *iff[j].expr); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5417 | LY_CHECK_ERR_GOTO(!iff[j].expr, LOGMEM(ctx), fail); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5418 | memcpy(iff[j].expr, rfn->iffeature[k].expr, usize * sizeof *iff[j].expr); |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 5419 | |
| 5420 | /* duplicate list of feature pointers */ |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5421 | iff[j].features = malloc(usize2 * sizeof *iff[k].features); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5422 | LY_CHECK_ERR_GOTO(!iff[j].expr, LOGMEM(ctx), fail); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5423 | memcpy(iff[j].features, rfn->iffeature[k].features, usize2 * sizeof *iff[j].features); |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 5424 | |
Radek Krejci | 3a3b200 | 2017-09-13 16:39:02 +0200 | [diff] [blame] | 5425 | /* duplicate extensions */ |
| 5426 | iff[j].ext_size = rfn->iffeature[k].ext_size; |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 5427 | lys_ext_dup(ctx, rfn->module, rfn->iffeature[k].ext, rfn->iffeature[k].ext_size, |
Radek Krejci | 3a3b200 | 2017-09-13 16:39:02 +0200 | [diff] [blame] | 5428 | &rfn->iffeature[k], LYEXT_PAR_IFFEATURE, &iff[j].ext, 0, unres); |
| 5429 | } |
| 5430 | (*old_size)++; |
| 5431 | } |
| 5432 | assert(*old_size == size); |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 5433 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5434 | } |
| 5435 | |
| 5436 | /* apply augments */ |
| 5437 | for (i = 0; i < uses->augment_size; i++) { |
Michal Vasko | 9723426 | 2018-02-01 09:53:01 +0100 | [diff] [blame] | 5438 | rc = resolve_augment(&uses->augment[i], (struct lys_node *)uses, unres); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5439 | if (rc) { |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 5440 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5441 | } |
| 5442 | } |
| 5443 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5444 | /* check refines */ |
| 5445 | for (i = 0; i < uses->refine_size; i++) { |
| 5446 | node = refine_nodes[i]; |
| 5447 | rfn = &uses->refine[i]; |
| 5448 | |
| 5449 | /* config on any nodetype */ |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 5450 | if ((rfn->flags & LYS_CONFIG_MASK) && (node->flags & LYS_CONFIG_MASK)) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5451 | for (parent = lys_parent(node); parent && parent->nodetype == LYS_USES; parent = lys_parent(parent)); |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 5452 | if (parent && parent->nodetype != LYS_GROUPING && (parent->flags & LYS_CONFIG_MASK) && |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5453 | ((parent->flags & LYS_CONFIG_MASK) != (rfn->flags & LYS_CONFIG_MASK)) && |
| 5454 | (rfn->flags & LYS_CONFIG_W)) { |
| 5455 | /* setting config true under config false is prohibited */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5456 | LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, uses, "config", "refine"); |
| 5457 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5458 | "changing config from 'false' to 'true' is prohibited while " |
| 5459 | "the target's parent is still config 'false'."); |
| 5460 | goto fail; |
| 5461 | } |
| 5462 | |
| 5463 | /* inherit config change to the target children */ |
| 5464 | LY_TREE_DFS_BEGIN(node->child, next, iter) { |
| 5465 | if (rfn->flags & LYS_CONFIG_W) { |
| 5466 | if (iter->flags & LYS_CONFIG_SET) { |
| 5467 | /* config is set explicitely, go to next sibling */ |
| 5468 | next = NULL; |
| 5469 | goto nextsibling; |
| 5470 | } |
| 5471 | } else { /* LYS_CONFIG_R */ |
| 5472 | if ((iter->flags & LYS_CONFIG_SET) && (iter->flags & LYS_CONFIG_W)) { |
| 5473 | /* error - we would have config data under status data */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5474 | LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, uses, "config", "refine"); |
| 5475 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5476 | "changing config from 'true' to 'false' is prohibited while the target " |
| 5477 | "has still a children with explicit config 'true'."); |
| 5478 | goto fail; |
| 5479 | } |
| 5480 | } |
| 5481 | /* change config */ |
| 5482 | iter->flags &= ~LYS_CONFIG_MASK; |
| 5483 | iter->flags |= (rfn->flags & LYS_CONFIG_MASK); |
| 5484 | |
| 5485 | /* select next iter - modified LY_TREE_DFS_END */ |
| 5486 | if (iter->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
| 5487 | next = NULL; |
| 5488 | } else { |
| 5489 | next = iter->child; |
| 5490 | } |
| 5491 | nextsibling: |
| 5492 | if (!next) { |
| 5493 | /* try siblings */ |
| 5494 | next = iter->next; |
| 5495 | } |
| 5496 | while (!next) { |
| 5497 | /* parent is already processed, go to its sibling */ |
| 5498 | iter = lys_parent(iter); |
| 5499 | |
| 5500 | /* no siblings, go back through parents */ |
| 5501 | if (iter == node) { |
| 5502 | /* we are done, no next element to process */ |
| 5503 | break; |
| 5504 | } |
| 5505 | next = iter->next; |
| 5506 | } |
| 5507 | } |
| 5508 | } |
| 5509 | |
| 5510 | /* default value */ |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 5511 | if (rfn->dflt_size) { |
| 5512 | if (node->nodetype == LYS_CHOICE) { |
| 5513 | /* choice */ |
| 5514 | ((struct lys_node_choice *)node)->dflt = resolve_choice_dflt((struct lys_node_choice *)node, |
| 5515 | rfn->dflt[0]); |
| 5516 | if (!((struct lys_node_choice *)node)->dflt) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5517 | LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, uses, rfn->dflt[0], "default"); |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 5518 | goto fail; |
| 5519 | } |
| 5520 | if (lyp_check_mandatory_choice(node)) { |
| 5521 | goto fail; |
| 5522 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5523 | } |
| 5524 | } |
| 5525 | |
| 5526 | /* min/max-elements on list or leaf-list */ |
Radek Krejci | 2d3c811 | 2017-04-19 10:20:50 +0200 | [diff] [blame] | 5527 | if (node->nodetype == LYS_LIST && ((struct lys_node_list *)node)->max) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5528 | if (((struct lys_node_list *)node)->min > ((struct lys_node_list *)node)->max) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5529 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, uses, "Invalid value \"%d\" of \"%s\".", rfn->mod.list.min, "min-elements"); |
| 5530 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "\"min-elements\" is bigger than \"max-elements\"."); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5531 | goto fail; |
| 5532 | } |
Radek Krejci | 2d3c811 | 2017-04-19 10:20:50 +0200 | [diff] [blame] | 5533 | } else if (node->nodetype == LYS_LEAFLIST && ((struct lys_node_leaflist *)node)->max) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5534 | if (((struct lys_node_leaflist *)node)->min > ((struct lys_node_leaflist *)node)->max) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5535 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, uses, "Invalid value \"%d\" of \"%s\".", rfn->mod.list.min, "min-elements"); |
| 5536 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "\"min-elements\" is bigger than \"max-elements\"."); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5537 | goto fail; |
| 5538 | } |
| 5539 | } |
| 5540 | |
| 5541 | /* additional checks */ |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 5542 | /* default value with mandatory/min-elements */ |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5543 | if (node->nodetype == LYS_LEAFLIST) { |
| 5544 | llist = (struct lys_node_leaflist *)node; |
| 5545 | if (llist->dflt_size && llist->min) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5546 | LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, uses, rfn->dflt_size ? "default" : "min-elements", "refine"); |
| 5547 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5548 | "The \"min-elements\" statement with non-zero value is forbidden on leaf-lists with the \"default\" statement."); |
| 5549 | goto fail; |
| 5550 | } |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 5551 | } else if (node->nodetype == LYS_LEAF) { |
| 5552 | leaf = (struct lys_node_leaf *)node; |
| 5553 | if (leaf->dflt && (leaf->flags & LYS_MAND_TRUE)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5554 | LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, uses, rfn->dflt_size ? "default" : "mandatory", "refine"); |
| 5555 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 5556 | "The \"mandatory\" statement is forbidden on leafs with the \"default\" statement."); |
| 5557 | goto fail; |
| 5558 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5559 | } |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 5560 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5561 | /* check for mandatory node in default case, first find the closest parent choice to the changed node */ |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 5562 | if ((rfn->flags & LYS_MAND_TRUE) || rfn->mod.list.min) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5563 | for (parent = node->parent; |
| 5564 | parent && !(parent->nodetype & (LYS_CHOICE | LYS_GROUPING | LYS_ACTION | LYS_USES)); |
| 5565 | parent = parent->parent) { |
| 5566 | if (parent->nodetype == LYS_CONTAINER && ((struct lys_node_container *)parent)->presence) { |
| 5567 | /* stop also on presence containers */ |
| 5568 | break; |
| 5569 | } |
| 5570 | } |
| 5571 | /* and if it is a choice with the default case, check it for presence of a mandatory node in it */ |
| 5572 | if (parent && parent->nodetype == LYS_CHOICE && ((struct lys_node_choice *)parent)->dflt) { |
| 5573 | if (lyp_check_mandatory_choice(parent)) { |
| 5574 | goto fail; |
| 5575 | } |
| 5576 | } |
| 5577 | } |
| 5578 | } |
| 5579 | free(refine_nodes); |
| 5580 | |
Michal Vasko | 4d886c4 | 2020-05-29 09:31:16 +0200 | [diff] [blame] | 5581 | /* check list config after all the refines were applied */ |
| 5582 | LY_TREE_DFS_BEGIN((struct lys_node *)uses, next, iter) { |
| 5583 | if ((iter->nodetype == LYS_LIST) && (iter->flags & LYS_CONFIG_W) |
| 5584 | && !((struct lys_node_list *)iter)->keys_size) { |
| 5585 | LOGVAL(ctx, LYE_MISSCHILDSTMT, LY_VLOG_LYS, iter, "key", "list"); |
| 5586 | goto fail; |
| 5587 | } |
| 5588 | LY_TREE_DFS_END((struct lys_node *)uses, next, iter); |
| 5589 | } |
| 5590 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5591 | return EXIT_SUCCESS; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 5592 | |
| 5593 | fail: |
| 5594 | LY_TREE_FOR_SAFE(uses->child, next, iter) { |
| 5595 | lys_node_free(iter, NULL, 0); |
| 5596 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5597 | free(refine_nodes); |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 5598 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5599 | } |
| 5600 | |
Radek Krejci | 83a4bac | 2017-02-07 15:53:04 +0100 | [diff] [blame] | 5601 | void |
| 5602 | resolve_identity_backlink_update(struct lys_ident *der, struct lys_ident *base) |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5603 | { |
| 5604 | int i; |
| 5605 | |
| 5606 | assert(der && base); |
| 5607 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5608 | if (!base->der) { |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5609 | /* create a set for backlinks if it does not exist */ |
| 5610 | base->der = ly_set_new(); |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5611 | } |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5612 | /* store backlink */ |
| 5613 | ly_set_add(base->der, der, LY_SET_OPT_USEASLIST); |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5614 | |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5615 | /* do it recursively */ |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5616 | for (i = 0; i < base->base_size; i++) { |
Radek Krejci | 83a4bac | 2017-02-07 15:53:04 +0100 | [diff] [blame] | 5617 | resolve_identity_backlink_update(der, base->base[i]); |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5618 | } |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5619 | } |
| 5620 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5621 | /** |
| 5622 | * @brief Resolve base identity recursively. Does not log. |
| 5623 | * |
| 5624 | * @param[in] module Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5625 | * @param[in] ident Identity to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5626 | * @param[in] basename Base name of the identity. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5627 | * @param[out] ret Pointer to the resolved identity. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5628 | * |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5629 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on crucial error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5630 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5631 | static int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 5632 | resolve_base_ident_sub(const struct lys_module *module, struct lys_ident *ident, const char *basename, |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5633 | struct unres_schema *unres, struct lys_ident **ret) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5634 | { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5635 | uint32_t i, j; |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5636 | struct lys_ident *base = NULL; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5637 | struct ly_ctx *ctx = module->ctx; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5638 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5639 | assert(ret); |
| 5640 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5641 | /* search module */ |
| 5642 | for (i = 0; i < module->ident_size; i++) { |
| 5643 | if (!strcmp(basename, module->ident[i].name)) { |
| 5644 | |
| 5645 | if (!ident) { |
| 5646 | /* just search for type, so do not modify anything, just return |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5647 | * the base identity pointer */ |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5648 | *ret = &module->ident[i]; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5649 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5650 | } |
| 5651 | |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5652 | base = &module->ident[i]; |
| 5653 | goto matchfound; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5654 | } |
| 5655 | } |
| 5656 | |
| 5657 | /* search submodules */ |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5658 | for (j = 0; j < module->inc_size && module->inc[j].submodule; j++) { |
| 5659 | for (i = 0; i < module->inc[j].submodule->ident_size; i++) { |
| 5660 | if (!strcmp(basename, module->inc[j].submodule->ident[i].name)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5661 | |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5662 | if (!ident) { |
| 5663 | *ret = &module->inc[j].submodule->ident[i]; |
| 5664 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5665 | } |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5666 | |
| 5667 | base = &module->inc[j].submodule->ident[i]; |
| 5668 | goto matchfound; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5669 | } |
| 5670 | } |
| 5671 | } |
| 5672 | |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5673 | matchfound: |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5674 | /* we found it somewhere */ |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 5675 | if (base) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5676 | /* is it already completely resolved? */ |
| 5677 | for (i = 0; i < unres->count; i++) { |
Radek Krejci | ba7b1cc | 2016-08-08 13:44:43 +0200 | [diff] [blame] | 5678 | if ((unres->item[i] == base) && (unres->type[i] == UNRES_IDENT)) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5679 | /* identity found, but not yet resolved, so do not return it in *res and try it again later */ |
| 5680 | |
| 5681 | /* simple check for circular reference, |
| 5682 | * the complete check is done as a side effect of using only completely |
| 5683 | * resolved identities (previous check of unres content) */ |
| 5684 | if (ly_strequal((const char *)unres->str_snode[i], ident->name, 1)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5685 | LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, basename, "base"); |
| 5686 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Circular reference of \"%s\" identity.", basename); |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5687 | return -1; |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5688 | } |
| 5689 | |
Radek Krejci | 06f64ed | 2016-08-15 11:07:44 +0200 | [diff] [blame] | 5690 | return EXIT_FAILURE; |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 5691 | } |
| 5692 | } |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5693 | |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 5694 | /* checks done, store the result */ |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5695 | *ret = base; |
Pavol Vican | fdab9f9 | 2016-09-07 15:23:27 +0200 | [diff] [blame] | 5696 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5697 | } |
| 5698 | |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5699 | /* base not found (maybe a forward reference) */ |
| 5700 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5701 | } |
| 5702 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5703 | /** |
| 5704 | * @brief Resolve base identity. Logs directly. |
| 5705 | * |
| 5706 | * @param[in] module Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5707 | * @param[in] ident Identity to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5708 | * @param[in] basename Base name of the identity. |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 5709 | * @param[in] parent Either "type" or "identity". |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5710 | * @param[in,out] type Type structure where we want to resolve identity. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5711 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5712 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5713 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5714 | static int |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5715 | resolve_base_ident(const struct lys_module *module, struct lys_ident *ident, const char *basename, const char *parent, |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5716 | struct lys_type *type, struct unres_schema *unres) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5717 | { |
| 5718 | const char *name; |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5719 | int mod_name_len = 0, rc; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5720 | struct lys_ident *target, **ret; |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 5721 | uint16_t flags; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5722 | struct lys_module *mod; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5723 | struct ly_ctx *ctx = module->ctx; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5724 | |
| 5725 | assert((ident && !type) || (!ident && type)); |
| 5726 | |
| 5727 | if (!type) { |
| 5728 | /* have ident to resolve */ |
| 5729 | ret = ⌖ |
| 5730 | flags = ident->flags; |
| 5731 | mod = ident->module; |
| 5732 | } else { |
| 5733 | /* have type to fill */ |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5734 | ++type->info.ident.count; |
| 5735 | type->info.ident.ref = ly_realloc(type->info.ident.ref, type->info.ident.count * sizeof *type->info.ident.ref); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5736 | LY_CHECK_ERR_RETURN(!type->info.ident.ref, LOGMEM(ctx), -1); |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5737 | |
| 5738 | ret = &type->info.ident.ref[type->info.ident.count - 1]; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5739 | flags = type->parent->flags; |
| 5740 | mod = type->parent->module; |
| 5741 | } |
Michal Vasko | f200600 | 2016-04-21 16:28:15 +0200 | [diff] [blame] | 5742 | *ret = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5743 | |
| 5744 | /* search for the base identity */ |
| 5745 | name = strchr(basename, ':'); |
| 5746 | if (name) { |
| 5747 | /* set name to correct position after colon */ |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 5748 | mod_name_len = name - basename; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5749 | name++; |
| 5750 | |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 5751 | if (!strncmp(basename, module->name, mod_name_len) && !module->name[mod_name_len]) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5752 | /* prefix refers to the current module, ignore it */ |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 5753 | mod_name_len = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5754 | } |
| 5755 | } else { |
| 5756 | name = basename; |
| 5757 | } |
| 5758 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 5759 | /* get module where to search */ |
Michal Vasko | 921eb6b | 2017-10-13 10:01:39 +0200 | [diff] [blame] | 5760 | module = lyp_get_module(module, NULL, 0, mod_name_len ? basename : NULL, mod_name_len, 0); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 5761 | if (!module) { |
| 5762 | /* identity refers unknown data model */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5763 | LOGVAL(ctx, LYE_INMOD, LY_VLOG_NONE, NULL, basename); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 5764 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5765 | } |
| 5766 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 5767 | /* search in the identified module ... */ |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5768 | rc = resolve_base_ident_sub(module, ident, name, unres, ret); |
| 5769 | if (!rc) { |
| 5770 | assert(*ret); |
| 5771 | |
| 5772 | /* check status */ |
| 5773 | if (lyp_check_status(flags, mod, ident ? ident->name : "of type", |
| 5774 | (*ret)->flags, (*ret)->module, (*ret)->name, NULL)) { |
| 5775 | rc = -1; |
Radek Krejci | 83a4bac | 2017-02-07 15:53:04 +0100 | [diff] [blame] | 5776 | } else if (ident) { |
| 5777 | ident->base[ident->base_size++] = *ret; |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5778 | if (lys_main_module(mod)->implemented) { |
| 5779 | /* in case of the implemented identity, maintain backlinks to it |
| 5780 | * from the base identities to make it available when resolving |
| 5781 | * data with the identity values (not implemented identity is not |
| 5782 | * allowed as an identityref value). */ |
| 5783 | resolve_identity_backlink_update(ident, *ret); |
| 5784 | } |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5785 | } |
| 5786 | } else if (rc == EXIT_FAILURE) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5787 | LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_NONE, NULL, parent, basename); |
Pavol Vican | 053a288 | 2016-09-05 14:40:33 +0200 | [diff] [blame] | 5788 | if (type) { |
| 5789 | --type->info.ident.count; |
| 5790 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5791 | } |
| 5792 | |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5793 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5794 | } |
| 5795 | |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5796 | /* |
| 5797 | * 1 - true (der is derived from base) |
| 5798 | * 0 - false (der is not derived from base) |
| 5799 | */ |
| 5800 | static int |
| 5801 | search_base_identity(struct lys_ident *der, struct lys_ident *base) |
| 5802 | { |
| 5803 | int i; |
| 5804 | |
| 5805 | if (der == base) { |
| 5806 | return 1; |
| 5807 | } else { |
| 5808 | for(i = 0; i < der->base_size; i++) { |
| 5809 | if (search_base_identity(der->base[i], base) == 1) { |
| 5810 | return 1; |
| 5811 | } |
| 5812 | } |
| 5813 | } |
| 5814 | |
| 5815 | return 0; |
| 5816 | } |
| 5817 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5818 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 5819 | * @brief Resolve JSON data format identityref. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5820 | * |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5821 | * @param[in] type Identityref type. |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 5822 | * @param[in] ident_name Identityref name. |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 5823 | * @param[in] node Node where the identityref is being resolved |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5824 | * @param[in] dflt flag if we are resolving default value in the schema |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5825 | * |
| 5826 | * @return Pointer to the identity resolvent, NULL on error. |
| 5827 | */ |
Radek Krejci | a52656e | 2015-08-05 13:41:50 +0200 | [diff] [blame] | 5828 | struct lys_ident * |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5829 | resolve_identref(struct lys_type *type, const char *ident_name, struct lyd_node *node, struct lys_module *mod, int dflt) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5830 | { |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5831 | const char *mod_name, *name; |
Michal Vasko | 08767f7 | 2017-10-06 14:38:08 +0200 | [diff] [blame] | 5832 | char *str; |
Radek Krejci | dce5f97 | 2017-09-12 15:47:49 +0200 | [diff] [blame] | 5833 | int mod_name_len, nam_len, rc; |
Michal Vasko | 3f3c6a8 | 2017-10-03 10:23:23 +0200 | [diff] [blame] | 5834 | int need_implemented = 0; |
Michal Vasko | 4ea8de2 | 2020-01-27 13:53:28 +0100 | [diff] [blame] | 5835 | unsigned int i, j, found; |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5836 | struct lys_ident *der, *cur; |
Andrew Langefeld | 8f50a2d | 2018-05-23 17:16:12 -0500 | [diff] [blame] | 5837 | struct lys_module *imod = NULL, *m, *tmod; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5838 | struct ly_ctx *ctx; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5839 | |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 5840 | assert(type && ident_name && mod); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5841 | ctx = mod->ctx; |
Radek Krejci | bb1ce0f | 2016-12-05 13:24:33 +0100 | [diff] [blame] | 5842 | |
Michal Vasko | bb24f28 | 2019-09-02 12:54:30 +0200 | [diff] [blame] | 5843 | if (!type || (!type->info.ident.count && !type->der->module) || !ident_name) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5844 | return NULL; |
| 5845 | } |
| 5846 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 5847 | rc = parse_node_identifier(ident_name, &mod_name, &mod_name_len, &name, &nam_len, NULL, 0); |
Michal Vasko | b43bb3c | 2016-03-17 15:00:27 +0100 | [diff] [blame] | 5848 | if (rc < 1) { |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 5849 | LOGVAL(ctx, LYE_INCHAR, node ? LY_VLOG_LYD : LY_VLOG_NONE, node, ident_name[-rc], &ident_name[-rc]); |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 5850 | return NULL; |
Michal Vasko | b43bb3c | 2016-03-17 15:00:27 +0100 | [diff] [blame] | 5851 | } else if (rc < (signed)strlen(ident_name)) { |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 5852 | LOGVAL(ctx, LYE_INCHAR, node ? LY_VLOG_LYD : LY_VLOG_NONE, node, ident_name[rc], &ident_name[rc]); |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 5853 | return NULL; |
| 5854 | } |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5855 | |
| 5856 | m = lys_main_module(mod); /* shortcut */ |
| 5857 | if (!mod_name || (!strncmp(mod_name, m->name, mod_name_len) && !m->name[mod_name_len])) { |
| 5858 | /* identity is defined in the same module as node */ |
| 5859 | imod = m; |
| 5860 | } else if (dflt) { |
| 5861 | /* solving identityref in default definition in schema - |
| 5862 | * find the identity's module in the imported modules list to have a correct revision */ |
| 5863 | for (i = 0; i < mod->imp_size; i++) { |
| 5864 | if (!strncmp(mod_name, mod->imp[i].module->name, mod_name_len) && !mod->imp[i].module->name[mod_name_len]) { |
| 5865 | imod = mod->imp[i].module; |
| 5866 | break; |
| 5867 | } |
| 5868 | } |
Andrew Langefeld | 8f50a2d | 2018-05-23 17:16:12 -0500 | [diff] [blame] | 5869 | |
| 5870 | /* We may need to pull it from the module that the typedef came from */ |
Michal Vasko | bb24f28 | 2019-09-02 12:54:30 +0200 | [diff] [blame] | 5871 | if (!imod && type && type->der->module) { |
Andrew Langefeld | 8f50a2d | 2018-05-23 17:16:12 -0500 | [diff] [blame] | 5872 | tmod = type->der->module; |
| 5873 | for (i = 0; i < tmod->imp_size; i++) { |
| 5874 | if (!strncmp(mod_name, tmod->imp[i].module->name, mod_name_len) && !tmod->imp[i].module->name[mod_name_len]) { |
| 5875 | imod = tmod->imp[i].module; |
| 5876 | break; |
| 5877 | } |
| 5878 | } |
| 5879 | } |
Michal Vasko | de3a8ff | 2019-09-04 13:34:59 +0200 | [diff] [blame] | 5880 | |
| 5881 | /* it can still be a default value in a foreign grouping */ |
| 5882 | if (!imod) { |
| 5883 | str = strndup(mod_name, mod_name_len); |
| 5884 | imod = (struct lys_module *)ly_ctx_get_module(ctx, str, NULL, 1); |
| 5885 | free(str); |
| 5886 | } |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5887 | } else { |
Michal Vasko | 08767f7 | 2017-10-06 14:38:08 +0200 | [diff] [blame] | 5888 | /* solving identityref in data - get the module from the context */ |
| 5889 | for (i = 0; i < (unsigned)mod->ctx->models.used; ++i) { |
| 5890 | imod = mod->ctx->models.list[i]; |
Michal Vasko | 3f3c6a8 | 2017-10-03 10:23:23 +0200 | [diff] [blame] | 5891 | if (!strncmp(mod_name, imod->name, mod_name_len) && !imod->name[mod_name_len]) { |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5892 | break; |
| 5893 | } |
Michal Vasko | 08767f7 | 2017-10-06 14:38:08 +0200 | [diff] [blame] | 5894 | imod = NULL; |
| 5895 | } |
Radek Krejci | 5ba0510 | 2017-10-26 15:02:52 +0200 | [diff] [blame] | 5896 | if (!imod && mod->ctx->models.parsing_sub_modules_count) { |
| 5897 | /* we are currently parsing some module and checking XPath or a default value, |
| 5898 | * so take this module into account */ |
| 5899 | for (i = 0; i < mod->ctx->models.parsing_sub_modules_count; i++) { |
| 5900 | imod = mod->ctx->models.parsing_sub_modules[i]; |
| 5901 | if (imod->type) { |
| 5902 | /* skip submodules */ |
| 5903 | continue; |
| 5904 | } |
| 5905 | if (!strncmp(mod_name, imod->name, mod_name_len) && !imod->name[mod_name_len]) { |
| 5906 | break; |
| 5907 | } |
| 5908 | imod = NULL; |
| 5909 | } |
| 5910 | } |
Michal Vasko | 08767f7 | 2017-10-06 14:38:08 +0200 | [diff] [blame] | 5911 | } |
| 5912 | |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5913 | if (!dflt && (!imod || !imod->implemented) && ctx->data_clb) { |
Michal Vasko | 08767f7 | 2017-10-06 14:38:08 +0200 | [diff] [blame] | 5914 | /* the needed module was not found, but it may have been expected so call the data callback */ |
| 5915 | if (imod) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5916 | ctx->data_clb(ctx, imod->name, imod->ns, LY_MODCLB_NOT_IMPLEMENTED, ctx->data_clb_data); |
Radek Krejci | 58523dc | 2017-10-27 10:00:17 +0200 | [diff] [blame] | 5917 | } else if (mod_name) { |
Michal Vasko | 08767f7 | 2017-10-06 14:38:08 +0200 | [diff] [blame] | 5918 | str = strndup(mod_name, mod_name_len); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5919 | imod = (struct lys_module *)ctx->data_clb(ctx, str, NULL, 0, ctx->data_clb_data); |
Michal Vasko | 08767f7 | 2017-10-06 14:38:08 +0200 | [diff] [blame] | 5920 | free(str); |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5921 | } |
| 5922 | } |
| 5923 | if (!imod) { |
| 5924 | goto fail; |
| 5925 | } |
| 5926 | |
Michal Vasko | 4ea8de2 | 2020-01-27 13:53:28 +0100 | [diff] [blame] | 5927 | /* find the type with base definitions */ |
| 5928 | while (!type->info.ident.count && type->der) { |
| 5929 | type = &type->der->type; |
| 5930 | } |
| 5931 | if (!type->info.ident.count) { |
| 5932 | goto fail; |
| 5933 | } |
| 5934 | |
Michal Vasko | 3f3c6a8 | 2017-10-03 10:23:23 +0200 | [diff] [blame] | 5935 | if (m != imod || lys_main_module(type->parent->module) != mod) { |
Michal Vasko | 08767f7 | 2017-10-06 14:38:08 +0200 | [diff] [blame] | 5936 | /* the type is not referencing the same schema, |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5937 | * THEN, we may need to make the module with the identity implemented, but only if it really |
| 5938 | * contains the identity */ |
| 5939 | if (!imod->implemented) { |
| 5940 | cur = NULL; |
| 5941 | /* get the identity in the module */ |
| 5942 | for (i = 0; i < imod->ident_size; i++) { |
| 5943 | if (!strcmp(name, imod->ident[i].name)) { |
| 5944 | cur = &imod->ident[i]; |
| 5945 | break; |
| 5946 | } |
| 5947 | } |
| 5948 | if (!cur) { |
| 5949 | /* go through includes */ |
| 5950 | for (j = 0; j < imod->inc_size; j++) { |
| 5951 | for (i = 0; i < imod->inc[j].submodule->ident_size; i++) { |
| 5952 | if (!strcmp(name, imod->inc[j].submodule->ident[i].name)) { |
| 5953 | cur = &imod->inc[j].submodule->ident[i]; |
| 5954 | break; |
| 5955 | } |
| 5956 | } |
| 5957 | } |
| 5958 | if (!cur) { |
| 5959 | goto fail; |
| 5960 | } |
| 5961 | } |
| 5962 | |
| 5963 | /* check that identity is derived from one of the type's base */ |
| 5964 | while (type->der) { |
| 5965 | for (i = 0; i < type->info.ident.count; i++) { |
| 5966 | if (search_base_identity(cur, type->info.ident.ref[i])) { |
| 5967 | /* cur's base matches the type's base */ |
Michal Vasko | 3f3c6a8 | 2017-10-03 10:23:23 +0200 | [diff] [blame] | 5968 | need_implemented = 1; |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5969 | goto match; |
| 5970 | } |
| 5971 | } |
| 5972 | type = &type->der->type; |
| 5973 | } |
| 5974 | /* matching base not found */ |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 5975 | LOGVAL(ctx, LYE_SPEC, node ? LY_VLOG_LYD : LY_VLOG_NONE, node, "Identity used as identityref value is not implemented."); |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5976 | goto fail; |
| 5977 | } |
Radek Krejci | f32c5f6 | 2016-12-05 09:27:38 +0100 | [diff] [blame] | 5978 | } |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 5979 | |
Radek Krejci | 98a1e2d | 2017-04-26 14:34:52 +0200 | [diff] [blame] | 5980 | /* go through all the derived types of all the bases */ |
Michal Vasko | 4ea8de2 | 2020-01-27 13:53:28 +0100 | [diff] [blame] | 5981 | found = 0; |
| 5982 | for (i = 0; i < type->info.ident.count; ++i) { |
| 5983 | cur = type->info.ident.ref[i]; |
| 5984 | if (cur->der) { |
| 5985 | /* there are some derived identities */ |
| 5986 | for (j = 0; j < cur->der->number; j++) { |
| 5987 | der = (struct lys_ident *)cur->der->set.g[j]; /* shortcut */ |
| 5988 | if (!strcmp(der->name, name) && lys_main_module(der->module) == imod) { |
| 5989 | /* we have a match on this base */ |
| 5990 | ++found; |
| 5991 | break; |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5992 | } |
| 5993 | } |
Michal Vasko | bd2e2d3 | 2020-03-09 11:57:39 +0100 | [diff] [blame] | 5994 | } else { |
| 5995 | LOGWRN(ctx, "Identity \"%s\" has no derived identities, identityref with this base can never be instatiated.", |
| 5996 | cur->name); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5997 | } |
Michal Vasko | 4ea8de2 | 2020-01-27 13:53:28 +0100 | [diff] [blame] | 5998 | } |
| 5999 | if (found == type->info.ident.count) { |
| 6000 | /* match found for all bases */ |
| 6001 | cur = der; |
| 6002 | goto match; |
Michal Vasko | bd2e2d3 | 2020-03-09 11:57:39 +0100 | [diff] [blame] | 6003 | } else { |
Michal Vasko | 4ea8de2 | 2020-01-27 13:53:28 +0100 | [diff] [blame] | 6004 | LOGVAL(ctx, LYE_SPEC, node ? LY_VLOG_LYD : LY_VLOG_NONE, node, "Identityref value is not derived from all its bases."); |
| 6005 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6006 | } |
| 6007 | |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 6008 | fail: |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 6009 | LOGVAL(ctx, LYE_INRESOLV, node ? LY_VLOG_LYD : LY_VLOG_NONE, node, "identityref", ident_name); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6010 | return NULL; |
Radek Krejci | f1ee2e2 | 2016-08-02 16:36:48 +0200 | [diff] [blame] | 6011 | |
| 6012 | match: |
Michal Vasko | 8dfaf7c | 2020-09-10 09:26:02 +0200 | [diff] [blame] | 6013 | if (!dflt) { |
| 6014 | for (i = 0; i < cur->iffeature_size; i++) { |
| 6015 | if (!resolve_iffeature(&cur->iffeature[i])) { |
| 6016 | if (node) { |
| 6017 | LOGVAL(ctx, LYE_INVAL, LY_VLOG_LYD, node, cur->name, node->schema->name); |
| 6018 | } |
Michal Vasko | 83cea22 | 2020-09-11 15:50:43 +0200 | [diff] [blame] | 6019 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Identity \"%s\" is disabled by its %d. if-feature condition.", |
| 6020 | cur->name, i); |
Michal Vasko | 8dfaf7c | 2020-09-10 09:26:02 +0200 | [diff] [blame] | 6021 | return NULL; |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 6022 | } |
Radek Krejci | f1ee2e2 | 2016-08-02 16:36:48 +0200 | [diff] [blame] | 6023 | } |
| 6024 | } |
Michal Vasko | 3f3c6a8 | 2017-10-03 10:23:23 +0200 | [diff] [blame] | 6025 | if (need_implemented) { |
| 6026 | if (dflt) { |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 6027 | /* later try to make the module implemented */ |
Michal Vasko | 3f3c6a8 | 2017-10-03 10:23:23 +0200 | [diff] [blame] | 6028 | LOGVRB("Making \"%s\" module implemented because of identityref default value \"%s\" used in the implemented \"%s\" module", |
| 6029 | imod->name, cur->name, mod->name); |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 6030 | /* to be more effective we should use UNRES_MOD_IMPLEMENT but that would require changing prototype of |
| 6031 | * several functions with little gain */ |
Michal Vasko | 3f3c6a8 | 2017-10-03 10:23:23 +0200 | [diff] [blame] | 6032 | if (lys_set_implemented(imod)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6033 | LOGERR(ctx, ly_errno, "Setting the module \"%s\" implemented because of used default identity \"%s\" failed.", |
Michal Vasko | 3f3c6a8 | 2017-10-03 10:23:23 +0200 | [diff] [blame] | 6034 | imod->name, cur->name); |
Michal Vasko | 3f3c6a8 | 2017-10-03 10:23:23 +0200 | [diff] [blame] | 6035 | goto fail; |
| 6036 | } |
| 6037 | } else { |
| 6038 | /* just say that it was found, but in a non-implemented module */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6039 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Identity found, but in a non-implemented module \"%s\".", |
Michal Vasko | 3f3c6a8 | 2017-10-03 10:23:23 +0200 | [diff] [blame] | 6040 | lys_main_module(cur->module)->name); |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 6041 | goto fail; |
| 6042 | } |
| 6043 | } |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 6044 | return cur; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6045 | } |
| 6046 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 6047 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6048 | * @brief Resolve unresolved uses. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 6049 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6050 | * @param[in] uses Uses to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 6051 | * @param[in] unres Specific unres item. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 6052 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6053 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 6054 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6055 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6056 | resolve_unres_schema_uses(struct lys_node_uses *uses, struct unres_schema *unres) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6057 | { |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 6058 | int rc; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6059 | struct lys_node *par_grp; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6060 | struct ly_ctx *ctx = uses->module->ctx; |
Michal Vasko | e91afce | 2015-08-12 12:21:00 +0200 | [diff] [blame] | 6061 | |
Radek Krejci | 6ff885d | 2017-01-03 14:06:22 +0100 | [diff] [blame] | 6062 | /* HACK: when a grouping has uses inside, all such uses have to be resolved before the grouping itself is used |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 6063 | * in some uses. When we see such a uses, the grouping's unres counter is used to store number of so far |
| 6064 | * unresolved uses. The grouping cannot be used unless this counter is decreased back to 0. To remember |
| 6065 | * that the uses already increased grouping's counter, the LYS_USESGRP flag is used. */ |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 6066 | for (par_grp = lys_parent((struct lys_node *)uses); par_grp && (par_grp->nodetype != LYS_GROUPING); par_grp = lys_parent(par_grp)); |
Michal Vasko | a979272 | 2018-06-28 09:01:02 +0200 | [diff] [blame] | 6067 | if (par_grp && ly_strequal(par_grp->name, uses->name, 1)) { |
| 6068 | LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name); |
| 6069 | return -1; |
| 6070 | } |
Michal Vasko | e91afce | 2015-08-12 12:21:00 +0200 | [diff] [blame] | 6071 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6072 | if (!uses->grp) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 6073 | rc = resolve_uses_schema_nodeid(uses->name, (const struct lys_node *)uses, (const struct lys_node_grp **)&uses->grp); |
| 6074 | if (rc == -1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6075 | LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 6076 | return -1; |
| 6077 | } else if (rc > 0) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6078 | LOGVAL(ctx, LYE_INCHAR, LY_VLOG_LYS, uses, uses->name[rc - 1], &uses->name[rc - 1]); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 6079 | return -1; |
| 6080 | } else if (!uses->grp) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6081 | if (par_grp && !(uses->flags & LYS_USESGRP)) { |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 6082 | if (++((struct lys_node_grp *)par_grp)->unres_count == 0) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6083 | LOGERR(ctx, LY_EINT, "Too many unresolved items (uses) inside a grouping."); |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 6084 | return -1; |
| 6085 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6086 | uses->flags |= LYS_USESGRP; |
Michal Vasko | 407f1bb | 2015-09-23 15:51:07 +0200 | [diff] [blame] | 6087 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6088 | LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 6089 | return EXIT_FAILURE; |
Michal Vasko | 12e3084 | 2015-08-04 11:54:00 +0200 | [diff] [blame] | 6090 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6091 | } |
| 6092 | |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 6093 | if (uses->grp->unres_count) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6094 | if (par_grp && !(uses->flags & LYS_USESGRP)) { |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 6095 | if (++((struct lys_node_grp *)par_grp)->unres_count == 0) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6096 | LOGERR(ctx, LY_EINT, "Too many unresolved items (uses) inside a grouping."); |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 6097 | return -1; |
| 6098 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6099 | uses->flags |= LYS_USESGRP; |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 6100 | } else { |
| 6101 | /* instantiate grouping only when it is completely resolved */ |
| 6102 | uses->grp = NULL; |
Michal Vasko | 407f1bb | 2015-09-23 15:51:07 +0200 | [diff] [blame] | 6103 | } |
Michal Vasko | a979272 | 2018-06-28 09:01:02 +0200 | [diff] [blame] | 6104 | LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6105 | return EXIT_FAILURE; |
| 6106 | } |
| 6107 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6108 | rc = resolve_uses(uses, unres); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6109 | if (!rc) { |
| 6110 | /* decrease unres count only if not first try */ |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6111 | if (par_grp && (uses->flags & LYS_USESGRP)) { |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 6112 | assert(((struct lys_node_grp *)par_grp)->unres_count); |
| 6113 | ((struct lys_node_grp *)par_grp)->unres_count--; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6114 | uses->flags &= ~LYS_USESGRP; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6115 | } |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 6116 | |
| 6117 | /* check status */ |
Radek Krejci | c655602 | 2016-01-27 15:16:45 +0100 | [diff] [blame] | 6118 | if (lyp_check_status(uses->flags, uses->module, "of uses", |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 6119 | uses->grp->flags, uses->grp->module, uses->grp->name, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6120 | (struct lys_node *)uses)) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 6121 | return -1; |
| 6122 | } |
| 6123 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6124 | return EXIT_SUCCESS; |
| 6125 | } |
| 6126 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6127 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6128 | } |
| 6129 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 6130 | /** |
Michal Vasko | 9957e59 | 2015-08-17 15:04:09 +0200 | [diff] [blame] | 6131 | * @brief Resolve list keys. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 6132 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6133 | * @param[in] list List to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 6134 | * @param[in] keys_str Keys node value. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 6135 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6136 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 6137 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6138 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6139 | resolve_list_keys(struct lys_node_list *list, const char *keys_str) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6140 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6141 | int i, len, rc; |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 6142 | const char *value; |
Radek Krejci | a98048c | 2017-05-24 16:35:48 +0200 | [diff] [blame] | 6143 | char *s = NULL; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6144 | struct ly_ctx *ctx = list->module->ctx; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6145 | |
| 6146 | for (i = 0; i < list->keys_size; ++i) { |
Radek Krejci | dea17dd | 2017-06-02 15:20:43 +0200 | [diff] [blame] | 6147 | assert(keys_str); |
| 6148 | |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 6149 | if (!list->child) { |
| 6150 | /* no child, possible forward reference */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6151 | LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_LYS, list, "list keys", keys_str); |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 6152 | return EXIT_FAILURE; |
| 6153 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6154 | /* get the key name */ |
| 6155 | if ((value = strpbrk(keys_str, " \t\n"))) { |
| 6156 | len = value - keys_str; |
| 6157 | while (isspace(value[0])) { |
| 6158 | value++; |
| 6159 | } |
| 6160 | } else { |
| 6161 | len = strlen(keys_str); |
| 6162 | } |
| 6163 | |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 6164 | rc = lys_getnext_data(lys_node_module((struct lys_node *)list), (struct lys_node *)list, keys_str, len, LYS_LEAF, |
Michal Vasko | 32fb499 | 2019-03-08 08:55:16 +0100 | [diff] [blame] | 6165 | LYS_GETNEXT_NOSTATECHECK, (const struct lys_node **)&list->keys[i]); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6166 | if (rc) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6167 | LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_LYS, list, "list key", keys_str); |
Michal Vasko | 7a55bea | 2016-05-02 14:51:20 +0200 | [diff] [blame] | 6168 | return EXIT_FAILURE; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6169 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6170 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6171 | if (check_key(list, i, keys_str, len)) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6172 | /* check_key logs */ |
| 6173 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6174 | } |
| 6175 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 6176 | /* check status */ |
Radek Krejci | c655602 | 2016-01-27 15:16:45 +0100 | [diff] [blame] | 6177 | if (lyp_check_status(list->flags, list->module, list->name, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6178 | list->keys[i]->flags, list->keys[i]->module, list->keys[i]->name, |
| 6179 | (struct lys_node *)list->keys[i])) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 6180 | return -1; |
| 6181 | } |
| 6182 | |
Radek Krejci | a98048c | 2017-05-24 16:35:48 +0200 | [diff] [blame] | 6183 | /* default value - is ignored, keep it but print a warning */ |
| 6184 | if (list->keys[i]->dflt) { |
| 6185 | /* log is not hidden only in case this resolving fails and in such a case |
| 6186 | * we cannot get here |
| 6187 | */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6188 | assert(log_opt == ILO_STORE); |
| 6189 | log_opt = ILO_LOG; |
| 6190 | LOGWRN(ctx, "Default value \"%s\" in the list key \"%s\" is ignored. (%s)", list->keys[i]->dflt, |
Michal Vasko | 395b0a0 | 2018-01-22 09:36:20 +0100 | [diff] [blame] | 6191 | list->keys[i]->name, s = lys_path((struct lys_node*)list, LYS_PATH_FIRST_PREFIX)); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6192 | log_opt = ILO_STORE; |
Radek Krejci | a98048c | 2017-05-24 16:35:48 +0200 | [diff] [blame] | 6193 | free(s); |
| 6194 | } |
| 6195 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6196 | /* prepare for next iteration */ |
| 6197 | while (value && isspace(value[0])) { |
| 6198 | value++; |
| 6199 | } |
| 6200 | keys_str = value; |
| 6201 | } |
| 6202 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6203 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6204 | } |
| 6205 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6206 | /** |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 6207 | * @brief Resolve (check) all must conditions of \p node. |
| 6208 | * Logs directly. |
| 6209 | * |
| 6210 | * @param[in] node Data node with optional must statements. |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6211 | * @param[in] inout_parent If set, must in input or output parent of node->schema will be resolved. |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 6212 | * |
| 6213 | * @return EXIT_SUCCESS on pass, EXIT_FAILURE on fail, -1 on error. |
| 6214 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6215 | static int |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6216 | resolve_must(struct lyd_node *node, int inout_parent, int ignore_fail) |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6217 | { |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 6218 | uint8_t i, must_size; |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6219 | struct lys_node *schema; |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 6220 | struct lys_restr *must; |
| 6221 | struct lyxp_set set; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6222 | struct ly_ctx *ctx = node->schema->module->ctx; |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 6223 | |
| 6224 | assert(node); |
| 6225 | memset(&set, 0, sizeof set); |
| 6226 | |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6227 | if (inout_parent) { |
| 6228 | for (schema = lys_parent(node->schema); |
| 6229 | schema && (schema->nodetype & (LYS_CHOICE | LYS_CASE | LYS_USES)); |
| 6230 | schema = lys_parent(schema)); |
| 6231 | if (!schema || !(schema->nodetype & (LYS_INPUT | LYS_OUTPUT))) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6232 | LOGINT(ctx); |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6233 | return -1; |
| 6234 | } |
| 6235 | must_size = ((struct lys_node_inout *)schema)->must_size; |
| 6236 | must = ((struct lys_node_inout *)schema)->must; |
| 6237 | |
| 6238 | /* context node is the RPC/action */ |
| 6239 | node = node->parent; |
| 6240 | if (!(node->schema->nodetype & (LYS_RPC | LYS_ACTION))) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6241 | LOGINT(ctx); |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6242 | return -1; |
| 6243 | } |
| 6244 | } else { |
| 6245 | switch (node->schema->nodetype) { |
| 6246 | case LYS_CONTAINER: |
| 6247 | must_size = ((struct lys_node_container *)node->schema)->must_size; |
| 6248 | must = ((struct lys_node_container *)node->schema)->must; |
| 6249 | break; |
| 6250 | case LYS_LEAF: |
| 6251 | must_size = ((struct lys_node_leaf *)node->schema)->must_size; |
| 6252 | must = ((struct lys_node_leaf *)node->schema)->must; |
| 6253 | break; |
| 6254 | case LYS_LEAFLIST: |
| 6255 | must_size = ((struct lys_node_leaflist *)node->schema)->must_size; |
| 6256 | must = ((struct lys_node_leaflist *)node->schema)->must; |
| 6257 | break; |
| 6258 | case LYS_LIST: |
| 6259 | must_size = ((struct lys_node_list *)node->schema)->must_size; |
| 6260 | must = ((struct lys_node_list *)node->schema)->must; |
| 6261 | break; |
| 6262 | case LYS_ANYXML: |
| 6263 | case LYS_ANYDATA: |
| 6264 | must_size = ((struct lys_node_anydata *)node->schema)->must_size; |
| 6265 | must = ((struct lys_node_anydata *)node->schema)->must; |
| 6266 | break; |
| 6267 | case LYS_NOTIF: |
| 6268 | must_size = ((struct lys_node_notif *)node->schema)->must_size; |
| 6269 | must = ((struct lys_node_notif *)node->schema)->must; |
| 6270 | break; |
| 6271 | default: |
| 6272 | must_size = 0; |
| 6273 | break; |
| 6274 | } |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 6275 | } |
| 6276 | |
| 6277 | for (i = 0; i < must_size; ++i) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6278 | if (lyxp_eval(must[i].expr, node, LYXP_NODE_ELEM, lyd_node_module(node), &set, LYXP_MUST)) { |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 6279 | return -1; |
| 6280 | } |
| 6281 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6282 | lyxp_set_cast(&set, LYXP_SET_BOOLEAN, node, lyd_node_module(node), LYXP_MUST); |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 6283 | |
Michal Vasko | 8146d4c | 2016-05-09 15:50:29 +0200 | [diff] [blame] | 6284 | if (!set.val.bool) { |
Michal Vasko | c04173b | 2018-03-09 10:43:22 +0100 | [diff] [blame] | 6285 | if ((ignore_fail == 1) || ((must[i].flags & (LYS_XPCONF_DEP | LYS_XPSTATE_DEP)) && (ignore_fail == 2))) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6286 | LOGVRB("Must condition \"%s\" not satisfied, but it is not required.", must[i].expr); |
| 6287 | } else { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6288 | LOGVAL(ctx, LYE_NOMUST, LY_VLOG_LYD, node, must[i].expr); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6289 | if (must[i].emsg) { |
Michal Vasko | b4b31f6 | 2018-11-23 11:49:20 +0100 | [diff] [blame] | 6290 | ly_vlog_str(ctx, LY_VLOG_PREV, must[i].emsg); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6291 | } |
| 6292 | if (must[i].eapptag) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6293 | ly_err_last_set_apptag(ctx, must[i].eapptag); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6294 | } |
| 6295 | return 1; |
Michal Vasko | 6ac6828 | 2016-04-11 10:56:47 +0200 | [diff] [blame] | 6296 | } |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 6297 | } |
| 6298 | } |
| 6299 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6300 | return EXIT_SUCCESS; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6301 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6302 | |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 6303 | /** |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6304 | * @brief Resolve (find) when condition schema context node. Does not log. |
| 6305 | * |
| 6306 | * @param[in] schema Schema node with the when condition. |
| 6307 | * @param[out] ctx_snode When schema context node. |
| 6308 | * @param[out] ctx_snode_type Schema context node type. |
| 6309 | */ |
| 6310 | void |
| 6311 | resolve_when_ctx_snode(const struct lys_node *schema, struct lys_node **ctx_snode, enum lyxp_node_type *ctx_snode_type) |
| 6312 | { |
| 6313 | const struct lys_node *sparent; |
| 6314 | |
| 6315 | /* find a not schema-only node */ |
| 6316 | *ctx_snode_type = LYXP_NODE_ELEM; |
| 6317 | while (schema->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE | LYS_AUGMENT | LYS_INPUT | LYS_OUTPUT)) { |
| 6318 | if (schema->nodetype == LYS_AUGMENT) { |
| 6319 | sparent = ((struct lys_node_augment *)schema)->target; |
| 6320 | } else { |
| 6321 | sparent = schema->parent; |
| 6322 | } |
| 6323 | if (!sparent) { |
| 6324 | /* context node is the document root (fake root in our case) */ |
| 6325 | if (schema->flags & LYS_CONFIG_W) { |
| 6326 | *ctx_snode_type = LYXP_NODE_ROOT_CONFIG; |
| 6327 | } else { |
Michal Vasko | b94a5e4 | 2016-09-08 14:01:56 +0200 | [diff] [blame] | 6328 | *ctx_snode_type = LYXP_NODE_ROOT; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6329 | } |
Michal Vasko | 2d2aec1 | 2016-09-08 11:42:50 +0200 | [diff] [blame] | 6330 | /* we need the first top-level sibling, but no uses or groupings */ |
Michal Vasko | cb45f47 | 2018-02-12 10:47:42 +0100 | [diff] [blame] | 6331 | schema = lys_getnext(NULL, NULL, lys_node_module(schema), LYS_GETNEXT_NOSTATECHECK); |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6332 | break; |
| 6333 | } |
| 6334 | schema = sparent; |
| 6335 | } |
| 6336 | |
| 6337 | *ctx_snode = (struct lys_node *)schema; |
| 6338 | } |
| 6339 | |
| 6340 | /** |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6341 | * @brief Resolve (find) when condition context node. Does not log. |
| 6342 | * |
| 6343 | * @param[in] node Data node, whose conditional definition is being decided. |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6344 | * @param[in] schema Schema node with the when condition. |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 6345 | * @param[out] ctx_node Context node. |
| 6346 | * @param[out] ctx_node_type Context node type. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6347 | * |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 6348 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6349 | */ |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 6350 | static int |
| 6351 | resolve_when_ctx_node(struct lyd_node *node, struct lys_node *schema, struct lyd_node **ctx_node, |
| 6352 | enum lyxp_node_type *ctx_node_type) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6353 | { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6354 | struct lyd_node *parent; |
| 6355 | struct lys_node *sparent; |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 6356 | enum lyxp_node_type node_type; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6357 | uint16_t i, data_depth, schema_depth; |
| 6358 | |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6359 | resolve_when_ctx_snode(schema, &schema, &node_type); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6360 | |
Michal Vasko | fe98975 | 2016-09-08 08:47:26 +0200 | [diff] [blame] | 6361 | if (node_type == LYXP_NODE_ELEM) { |
| 6362 | /* standard element context node */ |
| 6363 | for (parent = node, data_depth = 0; parent; parent = parent->parent, ++data_depth); |
| 6364 | for (sparent = schema, schema_depth = 0; |
| 6365 | sparent; |
| 6366 | sparent = (sparent->nodetype == LYS_AUGMENT ? ((struct lys_node_augment *)sparent)->target : sparent->parent)) { |
| 6367 | if (sparent->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_NOTIF | LYS_RPC)) { |
| 6368 | ++schema_depth; |
| 6369 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6370 | } |
Michal Vasko | fe98975 | 2016-09-08 08:47:26 +0200 | [diff] [blame] | 6371 | if (data_depth < schema_depth) { |
| 6372 | return -1; |
| 6373 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6374 | |
Michal Vasko | 956e854 | 2016-08-26 09:43:35 +0200 | [diff] [blame] | 6375 | /* find the corresponding data node */ |
| 6376 | for (i = 0; i < data_depth - schema_depth; ++i) { |
| 6377 | node = node->parent; |
| 6378 | } |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 6379 | if (node->schema != schema) { |
| 6380 | return -1; |
| 6381 | } |
Michal Vasko | fe98975 | 2016-09-08 08:47:26 +0200 | [diff] [blame] | 6382 | } else { |
| 6383 | /* root context node */ |
| 6384 | while (node->parent) { |
| 6385 | node = node->parent; |
| 6386 | } |
| 6387 | while (node->prev->next) { |
| 6388 | node = node->prev; |
| 6389 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6390 | } |
| 6391 | |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 6392 | *ctx_node = node; |
| 6393 | *ctx_node_type = node_type; |
| 6394 | return EXIT_SUCCESS; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6395 | } |
| 6396 | |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6397 | /** |
| 6398 | * @brief Temporarily unlink nodes as per YANG 1.1 RFC section 7.21.5 for when XPath evaluation. |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6399 | * The context node is adjusted if needed. |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6400 | * |
| 6401 | * @param[in] snode Schema node, whose children instances need to be unlinked. |
| 6402 | * @param[in,out] node Data siblings where to look for the children of \p snode. If it is unlinked, |
| 6403 | * it is moved to point to another sibling still in the original tree. |
| 6404 | * @param[in,out] ctx_node When context node, adjusted if needed. |
| 6405 | * @param[in] ctx_node_type Context node type, just for information to detect invalid situations. |
| 6406 | * @param[out] unlinked_nodes Unlinked siblings. Can be safely appended to \p node afterwards. |
| 6407 | * Ordering may change, but there will be no semantic change. |
| 6408 | * |
| 6409 | * @return EXIT_SUCCESS on success, -1 on error. |
| 6410 | */ |
| 6411 | static int |
| 6412 | resolve_when_unlink_nodes(struct lys_node *snode, struct lyd_node **node, struct lyd_node **ctx_node, |
| 6413 | enum lyxp_node_type ctx_node_type, struct lyd_node **unlinked_nodes) |
| 6414 | { |
| 6415 | struct lyd_node *next, *elem; |
Michal Vasko | eb81fb7 | 2017-02-06 12:11:08 +0100 | [diff] [blame] | 6416 | const struct lys_node *slast; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6417 | struct ly_ctx *ctx = snode->module->ctx; |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6418 | |
| 6419 | switch (snode->nodetype) { |
| 6420 | case LYS_AUGMENT: |
| 6421 | case LYS_USES: |
| 6422 | case LYS_CHOICE: |
| 6423 | case LYS_CASE: |
Michal Vasko | eb81fb7 | 2017-02-06 12:11:08 +0100 | [diff] [blame] | 6424 | slast = NULL; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 6425 | while ((slast = lys_getnext(slast, snode, NULL, LYS_GETNEXT_PARENTUSES))) { |
Michal Vasko | eb81fb7 | 2017-02-06 12:11:08 +0100 | [diff] [blame] | 6426 | if (slast->nodetype & (LYS_ACTION | LYS_NOTIF)) { |
| 6427 | continue; |
| 6428 | } |
| 6429 | |
| 6430 | if (resolve_when_unlink_nodes((struct lys_node *)slast, node, ctx_node, ctx_node_type, unlinked_nodes)) { |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6431 | return -1; |
| 6432 | } |
| 6433 | } |
| 6434 | break; |
| 6435 | case LYS_CONTAINER: |
| 6436 | case LYS_LIST: |
| 6437 | case LYS_LEAF: |
| 6438 | case LYS_LEAFLIST: |
| 6439 | case LYS_ANYXML: |
| 6440 | case LYS_ANYDATA: |
| 6441 | LY_TREE_FOR_SAFE(lyd_first_sibling(*node), next, elem) { |
| 6442 | if (elem->schema == snode) { |
| 6443 | |
| 6444 | if (elem == *ctx_node) { |
| 6445 | /* We are going to unlink our context node! This normally cannot happen, |
| 6446 | * but we use normal top-level data nodes for faking a document root node, |
| 6447 | * so if this is the context node, we just use the next top-level node. |
| 6448 | * Additionally, it can even happen that there are no top-level data nodes left, |
| 6449 | * all were unlinked, so in this case we pass NULL as the context node/data tree, |
| 6450 | * lyxp_eval() can handle this special situation. |
| 6451 | */ |
| 6452 | if (ctx_node_type == LYXP_NODE_ELEM) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6453 | LOGINT(ctx); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6454 | return -1; |
| 6455 | } |
| 6456 | |
| 6457 | if (elem->prev == elem) { |
| 6458 | /* unlinking last top-level element, use an empty data tree */ |
| 6459 | *ctx_node = NULL; |
| 6460 | } else { |
| 6461 | /* in this case just use the previous/last top-level data node */ |
| 6462 | *ctx_node = elem->prev; |
| 6463 | } |
| 6464 | } else if (elem == *node) { |
| 6465 | /* We are going to unlink the currently processed node. This does not matter that |
| 6466 | * much, but we would lose access to the original data tree, so just move our |
| 6467 | * pointer somewhere still inside it. |
| 6468 | */ |
| 6469 | if ((*node)->prev != *node) { |
| 6470 | *node = (*node)->prev; |
| 6471 | } else { |
| 6472 | /* the processed node with sibings were all unlinked, oh well */ |
| 6473 | *node = NULL; |
| 6474 | } |
| 6475 | } |
| 6476 | |
| 6477 | /* temporarily unlink the node */ |
Michal Vasko | 2bce30c | 2017-02-06 12:16:39 +0100 | [diff] [blame] | 6478 | lyd_unlink_internal(elem, 0); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6479 | if (*unlinked_nodes) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6480 | if (lyd_insert_after((*unlinked_nodes)->prev, elem)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6481 | LOGINT(ctx); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6482 | return -1; |
| 6483 | } |
| 6484 | } else { |
| 6485 | *unlinked_nodes = elem; |
| 6486 | } |
| 6487 | |
| 6488 | if (snode->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_ANYDATA)) { |
| 6489 | /* there can be only one instance */ |
| 6490 | break; |
| 6491 | } |
| 6492 | } |
| 6493 | } |
| 6494 | break; |
| 6495 | default: |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6496 | LOGINT(ctx); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6497 | return -1; |
| 6498 | } |
| 6499 | |
| 6500 | return EXIT_SUCCESS; |
| 6501 | } |
| 6502 | |
| 6503 | /** |
| 6504 | * @brief Relink the unlinked nodes back. |
| 6505 | * |
| 6506 | * @param[in] node Data node to link the nodes back to. It can actually be the adjusted context node, |
| 6507 | * we simply need a sibling from the original data tree. |
| 6508 | * @param[in] unlinked_nodes Unlinked nodes to relink to \p node. |
| 6509 | * @param[in] ctx_node_type Context node type to distinguish between \p node being the parent |
| 6510 | * or the sibling of \p unlinked_nodes. |
| 6511 | * |
| 6512 | * @return EXIT_SUCCESS on success, -1 on error. |
| 6513 | */ |
| 6514 | static int |
| 6515 | resolve_when_relink_nodes(struct lyd_node *node, struct lyd_node *unlinked_nodes, enum lyxp_node_type ctx_node_type) |
| 6516 | { |
| 6517 | struct lyd_node *elem; |
| 6518 | |
| 6519 | LY_TREE_FOR_SAFE(unlinked_nodes, unlinked_nodes, elem) { |
Michal Vasko | 2bce30c | 2017-02-06 12:16:39 +0100 | [diff] [blame] | 6520 | lyd_unlink_internal(elem, 0); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6521 | if (ctx_node_type == LYXP_NODE_ELEM) { |
Michal Vasko | 2bce30c | 2017-02-06 12:16:39 +0100 | [diff] [blame] | 6522 | if (lyd_insert_common(node, NULL, elem, 0)) { |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6523 | return -1; |
| 6524 | } |
| 6525 | } else { |
Michal Vasko | 2bce30c | 2017-02-06 12:16:39 +0100 | [diff] [blame] | 6526 | if (lyd_insert_nextto(node, elem, 0, 0)) { |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6527 | return -1; |
| 6528 | } |
| 6529 | } |
| 6530 | } |
| 6531 | |
| 6532 | return EXIT_SUCCESS; |
| 6533 | } |
| 6534 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6535 | int |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6536 | resolve_applies_must(const struct lyd_node *node) |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 6537 | { |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6538 | int ret = 0; |
| 6539 | uint8_t must_size; |
| 6540 | struct lys_node *schema, *iter; |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 6541 | |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6542 | assert(node); |
| 6543 | |
| 6544 | schema = node->schema; |
| 6545 | |
| 6546 | /* their own must */ |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 6547 | switch (schema->nodetype) { |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 6548 | case LYS_CONTAINER: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6549 | must_size = ((struct lys_node_container *)schema)->must_size; |
| 6550 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 6551 | case LYS_LEAF: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6552 | must_size = ((struct lys_node_leaf *)schema)->must_size; |
| 6553 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 6554 | case LYS_LEAFLIST: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6555 | must_size = ((struct lys_node_leaflist *)schema)->must_size; |
| 6556 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 6557 | case LYS_LIST: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6558 | must_size = ((struct lys_node_list *)schema)->must_size; |
| 6559 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 6560 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 6561 | case LYS_ANYDATA: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6562 | must_size = ((struct lys_node_anydata *)schema)->must_size; |
| 6563 | break; |
| 6564 | case LYS_NOTIF: |
| 6565 | must_size = ((struct lys_node_notif *)schema)->must_size; |
| 6566 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 6567 | default: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6568 | must_size = 0; |
| 6569 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 6570 | } |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6571 | |
| 6572 | if (must_size) { |
| 6573 | ++ret; |
| 6574 | } |
| 6575 | |
| 6576 | /* schema may be a direct data child of input/output with must (but it must be first, it needs to be evaluated only once) */ |
| 6577 | if (!node->prev->next) { |
| 6578 | for (iter = lys_parent(schema); iter && (iter->nodetype & (LYS_CHOICE | LYS_CASE | LYS_USES)); iter = lys_parent(iter)); |
| 6579 | if (iter && (iter->nodetype & (LYS_INPUT | LYS_OUTPUT))) { |
| 6580 | ret += 0x2; |
| 6581 | } |
| 6582 | } |
| 6583 | |
| 6584 | return ret; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 6585 | } |
| 6586 | |
Michal Vasko | dd96229 | 2018-09-10 08:53:01 +0200 | [diff] [blame] | 6587 | static struct lys_when * |
| 6588 | snode_get_when(const struct lys_node *schema) |
| 6589 | { |
| 6590 | switch (schema->nodetype) { |
| 6591 | case LYS_CONTAINER: |
| 6592 | return ((struct lys_node_container *)schema)->when; |
| 6593 | case LYS_CHOICE: |
| 6594 | return ((struct lys_node_choice *)schema)->when; |
| 6595 | case LYS_LEAF: |
| 6596 | return ((struct lys_node_leaf *)schema)->when; |
| 6597 | case LYS_LEAFLIST: |
| 6598 | return ((struct lys_node_leaflist *)schema)->when; |
| 6599 | case LYS_LIST: |
| 6600 | return ((struct lys_node_list *)schema)->when; |
| 6601 | case LYS_ANYDATA: |
| 6602 | case LYS_ANYXML: |
| 6603 | return ((struct lys_node_anydata *)schema)->when; |
| 6604 | case LYS_CASE: |
| 6605 | return ((struct lys_node_case *)schema)->when; |
| 6606 | case LYS_USES: |
| 6607 | return ((struct lys_node_uses *)schema)->when; |
| 6608 | case LYS_AUGMENT: |
| 6609 | return ((struct lys_node_augment *)schema)->when; |
| 6610 | default: |
| 6611 | return NULL; |
| 6612 | } |
| 6613 | } |
| 6614 | |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 6615 | int |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 6616 | resolve_applies_when(const struct lys_node *schema, int mode, const struct lys_node *stop) |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6617 | { |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 6618 | const struct lys_node *parent; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6619 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 6620 | assert(schema); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6621 | |
Michal Vasko | dd96229 | 2018-09-10 08:53:01 +0200 | [diff] [blame] | 6622 | if (!(schema->nodetype & (LYS_NOTIF | LYS_RPC)) && snode_get_when(schema)) { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6623 | return 1; |
| 6624 | } |
| 6625 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 6626 | parent = schema; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6627 | goto check_augment; |
| 6628 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 6629 | while (parent) { |
| 6630 | /* stop conditions */ |
| 6631 | if (!mode) { |
| 6632 | /* stop on node that can be instantiated in data tree */ |
| 6633 | if (!(parent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) { |
| 6634 | break; |
| 6635 | } |
| 6636 | } else { |
| 6637 | /* stop on the specified node */ |
| 6638 | if (parent == stop) { |
| 6639 | break; |
| 6640 | } |
| 6641 | } |
| 6642 | |
Michal Vasko | dd96229 | 2018-09-10 08:53:01 +0200 | [diff] [blame] | 6643 | if (snode_get_when(parent)) { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6644 | return 1; |
| 6645 | } |
| 6646 | check_augment: |
| 6647 | |
Michal Vasko | dd96229 | 2018-09-10 08:53:01 +0200 | [diff] [blame] | 6648 | if (parent->parent && (parent->parent->nodetype == LYS_AUGMENT) && snode_get_when(parent->parent)) { |
Michal Vasko | e365556 | 2016-08-24 15:56:17 +0200 | [diff] [blame] | 6649 | return 1; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6650 | } |
| 6651 | parent = lys_parent(parent); |
| 6652 | } |
| 6653 | |
| 6654 | return 0; |
| 6655 | } |
| 6656 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6657 | /** |
| 6658 | * @brief Resolve (check) all when conditions relevant for \p node. |
| 6659 | * Logs directly. |
| 6660 | * |
| 6661 | * @param[in] node Data node, whose conditional reference, if such, is being decided. |
Michal Vasko | e446b09 | 2017-08-11 10:58:09 +0200 | [diff] [blame] | 6662 | * @param[in] ignore_fail 1 if when does not have to be satisfied, 2 if it does not have to be satisfied |
| 6663 | * only when requiring external dependencies. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6664 | * |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6665 | * @return |
| 6666 | * -1 - error, ly_errno is set |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 6667 | * 0 - all "when" statements true |
| 6668 | * 0, ly_vecode = LYVE_NOWHEN - some "when" statement false, returned in failed_when |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6669 | * 1, ly_vecode = LYVE_INWHEN - nodes needed to resolve are conditional and not yet resolved (under another "when") |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6670 | */ |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 6671 | int |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 6672 | resolve_when(struct lyd_node *node, int ignore_fail, struct lys_when **failed_when) |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6673 | { |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6674 | struct lyd_node *ctx_node = NULL, *unlinked_nodes, *tmp_node; |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6675 | struct lys_node *sparent; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6676 | struct lyxp_set set; |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 6677 | enum lyxp_node_type ctx_node_type; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6678 | struct ly_ctx *ctx = node->schema->module->ctx; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6679 | int rc = 0; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6680 | |
| 6681 | assert(node); |
| 6682 | memset(&set, 0, sizeof set); |
| 6683 | |
Michal Vasko | dd96229 | 2018-09-10 08:53:01 +0200 | [diff] [blame] | 6684 | if (!(node->schema->nodetype & (LYS_NOTIF | LYS_RPC | LYS_ACTION)) && snode_get_when(node->schema)) { |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6685 | /* make the node dummy for the evaluation */ |
| 6686 | node->validity |= LYD_VAL_INUSE; |
Michal Vasko | dd96229 | 2018-09-10 08:53:01 +0200 | [diff] [blame] | 6687 | rc = lyxp_eval(snode_get_when(node->schema)->cond, node, LYXP_NODE_ELEM, lyd_node_module(node), |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6688 | &set, LYXP_WHEN); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6689 | node->validity &= ~LYD_VAL_INUSE; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6690 | if (rc) { |
| 6691 | if (rc == 1) { |
Michal Vasko | dd96229 | 2018-09-10 08:53:01 +0200 | [diff] [blame] | 6692 | LOGVAL(ctx, LYE_INWHEN, LY_VLOG_LYD, node, snode_get_when(node->schema)->cond); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6693 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6694 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6695 | } |
| 6696 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6697 | /* set boolean result of the condition */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6698 | lyxp_set_cast(&set, LYXP_SET_BOOLEAN, node, lyd_node_module(node), LYXP_WHEN); |
Michal Vasko | 8146d4c | 2016-05-09 15:50:29 +0200 | [diff] [blame] | 6699 | if (!set.val.bool) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6700 | node->when_status |= LYD_WHEN_FALSE; |
Michal Vasko | dd96229 | 2018-09-10 08:53:01 +0200 | [diff] [blame] | 6701 | if ((ignore_fail == 1) || ((snode_get_when(node->schema)->flags & (LYS_XPCONF_DEP | LYS_XPSTATE_DEP)) |
Michal Vasko | c04173b | 2018-03-09 10:43:22 +0100 | [diff] [blame] | 6702 | && (ignore_fail == 2))) { |
Michal Vasko | dd96229 | 2018-09-10 08:53:01 +0200 | [diff] [blame] | 6703 | LOGVRB("When condition \"%s\" is not satisfied, but it is not required.", snode_get_when(node->schema)->cond); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6704 | } else { |
Michal Vasko | dd96229 | 2018-09-10 08:53:01 +0200 | [diff] [blame] | 6705 | LOGVAL(ctx, LYE_NOWHEN, LY_VLOG_LYD, node, snode_get_when(node->schema)->cond); |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 6706 | if (failed_when) { |
Michal Vasko | dd96229 | 2018-09-10 08:53:01 +0200 | [diff] [blame] | 6707 | *failed_when = snode_get_when(node->schema); |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 6708 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6709 | goto cleanup; |
| 6710 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6711 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6712 | |
| 6713 | /* free xpath set content */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6714 | lyxp_set_cast(&set, LYXP_SET_EMPTY, node, lyd_node_module(node), 0); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6715 | } |
| 6716 | |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6717 | sparent = node->schema; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6718 | goto check_augment; |
| 6719 | |
| 6720 | /* check when in every schema node that affects node */ |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6721 | while (sparent && (sparent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) { |
Michal Vasko | dd96229 | 2018-09-10 08:53:01 +0200 | [diff] [blame] | 6722 | if (snode_get_when(sparent)) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6723 | if (!ctx_node) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6724 | rc = resolve_when_ctx_node(node, sparent, &ctx_node, &ctx_node_type); |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 6725 | if (rc) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6726 | LOGINT(ctx); |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6727 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6728 | } |
| 6729 | } |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6730 | |
| 6731 | unlinked_nodes = NULL; |
| 6732 | /* we do not want our node pointer to change */ |
| 6733 | tmp_node = node; |
| 6734 | rc = resolve_when_unlink_nodes(sparent, &tmp_node, &ctx_node, ctx_node_type, &unlinked_nodes); |
| 6735 | if (rc) { |
| 6736 | goto cleanup; |
| 6737 | } |
| 6738 | |
Michal Vasko | dd96229 | 2018-09-10 08:53:01 +0200 | [diff] [blame] | 6739 | rc = lyxp_eval(snode_get_when(sparent)->cond, ctx_node, ctx_node_type, lys_node_module(sparent), |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6740 | &set, LYXP_WHEN); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6741 | |
| 6742 | if (unlinked_nodes && ctx_node) { |
| 6743 | if (resolve_when_relink_nodes(ctx_node, unlinked_nodes, ctx_node_type)) { |
| 6744 | rc = -1; |
| 6745 | goto cleanup; |
| 6746 | } |
| 6747 | } |
| 6748 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6749 | if (rc) { |
| 6750 | if (rc == 1) { |
Michal Vasko | dd96229 | 2018-09-10 08:53:01 +0200 | [diff] [blame] | 6751 | LOGVAL(ctx, LYE_INWHEN, LY_VLOG_LYD, node, snode_get_when(sparent)->cond); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6752 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6753 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6754 | } |
| 6755 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6756 | lyxp_set_cast(&set, LYXP_SET_BOOLEAN, ctx_node, lys_node_module(sparent), LYXP_WHEN); |
Michal Vasko | 8146d4c | 2016-05-09 15:50:29 +0200 | [diff] [blame] | 6757 | if (!set.val.bool) { |
Michal Vasko | dd96229 | 2018-09-10 08:53:01 +0200 | [diff] [blame] | 6758 | if ((ignore_fail == 1) || ((snode_get_when(sparent)->flags & (LYS_XPCONF_DEP | LYS_XPSTATE_DEP)) |
| 6759 | && (ignore_fail == 2))) { |
| 6760 | LOGVRB("When condition \"%s\" is not satisfied, but it is not required.", snode_get_when(sparent)->cond); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6761 | } else { |
Michal Vasko | 2cb18e7 | 2017-03-28 14:46:33 +0200 | [diff] [blame] | 6762 | node->when_status |= LYD_WHEN_FALSE; |
Michal Vasko | dd96229 | 2018-09-10 08:53:01 +0200 | [diff] [blame] | 6763 | LOGVAL(ctx, LYE_NOWHEN, LY_VLOG_LYD, node, snode_get_when(sparent)->cond); |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 6764 | if (failed_when) { |
Michal Vasko | dd96229 | 2018-09-10 08:53:01 +0200 | [diff] [blame] | 6765 | *failed_when = snode_get_when(sparent); |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 6766 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6767 | goto cleanup; |
| 6768 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6769 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6770 | |
| 6771 | /* free xpath set content */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6772 | lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node, lys_node_module(sparent), 0); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6773 | } |
| 6774 | |
| 6775 | check_augment: |
Michal Vasko | dd96229 | 2018-09-10 08:53:01 +0200 | [diff] [blame] | 6776 | if ((sparent->parent && (sparent->parent->nodetype == LYS_AUGMENT) && snode_get_when(sparent->parent))) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6777 | if (!ctx_node) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6778 | rc = resolve_when_ctx_node(node, sparent->parent, &ctx_node, &ctx_node_type); |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 6779 | if (rc) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6780 | LOGINT(ctx); |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6781 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6782 | } |
| 6783 | } |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6784 | |
| 6785 | unlinked_nodes = NULL; |
| 6786 | tmp_node = node; |
| 6787 | rc = resolve_when_unlink_nodes(sparent->parent, &tmp_node, &ctx_node, ctx_node_type, &unlinked_nodes); |
| 6788 | if (rc) { |
| 6789 | goto cleanup; |
| 6790 | } |
| 6791 | |
Michal Vasko | dd96229 | 2018-09-10 08:53:01 +0200 | [diff] [blame] | 6792 | rc = lyxp_eval(snode_get_when(sparent->parent)->cond, ctx_node, ctx_node_type, |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6793 | lys_node_module(sparent->parent), &set, LYXP_WHEN); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6794 | |
| 6795 | /* reconnect nodes, if ctx_node is NULL then all the nodes were unlinked, but linked together, |
| 6796 | * so the tree did not actually change and there is nothing for us to do |
| 6797 | */ |
| 6798 | if (unlinked_nodes && ctx_node) { |
| 6799 | if (resolve_when_relink_nodes(ctx_node, unlinked_nodes, ctx_node_type)) { |
| 6800 | rc = -1; |
| 6801 | goto cleanup; |
| 6802 | } |
| 6803 | } |
| 6804 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6805 | if (rc) { |
| 6806 | if (rc == 1) { |
Michal Vasko | dd96229 | 2018-09-10 08:53:01 +0200 | [diff] [blame] | 6807 | LOGVAL(ctx, LYE_INWHEN, LY_VLOG_LYD, node, snode_get_when(sparent->parent)->cond); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6808 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6809 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6810 | } |
| 6811 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6812 | lyxp_set_cast(&set, LYXP_SET_BOOLEAN, ctx_node, lys_node_module(sparent->parent), LYXP_WHEN); |
Michal Vasko | 8146d4c | 2016-05-09 15:50:29 +0200 | [diff] [blame] | 6813 | if (!set.val.bool) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6814 | node->when_status |= LYD_WHEN_FALSE; |
Michal Vasko | dd96229 | 2018-09-10 08:53:01 +0200 | [diff] [blame] | 6815 | if ((ignore_fail == 1) || ((snode_get_when(sparent->parent)->flags & (LYS_XPCONF_DEP | LYS_XPSTATE_DEP)) |
Michal Vasko | c04173b | 2018-03-09 10:43:22 +0100 | [diff] [blame] | 6816 | && (ignore_fail == 2))) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6817 | LOGVRB("When condition \"%s\" is not satisfied, but it is not required.", |
Michal Vasko | dd96229 | 2018-09-10 08:53:01 +0200 | [diff] [blame] | 6818 | snode_get_when(sparent->parent)->cond); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6819 | } else { |
Michal Vasko | dd96229 | 2018-09-10 08:53:01 +0200 | [diff] [blame] | 6820 | LOGVAL(ctx, LYE_NOWHEN, LY_VLOG_LYD, node, snode_get_when(sparent->parent)->cond); |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 6821 | if (failed_when) { |
Michal Vasko | dd96229 | 2018-09-10 08:53:01 +0200 | [diff] [blame] | 6822 | *failed_when = snode_get_when(sparent->parent); |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 6823 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6824 | goto cleanup; |
| 6825 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6826 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6827 | |
| 6828 | /* free xpath set content */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6829 | lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node, lys_node_module(sparent->parent), 0); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6830 | } |
| 6831 | |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6832 | sparent = lys_parent(sparent); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6833 | } |
| 6834 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6835 | node->when_status |= LYD_WHEN_TRUE; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6836 | |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6837 | cleanup: |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6838 | /* free xpath set content */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6839 | lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node ? ctx_node : node, NULL, 0); |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6840 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6841 | } |
| 6842 | |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6843 | static int |
Michal Vasko | e1c7a82 | 2017-06-30 13:15:18 +0200 | [diff] [blame] | 6844 | check_type_union_leafref(struct lys_type *type) |
| 6845 | { |
| 6846 | uint8_t i; |
| 6847 | |
| 6848 | if ((type->base == LY_TYPE_UNION) && type->info.uni.count) { |
| 6849 | /* go through unions and look for leafref */ |
| 6850 | for (i = 0; i < type->info.uni.count; ++i) { |
| 6851 | switch (type->info.uni.types[i].base) { |
| 6852 | case LY_TYPE_LEAFREF: |
| 6853 | return 1; |
| 6854 | case LY_TYPE_UNION: |
| 6855 | if (check_type_union_leafref(&type->info.uni.types[i])) { |
| 6856 | return 1; |
| 6857 | } |
| 6858 | break; |
| 6859 | default: |
| 6860 | break; |
| 6861 | } |
| 6862 | } |
| 6863 | |
| 6864 | return 0; |
| 6865 | } |
| 6866 | |
| 6867 | /* just inherit the flag value */ |
| 6868 | return type->der->has_union_leafref; |
| 6869 | } |
| 6870 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6871 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6872 | * @brief Resolve a single unres schema item. Logs indirectly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6873 | * |
| 6874 | * @param[in] mod Main module. |
| 6875 | * @param[in] item Item to resolve. Type determined by \p type. |
| 6876 | * @param[in] type Type of the unresolved item. |
| 6877 | * @param[in] str_snode String, a schema node, or NULL. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6878 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 769f803 | 2017-01-24 13:11:55 +0100 | [diff] [blame] | 6879 | * @param[in] final_fail Whether we are just printing errors of the failed unres items. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6880 | * |
| 6881 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 6882 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6883 | static int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 6884 | resolve_unres_schema_item(struct lys_module *mod, void *item, enum UNRES_ITEM type, void *str_snode, |
Michal Vasko | f96dfb6 | 2017-08-17 12:23:49 +0200 | [diff] [blame] | 6885 | struct unres_schema *unres) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6886 | { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6887 | /* has_str - whether the str_snode is a string in a dictionary that needs to be freed */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6888 | int rc = -1, has_str = 0, parent_type = 0, i, k; |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6889 | unsigned int j; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6890 | struct ly_ctx * ctx = mod->ctx; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6891 | struct lys_node *root, *next, *node, *par_grp; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6892 | const char *expr; |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 6893 | uint8_t *u; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6894 | |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6895 | struct ly_set *refs, *procs; |
| 6896 | struct lys_feature *ref, *feat; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6897 | struct lys_ident *ident; |
| 6898 | struct lys_type *stype; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6899 | struct lys_node_choice *choic; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6900 | struct lyxml_elem *yin; |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6901 | struct yang_type *yang; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6902 | struct unres_list_uniq *unique_info; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6903 | struct unres_iffeat_data *iff_data; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6904 | struct unres_ext *ext_data; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6905 | struct lys_ext_instance *ext, **extlist; |
| 6906 | struct lyext_plugin *eplugin; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6907 | |
| 6908 | switch (type) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6909 | case UNRES_IDENT: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6910 | expr = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 6911 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6912 | ident = item; |
| 6913 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6914 | rc = resolve_base_ident(mod, ident, expr, "identity", NULL, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6915 | break; |
| 6916 | case UNRES_TYPE_IDENTREF: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6917 | expr = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 6918 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6919 | stype = item; |
| 6920 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6921 | rc = resolve_base_ident(mod, NULL, expr, "type", stype, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6922 | break; |
| 6923 | case UNRES_TYPE_LEAFREF: |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 6924 | node = str_snode; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6925 | stype = item; |
| 6926 | |
Michal Vasko | 74083ec | 2018-06-15 10:00:12 +0200 | [diff] [blame] | 6927 | rc = resolve_schema_leafref(stype, node, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6928 | break; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 6929 | case UNRES_TYPE_DER_EXT: |
| 6930 | parent_type++; |
Radek Krejci | 3d679d7 | 2017-08-01 10:44:37 +0200 | [diff] [blame] | 6931 | /* falls through */ |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 6932 | case UNRES_TYPE_DER_TPDF: |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 6933 | parent_type++; |
Radek Krejci | 3d679d7 | 2017-08-01 10:44:37 +0200 | [diff] [blame] | 6934 | /* falls through */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6935 | case UNRES_TYPE_DER: |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6936 | /* parent */ |
| 6937 | node = str_snode; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6938 | stype = item; |
| 6939 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6940 | /* HACK type->der is temporarily unparsed type statement */ |
| 6941 | yin = (struct lyxml_elem *)stype->der; |
| 6942 | stype->der = NULL; |
| 6943 | |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6944 | if (yin->flags & LY_YANG_STRUCTURE_FLAG) { |
| 6945 | yang = (struct yang_type *)yin; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 6946 | rc = yang_check_type(mod, node, yang, stype, parent_type, unres); |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6947 | |
| 6948 | if (rc) { |
Pavol Vican | 8bd72e4 | 2016-08-29 09:53:05 +0200 | [diff] [blame] | 6949 | /* may try again later */ |
| 6950 | stype->der = (struct lys_tpdf *)yang; |
Pavol Vican | d01d8ae | 2016-03-01 10:45:59 +0100 | [diff] [blame] | 6951 | } else { |
| 6952 | /* we need to always be able to free this, it's safe only in this case */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6953 | lydict_remove(ctx, yang->name); |
Pavol Vican | d01d8ae | 2016-03-01 10:45:59 +0100 | [diff] [blame] | 6954 | free(yang); |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6955 | } |
| 6956 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6957 | } else { |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 6958 | rc = fill_yin_type(mod, node, yin, stype, parent_type, unres); |
Radek Krejci | 63fc096 | 2017-02-15 13:20:18 +0100 | [diff] [blame] | 6959 | if (!rc || rc == -1) { |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6960 | /* we need to always be able to free this, it's safe only in this case */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6961 | lyxml_free(ctx, yin); |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6962 | } else { |
| 6963 | /* may try again later, put all back how it was */ |
| 6964 | stype->der = (struct lys_tpdf *)yin; |
| 6965 | } |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6966 | } |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 6967 | if (rc == EXIT_SUCCESS) { |
Radek Krejci | 2c2fce8 | 2016-08-01 13:52:26 +0200 | [diff] [blame] | 6968 | /* it does not make sense to have leaf-list of empty type */ |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 6969 | if (!parent_type && node->nodetype == LYS_LEAFLIST && stype->base == LY_TYPE_EMPTY) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6970 | LOGWRN(ctx, "The leaf-list \"%s\" is of \"empty\" type, which does not make sense.", node->name); |
Radek Krejci | 2c2fce8 | 2016-08-01 13:52:26 +0200 | [diff] [blame] | 6971 | } |
Michal Vasko | e1c7a82 | 2017-06-30 13:15:18 +0200 | [diff] [blame] | 6972 | |
| 6973 | if ((type == UNRES_TYPE_DER_TPDF) && (stype->base == LY_TYPE_UNION)) { |
| 6974 | /* fill typedef union leafref flag */ |
| 6975 | ((struct lys_tpdf *)stype->parent)->has_union_leafref = check_type_union_leafref(stype); |
| 6976 | } else if ((type == UNRES_TYPE_DER) && stype->der->has_union_leafref) { |
| 6977 | /* copy the type in case it has union leafref flag */ |
| 6978 | if (lys_copy_union_leafrefs(mod, node, stype, NULL, unres)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6979 | LOGERR(ctx, LY_EINT, "Failed to duplicate type."); |
Michal Vasko | e1c7a82 | 2017-06-30 13:15:18 +0200 | [diff] [blame] | 6980 | return -1; |
| 6981 | } |
| 6982 | } |
Michal Vasko | 101658e | 2018-06-05 15:05:54 +0200 | [diff] [blame] | 6983 | } else if (rc == EXIT_FAILURE && !(stype->value_flags & LY_VALUE_UNRESGRP)) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 6984 | /* forward reference - in case the type is in grouping, we have to make the grouping unusable |
| 6985 | * by uses statement until the type is resolved. We do that the same way as uses statements inside |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 6986 | * grouping. The grouping cannot be used unless the unres counter is 0. |
Michal Vasko | 70bf8e5 | 2018-03-26 11:32:33 +0200 | [diff] [blame] | 6987 | * To remember that the grouping already increased the counter, the LYTYPE_GRP is used as value |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 6988 | * of the type's base member. */ |
| 6989 | for (par_grp = node; par_grp && (par_grp->nodetype != LYS_GROUPING); par_grp = lys_parent(par_grp)); |
| 6990 | if (par_grp) { |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 6991 | if (++((struct lys_node_grp *)par_grp)->unres_count == 0) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6992 | LOGERR(ctx, LY_EINT, "Too many unresolved items (type) inside a grouping."); |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 6993 | return -1; |
| 6994 | } |
Michal Vasko | 101658e | 2018-06-05 15:05:54 +0200 | [diff] [blame] | 6995 | stype->value_flags |= LY_VALUE_UNRESGRP; |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 6996 | } |
Radek Krejci | 2c2fce8 | 2016-08-01 13:52:26 +0200 | [diff] [blame] | 6997 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6998 | break; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6999 | case UNRES_IFFEAT: |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7000 | iff_data = str_snode; |
| 7001 | rc = resolve_feature(iff_data->fname, strlen(iff_data->fname), iff_data->node, item); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 7002 | if (!rc) { |
| 7003 | /* success */ |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 7004 | if (iff_data->infeature) { |
| 7005 | /* store backlink into the target feature to allow reverse changes in case of changing feature status */ |
| 7006 | feat = *((struct lys_feature **)item); |
| 7007 | if (!feat->depfeatures) { |
| 7008 | feat->depfeatures = ly_set_new(); |
| 7009 | } |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 7010 | ly_set_add(feat->depfeatures, iff_data->node, LY_SET_OPT_USEASLIST); |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 7011 | } |
| 7012 | /* cleanup temporary data */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7013 | lydict_remove(ctx, iff_data->fname); |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7014 | free(iff_data); |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 7015 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7016 | break; |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 7017 | case UNRES_FEATURE: |
| 7018 | feat = (struct lys_feature *)item; |
| 7019 | |
| 7020 | if (feat->iffeature_size) { |
| 7021 | refs = ly_set_new(); |
| 7022 | procs = ly_set_new(); |
| 7023 | ly_set_add(procs, feat, 0); |
| 7024 | |
| 7025 | while (procs->number) { |
| 7026 | ref = procs->set.g[procs->number - 1]; |
| 7027 | ly_set_rm_index(procs, procs->number - 1); |
| 7028 | |
| 7029 | for (i = 0; i < ref->iffeature_size; i++) { |
| 7030 | resolve_iffeature_getsizes(&ref->iffeature[i], NULL, &j); |
| 7031 | for (; j > 0 ; j--) { |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7032 | if (ref->iffeature[i].features[j - 1]) { |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 7033 | if (ref->iffeature[i].features[j - 1] == feat) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7034 | LOGVAL(ctx, LYE_CIRC_FEATURES, LY_VLOG_NONE, NULL, feat->name); |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 7035 | goto featurecheckdone; |
| 7036 | } |
| 7037 | |
| 7038 | if (ref->iffeature[i].features[j - 1]->iffeature_size) { |
| 7039 | k = refs->number; |
| 7040 | if (ly_set_add(refs, ref->iffeature[i].features[j - 1], 0) == k) { |
| 7041 | /* not yet seen feature, add it for processing */ |
| 7042 | ly_set_add(procs, ref->iffeature[i].features[j - 1], 0); |
| 7043 | } |
| 7044 | } |
| 7045 | } else { |
| 7046 | /* forward reference */ |
| 7047 | rc = EXIT_FAILURE; |
| 7048 | goto featurecheckdone; |
| 7049 | } |
| 7050 | } |
| 7051 | |
| 7052 | } |
| 7053 | } |
| 7054 | rc = EXIT_SUCCESS; |
| 7055 | |
| 7056 | featurecheckdone: |
| 7057 | ly_set_free(refs); |
| 7058 | ly_set_free(procs); |
| 7059 | } |
| 7060 | |
| 7061 | break; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7062 | case UNRES_USES: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7063 | rc = resolve_unres_schema_uses(item, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7064 | break; |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 7065 | case UNRES_TYPEDEF_DFLT: |
| 7066 | parent_type++; |
Radek Krejci | 3d679d7 | 2017-08-01 10:44:37 +0200 | [diff] [blame] | 7067 | /* falls through */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7068 | case UNRES_TYPE_DFLT: |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 7069 | stype = item; |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 7070 | rc = check_default(stype, (const char **)str_snode, mod, parent_type); |
Michal Vasko | c40a209 | 2018-11-20 09:32:23 +0100 | [diff] [blame] | 7071 | if ((rc == EXIT_FAILURE) && !parent_type && (stype->base == LY_TYPE_LEAFREF)) { |
Michal Vasko | 7d89492 | 2018-11-12 11:55:55 +0100 | [diff] [blame] | 7072 | for (par_grp = (struct lys_node *)stype->parent; |
| 7073 | par_grp && (par_grp->nodetype != LYS_GROUPING); |
| 7074 | par_grp = lys_parent(par_grp)); |
| 7075 | if (par_grp) { |
| 7076 | /* checking default value in a grouping finished with forward reference means we cannot check the value */ |
| 7077 | rc = EXIT_SUCCESS; |
| 7078 | } |
| 7079 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7080 | break; |
| 7081 | case UNRES_CHOICE_DFLT: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 7082 | expr = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 7083 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 7084 | choic = item; |
| 7085 | |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 7086 | if (!choic->dflt) { |
| 7087 | choic->dflt = resolve_choice_dflt(choic, expr); |
| 7088 | } |
Michal Vasko | 7955b36 | 2015-09-04 14:18:15 +0200 | [diff] [blame] | 7089 | if (choic->dflt) { |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 7090 | rc = lyp_check_mandatory_choice((struct lys_node *)choic); |
Michal Vasko | 7955b36 | 2015-09-04 14:18:15 +0200 | [diff] [blame] | 7091 | } else { |
| 7092 | rc = EXIT_FAILURE; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 7093 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7094 | break; |
| 7095 | case UNRES_LIST_KEYS: |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 7096 | rc = resolve_list_keys(item, ((struct lys_node_list *)item)->keys_str); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7097 | break; |
| 7098 | case UNRES_LIST_UNIQ: |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7099 | unique_info = (struct unres_list_uniq *)item; |
| 7100 | rc = resolve_unique(unique_info->list, unique_info->expr, unique_info->trg_type); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7101 | break; |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 7102 | case UNRES_AUGMENT: |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 7103 | rc = resolve_augment(item, NULL, unres); |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 7104 | break; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 7105 | case UNRES_XPATH: |
| 7106 | node = (struct lys_node *)item; |
Michal Vasko | 895c11f | 2018-03-12 11:35:58 +0100 | [diff] [blame] | 7107 | rc = check_xpath(node, 1); |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 7108 | break; |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 7109 | case UNRES_MOD_IMPLEMENT: |
| 7110 | rc = lys_make_implemented_r(mod, unres); |
| 7111 | break; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 7112 | case UNRES_EXT: |
| 7113 | ext_data = (struct unres_ext *)str_snode; |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 7114 | extlist = &(*(struct lys_ext_instance ***)item)[ext_data->ext_index]; |
Radek Krejci | a7db970 | 2017-01-20 12:55:14 +0100 | [diff] [blame] | 7115 | rc = resolve_extension(ext_data, extlist, unres); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 7116 | if (!rc) { |
Radek Krejci | 79685c9 | 2017-02-17 10:49:43 +0100 | [diff] [blame] | 7117 | /* success */ |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7118 | /* is there a callback to be done to finalize the extension? */ |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 7119 | eplugin = extlist[0]->def->plugin; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7120 | if (eplugin) { |
| 7121 | if (eplugin->check_result || (eplugin->flags & LYEXT_OPT_INHERIT)) { |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 7122 | u = malloc(sizeof *u); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7123 | LY_CHECK_ERR_RETURN(!u, LOGMEM(ctx), -1); |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 7124 | (*u) = ext_data->ext_index; |
Radek Krejci | b08bc17 | 2017-02-27 13:17:14 +0100 | [diff] [blame] | 7125 | if (unres_schema_add_node(mod, unres, item, UNRES_EXT_FINALIZE, (struct lys_node *)u) == -1) { |
fredgan | 31f56fd | 2019-09-26 14:34:03 +0800 | [diff] [blame] | 7126 | /* something really bad happened since the extension finalization is not actually |
Radek Krejci | b08bc17 | 2017-02-27 13:17:14 +0100 | [diff] [blame] | 7127 | * being resolved while adding into unres, so something more serious with the unres |
| 7128 | * list itself must happened */ |
| 7129 | return -1; |
| 7130 | } |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7131 | } |
| 7132 | } |
Radek Krejci | 79685c9 | 2017-02-17 10:49:43 +0100 | [diff] [blame] | 7133 | } |
| 7134 | if (!rc || rc == -1) { |
| 7135 | /* cleanup on success or fatal error */ |
| 7136 | if (ext_data->datatype == LYS_IN_YIN) { |
| 7137 | /* YIN */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7138 | lyxml_free(ctx, ext_data->data.yin); |
Radek Krejci | 79685c9 | 2017-02-17 10:49:43 +0100 | [diff] [blame] | 7139 | } else { |
PavolVican | db0e817 | 2017-02-20 00:46:09 +0100 | [diff] [blame] | 7140 | /* YANG */ |
| 7141 | yang_free_ext_data(ext_data->data.yang); |
Radek Krejci | 79685c9 | 2017-02-17 10:49:43 +0100 | [diff] [blame] | 7142 | } |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 7143 | free(ext_data); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 7144 | } |
| 7145 | break; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7146 | case UNRES_EXT_FINALIZE: |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 7147 | u = (uint8_t *)str_snode; |
| 7148 | ext = (*(struct lys_ext_instance ***)item)[*u]; |
| 7149 | free(u); |
| 7150 | |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7151 | eplugin = ext->def->plugin; |
| 7152 | |
| 7153 | /* inherit */ |
| 7154 | if ((eplugin->flags & LYEXT_OPT_INHERIT) && (ext->parent_type == LYEXT_PAR_NODE)) { |
| 7155 | root = (struct lys_node *)ext->parent; |
| 7156 | if (!(root->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) { |
| 7157 | LY_TREE_DFS_BEGIN(root->child, next, node) { |
| 7158 | /* first, check if the node already contain instance of the same extension, |
| 7159 | * in such a case we won't inherit. In case the node was actually defined as |
| 7160 | * augment data, we are supposed to check the same way also the augment node itself */ |
| 7161 | if (lys_ext_instance_presence(ext->def, node->ext, node->ext_size) != -1) { |
| 7162 | goto inherit_dfs_sibling; |
| 7163 | } else if (node->parent != root && node->parent->nodetype == LYS_AUGMENT && |
| 7164 | lys_ext_instance_presence(ext->def, node->parent->ext, node->parent->ext_size) != -1) { |
| 7165 | goto inherit_dfs_sibling; |
| 7166 | } |
| 7167 | |
| 7168 | if (eplugin->check_inherit) { |
| 7169 | /* we have a callback to check the inheritance, use it */ |
| 7170 | switch ((rc = (*eplugin->check_inherit)(ext, node))) { |
| 7171 | case 0: |
| 7172 | /* yes - continue with the inheriting code */ |
| 7173 | break; |
| 7174 | case 1: |
| 7175 | /* no - continue with the node's sibling */ |
| 7176 | goto inherit_dfs_sibling; |
| 7177 | case 2: |
| 7178 | /* no, but continue with the children, just skip the inheriting code for this node */ |
| 7179 | goto inherit_dfs_child; |
| 7180 | default: |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7181 | LOGERR(ctx, LY_EINT, "Plugin's (%s:%s) check_inherit callback returns invalid value (%d),", |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7182 | ext->def->module->name, ext->def->name, rc); |
| 7183 | } |
| 7184 | } |
| 7185 | |
| 7186 | /* inherit the extension */ |
| 7187 | extlist = realloc(node->ext, (node->ext_size + 1) * sizeof *node->ext); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7188 | LY_CHECK_ERR_RETURN(!extlist, LOGMEM(ctx), -1); |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7189 | extlist[node->ext_size] = malloc(sizeof **extlist); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7190 | LY_CHECK_ERR_RETURN(!extlist[node->ext_size], LOGMEM(ctx); node->ext = extlist, -1); |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7191 | memcpy(extlist[node->ext_size], ext, sizeof *ext); |
| 7192 | extlist[node->ext_size]->flags |= LYEXT_OPT_INHERIT; |
| 7193 | |
| 7194 | node->ext = extlist; |
| 7195 | node->ext_size++; |
| 7196 | |
| 7197 | inherit_dfs_child: |
| 7198 | /* modification of - select element for the next run - children first */ |
| 7199 | if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
| 7200 | next = NULL; |
| 7201 | } else { |
| 7202 | next = node->child; |
| 7203 | } |
| 7204 | if (!next) { |
| 7205 | inherit_dfs_sibling: |
| 7206 | /* no children, try siblings */ |
| 7207 | next = node->next; |
| 7208 | } |
| 7209 | while (!next) { |
| 7210 | /* go to the parent */ |
| 7211 | node = lys_parent(node); |
| 7212 | |
| 7213 | /* we are done if we are back in the root (the starter's parent */ |
| 7214 | if (node == root) { |
| 7215 | break; |
| 7216 | } |
| 7217 | |
| 7218 | /* parent is already processed, go to its sibling */ |
| 7219 | next = node->next; |
| 7220 | } |
| 7221 | } |
| 7222 | } |
| 7223 | } |
| 7224 | |
| 7225 | /* final check */ |
| 7226 | if (eplugin->check_result) { |
| 7227 | if ((*eplugin->check_result)(ext)) { |
Michal Vasko | c6cd3f0 | 2018-03-02 14:07:42 +0100 | [diff] [blame] | 7228 | LOGERR(ctx, LY_EPLUGIN, "Resolving extension failed."); |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7229 | return -1; |
| 7230 | } |
| 7231 | } |
| 7232 | |
| 7233 | rc = 0; |
| 7234 | break; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7235 | default: |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7236 | LOGINT(ctx); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7237 | break; |
| 7238 | } |
| 7239 | |
Radek Krejci | 54081ce | 2016-08-12 15:21:47 +0200 | [diff] [blame] | 7240 | if (has_str && !rc) { |
| 7241 | /* the string is no more needed in case of success. |
| 7242 | * In case of forward reference, we will try to resolve the string later */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7243 | lydict_remove(ctx, str_snode); |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 7244 | } |
| 7245 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7246 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7247 | } |
| 7248 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7249 | /* logs directly */ |
| 7250 | static void |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7251 | print_unres_schema_item_fail(void *item, enum UNRES_ITEM type, void *str_node) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7252 | { |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 7253 | struct lyxml_elem *xml; |
| 7254 | struct lyxml_attr *attr; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7255 | struct unres_iffeat_data *iff_data; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 7256 | const char *name = NULL; |
| 7257 | struct unres_ext *extinfo; |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 7258 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7259 | switch (type) { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7260 | case UNRES_IDENT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7261 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "identity", (char *)str_node); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7262 | break; |
| 7263 | case UNRES_TYPE_IDENTREF: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7264 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "identityref", (char *)str_node); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7265 | break; |
| 7266 | case UNRES_TYPE_LEAFREF: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7267 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "leafref", |
| 7268 | ((struct lys_type *)item)->info.lref.path); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7269 | break; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 7270 | case UNRES_TYPE_DER_EXT: |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 7271 | case UNRES_TYPE_DER_TPDF: |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7272 | case UNRES_TYPE_DER: |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 7273 | xml = (struct lyxml_elem *)((struct lys_type *)item)->der; |
| 7274 | if (xml->flags & LY_YANG_STRUCTURE_FLAG) { |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 7275 | name = ((struct yang_type *)xml)->name; |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 7276 | } else { |
| 7277 | LY_TREE_FOR(xml->attr, attr) { |
| 7278 | if ((attr->type == LYXML_ATTR_STD) && !strcmp(attr->name, "name")) { |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 7279 | name = attr->value; |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 7280 | break; |
| 7281 | } |
| 7282 | } |
| 7283 | assert(attr); |
| 7284 | } |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 7285 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "derived type", name); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7286 | break; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7287 | case UNRES_IFFEAT: |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7288 | iff_data = str_node; |
| 7289 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "if-feature", iff_data->fname); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7290 | break; |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 7291 | case UNRES_FEATURE: |
| 7292 | LOGVRB("There are unresolved if-features for \"%s\" feature circular dependency check, it will be attempted later", |
| 7293 | ((struct lys_feature *)item)->name); |
| 7294 | break; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7295 | case UNRES_USES: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7296 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "uses", ((struct lys_node_uses *)item)->name); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7297 | break; |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 7298 | case UNRES_TYPEDEF_DFLT: |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7299 | case UNRES_TYPE_DFLT: |
Michal Vasko | ba835cd | 2018-02-23 09:25:48 +0100 | [diff] [blame] | 7300 | if (*(char **)str_node) { |
| 7301 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "type default", *(char **)str_node); |
Radek Krejci | 2e2de83 | 2016-10-13 16:12:26 +0200 | [diff] [blame] | 7302 | } /* else no default value in the type itself, but we are checking some restrictions against |
| 7303 | * possible default value of some base type. The failure is caused by not resolved base type, |
| 7304 | * so it was already reported */ |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7305 | break; |
| 7306 | case UNRES_CHOICE_DFLT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7307 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "choice default", (char *)str_node); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7308 | break; |
| 7309 | case UNRES_LIST_KEYS: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7310 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "list keys", (char *)str_node); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7311 | break; |
| 7312 | case UNRES_LIST_UNIQ: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7313 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "list unique", (char *)str_node); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7314 | break; |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 7315 | case UNRES_AUGMENT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7316 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "augment target", |
| 7317 | ((struct lys_node_augment *)item)->target_name); |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 7318 | break; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 7319 | case UNRES_XPATH: |
Michal Vasko | 0d19837 | 2016-11-16 11:40:03 +0100 | [diff] [blame] | 7320 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "XPath expressions of", |
| 7321 | ((struct lys_node *)item)->name); |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 7322 | break; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 7323 | case UNRES_EXT: |
| 7324 | extinfo = (struct unres_ext *)str_node; |
| 7325 | name = extinfo->datatype == LYS_IN_YIN ? extinfo->data.yin->name : NULL; /* TODO YANG extension */ |
| 7326 | LOGVRB("Resolving extension \"%s\" failed, it will be attempted later.", name); |
| 7327 | break; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7328 | default: |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7329 | LOGINT(NULL); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7330 | break; |
| 7331 | } |
| 7332 | } |
| 7333 | |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 7334 | static int |
| 7335 | resolve_unres_schema_types(struct unres_schema *unres, enum UNRES_ITEM types, struct ly_ctx *ctx, int forward_ref, |
| 7336 | int print_all_errors, uint32_t *resolved) |
| 7337 | { |
| 7338 | uint32_t i, unres_count, res_count; |
| 7339 | int ret = 0, rc; |
| 7340 | struct ly_err_item *prev_eitem; |
| 7341 | enum int_log_opts prev_ilo; |
| 7342 | LY_ERR prev_ly_errno; |
| 7343 | |
| 7344 | /* if there can be no forward references, every failure is final, so we can print it directly */ |
| 7345 | if (forward_ref) { |
| 7346 | prev_ly_errno = ly_errno; |
| 7347 | ly_ilo_change(ctx, ILO_STORE, &prev_ilo, &prev_eitem); |
| 7348 | } |
| 7349 | |
| 7350 | do { |
| 7351 | unres_count = 0; |
| 7352 | res_count = 0; |
| 7353 | |
| 7354 | for (i = 0; i < unres->count; ++i) { |
| 7355 | /* UNRES_TYPE_LEAFREF must be resolved (for storing leafref target pointers); |
| 7356 | * if-features are resolved here to make sure that we will have all if-features for |
| 7357 | * later check of feature circular dependency */ |
| 7358 | if (unres->type[i] & types) { |
| 7359 | ++unres_count; |
| 7360 | rc = resolve_unres_schema_item(unres->module[i], unres->item[i], unres->type[i], unres->str_snode[i], unres); |
| 7361 | if (unres->type[i] == UNRES_EXT_FINALIZE) { |
| 7362 | /* to avoid double free */ |
| 7363 | unres->type[i] = UNRES_RESOLVED; |
| 7364 | } |
| 7365 | if (!rc || (unres->type[i] == UNRES_XPATH)) { |
| 7366 | /* invalid XPath can never cause an error, only a warning */ |
| 7367 | if (unres->type[i] == UNRES_LIST_UNIQ) { |
| 7368 | /* free the allocated structure */ |
| 7369 | free(unres->item[i]); |
| 7370 | } |
| 7371 | |
| 7372 | unres->type[i] = UNRES_RESOLVED; |
| 7373 | ++(*resolved); |
| 7374 | ++res_count; |
| 7375 | } else if ((rc == EXIT_FAILURE) && forward_ref) { |
| 7376 | /* forward reference, erase errors */ |
| 7377 | ly_err_free_next(ctx, prev_eitem); |
| 7378 | } else if (print_all_errors) { |
| 7379 | /* just so that we quit the loop */ |
| 7380 | ++res_count; |
| 7381 | ret = -1; |
| 7382 | } else { |
Michal Vasko | ffdf4ce | 2018-08-17 10:31:49 +0200 | [diff] [blame] | 7383 | if (forward_ref) { |
| 7384 | ly_ilo_restore(ctx, prev_ilo, prev_eitem, 1); |
| 7385 | } |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 7386 | return -1; |
| 7387 | } |
| 7388 | } |
| 7389 | } |
| 7390 | } while (res_count && (res_count < unres_count)); |
| 7391 | |
| 7392 | if (res_count < unres_count) { |
| 7393 | assert(forward_ref); |
| 7394 | /* just print the errors (but we must free the ones we have and get them again :-/ ) */ |
| 7395 | ly_ilo_restore(ctx, prev_ilo, prev_eitem, 0); |
| 7396 | |
| 7397 | for (i = 0; i < unres->count; ++i) { |
| 7398 | if (unres->type[i] & types) { |
| 7399 | resolve_unres_schema_item(unres->module[i], unres->item[i], unres->type[i], unres->str_snode[i], unres); |
| 7400 | } |
| 7401 | } |
| 7402 | return -1; |
| 7403 | } |
| 7404 | |
| 7405 | if (forward_ref) { |
| 7406 | /* restore log */ |
| 7407 | ly_ilo_restore(ctx, prev_ilo, prev_eitem, 0); |
| 7408 | ly_errno = prev_ly_errno; |
| 7409 | } |
| 7410 | |
| 7411 | return ret; |
| 7412 | } |
| 7413 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7414 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 7415 | * @brief Resolve every unres schema item in the structure. Logs directly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7416 | * |
| 7417 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 7418 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7419 | * |
Michal Vasko | 92b8a38 | 2015-08-19 14:03:49 +0200 | [diff] [blame] | 7420 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7421 | */ |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7422 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 7423 | resolve_unres_schema(struct lys_module *mod, struct unres_schema *unres) |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7424 | { |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 7425 | uint32_t resolved = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7426 | |
| 7427 | assert(unres); |
| 7428 | |
Michal Vasko | e873426 | 2016-09-29 14:12:06 +0200 | [diff] [blame] | 7429 | LOGVRB("Resolving \"%s\" unresolved schema nodes and their constraints...", mod->name); |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 7430 | |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 7431 | /* UNRES_TYPE_LEAFREF must be resolved (for storing leafref target pointers); |
| 7432 | * if-features are resolved here to make sure that we will have all if-features for |
| 7433 | * later check of feature circular dependency */ |
| 7434 | if (resolve_unres_schema_types(unres, UNRES_USES | UNRES_IFFEAT | UNRES_TYPE_DER | UNRES_TYPE_DER_TPDF | UNRES_TYPE_DER_TPDF |
Michal Vasko | 6130b11 | 2020-07-02 09:28:41 +0200 | [diff] [blame] | 7435 | | UNRES_AUGMENT | UNRES_CHOICE_DFLT | UNRES_IDENT, |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 7436 | mod->ctx, 1, 0, &resolved)) { |
Michal Vasko | 92b8a38 | 2015-08-19 14:03:49 +0200 | [diff] [blame] | 7437 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7438 | } |
| 7439 | |
Michal Vasko | 6130b11 | 2020-07-02 09:28:41 +0200 | [diff] [blame] | 7440 | if (resolve_unres_schema_types(unres, UNRES_MOD_IMPLEMENT, mod->ctx, 1, 0, &resolved)) { |
| 7441 | return -1; |
| 7442 | } |
| 7443 | |
Michal Vasko | cab70b1 | 2020-08-20 10:16:45 +0200 | [diff] [blame] | 7444 | /* another batch of resolved items (UNRES_MOD_IMPLEMENT must be set again because it can be added again) */ |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 7445 | if (resolve_unres_schema_types(unres, UNRES_TYPE_IDENTREF | UNRES_FEATURE | UNRES_TYPEDEF_DFLT | UNRES_TYPE_DFLT |
Michal Vasko | cab70b1 | 2020-08-20 10:16:45 +0200 | [diff] [blame] | 7446 | | UNRES_TYPE_LEAFREF | UNRES_LIST_KEYS | UNRES_LIST_UNIQ | UNRES_EXT | UNRES_MOD_IMPLEMENT, |
| 7447 | mod->ctx, 1, 0, &resolved)) { |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 7448 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7449 | } |
| 7450 | |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 7451 | /* print xpath warnings and finalize extensions, keep it last to provide the complete schema tree information to the plugin's checkers */ |
| 7452 | if (resolve_unres_schema_types(unres, UNRES_XPATH | UNRES_EXT_FINALIZE, mod->ctx, 0, 1, &resolved)) { |
| 7453 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7454 | } |
| 7455 | |
Michal Vasko | e873426 | 2016-09-29 14:12:06 +0200 | [diff] [blame] | 7456 | LOGVRB("All \"%s\" schema nodes and constraints resolved.", mod->name); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7457 | unres->count = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7458 | return EXIT_SUCCESS; |
| 7459 | } |
| 7460 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7461 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 7462 | * @brief Try to resolve an unres schema item with a string argument. Logs indirectly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7463 | * |
| 7464 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 7465 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7466 | * @param[in] item Item to resolve. Type determined by \p type. |
| 7467 | * @param[in] type Type of the unresolved item. |
| 7468 | * @param[in] str String argument. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7469 | * |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 7470 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on storing the item in unres, -1 on error. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7471 | */ |
| 7472 | int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7473 | unres_schema_add_str(struct lys_module *mod, struct unres_schema *unres, void *item, enum UNRES_ITEM type, |
| 7474 | const char *str) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7475 | { |
Radek Krejci | 54081ce | 2016-08-12 15:21:47 +0200 | [diff] [blame] | 7476 | int rc; |
| 7477 | const char *dictstr; |
| 7478 | |
| 7479 | dictstr = lydict_insert(mod->ctx, str, 0); |
| 7480 | rc = unres_schema_add_node(mod, unres, item, type, (struct lys_node *)dictstr); |
| 7481 | |
Radek Krejci | d9c0ce2 | 2017-01-20 15:20:16 +0100 | [diff] [blame] | 7482 | if (rc < 0) { |
Radek Krejci | 54081ce | 2016-08-12 15:21:47 +0200 | [diff] [blame] | 7483 | lydict_remove(mod->ctx, dictstr); |
| 7484 | } |
| 7485 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7486 | } |
| 7487 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7488 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 7489 | * @brief Try to resolve an unres schema item with a schema node argument. Logs indirectly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7490 | * |
| 7491 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 7492 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7493 | * @param[in] item Item to resolve. Type determined by \p type. |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7494 | * @param[in] type Type of the unresolved item. UNRES_TYPE_DER is handled specially! |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7495 | * @param[in] snode Schema node argument. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7496 | * |
Radek Krejci | 62f7aad | 2017-10-26 14:53:52 +0200 | [diff] [blame] | 7497 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on storing the item in unres, -1 on error. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7498 | */ |
| 7499 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 7500 | unres_schema_add_node(struct lys_module *mod, struct unres_schema *unres, void *item, enum UNRES_ITEM type, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7501 | struct lys_node *snode) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7502 | { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7503 | int rc; |
Radek Krejci | 62f7aad | 2017-10-26 14:53:52 +0200 | [diff] [blame] | 7504 | uint32_t u; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7505 | enum int_log_opts prev_ilo; |
| 7506 | struct ly_err_item *prev_eitem; |
| 7507 | LY_ERR prev_ly_errno; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7508 | struct lyxml_elem *yin; |
Michal Vasko | d439b2a | 2020-05-04 17:45:14 +0200 | [diff] [blame] | 7509 | struct lys_type *stype; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7510 | struct ly_ctx *ctx = mod->ctx; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7511 | |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 7512 | assert(unres && (item || (type == UNRES_MOD_IMPLEMENT)) && ((type != UNRES_LEAFREF) && (type != UNRES_INSTID) |
| 7513 | && (type != UNRES_WHEN) && (type != UNRES_MUST))); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7514 | |
Radek Krejci | 850a5de | 2016-11-08 14:06:40 +0100 | [diff] [blame] | 7515 | /* check for duplicities in unres */ |
| 7516 | for (u = 0; u < unres->count; u++) { |
| 7517 | if (unres->type[u] == type && unres->item[u] == item && |
| 7518 | unres->str_snode[u] == snode && unres->module[u] == mod) { |
Radek Krejci | 62f7aad | 2017-10-26 14:53:52 +0200 | [diff] [blame] | 7519 | /* duplication can happen when the node contains multiple statements of the same type to check, |
| 7520 | * this can happen for example when refinement is being applied, so we just postpone the processing |
| 7521 | * and do not duplicate the information */ |
| 7522 | return EXIT_FAILURE; |
Radek Krejci | 850a5de | 2016-11-08 14:06:40 +0100 | [diff] [blame] | 7523 | } |
| 7524 | } |
| 7525 | |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 7526 | if ((type == UNRES_EXT_FINALIZE) || (type == UNRES_XPATH) || (type == UNRES_MOD_IMPLEMENT)) { |
Michal Vasko | f96dfb6 | 2017-08-17 12:23:49 +0200 | [diff] [blame] | 7527 | /* extension finalization is not even tried when adding the item into the inres list, |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 7528 | * xpath is not tried because it would hide some potential warnings, |
| 7529 | * implementing module must be deferred because some other nodes can be added that will need to be traversed |
| 7530 | * and their targets made implemented */ |
Radek Krejci | c293bac | 2017-02-27 11:25:28 +0100 | [diff] [blame] | 7531 | rc = EXIT_FAILURE; |
| 7532 | } else { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7533 | prev_ly_errno = ly_errno; |
| 7534 | ly_ilo_change(ctx, ILO_STORE, &prev_ilo, &prev_eitem); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7535 | |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7536 | rc = resolve_unres_schema_item(mod, item, type, snode, unres); |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7537 | if (rc != EXIT_FAILURE) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7538 | ly_ilo_restore(ctx, prev_ilo, prev_eitem, rc == -1 ? 1 : 0); |
| 7539 | if (rc != -1) { |
Michal Vasko | d439b2a | 2020-05-04 17:45:14 +0200 | [diff] [blame] | 7540 | /* print warnings here so that they are actually printed */ |
| 7541 | if ((type == UNRES_TYPE_DER_TPDF) || (type == UNRES_TYPE_DER)) { |
| 7542 | stype = item; |
| 7543 | if (stype->der->module && (((stype->base == LY_TYPE_LEAFREF) |
| 7544 | && (stype->info.lref.req != stype->der->type.info.lref.req)) || ((stype->base == LY_TYPE_INST) |
| 7545 | && (stype->info.inst.req != stype->der->type.info.inst.req)))) { |
| 7546 | if (type == UNRES_TYPE_DER_TPDF) { |
| 7547 | /* typedef */ |
| 7548 | LOGWRN(ctx, "Derived typedef \"%s\" is changing the \"require-instance\" property, " |
| 7549 | "which is discouraged.", stype->parent->name); |
| 7550 | } else { |
| 7551 | /* leaf */ |
| 7552 | LOGWRN(ctx, "Node \"%s\" type is changing the \"require-instance\" property, " |
| 7553 | "which is discouraged.", snode->name); |
| 7554 | } |
| 7555 | } |
| 7556 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7557 | ly_errno = prev_ly_errno; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7558 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7559 | |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7560 | if (type == UNRES_LIST_UNIQ) { |
| 7561 | /* free the allocated structure */ |
| 7562 | free(item); |
| 7563 | } else if (rc == -1 && type == UNRES_IFFEAT) { |
| 7564 | /* free the allocated resources */ |
| 7565 | free(*((char **)item)); |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 7566 | } |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7567 | return rc; |
| 7568 | } else { |
| 7569 | /* erase info about validation errors */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7570 | ly_ilo_restore(ctx, prev_ilo, prev_eitem, 0); |
| 7571 | ly_errno = prev_ly_errno; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7572 | } |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7573 | |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7574 | print_unres_schema_item_fail(item, type, snode); |
| 7575 | |
| 7576 | /* HACK unlinking is performed here so that we do not do any (NS) copying in vain */ |
| 7577 | if (type == UNRES_TYPE_DER || type == UNRES_TYPE_DER_TPDF) { |
| 7578 | yin = (struct lyxml_elem *)((struct lys_type *)item)->der; |
| 7579 | if (!(yin->flags & LY_YANG_STRUCTURE_FLAG)) { |
| 7580 | lyxml_unlink_elem(mod->ctx, yin, 1); |
| 7581 | ((struct lys_type *)item)->der = (struct lys_tpdf *)yin; |
| 7582 | } |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 7583 | } |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7584 | } |
| 7585 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7586 | unres->count++; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 7587 | unres->item = ly_realloc(unres->item, unres->count*sizeof *unres->item); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7588 | LY_CHECK_ERR_RETURN(!unres->item, LOGMEM(ctx), -1); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7589 | unres->item[unres->count-1] = item; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 7590 | unres->type = ly_realloc(unres->type, unres->count*sizeof *unres->type); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7591 | LY_CHECK_ERR_RETURN(!unres->type, LOGMEM(ctx), -1); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7592 | unres->type[unres->count-1] = type; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 7593 | unres->str_snode = ly_realloc(unres->str_snode, unres->count*sizeof *unres->str_snode); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7594 | LY_CHECK_ERR_RETURN(!unres->str_snode, LOGMEM(ctx), -1); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7595 | unres->str_snode[unres->count-1] = snode; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7596 | unres->module = ly_realloc(unres->module, unres->count*sizeof *unres->module); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7597 | LY_CHECK_ERR_RETURN(!unres->module, LOGMEM(ctx), -1); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7598 | unres->module[unres->count-1] = mod; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7599 | |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 7600 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7601 | } |
| 7602 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7603 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 7604 | * @brief Duplicate an unres schema item. Logs indirectly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7605 | * |
| 7606 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 7607 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7608 | * @param[in] item Old item to be resolved. |
| 7609 | * @param[in] type Type of the old unresolved item. |
| 7610 | * @param[in] new_item New item to use in the duplicate. |
| 7611 | * |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 7612 | * @return EXIT_SUCCESS on success, EXIT_FAILURE if item is not in unres, -1 on error. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7613 | */ |
Michal Vasko | dad1940 | 2015-08-06 09:51:53 +0200 | [diff] [blame] | 7614 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 7615 | unres_schema_dup(struct lys_module *mod, struct unres_schema *unres, void *item, enum UNRES_ITEM type, void *new_item) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7616 | { |
| 7617 | int i; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7618 | struct unres_list_uniq aux_uniq; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7619 | struct unres_iffeat_data *iff_data; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7620 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7621 | assert(item && new_item && ((type != UNRES_LEAFREF) && (type != UNRES_INSTID) && (type != UNRES_WHEN))); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7622 | |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7623 | /* hack for UNRES_LIST_UNIQ, which stores multiple items behind its item */ |
| 7624 | if (type == UNRES_LIST_UNIQ) { |
| 7625 | aux_uniq.list = item; |
| 7626 | aux_uniq.expr = ((struct unres_list_uniq *)new_item)->expr; |
| 7627 | item = &aux_uniq; |
| 7628 | } |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7629 | i = unres_schema_find(unres, -1, item, type); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7630 | |
| 7631 | if (i == -1) { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7632 | if (type == UNRES_LIST_UNIQ) { |
| 7633 | free(new_item); |
| 7634 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 7635 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7636 | } |
| 7637 | |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 7638 | if ((type == UNRES_TYPE_LEAFREF) || (type == UNRES_USES) || (type == UNRES_TYPE_DFLT) || |
Radek Krejci | b69f323 | 2016-09-16 17:41:07 +0200 | [diff] [blame] | 7639 | (type == UNRES_FEATURE) || (type == UNRES_LIST_UNIQ)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7640 | if (unres_schema_add_node(mod, unres, new_item, type, unres->str_snode[i]) == -1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7641 | LOGINT(mod->ctx); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7642 | return -1; |
| 7643 | } |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7644 | } else if (type == UNRES_IFFEAT) { |
| 7645 | /* duplicate unres_iffeature_data */ |
| 7646 | iff_data = malloc(sizeof *iff_data); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7647 | LY_CHECK_ERR_RETURN(!iff_data, LOGMEM(mod->ctx), -1); |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7648 | iff_data->fname = lydict_insert(mod->ctx, ((struct unres_iffeat_data *)unres->str_snode[i])->fname, 0); |
| 7649 | iff_data->node = ((struct unres_iffeat_data *)unres->str_snode[i])->node; |
Frank Rimpler | dddb321 | 2019-06-11 12:28:38 +0000 | [diff] [blame] | 7650 | iff_data->infeature = ((struct unres_iffeat_data *)unres->str_snode[i])->infeature; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7651 | if (unres_schema_add_node(mod, unres, new_item, type, (struct lys_node *)iff_data) == -1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7652 | LOGINT(mod->ctx); |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7653 | return -1; |
| 7654 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7655 | } else { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7656 | if (unres_schema_add_str(mod, unres, new_item, type, unres->str_snode[i]) == -1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7657 | LOGINT(mod->ctx); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7658 | return -1; |
| 7659 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7660 | } |
Michal Vasko | dad1940 | 2015-08-06 09:51:53 +0200 | [diff] [blame] | 7661 | |
| 7662 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7663 | } |
| 7664 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7665 | /* does not log */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7666 | int |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7667 | unres_schema_find(struct unres_schema *unres, int start_on_backwards, void *item, enum UNRES_ITEM type) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7668 | { |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7669 | int i; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7670 | struct unres_list_uniq *aux_uniq1, *aux_uniq2; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7671 | |
Michal Vasko | c643f71 | 2018-09-14 08:26:13 +0200 | [diff] [blame] | 7672 | if (!unres->count) { |
| 7673 | return -1; |
| 7674 | } |
| 7675 | |
Radek Krejci | ddddd0d | 2017-01-20 15:20:46 +0100 | [diff] [blame] | 7676 | if (start_on_backwards >= 0) { |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7677 | i = start_on_backwards; |
| 7678 | } else { |
| 7679 | i = unres->count - 1; |
| 7680 | } |
| 7681 | for (; i > -1; i--) { |
| 7682 | if (unres->type[i] != type) { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7683 | continue; |
| 7684 | } |
| 7685 | if (type != UNRES_LIST_UNIQ) { |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7686 | if (unres->item[i] == item) { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7687 | break; |
| 7688 | } |
| 7689 | } else { |
Соколов Пётр | 0d921ef | 2019-01-23 10:13:50 +0300 | [diff] [blame] | 7690 | aux_uniq1 = (struct unres_list_uniq *)unres->item[i]; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7691 | aux_uniq2 = (struct unres_list_uniq *)item; |
| 7692 | if ((aux_uniq1->list == aux_uniq2->list) && ly_strequal(aux_uniq1->expr, aux_uniq2->expr, 0)) { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7693 | break; |
| 7694 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7695 | } |
| 7696 | } |
| 7697 | |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7698 | return i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7699 | } |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7700 | |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7701 | static void |
| 7702 | unres_schema_free_item(struct ly_ctx *ctx, struct unres_schema *unres, uint32_t i) |
| 7703 | { |
| 7704 | struct lyxml_elem *yin; |
| 7705 | struct yang_type *yang; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7706 | struct unres_iffeat_data *iff_data; |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7707 | |
| 7708 | switch (unres->type[i]) { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 7709 | case UNRES_TYPE_DER_TPDF: |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7710 | case UNRES_TYPE_DER: |
| 7711 | yin = (struct lyxml_elem *)((struct lys_type *)unres->item[i])->der; |
| 7712 | if (yin->flags & LY_YANG_STRUCTURE_FLAG) { |
| 7713 | yang =(struct yang_type *)yin; |
Pavol Vican | cf2af4d | 2016-12-21 14:13:06 +0100 | [diff] [blame] | 7714 | ((struct lys_type *)unres->item[i])->base = yang->base; |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7715 | lydict_remove(ctx, yang->name); |
| 7716 | free(yang); |
Pavol Vican | cf2af4d | 2016-12-21 14:13:06 +0100 | [diff] [blame] | 7717 | if (((struct lys_type *)unres->item[i])->base == LY_TYPE_UNION) { |
| 7718 | yang_free_type_union(ctx, (struct lys_type *)unres->item[i]); |
| 7719 | } |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7720 | } else { |
| 7721 | lyxml_free(ctx, yin); |
| 7722 | } |
| 7723 | break; |
Pavol Vican | 88e16c9 | 2016-09-07 15:41:50 +0200 | [diff] [blame] | 7724 | case UNRES_IFFEAT: |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7725 | iff_data = (struct unres_iffeat_data *)unres->str_snode[i]; |
| 7726 | lydict_remove(ctx, iff_data->fname); |
| 7727 | free(unres->str_snode[i]); |
Pavol Vican | 88e16c9 | 2016-09-07 15:41:50 +0200 | [diff] [blame] | 7728 | break; |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7729 | case UNRES_IDENT: |
| 7730 | case UNRES_TYPE_IDENTREF: |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7731 | case UNRES_CHOICE_DFLT: |
| 7732 | case UNRES_LIST_KEYS: |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7733 | lydict_remove(ctx, (const char *)unres->str_snode[i]); |
| 7734 | break; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7735 | case UNRES_LIST_UNIQ: |
| 7736 | free(unres->item[i]); |
| 7737 | break; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 7738 | case UNRES_EXT: |
| 7739 | free(unres->str_snode[i]); |
| 7740 | break; |
PavolVican | fcc9876 | 2017-09-01 15:51:39 +0200 | [diff] [blame] | 7741 | case UNRES_EXT_FINALIZE: |
| 7742 | free(unres->str_snode[i]); |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7743 | default: |
| 7744 | break; |
| 7745 | } |
| 7746 | unres->type[i] = UNRES_RESOLVED; |
| 7747 | } |
| 7748 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7749 | void |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 7750 | unres_schema_free(struct lys_module *module, struct unres_schema **unres, int all) |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7751 | { |
| 7752 | uint32_t i; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7753 | unsigned int unresolved = 0; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7754 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7755 | if (!unres || !(*unres)) { |
| 7756 | return; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7757 | } |
| 7758 | |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 7759 | assert(module || ((*unres)->count == 0)); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7760 | |
| 7761 | for (i = 0; i < (*unres)->count; ++i) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 7762 | if (!all && ((*unres)->module[i] != module)) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7763 | if ((*unres)->type[i] != UNRES_RESOLVED) { |
| 7764 | unresolved++; |
| 7765 | } |
| 7766 | continue; |
| 7767 | } |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7768 | |
| 7769 | /* free heap memory for the specific item */ |
| 7770 | unres_schema_free_item(module->ctx, *unres, i); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7771 | } |
| 7772 | |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7773 | /* free it all */ |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 7774 | if (!module || all || (!unresolved && !module->type)) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7775 | free((*unres)->item); |
| 7776 | free((*unres)->type); |
| 7777 | free((*unres)->str_snode); |
| 7778 | free((*unres)->module); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7779 | free((*unres)); |
| 7780 | (*unres) = NULL; |
| 7781 | } |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7782 | } |
| 7783 | |
Michal Vasko | ff690e7 | 2017-08-03 14:25:07 +0200 | [diff] [blame] | 7784 | /* check whether instance-identifier points outside its data subtree (for operation it is any node |
| 7785 | * outside the operation subtree, otherwise it is a node from a foreign model) */ |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7786 | static int |
| 7787 | check_instid_ext_dep(const struct lys_node *sleaf, const char *json_instid) |
| 7788 | { |
Michal Vasko | ff690e7 | 2017-08-03 14:25:07 +0200 | [diff] [blame] | 7789 | const struct lys_node *op_node, *first_node; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7790 | enum int_log_opts prev_ilo; |
Michal Vasko | 2e80aff | 2018-12-07 08:41:07 +0100 | [diff] [blame] | 7791 | char *buf, *tmp; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7792 | |
Radek Krejci | 034cb10 | 2017-08-01 15:45:13 +0200 | [diff] [blame] | 7793 | if (!json_instid || !json_instid[0]) { |
| 7794 | /* no/empty value */ |
| 7795 | return 0; |
| 7796 | } |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7797 | |
| 7798 | for (op_node = lys_parent(sleaf); |
| 7799 | op_node && !(op_node->nodetype & (LYS_NOTIF | LYS_RPC | LYS_ACTION)); |
| 7800 | op_node = lys_parent(op_node)); |
| 7801 | |
| 7802 | if (op_node && lys_parent(op_node)) { |
| 7803 | /* nested operation - any absolute path is external */ |
| 7804 | return 1; |
| 7805 | } |
| 7806 | |
| 7807 | /* get the first node from the instid */ |
Michal Vasko | 2e80aff | 2018-12-07 08:41:07 +0100 | [diff] [blame] | 7808 | tmp = strchr(json_instid + 1, '/'); |
Michal Vasko | 299aef1 | 2018-12-18 13:13:55 +0100 | [diff] [blame] | 7809 | buf = strndup(json_instid, tmp ? (size_t)(tmp - json_instid) : strlen(json_instid)); |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7810 | if (!buf) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7811 | /* so that we do not have to bother with logging, say it is not external */ |
| 7812 | return 0; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7813 | } |
| 7814 | |
Michal Vasko | ff690e7 | 2017-08-03 14:25:07 +0200 | [diff] [blame] | 7815 | /* find the first schema node, do not log */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7816 | ly_ilo_change(NULL, ILO_IGNORE, &prev_ilo, NULL); |
Michal Vasko | ff690e7 | 2017-08-03 14:25:07 +0200 | [diff] [blame] | 7817 | first_node = ly_ctx_get_node(NULL, sleaf, buf, 0); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7818 | ly_ilo_restore(NULL, prev_ilo, NULL, 0); |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7819 | |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7820 | free(buf); |
Michal Vasko | ff690e7 | 2017-08-03 14:25:07 +0200 | [diff] [blame] | 7821 | if (!first_node) { |
Michal Vasko | 299aef1 | 2018-12-18 13:13:55 +0100 | [diff] [blame] | 7822 | /* unknown path, say it is external */ |
| 7823 | return 1; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7824 | } |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7825 | |
| 7826 | /* based on the first schema node in the path we can decide whether it points to an external tree or not */ |
| 7827 | |
Michal Vasko | 299aef1 | 2018-12-18 13:13:55 +0100 | [diff] [blame] | 7828 | if (op_node) { |
| 7829 | if (op_node != first_node) { |
| 7830 | /* it is a top-level operation, so we're good if it points somewhere inside it */ |
| 7831 | return 1; |
| 7832 | } |
| 7833 | } else { |
| 7834 | if (lys_node_module(sleaf) != lys_node_module(first_node)) { |
| 7835 | /* modules differ */ |
| 7836 | return 1; |
| 7837 | } |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7838 | } |
| 7839 | |
Michal Vasko | 299aef1 | 2018-12-18 13:13:55 +0100 | [diff] [blame] | 7840 | return 0; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7841 | } |
| 7842 | |
Michal Vasko | 436d13f | 2020-02-25 14:31:00 +0100 | [diff] [blame] | 7843 | int |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7844 | resolve_instid(struct lyd_node *data, const char *path, int req_inst, struct lyd_node **ret) |
| 7845 | { |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 7846 | int i = 0, j, parsed, cur_idx; |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 7847 | const struct lys_module *mod, *prev_mod = NULL; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7848 | struct ly_ctx *ctx = data->schema->module->ctx; |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 7849 | struct lyd_node *root, *node; |
Radek Krejci | daa547a | 2017-09-22 15:56:27 +0200 | [diff] [blame] | 7850 | const char *model = NULL, *name; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7851 | char *str; |
| 7852 | int mod_len, name_len, has_predicate; |
| 7853 | struct unres_data node_match; |
| 7854 | |
| 7855 | memset(&node_match, 0, sizeof node_match); |
| 7856 | *ret = NULL; |
| 7857 | |
| 7858 | /* we need root to resolve absolute path */ |
Radek Krejci | 2c822ed | 2017-08-03 14:23:36 +0200 | [diff] [blame] | 7859 | for (root = data; root->parent; root = root->parent); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7860 | /* we're still parsing it and the pointer is not correct yet */ |
Radek Krejci | 2c822ed | 2017-08-03 14:23:36 +0200 | [diff] [blame] | 7861 | if (root->prev) { |
| 7862 | for (; root->prev->next; root = root->prev); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7863 | } |
| 7864 | |
| 7865 | /* search for the instance node */ |
| 7866 | while (path[i]) { |
| 7867 | j = parse_instance_identifier(&path[i], &model, &mod_len, &name, &name_len, &has_predicate); |
| 7868 | if (j <= 0) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7869 | LOGVAL(ctx, LYE_INCHAR, LY_VLOG_LYD, data, path[i-j], &path[i-j]); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7870 | goto error; |
| 7871 | } |
| 7872 | i += j; |
| 7873 | |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 7874 | if (model) { |
| 7875 | str = strndup(model, mod_len); |
| 7876 | if (!str) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7877 | LOGMEM(ctx); |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 7878 | goto error; |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 7879 | } |
Radek Krejci | dfb00d6 | 2017-09-06 09:39:35 +0200 | [diff] [blame] | 7880 | mod = ly_ctx_get_module(ctx, str, NULL, 1); |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 7881 | if (ctx->data_clb) { |
| 7882 | if (!mod) { |
| 7883 | mod = ctx->data_clb(ctx, str, NULL, 0, ctx->data_clb_data); |
| 7884 | } else if (!mod->implemented) { |
| 7885 | mod = ctx->data_clb(ctx, mod->name, mod->ns, LY_MODCLB_NOT_IMPLEMENTED, ctx->data_clb_data); |
| 7886 | } |
| 7887 | } |
| 7888 | free(str); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7889 | |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 7890 | if (!mod || !mod->implemented || mod->disabled) { |
| 7891 | break; |
| 7892 | } |
| 7893 | } else if (!prev_mod) { |
| 7894 | /* first iteration and we are missing module name */ |
Michal Vasko | af8ec36 | 2018-03-28 09:08:09 +0200 | [diff] [blame] | 7895 | LOGVAL(ctx, LYE_INELEM_LEN, LY_VLOG_LYD, data, name_len, name); |
| 7896 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Instance-identifier is missing prefix in the first node."); |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 7897 | goto error; |
| 7898 | } else { |
| 7899 | mod = prev_mod; |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 7900 | } |
| 7901 | |
Radek Krejci | 2c822ed | 2017-08-03 14:23:36 +0200 | [diff] [blame] | 7902 | if (resolve_data(mod, name, name_len, root, &node_match)) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7903 | /* no instance exists */ |
| 7904 | break; |
| 7905 | } |
| 7906 | |
| 7907 | if (has_predicate) { |
| 7908 | /* we have predicate, so the current results must be list or leaf-list */ |
Radek Krejci | daa547a | 2017-09-22 15:56:27 +0200 | [diff] [blame] | 7909 | parsed = j = 0; |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 7910 | /* index of the current node (for lists with position predicates) */ |
| 7911 | cur_idx = 1; |
| 7912 | while (j < (signed)node_match.count) { |
| 7913 | node = node_match.node[j]; |
| 7914 | parsed = resolve_instid_predicate(mod, &path[i], &node, cur_idx); |
| 7915 | if (parsed < 1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7916 | LOGVAL(ctx, LYE_INPRED, LY_VLOG_LYD, data, &path[i - parsed]); |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 7917 | goto error; |
| 7918 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7919 | |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 7920 | if (!node) { |
| 7921 | /* current node does not satisfy the predicate */ |
| 7922 | unres_data_del(&node_match, j); |
| 7923 | } else { |
| 7924 | ++j; |
| 7925 | } |
| 7926 | ++cur_idx; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7927 | } |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 7928 | |
| 7929 | i += parsed; |
Michal Vasko | 6f28e0f | 2017-04-18 15:14:13 +0200 | [diff] [blame] | 7930 | } else if (node_match.count) { |
| 7931 | /* check that we are not addressing lists */ |
| 7932 | for (j = 0; (unsigned)j < node_match.count; ++j) { |
| 7933 | if (node_match.node[j]->schema->nodetype == LYS_LIST) { |
| 7934 | unres_data_del(&node_match, j--); |
| 7935 | } |
| 7936 | } |
| 7937 | if (!node_match.count) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7938 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYD, data, "Instance identifier is missing list keys."); |
Michal Vasko | 6f28e0f | 2017-04-18 15:14:13 +0200 | [diff] [blame] | 7939 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7940 | } |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 7941 | |
| 7942 | prev_mod = mod; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7943 | } |
| 7944 | |
| 7945 | if (!node_match.count) { |
| 7946 | /* no instance exists */ |
| 7947 | if (req_inst > -1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7948 | LOGVAL(ctx, LYE_NOREQINS, LY_VLOG_LYD, data, path); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7949 | return EXIT_FAILURE; |
| 7950 | } |
| 7951 | LOGVRB("There is no instance of \"%s\", but it is not required.", path); |
| 7952 | return EXIT_SUCCESS; |
| 7953 | } else if (node_match.count > 1) { |
| 7954 | /* instance identifier must resolve to a single node */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7955 | LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_LYD, data, path, "data tree"); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7956 | goto error; |
| 7957 | } else { |
| 7958 | /* we have required result, remember it and cleanup */ |
| 7959 | *ret = node_match.node[0]; |
| 7960 | free(node_match.node); |
| 7961 | return EXIT_SUCCESS; |
| 7962 | } |
| 7963 | |
| 7964 | error: |
| 7965 | /* cleanup */ |
| 7966 | free(node_match.node); |
| 7967 | return -1; |
| 7968 | } |
| 7969 | |
Michal Vasko | 436d13f | 2020-02-25 14:31:00 +0100 | [diff] [blame] | 7970 | int |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7971 | resolve_leafref(struct lyd_node_leaf_list *leaf, const char *path, int req_inst, struct lyd_node **ret) |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7972 | { |
Michal Vasko | b5f9ffb | 2018-08-06 09:01:27 +0200 | [diff] [blame] | 7973 | struct lyxp_set xp_set; |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7974 | uint32_t i; |
| 7975 | |
Michal Vasko | b5f9ffb | 2018-08-06 09:01:27 +0200 | [diff] [blame] | 7976 | memset(&xp_set, 0, sizeof xp_set); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7977 | *ret = NULL; |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7978 | |
Michal Vasko | ca16cb3 | 2017-07-10 11:50:33 +0200 | [diff] [blame] | 7979 | /* syntax was already checked, so just evaluate the path using standard XPath */ |
Michal Vasko | b5f9ffb | 2018-08-06 09:01:27 +0200 | [diff] [blame] | 7980 | if (lyxp_eval(path, (struct lyd_node *)leaf, LYXP_NODE_ELEM, lyd_node_module((struct lyd_node *)leaf), &xp_set, 0) != EXIT_SUCCESS) { |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7981 | return -1; |
| 7982 | } |
| 7983 | |
Michal Vasko | b5f9ffb | 2018-08-06 09:01:27 +0200 | [diff] [blame] | 7984 | if (xp_set.type == LYXP_SET_NODE_SET) { |
| 7985 | for (i = 0; i < xp_set.used; ++i) { |
| 7986 | if ((xp_set.val.nodes[i].type != LYXP_NODE_ELEM) || !(xp_set.val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 7987 | continue; |
| 7988 | } |
Michal Vasko | ca16cb3 | 2017-07-10 11:50:33 +0200 | [diff] [blame] | 7989 | |
Michal Vasko | b5f9ffb | 2018-08-06 09:01:27 +0200 | [diff] [blame] | 7990 | /* not that the value is already in canonical form since the parsers does the conversion, |
| 7991 | * so we can simply compare just the values */ |
| 7992 | if (ly_strequal(leaf->value_str, ((struct lyd_node_leaf_list *)xp_set.val.nodes[i].node)->value_str, 1)) { |
| 7993 | /* we have the match */ |
| 7994 | *ret = xp_set.val.nodes[i].node; |
| 7995 | break; |
| 7996 | } |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7997 | } |
| 7998 | } |
| 7999 | |
Michal Vasko | b5f9ffb | 2018-08-06 09:01:27 +0200 | [diff] [blame] | 8000 | lyxp_set_cast(&xp_set, LYXP_SET_EMPTY, (struct lyd_node *)leaf, NULL, 0); |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 8001 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8002 | if (!*ret) { |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 8003 | /* reference not found */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8004 | if (req_inst > -1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8005 | LOGVAL(leaf->schema->module->ctx, LYE_NOLEAFREF, LY_VLOG_LYD, leaf, path, leaf->value_str); |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 8006 | return EXIT_FAILURE; |
| 8007 | } else { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8008 | LOGVRB("There is no leafref \"%s\" with the value \"%s\", but it is not required.", path, leaf->value_str); |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 8009 | } |
| 8010 | } |
| 8011 | |
| 8012 | return EXIT_SUCCESS; |
| 8013 | } |
| 8014 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8015 | /* ignore fail because we are parsing edit-config, get, or get-config - but only if the union includes leafref or instid */ |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 8016 | int |
| 8017 | resolve_union(struct lyd_node_leaf_list *leaf, struct lys_type *type, int store, int ignore_fail, |
| 8018 | struct lys_type **resolved_type) |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 8019 | { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8020 | struct ly_ctx *ctx = leaf->schema->module->ctx; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8021 | struct lys_type *t; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 8022 | struct lyd_node *ret; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8023 | enum int_log_opts prev_ilo; |
| 8024 | int found, success = 0, ext_dep, req_inst; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8025 | const char *json_val = NULL; |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 8026 | |
| 8027 | assert(type->base == LY_TYPE_UNION); |
| 8028 | |
Michal Vasko | 101658e | 2018-06-05 15:05:54 +0200 | [diff] [blame] | 8029 | if ((leaf->value_type == LY_TYPE_UNION) || ((leaf->value_type == LY_TYPE_INST) && (leaf->value_flags & LY_VALUE_UNRES))) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8030 | /* either NULL or instid previously converted to JSON */ |
Michal Vasko | c6cd3f0 | 2018-03-02 14:07:42 +0100 | [diff] [blame] | 8031 | json_val = lydict_insert(ctx, leaf->value.string, 0); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8032 | } |
Michal Vasko | 1c8567a | 2017-01-05 13:42:27 +0100 | [diff] [blame] | 8033 | |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 8034 | if (store) { |
Michal Vasko | 7ba3744 | 2018-11-06 08:59:27 +0100 | [diff] [blame] | 8035 | lyd_free_value(leaf->value, leaf->value_type, leaf->value_flags, &((struct lys_node_leaf *)leaf->schema)->type, |
Michal Vasko | d3dd15d | 2019-01-23 14:06:58 +0100 | [diff] [blame] | 8036 | leaf->value_str, NULL, NULL, NULL); |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 8037 | memset(&leaf->value, 0, sizeof leaf->value); |
Michal Vasko | 1c8567a | 2017-01-05 13:42:27 +0100 | [diff] [blame] | 8038 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8039 | |
| 8040 | /* turn logging off, we are going to try to validate the value with all the types in order */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8041 | ly_ilo_change(NULL, ILO_IGNORE, &prev_ilo, 0); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8042 | |
| 8043 | t = NULL; |
| 8044 | found = 0; |
| 8045 | while ((t = lyp_get_next_union_type(type, t, &found))) { |
| 8046 | found = 0; |
| 8047 | |
| 8048 | switch (t->base) { |
| 8049 | case LY_TYPE_LEAFREF: |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 8050 | if ((ignore_fail == 1) || ((leaf->schema->flags & LYS_LEAFREF_DEP) && (ignore_fail == 2))) { |
| 8051 | req_inst = -1; |
| 8052 | } else { |
| 8053 | req_inst = t->info.lref.req; |
| 8054 | } |
| 8055 | |
| 8056 | if (!resolve_leafref(leaf, t->info.lref.path, req_inst, &ret)) { |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 8057 | if (store) { |
| 8058 | if (ret && !(leaf->schema->flags & LYS_LEAFREF_DEP)) { |
| 8059 | /* valid resolved */ |
| 8060 | leaf->value.leafref = ret; |
| 8061 | leaf->value_type = LY_TYPE_LEAFREF; |
| 8062 | } else { |
| 8063 | /* valid unresolved */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8064 | ly_ilo_restore(NULL, prev_ilo, NULL, 0); |
Michal Vasko | 0abb1bc | 2020-02-25 15:47:10 +0100 | [diff] [blame] | 8065 | if (!lyp_parse_value(t, &leaf->value_str, NULL, leaf, NULL, NULL, 1, 0)) { |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 8066 | return -1; |
| 8067 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8068 | ly_ilo_change(NULL, ILO_IGNORE, &prev_ilo, NULL); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8069 | } |
| 8070 | } |
| 8071 | |
| 8072 | success = 1; |
| 8073 | } |
| 8074 | break; |
| 8075 | case LY_TYPE_INST: |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 8076 | ext_dep = check_instid_ext_dep(leaf->schema, (json_val ? json_val : leaf->value_str)); |
| 8077 | if ((ignore_fail == 1) || (ext_dep && (ignore_fail == 2))) { |
| 8078 | req_inst = -1; |
| 8079 | } else { |
| 8080 | req_inst = t->info.inst.req; |
| 8081 | } |
| 8082 | |
Michal Vasko | d3a0311 | 2017-01-23 09:56:02 +0100 | [diff] [blame] | 8083 | if (!resolve_instid((struct lyd_node *)leaf, (json_val ? json_val : leaf->value_str), req_inst, &ret)) { |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 8084 | if (store) { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 8085 | if (ret && !ext_dep) { |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 8086 | /* valid resolved */ |
| 8087 | leaf->value.instance = ret; |
| 8088 | leaf->value_type = LY_TYPE_INST; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8089 | |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 8090 | if (json_val) { |
| 8091 | lydict_remove(leaf->schema->module->ctx, leaf->value_str); |
| 8092 | leaf->value_str = json_val; |
| 8093 | json_val = NULL; |
| 8094 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8095 | } else { |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 8096 | /* valid unresolved */ |
| 8097 | if (json_val) { |
| 8098 | /* put the JSON val back */ |
| 8099 | leaf->value.string = json_val; |
| 8100 | json_val = NULL; |
| 8101 | } else { |
| 8102 | leaf->value.instance = NULL; |
| 8103 | } |
Michal Vasko | 70bf8e5 | 2018-03-26 11:32:33 +0200 | [diff] [blame] | 8104 | leaf->value_type = LY_TYPE_INST; |
Michal Vasko | 101658e | 2018-06-05 15:05:54 +0200 | [diff] [blame] | 8105 | leaf->value_flags |= LY_VALUE_UNRES; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8106 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8107 | } |
| 8108 | |
| 8109 | success = 1; |
| 8110 | } |
| 8111 | break; |
| 8112 | default: |
Michal Vasko | 0abb1bc | 2020-02-25 15:47:10 +0100 | [diff] [blame] | 8113 | if (lyp_parse_value(t, &leaf->value_str, NULL, leaf, NULL, NULL, store, 0)) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8114 | success = 1; |
| 8115 | } |
| 8116 | break; |
| 8117 | } |
| 8118 | |
| 8119 | if (success) { |
| 8120 | break; |
| 8121 | } |
| 8122 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8123 | /* erase possible present and invalid value data */ |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 8124 | if (store) { |
Michal Vasko | d3dd15d | 2019-01-23 14:06:58 +0100 | [diff] [blame] | 8125 | lyd_free_value(leaf->value, leaf->value_type, leaf->value_flags, t, leaf->value_str, NULL, NULL, NULL); |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 8126 | memset(&leaf->value, 0, sizeof leaf->value); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8127 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8128 | } |
| 8129 | |
| 8130 | /* turn logging back on */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8131 | ly_ilo_restore(NULL, prev_ilo, NULL, 0); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8132 | |
| 8133 | if (json_val) { |
| 8134 | if (!success) { |
| 8135 | /* put the value back for now */ |
| 8136 | assert(leaf->value_type == LY_TYPE_UNION); |
| 8137 | leaf->value.string = json_val; |
| 8138 | } else { |
| 8139 | /* value was ultimately useless, but we could not have known */ |
| 8140 | lydict_remove(leaf->schema->module->ctx, json_val); |
| 8141 | } |
| 8142 | } |
| 8143 | |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 8144 | if (success) { |
| 8145 | if (resolved_type) { |
| 8146 | *resolved_type = t; |
| 8147 | } |
| 8148 | } else if (!ignore_fail || !type->info.uni.has_ptr_type) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8149 | /* not found and it is required */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8150 | LOGVAL(ctx, LYE_INVAL, LY_VLOG_LYD, leaf, leaf->value_str ? leaf->value_str : "", leaf->schema->name); |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 8151 | return EXIT_FAILURE; |
| 8152 | } |
| 8153 | |
| 8154 | return EXIT_SUCCESS; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8155 | |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 8156 | } |
| 8157 | |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8158 | /** |
| 8159 | * @brief Resolve a single unres data item. Logs directly. |
| 8160 | * |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 8161 | * @param[in] node Data node to resolve. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 8162 | * @param[in] type Type of the unresolved item. |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 8163 | * @param[in] ignore_fail 0 - no, 1 - yes, 2 - yes, but only for external dependencies. |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8164 | * |
| 8165 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 8166 | */ |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 8167 | int |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 8168 | resolve_unres_data_item(struct lyd_node *node, enum UNRES_ITEM type, int ignore_fail, struct lys_when **failed_when) |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8169 | { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 8170 | int rc, req_inst, ext_dep; |
Michal Vasko | 83a6c46 | 2015-10-08 16:43:53 +0200 | [diff] [blame] | 8171 | struct lyd_node_leaf_list *leaf; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 8172 | struct lyd_node *ret; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8173 | struct lys_node_leaf *sleaf; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8174 | |
Michal Vasko | 83a6c46 | 2015-10-08 16:43:53 +0200 | [diff] [blame] | 8175 | leaf = (struct lyd_node_leaf_list *)node; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 8176 | sleaf = (struct lys_node_leaf *)leaf->schema; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8177 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 8178 | switch (type) { |
| 8179 | case UNRES_LEAFREF: |
| 8180 | assert(sleaf->type.base == LY_TYPE_LEAFREF); |
Michal Vasko | 3568994 | 2019-09-10 10:33:33 +0200 | [diff] [blame] | 8181 | if (ignore_fail) { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 8182 | req_inst = -1; |
| 8183 | } else { |
| 8184 | req_inst = sleaf->type.info.lref.req; |
| 8185 | } |
Michal Vasko | 3568994 | 2019-09-10 10:33:33 +0200 | [diff] [blame] | 8186 | if ((leaf->schema->flags & LYS_LEAFREF_DEP) && (ignore_fail == 2)) { |
| 8187 | /* do not even try to resolve */ |
| 8188 | rc = 0; |
| 8189 | ret = NULL; |
| 8190 | } else { |
| 8191 | rc = resolve_leafref(leaf, sleaf->type.info.lref.path, req_inst, &ret); |
| 8192 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8193 | if (!rc) { |
Michal Vasko | b1ac872 | 2017-01-02 13:04:25 +0100 | [diff] [blame] | 8194 | if (ret && !(leaf->schema->flags & LYS_LEAFREF_DEP)) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8195 | /* valid resolved */ |
Michal Vasko | 70bf8e5 | 2018-03-26 11:32:33 +0200 | [diff] [blame] | 8196 | if (leaf->value_type == LY_TYPE_BITS) { |
Michal Vasko | 1c8567a | 2017-01-05 13:42:27 +0100 | [diff] [blame] | 8197 | free(leaf->value.bit); |
| 8198 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8199 | leaf->value.leafref = ret; |
| 8200 | leaf->value_type = LY_TYPE_LEAFREF; |
Michal Vasko | 101658e | 2018-06-05 15:05:54 +0200 | [diff] [blame] | 8201 | leaf->value_flags &= ~LY_VALUE_UNRES; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8202 | } else { |
| 8203 | /* valid unresolved */ |
Michal Vasko | 101658e | 2018-06-05 15:05:54 +0200 | [diff] [blame] | 8204 | if (!(leaf->value_flags & LY_VALUE_UNRES)) { |
Michal Vasko | 0abb1bc | 2020-02-25 15:47:10 +0100 | [diff] [blame] | 8205 | if (!lyp_parse_value(&sleaf->type, &leaf->value_str, NULL, leaf, NULL, NULL, 1, 0)) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8206 | return -1; |
| 8207 | } |
| 8208 | } |
| 8209 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8210 | } else { |
| 8211 | return rc; |
| 8212 | } |
| 8213 | break; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8214 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 8215 | case UNRES_INSTID: |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 8216 | assert(sleaf->type.base == LY_TYPE_INST); |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 8217 | ext_dep = check_instid_ext_dep(leaf->schema, leaf->value_str); |
| 8218 | if (ext_dep == -1) { |
| 8219 | return -1; |
| 8220 | } |
| 8221 | |
Michal Vasko | 3568994 | 2019-09-10 10:33:33 +0200 | [diff] [blame] | 8222 | if (ignore_fail) { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 8223 | req_inst = -1; |
| 8224 | } else { |
| 8225 | req_inst = sleaf->type.info.inst.req; |
| 8226 | } |
Michal Vasko | 3568994 | 2019-09-10 10:33:33 +0200 | [diff] [blame] | 8227 | if (ext_dep && (ignore_fail == 2)) { |
| 8228 | /* do not even try to resolve */ |
| 8229 | rc = 0; |
| 8230 | ret = NULL; |
| 8231 | } else { |
| 8232 | rc = resolve_instid(node, leaf->value_str, req_inst, &ret); |
| 8233 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8234 | if (!rc) { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 8235 | if (ret && !ext_dep) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8236 | /* valid resolved */ |
| 8237 | leaf->value.instance = ret; |
| 8238 | leaf->value_type = LY_TYPE_INST; |
Michal Vasko | 101658e | 2018-06-05 15:05:54 +0200 | [diff] [blame] | 8239 | leaf->value_flags &= ~LY_VALUE_UNRES; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8240 | } else { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8241 | /* valid unresolved */ |
| 8242 | leaf->value.instance = NULL; |
Michal Vasko | 70bf8e5 | 2018-03-26 11:32:33 +0200 | [diff] [blame] | 8243 | leaf->value_type = LY_TYPE_INST; |
Michal Vasko | 101658e | 2018-06-05 15:05:54 +0200 | [diff] [blame] | 8244 | leaf->value_flags |= LY_VALUE_UNRES; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8245 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8246 | } else { |
| 8247 | return rc; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8248 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 8249 | break; |
| 8250 | |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 8251 | case UNRES_UNION: |
| 8252 | assert(sleaf->type.base == LY_TYPE_UNION); |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 8253 | return resolve_union(leaf, &sleaf->type, 1, ignore_fail, NULL); |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 8254 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 8255 | case UNRES_WHEN: |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 8256 | if ((rc = resolve_when(node, ignore_fail, failed_when))) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 8257 | return rc; |
| 8258 | } |
| 8259 | break; |
| 8260 | |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 8261 | case UNRES_MUST: |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8262 | if ((rc = resolve_must(node, 0, ignore_fail))) { |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 8263 | return rc; |
| 8264 | } |
| 8265 | break; |
| 8266 | |
| 8267 | case UNRES_MUST_INOUT: |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8268 | if ((rc = resolve_must(node, 1, ignore_fail))) { |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 8269 | return rc; |
| 8270 | } |
| 8271 | break; |
| 8272 | |
Michal Vasko | 185b527 | 2018-09-13 14:26:12 +0200 | [diff] [blame] | 8273 | case UNRES_UNIQ_LEAVES: |
| 8274 | if (lyv_data_unique(node)) { |
| 8275 | return -1; |
| 8276 | } |
| 8277 | break; |
| 8278 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 8279 | default: |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8280 | LOGINT(NULL); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8281 | return -1; |
| 8282 | } |
| 8283 | |
| 8284 | return EXIT_SUCCESS; |
| 8285 | } |
| 8286 | |
| 8287 | /** |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8288 | * @brief add data unres item |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8289 | * |
| 8290 | * @param[in] unres Unres data structure to use. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 8291 | * @param[in] node Data node to use. |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8292 | * |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8293 | * @return 0 on success, -1 on error. |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8294 | */ |
| 8295 | int |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8296 | unres_data_add(struct unres_data *unres, struct lyd_node *node, enum UNRES_ITEM type) |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8297 | { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 8298 | assert(unres && node); |
Michal Vasko | c428084 | 2016-04-19 16:10:42 +0200 | [diff] [blame] | 8299 | assert((type == UNRES_LEAFREF) || (type == UNRES_INSTID) || (type == UNRES_WHEN) || (type == UNRES_MUST) |
Michal Vasko | 185b527 | 2018-09-13 14:26:12 +0200 | [diff] [blame] | 8300 | || (type == UNRES_MUST_INOUT) || (type == UNRES_UNION) || (type == UNRES_UNIQ_LEAVES)); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8301 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 8302 | unres->count++; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 8303 | unres->node = ly_realloc(unres->node, unres->count * sizeof *unres->node); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8304 | LY_CHECK_ERR_RETURN(!unres->node, LOGMEM(NULL), -1); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 8305 | unres->node[unres->count - 1] = node; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 8306 | unres->type = ly_realloc(unres->type, unres->count * sizeof *unres->type); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8307 | LY_CHECK_ERR_RETURN(!unres->type, LOGMEM(NULL), -1); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 8308 | unres->type[unres->count - 1] = type; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8309 | |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8310 | return 0; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8311 | } |
| 8312 | |
Michal Vasko | 20555d8 | 2018-12-12 16:30:50 +0100 | [diff] [blame] | 8313 | static void |
| 8314 | resolve_unres_data_autodel_diff(struct unres_data *unres, uint32_t unres_i) |
| 8315 | { |
| 8316 | struct lyd_node *next, *child, *parent; |
| 8317 | uint32_t i; |
| 8318 | |
| 8319 | for (i = 0; i < unres->diff_idx; ++i) { |
| 8320 | if (unres->diff->type[i] == LYD_DIFF_DELETED) { |
| 8321 | /* only leaf(-list) default could be removed and there is nothing to be checked in that case */ |
| 8322 | continue; |
| 8323 | } |
| 8324 | |
| 8325 | if (unres->diff->second[i] == unres->node[unres_i]) { |
| 8326 | /* 1) default value was supposed to be created, but is disabled by when |
| 8327 | * -> remove it from diff altogether |
| 8328 | */ |
| 8329 | unres_data_diff_rem(unres, i); |
| 8330 | /* if diff type is CREATED, the value was just a pointer, it can be freed normally (unlike in 4) */ |
| 8331 | return; |
| 8332 | } else { |
| 8333 | parent = unres->diff->second[i]->parent; |
| 8334 | while (parent && (parent != unres->node[unres_i])) { |
| 8335 | parent = parent->parent; |
| 8336 | } |
| 8337 | if (parent) { |
| 8338 | /* 2) default value was supposed to be created but is disabled by when in some parent |
| 8339 | * -> remove this default subtree and add the rest into diff as deleted instead in 4) |
| 8340 | */ |
| 8341 | unres_data_diff_rem(unres, i); |
| 8342 | break; |
| 8343 | } |
| 8344 | |
Michal Vasko | d6162b7 | 2019-11-13 14:04:38 +0100 | [diff] [blame] | 8345 | LY_TREE_DFS_BEGIN(unres->diff->second[i], next, child) { |
Michal Vasko | 20555d8 | 2018-12-12 16:30:50 +0100 | [diff] [blame] | 8346 | if (child == unres->node[unres_i]) { |
| 8347 | /* 3) some default child of a default value was supposed to be created but has false when |
| 8348 | * -> the subtree will be freed later and automatically disconnected from the diff parent node |
| 8349 | */ |
| 8350 | return; |
| 8351 | } |
| 8352 | |
Michal Vasko | d6162b7 | 2019-11-13 14:04:38 +0100 | [diff] [blame] | 8353 | LY_TREE_DFS_END(unres->diff->second[i], next, child); |
Michal Vasko | 20555d8 | 2018-12-12 16:30:50 +0100 | [diff] [blame] | 8354 | } |
| 8355 | } |
| 8356 | } |
| 8357 | |
| 8358 | /* 4) it does not overlap with created default values in any way |
| 8359 | * -> just add it into diff as deleted |
| 8360 | */ |
| 8361 | unres_data_diff_new(unres, unres->node[unres_i], unres->node[unres_i]->parent, 0); |
| 8362 | lyd_unlink(unres->node[unres_i]); |
| 8363 | |
| 8364 | /* should not be freed anymore */ |
| 8365 | unres->node[unres_i] = NULL; |
| 8366 | } |
| 8367 | |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8368 | /** |
| 8369 | * @brief Resolve every unres data item in the structure. Logs directly. |
| 8370 | * |
Michal Vasko | 660582a | 2018-03-19 10:10:08 +0100 | [diff] [blame] | 8371 | * If options include #LYD_OPT_TRUSTED, the data are considered trusted (must conditions are not expected, |
| 8372 | * unresolved leafrefs/instids are accepted, when conditions are normally resolved because at least some implicit |
| 8373 | * non-presence containers may need to be deleted). |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 8374 | * |
Michal Vasko | bbc43b1 | 2018-10-12 09:22:00 +0200 | [diff] [blame] | 8375 | * If options includes #LYD_OPT_WHENAUTODEL, the non-default nodes with false when conditions are auto-deleted. |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 8376 | * |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8377 | * @param[in] ctx Context used. |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8378 | * @param[in] unres Unres data structure to use. |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 8379 | * @param[in,out] root Root node of the data tree, can be changed due to autodeletion. |
| 8380 | * @param[in] options Data options as described above. |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8381 | * |
| 8382 | * @return EXIT_SUCCESS on success, -1 on error. |
| 8383 | */ |
| 8384 | int |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8385 | resolve_unres_data(struct ly_ctx *ctx, struct unres_data *unres, struct lyd_node **root, int options) |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8386 | { |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 8387 | uint32_t i, j, first, resolved, del_items, stmt_count; |
Michal Vasko | 8af8220 | 2018-11-14 11:21:01 +0100 | [diff] [blame] | 8388 | uint8_t prev_when_status; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 8389 | int rc, progress, ignore_fail; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8390 | enum int_log_opts prev_ilo; |
| 8391 | struct ly_err_item *prev_eitem; |
mohitarora24 | 89837dc | 2018-05-01 15:09:36 +0530 | [diff] [blame] | 8392 | LY_ERR prev_ly_errno = ly_errno; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8393 | struct lyd_node *parent; |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 8394 | struct lys_when *when; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8395 | |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 8396 | assert(root); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 8397 | assert(unres); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 8398 | |
| 8399 | if (!unres->count) { |
| 8400 | return EXIT_SUCCESS; |
| 8401 | } |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8402 | |
Michal Vasko | 7fa9314 | 2018-03-19 09:59:10 +0100 | [diff] [blame] | 8403 | if (options & (LYD_OPT_NOTIF_FILTER | LYD_OPT_GET | LYD_OPT_GETCONFIG | LYD_OPT_EDIT)) { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 8404 | ignore_fail = 1; |
| 8405 | } else if (options & LYD_OPT_NOEXTDEPS) { |
| 8406 | ignore_fail = 2; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8407 | } else { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 8408 | ignore_fail = 0; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8409 | } |
| 8410 | |
Michal Vasko | a0ffcab | 2016-05-02 14:52:08 +0200 | [diff] [blame] | 8411 | LOGVRB("Resolving unresolved data nodes and their constraints..."); |
Michal Vasko | 7fa9314 | 2018-03-19 09:59:10 +0100 | [diff] [blame] | 8412 | if (!ignore_fail) { |
| 8413 | /* remember logging state only if errors are generated and valid */ |
Michal Vasko | 7fa9314 | 2018-03-19 09:59:10 +0100 | [diff] [blame] | 8414 | ly_ilo_change(ctx, ILO_STORE, &prev_ilo, &prev_eitem); |
| 8415 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 8416 | |
Michal Vasko | 7fa9314 | 2018-03-19 09:59:10 +0100 | [diff] [blame] | 8417 | /* |
| 8418 | * when-stmt first |
| 8419 | */ |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 8420 | first = 1; |
| 8421 | stmt_count = 0; |
| 8422 | resolved = 0; |
| 8423 | del_items = 0; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 8424 | do { |
Michal Vasko | 7fa9314 | 2018-03-19 09:59:10 +0100 | [diff] [blame] | 8425 | if (!ignore_fail) { |
| 8426 | ly_err_free_next(ctx, prev_eitem); |
| 8427 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 8428 | progress = 0; |
Michal Vasko | 6df9413 | 2016-09-22 11:08:09 +0200 | [diff] [blame] | 8429 | for (i = 0; i < unres->count; i++) { |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 8430 | if (unres->type[i] != UNRES_WHEN) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 8431 | continue; |
| 8432 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 8433 | if (first) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8434 | /* count when-stmt nodes in unres list */ |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 8435 | stmt_count++; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8436 | } |
| 8437 | |
| 8438 | /* resolve when condition only when all parent when conditions are already resolved */ |
| 8439 | for (parent = unres->node[i]->parent; |
| 8440 | parent && LYD_WHEN_DONE(parent->when_status); |
| 8441 | parent = parent->parent) { |
| 8442 | if (!parent->parent && (parent->when_status & LYD_WHEN_FALSE)) { |
| 8443 | /* the parent node was already unlinked, do not resolve this node, |
Michal Vasko | e446b09 | 2017-08-11 10:58:09 +0200 | [diff] [blame] | 8444 | * it will be removed anyway, so just mark it as resolved |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8445 | */ |
| 8446 | unres->node[i]->when_status |= LYD_WHEN_FALSE; |
| 8447 | unres->type[i] = UNRES_RESOLVED; |
| 8448 | resolved++; |
| 8449 | break; |
| 8450 | } |
| 8451 | } |
| 8452 | if (parent) { |
| 8453 | continue; |
| 8454 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 8455 | |
Michal Vasko | 8af8220 | 2018-11-14 11:21:01 +0100 | [diff] [blame] | 8456 | prev_when_status = unres->node[i]->when_status; |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 8457 | rc = resolve_unres_data_item(unres->node[i], unres->type[i], ignore_fail, &when); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 8458 | if (!rc) { |
Michal Vasko | 8af8220 | 2018-11-14 11:21:01 +0100 | [diff] [blame] | 8459 | /* finish with error/delete the node only if when was changed from true to false, an external |
| 8460 | * dependency was not required, or it was not provided (the flag would not be passed down otherwise, |
| 8461 | * checked in upper functions) */ |
Michal Vasko | e446b09 | 2017-08-11 10:58:09 +0200 | [diff] [blame] | 8462 | if ((unres->node[i]->when_status & LYD_WHEN_FALSE) |
Michal Vasko | c04173b | 2018-03-09 10:43:22 +0100 | [diff] [blame] | 8463 | && (!(when->flags & (LYS_XPCONF_DEP | LYS_XPSTATE_DEP)) || !(options & LYD_OPT_NOEXTDEPS))) { |
Michal Vasko | 8af8220 | 2018-11-14 11:21:01 +0100 | [diff] [blame] | 8464 | if ((!(prev_when_status & LYD_WHEN_TRUE) || !(options & LYD_OPT_WHENAUTODEL)) && !unres->node[i]->dflt) { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 8465 | /* false when condition */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8466 | goto error; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8467 | } /* follows else */ |
| 8468 | |
Michal Vasko | e31d34a | 2017-03-28 14:50:38 +0200 | [diff] [blame] | 8469 | /* auto-delete */ |
Michal Vasko | 8af8220 | 2018-11-14 11:21:01 +0100 | [diff] [blame] | 8470 | LOGVRB("Auto-deleting node \"%s\" due to when condition (%s)", ly_errpath(ctx), when->cond); |
Michal Vasko | e31d34a | 2017-03-28 14:50:38 +0200 | [diff] [blame] | 8471 | |
Michal Vasko | 56c1428 | 2019-11-12 10:02:27 +0100 | [diff] [blame] | 8472 | /* do not delete yet, the subtree can contain another nodes stored in the unres list */ |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 8473 | /* if it has parent non-presence containers that would be empty, we should actually |
| 8474 | * remove the container |
| 8475 | */ |
Radek Krejci | 2537fd3 | 2016-09-07 16:22:41 +0200 | [diff] [blame] | 8476 | for (parent = unres->node[i]; |
| 8477 | parent->parent && parent->parent->schema->nodetype == LYS_CONTAINER; |
| 8478 | parent = parent->parent) { |
| 8479 | if (((struct lys_node_container *)parent->parent->schema)->presence) { |
| 8480 | /* presence container */ |
| 8481 | break; |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 8482 | } |
Radek Krejci | 2537fd3 | 2016-09-07 16:22:41 +0200 | [diff] [blame] | 8483 | if (parent->next || parent->prev != parent) { |
| 8484 | /* non empty (the child we are in and we are going to remove is not the only child) */ |
| 8485 | break; |
| 8486 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 8487 | } |
Radek Krejci | 2537fd3 | 2016-09-07 16:22:41 +0200 | [diff] [blame] | 8488 | unres->node[i] = parent; |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 8489 | |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 8490 | if (*root && *root == unres->node[i]) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8491 | *root = (*root)->next; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 8492 | } |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8493 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8494 | unres->type[i] = UNRES_DELETE; |
| 8495 | del_items++; |
Radek Krejci | 51fd816 | 2016-03-24 15:49:51 +0100 | [diff] [blame] | 8496 | |
| 8497 | /* update the rest of unres items */ |
| 8498 | for (j = 0; j < unres->count; j++) { |
Radek Krejci | 3db819b | 2016-03-24 16:29:48 +0100 | [diff] [blame] | 8499 | if (unres->type[j] == UNRES_RESOLVED || unres->type[j] == UNRES_DELETE) { |
Radek Krejci | 51fd816 | 2016-03-24 15:49:51 +0100 | [diff] [blame] | 8500 | continue; |
| 8501 | } |
| 8502 | |
| 8503 | /* test if the node is in subtree to be deleted */ |
| 8504 | for (parent = unres->node[j]; parent; parent = parent->parent) { |
| 8505 | if (parent == unres->node[i]) { |
| 8506 | /* yes, it is */ |
| 8507 | unres->type[j] = UNRES_RESOLVED; |
| 8508 | resolved++; |
| 8509 | break; |
| 8510 | } |
| 8511 | } |
| 8512 | } |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8513 | } else { |
| 8514 | unres->type[i] = UNRES_RESOLVED; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 8515 | } |
Michal Vasko | 7fa9314 | 2018-03-19 09:59:10 +0100 | [diff] [blame] | 8516 | if (!ignore_fail) { |
| 8517 | ly_err_free_next(ctx, prev_eitem); |
| 8518 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 8519 | resolved++; |
| 8520 | progress = 1; |
| 8521 | } else if (rc == -1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8522 | goto error; |
Radek Krejci | 2467a49 | 2016-10-24 15:16:59 +0200 | [diff] [blame] | 8523 | } /* else forward reference */ |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 8524 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 8525 | first = 0; |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 8526 | } while (progress && resolved < stmt_count); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 8527 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8528 | /* do we have some unresolved when-stmt? */ |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 8529 | if (stmt_count > resolved) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8530 | goto error; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8531 | } |
| 8532 | |
| 8533 | for (i = 0; del_items && i < unres->count; i++) { |
| 8534 | /* we had some when-stmt resulted to false, so now we have to sanitize the unres list */ |
| 8535 | if (unres->type[i] != UNRES_DELETE) { |
| 8536 | continue; |
| 8537 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 8538 | if (!unres->node[i]) { |
| 8539 | unres->type[i] = UNRES_RESOLVED; |
| 8540 | del_items--; |
| 8541 | continue; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8542 | } |
| 8543 | |
Michal Vasko | 20555d8 | 2018-12-12 16:30:50 +0100 | [diff] [blame] | 8544 | if (unres->store_diff) { |
| 8545 | resolve_unres_data_autodel_diff(unres, i); |
| 8546 | } |
| 8547 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8548 | /* really remove the complete subtree */ |
| 8549 | lyd_free(unres->node[i]); |
| 8550 | unres->type[i] = UNRES_RESOLVED; |
| 8551 | del_items--; |
| 8552 | } |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 8553 | |
Michal Vasko | 7fa9314 | 2018-03-19 09:59:10 +0100 | [diff] [blame] | 8554 | /* |
| 8555 | * now leafrefs |
| 8556 | */ |
| 8557 | if (options & LYD_OPT_TRUSTED) { |
| 8558 | /* we want to attempt to resolve leafrefs */ |
| 8559 | assert(!ignore_fail); |
| 8560 | ignore_fail = 1; |
| 8561 | |
| 8562 | ly_ilo_restore(ctx, prev_ilo, prev_eitem, 0); |
| 8563 | ly_errno = prev_ly_errno; |
| 8564 | } |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 8565 | first = 1; |
| 8566 | stmt_count = 0; |
| 8567 | resolved = 0; |
| 8568 | do { |
| 8569 | progress = 0; |
| 8570 | for (i = 0; i < unres->count; i++) { |
| 8571 | if (unres->type[i] != UNRES_LEAFREF) { |
| 8572 | continue; |
| 8573 | } |
| 8574 | if (first) { |
| 8575 | /* count leafref nodes in unres list */ |
| 8576 | stmt_count++; |
| 8577 | } |
| 8578 | |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 8579 | rc = resolve_unres_data_item(unres->node[i], unres->type[i], ignore_fail, NULL); |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 8580 | if (!rc) { |
| 8581 | unres->type[i] = UNRES_RESOLVED; |
Michal Vasko | 7fa9314 | 2018-03-19 09:59:10 +0100 | [diff] [blame] | 8582 | if (!ignore_fail) { |
| 8583 | ly_err_free_next(ctx, prev_eitem); |
| 8584 | } |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 8585 | resolved++; |
| 8586 | progress = 1; |
| 8587 | } else if (rc == -1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8588 | goto error; |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 8589 | } /* else forward reference */ |
| 8590 | } |
| 8591 | first = 0; |
| 8592 | } while (progress && resolved < stmt_count); |
| 8593 | |
| 8594 | /* do we have some unresolved leafrefs? */ |
| 8595 | if (stmt_count > resolved) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8596 | goto error; |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 8597 | } |
| 8598 | |
Michal Vasko | 7fa9314 | 2018-03-19 09:59:10 +0100 | [diff] [blame] | 8599 | if (!ignore_fail) { |
| 8600 | /* log normally now, throw away irrelevant errors */ |
| 8601 | ly_ilo_restore(ctx, prev_ilo, prev_eitem, 0); |
| 8602 | ly_errno = prev_ly_errno; |
| 8603 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 8604 | |
Michal Vasko | 7fa9314 | 2018-03-19 09:59:10 +0100 | [diff] [blame] | 8605 | /* |
| 8606 | * rest |
| 8607 | */ |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8608 | for (i = 0; i < unres->count; ++i) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 8609 | if (unres->type[i] == UNRES_RESOLVED) { |
| 8610 | continue; |
| 8611 | } |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 8612 | assert(!(options & LYD_OPT_TRUSTED) || ((unres->type[i] != UNRES_MUST) && (unres->type[i] != UNRES_MUST_INOUT))); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 8613 | |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 8614 | rc = resolve_unres_data_item(unres->node[i], unres->type[i], ignore_fail, NULL); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8615 | if (rc) { |
| 8616 | /* since when was already resolved, a forward reference is an error */ |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8617 | return -1; |
| 8618 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8619 | |
| 8620 | unres->type[i] = UNRES_RESOLVED; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8621 | } |
| 8622 | |
Michal Vasko | a0ffcab | 2016-05-02 14:52:08 +0200 | [diff] [blame] | 8623 | LOGVRB("All data nodes and constraints resolved."); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 8624 | unres->count = 0; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8625 | return EXIT_SUCCESS; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8626 | |
| 8627 | error: |
Michal Vasko | 7fa9314 | 2018-03-19 09:59:10 +0100 | [diff] [blame] | 8628 | if (!ignore_fail) { |
| 8629 | /* print all the new errors */ |
| 8630 | ly_ilo_restore(ctx, prev_ilo, prev_eitem, 1); |
| 8631 | /* do not restore ly_errno, it was udpated properly */ |
| 8632 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8633 | return -1; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8634 | } |