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 | * |
| 6 | * Copyright (c) 2015 CESNET, z.s.p.o. |
| 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" |
Radek Krejci | 41912fe | 2015-10-22 10:22:12 +0200 | [diff] [blame] | 30 | #include "dict_private.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 | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 33 | |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 34 | int |
| 35 | 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] | 36 | { |
| 37 | const char *ptr; |
| 38 | int minus = 0; |
| 39 | int64_t ret = 0; |
Radek Krejci | bf47a82 | 2016-11-04 10:06:08 +0100 | [diff] [blame] | 40 | int8_t str_exp, str_dig = -1, trailing_zeros = 0; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 41 | |
| 42 | ptr = *str_num; |
| 43 | |
| 44 | if (ptr[0] == '-') { |
| 45 | minus = 1; |
| 46 | ++ptr; |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 47 | } else if (ptr[0] == '+') { |
| 48 | ++ptr; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 49 | } |
| 50 | |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 51 | if (!isdigit(ptr[0])) { |
| 52 | /* there must be at least one */ |
| 53 | return 1; |
| 54 | } |
| 55 | |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 56 | for (str_exp = 0; isdigit(ptr[0]) || ((ptr[0] == '.') && (str_dig < 0)); ++ptr) { |
| 57 | if (str_exp > 18) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 58 | return 1; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | if (ptr[0] == '.') { |
| 62 | if (ptr[1] == '.') { |
| 63 | /* it's the next interval */ |
| 64 | break; |
| 65 | } |
| 66 | ++str_dig; |
| 67 | } else { |
Radek Krejci | bf47a82 | 2016-11-04 10:06:08 +0100 | [diff] [blame] | 68 | ret = ret * 10 + (ptr[0] - '0'); |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 69 | if (str_dig > -1) { |
| 70 | ++str_dig; |
Radek Krejci | bf47a82 | 2016-11-04 10:06:08 +0100 | [diff] [blame] | 71 | if (ptr[0] == '0') { |
| 72 | /* possibly trailing zero */ |
| 73 | trailing_zeros++; |
| 74 | } else { |
| 75 | trailing_zeros = 0; |
| 76 | } |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 77 | } |
| 78 | ++str_exp; |
| 79 | } |
| 80 | } |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 81 | if (str_dig == 0) { |
| 82 | /* no digits after '.' */ |
| 83 | return 1; |
| 84 | } else if (str_dig == -1) { |
| 85 | /* there are 0 numbers after the floating point */ |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 86 | str_dig = 0; |
| 87 | } |
Radek Krejci | bf47a82 | 2016-11-04 10:06:08 +0100 | [diff] [blame] | 88 | /* remove trailing zeros */ |
| 89 | if (trailing_zeros) { |
Michal Vasko | 6ca5ca7 | 2016-11-28 09:21:51 +0100 | [diff] [blame] | 90 | str_dig -= trailing_zeros; |
| 91 | str_exp -= trailing_zeros; |
Radek Krejci | bf47a82 | 2016-11-04 10:06:08 +0100 | [diff] [blame] | 92 | ret = ret / dec_pow(trailing_zeros); |
| 93 | } |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 94 | |
| 95 | /* it's parsed, now adjust the number based on fraction-digits, if needed */ |
| 96 | if (str_dig < dig) { |
| 97 | if ((str_exp - 1) + (dig - str_dig) > 18) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 98 | return 1; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 99 | } |
| 100 | ret *= dec_pow(dig - str_dig); |
| 101 | } |
| 102 | if (str_dig > dig) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 103 | return 1; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | if (minus) { |
| 107 | ret *= -1; |
| 108 | } |
| 109 | *str_num = ptr; |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 110 | *num = ret; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 111 | |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 112 | return 0; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /** |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 116 | * @brief Parse an identifier. |
| 117 | * |
| 118 | * ;; An identifier MUST NOT start with (('X'|'x') ('M'|'m') ('L'|'l')) |
| 119 | * identifier = (ALPHA / "_") |
| 120 | * *(ALPHA / DIGIT / "_" / "-" / ".") |
| 121 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 122 | * @param[in] id Identifier to use. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 123 | * |
| 124 | * @return Number of characters successfully parsed. |
| 125 | */ |
Michal Vasko | 249e6b5 | 2015-08-19 11:08:52 +0200 | [diff] [blame] | 126 | int |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 127 | parse_identifier(const char *id) |
| 128 | { |
| 129 | int parsed = 0; |
| 130 | |
Michal Vasko | 1ab90bc | 2016-03-15 10:40:22 +0100 | [diff] [blame] | 131 | assert(id); |
| 132 | |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 133 | if (!isalpha(id[0]) && (id[0] != '_')) { |
| 134 | return -parsed; |
| 135 | } |
| 136 | |
| 137 | ++parsed; |
| 138 | ++id; |
| 139 | |
| 140 | while (isalnum(id[0]) || (id[0] == '_') || (id[0] == '-') || (id[0] == '.')) { |
| 141 | ++parsed; |
| 142 | ++id; |
| 143 | } |
| 144 | |
| 145 | return parsed; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * @brief Parse a node-identifier. |
| 150 | * |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 151 | * node-identifier = [module-name ":"] identifier |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 152 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 153 | * @param[in] id Identifier to use. |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 154 | * @param[out] mod_name Points to the module name, NULL if there is not any. |
| 155 | * @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] | 156 | * @param[out] name Points to the node name. |
| 157 | * @param[out] nam_len Length of the node name. |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 158 | * @param[out] all_desc Whether the path starts with '/', only supported in extended paths. |
| 159 | * @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] | 160 | * |
| 161 | * @return Number of characters successfully parsed, |
| 162 | * positive on success, negative on failure. |
| 163 | */ |
| 164 | static int |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 165 | parse_node_identifier(const char *id, const char **mod_name, int *mod_name_len, const char **name, int *nam_len, |
| 166 | int *all_desc, int extended) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 167 | { |
| 168 | int parsed = 0, ret; |
| 169 | |
| 170 | assert(id); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 171 | assert((mod_name && mod_name_len) || (!mod_name && !mod_name_len)); |
| 172 | assert((name && nam_len) || (!name && !nam_len)); |
| 173 | assert(!extended || all_desc); |
| 174 | |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 175 | if (mod_name) { |
| 176 | *mod_name = NULL; |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 177 | *mod_name_len = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 178 | } |
| 179 | if (name) { |
| 180 | *name = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 181 | *nam_len = 0; |
| 182 | } |
| 183 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 184 | if (extended) { |
| 185 | /* try to parse only the extended expressions */ |
| 186 | if (id[parsed] == '/') { |
| 187 | *all_desc = 1; |
| 188 | } else { |
| 189 | *all_desc = 0; |
| 190 | } |
| 191 | |
| 192 | /* is there a prefix? */ |
| 193 | ret = parse_identifier(id + *all_desc); |
| 194 | if (ret > 0) { |
| 195 | if (id[*all_desc + ret] != ':') { |
| 196 | /* this is not a prefix, so not an extended id */ |
| 197 | goto standard_id; |
| 198 | } |
| 199 | |
| 200 | if (mod_name) { |
| 201 | *mod_name = id + *all_desc; |
| 202 | *mod_name_len = ret; |
| 203 | } |
| 204 | |
| 205 | /* "/" and ":" */ |
| 206 | ret += *all_desc + 1; |
| 207 | } else { |
| 208 | ret = *all_desc; |
| 209 | } |
| 210 | |
| 211 | /* parse either "*" or "." */ |
| 212 | if (!strcmp(id + ret, "*")) { |
| 213 | if (name) { |
| 214 | *name = id + ret; |
| 215 | *nam_len = 1; |
| 216 | } |
| 217 | ++ret; |
| 218 | |
| 219 | return ret; |
| 220 | } else if (!strcmp(id + ret, ".")) { |
| 221 | if (!*all_desc) { |
| 222 | /* /. is redundant expression, we do not accept it */ |
| 223 | return -ret; |
| 224 | } |
| 225 | |
| 226 | if (name) { |
| 227 | *name = id + ret; |
| 228 | *nam_len = 1; |
| 229 | } |
| 230 | ++ret; |
| 231 | |
| 232 | return ret; |
| 233 | } |
| 234 | /* else a standard id, parse it all again */ |
| 235 | } |
| 236 | |
| 237 | standard_id: |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 238 | if ((ret = parse_identifier(id)) < 1) { |
| 239 | return ret; |
| 240 | } |
| 241 | |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 242 | if (mod_name) { |
| 243 | *mod_name = id; |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 244 | *mod_name_len = ret; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | parsed += ret; |
| 248 | id += ret; |
| 249 | |
| 250 | /* there is prefix */ |
| 251 | if (id[0] == ':') { |
| 252 | ++parsed; |
| 253 | ++id; |
| 254 | |
| 255 | /* there isn't */ |
| 256 | } else { |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 257 | if (name && mod_name) { |
| 258 | *name = *mod_name; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 259 | } |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 260 | if (mod_name) { |
| 261 | *mod_name = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 262 | } |
| 263 | |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 264 | if (nam_len && mod_name_len) { |
| 265 | *nam_len = *mod_name_len; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 266 | } |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 267 | if (mod_name_len) { |
| 268 | *mod_name_len = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | return parsed; |
| 272 | } |
| 273 | |
| 274 | /* identifier (node name) */ |
| 275 | if ((ret = parse_identifier(id)) < 1) { |
| 276 | return -parsed+ret; |
| 277 | } |
| 278 | |
| 279 | if (name) { |
| 280 | *name = id; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 281 | *nam_len = ret; |
| 282 | } |
| 283 | |
| 284 | return parsed+ret; |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * @brief Parse a path-predicate (leafref). |
| 289 | * |
| 290 | * path-predicate = "[" *WSP path-equality-expr *WSP "]" |
| 291 | * path-equality-expr = node-identifier *WSP "=" *WSP path-key-expr |
| 292 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 293 | * @param[in] id Identifier to use. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 294 | * @param[out] prefix Points to the prefix, NULL if there is not any. |
| 295 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 296 | * @param[out] name Points to the node name. |
| 297 | * @param[out] nam_len Length of the node name. |
| 298 | * @param[out] path_key_expr Points to the path-key-expr. |
| 299 | * @param[out] pke_len Length of the path-key-expr. |
| 300 | * @param[out] has_predicate Flag to mark whether there is another predicate following. |
| 301 | * |
| 302 | * @return Number of characters successfully parsed, |
| 303 | * positive on success, negative on failure. |
| 304 | */ |
| 305 | static int |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 306 | parse_path_predicate(const char *id, const char **prefix, int *pref_len, const char **name, int *nam_len, |
| 307 | const char **path_key_expr, int *pke_len, int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 308 | { |
| 309 | const char *ptr; |
| 310 | int parsed = 0, ret; |
| 311 | |
| 312 | assert(id); |
| 313 | if (prefix) { |
| 314 | *prefix = NULL; |
| 315 | } |
| 316 | if (pref_len) { |
| 317 | *pref_len = 0; |
| 318 | } |
| 319 | if (name) { |
| 320 | *name = NULL; |
| 321 | } |
| 322 | if (nam_len) { |
| 323 | *nam_len = 0; |
| 324 | } |
| 325 | if (path_key_expr) { |
| 326 | *path_key_expr = NULL; |
| 327 | } |
| 328 | if (pke_len) { |
| 329 | *pke_len = 0; |
| 330 | } |
| 331 | if (has_predicate) { |
| 332 | *has_predicate = 0; |
| 333 | } |
| 334 | |
| 335 | if (id[0] != '[') { |
| 336 | return -parsed; |
| 337 | } |
| 338 | |
| 339 | ++parsed; |
| 340 | ++id; |
| 341 | |
| 342 | while (isspace(id[0])) { |
| 343 | ++parsed; |
| 344 | ++id; |
| 345 | } |
| 346 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 347 | if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len, NULL, 0)) < 1) { |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 348 | return -parsed+ret; |
| 349 | } |
| 350 | |
| 351 | parsed += ret; |
| 352 | id += ret; |
| 353 | |
| 354 | while (isspace(id[0])) { |
| 355 | ++parsed; |
| 356 | ++id; |
| 357 | } |
| 358 | |
| 359 | if (id[0] != '=') { |
| 360 | return -parsed; |
| 361 | } |
| 362 | |
| 363 | ++parsed; |
| 364 | ++id; |
| 365 | |
| 366 | while (isspace(id[0])) { |
| 367 | ++parsed; |
| 368 | ++id; |
| 369 | } |
| 370 | |
| 371 | if ((ptr = strchr(id, ']')) == NULL) { |
| 372 | return -parsed; |
| 373 | } |
| 374 | |
| 375 | --ptr; |
| 376 | while (isspace(ptr[0])) { |
| 377 | --ptr; |
| 378 | } |
| 379 | ++ptr; |
| 380 | |
| 381 | ret = ptr-id; |
| 382 | if (path_key_expr) { |
| 383 | *path_key_expr = id; |
| 384 | } |
| 385 | if (pke_len) { |
| 386 | *pke_len = ret; |
| 387 | } |
| 388 | |
| 389 | parsed += ret; |
| 390 | id += ret; |
| 391 | |
| 392 | while (isspace(id[0])) { |
| 393 | ++parsed; |
| 394 | ++id; |
| 395 | } |
| 396 | |
| 397 | assert(id[0] == ']'); |
| 398 | |
| 399 | if (id[1] == '[') { |
| 400 | *has_predicate = 1; |
| 401 | } |
| 402 | |
| 403 | return parsed+1; |
| 404 | } |
| 405 | |
| 406 | /** |
| 407 | * @brief Parse a path-key-expr (leafref). First call parses "current()", all |
| 408 | * the ".." and the first node-identifier, other calls parse a single |
| 409 | * node-identifier each. |
| 410 | * |
| 411 | * path-key-expr = current-function-invocation *WSP "/" *WSP |
| 412 | * rel-path-keyexpr |
| 413 | * rel-path-keyexpr = 1*(".." *WSP "/" *WSP) |
| 414 | * *(node-identifier *WSP "/" *WSP) |
| 415 | * node-identifier |
| 416 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 417 | * @param[in] id Identifier to use. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 418 | * @param[out] prefix Points to the prefix, NULL if there is not any. |
| 419 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 420 | * @param[out] name Points to the node name. |
| 421 | * @param[out] nam_len Length of the node name. |
| 422 | * @param[out] parent_times Number of ".." in the path. Must be 0 on the first call, |
| 423 | * must not be changed between consecutive calls. |
| 424 | * @return Number of characters successfully parsed, |
| 425 | * positive on success, negative on failure. |
| 426 | */ |
| 427 | static int |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 428 | parse_path_key_expr(const char *id, const char **prefix, int *pref_len, const char **name, int *nam_len, |
| 429 | int *parent_times) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 430 | { |
| 431 | int parsed = 0, ret, par_times = 0; |
| 432 | |
| 433 | assert(id); |
| 434 | assert(parent_times); |
| 435 | if (prefix) { |
| 436 | *prefix = NULL; |
| 437 | } |
| 438 | if (pref_len) { |
| 439 | *pref_len = 0; |
| 440 | } |
| 441 | if (name) { |
| 442 | *name = NULL; |
| 443 | } |
| 444 | if (nam_len) { |
| 445 | *nam_len = 0; |
| 446 | } |
| 447 | |
| 448 | if (!*parent_times) { |
| 449 | /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */ |
| 450 | if (strncmp(id, "current()", 9)) { |
| 451 | return -parsed; |
| 452 | } |
| 453 | |
| 454 | parsed += 9; |
| 455 | id += 9; |
| 456 | |
| 457 | while (isspace(id[0])) { |
| 458 | ++parsed; |
| 459 | ++id; |
| 460 | } |
| 461 | |
| 462 | if (id[0] != '/') { |
| 463 | return -parsed; |
| 464 | } |
| 465 | |
| 466 | ++parsed; |
| 467 | ++id; |
| 468 | |
| 469 | while (isspace(id[0])) { |
| 470 | ++parsed; |
| 471 | ++id; |
| 472 | } |
| 473 | |
| 474 | /* rel-path-keyexpr */ |
| 475 | if (strncmp(id, "..", 2)) { |
| 476 | return -parsed; |
| 477 | } |
| 478 | ++par_times; |
| 479 | |
| 480 | parsed += 2; |
| 481 | id += 2; |
| 482 | |
| 483 | while (isspace(id[0])) { |
| 484 | ++parsed; |
| 485 | ++id; |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | /* 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier |
| 490 | * |
| 491 | * first parent reference with whitespaces already parsed |
| 492 | */ |
| 493 | if (id[0] != '/') { |
| 494 | return -parsed; |
| 495 | } |
| 496 | |
| 497 | ++parsed; |
| 498 | ++id; |
| 499 | |
| 500 | while (isspace(id[0])) { |
| 501 | ++parsed; |
| 502 | ++id; |
| 503 | } |
| 504 | |
| 505 | while (!strncmp(id, "..", 2) && !*parent_times) { |
| 506 | ++par_times; |
| 507 | |
| 508 | parsed += 2; |
| 509 | id += 2; |
| 510 | |
| 511 | while (isspace(id[0])) { |
| 512 | ++parsed; |
| 513 | ++id; |
| 514 | } |
| 515 | |
| 516 | if (id[0] != '/') { |
| 517 | return -parsed; |
| 518 | } |
| 519 | |
| 520 | ++parsed; |
| 521 | ++id; |
| 522 | |
| 523 | while (isspace(id[0])) { |
| 524 | ++parsed; |
| 525 | ++id; |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | if (!*parent_times) { |
| 530 | *parent_times = par_times; |
| 531 | } |
| 532 | |
| 533 | /* all parent references must be parsed at this point */ |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 534 | if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len, NULL, 0)) < 1) { |
| 535 | return -parsed + ret; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | parsed += ret; |
| 539 | id += ret; |
| 540 | |
| 541 | return parsed; |
| 542 | } |
| 543 | |
| 544 | /** |
| 545 | * @brief Parse path-arg (leafref). |
| 546 | * |
| 547 | * path-arg = absolute-path / relative-path |
| 548 | * absolute-path = 1*("/" (node-identifier *path-predicate)) |
| 549 | * relative-path = 1*(".." "/") descendant-path |
| 550 | * |
Radek Krejci | f7ed4c3 | 2016-10-27 16:20:03 +0200 | [diff] [blame] | 551 | * @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] | 552 | * @param[in] id Identifier to use. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 553 | * @param[out] prefix Points to the prefix, NULL if there is not any. |
| 554 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 555 | * @param[out] name Points to the node name. |
| 556 | * @param[out] nam_len Length of the node name. |
| 557 | * @param[out] parent_times Number of ".." in the path. Must be 0 on the first call, |
| 558 | * must not be changed between consecutive calls. -1 if the |
| 559 | * path is relative. |
| 560 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 561 | * |
| 562 | * @return Number of characters successfully parsed, |
| 563 | * positive on success, negative on failure. |
| 564 | */ |
| 565 | static int |
Michal Vasko | 3c60cbb | 2017-07-10 11:50:03 +0200 | [diff] [blame] | 566 | 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] | 567 | const char **name, int *nam_len, int *parent_times, int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 568 | { |
| 569 | int parsed = 0, ret, par_times = 0; |
| 570 | |
| 571 | assert(id); |
| 572 | assert(parent_times); |
| 573 | if (prefix) { |
| 574 | *prefix = NULL; |
| 575 | } |
| 576 | if (pref_len) { |
| 577 | *pref_len = 0; |
| 578 | } |
| 579 | if (name) { |
| 580 | *name = NULL; |
| 581 | } |
| 582 | if (nam_len) { |
| 583 | *nam_len = 0; |
| 584 | } |
| 585 | if (has_predicate) { |
| 586 | *has_predicate = 0; |
| 587 | } |
| 588 | |
| 589 | if (!*parent_times && !strncmp(id, "..", 2)) { |
| 590 | ++par_times; |
| 591 | |
| 592 | parsed += 2; |
| 593 | id += 2; |
| 594 | |
| 595 | while (!strncmp(id, "/..", 3)) { |
| 596 | ++par_times; |
| 597 | |
| 598 | parsed += 3; |
| 599 | id += 3; |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | if (!*parent_times) { |
| 604 | if (par_times) { |
| 605 | *parent_times = par_times; |
| 606 | } else { |
| 607 | *parent_times = -1; |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | if (id[0] != '/') { |
| 612 | return -parsed; |
| 613 | } |
| 614 | |
| 615 | /* skip '/' */ |
| 616 | ++parsed; |
| 617 | ++id; |
| 618 | |
| 619 | /* node-identifier ([prefix:]identifier) */ |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 620 | if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len, NULL, 0)) < 1) { |
| 621 | return -parsed - ret; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 622 | } |
Michal Vasko | 3c60cbb | 2017-07-10 11:50:03 +0200 | [diff] [blame] | 623 | if (prefix && !(*prefix)) { |
Radek Krejci | f7ed4c3 | 2016-10-27 16:20:03 +0200 | [diff] [blame] | 624 | /* actually we always need prefix even it is not specified */ |
| 625 | *prefix = lys_main_module(mod)->name; |
| 626 | *pref_len = strlen(*prefix); |
| 627 | } |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 628 | |
| 629 | parsed += ret; |
| 630 | id += ret; |
| 631 | |
| 632 | /* there is no predicate */ |
| 633 | if ((id[0] == '/') || !id[0]) { |
| 634 | return parsed; |
| 635 | } else if (id[0] != '[') { |
| 636 | return -parsed; |
| 637 | } |
| 638 | |
| 639 | if (has_predicate) { |
| 640 | *has_predicate = 1; |
| 641 | } |
| 642 | |
| 643 | return parsed; |
| 644 | } |
| 645 | |
| 646 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 647 | * @brief Parse instance-identifier in JSON data format. That means that prefixes |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 648 | * are actually model names. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 649 | * |
| 650 | * instance-identifier = 1*("/" (node-identifier *predicate)) |
| 651 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 652 | * @param[in] id Identifier to use. |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 653 | * @param[out] model Points to the model name. |
| 654 | * @param[out] mod_len Length of the model name. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 655 | * @param[out] name Points to the node name. |
| 656 | * @param[out] nam_len Length of the node name. |
| 657 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 658 | * |
| 659 | * @return Number of characters successfully parsed, |
| 660 | * positive on success, negative on failure. |
| 661 | */ |
| 662 | static int |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 663 | parse_instance_identifier(const char *id, const char **model, int *mod_len, const char **name, int *nam_len, |
| 664 | int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 665 | { |
| 666 | int parsed = 0, ret; |
| 667 | |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 668 | assert(id && model && mod_len && name && nam_len); |
| 669 | |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 670 | if (has_predicate) { |
| 671 | *has_predicate = 0; |
| 672 | } |
| 673 | |
| 674 | if (id[0] != '/') { |
| 675 | return -parsed; |
| 676 | } |
| 677 | |
| 678 | ++parsed; |
| 679 | ++id; |
| 680 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 681 | if ((ret = parse_identifier(id)) < 1) { |
| 682 | return ret; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 683 | } |
| 684 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 685 | *name = id; |
| 686 | *nam_len = ret; |
| 687 | |
| 688 | parsed += ret; |
| 689 | id += ret; |
| 690 | |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 691 | if (id[0] == ':') { |
| 692 | /* we have prefix */ |
| 693 | *model = *name; |
| 694 | *mod_len = *nam_len; |
| 695 | |
| 696 | ++parsed; |
| 697 | ++id; |
| 698 | |
| 699 | if ((ret = parse_identifier(id)) < 1) { |
| 700 | return ret; |
| 701 | } |
| 702 | |
| 703 | *name = id; |
| 704 | *nam_len = ret; |
| 705 | |
| 706 | parsed += ret; |
| 707 | id += ret; |
| 708 | } |
| 709 | |
Radek Krejci | 4967cb6 | 2016-09-14 16:40:28 +0200 | [diff] [blame] | 710 | if (id[0] == '[' && has_predicate) { |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 711 | *has_predicate = 1; |
| 712 | } |
| 713 | |
| 714 | return parsed; |
| 715 | } |
| 716 | |
| 717 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 718 | * @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] | 719 | * (which are mandatory) are actually model names. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 720 | * |
| 721 | * predicate = "[" *WSP (predicate-expr / pos) *WSP "]" |
| 722 | * predicate-expr = (node-identifier / ".") *WSP "=" *WSP |
| 723 | * ((DQUOTE string DQUOTE) / |
| 724 | * (SQUOTE string SQUOTE)) |
| 725 | * pos = non-negative-integer-value |
| 726 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 727 | * @param[in] id Identifier to use. |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 728 | * @param[out] model Points to the model name. |
| 729 | * @param[out] mod_len Length of the model name. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 730 | * @param[out] name Points to the node name. Can be identifier (from node-identifier), "." or pos. |
| 731 | * @param[out] nam_len Length of the node name. |
| 732 | * @param[out] value Value the node-identifier must have (string from the grammar), |
| 733 | * NULL if there is not any. |
| 734 | * @param[out] val_len Length of the value, 0 if there is not any. |
| 735 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 736 | * |
| 737 | * @return Number of characters successfully parsed, |
| 738 | * positive on success, negative on failure. |
| 739 | */ |
| 740 | static int |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 741 | parse_predicate(const char *id, const char **model, int *mod_len, const char **name, int *nam_len, |
| 742 | const char **value, int *val_len, int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 743 | { |
| 744 | const char *ptr; |
| 745 | int parsed = 0, ret; |
| 746 | char quote; |
| 747 | |
| 748 | assert(id); |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 749 | if (model) { |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 750 | assert(mod_len); |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 751 | *model = NULL; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 752 | *mod_len = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 753 | } |
| 754 | if (name) { |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 755 | assert(nam_len); |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 756 | *name = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 757 | *nam_len = 0; |
| 758 | } |
| 759 | if (value) { |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 760 | assert(val_len); |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 761 | *value = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 762 | *val_len = 0; |
| 763 | } |
| 764 | if (has_predicate) { |
| 765 | *has_predicate = 0; |
| 766 | } |
| 767 | |
| 768 | if (id[0] != '[') { |
| 769 | return -parsed; |
| 770 | } |
| 771 | |
| 772 | ++parsed; |
| 773 | ++id; |
| 774 | |
| 775 | while (isspace(id[0])) { |
| 776 | ++parsed; |
| 777 | ++id; |
| 778 | } |
| 779 | |
| 780 | /* pos */ |
| 781 | if (isdigit(id[0])) { |
| 782 | if (name) { |
| 783 | *name = id; |
| 784 | } |
| 785 | |
| 786 | if (id[0] == '0') { |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 787 | return -parsed; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | while (isdigit(id[0])) { |
| 791 | ++parsed; |
| 792 | ++id; |
| 793 | } |
| 794 | |
| 795 | if (nam_len) { |
| 796 | *nam_len = id-(*name); |
| 797 | } |
| 798 | |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 799 | /* "." or node-identifier */ |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 800 | } else { |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 801 | if (id[0] == '.') { |
| 802 | if (name) { |
| 803 | *name = id; |
| 804 | } |
| 805 | if (nam_len) { |
| 806 | *nam_len = 1; |
| 807 | } |
| 808 | |
| 809 | ++parsed; |
| 810 | ++id; |
| 811 | |
| 812 | } else { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 813 | 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] | 814 | return -parsed + ret; |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 815 | } |
| 816 | |
| 817 | parsed += ret; |
| 818 | id += ret; |
| 819 | } |
| 820 | |
| 821 | while (isspace(id[0])) { |
| 822 | ++parsed; |
| 823 | ++id; |
| 824 | } |
| 825 | |
| 826 | if (id[0] != '=') { |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 827 | return -parsed; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 828 | } |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 829 | |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 830 | ++parsed; |
| 831 | ++id; |
| 832 | |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 833 | while (isspace(id[0])) { |
| 834 | ++parsed; |
| 835 | ++id; |
| 836 | } |
| 837 | |
| 838 | /* ((DQUOTE string DQUOTE) / (SQUOTE string SQUOTE)) */ |
| 839 | if ((id[0] == '\"') || (id[0] == '\'')) { |
| 840 | quote = id[0]; |
| 841 | |
| 842 | ++parsed; |
| 843 | ++id; |
| 844 | |
| 845 | if ((ptr = strchr(id, quote)) == NULL) { |
| 846 | return -parsed; |
| 847 | } |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 848 | ret = ptr - id; |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 849 | |
| 850 | if (value) { |
| 851 | *value = id; |
| 852 | } |
| 853 | if (val_len) { |
| 854 | *val_len = ret; |
| 855 | } |
| 856 | |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 857 | parsed += ret + 1; |
| 858 | id += ret + 1; |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 859 | } else { |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 860 | return -parsed; |
| 861 | } |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | while (isspace(id[0])) { |
| 865 | ++parsed; |
| 866 | ++id; |
| 867 | } |
| 868 | |
| 869 | if (id[0] != ']') { |
| 870 | return -parsed; |
| 871 | } |
| 872 | |
| 873 | ++parsed; |
| 874 | ++id; |
| 875 | |
| 876 | if ((id[0] == '[') && has_predicate) { |
| 877 | *has_predicate = 1; |
| 878 | } |
| 879 | |
| 880 | return parsed; |
| 881 | } |
| 882 | |
| 883 | /** |
| 884 | * @brief Parse schema-nodeid. |
| 885 | * |
| 886 | * schema-nodeid = absolute-schema-nodeid / |
| 887 | * descendant-schema-nodeid |
| 888 | * absolute-schema-nodeid = 1*("/" node-identifier) |
Michal Vasko | 4893535 | 2016-03-29 11:52:36 +0200 | [diff] [blame] | 889 | * descendant-schema-nodeid = ["." "/"] |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 890 | * node-identifier |
| 891 | * absolute-schema-nodeid |
| 892 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 893 | * @param[in] id Identifier to use. |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 894 | * @param[out] mod_name Points to the module name, NULL if there is not any. |
| 895 | * @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] | 896 | * @param[out] name Points to the node name. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 897 | * @param[out] nam_len Length of the node name. |
| 898 | * @param[out] is_relative Flag to mark whether the nodeid is absolute or descendant. Must be -1 |
| 899 | * on the first call, must not be changed between consecutive calls. |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 900 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. It cannot be |
| 901 | * based on the grammar, in those cases use NULL. |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 902 | * @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] | 903 | * |
| 904 | * @return Number of characters successfully parsed, |
| 905 | * positive on success, negative on failure. |
| 906 | */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 907 | int |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 908 | 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] | 909 | int *is_relative, int *has_predicate, int *all_desc, int extended) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 910 | { |
| 911 | int parsed = 0, ret; |
| 912 | |
| 913 | assert(id); |
| 914 | assert(is_relative); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 915 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 916 | if (has_predicate) { |
| 917 | *has_predicate = 0; |
| 918 | } |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 919 | |
| 920 | if (id[0] != '/') { |
| 921 | if (*is_relative != -1) { |
| 922 | return -parsed; |
| 923 | } else { |
| 924 | *is_relative = 1; |
| 925 | } |
Michal Vasko | 4893535 | 2016-03-29 11:52:36 +0200 | [diff] [blame] | 926 | if (!strncmp(id, "./", 2)) { |
| 927 | parsed += 2; |
| 928 | id += 2; |
| 929 | } |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 930 | } else { |
| 931 | if (*is_relative == -1) { |
| 932 | *is_relative = 0; |
| 933 | } |
| 934 | ++parsed; |
| 935 | ++id; |
| 936 | } |
| 937 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 938 | if ((ret = parse_node_identifier(id, mod_name, mod_name_len, name, nam_len, all_desc, extended)) < 1) { |
| 939 | return -parsed + ret; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 940 | } |
| 941 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 942 | parsed += ret; |
| 943 | id += ret; |
| 944 | |
| 945 | if ((id[0] == '[') && has_predicate) { |
| 946 | *has_predicate = 1; |
| 947 | } |
| 948 | |
| 949 | return parsed; |
| 950 | } |
| 951 | |
| 952 | /** |
| 953 | * @brief Parse schema predicate (special format internally used). |
| 954 | * |
| 955 | * predicate = "[" *WSP predicate-expr *WSP "]" |
Michal Vasko | f359b02 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 956 | * predicate-expr = "." / [prefix:]identifier / positive-integer / key-with-value |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 957 | * key-with-value = identifier *WSP "=" *WSP |
| 958 | * ((DQUOTE string DQUOTE) / |
| 959 | * (SQUOTE string SQUOTE)) |
| 960 | * |
| 961 | * @param[in] id Identifier to use. |
Michal Vasko | f359b02 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 962 | * @param[out] mod_name Points to the list key module name. |
| 963 | * @param[out] mod_name_len Length of \p mod_name. |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 964 | * @param[out] name Points to the list key name. |
| 965 | * @param[out] nam_len Length of \p name. |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 966 | * @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] | 967 | * @param[out] val_len Length of \p value. |
| 968 | * @param[out] has_predicate Flag to mark whether there is another predicate specified. |
| 969 | */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 970 | int |
Michal Vasko | f359b02 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 971 | parse_schema_json_predicate(const char *id, const char **mod_name, int *mod_name_len, const char **name, int *nam_len, |
| 972 | const char **value, int *val_len, int *has_predicate) |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 973 | { |
| 974 | const char *ptr; |
| 975 | int parsed = 0, ret; |
| 976 | char quote; |
| 977 | |
| 978 | assert(id); |
Michal Vasko | f359b02 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 979 | if (mod_name) { |
| 980 | *mod_name = NULL; |
| 981 | } |
| 982 | if (mod_name_len) { |
| 983 | *mod_name_len = 0; |
| 984 | } |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 985 | if (name) { |
| 986 | *name = NULL; |
| 987 | } |
| 988 | if (nam_len) { |
| 989 | *nam_len = 0; |
| 990 | } |
| 991 | if (value) { |
| 992 | *value = NULL; |
| 993 | } |
| 994 | if (val_len) { |
| 995 | *val_len = 0; |
| 996 | } |
| 997 | if (has_predicate) { |
| 998 | *has_predicate = 0; |
| 999 | } |
| 1000 | |
| 1001 | if (id[0] != '[') { |
| 1002 | return -parsed; |
| 1003 | } |
| 1004 | |
| 1005 | ++parsed; |
| 1006 | ++id; |
| 1007 | |
| 1008 | while (isspace(id[0])) { |
| 1009 | ++parsed; |
| 1010 | ++id; |
| 1011 | } |
| 1012 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1013 | /* identifier */ |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 1014 | if (id[0] == '.') { |
| 1015 | ret = 1; |
Michal Vasko | f359b02 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 1016 | |
| 1017 | if (name) { |
| 1018 | *name = id; |
| 1019 | } |
| 1020 | if (nam_len) { |
| 1021 | *nam_len = ret; |
| 1022 | } |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 1023 | } else if (isdigit(id[0])) { |
| 1024 | if (id[0] == '0') { |
| 1025 | return -parsed; |
| 1026 | } |
| 1027 | ret = 1; |
| 1028 | while (isdigit(id[ret])) { |
| 1029 | ++ret; |
| 1030 | } |
Michal Vasko | f359b02 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 1031 | |
| 1032 | if (name) { |
| 1033 | *name = id; |
| 1034 | } |
| 1035 | if (nam_len) { |
| 1036 | *nam_len = ret; |
| 1037 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1038 | } 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] | 1039 | return -parsed + ret; |
| 1040 | } |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1041 | |
| 1042 | parsed += ret; |
| 1043 | id += ret; |
| 1044 | |
| 1045 | while (isspace(id[0])) { |
| 1046 | ++parsed; |
| 1047 | ++id; |
| 1048 | } |
| 1049 | |
| 1050 | /* there is value as well */ |
| 1051 | if (id[0] == '=') { |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 1052 | if (name && isdigit(**name)) { |
| 1053 | return -parsed; |
| 1054 | } |
| 1055 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1056 | ++parsed; |
| 1057 | ++id; |
| 1058 | |
| 1059 | while (isspace(id[0])) { |
| 1060 | ++parsed; |
| 1061 | ++id; |
| 1062 | } |
| 1063 | |
| 1064 | /* ((DQUOTE string DQUOTE) / (SQUOTE string SQUOTE)) */ |
| 1065 | if ((id[0] == '\"') || (id[0] == '\'')) { |
| 1066 | quote = id[0]; |
| 1067 | |
| 1068 | ++parsed; |
| 1069 | ++id; |
| 1070 | |
| 1071 | if ((ptr = strchr(id, quote)) == NULL) { |
| 1072 | return -parsed; |
| 1073 | } |
| 1074 | ret = ptr - id; |
| 1075 | |
| 1076 | if (value) { |
| 1077 | *value = id; |
| 1078 | } |
| 1079 | if (val_len) { |
| 1080 | *val_len = ret; |
| 1081 | } |
| 1082 | |
| 1083 | parsed += ret + 1; |
| 1084 | id += ret + 1; |
| 1085 | } else { |
| 1086 | return -parsed; |
| 1087 | } |
| 1088 | |
| 1089 | while (isspace(id[0])) { |
| 1090 | ++parsed; |
| 1091 | ++id; |
| 1092 | } |
| 1093 | } |
| 1094 | |
| 1095 | if (id[0] != ']') { |
| 1096 | return -parsed; |
| 1097 | } |
| 1098 | |
| 1099 | ++parsed; |
| 1100 | ++id; |
| 1101 | |
| 1102 | if ((id[0] == '[') && has_predicate) { |
| 1103 | *has_predicate = 1; |
| 1104 | } |
| 1105 | |
| 1106 | return parsed; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 1107 | } |
| 1108 | |
| 1109 | /** |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1110 | * @brief Resolve (find) a feature definition. Logs directly. |
| 1111 | * |
| 1112 | * @param[in] feat_name Feature name to resolve. |
| 1113 | * @param[in] len Length of \p feat_name. |
| 1114 | * @param[in] node Node with the if-feature expression. |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1115 | * @param[out] feature Pointer to be set to point to the feature definition, if feature not found |
| 1116 | * (return code 1), the pointer is untouched. |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1117 | * |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1118 | * @return 0 on success, 1 on forward reference, -1 on error. |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1119 | */ |
| 1120 | static int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1121 | 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] | 1122 | { |
| 1123 | char *str; |
| 1124 | const char *mod_name, *name; |
| 1125 | int mod_name_len, nam_len, i, j; |
| 1126 | const struct lys_module *module; |
| 1127 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1128 | assert(feature); |
| 1129 | |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1130 | /* check prefix */ |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1131 | if ((i = parse_node_identifier(feat_name, &mod_name, &mod_name_len, &name, &nam_len, NULL, 0)) < 1) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1132 | LOGVAL(LYE_INCHAR, LY_VLOG_NONE, NULL, feat_name[-i], &feat_name[-i]); |
| 1133 | return -1; |
| 1134 | } |
| 1135 | |
| 1136 | module = lys_get_import_module(lys_node_module(node), NULL, 0, mod_name, mod_name_len); |
| 1137 | if (!module) { |
| 1138 | /* identity refers unknown data model */ |
| 1139 | LOGVAL(LYE_INMOD_LEN, LY_VLOG_NONE, NULL, mod_name_len, mod_name); |
| 1140 | return -1; |
| 1141 | } |
| 1142 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1143 | if (module != node->module && module == lys_node_module(node)) { |
| 1144 | /* first, try to search directly in submodule where the feature was mentioned */ |
| 1145 | for (j = 0; j < node->module->features_size; j++) { |
| 1146 | if (!strncmp(name, node->module->features[j].name, nam_len) && !node->module->features[j].name[nam_len]) { |
| 1147 | /* check status */ |
| 1148 | 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] | 1149 | node->module->features[j].module, node->module->features[j].name, NULL)) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1150 | return -1; |
| 1151 | } |
| 1152 | *feature = &node->module->features[j]; |
| 1153 | return 0; |
| 1154 | } |
| 1155 | } |
| 1156 | } |
| 1157 | |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1158 | /* search in the identified module ... */ |
| 1159 | for (j = 0; j < module->features_size; j++) { |
Michal Vasko | 3def867 | 2016-07-01 11:43:09 +0200 | [diff] [blame] | 1160 | 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] | 1161 | /* check status */ |
| 1162 | 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] | 1163 | module->features[j].module, module->features[j].name, NULL)) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1164 | return -1; |
| 1165 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1166 | *feature = &module->features[j]; |
| 1167 | return 0; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1168 | } |
| 1169 | } |
| 1170 | /* ... and all its submodules */ |
Radek Krejci | d4c1d0f | 2017-01-19 16:11:38 +0100 | [diff] [blame] | 1171 | for (i = 0; i < module->inc_size && module->inc[i].submodule; i++) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1172 | for (j = 0; j < module->inc[i].submodule->features_size; j++) { |
Michal Vasko | 3def867 | 2016-07-01 11:43:09 +0200 | [diff] [blame] | 1173 | if (!strncmp(name, module->inc[i].submodule->features[j].name, nam_len) |
| 1174 | && !module->inc[i].submodule->features[j].name[nam_len]) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1175 | /* check status */ |
| 1176 | if (lyp_check_status(node->flags, lys_node_module(node), node->name, |
| 1177 | module->inc[i].submodule->features[j].flags, |
| 1178 | module->inc[i].submodule->features[j].module, |
Pavol Vican | fdab9f9 | 2016-09-07 15:23:27 +0200 | [diff] [blame] | 1179 | module->inc[i].submodule->features[j].name, NULL)) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1180 | return -1; |
| 1181 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1182 | *feature = &module->inc[i].submodule->features[j]; |
| 1183 | return 0; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1184 | } |
| 1185 | } |
| 1186 | } |
| 1187 | |
| 1188 | /* not found */ |
| 1189 | str = strndup(feat_name, len); |
| 1190 | LOGVAL(LYE_INRESOLV, LY_VLOG_NONE, NULL, "feature", str); |
| 1191 | free(str); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1192 | return 1; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1193 | } |
| 1194 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1195 | /* |
| 1196 | * @return |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1197 | * - 1 if enabled |
| 1198 | * - 0 if disabled |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1199 | */ |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1200 | static int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1201 | resolve_feature_value(const struct lys_feature *feat) |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1202 | { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1203 | int i; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1204 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1205 | for (i = 0; i < feat->iffeature_size; i++) { |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1206 | if (!resolve_iffeature(&feat->iffeature[i])) { |
Radek Krejci | af56633 | 2017-02-07 15:56:59 +0100 | [diff] [blame] | 1207 | return 0; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1208 | } |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1209 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1210 | |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1211 | return feat->flags & LYS_FENABLED ? 1 : 0; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1212 | } |
| 1213 | |
| 1214 | static int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1215 | 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] | 1216 | { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1217 | uint8_t op; |
Radek Krejci | af56633 | 2017-02-07 15:56:59 +0100 | [diff] [blame] | 1218 | int a, b; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1219 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1220 | op = iff_getop(expr->expr, *index_e); |
| 1221 | (*index_e)++; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1222 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1223 | switch (op) { |
| 1224 | case LYS_IFF_F: |
| 1225 | /* resolve feature */ |
| 1226 | return resolve_feature_value(expr->features[(*index_f)++]); |
| 1227 | case LYS_IFF_NOT: |
Radek Krejci | af56633 | 2017-02-07 15:56:59 +0100 | [diff] [blame] | 1228 | /* invert result */ |
| 1229 | return resolve_iffeature_recursive(expr, index_e, index_f) ? 0 : 1; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1230 | case LYS_IFF_AND: |
| 1231 | case LYS_IFF_OR: |
| 1232 | a = resolve_iffeature_recursive(expr, index_e, index_f); |
| 1233 | b = resolve_iffeature_recursive(expr, index_e, index_f); |
Radek Krejci | af56633 | 2017-02-07 15:56:59 +0100 | [diff] [blame] | 1234 | if (op == LYS_IFF_AND) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1235 | return a && b; |
| 1236 | } else { /* LYS_IFF_OR */ |
| 1237 | return a || b; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1238 | } |
| 1239 | } |
| 1240 | |
Radek Krejci | af56633 | 2017-02-07 15:56:59 +0100 | [diff] [blame] | 1241 | return 0; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1242 | } |
| 1243 | |
| 1244 | int |
| 1245 | resolve_iffeature(struct lys_iffeature *expr) |
| 1246 | { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1247 | int index_e = 0, index_f = 0; |
| 1248 | |
| 1249 | if (expr->expr) { |
Radek Krejci | af56633 | 2017-02-07 15:56:59 +0100 | [diff] [blame] | 1250 | return resolve_iffeature_recursive(expr, &index_e, &index_f); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1251 | } |
Radek Krejci | af56633 | 2017-02-07 15:56:59 +0100 | [diff] [blame] | 1252 | return 0; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1253 | } |
| 1254 | |
| 1255 | struct iff_stack { |
| 1256 | int size; |
| 1257 | int index; /* first empty item */ |
| 1258 | uint8_t *stack; |
| 1259 | }; |
| 1260 | |
| 1261 | static int |
| 1262 | iff_stack_push(struct iff_stack *stack, uint8_t value) |
| 1263 | { |
| 1264 | if (stack->index == stack->size) { |
| 1265 | stack->size += 4; |
| 1266 | stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 1267 | LY_CHECK_ERR_RETURN(!stack->stack, LOGMEM; stack->size = 0, EXIT_FAILURE); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1268 | } |
| 1269 | |
| 1270 | stack->stack[stack->index++] = value; |
| 1271 | return EXIT_SUCCESS; |
| 1272 | } |
| 1273 | |
| 1274 | static uint8_t |
| 1275 | iff_stack_pop(struct iff_stack *stack) |
| 1276 | { |
| 1277 | stack->index--; |
| 1278 | return stack->stack[stack->index]; |
| 1279 | } |
| 1280 | |
| 1281 | static void |
| 1282 | iff_stack_clean(struct iff_stack *stack) |
| 1283 | { |
| 1284 | stack->size = 0; |
| 1285 | free(stack->stack); |
| 1286 | } |
| 1287 | |
| 1288 | static void |
| 1289 | iff_setop(uint8_t *list, uint8_t op, int pos) |
| 1290 | { |
| 1291 | uint8_t *item; |
| 1292 | uint8_t mask = 3; |
| 1293 | |
| 1294 | assert(pos >= 0); |
| 1295 | assert(op <= 3); /* max 2 bits */ |
| 1296 | |
| 1297 | item = &list[pos / 4]; |
| 1298 | mask = mask << 2 * (pos % 4); |
| 1299 | *item = (*item) & ~mask; |
| 1300 | *item = (*item) | (op << 2 * (pos % 4)); |
| 1301 | } |
| 1302 | |
| 1303 | uint8_t |
| 1304 | iff_getop(uint8_t *list, int pos) |
| 1305 | { |
| 1306 | uint8_t *item; |
| 1307 | uint8_t mask = 3, result; |
| 1308 | |
| 1309 | assert(pos >= 0); |
| 1310 | |
| 1311 | item = &list[pos / 4]; |
| 1312 | result = (*item) & (mask << 2 * (pos % 4)); |
| 1313 | return result >> 2 * (pos % 4); |
| 1314 | } |
| 1315 | |
| 1316 | #define LYS_IFF_LP 0x04 /* ( */ |
| 1317 | #define LYS_IFF_RP 0x08 /* ) */ |
| 1318 | |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1319 | /* internal structure for passing data for UNRES_IFFEAT */ |
| 1320 | struct unres_iffeat_data { |
| 1321 | struct lys_node *node; |
| 1322 | const char *fname; |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 1323 | int infeature; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1324 | }; |
| 1325 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1326 | void |
| 1327 | resolve_iffeature_getsizes(struct lys_iffeature *iffeat, unsigned int *expr_size, unsigned int *feat_size) |
| 1328 | { |
| 1329 | unsigned int e = 0, f = 0, r = 0; |
| 1330 | uint8_t op; |
| 1331 | |
| 1332 | assert(iffeat); |
| 1333 | |
| 1334 | if (!iffeat->expr) { |
| 1335 | goto result; |
| 1336 | } |
| 1337 | |
| 1338 | do { |
| 1339 | op = iff_getop(iffeat->expr, e++); |
| 1340 | switch (op) { |
| 1341 | case LYS_IFF_NOT: |
| 1342 | if (!r) { |
| 1343 | r += 1; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1344 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1345 | break; |
| 1346 | case LYS_IFF_AND: |
| 1347 | case LYS_IFF_OR: |
| 1348 | if (!r) { |
| 1349 | r += 2; |
| 1350 | } else { |
| 1351 | r += 1; |
| 1352 | } |
| 1353 | break; |
| 1354 | case LYS_IFF_F: |
| 1355 | f++; |
| 1356 | if (r) { |
| 1357 | r--; |
| 1358 | } |
| 1359 | break; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1360 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1361 | } while(r); |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1362 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1363 | result: |
| 1364 | if (expr_size) { |
| 1365 | *expr_size = e; |
| 1366 | } |
| 1367 | if (feat_size) { |
| 1368 | *feat_size = f; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1369 | } |
| 1370 | } |
| 1371 | |
| 1372 | int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1373 | 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] | 1374 | int infeature, struct unres_schema *unres) |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1375 | { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1376 | const char *c = value; |
| 1377 | int r, rc = EXIT_FAILURE; |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1378 | int i, j, last_not, checkversion = 0; |
| 1379 | unsigned int f_size = 0, expr_size = 0, f_exp = 1; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1380 | uint8_t op; |
| 1381 | struct iff_stack stack = {0, 0, NULL}; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1382 | struct unres_iffeat_data *iff_data; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1383 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1384 | assert(c); |
| 1385 | |
| 1386 | if (isspace(c[0])) { |
| 1387 | LOGVAL(LYE_INCHAR, LY_VLOG_NONE, NULL, c[0], c); |
| 1388 | return EXIT_FAILURE; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1389 | } |
| 1390 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1391 | /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */ |
| 1392 | for (i = j = last_not = 0; c[i]; i++) { |
| 1393 | if (c[i] == '(') { |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1394 | checkversion = 1; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1395 | j++; |
| 1396 | continue; |
| 1397 | } else if (c[i] == ')') { |
| 1398 | j--; |
| 1399 | continue; |
| 1400 | } else if (isspace(c[i])) { |
| 1401 | continue; |
| 1402 | } |
| 1403 | |
| 1404 | if (!strncmp(&c[i], "not", r = 3) || !strncmp(&c[i], "and", r = 3) || !strncmp(&c[i], "or", r = 2)) { |
| 1405 | if (c[i + r] == '\0') { |
Radek Krejci | a98da3f | 2016-07-27 14:05:22 +0200 | [diff] [blame] | 1406 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1407 | return EXIT_FAILURE; |
| 1408 | } else if (!isspace(c[i + r])) { |
| 1409 | /* feature name starting with the not/and/or */ |
| 1410 | last_not = 0; |
| 1411 | f_size++; |
| 1412 | } else if (c[i] == 'n') { /* not operation */ |
| 1413 | if (last_not) { |
| 1414 | /* double not */ |
| 1415 | expr_size = expr_size - 2; |
| 1416 | last_not = 0; |
| 1417 | } else { |
| 1418 | last_not = 1; |
| 1419 | } |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1420 | } else { /* and, or */ |
| 1421 | f_exp++; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1422 | /* not a not operation */ |
| 1423 | last_not = 0; |
| 1424 | } |
| 1425 | i += r; |
| 1426 | } else { |
| 1427 | f_size++; |
| 1428 | last_not = 0; |
| 1429 | } |
| 1430 | expr_size++; |
| 1431 | |
| 1432 | while (!isspace(c[i])) { |
| 1433 | if (!c[i] || c[i] == ')') { |
| 1434 | i--; |
| 1435 | break; |
| 1436 | } |
| 1437 | i++; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1438 | } |
| 1439 | } |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1440 | if (j || f_exp != f_size) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1441 | /* not matching count of ( and ) */ |
| 1442 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
| 1443 | return EXIT_FAILURE; |
| 1444 | } |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1445 | |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1446 | if (checkversion || expr_size > 1) { |
| 1447 | /* check that we have 1.1 module */ |
| 1448 | if (node->module->version != 2) { |
| 1449 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
| 1450 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "YANG 1.1 if-feature expression found in 1.0 module."); |
| 1451 | return EXIT_FAILURE; |
| 1452 | } |
| 1453 | } |
| 1454 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1455 | /* allocate the memory */ |
| 1456 | 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] | 1457 | iffeat_expr->features = calloc(f_size, sizeof *iffeat_expr->features); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1458 | stack.stack = malloc(expr_size * sizeof *stack.stack); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 1459 | LY_CHECK_ERR_GOTO(!stack.stack || !iffeat_expr->expr || !iffeat_expr->features, LOGMEM, error); |
| 1460 | stack.size = expr_size; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1461 | f_size--; expr_size--; /* used as indexes from now */ |
| 1462 | |
| 1463 | for (i--; i >= 0; i--) { |
| 1464 | if (c[i] == ')') { |
| 1465 | /* push it on stack */ |
| 1466 | iff_stack_push(&stack, LYS_IFF_RP); |
| 1467 | continue; |
| 1468 | } else if (c[i] == '(') { |
| 1469 | /* pop from the stack into result all operators until ) */ |
| 1470 | while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) { |
| 1471 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1472 | } |
| 1473 | continue; |
| 1474 | } else if (isspace(c[i])) { |
| 1475 | continue; |
| 1476 | } |
| 1477 | |
| 1478 | /* end operator or operand -> find beginning and get what is it */ |
| 1479 | j = i + 1; |
| 1480 | while (i >= 0 && !isspace(c[i]) && c[i] != '(') { |
| 1481 | i--; |
| 1482 | } |
| 1483 | i++; /* get back by one step */ |
| 1484 | |
| 1485 | if (!strncmp(&c[i], "not ", 4)) { |
| 1486 | if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) { |
| 1487 | /* double not */ |
| 1488 | iff_stack_pop(&stack); |
| 1489 | } else { |
| 1490 | /* not has the highest priority, so do not pop from the stack |
| 1491 | * as in case of AND and OR */ |
| 1492 | iff_stack_push(&stack, LYS_IFF_NOT); |
| 1493 | } |
| 1494 | } else if (!strncmp(&c[i], "and ", 4)) { |
| 1495 | /* as for OR - pop from the stack all operators with the same or higher |
| 1496 | * priority and store them to the result, then push the AND to the stack */ |
| 1497 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) { |
| 1498 | op = iff_stack_pop(&stack); |
| 1499 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1500 | } |
| 1501 | iff_stack_push(&stack, LYS_IFF_AND); |
| 1502 | } else if (!strncmp(&c[i], "or ", 3)) { |
| 1503 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) { |
| 1504 | op = iff_stack_pop(&stack); |
| 1505 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1506 | } |
| 1507 | iff_stack_push(&stack, LYS_IFF_OR); |
| 1508 | } else { |
| 1509 | /* feature name, length is j - i */ |
| 1510 | |
| 1511 | /* add it to the result */ |
| 1512 | iff_setop(iffeat_expr->expr, LYS_IFF_F, expr_size--); |
| 1513 | |
| 1514 | /* now get the link to the feature definition. Since it can be |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1515 | * forward referenced, we have to keep the feature name in auxiliary |
| 1516 | * structure passed into unres */ |
| 1517 | iff_data = malloc(sizeof *iff_data); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 1518 | LY_CHECK_ERR_GOTO(!iff_data, LOGMEM, error); |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1519 | iff_data->node = node; |
| 1520 | iff_data->fname = lydict_insert(node->module->ctx, &c[i], j - i); |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 1521 | iff_data->infeature = infeature; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1522 | r = unres_schema_add_node(node->module, unres, &iffeat_expr->features[f_size], UNRES_IFFEAT, |
| 1523 | (struct lys_node *)iff_data); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1524 | f_size--; |
| 1525 | |
| 1526 | if (r == -1) { |
Pavol Vican | 4d08451 | 2016-09-29 16:38:12 +0200 | [diff] [blame] | 1527 | free(iff_data); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1528 | goto error; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1529 | } |
| 1530 | } |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1531 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1532 | while (stack.index) { |
| 1533 | op = iff_stack_pop(&stack); |
| 1534 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1535 | } |
| 1536 | |
| 1537 | if (++expr_size || ++f_size) { |
| 1538 | /* not all expected operators and operands found */ |
| 1539 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
| 1540 | rc = EXIT_FAILURE; |
| 1541 | } else { |
| 1542 | rc = EXIT_SUCCESS; |
| 1543 | } |
| 1544 | |
| 1545 | error: |
| 1546 | /* cleanup */ |
| 1547 | iff_stack_clean(&stack); |
| 1548 | |
| 1549 | return rc; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1550 | } |
| 1551 | |
| 1552 | /** |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1553 | * @brief Resolve (find) a data node based on a schema-nodeid. |
| 1554 | * |
| 1555 | * Used for resolving unique statements - so id is expected to be relative and local (without reference to a different |
| 1556 | * module). |
| 1557 | * |
| 1558 | */ |
| 1559 | struct lyd_node * |
| 1560 | resolve_data_descendant_schema_nodeid(const char *nodeid, struct lyd_node *start) |
| 1561 | { |
| 1562 | char *str, *token, *p; |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1563 | struct lyd_node *result = NULL, *iter; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1564 | const struct lys_node *schema = NULL; |
| 1565 | |
| 1566 | assert(nodeid && start); |
| 1567 | |
| 1568 | if (nodeid[0] == '/') { |
| 1569 | return NULL; |
| 1570 | } |
| 1571 | |
| 1572 | str = p = strdup(nodeid); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 1573 | LY_CHECK_ERR_RETURN(!str, LOGMEM, NULL); |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1574 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1575 | while (p) { |
| 1576 | token = p; |
| 1577 | p = strchr(p, '/'); |
| 1578 | if (p) { |
| 1579 | *p = '\0'; |
| 1580 | p++; |
| 1581 | } |
| 1582 | |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1583 | if (p) { |
| 1584 | /* inner node */ |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1585 | if (resolve_descendant_schema_nodeid(token, schema ? schema->child : start->schema, |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 1586 | LYS_CONTAINER | LYS_CHOICE | LYS_CASE | LYS_LEAF, 0, &schema) |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1587 | || !schema) { |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1588 | result = NULL; |
| 1589 | break; |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1590 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1591 | |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1592 | if (schema->nodetype & (LYS_CHOICE | LYS_CASE)) { |
| 1593 | continue; |
| 1594 | } |
| 1595 | } else { |
| 1596 | /* final node */ |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 1597 | 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] | 1598 | || !schema) { |
| 1599 | result = NULL; |
| 1600 | break; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1601 | } |
| 1602 | } |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1603 | LY_TREE_FOR(result ? result->child : start, iter) { |
| 1604 | if (iter->schema == schema) { |
| 1605 | /* move in data tree according to returned schema */ |
| 1606 | result = iter; |
| 1607 | break; |
| 1608 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1609 | } |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1610 | if (!iter) { |
| 1611 | /* instance not found */ |
| 1612 | result = NULL; |
| 1613 | break; |
| 1614 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1615 | } |
| 1616 | free(str); |
| 1617 | |
| 1618 | return result; |
| 1619 | } |
| 1620 | |
Radek Krejci | 1a9c361 | 2017-04-24 14:49:43 +0200 | [diff] [blame] | 1621 | int |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1622 | schema_nodeid_siblingcheck(const struct lys_node *sibling, const struct lys_module *cur_module, const char *mod_name, |
| 1623 | int mod_name_len, const char *name, int nam_len) |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1624 | { |
| 1625 | const struct lys_module *prefix_mod; |
| 1626 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1627 | /* name check */ |
| 1628 | if ((name[0] != '*') && (name[0] != '.') && (strncmp(name, sibling->name, nam_len) || sibling->name[nam_len])) { |
| 1629 | return 1; |
| 1630 | } |
| 1631 | |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1632 | /* module check */ |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1633 | if (mod_name) { |
| 1634 | prefix_mod = lys_get_import_module(cur_module, NULL, 0, mod_name, mod_name_len); |
| 1635 | if (!prefix_mod) { |
| 1636 | return -1; |
| 1637 | } |
| 1638 | } else { |
| 1639 | prefix_mod = cur_module; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1640 | } |
| 1641 | if (prefix_mod != lys_node_module(sibling)) { |
| 1642 | return 1; |
| 1643 | } |
| 1644 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1645 | /* match */ |
| 1646 | switch (name[0]) { |
| 1647 | case '*': |
| 1648 | return 2; |
| 1649 | case '.': |
| 1650 | return 3; |
| 1651 | default: |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1652 | return 0; |
| 1653 | } |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1654 | } |
| 1655 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1656 | /* keys do not have to be ordered and do not have to be all of them */ |
| 1657 | static int |
| 1658 | resolve_extended_schema_nodeid_predicate(const char *nodeid, const struct lys_node *node, |
| 1659 | const struct lys_module *cur_module, int *nodeid_end) |
| 1660 | { |
| 1661 | int mod_len, nam_len, has_predicate, r, i; |
| 1662 | const char *model, *name; |
| 1663 | struct lys_node_list *list; |
| 1664 | |
| 1665 | if (!(node->nodetype & (LYS_LIST | LYS_LEAFLIST))) { |
| 1666 | return 1; |
| 1667 | } |
| 1668 | |
| 1669 | list = (struct lys_node_list *)node; |
| 1670 | do { |
| 1671 | r = parse_schema_json_predicate(nodeid, &model, &mod_len, &name, &nam_len, NULL, NULL, &has_predicate); |
| 1672 | if (r < 1) { |
| 1673 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, nodeid[r], &nodeid[r]); |
| 1674 | return -1; |
| 1675 | } |
| 1676 | nodeid += r; |
| 1677 | |
| 1678 | if (node->nodetype == LYS_LEAFLIST) { |
| 1679 | /* just check syntax */ |
| 1680 | if (model || !name || (name[0] != '.') || has_predicate) { |
| 1681 | return 1; |
| 1682 | } |
| 1683 | break; |
| 1684 | } else { |
| 1685 | /* check the key */ |
| 1686 | for (i = 0; i < list->keys_size; ++i) { |
| 1687 | if (strncmp(list->keys[i]->name, name, nam_len) || list->keys[i]->name[nam_len]) { |
| 1688 | continue; |
| 1689 | } |
| 1690 | if (model) { |
| 1691 | if (strncmp(lys_node_module((struct lys_node *)list->keys[i])->name, model, mod_len) |
| 1692 | || lys_node_module((struct lys_node *)list->keys[i])->name[mod_len]) { |
| 1693 | continue; |
| 1694 | } |
| 1695 | } else { |
| 1696 | if (lys_node_module((struct lys_node *)list->keys[i]) != cur_module) { |
| 1697 | continue; |
| 1698 | } |
| 1699 | } |
| 1700 | |
| 1701 | /* match */ |
| 1702 | break; |
| 1703 | } |
| 1704 | |
| 1705 | if (i == list->keys_size) { |
| 1706 | return 1; |
| 1707 | } |
| 1708 | } |
| 1709 | } while (has_predicate); |
| 1710 | |
| 1711 | if (!nodeid[0]) { |
| 1712 | *nodeid_end = 1; |
| 1713 | } |
| 1714 | return 0; |
| 1715 | } |
| 1716 | |
| 1717 | /* start - relative, module - absolute, -1 error (logged), EXIT_SUCCESS ok |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 1718 | */ |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1719 | int |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1720 | resolve_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_module *cur_module, |
| 1721 | struct ly_set **ret, int extended, int no_node_error) |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1722 | { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1723 | const char *name, *mod_name, *id; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1724 | const struct lys_node *sibling, *start_parent, *next, *elem; |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1725 | struct lys_node_augment *last_aug; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1726 | int r, nam_len, mod_name_len = 0, is_relative = -1, all_desc, has_predicate, nodeid_end = 0; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1727 | /* resolved import module from the start module, it must match the next node-name-match sibling */ |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 1728 | const struct lys_module *start_mod, *aux_mod; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1729 | char *str; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1730 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1731 | assert(nodeid && (start || cur_module) && ret); |
| 1732 | *ret = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1733 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1734 | if (!cur_module) { |
| 1735 | cur_module = lys_node_module(start); |
| 1736 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1737 | id = nodeid; |
| 1738 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1739 | r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate, |
| 1740 | (extended ? &all_desc : NULL), extended); |
| 1741 | if (r < 1) { |
| 1742 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[r], &id[r]); |
| 1743 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1744 | } |
| 1745 | id += r; |
| 1746 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1747 | if (is_relative && !start) { |
| 1748 | LOGINT; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1749 | return -1; |
| 1750 | } |
| 1751 | |
| 1752 | /* descendant-schema-nodeid */ |
| 1753 | if (is_relative) { |
Michal Vasko | 3c60cbb | 2017-07-10 11:50:03 +0200 | [diff] [blame] | 1754 | cur_module = start_mod = start->module; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1755 | start_parent = lys_parent(start); |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1756 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1757 | /* absolute-schema-nodeid */ |
| 1758 | } else { |
Michal Vasko | 3c60cbb | 2017-07-10 11:50:03 +0200 | [diff] [blame] | 1759 | start_mod = lys_get_import_module(cur_module, NULL, 0, mod_name, mod_name_len); |
Michal Vasko | e290563 | 2016-02-11 15:42:24 +0100 | [diff] [blame] | 1760 | if (!start_mod) { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1761 | str = strndup(mod_name, mod_name_len); |
| 1762 | LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str); |
| 1763 | free(str); |
Michal Vasko | e290563 | 2016-02-11 15:42:24 +0100 | [diff] [blame] | 1764 | return -1; |
| 1765 | } |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1766 | start_parent = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1767 | } |
| 1768 | |
| 1769 | while (1) { |
| 1770 | sibling = NULL; |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1771 | last_aug = NULL; |
| 1772 | |
| 1773 | if (start_parent) { |
Michal Vasko | 1731577 | 2017-07-10 15:15:39 +0200 | [diff] [blame] | 1774 | if (mod_name && (strncmp(mod_name, cur_module->name, mod_name_len) |
| 1775 | || (mod_name_len != (signed)strlen(cur_module->name)))) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1776 | /* we are getting into another module (augment) */ |
Michal Vasko | 3c60cbb | 2017-07-10 11:50:03 +0200 | [diff] [blame] | 1777 | aux_mod = lys_get_import_module(cur_module, NULL, 0, mod_name, mod_name_len); |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1778 | if (!aux_mod) { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1779 | str = strndup(mod_name, mod_name_len); |
| 1780 | LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str); |
| 1781 | free(str); |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1782 | return -1; |
| 1783 | } |
| 1784 | } else { |
Michal Vasko | 1731577 | 2017-07-10 15:15:39 +0200 | [diff] [blame] | 1785 | /* there is no mod_name, so why are we checking augments again? |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1786 | * because this module may be not implemented and it augments something in another module and |
| 1787 | * there is another augment augmenting that previous one */ |
Michal Vasko | 1731577 | 2017-07-10 15:15:39 +0200 | [diff] [blame] | 1788 | aux_mod = cur_module; |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1789 | } |
| 1790 | |
| 1791 | /* if the module is implemented, all the augments will be connected */ |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1792 | if (!aux_mod->implemented && !extended) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1793 | get_next_augment: |
| 1794 | last_aug = lys_getnext_target_aug(last_aug, aux_mod, start_parent); |
| 1795 | } |
| 1796 | } |
| 1797 | |
| 1798 | while ((sibling = lys_getnext(sibling, (last_aug ? (struct lys_node *)last_aug : start_parent), start_mod, |
Michal Vasko | 5b99790 | 2017-04-03 14:16:22 +0200 | [diff] [blame] | 1799 | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT | LYS_GETNEXT_PARENTUSES))) { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1800 | r = schema_nodeid_siblingcheck(sibling, cur_module, mod_name, mod_name_len, name, nam_len); |
| 1801 | |
| 1802 | /* resolve predicate */ |
| 1803 | if (extended && ((r == 0) || (r == 2) || (r == 3)) && has_predicate) { |
| 1804 | r = resolve_extended_schema_nodeid_predicate(id, sibling, cur_module, &nodeid_end); |
| 1805 | if (r == 1) { |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1806 | continue; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1807 | } else if (r == -1) { |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1808 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1809 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1810 | } else if (!id[0]) { |
| 1811 | nodeid_end = 1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1812 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1813 | |
| 1814 | if (r == 0) { |
| 1815 | /* one matching result */ |
| 1816 | if (nodeid_end) { |
| 1817 | *ret = ly_set_new(); |
| 1818 | LY_CHECK_ERR_RETURN(!*ret, LOGMEM, -1); |
| 1819 | ly_set_add(*ret, (void *)sibling, LY_SET_OPT_USEASLIST); |
| 1820 | } else { |
| 1821 | if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
| 1822 | return -1; |
| 1823 | } |
| 1824 | start_parent = sibling; |
| 1825 | } |
| 1826 | break; |
| 1827 | } else if (r == 1) { |
| 1828 | continue; |
| 1829 | } else if (r == 2) { |
| 1830 | /* "*" */ |
| 1831 | if (!*ret) { |
| 1832 | *ret = ly_set_new(); |
| 1833 | LY_CHECK_ERR_RETURN(!*ret, LOGMEM, -1); |
| 1834 | } |
| 1835 | ly_set_add(*ret, (void *)sibling, LY_SET_OPT_USEASLIST); |
| 1836 | if (all_desc) { |
| 1837 | LY_TREE_DFS_BEGIN(sibling, next, elem) { |
| 1838 | if (elem != sibling) { |
| 1839 | ly_set_add(*ret, (void *)elem, LY_SET_OPT_USEASLIST); |
| 1840 | } |
| 1841 | |
| 1842 | LY_TREE_DFS_END(sibling, next, elem); |
| 1843 | } |
| 1844 | } |
| 1845 | } else if (r == 3) { |
| 1846 | /* "." */ |
| 1847 | if (!*ret) { |
| 1848 | *ret = ly_set_new(); |
| 1849 | LY_CHECK_ERR_RETURN(!*ret, LOGMEM, -1); |
| 1850 | ly_set_add(*ret, (void *)start_parent, LY_SET_OPT_USEASLIST); |
| 1851 | } |
| 1852 | ly_set_add(*ret, (void *)sibling, LY_SET_OPT_USEASLIST); |
| 1853 | if (all_desc) { |
| 1854 | LY_TREE_DFS_BEGIN(sibling, next, elem) { |
| 1855 | if (elem != sibling) { |
| 1856 | ly_set_add(*ret, (void *)elem, LY_SET_OPT_USEASLIST); |
| 1857 | } |
| 1858 | |
| 1859 | LY_TREE_DFS_END(sibling, next, elem); |
| 1860 | } |
| 1861 | } |
| 1862 | } else { |
| 1863 | LOGINT; |
| 1864 | return -1; |
| 1865 | } |
| 1866 | } |
| 1867 | |
| 1868 | /* skip predicate */ |
| 1869 | if (extended && has_predicate) { |
| 1870 | while (id[0] == '[') { |
| 1871 | id = strchr(id, ']'); |
| 1872 | if (!id) { |
| 1873 | LOGINT; |
| 1874 | return -1; |
| 1875 | } |
| 1876 | ++id; |
| 1877 | } |
| 1878 | } |
| 1879 | |
| 1880 | if (nodeid_end && ((r == 0) || (r == 2) || (r == 3))) { |
| 1881 | return EXIT_SUCCESS; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1882 | } |
| 1883 | |
| 1884 | /* no match */ |
| 1885 | if (!sibling) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1886 | if (last_aug) { |
| 1887 | /* it still could be in another augment */ |
| 1888 | goto get_next_augment; |
| 1889 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1890 | if (no_node_error) { |
| 1891 | str = strndup(nodeid, (name - nodeid) + nam_len); |
| 1892 | LOGVAL(LYE_PATH_INNODE, LY_VLOG_STR, str); |
| 1893 | free(str); |
| 1894 | return -1; |
| 1895 | } |
Michal Vasko | a426fef | 2016-03-07 10:47:31 +0100 | [diff] [blame] | 1896 | *ret = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1897 | return EXIT_SUCCESS; |
| 1898 | } |
| 1899 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1900 | r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate, |
| 1901 | (extended ? &all_desc : NULL), extended); |
| 1902 | if (r < 1) { |
| 1903 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[r], &id[r]); |
| 1904 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1905 | } |
| 1906 | id += r; |
| 1907 | } |
| 1908 | |
| 1909 | /* cannot get here */ |
| 1910 | LOGINT; |
| 1911 | return -1; |
| 1912 | } |
| 1913 | |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 1914 | /* unique, refine, |
| 1915 | * >0 - unexpected char on position (ret - 1), |
| 1916 | * 0 - ok (but ret can still be NULL), |
| 1917 | * -1 - error, |
| 1918 | * -2 - violated no_innerlist */ |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1919 | int |
| 1920 | 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] | 1921 | int no_innerlist, const struct lys_node **ret) |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1922 | { |
| 1923 | const char *name, *mod_name, *id; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1924 | const struct lys_node *sibling, *start_parent; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1925 | int r, nam_len, mod_name_len, is_relative = -1; |
| 1926 | /* 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] | 1927 | const struct lys_module *module; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1928 | |
Radek Krejci | c3f1b6f | 2017-02-15 10:51:10 +0100 | [diff] [blame] | 1929 | assert(nodeid && ret); |
Radek Krejci | e207741 | 2017-01-26 16:03:39 +0100 | [diff] [blame] | 1930 | assert(!(ret_nodetype & (LYS_USES | LYS_AUGMENT | LYS_GROUPING))); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1931 | |
Radek Krejci | c3f1b6f | 2017-02-15 10:51:10 +0100 | [diff] [blame] | 1932 | if (!start) { |
| 1933 | /* leaf not found */ |
| 1934 | return 0; |
| 1935 | } |
| 1936 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1937 | id = nodeid; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1938 | module = lys_node_module(start); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1939 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1940 | 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] | 1941 | return ((id - nodeid) - r) + 1; |
| 1942 | } |
| 1943 | id += r; |
| 1944 | |
| 1945 | if (!is_relative) { |
| 1946 | return -1; |
| 1947 | } |
| 1948 | |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1949 | start_parent = lys_parent(start); |
Michal Vasko | 74a991b | 2017-03-31 09:17:22 +0200 | [diff] [blame] | 1950 | while ((start_parent->nodetype == LYS_USES) && lys_parent(start_parent)) { |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1951 | start_parent = lys_parent(start_parent); |
| 1952 | } |
| 1953 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1954 | while (1) { |
| 1955 | sibling = NULL; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1956 | while ((sibling = lys_getnext(sibling, start_parent, module, |
Michal Vasko | 74a991b | 2017-03-31 09:17:22 +0200 | [diff] [blame] | 1957 | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_PARENTUSES))) { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1958 | r = schema_nodeid_siblingcheck(sibling, module, mod_name, mod_name_len, name, nam_len); |
| 1959 | if (r == 0) { |
| 1960 | if (!id[0]) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1961 | if (!(sibling->nodetype & ret_nodetype)) { |
| 1962 | /* wrong node type, too bad */ |
| 1963 | continue; |
| 1964 | } |
| 1965 | *ret = sibling; |
| 1966 | return EXIT_SUCCESS; |
| 1967 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1968 | start_parent = sibling; |
| 1969 | break; |
| 1970 | } else if (r == 1) { |
| 1971 | continue; |
| 1972 | } else { |
| 1973 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1974 | } |
| 1975 | } |
| 1976 | |
| 1977 | /* no match */ |
| 1978 | if (!sibling) { |
Michal Vasko | a426fef | 2016-03-07 10:47:31 +0100 | [diff] [blame] | 1979 | *ret = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1980 | return EXIT_SUCCESS; |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 1981 | } else if (no_innerlist && sibling->nodetype == LYS_LIST) { |
| 1982 | *ret = NULL; |
| 1983 | return -2; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1984 | } |
| 1985 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1986 | 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] | 1987 | return ((id - nodeid) - r) + 1; |
| 1988 | } |
| 1989 | id += r; |
| 1990 | } |
| 1991 | |
| 1992 | /* cannot get here */ |
| 1993 | LOGINT; |
| 1994 | return -1; |
| 1995 | } |
| 1996 | |
| 1997 | /* choice default */ |
| 1998 | int |
| 1999 | resolve_choice_default_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_node **ret) |
| 2000 | { |
| 2001 | /* cannot actually be a path */ |
| 2002 | if (strchr(nodeid, '/')) { |
| 2003 | return -1; |
| 2004 | } |
| 2005 | |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 2006 | 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] | 2007 | } |
| 2008 | |
| 2009 | /* uses, -1 error, EXIT_SUCCESS ok (but ret can still be NULL), >0 unexpected char on ret - 1 */ |
| 2010 | static int |
| 2011 | resolve_uses_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_node_grp **ret) |
| 2012 | { |
| 2013 | const struct lys_module *module; |
| 2014 | const char *mod_prefix, *name; |
| 2015 | int i, mod_prefix_len, nam_len; |
| 2016 | |
| 2017 | /* parse the identifier, it must be parsed on one call */ |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2018 | 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] | 2019 | return -i + 1; |
| 2020 | } |
| 2021 | |
| 2022 | module = lys_get_import_module(start->module, mod_prefix, mod_prefix_len, NULL, 0); |
| 2023 | if (!module) { |
| 2024 | return -1; |
| 2025 | } |
Radek Krejci | 0a8205d | 2017-03-01 16:25:29 +0100 | [diff] [blame] | 2026 | if (module != lys_main_module(start->module)) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2027 | start = module->data; |
| 2028 | } |
| 2029 | |
| 2030 | *ret = lys_find_grouping_up(name, (struct lys_node *)start); |
| 2031 | |
| 2032 | return EXIT_SUCCESS; |
| 2033 | } |
| 2034 | |
| 2035 | int |
| 2036 | resolve_absolute_schema_nodeid(const char *nodeid, const struct lys_module *module, int ret_nodetype, |
| 2037 | const struct lys_node **ret) |
| 2038 | { |
| 2039 | const char *name, *mod_name, *id; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 2040 | const struct lys_node *sibling, *start_parent; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2041 | int r, nam_len, mod_name_len, is_relative = -1; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 2042 | const struct lys_module *abs_start_mod; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2043 | |
| 2044 | assert(nodeid && module && ret); |
| 2045 | assert(!(ret_nodetype & (LYS_USES | LYS_AUGMENT)) && ((ret_nodetype == LYS_GROUPING) || !(ret_nodetype & LYS_GROUPING))); |
| 2046 | |
| 2047 | id = nodeid; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 2048 | start_parent = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2049 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2050 | 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] | 2051 | return ((id - nodeid) - r) + 1; |
| 2052 | } |
| 2053 | id += r; |
| 2054 | |
| 2055 | if (is_relative) { |
| 2056 | return -1; |
| 2057 | } |
| 2058 | |
| 2059 | abs_start_mod = lys_get_import_module(module, NULL, 0, mod_name, mod_name_len); |
Michal Vasko | e290563 | 2016-02-11 15:42:24 +0100 | [diff] [blame] | 2060 | if (!abs_start_mod) { |
| 2061 | return -1; |
| 2062 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2063 | |
| 2064 | while (1) { |
| 2065 | sibling = NULL; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 2066 | while ((sibling = lys_getnext(sibling, start_parent, abs_start_mod, LYS_GETNEXT_WITHCHOICE |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2067 | | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT | LYS_GETNEXT_WITHGROUPING))) { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2068 | r = schema_nodeid_siblingcheck(sibling, module, mod_name, mod_name_len, name, nam_len); |
| 2069 | if (r == 0) { |
| 2070 | if (!id[0]) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2071 | if (!(sibling->nodetype & ret_nodetype)) { |
| 2072 | /* wrong node type, too bad */ |
| 2073 | continue; |
| 2074 | } |
| 2075 | *ret = sibling; |
| 2076 | return EXIT_SUCCESS; |
| 2077 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2078 | start_parent = sibling; |
| 2079 | break; |
| 2080 | } else if (r == 1) { |
| 2081 | continue; |
| 2082 | } else { |
| 2083 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2084 | } |
| 2085 | } |
| 2086 | |
| 2087 | /* no match */ |
| 2088 | if (!sibling) { |
Michal Vasko | a426fef | 2016-03-07 10:47:31 +0100 | [diff] [blame] | 2089 | *ret = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2090 | return EXIT_SUCCESS; |
| 2091 | } |
| 2092 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2093 | 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] | 2094 | return ((id - nodeid) - r) + 1; |
| 2095 | } |
| 2096 | id += r; |
| 2097 | } |
| 2098 | |
| 2099 | /* cannot get here */ |
| 2100 | LOGINT; |
| 2101 | return -1; |
| 2102 | } |
| 2103 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2104 | static int |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2105 | 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] | 2106 | { |
Michal Vasko | f359b02 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2107 | const char *mod_name, *name; |
| 2108 | int mod_name_len, nam_len, has_predicate, i; |
| 2109 | struct lys_node *key; |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2110 | |
Michal Vasko | f359b02 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2111 | 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] | 2112 | || !strncmp(name, ".", nam_len)) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2113 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-i], &predicate[-i]); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2114 | return -1; |
| 2115 | } |
| 2116 | |
| 2117 | predicate += i; |
| 2118 | *parsed += i; |
| 2119 | |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2120 | if (!isdigit(name[0])) { |
| 2121 | for (i = 0; i < list->keys_size; ++i) { |
Michal Vasko | f359b02 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2122 | key = (struct lys_node *)list->keys[i]; |
| 2123 | if (!strncmp(key->name, name, nam_len) && !key->name[nam_len]) { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2124 | break; |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2125 | } |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2126 | } |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2127 | |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2128 | if (i == list->keys_size) { |
| 2129 | LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name); |
| 2130 | return -1; |
| 2131 | } |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2132 | } |
| 2133 | |
| 2134 | /* more predicates? */ |
| 2135 | if (has_predicate) { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2136 | return resolve_json_schema_list_predicate(predicate, list, parsed); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2137 | } |
| 2138 | |
| 2139 | return 0; |
| 2140 | } |
| 2141 | |
Michal Vasko | 8d26e5c | 2016-09-08 10:03:49 +0200 | [diff] [blame] | 2142 | /* cannot return LYS_GROUPING, LYS_AUGMENT, LYS_USES, logs directly */ |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2143 | const struct lys_node * |
Michal Vasko | b374440 | 2017-08-03 14:23:58 +0200 | [diff] [blame] | 2144 | resolve_json_nodeid(const char *nodeid, struct ly_ctx *ctx, const struct lys_node *start, int output) |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2145 | { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2146 | char *module_name = ly_buf(), *buf_backup = NULL, *str; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2147 | const char *name, *mod_name, *id; |
Michal Vasko | b374440 | 2017-08-03 14:23:58 +0200 | [diff] [blame] | 2148 | const struct lys_node *sibling, *start_parent, *parent; |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 2149 | int r, nam_len, mod_name_len, is_relative = -1, has_predicate; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2150 | /* resolved import module from the start module, it must match the next node-name-match sibling */ |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2151 | const struct lys_module *prefix_mod, *module, *prev_mod; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2152 | |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2153 | assert(nodeid && (ctx || start)); |
| 2154 | if (!ctx) { |
| 2155 | ctx = start->module->ctx; |
| 2156 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2157 | |
| 2158 | id = nodeid; |
| 2159 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2160 | if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate, NULL, 0)) < 1) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2161 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2162 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2163 | } |
| 2164 | id += r; |
| 2165 | |
| 2166 | if (is_relative) { |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2167 | assert(start); |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 2168 | start_parent = start; |
| 2169 | while (start_parent && (start_parent->nodetype == LYS_USES)) { |
| 2170 | start_parent = lys_parent(start_parent); |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2171 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2172 | module = start->module; |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2173 | } else { |
| 2174 | if (!mod_name) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2175 | str = strndup(nodeid, (name + nam_len) - nodeid); |
| 2176 | LOGVAL(LYE_PATH_MISSMOD, LY_VLOG_STR, nodeid); |
| 2177 | free(str); |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2178 | return NULL; |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2179 | } else if (mod_name_len > LY_BUF_SIZE - 1) { |
| 2180 | LOGINT; |
| 2181 | return NULL; |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2182 | } |
| 2183 | |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2184 | if (ly_buf_used && module_name[0]) { |
| 2185 | buf_backup = strndup(module_name, LY_BUF_SIZE - 1); |
| 2186 | } |
| 2187 | ly_buf_used++; |
| 2188 | |
Michal Vasko | df3f1ab | 2016-04-01 15:07:22 +0200 | [diff] [blame] | 2189 | memmove(module_name, mod_name, mod_name_len); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2190 | module_name[mod_name_len] = '\0'; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2191 | module = ly_ctx_get_module(ctx, module_name, NULL); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2192 | |
| 2193 | if (buf_backup) { |
| 2194 | /* return previous internal buffer content */ |
| 2195 | strcpy(module_name, buf_backup); |
| 2196 | free(buf_backup); |
| 2197 | buf_backup = NULL; |
| 2198 | } |
| 2199 | ly_buf_used--; |
| 2200 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2201 | if (!module) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2202 | str = strndup(nodeid, (mod_name + mod_name_len) - nodeid); |
| 2203 | LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str); |
| 2204 | free(str); |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2205 | return NULL; |
| 2206 | } |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 2207 | start_parent = NULL; |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2208 | |
| 2209 | /* now it's as if there was no module name */ |
| 2210 | mod_name = NULL; |
| 2211 | mod_name_len = 0; |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2212 | } |
| 2213 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2214 | prev_mod = module; |
| 2215 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2216 | while (1) { |
| 2217 | sibling = NULL; |
Michal Vasko | b374440 | 2017-08-03 14:23:58 +0200 | [diff] [blame] | 2218 | while ((sibling = lys_getnext(sibling, start_parent, module, 0))) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2219 | /* name match */ |
Michal Vasko | 0a1aaa4 | 2016-04-19 09:48:25 +0200 | [diff] [blame] | 2220 | 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] | 2221 | /* output check */ |
| 2222 | for (parent = lys_parent(sibling); parent && !(parent->nodetype & (LYS_INPUT | LYS_OUTPUT)); parent = lys_parent(parent)); |
| 2223 | if (parent) { |
| 2224 | if (output && (parent->nodetype == LYS_INPUT)) { |
| 2225 | continue; |
| 2226 | } else if (!output && (parent->nodetype == LYS_OUTPUT)) { |
| 2227 | continue; |
| 2228 | } |
| 2229 | } |
| 2230 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2231 | /* module check */ |
| 2232 | if (mod_name) { |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2233 | if (mod_name_len > LY_BUF_SIZE - 1) { |
Michal Vasko | 8757e7c | 2016-03-15 10:41:30 +0100 | [diff] [blame] | 2234 | LOGINT; |
| 2235 | return NULL; |
| 2236 | } |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2237 | |
| 2238 | if (ly_buf_used && module_name[0]) { |
| 2239 | buf_backup = strndup(module_name, LY_BUF_SIZE - 1); |
| 2240 | } |
| 2241 | ly_buf_used++; |
| 2242 | |
Michal Vasko | df3f1ab | 2016-04-01 15:07:22 +0200 | [diff] [blame] | 2243 | memmove(module_name, mod_name, mod_name_len); |
Michal Vasko | 8757e7c | 2016-03-15 10:41:30 +0100 | [diff] [blame] | 2244 | module_name[mod_name_len] = '\0'; |
| 2245 | /* will also find an augment module */ |
| 2246 | prefix_mod = ly_ctx_get_module(ctx, module_name, NULL); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2247 | |
| 2248 | if (buf_backup) { |
| 2249 | /* return previous internal buffer content */ |
Michal Vasko | 888a129 | 2016-04-05 12:08:54 +0200 | [diff] [blame] | 2250 | strncpy(module_name, buf_backup, LY_BUF_SIZE - 1); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2251 | free(buf_backup); |
| 2252 | buf_backup = NULL; |
| 2253 | } |
| 2254 | ly_buf_used--; |
| 2255 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2256 | if (!prefix_mod) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2257 | str = strndup(nodeid, (mod_name + mod_name_len) - nodeid); |
| 2258 | LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str); |
| 2259 | free(str); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2260 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2261 | } |
| 2262 | } else { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2263 | prefix_mod = prev_mod; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2264 | } |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 2265 | if (prefix_mod != lys_node_module(sibling)) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2266 | continue; |
| 2267 | } |
| 2268 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2269 | /* do we have some predicates on it? */ |
| 2270 | if (has_predicate) { |
| 2271 | r = 0; |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 2272 | if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
Michal Vasko | f359b02 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2273 | if ((r = parse_schema_json_predicate(id, NULL, NULL, NULL, NULL, NULL, NULL, &has_predicate)) < 1) { |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 2274 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
| 2275 | return NULL; |
| 2276 | } |
| 2277 | } else if (sibling->nodetype == LYS_LIST) { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2278 | 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] | 2279 | return NULL; |
| 2280 | } |
| 2281 | } else { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2282 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2283 | return NULL; |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2284 | } |
| 2285 | id += r; |
| 2286 | } |
| 2287 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2288 | /* the result node? */ |
| 2289 | if (!id[0]) { |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2290 | return sibling; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2291 | } |
| 2292 | |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 2293 | /* move down the tree, if possible */ |
| 2294 | if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
| 2295 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
| 2296 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2297 | } |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 2298 | start_parent = sibling; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2299 | |
| 2300 | /* update prev mod */ |
| 2301 | prev_mod = (start_parent->child ? lys_node_module(start_parent->child) : module); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2302 | break; |
| 2303 | } |
| 2304 | } |
| 2305 | |
| 2306 | /* no match */ |
| 2307 | if (!sibling) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2308 | str = strndup(nodeid, (name + nam_len) - nodeid); |
| 2309 | LOGVAL(LYE_PATH_INNODE, LY_VLOG_STR, str); |
| 2310 | free(str); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2311 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2312 | } |
| 2313 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2314 | if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate, NULL, 0)) < 1) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2315 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2316 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2317 | } |
| 2318 | id += r; |
| 2319 | } |
| 2320 | |
| 2321 | /* cannot get here */ |
| 2322 | LOGINT; |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2323 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2324 | } |
| 2325 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2326 | static int |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2327 | resolve_partial_json_data_list_predicate(const char *predicate, const char *node_name, struct lyd_node *node, |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2328 | int position, int *parsed) |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2329 | { |
Michal Vasko | f359b02 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2330 | const char *mod_name, *name, *value, *key_val; |
| 2331 | int mod_name_len, nam_len, val_len, has_predicate = 1, r; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2332 | uint16_t i; |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2333 | struct lyd_node_leaf_list *key; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2334 | |
Radek Krejci | 61a86c6 | 2016-03-24 11:06:44 +0100 | [diff] [blame] | 2335 | assert(node); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2336 | assert(node->schema->nodetype == LYS_LIST); |
| 2337 | |
Michal Vasko | 53adfc7 | 2017-01-06 10:39:10 +0100 | [diff] [blame] | 2338 | /* is the predicate a number? */ |
Michal Vasko | f359b02 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2339 | if (((r = parse_schema_json_predicate(predicate, &mod_name, &mod_name_len, &name, &nam_len, &value, &val_len, &has_predicate)) < 1) |
Michal Vasko | 53adfc7 | 2017-01-06 10:39:10 +0100 | [diff] [blame] | 2340 | || !strncmp(name, ".", nam_len)) { |
| 2341 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-r], &predicate[-r]); |
| 2342 | return -1; |
| 2343 | } |
| 2344 | |
| 2345 | if (isdigit(name[0])) { |
| 2346 | if (position == atoi(name)) { |
| 2347 | /* match */ |
| 2348 | *parsed += r; |
| 2349 | return 0; |
| 2350 | } else { |
| 2351 | /* not a match */ |
| 2352 | return 1; |
| 2353 | } |
| 2354 | } |
| 2355 | |
| 2356 | if (!((struct lys_node_list *)node->schema)->keys_size) { |
| 2357 | /* no keys in schema - causes an error later */ |
| 2358 | return 0; |
| 2359 | } |
| 2360 | |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2361 | key = (struct lyd_node_leaf_list *)node->child; |
Michal Vasko | 53adfc7 | 2017-01-06 10:39:10 +0100 | [diff] [blame] | 2362 | if (!key) { |
| 2363 | /* it is not a position, so we need a key for it to be a match */ |
| 2364 | return 1; |
| 2365 | } |
| 2366 | |
| 2367 | /* go through all the keys */ |
| 2368 | i = 0; |
| 2369 | goto check_parsed_values; |
| 2370 | |
| 2371 | for (; i < ((struct lys_node_list *)node->schema)->keys_size; ++i) { |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2372 | if (!has_predicate) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2373 | LOGVAL(LYE_PATH_MISSKEY, LY_VLOG_NONE, NULL, node_name); |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2374 | return -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2375 | } |
| 2376 | |
Michal Vasko | f359b02 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2377 | if (((r = parse_schema_json_predicate(predicate, &mod_name, &mod_name_len, &name, &nam_len, &value, &val_len, &has_predicate)) < 1) |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 2378 | || !strncmp(name, ".", nam_len)) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2379 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-r], &predicate[-r]); |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2380 | return -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2381 | } |
| 2382 | |
Michal Vasko | 53adfc7 | 2017-01-06 10:39:10 +0100 | [diff] [blame] | 2383 | check_parsed_values: |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2384 | predicate += r; |
| 2385 | *parsed += r; |
| 2386 | |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2387 | if (strncmp(key->schema->name, name, nam_len) || key->schema->name[nam_len]) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2388 | LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name); |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2389 | return -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2390 | } |
| 2391 | |
Michal Vasko | f359b02 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2392 | if (mod_name) { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2393 | /* specific module, check that the found key is from that module */ |
Michal Vasko | f359b02 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2394 | if (strncmp(lyd_node_module((struct lyd_node *)key)->name, mod_name, mod_name_len) |
| 2395 | || lyd_node_module((struct lyd_node *)key)->name[mod_name_len]) { |
| 2396 | LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name); |
| 2397 | return -1; |
| 2398 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2399 | |
| 2400 | /* but if the module is the same as the parent, it should have been omitted */ |
| 2401 | if (lyd_node_module((struct lyd_node *)key) == lyd_node_module(node)) { |
| 2402 | LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name); |
| 2403 | return -1; |
| 2404 | } |
Michal Vasko | f359b02 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2405 | } else { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2406 | /* no module, so it must be the same as the list (parent) */ |
| 2407 | if (lyd_node_module((struct lyd_node *)key) != lyd_node_module(node)) { |
Michal Vasko | f359b02 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2408 | LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name); |
| 2409 | return -1; |
| 2410 | } |
| 2411 | } |
| 2412 | |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2413 | /* make value canonical */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 2414 | if ((key->value_type & LY_TYPE_IDENT) |
Michal Vasko | 6a938d6 | 2016-12-21 09:21:30 +0100 | [diff] [blame] | 2415 | && !strncmp(key->value_str, lyd_node_module(node)->name, strlen(lyd_node_module(node)->name)) |
| 2416 | && (key->value_str[strlen(lyd_node_module(node)->name)] == ':')) { |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2417 | key_val = key->value_str + strlen(lyd_node_module(node)->name) + 1; |
| 2418 | } else { |
| 2419 | key_val = key->value_str; |
| 2420 | } |
| 2421 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2422 | /* value does not match */ |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2423 | if (strncmp(key_val, value, val_len) || key_val[val_len]) { |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2424 | return 1; |
| 2425 | } |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2426 | |
| 2427 | key = (struct lyd_node_leaf_list *)key->next; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2428 | } |
| 2429 | |
| 2430 | if (has_predicate) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2431 | LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2432 | return -1; |
| 2433 | } |
| 2434 | |
| 2435 | return 0; |
| 2436 | } |
| 2437 | |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 2438 | /** |
| 2439 | * @brief get the closest parent of the node (or the node itself) identified by the nodeid (path) |
| 2440 | * |
| 2441 | * @param[in] nodeid Node data path to find |
| 2442 | * @param[in] llist_value If the \p nodeid identifies leaf-list, this is expected value of the leaf-list instance. |
| 2443 | * @param[in] options Bitmask of options flags, see @ref pathoptions. |
| 2444 | * @param[out] parsed Number of characters processed in \p id |
| 2445 | * @return The closes parent (or the node itself) from the path |
| 2446 | */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2447 | struct lyd_node * |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2448 | resolve_partial_json_data_nodeid(const char *nodeid, const char *llist_value, struct lyd_node *start, int options, |
| 2449 | int *parsed) |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2450 | { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2451 | char *module_name = ly_buf(), *buf_backup = NULL, *str; |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2452 | const char *id, *mod_name, *name, *pred_name, *data_val; |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2453 | int r, ret, mod_name_len, nam_len, is_relative = -1, list_instance_position; |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2454 | int has_predicate, last_parsed, llval_len, pred_name_len, last_has_pred; |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2455 | struct lyd_node *sibling, *last_match = NULL; |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2456 | struct lyd_node_leaf_list *llist; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2457 | const struct lys_module *prefix_mod, *prev_mod; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2458 | struct ly_ctx *ctx; |
| 2459 | |
| 2460 | assert(nodeid && start && parsed); |
| 2461 | |
| 2462 | ctx = start->schema->module->ctx; |
| 2463 | id = nodeid; |
| 2464 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2465 | if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate, NULL, 0)) < 1) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2466 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2467 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2468 | return NULL; |
| 2469 | } |
| 2470 | id += r; |
| 2471 | /* add it to parsed only after the data node was actually found */ |
| 2472 | last_parsed = r; |
| 2473 | |
| 2474 | if (is_relative) { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2475 | prev_mod = lyd_node_module(start); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2476 | start = start->child; |
| 2477 | } else { |
| 2478 | for (; start->parent; start = start->parent); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2479 | prev_mod = lyd_node_module(start); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2480 | } |
| 2481 | |
| 2482 | while (1) { |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2483 | list_instance_position = 0; |
| 2484 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2485 | LY_TREE_FOR(start, sibling) { |
Michal Vasko | 945b96b | 2016-10-18 11:49:12 +0200 | [diff] [blame] | 2486 | /* RPC/action data check, return simply invalid argument, because the data tree is invalid */ |
Michal Vasko | 2411b94 | 2016-03-23 13:50:03 +0100 | [diff] [blame] | 2487 | if (lys_parent(sibling->schema)) { |
| 2488 | if (options & LYD_PATH_OPT_OUTPUT) { |
| 2489 | if (lys_parent(sibling->schema)->nodetype == LYS_INPUT) { |
Michal Vasko | b724689 | 2016-04-19 10:37:35 +0200 | [diff] [blame] | 2490 | LOGERR(LY_EINVAL, "Provided data tree includes some RPC input nodes (%s).", sibling->schema->name); |
Michal Vasko | 2411b94 | 2016-03-23 13:50:03 +0100 | [diff] [blame] | 2491 | *parsed = -1; |
| 2492 | return NULL; |
| 2493 | } |
| 2494 | } else { |
| 2495 | if (lys_parent(sibling->schema)->nodetype == LYS_OUTPUT) { |
Michal Vasko | b724689 | 2016-04-19 10:37:35 +0200 | [diff] [blame] | 2496 | LOGERR(LY_EINVAL, "Provided data tree includes some RPC output nodes (%s).", sibling->schema->name); |
Michal Vasko | 2411b94 | 2016-03-23 13:50:03 +0100 | [diff] [blame] | 2497 | *parsed = -1; |
| 2498 | return NULL; |
| 2499 | } |
| 2500 | } |
| 2501 | } |
| 2502 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2503 | /* name match */ |
| 2504 | if (!strncmp(name, sibling->schema->name, nam_len) && !sibling->schema->name[nam_len]) { |
| 2505 | |
| 2506 | /* module check */ |
| 2507 | if (mod_name) { |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2508 | if (mod_name_len > LY_BUF_SIZE - 1) { |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2509 | LOGINT; |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2510 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2511 | return NULL; |
| 2512 | } |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2513 | |
| 2514 | if (ly_buf_used && module_name[0]) { |
| 2515 | buf_backup = strndup(module_name, LY_BUF_SIZE - 1); |
| 2516 | } |
| 2517 | ly_buf_used++; |
| 2518 | |
Michal Vasko | df3f1ab | 2016-04-01 15:07:22 +0200 | [diff] [blame] | 2519 | memmove(module_name, mod_name, mod_name_len); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2520 | module_name[mod_name_len] = '\0'; |
| 2521 | /* will also find an augment module */ |
| 2522 | prefix_mod = ly_ctx_get_module(ctx, module_name, NULL); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2523 | |
| 2524 | if (buf_backup) { |
| 2525 | /* return previous internal buffer content */ |
Michal Vasko | 888a129 | 2016-04-05 12:08:54 +0200 | [diff] [blame] | 2526 | strncpy(module_name, buf_backup, LY_BUF_SIZE - 1); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2527 | free(buf_backup); |
| 2528 | buf_backup = NULL; |
| 2529 | } |
| 2530 | ly_buf_used--; |
| 2531 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2532 | if (!prefix_mod) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2533 | str = strndup(nodeid, (mod_name + mod_name_len) - nodeid); |
| 2534 | LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str); |
| 2535 | free(str); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2536 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2537 | return NULL; |
| 2538 | } |
| 2539 | } else { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2540 | prefix_mod = prev_mod; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2541 | } |
Michal Vasko | 1adc724 | 2016-11-16 11:05:28 +0100 | [diff] [blame] | 2542 | if (prefix_mod != lyd_node_module(sibling)) { |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2543 | continue; |
| 2544 | } |
| 2545 | |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2546 | /* leaf-list, did we find it with the correct value or not? */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2547 | if (sibling->schema->nodetype == LYS_LEAFLIST) { |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2548 | llist = (struct lyd_node_leaf_list *)sibling; |
| 2549 | |
Michal Vasko | 0a8d17f | 2016-10-19 15:51:36 +0200 | [diff] [blame] | 2550 | last_has_pred = 0; |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2551 | if (has_predicate) { |
Michal Vasko | f359b02 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2552 | if ((r = parse_schema_json_predicate(id, NULL, NULL, &pred_name, &pred_name_len, &llist_value, |
| 2553 | &llval_len, &last_has_pred)) < 1) { |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2554 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
| 2555 | *parsed = -1; |
| 2556 | return NULL; |
| 2557 | } |
Michal Vasko | 0a8d17f | 2016-10-19 15:51:36 +0200 | [diff] [blame] | 2558 | if ((pred_name[0] != '.') || (pred_name_len != 1)) { |
| 2559 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[1], id + 1); |
| 2560 | *parsed = -1; |
| 2561 | return NULL; |
| 2562 | } |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2563 | } else { |
| 2564 | r = 0; |
| 2565 | if (llist_value) { |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2566 | llval_len = strlen(llist_value); |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2567 | } |
| 2568 | } |
| 2569 | |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2570 | /* make value canonical */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 2571 | if ((llist->value_type & LY_TYPE_IDENT) |
Michal Vasko | 6a938d6 | 2016-12-21 09:21:30 +0100 | [diff] [blame] | 2572 | && !strncmp(llist->value_str, lyd_node_module(sibling)->name, strlen(lyd_node_module(sibling)->name)) |
| 2573 | && (llist->value_str[strlen(lyd_node_module(sibling)->name)] == ':')) { |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2574 | data_val = llist->value_str + strlen(lyd_node_module(sibling)->name) + 1; |
| 2575 | } else { |
| 2576 | data_val = llist->value_str; |
| 2577 | } |
| 2578 | |
| 2579 | if ((!llist_value && data_val && data_val[0]) |
| 2580 | || (llist_value && (strncmp(llist_value, data_val, llval_len) || data_val[llval_len]))) { |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2581 | continue; |
| 2582 | } |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2583 | |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2584 | id += r; |
| 2585 | last_parsed += r; |
Michal Vasko | 0a8d17f | 2016-10-19 15:51:36 +0200 | [diff] [blame] | 2586 | has_predicate = last_has_pred; |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2587 | |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 2588 | } else if (sibling->schema->nodetype == LYS_LIST) { |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2589 | /* list, we likely need predicates'n'stuff then, but if without a predicate, we are always creating it */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2590 | if (!has_predicate) { |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2591 | /* none match */ |
| 2592 | return last_match; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2593 | } |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2594 | |
| 2595 | ++list_instance_position; |
| 2596 | r = 0; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2597 | ret = resolve_partial_json_data_list_predicate(id, name, sibling, list_instance_position, &r); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2598 | if (ret == -1) { |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2599 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2600 | return NULL; |
| 2601 | } else if (ret == 1) { |
| 2602 | /* this list instance does not match */ |
| 2603 | continue; |
| 2604 | } |
| 2605 | id += r; |
| 2606 | last_parsed += r; |
| 2607 | } |
| 2608 | |
| 2609 | *parsed += last_parsed; |
| 2610 | |
| 2611 | /* the result node? */ |
| 2612 | if (!id[0]) { |
| 2613 | return sibling; |
| 2614 | } |
| 2615 | |
| 2616 | /* move down the tree, if possible */ |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 2617 | if (sibling->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2618 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2619 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2620 | return NULL; |
| 2621 | } |
Michal Vasko | c6c52cf | 2016-03-29 11:53:04 +0200 | [diff] [blame] | 2622 | last_match = sibling; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2623 | prev_mod = lyd_node_module(sibling); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2624 | start = sibling->child; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2625 | break; |
| 2626 | } |
| 2627 | } |
| 2628 | |
| 2629 | /* no match, return last match */ |
| 2630 | if (!sibling) { |
| 2631 | return last_match; |
| 2632 | } |
| 2633 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2634 | if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate, NULL, 0)) < 1) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2635 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2636 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2637 | return NULL; |
| 2638 | } |
| 2639 | id += r; |
| 2640 | last_parsed = r; |
| 2641 | } |
| 2642 | |
| 2643 | /* cannot get here */ |
| 2644 | LOGINT; |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2645 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2646 | return NULL; |
| 2647 | } |
| 2648 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2649 | /** |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2650 | * @brief Resolves length or range intervals. Does not log. |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2651 | * Syntax is assumed to be correct, *ret MUST be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2652 | * |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2653 | * @param[in] str_restr Restriction as a string. |
| 2654 | * @param[in] type Type of the restriction. |
| 2655 | * @param[out] ret Final interval structure that starts with |
| 2656 | * the interval of the initial type, continues with intervals |
| 2657 | * of any superior types derived from the initial one, and |
| 2658 | * finishes with intervals from our \p type. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2659 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2660 | * @return EXIT_SUCCESS on succes, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2661 | */ |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2662 | int |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2663 | resolve_len_ran_interval(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] | 2664 | { |
| 2665 | /* 0 - unsigned, 1 - signed, 2 - floating point */ |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2666 | int kind; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2667 | int64_t local_smin, local_smax, local_fmin, local_fmax; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2668 | uint64_t local_umin, local_umax; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2669 | uint8_t local_fdig; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2670 | const char *seg_ptr, *ptr; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2671 | 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] | 2672 | |
| 2673 | switch (type->base) { |
| 2674 | case LY_TYPE_BINARY: |
| 2675 | kind = 0; |
| 2676 | local_umin = 0; |
| 2677 | local_umax = 18446744073709551615UL; |
| 2678 | |
| 2679 | if (!str_restr && type->info.binary.length) { |
| 2680 | str_restr = type->info.binary.length->expr; |
| 2681 | } |
| 2682 | break; |
| 2683 | case LY_TYPE_DEC64: |
| 2684 | kind = 2; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2685 | local_fmin = __INT64_C(-9223372036854775807) - __INT64_C(1); |
| 2686 | local_fmax = __INT64_C(9223372036854775807); |
| 2687 | local_fdig = type->info.dec64.dig; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2688 | |
| 2689 | if (!str_restr && type->info.dec64.range) { |
| 2690 | str_restr = type->info.dec64.range->expr; |
| 2691 | } |
| 2692 | break; |
| 2693 | case LY_TYPE_INT8: |
| 2694 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2695 | local_smin = __INT64_C(-128); |
| 2696 | local_smax = __INT64_C(127); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2697 | |
| 2698 | if (!str_restr && type->info.num.range) { |
| 2699 | str_restr = type->info.num.range->expr; |
| 2700 | } |
| 2701 | break; |
| 2702 | case LY_TYPE_INT16: |
| 2703 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2704 | local_smin = __INT64_C(-32768); |
| 2705 | local_smax = __INT64_C(32767); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2706 | |
| 2707 | if (!str_restr && type->info.num.range) { |
| 2708 | str_restr = type->info.num.range->expr; |
| 2709 | } |
| 2710 | break; |
| 2711 | case LY_TYPE_INT32: |
| 2712 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2713 | local_smin = __INT64_C(-2147483648); |
| 2714 | local_smax = __INT64_C(2147483647); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2715 | |
| 2716 | if (!str_restr && type->info.num.range) { |
| 2717 | str_restr = type->info.num.range->expr; |
| 2718 | } |
| 2719 | break; |
| 2720 | case LY_TYPE_INT64: |
| 2721 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2722 | local_smin = __INT64_C(-9223372036854775807) - __INT64_C(1); |
| 2723 | local_smax = __INT64_C(9223372036854775807); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2724 | |
| 2725 | if (!str_restr && type->info.num.range) { |
| 2726 | str_restr = type->info.num.range->expr; |
| 2727 | } |
| 2728 | break; |
| 2729 | case LY_TYPE_UINT8: |
| 2730 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2731 | local_umin = __UINT64_C(0); |
| 2732 | local_umax = __UINT64_C(255); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2733 | |
| 2734 | if (!str_restr && type->info.num.range) { |
| 2735 | str_restr = type->info.num.range->expr; |
| 2736 | } |
| 2737 | break; |
| 2738 | case LY_TYPE_UINT16: |
| 2739 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2740 | local_umin = __UINT64_C(0); |
| 2741 | local_umax = __UINT64_C(65535); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2742 | |
| 2743 | if (!str_restr && type->info.num.range) { |
| 2744 | str_restr = type->info.num.range->expr; |
| 2745 | } |
| 2746 | break; |
| 2747 | case LY_TYPE_UINT32: |
| 2748 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2749 | local_umin = __UINT64_C(0); |
| 2750 | local_umax = __UINT64_C(4294967295); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2751 | |
| 2752 | if (!str_restr && type->info.num.range) { |
| 2753 | str_restr = type->info.num.range->expr; |
| 2754 | } |
| 2755 | break; |
| 2756 | case LY_TYPE_UINT64: |
| 2757 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2758 | local_umin = __UINT64_C(0); |
| 2759 | local_umax = __UINT64_C(18446744073709551615); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2760 | |
| 2761 | if (!str_restr && type->info.num.range) { |
| 2762 | str_restr = type->info.num.range->expr; |
| 2763 | } |
| 2764 | break; |
| 2765 | case LY_TYPE_STRING: |
| 2766 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2767 | local_umin = __UINT64_C(0); |
| 2768 | local_umax = __UINT64_C(18446744073709551615); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2769 | |
| 2770 | if (!str_restr && type->info.str.length) { |
| 2771 | str_restr = type->info.str.length->expr; |
| 2772 | } |
| 2773 | break; |
| 2774 | default: |
| 2775 | LOGINT; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2776 | return -1; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2777 | } |
| 2778 | |
| 2779 | /* process superior types */ |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2780 | if (type->der) { |
| 2781 | if (resolve_len_ran_interval(NULL, &type->der->type, &intv)) { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2782 | LOGINT; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2783 | return -1; |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2784 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2785 | assert(!intv || (intv->kind == kind)); |
| 2786 | } |
| 2787 | |
| 2788 | if (!str_restr) { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2789 | /* we do not have any restriction, return superior ones */ |
| 2790 | *ret = intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2791 | return EXIT_SUCCESS; |
| 2792 | } |
| 2793 | |
| 2794 | /* adjust local min and max */ |
| 2795 | if (intv) { |
| 2796 | tmp_intv = intv; |
| 2797 | |
| 2798 | if (kind == 0) { |
| 2799 | local_umin = tmp_intv->value.uval.min; |
| 2800 | } else if (kind == 1) { |
| 2801 | local_smin = tmp_intv->value.sval.min; |
| 2802 | } else if (kind == 2) { |
| 2803 | local_fmin = tmp_intv->value.fval.min; |
| 2804 | } |
| 2805 | |
| 2806 | while (tmp_intv->next) { |
| 2807 | tmp_intv = tmp_intv->next; |
| 2808 | } |
| 2809 | |
| 2810 | if (kind == 0) { |
| 2811 | local_umax = tmp_intv->value.uval.max; |
| 2812 | } else if (kind == 1) { |
| 2813 | local_smax = tmp_intv->value.sval.max; |
| 2814 | } else if (kind == 2) { |
| 2815 | local_fmax = tmp_intv->value.fval.max; |
| 2816 | } |
| 2817 | } |
| 2818 | |
| 2819 | /* finally parse our restriction */ |
| 2820 | seg_ptr = str_restr; |
Pavol Vican | 9e7c01d | 2016-08-29 09:36:17 +0200 | [diff] [blame] | 2821 | tmp_intv = NULL; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2822 | while (1) { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2823 | if (!tmp_local_intv) { |
| 2824 | assert(!local_intv); |
| 2825 | local_intv = malloc(sizeof *local_intv); |
| 2826 | tmp_local_intv = local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2827 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2828 | tmp_local_intv->next = malloc(sizeof *tmp_local_intv); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2829 | tmp_local_intv = tmp_local_intv->next; |
| 2830 | } |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 2831 | LY_CHECK_ERR_GOTO(!tmp_local_intv, LOGMEM, error); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2832 | |
| 2833 | tmp_local_intv->kind = kind; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2834 | tmp_local_intv->type = type; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2835 | tmp_local_intv->next = NULL; |
| 2836 | |
| 2837 | /* min */ |
| 2838 | ptr = seg_ptr; |
| 2839 | while (isspace(ptr[0])) { |
| 2840 | ++ptr; |
| 2841 | } |
| 2842 | if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) { |
| 2843 | if (kind == 0) { |
Radek Krejci | f87f07d | 2017-07-11 10:53:16 +0200 | [diff] [blame] | 2844 | tmp_local_intv->value.uval.min = strtoll(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2845 | } else if (kind == 1) { |
Radek Krejci | f87f07d | 2017-07-11 10:53:16 +0200 | [diff] [blame] | 2846 | tmp_local_intv->value.sval.min = strtoll(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2847 | } else if (kind == 2) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 2848 | if (parse_range_dec64(&ptr, local_fdig, &tmp_local_intv->value.fval.min)) { |
| 2849 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, ptr, "range"); |
| 2850 | goto error; |
| 2851 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2852 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2853 | } else if (!strncmp(ptr, "min", 3)) { |
| 2854 | if (kind == 0) { |
| 2855 | tmp_local_intv->value.uval.min = local_umin; |
| 2856 | } else if (kind == 1) { |
| 2857 | tmp_local_intv->value.sval.min = local_smin; |
| 2858 | } else if (kind == 2) { |
| 2859 | tmp_local_intv->value.fval.min = local_fmin; |
| 2860 | } |
| 2861 | |
| 2862 | ptr += 3; |
| 2863 | } else if (!strncmp(ptr, "max", 3)) { |
| 2864 | if (kind == 0) { |
| 2865 | tmp_local_intv->value.uval.min = local_umax; |
| 2866 | } else if (kind == 1) { |
| 2867 | tmp_local_intv->value.sval.min = local_smax; |
| 2868 | } else if (kind == 2) { |
| 2869 | tmp_local_intv->value.fval.min = local_fmax; |
| 2870 | } |
| 2871 | |
| 2872 | ptr += 3; |
| 2873 | } else { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2874 | LOGINT; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2875 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2876 | } |
| 2877 | |
| 2878 | while (isspace(ptr[0])) { |
| 2879 | ptr++; |
| 2880 | } |
| 2881 | |
| 2882 | /* no interval or interval */ |
| 2883 | if ((ptr[0] == '|') || !ptr[0]) { |
| 2884 | if (kind == 0) { |
| 2885 | tmp_local_intv->value.uval.max = tmp_local_intv->value.uval.min; |
| 2886 | } else if (kind == 1) { |
| 2887 | tmp_local_intv->value.sval.max = tmp_local_intv->value.sval.min; |
| 2888 | } else if (kind == 2) { |
| 2889 | tmp_local_intv->value.fval.max = tmp_local_intv->value.fval.min; |
| 2890 | } |
| 2891 | } else if (!strncmp(ptr, "..", 2)) { |
| 2892 | /* skip ".." */ |
| 2893 | ptr += 2; |
| 2894 | while (isspace(ptr[0])) { |
| 2895 | ++ptr; |
| 2896 | } |
| 2897 | |
| 2898 | /* max */ |
| 2899 | if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) { |
| 2900 | if (kind == 0) { |
Radek Krejci | f87f07d | 2017-07-11 10:53:16 +0200 | [diff] [blame] | 2901 | tmp_local_intv->value.uval.max = strtoll(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2902 | } else if (kind == 1) { |
Radek Krejci | f87f07d | 2017-07-11 10:53:16 +0200 | [diff] [blame] | 2903 | tmp_local_intv->value.sval.max = strtoll(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2904 | } else if (kind == 2) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 2905 | if (parse_range_dec64(&ptr, local_fdig, &tmp_local_intv->value.fval.max)) { |
| 2906 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, ptr, "range"); |
| 2907 | goto error; |
| 2908 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2909 | } |
| 2910 | } else if (!strncmp(ptr, "max", 3)) { |
| 2911 | if (kind == 0) { |
| 2912 | tmp_local_intv->value.uval.max = local_umax; |
| 2913 | } else if (kind == 1) { |
| 2914 | tmp_local_intv->value.sval.max = local_smax; |
| 2915 | } else if (kind == 2) { |
| 2916 | tmp_local_intv->value.fval.max = local_fmax; |
| 2917 | } |
| 2918 | } else { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2919 | LOGINT; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2920 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2921 | } |
| 2922 | } else { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2923 | LOGINT; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2924 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2925 | } |
| 2926 | |
Pavol Vican | 9e7c01d | 2016-08-29 09:36:17 +0200 | [diff] [blame] | 2927 | /* check min and max in correct order*/ |
| 2928 | if (kind == 0) { |
| 2929 | /* current segment */ |
| 2930 | if (tmp_local_intv->value.uval.min > tmp_local_intv->value.uval.max) { |
| 2931 | goto error; |
| 2932 | } |
| 2933 | if (tmp_local_intv->value.uval.min < local_umin || tmp_local_intv->value.uval.max > local_umax) { |
| 2934 | goto error; |
| 2935 | } |
| 2936 | /* segments sholud be ascending order */ |
Pavol Vican | 69f62c9 | 2016-08-30 09:06:25 +0200 | [diff] [blame] | 2937 | 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] | 2938 | goto error; |
| 2939 | } |
| 2940 | } else if (kind == 1) { |
| 2941 | if (tmp_local_intv->value.sval.min > tmp_local_intv->value.sval.max) { |
| 2942 | goto error; |
| 2943 | } |
| 2944 | if (tmp_local_intv->value.sval.min < local_smin || tmp_local_intv->value.sval.max > local_smax) { |
| 2945 | goto error; |
| 2946 | } |
Pavol Vican | 69f62c9 | 2016-08-30 09:06:25 +0200 | [diff] [blame] | 2947 | 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] | 2948 | goto error; |
| 2949 | } |
| 2950 | } else if (kind == 2) { |
| 2951 | if (tmp_local_intv->value.fval.min > tmp_local_intv->value.fval.max) { |
| 2952 | goto error; |
| 2953 | } |
| 2954 | if (tmp_local_intv->value.fval.min < local_fmin || tmp_local_intv->value.fval.max > local_fmax) { |
| 2955 | goto error; |
| 2956 | } |
Pavol Vican | 69f62c9 | 2016-08-30 09:06:25 +0200 | [diff] [blame] | 2957 | 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] | 2958 | /* 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] | 2959 | goto error; |
| 2960 | } |
| 2961 | } |
| 2962 | |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2963 | /* next segment (next OR) */ |
| 2964 | seg_ptr = strchr(seg_ptr, '|'); |
| 2965 | if (!seg_ptr) { |
| 2966 | break; |
| 2967 | } |
| 2968 | seg_ptr++; |
Pavol Vican | 9e7c01d | 2016-08-29 09:36:17 +0200 | [diff] [blame] | 2969 | tmp_intv = tmp_local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2970 | } |
| 2971 | |
| 2972 | /* check local restrictions against superior ones */ |
| 2973 | if (intv) { |
| 2974 | tmp_intv = intv; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2975 | tmp_local_intv = local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2976 | |
| 2977 | while (tmp_local_intv && tmp_intv) { |
| 2978 | /* reuse local variables */ |
| 2979 | if (kind == 0) { |
| 2980 | local_umin = tmp_local_intv->value.uval.min; |
| 2981 | local_umax = tmp_local_intv->value.uval.max; |
| 2982 | |
| 2983 | /* it must be in this interval */ |
| 2984 | if ((local_umin >= tmp_intv->value.uval.min) && (local_umin <= tmp_intv->value.uval.max)) { |
| 2985 | /* this interval is covered, next one */ |
| 2986 | if (local_umax <= tmp_intv->value.uval.max) { |
| 2987 | tmp_local_intv = tmp_local_intv->next; |
| 2988 | continue; |
| 2989 | /* ascending order of restrictions -> fail */ |
| 2990 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2991 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2992 | } |
| 2993 | } |
| 2994 | } else if (kind == 1) { |
| 2995 | local_smin = tmp_local_intv->value.sval.min; |
| 2996 | local_smax = tmp_local_intv->value.sval.max; |
| 2997 | |
| 2998 | if ((local_smin >= tmp_intv->value.sval.min) && (local_smin <= tmp_intv->value.sval.max)) { |
| 2999 | if (local_smax <= tmp_intv->value.sval.max) { |
| 3000 | tmp_local_intv = tmp_local_intv->next; |
| 3001 | continue; |
| 3002 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3003 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3004 | } |
| 3005 | } |
| 3006 | } else if (kind == 2) { |
| 3007 | local_fmin = tmp_local_intv->value.fval.min; |
| 3008 | local_fmax = tmp_local_intv->value.fval.max; |
| 3009 | |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 3010 | 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] | 3011 | && (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] | 3012 | 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] | 3013 | tmp_local_intv = tmp_local_intv->next; |
| 3014 | continue; |
| 3015 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3016 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3017 | } |
| 3018 | } |
| 3019 | } |
| 3020 | |
| 3021 | tmp_intv = tmp_intv->next; |
| 3022 | } |
| 3023 | |
| 3024 | /* some interval left uncovered -> fail */ |
| 3025 | if (tmp_local_intv) { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3026 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3027 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3028 | } |
| 3029 | |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3030 | /* append the local intervals to all the intervals of the superior types, return it all */ |
| 3031 | if (intv) { |
| 3032 | for (tmp_intv = intv; tmp_intv->next; tmp_intv = tmp_intv->next); |
| 3033 | tmp_intv->next = local_intv; |
| 3034 | } else { |
| 3035 | intv = local_intv; |
| 3036 | } |
| 3037 | *ret = intv; |
| 3038 | |
| 3039 | return EXIT_SUCCESS; |
| 3040 | |
| 3041 | error: |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3042 | while (intv) { |
| 3043 | tmp_intv = intv->next; |
| 3044 | free(intv); |
| 3045 | intv = tmp_intv; |
| 3046 | } |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3047 | while (local_intv) { |
| 3048 | tmp_local_intv = local_intv->next; |
| 3049 | free(local_intv); |
| 3050 | local_intv = tmp_local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3051 | } |
| 3052 | |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3053 | return -1; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3054 | } |
| 3055 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3056 | /** |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 3057 | * @brief Resolve a typedef, return only resolved typedefs if derived. If leafref, it must be |
| 3058 | * resolved for this function to return it. Does not log. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3059 | * |
| 3060 | * @param[in] name Typedef name. |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3061 | * @param[in] mod_name Typedef name module name. |
| 3062 | * @param[in] module Main module. |
| 3063 | * @param[in] parent Parent of the resolved type definition. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3064 | * @param[out] ret Pointer to the resolved typedef. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3065 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3066 | * @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] | 3067 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3068 | int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3069 | resolve_superior_type(const char *name, const char *mod_name, const struct lys_module *module, |
| 3070 | const struct lys_node *parent, struct lys_tpdf **ret) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3071 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3072 | int i, j; |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 3073 | struct lys_tpdf *tpdf, *match; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3074 | int tpdf_size; |
| 3075 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3076 | if (!mod_name) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3077 | /* no prefix, try built-in types */ |
| 3078 | for (i = 1; i < LY_DATA_TYPE_COUNT; i++) { |
Radek Krejci | a68ddeb | 2017-02-24 12:49:44 +0100 | [diff] [blame] | 3079 | if (!strcmp(ly_types[i]->name, name)) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3080 | if (ret) { |
Radek Krejci | a68ddeb | 2017-02-24 12:49:44 +0100 | [diff] [blame] | 3081 | *ret = ly_types[i]; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3082 | } |
| 3083 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3084 | } |
| 3085 | } |
| 3086 | } else { |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3087 | if (!strcmp(mod_name, module->name)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3088 | /* prefix refers to the current module, ignore it */ |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3089 | mod_name = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3090 | } |
| 3091 | } |
| 3092 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3093 | if (!mod_name && parent) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3094 | /* search in local typedefs */ |
| 3095 | while (parent) { |
| 3096 | switch (parent->nodetype) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3097 | case LYS_CONTAINER: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 3098 | tpdf_size = ((struct lys_node_container *)parent)->tpdf_size; |
| 3099 | tpdf = ((struct lys_node_container *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3100 | break; |
| 3101 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3102 | case LYS_LIST: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 3103 | tpdf_size = ((struct lys_node_list *)parent)->tpdf_size; |
| 3104 | tpdf = ((struct lys_node_list *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3105 | break; |
| 3106 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3107 | case LYS_GROUPING: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 3108 | tpdf_size = ((struct lys_node_grp *)parent)->tpdf_size; |
| 3109 | tpdf = ((struct lys_node_grp *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3110 | break; |
| 3111 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3112 | case LYS_RPC: |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 3113 | case LYS_ACTION: |
| 3114 | tpdf_size = ((struct lys_node_rpc_action *)parent)->tpdf_size; |
| 3115 | tpdf = ((struct lys_node_rpc_action *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3116 | break; |
| 3117 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3118 | case LYS_NOTIF: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 3119 | tpdf_size = ((struct lys_node_notif *)parent)->tpdf_size; |
| 3120 | tpdf = ((struct lys_node_notif *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3121 | break; |
| 3122 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3123 | case LYS_INPUT: |
| 3124 | case LYS_OUTPUT: |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 3125 | tpdf_size = ((struct lys_node_inout *)parent)->tpdf_size; |
| 3126 | tpdf = ((struct lys_node_inout *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3127 | break; |
| 3128 | |
| 3129 | default: |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 3130 | parent = lys_parent(parent); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3131 | continue; |
| 3132 | } |
| 3133 | |
| 3134 | for (i = 0; i < tpdf_size; i++) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 3135 | if (!strcmp(tpdf[i].name, name) && tpdf[i].type.base > 0) { |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 3136 | match = &tpdf[i]; |
| 3137 | goto check_leafref; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3138 | } |
| 3139 | } |
| 3140 | |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 3141 | parent = lys_parent(parent); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3142 | } |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 3143 | } else { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3144 | /* get module where to search */ |
Michal Vasko | b6729c6 | 2015-10-21 12:09:47 +0200 | [diff] [blame] | 3145 | module = lys_get_import_module(module, NULL, 0, mod_name, 0); |
Michal Vasko | c935fff | 2015-08-17 14:02:06 +0200 | [diff] [blame] | 3146 | if (!module) { |
| 3147 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3148 | } |
| 3149 | } |
| 3150 | |
| 3151 | /* search in top level typedefs */ |
| 3152 | for (i = 0; i < module->tpdf_size; i++) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 3153 | if (!strcmp(module->tpdf[i].name, name) && module->tpdf[i].type.base > 0) { |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 3154 | match = &module->tpdf[i]; |
| 3155 | goto check_leafref; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3156 | } |
| 3157 | } |
| 3158 | |
| 3159 | /* search in submodules */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 3160 | for (i = 0; i < module->inc_size && module->inc[i].submodule; i++) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3161 | for (j = 0; j < module->inc[i].submodule->tpdf_size; j++) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 3162 | if (!strcmp(module->inc[i].submodule->tpdf[j].name, name) && module->inc[i].submodule->tpdf[j].type.base > 0) { |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 3163 | match = &module->inc[i].submodule->tpdf[j]; |
| 3164 | goto check_leafref; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3165 | } |
| 3166 | } |
| 3167 | } |
| 3168 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3169 | return EXIT_FAILURE; |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 3170 | |
| 3171 | check_leafref: |
| 3172 | if (ret) { |
| 3173 | *ret = match; |
| 3174 | } |
| 3175 | if (match->type.base == LY_TYPE_LEAFREF) { |
| 3176 | while (!match->type.info.lref.path) { |
| 3177 | match = match->type.der; |
| 3178 | assert(match); |
| 3179 | } |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 3180 | } |
| 3181 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3182 | } |
| 3183 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3184 | /** |
| 3185 | * @brief Check the default \p value of the \p type. Logs directly. |
| 3186 | * |
| 3187 | * @param[in] type Type definition to use. |
| 3188 | * @param[in] value Default value to check. |
Michal Vasko | 5682640 | 2016-03-02 11:11:37 +0100 | [diff] [blame] | 3189 | * @param[in] module Type module. |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3190 | * |
| 3191 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 3192 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3193 | static int |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 3194 | 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] | 3195 | { |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3196 | struct lys_tpdf *base_tpdf = NULL; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3197 | struct lyd_node_leaf_list node; |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3198 | const char *dflt = NULL; |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 3199 | char *s; |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 3200 | int ret = EXIT_SUCCESS; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3201 | |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3202 | assert(value); |
| 3203 | |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 3204 | if (type->base <= LY_TYPE_DER) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3205 | /* the type was not resolved yet, nothing to do for now */ |
| 3206 | return EXIT_FAILURE; |
Radek Krejci | 29eac3d | 2017-06-01 16:50:02 +0200 | [diff] [blame] | 3207 | } else if (!tpdf && !module->implemented) { |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 3208 | /* do not check defaults in not implemented module's data */ |
| 3209 | return EXIT_SUCCESS; |
Radek Krejci | 29eac3d | 2017-06-01 16:50:02 +0200 | [diff] [blame] | 3210 | } else if (tpdf && !module->implemented && type->base == LY_TYPE_IDENT) { |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 3211 | /* identityrefs are checked when instantiated in data instead of typedef, |
| 3212 | * but in typedef the value has to be modified to include the prefix */ |
| 3213 | if (*value) { |
| 3214 | if (strchr(*value, ':')) { |
| 3215 | dflt = transform_schema2json(module, *value); |
| 3216 | } else { |
| 3217 | /* default prefix of the module where the typedef is defined */ |
| 3218 | asprintf(&s, "%s:%s", lys_main_module(module)->name, *value); |
| 3219 | dflt = lydict_insert_zc(module->ctx, s); |
| 3220 | } |
| 3221 | lydict_remove(module->ctx, *value); |
| 3222 | *value = dflt; |
| 3223 | } |
| 3224 | return EXIT_SUCCESS; |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 3225 | } else if (type->base == LY_TYPE_LEAFREF && tpdf) { |
| 3226 | /* leafref in typedef cannot be checked */ |
| 3227 | return EXIT_SUCCESS; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3228 | } |
| 3229 | |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3230 | dflt = *value; |
| 3231 | if (!dflt) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3232 | /* we do not have a new default value, so is there any to check even, in some base type? */ |
| 3233 | for (base_tpdf = type->der; base_tpdf->type.der; base_tpdf = base_tpdf->type.der) { |
| 3234 | if (base_tpdf->dflt) { |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3235 | dflt = base_tpdf->dflt; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3236 | break; |
| 3237 | } |
| 3238 | } |
| 3239 | |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3240 | if (!dflt) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3241 | /* no default value, nothing to check, all is well */ |
| 3242 | return EXIT_SUCCESS; |
| 3243 | } |
| 3244 | |
| 3245 | /* 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)? */ |
| 3246 | switch (type->base) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3247 | case LY_TYPE_IDENT: |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 3248 | if (lys_main_module(base_tpdf->type.parent->module)->implemented) { |
| 3249 | return EXIT_SUCCESS; |
| 3250 | } else { |
| 3251 | /* check the default value from typedef, but use also the typedef's module |
| 3252 | * due to possible searching in imported modules which is expected in |
| 3253 | * typedef's module instead of module where the typedef is used */ |
| 3254 | module = base_tpdf->module; |
| 3255 | } |
| 3256 | break; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3257 | case LY_TYPE_INST: |
| 3258 | case LY_TYPE_LEAFREF: |
| 3259 | case LY_TYPE_BOOL: |
| 3260 | case LY_TYPE_EMPTY: |
| 3261 | /* these have no restrictions, so we would do the exact same work as the unres in the base typedef */ |
| 3262 | return EXIT_SUCCESS; |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3263 | case LY_TYPE_BITS: |
| 3264 | /* the default value must match the restricted list of values, if the type was restricted */ |
| 3265 | if (type->info.bits.count) { |
| 3266 | break; |
| 3267 | } |
| 3268 | return EXIT_SUCCESS; |
| 3269 | case LY_TYPE_ENUM: |
| 3270 | /* the default value must match the restricted list of values, if the type was restricted */ |
| 3271 | if (type->info.enums.count) { |
| 3272 | break; |
| 3273 | } |
| 3274 | return EXIT_SUCCESS; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3275 | case LY_TYPE_DEC64: |
| 3276 | if (type->info.dec64.range) { |
| 3277 | break; |
| 3278 | } |
| 3279 | return EXIT_SUCCESS; |
| 3280 | case LY_TYPE_BINARY: |
| 3281 | if (type->info.binary.length) { |
| 3282 | break; |
| 3283 | } |
| 3284 | return EXIT_SUCCESS; |
| 3285 | case LY_TYPE_INT8: |
| 3286 | case LY_TYPE_INT16: |
| 3287 | case LY_TYPE_INT32: |
| 3288 | case LY_TYPE_INT64: |
| 3289 | case LY_TYPE_UINT8: |
| 3290 | case LY_TYPE_UINT16: |
| 3291 | case LY_TYPE_UINT32: |
| 3292 | case LY_TYPE_UINT64: |
| 3293 | if (type->info.num.range) { |
| 3294 | break; |
| 3295 | } |
| 3296 | return EXIT_SUCCESS; |
| 3297 | case LY_TYPE_STRING: |
| 3298 | if (type->info.str.length || type->info.str.patterns) { |
| 3299 | break; |
| 3300 | } |
| 3301 | return EXIT_SUCCESS; |
| 3302 | case LY_TYPE_UNION: |
| 3303 | /* way too much trouble learning whether we need to check the default again, so just do it */ |
| 3304 | break; |
| 3305 | default: |
| 3306 | LOGINT; |
| 3307 | return -1; |
| 3308 | } |
Radek Krejci | 55a161c | 2016-09-05 17:13:25 +0200 | [diff] [blame] | 3309 | } else if (type->base == LY_TYPE_EMPTY) { |
| 3310 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "default", type->parent->name); |
| 3311 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The \"empty\" data type cannot have a default value."); |
| 3312 | return -1; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3313 | } |
| 3314 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3315 | /* dummy leaf */ |
Radek Krejci | 4fe36bd | 2016-03-17 16:47:16 +0100 | [diff] [blame] | 3316 | memset(&node, 0, sizeof node); |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3317 | node.value_str = dflt; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3318 | node.value_type = type->base; |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 3319 | node.schema = calloc(1, sizeof (struct lys_node_leaf)); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3320 | LY_CHECK_ERR_RETURN(!node.schema, LOGMEM, -1); |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3321 | node.schema->name = strdup("fake-default"); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3322 | LY_CHECK_ERR_RETURN(!node.schema->name, LOGMEM; free(node.schema), -1); |
Michal Vasko | 5682640 | 2016-03-02 11:11:37 +0100 | [diff] [blame] | 3323 | node.schema->module = module; |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 3324 | memcpy(&((struct lys_node_leaf *)node.schema)->type, type, sizeof *type); |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3325 | |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 3326 | if (type->base == LY_TYPE_LEAFREF) { |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3327 | if (!type->info.lref.target) { |
| 3328 | ret = EXIT_FAILURE; |
| 3329 | goto finish; |
| 3330 | } |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 3331 | ret = check_default(&type->info.lref.target->type, &dflt, module, 0); |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3332 | if (!ret) { |
| 3333 | /* adopt possibly changed default value to its canonical form */ |
| 3334 | if (*value) { |
| 3335 | *value = dflt; |
| 3336 | } |
| 3337 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3338 | } else { |
Radek Krejci | a571d94 | 2017-02-24 09:26:49 +0100 | [diff] [blame] | 3339 | if (!lyp_parse_value(&((struct lys_node_leaf *)node.schema)->type, &node.value_str, NULL, &node, NULL, 1, 1)) { |
Radek Krejci | 5dca593 | 2016-11-04 14:30:47 +0100 | [diff] [blame] | 3340 | /* possible forward reference */ |
| 3341 | ret = 1; |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3342 | if (base_tpdf) { |
Radek Krejci | 9ad23f4 | 2016-10-31 15:46:52 +0100 | [diff] [blame] | 3343 | /* default value is defined in some base typedef */ |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3344 | if ((type->base == LY_TYPE_BITS && type->der->type.der) || |
| 3345 | (type->base == LY_TYPE_ENUM && type->der->type.der)) { |
| 3346 | /* we have refined bits/enums */ |
| 3347 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, |
| 3348 | "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] | 3349 | dflt, type->parent->name, base_tpdf->name); |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3350 | } |
| 3351 | } |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3352 | } else { |
| 3353 | /* success - adopt canonical form from the node into the default value */ |
| 3354 | if (dflt != node.value_str) { |
| 3355 | /* this can happen only if we have non-inherited default value, |
| 3356 | * inherited default values are already in canonical form */ |
| 3357 | assert(dflt == *value); |
| 3358 | *value = node.value_str; |
| 3359 | } |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 3360 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3361 | } |
| 3362 | |
| 3363 | finish: |
| 3364 | if (node.value_type == LY_TYPE_BITS) { |
| 3365 | free(node.value.bit); |
| 3366 | } |
| 3367 | free((char *)node.schema->name); |
| 3368 | free(node.schema); |
| 3369 | |
| 3370 | return ret; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3371 | } |
| 3372 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3373 | /** |
| 3374 | * @brief Check a key for mandatory attributes. Logs directly. |
| 3375 | * |
| 3376 | * @param[in] key The key to check. |
| 3377 | * @param[in] flags What flags to check. |
| 3378 | * @param[in] list The list of all the keys. |
| 3379 | * @param[in] index Index of the key in the key list. |
| 3380 | * @param[in] name The name of the keys. |
| 3381 | * @param[in] len The name length. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3382 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3383 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3384 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3385 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3386 | 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] | 3387 | { |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3388 | struct lys_node_leaf *key = list->keys[index]; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3389 | char *dup = NULL; |
| 3390 | int j; |
| 3391 | |
| 3392 | /* existence */ |
| 3393 | if (!key) { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3394 | if (name[len] != '\0') { |
| 3395 | dup = strdup(name); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3396 | LY_CHECK_ERR_RETURN(!dup, LOGMEM, -1); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3397 | dup[len] = '\0'; |
| 3398 | name = dup; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3399 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3400 | LOGVAL(LYE_KEY_MISS, LY_VLOG_LYS, list, name); |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 3401 | free(dup); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3402 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3403 | } |
| 3404 | |
| 3405 | /* uniqueness */ |
| 3406 | for (j = index - 1; j >= 0; j--) { |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3407 | if (key == list->keys[j]) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3408 | LOGVAL(LYE_KEY_DUP, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3409 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3410 | } |
| 3411 | } |
| 3412 | |
| 3413 | /* key is a leaf */ |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3414 | if (key->nodetype != LYS_LEAF) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3415 | LOGVAL(LYE_KEY_NLEAF, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3416 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3417 | } |
| 3418 | |
| 3419 | /* type of the leaf is not built-in empty */ |
Radek Krejci | c3738db | 2016-09-15 15:51:21 +0200 | [diff] [blame] | 3420 | if (key->type.base == LY_TYPE_EMPTY && key->module->version < 2) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3421 | LOGVAL(LYE_KEY_TYPE, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3422 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3423 | } |
| 3424 | |
| 3425 | /* config attribute is the same as of the list */ |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 3426 | if ((key->flags & LYS_CONFIG_MASK) && (list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3427 | LOGVAL(LYE_KEY_CONFIG, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3428 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3429 | } |
| 3430 | |
Radek Krejci | 55e2cdc | 2016-03-11 13:51:09 +0100 | [diff] [blame] | 3431 | /* key is not placed from augment */ |
| 3432 | if (key->parent->nodetype == LYS_AUGMENT) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3433 | LOGVAL(LYE_KEY_MISS, LY_VLOG_LYS, key, key->name); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 3434 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Key inserted from augment."); |
Radek Krejci | 55e2cdc | 2016-03-11 13:51:09 +0100 | [diff] [blame] | 3435 | return -1; |
| 3436 | } |
| 3437 | |
Radek Krejci | 3f21ada | 2016-08-01 13:34:31 +0200 | [diff] [blame] | 3438 | /* key is not when/if-feature -conditional */ |
| 3439 | j = 0; |
| 3440 | if (key->when || (key->iffeature_size && (j = 1))) { |
| 3441 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, key, j ? "if-feature" : "when", "leaf"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 3442 | LOGVAL(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] | 3443 | j ? "if-feature" : "when"); |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3444 | return -1; |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3445 | } |
| 3446 | |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3447 | return EXIT_SUCCESS; |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3448 | } |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3449 | |
| 3450 | /** |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3451 | * @brief Resolve (test the target exists) unique. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3452 | * |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3453 | * @param[in] parent The parent node of the unique structure. |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3454 | * @param[in] uniq_str_path One path from the unique string. |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3455 | * |
| 3456 | * @return EXIT_SUCCESS on succes, EXIT_FAILURE on forward reference, -1 on error. |
| 3457 | */ |
| 3458 | int |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3459 | 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] | 3460 | { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3461 | int rc; |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3462 | const struct lys_node *leaf = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3463 | |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 3464 | 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] | 3465 | if (rc || !leaf) { |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3466 | if (rc) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3467 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3468 | if (rc > 0) { |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 3469 | LOGVAL(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] | 3470 | } else if (rc == -2) { |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 3471 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Unique argument references list."); |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3472 | } |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3473 | rc = -1; |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3474 | } else { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3475 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 3476 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Target leaf not found."); |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3477 | rc = EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3478 | } |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3479 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3480 | } |
Michal Vasko | 9bb061b | 2016-02-12 11:00:19 +0100 | [diff] [blame] | 3481 | if (leaf->nodetype != LYS_LEAF) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3482 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 3483 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Target is not a leaf."); |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3484 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3485 | } |
| 3486 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3487 | /* check status */ |
Radek Krejci | c3f1b6f | 2017-02-15 10:51:10 +0100 | [diff] [blame] | 3488 | if (parent->nodetype != LYS_EXT && lyp_check_status(parent->flags, parent->module, parent->name, |
| 3489 | leaf->flags, leaf->module, leaf->name, leaf)) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3490 | return -1; |
| 3491 | } |
| 3492 | |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3493 | /* check that all unique's targets are of the same config type */ |
| 3494 | if (*trg_type) { |
| 3495 | if (((*trg_type == 1) && (leaf->flags & LYS_CONFIG_R)) || ((*trg_type == 2) && (leaf->flags & LYS_CONFIG_W))) { |
| 3496 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 3497 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3498 | "Leaf \"%s\" referenced in unique statement is config %s, but previous referenced leaf is config %s.", |
| 3499 | uniq_str_path, *trg_type == 1 ? "false" : "true", *trg_type == 1 ? "true" : "false"); |
| 3500 | return -1; |
| 3501 | } |
| 3502 | } else { |
| 3503 | /* first unique */ |
| 3504 | if (leaf->flags & LYS_CONFIG_W) { |
| 3505 | *trg_type = 1; |
| 3506 | } else { |
| 3507 | *trg_type = 2; |
| 3508 | } |
| 3509 | } |
| 3510 | |
Radek Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame] | 3511 | /* set leaf's unique flag */ |
| 3512 | ((struct lys_node_leaf *)leaf)->flags |= LYS_UNIQUE; |
| 3513 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3514 | return EXIT_SUCCESS; |
| 3515 | |
| 3516 | error: |
| 3517 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3518 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3519 | } |
| 3520 | |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 3521 | void |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3522 | unres_data_del(struct unres_data *unres, uint32_t i) |
| 3523 | { |
| 3524 | /* there are items after the one deleted */ |
| 3525 | if (i+1 < unres->count) { |
| 3526 | /* we only move the data, memory is left allocated, why bother */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3527 | 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] | 3528 | |
| 3529 | /* deleting the last item */ |
| 3530 | } else if (i == 0) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3531 | free(unres->node); |
| 3532 | unres->node = NULL; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3533 | } |
| 3534 | |
| 3535 | /* if there are no items after and it is not the last one, just move the counter */ |
| 3536 | --unres->count; |
| 3537 | } |
| 3538 | |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3539 | /** |
| 3540 | * @brief Resolve (find) a data node from a specific module. Does not log. |
| 3541 | * |
| 3542 | * @param[in] mod Module to search in. |
| 3543 | * @param[in] name Name of the data node. |
| 3544 | * @param[in] nam_len Length of the name. |
| 3545 | * @param[in] start Data node to start the search from. |
| 3546 | * @param[in,out] parents Resolved nodes. If there are some parents, |
| 3547 | * they are replaced (!!) with the resolvents. |
| 3548 | * |
Michal Vasko | 2471e7f | 2016-04-11 11:00:15 +0200 | [diff] [blame] | 3549 | * @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] | 3550 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3551 | static int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3552 | 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] | 3553 | { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3554 | struct lyd_node *node; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3555 | int flag; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3556 | uint32_t i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3557 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3558 | if (!parents->count) { |
| 3559 | parents->count = 1; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3560 | parents->node = malloc(sizeof *parents->node); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3561 | LY_CHECK_ERR_RETURN(!parents->node, LOGMEM, -1); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3562 | parents->node[0] = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3563 | } |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3564 | for (i = 0; i < parents->count;) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 3565 | 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] | 3566 | /* skip */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3567 | ++i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3568 | continue; |
| 3569 | } |
| 3570 | flag = 0; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3571 | LY_TREE_FOR(parents->node[i] ? parents->node[i]->child : start, node) { |
Michal Vasko | 3960835 | 2017-05-11 10:37:10 +0200 | [diff] [blame] | 3572 | 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] | 3573 | && node->schema->name[nam_len] == '\0') { |
| 3574 | /* matching target */ |
| 3575 | if (!flag) { |
Michal Vasko | 9a47e12 | 2015-09-03 14:26:32 +0200 | [diff] [blame] | 3576 | /* put node instead of the current parent */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3577 | parents->node[i] = node; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3578 | flag = 1; |
| 3579 | } else { |
Michal Vasko | 9a47e12 | 2015-09-03 14:26:32 +0200 | [diff] [blame] | 3580 | /* multiple matching, so create a new node */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3581 | ++parents->count; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3582 | parents->node = ly_realloc(parents->node, parents->count * sizeof *parents->node); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3583 | LY_CHECK_ERR_RETURN(!parents->node, LOGMEM, EXIT_FAILURE); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3584 | parents->node[parents->count-1] = node; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3585 | ++i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3586 | } |
| 3587 | } |
| 3588 | } |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3589 | |
| 3590 | if (!flag) { |
| 3591 | /* remove item from the parents list */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3592 | unres_data_del(parents, i); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3593 | } else { |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3594 | ++i; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3595 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3596 | } |
| 3597 | |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3598 | return parents->count ? EXIT_SUCCESS : EXIT_FAILURE; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3599 | } |
| 3600 | |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3601 | static int |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3602 | resolve_schema_leafref_valid_dep_flag(const struct lys_node *op_node, const struct lys_node *first_node, int abs_path) |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3603 | { |
| 3604 | int dep1, dep2; |
| 3605 | const struct lys_node *node; |
| 3606 | |
| 3607 | if (lys_parent(op_node)) { |
| 3608 | /* inner operation (notif/action) */ |
| 3609 | if (abs_path) { |
| 3610 | return 1; |
| 3611 | } else { |
| 3612 | /* compare depth of both nodes */ |
| 3613 | for (dep1 = 0, node = op_node; lys_parent(node); node = lys_parent(node)); |
| 3614 | for (dep2 = 0, node = first_node; lys_parent(node); node = lys_parent(node)); |
| 3615 | if ((dep2 > dep1) || ((dep2 == dep1) && (op_node != first_node))) { |
| 3616 | return 1; |
| 3617 | } |
| 3618 | } |
| 3619 | } else { |
| 3620 | /* top-level operation (notif/rpc) */ |
| 3621 | if (op_node != first_node) { |
| 3622 | return 1; |
| 3623 | } |
| 3624 | } |
| 3625 | |
| 3626 | return 0; |
| 3627 | } |
| 3628 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3629 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3630 | * @brief Resolve a path (leafref) predicate in JSON schema context. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3631 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3632 | * @param[in] path Path to use. |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3633 | * @param[in] context_node Predicate context node (where the predicate is placed). |
| 3634 | * @param[in] parent Path context node (where the path begins/is placed). |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3635 | * @param[in] op_node Optional node if the leafref is in an operation (action/rpc/notif). |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3636 | * |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3637 | * @return 0 on forward reference, otherwise the number |
| 3638 | * of characters successfully parsed, |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3639 | * positive on success, negative on failure. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3640 | */ |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3641 | static int |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3642 | resolve_schema_leafref_predicate(const char *path, const struct lys_node *context_node, |
| 3643 | struct lys_node *parent, const struct lys_node *op_node) |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3644 | { |
Michal Vasko | 0f99d3e | 2017-01-10 10:50:40 +0100 | [diff] [blame] | 3645 | const struct lys_module *trg_mod; |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3646 | const struct lys_node *src_node, *dst_node; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3647 | const char *path_key_expr, *source, *sour_pref, *dest, *dest_pref; |
Michal Vasko | f9b35d9 | 2016-10-21 15:19:30 +0200 | [diff] [blame] | 3648 | int pke_len, sour_len, sour_pref_len, dest_len, dest_pref_len, pke_parsed, parsed = 0; |
| 3649 | int has_predicate, dest_parent_times, i, rc, first_iter; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3650 | |
| 3651 | do { |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3652 | 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] | 3653 | &pke_len, &has_predicate)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3654 | LOGVAL(LYE_INCHAR, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, path[-i], path-i); |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3655 | return -parsed+i; |
| 3656 | } |
| 3657 | parsed += i; |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3658 | path += i; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3659 | |
Michal Vasko | 5809090 | 2015-08-13 14:04:15 +0200 | [diff] [blame] | 3660 | /* source (must be leaf) */ |
Michal Vasko | 0f99d3e | 2017-01-10 10:50:40 +0100 | [diff] [blame] | 3661 | if (sour_pref) { |
| 3662 | trg_mod = lys_get_import_module(lys_node_module(parent), NULL, 0, sour_pref, sour_pref_len); |
| 3663 | } else { |
| 3664 | trg_mod = NULL; |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3665 | } |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3666 | rc = lys_getnext_data(trg_mod, context_node, source, sour_len, LYS_LEAF | LYS_LEAFLIST, &src_node); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3667 | if (rc) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3668 | LOGVAL(LYE_NORESOLV, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, "leafref predicate", path-parsed); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3669 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3670 | } |
| 3671 | |
| 3672 | /* destination */ |
Michal Vasko | f9b35d9 | 2016-10-21 15:19:30 +0200 | [diff] [blame] | 3673 | dest_parent_times = 0; |
| 3674 | pke_parsed = 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3675 | if ((i = parse_path_key_expr(path_key_expr, &dest_pref, &dest_pref_len, &dest, &dest_len, |
| 3676 | &dest_parent_times)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3677 | LOGVAL(LYE_INCHAR, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, path_key_expr[-i], path_key_expr-i); |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3678 | return -parsed; |
| 3679 | } |
| 3680 | pke_parsed += i; |
| 3681 | |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3682 | for (i = 0, dst_node = parent; i < dest_parent_times; ++i) { |
Michal Vasko | 3ba2d79 | 2017-07-10 15:14:43 +0200 | [diff] [blame] | 3683 | if (dst_node->parent && (dst_node->parent->nodetype == LYS_AUGMENT) |
| 3684 | && !((struct lys_node_augment *)dst_node->parent)->target) { |
| 3685 | /* we are in an unresolved augment, cannot evaluate */ |
| 3686 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, dst_node->parent, |
| 3687 | "Cannot resolve leafref predicate \"%s\" because it is in an unresolved augment.", path_key_expr); |
| 3688 | return 0; |
| 3689 | } |
| 3690 | |
Michal Vasko | fbaead7 | 2016-10-07 10:54:48 +0200 | [diff] [blame] | 3691 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 3692 | * all schema nodes that cannot be instantiated in data tree */ |
| 3693 | for (dst_node = lys_parent(dst_node); |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3694 | 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] | 3695 | dst_node = lys_parent(dst_node)); |
| 3696 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3697 | if (!dst_node) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3698 | LOGVAL(LYE_NORESOLV, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, "leafref predicate", path_key_expr); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3699 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3700 | } |
| 3701 | } |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3702 | first_iter = 1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3703 | while (1) { |
Michal Vasko | 0f99d3e | 2017-01-10 10:50:40 +0100 | [diff] [blame] | 3704 | if (dest_pref) { |
| 3705 | trg_mod = lys_get_import_module(lys_node_module(parent), NULL, 0, dest_pref, dest_pref_len); |
| 3706 | } else { |
| 3707 | trg_mod = NULL; |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3708 | } |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3709 | rc = lys_getnext_data(trg_mod, dst_node, dest, dest_len, LYS_CONTAINER | LYS_LIST | LYS_LEAF, &dst_node); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3710 | if (rc) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3711 | LOGVAL(LYE_NORESOLV, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, "leafref predicate", path_key_expr); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3712 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3713 | } |
| 3714 | |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3715 | if (first_iter) { |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3716 | if (resolve_schema_leafref_valid_dep_flag(op_node, dst_node, 0)) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 3717 | parent->flags |= LYS_LEAFREF_DEP; |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3718 | } |
| 3719 | first_iter = 0; |
| 3720 | } |
| 3721 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3722 | if (pke_len == pke_parsed) { |
| 3723 | break; |
| 3724 | } |
| 3725 | |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3726 | 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] | 3727 | &dest_parent_times)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3728 | LOGVAL(LYE_INCHAR, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3729 | (path_key_expr + pke_parsed)[-i], (path_key_expr + pke_parsed)-i); |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3730 | return -parsed; |
| 3731 | } |
| 3732 | pke_parsed += i; |
| 3733 | } |
| 3734 | |
| 3735 | /* check source - dest match */ |
Michal Vasko | 59ad458 | 2016-09-16 13:15:41 +0200 | [diff] [blame] | 3736 | if (dst_node->nodetype != src_node->nodetype) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3737 | LOGVAL(LYE_NORESOLV, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, "leafref predicate", path - parsed); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 3738 | LOGVAL(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] | 3739 | strnodetype(src_node->nodetype), strnodetype(dst_node->nodetype)); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3740 | return -parsed; |
| 3741 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3742 | } while (has_predicate); |
| 3743 | |
| 3744 | return parsed; |
| 3745 | } |
| 3746 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3747 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3748 | * @brief Resolve a path (leafref) in JSON schema context. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3749 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3750 | * @param[in] path Path to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3751 | * @param[in] parent_node Parent of the leafref. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3752 | * @param[out] ret Pointer to the resolved schema node. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3753 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3754 | * @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] | 3755 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3756 | static int |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3757 | resolve_schema_leafref(const char *path, struct lys_node *parent, const struct lys_node **ret) |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3758 | { |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3759 | const struct lys_node *node, *op_node = NULL; |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3760 | struct lys_node_augment *last_aug; |
Michal Vasko | 3c60cbb | 2017-07-10 11:50:03 +0200 | [diff] [blame] | 3761 | const struct lys_module *tmp_mod, *cur_module; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3762 | const char *id, *prefix, *name; |
| 3763 | int pref_len, nam_len, parent_times, has_predicate; |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3764 | int i, first_iter, rc; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3765 | |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3766 | first_iter = 1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3767 | parent_times = 0; |
| 3768 | id = path; |
| 3769 | |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3770 | /* find operation schema we are in */ |
| 3771 | for (op_node = lys_parent(parent); |
| 3772 | op_node && !(op_node->nodetype & (LYS_ACTION | LYS_NOTIF | LYS_RPC)); |
| 3773 | op_node = lys_parent(op_node)); |
Michal Vasko | e9914d1 | 2016-10-07 14:32:37 +0200 | [diff] [blame] | 3774 | |
Michal Vasko | 3c60cbb | 2017-07-10 11:50:03 +0200 | [diff] [blame] | 3775 | cur_module = lys_node_module(parent); |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3776 | do { |
Michal Vasko | 3c60cbb | 2017-07-10 11:50:03 +0200 | [diff] [blame] | 3777 | if ((i = parse_path_arg(cur_module, id, &prefix, &pref_len, &name, &nam_len, &parent_times, &has_predicate)) < 1) { |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3778 | LOGVAL(LYE_INCHAR, LY_VLOG_LYS, parent, id[-i], &id[-i]); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3779 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3780 | } |
| 3781 | id += i; |
| 3782 | |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3783 | /* get the current module */ |
Michal Vasko | 3c60cbb | 2017-07-10 11:50:03 +0200 | [diff] [blame] | 3784 | tmp_mod = prefix ? lys_get_import_module(cur_module, NULL, 0, prefix, pref_len) : cur_module; |
| 3785 | if (!tmp_mod) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3786 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path); |
| 3787 | return EXIT_FAILURE; |
| 3788 | } |
| 3789 | last_aug = NULL; |
| 3790 | |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3791 | if (first_iter) { |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3792 | if (parent_times == -1) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3793 | /* use module data */ |
| 3794 | node = NULL; |
Radek Krejci | 990af1f | 2016-11-09 13:53:36 +0100 | [diff] [blame] | 3795 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3796 | } else if (parent_times > 0) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3797 | /* we are looking for the right parent */ |
| 3798 | for (i = 0, node = parent; i < parent_times; i++) { |
Michal Vasko | 3ba2d79 | 2017-07-10 15:14:43 +0200 | [diff] [blame] | 3799 | if (node->parent && (node->parent->nodetype == LYS_AUGMENT) |
| 3800 | && !((struct lys_node_augment *)node->parent)->target) { |
| 3801 | /* we are in an unresolved augment, cannot evaluate */ |
| 3802 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, node->parent, |
| 3803 | "Cannot resolve leafref \"%s\" because it is in an unresolved augment.", path); |
| 3804 | return EXIT_FAILURE; |
| 3805 | } |
| 3806 | |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 3807 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 3808 | * all schema nodes that cannot be instantiated in data tree */ |
| 3809 | for (node = lys_parent(node); |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3810 | node && !(node->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_ACTION | LYS_NOTIF | LYS_RPC)); |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 3811 | node = lys_parent(node)); |
| 3812 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3813 | if (!node) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3814 | if (i == parent_times - 1) { |
| 3815 | /* top-level */ |
| 3816 | break; |
| 3817 | } |
| 3818 | |
| 3819 | /* higher than top-level */ |
Michal Vasko | e9914d1 | 2016-10-07 14:32:37 +0200 | [diff] [blame] | 3820 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3821 | return EXIT_FAILURE; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3822 | } |
| 3823 | } |
Michal Vasko | e01eca5 | 2015-08-13 14:42:02 +0200 | [diff] [blame] | 3824 | } else { |
| 3825 | LOGINT; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3826 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3827 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3828 | } |
| 3829 | |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3830 | /* find the next node (either in unconnected augment or as a schema sibling, node is NULL for top-level node - |
| 3831 | * - useless to search for that in augments) */ |
Michal Vasko | 3c60cbb | 2017-07-10 11:50:03 +0200 | [diff] [blame] | 3832 | if (!tmp_mod->implemented && node) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3833 | get_next_augment: |
Michal Vasko | 3c60cbb | 2017-07-10 11:50:03 +0200 | [diff] [blame] | 3834 | last_aug = lys_getnext_target_aug(last_aug, tmp_mod, node); |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3835 | } |
| 3836 | |
Michal Vasko | 3c60cbb | 2017-07-10 11:50:03 +0200 | [diff] [blame] | 3837 | rc = lys_getnext_data(tmp_mod, (last_aug ? (struct lys_node *)last_aug : node), name, nam_len, LYS_LIST |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3838 | | LYS_CONTAINER | LYS_RPC | LYS_ACTION | LYS_NOTIF | LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA, &node); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3839 | if (rc) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3840 | if (last_aug) { |
| 3841 | goto get_next_augment; |
| 3842 | } |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3843 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path); |
Michal Vasko | 7a55bea | 2016-05-02 14:51:20 +0200 | [diff] [blame] | 3844 | return EXIT_FAILURE; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3845 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3846 | |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3847 | if (first_iter) { |
| 3848 | /* set external dependency flag, we can decide based on the first found node */ |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3849 | if (op_node && parent_times && |
| 3850 | resolve_schema_leafref_valid_dep_flag(op_node, node, (parent_times == -1 ? 1 : 0))) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 3851 | parent->flags |= LYS_LEAFREF_DEP; |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3852 | } |
| 3853 | first_iter = 0; |
| 3854 | } |
| 3855 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3856 | if (has_predicate) { |
| 3857 | /* we have predicate, so the current result must be list */ |
| 3858 | if (node->nodetype != LYS_LIST) { |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3859 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3860 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3861 | } |
| 3862 | |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3863 | i = resolve_schema_leafref_predicate(id, node, parent, op_node); |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3864 | if (!i) { |
| 3865 | return EXIT_FAILURE; |
| 3866 | } else if (i < 0) { |
| 3867 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3868 | } |
| 3869 | id += i; |
Michal Vasko | f9b35d9 | 2016-10-21 15:19:30 +0200 | [diff] [blame] | 3870 | has_predicate = 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3871 | } |
| 3872 | } while (id[0]); |
| 3873 | |
Michal Vasko | ca91768 | 2016-07-25 11:00:37 +0200 | [diff] [blame] | 3874 | /* the target must be leaf or leaf-list (in YANG 1.1 only) */ |
Radek Krejci | 2a5a960 | 2016-11-04 10:21:13 +0100 | [diff] [blame] | 3875 | if ((node->nodetype != LYS_LEAF) && (node->nodetype != LYS_LEAFLIST)) { |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3876 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 3877 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Leafref target \"%s\" is not a leaf nor a leaf-list.", path); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3878 | return -1; |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 3879 | } |
| 3880 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3881 | /* check status */ |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3882 | if (lyp_check_status(parent->flags, parent->module, parent->name, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3883 | node->flags, node->module, node->name, node)) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3884 | return -1; |
| 3885 | } |
| 3886 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3887 | if (ret) { |
| 3888 | *ret = node; |
| 3889 | } |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 3890 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3891 | return EXIT_SUCCESS; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3892 | } |
| 3893 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3894 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3895 | * @brief Resolve instance-identifier predicate in JSON data format. |
| 3896 | * Does not log. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3897 | * |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 3898 | * @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] | 3899 | * @param[in] pred Predicate to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3900 | * @param[in,out] node_match Nodes matching the restriction without |
| 3901 | * the predicate. Nodes not satisfying |
| 3902 | * the predicate are removed. |
| 3903 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3904 | * @return Number of characters successfully parsed, |
| 3905 | * positive on success, negative on failure. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3906 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3907 | static int |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 3908 | resolve_instid_predicate(const struct lys_module *prev_mod, const char *pred, struct unres_data *node_match) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3909 | { |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3910 | /* ... /node[target = value] ... */ |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3911 | struct lyd_node *target; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3912 | const char *model, *name, *value; |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3913 | int mod_len, nam_len, val_len, i, has_predicate, cur_idx, idx, parsed, pred_iter, k; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3914 | uint32_t j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3915 | |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3916 | assert(pred && node_match->count); |
| 3917 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3918 | idx = -1; |
| 3919 | parsed = 0; |
| 3920 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3921 | pred_iter = -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3922 | do { |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3923 | if ((i = parse_predicate(pred, &model, &mod_len, &name, &nam_len, &value, &val_len, &has_predicate)) < 1) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3924 | return -parsed+i; |
| 3925 | } |
| 3926 | parsed += i; |
| 3927 | pred += i; |
| 3928 | |
| 3929 | if (isdigit(name[0])) { |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3930 | /* pos */ |
| 3931 | assert(!value); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3932 | idx = atoi(name); |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3933 | } else if (name[0] != '.') { |
| 3934 | /* list keys */ |
| 3935 | if (pred_iter < 0) { |
| 3936 | pred_iter = 1; |
| 3937 | } else { |
| 3938 | ++pred_iter; |
| 3939 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3940 | } |
| 3941 | |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 3942 | for (cur_idx = 1, j = 0; j < node_match->count; ++cur_idx) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3943 | /* target */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3944 | if (name[0] == '.') { |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3945 | /* leaf-list value */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3946 | if (node_match->node[j]->schema->nodetype != LYS_LEAFLIST) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3947 | goto remove_instid; |
| 3948 | } |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3949 | |
| 3950 | target = node_match->node[j]; |
| 3951 | /* check the value */ |
| 3952 | if (strncmp(((struct lyd_node_leaf_list *)target)->value_str, value, val_len) |
| 3953 | || ((struct lyd_node_leaf_list *)target)->value_str[val_len]) { |
| 3954 | goto remove_instid; |
| 3955 | } |
| 3956 | |
| 3957 | } else if (!value) { |
| 3958 | /* keyless list position */ |
| 3959 | if ((node_match->node[j]->schema->nodetype != LYS_LIST) |
| 3960 | || ((struct lys_node_list *)node_match->node[j]->schema)->keys) { |
| 3961 | goto remove_instid; |
| 3962 | } |
| 3963 | |
| 3964 | if (idx != cur_idx) { |
| 3965 | goto remove_instid; |
| 3966 | } |
| 3967 | |
| 3968 | } else { |
| 3969 | /* list key value */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3970 | if (node_match->node[j]->schema->nodetype != LYS_LIST) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3971 | goto remove_instid; |
| 3972 | } |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3973 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3974 | /* find the key leaf */ |
Michal Vasko | 045182c | 2016-09-09 12:44:07 +0200 | [diff] [blame] | 3975 | for (k = 1, target = node_match->node[j]->child; target && (k < pred_iter); k++, target = target->next); |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3976 | if (!target) { |
| 3977 | goto remove_instid; |
| 3978 | } |
| 3979 | if ((struct lys_node_leaf *)target->schema != |
| 3980 | ((struct lys_node_list *)node_match->node[j]->schema)->keys[pred_iter - 1]) { |
| 3981 | goto remove_instid; |
| 3982 | } |
| 3983 | |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 3984 | /* check name */ |
| 3985 | if (strncmp(target->schema->name, name, nam_len) || target->schema->name[nam_len]) { |
| 3986 | goto remove_instid; |
| 3987 | } |
| 3988 | |
| 3989 | /* check module */ |
| 3990 | if (model) { |
| 3991 | if (strncmp(target->schema->module->name, model, mod_len) |
| 3992 | || target->schema->module->name[mod_len]) { |
| 3993 | goto remove_instid; |
| 3994 | } |
| 3995 | } else { |
| 3996 | if (target->schema->module != prev_mod) { |
| 3997 | goto remove_instid; |
| 3998 | } |
| 3999 | } |
| 4000 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4001 | /* check the value */ |
| 4002 | if (strncmp(((struct lyd_node_leaf_list *)target)->value_str, value, val_len) |
| 4003 | || ((struct lyd_node_leaf_list *)target)->value_str[val_len]) { |
| 4004 | goto remove_instid; |
| 4005 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4006 | } |
| 4007 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4008 | /* instid is ok, continue check with the next one */ |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4009 | ++j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4010 | continue; |
| 4011 | |
| 4012 | remove_instid: |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4013 | /* does not fulfill conditions, remove instid record */ |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4014 | unres_data_del(node_match, j); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4015 | } |
| 4016 | } while (has_predicate); |
| 4017 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4018 | /* check that all list keys were specified */ |
| 4019 | if ((pred_iter > 0) && node_match->count) { |
Michal Vasko | 045182c | 2016-09-09 12:44:07 +0200 | [diff] [blame] | 4020 | j = 0; |
| 4021 | while (j < node_match->count) { |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4022 | assert(node_match->node[j]->schema->nodetype == LYS_LIST); |
| 4023 | if (pred_iter < ((struct lys_node_list *)node_match->node[j]->schema)->keys_size) { |
| 4024 | /* not enough predicates, just remove the list instance */ |
| 4025 | unres_data_del(node_match, j); |
Michal Vasko | 045182c | 2016-09-09 12:44:07 +0200 | [diff] [blame] | 4026 | } else { |
| 4027 | ++j; |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4028 | } |
| 4029 | } |
| 4030 | |
| 4031 | if (!node_match->count) { |
| 4032 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier is missing some list keys."); |
| 4033 | } |
| 4034 | } |
| 4035 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4036 | return parsed; |
| 4037 | } |
| 4038 | |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4039 | int |
Michal Vasko | 769f803 | 2017-01-24 13:11:55 +0100 | [diff] [blame] | 4040 | lys_check_xpath(struct lys_node *node, int check_place, int warn_on_fwd_ref) |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4041 | { |
| 4042 | struct lys_node *parent, *elem; |
| 4043 | struct lyxp_set set; |
| 4044 | uint32_t i; |
Michal Vasko | 769f803 | 2017-01-24 13:11:55 +0100 | [diff] [blame] | 4045 | int ret; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4046 | |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4047 | if (check_place) { |
| 4048 | parent = node; |
| 4049 | while (parent) { |
| 4050 | if (parent->nodetype == LYS_GROUPING) { |
| 4051 | /* unresolved grouping, skip for now (will be checked later) */ |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4052 | return EXIT_SUCCESS; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4053 | } |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4054 | if (parent->nodetype == LYS_AUGMENT) { |
| 4055 | if (!((struct lys_node_augment *)parent)->target) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4056 | /* unresolved augment, skip for now (will be checked later) */ |
| 4057 | return EXIT_FAILURE; |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4058 | } else { |
| 4059 | parent = ((struct lys_node_augment *)parent)->target; |
| 4060 | continue; |
| 4061 | } |
| 4062 | } |
| 4063 | parent = parent->parent; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4064 | } |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4065 | } |
| 4066 | |
Michal Vasko | 769f803 | 2017-01-24 13:11:55 +0100 | [diff] [blame] | 4067 | ret = lyxp_node_atomize(node, &set, warn_on_fwd_ref); |
| 4068 | if (ret == -1) { |
| 4069 | return -1; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4070 | } |
| 4071 | |
| 4072 | for (parent = node; parent && !(parent->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); parent = lys_parent(parent)); |
| 4073 | |
| 4074 | for (i = 0; i < set.used; ++i) { |
| 4075 | /* skip roots'n'stuff */ |
| 4076 | if (set.val.snodes[i].type == LYXP_NODE_ELEM) { |
| 4077 | /* XPath expression cannot reference "lower" status than the node that has the definition */ |
| 4078 | if (lyp_check_status(node->flags, lys_node_module(node), node->name, set.val.snodes[i].snode->flags, |
| 4079 | lys_node_module(set.val.snodes[i].snode), set.val.snodes[i].snode->name, node)) { |
| 4080 | return -1; |
| 4081 | } |
| 4082 | |
| 4083 | if (parent) { |
| 4084 | for (elem = set.val.snodes[i].snode; elem && (elem != parent); elem = lys_parent(elem)); |
| 4085 | if (!elem) { |
| 4086 | /* not in node's RPC or notification subtree, set the flag */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 4087 | node->flags |= LYS_XPATH_DEP; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4088 | break; |
| 4089 | } |
| 4090 | } |
| 4091 | } |
| 4092 | } |
| 4093 | |
| 4094 | free(set.val.snodes); |
Michal Vasko | 769f803 | 2017-01-24 13:11:55 +0100 | [diff] [blame] | 4095 | return ret; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4096 | } |
| 4097 | |
Radek Krejci | f71f48f | 2016-10-25 16:37:24 +0200 | [diff] [blame] | 4098 | static int |
| 4099 | check_leafref_config(struct lys_node_leaf *leaf, struct lys_type *type) |
| 4100 | { |
| 4101 | int i; |
| 4102 | |
| 4103 | if (type->base == LY_TYPE_LEAFREF) { |
Radek Krejci | c688ca0 | 2017-03-20 12:54:39 +0100 | [diff] [blame] | 4104 | if ((leaf->flags & LYS_CONFIG_W) && type->info.lref.target && type->info.lref.req != -1 && |
| 4105 | (type->info.lref.target->flags & LYS_CONFIG_R)) { |
Radek Krejci | d831dd4 | 2017-03-16 12:59:30 +0100 | [diff] [blame] | 4106 | LOGVAL(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] | 4107 | strnodetype(leaf->nodetype), strnodetype(type->info.lref.target->nodetype)); |
| 4108 | return -1; |
| 4109 | } |
| 4110 | /* we can skip the test in case the leafref is not yet resolved. In that case the test is done in the time |
| 4111 | * of leafref resolving (lys_leaf_add_leafref_target()) */ |
| 4112 | } else if (type->base == LY_TYPE_UNION) { |
| 4113 | for (i = 0; i < type->info.uni.count; i++) { |
| 4114 | if (check_leafref_config(leaf, &type->info.uni.types[i])) { |
| 4115 | return -1; |
| 4116 | } |
| 4117 | } |
| 4118 | } |
| 4119 | return 0; |
| 4120 | } |
| 4121 | |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4122 | /** |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4123 | * @brief Passes config flag down to children, skips nodes without config flags. |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4124 | * Logs. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4125 | * |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4126 | * @param[in] node Siblings and their children to have flags changed. |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4127 | * @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] | 4128 | * @param[in] flags Flags to assign to all the nodes. |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 4129 | * @param[in,out] unres List of unresolved items. |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4130 | * |
| 4131 | * @return 0 on success, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4132 | */ |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4133 | int |
| 4134 | inherit_config_flag(struct lys_node *node, int flags, int clear) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4135 | { |
Radek Krejci | f71f48f | 2016-10-25 16:37:24 +0200 | [diff] [blame] | 4136 | struct lys_node_leaf *leaf; |
| 4137 | |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4138 | assert(!(flags ^ (flags & LYS_CONFIG_MASK))); |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4139 | LY_TREE_FOR(node, node) { |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4140 | if (clear) { |
| 4141 | node->flags &= ~LYS_CONFIG_MASK; |
Michal Vasko | c2a8d36 | 2016-09-29 08:50:13 +0200 | [diff] [blame] | 4142 | node->flags &= ~LYS_CONFIG_SET; |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4143 | } else { |
| 4144 | if (node->flags & LYS_CONFIG_SET) { |
| 4145 | /* skip nodes with an explicit config value */ |
| 4146 | if ((flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) { |
| 4147 | LOGVAL(LYE_INARG, LY_VLOG_LYS, node, "true", "config"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 4148 | LOGVAL(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] | 4149 | return -1; |
| 4150 | } |
| 4151 | continue; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4152 | } |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4153 | |
| 4154 | if (!(node->nodetype & (LYS_USES | LYS_GROUPING))) { |
| 4155 | node->flags = (node->flags & ~LYS_CONFIG_MASK) | flags; |
| 4156 | /* check that configuration lists have keys */ |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4157 | if ((node->nodetype == LYS_LIST) && (node->flags & LYS_CONFIG_W) |
| 4158 | && !((struct lys_node_list *)node)->keys_size) { |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4159 | LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_LYS, node, "key", "list"); |
| 4160 | return -1; |
| 4161 | } |
| 4162 | } |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4163 | } |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4164 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4165 | if (inherit_config_flag(node->child, flags, clear)) { |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4166 | return -1; |
| 4167 | } |
Radek Krejci | f71f48f | 2016-10-25 16:37:24 +0200 | [diff] [blame] | 4168 | } else if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 4169 | leaf = (struct lys_node_leaf *)node; |
| 4170 | if (check_leafref_config(leaf, &leaf->type)) { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 4171 | return -1; |
| 4172 | } |
| 4173 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4174 | } |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4175 | |
| 4176 | return 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4177 | } |
| 4178 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4179 | /** |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 4180 | * @brief Resolve augment target. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4181 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4182 | * @param[in] aug Augment to use. |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 4183 | * @param[in] siblings Nodes 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] | 4184 | * @param[in,out] unres List of unresolved items. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4185 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4186 | * @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] | 4187 | */ |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 4188 | static int |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 4189 | resolve_augment(struct lys_node_augment *aug, struct lys_node *siblings, struct unres_schema *unres) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4190 | { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4191 | int rc; |
Michal Vasko | 1d87a92 | 2015-08-21 12:57:16 +0200 | [diff] [blame] | 4192 | struct lys_node *sub; |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4193 | struct lys_module *mod; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4194 | struct ly_set *set; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4195 | |
Michal Vasko | 9bf4aa0 | 2017-06-12 09:24:02 +0200 | [diff] [blame] | 4196 | assert(aug); |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 4197 | mod = lys_main_module(aug->module); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4198 | |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 4199 | /* set it as not applied for now */ |
| 4200 | aug->flags |= LYS_NOTAPPLIED; |
| 4201 | |
Michal Vasko | 9bf4aa0 | 2017-06-12 09:24:02 +0200 | [diff] [blame] | 4202 | /* 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] | 4203 | if (!aug->target) { |
Michal Vasko | 9bf4aa0 | 2017-06-12 09:24:02 +0200 | [diff] [blame] | 4204 | /* resolve target node */ |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4205 | rc = resolve_schema_nodeid(aug->target_name, siblings, (siblings ? NULL : lys_node_module((struct lys_node *)aug)), &set, 0, 0); |
Michal Vasko | 9bf4aa0 | 2017-06-12 09:24:02 +0200 | [diff] [blame] | 4206 | if (rc == -1) { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4207 | LOGVAL(LYE_PATH, LY_VLOG_LYS, aug); |
Michal Vasko | 9bf4aa0 | 2017-06-12 09:24:02 +0200 | [diff] [blame] | 4208 | return -1; |
| 4209 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4210 | if (!set) { |
Michal Vasko | 9bf4aa0 | 2017-06-12 09:24:02 +0200 | [diff] [blame] | 4211 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, aug, "augment", aug->target_name); |
| 4212 | return EXIT_FAILURE; |
| 4213 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4214 | aug->target = set->set.s[0]; |
| 4215 | ly_set_free(set); |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4216 | } |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4217 | |
Michal Vasko | d58d596 | 2016-03-02 14:29:41 +0100 | [diff] [blame] | 4218 | /* check for mandatory nodes - if the target node is in another module |
| 4219 | * the added nodes cannot be mandatory |
| 4220 | */ |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4221 | if (!aug->parent && (lys_node_module((struct lys_node *)aug) != lys_node_module(aug->target)) |
| 4222 | && (rc = lyp_check_mandatory_augment(aug, aug->target))) { |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 4223 | return rc; |
Michal Vasko | d58d596 | 2016-03-02 14:29:41 +0100 | [diff] [blame] | 4224 | } |
| 4225 | |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4226 | /* check augment target type and then augment nodes type */ |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4227 | if (aug->target->nodetype & (LYS_CONTAINER | LYS_LIST)) { |
Michal Vasko | db01726 | 2017-01-24 13:10:04 +0100 | [diff] [blame] | 4228 | LY_TREE_FOR(aug->child, sub) { |
| 4229 | if (!(sub->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_LEAF | LYS_LIST | LYS_LEAFLIST | LYS_USES |
| 4230 | | LYS_CHOICE | LYS_ACTION | LYS_NOTIF))) { |
| 4231 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment"); |
| 4232 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Cannot augment \"%s\" with a \"%s\".", |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4233 | strnodetype(aug->target->nodetype), strnodetype(sub->nodetype)); |
Michal Vasko | db01726 | 2017-01-24 13:10:04 +0100 | [diff] [blame] | 4234 | return -1; |
| 4235 | } |
| 4236 | } |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4237 | } 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] | 4238 | LY_TREE_FOR(aug->child, sub) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4239 | if (!(sub->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_LEAF | LYS_LIST | LYS_LEAFLIST | LYS_USES | LYS_CHOICE))) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4240 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 4241 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Cannot augment \"%s\" with a \"%s\".", |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4242 | strnodetype(aug->target->nodetype), strnodetype(sub->nodetype)); |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4243 | return -1; |
| 4244 | } |
| 4245 | } |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4246 | } else if (aug->target->nodetype == LYS_CHOICE) { |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4247 | LY_TREE_FOR(aug->child, sub) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4248 | if (!(sub->nodetype & (LYS_CASE | LYS_ANYDATA | LYS_CONTAINER | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4249 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 4250 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Cannot augment \"%s\" with a \"%s\".", |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4251 | strnodetype(aug->target->nodetype), strnodetype(sub->nodetype)); |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4252 | return -1; |
| 4253 | } |
| 4254 | } |
| 4255 | } else { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4256 | LOGVAL(LYE_INARG, LY_VLOG_LYS, aug, aug->target_name, "target-node"); |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4257 | LOGVAL(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] | 4258 | return -1; |
| 4259 | } |
| 4260 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 4261 | /* check identifier uniqueness as in lys_node_addchild() */ |
Michal Vasko | 1d87a92 | 2015-08-21 12:57:16 +0200 | [diff] [blame] | 4262 | LY_TREE_FOR(aug->child, sub) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4263 | if (lys_check_id(sub, aug->target, NULL)) { |
Michal Vasko | 3e6665f | 2015-08-17 14:00:38 +0200 | [diff] [blame] | 4264 | return -1; |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 4265 | } |
| 4266 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4267 | |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4268 | if (!aug->child) { |
| 4269 | /* empty augment, nothing to connect, but it is techincally applied */ |
| 4270 | LOGWRN("Augment \"%s\" without children.", aug->target_name); |
| 4271 | aug->flags &= ~LYS_NOTAPPLIED; |
| 4272 | } else if (mod->implemented && apply_aug(aug, unres)) { |
| 4273 | /* we tried to connect it, we failed */ |
| 4274 | return -1; |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4275 | } |
| 4276 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4277 | return EXIT_SUCCESS; |
| 4278 | } |
| 4279 | |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4280 | static int |
Radek Krejci | a7db970 | 2017-01-20 12:55:14 +0100 | [diff] [blame] | 4281 | 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] | 4282 | { |
| 4283 | enum LY_VLOG_ELEM vlog_type; |
| 4284 | void *vlog_node; |
| 4285 | unsigned int i, j; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4286 | struct lys_ext *e; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4287 | char *ext_name, *ext_prefix, *tmp; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4288 | struct lyxml_elem *next_yin, *yin; |
Radek Krejci | a7db970 | 2017-01-20 12:55:14 +0100 | [diff] [blame] | 4289 | const struct lys_module *mod; |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4290 | struct lys_ext_instance *tmp_ext; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4291 | LYEXT_TYPE etype; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4292 | |
| 4293 | switch (info->parent_type) { |
Radek Krejci | 0aa821a | 2016-12-08 11:21:35 +0100 | [diff] [blame] | 4294 | case LYEXT_PAR_NODE: |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4295 | vlog_node = info->parent; |
| 4296 | vlog_type = LY_VLOG_LYS; |
| 4297 | break; |
Radek Krejci | 0aa821a | 2016-12-08 11:21:35 +0100 | [diff] [blame] | 4298 | case LYEXT_PAR_MODULE: |
| 4299 | case LYEXT_PAR_IMPORT: |
| 4300 | case LYEXT_PAR_INCLUDE: |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4301 | vlog_node = NULL; |
| 4302 | vlog_type = LY_VLOG_LYS; |
| 4303 | break; |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 4304 | default: |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4305 | vlog_node = NULL; |
Radek Krejci | 6a7fedf | 2017-02-10 12:38:06 +0100 | [diff] [blame] | 4306 | vlog_type = LY_VLOG_NONE; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4307 | break; |
| 4308 | } |
| 4309 | |
| 4310 | if (info->datatype == LYS_IN_YIN) { |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4311 | /* YIN */ |
| 4312 | |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4313 | /* get the module where the extension is supposed to be defined */ |
Radek Krejci | a7db970 | 2017-01-20 12:55:14 +0100 | [diff] [blame] | 4314 | mod = lys_get_import_module_ns(info->mod, info->data.yin->ns->value); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4315 | if (!mod) { |
| 4316 | LOGVAL(LYE_INSTMT, vlog_type, vlog_node, info->data.yin->name); |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 4317 | return EXIT_FAILURE; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4318 | } |
| 4319 | |
| 4320 | /* find the extension definition */ |
| 4321 | e = NULL; |
| 4322 | for (i = 0; i < mod->extensions_size; i++) { |
| 4323 | if (ly_strequal(mod->extensions[i].name, info->data.yin->name, 1)) { |
| 4324 | e = &mod->extensions[i]; |
| 4325 | break; |
| 4326 | } |
| 4327 | } |
| 4328 | /* try submodules */ |
| 4329 | for (j = 0; !e && j < mod->inc_size; j++) { |
| 4330 | for (i = 0; i < mod->inc[j].submodule->extensions_size; i++) { |
| 4331 | if (ly_strequal(mod->inc[j].submodule->extensions[i].name, info->data.yin->name, 1)) { |
| 4332 | e = &mod->inc[j].submodule->extensions[i]; |
| 4333 | break; |
| 4334 | } |
| 4335 | } |
| 4336 | } |
| 4337 | if (!e) { |
| 4338 | LOGVAL(LYE_INSTMT, vlog_type, vlog_node, info->data.yin->name); |
| 4339 | return EXIT_FAILURE; |
| 4340 | } |
| 4341 | |
| 4342 | /* 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] | 4343 | |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4344 | if (e->plugin && e->plugin->check_position) { |
| 4345 | /* common part - we have plugin with position checking function, use it first */ |
| 4346 | if ((*e->plugin->check_position)(info->parent, info->parent_type, info->substmt)) { |
| 4347 | /* extension is not allowed here */ |
| 4348 | LOGVAL(LYE_INSTMT, vlog_type, vlog_node, e->name); |
| 4349 | return -1; |
| 4350 | } |
| 4351 | } |
| 4352 | |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4353 | /* extension type-specific part - allocation */ |
| 4354 | if (e->plugin) { |
| 4355 | etype = e->plugin->type; |
| 4356 | } else { |
| 4357 | /* default type */ |
| 4358 | etype = LYEXT_FLAG; |
| 4359 | } |
| 4360 | switch (etype) { |
| 4361 | case LYEXT_FLAG: |
| 4362 | (*ext) = calloc(1, sizeof(struct lys_ext_instance)); |
| 4363 | break; |
| 4364 | case LYEXT_COMPLEX: |
| 4365 | (*ext) = calloc(1, ((struct lyext_plugin_complex*)e->plugin)->instance_size); |
| 4366 | break; |
| 4367 | case LYEXT_ERR: |
| 4368 | /* we never should be here */ |
| 4369 | LOGINT; |
| 4370 | return -1; |
| 4371 | } |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 4372 | LY_CHECK_ERR_RETURN(!*ext, LOGMEM, -1); |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4373 | |
| 4374 | /* common part for all extension types */ |
| 4375 | (*ext)->def = e; |
| 4376 | (*ext)->parent = info->parent; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4377 | (*ext)->parent_type = info->parent_type; |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 4378 | (*ext)->insubstmt = info->substmt; |
| 4379 | (*ext)->insubstmt_index = info->substmt_index; |
Radek Krejci | 8de8f61 | 2017-02-16 15:03:32 +0100 | [diff] [blame] | 4380 | (*ext)->ext_type = e->plugin ? e->plugin->type : LYEXT_FLAG; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4381 | |
| 4382 | if (!(e->flags & LYS_YINELEM) && e->argument) { |
| 4383 | (*ext)->arg_value = lyxml_get_attr(info->data.yin, e->argument, NULL); |
| 4384 | if (!(*ext)->arg_value) { |
| 4385 | LOGVAL(LYE_MISSARG, LY_VLOG_NONE, NULL, e->argument, info->data.yin->name); |
| 4386 | return -1; |
| 4387 | } |
| 4388 | (*ext)->arg_value = lydict_insert(mod->ctx, (*ext)->arg_value, 0); |
| 4389 | } |
| 4390 | |
Radek Krejci | 7f1d47e | 2017-04-12 15:29:02 +0200 | [diff] [blame] | 4391 | (*ext)->nodetype = LYS_EXT; |
| 4392 | (*ext)->module = info->mod; |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 4393 | |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4394 | /* extension type-specific part - parsing content */ |
| 4395 | switch (etype) { |
| 4396 | case LYEXT_FLAG: |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4397 | LY_TREE_FOR_SAFE(info->data.yin->child, next_yin, yin) { |
| 4398 | if (!yin->ns) { |
| 4399 | /* garbage */ |
| 4400 | lyxml_free(mod->ctx, yin); |
| 4401 | continue; |
| 4402 | } else if (!strcmp(yin->ns->value, LY_NSYIN)) { |
| 4403 | /* standard YANG statements are not expected here */ |
| 4404 | LOGVAL(LYE_INCHILDSTMT, vlog_type, vlog_node, yin->name, info->data.yin->name); |
| 4405 | return -1; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4406 | } else if (yin->ns == info->data.yin->ns && |
| 4407 | (e->flags & LYS_YINELEM) && ly_strequal(yin->name, e->argument, 1)) { |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4408 | /* we have the extension's argument */ |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4409 | if ((*ext)->arg_value) { |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4410 | LOGVAL(LYE_TOOMANY, vlog_type, vlog_node, yin->name, info->data.yin->name); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4411 | return -1; |
| 4412 | } |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4413 | (*ext)->arg_value = yin->content; |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4414 | yin->content = NULL; |
| 4415 | lyxml_free(mod->ctx, yin); |
| 4416 | } else { |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4417 | /* extension instance */ |
| 4418 | if (lyp_yin_parse_subnode_ext(info->mod, *ext, LYEXT_PAR_EXTINST, yin, |
| 4419 | LYEXT_SUBSTMT_SELF, 0, unres)) { |
| 4420 | return -1; |
| 4421 | } |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4422 | |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4423 | continue; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4424 | } |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4425 | } |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4426 | break; |
| 4427 | case LYEXT_COMPLEX: |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 4428 | ((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] | 4429 | if (lyp_yin_parse_complex_ext(info->mod, (struct lys_ext_instance_complex*)(*ext), info->data.yin, unres)) { |
| 4430 | /* TODO memory cleanup */ |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4431 | return -1; |
| 4432 | } |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4433 | break; |
| 4434 | default: |
| 4435 | break; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4436 | } |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4437 | |
| 4438 | /* TODO - lyext_check_result_clb, other than LYEXT_FLAG plugins */ |
| 4439 | |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4440 | } else { |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4441 | /* YANG */ |
| 4442 | |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4443 | ext_prefix = (char *)(*ext)->def; |
| 4444 | tmp = strchr(ext_prefix, ':'); |
| 4445 | if (!tmp) { |
| 4446 | LOGVAL(LYE_INSTMT, vlog_type, vlog_node, ext_prefix); |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4447 | goto error; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4448 | } |
| 4449 | ext_name = tmp + 1; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4450 | |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4451 | /* get the module where the extension is supposed to be defined */ |
| 4452 | mod = lys_get_import_module(info->mod, ext_prefix, tmp - ext_prefix, NULL, 0); |
| 4453 | if (!mod) { |
| 4454 | LOGVAL(LYE_INSTMT, vlog_type, vlog_node, ext_prefix); |
| 4455 | return EXIT_FAILURE; |
| 4456 | } |
| 4457 | |
| 4458 | /* find the extension definition */ |
| 4459 | e = NULL; |
| 4460 | for (i = 0; i < mod->extensions_size; i++) { |
| 4461 | if (ly_strequal(mod->extensions[i].name, ext_name, 0)) { |
| 4462 | e = &mod->extensions[i]; |
| 4463 | break; |
| 4464 | } |
| 4465 | } |
| 4466 | /* try submodules */ |
| 4467 | for (j = 0; !e && j < mod->inc_size; j++) { |
| 4468 | for (i = 0; i < mod->inc[j].submodule->extensions_size; i++) { |
| 4469 | if (ly_strequal(mod->inc[j].submodule->extensions[i].name, ext_name, 0)) { |
| 4470 | e = &mod->inc[j].submodule->extensions[i]; |
| 4471 | break; |
| 4472 | } |
| 4473 | } |
| 4474 | } |
| 4475 | if (!e) { |
| 4476 | LOGVAL(LYE_INSTMT, vlog_type, vlog_node, ext_prefix); |
| 4477 | return EXIT_FAILURE; |
| 4478 | } |
| 4479 | |
| 4480 | /* we have the extension definition, so now it cannot be forward referenced and error is always fatal */ |
| 4481 | |
| 4482 | if (e->plugin && e->plugin->check_position) { |
| 4483 | /* common part - we have plugin with position checking function, use it first */ |
| 4484 | if ((*e->plugin->check_position)(info->parent, info->parent_type, info->substmt)) { |
| 4485 | /* extension is not allowed here */ |
| 4486 | LOGVAL(LYE_INSTMT, vlog_type, vlog_node, e->name); |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4487 | goto error; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4488 | } |
| 4489 | } |
| 4490 | |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4491 | /* extension common part */ |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4492 | (*ext)->flags &= ~LYEXT_OPT_YANG; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4493 | (*ext)->def = e; |
| 4494 | (*ext)->parent = info->parent; |
Radek Krejci | 8de8f61 | 2017-02-16 15:03:32 +0100 | [diff] [blame] | 4495 | (*ext)->ext_type = e->plugin ? e->plugin->type : LYEXT_FLAG; |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4496 | |
PavolVican | b0d8410 | 2017-02-15 16:32:42 +0100 | [diff] [blame] | 4497 | if (e->argument && !(*ext)->arg_value) { |
| 4498 | LOGVAL(LYE_MISSARG, LY_VLOG_NONE, NULL, e->argument, ext_name); |
| 4499 | goto error; |
| 4500 | } |
| 4501 | |
Radek Krejci | 7f1d47e | 2017-04-12 15:29:02 +0200 | [diff] [blame] | 4502 | (*ext)->module = info->mod; |
| 4503 | (*ext)->nodetype = LYS_EXT; |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 4504 | |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4505 | /* extension type-specific part */ |
| 4506 | if (e->plugin) { |
| 4507 | etype = e->plugin->type; |
| 4508 | } else { |
| 4509 | /* default type */ |
| 4510 | etype = LYEXT_FLAG; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4511 | } |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4512 | switch (etype) { |
| 4513 | case LYEXT_FLAG: |
| 4514 | /* nothing change */ |
| 4515 | break; |
| 4516 | case LYEXT_COMPLEX: |
| 4517 | tmp_ext = realloc(*ext, ((struct lyext_plugin_complex*)e->plugin)->instance_size); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 4518 | LY_CHECK_ERR_GOTO(!tmp_ext, LOGMEM, error); |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4519 | memset((char *)tmp_ext + sizeof **ext, 0, ((struct lyext_plugin_complex*)e->plugin)->instance_size - sizeof **ext); |
| 4520 | (*ext) = tmp_ext; |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4521 | ((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] | 4522 | if (info->data.yang) { |
| 4523 | *tmp = ':'; |
PavolVican | db0e817 | 2017-02-20 00:46:09 +0100 | [diff] [blame] | 4524 | if (yang_parse_ext_substatement(info->mod, unres, info->data.yang->ext_substmt, ext_prefix, |
| 4525 | (struct lys_ext_instance_complex*)(*ext))) { |
| 4526 | goto error; |
| 4527 | } |
| 4528 | if (yang_fill_extcomplex_module(info->mod->ctx, (struct lys_ext_instance_complex*)(*ext), ext_prefix, |
| 4529 | info->data.yang->ext_modules, info->mod->implemented)) { |
PavolVican | a1e291f | 2017-02-19 16:07:12 +0100 | [diff] [blame] | 4530 | goto error; |
| 4531 | } |
PavolVican | a387667 | 2017-02-21 15:49:51 +0100 | [diff] [blame] | 4532 | } |
| 4533 | if (lyp_mand_check_ext((struct lys_ext_instance_complex*)(*ext), ext_prefix)) { |
| 4534 | goto error; |
PavolVican | a1e291f | 2017-02-19 16:07:12 +0100 | [diff] [blame] | 4535 | } |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4536 | break; |
| 4537 | case LYEXT_ERR: |
| 4538 | /* we never should be here */ |
| 4539 | LOGINT; |
| 4540 | goto error; |
| 4541 | } |
| 4542 | |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4543 | if (yang_check_ext_instance(info->mod, &(*ext)->ext, (*ext)->ext_size, *ext, unres)) { |
| 4544 | goto error; |
| 4545 | } |
| 4546 | free(ext_prefix); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4547 | } |
| 4548 | |
| 4549 | return EXIT_SUCCESS; |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4550 | error: |
| 4551 | free(ext_prefix); |
| 4552 | return -1; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4553 | } |
| 4554 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4555 | /** |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4556 | * @brief Resolve (find) choice default case. Does not log. |
| 4557 | * |
| 4558 | * @param[in] choic Choice to use. |
| 4559 | * @param[in] dflt Name of the default case. |
| 4560 | * |
| 4561 | * @return Pointer to the default node or NULL. |
| 4562 | */ |
| 4563 | static struct lys_node * |
| 4564 | resolve_choice_dflt(struct lys_node_choice *choic, const char *dflt) |
| 4565 | { |
| 4566 | struct lys_node *child, *ret; |
| 4567 | |
| 4568 | LY_TREE_FOR(choic->child, child) { |
| 4569 | if (child->nodetype == LYS_USES) { |
| 4570 | ret = resolve_choice_dflt((struct lys_node_choice *)child, dflt); |
| 4571 | if (ret) { |
| 4572 | return ret; |
| 4573 | } |
| 4574 | } |
| 4575 | |
| 4576 | 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] | 4577 | | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CHOICE))) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4578 | return child; |
| 4579 | } |
| 4580 | } |
| 4581 | |
| 4582 | return NULL; |
| 4583 | } |
| 4584 | |
| 4585 | /** |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4586 | * @brief Resolve uses, apply augments, refines. Logs directly. |
| 4587 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4588 | * @param[in] uses Uses to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4589 | * @param[in,out] unres List of unresolved items. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4590 | * |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 4591 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4592 | */ |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 4593 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4594 | resolve_uses(struct lys_node_uses *uses, struct unres_schema *unres) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4595 | { |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4596 | struct ly_ctx *ctx = uses->module->ctx; /* shortcut */ |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4597 | struct lys_node *node = NULL, *next, *iter, **refine_nodes = NULL; |
Pavol Vican | 55abd33 | 2016-07-12 15:54:49 +0200 | [diff] [blame] | 4598 | struct lys_node *node_aux, *parent, *tmp; |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4599 | struct lys_node_leaflist *llist; |
| 4600 | struct lys_node_leaf *leaf; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 4601 | struct lys_refine *rfn; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4602 | struct lys_restr *must, **old_must; |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4603 | struct lys_iffeature *iff, **old_iff; |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4604 | int i, j, k, rc; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4605 | uint8_t size, *old_size; |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4606 | unsigned int usize, usize1, usize2; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4607 | |
Michal Vasko | 71e1aa8 | 2015-08-12 12:17:51 +0200 | [diff] [blame] | 4608 | assert(uses->grp); |
Radek Krejci | 6ff885d | 2017-01-03 14:06:22 +0100 | [diff] [blame] | 4609 | |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 4610 | /* check that the grouping is resolved (no unresolved uses inside) */ |
| 4611 | assert(!uses->grp->unres_count); |
Michal Vasko | 71e1aa8 | 2015-08-12 12:17:51 +0200 | [diff] [blame] | 4612 | |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4613 | if (!uses->grp->child) { |
| 4614 | /* grouping without children, warning was already displayed */ |
| 4615 | return EXIT_SUCCESS; |
| 4616 | } |
| 4617 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4618 | /* copy the data nodes from grouping into the uses context */ |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 4619 | LY_TREE_FOR(uses->grp->child, node_aux) { |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 4620 | if (node_aux->nodetype & LYS_GROUPING) { |
| 4621 | /* do not instantiate groupings from groupings */ |
| 4622 | continue; |
| 4623 | } |
Radek Krejci | 6ff885d | 2017-01-03 14:06:22 +0100 | [diff] [blame] | 4624 | 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] | 4625 | if (!node) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4626 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, uses->grp->name, "uses"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 4627 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Copying data from grouping failed."); |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4628 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4629 | } |
Pavol Vican | 55abd33 | 2016-07-12 15:54:49 +0200 | [diff] [blame] | 4630 | /* test the name of siblings */ |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 4631 | 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] | 4632 | 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] | 4633 | goto fail; |
Pavol Vican | 55abd33 | 2016-07-12 15:54:49 +0200 | [diff] [blame] | 4634 | } |
| 4635 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4636 | } |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4637 | |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 4638 | /* we managed to copy the grouping, the rest must be possible to resolve */ |
| 4639 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4640 | if (uses->refine_size) { |
| 4641 | refine_nodes = malloc(uses->refine_size * sizeof *refine_nodes); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 4642 | LY_CHECK_ERR_GOTO(!refine_nodes, LOGMEM, fail); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4643 | } |
| 4644 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4645 | /* apply refines */ |
| 4646 | for (i = 0; i < uses->refine_size; i++) { |
| 4647 | rfn = &uses->refine[i]; |
Radek Krejci | e207741 | 2017-01-26 16:03:39 +0100 | [diff] [blame] | 4648 | rc = resolve_descendant_schema_nodeid(rfn->target_name, uses->child, |
| 4649 | LYS_NO_RPC_NOTIF_NODE | LYS_ACTION | LYS_NOTIF, |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 4650 | 0, (const struct lys_node **)&node); |
Michal Vasko | 9bb061b | 2016-02-12 11:00:19 +0100 | [diff] [blame] | 4651 | if (rc || !node) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4652 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine"); |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4653 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4654 | } |
| 4655 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4656 | if (rfn->target_type && !(node->nodetype & rfn->target_type)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4657 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 4658 | LOGVAL(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] | 4659 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4660 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4661 | refine_nodes[i] = node; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4662 | |
| 4663 | /* description on any nodetype */ |
| 4664 | if (rfn->dsc) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4665 | lydict_remove(ctx, node->dsc); |
| 4666 | node->dsc = lydict_insert(ctx, rfn->dsc, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4667 | } |
| 4668 | |
| 4669 | /* reference on any nodetype */ |
| 4670 | if (rfn->ref) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4671 | lydict_remove(ctx, node->ref); |
| 4672 | node->ref = lydict_insert(ctx, rfn->ref, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4673 | } |
| 4674 | |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4675 | /* config on any nodetype, |
| 4676 | * in case of notification or rpc/action, the config is not applicable (there is no config status) */ |
| 4677 | if ((rfn->flags & LYS_CONFIG_MASK) && (node->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4678 | node->flags &= ~LYS_CONFIG_MASK; |
| 4679 | node->flags |= (rfn->flags & LYS_CONFIG_MASK); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4680 | } |
| 4681 | |
| 4682 | /* default value ... */ |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4683 | if (rfn->dflt_size) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4684 | if (node->nodetype == LYS_LEAF) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4685 | /* leaf */ |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4686 | leaf = (struct lys_node_leaf *)node; |
| 4687 | |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 4688 | /* replace default value */ |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4689 | lydict_remove(ctx, leaf->dflt); |
| 4690 | leaf->dflt = lydict_insert(ctx, rfn->dflt[0], 0); |
| 4691 | |
| 4692 | /* check the default value */ |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 4693 | if (unres_schema_add_node(leaf->module, unres, &leaf->type, UNRES_TYPE_DFLT, |
| 4694 | (struct lys_node *)(&leaf->dflt)) == -1) { |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4695 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4696 | } |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4697 | } else if (node->nodetype == LYS_LEAFLIST) { |
| 4698 | /* leaf-list */ |
| 4699 | llist = (struct lys_node_leaflist *)node; |
| 4700 | |
| 4701 | /* remove complete set of defaults in target */ |
Radek Krejci | 542ab14 | 2017-01-23 15:57:08 +0100 | [diff] [blame] | 4702 | for (j = 0; j < llist->dflt_size; j++) { |
| 4703 | lydict_remove(ctx, llist->dflt[j]); |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4704 | } |
| 4705 | free(llist->dflt); |
| 4706 | |
| 4707 | /* copy the default set from refine */ |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 4708 | llist->dflt = malloc(rfn->dflt_size * sizeof *llist->dflt); |
| 4709 | LY_CHECK_ERR_GOTO(!llist->dflt, LOGMEM, fail); |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4710 | llist->dflt_size = rfn->dflt_size; |
Radek Krejci | 542ab14 | 2017-01-23 15:57:08 +0100 | [diff] [blame] | 4711 | for (j = 0; j < llist->dflt_size; j++) { |
| 4712 | llist->dflt[j] = lydict_insert(ctx, rfn->dflt[j], 0); |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4713 | } |
| 4714 | |
| 4715 | /* check default value */ |
Radek Krejci | 542ab14 | 2017-01-23 15:57:08 +0100 | [diff] [blame] | 4716 | for (j = 0; j < llist->dflt_size; j++) { |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 4717 | 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] | 4718 | (struct lys_node *)(&llist->dflt[j])) == -1) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4719 | goto fail; |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4720 | } |
| 4721 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4722 | } |
| 4723 | } |
| 4724 | |
| 4725 | /* mandatory on leaf, anyxml or choice */ |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 4726 | if (rfn->flags & LYS_MAND_MASK) { |
Radek Krejci | bf28583 | 2017-01-26 16:05:41 +0100 | [diff] [blame] | 4727 | /* remove current value */ |
| 4728 | node->flags &= ~LYS_MAND_MASK; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4729 | |
Radek Krejci | bf28583 | 2017-01-26 16:05:41 +0100 | [diff] [blame] | 4730 | /* set new value */ |
| 4731 | node->flags |= (rfn->flags & LYS_MAND_MASK); |
| 4732 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4733 | if (rfn->flags & LYS_MAND_TRUE) { |
| 4734 | /* check if node has default value */ |
| 4735 | if ((node->nodetype & LYS_LEAF) && ((struct lys_node_leaf *)node)->dflt) { |
Radek Krejci | bdcaf24 | 2017-04-19 10:29:47 +0200 | [diff] [blame] | 4736 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, |
| 4737 | "The \"mandatory\" statement is forbidden on leaf with \"default\"."); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4738 | goto fail; |
| 4739 | } |
| 4740 | if ((node->nodetype & LYS_CHOICE) && ((struct lys_node_choice *)node)->dflt) { |
Radek Krejci | bdcaf24 | 2017-04-19 10:29:47 +0200 | [diff] [blame] | 4741 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, |
| 4742 | "The \"mandatory\" statement is forbidden on choices with \"default\"."); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4743 | goto fail; |
| 4744 | } |
| 4745 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4746 | } |
| 4747 | |
| 4748 | /* presence on container */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4749 | if ((node->nodetype & LYS_CONTAINER) && rfn->mod.presence) { |
| 4750 | lydict_remove(ctx, ((struct lys_node_container *)node)->presence); |
| 4751 | ((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] | 4752 | } |
| 4753 | |
| 4754 | /* min/max-elements on list or leaf-list */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4755 | if (node->nodetype == LYS_LIST) { |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 4756 | if (rfn->flags & LYS_RFN_MINSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4757 | ((struct lys_node_list *)node)->min = rfn->mod.list.min; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4758 | } |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 4759 | if (rfn->flags & LYS_RFN_MAXSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4760 | ((struct lys_node_list *)node)->max = rfn->mod.list.max; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4761 | } |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4762 | } else if (node->nodetype == LYS_LEAFLIST) { |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 4763 | if (rfn->flags & LYS_RFN_MINSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4764 | ((struct lys_node_leaflist *)node)->min = rfn->mod.list.min; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4765 | } |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 4766 | if (rfn->flags & LYS_RFN_MAXSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4767 | ((struct lys_node_leaflist *)node)->max = rfn->mod.list.max; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4768 | } |
| 4769 | } |
| 4770 | |
| 4771 | /* must in leaf, leaf-list, list, container or anyxml */ |
| 4772 | if (rfn->must_size) { |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4773 | switch (node->nodetype) { |
| 4774 | case LYS_LEAF: |
| 4775 | old_size = &((struct lys_node_leaf *)node)->must_size; |
| 4776 | old_must = &((struct lys_node_leaf *)node)->must; |
| 4777 | break; |
| 4778 | case LYS_LEAFLIST: |
| 4779 | old_size = &((struct lys_node_leaflist *)node)->must_size; |
| 4780 | old_must = &((struct lys_node_leaflist *)node)->must; |
| 4781 | break; |
| 4782 | case LYS_LIST: |
| 4783 | old_size = &((struct lys_node_list *)node)->must_size; |
| 4784 | old_must = &((struct lys_node_list *)node)->must; |
| 4785 | break; |
| 4786 | case LYS_CONTAINER: |
| 4787 | old_size = &((struct lys_node_container *)node)->must_size; |
| 4788 | old_must = &((struct lys_node_container *)node)->must; |
| 4789 | break; |
| 4790 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4791 | case LYS_ANYDATA: |
| 4792 | old_size = &((struct lys_node_anydata *)node)->must_size; |
| 4793 | old_must = &((struct lys_node_anydata *)node)->must; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4794 | break; |
| 4795 | default: |
| 4796 | LOGINT; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4797 | goto fail; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4798 | } |
| 4799 | |
| 4800 | size = *old_size + rfn->must_size; |
| 4801 | must = realloc(*old_must, size * sizeof *rfn->must); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 4802 | LY_CHECK_ERR_GOTO(!must, LOGMEM, fail); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4803 | for (k = 0, j = *old_size; k < rfn->must_size; k++, j++) { |
Radek Krejci | 7f0164a | 2017-01-25 17:04:06 +0100 | [diff] [blame] | 4804 | must[j].ext_size = rfn->must[k].ext_size; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4805 | lys_ext_dup(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] | 4806 | &must[j].ext, 0, unres); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4807 | must[j].expr = lydict_insert(ctx, rfn->must[k].expr, 0); |
| 4808 | must[j].dsc = lydict_insert(ctx, rfn->must[k].dsc, 0); |
| 4809 | must[j].ref = lydict_insert(ctx, rfn->must[k].ref, 0); |
| 4810 | must[j].eapptag = lydict_insert(ctx, rfn->must[k].eapptag, 0); |
| 4811 | must[j].emsg = lydict_insert(ctx, rfn->must[k].emsg, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4812 | } |
| 4813 | |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4814 | *old_must = must; |
| 4815 | *old_size = size; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 4816 | |
| 4817 | /* check XPath dependencies again */ |
| 4818 | if (unres_schema_add_node(node->module, unres, node, UNRES_XPATH, NULL) == -1) { |
| 4819 | goto fail; |
| 4820 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4821 | } |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4822 | |
| 4823 | /* if-feature in leaf, leaf-list, list, container or anyxml */ |
| 4824 | if (rfn->iffeature_size) { |
| 4825 | old_size = &node->iffeature_size; |
| 4826 | old_iff = &node->iffeature; |
| 4827 | |
| 4828 | size = *old_size + rfn->iffeature_size; |
| 4829 | iff = realloc(*old_iff, size * sizeof *rfn->iffeature); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 4830 | LY_CHECK_ERR_GOTO(!iff, LOGMEM, fail); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4831 | for (k = 0, j = *old_size; k < rfn->iffeature_size; k++, j++) { |
| 4832 | resolve_iffeature_getsizes(&rfn->iffeature[k], &usize1, &usize2); |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4833 | if (usize1) { |
| 4834 | /* there is something to duplicate */ |
| 4835 | /* duplicate compiled expression */ |
| 4836 | usize = (usize1 / 4) + (usize1 % 4) ? 1 : 0; |
| 4837 | iff[j].expr = malloc(usize * sizeof *iff[j].expr); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 4838 | LY_CHECK_ERR_GOTO(!iff[j].expr, LOGMEM, fail); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4839 | 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] | 4840 | |
| 4841 | /* duplicate list of feature pointers */ |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4842 | iff[j].features = malloc(usize2 * sizeof *iff[k].features); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 4843 | LY_CHECK_ERR_GOTO(!iff[j].expr, LOGMEM, fail); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4844 | 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] | 4845 | } |
| 4846 | } |
| 4847 | |
| 4848 | *old_iff = iff; |
| 4849 | *old_size = size; |
| 4850 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4851 | } |
| 4852 | |
| 4853 | /* apply augments */ |
| 4854 | for (i = 0; i < uses->augment_size; i++) { |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 4855 | rc = resolve_augment(&uses->augment[i], uses->child, unres); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4856 | if (rc) { |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4857 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4858 | } |
| 4859 | } |
| 4860 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4861 | /* check refines */ |
| 4862 | for (i = 0; i < uses->refine_size; i++) { |
| 4863 | node = refine_nodes[i]; |
| 4864 | rfn = &uses->refine[i]; |
| 4865 | |
| 4866 | /* config on any nodetype */ |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4867 | if ((rfn->flags & LYS_CONFIG_MASK) && (node->flags & LYS_CONFIG_MASK)) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4868 | 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] | 4869 | if (parent && parent->nodetype != LYS_GROUPING && (parent->flags & LYS_CONFIG_MASK) && |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4870 | ((parent->flags & LYS_CONFIG_MASK) != (rfn->flags & LYS_CONFIG_MASK)) && |
| 4871 | (rfn->flags & LYS_CONFIG_W)) { |
| 4872 | /* setting config true under config false is prohibited */ |
| 4873 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, "config", "refine"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 4874 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4875 | "changing config from 'false' to 'true' is prohibited while " |
| 4876 | "the target's parent is still config 'false'."); |
| 4877 | goto fail; |
| 4878 | } |
| 4879 | |
| 4880 | /* inherit config change to the target children */ |
| 4881 | LY_TREE_DFS_BEGIN(node->child, next, iter) { |
| 4882 | if (rfn->flags & LYS_CONFIG_W) { |
| 4883 | if (iter->flags & LYS_CONFIG_SET) { |
| 4884 | /* config is set explicitely, go to next sibling */ |
| 4885 | next = NULL; |
| 4886 | goto nextsibling; |
| 4887 | } |
| 4888 | } else { /* LYS_CONFIG_R */ |
| 4889 | if ((iter->flags & LYS_CONFIG_SET) && (iter->flags & LYS_CONFIG_W)) { |
| 4890 | /* error - we would have config data under status data */ |
| 4891 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, "config", "refine"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 4892 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4893 | "changing config from 'true' to 'false' is prohibited while the target " |
| 4894 | "has still a children with explicit config 'true'."); |
| 4895 | goto fail; |
| 4896 | } |
| 4897 | } |
| 4898 | /* change config */ |
| 4899 | iter->flags &= ~LYS_CONFIG_MASK; |
| 4900 | iter->flags |= (rfn->flags & LYS_CONFIG_MASK); |
| 4901 | |
| 4902 | /* select next iter - modified LY_TREE_DFS_END */ |
| 4903 | if (iter->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
| 4904 | next = NULL; |
| 4905 | } else { |
| 4906 | next = iter->child; |
| 4907 | } |
| 4908 | nextsibling: |
| 4909 | if (!next) { |
| 4910 | /* try siblings */ |
| 4911 | next = iter->next; |
| 4912 | } |
| 4913 | while (!next) { |
| 4914 | /* parent is already processed, go to its sibling */ |
| 4915 | iter = lys_parent(iter); |
| 4916 | |
| 4917 | /* no siblings, go back through parents */ |
| 4918 | if (iter == node) { |
| 4919 | /* we are done, no next element to process */ |
| 4920 | break; |
| 4921 | } |
| 4922 | next = iter->next; |
| 4923 | } |
| 4924 | } |
| 4925 | } |
| 4926 | |
| 4927 | /* default value */ |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 4928 | if (rfn->dflt_size) { |
| 4929 | if (node->nodetype == LYS_CHOICE) { |
| 4930 | /* choice */ |
| 4931 | ((struct lys_node_choice *)node)->dflt = resolve_choice_dflt((struct lys_node_choice *)node, |
| 4932 | rfn->dflt[0]); |
| 4933 | if (!((struct lys_node_choice *)node)->dflt) { |
| 4934 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->dflt[0], "default"); |
| 4935 | goto fail; |
| 4936 | } |
| 4937 | if (lyp_check_mandatory_choice(node)) { |
| 4938 | goto fail; |
| 4939 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4940 | } |
| 4941 | } |
| 4942 | |
| 4943 | /* min/max-elements on list or leaf-list */ |
Radek Krejci | 2d3c811 | 2017-04-19 10:20:50 +0200 | [diff] [blame] | 4944 | if (node->nodetype == LYS_LIST && ((struct lys_node_list *)node)->max) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4945 | if (((struct lys_node_list *)node)->min > ((struct lys_node_list *)node)->max) { |
Radek Krejci | bdcaf24 | 2017-04-19 10:29:47 +0200 | [diff] [blame] | 4946 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, "Invalid value \"%d\" of \"%s\".", rfn->mod.list.min, "min-elements"); |
| 4947 | LOGVAL(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] | 4948 | goto fail; |
| 4949 | } |
Radek Krejci | 2d3c811 | 2017-04-19 10:20:50 +0200 | [diff] [blame] | 4950 | } else if (node->nodetype == LYS_LEAFLIST && ((struct lys_node_leaflist *)node)->max) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4951 | if (((struct lys_node_leaflist *)node)->min > ((struct lys_node_leaflist *)node)->max) { |
Radek Krejci | bdcaf24 | 2017-04-19 10:29:47 +0200 | [diff] [blame] | 4952 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, "Invalid value \"%d\" of \"%s\".", rfn->mod.list.min, "min-elements"); |
| 4953 | LOGVAL(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] | 4954 | goto fail; |
| 4955 | } |
| 4956 | } |
| 4957 | |
| 4958 | /* additional checks */ |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 4959 | /* default value with mandatory/min-elements */ |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4960 | if (node->nodetype == LYS_LEAFLIST) { |
| 4961 | llist = (struct lys_node_leaflist *)node; |
| 4962 | if (llist->dflt_size && llist->min) { |
Radek Krejci | bdcaf24 | 2017-04-19 10:29:47 +0200 | [diff] [blame] | 4963 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, uses, rfn->dflt_size ? "default" : "min-elements", "refine"); |
| 4964 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4965 | "The \"min-elements\" statement with non-zero value is forbidden on leaf-lists with the \"default\" statement."); |
| 4966 | goto fail; |
| 4967 | } |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 4968 | } else if (node->nodetype == LYS_LEAF) { |
| 4969 | leaf = (struct lys_node_leaf *)node; |
| 4970 | if (leaf->dflt && (leaf->flags & LYS_MAND_TRUE)) { |
Radek Krejci | bdcaf24 | 2017-04-19 10:29:47 +0200 | [diff] [blame] | 4971 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, uses, rfn->dflt_size ? "default" : "mandatory", "refine"); |
| 4972 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 4973 | "The \"mandatory\" statement is forbidden on leafs with the \"default\" statement."); |
| 4974 | goto fail; |
| 4975 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4976 | } |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 4977 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4978 | /* 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] | 4979 | if ((rfn->flags & LYS_MAND_TRUE) || rfn->mod.list.min) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4980 | for (parent = node->parent; |
| 4981 | parent && !(parent->nodetype & (LYS_CHOICE | LYS_GROUPING | LYS_ACTION | LYS_USES)); |
| 4982 | parent = parent->parent) { |
| 4983 | if (parent->nodetype == LYS_CONTAINER && ((struct lys_node_container *)parent)->presence) { |
| 4984 | /* stop also on presence containers */ |
| 4985 | break; |
| 4986 | } |
| 4987 | } |
| 4988 | /* and if it is a choice with the default case, check it for presence of a mandatory node in it */ |
| 4989 | if (parent && parent->nodetype == LYS_CHOICE && ((struct lys_node_choice *)parent)->dflt) { |
| 4990 | if (lyp_check_mandatory_choice(parent)) { |
| 4991 | goto fail; |
| 4992 | } |
| 4993 | } |
| 4994 | } |
| 4995 | } |
| 4996 | free(refine_nodes); |
| 4997 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4998 | return EXIT_SUCCESS; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4999 | |
| 5000 | fail: |
| 5001 | LY_TREE_FOR_SAFE(uses->child, next, iter) { |
| 5002 | lys_node_free(iter, NULL, 0); |
| 5003 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5004 | free(refine_nodes); |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 5005 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5006 | } |
| 5007 | |
Radek Krejci | 83a4bac | 2017-02-07 15:53:04 +0100 | [diff] [blame] | 5008 | void |
| 5009 | resolve_identity_backlink_update(struct lys_ident *der, struct lys_ident *base) |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5010 | { |
| 5011 | int i; |
| 5012 | |
| 5013 | assert(der && base); |
| 5014 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5015 | if (!base->der) { |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5016 | /* create a set for backlinks if it does not exist */ |
| 5017 | base->der = ly_set_new(); |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5018 | } |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5019 | /* store backlink */ |
| 5020 | ly_set_add(base->der, der, LY_SET_OPT_USEASLIST); |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5021 | |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5022 | /* do it recursively */ |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5023 | for (i = 0; i < base->base_size; i++) { |
Radek Krejci | 83a4bac | 2017-02-07 15:53:04 +0100 | [diff] [blame] | 5024 | resolve_identity_backlink_update(der, base->base[i]); |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5025 | } |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5026 | } |
| 5027 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5028 | /** |
| 5029 | * @brief Resolve base identity recursively. Does not log. |
| 5030 | * |
| 5031 | * @param[in] module Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5032 | * @param[in] ident Identity to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5033 | * @param[in] basename Base name of the identity. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5034 | * @param[out] ret Pointer to the resolved identity. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5035 | * |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5036 | * @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] | 5037 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5038 | static int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 5039 | 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] | 5040 | struct unres_schema *unres, struct lys_ident **ret) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5041 | { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5042 | uint32_t i, j; |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5043 | struct lys_ident *base = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5044 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5045 | assert(ret); |
| 5046 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5047 | /* search module */ |
| 5048 | for (i = 0; i < module->ident_size; i++) { |
| 5049 | if (!strcmp(basename, module->ident[i].name)) { |
| 5050 | |
| 5051 | if (!ident) { |
| 5052 | /* just search for type, so do not modify anything, just return |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5053 | * the base identity pointer */ |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5054 | *ret = &module->ident[i]; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5055 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5056 | } |
| 5057 | |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5058 | base = &module->ident[i]; |
| 5059 | goto matchfound; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5060 | } |
| 5061 | } |
| 5062 | |
| 5063 | /* search submodules */ |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5064 | for (j = 0; j < module->inc_size && module->inc[j].submodule; j++) { |
| 5065 | for (i = 0; i < module->inc[j].submodule->ident_size; i++) { |
| 5066 | if (!strcmp(basename, module->inc[j].submodule->ident[i].name)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5067 | |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5068 | if (!ident) { |
| 5069 | *ret = &module->inc[j].submodule->ident[i]; |
| 5070 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5071 | } |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5072 | |
| 5073 | base = &module->inc[j].submodule->ident[i]; |
| 5074 | goto matchfound; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5075 | } |
| 5076 | } |
| 5077 | } |
| 5078 | |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5079 | matchfound: |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5080 | /* we found it somewhere */ |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 5081 | if (base) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5082 | /* is it already completely resolved? */ |
| 5083 | for (i = 0; i < unres->count; i++) { |
Radek Krejci | ba7b1cc | 2016-08-08 13:44:43 +0200 | [diff] [blame] | 5084 | if ((unres->item[i] == base) && (unres->type[i] == UNRES_IDENT)) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5085 | /* identity found, but not yet resolved, so do not return it in *res and try it again later */ |
| 5086 | |
| 5087 | /* simple check for circular reference, |
| 5088 | * the complete check is done as a side effect of using only completely |
| 5089 | * resolved identities (previous check of unres content) */ |
| 5090 | if (ly_strequal((const char *)unres->str_snode[i], ident->name, 1)) { |
| 5091 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, basename, "base"); |
| 5092 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Circular reference of \"%s\" identity.", basename); |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5093 | return -1; |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5094 | } |
| 5095 | |
Radek Krejci | 06f64ed | 2016-08-15 11:07:44 +0200 | [diff] [blame] | 5096 | return EXIT_FAILURE; |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 5097 | } |
| 5098 | } |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5099 | |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 5100 | /* checks done, store the result */ |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5101 | *ret = base; |
Pavol Vican | fdab9f9 | 2016-09-07 15:23:27 +0200 | [diff] [blame] | 5102 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5103 | } |
| 5104 | |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5105 | /* base not found (maybe a forward reference) */ |
| 5106 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5107 | } |
| 5108 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5109 | /** |
| 5110 | * @brief Resolve base identity. Logs directly. |
| 5111 | * |
| 5112 | * @param[in] module Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5113 | * @param[in] ident Identity to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5114 | * @param[in] basename Base name of the identity. |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 5115 | * @param[in] parent Either "type" or "identity". |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5116 | * @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] | 5117 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5118 | * @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] | 5119 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5120 | static int |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5121 | 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] | 5122 | struct lys_type *type, struct unres_schema *unres) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5123 | { |
| 5124 | const char *name; |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5125 | int mod_name_len = 0, rc; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5126 | struct lys_ident *target, **ret; |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 5127 | uint16_t flags; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5128 | struct lys_module *mod; |
| 5129 | |
| 5130 | assert((ident && !type) || (!ident && type)); |
| 5131 | |
| 5132 | if (!type) { |
| 5133 | /* have ident to resolve */ |
| 5134 | ret = ⌖ |
| 5135 | flags = ident->flags; |
| 5136 | mod = ident->module; |
| 5137 | } else { |
| 5138 | /* have type to fill */ |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5139 | ++type->info.ident.count; |
| 5140 | type->info.ident.ref = ly_realloc(type->info.ident.ref, type->info.ident.count * sizeof *type->info.ident.ref); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 5141 | LY_CHECK_ERR_RETURN(!type->info.ident.ref, LOGMEM, -1); |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5142 | |
| 5143 | ret = &type->info.ident.ref[type->info.ident.count - 1]; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5144 | flags = type->parent->flags; |
| 5145 | mod = type->parent->module; |
| 5146 | } |
Michal Vasko | f200600 | 2016-04-21 16:28:15 +0200 | [diff] [blame] | 5147 | *ret = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5148 | |
| 5149 | /* search for the base identity */ |
| 5150 | name = strchr(basename, ':'); |
| 5151 | if (name) { |
| 5152 | /* set name to correct position after colon */ |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 5153 | mod_name_len = name - basename; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5154 | name++; |
| 5155 | |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 5156 | 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] | 5157 | /* prefix refers to the current module, ignore it */ |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 5158 | mod_name_len = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5159 | } |
| 5160 | } else { |
| 5161 | name = basename; |
| 5162 | } |
| 5163 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 5164 | /* get module where to search */ |
| 5165 | module = lys_get_import_module(module, NULL, 0, mod_name_len ? basename : NULL, mod_name_len); |
| 5166 | if (!module) { |
| 5167 | /* identity refers unknown data model */ |
Radek Krejci | 02a0499 | 2016-03-17 16:06:37 +0100 | [diff] [blame] | 5168 | LOGVAL(LYE_INMOD, LY_VLOG_NONE, NULL, basename); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 5169 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5170 | } |
| 5171 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 5172 | /* search in the identified module ... */ |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5173 | rc = resolve_base_ident_sub(module, ident, name, unres, ret); |
| 5174 | if (!rc) { |
| 5175 | assert(*ret); |
| 5176 | |
| 5177 | /* check status */ |
| 5178 | if (lyp_check_status(flags, mod, ident ? ident->name : "of type", |
| 5179 | (*ret)->flags, (*ret)->module, (*ret)->name, NULL)) { |
| 5180 | rc = -1; |
Radek Krejci | 83a4bac | 2017-02-07 15:53:04 +0100 | [diff] [blame] | 5181 | } else if (ident) { |
| 5182 | ident->base[ident->base_size++] = *ret; |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5183 | if (lys_main_module(mod)->implemented) { |
| 5184 | /* in case of the implemented identity, maintain backlinks to it |
| 5185 | * from the base identities to make it available when resolving |
| 5186 | * data with the identity values (not implemented identity is not |
| 5187 | * allowed as an identityref value). */ |
| 5188 | resolve_identity_backlink_update(ident, *ret); |
| 5189 | } |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5190 | } |
| 5191 | } else if (rc == EXIT_FAILURE) { |
| 5192 | LOGVAL(LYE_INRESOLV, LY_VLOG_NONE, NULL, parent, basename); |
Pavol Vican | 053a288 | 2016-09-05 14:40:33 +0200 | [diff] [blame] | 5193 | if (type) { |
| 5194 | --type->info.ident.count; |
| 5195 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5196 | } |
| 5197 | |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5198 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5199 | } |
| 5200 | |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5201 | /* |
| 5202 | * 1 - true (der is derived from base) |
| 5203 | * 0 - false (der is not derived from base) |
| 5204 | */ |
| 5205 | static int |
| 5206 | search_base_identity(struct lys_ident *der, struct lys_ident *base) |
| 5207 | { |
| 5208 | int i; |
| 5209 | |
| 5210 | if (der == base) { |
| 5211 | return 1; |
| 5212 | } else { |
| 5213 | for(i = 0; i < der->base_size; i++) { |
| 5214 | if (search_base_identity(der->base[i], base) == 1) { |
| 5215 | return 1; |
| 5216 | } |
| 5217 | } |
| 5218 | } |
| 5219 | |
| 5220 | return 0; |
| 5221 | } |
| 5222 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5223 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 5224 | * @brief Resolve JSON data format identityref. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5225 | * |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5226 | * @param[in] type Identityref type. |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 5227 | * @param[in] ident_name Identityref name. |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 5228 | * @param[in] node Node where the identityref is being resolved |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5229 | * @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] | 5230 | * |
| 5231 | * @return Pointer to the identity resolvent, NULL on error. |
| 5232 | */ |
Radek Krejci | a52656e | 2015-08-05 13:41:50 +0200 | [diff] [blame] | 5233 | struct lys_ident * |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5234 | 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] | 5235 | { |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5236 | const char *mod_name, *name; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 5237 | int mod_name_len, nam_len, rc, i, j; |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5238 | int make_implemented = 0; |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5239 | unsigned int u; |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5240 | struct lys_ident *der, *cur; |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5241 | struct lys_module *imod = NULL, *m; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5242 | |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5243 | assert(type && ident_name && node && mod); |
Radek Krejci | bb1ce0f | 2016-12-05 13:24:33 +0100 | [diff] [blame] | 5244 | |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5245 | if (!type || (!type->info.ident.count && !type->der) || !ident_name) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5246 | return NULL; |
| 5247 | } |
| 5248 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 5249 | 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] | 5250 | if (rc < 1) { |
Radek Krejci | bb1ce0f | 2016-12-05 13:24:33 +0100 | [diff] [blame] | 5251 | LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, ident_name[-rc], &ident_name[-rc]); |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 5252 | return NULL; |
Michal Vasko | b43bb3c | 2016-03-17 15:00:27 +0100 | [diff] [blame] | 5253 | } else if (rc < (signed)strlen(ident_name)) { |
Radek Krejci | bb1ce0f | 2016-12-05 13:24:33 +0100 | [diff] [blame] | 5254 | LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, ident_name[rc], &ident_name[rc]); |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 5255 | return NULL; |
| 5256 | } |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5257 | |
| 5258 | m = lys_main_module(mod); /* shortcut */ |
| 5259 | if (!mod_name || (!strncmp(mod_name, m->name, mod_name_len) && !m->name[mod_name_len])) { |
| 5260 | /* identity is defined in the same module as node */ |
| 5261 | imod = m; |
| 5262 | } else if (dflt) { |
| 5263 | /* solving identityref in default definition in schema - |
| 5264 | * find the identity's module in the imported modules list to have a correct revision */ |
| 5265 | for (i = 0; i < mod->imp_size; i++) { |
| 5266 | if (!strncmp(mod_name, mod->imp[i].module->name, mod_name_len) && !mod->imp[i].module->name[mod_name_len]) { |
| 5267 | imod = mod->imp[i].module; |
| 5268 | break; |
| 5269 | } |
| 5270 | } |
| 5271 | } else { |
| 5272 | /* solving identityref in data - get the (implemented) module from the context */ |
| 5273 | u = 0; |
| 5274 | while ((imod = (struct lys_module*)ly_ctx_get_module_iter(mod->ctx, &u))) { |
| 5275 | if (imod->implemented && !strncmp(mod_name, imod->name, mod_name_len) && !imod->name[mod_name_len]) { |
| 5276 | break; |
| 5277 | } |
| 5278 | } |
| 5279 | } |
| 5280 | if (!imod) { |
| 5281 | goto fail; |
| 5282 | } |
| 5283 | |
| 5284 | if (dflt && (m != imod || lys_main_module(type->parent->module) != mod)) { |
| 5285 | /* we are solving default statement in schema AND the type is not referencing the same schema, |
| 5286 | * THEN, we may need to make the module with the identity implemented, but only if it really |
| 5287 | * contains the identity */ |
| 5288 | if (!imod->implemented) { |
| 5289 | cur = NULL; |
| 5290 | /* get the identity in the module */ |
| 5291 | for (i = 0; i < imod->ident_size; i++) { |
| 5292 | if (!strcmp(name, imod->ident[i].name)) { |
| 5293 | cur = &imod->ident[i]; |
| 5294 | break; |
| 5295 | } |
| 5296 | } |
| 5297 | if (!cur) { |
| 5298 | /* go through includes */ |
| 5299 | for (j = 0; j < imod->inc_size; j++) { |
| 5300 | for (i = 0; i < imod->inc[j].submodule->ident_size; i++) { |
| 5301 | if (!strcmp(name, imod->inc[j].submodule->ident[i].name)) { |
| 5302 | cur = &imod->inc[j].submodule->ident[i]; |
| 5303 | break; |
| 5304 | } |
| 5305 | } |
| 5306 | } |
| 5307 | if (!cur) { |
| 5308 | goto fail; |
| 5309 | } |
| 5310 | } |
| 5311 | |
| 5312 | /* check that identity is derived from one of the type's base */ |
| 5313 | while (type->der) { |
| 5314 | for (i = 0; i < type->info.ident.count; i++) { |
| 5315 | if (search_base_identity(cur, type->info.ident.ref[i])) { |
| 5316 | /* cur's base matches the type's base */ |
| 5317 | make_implemented = 1; |
| 5318 | goto match; |
| 5319 | } |
| 5320 | } |
| 5321 | type = &type->der->type; |
| 5322 | } |
| 5323 | /* matching base not found */ |
| 5324 | LOGVAL(LYE_SPEC, LY_VLOG_LYD, node, "Identity used as identityref value is not implemented."); |
| 5325 | goto fail; |
| 5326 | } |
Radek Krejci | f32c5f6 | 2016-12-05 09:27:38 +0100 | [diff] [blame] | 5327 | } |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 5328 | |
Radek Krejci | 98a1e2d | 2017-04-26 14:34:52 +0200 | [diff] [blame] | 5329 | /* go through all the derived types of all the bases */ |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5330 | while (type->der) { |
| 5331 | for (i = 0; i < type->info.ident.count; ++i) { |
| 5332 | cur = type->info.ident.ref[i]; |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 5333 | |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5334 | if (cur->der) { |
Radek Krejci | 98a1e2d | 2017-04-26 14:34:52 +0200 | [diff] [blame] | 5335 | /* there are some derived identities */ |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5336 | for (u = 0; u < cur->der->number; u++) { |
| 5337 | der = (struct lys_ident *)cur->der->set.g[u]; /* shortcut */ |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5338 | if (!strcmp(der->name, name) && lys_main_module(der->module) == imod) { |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5339 | /* we have match */ |
| 5340 | cur = der; |
| 5341 | goto match; |
| 5342 | } |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5343 | } |
| 5344 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5345 | } |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5346 | type = &type->der->type; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5347 | } |
| 5348 | |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5349 | fail: |
Radek Krejci | bb1ce0f | 2016-12-05 13:24:33 +0100 | [diff] [blame] | 5350 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYD, node, "identityref", ident_name); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5351 | return NULL; |
Radek Krejci | f1ee2e2 | 2016-08-02 16:36:48 +0200 | [diff] [blame] | 5352 | |
| 5353 | match: |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5354 | for (i = 0; i < cur->iffeature_size; i++) { |
| 5355 | if (!resolve_iffeature(&cur->iffeature[i])) { |
Radek Krejci | bb1ce0f | 2016-12-05 13:24:33 +0100 | [diff] [blame] | 5356 | LOGVAL(LYE_INVAL, LY_VLOG_LYD, node, cur->name, node->schema->name); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 5357 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Identity \"%s\" is disabled by its if-feature condition.", cur->name); |
Radek Krejci | f1ee2e2 | 2016-08-02 16:36:48 +0200 | [diff] [blame] | 5358 | return NULL; |
| 5359 | } |
| 5360 | } |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5361 | if (make_implemented) { |
| 5362 | LOGVRB("Making \"%s\" module implemented because of identityref default value \"%s\" used in the implemented \"%s\" module", |
| 5363 | imod->name, cur->name, mod->name); |
| 5364 | if (lys_set_implemented(imod)) { |
| 5365 | LOGERR(ly_errno, "Setting the module \"%s\" implemented because of used default identity \"%s\" failed.", |
| 5366 | imod->name, cur->name); |
| 5367 | LOGVAL(LYE_SPEC, LY_VLOG_LYD, node, "Identity used as identityref value is not implemented."); |
| 5368 | goto fail; |
| 5369 | } |
| 5370 | } |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5371 | return cur; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5372 | } |
| 5373 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5374 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5375 | * @brief Resolve unresolved uses. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5376 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5377 | * @param[in] uses Uses to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5378 | * @param[in] unres Specific unres item. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5379 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5380 | * @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] | 5381 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5382 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5383 | 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] | 5384 | { |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 5385 | int rc; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5386 | struct lys_node *par_grp; |
Michal Vasko | e91afce | 2015-08-12 12:21:00 +0200 | [diff] [blame] | 5387 | |
Radek Krejci | 6ff885d | 2017-01-03 14:06:22 +0100 | [diff] [blame] | 5388 | /* 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] | 5389 | * in some uses. When we see such a uses, the grouping's unres counter is used to store number of so far |
| 5390 | * unresolved uses. The grouping cannot be used unless this counter is decreased back to 0. To remember |
| 5391 | * 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] | 5392 | for (par_grp = lys_parent((struct lys_node *)uses); par_grp && (par_grp->nodetype != LYS_GROUPING); par_grp = lys_parent(par_grp)); |
Michal Vasko | e91afce | 2015-08-12 12:21:00 +0200 | [diff] [blame] | 5393 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5394 | if (!uses->grp) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 5395 | rc = resolve_uses_schema_nodeid(uses->name, (const struct lys_node *)uses, (const struct lys_node_grp **)&uses->grp); |
| 5396 | if (rc == -1) { |
Michal Vasko | 92981a6 | 2016-10-14 10:25:16 +0200 | [diff] [blame] | 5397 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 5398 | return -1; |
| 5399 | } else if (rc > 0) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5400 | LOGVAL(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] | 5401 | return -1; |
| 5402 | } else if (!uses->grp) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5403 | if (par_grp && !(uses->flags & LYS_USESGRP)) { |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 5404 | if (++((struct lys_node_grp *)par_grp)->unres_count == 0) { |
| 5405 | LOGERR(LY_EINT, "Too many unresolved items (uses) inside a grouping."); |
| 5406 | return -1; |
| 5407 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5408 | uses->flags |= LYS_USESGRP; |
Michal Vasko | 407f1bb | 2015-09-23 15:51:07 +0200 | [diff] [blame] | 5409 | } |
Michal Vasko | 92981a6 | 2016-10-14 10:25:16 +0200 | [diff] [blame] | 5410 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 5411 | return EXIT_FAILURE; |
Michal Vasko | 12e3084 | 2015-08-04 11:54:00 +0200 | [diff] [blame] | 5412 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5413 | } |
| 5414 | |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 5415 | if (uses->grp->unres_count) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5416 | if (par_grp && !(uses->flags & LYS_USESGRP)) { |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 5417 | if (++((struct lys_node_grp *)par_grp)->unres_count == 0) { |
| 5418 | LOGERR(LY_EINT, "Too many unresolved items (uses) inside a grouping."); |
| 5419 | return -1; |
| 5420 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5421 | uses->flags |= LYS_USESGRP; |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 5422 | } else { |
| 5423 | /* instantiate grouping only when it is completely resolved */ |
| 5424 | uses->grp = NULL; |
Michal Vasko | 407f1bb | 2015-09-23 15:51:07 +0200 | [diff] [blame] | 5425 | } |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5426 | return EXIT_FAILURE; |
| 5427 | } |
| 5428 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5429 | rc = resolve_uses(uses, unres); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5430 | if (!rc) { |
| 5431 | /* decrease unres count only if not first try */ |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5432 | if (par_grp && (uses->flags & LYS_USESGRP)) { |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 5433 | assert(((struct lys_node_grp *)par_grp)->unres_count); |
| 5434 | ((struct lys_node_grp *)par_grp)->unres_count--; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5435 | uses->flags &= ~LYS_USESGRP; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5436 | } |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5437 | |
| 5438 | /* check status */ |
Radek Krejci | c655602 | 2016-01-27 15:16:45 +0100 | [diff] [blame] | 5439 | if (lyp_check_status(uses->flags, uses->module, "of uses", |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 5440 | uses->grp->flags, uses->grp->module, uses->grp->name, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5441 | (struct lys_node *)uses)) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5442 | return -1; |
| 5443 | } |
| 5444 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5445 | return EXIT_SUCCESS; |
| 5446 | } |
| 5447 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5448 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5449 | } |
| 5450 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5451 | /** |
Michal Vasko | 9957e59 | 2015-08-17 15:04:09 +0200 | [diff] [blame] | 5452 | * @brief Resolve list keys. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5453 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5454 | * @param[in] list List to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5455 | * @param[in] keys_str Keys node value. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5456 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5457 | * @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] | 5458 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5459 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5460 | resolve_list_keys(struct lys_node_list *list, const char *keys_str) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5461 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5462 | int i, len, rc; |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 5463 | const char *value; |
Radek Krejci | a98048c | 2017-05-24 16:35:48 +0200 | [diff] [blame] | 5464 | char *s = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5465 | |
| 5466 | for (i = 0; i < list->keys_size; ++i) { |
Radek Krejci | dea17dd | 2017-06-02 15:20:43 +0200 | [diff] [blame] | 5467 | assert(keys_str); |
| 5468 | |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 5469 | if (!list->child) { |
| 5470 | /* no child, possible forward reference */ |
| 5471 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, list, "list keys", keys_str); |
| 5472 | return EXIT_FAILURE; |
| 5473 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5474 | /* get the key name */ |
| 5475 | if ((value = strpbrk(keys_str, " \t\n"))) { |
| 5476 | len = value - keys_str; |
| 5477 | while (isspace(value[0])) { |
| 5478 | value++; |
| 5479 | } |
| 5480 | } else { |
| 5481 | len = strlen(keys_str); |
| 5482 | } |
| 5483 | |
Radek Krejci | a98048c | 2017-05-24 16:35:48 +0200 | [diff] [blame] | 5484 | if (list->keys[i]) { |
| 5485 | /* skip already resolved keys */ |
| 5486 | goto next; |
| 5487 | } |
| 5488 | |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 5489 | rc = lys_getnext_data(lys_node_module((struct lys_node *)list), (struct lys_node *)list, keys_str, len, LYS_LEAF, |
| 5490 | (const struct lys_node **)&list->keys[i]); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5491 | if (rc) { |
Radek Krejci | a98048c | 2017-05-24 16:35:48 +0200 | [diff] [blame] | 5492 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, list, "list key", keys_str); |
Michal Vasko | 7a55bea | 2016-05-02 14:51:20 +0200 | [diff] [blame] | 5493 | return EXIT_FAILURE; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5494 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5495 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5496 | if (check_key(list, i, keys_str, len)) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5497 | /* check_key logs */ |
| 5498 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5499 | } |
| 5500 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5501 | /* check status */ |
Radek Krejci | c655602 | 2016-01-27 15:16:45 +0100 | [diff] [blame] | 5502 | if (lyp_check_status(list->flags, list->module, list->name, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5503 | list->keys[i]->flags, list->keys[i]->module, list->keys[i]->name, |
| 5504 | (struct lys_node *)list->keys[i])) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5505 | return -1; |
| 5506 | } |
| 5507 | |
Radek Krejci | a98048c | 2017-05-24 16:35:48 +0200 | [diff] [blame] | 5508 | /* default value - is ignored, keep it but print a warning */ |
| 5509 | if (list->keys[i]->dflt) { |
| 5510 | /* log is not hidden only in case this resolving fails and in such a case |
| 5511 | * we cannot get here |
| 5512 | */ |
| 5513 | assert(*ly_vlog_hide_location()); |
| 5514 | ly_vlog_hide(0); |
| 5515 | LOGWRN("Default value \"%s\" in the list key \"%s\" is ignored. (%s)", list->keys[i]->dflt, |
| 5516 | list->keys[i]->name, s = lys_path((struct lys_node*)list)); |
| 5517 | ly_vlog_hide(1); |
| 5518 | free(s); |
| 5519 | } |
| 5520 | |
| 5521 | next: |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5522 | /* prepare for next iteration */ |
| 5523 | while (value && isspace(value[0])) { |
| 5524 | value++; |
| 5525 | } |
| 5526 | keys_str = value; |
| 5527 | } |
| 5528 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5529 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5530 | } |
| 5531 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5532 | /** |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5533 | * @brief Resolve (check) all must conditions of \p node. |
| 5534 | * Logs directly. |
| 5535 | * |
| 5536 | * @param[in] node Data node with optional must statements. |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5537 | * @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] | 5538 | * |
| 5539 | * @return EXIT_SUCCESS on pass, EXIT_FAILURE on fail, -1 on error. |
| 5540 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5541 | static int |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5542 | resolve_must(struct lyd_node *node, int inout_parent, int ignore_fail) |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5543 | { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 5544 | int node_flags; |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5545 | uint8_t i, must_size; |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5546 | struct lys_node *schema; |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5547 | struct lys_restr *must; |
| 5548 | struct lyxp_set set; |
| 5549 | |
| 5550 | assert(node); |
| 5551 | memset(&set, 0, sizeof set); |
| 5552 | |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5553 | if (inout_parent) { |
| 5554 | for (schema = lys_parent(node->schema); |
| 5555 | schema && (schema->nodetype & (LYS_CHOICE | LYS_CASE | LYS_USES)); |
| 5556 | schema = lys_parent(schema)); |
| 5557 | if (!schema || !(schema->nodetype & (LYS_INPUT | LYS_OUTPUT))) { |
| 5558 | LOGINT; |
| 5559 | return -1; |
| 5560 | } |
| 5561 | must_size = ((struct lys_node_inout *)schema)->must_size; |
| 5562 | must = ((struct lys_node_inout *)schema)->must; |
| 5563 | |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 5564 | node_flags = schema->flags; |
| 5565 | |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5566 | /* context node is the RPC/action */ |
| 5567 | node = node->parent; |
| 5568 | if (!(node->schema->nodetype & (LYS_RPC | LYS_ACTION))) { |
| 5569 | LOGINT; |
| 5570 | return -1; |
| 5571 | } |
| 5572 | } else { |
| 5573 | switch (node->schema->nodetype) { |
| 5574 | case LYS_CONTAINER: |
| 5575 | must_size = ((struct lys_node_container *)node->schema)->must_size; |
| 5576 | must = ((struct lys_node_container *)node->schema)->must; |
| 5577 | break; |
| 5578 | case LYS_LEAF: |
| 5579 | must_size = ((struct lys_node_leaf *)node->schema)->must_size; |
| 5580 | must = ((struct lys_node_leaf *)node->schema)->must; |
| 5581 | break; |
| 5582 | case LYS_LEAFLIST: |
| 5583 | must_size = ((struct lys_node_leaflist *)node->schema)->must_size; |
| 5584 | must = ((struct lys_node_leaflist *)node->schema)->must; |
| 5585 | break; |
| 5586 | case LYS_LIST: |
| 5587 | must_size = ((struct lys_node_list *)node->schema)->must_size; |
| 5588 | must = ((struct lys_node_list *)node->schema)->must; |
| 5589 | break; |
| 5590 | case LYS_ANYXML: |
| 5591 | case LYS_ANYDATA: |
| 5592 | must_size = ((struct lys_node_anydata *)node->schema)->must_size; |
| 5593 | must = ((struct lys_node_anydata *)node->schema)->must; |
| 5594 | break; |
| 5595 | case LYS_NOTIF: |
| 5596 | must_size = ((struct lys_node_notif *)node->schema)->must_size; |
| 5597 | must = ((struct lys_node_notif *)node->schema)->must; |
| 5598 | break; |
| 5599 | default: |
| 5600 | must_size = 0; |
| 5601 | break; |
| 5602 | } |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 5603 | |
| 5604 | node_flags = node->schema->flags; |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5605 | } |
| 5606 | |
| 5607 | for (i = 0; i < must_size; ++i) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5608 | 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] | 5609 | return -1; |
| 5610 | } |
| 5611 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5612 | 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] | 5613 | |
Michal Vasko | 8146d4c | 2016-05-09 15:50:29 +0200 | [diff] [blame] | 5614 | if (!set.val.bool) { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 5615 | if ((ignore_fail == 1) || ((node_flags & LYS_XPATH_DEP) && (ignore_fail == 2))) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5616 | LOGVRB("Must condition \"%s\" not satisfied, but it is not required.", must[i].expr); |
| 5617 | } else { |
| 5618 | LOGVAL(LYE_NOMUST, LY_VLOG_LYD, node, must[i].expr); |
| 5619 | if (must[i].emsg) { |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 5620 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, must[i].emsg); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5621 | } |
| 5622 | if (must[i].eapptag) { |
| 5623 | strncpy(((struct ly_err *)&ly_errno)->apptag, must[i].eapptag, LY_APPTAG_LEN - 1); |
| 5624 | } |
| 5625 | return 1; |
Michal Vasko | 6ac6828 | 2016-04-11 10:56:47 +0200 | [diff] [blame] | 5626 | } |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5627 | } |
| 5628 | } |
| 5629 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5630 | return EXIT_SUCCESS; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5631 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5632 | |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5633 | /** |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5634 | * @brief Resolve (find) when condition schema context node. Does not log. |
| 5635 | * |
| 5636 | * @param[in] schema Schema node with the when condition. |
| 5637 | * @param[out] ctx_snode When schema context node. |
| 5638 | * @param[out] ctx_snode_type Schema context node type. |
| 5639 | */ |
| 5640 | void |
| 5641 | resolve_when_ctx_snode(const struct lys_node *schema, struct lys_node **ctx_snode, enum lyxp_node_type *ctx_snode_type) |
| 5642 | { |
| 5643 | const struct lys_node *sparent; |
| 5644 | |
| 5645 | /* find a not schema-only node */ |
| 5646 | *ctx_snode_type = LYXP_NODE_ELEM; |
| 5647 | while (schema->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE | LYS_AUGMENT | LYS_INPUT | LYS_OUTPUT)) { |
| 5648 | if (schema->nodetype == LYS_AUGMENT) { |
| 5649 | sparent = ((struct lys_node_augment *)schema)->target; |
| 5650 | } else { |
| 5651 | sparent = schema->parent; |
| 5652 | } |
| 5653 | if (!sparent) { |
| 5654 | /* context node is the document root (fake root in our case) */ |
| 5655 | if (schema->flags & LYS_CONFIG_W) { |
| 5656 | *ctx_snode_type = LYXP_NODE_ROOT_CONFIG; |
| 5657 | } else { |
Michal Vasko | b94a5e4 | 2016-09-08 14:01:56 +0200 | [diff] [blame] | 5658 | *ctx_snode_type = LYXP_NODE_ROOT; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5659 | } |
Michal Vasko | 2d2aec1 | 2016-09-08 11:42:50 +0200 | [diff] [blame] | 5660 | /* we need the first top-level sibling, but no uses or groupings */ |
Michal Vasko | b94a5e4 | 2016-09-08 14:01:56 +0200 | [diff] [blame] | 5661 | schema = lys_getnext(NULL, NULL, lys_node_module(schema), 0); |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5662 | break; |
| 5663 | } |
| 5664 | schema = sparent; |
| 5665 | } |
| 5666 | |
| 5667 | *ctx_snode = (struct lys_node *)schema; |
| 5668 | } |
| 5669 | |
| 5670 | /** |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5671 | * @brief Resolve (find) when condition context node. Does not log. |
| 5672 | * |
| 5673 | * @param[in] node Data node, whose conditional definition is being decided. |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5674 | * @param[in] schema Schema node with the when condition. |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5675 | * @param[out] ctx_node Context node. |
| 5676 | * @param[out] ctx_node_type Context node type. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5677 | * |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5678 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5679 | */ |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5680 | static int |
| 5681 | resolve_when_ctx_node(struct lyd_node *node, struct lys_node *schema, struct lyd_node **ctx_node, |
| 5682 | enum lyxp_node_type *ctx_node_type) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5683 | { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5684 | struct lyd_node *parent; |
| 5685 | struct lys_node *sparent; |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5686 | enum lyxp_node_type node_type; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5687 | uint16_t i, data_depth, schema_depth; |
| 5688 | |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5689 | resolve_when_ctx_snode(schema, &schema, &node_type); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5690 | |
Michal Vasko | fe98975 | 2016-09-08 08:47:26 +0200 | [diff] [blame] | 5691 | if (node_type == LYXP_NODE_ELEM) { |
| 5692 | /* standard element context node */ |
| 5693 | for (parent = node, data_depth = 0; parent; parent = parent->parent, ++data_depth); |
| 5694 | for (sparent = schema, schema_depth = 0; |
| 5695 | sparent; |
| 5696 | sparent = (sparent->nodetype == LYS_AUGMENT ? ((struct lys_node_augment *)sparent)->target : sparent->parent)) { |
| 5697 | if (sparent->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_NOTIF | LYS_RPC)) { |
| 5698 | ++schema_depth; |
| 5699 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5700 | } |
Michal Vasko | fe98975 | 2016-09-08 08:47:26 +0200 | [diff] [blame] | 5701 | if (data_depth < schema_depth) { |
| 5702 | return -1; |
| 5703 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5704 | |
Michal Vasko | 956e854 | 2016-08-26 09:43:35 +0200 | [diff] [blame] | 5705 | /* find the corresponding data node */ |
| 5706 | for (i = 0; i < data_depth - schema_depth; ++i) { |
| 5707 | node = node->parent; |
| 5708 | } |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5709 | if (node->schema != schema) { |
| 5710 | return -1; |
| 5711 | } |
Michal Vasko | fe98975 | 2016-09-08 08:47:26 +0200 | [diff] [blame] | 5712 | } else { |
| 5713 | /* root context node */ |
| 5714 | while (node->parent) { |
| 5715 | node = node->parent; |
| 5716 | } |
| 5717 | while (node->prev->next) { |
| 5718 | node = node->prev; |
| 5719 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5720 | } |
| 5721 | |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5722 | *ctx_node = node; |
| 5723 | *ctx_node_type = node_type; |
| 5724 | return EXIT_SUCCESS; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5725 | } |
| 5726 | |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5727 | /** |
| 5728 | * @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] | 5729 | * The context node is adjusted if needed. |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5730 | * |
| 5731 | * @param[in] snode Schema node, whose children instances need to be unlinked. |
| 5732 | * @param[in,out] node Data siblings where to look for the children of \p snode. If it is unlinked, |
| 5733 | * it is moved to point to another sibling still in the original tree. |
| 5734 | * @param[in,out] ctx_node When context node, adjusted if needed. |
| 5735 | * @param[in] ctx_node_type Context node type, just for information to detect invalid situations. |
| 5736 | * @param[out] unlinked_nodes Unlinked siblings. Can be safely appended to \p node afterwards. |
| 5737 | * Ordering may change, but there will be no semantic change. |
| 5738 | * |
| 5739 | * @return EXIT_SUCCESS on success, -1 on error. |
| 5740 | */ |
| 5741 | static int |
| 5742 | resolve_when_unlink_nodes(struct lys_node *snode, struct lyd_node **node, struct lyd_node **ctx_node, |
| 5743 | enum lyxp_node_type ctx_node_type, struct lyd_node **unlinked_nodes) |
| 5744 | { |
| 5745 | struct lyd_node *next, *elem; |
Michal Vasko | eb81fb7 | 2017-02-06 12:11:08 +0100 | [diff] [blame] | 5746 | const struct lys_node *slast; |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5747 | |
| 5748 | switch (snode->nodetype) { |
| 5749 | case LYS_AUGMENT: |
| 5750 | case LYS_USES: |
| 5751 | case LYS_CHOICE: |
| 5752 | case LYS_CASE: |
Michal Vasko | eb81fb7 | 2017-02-06 12:11:08 +0100 | [diff] [blame] | 5753 | slast = NULL; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 5754 | while ((slast = lys_getnext(slast, snode, NULL, LYS_GETNEXT_PARENTUSES))) { |
Michal Vasko | eb81fb7 | 2017-02-06 12:11:08 +0100 | [diff] [blame] | 5755 | if (slast->nodetype & (LYS_ACTION | LYS_NOTIF)) { |
| 5756 | continue; |
| 5757 | } |
| 5758 | |
| 5759 | 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] | 5760 | return -1; |
| 5761 | } |
| 5762 | } |
| 5763 | break; |
| 5764 | case LYS_CONTAINER: |
| 5765 | case LYS_LIST: |
| 5766 | case LYS_LEAF: |
| 5767 | case LYS_LEAFLIST: |
| 5768 | case LYS_ANYXML: |
| 5769 | case LYS_ANYDATA: |
| 5770 | LY_TREE_FOR_SAFE(lyd_first_sibling(*node), next, elem) { |
| 5771 | if (elem->schema == snode) { |
| 5772 | |
| 5773 | if (elem == *ctx_node) { |
| 5774 | /* We are going to unlink our context node! This normally cannot happen, |
| 5775 | * but we use normal top-level data nodes for faking a document root node, |
| 5776 | * so if this is the context node, we just use the next top-level node. |
| 5777 | * Additionally, it can even happen that there are no top-level data nodes left, |
| 5778 | * all were unlinked, so in this case we pass NULL as the context node/data tree, |
| 5779 | * lyxp_eval() can handle this special situation. |
| 5780 | */ |
| 5781 | if (ctx_node_type == LYXP_NODE_ELEM) { |
| 5782 | LOGINT; |
| 5783 | return -1; |
| 5784 | } |
| 5785 | |
| 5786 | if (elem->prev == elem) { |
| 5787 | /* unlinking last top-level element, use an empty data tree */ |
| 5788 | *ctx_node = NULL; |
| 5789 | } else { |
| 5790 | /* in this case just use the previous/last top-level data node */ |
| 5791 | *ctx_node = elem->prev; |
| 5792 | } |
| 5793 | } else if (elem == *node) { |
| 5794 | /* We are going to unlink the currently processed node. This does not matter that |
| 5795 | * much, but we would lose access to the original data tree, so just move our |
| 5796 | * pointer somewhere still inside it. |
| 5797 | */ |
| 5798 | if ((*node)->prev != *node) { |
| 5799 | *node = (*node)->prev; |
| 5800 | } else { |
| 5801 | /* the processed node with sibings were all unlinked, oh well */ |
| 5802 | *node = NULL; |
| 5803 | } |
| 5804 | } |
| 5805 | |
| 5806 | /* temporarily unlink the node */ |
Michal Vasko | 2bce30c | 2017-02-06 12:16:39 +0100 | [diff] [blame] | 5807 | lyd_unlink_internal(elem, 0); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5808 | if (*unlinked_nodes) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5809 | if (lyd_insert_after((*unlinked_nodes)->prev, elem)) { |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5810 | LOGINT; |
| 5811 | return -1; |
| 5812 | } |
| 5813 | } else { |
| 5814 | *unlinked_nodes = elem; |
| 5815 | } |
| 5816 | |
| 5817 | if (snode->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_ANYDATA)) { |
| 5818 | /* there can be only one instance */ |
| 5819 | break; |
| 5820 | } |
| 5821 | } |
| 5822 | } |
| 5823 | break; |
| 5824 | default: |
| 5825 | LOGINT; |
| 5826 | return -1; |
| 5827 | } |
| 5828 | |
| 5829 | return EXIT_SUCCESS; |
| 5830 | } |
| 5831 | |
| 5832 | /** |
| 5833 | * @brief Relink the unlinked nodes back. |
| 5834 | * |
| 5835 | * @param[in] node Data node to link the nodes back to. It can actually be the adjusted context node, |
| 5836 | * we simply need a sibling from the original data tree. |
| 5837 | * @param[in] unlinked_nodes Unlinked nodes to relink to \p node. |
| 5838 | * @param[in] ctx_node_type Context node type to distinguish between \p node being the parent |
| 5839 | * or the sibling of \p unlinked_nodes. |
| 5840 | * |
| 5841 | * @return EXIT_SUCCESS on success, -1 on error. |
| 5842 | */ |
| 5843 | static int |
| 5844 | resolve_when_relink_nodes(struct lyd_node *node, struct lyd_node *unlinked_nodes, enum lyxp_node_type ctx_node_type) |
| 5845 | { |
| 5846 | struct lyd_node *elem; |
| 5847 | |
| 5848 | LY_TREE_FOR_SAFE(unlinked_nodes, unlinked_nodes, elem) { |
Michal Vasko | 2bce30c | 2017-02-06 12:16:39 +0100 | [diff] [blame] | 5849 | lyd_unlink_internal(elem, 0); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5850 | if (ctx_node_type == LYXP_NODE_ELEM) { |
Michal Vasko | 2bce30c | 2017-02-06 12:16:39 +0100 | [diff] [blame] | 5851 | if (lyd_insert_common(node, NULL, elem, 0)) { |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5852 | return -1; |
| 5853 | } |
| 5854 | } else { |
Michal Vasko | 2bce30c | 2017-02-06 12:16:39 +0100 | [diff] [blame] | 5855 | if (lyd_insert_nextto(node, elem, 0, 0)) { |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5856 | return -1; |
| 5857 | } |
| 5858 | } |
| 5859 | } |
| 5860 | |
| 5861 | return EXIT_SUCCESS; |
| 5862 | } |
| 5863 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5864 | int |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5865 | resolve_applies_must(const struct lyd_node *node) |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5866 | { |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5867 | int ret = 0; |
| 5868 | uint8_t must_size; |
| 5869 | struct lys_node *schema, *iter; |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5870 | |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5871 | assert(node); |
| 5872 | |
| 5873 | schema = node->schema; |
| 5874 | |
| 5875 | /* their own must */ |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5876 | switch (schema->nodetype) { |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5877 | case LYS_CONTAINER: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5878 | must_size = ((struct lys_node_container *)schema)->must_size; |
| 5879 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5880 | case LYS_LEAF: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5881 | must_size = ((struct lys_node_leaf *)schema)->must_size; |
| 5882 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5883 | case LYS_LEAFLIST: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5884 | must_size = ((struct lys_node_leaflist *)schema)->must_size; |
| 5885 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5886 | case LYS_LIST: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5887 | must_size = ((struct lys_node_list *)schema)->must_size; |
| 5888 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5889 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 5890 | case LYS_ANYDATA: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5891 | must_size = ((struct lys_node_anydata *)schema)->must_size; |
| 5892 | break; |
| 5893 | case LYS_NOTIF: |
| 5894 | must_size = ((struct lys_node_notif *)schema)->must_size; |
| 5895 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5896 | default: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5897 | must_size = 0; |
| 5898 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5899 | } |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5900 | |
| 5901 | if (must_size) { |
| 5902 | ++ret; |
| 5903 | } |
| 5904 | |
| 5905 | /* schema may be a direct data child of input/output with must (but it must be first, it needs to be evaluated only once) */ |
| 5906 | if (!node->prev->next) { |
| 5907 | for (iter = lys_parent(schema); iter && (iter->nodetype & (LYS_CHOICE | LYS_CASE | LYS_USES)); iter = lys_parent(iter)); |
| 5908 | if (iter && (iter->nodetype & (LYS_INPUT | LYS_OUTPUT))) { |
| 5909 | ret += 0x2; |
| 5910 | } |
| 5911 | } |
| 5912 | |
| 5913 | return ret; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5914 | } |
| 5915 | |
| 5916 | int |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5917 | 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] | 5918 | { |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5919 | const struct lys_node *parent; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5920 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5921 | assert(schema); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5922 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5923 | if (!(schema->nodetype & (LYS_NOTIF | LYS_RPC)) && (((struct lys_node_container *)schema)->when)) { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5924 | return 1; |
| 5925 | } |
| 5926 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5927 | parent = schema; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5928 | goto check_augment; |
| 5929 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5930 | while (parent) { |
| 5931 | /* stop conditions */ |
| 5932 | if (!mode) { |
| 5933 | /* stop on node that can be instantiated in data tree */ |
| 5934 | if (!(parent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) { |
| 5935 | break; |
| 5936 | } |
| 5937 | } else { |
| 5938 | /* stop on the specified node */ |
| 5939 | if (parent == stop) { |
| 5940 | break; |
| 5941 | } |
| 5942 | } |
| 5943 | |
| 5944 | if (((const struct lys_node_uses *)parent)->when) { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5945 | return 1; |
| 5946 | } |
| 5947 | check_augment: |
| 5948 | |
| 5949 | if ((parent->parent && (parent->parent->nodetype == LYS_AUGMENT) && |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5950 | (((const struct lys_node_augment *)parent->parent)->when))) { |
Michal Vasko | e365556 | 2016-08-24 15:56:17 +0200 | [diff] [blame] | 5951 | return 1; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5952 | } |
| 5953 | parent = lys_parent(parent); |
| 5954 | } |
| 5955 | |
| 5956 | return 0; |
| 5957 | } |
| 5958 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5959 | /** |
| 5960 | * @brief Resolve (check) all when conditions relevant for \p node. |
| 5961 | * Logs directly. |
| 5962 | * |
| 5963 | * @param[in] node Data node, whose conditional reference, if such, is being decided. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5964 | * |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5965 | * @return |
| 5966 | * -1 - error, ly_errno is set |
| 5967 | * 0 - true "when" statement |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5968 | * 0, ly_vecode = LYVE_NOWHEN - false "when" statement |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5969 | * 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] | 5970 | */ |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5971 | int |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5972 | resolve_when(struct lyd_node *node, int *result, int ignore_fail) |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5973 | { |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5974 | struct lyd_node *ctx_node = NULL, *unlinked_nodes, *tmp_node; |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5975 | struct lys_node *sparent; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5976 | struct lyxp_set set; |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5977 | enum lyxp_node_type ctx_node_type; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5978 | int rc = 0; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5979 | |
| 5980 | assert(node); |
| 5981 | memset(&set, 0, sizeof set); |
| 5982 | |
Michal Vasko | 78d97e2 | 2017-02-21 09:54:38 +0100 | [diff] [blame] | 5983 | if (!(node->schema->nodetype & (LYS_NOTIF | LYS_RPC | LYS_ACTION)) && (((struct lys_node_container *)node->schema)->when)) { |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5984 | /* make the node dummy for the evaluation */ |
| 5985 | node->validity |= LYD_VAL_INUSE; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5986 | rc = lyxp_eval(((struct lys_node_container *)node->schema)->when->cond, node, LYXP_NODE_ELEM, lyd_node_module(node), |
| 5987 | &set, LYXP_WHEN); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5988 | node->validity &= ~LYD_VAL_INUSE; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5989 | if (rc) { |
| 5990 | if (rc == 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5991 | LOGVAL(LYE_INWHEN, LY_VLOG_LYD, node, ((struct lys_node_container *)node->schema)->when->cond); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5992 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5993 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5994 | } |
| 5995 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5996 | /* set boolean result of the condition */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5997 | 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] | 5998 | if (!set.val.bool) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5999 | node->when_status |= LYD_WHEN_FALSE; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 6000 | if ((ignore_fail == 1) || ((node->schema->flags & LYS_XPATH_DEP) && (ignore_fail == 2))) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6001 | LOGVRB("When condition \"%s\" is not satisfied, but it is not required.", |
| 6002 | ((struct lys_node_container *)node->schema)->when->cond); |
| 6003 | } else { |
| 6004 | LOGVAL(LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_container *)node->schema)->when->cond); |
| 6005 | goto cleanup; |
| 6006 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6007 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6008 | |
| 6009 | /* free xpath set content */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6010 | 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] | 6011 | } |
| 6012 | |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6013 | sparent = node->schema; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6014 | goto check_augment; |
| 6015 | |
| 6016 | /* check when in every schema node that affects node */ |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6017 | while (sparent && (sparent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) { |
| 6018 | if (((struct lys_node_uses *)sparent)->when) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6019 | if (!ctx_node) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6020 | rc = resolve_when_ctx_node(node, sparent, &ctx_node, &ctx_node_type); |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 6021 | if (rc) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6022 | LOGINT; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6023 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6024 | } |
| 6025 | } |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6026 | |
| 6027 | unlinked_nodes = NULL; |
| 6028 | /* we do not want our node pointer to change */ |
| 6029 | tmp_node = node; |
| 6030 | rc = resolve_when_unlink_nodes(sparent, &tmp_node, &ctx_node, ctx_node_type, &unlinked_nodes); |
| 6031 | if (rc) { |
| 6032 | goto cleanup; |
| 6033 | } |
| 6034 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6035 | rc = lyxp_eval(((struct lys_node_uses *)sparent)->when->cond, ctx_node, ctx_node_type, lys_node_module(sparent), |
| 6036 | &set, LYXP_WHEN); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6037 | |
| 6038 | if (unlinked_nodes && ctx_node) { |
| 6039 | if (resolve_when_relink_nodes(ctx_node, unlinked_nodes, ctx_node_type)) { |
| 6040 | rc = -1; |
| 6041 | goto cleanup; |
| 6042 | } |
| 6043 | } |
| 6044 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6045 | if (rc) { |
| 6046 | if (rc == 1) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6047 | LOGVAL(LYE_INWHEN, LY_VLOG_LYD, node, ((struct lys_node_uses *)sparent)->when->cond); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6048 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6049 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6050 | } |
| 6051 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6052 | 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] | 6053 | if (!set.val.bool) { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 6054 | if ((ignore_fail == 1) || ((sparent->flags & LYS_XPATH_DEP) || (ignore_fail == 2))) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6055 | LOGVRB("When condition \"%s\" is not satisfied, but it is not required.", |
| 6056 | ((struct lys_node_uses *)sparent)->when->cond); |
| 6057 | } else { |
Michal Vasko | 2cb18e7 | 2017-03-28 14:46:33 +0200 | [diff] [blame] | 6058 | node->when_status |= LYD_WHEN_FALSE; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6059 | LOGVAL(LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_uses *)sparent)->when->cond); |
| 6060 | goto cleanup; |
| 6061 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6062 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6063 | |
| 6064 | /* free xpath set content */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6065 | 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] | 6066 | } |
| 6067 | |
| 6068 | check_augment: |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6069 | if ((sparent->parent && (sparent->parent->nodetype == LYS_AUGMENT) && (((struct lys_node_augment *)sparent->parent)->when))) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6070 | if (!ctx_node) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6071 | 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] | 6072 | if (rc) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6073 | LOGINT; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6074 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6075 | } |
| 6076 | } |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6077 | |
| 6078 | unlinked_nodes = NULL; |
| 6079 | tmp_node = node; |
| 6080 | rc = resolve_when_unlink_nodes(sparent->parent, &tmp_node, &ctx_node, ctx_node_type, &unlinked_nodes); |
| 6081 | if (rc) { |
| 6082 | goto cleanup; |
| 6083 | } |
| 6084 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6085 | rc = lyxp_eval(((struct lys_node_augment *)sparent->parent)->when->cond, ctx_node, ctx_node_type, |
| 6086 | lys_node_module(sparent->parent), &set, LYXP_WHEN); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6087 | |
| 6088 | /* reconnect nodes, if ctx_node is NULL then all the nodes were unlinked, but linked together, |
| 6089 | * so the tree did not actually change and there is nothing for us to do |
| 6090 | */ |
| 6091 | if (unlinked_nodes && ctx_node) { |
| 6092 | if (resolve_when_relink_nodes(ctx_node, unlinked_nodes, ctx_node_type)) { |
| 6093 | rc = -1; |
| 6094 | goto cleanup; |
| 6095 | } |
| 6096 | } |
| 6097 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6098 | if (rc) { |
| 6099 | if (rc == 1) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6100 | LOGVAL(LYE_INWHEN, LY_VLOG_LYD, node, ((struct lys_node_augment *)sparent->parent)->when->cond); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6101 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6102 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6103 | } |
| 6104 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6105 | 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] | 6106 | if (!set.val.bool) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6107 | node->when_status |= LYD_WHEN_FALSE; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 6108 | if ((ignore_fail == 1) || ((sparent->parent->flags & LYS_XPATH_DEP) && (ignore_fail == 2))) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6109 | LOGVRB("When condition \"%s\" is not satisfied, but it is not required.", |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 6110 | ((struct lys_node_augment *)sparent->parent)->when->cond); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6111 | } else { |
| 6112 | LOGVAL(LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_augment *)sparent->parent)->when->cond); |
| 6113 | goto cleanup; |
| 6114 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6115 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6116 | |
| 6117 | /* free xpath set content */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6118 | 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] | 6119 | } |
| 6120 | |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6121 | sparent = lys_parent(sparent); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6122 | } |
| 6123 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6124 | node->when_status |= LYD_WHEN_TRUE; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6125 | |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6126 | cleanup: |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6127 | /* free xpath set content */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6128 | 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] | 6129 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 6130 | if (result) { |
| 6131 | if (node->when_status & LYD_WHEN_TRUE) { |
| 6132 | *result = 1; |
| 6133 | } else { |
| 6134 | *result = 0; |
| 6135 | } |
| 6136 | } |
| 6137 | |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6138 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6139 | } |
| 6140 | |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6141 | static int |
| 6142 | check_leafref_features(struct lys_type *type) |
| 6143 | { |
| 6144 | struct lys_node *iter; |
| 6145 | struct ly_set *src_parents, *trg_parents, *features; |
| 6146 | unsigned int i, j, size, x; |
| 6147 | int ret = EXIT_SUCCESS; |
| 6148 | |
| 6149 | assert(type->parent); |
| 6150 | |
| 6151 | src_parents = ly_set_new(); |
| 6152 | trg_parents = ly_set_new(); |
| 6153 | features = ly_set_new(); |
| 6154 | |
| 6155 | /* get parents chain of source (leafref) */ |
Radek Krejci | ecda01a | 2017-04-05 15:44:27 +0200 | [diff] [blame] | 6156 | for (iter = (struct lys_node *)type->parent; iter; iter = lys_parent(iter)) { |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6157 | if (iter->nodetype & (LYS_INPUT | LYS_OUTPUT)) { |
| 6158 | continue; |
| 6159 | } |
| 6160 | ly_set_add(src_parents, iter, LY_SET_OPT_USEASLIST); |
| 6161 | } |
| 6162 | /* get parents chain of target */ |
Radek Krejci | ecda01a | 2017-04-05 15:44:27 +0200 | [diff] [blame] | 6163 | for (iter = (struct lys_node *)type->info.lref.target; iter; iter = lys_parent(iter)) { |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6164 | if (iter->nodetype & (LYS_INPUT | LYS_OUTPUT)) { |
| 6165 | continue; |
| 6166 | } |
| 6167 | ly_set_add(trg_parents, iter, LY_SET_OPT_USEASLIST); |
| 6168 | } |
| 6169 | |
| 6170 | /* compare the features used in if-feature statements in the rest of both |
| 6171 | * chains of parents. The set of features used for target must be a subset |
| 6172 | * of features used for the leafref. This is not a perfect, we should compare |
| 6173 | * the truth tables but it could require too much resources, so we simplify that */ |
| 6174 | for (i = 0; i < src_parents->number; i++) { |
| 6175 | iter = src_parents->set.s[i]; /* shortcut */ |
| 6176 | if (!iter->iffeature_size) { |
| 6177 | continue; |
| 6178 | } |
| 6179 | for (j = 0; j < iter->iffeature_size; j++) { |
| 6180 | resolve_iffeature_getsizes(&iter->iffeature[j], NULL, &size); |
| 6181 | for (; size; size--) { |
| 6182 | if (!iter->iffeature[j].features[size - 1]) { |
| 6183 | /* not yet resolved feature, postpone this check */ |
| 6184 | ret = EXIT_FAILURE; |
| 6185 | goto cleanup; |
| 6186 | } |
| 6187 | ly_set_add(features, iter->iffeature[j].features[size - 1], 0); |
| 6188 | } |
| 6189 | } |
| 6190 | } |
| 6191 | x = features->number; |
| 6192 | for (i = 0; i < trg_parents->number; i++) { |
| 6193 | iter = trg_parents->set.s[i]; /* shortcut */ |
| 6194 | if (!iter->iffeature_size) { |
| 6195 | continue; |
| 6196 | } |
| 6197 | for (j = 0; j < iter->iffeature_size; j++) { |
| 6198 | resolve_iffeature_getsizes(&iter->iffeature[j], NULL, &size); |
| 6199 | for (; size; size--) { |
| 6200 | if (!iter->iffeature[j].features[size - 1]) { |
| 6201 | /* not yet resolved feature, postpone this check */ |
| 6202 | ret = EXIT_FAILURE; |
| 6203 | goto cleanup; |
| 6204 | } |
| 6205 | if ((unsigned int)ly_set_add(features, iter->iffeature[j].features[size - 1], 0) >= x) { |
| 6206 | /* the feature is not present in features set of target's parents chain */ |
| 6207 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, type->parent, "leafref", type->info.lref.path); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 6208 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6209 | "Leafref is not conditional based on \"%s\" feature as its target.", |
| 6210 | iter->iffeature[j].features[size - 1]->name); |
| 6211 | ret = -1; |
| 6212 | goto cleanup; |
| 6213 | } |
| 6214 | } |
| 6215 | } |
| 6216 | } |
| 6217 | |
| 6218 | cleanup: |
| 6219 | ly_set_free(features); |
| 6220 | ly_set_free(src_parents); |
| 6221 | ly_set_free(trg_parents); |
| 6222 | |
| 6223 | return ret; |
| 6224 | } |
| 6225 | |
Michal Vasko | c5c55ca | 2017-06-30 13:15:18 +0200 | [diff] [blame] | 6226 | static int |
| 6227 | check_type_union_leafref(struct lys_type *type) |
| 6228 | { |
| 6229 | uint8_t i; |
| 6230 | |
| 6231 | if ((type->base == LY_TYPE_UNION) && type->info.uni.count) { |
| 6232 | /* go through unions and look for leafref */ |
| 6233 | for (i = 0; i < type->info.uni.count; ++i) { |
| 6234 | switch (type->info.uni.types[i].base) { |
| 6235 | case LY_TYPE_LEAFREF: |
| 6236 | return 1; |
| 6237 | case LY_TYPE_UNION: |
| 6238 | if (check_type_union_leafref(&type->info.uni.types[i])) { |
| 6239 | return 1; |
| 6240 | } |
| 6241 | break; |
| 6242 | default: |
| 6243 | break; |
| 6244 | } |
| 6245 | } |
| 6246 | |
| 6247 | return 0; |
| 6248 | } |
| 6249 | |
| 6250 | /* just inherit the flag value */ |
| 6251 | return type->der->has_union_leafref; |
| 6252 | } |
| 6253 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6254 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6255 | * @brief Resolve a single unres schema item. Logs indirectly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6256 | * |
| 6257 | * @param[in] mod Main module. |
| 6258 | * @param[in] item Item to resolve. Type determined by \p type. |
| 6259 | * @param[in] type Type of the unresolved item. |
| 6260 | * @param[in] str_snode String, a schema node, or NULL. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6261 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 769f803 | 2017-01-24 13:11:55 +0100 | [diff] [blame] | 6262 | * @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] | 6263 | * |
| 6264 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 6265 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6266 | static int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 6267 | resolve_unres_schema_item(struct lys_module *mod, void *item, enum UNRES_ITEM type, void *str_snode, |
Michal Vasko | 769f803 | 2017-01-24 13:11:55 +0100 | [diff] [blame] | 6268 | struct unres_schema *unres, int final_fail) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6269 | { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6270 | /* has_str - whether the str_snode is a string in a dictionary that needs to be freed */ |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 6271 | int rc = -1, has_str = 0, parent_type = 0, i, k; |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6272 | unsigned int j; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6273 | struct lys_node *root, *next, *node, *par_grp; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6274 | const char *expr; |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 6275 | uint8_t *u; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6276 | |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6277 | struct ly_set *refs, *procs; |
| 6278 | struct lys_feature *ref, *feat; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6279 | struct lys_ident *ident; |
| 6280 | struct lys_type *stype; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6281 | struct lys_node_choice *choic; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6282 | struct lyxml_elem *yin; |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6283 | struct yang_type *yang; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6284 | struct unres_list_uniq *unique_info; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6285 | struct unres_iffeat_data *iff_data; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6286 | struct unres_ext *ext_data; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6287 | struct lys_ext_instance *ext, **extlist; |
| 6288 | struct lyext_plugin *eplugin; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6289 | |
| 6290 | switch (type) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6291 | case UNRES_IDENT: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6292 | expr = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 6293 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6294 | ident = item; |
| 6295 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6296 | rc = resolve_base_ident(mod, ident, expr, "identity", NULL, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6297 | break; |
| 6298 | case UNRES_TYPE_IDENTREF: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6299 | expr = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 6300 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6301 | stype = item; |
| 6302 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6303 | rc = resolve_base_ident(mod, NULL, expr, "type", stype, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6304 | break; |
| 6305 | case UNRES_TYPE_LEAFREF: |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 6306 | node = str_snode; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6307 | stype = item; |
| 6308 | |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 6309 | rc = resolve_schema_leafref(stype->info.lref.path, node, (const struct lys_node **)&stype->info.lref.target); |
| 6310 | if (!rc) { |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 6311 | assert(stype->info.lref.target); |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6312 | /* check if leafref and its target are under a common if-features */ |
| 6313 | rc = check_leafref_features(stype); |
| 6314 | if (rc) { |
| 6315 | break; |
| 6316 | } |
| 6317 | |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 6318 | if (lys_node_module(node)->implemented) { |
| 6319 | /* make all the modules on the path implemented */ |
| 6320 | for (next = (struct lys_node *)stype->info.lref.target; next; next = lys_parent(next)) { |
| 6321 | if (!lys_node_module(next)->implemented) { |
| 6322 | if (lys_set_implemented(lys_node_module(next))) { |
| 6323 | rc = -1; |
| 6324 | break; |
| 6325 | } |
| 6326 | } |
| 6327 | } |
| 6328 | if (next) { |
| 6329 | break; |
| 6330 | } |
| 6331 | |
| 6332 | /* store the backlink from leafref target */ |
| 6333 | if (lys_leaf_add_leafref_target(stype->info.lref.target, (struct lys_node *)stype->parent)) { |
| 6334 | rc = -1; |
| 6335 | } |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 6336 | } |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 6337 | } |
| 6338 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6339 | break; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 6340 | case UNRES_TYPE_DER_EXT: |
| 6341 | parent_type++; |
Radek Krejci | 3d679d7 | 2017-08-01 10:44:37 +0200 | [diff] [blame] | 6342 | /* falls through */ |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 6343 | case UNRES_TYPE_DER_TPDF: |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 6344 | parent_type++; |
Radek Krejci | 3d679d7 | 2017-08-01 10:44:37 +0200 | [diff] [blame] | 6345 | /* falls through */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6346 | case UNRES_TYPE_DER: |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6347 | /* parent */ |
| 6348 | node = str_snode; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6349 | stype = item; |
| 6350 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6351 | /* HACK type->der is temporarily unparsed type statement */ |
| 6352 | yin = (struct lyxml_elem *)stype->der; |
| 6353 | stype->der = NULL; |
| 6354 | |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6355 | if (yin->flags & LY_YANG_STRUCTURE_FLAG) { |
| 6356 | yang = (struct yang_type *)yin; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 6357 | rc = yang_check_type(mod, node, yang, stype, parent_type, unres); |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6358 | |
| 6359 | if (rc) { |
Pavol Vican | 8bd72e4 | 2016-08-29 09:53:05 +0200 | [diff] [blame] | 6360 | /* may try again later */ |
| 6361 | stype->der = (struct lys_tpdf *)yang; |
Pavol Vican | d01d8ae | 2016-03-01 10:45:59 +0100 | [diff] [blame] | 6362 | } else { |
| 6363 | /* we need to always be able to free this, it's safe only in this case */ |
Pavol Vican | 5f0316a | 2016-04-05 21:21:11 +0200 | [diff] [blame] | 6364 | lydict_remove(mod->ctx, yang->name); |
Pavol Vican | d01d8ae | 2016-03-01 10:45:59 +0100 | [diff] [blame] | 6365 | free(yang); |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6366 | } |
| 6367 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6368 | } else { |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 6369 | rc = fill_yin_type(mod, node, yin, stype, parent_type, unres); |
Radek Krejci | 63fc096 | 2017-02-15 13:20:18 +0100 | [diff] [blame] | 6370 | if (!rc || rc == -1) { |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6371 | /* we need to always be able to free this, it's safe only in this case */ |
| 6372 | lyxml_free(mod->ctx, yin); |
| 6373 | } else { |
| 6374 | /* may try again later, put all back how it was */ |
| 6375 | stype->der = (struct lys_tpdf *)yin; |
| 6376 | } |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6377 | } |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 6378 | if (rc == EXIT_SUCCESS) { |
Radek Krejci | 2c2fce8 | 2016-08-01 13:52:26 +0200 | [diff] [blame] | 6379 | /* it does not make sense to have leaf-list of empty type */ |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 6380 | if (!parent_type && node->nodetype == LYS_LEAFLIST && stype->base == LY_TYPE_EMPTY) { |
Radek Krejci | 2c2fce8 | 2016-08-01 13:52:26 +0200 | [diff] [blame] | 6381 | LOGWRN("The leaf-list \"%s\" is of \"empty\" type, which does not make sense.", node->name); |
| 6382 | } |
Michal Vasko | c5c55ca | 2017-06-30 13:15:18 +0200 | [diff] [blame] | 6383 | |
| 6384 | if ((type == UNRES_TYPE_DER_TPDF) && (stype->base == LY_TYPE_UNION)) { |
| 6385 | /* fill typedef union leafref flag */ |
| 6386 | ((struct lys_tpdf *)stype->parent)->has_union_leafref = check_type_union_leafref(stype); |
| 6387 | } else if ((type == UNRES_TYPE_DER) && stype->der->has_union_leafref) { |
| 6388 | /* copy the type in case it has union leafref flag */ |
| 6389 | if (lys_copy_union_leafrefs(mod, node, stype, NULL, unres)) { |
| 6390 | LOGERR(LY_EINT, "Failed to duplicate type."); |
| 6391 | return -1; |
| 6392 | } |
| 6393 | } |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6394 | } else if (rc == EXIT_FAILURE && stype->base != LY_TYPE_ERR) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 6395 | /* forward reference - in case the type is in grouping, we have to make the grouping unusable |
| 6396 | * 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] | 6397 | * grouping. The grouping cannot be used unless the unres counter is 0. |
| 6398 | * To remember that the grouping already increased the counter, the LY_TYPE_ERR is used as value |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 6399 | * of the type's base member. */ |
| 6400 | for (par_grp = node; par_grp && (par_grp->nodetype != LYS_GROUPING); par_grp = lys_parent(par_grp)); |
| 6401 | if (par_grp) { |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 6402 | if (++((struct lys_node_grp *)par_grp)->unres_count == 0) { |
| 6403 | LOGERR(LY_EINT, "Too many unresolved items (type) inside a grouping."); |
| 6404 | return -1; |
| 6405 | } |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6406 | stype->base = LY_TYPE_ERR; |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 6407 | } |
Radek Krejci | 2c2fce8 | 2016-08-01 13:52:26 +0200 | [diff] [blame] | 6408 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6409 | break; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6410 | case UNRES_IFFEAT: |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6411 | iff_data = str_snode; |
| 6412 | 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] | 6413 | if (!rc) { |
| 6414 | /* success */ |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 6415 | if (iff_data->infeature) { |
| 6416 | /* store backlink into the target feature to allow reverse changes in case of changing feature status */ |
| 6417 | feat = *((struct lys_feature **)item); |
| 6418 | if (!feat->depfeatures) { |
| 6419 | feat->depfeatures = ly_set_new(); |
| 6420 | } |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 6421 | ly_set_add(feat->depfeatures, iff_data->node, LY_SET_OPT_USEASLIST); |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 6422 | } |
| 6423 | /* cleanup temporary data */ |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6424 | lydict_remove(mod->ctx, iff_data->fname); |
| 6425 | free(iff_data); |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6426 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6427 | break; |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6428 | case UNRES_FEATURE: |
| 6429 | feat = (struct lys_feature *)item; |
| 6430 | |
| 6431 | if (feat->iffeature_size) { |
| 6432 | refs = ly_set_new(); |
| 6433 | procs = ly_set_new(); |
| 6434 | ly_set_add(procs, feat, 0); |
| 6435 | |
| 6436 | while (procs->number) { |
| 6437 | ref = procs->set.g[procs->number - 1]; |
| 6438 | ly_set_rm_index(procs, procs->number - 1); |
| 6439 | |
| 6440 | for (i = 0; i < ref->iffeature_size; i++) { |
| 6441 | resolve_iffeature_getsizes(&ref->iffeature[i], NULL, &j); |
| 6442 | for (; j > 0 ; j--) { |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6443 | if (ref->iffeature[i].features[j - 1]) { |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6444 | if (ref->iffeature[i].features[j - 1] == feat) { |
| 6445 | LOGVAL(LYE_CIRC_FEATURES, LY_VLOG_NONE, NULL, feat->name); |
| 6446 | goto featurecheckdone; |
| 6447 | } |
| 6448 | |
| 6449 | if (ref->iffeature[i].features[j - 1]->iffeature_size) { |
| 6450 | k = refs->number; |
| 6451 | if (ly_set_add(refs, ref->iffeature[i].features[j - 1], 0) == k) { |
| 6452 | /* not yet seen feature, add it for processing */ |
| 6453 | ly_set_add(procs, ref->iffeature[i].features[j - 1], 0); |
| 6454 | } |
| 6455 | } |
| 6456 | } else { |
| 6457 | /* forward reference */ |
| 6458 | rc = EXIT_FAILURE; |
| 6459 | goto featurecheckdone; |
| 6460 | } |
| 6461 | } |
| 6462 | |
| 6463 | } |
| 6464 | } |
| 6465 | rc = EXIT_SUCCESS; |
| 6466 | |
| 6467 | featurecheckdone: |
| 6468 | ly_set_free(refs); |
| 6469 | ly_set_free(procs); |
| 6470 | } |
| 6471 | |
| 6472 | break; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6473 | case UNRES_USES: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6474 | rc = resolve_unres_schema_uses(item, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6475 | break; |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 6476 | case UNRES_TYPEDEF_DFLT: |
| 6477 | parent_type++; |
Radek Krejci | 3d679d7 | 2017-08-01 10:44:37 +0200 | [diff] [blame] | 6478 | /* falls through */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6479 | case UNRES_TYPE_DFLT: |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6480 | stype = item; |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 6481 | rc = check_default(stype, (const char **)str_snode, mod, parent_type); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6482 | break; |
| 6483 | case UNRES_CHOICE_DFLT: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6484 | expr = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 6485 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6486 | choic = item; |
| 6487 | |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 6488 | if (!choic->dflt) { |
| 6489 | choic->dflt = resolve_choice_dflt(choic, expr); |
| 6490 | } |
Michal Vasko | 7955b36 | 2015-09-04 14:18:15 +0200 | [diff] [blame] | 6491 | if (choic->dflt) { |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 6492 | rc = lyp_check_mandatory_choice((struct lys_node *)choic); |
Michal Vasko | 7955b36 | 2015-09-04 14:18:15 +0200 | [diff] [blame] | 6493 | } else { |
| 6494 | rc = EXIT_FAILURE; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6495 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6496 | break; |
| 6497 | case UNRES_LIST_KEYS: |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 6498 | rc = resolve_list_keys(item, ((struct lys_node_list *)item)->keys_str); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6499 | break; |
| 6500 | case UNRES_LIST_UNIQ: |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6501 | unique_info = (struct unres_list_uniq *)item; |
| 6502 | 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] | 6503 | break; |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 6504 | case UNRES_AUGMENT: |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 6505 | rc = resolve_augment(item, NULL, unres); |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 6506 | break; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6507 | case UNRES_XPATH: |
| 6508 | node = (struct lys_node *)item; |
Michal Vasko | 769f803 | 2017-01-24 13:11:55 +0100 | [diff] [blame] | 6509 | rc = lys_check_xpath(node, 1, final_fail); |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6510 | break; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6511 | case UNRES_EXT: |
| 6512 | ext_data = (struct unres_ext *)str_snode; |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 6513 | extlist = &(*(struct lys_ext_instance ***)item)[ext_data->ext_index]; |
Radek Krejci | a7db970 | 2017-01-20 12:55:14 +0100 | [diff] [blame] | 6514 | rc = resolve_extension(ext_data, extlist, unres); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6515 | if (!rc) { |
Radek Krejci | 79685c9 | 2017-02-17 10:49:43 +0100 | [diff] [blame] | 6516 | /* success */ |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6517 | /* is there a callback to be done to finalize the extension? */ |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 6518 | eplugin = extlist[0]->def->plugin; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6519 | if (eplugin) { |
| 6520 | if (eplugin->check_result || (eplugin->flags & LYEXT_OPT_INHERIT)) { |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 6521 | u = malloc(sizeof *u); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 6522 | LY_CHECK_ERR_RETURN(!u, LOGMEM, -1); |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 6523 | (*u) = ext_data->ext_index; |
Radek Krejci | b08bc17 | 2017-02-27 13:17:14 +0100 | [diff] [blame] | 6524 | if (unres_schema_add_node(mod, unres, item, UNRES_EXT_FINALIZE, (struct lys_node *)u) == -1) { |
| 6525 | /* something really bad happend since the extension finalization is not actually |
| 6526 | * being resolved while adding into unres, so something more serious with the unres |
| 6527 | * list itself must happened */ |
| 6528 | return -1; |
| 6529 | } |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6530 | } |
| 6531 | } |
Radek Krejci | 79685c9 | 2017-02-17 10:49:43 +0100 | [diff] [blame] | 6532 | } |
| 6533 | if (!rc || rc == -1) { |
| 6534 | /* cleanup on success or fatal error */ |
| 6535 | if (ext_data->datatype == LYS_IN_YIN) { |
| 6536 | /* YIN */ |
| 6537 | lyxml_free(mod->ctx, ext_data->data.yin); |
| 6538 | } else { |
PavolVican | db0e817 | 2017-02-20 00:46:09 +0100 | [diff] [blame] | 6539 | /* YANG */ |
| 6540 | yang_free_ext_data(ext_data->data.yang); |
Radek Krejci | 79685c9 | 2017-02-17 10:49:43 +0100 | [diff] [blame] | 6541 | } |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 6542 | free(ext_data); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6543 | } |
| 6544 | break; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6545 | case UNRES_EXT_FINALIZE: |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 6546 | u = (uint8_t *)str_snode; |
| 6547 | ext = (*(struct lys_ext_instance ***)item)[*u]; |
| 6548 | free(u); |
| 6549 | |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6550 | eplugin = ext->def->plugin; |
| 6551 | |
| 6552 | /* inherit */ |
| 6553 | if ((eplugin->flags & LYEXT_OPT_INHERIT) && (ext->parent_type == LYEXT_PAR_NODE)) { |
| 6554 | root = (struct lys_node *)ext->parent; |
| 6555 | if (!(root->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) { |
| 6556 | LY_TREE_DFS_BEGIN(root->child, next, node) { |
| 6557 | /* first, check if the node already contain instance of the same extension, |
| 6558 | * in such a case we won't inherit. In case the node was actually defined as |
| 6559 | * augment data, we are supposed to check the same way also the augment node itself */ |
| 6560 | if (lys_ext_instance_presence(ext->def, node->ext, node->ext_size) != -1) { |
| 6561 | goto inherit_dfs_sibling; |
| 6562 | } else if (node->parent != root && node->parent->nodetype == LYS_AUGMENT && |
| 6563 | lys_ext_instance_presence(ext->def, node->parent->ext, node->parent->ext_size) != -1) { |
| 6564 | goto inherit_dfs_sibling; |
| 6565 | } |
| 6566 | |
| 6567 | if (eplugin->check_inherit) { |
| 6568 | /* we have a callback to check the inheritance, use it */ |
| 6569 | switch ((rc = (*eplugin->check_inherit)(ext, node))) { |
| 6570 | case 0: |
| 6571 | /* yes - continue with the inheriting code */ |
| 6572 | break; |
| 6573 | case 1: |
| 6574 | /* no - continue with the node's sibling */ |
| 6575 | goto inherit_dfs_sibling; |
| 6576 | case 2: |
| 6577 | /* no, but continue with the children, just skip the inheriting code for this node */ |
| 6578 | goto inherit_dfs_child; |
| 6579 | default: |
| 6580 | LOGERR(LY_EINT, "Plugin's (%s:%s) check_inherit callback returns invalid value (%d),", |
| 6581 | ext->def->module->name, ext->def->name, rc); |
| 6582 | } |
| 6583 | } |
| 6584 | |
| 6585 | /* inherit the extension */ |
| 6586 | extlist = realloc(node->ext, (node->ext_size + 1) * sizeof *node->ext); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 6587 | LY_CHECK_ERR_RETURN(!extlist, LOGMEM, -1); |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6588 | extlist[node->ext_size] = malloc(sizeof **extlist); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 6589 | LY_CHECK_ERR_RETURN(!extlist[node->ext_size], LOGMEM; node->ext = extlist, -1); |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6590 | memcpy(extlist[node->ext_size], ext, sizeof *ext); |
| 6591 | extlist[node->ext_size]->flags |= LYEXT_OPT_INHERIT; |
| 6592 | |
| 6593 | node->ext = extlist; |
| 6594 | node->ext_size++; |
| 6595 | |
| 6596 | inherit_dfs_child: |
| 6597 | /* modification of - select element for the next run - children first */ |
| 6598 | if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
| 6599 | next = NULL; |
| 6600 | } else { |
| 6601 | next = node->child; |
| 6602 | } |
| 6603 | if (!next) { |
| 6604 | inherit_dfs_sibling: |
| 6605 | /* no children, try siblings */ |
| 6606 | next = node->next; |
| 6607 | } |
| 6608 | while (!next) { |
| 6609 | /* go to the parent */ |
| 6610 | node = lys_parent(node); |
| 6611 | |
| 6612 | /* we are done if we are back in the root (the starter's parent */ |
| 6613 | if (node == root) { |
| 6614 | break; |
| 6615 | } |
| 6616 | |
| 6617 | /* parent is already processed, go to its sibling */ |
| 6618 | next = node->next; |
| 6619 | } |
| 6620 | } |
| 6621 | } |
| 6622 | } |
| 6623 | |
| 6624 | /* final check */ |
| 6625 | if (eplugin->check_result) { |
| 6626 | if ((*eplugin->check_result)(ext)) { |
Radek Krejci | 2c121b3 | 2017-02-24 10:03:16 +0100 | [diff] [blame] | 6627 | ly_errno = LY_EEXT; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6628 | return -1; |
| 6629 | } |
| 6630 | } |
| 6631 | |
| 6632 | rc = 0; |
| 6633 | break; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6634 | default: |
| 6635 | LOGINT; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6636 | break; |
| 6637 | } |
| 6638 | |
Radek Krejci | 54081ce | 2016-08-12 15:21:47 +0200 | [diff] [blame] | 6639 | if (has_str && !rc) { |
| 6640 | /* the string is no more needed in case of success. |
| 6641 | * In case of forward reference, we will try to resolve the string later */ |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 6642 | lydict_remove(mod->ctx, str_snode); |
| 6643 | } |
| 6644 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6645 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6646 | } |
| 6647 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6648 | /* logs directly */ |
| 6649 | static void |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6650 | 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] | 6651 | { |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 6652 | struct lyxml_elem *xml; |
| 6653 | struct lyxml_attr *attr; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6654 | struct unres_iffeat_data *iff_data; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6655 | const char *name = NULL; |
| 6656 | struct unres_ext *extinfo; |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 6657 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6658 | switch (type) { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6659 | case UNRES_IDENT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6660 | 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] | 6661 | break; |
| 6662 | case UNRES_TYPE_IDENTREF: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6663 | 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] | 6664 | break; |
| 6665 | case UNRES_TYPE_LEAFREF: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6666 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "leafref", |
| 6667 | ((struct lys_type *)item)->info.lref.path); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6668 | break; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 6669 | case UNRES_TYPE_DER_EXT: |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 6670 | case UNRES_TYPE_DER_TPDF: |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6671 | case UNRES_TYPE_DER: |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 6672 | xml = (struct lyxml_elem *)((struct lys_type *)item)->der; |
| 6673 | if (xml->flags & LY_YANG_STRUCTURE_FLAG) { |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6674 | name = ((struct yang_type *)xml)->name; |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 6675 | } else { |
| 6676 | LY_TREE_FOR(xml->attr, attr) { |
| 6677 | if ((attr->type == LYXML_ATTR_STD) && !strcmp(attr->name, "name")) { |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6678 | name = attr->value; |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 6679 | break; |
| 6680 | } |
| 6681 | } |
| 6682 | assert(attr); |
| 6683 | } |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6684 | 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] | 6685 | break; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6686 | case UNRES_IFFEAT: |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6687 | iff_data = str_node; |
| 6688 | 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] | 6689 | break; |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6690 | case UNRES_FEATURE: |
| 6691 | LOGVRB("There are unresolved if-features for \"%s\" feature circular dependency check, it will be attempted later", |
| 6692 | ((struct lys_feature *)item)->name); |
| 6693 | break; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6694 | case UNRES_USES: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6695 | 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] | 6696 | break; |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 6697 | case UNRES_TYPEDEF_DFLT: |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6698 | case UNRES_TYPE_DFLT: |
Radek Krejci | 2e2de83 | 2016-10-13 16:12:26 +0200 | [diff] [blame] | 6699 | if (str_node) { |
| 6700 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "type default", (char *)str_node); |
| 6701 | } /* else no default value in the type itself, but we are checking some restrictions against |
| 6702 | * possible default value of some base type. The failure is caused by not resolved base type, |
| 6703 | * so it was already reported */ |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6704 | break; |
| 6705 | case UNRES_CHOICE_DFLT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6706 | 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] | 6707 | break; |
| 6708 | case UNRES_LIST_KEYS: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6709 | 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] | 6710 | break; |
| 6711 | case UNRES_LIST_UNIQ: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6712 | 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] | 6713 | break; |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 6714 | case UNRES_AUGMENT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6715 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "augment target", |
| 6716 | ((struct lys_node_augment *)item)->target_name); |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 6717 | break; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6718 | case UNRES_XPATH: |
Michal Vasko | 0d19837 | 2016-11-16 11:40:03 +0100 | [diff] [blame] | 6719 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "XPath expressions of", |
| 6720 | ((struct lys_node *)item)->name); |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6721 | break; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6722 | case UNRES_EXT: |
| 6723 | extinfo = (struct unres_ext *)str_node; |
| 6724 | name = extinfo->datatype == LYS_IN_YIN ? extinfo->data.yin->name : NULL; /* TODO YANG extension */ |
| 6725 | LOGVRB("Resolving extension \"%s\" failed, it will be attempted later.", name); |
| 6726 | break; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6727 | default: |
| 6728 | LOGINT; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6729 | break; |
| 6730 | } |
| 6731 | } |
| 6732 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6733 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6734 | * @brief Resolve every unres schema item in the structure. Logs directly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6735 | * |
| 6736 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6737 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6738 | * |
Michal Vasko | 92b8a38 | 2015-08-19 14:03:49 +0200 | [diff] [blame] | 6739 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6740 | */ |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6741 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 6742 | resolve_unres_schema(struct lys_module *mod, struct unres_schema *unres) |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6743 | { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6744 | uint32_t i, resolved = 0, unres_count, res_count; |
PavolVican | a0fdbf3 | 2017-02-15 17:59:02 +0100 | [diff] [blame] | 6745 | struct lyxml_elem *yin; |
| 6746 | struct yang_type *yang; |
Michal Vasko | 74a60c0 | 2017-03-08 10:19:48 +0100 | [diff] [blame] | 6747 | int rc, log_hidden; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6748 | |
| 6749 | assert(unres); |
| 6750 | |
Michal Vasko | e873426 | 2016-09-29 14:12:06 +0200 | [diff] [blame] | 6751 | LOGVRB("Resolving \"%s\" unresolved schema nodes and their constraints...", mod->name); |
Michal Vasko | 74a60c0 | 2017-03-08 10:19:48 +0100 | [diff] [blame] | 6752 | if (*ly_vlog_hide_location()) { |
| 6753 | log_hidden = 1; |
| 6754 | } else { |
| 6755 | log_hidden = 0; |
| 6756 | ly_vlog_hide(1); |
| 6757 | } |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6758 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6759 | /* uses */ |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6760 | do { |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6761 | unres_count = 0; |
| 6762 | res_count = 0; |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6763 | |
| 6764 | for (i = 0; i < unres->count; ++i) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6765 | /* UNRES_TYPE_LEAFREF must be resolved (for storing leafref target pointers); |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6766 | * if-features are resolved here to make sure that we will have all if-features for |
| 6767 | * later check of feature circular dependency */ |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6768 | if (unres->type[i] > UNRES_IDENT) { |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6769 | continue; |
| 6770 | } |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6771 | /* processes UNRES_USES, UNRES_IFFEAT, UNRES_TYPE_DER, UNRES_TYPE_DER_TPDF, UNRES_TYPE_LEAFREF, |
Radek Krejci | 818b0c5 | 2016-11-09 15:10:51 +0100 | [diff] [blame] | 6772 | * UNRES_AUGMENT, UNRES_CHOICE_DFLT and UNRES_IDENT */ |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6773 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6774 | ++unres_count; |
Radek Krejci | cd9a95a | 2017-03-25 12:02:35 -0500 | [diff] [blame] | 6775 | rc = resolve_unres_schema_item(unres->module[i], unres->item[i], unres->type[i], unres->str_snode[i], unres, 0); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6776 | if (!rc) { |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6777 | unres->type[i] = UNRES_RESOLVED; |
| 6778 | ++resolved; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6779 | ++res_count; |
Michal Vasko | 89e1532 | 2015-08-17 15:46:55 +0200 | [diff] [blame] | 6780 | } else if (rc == -1) { |
Michal Vasko | 74a60c0 | 2017-03-08 10:19:48 +0100 | [diff] [blame] | 6781 | if (!log_hidden) { |
| 6782 | ly_vlog_hide(0); |
| 6783 | } |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6784 | /* print the error */ |
Radek Krejci | 791f6c7 | 2017-02-22 15:23:39 +0100 | [diff] [blame] | 6785 | ly_err_repeat(); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6786 | return -1; |
Radek Krejci | c2a180f | 2016-06-22 13:28:16 +0200 | [diff] [blame] | 6787 | } else { |
| 6788 | /* forward reference, erase ly_errno */ |
Radek Krejci | 00a0e71 | 2016-10-26 10:24:46 +0200 | [diff] [blame] | 6789 | ly_err_clean(1); |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6790 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6791 | } |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6792 | } while (res_count && (res_count < unres_count)); |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6793 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6794 | if (res_count < unres_count) { |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6795 | /* just print the errors */ |
Michal Vasko | 74a60c0 | 2017-03-08 10:19:48 +0100 | [diff] [blame] | 6796 | if (!log_hidden) { |
| 6797 | ly_vlog_hide(0); |
| 6798 | } |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6799 | |
| 6800 | for (i = 0; i < unres->count; ++i) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6801 | if (unres->type[i] > UNRES_IDENT) { |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6802 | continue; |
| 6803 | } |
Radek Krejci | cd9a95a | 2017-03-25 12:02:35 -0500 | [diff] [blame] | 6804 | resolve_unres_schema_item(unres->module[i], unres->item[i], unres->type[i], unres->str_snode[i], unres, 1); |
Radek Krejci | 63fc096 | 2017-02-15 13:20:18 +0100 | [diff] [blame] | 6805 | if (unres->type[i] == UNRES_TYPE_DER_EXT) { |
PavolVican | a0fdbf3 | 2017-02-15 17:59:02 +0100 | [diff] [blame] | 6806 | yin = (struct lyxml_elem*)((struct lys_type *)unres->item[i])->der; |
| 6807 | if (yin->flags & LY_YANG_STRUCTURE_FLAG) { |
| 6808 | yang =(struct yang_type *)yin; |
| 6809 | ((struct lys_type *)unres->item[i])->base = yang->base; |
| 6810 | if (yang->base == LY_TYPE_UNION) { |
| 6811 | yang_free_type_union(mod->ctx, (struct lys_type *)unres->item[i]); |
| 6812 | } |
| 6813 | lydict_remove(mod->ctx, yang->name); |
| 6814 | free(yang); |
| 6815 | } else { |
| 6816 | lyxml_free(mod->ctx, yin); |
| 6817 | } |
Radek Krejci | 63fc096 | 2017-02-15 13:20:18 +0100 | [diff] [blame] | 6818 | } |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6819 | } |
Michal Vasko | 92b8a38 | 2015-08-19 14:03:49 +0200 | [diff] [blame] | 6820 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6821 | } |
| 6822 | |
Radek Krejci | 07d0fb9 | 2017-01-13 14:11:05 +0100 | [diff] [blame] | 6823 | /* the rest except finalizing extensions */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6824 | for (i = 0; i < unres->count; ++i) { |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6825 | if (unres->type[i] == UNRES_RESOLVED || unres->type[i] == UNRES_EXT_FINALIZE) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6826 | continue; |
| 6827 | } |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 6828 | |
Radek Krejci | cd9a95a | 2017-03-25 12:02:35 -0500 | [diff] [blame] | 6829 | rc = resolve_unres_schema_item(unres->module[i], unres->item[i], unres->type[i], unres->str_snode[i], unres, 0); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6830 | if (rc == 0) { |
Pavol Vican | 88e16c9 | 2016-09-07 15:41:50 +0200 | [diff] [blame] | 6831 | if (unres->type[i] == UNRES_LIST_UNIQ) { |
| 6832 | /* free the allocated structure */ |
| 6833 | free(unres->item[i]); |
| 6834 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6835 | unres->type[i] = UNRES_RESOLVED; |
| 6836 | ++resolved; |
| 6837 | } else if (rc == -1) { |
Michal Vasko | 74a60c0 | 2017-03-08 10:19:48 +0100 | [diff] [blame] | 6838 | if (!log_hidden) { |
| 6839 | ly_vlog_hide(0); |
| 6840 | } |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6841 | /* print the error */ |
Radek Krejci | 791f6c7 | 2017-02-22 15:23:39 +0100 | [diff] [blame] | 6842 | ly_err_repeat(); |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6843 | return -1; |
Radek Krejci | 791f6c7 | 2017-02-22 15:23:39 +0100 | [diff] [blame] | 6844 | } else { |
| 6845 | /* forward reference, erase ly_errno */ |
| 6846 | ly_err_clean(1); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6847 | } |
| 6848 | } |
| 6849 | |
Michal Vasko | 74a60c0 | 2017-03-08 10:19:48 +0100 | [diff] [blame] | 6850 | if (!log_hidden) { |
| 6851 | ly_vlog_hide(0); |
| 6852 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6853 | |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6854 | /* finalize extensions, keep it last to provide the complete schema tree information to the plugin's checkers */ |
| 6855 | for (i = 0; i < unres->count; ++i) { |
| 6856 | if (unres->type[i] != UNRES_EXT_FINALIZE) { |
| 6857 | continue; |
| 6858 | } |
| 6859 | |
Radek Krejci | cd9a95a | 2017-03-25 12:02:35 -0500 | [diff] [blame] | 6860 | rc = resolve_unres_schema_item(unres->module[i], unres->item[i], unres->type[i], unres->str_snode[i], unres, 0); |
Radek Krejci | 791f6c7 | 2017-02-22 15:23:39 +0100 | [diff] [blame] | 6861 | unres->type[i] = UNRES_RESOLVED; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6862 | if (rc == 0) { |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6863 | ++resolved; |
| 6864 | } |
Radek Krejci | 791f6c7 | 2017-02-22 15:23:39 +0100 | [diff] [blame] | 6865 | /* else error - it was already printed, but resolved was not increased, |
| 6866 | so this unres item will not be resolved again in the following code, |
| 6867 | but it will cause returning -1 at the end, this way we are able to |
| 6868 | print all the issues with unres */ |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6869 | } |
| 6870 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6871 | if (resolved < unres->count) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6872 | /* try to resolve the unresolved nodes again, it will not resolve anything, but it will print |
| 6873 | * all the validation errors |
| 6874 | */ |
| 6875 | for (i = 0; i < unres->count; ++i) { |
| 6876 | if (unres->type[i] == UNRES_RESOLVED) { |
| 6877 | continue; |
| 6878 | } |
Radek Krejci | cd9a95a | 2017-03-25 12:02:35 -0500 | [diff] [blame] | 6879 | resolve_unres_schema_item(unres->module[i], unres->item[i], unres->type[i], unres->str_snode[i], unres, 1); |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 6880 | if (unres->type[i] == UNRES_XPATH) { |
Michal Vasko | 769f803 | 2017-01-24 13:11:55 +0100 | [diff] [blame] | 6881 | /* XPath referencing an unknown node is actually supposed to be just a warning */ |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 6882 | unres->type[i] = UNRES_RESOLVED; |
| 6883 | resolved++; |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 6884 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6885 | } |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 6886 | if (resolved < unres->count) { |
| 6887 | return -1; |
| 6888 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6889 | } |
| 6890 | |
Michal Vasko | e873426 | 2016-09-29 14:12:06 +0200 | [diff] [blame] | 6891 | LOGVRB("All \"%s\" schema nodes and constraints resolved.", mod->name); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6892 | unres->count = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6893 | return EXIT_SUCCESS; |
| 6894 | } |
| 6895 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6896 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6897 | * @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] | 6898 | * |
| 6899 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6900 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6901 | * @param[in] item Item to resolve. Type determined by \p type. |
| 6902 | * @param[in] type Type of the unresolved item. |
| 6903 | * @param[in] str String argument. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6904 | * |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 6905 | * @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] | 6906 | */ |
| 6907 | int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6908 | unres_schema_add_str(struct lys_module *mod, struct unres_schema *unres, void *item, enum UNRES_ITEM type, |
| 6909 | const char *str) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6910 | { |
Radek Krejci | 54081ce | 2016-08-12 15:21:47 +0200 | [diff] [blame] | 6911 | int rc; |
| 6912 | const char *dictstr; |
| 6913 | |
| 6914 | dictstr = lydict_insert(mod->ctx, str, 0); |
| 6915 | rc = unres_schema_add_node(mod, unres, item, type, (struct lys_node *)dictstr); |
| 6916 | |
Radek Krejci | d9c0ce2 | 2017-01-20 15:20:16 +0100 | [diff] [blame] | 6917 | if (rc < 0) { |
Radek Krejci | 54081ce | 2016-08-12 15:21:47 +0200 | [diff] [blame] | 6918 | lydict_remove(mod->ctx, dictstr); |
| 6919 | } |
| 6920 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6921 | } |
| 6922 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6923 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6924 | * @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] | 6925 | * |
| 6926 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6927 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6928 | * @param[in] item Item to resolve. Type determined by \p type. |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6929 | * @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] | 6930 | * @param[in] snode Schema node argument. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6931 | * |
Radek Krejci | d9c0ce2 | 2017-01-20 15:20:16 +0100 | [diff] [blame] | 6932 | * @return EXIT_SUCCESS on success, EXIT_FIALURE on storing the item in unres, -1 on error, -2 if the unres item |
| 6933 | * is already in the unres list. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6934 | */ |
| 6935 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 6936 | 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] | 6937 | struct lys_node *snode) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6938 | { |
Michal Vasko | ef486d7 | 2016-09-27 12:10:44 +0200 | [diff] [blame] | 6939 | int rc, log_hidden; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6940 | struct lyxml_elem *yin; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6941 | |
Michal Vasko | 9bf425b | 2015-10-22 11:42:03 +0200 | [diff] [blame] | 6942 | assert(unres && item && ((type != UNRES_LEAFREF) && (type != UNRES_INSTID) && (type != UNRES_WHEN) |
| 6943 | && (type != UNRES_MUST))); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6944 | |
Michal Vasko | 9e862e8 | 2017-03-08 10:20:49 +0100 | [diff] [blame] | 6945 | #ifndef NDEBUG |
Radek Krejci | df056df | 2017-03-09 13:24:45 +0100 | [diff] [blame] | 6946 | uint32_t u; |
| 6947 | |
Radek Krejci | 850a5de | 2016-11-08 14:06:40 +0100 | [diff] [blame] | 6948 | /* check for duplicities in unres */ |
| 6949 | for (u = 0; u < unres->count; u++) { |
| 6950 | if (unres->type[u] == type && unres->item[u] == item && |
| 6951 | unres->str_snode[u] == snode && unres->module[u] == mod) { |
Michal Vasko | 9e862e8 | 2017-03-08 10:20:49 +0100 | [diff] [blame] | 6952 | /* duplication, should not happen */ |
| 6953 | assert(0); |
Radek Krejci | 850a5de | 2016-11-08 14:06:40 +0100 | [diff] [blame] | 6954 | } |
| 6955 | } |
Michal Vasko | 9e862e8 | 2017-03-08 10:20:49 +0100 | [diff] [blame] | 6956 | #endif |
Radek Krejci | 850a5de | 2016-11-08 14:06:40 +0100 | [diff] [blame] | 6957 | |
Radek Krejci | c293bac | 2017-02-27 11:25:28 +0100 | [diff] [blame] | 6958 | if (type == UNRES_EXT_FINALIZE) { |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6959 | /* extension finalization is not even tried when adding the item into the inres list */ |
Radek Krejci | c293bac | 2017-02-27 11:25:28 +0100 | [diff] [blame] | 6960 | rc = EXIT_FAILURE; |
| 6961 | } else { |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6962 | if (*ly_vlog_hide_location()) { |
| 6963 | log_hidden = 1; |
| 6964 | } else { |
| 6965 | log_hidden = 0; |
| 6966 | ly_vlog_hide(1); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6967 | } |
Radek Krejci | cbba57c | 2017-01-24 13:43:20 +0100 | [diff] [blame] | 6968 | rc = resolve_unres_schema_item(mod, item, type, snode, unres, 0); |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6969 | if (!log_hidden) { |
| 6970 | ly_vlog_hide(0); |
| 6971 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6972 | |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6973 | if (rc != EXIT_FAILURE) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 6974 | if (rc == -1) { |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6975 | ly_err_repeat(); |
| 6976 | } |
| 6977 | if (type == UNRES_LIST_UNIQ) { |
| 6978 | /* free the allocated structure */ |
| 6979 | free(item); |
| 6980 | } else if (rc == -1 && type == UNRES_IFFEAT) { |
| 6981 | /* free the allocated resources */ |
| 6982 | free(*((char **)item)); |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 6983 | } |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6984 | return rc; |
| 6985 | } else { |
| 6986 | /* erase info about validation errors */ |
| 6987 | ly_err_clean(1); |
| 6988 | } |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6989 | |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6990 | print_unres_schema_item_fail(item, type, snode); |
| 6991 | |
| 6992 | /* HACK unlinking is performed here so that we do not do any (NS) copying in vain */ |
| 6993 | if (type == UNRES_TYPE_DER || type == UNRES_TYPE_DER_TPDF) { |
| 6994 | yin = (struct lyxml_elem *)((struct lys_type *)item)->der; |
| 6995 | if (!(yin->flags & LY_YANG_STRUCTURE_FLAG)) { |
| 6996 | lyxml_unlink_elem(mod->ctx, yin, 1); |
| 6997 | ((struct lys_type *)item)->der = (struct lys_tpdf *)yin; |
| 6998 | } |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6999 | } |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7000 | } |
| 7001 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7002 | unres->count++; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 7003 | unres->item = ly_realloc(unres->item, unres->count*sizeof *unres->item); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 7004 | LY_CHECK_ERR_RETURN(!unres->item, LOGMEM, -1); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7005 | unres->item[unres->count-1] = item; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 7006 | unres->type = ly_realloc(unres->type, unres->count*sizeof *unres->type); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 7007 | LY_CHECK_ERR_RETURN(!unres->type, LOGMEM, -1); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7008 | unres->type[unres->count-1] = type; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 7009 | unres->str_snode = ly_realloc(unres->str_snode, unres->count*sizeof *unres->str_snode); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 7010 | LY_CHECK_ERR_RETURN(!unres->str_snode, LOGMEM, -1); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7011 | unres->str_snode[unres->count-1] = snode; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7012 | unres->module = ly_realloc(unres->module, unres->count*sizeof *unres->module); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 7013 | LY_CHECK_ERR_RETURN(!unres->module, LOGMEM, -1); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7014 | unres->module[unres->count-1] = mod; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7015 | |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 7016 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7017 | } |
| 7018 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7019 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 7020 | * @brief Duplicate an unres schema item. Logs indirectly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7021 | * |
| 7022 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 7023 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7024 | * @param[in] item Old item to be resolved. |
| 7025 | * @param[in] type Type of the old unresolved item. |
| 7026 | * @param[in] new_item New item to use in the duplicate. |
| 7027 | * |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 7028 | * @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] | 7029 | */ |
Michal Vasko | dad1940 | 2015-08-06 09:51:53 +0200 | [diff] [blame] | 7030 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 7031 | 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] | 7032 | { |
| 7033 | int i; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7034 | struct unres_list_uniq aux_uniq; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7035 | struct unres_iffeat_data *iff_data; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7036 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7037 | 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] | 7038 | |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7039 | /* hack for UNRES_LIST_UNIQ, which stores multiple items behind its item */ |
| 7040 | if (type == UNRES_LIST_UNIQ) { |
| 7041 | aux_uniq.list = item; |
| 7042 | aux_uniq.expr = ((struct unres_list_uniq *)new_item)->expr; |
| 7043 | item = &aux_uniq; |
| 7044 | } |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7045 | i = unres_schema_find(unres, -1, item, type); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7046 | |
| 7047 | if (i == -1) { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7048 | if (type == UNRES_LIST_UNIQ) { |
| 7049 | free(new_item); |
| 7050 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 7051 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7052 | } |
| 7053 | |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 7054 | if ((type == UNRES_TYPE_LEAFREF) || (type == UNRES_USES) || (type == UNRES_TYPE_DFLT) || |
Radek Krejci | b69f323 | 2016-09-16 17:41:07 +0200 | [diff] [blame] | 7055 | (type == UNRES_FEATURE) || (type == UNRES_LIST_UNIQ)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7056 | if (unres_schema_add_node(mod, unres, new_item, type, unres->str_snode[i]) == -1) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7057 | LOGINT; |
| 7058 | return -1; |
| 7059 | } |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7060 | } else if (type == UNRES_IFFEAT) { |
| 7061 | /* duplicate unres_iffeature_data */ |
| 7062 | iff_data = malloc(sizeof *iff_data); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 7063 | LY_CHECK_ERR_RETURN(!iff_data, LOGMEM, -1); |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7064 | iff_data->fname = lydict_insert(mod->ctx, ((struct unres_iffeat_data *)unres->str_snode[i])->fname, 0); |
| 7065 | iff_data->node = ((struct unres_iffeat_data *)unres->str_snode[i])->node; |
| 7066 | if (unres_schema_add_node(mod, unres, new_item, type, (struct lys_node *)iff_data) == -1) { |
| 7067 | LOGINT; |
| 7068 | return -1; |
| 7069 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7070 | } else { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7071 | if (unres_schema_add_str(mod, unres, new_item, type, unres->str_snode[i]) == -1) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7072 | LOGINT; |
| 7073 | return -1; |
| 7074 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7075 | } |
Michal Vasko | dad1940 | 2015-08-06 09:51:53 +0200 | [diff] [blame] | 7076 | |
| 7077 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7078 | } |
| 7079 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7080 | /* does not log */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7081 | int |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7082 | 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] | 7083 | { |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7084 | int i; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7085 | struct unres_list_uniq *aux_uniq1, *aux_uniq2; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7086 | |
Radek Krejci | ddddd0d | 2017-01-20 15:20:46 +0100 | [diff] [blame] | 7087 | if (start_on_backwards >= 0) { |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7088 | i = start_on_backwards; |
| 7089 | } else { |
| 7090 | i = unres->count - 1; |
| 7091 | } |
| 7092 | for (; i > -1; i--) { |
| 7093 | if (unres->type[i] != type) { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7094 | continue; |
| 7095 | } |
| 7096 | if (type != UNRES_LIST_UNIQ) { |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7097 | if (unres->item[i] == item) { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7098 | break; |
| 7099 | } |
| 7100 | } else { |
| 7101 | aux_uniq1 = (struct unres_list_uniq *)unres->item[i - 1]; |
| 7102 | aux_uniq2 = (struct unres_list_uniq *)item; |
| 7103 | 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] | 7104 | break; |
| 7105 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7106 | } |
| 7107 | } |
| 7108 | |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7109 | return i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7110 | } |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7111 | |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7112 | static void |
| 7113 | unres_schema_free_item(struct ly_ctx *ctx, struct unres_schema *unres, uint32_t i) |
| 7114 | { |
| 7115 | struct lyxml_elem *yin; |
| 7116 | struct yang_type *yang; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7117 | struct unres_iffeat_data *iff_data; |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7118 | |
| 7119 | switch (unres->type[i]) { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 7120 | case UNRES_TYPE_DER_TPDF: |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7121 | case UNRES_TYPE_DER: |
| 7122 | yin = (struct lyxml_elem *)((struct lys_type *)unres->item[i])->der; |
| 7123 | if (yin->flags & LY_YANG_STRUCTURE_FLAG) { |
| 7124 | yang =(struct yang_type *)yin; |
Pavol Vican | cf2af4d | 2016-12-21 14:13:06 +0100 | [diff] [blame] | 7125 | ((struct lys_type *)unres->item[i])->base = yang->base; |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7126 | lydict_remove(ctx, yang->name); |
| 7127 | free(yang); |
Pavol Vican | cf2af4d | 2016-12-21 14:13:06 +0100 | [diff] [blame] | 7128 | if (((struct lys_type *)unres->item[i])->base == LY_TYPE_UNION) { |
| 7129 | yang_free_type_union(ctx, (struct lys_type *)unres->item[i]); |
| 7130 | } |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7131 | } else { |
| 7132 | lyxml_free(ctx, yin); |
| 7133 | } |
| 7134 | break; |
Pavol Vican | 88e16c9 | 2016-09-07 15:41:50 +0200 | [diff] [blame] | 7135 | case UNRES_IFFEAT: |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7136 | iff_data = (struct unres_iffeat_data *)unres->str_snode[i]; |
| 7137 | lydict_remove(ctx, iff_data->fname); |
| 7138 | free(unres->str_snode[i]); |
Pavol Vican | 88e16c9 | 2016-09-07 15:41:50 +0200 | [diff] [blame] | 7139 | break; |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7140 | case UNRES_IDENT: |
| 7141 | case UNRES_TYPE_IDENTREF: |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7142 | case UNRES_CHOICE_DFLT: |
| 7143 | case UNRES_LIST_KEYS: |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7144 | lydict_remove(ctx, (const char *)unres->str_snode[i]); |
| 7145 | break; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7146 | case UNRES_LIST_UNIQ: |
| 7147 | free(unres->item[i]); |
| 7148 | break; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 7149 | case UNRES_EXT: |
| 7150 | free(unres->str_snode[i]); |
| 7151 | break; |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7152 | default: |
| 7153 | break; |
| 7154 | } |
| 7155 | unres->type[i] = UNRES_RESOLVED; |
| 7156 | } |
| 7157 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7158 | void |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 7159 | 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] | 7160 | { |
| 7161 | uint32_t i; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7162 | unsigned int unresolved = 0; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7163 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7164 | if (!unres || !(*unres)) { |
| 7165 | return; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7166 | } |
| 7167 | |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 7168 | assert(module || ((*unres)->count == 0)); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7169 | |
| 7170 | for (i = 0; i < (*unres)->count; ++i) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 7171 | if (!all && ((*unres)->module[i] != module)) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7172 | if ((*unres)->type[i] != UNRES_RESOLVED) { |
| 7173 | unresolved++; |
| 7174 | } |
| 7175 | continue; |
| 7176 | } |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7177 | |
| 7178 | /* free heap memory for the specific item */ |
| 7179 | unres_schema_free_item(module->ctx, *unres, i); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7180 | } |
| 7181 | |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7182 | /* free it all */ |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 7183 | if (!module || all || (!unresolved && !module->type)) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7184 | free((*unres)->item); |
| 7185 | free((*unres)->type); |
| 7186 | free((*unres)->str_snode); |
| 7187 | free((*unres)->module); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7188 | free((*unres)); |
| 7189 | (*unres) = NULL; |
| 7190 | } |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7191 | } |
| 7192 | |
Michal Vasko | ff690e7 | 2017-08-03 14:25:07 +0200 | [diff] [blame^] | 7193 | /* check whether instance-identifier points outside its data subtree (for operation it is any node |
| 7194 | * 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] | 7195 | static int |
| 7196 | check_instid_ext_dep(const struct lys_node *sleaf, const char *json_instid) |
| 7197 | { |
Michal Vasko | ff690e7 | 2017-08-03 14:25:07 +0200 | [diff] [blame^] | 7198 | const struct lys_node *op_node, *first_node; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7199 | char *buf; |
Michal Vasko | ff690e7 | 2017-08-03 14:25:07 +0200 | [diff] [blame^] | 7200 | int ret = 0, hidden; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7201 | |
Radek Krejci | 034cb10 | 2017-08-01 15:45:13 +0200 | [diff] [blame] | 7202 | if (!json_instid || !json_instid[0]) { |
| 7203 | /* no/empty value */ |
| 7204 | return 0; |
| 7205 | } |
| 7206 | |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7207 | for (op_node = lys_parent(sleaf); |
| 7208 | op_node && !(op_node->nodetype & (LYS_NOTIF | LYS_RPC | LYS_ACTION)); |
| 7209 | op_node = lys_parent(op_node)); |
| 7210 | |
| 7211 | if (op_node && lys_parent(op_node)) { |
| 7212 | /* nested operation - any absolute path is external */ |
| 7213 | return 1; |
| 7214 | } |
| 7215 | |
| 7216 | /* get the first node from the instid */ |
| 7217 | buf = strndup(json_instid, strchr(json_instid + 1, '/') - json_instid); |
| 7218 | if (!buf) { |
| 7219 | LOGMEM; |
| 7220 | return -1; |
| 7221 | } |
| 7222 | |
Michal Vasko | ff690e7 | 2017-08-03 14:25:07 +0200 | [diff] [blame^] | 7223 | /* find the first schema node, do not log */ |
| 7224 | hidden = *ly_vlog_hide_location(); |
| 7225 | if (!hidden) { |
| 7226 | ly_vlog_hide(1); |
| 7227 | } |
| 7228 | first_node = ly_ctx_get_node(NULL, sleaf, buf, 0); |
| 7229 | if (!hidden) { |
| 7230 | ly_vlog_hide(0); |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7231 | } |
| 7232 | |
Michal Vasko | ff690e7 | 2017-08-03 14:25:07 +0200 | [diff] [blame^] | 7233 | if (!first_node) { |
| 7234 | /* unknown path, say it is not external */ |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7235 | free(buf); |
Michal Vasko | ff690e7 | 2017-08-03 14:25:07 +0200 | [diff] [blame^] | 7236 | ly_errno = LYE_SUCCESS; |
| 7237 | return 0; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7238 | } |
| 7239 | free(buf); |
| 7240 | |
| 7241 | /* based on the first schema node in the path we can decide whether it points to an external tree or not */ |
| 7242 | |
Michal Vasko | ff690e7 | 2017-08-03 14:25:07 +0200 | [diff] [blame^] | 7243 | if (op_node && (op_node != first_node)) { |
| 7244 | /* it is a top-level operation, so we're good if it points somewhere inside it */ |
| 7245 | ret = 1; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7246 | } |
| 7247 | |
| 7248 | /* we cannot know whether it points to a tree that is going to be unlinked (application must handle |
| 7249 | * this itself), so we say it's not external */ |
Radek Krejci | 81c38b8 | 2017-06-02 15:04:16 +0200 | [diff] [blame] | 7250 | return ret; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7251 | } |
| 7252 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7253 | /** |
| 7254 | * @brief Resolve instance-identifier in JSON data format. Logs directly. |
| 7255 | * |
| 7256 | * @param[in] data Data node where the path is used |
| 7257 | * @param[in] path Instance-identifier node value. |
| 7258 | * @param[in,out] ret Resolved instance or NULL. |
| 7259 | * |
| 7260 | * @return 0 on success (even if unresolved and \p ret is NULL), -1 on error. |
| 7261 | */ |
| 7262 | static int |
| 7263 | resolve_instid(struct lyd_node *data, const char *path, int req_inst, struct lyd_node **ret) |
| 7264 | { |
| 7265 | int i = 0, j; |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 7266 | const struct lys_module *mod, *prev_mod = NULL; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7267 | struct ly_ctx *ctx = data->schema->module->ctx; |
| 7268 | const char *model, *name; |
| 7269 | char *str; |
| 7270 | int mod_len, name_len, has_predicate; |
| 7271 | struct unres_data node_match; |
| 7272 | |
| 7273 | memset(&node_match, 0, sizeof node_match); |
| 7274 | *ret = NULL; |
| 7275 | |
| 7276 | /* we need root to resolve absolute path */ |
| 7277 | for (; data->parent; data = data->parent); |
| 7278 | /* we're still parsing it and the pointer is not correct yet */ |
| 7279 | if (data->prev) { |
| 7280 | for (; data->prev->next; data = data->prev); |
| 7281 | } |
| 7282 | |
| 7283 | /* search for the instance node */ |
| 7284 | while (path[i]) { |
| 7285 | j = parse_instance_identifier(&path[i], &model, &mod_len, &name, &name_len, &has_predicate); |
| 7286 | if (j <= 0) { |
| 7287 | LOGVAL(LYE_INCHAR, LY_VLOG_LYD, data, path[i-j], &path[i-j]); |
| 7288 | goto error; |
| 7289 | } |
| 7290 | i += j; |
| 7291 | |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 7292 | if (model) { |
| 7293 | str = strndup(model, mod_len); |
| 7294 | if (!str) { |
| 7295 | LOGMEM; |
| 7296 | goto error; |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 7297 | } |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 7298 | mod = ly_ctx_get_module(ctx, str, NULL); |
| 7299 | if (ctx->data_clb) { |
| 7300 | if (!mod) { |
| 7301 | mod = ctx->data_clb(ctx, str, NULL, 0, ctx->data_clb_data); |
| 7302 | } else if (!mod->implemented) { |
| 7303 | mod = ctx->data_clb(ctx, mod->name, mod->ns, LY_MODCLB_NOT_IMPLEMENTED, ctx->data_clb_data); |
| 7304 | } |
| 7305 | } |
| 7306 | free(str); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7307 | |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 7308 | if (!mod || !mod->implemented || mod->disabled) { |
| 7309 | break; |
| 7310 | } |
| 7311 | } else if (!prev_mod) { |
| 7312 | /* first iteration and we are missing module name */ |
| 7313 | LOGVAL(LYE_INELEM_LEN, LY_VLOG_NONE, NULL, name_len, name); |
| 7314 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Instane-identifier is missing prefix in the first node."); |
| 7315 | goto error; |
| 7316 | } else { |
| 7317 | mod = prev_mod; |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 7318 | } |
| 7319 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7320 | if (resolve_data(mod, name, name_len, data, &node_match)) { |
| 7321 | /* no instance exists */ |
| 7322 | break; |
| 7323 | } |
| 7324 | |
| 7325 | if (has_predicate) { |
| 7326 | /* we have predicate, so the current results must be list or leaf-list */ |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 7327 | j = resolve_instid_predicate(mod, &path[i], &node_match); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7328 | if (j < 1) { |
| 7329 | LOGVAL(LYE_INPRED, LY_VLOG_LYD, data, &path[i-j]); |
| 7330 | goto error; |
| 7331 | } |
| 7332 | i += j; |
| 7333 | |
| 7334 | if (!node_match.count) { |
| 7335 | /* no instance exists */ |
| 7336 | break; |
| 7337 | } |
Michal Vasko | 6f28e0f | 2017-04-18 15:14:13 +0200 | [diff] [blame] | 7338 | } else if (node_match.count) { |
| 7339 | /* check that we are not addressing lists */ |
| 7340 | for (j = 0; (unsigned)j < node_match.count; ++j) { |
| 7341 | if (node_match.node[j]->schema->nodetype == LYS_LIST) { |
| 7342 | unres_data_del(&node_match, j--); |
| 7343 | } |
| 7344 | } |
| 7345 | if (!node_match.count) { |
| 7346 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier is missing list keys."); |
| 7347 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7348 | } |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 7349 | |
| 7350 | prev_mod = mod; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7351 | } |
| 7352 | |
| 7353 | if (!node_match.count) { |
| 7354 | /* no instance exists */ |
| 7355 | if (req_inst > -1) { |
| 7356 | LOGVAL(LYE_NOREQINS, LY_VLOG_NONE, NULL, path); |
| 7357 | return EXIT_FAILURE; |
| 7358 | } |
| 7359 | LOGVRB("There is no instance of \"%s\", but it is not required.", path); |
| 7360 | return EXIT_SUCCESS; |
| 7361 | } else if (node_match.count > 1) { |
| 7362 | /* instance identifier must resolve to a single node */ |
| 7363 | LOGVAL(LYE_TOOMANY, LY_VLOG_LYD, data, path, "data tree"); |
| 7364 | goto error; |
| 7365 | } else { |
| 7366 | /* we have required result, remember it and cleanup */ |
| 7367 | *ret = node_match.node[0]; |
| 7368 | free(node_match.node); |
| 7369 | return EXIT_SUCCESS; |
| 7370 | } |
| 7371 | |
| 7372 | error: |
| 7373 | /* cleanup */ |
| 7374 | free(node_match.node); |
| 7375 | return -1; |
| 7376 | } |
| 7377 | |
| 7378 | static int |
| 7379 | 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] | 7380 | { |
Michal Vasko | 86ae1f2 | 2017-07-10 11:50:33 +0200 | [diff] [blame] | 7381 | struct ly_set *set; |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7382 | uint32_t i; |
| 7383 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7384 | *ret = NULL; |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7385 | |
Michal Vasko | 86ae1f2 | 2017-07-10 11:50:33 +0200 | [diff] [blame] | 7386 | /* syntax was already checked, so just evaluate the path using standard XPath */ |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 7387 | set = lyd_find_path((struct lyd_node *)leaf, path); |
Michal Vasko | 86ae1f2 | 2017-07-10 11:50:33 +0200 | [diff] [blame] | 7388 | if (!set) { |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7389 | return -1; |
| 7390 | } |
| 7391 | |
Michal Vasko | 86ae1f2 | 2017-07-10 11:50:33 +0200 | [diff] [blame] | 7392 | for (i = 0; i < set->number; ++i) { |
| 7393 | if (!(set->set.d[i]->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 7394 | continue; |
| 7395 | } |
| 7396 | |
Radek Krejci | 1899d6a | 2016-11-03 13:48:07 +0100 | [diff] [blame] | 7397 | /* not that the value is already in canonical form since the parsers does the conversion, |
| 7398 | * so we can simply compare just the values */ |
Michal Vasko | 86ae1f2 | 2017-07-10 11:50:33 +0200 | [diff] [blame] | 7399 | if (ly_strequal(leaf->value_str, ((struct lyd_node_leaf_list *)set->set.d[i])->value_str, 1)) { |
Radek Krejci | 1899d6a | 2016-11-03 13:48:07 +0100 | [diff] [blame] | 7400 | /* we have the match */ |
Michal Vasko | 86ae1f2 | 2017-07-10 11:50:33 +0200 | [diff] [blame] | 7401 | *ret = set->set.d[i]; |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7402 | break; |
| 7403 | } |
| 7404 | } |
| 7405 | |
Michal Vasko | 86ae1f2 | 2017-07-10 11:50:33 +0200 | [diff] [blame] | 7406 | ly_set_free(set); |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7407 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7408 | if (!*ret) { |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7409 | /* reference not found */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7410 | if (req_inst > -1) { |
| 7411 | LOGVAL(LYE_NOLEAFREF, LY_VLOG_LYD, leaf, path, leaf->value_str); |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7412 | return EXIT_FAILURE; |
| 7413 | } else { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7414 | 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] | 7415 | } |
| 7416 | } |
| 7417 | |
| 7418 | return EXIT_SUCCESS; |
| 7419 | } |
| 7420 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7421 | /* 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] | 7422 | int |
| 7423 | resolve_union(struct lyd_node_leaf_list *leaf, struct lys_type *type, int store, int ignore_fail, |
| 7424 | struct lys_type **resolved_type) |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 7425 | { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7426 | struct lys_type *t; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7427 | struct lyd_node *ret; |
| 7428 | int found, hidden, success = 0, ext_dep, req_inst; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7429 | const char *json_val = NULL; |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 7430 | |
| 7431 | assert(type->base == LY_TYPE_UNION); |
| 7432 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7433 | if ((leaf->value_type == LY_TYPE_UNION) || (leaf->value_type == (LY_TYPE_INST | LY_TYPE_INST_UNRES))) { |
| 7434 | /* either NULL or instid previously converted to JSON */ |
| 7435 | json_val = leaf->value.string; |
| 7436 | } |
Michal Vasko | 1c8567a | 2017-01-05 13:42:27 +0100 | [diff] [blame] | 7437 | |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7438 | if (store) { |
| 7439 | if ((leaf->value_type & LY_DATA_TYPE_MASK) == LY_TYPE_BITS) { |
| 7440 | free(leaf->value.bit); |
| 7441 | } |
| 7442 | memset(&leaf->value, 0, sizeof leaf->value); |
Michal Vasko | 1c8567a | 2017-01-05 13:42:27 +0100 | [diff] [blame] | 7443 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7444 | |
| 7445 | /* turn logging off, we are going to try to validate the value with all the types in order */ |
| 7446 | hidden = *ly_vlog_hide_location(); |
| 7447 | ly_vlog_hide(1); |
| 7448 | |
| 7449 | t = NULL; |
| 7450 | found = 0; |
| 7451 | while ((t = lyp_get_next_union_type(type, t, &found))) { |
| 7452 | found = 0; |
| 7453 | |
| 7454 | switch (t->base) { |
| 7455 | case LY_TYPE_LEAFREF: |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7456 | if ((ignore_fail == 1) || ((leaf->schema->flags & LYS_LEAFREF_DEP) && (ignore_fail == 2))) { |
| 7457 | req_inst = -1; |
| 7458 | } else { |
| 7459 | req_inst = t->info.lref.req; |
| 7460 | } |
| 7461 | |
| 7462 | if (!resolve_leafref(leaf, t->info.lref.path, req_inst, &ret)) { |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7463 | if (store) { |
| 7464 | if (ret && !(leaf->schema->flags & LYS_LEAFREF_DEP)) { |
| 7465 | /* valid resolved */ |
| 7466 | leaf->value.leafref = ret; |
| 7467 | leaf->value_type = LY_TYPE_LEAFREF; |
| 7468 | } else { |
| 7469 | /* valid unresolved */ |
Radek Krejci | a571d94 | 2017-02-24 09:26:49 +0100 | [diff] [blame] | 7470 | if (!lyp_parse_value(t, &leaf->value_str, NULL, leaf, NULL, 1, 0)) { |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7471 | return -1; |
| 7472 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7473 | } |
| 7474 | } |
| 7475 | |
| 7476 | success = 1; |
| 7477 | } |
| 7478 | break; |
| 7479 | case LY_TYPE_INST: |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7480 | ext_dep = check_instid_ext_dep(leaf->schema, (json_val ? json_val : leaf->value_str)); |
Radek Krejci | 034cb10 | 2017-08-01 15:45:13 +0200 | [diff] [blame] | 7481 | if (ext_dep == -1) { |
| 7482 | return -1; |
| 7483 | } |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7484 | if ((ignore_fail == 1) || (ext_dep && (ignore_fail == 2))) { |
| 7485 | req_inst = -1; |
| 7486 | } else { |
| 7487 | req_inst = t->info.inst.req; |
| 7488 | } |
| 7489 | |
Michal Vasko | d3a0311 | 2017-01-23 09:56:02 +0100 | [diff] [blame] | 7490 | 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] | 7491 | if (store) { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7492 | if (ret && !ext_dep) { |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7493 | /* valid resolved */ |
| 7494 | leaf->value.instance = ret; |
| 7495 | leaf->value_type = LY_TYPE_INST; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7496 | |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7497 | if (json_val) { |
| 7498 | lydict_remove(leaf->schema->module->ctx, leaf->value_str); |
| 7499 | leaf->value_str = json_val; |
| 7500 | json_val = NULL; |
| 7501 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7502 | } else { |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7503 | /* valid unresolved */ |
| 7504 | if (json_val) { |
| 7505 | /* put the JSON val back */ |
| 7506 | leaf->value.string = json_val; |
| 7507 | json_val = NULL; |
| 7508 | } else { |
| 7509 | leaf->value.instance = NULL; |
| 7510 | } |
| 7511 | leaf->value_type = LY_TYPE_INST | LY_TYPE_INST_UNRES; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7512 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7513 | } |
| 7514 | |
| 7515 | success = 1; |
| 7516 | } |
| 7517 | break; |
| 7518 | default: |
Radek Krejci | a571d94 | 2017-02-24 09:26:49 +0100 | [diff] [blame] | 7519 | if (lyp_parse_value(t, &leaf->value_str, NULL, leaf, NULL, store, 0)) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7520 | success = 1; |
| 7521 | } |
| 7522 | break; |
| 7523 | } |
| 7524 | |
| 7525 | if (success) { |
| 7526 | break; |
| 7527 | } |
| 7528 | |
| 7529 | /* erase information about errors - they are false or irrelevant |
| 7530 | * and will be replaced by a single error messages */ |
| 7531 | ly_err_clean(1); |
| 7532 | |
| 7533 | /* erase possible present and invalid value data */ |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7534 | if (store) { |
| 7535 | if (t->base == LY_TYPE_BITS) { |
| 7536 | free(leaf->value.bit); |
| 7537 | } |
| 7538 | memset(&leaf->value, 0, sizeof leaf->value); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7539 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7540 | } |
| 7541 | |
| 7542 | /* turn logging back on */ |
| 7543 | if (!hidden) { |
| 7544 | ly_vlog_hide(0); |
| 7545 | } |
| 7546 | |
| 7547 | if (json_val) { |
| 7548 | if (!success) { |
| 7549 | /* put the value back for now */ |
| 7550 | assert(leaf->value_type == LY_TYPE_UNION); |
| 7551 | leaf->value.string = json_val; |
| 7552 | } else { |
| 7553 | /* value was ultimately useless, but we could not have known */ |
| 7554 | lydict_remove(leaf->schema->module->ctx, json_val); |
| 7555 | } |
| 7556 | } |
| 7557 | |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7558 | if (success) { |
| 7559 | if (resolved_type) { |
| 7560 | *resolved_type = t; |
| 7561 | } |
| 7562 | } else if (!ignore_fail || !type->info.uni.has_ptr_type) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7563 | /* not found and it is required */ |
| 7564 | LOGVAL(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] | 7565 | return EXIT_FAILURE; |
| 7566 | } |
| 7567 | |
| 7568 | return EXIT_SUCCESS; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7569 | |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 7570 | } |
| 7571 | |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7572 | /** |
| 7573 | * @brief Resolve a single unres data item. Logs directly. |
| 7574 | * |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7575 | * @param[in] node Data node to resolve. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7576 | * @param[in] type Type of the unresolved item. |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7577 | * @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] | 7578 | * |
| 7579 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 7580 | */ |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 7581 | int |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7582 | resolve_unres_data_item(struct lyd_node *node, enum UNRES_ITEM type, int ignore_fail) |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7583 | { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7584 | int rc, req_inst, ext_dep; |
Michal Vasko | 83a6c46 | 2015-10-08 16:43:53 +0200 | [diff] [blame] | 7585 | struct lyd_node_leaf_list *leaf; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7586 | struct lyd_node *ret; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7587 | struct lys_node_leaf *sleaf; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7588 | |
Michal Vasko | 83a6c46 | 2015-10-08 16:43:53 +0200 | [diff] [blame] | 7589 | leaf = (struct lyd_node_leaf_list *)node; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7590 | sleaf = (struct lys_node_leaf *)leaf->schema; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7591 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7592 | switch (type) { |
| 7593 | case UNRES_LEAFREF: |
| 7594 | assert(sleaf->type.base == LY_TYPE_LEAFREF); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7595 | assert(leaf->validity & LYD_VAL_LEAFREF); |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7596 | if ((ignore_fail == 1) || ((leaf->schema->flags & LYS_LEAFREF_DEP) && (ignore_fail == 2))) { |
| 7597 | req_inst = -1; |
| 7598 | } else { |
| 7599 | req_inst = sleaf->type.info.lref.req; |
| 7600 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7601 | rc = resolve_leafref(leaf, sleaf->type.info.lref.path, req_inst, &ret); |
| 7602 | if (!rc) { |
Michal Vasko | b1ac872 | 2017-01-02 13:04:25 +0100 | [diff] [blame] | 7603 | if (ret && !(leaf->schema->flags & LYS_LEAFREF_DEP)) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7604 | /* valid resolved */ |
Michal Vasko | 1c8567a | 2017-01-05 13:42:27 +0100 | [diff] [blame] | 7605 | if ((leaf->value_type & LY_DATA_TYPE_MASK) == LY_TYPE_BITS) { |
| 7606 | free(leaf->value.bit); |
| 7607 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7608 | leaf->value.leafref = ret; |
| 7609 | leaf->value_type = LY_TYPE_LEAFREF; |
| 7610 | } else { |
| 7611 | /* valid unresolved */ |
| 7612 | if (!(leaf->value_type & LY_TYPE_LEAFREF_UNRES)) { |
Radek Krejci | a571d94 | 2017-02-24 09:26:49 +0100 | [diff] [blame] | 7613 | if (!lyp_parse_value(&sleaf->type, &leaf->value_str, NULL, leaf, NULL, 1, 0)) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7614 | return -1; |
| 7615 | } |
| 7616 | } |
| 7617 | } |
| 7618 | leaf->validity &= ~LYD_VAL_LEAFREF; |
| 7619 | } else { |
| 7620 | return rc; |
| 7621 | } |
| 7622 | break; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7623 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7624 | case UNRES_INSTID: |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7625 | assert(sleaf->type.base == LY_TYPE_INST); |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7626 | ext_dep = check_instid_ext_dep(leaf->schema, leaf->value_str); |
| 7627 | if (ext_dep == -1) { |
| 7628 | return -1; |
| 7629 | } |
| 7630 | |
| 7631 | if ((ignore_fail == 1) || (ext_dep && (ignore_fail == 2))) { |
| 7632 | req_inst = -1; |
| 7633 | } else { |
| 7634 | req_inst = sleaf->type.info.inst.req; |
| 7635 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7636 | rc = resolve_instid(node, leaf->value_str, req_inst, &ret); |
| 7637 | if (!rc) { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7638 | if (ret && !ext_dep) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7639 | /* valid resolved */ |
| 7640 | leaf->value.instance = ret; |
| 7641 | leaf->value_type = LY_TYPE_INST; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7642 | } else { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7643 | /* valid unresolved */ |
| 7644 | leaf->value.instance = NULL; |
| 7645 | leaf->value_type = LY_TYPE_INST | LY_TYPE_INST_UNRES; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7646 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7647 | } else { |
| 7648 | return rc; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7649 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7650 | break; |
| 7651 | |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7652 | case UNRES_UNION: |
| 7653 | assert(sleaf->type.base == LY_TYPE_UNION); |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7654 | return resolve_union(leaf, &sleaf->type, 1, ignore_fail, NULL); |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7655 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7656 | case UNRES_WHEN: |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7657 | if ((rc = resolve_when(node, NULL, ignore_fail))) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7658 | return rc; |
| 7659 | } |
| 7660 | break; |
| 7661 | |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 7662 | case UNRES_MUST: |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7663 | if ((rc = resolve_must(node, 0, ignore_fail))) { |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 7664 | return rc; |
| 7665 | } |
| 7666 | break; |
| 7667 | |
| 7668 | case UNRES_MUST_INOUT: |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7669 | if ((rc = resolve_must(node, 1, ignore_fail))) { |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 7670 | return rc; |
| 7671 | } |
| 7672 | break; |
| 7673 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7674 | default: |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7675 | LOGINT; |
| 7676 | return -1; |
| 7677 | } |
| 7678 | |
| 7679 | return EXIT_SUCCESS; |
| 7680 | } |
| 7681 | |
| 7682 | /** |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7683 | * @brief add data unres item |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7684 | * |
| 7685 | * @param[in] unres Unres data structure to use. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7686 | * @param[in] node Data node to use. |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7687 | * |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7688 | * @return 0 on success, -1 on error. |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7689 | */ |
| 7690 | int |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7691 | 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] | 7692 | { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7693 | assert(unres && node); |
Michal Vasko | c428084 | 2016-04-19 16:10:42 +0200 | [diff] [blame] | 7694 | assert((type == UNRES_LEAFREF) || (type == UNRES_INSTID) || (type == UNRES_WHEN) || (type == UNRES_MUST) |
Radek Krejci | bacc744 | 2016-10-27 13:39:56 +0200 | [diff] [blame] | 7695 | || (type == UNRES_MUST_INOUT) || (type == UNRES_UNION)); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7696 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7697 | unres->count++; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 7698 | unres->node = ly_realloc(unres->node, unres->count * sizeof *unres->node); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 7699 | LY_CHECK_ERR_RETURN(!unres->node, LOGMEM, -1); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7700 | unres->node[unres->count - 1] = node; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 7701 | unres->type = ly_realloc(unres->type, unres->count * sizeof *unres->type); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 7702 | LY_CHECK_ERR_RETURN(!unres->type, LOGMEM, -1); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7703 | unres->type[unres->count - 1] = type; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7704 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7705 | if (type == UNRES_WHEN) { |
| 7706 | /* remove previous result */ |
| 7707 | node->when_status = LYD_WHEN; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7708 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7709 | |
| 7710 | return EXIT_SUCCESS; |
| 7711 | } |
| 7712 | |
| 7713 | /** |
| 7714 | * @brief Resolve every unres data item in the structure. Logs directly. |
| 7715 | * |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 7716 | * If options includes LYD_OPT_TRUSTED, the data are considered trusted (when, must conditions are not expected, |
| 7717 | * unresolved leafrefs/instids are accepted). |
| 7718 | * |
| 7719 | * If options includes LYD_OPT_NOAUTODEL, the false resulting when condition on non-default nodes, the error is raised. |
| 7720 | * |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7721 | * @param[in] unres Unres data structure to use. |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 7722 | * @param[in,out] root Root node of the data tree, can be changed due to autodeletion. |
| 7723 | * @param[in] options Data options as described above. |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7724 | * |
| 7725 | * @return EXIT_SUCCESS on success, -1 on error. |
| 7726 | */ |
| 7727 | int |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 7728 | resolve_unres_data(struct unres_data *unres, struct lyd_node **root, int options) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7729 | { |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 7730 | uint32_t i, j, first, resolved, del_items, stmt_count; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7731 | int rc, progress, ignore_fail; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7732 | struct lyd_node *parent; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7733 | |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 7734 | assert(root); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7735 | assert(unres); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7736 | |
| 7737 | if (!unres->count) { |
| 7738 | return EXIT_SUCCESS; |
| 7739 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7740 | |
Michal Vasko | ad2e44a | 2017-01-03 10:31:35 +0100 | [diff] [blame] | 7741 | if (options & (LYD_OPT_TRUSTED | 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] | 7742 | ignore_fail = 1; |
| 7743 | } else if (options & LYD_OPT_NOEXTDEPS) { |
| 7744 | ignore_fail = 2; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7745 | } else { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7746 | ignore_fail = 0; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7747 | } |
| 7748 | |
Michal Vasko | a0ffcab | 2016-05-02 14:52:08 +0200 | [diff] [blame] | 7749 | LOGVRB("Resolving unresolved data nodes and their constraints..."); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7750 | ly_vlog_hide(1); |
| 7751 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7752 | /* when-stmt first */ |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 7753 | first = 1; |
| 7754 | stmt_count = 0; |
| 7755 | resolved = 0; |
| 7756 | del_items = 0; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7757 | do { |
Radek Krejci | 00a0e71 | 2016-10-26 10:24:46 +0200 | [diff] [blame] | 7758 | ly_err_clean(1); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7759 | progress = 0; |
Michal Vasko | 6df9413 | 2016-09-22 11:08:09 +0200 | [diff] [blame] | 7760 | for (i = 0; i < unres->count; i++) { |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 7761 | if (unres->type[i] != UNRES_WHEN) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7762 | continue; |
| 7763 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7764 | if (first) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7765 | /* count when-stmt nodes in unres list */ |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 7766 | stmt_count++; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7767 | } |
| 7768 | |
| 7769 | /* resolve when condition only when all parent when conditions are already resolved */ |
| 7770 | for (parent = unres->node[i]->parent; |
| 7771 | parent && LYD_WHEN_DONE(parent->when_status); |
| 7772 | parent = parent->parent) { |
| 7773 | if (!parent->parent && (parent->when_status & LYD_WHEN_FALSE)) { |
| 7774 | /* the parent node was already unlinked, do not resolve this node, |
| 7775 | * it will be removed anyway, so just mark it as resolved |
| 7776 | */ |
| 7777 | unres->node[i]->when_status |= LYD_WHEN_FALSE; |
| 7778 | unres->type[i] = UNRES_RESOLVED; |
| 7779 | resolved++; |
| 7780 | break; |
| 7781 | } |
| 7782 | } |
| 7783 | if (parent) { |
| 7784 | continue; |
| 7785 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7786 | |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7787 | rc = resolve_unres_data_item(unres->node[i], unres->type[i], ignore_fail); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7788 | if (!rc) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7789 | if (unres->node[i]->when_status & LYD_WHEN_FALSE) { |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 7790 | if ((options & LYD_OPT_NOAUTODEL) && !unres->node[i]->dflt) { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7791 | /* false when condition */ |
| 7792 | ly_vlog_hide(0); |
Radek Krejci | 2467a49 | 2016-10-24 15:16:59 +0200 | [diff] [blame] | 7793 | ly_err_repeat(); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7794 | return -1; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7795 | } /* follows else */ |
| 7796 | |
Michal Vasko | e31d34a | 2017-03-28 14:50:38 +0200 | [diff] [blame] | 7797 | /* auto-delete */ |
| 7798 | LOGVRB("auto-delete node \"%s\" due to when condition (%s)", ly_errpath(), |
| 7799 | ((struct lys_node_leaf *)unres->node[i]->schema)->when->cond); |
| 7800 | |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7801 | /* only unlink now, the subtree can contain another nodes stored in the unres list */ |
| 7802 | /* if it has parent non-presence containers that would be empty, we should actually |
| 7803 | * remove the container |
| 7804 | */ |
Radek Krejci | 2537fd3 | 2016-09-07 16:22:41 +0200 | [diff] [blame] | 7805 | for (parent = unres->node[i]; |
| 7806 | parent->parent && parent->parent->schema->nodetype == LYS_CONTAINER; |
| 7807 | parent = parent->parent) { |
| 7808 | if (((struct lys_node_container *)parent->parent->schema)->presence) { |
| 7809 | /* presence container */ |
| 7810 | break; |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7811 | } |
Radek Krejci | 2537fd3 | 2016-09-07 16:22:41 +0200 | [diff] [blame] | 7812 | if (parent->next || parent->prev != parent) { |
| 7813 | /* non empty (the child we are in and we are going to remove is not the only child) */ |
| 7814 | break; |
| 7815 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7816 | } |
Radek Krejci | 2537fd3 | 2016-09-07 16:22:41 +0200 | [diff] [blame] | 7817 | unres->node[i] = parent; |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7818 | |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7819 | if (*root && *root == unres->node[i]) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7820 | *root = (*root)->next; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7821 | } |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7822 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7823 | lyd_unlink(unres->node[i]); |
| 7824 | unres->type[i] = UNRES_DELETE; |
| 7825 | del_items++; |
Radek Krejci | 51fd816 | 2016-03-24 15:49:51 +0100 | [diff] [blame] | 7826 | |
| 7827 | /* update the rest of unres items */ |
| 7828 | for (j = 0; j < unres->count; j++) { |
Radek Krejci | 3db819b | 2016-03-24 16:29:48 +0100 | [diff] [blame] | 7829 | if (unres->type[j] == UNRES_RESOLVED || unres->type[j] == UNRES_DELETE) { |
Radek Krejci | 51fd816 | 2016-03-24 15:49:51 +0100 | [diff] [blame] | 7830 | continue; |
| 7831 | } |
| 7832 | |
| 7833 | /* test if the node is in subtree to be deleted */ |
| 7834 | for (parent = unres->node[j]; parent; parent = parent->parent) { |
| 7835 | if (parent == unres->node[i]) { |
| 7836 | /* yes, it is */ |
| 7837 | unres->type[j] = UNRES_RESOLVED; |
| 7838 | resolved++; |
| 7839 | break; |
| 7840 | } |
| 7841 | } |
| 7842 | } |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7843 | } else { |
| 7844 | unres->type[i] = UNRES_RESOLVED; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7845 | } |
Radek Krejci | 00a0e71 | 2016-10-26 10:24:46 +0200 | [diff] [blame] | 7846 | ly_err_clean(1); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7847 | resolved++; |
| 7848 | progress = 1; |
| 7849 | } else if (rc == -1) { |
| 7850 | ly_vlog_hide(0); |
Michal Vasko | 76e7340 | 2016-08-24 16:00:13 +0200 | [diff] [blame] | 7851 | /* print only this last error */ |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7852 | resolve_unres_data_item(unres->node[i], unres->type[i], ignore_fail); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7853 | return -1; |
Radek Krejci | 2467a49 | 2016-10-24 15:16:59 +0200 | [diff] [blame] | 7854 | } /* else forward reference */ |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7855 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7856 | first = 0; |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 7857 | } while (progress && resolved < stmt_count); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7858 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7859 | /* do we have some unresolved when-stmt? */ |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 7860 | if (stmt_count > resolved) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7861 | ly_vlog_hide(0); |
Radek Krejci | 2467a49 | 2016-10-24 15:16:59 +0200 | [diff] [blame] | 7862 | ly_err_repeat(); |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7863 | return -1; |
| 7864 | } |
| 7865 | |
| 7866 | for (i = 0; del_items && i < unres->count; i++) { |
| 7867 | /* we had some when-stmt resulted to false, so now we have to sanitize the unres list */ |
| 7868 | if (unres->type[i] != UNRES_DELETE) { |
| 7869 | continue; |
| 7870 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7871 | if (!unres->node[i]) { |
| 7872 | unres->type[i] = UNRES_RESOLVED; |
| 7873 | del_items--; |
| 7874 | continue; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7875 | } |
| 7876 | |
| 7877 | /* really remove the complete subtree */ |
| 7878 | lyd_free(unres->node[i]); |
| 7879 | unres->type[i] = UNRES_RESOLVED; |
| 7880 | del_items--; |
| 7881 | } |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 7882 | |
| 7883 | /* now leafrefs */ |
| 7884 | first = 1; |
| 7885 | stmt_count = 0; |
| 7886 | resolved = 0; |
| 7887 | do { |
| 7888 | progress = 0; |
| 7889 | for (i = 0; i < unres->count; i++) { |
| 7890 | if (unres->type[i] != UNRES_LEAFREF) { |
| 7891 | continue; |
| 7892 | } |
| 7893 | if (first) { |
| 7894 | /* count leafref nodes in unres list */ |
| 7895 | stmt_count++; |
| 7896 | } |
| 7897 | |
| 7898 | rc = resolve_unres_data_item(unres->node[i], unres->type[i], ignore_fail); |
| 7899 | if (!rc) { |
| 7900 | unres->type[i] = UNRES_RESOLVED; |
| 7901 | ly_err_clean(1); |
| 7902 | resolved++; |
| 7903 | progress = 1; |
| 7904 | } else if (rc == -1) { |
| 7905 | ly_vlog_hide(0); |
| 7906 | /* print only this last error */ |
| 7907 | resolve_unres_data_item(unres->node[i], unres->type[i], ignore_fail); |
| 7908 | return -1; |
| 7909 | } /* else forward reference */ |
| 7910 | } |
| 7911 | first = 0; |
| 7912 | } while (progress && resolved < stmt_count); |
| 7913 | |
| 7914 | /* do we have some unresolved leafrefs? */ |
| 7915 | if (stmt_count > resolved) { |
| 7916 | ly_vlog_hide(0); |
| 7917 | ly_err_repeat(); |
| 7918 | return -1; |
| 7919 | } |
| 7920 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7921 | ly_vlog_hide(0); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7922 | |
| 7923 | /* rest */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7924 | for (i = 0; i < unres->count; ++i) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7925 | if (unres->type[i] == UNRES_RESOLVED) { |
| 7926 | continue; |
| 7927 | } |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 7928 | 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] | 7929 | |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7930 | rc = resolve_unres_data_item(unres->node[i], unres->type[i], ignore_fail); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7931 | if (rc) { |
| 7932 | /* since when was already resolved, a forward reference is an error */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7933 | return -1; |
| 7934 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7935 | |
| 7936 | unres->type[i] = UNRES_RESOLVED; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7937 | } |
| 7938 | |
Michal Vasko | a0ffcab | 2016-05-02 14:52:08 +0200 | [diff] [blame] | 7939 | LOGVRB("All data nodes and constraints resolved."); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7940 | unres->count = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7941 | return EXIT_SUCCESS; |
| 7942 | } |