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. |
| 158 | * |
| 159 | * @return Number of characters successfully parsed, |
| 160 | * positive on success, negative on failure. |
| 161 | */ |
| 162 | static int |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 163 | parse_node_identifier(const char *id, const char **mod_name, int *mod_name_len, const char **name, int *nam_len) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 164 | { |
| 165 | int parsed = 0, ret; |
| 166 | |
| 167 | assert(id); |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 168 | if (mod_name) { |
| 169 | *mod_name = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 170 | } |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 171 | if (mod_name_len) { |
| 172 | *mod_name_len = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 173 | } |
| 174 | if (name) { |
| 175 | *name = NULL; |
| 176 | } |
| 177 | if (nam_len) { |
| 178 | *nam_len = 0; |
| 179 | } |
| 180 | |
| 181 | if ((ret = parse_identifier(id)) < 1) { |
| 182 | return ret; |
| 183 | } |
| 184 | |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 185 | if (mod_name) { |
| 186 | *mod_name = id; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 187 | } |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 188 | if (mod_name_len) { |
| 189 | *mod_name_len = ret; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | parsed += ret; |
| 193 | id += ret; |
| 194 | |
| 195 | /* there is prefix */ |
| 196 | if (id[0] == ':') { |
| 197 | ++parsed; |
| 198 | ++id; |
| 199 | |
| 200 | /* there isn't */ |
| 201 | } else { |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 202 | if (name && mod_name) { |
| 203 | *name = *mod_name; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 204 | } |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 205 | if (mod_name) { |
| 206 | *mod_name = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 207 | } |
| 208 | |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 209 | if (nam_len && mod_name_len) { |
| 210 | *nam_len = *mod_name_len; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 211 | } |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 212 | if (mod_name_len) { |
| 213 | *mod_name_len = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | return parsed; |
| 217 | } |
| 218 | |
| 219 | /* identifier (node name) */ |
| 220 | if ((ret = parse_identifier(id)) < 1) { |
| 221 | return -parsed+ret; |
| 222 | } |
| 223 | |
| 224 | if (name) { |
| 225 | *name = id; |
| 226 | } |
| 227 | if (nam_len) { |
| 228 | *nam_len = ret; |
| 229 | } |
| 230 | |
| 231 | return parsed+ret; |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * @brief Parse a path-predicate (leafref). |
| 236 | * |
| 237 | * path-predicate = "[" *WSP path-equality-expr *WSP "]" |
| 238 | * path-equality-expr = node-identifier *WSP "=" *WSP path-key-expr |
| 239 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 240 | * @param[in] id Identifier to use. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 241 | * @param[out] prefix Points to the prefix, NULL if there is not any. |
| 242 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 243 | * @param[out] name Points to the node name. |
| 244 | * @param[out] nam_len Length of the node name. |
| 245 | * @param[out] path_key_expr Points to the path-key-expr. |
| 246 | * @param[out] pke_len Length of the path-key-expr. |
| 247 | * @param[out] has_predicate Flag to mark whether there is another predicate following. |
| 248 | * |
| 249 | * @return Number of characters successfully parsed, |
| 250 | * positive on success, negative on failure. |
| 251 | */ |
| 252 | static int |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 253 | parse_path_predicate(const char *id, const char **prefix, int *pref_len, const char **name, int *nam_len, |
| 254 | const char **path_key_expr, int *pke_len, int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 255 | { |
| 256 | const char *ptr; |
| 257 | int parsed = 0, ret; |
| 258 | |
| 259 | assert(id); |
| 260 | if (prefix) { |
| 261 | *prefix = NULL; |
| 262 | } |
| 263 | if (pref_len) { |
| 264 | *pref_len = 0; |
| 265 | } |
| 266 | if (name) { |
| 267 | *name = NULL; |
| 268 | } |
| 269 | if (nam_len) { |
| 270 | *nam_len = 0; |
| 271 | } |
| 272 | if (path_key_expr) { |
| 273 | *path_key_expr = NULL; |
| 274 | } |
| 275 | if (pke_len) { |
| 276 | *pke_len = 0; |
| 277 | } |
| 278 | if (has_predicate) { |
| 279 | *has_predicate = 0; |
| 280 | } |
| 281 | |
| 282 | if (id[0] != '[') { |
| 283 | return -parsed; |
| 284 | } |
| 285 | |
| 286 | ++parsed; |
| 287 | ++id; |
| 288 | |
| 289 | while (isspace(id[0])) { |
| 290 | ++parsed; |
| 291 | ++id; |
| 292 | } |
| 293 | |
| 294 | if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len)) < 1) { |
| 295 | return -parsed+ret; |
| 296 | } |
| 297 | |
| 298 | parsed += ret; |
| 299 | id += ret; |
| 300 | |
| 301 | while (isspace(id[0])) { |
| 302 | ++parsed; |
| 303 | ++id; |
| 304 | } |
| 305 | |
| 306 | if (id[0] != '=') { |
| 307 | return -parsed; |
| 308 | } |
| 309 | |
| 310 | ++parsed; |
| 311 | ++id; |
| 312 | |
| 313 | while (isspace(id[0])) { |
| 314 | ++parsed; |
| 315 | ++id; |
| 316 | } |
| 317 | |
| 318 | if ((ptr = strchr(id, ']')) == NULL) { |
| 319 | return -parsed; |
| 320 | } |
| 321 | |
| 322 | --ptr; |
| 323 | while (isspace(ptr[0])) { |
| 324 | --ptr; |
| 325 | } |
| 326 | ++ptr; |
| 327 | |
| 328 | ret = ptr-id; |
| 329 | if (path_key_expr) { |
| 330 | *path_key_expr = id; |
| 331 | } |
| 332 | if (pke_len) { |
| 333 | *pke_len = ret; |
| 334 | } |
| 335 | |
| 336 | parsed += ret; |
| 337 | id += ret; |
| 338 | |
| 339 | while (isspace(id[0])) { |
| 340 | ++parsed; |
| 341 | ++id; |
| 342 | } |
| 343 | |
| 344 | assert(id[0] == ']'); |
| 345 | |
| 346 | if (id[1] == '[') { |
| 347 | *has_predicate = 1; |
| 348 | } |
| 349 | |
| 350 | return parsed+1; |
| 351 | } |
| 352 | |
| 353 | /** |
| 354 | * @brief Parse a path-key-expr (leafref). First call parses "current()", all |
| 355 | * the ".." and the first node-identifier, other calls parse a single |
| 356 | * node-identifier each. |
| 357 | * |
| 358 | * path-key-expr = current-function-invocation *WSP "/" *WSP |
| 359 | * rel-path-keyexpr |
| 360 | * rel-path-keyexpr = 1*(".." *WSP "/" *WSP) |
| 361 | * *(node-identifier *WSP "/" *WSP) |
| 362 | * node-identifier |
| 363 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 364 | * @param[in] id Identifier to use. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 365 | * @param[out] prefix Points to the prefix, NULL if there is not any. |
| 366 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 367 | * @param[out] name Points to the node name. |
| 368 | * @param[out] nam_len Length of the node name. |
| 369 | * @param[out] parent_times Number of ".." in the path. Must be 0 on the first call, |
| 370 | * must not be changed between consecutive calls. |
| 371 | * @return Number of characters successfully parsed, |
| 372 | * positive on success, negative on failure. |
| 373 | */ |
| 374 | static int |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 375 | parse_path_key_expr(const char *id, const char **prefix, int *pref_len, const char **name, int *nam_len, |
| 376 | int *parent_times) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 377 | { |
| 378 | int parsed = 0, ret, par_times = 0; |
| 379 | |
| 380 | assert(id); |
| 381 | assert(parent_times); |
| 382 | if (prefix) { |
| 383 | *prefix = NULL; |
| 384 | } |
| 385 | if (pref_len) { |
| 386 | *pref_len = 0; |
| 387 | } |
| 388 | if (name) { |
| 389 | *name = NULL; |
| 390 | } |
| 391 | if (nam_len) { |
| 392 | *nam_len = 0; |
| 393 | } |
| 394 | |
| 395 | if (!*parent_times) { |
| 396 | /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */ |
| 397 | if (strncmp(id, "current()", 9)) { |
| 398 | return -parsed; |
| 399 | } |
| 400 | |
| 401 | parsed += 9; |
| 402 | id += 9; |
| 403 | |
| 404 | while (isspace(id[0])) { |
| 405 | ++parsed; |
| 406 | ++id; |
| 407 | } |
| 408 | |
| 409 | if (id[0] != '/') { |
| 410 | return -parsed; |
| 411 | } |
| 412 | |
| 413 | ++parsed; |
| 414 | ++id; |
| 415 | |
| 416 | while (isspace(id[0])) { |
| 417 | ++parsed; |
| 418 | ++id; |
| 419 | } |
| 420 | |
| 421 | /* rel-path-keyexpr */ |
| 422 | if (strncmp(id, "..", 2)) { |
| 423 | return -parsed; |
| 424 | } |
| 425 | ++par_times; |
| 426 | |
| 427 | parsed += 2; |
| 428 | id += 2; |
| 429 | |
| 430 | while (isspace(id[0])) { |
| 431 | ++parsed; |
| 432 | ++id; |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | /* 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier |
| 437 | * |
| 438 | * first parent reference with whitespaces already parsed |
| 439 | */ |
| 440 | if (id[0] != '/') { |
| 441 | return -parsed; |
| 442 | } |
| 443 | |
| 444 | ++parsed; |
| 445 | ++id; |
| 446 | |
| 447 | while (isspace(id[0])) { |
| 448 | ++parsed; |
| 449 | ++id; |
| 450 | } |
| 451 | |
| 452 | while (!strncmp(id, "..", 2) && !*parent_times) { |
| 453 | ++par_times; |
| 454 | |
| 455 | parsed += 2; |
| 456 | id += 2; |
| 457 | |
| 458 | while (isspace(id[0])) { |
| 459 | ++parsed; |
| 460 | ++id; |
| 461 | } |
| 462 | |
| 463 | if (id[0] != '/') { |
| 464 | return -parsed; |
| 465 | } |
| 466 | |
| 467 | ++parsed; |
| 468 | ++id; |
| 469 | |
| 470 | while (isspace(id[0])) { |
| 471 | ++parsed; |
| 472 | ++id; |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | if (!*parent_times) { |
| 477 | *parent_times = par_times; |
| 478 | } |
| 479 | |
| 480 | /* all parent references must be parsed at this point */ |
| 481 | if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len)) < 1) { |
| 482 | return -parsed+ret; |
| 483 | } |
| 484 | |
| 485 | parsed += ret; |
| 486 | id += ret; |
| 487 | |
| 488 | return parsed; |
| 489 | } |
| 490 | |
| 491 | /** |
| 492 | * @brief Parse path-arg (leafref). |
| 493 | * |
| 494 | * path-arg = absolute-path / relative-path |
| 495 | * absolute-path = 1*("/" (node-identifier *path-predicate)) |
| 496 | * relative-path = 1*(".." "/") descendant-path |
| 497 | * |
Radek Krejci | f7ed4c3 | 2016-10-27 16:20:03 +0200 | [diff] [blame] | 498 | * @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] | 499 | * @param[in] id Identifier to use. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 500 | * @param[out] prefix Points to the prefix, NULL if there is not any. |
| 501 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 502 | * @param[out] name Points to the node name. |
| 503 | * @param[out] nam_len Length of the node name. |
| 504 | * @param[out] parent_times Number of ".." in the path. Must be 0 on the first call, |
| 505 | * must not be changed between consecutive calls. -1 if the |
| 506 | * path is relative. |
| 507 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 508 | * |
| 509 | * @return Number of characters successfully parsed, |
| 510 | * positive on success, negative on failure. |
| 511 | */ |
| 512 | static int |
Radek Krejci | f7ed4c3 | 2016-10-27 16:20:03 +0200 | [diff] [blame] | 513 | parse_path_arg(struct lys_module *mod, const char *id, const char **prefix, int *pref_len, |
| 514 | const char **name, int *nam_len, int *parent_times, int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 515 | { |
| 516 | int parsed = 0, ret, par_times = 0; |
| 517 | |
| 518 | assert(id); |
| 519 | assert(parent_times); |
| 520 | if (prefix) { |
| 521 | *prefix = NULL; |
| 522 | } |
| 523 | if (pref_len) { |
| 524 | *pref_len = 0; |
| 525 | } |
| 526 | if (name) { |
| 527 | *name = NULL; |
| 528 | } |
| 529 | if (nam_len) { |
| 530 | *nam_len = 0; |
| 531 | } |
| 532 | if (has_predicate) { |
| 533 | *has_predicate = 0; |
| 534 | } |
| 535 | |
| 536 | if (!*parent_times && !strncmp(id, "..", 2)) { |
| 537 | ++par_times; |
| 538 | |
| 539 | parsed += 2; |
| 540 | id += 2; |
| 541 | |
| 542 | while (!strncmp(id, "/..", 3)) { |
| 543 | ++par_times; |
| 544 | |
| 545 | parsed += 3; |
| 546 | id += 3; |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | if (!*parent_times) { |
| 551 | if (par_times) { |
| 552 | *parent_times = par_times; |
| 553 | } else { |
| 554 | *parent_times = -1; |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | if (id[0] != '/') { |
| 559 | return -parsed; |
| 560 | } |
| 561 | |
| 562 | /* skip '/' */ |
| 563 | ++parsed; |
| 564 | ++id; |
| 565 | |
| 566 | /* node-identifier ([prefix:]identifier) */ |
| 567 | if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len)) < 1) { |
| 568 | return -parsed-ret; |
| 569 | } |
Radek Krejci | f7ed4c3 | 2016-10-27 16:20:03 +0200 | [diff] [blame] | 570 | if (!(*prefix)) { |
| 571 | /* actually we always need prefix even it is not specified */ |
| 572 | *prefix = lys_main_module(mod)->name; |
| 573 | *pref_len = strlen(*prefix); |
| 574 | } |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 575 | |
| 576 | parsed += ret; |
| 577 | id += ret; |
| 578 | |
| 579 | /* there is no predicate */ |
| 580 | if ((id[0] == '/') || !id[0]) { |
| 581 | return parsed; |
| 582 | } else if (id[0] != '[') { |
| 583 | return -parsed; |
| 584 | } |
| 585 | |
| 586 | if (has_predicate) { |
| 587 | *has_predicate = 1; |
| 588 | } |
| 589 | |
| 590 | return parsed; |
| 591 | } |
| 592 | |
| 593 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 594 | * @brief Parse instance-identifier in JSON data format. That means that prefixes |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 595 | * (which are mandatory for every node-identifier) are actually model names. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 596 | * |
| 597 | * instance-identifier = 1*("/" (node-identifier *predicate)) |
| 598 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 599 | * @param[in] id Identifier to use. |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 600 | * @param[out] model Points to the model name. |
| 601 | * @param[out] mod_len Length of the model name. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 602 | * @param[out] name Points to the node name. |
| 603 | * @param[out] nam_len Length of the node name. |
| 604 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 605 | * |
| 606 | * @return Number of characters successfully parsed, |
| 607 | * positive on success, negative on failure. |
| 608 | */ |
| 609 | static int |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 610 | parse_instance_identifier(const char *id, const char **model, int *mod_len, const char **name, int *nam_len, |
| 611 | int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 612 | { |
| 613 | int parsed = 0, ret; |
| 614 | |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 615 | if (has_predicate) { |
| 616 | *has_predicate = 0; |
| 617 | } |
| 618 | |
| 619 | if (id[0] != '/') { |
| 620 | return -parsed; |
| 621 | } |
| 622 | |
| 623 | ++parsed; |
| 624 | ++id; |
| 625 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 626 | if ((ret = parse_identifier(id)) < 1) { |
| 627 | return ret; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 628 | } |
| 629 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 630 | *model = id; |
| 631 | *mod_len = ret; |
| 632 | |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 633 | parsed += ret; |
| 634 | id += ret; |
| 635 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 636 | if (id[0] != ':') { |
| 637 | return -parsed; |
| 638 | } |
| 639 | |
| 640 | ++parsed; |
| 641 | ++id; |
| 642 | |
| 643 | if ((ret = parse_identifier(id)) < 1) { |
| 644 | return ret; |
| 645 | } |
| 646 | |
| 647 | *name = id; |
| 648 | *nam_len = ret; |
| 649 | |
| 650 | parsed += ret; |
| 651 | id += ret; |
| 652 | |
Radek Krejci | 4967cb6 | 2016-09-14 16:40:28 +0200 | [diff] [blame] | 653 | if (id[0] == '[' && has_predicate) { |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 654 | *has_predicate = 1; |
| 655 | } |
| 656 | |
| 657 | return parsed; |
| 658 | } |
| 659 | |
| 660 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 661 | * @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] | 662 | * (which are mandatory) are actually model names. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 663 | * |
| 664 | * predicate = "[" *WSP (predicate-expr / pos) *WSP "]" |
| 665 | * predicate-expr = (node-identifier / ".") *WSP "=" *WSP |
| 666 | * ((DQUOTE string DQUOTE) / |
| 667 | * (SQUOTE string SQUOTE)) |
| 668 | * pos = non-negative-integer-value |
| 669 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 670 | * @param[in] id Identifier to use. |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 671 | * @param[out] model Points to the model name. |
| 672 | * @param[out] mod_len Length of the model name. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 673 | * @param[out] name Points to the node name. Can be identifier (from node-identifier), "." or pos. |
| 674 | * @param[out] nam_len Length of the node name. |
| 675 | * @param[out] value Value the node-identifier must have (string from the grammar), |
| 676 | * NULL if there is not any. |
| 677 | * @param[out] val_len Length of the value, 0 if there is not any. |
| 678 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 679 | * |
| 680 | * @return Number of characters successfully parsed, |
| 681 | * positive on success, negative on failure. |
| 682 | */ |
| 683 | static int |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 684 | parse_predicate(const char *id, const char **model, int *mod_len, const char **name, int *nam_len, |
| 685 | const char **value, int *val_len, int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 686 | { |
| 687 | const char *ptr; |
| 688 | int parsed = 0, ret; |
| 689 | char quote; |
| 690 | |
| 691 | assert(id); |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 692 | if (model) { |
| 693 | *model = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 694 | } |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 695 | if (mod_len) { |
| 696 | *mod_len = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 697 | } |
| 698 | if (name) { |
| 699 | *name = NULL; |
| 700 | } |
| 701 | if (nam_len) { |
| 702 | *nam_len = 0; |
| 703 | } |
| 704 | if (value) { |
| 705 | *value = NULL; |
| 706 | } |
| 707 | if (val_len) { |
| 708 | *val_len = 0; |
| 709 | } |
| 710 | if (has_predicate) { |
| 711 | *has_predicate = 0; |
| 712 | } |
| 713 | |
| 714 | if (id[0] != '[') { |
| 715 | return -parsed; |
| 716 | } |
| 717 | |
| 718 | ++parsed; |
| 719 | ++id; |
| 720 | |
| 721 | while (isspace(id[0])) { |
| 722 | ++parsed; |
| 723 | ++id; |
| 724 | } |
| 725 | |
| 726 | /* pos */ |
| 727 | if (isdigit(id[0])) { |
| 728 | if (name) { |
| 729 | *name = id; |
| 730 | } |
| 731 | |
| 732 | if (id[0] == '0') { |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 733 | return -parsed; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | while (isdigit(id[0])) { |
| 737 | ++parsed; |
| 738 | ++id; |
| 739 | } |
| 740 | |
| 741 | if (nam_len) { |
| 742 | *nam_len = id-(*name); |
| 743 | } |
| 744 | |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 745 | /* "." or node-identifier */ |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 746 | } else { |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 747 | if (id[0] == '.') { |
| 748 | if (name) { |
| 749 | *name = id; |
| 750 | } |
| 751 | if (nam_len) { |
| 752 | *nam_len = 1; |
| 753 | } |
| 754 | |
| 755 | ++parsed; |
| 756 | ++id; |
| 757 | |
| 758 | } else { |
| 759 | if ((ret = parse_node_identifier(id, model, mod_len, name, nam_len)) < 1) { |
| 760 | return -parsed+ret; |
| 761 | } else if (model && !*model) { |
| 762 | return -parsed; |
| 763 | } |
| 764 | |
| 765 | parsed += ret; |
| 766 | id += ret; |
| 767 | } |
| 768 | |
| 769 | while (isspace(id[0])) { |
| 770 | ++parsed; |
| 771 | ++id; |
| 772 | } |
| 773 | |
| 774 | if (id[0] != '=') { |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 775 | return -parsed; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 776 | } |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 777 | |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 778 | ++parsed; |
| 779 | ++id; |
| 780 | |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 781 | while (isspace(id[0])) { |
| 782 | ++parsed; |
| 783 | ++id; |
| 784 | } |
| 785 | |
| 786 | /* ((DQUOTE string DQUOTE) / (SQUOTE string SQUOTE)) */ |
| 787 | if ((id[0] == '\"') || (id[0] == '\'')) { |
| 788 | quote = id[0]; |
| 789 | |
| 790 | ++parsed; |
| 791 | ++id; |
| 792 | |
| 793 | if ((ptr = strchr(id, quote)) == NULL) { |
| 794 | return -parsed; |
| 795 | } |
| 796 | ret = ptr-id; |
| 797 | |
| 798 | if (value) { |
| 799 | *value = id; |
| 800 | } |
| 801 | if (val_len) { |
| 802 | *val_len = ret; |
| 803 | } |
| 804 | |
| 805 | parsed += ret+1; |
| 806 | id += ret+1; |
| 807 | } else { |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 808 | return -parsed; |
| 809 | } |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | while (isspace(id[0])) { |
| 813 | ++parsed; |
| 814 | ++id; |
| 815 | } |
| 816 | |
| 817 | if (id[0] != ']') { |
| 818 | return -parsed; |
| 819 | } |
| 820 | |
| 821 | ++parsed; |
| 822 | ++id; |
| 823 | |
| 824 | if ((id[0] == '[') && has_predicate) { |
| 825 | *has_predicate = 1; |
| 826 | } |
| 827 | |
| 828 | return parsed; |
| 829 | } |
| 830 | |
| 831 | /** |
| 832 | * @brief Parse schema-nodeid. |
| 833 | * |
| 834 | * schema-nodeid = absolute-schema-nodeid / |
| 835 | * descendant-schema-nodeid |
| 836 | * absolute-schema-nodeid = 1*("/" node-identifier) |
Michal Vasko | 4893535 | 2016-03-29 11:52:36 +0200 | [diff] [blame] | 837 | * descendant-schema-nodeid = ["." "/"] |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 838 | * node-identifier |
| 839 | * absolute-schema-nodeid |
| 840 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 841 | * @param[in] id Identifier to use. |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 842 | * @param[out] mod_name Points to the module name, NULL if there is not any. |
| 843 | * @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] | 844 | * @param[out] name Points to the node name. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 845 | * @param[out] nam_len Length of the node name. |
| 846 | * @param[out] is_relative Flag to mark whether the nodeid is absolute or descendant. Must be -1 |
| 847 | * on the first call, must not be changed between consecutive calls. |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 848 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. It cannot be |
| 849 | * based on the grammar, in those cases use NULL. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 850 | * |
| 851 | * @return Number of characters successfully parsed, |
| 852 | * positive on success, negative on failure. |
| 853 | */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 854 | int |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 855 | parse_schema_nodeid(const char *id, const char **mod_name, int *mod_name_len, const char **name, int *nam_len, |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 856 | int *is_relative, int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 857 | { |
| 858 | int parsed = 0, ret; |
| 859 | |
| 860 | assert(id); |
| 861 | assert(is_relative); |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 862 | if (mod_name) { |
| 863 | *mod_name = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 864 | } |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 865 | if (mod_name_len) { |
| 866 | *mod_name_len = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 867 | } |
| 868 | if (name) { |
| 869 | *name = NULL; |
| 870 | } |
| 871 | if (nam_len) { |
| 872 | *nam_len = 0; |
| 873 | } |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 874 | if (has_predicate) { |
| 875 | *has_predicate = 0; |
| 876 | } |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 877 | |
| 878 | if (id[0] != '/') { |
| 879 | if (*is_relative != -1) { |
| 880 | return -parsed; |
| 881 | } else { |
| 882 | *is_relative = 1; |
| 883 | } |
Michal Vasko | 4893535 | 2016-03-29 11:52:36 +0200 | [diff] [blame] | 884 | if (!strncmp(id, "./", 2)) { |
| 885 | parsed += 2; |
| 886 | id += 2; |
| 887 | } |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 888 | } else { |
| 889 | if (*is_relative == -1) { |
| 890 | *is_relative = 0; |
| 891 | } |
| 892 | ++parsed; |
| 893 | ++id; |
| 894 | } |
| 895 | |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 896 | if ((ret = parse_node_identifier(id, mod_name, mod_name_len, name, nam_len)) < 1) { |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 897 | return -parsed+ret; |
| 898 | } |
| 899 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 900 | parsed += ret; |
| 901 | id += ret; |
| 902 | |
| 903 | if ((id[0] == '[') && has_predicate) { |
| 904 | *has_predicate = 1; |
| 905 | } |
| 906 | |
| 907 | return parsed; |
| 908 | } |
| 909 | |
| 910 | /** |
| 911 | * @brief Parse schema predicate (special format internally used). |
| 912 | * |
| 913 | * predicate = "[" *WSP predicate-expr *WSP "]" |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 914 | * predicate-expr = "." / identifier / positive-integer / key-with-value |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 915 | * key-with-value = identifier *WSP "=" *WSP |
| 916 | * ((DQUOTE string DQUOTE) / |
| 917 | * (SQUOTE string SQUOTE)) |
| 918 | * |
| 919 | * @param[in] id Identifier to use. |
| 920 | * @param[out] name Points to the list key name. |
| 921 | * @param[out] nam_len Length of \p name. |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 922 | * @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] | 923 | * @param[out] val_len Length of \p value. |
| 924 | * @param[out] has_predicate Flag to mark whether there is another predicate specified. |
| 925 | */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 926 | int |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 927 | parse_schema_json_predicate(const char *id, const char **name, int *nam_len, const char **value, int *val_len, |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 928 | int *has_predicate) |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 929 | { |
| 930 | const char *ptr; |
| 931 | int parsed = 0, ret; |
| 932 | char quote; |
| 933 | |
| 934 | assert(id); |
| 935 | if (name) { |
| 936 | *name = NULL; |
| 937 | } |
| 938 | if (nam_len) { |
| 939 | *nam_len = 0; |
| 940 | } |
| 941 | if (value) { |
| 942 | *value = NULL; |
| 943 | } |
| 944 | if (val_len) { |
| 945 | *val_len = 0; |
| 946 | } |
| 947 | if (has_predicate) { |
| 948 | *has_predicate = 0; |
| 949 | } |
| 950 | |
| 951 | if (id[0] != '[') { |
| 952 | return -parsed; |
| 953 | } |
| 954 | |
| 955 | ++parsed; |
| 956 | ++id; |
| 957 | |
| 958 | while (isspace(id[0])) { |
| 959 | ++parsed; |
| 960 | ++id; |
| 961 | } |
| 962 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 963 | /* identifier */ |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 964 | if (id[0] == '.') { |
| 965 | ret = 1; |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 966 | } else if (isdigit(id[0])) { |
| 967 | if (id[0] == '0') { |
| 968 | return -parsed; |
| 969 | } |
| 970 | ret = 1; |
| 971 | while (isdigit(id[ret])) { |
| 972 | ++ret; |
| 973 | } |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 974 | } else if ((ret = parse_identifier(id)) < 1) { |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 975 | return -parsed + ret; |
| 976 | } |
| 977 | if (name) { |
| 978 | *name = id; |
| 979 | } |
| 980 | if (nam_len) { |
| 981 | *nam_len = ret; |
| 982 | } |
| 983 | |
| 984 | parsed += ret; |
| 985 | id += ret; |
| 986 | |
| 987 | while (isspace(id[0])) { |
| 988 | ++parsed; |
| 989 | ++id; |
| 990 | } |
| 991 | |
| 992 | /* there is value as well */ |
| 993 | if (id[0] == '=') { |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 994 | if (name && isdigit(**name)) { |
| 995 | return -parsed; |
| 996 | } |
| 997 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 998 | ++parsed; |
| 999 | ++id; |
| 1000 | |
| 1001 | while (isspace(id[0])) { |
| 1002 | ++parsed; |
| 1003 | ++id; |
| 1004 | } |
| 1005 | |
| 1006 | /* ((DQUOTE string DQUOTE) / (SQUOTE string SQUOTE)) */ |
| 1007 | if ((id[0] == '\"') || (id[0] == '\'')) { |
| 1008 | quote = id[0]; |
| 1009 | |
| 1010 | ++parsed; |
| 1011 | ++id; |
| 1012 | |
| 1013 | if ((ptr = strchr(id, quote)) == NULL) { |
| 1014 | return -parsed; |
| 1015 | } |
| 1016 | ret = ptr - id; |
| 1017 | |
| 1018 | if (value) { |
| 1019 | *value = id; |
| 1020 | } |
| 1021 | if (val_len) { |
| 1022 | *val_len = ret; |
| 1023 | } |
| 1024 | |
| 1025 | parsed += ret + 1; |
| 1026 | id += ret + 1; |
| 1027 | } else { |
| 1028 | return -parsed; |
| 1029 | } |
| 1030 | |
| 1031 | while (isspace(id[0])) { |
| 1032 | ++parsed; |
| 1033 | ++id; |
| 1034 | } |
| 1035 | } |
| 1036 | |
| 1037 | if (id[0] != ']') { |
| 1038 | return -parsed; |
| 1039 | } |
| 1040 | |
| 1041 | ++parsed; |
| 1042 | ++id; |
| 1043 | |
| 1044 | if ((id[0] == '[') && has_predicate) { |
| 1045 | *has_predicate = 1; |
| 1046 | } |
| 1047 | |
| 1048 | return parsed; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | /** |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1052 | * @brief Resolve (find) a feature definition. Logs directly. |
| 1053 | * |
| 1054 | * @param[in] feat_name Feature name to resolve. |
| 1055 | * @param[in] len Length of \p feat_name. |
| 1056 | * @param[in] node Node with the if-feature expression. |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1057 | * @param[out] feature Pointer to be set to point to the feature definition, if feature not found |
| 1058 | * (return code 1), the pointer is untouched. |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1059 | * |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1060 | * @return 0 on success, 1 on forward reference, -1 on error. |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1061 | */ |
| 1062 | static int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1063 | 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] | 1064 | { |
| 1065 | char *str; |
| 1066 | const char *mod_name, *name; |
| 1067 | int mod_name_len, nam_len, i, j; |
| 1068 | const struct lys_module *module; |
| 1069 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1070 | assert(feature); |
| 1071 | |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1072 | /* check prefix */ |
| 1073 | if ((i = parse_node_identifier(feat_name, &mod_name, &mod_name_len, &name, &nam_len)) < 1) { |
| 1074 | LOGVAL(LYE_INCHAR, LY_VLOG_NONE, NULL, feat_name[-i], &feat_name[-i]); |
| 1075 | return -1; |
| 1076 | } |
| 1077 | |
| 1078 | module = lys_get_import_module(lys_node_module(node), NULL, 0, mod_name, mod_name_len); |
| 1079 | if (!module) { |
| 1080 | /* identity refers unknown data model */ |
| 1081 | LOGVAL(LYE_INMOD_LEN, LY_VLOG_NONE, NULL, mod_name_len, mod_name); |
| 1082 | return -1; |
| 1083 | } |
| 1084 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1085 | if (module != node->module && module == lys_node_module(node)) { |
| 1086 | /* first, try to search directly in submodule where the feature was mentioned */ |
| 1087 | for (j = 0; j < node->module->features_size; j++) { |
| 1088 | if (!strncmp(name, node->module->features[j].name, nam_len) && !node->module->features[j].name[nam_len]) { |
| 1089 | /* check status */ |
| 1090 | 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] | 1091 | node->module->features[j].module, node->module->features[j].name, NULL)) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1092 | return -1; |
| 1093 | } |
| 1094 | *feature = &node->module->features[j]; |
| 1095 | return 0; |
| 1096 | } |
| 1097 | } |
| 1098 | } |
| 1099 | |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1100 | /* search in the identified module ... */ |
| 1101 | for (j = 0; j < module->features_size; j++) { |
Michal Vasko | 3def867 | 2016-07-01 11:43:09 +0200 | [diff] [blame] | 1102 | 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] | 1103 | /* check status */ |
| 1104 | 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] | 1105 | module->features[j].module, module->features[j].name, NULL)) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1106 | return -1; |
| 1107 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1108 | *feature = &module->features[j]; |
| 1109 | return 0; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1110 | } |
| 1111 | } |
| 1112 | /* ... and all its submodules */ |
Radek Krejci | d4c1d0f | 2017-01-19 16:11:38 +0100 | [diff] [blame] | 1113 | for (i = 0; i < module->inc_size && module->inc[i].submodule; i++) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1114 | for (j = 0; j < module->inc[i].submodule->features_size; j++) { |
Michal Vasko | 3def867 | 2016-07-01 11:43:09 +0200 | [diff] [blame] | 1115 | if (!strncmp(name, module->inc[i].submodule->features[j].name, nam_len) |
| 1116 | && !module->inc[i].submodule->features[j].name[nam_len]) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1117 | /* check status */ |
| 1118 | if (lyp_check_status(node->flags, lys_node_module(node), node->name, |
| 1119 | module->inc[i].submodule->features[j].flags, |
| 1120 | module->inc[i].submodule->features[j].module, |
Pavol Vican | fdab9f9 | 2016-09-07 15:23:27 +0200 | [diff] [blame] | 1121 | module->inc[i].submodule->features[j].name, NULL)) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1122 | return -1; |
| 1123 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1124 | *feature = &module->inc[i].submodule->features[j]; |
| 1125 | return 0; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1126 | } |
| 1127 | } |
| 1128 | } |
| 1129 | |
| 1130 | /* not found */ |
| 1131 | str = strndup(feat_name, len); |
| 1132 | LOGVAL(LYE_INRESOLV, LY_VLOG_NONE, NULL, "feature", str); |
| 1133 | free(str); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1134 | return 1; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1135 | } |
| 1136 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1137 | /* |
| 1138 | * @return |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1139 | * - 1 if enabled |
| 1140 | * - 0 if disabled |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1141 | */ |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1142 | static int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1143 | resolve_feature_value(const struct lys_feature *feat) |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1144 | { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1145 | int i; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1146 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1147 | for (i = 0; i < feat->iffeature_size; i++) { |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1148 | if (!resolve_iffeature(&feat->iffeature[i])) { |
Radek Krejci | af56633 | 2017-02-07 15:56:59 +0100 | [diff] [blame] | 1149 | return 0; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1150 | } |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1151 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1152 | |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1153 | return feat->flags & LYS_FENABLED ? 1 : 0; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1154 | } |
| 1155 | |
| 1156 | static int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1157 | 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] | 1158 | { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1159 | uint8_t op; |
Radek Krejci | af56633 | 2017-02-07 15:56:59 +0100 | [diff] [blame] | 1160 | int a, b; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1161 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1162 | op = iff_getop(expr->expr, *index_e); |
| 1163 | (*index_e)++; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1164 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1165 | switch (op) { |
| 1166 | case LYS_IFF_F: |
| 1167 | /* resolve feature */ |
| 1168 | return resolve_feature_value(expr->features[(*index_f)++]); |
| 1169 | case LYS_IFF_NOT: |
Radek Krejci | af56633 | 2017-02-07 15:56:59 +0100 | [diff] [blame] | 1170 | /* invert result */ |
| 1171 | return resolve_iffeature_recursive(expr, index_e, index_f) ? 0 : 1; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1172 | case LYS_IFF_AND: |
| 1173 | case LYS_IFF_OR: |
| 1174 | a = resolve_iffeature_recursive(expr, index_e, index_f); |
| 1175 | b = resolve_iffeature_recursive(expr, index_e, index_f); |
Radek Krejci | af56633 | 2017-02-07 15:56:59 +0100 | [diff] [blame] | 1176 | if (op == LYS_IFF_AND) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1177 | return a && b; |
| 1178 | } else { /* LYS_IFF_OR */ |
| 1179 | return a || b; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1180 | } |
| 1181 | } |
| 1182 | |
Radek Krejci | af56633 | 2017-02-07 15:56:59 +0100 | [diff] [blame] | 1183 | return 0; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1184 | } |
| 1185 | |
| 1186 | int |
| 1187 | resolve_iffeature(struct lys_iffeature *expr) |
| 1188 | { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1189 | int index_e = 0, index_f = 0; |
| 1190 | |
| 1191 | if (expr->expr) { |
Radek Krejci | af56633 | 2017-02-07 15:56:59 +0100 | [diff] [blame] | 1192 | return resolve_iffeature_recursive(expr, &index_e, &index_f); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1193 | } |
Radek Krejci | af56633 | 2017-02-07 15:56:59 +0100 | [diff] [blame] | 1194 | return 0; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1195 | } |
| 1196 | |
| 1197 | struct iff_stack { |
| 1198 | int size; |
| 1199 | int index; /* first empty item */ |
| 1200 | uint8_t *stack; |
| 1201 | }; |
| 1202 | |
| 1203 | static int |
| 1204 | iff_stack_push(struct iff_stack *stack, uint8_t value) |
| 1205 | { |
| 1206 | if (stack->index == stack->size) { |
| 1207 | stack->size += 4; |
| 1208 | stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack); |
| 1209 | if (!stack->stack) { |
| 1210 | LOGMEM; |
| 1211 | stack->size = 0; |
| 1212 | return EXIT_FAILURE; |
| 1213 | } |
| 1214 | } |
| 1215 | |
| 1216 | stack->stack[stack->index++] = value; |
| 1217 | return EXIT_SUCCESS; |
| 1218 | } |
| 1219 | |
| 1220 | static uint8_t |
| 1221 | iff_stack_pop(struct iff_stack *stack) |
| 1222 | { |
| 1223 | stack->index--; |
| 1224 | return stack->stack[stack->index]; |
| 1225 | } |
| 1226 | |
| 1227 | static void |
| 1228 | iff_stack_clean(struct iff_stack *stack) |
| 1229 | { |
| 1230 | stack->size = 0; |
| 1231 | free(stack->stack); |
| 1232 | } |
| 1233 | |
| 1234 | static void |
| 1235 | iff_setop(uint8_t *list, uint8_t op, int pos) |
| 1236 | { |
| 1237 | uint8_t *item; |
| 1238 | uint8_t mask = 3; |
| 1239 | |
| 1240 | assert(pos >= 0); |
| 1241 | assert(op <= 3); /* max 2 bits */ |
| 1242 | |
| 1243 | item = &list[pos / 4]; |
| 1244 | mask = mask << 2 * (pos % 4); |
| 1245 | *item = (*item) & ~mask; |
| 1246 | *item = (*item) | (op << 2 * (pos % 4)); |
| 1247 | } |
| 1248 | |
| 1249 | uint8_t |
| 1250 | iff_getop(uint8_t *list, int pos) |
| 1251 | { |
| 1252 | uint8_t *item; |
| 1253 | uint8_t mask = 3, result; |
| 1254 | |
| 1255 | assert(pos >= 0); |
| 1256 | |
| 1257 | item = &list[pos / 4]; |
| 1258 | result = (*item) & (mask << 2 * (pos % 4)); |
| 1259 | return result >> 2 * (pos % 4); |
| 1260 | } |
| 1261 | |
| 1262 | #define LYS_IFF_LP 0x04 /* ( */ |
| 1263 | #define LYS_IFF_RP 0x08 /* ) */ |
| 1264 | |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1265 | /* internal structure for passing data for UNRES_IFFEAT */ |
| 1266 | struct unres_iffeat_data { |
| 1267 | struct lys_node *node; |
| 1268 | const char *fname; |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 1269 | int infeature; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1270 | }; |
| 1271 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1272 | void |
| 1273 | resolve_iffeature_getsizes(struct lys_iffeature *iffeat, unsigned int *expr_size, unsigned int *feat_size) |
| 1274 | { |
| 1275 | unsigned int e = 0, f = 0, r = 0; |
| 1276 | uint8_t op; |
| 1277 | |
| 1278 | assert(iffeat); |
| 1279 | |
| 1280 | if (!iffeat->expr) { |
| 1281 | goto result; |
| 1282 | } |
| 1283 | |
| 1284 | do { |
| 1285 | op = iff_getop(iffeat->expr, e++); |
| 1286 | switch (op) { |
| 1287 | case LYS_IFF_NOT: |
| 1288 | if (!r) { |
| 1289 | r += 1; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1290 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1291 | break; |
| 1292 | case LYS_IFF_AND: |
| 1293 | case LYS_IFF_OR: |
| 1294 | if (!r) { |
| 1295 | r += 2; |
| 1296 | } else { |
| 1297 | r += 1; |
| 1298 | } |
| 1299 | break; |
| 1300 | case LYS_IFF_F: |
| 1301 | f++; |
| 1302 | if (r) { |
| 1303 | r--; |
| 1304 | } |
| 1305 | break; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1306 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1307 | } while(r); |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1308 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1309 | result: |
| 1310 | if (expr_size) { |
| 1311 | *expr_size = e; |
| 1312 | } |
| 1313 | if (feat_size) { |
| 1314 | *feat_size = f; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1315 | } |
| 1316 | } |
| 1317 | |
| 1318 | int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1319 | 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] | 1320 | int infeature, struct unres_schema *unres) |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1321 | { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1322 | const char *c = value; |
| 1323 | int r, rc = EXIT_FAILURE; |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1324 | int i, j, last_not, checkversion = 0; |
| 1325 | unsigned int f_size = 0, expr_size = 0, f_exp = 1; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1326 | uint8_t op; |
| 1327 | struct iff_stack stack = {0, 0, NULL}; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1328 | struct unres_iffeat_data *iff_data; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1329 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1330 | assert(c); |
| 1331 | |
| 1332 | if (isspace(c[0])) { |
| 1333 | LOGVAL(LYE_INCHAR, LY_VLOG_NONE, NULL, c[0], c); |
| 1334 | return EXIT_FAILURE; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1335 | } |
| 1336 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1337 | /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */ |
| 1338 | for (i = j = last_not = 0; c[i]; i++) { |
| 1339 | if (c[i] == '(') { |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1340 | checkversion = 1; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1341 | j++; |
| 1342 | continue; |
| 1343 | } else if (c[i] == ')') { |
| 1344 | j--; |
| 1345 | continue; |
| 1346 | } else if (isspace(c[i])) { |
| 1347 | continue; |
| 1348 | } |
| 1349 | |
| 1350 | if (!strncmp(&c[i], "not", r = 3) || !strncmp(&c[i], "and", r = 3) || !strncmp(&c[i], "or", r = 2)) { |
| 1351 | if (c[i + r] == '\0') { |
Radek Krejci | a98da3f | 2016-07-27 14:05:22 +0200 | [diff] [blame] | 1352 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1353 | return EXIT_FAILURE; |
| 1354 | } else if (!isspace(c[i + r])) { |
| 1355 | /* feature name starting with the not/and/or */ |
| 1356 | last_not = 0; |
| 1357 | f_size++; |
| 1358 | } else if (c[i] == 'n') { /* not operation */ |
| 1359 | if (last_not) { |
| 1360 | /* double not */ |
| 1361 | expr_size = expr_size - 2; |
| 1362 | last_not = 0; |
| 1363 | } else { |
| 1364 | last_not = 1; |
| 1365 | } |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1366 | } else { /* and, or */ |
| 1367 | f_exp++; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1368 | /* not a not operation */ |
| 1369 | last_not = 0; |
| 1370 | } |
| 1371 | i += r; |
| 1372 | } else { |
| 1373 | f_size++; |
| 1374 | last_not = 0; |
| 1375 | } |
| 1376 | expr_size++; |
| 1377 | |
| 1378 | while (!isspace(c[i])) { |
| 1379 | if (!c[i] || c[i] == ')') { |
| 1380 | i--; |
| 1381 | break; |
| 1382 | } |
| 1383 | i++; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1384 | } |
| 1385 | } |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1386 | if (j || f_exp != f_size) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1387 | /* not matching count of ( and ) */ |
| 1388 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
| 1389 | return EXIT_FAILURE; |
| 1390 | } |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1391 | |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1392 | if (checkversion || expr_size > 1) { |
| 1393 | /* check that we have 1.1 module */ |
| 1394 | if (node->module->version != 2) { |
| 1395 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
| 1396 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "YANG 1.1 if-feature expression found in 1.0 module."); |
| 1397 | return EXIT_FAILURE; |
| 1398 | } |
| 1399 | } |
| 1400 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1401 | /* allocate the memory */ |
| 1402 | 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] | 1403 | iffeat_expr->features = calloc(f_size, sizeof *iffeat_expr->features); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1404 | stack.size = expr_size; |
| 1405 | stack.stack = malloc(expr_size * sizeof *stack.stack); |
| 1406 | if (!stack.stack || !iffeat_expr->expr || !iffeat_expr->features) { |
| 1407 | LOGMEM; |
| 1408 | goto error; |
| 1409 | } |
| 1410 | f_size--; expr_size--; /* used as indexes from now */ |
| 1411 | |
| 1412 | for (i--; i >= 0; i--) { |
| 1413 | if (c[i] == ')') { |
| 1414 | /* push it on stack */ |
| 1415 | iff_stack_push(&stack, LYS_IFF_RP); |
| 1416 | continue; |
| 1417 | } else if (c[i] == '(') { |
| 1418 | /* pop from the stack into result all operators until ) */ |
| 1419 | while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) { |
| 1420 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1421 | } |
| 1422 | continue; |
| 1423 | } else if (isspace(c[i])) { |
| 1424 | continue; |
| 1425 | } |
| 1426 | |
| 1427 | /* end operator or operand -> find beginning and get what is it */ |
| 1428 | j = i + 1; |
| 1429 | while (i >= 0 && !isspace(c[i]) && c[i] != '(') { |
| 1430 | i--; |
| 1431 | } |
| 1432 | i++; /* get back by one step */ |
| 1433 | |
| 1434 | if (!strncmp(&c[i], "not ", 4)) { |
| 1435 | if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) { |
| 1436 | /* double not */ |
| 1437 | iff_stack_pop(&stack); |
| 1438 | } else { |
| 1439 | /* not has the highest priority, so do not pop from the stack |
| 1440 | * as in case of AND and OR */ |
| 1441 | iff_stack_push(&stack, LYS_IFF_NOT); |
| 1442 | } |
| 1443 | } else if (!strncmp(&c[i], "and ", 4)) { |
| 1444 | /* as for OR - pop from the stack all operators with the same or higher |
| 1445 | * priority and store them to the result, then push the AND to the stack */ |
| 1446 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) { |
| 1447 | op = iff_stack_pop(&stack); |
| 1448 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1449 | } |
| 1450 | iff_stack_push(&stack, LYS_IFF_AND); |
| 1451 | } else if (!strncmp(&c[i], "or ", 3)) { |
| 1452 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) { |
| 1453 | op = iff_stack_pop(&stack); |
| 1454 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1455 | } |
| 1456 | iff_stack_push(&stack, LYS_IFF_OR); |
| 1457 | } else { |
| 1458 | /* feature name, length is j - i */ |
| 1459 | |
| 1460 | /* add it to the result */ |
| 1461 | iff_setop(iffeat_expr->expr, LYS_IFF_F, expr_size--); |
| 1462 | |
| 1463 | /* now get the link to the feature definition. Since it can be |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1464 | * forward referenced, we have to keep the feature name in auxiliary |
| 1465 | * structure passed into unres */ |
| 1466 | iff_data = malloc(sizeof *iff_data); |
| 1467 | iff_data->node = node; |
| 1468 | iff_data->fname = lydict_insert(node->module->ctx, &c[i], j - i); |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 1469 | iff_data->infeature = infeature; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1470 | r = unres_schema_add_node(node->module, unres, &iffeat_expr->features[f_size], UNRES_IFFEAT, |
| 1471 | (struct lys_node *)iff_data); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1472 | f_size--; |
| 1473 | |
| 1474 | if (r == -1) { |
Pavol Vican | 4d08451 | 2016-09-29 16:38:12 +0200 | [diff] [blame] | 1475 | free(iff_data); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1476 | goto error; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1477 | } |
| 1478 | } |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1479 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1480 | while (stack.index) { |
| 1481 | op = iff_stack_pop(&stack); |
| 1482 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1483 | } |
| 1484 | |
| 1485 | if (++expr_size || ++f_size) { |
| 1486 | /* not all expected operators and operands found */ |
| 1487 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
| 1488 | rc = EXIT_FAILURE; |
| 1489 | } else { |
| 1490 | rc = EXIT_SUCCESS; |
| 1491 | } |
| 1492 | |
| 1493 | error: |
| 1494 | /* cleanup */ |
| 1495 | iff_stack_clean(&stack); |
| 1496 | |
| 1497 | return rc; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1498 | } |
| 1499 | |
| 1500 | /** |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1501 | * @brief Resolve (find) a data node based on a schema-nodeid. |
| 1502 | * |
| 1503 | * Used for resolving unique statements - so id is expected to be relative and local (without reference to a different |
| 1504 | * module). |
| 1505 | * |
| 1506 | */ |
| 1507 | struct lyd_node * |
| 1508 | resolve_data_descendant_schema_nodeid(const char *nodeid, struct lyd_node *start) |
| 1509 | { |
| 1510 | char *str, *token, *p; |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1511 | struct lyd_node *result = NULL, *iter; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1512 | const struct lys_node *schema = NULL; |
| 1513 | |
| 1514 | assert(nodeid && start); |
| 1515 | |
| 1516 | if (nodeid[0] == '/') { |
| 1517 | return NULL; |
| 1518 | } |
| 1519 | |
| 1520 | str = p = strdup(nodeid); |
| 1521 | if (!str) { |
| 1522 | LOGMEM; |
| 1523 | return NULL; |
| 1524 | } |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1525 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1526 | while (p) { |
| 1527 | token = p; |
| 1528 | p = strchr(p, '/'); |
| 1529 | if (p) { |
| 1530 | *p = '\0'; |
| 1531 | p++; |
| 1532 | } |
| 1533 | |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1534 | if (p) { |
| 1535 | /* inner node */ |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1536 | if (resolve_descendant_schema_nodeid(token, schema ? schema->child : start->schema, |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 1537 | LYS_CONTAINER | LYS_CHOICE | LYS_CASE | LYS_LEAF, 0, &schema) |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1538 | || !schema) { |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1539 | result = NULL; |
| 1540 | break; |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1541 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1542 | |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1543 | if (schema->nodetype & (LYS_CHOICE | LYS_CASE)) { |
| 1544 | continue; |
| 1545 | } |
| 1546 | } else { |
| 1547 | /* final node */ |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 1548 | 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] | 1549 | || !schema) { |
| 1550 | result = NULL; |
| 1551 | break; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1552 | } |
| 1553 | } |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1554 | LY_TREE_FOR(result ? result->child : start, iter) { |
| 1555 | if (iter->schema == schema) { |
| 1556 | /* move in data tree according to returned schema */ |
| 1557 | result = iter; |
| 1558 | break; |
| 1559 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1560 | } |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1561 | if (!iter) { |
| 1562 | /* instance not found */ |
| 1563 | result = NULL; |
| 1564 | break; |
| 1565 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1566 | } |
| 1567 | free(str); |
| 1568 | |
| 1569 | return result; |
| 1570 | } |
| 1571 | |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1572 | /* |
| 1573 | * 0 - ok (done) |
| 1574 | * 1 - continue |
| 1575 | * 2 - break |
| 1576 | * -1 - error |
| 1577 | */ |
Radek Krejci | 1a9c361 | 2017-04-24 14:49:43 +0200 | [diff] [blame] | 1578 | int |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 1579 | schema_nodeid_siblingcheck(const struct lys_node *sibling, const char *id, const struct lys_module *module, |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1580 | const char *mod_name, int mod_name_len, const struct lys_node **start_parent) |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1581 | { |
| 1582 | const struct lys_module *prefix_mod; |
| 1583 | |
| 1584 | /* module check */ |
| 1585 | prefix_mod = lys_get_import_module(module, NULL, 0, mod_name, mod_name_len); |
| 1586 | if (!prefix_mod) { |
| 1587 | return -1; |
| 1588 | } |
| 1589 | if (prefix_mod != lys_node_module(sibling)) { |
| 1590 | return 1; |
| 1591 | } |
| 1592 | |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1593 | /* the result node? */ |
| 1594 | if (!id[0]) { |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1595 | return 0; |
| 1596 | } |
| 1597 | |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 1598 | /* move down the tree, if possible */ |
| 1599 | if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
| 1600 | return -1; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1601 | } |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 1602 | *start_parent = sibling; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1603 | |
| 1604 | return 2; |
| 1605 | } |
| 1606 | |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 1607 | /* start - relative, module - absolute, -1 error, EXIT_SUCCESS ok (but ret can still be NULL), >0 unexpected char on ret - 1 |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 1608 | */ |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1609 | int |
| 1610 | resolve_augment_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_module *module, |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1611 | const struct lys_node **ret) |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1612 | { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1613 | const char *name, *mod_name, *id; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1614 | const struct lys_node *sibling, *start_parent; |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1615 | struct lys_node_augment *last_aug; |
| 1616 | int r, nam_len, mod_name_len = 0, is_relative = -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1617 | /* 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] | 1618 | const struct lys_module *start_mod, *aux_mod; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1619 | |
| 1620 | assert(nodeid && (start || module) && !(start && module) && ret); |
| 1621 | |
| 1622 | id = nodeid; |
| 1623 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1624 | if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, NULL)) < 1) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1625 | return ((id - nodeid) - r) + 1; |
| 1626 | } |
| 1627 | id += r; |
| 1628 | |
| 1629 | if ((is_relative && !start) || (!is_relative && !module)) { |
| 1630 | return -1; |
| 1631 | } |
| 1632 | |
| 1633 | /* descendant-schema-nodeid */ |
| 1634 | if (is_relative) { |
Michal Vasko | 4c06fd8 | 2016-02-17 13:43:38 +0100 | [diff] [blame] | 1635 | module = start_mod = start->module; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1636 | start_parent = lys_parent(start); |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1637 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1638 | /* absolute-schema-nodeid */ |
| 1639 | } else { |
| 1640 | 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] | 1641 | if (!start_mod) { |
| 1642 | return -1; |
| 1643 | } |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1644 | start_parent = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1645 | } |
| 1646 | |
| 1647 | while (1) { |
| 1648 | sibling = NULL; |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1649 | last_aug = NULL; |
| 1650 | |
| 1651 | if (start_parent) { |
| 1652 | if (mod_name && (strncmp(mod_name, lys_node_module(start_parent)->name, mod_name_len) |
| 1653 | || (mod_name_len != (signed) strlen(lys_node_module(start_parent)->name)))) { |
| 1654 | /* we are getting into another module (augment) */ |
| 1655 | aux_mod = lys_get_import_module(module, NULL, 0, mod_name, mod_name_len); |
| 1656 | if (!aux_mod) { |
| 1657 | return -1; |
| 1658 | } |
| 1659 | } else { |
| 1660 | /* there is no mod_name or it matches the previous one, so why are we checking augments again? |
| 1661 | * because this module may be not implemented and it augments something in another module and |
| 1662 | * there is another augment augmenting that previous one */ |
| 1663 | aux_mod = lys_node_module(start_parent); |
| 1664 | } |
| 1665 | |
| 1666 | /* if the module is implemented, all the augments will be connected */ |
| 1667 | if (!aux_mod->implemented) { |
| 1668 | get_next_augment: |
| 1669 | last_aug = lys_getnext_target_aug(last_aug, aux_mod, start_parent); |
| 1670 | } |
| 1671 | } |
| 1672 | |
| 1673 | 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] | 1674 | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT | LYS_GETNEXT_PARENTUSES))) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1675 | /* name match */ |
Michal Vasko | 0a1aaa4 | 2016-04-19 09:48:25 +0200 | [diff] [blame] | 1676 | if (sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len]) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1677 | r = schema_nodeid_siblingcheck(sibling, id, module, mod_name, mod_name_len, &start_parent); |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1678 | if (r == 0) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1679 | *ret = sibling; |
| 1680 | return EXIT_SUCCESS; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1681 | } else if (r == 1) { |
| 1682 | continue; |
| 1683 | } else if (r == 2) { |
| 1684 | break; |
| 1685 | } else { |
| 1686 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1687 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1688 | } |
| 1689 | } |
| 1690 | |
| 1691 | /* no match */ |
| 1692 | if (!sibling) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1693 | if (last_aug) { |
| 1694 | /* it still could be in another augment */ |
| 1695 | goto get_next_augment; |
| 1696 | } |
Michal Vasko | a426fef | 2016-03-07 10:47:31 +0100 | [diff] [blame] | 1697 | *ret = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1698 | return EXIT_SUCCESS; |
| 1699 | } |
| 1700 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1701 | if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, NULL)) < 1) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1702 | return ((id - nodeid) - r) + 1; |
| 1703 | } |
| 1704 | id += r; |
| 1705 | } |
| 1706 | |
| 1707 | /* cannot get here */ |
| 1708 | LOGINT; |
| 1709 | return -1; |
| 1710 | } |
| 1711 | |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 1712 | /* unique, refine, |
| 1713 | * >0 - unexpected char on position (ret - 1), |
| 1714 | * 0 - ok (but ret can still be NULL), |
| 1715 | * -1 - error, |
| 1716 | * -2 - violated no_innerlist */ |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1717 | int |
| 1718 | 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] | 1719 | int no_innerlist, const struct lys_node **ret) |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1720 | { |
| 1721 | const char *name, *mod_name, *id; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1722 | const struct lys_node *sibling, *start_parent; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1723 | int r, nam_len, mod_name_len, is_relative = -1; |
| 1724 | /* 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] | 1725 | const struct lys_module *module; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1726 | |
Radek Krejci | c3f1b6f | 2017-02-15 10:51:10 +0100 | [diff] [blame] | 1727 | assert(nodeid && ret); |
Radek Krejci | e207741 | 2017-01-26 16:03:39 +0100 | [diff] [blame] | 1728 | assert(!(ret_nodetype & (LYS_USES | LYS_AUGMENT | LYS_GROUPING))); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1729 | |
Radek Krejci | c3f1b6f | 2017-02-15 10:51:10 +0100 | [diff] [blame] | 1730 | if (!start) { |
| 1731 | /* leaf not found */ |
| 1732 | return 0; |
| 1733 | } |
| 1734 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1735 | id = nodeid; |
| 1736 | module = start->module; |
| 1737 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1738 | if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, NULL)) < 1) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1739 | return ((id - nodeid) - r) + 1; |
| 1740 | } |
| 1741 | id += r; |
| 1742 | |
| 1743 | if (!is_relative) { |
| 1744 | return -1; |
| 1745 | } |
| 1746 | |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1747 | start_parent = lys_parent(start); |
Michal Vasko | 74a991b | 2017-03-31 09:17:22 +0200 | [diff] [blame] | 1748 | while ((start_parent->nodetype == LYS_USES) && lys_parent(start_parent)) { |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1749 | start_parent = lys_parent(start_parent); |
| 1750 | } |
| 1751 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1752 | while (1) { |
| 1753 | sibling = NULL; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1754 | while ((sibling = lys_getnext(sibling, start_parent, module, |
Michal Vasko | 74a991b | 2017-03-31 09:17:22 +0200 | [diff] [blame] | 1755 | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_PARENTUSES))) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1756 | /* name match */ |
| 1757 | if (sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len]) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1758 | r = schema_nodeid_siblingcheck(sibling, id, module, mod_name, mod_name_len, &start_parent); |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1759 | if (r == 0) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1760 | if (!(sibling->nodetype & ret_nodetype)) { |
| 1761 | /* wrong node type, too bad */ |
| 1762 | continue; |
| 1763 | } |
| 1764 | *ret = sibling; |
| 1765 | return EXIT_SUCCESS; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1766 | } else if (r == 1) { |
| 1767 | continue; |
| 1768 | } else if (r == 2) { |
| 1769 | break; |
| 1770 | } else { |
| 1771 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1772 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1773 | } |
| 1774 | } |
| 1775 | |
| 1776 | /* no match */ |
| 1777 | if (!sibling) { |
Michal Vasko | a426fef | 2016-03-07 10:47:31 +0100 | [diff] [blame] | 1778 | *ret = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1779 | return EXIT_SUCCESS; |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 1780 | } else if (no_innerlist && sibling->nodetype == LYS_LIST) { |
| 1781 | *ret = NULL; |
| 1782 | return -2; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1783 | } |
| 1784 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1785 | if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, NULL)) < 1) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1786 | return ((id - nodeid) - r) + 1; |
| 1787 | } |
| 1788 | id += r; |
| 1789 | } |
| 1790 | |
| 1791 | /* cannot get here */ |
| 1792 | LOGINT; |
| 1793 | return -1; |
| 1794 | } |
| 1795 | |
| 1796 | /* choice default */ |
| 1797 | int |
| 1798 | resolve_choice_default_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_node **ret) |
| 1799 | { |
| 1800 | /* cannot actually be a path */ |
| 1801 | if (strchr(nodeid, '/')) { |
| 1802 | return -1; |
| 1803 | } |
| 1804 | |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 1805 | 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] | 1806 | } |
| 1807 | |
| 1808 | /* uses, -1 error, EXIT_SUCCESS ok (but ret can still be NULL), >0 unexpected char on ret - 1 */ |
| 1809 | static int |
| 1810 | resolve_uses_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_node_grp **ret) |
| 1811 | { |
| 1812 | const struct lys_module *module; |
| 1813 | const char *mod_prefix, *name; |
| 1814 | int i, mod_prefix_len, nam_len; |
| 1815 | |
| 1816 | /* parse the identifier, it must be parsed on one call */ |
| 1817 | if (((i = parse_node_identifier(nodeid, &mod_prefix, &mod_prefix_len, &name, &nam_len)) < 1) || nodeid[i]) { |
| 1818 | return -i + 1; |
| 1819 | } |
| 1820 | |
| 1821 | module = lys_get_import_module(start->module, mod_prefix, mod_prefix_len, NULL, 0); |
| 1822 | if (!module) { |
| 1823 | return -1; |
| 1824 | } |
Radek Krejci | 0a8205d | 2017-03-01 16:25:29 +0100 | [diff] [blame] | 1825 | if (module != lys_main_module(start->module)) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1826 | start = module->data; |
| 1827 | } |
| 1828 | |
| 1829 | *ret = lys_find_grouping_up(name, (struct lys_node *)start); |
| 1830 | |
| 1831 | return EXIT_SUCCESS; |
| 1832 | } |
| 1833 | |
| 1834 | int |
| 1835 | resolve_absolute_schema_nodeid(const char *nodeid, const struct lys_module *module, int ret_nodetype, |
| 1836 | const struct lys_node **ret) |
| 1837 | { |
| 1838 | const char *name, *mod_name, *id; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1839 | const struct lys_node *sibling, *start_parent; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1840 | int r, nam_len, mod_name_len, is_relative = -1; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1841 | const struct lys_module *abs_start_mod; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1842 | |
| 1843 | assert(nodeid && module && ret); |
| 1844 | assert(!(ret_nodetype & (LYS_USES | LYS_AUGMENT)) && ((ret_nodetype == LYS_GROUPING) || !(ret_nodetype & LYS_GROUPING))); |
| 1845 | |
| 1846 | id = nodeid; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1847 | start_parent = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1848 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1849 | if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, NULL)) < 1) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1850 | return ((id - nodeid) - r) + 1; |
| 1851 | } |
| 1852 | id += r; |
| 1853 | |
| 1854 | if (is_relative) { |
| 1855 | return -1; |
| 1856 | } |
| 1857 | |
| 1858 | 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] | 1859 | if (!abs_start_mod) { |
| 1860 | return -1; |
| 1861 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1862 | |
| 1863 | while (1) { |
| 1864 | sibling = NULL; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1865 | 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] | 1866 | | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT | LYS_GETNEXT_WITHGROUPING))) { |
| 1867 | /* name match */ |
Michal Vasko | 0a1aaa4 | 2016-04-19 09:48:25 +0200 | [diff] [blame] | 1868 | if (sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len]) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1869 | r = schema_nodeid_siblingcheck(sibling, id, module, mod_name, mod_name_len, &start_parent); |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1870 | if (r == 0) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1871 | if (!(sibling->nodetype & ret_nodetype)) { |
| 1872 | /* wrong node type, too bad */ |
| 1873 | continue; |
| 1874 | } |
| 1875 | *ret = sibling; |
| 1876 | return EXIT_SUCCESS; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1877 | } else if (r == 1) { |
| 1878 | continue; |
| 1879 | } else if (r == 2) { |
| 1880 | break; |
| 1881 | } else { |
| 1882 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1883 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1884 | } |
| 1885 | } |
| 1886 | |
| 1887 | /* no match */ |
| 1888 | if (!sibling) { |
Michal Vasko | a426fef | 2016-03-07 10:47:31 +0100 | [diff] [blame] | 1889 | *ret = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1890 | return EXIT_SUCCESS; |
| 1891 | } |
| 1892 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1893 | if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, NULL)) < 1) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1894 | return ((id - nodeid) - r) + 1; |
| 1895 | } |
| 1896 | id += r; |
| 1897 | } |
| 1898 | |
| 1899 | /* cannot get here */ |
| 1900 | LOGINT; |
| 1901 | return -1; |
| 1902 | } |
| 1903 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1904 | static int |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1905 | 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] | 1906 | { |
| 1907 | const char *name; |
| 1908 | int nam_len, has_predicate, i; |
| 1909 | |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 1910 | if (((i = parse_schema_json_predicate(predicate, &name, &nam_len, NULL, NULL, &has_predicate)) < 1) |
| 1911 | || !strncmp(name, ".", nam_len)) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 1912 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-i], &predicate[-i]); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1913 | return -1; |
| 1914 | } |
| 1915 | |
| 1916 | predicate += i; |
| 1917 | *parsed += i; |
| 1918 | |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 1919 | if (!isdigit(name[0])) { |
| 1920 | for (i = 0; i < list->keys_size; ++i) { |
| 1921 | if (!strncmp(list->keys[i]->name, name, nam_len) && !list->keys[i]->name[nam_len]) { |
| 1922 | break; |
| 1923 | } |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1924 | } |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1925 | |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 1926 | if (i == list->keys_size) { |
| 1927 | LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name); |
| 1928 | return -1; |
| 1929 | } |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1930 | } |
| 1931 | |
| 1932 | /* more predicates? */ |
| 1933 | if (has_predicate) { |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1934 | return resolve_json_schema_list_predicate(predicate, list, parsed); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1935 | } |
| 1936 | |
| 1937 | return 0; |
| 1938 | } |
| 1939 | |
Michal Vasko | 8d26e5c | 2016-09-08 10:03:49 +0200 | [diff] [blame] | 1940 | /* cannot return LYS_GROUPING, LYS_AUGMENT, LYS_USES, logs directly */ |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1941 | const struct lys_node * |
Michal Vasko | 8d26e5c | 2016-09-08 10:03:49 +0200 | [diff] [blame] | 1942 | resolve_json_nodeid(const char *nodeid, struct ly_ctx *ctx, const struct lys_node *start) |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1943 | { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 1944 | char *module_name = ly_buf(), *buf_backup = NULL, *str; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1945 | const char *name, *mod_name, *id; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1946 | const struct lys_node *sibling, *start_parent; |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 1947 | int r, nam_len, mod_name_len, is_relative = -1, has_predicate; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1948 | /* resolved import module from the start module, it must match the next node-name-match sibling */ |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1949 | const struct lys_module *prefix_mod, *module, *prev_mod; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1950 | |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1951 | assert(nodeid && (ctx || start)); |
| 1952 | if (!ctx) { |
| 1953 | ctx = start->module->ctx; |
| 1954 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1955 | |
| 1956 | id = nodeid; |
| 1957 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1958 | if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate)) < 1) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 1959 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1960 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1961 | } |
| 1962 | id += r; |
| 1963 | |
| 1964 | if (is_relative) { |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1965 | assert(start); |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1966 | start_parent = start; |
| 1967 | while (start_parent && (start_parent->nodetype == LYS_USES)) { |
| 1968 | start_parent = lys_parent(start_parent); |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1969 | } |
| 1970 | module = start->module; |
| 1971 | } else { |
| 1972 | if (!mod_name) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 1973 | str = strndup(nodeid, (name + nam_len) - nodeid); |
| 1974 | LOGVAL(LYE_PATH_MISSMOD, LY_VLOG_STR, nodeid); |
| 1975 | free(str); |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1976 | return NULL; |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 1977 | } else if (mod_name_len > LY_BUF_SIZE - 1) { |
| 1978 | LOGINT; |
| 1979 | return NULL; |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1980 | } |
| 1981 | |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 1982 | if (ly_buf_used && module_name[0]) { |
| 1983 | buf_backup = strndup(module_name, LY_BUF_SIZE - 1); |
| 1984 | } |
| 1985 | ly_buf_used++; |
| 1986 | |
Michal Vasko | df3f1ab | 2016-04-01 15:07:22 +0200 | [diff] [blame] | 1987 | memmove(module_name, mod_name, mod_name_len); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 1988 | module_name[mod_name_len] = '\0'; |
| 1989 | module = ly_ctx_get_module(ctx, module_name, NULL); |
| 1990 | |
| 1991 | if (buf_backup) { |
| 1992 | /* return previous internal buffer content */ |
| 1993 | strcpy(module_name, buf_backup); |
| 1994 | free(buf_backup); |
| 1995 | buf_backup = NULL; |
| 1996 | } |
| 1997 | ly_buf_used--; |
| 1998 | |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1999 | if (!module) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2000 | str = strndup(nodeid, (mod_name + mod_name_len) - nodeid); |
| 2001 | LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str); |
| 2002 | free(str); |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2003 | return NULL; |
| 2004 | } |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 2005 | start_parent = NULL; |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2006 | |
| 2007 | /* now it's as if there was no module name */ |
| 2008 | mod_name = NULL; |
| 2009 | mod_name_len = 0; |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2010 | } |
| 2011 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2012 | prev_mod = module; |
| 2013 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2014 | while (1) { |
| 2015 | sibling = NULL; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 2016 | while ((sibling = lys_getnext(sibling, start_parent, module, |
Michal Vasko | 8d26e5c | 2016-09-08 10:03:49 +0200 | [diff] [blame] | 2017 | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT))) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2018 | /* name match */ |
Michal Vasko | 0a1aaa4 | 2016-04-19 09:48:25 +0200 | [diff] [blame] | 2019 | if (sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len]) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2020 | /* module check */ |
| 2021 | if (mod_name) { |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2022 | if (mod_name_len > LY_BUF_SIZE - 1) { |
Michal Vasko | 8757e7c | 2016-03-15 10:41:30 +0100 | [diff] [blame] | 2023 | LOGINT; |
| 2024 | return NULL; |
| 2025 | } |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2026 | |
| 2027 | if (ly_buf_used && module_name[0]) { |
| 2028 | buf_backup = strndup(module_name, LY_BUF_SIZE - 1); |
| 2029 | } |
| 2030 | ly_buf_used++; |
| 2031 | |
Michal Vasko | df3f1ab | 2016-04-01 15:07:22 +0200 | [diff] [blame] | 2032 | memmove(module_name, mod_name, mod_name_len); |
Michal Vasko | 8757e7c | 2016-03-15 10:41:30 +0100 | [diff] [blame] | 2033 | module_name[mod_name_len] = '\0'; |
| 2034 | /* will also find an augment module */ |
| 2035 | prefix_mod = ly_ctx_get_module(ctx, module_name, NULL); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2036 | |
| 2037 | if (buf_backup) { |
| 2038 | /* return previous internal buffer content */ |
Michal Vasko | 888a129 | 2016-04-05 12:08:54 +0200 | [diff] [blame] | 2039 | strncpy(module_name, buf_backup, LY_BUF_SIZE - 1); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2040 | free(buf_backup); |
| 2041 | buf_backup = NULL; |
| 2042 | } |
| 2043 | ly_buf_used--; |
| 2044 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2045 | if (!prefix_mod) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2046 | str = strndup(nodeid, (mod_name + mod_name_len) - nodeid); |
| 2047 | LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str); |
| 2048 | free(str); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2049 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2050 | } |
| 2051 | } else { |
| 2052 | prefix_mod = prev_mod; |
| 2053 | } |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 2054 | if (prefix_mod != lys_node_module(sibling)) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2055 | continue; |
| 2056 | } |
| 2057 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2058 | /* do we have some predicates on it? */ |
| 2059 | if (has_predicate) { |
| 2060 | r = 0; |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 2061 | if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 2062 | if ((r = parse_schema_json_predicate(id, NULL, NULL, NULL, NULL, &has_predicate)) < 1) { |
| 2063 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
| 2064 | return NULL; |
| 2065 | } |
| 2066 | } else if (sibling->nodetype == LYS_LIST) { |
| 2067 | if (resolve_json_schema_list_predicate(id, (const struct lys_node_list *)sibling, &r)) { |
| 2068 | return NULL; |
| 2069 | } |
| 2070 | } else { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2071 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2072 | return NULL; |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2073 | } |
| 2074 | id += r; |
| 2075 | } |
| 2076 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2077 | /* the result node? */ |
| 2078 | if (!id[0]) { |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2079 | return sibling; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2080 | } |
| 2081 | |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 2082 | /* move down the tree, if possible */ |
| 2083 | if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
| 2084 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
| 2085 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2086 | } |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 2087 | start_parent = sibling; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2088 | |
| 2089 | /* update prev mod */ |
Michal Vasko | 3960835 | 2017-05-11 10:37:10 +0200 | [diff] [blame] | 2090 | prev_mod = (start_parent->child ? lys_node_module(start_parent->child) : module); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2091 | break; |
| 2092 | } |
| 2093 | } |
| 2094 | |
| 2095 | /* no match */ |
| 2096 | if (!sibling) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2097 | str = strndup(nodeid, (name + nam_len) - nodeid); |
| 2098 | LOGVAL(LYE_PATH_INNODE, LY_VLOG_STR, str); |
| 2099 | free(str); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2100 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2101 | } |
| 2102 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2103 | if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate)) < 1) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2104 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2105 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2106 | } |
| 2107 | id += r; |
| 2108 | } |
| 2109 | |
| 2110 | /* cannot get here */ |
| 2111 | LOGINT; |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2112 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2113 | } |
| 2114 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2115 | static int |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2116 | resolve_partial_json_data_list_predicate(const char *predicate, const char *node_name, struct lyd_node *node, |
| 2117 | int position, int *parsed) |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2118 | { |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2119 | const char *name, *value, *key_val; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2120 | int nam_len, val_len, has_predicate = 1, r; |
| 2121 | uint16_t i; |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2122 | struct lyd_node_leaf_list *key; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2123 | |
Radek Krejci | 61a86c6 | 2016-03-24 11:06:44 +0100 | [diff] [blame] | 2124 | assert(node); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2125 | assert(node->schema->nodetype == LYS_LIST); |
| 2126 | |
Michal Vasko | 53adfc7 | 2017-01-06 10:39:10 +0100 | [diff] [blame] | 2127 | /* is the predicate a number? */ |
| 2128 | if (((r = parse_schema_json_predicate(predicate, &name, &nam_len, &value, &val_len, &has_predicate)) < 1) |
| 2129 | || !strncmp(name, ".", nam_len)) { |
| 2130 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-r], &predicate[-r]); |
| 2131 | return -1; |
| 2132 | } |
| 2133 | |
| 2134 | if (isdigit(name[0])) { |
| 2135 | if (position == atoi(name)) { |
| 2136 | /* match */ |
| 2137 | *parsed += r; |
| 2138 | return 0; |
| 2139 | } else { |
| 2140 | /* not a match */ |
| 2141 | return 1; |
| 2142 | } |
| 2143 | } |
| 2144 | |
| 2145 | if (!((struct lys_node_list *)node->schema)->keys_size) { |
| 2146 | /* no keys in schema - causes an error later */ |
| 2147 | return 0; |
| 2148 | } |
| 2149 | |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2150 | key = (struct lyd_node_leaf_list *)node->child; |
Michal Vasko | 53adfc7 | 2017-01-06 10:39:10 +0100 | [diff] [blame] | 2151 | if (!key) { |
| 2152 | /* it is not a position, so we need a key for it to be a match */ |
| 2153 | return 1; |
| 2154 | } |
| 2155 | |
| 2156 | /* go through all the keys */ |
| 2157 | i = 0; |
| 2158 | goto check_parsed_values; |
| 2159 | |
| 2160 | for (; i < ((struct lys_node_list *)node->schema)->keys_size; ++i) { |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2161 | if (!has_predicate) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2162 | LOGVAL(LYE_PATH_MISSKEY, LY_VLOG_NONE, NULL, node_name); |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2163 | return -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2164 | } |
| 2165 | |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 2166 | if (((r = parse_schema_json_predicate(predicate, &name, &nam_len, &value, &val_len, &has_predicate)) < 1) |
| 2167 | || !strncmp(name, ".", nam_len)) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2168 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-r], &predicate[-r]); |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2169 | return -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2170 | } |
| 2171 | |
Michal Vasko | 53adfc7 | 2017-01-06 10:39:10 +0100 | [diff] [blame] | 2172 | check_parsed_values: |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2173 | predicate += r; |
| 2174 | *parsed += r; |
| 2175 | |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2176 | 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] | 2177 | LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name); |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2178 | return -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2179 | } |
| 2180 | |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2181 | /* make value canonical */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 2182 | if ((key->value_type & LY_TYPE_IDENT) |
Michal Vasko | 6a938d6 | 2016-12-21 09:21:30 +0100 | [diff] [blame] | 2183 | && !strncmp(key->value_str, lyd_node_module(node)->name, strlen(lyd_node_module(node)->name)) |
| 2184 | && (key->value_str[strlen(lyd_node_module(node)->name)] == ':')) { |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2185 | key_val = key->value_str + strlen(lyd_node_module(node)->name) + 1; |
| 2186 | } else { |
| 2187 | key_val = key->value_str; |
| 2188 | } |
| 2189 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2190 | /* value does not match */ |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2191 | if (strncmp(key_val, value, val_len) || key_val[val_len]) { |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2192 | return 1; |
| 2193 | } |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2194 | |
| 2195 | key = (struct lyd_node_leaf_list *)key->next; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2196 | } |
| 2197 | |
| 2198 | if (has_predicate) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2199 | LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2200 | return -1; |
| 2201 | } |
| 2202 | |
| 2203 | return 0; |
| 2204 | } |
| 2205 | |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 2206 | /** |
| 2207 | * @brief get the closest parent of the node (or the node itself) identified by the nodeid (path) |
| 2208 | * |
| 2209 | * @param[in] nodeid Node data path to find |
| 2210 | * @param[in] llist_value If the \p nodeid identifies leaf-list, this is expected value of the leaf-list instance. |
| 2211 | * @param[in] options Bitmask of options flags, see @ref pathoptions. |
| 2212 | * @param[out] parsed Number of characters processed in \p id |
| 2213 | * @return The closes parent (or the node itself) from the path |
| 2214 | */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2215 | struct lyd_node * |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2216 | resolve_partial_json_data_nodeid(const char *nodeid, const char *llist_value, struct lyd_node *start, int options, |
| 2217 | int *parsed) |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2218 | { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2219 | char *module_name = ly_buf(), *buf_backup = NULL, *str; |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2220 | const char *id, *mod_name, *name, *pred_name, *data_val; |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2221 | 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] | 2222 | 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] | 2223 | struct lyd_node *sibling, *last_match = NULL; |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2224 | struct lyd_node_leaf_list *llist; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2225 | const struct lys_module *prefix_mod, *prev_mod; |
| 2226 | struct ly_ctx *ctx; |
| 2227 | |
| 2228 | assert(nodeid && start && parsed); |
| 2229 | |
| 2230 | ctx = start->schema->module->ctx; |
| 2231 | id = nodeid; |
| 2232 | |
| 2233 | if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate)) < 1) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2234 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2235 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2236 | return NULL; |
| 2237 | } |
| 2238 | id += r; |
| 2239 | /* add it to parsed only after the data node was actually found */ |
| 2240 | last_parsed = r; |
| 2241 | |
| 2242 | if (is_relative) { |
Michal Vasko | 1adc724 | 2016-11-16 11:05:28 +0100 | [diff] [blame] | 2243 | prev_mod = lyd_node_module(start); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2244 | start = start->child; |
| 2245 | } else { |
| 2246 | for (; start->parent; start = start->parent); |
Michal Vasko | 1adc724 | 2016-11-16 11:05:28 +0100 | [diff] [blame] | 2247 | prev_mod = lyd_node_module(start); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2248 | } |
| 2249 | |
| 2250 | while (1) { |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2251 | list_instance_position = 0; |
| 2252 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2253 | LY_TREE_FOR(start, sibling) { |
Michal Vasko | 945b96b | 2016-10-18 11:49:12 +0200 | [diff] [blame] | 2254 | /* 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] | 2255 | if (lys_parent(sibling->schema)) { |
| 2256 | if (options & LYD_PATH_OPT_OUTPUT) { |
| 2257 | if (lys_parent(sibling->schema)->nodetype == LYS_INPUT) { |
Michal Vasko | b724689 | 2016-04-19 10:37:35 +0200 | [diff] [blame] | 2258 | 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] | 2259 | *parsed = -1; |
| 2260 | return NULL; |
| 2261 | } |
| 2262 | } else { |
| 2263 | if (lys_parent(sibling->schema)->nodetype == LYS_OUTPUT) { |
Michal Vasko | b724689 | 2016-04-19 10:37:35 +0200 | [diff] [blame] | 2264 | 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] | 2265 | *parsed = -1; |
| 2266 | return NULL; |
| 2267 | } |
| 2268 | } |
| 2269 | } |
| 2270 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2271 | /* name match */ |
| 2272 | if (!strncmp(name, sibling->schema->name, nam_len) && !sibling->schema->name[nam_len]) { |
| 2273 | |
| 2274 | /* module check */ |
| 2275 | if (mod_name) { |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2276 | if (mod_name_len > LY_BUF_SIZE - 1) { |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2277 | LOGINT; |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2278 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2279 | return NULL; |
| 2280 | } |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2281 | |
| 2282 | if (ly_buf_used && module_name[0]) { |
| 2283 | buf_backup = strndup(module_name, LY_BUF_SIZE - 1); |
| 2284 | } |
| 2285 | ly_buf_used++; |
| 2286 | |
Michal Vasko | df3f1ab | 2016-04-01 15:07:22 +0200 | [diff] [blame] | 2287 | memmove(module_name, mod_name, mod_name_len); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2288 | module_name[mod_name_len] = '\0'; |
| 2289 | /* will also find an augment module */ |
| 2290 | prefix_mod = ly_ctx_get_module(ctx, module_name, NULL); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2291 | |
| 2292 | if (buf_backup) { |
| 2293 | /* return previous internal buffer content */ |
Michal Vasko | 888a129 | 2016-04-05 12:08:54 +0200 | [diff] [blame] | 2294 | strncpy(module_name, buf_backup, LY_BUF_SIZE - 1); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2295 | free(buf_backup); |
| 2296 | buf_backup = NULL; |
| 2297 | } |
| 2298 | ly_buf_used--; |
| 2299 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2300 | if (!prefix_mod) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2301 | str = strndup(nodeid, (mod_name + mod_name_len) - nodeid); |
| 2302 | LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str); |
| 2303 | free(str); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2304 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2305 | return NULL; |
| 2306 | } |
| 2307 | } else { |
| 2308 | prefix_mod = prev_mod; |
| 2309 | } |
Michal Vasko | 1adc724 | 2016-11-16 11:05:28 +0100 | [diff] [blame] | 2310 | if (prefix_mod != lyd_node_module(sibling)) { |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2311 | continue; |
| 2312 | } |
| 2313 | |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2314 | /* leaf-list, did we find it with the correct value or not? */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2315 | if (sibling->schema->nodetype == LYS_LEAFLIST) { |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2316 | llist = (struct lyd_node_leaf_list *)sibling; |
| 2317 | |
Michal Vasko | 0a8d17f | 2016-10-19 15:51:36 +0200 | [diff] [blame] | 2318 | last_has_pred = 0; |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2319 | if (has_predicate) { |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2320 | if ((r = parse_schema_json_predicate(id, &pred_name, &pred_name_len, &llist_value, &llval_len, &last_has_pred)) < 1) { |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2321 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
| 2322 | *parsed = -1; |
| 2323 | return NULL; |
| 2324 | } |
Michal Vasko | 0a8d17f | 2016-10-19 15:51:36 +0200 | [diff] [blame] | 2325 | if ((pred_name[0] != '.') || (pred_name_len != 1)) { |
| 2326 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[1], id + 1); |
| 2327 | *parsed = -1; |
| 2328 | return NULL; |
| 2329 | } |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2330 | } else { |
| 2331 | r = 0; |
| 2332 | if (llist_value) { |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2333 | llval_len = strlen(llist_value); |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2334 | } |
| 2335 | } |
| 2336 | |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2337 | /* make value canonical */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 2338 | if ((llist->value_type & LY_TYPE_IDENT) |
Michal Vasko | 6a938d6 | 2016-12-21 09:21:30 +0100 | [diff] [blame] | 2339 | && !strncmp(llist->value_str, lyd_node_module(sibling)->name, strlen(lyd_node_module(sibling)->name)) |
| 2340 | && (llist->value_str[strlen(lyd_node_module(sibling)->name)] == ':')) { |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2341 | data_val = llist->value_str + strlen(lyd_node_module(sibling)->name) + 1; |
| 2342 | } else { |
| 2343 | data_val = llist->value_str; |
| 2344 | } |
| 2345 | |
| 2346 | if ((!llist_value && data_val && data_val[0]) |
| 2347 | || (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] | 2348 | continue; |
| 2349 | } |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2350 | |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2351 | id += r; |
| 2352 | last_parsed += r; |
Michal Vasko | 0a8d17f | 2016-10-19 15:51:36 +0200 | [diff] [blame] | 2353 | has_predicate = last_has_pred; |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2354 | |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 2355 | } else if (sibling->schema->nodetype == LYS_LIST) { |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2356 | /* 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] | 2357 | if (!has_predicate) { |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2358 | /* none match */ |
| 2359 | return last_match; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2360 | } |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2361 | |
| 2362 | ++list_instance_position; |
| 2363 | r = 0; |
| 2364 | 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] | 2365 | if (ret == -1) { |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2366 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2367 | return NULL; |
| 2368 | } else if (ret == 1) { |
| 2369 | /* this list instance does not match */ |
| 2370 | continue; |
| 2371 | } |
| 2372 | id += r; |
| 2373 | last_parsed += r; |
| 2374 | } |
| 2375 | |
| 2376 | *parsed += last_parsed; |
| 2377 | |
| 2378 | /* the result node? */ |
| 2379 | if (!id[0]) { |
| 2380 | return sibling; |
| 2381 | } |
| 2382 | |
| 2383 | /* move down the tree, if possible */ |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 2384 | if (sibling->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2385 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2386 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2387 | return NULL; |
| 2388 | } |
Michal Vasko | c6c52cf | 2016-03-29 11:53:04 +0200 | [diff] [blame] | 2389 | last_match = sibling; |
Michal Vasko | fc11b68 | 2016-11-18 09:52:41 +0100 | [diff] [blame] | 2390 | prev_mod = lyd_node_module(sibling); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2391 | start = sibling->child; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2392 | break; |
| 2393 | } |
| 2394 | } |
| 2395 | |
| 2396 | /* no match, return last match */ |
| 2397 | if (!sibling) { |
| 2398 | return last_match; |
| 2399 | } |
| 2400 | |
| 2401 | if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate)) < 1) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2402 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2403 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2404 | return NULL; |
| 2405 | } |
| 2406 | id += r; |
| 2407 | last_parsed = r; |
| 2408 | } |
| 2409 | |
| 2410 | /* cannot get here */ |
| 2411 | LOGINT; |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2412 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2413 | return NULL; |
| 2414 | } |
| 2415 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2416 | /** |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2417 | * @brief Resolves length or range intervals. Does not log. |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2418 | * Syntax is assumed to be correct, *ret MUST be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2419 | * |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2420 | * @param[in] str_restr Restriction as a string. |
| 2421 | * @param[in] type Type of the restriction. |
| 2422 | * @param[out] ret Final interval structure that starts with |
| 2423 | * the interval of the initial type, continues with intervals |
| 2424 | * of any superior types derived from the initial one, and |
| 2425 | * finishes with intervals from our \p type. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2426 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2427 | * @return EXIT_SUCCESS on succes, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2428 | */ |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2429 | int |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2430 | 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] | 2431 | { |
| 2432 | /* 0 - unsigned, 1 - signed, 2 - floating point */ |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2433 | int kind; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2434 | int64_t local_smin, local_smax, local_fmin, local_fmax; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2435 | uint64_t local_umin, local_umax; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2436 | uint8_t local_fdig; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2437 | const char *seg_ptr, *ptr; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2438 | 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] | 2439 | |
| 2440 | switch (type->base) { |
| 2441 | case LY_TYPE_BINARY: |
| 2442 | kind = 0; |
| 2443 | local_umin = 0; |
| 2444 | local_umax = 18446744073709551615UL; |
| 2445 | |
| 2446 | if (!str_restr && type->info.binary.length) { |
| 2447 | str_restr = type->info.binary.length->expr; |
| 2448 | } |
| 2449 | break; |
| 2450 | case LY_TYPE_DEC64: |
| 2451 | kind = 2; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2452 | local_fmin = __INT64_C(-9223372036854775807) - __INT64_C(1); |
| 2453 | local_fmax = __INT64_C(9223372036854775807); |
| 2454 | local_fdig = type->info.dec64.dig; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2455 | |
| 2456 | if (!str_restr && type->info.dec64.range) { |
| 2457 | str_restr = type->info.dec64.range->expr; |
| 2458 | } |
| 2459 | break; |
| 2460 | case LY_TYPE_INT8: |
| 2461 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2462 | local_smin = __INT64_C(-128); |
| 2463 | local_smax = __INT64_C(127); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2464 | |
| 2465 | if (!str_restr && type->info.num.range) { |
| 2466 | str_restr = type->info.num.range->expr; |
| 2467 | } |
| 2468 | break; |
| 2469 | case LY_TYPE_INT16: |
| 2470 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2471 | local_smin = __INT64_C(-32768); |
| 2472 | local_smax = __INT64_C(32767); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2473 | |
| 2474 | if (!str_restr && type->info.num.range) { |
| 2475 | str_restr = type->info.num.range->expr; |
| 2476 | } |
| 2477 | break; |
| 2478 | case LY_TYPE_INT32: |
| 2479 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2480 | local_smin = __INT64_C(-2147483648); |
| 2481 | local_smax = __INT64_C(2147483647); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2482 | |
| 2483 | if (!str_restr && type->info.num.range) { |
| 2484 | str_restr = type->info.num.range->expr; |
| 2485 | } |
| 2486 | break; |
| 2487 | case LY_TYPE_INT64: |
| 2488 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2489 | local_smin = __INT64_C(-9223372036854775807) - __INT64_C(1); |
| 2490 | local_smax = __INT64_C(9223372036854775807); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2491 | |
| 2492 | if (!str_restr && type->info.num.range) { |
| 2493 | str_restr = type->info.num.range->expr; |
| 2494 | } |
| 2495 | break; |
| 2496 | case LY_TYPE_UINT8: |
| 2497 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2498 | local_umin = __UINT64_C(0); |
| 2499 | local_umax = __UINT64_C(255); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2500 | |
| 2501 | if (!str_restr && type->info.num.range) { |
| 2502 | str_restr = type->info.num.range->expr; |
| 2503 | } |
| 2504 | break; |
| 2505 | case LY_TYPE_UINT16: |
| 2506 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2507 | local_umin = __UINT64_C(0); |
| 2508 | local_umax = __UINT64_C(65535); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2509 | |
| 2510 | if (!str_restr && type->info.num.range) { |
| 2511 | str_restr = type->info.num.range->expr; |
| 2512 | } |
| 2513 | break; |
| 2514 | case LY_TYPE_UINT32: |
| 2515 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2516 | local_umin = __UINT64_C(0); |
| 2517 | local_umax = __UINT64_C(4294967295); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2518 | |
| 2519 | if (!str_restr && type->info.num.range) { |
| 2520 | str_restr = type->info.num.range->expr; |
| 2521 | } |
| 2522 | break; |
| 2523 | case LY_TYPE_UINT64: |
| 2524 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2525 | local_umin = __UINT64_C(0); |
| 2526 | local_umax = __UINT64_C(18446744073709551615); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2527 | |
| 2528 | if (!str_restr && type->info.num.range) { |
| 2529 | str_restr = type->info.num.range->expr; |
| 2530 | } |
| 2531 | break; |
| 2532 | case LY_TYPE_STRING: |
| 2533 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2534 | local_umin = __UINT64_C(0); |
| 2535 | local_umax = __UINT64_C(18446744073709551615); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2536 | |
| 2537 | if (!str_restr && type->info.str.length) { |
| 2538 | str_restr = type->info.str.length->expr; |
| 2539 | } |
| 2540 | break; |
| 2541 | default: |
| 2542 | LOGINT; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2543 | return -1; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2544 | } |
| 2545 | |
| 2546 | /* process superior types */ |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2547 | if (type->der) { |
| 2548 | if (resolve_len_ran_interval(NULL, &type->der->type, &intv)) { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2549 | LOGINT; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2550 | return -1; |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2551 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2552 | assert(!intv || (intv->kind == kind)); |
| 2553 | } |
| 2554 | |
| 2555 | if (!str_restr) { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2556 | /* we do not have any restriction, return superior ones */ |
| 2557 | *ret = intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2558 | return EXIT_SUCCESS; |
| 2559 | } |
| 2560 | |
| 2561 | /* adjust local min and max */ |
| 2562 | if (intv) { |
| 2563 | tmp_intv = intv; |
| 2564 | |
| 2565 | if (kind == 0) { |
| 2566 | local_umin = tmp_intv->value.uval.min; |
| 2567 | } else if (kind == 1) { |
| 2568 | local_smin = tmp_intv->value.sval.min; |
| 2569 | } else if (kind == 2) { |
| 2570 | local_fmin = tmp_intv->value.fval.min; |
| 2571 | } |
| 2572 | |
| 2573 | while (tmp_intv->next) { |
| 2574 | tmp_intv = tmp_intv->next; |
| 2575 | } |
| 2576 | |
| 2577 | if (kind == 0) { |
| 2578 | local_umax = tmp_intv->value.uval.max; |
| 2579 | } else if (kind == 1) { |
| 2580 | local_smax = tmp_intv->value.sval.max; |
| 2581 | } else if (kind == 2) { |
| 2582 | local_fmax = tmp_intv->value.fval.max; |
| 2583 | } |
| 2584 | } |
| 2585 | |
| 2586 | /* finally parse our restriction */ |
| 2587 | seg_ptr = str_restr; |
Pavol Vican | 9e7c01d | 2016-08-29 09:36:17 +0200 | [diff] [blame] | 2588 | tmp_intv = NULL; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2589 | while (1) { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2590 | if (!tmp_local_intv) { |
| 2591 | assert(!local_intv); |
| 2592 | local_intv = malloc(sizeof *local_intv); |
| 2593 | tmp_local_intv = local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2594 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2595 | tmp_local_intv->next = malloc(sizeof *tmp_local_intv); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2596 | tmp_local_intv = tmp_local_intv->next; |
| 2597 | } |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2598 | if (!tmp_local_intv) { |
| 2599 | LOGMEM; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2600 | goto error; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2601 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2602 | |
| 2603 | tmp_local_intv->kind = kind; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2604 | tmp_local_intv->type = type; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2605 | tmp_local_intv->next = NULL; |
| 2606 | |
| 2607 | /* min */ |
| 2608 | ptr = seg_ptr; |
| 2609 | while (isspace(ptr[0])) { |
| 2610 | ++ptr; |
| 2611 | } |
| 2612 | if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) { |
| 2613 | if (kind == 0) { |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2614 | tmp_local_intv->value.uval.min = strtol(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2615 | } else if (kind == 1) { |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2616 | tmp_local_intv->value.sval.min = strtol(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2617 | } else if (kind == 2) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 2618 | if (parse_range_dec64(&ptr, local_fdig, &tmp_local_intv->value.fval.min)) { |
| 2619 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, ptr, "range"); |
| 2620 | goto error; |
| 2621 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2622 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2623 | } else if (!strncmp(ptr, "min", 3)) { |
| 2624 | if (kind == 0) { |
| 2625 | tmp_local_intv->value.uval.min = local_umin; |
| 2626 | } else if (kind == 1) { |
| 2627 | tmp_local_intv->value.sval.min = local_smin; |
| 2628 | } else if (kind == 2) { |
| 2629 | tmp_local_intv->value.fval.min = local_fmin; |
| 2630 | } |
| 2631 | |
| 2632 | ptr += 3; |
| 2633 | } else if (!strncmp(ptr, "max", 3)) { |
| 2634 | if (kind == 0) { |
| 2635 | tmp_local_intv->value.uval.min = local_umax; |
| 2636 | } else if (kind == 1) { |
| 2637 | tmp_local_intv->value.sval.min = local_smax; |
| 2638 | } else if (kind == 2) { |
| 2639 | tmp_local_intv->value.fval.min = local_fmax; |
| 2640 | } |
| 2641 | |
| 2642 | ptr += 3; |
| 2643 | } else { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2644 | LOGINT; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2645 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2646 | } |
| 2647 | |
| 2648 | while (isspace(ptr[0])) { |
| 2649 | ptr++; |
| 2650 | } |
| 2651 | |
| 2652 | /* no interval or interval */ |
| 2653 | if ((ptr[0] == '|') || !ptr[0]) { |
| 2654 | if (kind == 0) { |
| 2655 | tmp_local_intv->value.uval.max = tmp_local_intv->value.uval.min; |
| 2656 | } else if (kind == 1) { |
| 2657 | tmp_local_intv->value.sval.max = tmp_local_intv->value.sval.min; |
| 2658 | } else if (kind == 2) { |
| 2659 | tmp_local_intv->value.fval.max = tmp_local_intv->value.fval.min; |
| 2660 | } |
| 2661 | } else if (!strncmp(ptr, "..", 2)) { |
| 2662 | /* skip ".." */ |
| 2663 | ptr += 2; |
| 2664 | while (isspace(ptr[0])) { |
| 2665 | ++ptr; |
| 2666 | } |
| 2667 | |
| 2668 | /* max */ |
| 2669 | if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) { |
| 2670 | if (kind == 0) { |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2671 | tmp_local_intv->value.uval.max = strtol(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2672 | } else if (kind == 1) { |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2673 | tmp_local_intv->value.sval.max = strtol(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2674 | } else if (kind == 2) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 2675 | if (parse_range_dec64(&ptr, local_fdig, &tmp_local_intv->value.fval.max)) { |
| 2676 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, ptr, "range"); |
| 2677 | goto error; |
| 2678 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2679 | } |
| 2680 | } else if (!strncmp(ptr, "max", 3)) { |
| 2681 | if (kind == 0) { |
| 2682 | tmp_local_intv->value.uval.max = local_umax; |
| 2683 | } else if (kind == 1) { |
| 2684 | tmp_local_intv->value.sval.max = local_smax; |
| 2685 | } else if (kind == 2) { |
| 2686 | tmp_local_intv->value.fval.max = local_fmax; |
| 2687 | } |
| 2688 | } else { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2689 | LOGINT; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2690 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2691 | } |
| 2692 | } else { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2693 | LOGINT; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2694 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2695 | } |
| 2696 | |
Pavol Vican | 9e7c01d | 2016-08-29 09:36:17 +0200 | [diff] [blame] | 2697 | /* check min and max in correct order*/ |
| 2698 | if (kind == 0) { |
| 2699 | /* current segment */ |
| 2700 | if (tmp_local_intv->value.uval.min > tmp_local_intv->value.uval.max) { |
| 2701 | goto error; |
| 2702 | } |
| 2703 | if (tmp_local_intv->value.uval.min < local_umin || tmp_local_intv->value.uval.max > local_umax) { |
| 2704 | goto error; |
| 2705 | } |
| 2706 | /* segments sholud be ascending order */ |
Pavol Vican | 69f62c9 | 2016-08-30 09:06:25 +0200 | [diff] [blame] | 2707 | 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] | 2708 | goto error; |
| 2709 | } |
| 2710 | } else if (kind == 1) { |
| 2711 | if (tmp_local_intv->value.sval.min > tmp_local_intv->value.sval.max) { |
| 2712 | goto error; |
| 2713 | } |
| 2714 | if (tmp_local_intv->value.sval.min < local_smin || tmp_local_intv->value.sval.max > local_smax) { |
| 2715 | goto error; |
| 2716 | } |
Pavol Vican | 69f62c9 | 2016-08-30 09:06:25 +0200 | [diff] [blame] | 2717 | 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] | 2718 | goto error; |
| 2719 | } |
| 2720 | } else if (kind == 2) { |
| 2721 | if (tmp_local_intv->value.fval.min > tmp_local_intv->value.fval.max) { |
| 2722 | goto error; |
| 2723 | } |
| 2724 | if (tmp_local_intv->value.fval.min < local_fmin || tmp_local_intv->value.fval.max > local_fmax) { |
| 2725 | goto error; |
| 2726 | } |
Pavol Vican | 69f62c9 | 2016-08-30 09:06:25 +0200 | [diff] [blame] | 2727 | 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] | 2728 | /* 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] | 2729 | goto error; |
| 2730 | } |
| 2731 | } |
| 2732 | |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2733 | /* next segment (next OR) */ |
| 2734 | seg_ptr = strchr(seg_ptr, '|'); |
| 2735 | if (!seg_ptr) { |
| 2736 | break; |
| 2737 | } |
| 2738 | seg_ptr++; |
Pavol Vican | 9e7c01d | 2016-08-29 09:36:17 +0200 | [diff] [blame] | 2739 | tmp_intv = tmp_local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2740 | } |
| 2741 | |
| 2742 | /* check local restrictions against superior ones */ |
| 2743 | if (intv) { |
| 2744 | tmp_intv = intv; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2745 | tmp_local_intv = local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2746 | |
| 2747 | while (tmp_local_intv && tmp_intv) { |
| 2748 | /* reuse local variables */ |
| 2749 | if (kind == 0) { |
| 2750 | local_umin = tmp_local_intv->value.uval.min; |
| 2751 | local_umax = tmp_local_intv->value.uval.max; |
| 2752 | |
| 2753 | /* it must be in this interval */ |
| 2754 | if ((local_umin >= tmp_intv->value.uval.min) && (local_umin <= tmp_intv->value.uval.max)) { |
| 2755 | /* this interval is covered, next one */ |
| 2756 | if (local_umax <= tmp_intv->value.uval.max) { |
| 2757 | tmp_local_intv = tmp_local_intv->next; |
| 2758 | continue; |
| 2759 | /* ascending order of restrictions -> fail */ |
| 2760 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2761 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2762 | } |
| 2763 | } |
| 2764 | } else if (kind == 1) { |
| 2765 | local_smin = tmp_local_intv->value.sval.min; |
| 2766 | local_smax = tmp_local_intv->value.sval.max; |
| 2767 | |
| 2768 | if ((local_smin >= tmp_intv->value.sval.min) && (local_smin <= tmp_intv->value.sval.max)) { |
| 2769 | if (local_smax <= tmp_intv->value.sval.max) { |
| 2770 | tmp_local_intv = tmp_local_intv->next; |
| 2771 | continue; |
| 2772 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2773 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2774 | } |
| 2775 | } |
| 2776 | } else if (kind == 2) { |
| 2777 | local_fmin = tmp_local_intv->value.fval.min; |
| 2778 | local_fmax = tmp_local_intv->value.fval.max; |
| 2779 | |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2780 | 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] | 2781 | && (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] | 2782 | 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] | 2783 | tmp_local_intv = tmp_local_intv->next; |
| 2784 | continue; |
| 2785 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2786 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2787 | } |
| 2788 | } |
| 2789 | } |
| 2790 | |
| 2791 | tmp_intv = tmp_intv->next; |
| 2792 | } |
| 2793 | |
| 2794 | /* some interval left uncovered -> fail */ |
| 2795 | if (tmp_local_intv) { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2796 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2797 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2798 | } |
| 2799 | |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2800 | /* append the local intervals to all the intervals of the superior types, return it all */ |
| 2801 | if (intv) { |
| 2802 | for (tmp_intv = intv; tmp_intv->next; tmp_intv = tmp_intv->next); |
| 2803 | tmp_intv->next = local_intv; |
| 2804 | } else { |
| 2805 | intv = local_intv; |
| 2806 | } |
| 2807 | *ret = intv; |
| 2808 | |
| 2809 | return EXIT_SUCCESS; |
| 2810 | |
| 2811 | error: |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2812 | while (intv) { |
| 2813 | tmp_intv = intv->next; |
| 2814 | free(intv); |
| 2815 | intv = tmp_intv; |
| 2816 | } |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2817 | while (local_intv) { |
| 2818 | tmp_local_intv = local_intv->next; |
| 2819 | free(local_intv); |
| 2820 | local_intv = tmp_local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2821 | } |
| 2822 | |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2823 | return -1; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2824 | } |
| 2825 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2826 | /** |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2827 | * @brief Resolve a typedef, return only resolved typedefs if derived. If leafref, it must be |
| 2828 | * resolved for this function to return it. Does not log. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2829 | * |
| 2830 | * @param[in] name Typedef name. |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2831 | * @param[in] mod_name Typedef name module name. |
| 2832 | * @param[in] module Main module. |
| 2833 | * @param[in] parent Parent of the resolved type definition. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2834 | * @param[out] ret Pointer to the resolved typedef. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2835 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2836 | * @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] | 2837 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2838 | int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 2839 | resolve_superior_type(const char *name, const char *mod_name, const struct lys_module *module, |
| 2840 | const struct lys_node *parent, struct lys_tpdf **ret) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2841 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2842 | int i, j; |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2843 | struct lys_tpdf *tpdf, *match; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2844 | int tpdf_size; |
| 2845 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2846 | if (!mod_name) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2847 | /* no prefix, try built-in types */ |
| 2848 | for (i = 1; i < LY_DATA_TYPE_COUNT; i++) { |
Radek Krejci | a68ddeb | 2017-02-24 12:49:44 +0100 | [diff] [blame] | 2849 | if (!strcmp(ly_types[i]->name, name)) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2850 | if (ret) { |
Radek Krejci | a68ddeb | 2017-02-24 12:49:44 +0100 | [diff] [blame] | 2851 | *ret = ly_types[i]; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2852 | } |
| 2853 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2854 | } |
| 2855 | } |
| 2856 | } else { |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2857 | if (!strcmp(mod_name, module->name)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2858 | /* prefix refers to the current module, ignore it */ |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2859 | mod_name = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2860 | } |
| 2861 | } |
| 2862 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2863 | if (!mod_name && parent) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2864 | /* search in local typedefs */ |
| 2865 | while (parent) { |
| 2866 | switch (parent->nodetype) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2867 | case LYS_CONTAINER: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 2868 | tpdf_size = ((struct lys_node_container *)parent)->tpdf_size; |
| 2869 | tpdf = ((struct lys_node_container *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2870 | break; |
| 2871 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2872 | case LYS_LIST: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 2873 | tpdf_size = ((struct lys_node_list *)parent)->tpdf_size; |
| 2874 | tpdf = ((struct lys_node_list *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2875 | break; |
| 2876 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2877 | case LYS_GROUPING: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 2878 | tpdf_size = ((struct lys_node_grp *)parent)->tpdf_size; |
| 2879 | tpdf = ((struct lys_node_grp *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2880 | break; |
| 2881 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2882 | case LYS_RPC: |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 2883 | case LYS_ACTION: |
| 2884 | tpdf_size = ((struct lys_node_rpc_action *)parent)->tpdf_size; |
| 2885 | tpdf = ((struct lys_node_rpc_action *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2886 | break; |
| 2887 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2888 | case LYS_NOTIF: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 2889 | tpdf_size = ((struct lys_node_notif *)parent)->tpdf_size; |
| 2890 | tpdf = ((struct lys_node_notif *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2891 | break; |
| 2892 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2893 | case LYS_INPUT: |
| 2894 | case LYS_OUTPUT: |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 2895 | tpdf_size = ((struct lys_node_inout *)parent)->tpdf_size; |
| 2896 | tpdf = ((struct lys_node_inout *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2897 | break; |
| 2898 | |
| 2899 | default: |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 2900 | parent = lys_parent(parent); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2901 | continue; |
| 2902 | } |
| 2903 | |
| 2904 | for (i = 0; i < tpdf_size; i++) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 2905 | if (!strcmp(tpdf[i].name, name) && tpdf[i].type.base > 0) { |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2906 | match = &tpdf[i]; |
| 2907 | goto check_leafref; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2908 | } |
| 2909 | } |
| 2910 | |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 2911 | parent = lys_parent(parent); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2912 | } |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2913 | } else { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2914 | /* get module where to search */ |
Michal Vasko | b6729c6 | 2015-10-21 12:09:47 +0200 | [diff] [blame] | 2915 | module = lys_get_import_module(module, NULL, 0, mod_name, 0); |
Michal Vasko | c935fff | 2015-08-17 14:02:06 +0200 | [diff] [blame] | 2916 | if (!module) { |
| 2917 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2918 | } |
| 2919 | } |
| 2920 | |
| 2921 | /* search in top level typedefs */ |
| 2922 | for (i = 0; i < module->tpdf_size; i++) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 2923 | 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] | 2924 | match = &module->tpdf[i]; |
| 2925 | goto check_leafref; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2926 | } |
| 2927 | } |
| 2928 | |
| 2929 | /* search in submodules */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2930 | for (i = 0; i < module->inc_size && module->inc[i].submodule; i++) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2931 | for (j = 0; j < module->inc[i].submodule->tpdf_size; j++) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 2932 | 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] | 2933 | match = &module->inc[i].submodule->tpdf[j]; |
| 2934 | goto check_leafref; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2935 | } |
| 2936 | } |
| 2937 | } |
| 2938 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2939 | return EXIT_FAILURE; |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2940 | |
| 2941 | check_leafref: |
| 2942 | if (ret) { |
| 2943 | *ret = match; |
| 2944 | } |
| 2945 | if (match->type.base == LY_TYPE_LEAFREF) { |
| 2946 | while (!match->type.info.lref.path) { |
| 2947 | match = match->type.der; |
| 2948 | assert(match); |
| 2949 | } |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2950 | } |
| 2951 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2952 | } |
| 2953 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2954 | /** |
| 2955 | * @brief Check the default \p value of the \p type. Logs directly. |
| 2956 | * |
| 2957 | * @param[in] type Type definition to use. |
| 2958 | * @param[in] value Default value to check. |
Michal Vasko | 5682640 | 2016-03-02 11:11:37 +0100 | [diff] [blame] | 2959 | * @param[in] module Type module. |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2960 | * |
| 2961 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 2962 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2963 | static int |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 2964 | 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] | 2965 | { |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 2966 | struct lys_tpdf *base_tpdf = NULL; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2967 | struct lyd_node_leaf_list node; |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 2968 | const char *dflt = NULL; |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 2969 | char *s; |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 2970 | int ret = EXIT_SUCCESS; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2971 | |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 2972 | assert(value); |
| 2973 | |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 2974 | if (type->base <= LY_TYPE_DER) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 2975 | /* the type was not resolved yet, nothing to do for now */ |
| 2976 | return EXIT_FAILURE; |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 2977 | } else if (!tpdf && !lys_main_module(module)->implemented) { |
| 2978 | /* do not check defaults in not implemented module's data */ |
| 2979 | return EXIT_SUCCESS; |
| 2980 | } else if (tpdf && !lys_main_module(module)->implemented && type->base == LY_TYPE_IDENT) { |
| 2981 | /* identityrefs are checked when instantiated in data instead of typedef, |
| 2982 | * but in typedef the value has to be modified to include the prefix */ |
| 2983 | if (*value) { |
| 2984 | if (strchr(*value, ':')) { |
| 2985 | dflt = transform_schema2json(module, *value); |
| 2986 | } else { |
| 2987 | /* default prefix of the module where the typedef is defined */ |
| 2988 | asprintf(&s, "%s:%s", lys_main_module(module)->name, *value); |
| 2989 | dflt = lydict_insert_zc(module->ctx, s); |
| 2990 | } |
| 2991 | lydict_remove(module->ctx, *value); |
| 2992 | *value = dflt; |
| 2993 | } |
| 2994 | return EXIT_SUCCESS; |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 2995 | } else if (type->base == LY_TYPE_LEAFREF && tpdf) { |
| 2996 | /* leafref in typedef cannot be checked */ |
| 2997 | return EXIT_SUCCESS; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 2998 | } |
| 2999 | |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3000 | dflt = *value; |
| 3001 | if (!dflt) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3002 | /* we do not have a new default value, so is there any to check even, in some base type? */ |
| 3003 | for (base_tpdf = type->der; base_tpdf->type.der; base_tpdf = base_tpdf->type.der) { |
| 3004 | if (base_tpdf->dflt) { |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3005 | dflt = base_tpdf->dflt; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3006 | break; |
| 3007 | } |
| 3008 | } |
| 3009 | |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3010 | if (!dflt) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3011 | /* no default value, nothing to check, all is well */ |
| 3012 | return EXIT_SUCCESS; |
| 3013 | } |
| 3014 | |
| 3015 | /* 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)? */ |
| 3016 | switch (type->base) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3017 | case LY_TYPE_IDENT: |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 3018 | if (lys_main_module(base_tpdf->type.parent->module)->implemented) { |
| 3019 | return EXIT_SUCCESS; |
| 3020 | } else { |
| 3021 | /* check the default value from typedef, but use also the typedef's module |
| 3022 | * due to possible searching in imported modules which is expected in |
| 3023 | * typedef's module instead of module where the typedef is used */ |
| 3024 | module = base_tpdf->module; |
| 3025 | } |
| 3026 | break; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3027 | case LY_TYPE_INST: |
| 3028 | case LY_TYPE_LEAFREF: |
| 3029 | case LY_TYPE_BOOL: |
| 3030 | case LY_TYPE_EMPTY: |
| 3031 | /* these have no restrictions, so we would do the exact same work as the unres in the base typedef */ |
| 3032 | return EXIT_SUCCESS; |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3033 | case LY_TYPE_BITS: |
| 3034 | /* the default value must match the restricted list of values, if the type was restricted */ |
| 3035 | if (type->info.bits.count) { |
| 3036 | break; |
| 3037 | } |
| 3038 | return EXIT_SUCCESS; |
| 3039 | case LY_TYPE_ENUM: |
| 3040 | /* the default value must match the restricted list of values, if the type was restricted */ |
| 3041 | if (type->info.enums.count) { |
| 3042 | break; |
| 3043 | } |
| 3044 | return EXIT_SUCCESS; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3045 | case LY_TYPE_DEC64: |
| 3046 | if (type->info.dec64.range) { |
| 3047 | break; |
| 3048 | } |
| 3049 | return EXIT_SUCCESS; |
| 3050 | case LY_TYPE_BINARY: |
| 3051 | if (type->info.binary.length) { |
| 3052 | break; |
| 3053 | } |
| 3054 | return EXIT_SUCCESS; |
| 3055 | case LY_TYPE_INT8: |
| 3056 | case LY_TYPE_INT16: |
| 3057 | case LY_TYPE_INT32: |
| 3058 | case LY_TYPE_INT64: |
| 3059 | case LY_TYPE_UINT8: |
| 3060 | case LY_TYPE_UINT16: |
| 3061 | case LY_TYPE_UINT32: |
| 3062 | case LY_TYPE_UINT64: |
| 3063 | if (type->info.num.range) { |
| 3064 | break; |
| 3065 | } |
| 3066 | return EXIT_SUCCESS; |
| 3067 | case LY_TYPE_STRING: |
| 3068 | if (type->info.str.length || type->info.str.patterns) { |
| 3069 | break; |
| 3070 | } |
| 3071 | return EXIT_SUCCESS; |
| 3072 | case LY_TYPE_UNION: |
| 3073 | /* way too much trouble learning whether we need to check the default again, so just do it */ |
| 3074 | break; |
| 3075 | default: |
| 3076 | LOGINT; |
| 3077 | return -1; |
| 3078 | } |
Radek Krejci | 55a161c | 2016-09-05 17:13:25 +0200 | [diff] [blame] | 3079 | } else if (type->base == LY_TYPE_EMPTY) { |
| 3080 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "default", type->parent->name); |
| 3081 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The \"empty\" data type cannot have a default value."); |
| 3082 | return -1; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3083 | } |
| 3084 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3085 | /* dummy leaf */ |
Radek Krejci | 4fe36bd | 2016-03-17 16:47:16 +0100 | [diff] [blame] | 3086 | memset(&node, 0, sizeof node); |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3087 | node.value_str = dflt; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3088 | node.value_type = type->base; |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 3089 | node.schema = calloc(1, sizeof (struct lys_node_leaf)); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3090 | if (!node.schema) { |
| 3091 | LOGMEM; |
| 3092 | return -1; |
| 3093 | } |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3094 | node.schema->name = strdup("fake-default"); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3095 | if (!node.schema->name) { |
| 3096 | LOGMEM; |
Michal Vasko | f49eda0 | 2016-07-21 12:17:01 +0200 | [diff] [blame] | 3097 | free(node.schema); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3098 | return -1; |
| 3099 | } |
Michal Vasko | 5682640 | 2016-03-02 11:11:37 +0100 | [diff] [blame] | 3100 | node.schema->module = module; |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 3101 | memcpy(&((struct lys_node_leaf *)node.schema)->type, type, sizeof *type); |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3102 | |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 3103 | if (type->base == LY_TYPE_LEAFREF) { |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3104 | if (!type->info.lref.target) { |
| 3105 | ret = EXIT_FAILURE; |
| 3106 | goto finish; |
| 3107 | } |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 3108 | ret = check_default(&type->info.lref.target->type, &dflt, module, 0); |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3109 | if (!ret) { |
| 3110 | /* adopt possibly changed default value to its canonical form */ |
| 3111 | if (*value) { |
| 3112 | *value = dflt; |
| 3113 | } |
| 3114 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3115 | } else { |
Radek Krejci | a571d94 | 2017-02-24 09:26:49 +0100 | [diff] [blame] | 3116 | 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] | 3117 | /* possible forward reference */ |
| 3118 | ret = 1; |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3119 | if (base_tpdf) { |
Radek Krejci | 9ad23f4 | 2016-10-31 15:46:52 +0100 | [diff] [blame] | 3120 | /* default value is defined in some base typedef */ |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3121 | if ((type->base == LY_TYPE_BITS && type->der->type.der) || |
| 3122 | (type->base == LY_TYPE_ENUM && type->der->type.der)) { |
| 3123 | /* we have refined bits/enums */ |
| 3124 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, |
| 3125 | "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] | 3126 | dflt, type->parent->name, base_tpdf->name); |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3127 | } |
| 3128 | } |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3129 | } else { |
| 3130 | /* success - adopt canonical form from the node into the default value */ |
| 3131 | if (dflt != node.value_str) { |
| 3132 | /* this can happen only if we have non-inherited default value, |
| 3133 | * inherited default values are already in canonical form */ |
| 3134 | assert(dflt == *value); |
| 3135 | *value = node.value_str; |
| 3136 | } |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 3137 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3138 | } |
| 3139 | |
| 3140 | finish: |
| 3141 | if (node.value_type == LY_TYPE_BITS) { |
| 3142 | free(node.value.bit); |
| 3143 | } |
| 3144 | free((char *)node.schema->name); |
| 3145 | free(node.schema); |
| 3146 | |
| 3147 | return ret; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3148 | } |
| 3149 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3150 | /** |
| 3151 | * @brief Check a key for mandatory attributes. Logs directly. |
| 3152 | * |
| 3153 | * @param[in] key The key to check. |
| 3154 | * @param[in] flags What flags to check. |
| 3155 | * @param[in] list The list of all the keys. |
| 3156 | * @param[in] index Index of the key in the key list. |
| 3157 | * @param[in] name The name of the keys. |
| 3158 | * @param[in] len The name length. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3159 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3160 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3161 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3162 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3163 | 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] | 3164 | { |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3165 | struct lys_node_leaf *key = list->keys[index]; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3166 | char *dup = NULL; |
| 3167 | int j; |
| 3168 | |
| 3169 | /* existence */ |
| 3170 | if (!key) { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3171 | if (name[len] != '\0') { |
| 3172 | dup = strdup(name); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3173 | if (!dup) { |
| 3174 | LOGMEM; |
| 3175 | return -1; |
| 3176 | } |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3177 | dup[len] = '\0'; |
| 3178 | name = dup; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3179 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3180 | LOGVAL(LYE_KEY_MISS, LY_VLOG_LYS, list, name); |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 3181 | free(dup); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3182 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3183 | } |
| 3184 | |
| 3185 | /* uniqueness */ |
| 3186 | for (j = index - 1; j >= 0; j--) { |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3187 | if (key == list->keys[j]) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3188 | LOGVAL(LYE_KEY_DUP, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3189 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3190 | } |
| 3191 | } |
| 3192 | |
| 3193 | /* key is a leaf */ |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3194 | if (key->nodetype != LYS_LEAF) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3195 | LOGVAL(LYE_KEY_NLEAF, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3196 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3197 | } |
| 3198 | |
| 3199 | /* type of the leaf is not built-in empty */ |
Radek Krejci | c3738db | 2016-09-15 15:51:21 +0200 | [diff] [blame] | 3200 | if (key->type.base == LY_TYPE_EMPTY && key->module->version < 2) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3201 | LOGVAL(LYE_KEY_TYPE, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3202 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3203 | } |
| 3204 | |
| 3205 | /* config attribute is the same as of the list */ |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 3206 | 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] | 3207 | LOGVAL(LYE_KEY_CONFIG, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3208 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3209 | } |
| 3210 | |
Radek Krejci | 55e2cdc | 2016-03-11 13:51:09 +0100 | [diff] [blame] | 3211 | /* key is not placed from augment */ |
| 3212 | if (key->parent->nodetype == LYS_AUGMENT) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3213 | LOGVAL(LYE_KEY_MISS, LY_VLOG_LYS, key, key->name); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 3214 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Key inserted from augment."); |
Radek Krejci | 55e2cdc | 2016-03-11 13:51:09 +0100 | [diff] [blame] | 3215 | return -1; |
| 3216 | } |
| 3217 | |
Radek Krejci | 3f21ada | 2016-08-01 13:34:31 +0200 | [diff] [blame] | 3218 | /* key is not when/if-feature -conditional */ |
| 3219 | j = 0; |
| 3220 | if (key->when || (key->iffeature_size && (j = 1))) { |
| 3221 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, key, j ? "if-feature" : "when", "leaf"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 3222 | 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] | 3223 | j ? "if-feature" : "when"); |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3224 | return -1; |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3225 | } |
| 3226 | |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3227 | return EXIT_SUCCESS; |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3228 | } |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3229 | |
| 3230 | /** |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3231 | * @brief Resolve (test the target exists) unique. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3232 | * |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3233 | * @param[in] parent The parent node of the unique structure. |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3234 | * @param[in] uniq_str_path One path from the unique string. |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3235 | * |
| 3236 | * @return EXIT_SUCCESS on succes, EXIT_FAILURE on forward reference, -1 on error. |
| 3237 | */ |
| 3238 | int |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3239 | 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] | 3240 | { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3241 | int rc; |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3242 | const struct lys_node *leaf = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3243 | |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 3244 | 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] | 3245 | if (rc || !leaf) { |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3246 | if (rc) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3247 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3248 | if (rc > 0) { |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 3249 | 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] | 3250 | } else if (rc == -2) { |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 3251 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Unique argument references list."); |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3252 | } |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3253 | rc = -1; |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3254 | } else { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3255 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 3256 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Target leaf not found."); |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3257 | rc = EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3258 | } |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3259 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3260 | } |
Michal Vasko | 9bb061b | 2016-02-12 11:00:19 +0100 | [diff] [blame] | 3261 | if (leaf->nodetype != LYS_LEAF) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3262 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 3263 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Target is not a leaf."); |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3264 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3265 | } |
| 3266 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3267 | /* check status */ |
Radek Krejci | c3f1b6f | 2017-02-15 10:51:10 +0100 | [diff] [blame] | 3268 | if (parent->nodetype != LYS_EXT && lyp_check_status(parent->flags, parent->module, parent->name, |
| 3269 | leaf->flags, leaf->module, leaf->name, leaf)) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3270 | return -1; |
| 3271 | } |
| 3272 | |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3273 | /* check that all unique's targets are of the same config type */ |
| 3274 | if (*trg_type) { |
| 3275 | if (((*trg_type == 1) && (leaf->flags & LYS_CONFIG_R)) || ((*trg_type == 2) && (leaf->flags & LYS_CONFIG_W))) { |
| 3276 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 3277 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3278 | "Leaf \"%s\" referenced in unique statement is config %s, but previous referenced leaf is config %s.", |
| 3279 | uniq_str_path, *trg_type == 1 ? "false" : "true", *trg_type == 1 ? "true" : "false"); |
| 3280 | return -1; |
| 3281 | } |
| 3282 | } else { |
| 3283 | /* first unique */ |
| 3284 | if (leaf->flags & LYS_CONFIG_W) { |
| 3285 | *trg_type = 1; |
| 3286 | } else { |
| 3287 | *trg_type = 2; |
| 3288 | } |
| 3289 | } |
| 3290 | |
Radek Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame] | 3291 | /* set leaf's unique flag */ |
| 3292 | ((struct lys_node_leaf *)leaf)->flags |= LYS_UNIQUE; |
| 3293 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3294 | return EXIT_SUCCESS; |
| 3295 | |
| 3296 | error: |
| 3297 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3298 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3299 | } |
| 3300 | |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 3301 | void |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3302 | unres_data_del(struct unres_data *unres, uint32_t i) |
| 3303 | { |
| 3304 | /* there are items after the one deleted */ |
| 3305 | if (i+1 < unres->count) { |
| 3306 | /* we only move the data, memory is left allocated, why bother */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3307 | 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] | 3308 | |
| 3309 | /* deleting the last item */ |
| 3310 | } else if (i == 0) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3311 | free(unres->node); |
| 3312 | unres->node = NULL; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3313 | } |
| 3314 | |
| 3315 | /* if there are no items after and it is not the last one, just move the counter */ |
| 3316 | --unres->count; |
| 3317 | } |
| 3318 | |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3319 | /** |
| 3320 | * @brief Resolve (find) a data node from a specific module. Does not log. |
| 3321 | * |
| 3322 | * @param[in] mod Module to search in. |
| 3323 | * @param[in] name Name of the data node. |
| 3324 | * @param[in] nam_len Length of the name. |
| 3325 | * @param[in] start Data node to start the search from. |
| 3326 | * @param[in,out] parents Resolved nodes. If there are some parents, |
| 3327 | * they are replaced (!!) with the resolvents. |
| 3328 | * |
Michal Vasko | 2471e7f | 2016-04-11 11:00:15 +0200 | [diff] [blame] | 3329 | * @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] | 3330 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3331 | static int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3332 | 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] | 3333 | { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3334 | struct lyd_node *node; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3335 | int flag; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3336 | uint32_t i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3337 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3338 | if (!parents->count) { |
| 3339 | parents->count = 1; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3340 | parents->node = malloc(sizeof *parents->node); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3341 | if (!parents->node) { |
| 3342 | LOGMEM; |
Michal Vasko | 2471e7f | 2016-04-11 11:00:15 +0200 | [diff] [blame] | 3343 | return -1; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3344 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3345 | parents->node[0] = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3346 | } |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3347 | for (i = 0; i < parents->count;) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 3348 | 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] | 3349 | /* skip */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3350 | ++i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3351 | continue; |
| 3352 | } |
| 3353 | flag = 0; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3354 | LY_TREE_FOR(parents->node[i] ? parents->node[i]->child : start, node) { |
Michal Vasko | 3960835 | 2017-05-11 10:37:10 +0200 | [diff] [blame] | 3355 | 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] | 3356 | && node->schema->name[nam_len] == '\0') { |
| 3357 | /* matching target */ |
| 3358 | if (!flag) { |
Michal Vasko | 9a47e12 | 2015-09-03 14:26:32 +0200 | [diff] [blame] | 3359 | /* put node instead of the current parent */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3360 | parents->node[i] = node; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3361 | flag = 1; |
| 3362 | } else { |
Michal Vasko | 9a47e12 | 2015-09-03 14:26:32 +0200 | [diff] [blame] | 3363 | /* multiple matching, so create a new node */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3364 | ++parents->count; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3365 | parents->node = ly_realloc(parents->node, parents->count * sizeof *parents->node); |
| 3366 | if (!parents->node) { |
| 3367 | return EXIT_FAILURE; |
| 3368 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3369 | parents->node[parents->count-1] = node; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3370 | ++i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3371 | } |
| 3372 | } |
| 3373 | } |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3374 | |
| 3375 | if (!flag) { |
| 3376 | /* remove item from the parents list */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3377 | unres_data_del(parents, i); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3378 | } else { |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3379 | ++i; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3380 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3381 | } |
| 3382 | |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3383 | return parents->count ? EXIT_SUCCESS : EXIT_FAILURE; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3384 | } |
| 3385 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3386 | /** |
| 3387 | * @brief Resolve (find) a data node. Does not log. |
| 3388 | * |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3389 | * @param[in] mod_name Module name of the data node. |
| 3390 | * @param[in] mod_name_len Length of the module name. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3391 | * @param[in] name Name of the data node. |
| 3392 | * @param[in] nam_len Length of the name. |
| 3393 | * @param[in] start Data node to start the search from. |
| 3394 | * @param[in,out] parents Resolved nodes. If there are some parents, |
| 3395 | * they are replaced (!!) with the resolvents. |
| 3396 | * |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3397 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 otherwise. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3398 | */ |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3399 | static int |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3400 | resolve_data_node(const char *mod_name, int mod_name_len, const char *name, int name_len, struct lyd_node *start, |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3401 | struct unres_data *parents) |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3402 | { |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3403 | const struct lys_module *mod; |
Michal Vasko | 31fc367 | 2015-10-21 12:08:13 +0200 | [diff] [blame] | 3404 | char *str; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3405 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3406 | assert(start); |
| 3407 | |
Michal Vasko | 31fc367 | 2015-10-21 12:08:13 +0200 | [diff] [blame] | 3408 | if (mod_name) { |
| 3409 | /* we have mod_name, find appropriate module */ |
| 3410 | str = strndup(mod_name, mod_name_len); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3411 | if (!str) { |
| 3412 | LOGMEM; |
| 3413 | return -1; |
| 3414 | } |
Michal Vasko | 31fc367 | 2015-10-21 12:08:13 +0200 | [diff] [blame] | 3415 | mod = ly_ctx_get_module(start->schema->module->ctx, str, NULL); |
| 3416 | free(str); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3417 | if (!mod) { |
| 3418 | /* invalid prefix */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3419 | return -1; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3420 | } |
| 3421 | } else { |
| 3422 | /* no prefix, module is the same as of current node */ |
Michal Vasko | 3960835 | 2017-05-11 10:37:10 +0200 | [diff] [blame] | 3423 | mod = lyd_node_module(start); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3424 | } |
| 3425 | |
| 3426 | return resolve_data(mod, name, name_len, start, parents); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3427 | } |
| 3428 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3429 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3430 | * @brief Resolve a path predicate (leafref) in JSON data context. Logs directly |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3431 | * only specific errors, general no-resolvent error is left to the caller. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3432 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3433 | * @param[in] pred Predicate to use. |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3434 | * @param[in] node Node from which the predicate is being resolved |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3435 | * @param[in,out] node_match Nodes satisfying the restriction |
| 3436 | * without the predicate. Nodes not |
| 3437 | * satisfying the predicate are removed. |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3438 | * @param[out] parsed Number of characters parsed, negative on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3439 | * |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3440 | * @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] | 3441 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3442 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3443 | resolve_path_predicate_data(const char *pred, struct lyd_node *node, struct unres_data *node_match, |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 3444 | int *parsed) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3445 | { |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3446 | /* ... /node[source = destination] ... */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3447 | struct unres_data source_match, dest_match; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3448 | const char *path_key_expr, *source, *sour_pref, *dest, *dest_pref; |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3449 | int pke_len, sour_len, sour_pref_len, dest_len, dest_pref_len, parsed_loc = 0, pke_parsed = 0; |
| 3450 | int has_predicate, dest_parent_times, i, rc; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3451 | uint32_t j; |
Radek Krejci | 0c2fa3a | 2016-06-13 15:18:03 +0200 | [diff] [blame] | 3452 | struct lyd_node_leaf_list *leaf_dst, *leaf_src; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3453 | |
| 3454 | source_match.count = 1; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3455 | source_match.node = malloc(sizeof *source_match.node); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3456 | if (!source_match.node) { |
| 3457 | LOGMEM; |
| 3458 | return -1; |
| 3459 | } |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3460 | dest_match.count = 1; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3461 | dest_match.node = malloc(sizeof *dest_match.node); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3462 | if (!dest_match.node) { |
| 3463 | LOGMEM; |
| 3464 | return -1; |
| 3465 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3466 | |
| 3467 | do { |
| 3468 | if ((i = parse_path_predicate(pred, &sour_pref, &sour_pref_len, &source, &sour_len, &path_key_expr, |
| 3469 | &pke_len, &has_predicate)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3470 | LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, pred[-i], &pred[-i]); |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3471 | rc = -1; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3472 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3473 | } |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3474 | parsed_loc += i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3475 | pred += i; |
| 3476 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3477 | for (j = 0; j < node_match->count;) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3478 | /* source */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3479 | source_match.node[0] = node_match->node[j]; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3480 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3481 | /* must be leaf (key of a list) */ |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3482 | if ((rc = resolve_data_node(sour_pref, sour_pref_len, source, sour_len, node_match->node[j], |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3483 | &source_match)) || (source_match.count != 1) || (source_match.node[0]->schema->nodetype != LYS_LEAF)) { |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3484 | i = 0; |
| 3485 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3486 | } |
| 3487 | |
| 3488 | /* destination */ |
Radek Krejci | 0c2fa3a | 2016-06-13 15:18:03 +0200 | [diff] [blame] | 3489 | dest_match.node[0] = node; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3490 | dest_parent_times = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3491 | if ((i = parse_path_key_expr(path_key_expr, &dest_pref, &dest_pref_len, &dest, &dest_len, |
| 3492 | &dest_parent_times)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3493 | LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, path_key_expr[-i], &path_key_expr[-i]); |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3494 | rc = -1; |
| 3495 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3496 | } |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3497 | pke_parsed = i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3498 | for (i = 0; i < dest_parent_times; ++i) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3499 | dest_match.node[0] = dest_match.node[0]->parent; |
| 3500 | if (!dest_match.node[0]) { |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3501 | i = 0; |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3502 | rc = EXIT_FAILURE; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3503 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3504 | } |
| 3505 | } |
| 3506 | while (1) { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3507 | if ((rc = resolve_data_node(dest_pref, dest_pref_len, dest, dest_len, dest_match.node[0], |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3508 | &dest_match)) || (dest_match.count != 1)) { |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3509 | i = 0; |
| 3510 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3511 | } |
| 3512 | |
| 3513 | if (pke_len == pke_parsed) { |
| 3514 | break; |
| 3515 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3516 | if ((i = parse_path_key_expr(path_key_expr+pke_parsed, &dest_pref, &dest_pref_len, &dest, &dest_len, |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3517 | &dest_parent_times)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3518 | LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, path_key_expr[-i], &path_key_expr[-i]); |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3519 | rc = -1; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3520 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3521 | } |
| 3522 | pke_parsed += i; |
| 3523 | } |
| 3524 | |
| 3525 | /* check match between source and destination nodes */ |
Radek Krejci | 0c2fa3a | 2016-06-13 15:18:03 +0200 | [diff] [blame] | 3526 | leaf_dst = (struct lyd_node_leaf_list *)dest_match.node[0]; |
Radek Krejci | 981a8ee | 2017-03-17 16:48:26 +0100 | [diff] [blame] | 3527 | while (leaf_dst && leaf_dst->value_type == LY_TYPE_LEAFREF) { |
Radek Krejci | 0c2fa3a | 2016-06-13 15:18:03 +0200 | [diff] [blame] | 3528 | leaf_dst = (struct lyd_node_leaf_list *)leaf_dst->value.leafref; |
| 3529 | } |
| 3530 | leaf_src = (struct lyd_node_leaf_list *)source_match.node[0]; |
Radek Krejci | 981a8ee | 2017-03-17 16:48:26 +0100 | [diff] [blame] | 3531 | while (leaf_src && leaf_src->value_type == LY_TYPE_LEAFREF) { |
Radek Krejci | 0c2fa3a | 2016-06-13 15:18:03 +0200 | [diff] [blame] | 3532 | leaf_src = (struct lyd_node_leaf_list *)leaf_src->value.leafref; |
| 3533 | } |
Radek Krejci | 981a8ee | 2017-03-17 16:48:26 +0100 | [diff] [blame] | 3534 | if (!leaf_src || !leaf_dst) { |
| 3535 | /* not yet resolved leafrefs */ |
| 3536 | return EXIT_FAILURE; |
| 3537 | } |
Radek Krejci | c7408e7 | 2017-03-17 10:43:51 +0100 | [diff] [blame] | 3538 | if ((leaf_src->value_type & LY_DATA_TYPE_MASK) != (leaf_dst->value_type & LY_DATA_TYPE_MASK)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3539 | goto remove_leafref; |
| 3540 | } |
| 3541 | |
Radek Krejci | 0c2fa3a | 2016-06-13 15:18:03 +0200 | [diff] [blame] | 3542 | if (!ly_strequal(leaf_src->value_str, leaf_dst->value_str, 1)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3543 | goto remove_leafref; |
| 3544 | } |
| 3545 | |
| 3546 | /* leafref is ok, continue check with next leafref */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3547 | ++j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3548 | continue; |
| 3549 | |
| 3550 | remove_leafref: |
| 3551 | /* does not fulfill conditions, remove leafref record */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3552 | unres_data_del(node_match, j); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3553 | } |
| 3554 | } while (has_predicate); |
| 3555 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3556 | free(source_match.node); |
| 3557 | free(dest_match.node); |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3558 | if (parsed) { |
| 3559 | *parsed = parsed_loc; |
| 3560 | } |
| 3561 | return EXIT_SUCCESS; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3562 | |
| 3563 | error: |
| 3564 | |
| 3565 | if (source_match.count) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3566 | free(source_match.node); |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3567 | } |
| 3568 | if (dest_match.count) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3569 | free(dest_match.node); |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3570 | } |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3571 | if (parsed) { |
| 3572 | *parsed = -parsed_loc+i; |
| 3573 | } |
| 3574 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3575 | } |
| 3576 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3577 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3578 | * @brief Resolve a path (leafref) in JSON data context. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3579 | * |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3580 | * @param[in] node Leafref data node. |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3581 | * @param[in] path Path of the leafref. |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3582 | * @param[out] ret Matching nodes. Expects an empty, but allocated structure. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3583 | * |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3584 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 otherwise. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3585 | */ |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3586 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3587 | resolve_path_arg_data(struct lyd_node *node, const char *path, struct unres_data *ret) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3588 | { |
Radek Krejci | 71b795b | 2015-08-10 16:20:39 +0200 | [diff] [blame] | 3589 | struct lyd_node *data = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3590 | const char *prefix, *name; |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3591 | int pref_len, nam_len, has_predicate, parent_times, i, parsed, rc; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3592 | uint32_t j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3593 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3594 | assert(node && path && ret && !ret->count); |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3595 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3596 | parent_times = 0; |
Michal Vasko | d917334 | 2015-08-17 14:35:36 +0200 | [diff] [blame] | 3597 | parsed = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3598 | |
| 3599 | /* searching for nodeset */ |
| 3600 | do { |
Radek Krejci | f7ed4c3 | 2016-10-27 16:20:03 +0200 | [diff] [blame] | 3601 | if ((i = parse_path_arg(node->schema->module, path, &prefix, &pref_len, &name, &nam_len, &parent_times, &has_predicate)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3602 | LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, path[-i], &path[-i]); |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3603 | rc = -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3604 | goto error; |
| 3605 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3606 | path += i; |
Michal Vasko | d917334 | 2015-08-17 14:35:36 +0200 | [diff] [blame] | 3607 | parsed += i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3608 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3609 | if (!ret->count) { |
Michal Vasko | bfd98e6 | 2016-09-02 09:50:05 +0200 | [diff] [blame] | 3610 | if (parent_times > 0) { |
| 3611 | data = node; |
| 3612 | for (i = 1; i < parent_times; ++i) { |
| 3613 | data = data->parent; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3614 | } |
Michal Vasko | bfd98e6 | 2016-09-02 09:50:05 +0200 | [diff] [blame] | 3615 | } else if (!parent_times) { |
| 3616 | data = node->child; |
| 3617 | } else { |
| 3618 | /* absolute path */ |
| 3619 | for (data = node; data->parent; data = data->parent); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3620 | } |
| 3621 | |
Michal Vasko | bfd98e6 | 2016-09-02 09:50:05 +0200 | [diff] [blame] | 3622 | /* we may still be parsing it and the pointer is not correct yet */ |
| 3623 | if (data->prev) { |
| 3624 | while (data->prev->next) { |
| 3625 | data = data->prev; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 3626 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3627 | } |
| 3628 | } |
| 3629 | |
| 3630 | /* node identifier */ |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3631 | if ((rc = resolve_data_node(prefix, pref_len, name, nam_len, data, ret))) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 3632 | if (rc == -1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3633 | LOGVAL(LYE_INELEM_LEN, LY_VLOG_LYD, node, nam_len, name); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3634 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3635 | goto error; |
| 3636 | } |
| 3637 | |
| 3638 | if (has_predicate) { |
| 3639 | /* we have predicate, so the current results must be lists */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3640 | for (j = 0; j < ret->count;) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3641 | if (ret->node[j]->schema->nodetype == LYS_LIST && |
| 3642 | ((struct lys_node_list *)ret->node[0]->schema)->keys) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3643 | /* leafref is ok, continue check with next leafref */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3644 | ++j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3645 | continue; |
| 3646 | } |
| 3647 | |
| 3648 | /* does not fulfill conditions, remove leafref record */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3649 | unres_data_del(ret, j); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3650 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3651 | if ((rc = resolve_path_predicate_data(path, node, ret, &i))) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 3652 | if (rc == -1) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3653 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYD, node, "leafref", path); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3654 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3655 | goto error; |
| 3656 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3657 | path += i; |
Michal Vasko | d917334 | 2015-08-17 14:35:36 +0200 | [diff] [blame] | 3658 | parsed += i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3659 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3660 | if (!ret->count) { |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3661 | rc = EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3662 | goto error; |
| 3663 | } |
| 3664 | } |
| 3665 | } while (path[0] != '\0'); |
| 3666 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3667 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3668 | |
| 3669 | error: |
| 3670 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3671 | free(ret->node); |
| 3672 | ret->node = NULL; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3673 | ret->count = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3674 | |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3675 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3676 | } |
| 3677 | |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3678 | static int |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3679 | 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] | 3680 | { |
| 3681 | int dep1, dep2; |
| 3682 | const struct lys_node *node; |
| 3683 | |
| 3684 | if (lys_parent(op_node)) { |
| 3685 | /* inner operation (notif/action) */ |
| 3686 | if (abs_path) { |
| 3687 | return 1; |
| 3688 | } else { |
| 3689 | /* compare depth of both nodes */ |
| 3690 | for (dep1 = 0, node = op_node; lys_parent(node); node = lys_parent(node)); |
| 3691 | for (dep2 = 0, node = first_node; lys_parent(node); node = lys_parent(node)); |
| 3692 | if ((dep2 > dep1) || ((dep2 == dep1) && (op_node != first_node))) { |
| 3693 | return 1; |
| 3694 | } |
| 3695 | } |
| 3696 | } else { |
| 3697 | /* top-level operation (notif/rpc) */ |
| 3698 | if (op_node != first_node) { |
| 3699 | return 1; |
| 3700 | } |
| 3701 | } |
| 3702 | |
| 3703 | return 0; |
| 3704 | } |
| 3705 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3706 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3707 | * @brief Resolve a path (leafref) predicate in JSON schema context. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3708 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3709 | * @param[in] path Path to use. |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3710 | * @param[in] context_node Predicate context node (where the predicate is placed). |
| 3711 | * @param[in] parent Path context node (where the path begins/is placed). |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3712 | * @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] | 3713 | * |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3714 | * @return 0 on forward reference, otherwise the number |
| 3715 | * of characters successfully parsed, |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3716 | * positive on success, negative on failure. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3717 | */ |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3718 | static int |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3719 | resolve_schema_leafref_predicate(const char *path, const struct lys_node *context_node, |
| 3720 | struct lys_node *parent, const struct lys_node *op_node) |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3721 | { |
Michal Vasko | 0f99d3e | 2017-01-10 10:50:40 +0100 | [diff] [blame] | 3722 | const struct lys_module *trg_mod; |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3723 | const struct lys_node *src_node, *dst_node; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3724 | const char *path_key_expr, *source, *sour_pref, *dest, *dest_pref; |
Michal Vasko | f9b35d9 | 2016-10-21 15:19:30 +0200 | [diff] [blame] | 3725 | int pke_len, sour_len, sour_pref_len, dest_len, dest_pref_len, pke_parsed, parsed = 0; |
| 3726 | int has_predicate, dest_parent_times, i, rc, first_iter; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3727 | |
| 3728 | do { |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3729 | 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] | 3730 | &pke_len, &has_predicate)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3731 | 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] | 3732 | return -parsed+i; |
| 3733 | } |
| 3734 | parsed += i; |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3735 | path += i; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3736 | |
Michal Vasko | 5809090 | 2015-08-13 14:04:15 +0200 | [diff] [blame] | 3737 | /* source (must be leaf) */ |
Michal Vasko | 0f99d3e | 2017-01-10 10:50:40 +0100 | [diff] [blame] | 3738 | if (sour_pref) { |
| 3739 | trg_mod = lys_get_import_module(lys_node_module(parent), NULL, 0, sour_pref, sour_pref_len); |
| 3740 | } else { |
| 3741 | trg_mod = NULL; |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3742 | } |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3743 | 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] | 3744 | if (rc) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3745 | 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] | 3746 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3747 | } |
| 3748 | |
| 3749 | /* destination */ |
Michal Vasko | f9b35d9 | 2016-10-21 15:19:30 +0200 | [diff] [blame] | 3750 | dest_parent_times = 0; |
| 3751 | pke_parsed = 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3752 | if ((i = parse_path_key_expr(path_key_expr, &dest_pref, &dest_pref_len, &dest, &dest_len, |
| 3753 | &dest_parent_times)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3754 | 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] | 3755 | return -parsed; |
| 3756 | } |
| 3757 | pke_parsed += i; |
| 3758 | |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3759 | for (i = 0, dst_node = parent; i < dest_parent_times; ++i) { |
Michal Vasko | fbaead7 | 2016-10-07 10:54:48 +0200 | [diff] [blame] | 3760 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 3761 | * all schema nodes that cannot be instantiated in data tree */ |
| 3762 | for (dst_node = lys_parent(dst_node); |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3763 | 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] | 3764 | dst_node = lys_parent(dst_node)); |
| 3765 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3766 | if (!dst_node) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3767 | 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] | 3768 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3769 | } |
| 3770 | } |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3771 | first_iter = 1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3772 | while (1) { |
Michal Vasko | 0f99d3e | 2017-01-10 10:50:40 +0100 | [diff] [blame] | 3773 | if (dest_pref) { |
| 3774 | trg_mod = lys_get_import_module(lys_node_module(parent), NULL, 0, dest_pref, dest_pref_len); |
| 3775 | } else { |
| 3776 | trg_mod = NULL; |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3777 | } |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3778 | 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] | 3779 | if (rc) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3780 | 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] | 3781 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3782 | } |
| 3783 | |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3784 | if (first_iter) { |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3785 | if (resolve_schema_leafref_valid_dep_flag(op_node, dst_node, 0)) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 3786 | parent->flags |= LYS_LEAFREF_DEP; |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3787 | } |
| 3788 | first_iter = 0; |
| 3789 | } |
| 3790 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3791 | if (pke_len == pke_parsed) { |
| 3792 | break; |
| 3793 | } |
| 3794 | |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3795 | 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] | 3796 | &dest_parent_times)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3797 | LOGVAL(LYE_INCHAR, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3798 | (path_key_expr + pke_parsed)[-i], (path_key_expr + pke_parsed)-i); |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3799 | return -parsed; |
| 3800 | } |
| 3801 | pke_parsed += i; |
| 3802 | } |
| 3803 | |
| 3804 | /* check source - dest match */ |
Michal Vasko | 59ad458 | 2016-09-16 13:15:41 +0200 | [diff] [blame] | 3805 | if (dst_node->nodetype != src_node->nodetype) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3806 | 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] | 3807 | 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] | 3808 | strnodetype(src_node->nodetype), strnodetype(dst_node->nodetype)); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3809 | return -parsed; |
| 3810 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3811 | } while (has_predicate); |
| 3812 | |
| 3813 | return parsed; |
| 3814 | } |
| 3815 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3816 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3817 | * @brief Resolve a path (leafref) in JSON schema context. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3818 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3819 | * @param[in] path Path to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3820 | * @param[in] parent_node Parent of the leafref. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3821 | * @param[out] ret Pointer to the resolved schema node. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3822 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3823 | * @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] | 3824 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3825 | static int |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3826 | 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] | 3827 | { |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3828 | const struct lys_node *node, *op_node = NULL; |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3829 | struct lys_node_augment *last_aug; |
| 3830 | const struct lys_module *cur_mod; |
Radek Krejci | 990af1f | 2016-11-09 13:53:36 +0100 | [diff] [blame] | 3831 | struct lys_module *mod_start; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3832 | const char *id, *prefix, *name; |
| 3833 | int pref_len, nam_len, parent_times, has_predicate; |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3834 | int i, first_iter, rc; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3835 | |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3836 | first_iter = 1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3837 | parent_times = 0; |
| 3838 | id = path; |
| 3839 | |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3840 | /* find operation schema we are in */ |
| 3841 | for (op_node = lys_parent(parent); |
| 3842 | op_node && !(op_node->nodetype & (LYS_ACTION | LYS_NOTIF | LYS_RPC)); |
| 3843 | op_node = lys_parent(op_node)); |
Michal Vasko | e9914d1 | 2016-10-07 14:32:37 +0200 | [diff] [blame] | 3844 | |
Radek Krejci | 990af1f | 2016-11-09 13:53:36 +0100 | [diff] [blame] | 3845 | mod_start = lys_node_module(parent); |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3846 | do { |
Radek Krejci | 990af1f | 2016-11-09 13:53:36 +0100 | [diff] [blame] | 3847 | if ((i = parse_path_arg(mod_start, id, &prefix, &pref_len, &name, &nam_len, &parent_times, &has_predicate)) < 1) { |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3848 | LOGVAL(LYE_INCHAR, LY_VLOG_LYS, parent, id[-i], &id[-i]); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3849 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3850 | } |
| 3851 | id += i; |
| 3852 | |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3853 | /* get the current module */ |
| 3854 | cur_mod = prefix ? lys_get_import_module(mod_start, NULL, 0, prefix, pref_len) : mod_start; |
| 3855 | if (!cur_mod) { |
| 3856 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path); |
| 3857 | return EXIT_FAILURE; |
| 3858 | } |
| 3859 | last_aug = NULL; |
| 3860 | |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3861 | if (first_iter) { |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3862 | if (parent_times == -1) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3863 | /* use module data */ |
| 3864 | node = NULL; |
Radek Krejci | 990af1f | 2016-11-09 13:53:36 +0100 | [diff] [blame] | 3865 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3866 | } else if (parent_times > 0) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3867 | /* we are looking for the right parent */ |
| 3868 | for (i = 0, node = parent; i < parent_times; i++) { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 3869 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 3870 | * all schema nodes that cannot be instantiated in data tree */ |
| 3871 | for (node = lys_parent(node); |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3872 | 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] | 3873 | node = lys_parent(node)); |
| 3874 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3875 | if (!node) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3876 | if (i == parent_times - 1) { |
| 3877 | /* top-level */ |
| 3878 | break; |
| 3879 | } |
| 3880 | |
| 3881 | /* higher than top-level */ |
Michal Vasko | e9914d1 | 2016-10-07 14:32:37 +0200 | [diff] [blame] | 3882 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3883 | return EXIT_FAILURE; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3884 | } |
| 3885 | } |
Michal Vasko | e01eca5 | 2015-08-13 14:42:02 +0200 | [diff] [blame] | 3886 | } else { |
| 3887 | LOGINT; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3888 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3889 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3890 | } |
| 3891 | |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3892 | /* find the next node (either in unconnected augment or as a schema sibling, node is NULL for top-level node - |
| 3893 | * - useless to search for that in augments) */ |
| 3894 | if (!cur_mod->implemented && node) { |
| 3895 | get_next_augment: |
| 3896 | last_aug = lys_getnext_target_aug(last_aug, cur_mod, node); |
| 3897 | } |
| 3898 | |
| 3899 | rc = lys_getnext_data(cur_mod, (last_aug ? (struct lys_node *)last_aug : node), name, nam_len, LYS_LIST |
| 3900 | | 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] | 3901 | if (rc) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3902 | if (last_aug) { |
| 3903 | goto get_next_augment; |
| 3904 | } |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3905 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path); |
Michal Vasko | 7a55bea | 2016-05-02 14:51:20 +0200 | [diff] [blame] | 3906 | return EXIT_FAILURE; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3907 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3908 | |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3909 | if (first_iter) { |
| 3910 | /* 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] | 3911 | if (op_node && parent_times && |
| 3912 | 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] | 3913 | parent->flags |= LYS_LEAFREF_DEP; |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3914 | } |
| 3915 | first_iter = 0; |
| 3916 | } |
| 3917 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3918 | if (has_predicate) { |
| 3919 | /* we have predicate, so the current result must be list */ |
| 3920 | if (node->nodetype != LYS_LIST) { |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3921 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3922 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3923 | } |
| 3924 | |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3925 | i = resolve_schema_leafref_predicate(id, node, parent, op_node); |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3926 | if (!i) { |
| 3927 | return EXIT_FAILURE; |
| 3928 | } else if (i < 0) { |
| 3929 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3930 | } |
| 3931 | id += i; |
Michal Vasko | f9b35d9 | 2016-10-21 15:19:30 +0200 | [diff] [blame] | 3932 | has_predicate = 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3933 | } |
| 3934 | } while (id[0]); |
| 3935 | |
Michal Vasko | ca91768 | 2016-07-25 11:00:37 +0200 | [diff] [blame] | 3936 | /* 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] | 3937 | if ((node->nodetype != LYS_LEAF) && (node->nodetype != LYS_LEAFLIST)) { |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3938 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 3939 | 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] | 3940 | return -1; |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 3941 | } |
| 3942 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3943 | /* check status */ |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3944 | if (lyp_check_status(parent->flags, parent->module, parent->name, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3945 | node->flags, node->module, node->name, node)) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3946 | return -1; |
| 3947 | } |
| 3948 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3949 | if (ret) { |
| 3950 | *ret = node; |
| 3951 | } |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 3952 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3953 | return EXIT_SUCCESS; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3954 | } |
| 3955 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3956 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3957 | * @brief Resolve instance-identifier predicate in JSON data format. |
| 3958 | * Does not log. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3959 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3960 | * @param[in] pred Predicate to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3961 | * @param[in,out] node_match Nodes matching the restriction without |
| 3962 | * the predicate. Nodes not satisfying |
| 3963 | * the predicate are removed. |
| 3964 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3965 | * @return Number of characters successfully parsed, |
| 3966 | * positive on success, negative on failure. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3967 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3968 | static int |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3969 | resolve_instid_predicate(const char *pred, struct unres_data *node_match) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3970 | { |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3971 | /* ... /node[target = value] ... */ |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3972 | struct lyd_node *target; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3973 | const char *model, *name, *value; |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3974 | 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] | 3975 | uint32_t j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3976 | |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3977 | assert(pred && node_match->count); |
| 3978 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3979 | idx = -1; |
| 3980 | parsed = 0; |
| 3981 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3982 | pred_iter = -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3983 | do { |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3984 | 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] | 3985 | return -parsed+i; |
| 3986 | } |
| 3987 | parsed += i; |
| 3988 | pred += i; |
| 3989 | |
| 3990 | if (isdigit(name[0])) { |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3991 | /* pos */ |
| 3992 | assert(!value); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3993 | idx = atoi(name); |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3994 | } else if (name[0] != '.') { |
| 3995 | /* list keys */ |
| 3996 | if (pred_iter < 0) { |
| 3997 | pred_iter = 1; |
| 3998 | } else { |
| 3999 | ++pred_iter; |
| 4000 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4001 | } |
| 4002 | |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 4003 | for (cur_idx = 1, j = 0; j < node_match->count; ++cur_idx) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4004 | /* target */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4005 | if (name[0] == '.') { |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4006 | /* leaf-list value */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4007 | if (node_match->node[j]->schema->nodetype != LYS_LEAFLIST) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4008 | goto remove_instid; |
| 4009 | } |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4010 | |
| 4011 | target = node_match->node[j]; |
| 4012 | /* check the value */ |
| 4013 | if (strncmp(((struct lyd_node_leaf_list *)target)->value_str, value, val_len) |
| 4014 | || ((struct lyd_node_leaf_list *)target)->value_str[val_len]) { |
| 4015 | goto remove_instid; |
| 4016 | } |
| 4017 | |
| 4018 | } else if (!value) { |
| 4019 | /* keyless list position */ |
| 4020 | if ((node_match->node[j]->schema->nodetype != LYS_LIST) |
| 4021 | || ((struct lys_node_list *)node_match->node[j]->schema)->keys) { |
| 4022 | goto remove_instid; |
| 4023 | } |
| 4024 | |
| 4025 | if (idx != cur_idx) { |
| 4026 | goto remove_instid; |
| 4027 | } |
| 4028 | |
| 4029 | } else { |
| 4030 | /* list key value */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4031 | if (node_match->node[j]->schema->nodetype != LYS_LIST) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4032 | goto remove_instid; |
| 4033 | } |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4034 | |
| 4035 | /* key module must match the list module */ |
| 4036 | if (strncmp(node_match->node[j]->schema->module->name, model, mod_len) |
| 4037 | || node_match->node[j]->schema->module->name[mod_len]) { |
| 4038 | goto remove_instid; |
| 4039 | } |
| 4040 | /* find the key leaf */ |
Michal Vasko | 045182c | 2016-09-09 12:44:07 +0200 | [diff] [blame] | 4041 | 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] | 4042 | if (!target) { |
| 4043 | goto remove_instid; |
| 4044 | } |
| 4045 | if ((struct lys_node_leaf *)target->schema != |
| 4046 | ((struct lys_node_list *)node_match->node[j]->schema)->keys[pred_iter - 1]) { |
| 4047 | goto remove_instid; |
| 4048 | } |
| 4049 | |
| 4050 | /* check the value */ |
| 4051 | if (strncmp(((struct lyd_node_leaf_list *)target)->value_str, value, val_len) |
| 4052 | || ((struct lyd_node_leaf_list *)target)->value_str[val_len]) { |
| 4053 | goto remove_instid; |
| 4054 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4055 | } |
| 4056 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4057 | /* instid is ok, continue check with the next one */ |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4058 | ++j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4059 | continue; |
| 4060 | |
| 4061 | remove_instid: |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4062 | /* does not fulfill conditions, remove instid record */ |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4063 | unres_data_del(node_match, j); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4064 | } |
| 4065 | } while (has_predicate); |
| 4066 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4067 | /* check that all list keys were specified */ |
| 4068 | if ((pred_iter > 0) && node_match->count) { |
Michal Vasko | 045182c | 2016-09-09 12:44:07 +0200 | [diff] [blame] | 4069 | j = 0; |
| 4070 | while (j < node_match->count) { |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4071 | assert(node_match->node[j]->schema->nodetype == LYS_LIST); |
| 4072 | if (pred_iter < ((struct lys_node_list *)node_match->node[j]->schema)->keys_size) { |
| 4073 | /* not enough predicates, just remove the list instance */ |
| 4074 | unres_data_del(node_match, j); |
Michal Vasko | 045182c | 2016-09-09 12:44:07 +0200 | [diff] [blame] | 4075 | } else { |
| 4076 | ++j; |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4077 | } |
| 4078 | } |
| 4079 | |
| 4080 | if (!node_match->count) { |
| 4081 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier is missing some list keys."); |
| 4082 | } |
| 4083 | } |
| 4084 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4085 | return parsed; |
| 4086 | } |
| 4087 | |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4088 | int |
Michal Vasko | 769f803 | 2017-01-24 13:11:55 +0100 | [diff] [blame] | 4089 | 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] | 4090 | { |
| 4091 | struct lys_node *parent, *elem; |
| 4092 | struct lyxp_set set; |
| 4093 | uint32_t i; |
Michal Vasko | 769f803 | 2017-01-24 13:11:55 +0100 | [diff] [blame] | 4094 | int ret; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4095 | |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4096 | if (check_place) { |
| 4097 | parent = node; |
| 4098 | while (parent) { |
| 4099 | if (parent->nodetype == LYS_GROUPING) { |
| 4100 | /* unresolved grouping, skip for now (will be checked later) */ |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4101 | return EXIT_SUCCESS; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4102 | } |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4103 | if (parent->nodetype == LYS_AUGMENT) { |
| 4104 | if (!((struct lys_node_augment *)parent)->target) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4105 | /* unresolved augment, skip for now (will be checked later) */ |
| 4106 | return EXIT_FAILURE; |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4107 | } else { |
| 4108 | parent = ((struct lys_node_augment *)parent)->target; |
| 4109 | continue; |
| 4110 | } |
| 4111 | } |
| 4112 | parent = parent->parent; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4113 | } |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4114 | } |
| 4115 | |
Michal Vasko | 769f803 | 2017-01-24 13:11:55 +0100 | [diff] [blame] | 4116 | ret = lyxp_node_atomize(node, &set, warn_on_fwd_ref); |
| 4117 | if (ret == -1) { |
| 4118 | return -1; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4119 | } |
| 4120 | |
| 4121 | for (parent = node; parent && !(parent->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); parent = lys_parent(parent)); |
| 4122 | |
| 4123 | for (i = 0; i < set.used; ++i) { |
| 4124 | /* skip roots'n'stuff */ |
| 4125 | if (set.val.snodes[i].type == LYXP_NODE_ELEM) { |
| 4126 | /* XPath expression cannot reference "lower" status than the node that has the definition */ |
| 4127 | if (lyp_check_status(node->flags, lys_node_module(node), node->name, set.val.snodes[i].snode->flags, |
| 4128 | lys_node_module(set.val.snodes[i].snode), set.val.snodes[i].snode->name, node)) { |
| 4129 | return -1; |
| 4130 | } |
| 4131 | |
| 4132 | if (parent) { |
| 4133 | for (elem = set.val.snodes[i].snode; elem && (elem != parent); elem = lys_parent(elem)); |
| 4134 | if (!elem) { |
| 4135 | /* not in node's RPC or notification subtree, set the flag */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 4136 | node->flags |= LYS_XPATH_DEP; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4137 | break; |
| 4138 | } |
| 4139 | } |
| 4140 | } |
| 4141 | } |
| 4142 | |
| 4143 | free(set.val.snodes); |
Michal Vasko | 769f803 | 2017-01-24 13:11:55 +0100 | [diff] [blame] | 4144 | return ret; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4145 | } |
| 4146 | |
Radek Krejci | f71f48f | 2016-10-25 16:37:24 +0200 | [diff] [blame] | 4147 | static int |
| 4148 | check_leafref_config(struct lys_node_leaf *leaf, struct lys_type *type) |
| 4149 | { |
| 4150 | int i; |
| 4151 | |
| 4152 | if (type->base == LY_TYPE_LEAFREF) { |
Radek Krejci | c688ca0 | 2017-03-20 12:54:39 +0100 | [diff] [blame] | 4153 | if ((leaf->flags & LYS_CONFIG_W) && type->info.lref.target && type->info.lref.req != -1 && |
| 4154 | (type->info.lref.target->flags & LYS_CONFIG_R)) { |
Radek Krejci | d831dd4 | 2017-03-16 12:59:30 +0100 | [diff] [blame] | 4155 | 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] | 4156 | strnodetype(leaf->nodetype), strnodetype(type->info.lref.target->nodetype)); |
| 4157 | return -1; |
| 4158 | } |
| 4159 | /* we can skip the test in case the leafref is not yet resolved. In that case the test is done in the time |
| 4160 | * of leafref resolving (lys_leaf_add_leafref_target()) */ |
| 4161 | } else if (type->base == LY_TYPE_UNION) { |
| 4162 | for (i = 0; i < type->info.uni.count; i++) { |
| 4163 | if (check_leafref_config(leaf, &type->info.uni.types[i])) { |
| 4164 | return -1; |
| 4165 | } |
| 4166 | } |
| 4167 | } |
| 4168 | return 0; |
| 4169 | } |
| 4170 | |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4171 | /** |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4172 | * @brief Passes config flag down to children, skips nodes without config flags. |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4173 | * Logs. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4174 | * |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4175 | * @param[in] node Siblings and their children to have flags changed. |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4176 | * @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] | 4177 | * @param[in] flags Flags to assign to all the nodes. |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 4178 | * @param[in,out] unres List of unresolved items. |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4179 | * |
| 4180 | * @return 0 on success, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4181 | */ |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4182 | int |
| 4183 | inherit_config_flag(struct lys_node *node, int flags, int clear) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4184 | { |
Radek Krejci | f71f48f | 2016-10-25 16:37:24 +0200 | [diff] [blame] | 4185 | struct lys_node_leaf *leaf; |
| 4186 | |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4187 | assert(!(flags ^ (flags & LYS_CONFIG_MASK))); |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4188 | LY_TREE_FOR(node, node) { |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4189 | if (clear) { |
| 4190 | node->flags &= ~LYS_CONFIG_MASK; |
Michal Vasko | c2a8d36 | 2016-09-29 08:50:13 +0200 | [diff] [blame] | 4191 | node->flags &= ~LYS_CONFIG_SET; |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4192 | } else { |
| 4193 | if (node->flags & LYS_CONFIG_SET) { |
| 4194 | /* skip nodes with an explicit config value */ |
| 4195 | if ((flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) { |
| 4196 | LOGVAL(LYE_INARG, LY_VLOG_LYS, node, "true", "config"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 4197 | 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] | 4198 | return -1; |
| 4199 | } |
| 4200 | continue; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4201 | } |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4202 | |
| 4203 | if (!(node->nodetype & (LYS_USES | LYS_GROUPING))) { |
| 4204 | node->flags = (node->flags & ~LYS_CONFIG_MASK) | flags; |
| 4205 | /* check that configuration lists have keys */ |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4206 | if ((node->nodetype == LYS_LIST) && (node->flags & LYS_CONFIG_W) |
| 4207 | && !((struct lys_node_list *)node)->keys_size) { |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4208 | LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_LYS, node, "key", "list"); |
| 4209 | return -1; |
| 4210 | } |
| 4211 | } |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4212 | } |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4213 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4214 | if (inherit_config_flag(node->child, flags, clear)) { |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4215 | return -1; |
| 4216 | } |
Radek Krejci | f71f48f | 2016-10-25 16:37:24 +0200 | [diff] [blame] | 4217 | } else if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 4218 | leaf = (struct lys_node_leaf *)node; |
| 4219 | if (check_leafref_config(leaf, &leaf->type)) { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 4220 | return -1; |
| 4221 | } |
| 4222 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4223 | } |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4224 | |
| 4225 | return 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4226 | } |
| 4227 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4228 | /** |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 4229 | * @brief Resolve augment target. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4230 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4231 | * @param[in] aug Augment to use. |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 4232 | * @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] | 4233 | * @param[in,out] unres List of unresolved items. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4234 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4235 | * @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] | 4236 | */ |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 4237 | static int |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 4238 | 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] | 4239 | { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4240 | int rc; |
Michal Vasko | 1d87a92 | 2015-08-21 12:57:16 +0200 | [diff] [blame] | 4241 | struct lys_node *sub; |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4242 | struct lys_module *mod; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4243 | |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4244 | assert(aug && !aug->target); |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 4245 | mod = lys_main_module(aug->module); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4246 | |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 4247 | /* set it as not applied for now */ |
| 4248 | aug->flags |= LYS_NOTAPPLIED; |
| 4249 | |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4250 | /* resolve target node */ |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 4251 | rc = resolve_augment_schema_nodeid(aug->target_name, siblings, (siblings ? NULL : aug->module), |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4252 | (const struct lys_node **)&aug->target); |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4253 | if (rc == -1) { |
| 4254 | return -1; |
Radek Krejci | 5fb4c38 | 2016-11-28 09:21:42 +0100 | [diff] [blame] | 4255 | } else if (rc > 0) { |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4256 | LOGVAL(LYE_INCHAR, LY_VLOG_LYS, aug, aug->target_name[rc - 1], &aug->target_name[rc - 1]); |
| 4257 | return -1; |
| 4258 | } |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4259 | if (!aug->target) { |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4260 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, aug, "augment", aug->target_name); |
| 4261 | return EXIT_FAILURE; |
| 4262 | } |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4263 | |
Michal Vasko | d58d596 | 2016-03-02 14:29:41 +0100 | [diff] [blame] | 4264 | /* check for mandatory nodes - if the target node is in another module |
| 4265 | * the added nodes cannot be mandatory |
| 4266 | */ |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4267 | if (!aug->parent && (lys_node_module((struct lys_node *)aug) != lys_node_module(aug->target)) |
| 4268 | && (rc = lyp_check_mandatory_augment(aug, aug->target))) { |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 4269 | return rc; |
Michal Vasko | d58d596 | 2016-03-02 14:29:41 +0100 | [diff] [blame] | 4270 | } |
| 4271 | |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4272 | /* check augment target type and then augment nodes type */ |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4273 | if (aug->target->nodetype & (LYS_CONTAINER | LYS_LIST)) { |
Michal Vasko | db01726 | 2017-01-24 13:10:04 +0100 | [diff] [blame] | 4274 | LY_TREE_FOR(aug->child, sub) { |
| 4275 | if (!(sub->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_LEAF | LYS_LIST | LYS_LEAFLIST | LYS_USES |
| 4276 | | LYS_CHOICE | LYS_ACTION | LYS_NOTIF))) { |
| 4277 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment"); |
| 4278 | 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] | 4279 | strnodetype(aug->target->nodetype), strnodetype(sub->nodetype)); |
Michal Vasko | db01726 | 2017-01-24 13:10:04 +0100 | [diff] [blame] | 4280 | return -1; |
| 4281 | } |
| 4282 | } |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4283 | } 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] | 4284 | LY_TREE_FOR(aug->child, sub) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4285 | 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] | 4286 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 4287 | 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] | 4288 | strnodetype(aug->target->nodetype), strnodetype(sub->nodetype)); |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4289 | return -1; |
| 4290 | } |
| 4291 | } |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4292 | } else if (aug->target->nodetype == LYS_CHOICE) { |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4293 | LY_TREE_FOR(aug->child, sub) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4294 | 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] | 4295 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 4296 | 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] | 4297 | strnodetype(aug->target->nodetype), strnodetype(sub->nodetype)); |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4298 | return -1; |
| 4299 | } |
| 4300 | } |
| 4301 | } else { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4302 | LOGVAL(LYE_INARG, LY_VLOG_LYS, aug, aug->target_name, "target-node"); |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4303 | 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] | 4304 | return -1; |
| 4305 | } |
| 4306 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 4307 | /* check identifier uniqueness as in lys_node_addchild() */ |
Michal Vasko | 1d87a92 | 2015-08-21 12:57:16 +0200 | [diff] [blame] | 4308 | LY_TREE_FOR(aug->child, sub) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4309 | if (lys_check_id(sub, aug->target, NULL)) { |
Michal Vasko | 3e6665f | 2015-08-17 14:00:38 +0200 | [diff] [blame] | 4310 | return -1; |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 4311 | } |
| 4312 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4313 | |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4314 | if (!aug->child) { |
| 4315 | /* empty augment, nothing to connect, but it is techincally applied */ |
| 4316 | LOGWRN("Augment \"%s\" without children.", aug->target_name); |
| 4317 | aug->flags &= ~LYS_NOTAPPLIED; |
| 4318 | } else if (mod->implemented && apply_aug(aug, unres)) { |
| 4319 | /* we tried to connect it, we failed */ |
| 4320 | return -1; |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4321 | } |
| 4322 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4323 | return EXIT_SUCCESS; |
| 4324 | } |
| 4325 | |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4326 | static int |
Radek Krejci | a7db970 | 2017-01-20 12:55:14 +0100 | [diff] [blame] | 4327 | 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] | 4328 | { |
| 4329 | enum LY_VLOG_ELEM vlog_type; |
| 4330 | void *vlog_node; |
| 4331 | unsigned int i, j; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4332 | struct lys_ext *e; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4333 | char *ext_name, *ext_prefix, *tmp; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4334 | struct lyxml_elem *next_yin, *yin; |
Radek Krejci | a7db970 | 2017-01-20 12:55:14 +0100 | [diff] [blame] | 4335 | const struct lys_module *mod; |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4336 | struct lys_ext_instance *tmp_ext; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4337 | LYEXT_TYPE etype; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4338 | |
| 4339 | switch (info->parent_type) { |
Radek Krejci | 0aa821a | 2016-12-08 11:21:35 +0100 | [diff] [blame] | 4340 | case LYEXT_PAR_NODE: |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4341 | vlog_node = info->parent; |
| 4342 | vlog_type = LY_VLOG_LYS; |
| 4343 | break; |
Radek Krejci | 0aa821a | 2016-12-08 11:21:35 +0100 | [diff] [blame] | 4344 | case LYEXT_PAR_MODULE: |
| 4345 | case LYEXT_PAR_IMPORT: |
| 4346 | case LYEXT_PAR_INCLUDE: |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4347 | vlog_node = NULL; |
| 4348 | vlog_type = LY_VLOG_LYS; |
| 4349 | break; |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 4350 | default: |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4351 | vlog_node = NULL; |
Radek Krejci | 6a7fedf | 2017-02-10 12:38:06 +0100 | [diff] [blame] | 4352 | vlog_type = LY_VLOG_NONE; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4353 | break; |
| 4354 | } |
| 4355 | |
| 4356 | if (info->datatype == LYS_IN_YIN) { |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4357 | /* YIN */ |
| 4358 | |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4359 | /* get the module where the extension is supposed to be defined */ |
Radek Krejci | a7db970 | 2017-01-20 12:55:14 +0100 | [diff] [blame] | 4360 | 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] | 4361 | if (!mod) { |
| 4362 | LOGVAL(LYE_INSTMT, vlog_type, vlog_node, info->data.yin->name); |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 4363 | return EXIT_FAILURE; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4364 | } |
| 4365 | |
| 4366 | /* find the extension definition */ |
| 4367 | e = NULL; |
| 4368 | for (i = 0; i < mod->extensions_size; i++) { |
| 4369 | if (ly_strequal(mod->extensions[i].name, info->data.yin->name, 1)) { |
| 4370 | e = &mod->extensions[i]; |
| 4371 | break; |
| 4372 | } |
| 4373 | } |
| 4374 | /* try submodules */ |
| 4375 | for (j = 0; !e && j < mod->inc_size; j++) { |
| 4376 | for (i = 0; i < mod->inc[j].submodule->extensions_size; i++) { |
| 4377 | if (ly_strequal(mod->inc[j].submodule->extensions[i].name, info->data.yin->name, 1)) { |
| 4378 | e = &mod->inc[j].submodule->extensions[i]; |
| 4379 | break; |
| 4380 | } |
| 4381 | } |
| 4382 | } |
| 4383 | if (!e) { |
| 4384 | LOGVAL(LYE_INSTMT, vlog_type, vlog_node, info->data.yin->name); |
| 4385 | return EXIT_FAILURE; |
| 4386 | } |
| 4387 | |
| 4388 | /* 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] | 4389 | |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4390 | if (e->plugin && e->plugin->check_position) { |
| 4391 | /* common part - we have plugin with position checking function, use it first */ |
| 4392 | if ((*e->plugin->check_position)(info->parent, info->parent_type, info->substmt)) { |
| 4393 | /* extension is not allowed here */ |
| 4394 | LOGVAL(LYE_INSTMT, vlog_type, vlog_node, e->name); |
| 4395 | return -1; |
| 4396 | } |
| 4397 | } |
| 4398 | |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4399 | /* extension type-specific part - allocation */ |
| 4400 | if (e->plugin) { |
| 4401 | etype = e->plugin->type; |
| 4402 | } else { |
| 4403 | /* default type */ |
| 4404 | etype = LYEXT_FLAG; |
| 4405 | } |
| 4406 | switch (etype) { |
| 4407 | case LYEXT_FLAG: |
| 4408 | (*ext) = calloc(1, sizeof(struct lys_ext_instance)); |
| 4409 | break; |
| 4410 | case LYEXT_COMPLEX: |
| 4411 | (*ext) = calloc(1, ((struct lyext_plugin_complex*)e->plugin)->instance_size); |
| 4412 | break; |
| 4413 | case LYEXT_ERR: |
| 4414 | /* we never should be here */ |
| 4415 | LOGINT; |
| 4416 | return -1; |
| 4417 | } |
| 4418 | |
| 4419 | /* common part for all extension types */ |
| 4420 | (*ext)->def = e; |
| 4421 | (*ext)->parent = info->parent; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4422 | (*ext)->parent_type = info->parent_type; |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 4423 | (*ext)->insubstmt = info->substmt; |
| 4424 | (*ext)->insubstmt_index = info->substmt_index; |
Radek Krejci | 8de8f61 | 2017-02-16 15:03:32 +0100 | [diff] [blame] | 4425 | (*ext)->ext_type = e->plugin ? e->plugin->type : LYEXT_FLAG; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4426 | |
| 4427 | if (!(e->flags & LYS_YINELEM) && e->argument) { |
| 4428 | (*ext)->arg_value = lyxml_get_attr(info->data.yin, e->argument, NULL); |
| 4429 | if (!(*ext)->arg_value) { |
| 4430 | LOGVAL(LYE_MISSARG, LY_VLOG_NONE, NULL, e->argument, info->data.yin->name); |
| 4431 | return -1; |
| 4432 | } |
| 4433 | (*ext)->arg_value = lydict_insert(mod->ctx, (*ext)->arg_value, 0); |
| 4434 | } |
| 4435 | |
Radek Krejci | 7f1d47e | 2017-04-12 15:29:02 +0200 | [diff] [blame] | 4436 | (*ext)->nodetype = LYS_EXT; |
| 4437 | (*ext)->module = info->mod; |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 4438 | |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4439 | /* extension type-specific part - parsing content */ |
| 4440 | switch (etype) { |
| 4441 | case LYEXT_FLAG: |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4442 | LY_TREE_FOR_SAFE(info->data.yin->child, next_yin, yin) { |
| 4443 | if (!yin->ns) { |
| 4444 | /* garbage */ |
| 4445 | lyxml_free(mod->ctx, yin); |
| 4446 | continue; |
| 4447 | } else if (!strcmp(yin->ns->value, LY_NSYIN)) { |
| 4448 | /* standard YANG statements are not expected here */ |
| 4449 | LOGVAL(LYE_INCHILDSTMT, vlog_type, vlog_node, yin->name, info->data.yin->name); |
| 4450 | return -1; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4451 | } else if (yin->ns == info->data.yin->ns && |
| 4452 | (e->flags & LYS_YINELEM) && ly_strequal(yin->name, e->argument, 1)) { |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4453 | /* we have the extension's argument */ |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4454 | if ((*ext)->arg_value) { |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4455 | 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] | 4456 | return -1; |
| 4457 | } |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4458 | (*ext)->arg_value = yin->content; |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4459 | yin->content = NULL; |
| 4460 | lyxml_free(mod->ctx, yin); |
| 4461 | } else { |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4462 | /* extension instance */ |
| 4463 | if (lyp_yin_parse_subnode_ext(info->mod, *ext, LYEXT_PAR_EXTINST, yin, |
| 4464 | LYEXT_SUBSTMT_SELF, 0, unres)) { |
| 4465 | return -1; |
| 4466 | } |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4467 | |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4468 | continue; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4469 | } |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4470 | } |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4471 | break; |
| 4472 | case LYEXT_COMPLEX: |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 4473 | ((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] | 4474 | if (lyp_yin_parse_complex_ext(info->mod, (struct lys_ext_instance_complex*)(*ext), info->data.yin, unres)) { |
| 4475 | /* TODO memory cleanup */ |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4476 | return -1; |
| 4477 | } |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4478 | break; |
| 4479 | default: |
| 4480 | break; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4481 | } |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4482 | |
| 4483 | /* TODO - lyext_check_result_clb, other than LYEXT_FLAG plugins */ |
| 4484 | |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4485 | } else { |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4486 | /* YANG */ |
| 4487 | |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4488 | ext_prefix = (char *)(*ext)->def; |
| 4489 | tmp = strchr(ext_prefix, ':'); |
| 4490 | if (!tmp) { |
| 4491 | LOGVAL(LYE_INSTMT, vlog_type, vlog_node, ext_prefix); |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4492 | goto error; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4493 | } |
| 4494 | ext_name = tmp + 1; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4495 | |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4496 | /* get the module where the extension is supposed to be defined */ |
| 4497 | mod = lys_get_import_module(info->mod, ext_prefix, tmp - ext_prefix, NULL, 0); |
| 4498 | if (!mod) { |
| 4499 | LOGVAL(LYE_INSTMT, vlog_type, vlog_node, ext_prefix); |
| 4500 | return EXIT_FAILURE; |
| 4501 | } |
| 4502 | |
| 4503 | /* find the extension definition */ |
| 4504 | e = NULL; |
| 4505 | for (i = 0; i < mod->extensions_size; i++) { |
| 4506 | if (ly_strequal(mod->extensions[i].name, ext_name, 0)) { |
| 4507 | e = &mod->extensions[i]; |
| 4508 | break; |
| 4509 | } |
| 4510 | } |
| 4511 | /* try submodules */ |
| 4512 | for (j = 0; !e && j < mod->inc_size; j++) { |
| 4513 | for (i = 0; i < mod->inc[j].submodule->extensions_size; i++) { |
| 4514 | if (ly_strequal(mod->inc[j].submodule->extensions[i].name, ext_name, 0)) { |
| 4515 | e = &mod->inc[j].submodule->extensions[i]; |
| 4516 | break; |
| 4517 | } |
| 4518 | } |
| 4519 | } |
| 4520 | if (!e) { |
| 4521 | LOGVAL(LYE_INSTMT, vlog_type, vlog_node, ext_prefix); |
| 4522 | return EXIT_FAILURE; |
| 4523 | } |
| 4524 | |
| 4525 | /* we have the extension definition, so now it cannot be forward referenced and error is always fatal */ |
| 4526 | |
| 4527 | if (e->plugin && e->plugin->check_position) { |
| 4528 | /* common part - we have plugin with position checking function, use it first */ |
| 4529 | if ((*e->plugin->check_position)(info->parent, info->parent_type, info->substmt)) { |
| 4530 | /* extension is not allowed here */ |
| 4531 | LOGVAL(LYE_INSTMT, vlog_type, vlog_node, e->name); |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4532 | goto error; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4533 | } |
| 4534 | } |
| 4535 | |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4536 | /* extension common part */ |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4537 | (*ext)->flags &= ~LYEXT_OPT_YANG; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4538 | (*ext)->def = e; |
| 4539 | (*ext)->parent = info->parent; |
Radek Krejci | 8de8f61 | 2017-02-16 15:03:32 +0100 | [diff] [blame] | 4540 | (*ext)->ext_type = e->plugin ? e->plugin->type : LYEXT_FLAG; |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4541 | |
PavolVican | b0d8410 | 2017-02-15 16:32:42 +0100 | [diff] [blame] | 4542 | if (e->argument && !(*ext)->arg_value) { |
| 4543 | LOGVAL(LYE_MISSARG, LY_VLOG_NONE, NULL, e->argument, ext_name); |
| 4544 | goto error; |
| 4545 | } |
| 4546 | |
Radek Krejci | 7f1d47e | 2017-04-12 15:29:02 +0200 | [diff] [blame] | 4547 | (*ext)->module = info->mod; |
| 4548 | (*ext)->nodetype = LYS_EXT; |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 4549 | |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4550 | /* extension type-specific part */ |
| 4551 | if (e->plugin) { |
| 4552 | etype = e->plugin->type; |
| 4553 | } else { |
| 4554 | /* default type */ |
| 4555 | etype = LYEXT_FLAG; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4556 | } |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4557 | switch (etype) { |
| 4558 | case LYEXT_FLAG: |
| 4559 | /* nothing change */ |
| 4560 | break; |
| 4561 | case LYEXT_COMPLEX: |
| 4562 | tmp_ext = realloc(*ext, ((struct lyext_plugin_complex*)e->plugin)->instance_size); |
| 4563 | if (!tmp_ext) { |
| 4564 | LOGMEM; |
| 4565 | goto error; |
| 4566 | } |
| 4567 | memset((char *)tmp_ext + sizeof **ext, 0, ((struct lyext_plugin_complex*)e->plugin)->instance_size - sizeof **ext); |
| 4568 | (*ext) = tmp_ext; |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4569 | ((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] | 4570 | if (info->data.yang) { |
| 4571 | *tmp = ':'; |
PavolVican | db0e817 | 2017-02-20 00:46:09 +0100 | [diff] [blame] | 4572 | if (yang_parse_ext_substatement(info->mod, unres, info->data.yang->ext_substmt, ext_prefix, |
| 4573 | (struct lys_ext_instance_complex*)(*ext))) { |
| 4574 | goto error; |
| 4575 | } |
| 4576 | if (yang_fill_extcomplex_module(info->mod->ctx, (struct lys_ext_instance_complex*)(*ext), ext_prefix, |
| 4577 | info->data.yang->ext_modules, info->mod->implemented)) { |
PavolVican | a1e291f | 2017-02-19 16:07:12 +0100 | [diff] [blame] | 4578 | goto error; |
| 4579 | } |
PavolVican | a387667 | 2017-02-21 15:49:51 +0100 | [diff] [blame] | 4580 | } |
| 4581 | if (lyp_mand_check_ext((struct lys_ext_instance_complex*)(*ext), ext_prefix)) { |
| 4582 | goto error; |
PavolVican | a1e291f | 2017-02-19 16:07:12 +0100 | [diff] [blame] | 4583 | } |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4584 | break; |
| 4585 | case LYEXT_ERR: |
| 4586 | /* we never should be here */ |
| 4587 | LOGINT; |
| 4588 | goto error; |
| 4589 | } |
| 4590 | |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4591 | if (yang_check_ext_instance(info->mod, &(*ext)->ext, (*ext)->ext_size, *ext, unres)) { |
| 4592 | goto error; |
| 4593 | } |
| 4594 | free(ext_prefix); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4595 | } |
| 4596 | |
| 4597 | return EXIT_SUCCESS; |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4598 | error: |
| 4599 | free(ext_prefix); |
| 4600 | return -1; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4601 | } |
| 4602 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4603 | /** |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4604 | * @brief Resolve (find) choice default case. Does not log. |
| 4605 | * |
| 4606 | * @param[in] choic Choice to use. |
| 4607 | * @param[in] dflt Name of the default case. |
| 4608 | * |
| 4609 | * @return Pointer to the default node or NULL. |
| 4610 | */ |
| 4611 | static struct lys_node * |
| 4612 | resolve_choice_dflt(struct lys_node_choice *choic, const char *dflt) |
| 4613 | { |
| 4614 | struct lys_node *child, *ret; |
| 4615 | |
| 4616 | LY_TREE_FOR(choic->child, child) { |
| 4617 | if (child->nodetype == LYS_USES) { |
| 4618 | ret = resolve_choice_dflt((struct lys_node_choice *)child, dflt); |
| 4619 | if (ret) { |
| 4620 | return ret; |
| 4621 | } |
| 4622 | } |
| 4623 | |
| 4624 | 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] | 4625 | | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CHOICE))) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4626 | return child; |
| 4627 | } |
| 4628 | } |
| 4629 | |
| 4630 | return NULL; |
| 4631 | } |
| 4632 | |
| 4633 | /** |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4634 | * @brief Resolve uses, apply augments, refines. Logs directly. |
| 4635 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4636 | * @param[in] uses Uses to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4637 | * @param[in,out] unres List of unresolved items. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4638 | * |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 4639 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4640 | */ |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 4641 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4642 | resolve_uses(struct lys_node_uses *uses, struct unres_schema *unres) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4643 | { |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4644 | struct ly_ctx *ctx = uses->module->ctx; /* shortcut */ |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4645 | struct lys_node *node = NULL, *next, *iter, **refine_nodes = NULL; |
Pavol Vican | 55abd33 | 2016-07-12 15:54:49 +0200 | [diff] [blame] | 4646 | struct lys_node *node_aux, *parent, *tmp; |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4647 | struct lys_node_leaflist *llist; |
| 4648 | struct lys_node_leaf *leaf; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 4649 | struct lys_refine *rfn; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4650 | struct lys_restr *must, **old_must; |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4651 | struct lys_iffeature *iff, **old_iff; |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4652 | int i, j, k, rc; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4653 | uint8_t size, *old_size; |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4654 | unsigned int usize, usize1, usize2; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4655 | |
Michal Vasko | 71e1aa8 | 2015-08-12 12:17:51 +0200 | [diff] [blame] | 4656 | assert(uses->grp); |
Radek Krejci | 6ff885d | 2017-01-03 14:06:22 +0100 | [diff] [blame] | 4657 | |
| 4658 | /* HACK just check that the grouping is resolved |
| 4659 | * - the higher byte in flags is always empty in grouping (no flags there apply to the groupings) |
| 4660 | * so we use it to count unresolved uses inside the grouping */ |
| 4661 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
| 4662 | assert(!((uint8_t*)&uses->grp->flags)[1]); |
| 4663 | #else |
| 4664 | assert(!((uint8_t*)&uses->grp->flags)[0]); |
| 4665 | #endif |
Michal Vasko | 71e1aa8 | 2015-08-12 12:17:51 +0200 | [diff] [blame] | 4666 | |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4667 | if (!uses->grp->child) { |
| 4668 | /* grouping without children, warning was already displayed */ |
| 4669 | return EXIT_SUCCESS; |
| 4670 | } |
| 4671 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4672 | /* copy the data nodes from grouping into the uses context */ |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 4673 | LY_TREE_FOR(uses->grp->child, node_aux) { |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 4674 | if (node_aux->nodetype & LYS_GROUPING) { |
| 4675 | /* do not instantiate groupings from groupings */ |
| 4676 | continue; |
| 4677 | } |
Radek Krejci | 6ff885d | 2017-01-03 14:06:22 +0100 | [diff] [blame] | 4678 | 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] | 4679 | if (!node) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4680 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, uses->grp->name, "uses"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 4681 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Copying data from grouping failed."); |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4682 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4683 | } |
Pavol Vican | 55abd33 | 2016-07-12 15:54:49 +0200 | [diff] [blame] | 4684 | /* test the name of siblings */ |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 4685 | 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] | 4686 | 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] | 4687 | goto fail; |
Pavol Vican | 55abd33 | 2016-07-12 15:54:49 +0200 | [diff] [blame] | 4688 | } |
| 4689 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4690 | } |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4691 | |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 4692 | /* we managed to copy the grouping, the rest must be possible to resolve */ |
| 4693 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4694 | if (uses->refine_size) { |
| 4695 | refine_nodes = malloc(uses->refine_size * sizeof *refine_nodes); |
| 4696 | if (!refine_nodes) { |
| 4697 | LOGMEM; |
| 4698 | goto fail; |
| 4699 | } |
| 4700 | } |
| 4701 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4702 | /* apply refines */ |
| 4703 | for (i = 0; i < uses->refine_size; i++) { |
| 4704 | rfn = &uses->refine[i]; |
Radek Krejci | e207741 | 2017-01-26 16:03:39 +0100 | [diff] [blame] | 4705 | rc = resolve_descendant_schema_nodeid(rfn->target_name, uses->child, |
| 4706 | LYS_NO_RPC_NOTIF_NODE | LYS_ACTION | LYS_NOTIF, |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 4707 | 0, (const struct lys_node **)&node); |
Michal Vasko | 9bb061b | 2016-02-12 11:00:19 +0100 | [diff] [blame] | 4708 | if (rc || !node) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4709 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine"); |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4710 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4711 | } |
| 4712 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4713 | if (rfn->target_type && !(node->nodetype & rfn->target_type)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4714 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 4715 | 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] | 4716 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4717 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4718 | refine_nodes[i] = node; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4719 | |
| 4720 | /* description on any nodetype */ |
| 4721 | if (rfn->dsc) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4722 | lydict_remove(ctx, node->dsc); |
| 4723 | node->dsc = lydict_insert(ctx, rfn->dsc, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4724 | } |
| 4725 | |
| 4726 | /* reference on any nodetype */ |
| 4727 | if (rfn->ref) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4728 | lydict_remove(ctx, node->ref); |
| 4729 | node->ref = lydict_insert(ctx, rfn->ref, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4730 | } |
| 4731 | |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4732 | /* config on any nodetype, |
| 4733 | * in case of notification or rpc/action, the config is not applicable (there is no config status) */ |
| 4734 | if ((rfn->flags & LYS_CONFIG_MASK) && (node->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4735 | node->flags &= ~LYS_CONFIG_MASK; |
| 4736 | node->flags |= (rfn->flags & LYS_CONFIG_MASK); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4737 | } |
| 4738 | |
| 4739 | /* default value ... */ |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4740 | if (rfn->dflt_size) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4741 | if (node->nodetype == LYS_LEAF) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4742 | /* leaf */ |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4743 | leaf = (struct lys_node_leaf *)node; |
| 4744 | |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 4745 | /* replace default value */ |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4746 | lydict_remove(ctx, leaf->dflt); |
| 4747 | leaf->dflt = lydict_insert(ctx, rfn->dflt[0], 0); |
| 4748 | |
| 4749 | /* check the default value */ |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 4750 | if (unres_schema_add_node(leaf->module, unres, &leaf->type, UNRES_TYPE_DFLT, |
| 4751 | (struct lys_node *)(&leaf->dflt)) == -1) { |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4752 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4753 | } |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4754 | } else if (node->nodetype == LYS_LEAFLIST) { |
| 4755 | /* leaf-list */ |
| 4756 | llist = (struct lys_node_leaflist *)node; |
| 4757 | |
| 4758 | /* remove complete set of defaults in target */ |
Radek Krejci | 542ab14 | 2017-01-23 15:57:08 +0100 | [diff] [blame] | 4759 | for (j = 0; j < llist->dflt_size; j++) { |
| 4760 | lydict_remove(ctx, llist->dflt[j]); |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4761 | } |
| 4762 | free(llist->dflt); |
| 4763 | |
| 4764 | /* copy the default set from refine */ |
| 4765 | llist->dflt_size = rfn->dflt_size; |
| 4766 | llist->dflt = malloc(llist->dflt_size * sizeof *llist->dflt); |
Radek Krejci | 542ab14 | 2017-01-23 15:57:08 +0100 | [diff] [blame] | 4767 | for (j = 0; j < llist->dflt_size; j++) { |
| 4768 | llist->dflt[j] = lydict_insert(ctx, rfn->dflt[j], 0); |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4769 | } |
| 4770 | |
| 4771 | /* check default value */ |
Radek Krejci | 542ab14 | 2017-01-23 15:57:08 +0100 | [diff] [blame] | 4772 | for (j = 0; j < llist->dflt_size; j++) { |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 4773 | 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] | 4774 | (struct lys_node *)(&llist->dflt[j])) == -1) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4775 | goto fail; |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4776 | } |
| 4777 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4778 | } |
| 4779 | } |
| 4780 | |
| 4781 | /* mandatory on leaf, anyxml or choice */ |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 4782 | if (rfn->flags & LYS_MAND_MASK) { |
Radek Krejci | bf28583 | 2017-01-26 16:05:41 +0100 | [diff] [blame] | 4783 | /* remove current value */ |
| 4784 | node->flags &= ~LYS_MAND_MASK; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4785 | |
Radek Krejci | bf28583 | 2017-01-26 16:05:41 +0100 | [diff] [blame] | 4786 | /* set new value */ |
| 4787 | node->flags |= (rfn->flags & LYS_MAND_MASK); |
| 4788 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4789 | if (rfn->flags & LYS_MAND_TRUE) { |
| 4790 | /* check if node has default value */ |
| 4791 | if ((node->nodetype & LYS_LEAF) && ((struct lys_node_leaf *)node)->dflt) { |
Radek Krejci | bdcaf24 | 2017-04-19 10:29:47 +0200 | [diff] [blame] | 4792 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, |
| 4793 | "The \"mandatory\" statement is forbidden on leaf with \"default\"."); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4794 | goto fail; |
| 4795 | } |
| 4796 | if ((node->nodetype & LYS_CHOICE) && ((struct lys_node_choice *)node)->dflt) { |
Radek Krejci | bdcaf24 | 2017-04-19 10:29:47 +0200 | [diff] [blame] | 4797 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, |
| 4798 | "The \"mandatory\" statement is forbidden on choices with \"default\"."); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4799 | goto fail; |
| 4800 | } |
| 4801 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4802 | } |
| 4803 | |
| 4804 | /* presence on container */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4805 | if ((node->nodetype & LYS_CONTAINER) && rfn->mod.presence) { |
| 4806 | lydict_remove(ctx, ((struct lys_node_container *)node)->presence); |
| 4807 | ((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] | 4808 | } |
| 4809 | |
| 4810 | /* min/max-elements on list or leaf-list */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4811 | if (node->nodetype == LYS_LIST) { |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 4812 | if (rfn->flags & LYS_RFN_MINSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4813 | ((struct lys_node_list *)node)->min = rfn->mod.list.min; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4814 | } |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 4815 | if (rfn->flags & LYS_RFN_MAXSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4816 | ((struct lys_node_list *)node)->max = rfn->mod.list.max; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4817 | } |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4818 | } else if (node->nodetype == LYS_LEAFLIST) { |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 4819 | if (rfn->flags & LYS_RFN_MINSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4820 | ((struct lys_node_leaflist *)node)->min = rfn->mod.list.min; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4821 | } |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 4822 | if (rfn->flags & LYS_RFN_MAXSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4823 | ((struct lys_node_leaflist *)node)->max = rfn->mod.list.max; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4824 | } |
| 4825 | } |
| 4826 | |
| 4827 | /* must in leaf, leaf-list, list, container or anyxml */ |
| 4828 | if (rfn->must_size) { |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4829 | switch (node->nodetype) { |
| 4830 | case LYS_LEAF: |
| 4831 | old_size = &((struct lys_node_leaf *)node)->must_size; |
| 4832 | old_must = &((struct lys_node_leaf *)node)->must; |
| 4833 | break; |
| 4834 | case LYS_LEAFLIST: |
| 4835 | old_size = &((struct lys_node_leaflist *)node)->must_size; |
| 4836 | old_must = &((struct lys_node_leaflist *)node)->must; |
| 4837 | break; |
| 4838 | case LYS_LIST: |
| 4839 | old_size = &((struct lys_node_list *)node)->must_size; |
| 4840 | old_must = &((struct lys_node_list *)node)->must; |
| 4841 | break; |
| 4842 | case LYS_CONTAINER: |
| 4843 | old_size = &((struct lys_node_container *)node)->must_size; |
| 4844 | old_must = &((struct lys_node_container *)node)->must; |
| 4845 | break; |
| 4846 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4847 | case LYS_ANYDATA: |
| 4848 | old_size = &((struct lys_node_anydata *)node)->must_size; |
| 4849 | old_must = &((struct lys_node_anydata *)node)->must; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4850 | break; |
| 4851 | default: |
| 4852 | LOGINT; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4853 | goto fail; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4854 | } |
| 4855 | |
| 4856 | size = *old_size + rfn->must_size; |
| 4857 | must = realloc(*old_must, size * sizeof *rfn->must); |
| 4858 | if (!must) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4859 | LOGMEM; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4860 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4861 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4862 | for (k = 0, j = *old_size; k < rfn->must_size; k++, j++) { |
Radek Krejci | 7f0164a | 2017-01-25 17:04:06 +0100 | [diff] [blame] | 4863 | must[j].ext_size = rfn->must[k].ext_size; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4864 | 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] | 4865 | &must[j].ext, 0, unres); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4866 | must[j].expr = lydict_insert(ctx, rfn->must[k].expr, 0); |
| 4867 | must[j].dsc = lydict_insert(ctx, rfn->must[k].dsc, 0); |
| 4868 | must[j].ref = lydict_insert(ctx, rfn->must[k].ref, 0); |
| 4869 | must[j].eapptag = lydict_insert(ctx, rfn->must[k].eapptag, 0); |
| 4870 | must[j].emsg = lydict_insert(ctx, rfn->must[k].emsg, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4871 | } |
| 4872 | |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4873 | *old_must = must; |
| 4874 | *old_size = size; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 4875 | |
| 4876 | /* check XPath dependencies again */ |
| 4877 | if (unres_schema_add_node(node->module, unres, node, UNRES_XPATH, NULL) == -1) { |
| 4878 | goto fail; |
| 4879 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4880 | } |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4881 | |
| 4882 | /* if-feature in leaf, leaf-list, list, container or anyxml */ |
| 4883 | if (rfn->iffeature_size) { |
| 4884 | old_size = &node->iffeature_size; |
| 4885 | old_iff = &node->iffeature; |
| 4886 | |
| 4887 | size = *old_size + rfn->iffeature_size; |
| 4888 | iff = realloc(*old_iff, size * sizeof *rfn->iffeature); |
| 4889 | if (!iff) { |
| 4890 | LOGMEM; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4891 | goto fail; |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4892 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4893 | for (k = 0, j = *old_size; k < rfn->iffeature_size; k++, j++) { |
| 4894 | resolve_iffeature_getsizes(&rfn->iffeature[k], &usize1, &usize2); |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4895 | if (usize1) { |
| 4896 | /* there is something to duplicate */ |
| 4897 | /* duplicate compiled expression */ |
| 4898 | usize = (usize1 / 4) + (usize1 % 4) ? 1 : 0; |
| 4899 | iff[j].expr = malloc(usize * sizeof *iff[j].expr); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4900 | 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] | 4901 | |
| 4902 | /* duplicate list of feature pointers */ |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4903 | iff[j].features = malloc(usize2 * sizeof *iff[k].features); |
| 4904 | 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] | 4905 | } |
| 4906 | } |
| 4907 | |
| 4908 | *old_iff = iff; |
| 4909 | *old_size = size; |
| 4910 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4911 | } |
| 4912 | |
| 4913 | /* apply augments */ |
| 4914 | for (i = 0; i < uses->augment_size; i++) { |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 4915 | rc = resolve_augment(&uses->augment[i], uses->child, unres); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4916 | if (rc) { |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4917 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4918 | } |
| 4919 | } |
| 4920 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4921 | /* check refines */ |
| 4922 | for (i = 0; i < uses->refine_size; i++) { |
| 4923 | node = refine_nodes[i]; |
| 4924 | rfn = &uses->refine[i]; |
| 4925 | |
| 4926 | /* config on any nodetype */ |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4927 | if ((rfn->flags & LYS_CONFIG_MASK) && (node->flags & LYS_CONFIG_MASK)) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4928 | 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] | 4929 | if (parent && parent->nodetype != LYS_GROUPING && (parent->flags & LYS_CONFIG_MASK) && |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4930 | ((parent->flags & LYS_CONFIG_MASK) != (rfn->flags & LYS_CONFIG_MASK)) && |
| 4931 | (rfn->flags & LYS_CONFIG_W)) { |
| 4932 | /* setting config true under config false is prohibited */ |
| 4933 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, "config", "refine"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 4934 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4935 | "changing config from 'false' to 'true' is prohibited while " |
| 4936 | "the target's parent is still config 'false'."); |
| 4937 | goto fail; |
| 4938 | } |
| 4939 | |
| 4940 | /* inherit config change to the target children */ |
| 4941 | LY_TREE_DFS_BEGIN(node->child, next, iter) { |
| 4942 | if (rfn->flags & LYS_CONFIG_W) { |
| 4943 | if (iter->flags & LYS_CONFIG_SET) { |
| 4944 | /* config is set explicitely, go to next sibling */ |
| 4945 | next = NULL; |
| 4946 | goto nextsibling; |
| 4947 | } |
| 4948 | } else { /* LYS_CONFIG_R */ |
| 4949 | if ((iter->flags & LYS_CONFIG_SET) && (iter->flags & LYS_CONFIG_W)) { |
| 4950 | /* error - we would have config data under status data */ |
| 4951 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, "config", "refine"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 4952 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4953 | "changing config from 'true' to 'false' is prohibited while the target " |
| 4954 | "has still a children with explicit config 'true'."); |
| 4955 | goto fail; |
| 4956 | } |
| 4957 | } |
| 4958 | /* change config */ |
| 4959 | iter->flags &= ~LYS_CONFIG_MASK; |
| 4960 | iter->flags |= (rfn->flags & LYS_CONFIG_MASK); |
| 4961 | |
| 4962 | /* select next iter - modified LY_TREE_DFS_END */ |
| 4963 | if (iter->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
| 4964 | next = NULL; |
| 4965 | } else { |
| 4966 | next = iter->child; |
| 4967 | } |
| 4968 | nextsibling: |
| 4969 | if (!next) { |
| 4970 | /* try siblings */ |
| 4971 | next = iter->next; |
| 4972 | } |
| 4973 | while (!next) { |
| 4974 | /* parent is already processed, go to its sibling */ |
| 4975 | iter = lys_parent(iter); |
| 4976 | |
| 4977 | /* no siblings, go back through parents */ |
| 4978 | if (iter == node) { |
| 4979 | /* we are done, no next element to process */ |
| 4980 | break; |
| 4981 | } |
| 4982 | next = iter->next; |
| 4983 | } |
| 4984 | } |
| 4985 | } |
| 4986 | |
| 4987 | /* default value */ |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 4988 | if (rfn->dflt_size) { |
| 4989 | if (node->nodetype == LYS_CHOICE) { |
| 4990 | /* choice */ |
| 4991 | ((struct lys_node_choice *)node)->dflt = resolve_choice_dflt((struct lys_node_choice *)node, |
| 4992 | rfn->dflt[0]); |
| 4993 | if (!((struct lys_node_choice *)node)->dflt) { |
| 4994 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->dflt[0], "default"); |
| 4995 | goto fail; |
| 4996 | } |
| 4997 | if (lyp_check_mandatory_choice(node)) { |
| 4998 | goto fail; |
| 4999 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5000 | } |
| 5001 | } |
| 5002 | |
| 5003 | /* min/max-elements on list or leaf-list */ |
Radek Krejci | 2d3c811 | 2017-04-19 10:20:50 +0200 | [diff] [blame] | 5004 | if (node->nodetype == LYS_LIST && ((struct lys_node_list *)node)->max) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5005 | 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] | 5006 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, "Invalid value \"%d\" of \"%s\".", rfn->mod.list.min, "min-elements"); |
| 5007 | 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] | 5008 | goto fail; |
| 5009 | } |
Radek Krejci | 2d3c811 | 2017-04-19 10:20:50 +0200 | [diff] [blame] | 5010 | } else if (node->nodetype == LYS_LEAFLIST && ((struct lys_node_leaflist *)node)->max) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5011 | 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] | 5012 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, "Invalid value \"%d\" of \"%s\".", rfn->mod.list.min, "min-elements"); |
| 5013 | 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] | 5014 | goto fail; |
| 5015 | } |
| 5016 | } |
| 5017 | |
| 5018 | /* additional checks */ |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 5019 | /* default value with mandatory/min-elements */ |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5020 | if (node->nodetype == LYS_LEAFLIST) { |
| 5021 | llist = (struct lys_node_leaflist *)node; |
| 5022 | if (llist->dflt_size && llist->min) { |
Radek Krejci | bdcaf24 | 2017-04-19 10:29:47 +0200 | [diff] [blame] | 5023 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, uses, rfn->dflt_size ? "default" : "min-elements", "refine"); |
| 5024 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5025 | "The \"min-elements\" statement with non-zero value is forbidden on leaf-lists with the \"default\" statement."); |
| 5026 | goto fail; |
| 5027 | } |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 5028 | } else if (node->nodetype == LYS_LEAF) { |
| 5029 | leaf = (struct lys_node_leaf *)node; |
| 5030 | if (leaf->dflt && (leaf->flags & LYS_MAND_TRUE)) { |
Radek Krejci | bdcaf24 | 2017-04-19 10:29:47 +0200 | [diff] [blame] | 5031 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, uses, rfn->dflt_size ? "default" : "mandatory", "refine"); |
| 5032 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 5033 | "The \"mandatory\" statement is forbidden on leafs with the \"default\" statement."); |
| 5034 | goto fail; |
| 5035 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5036 | } |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 5037 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5038 | /* 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] | 5039 | if ((rfn->flags & LYS_MAND_TRUE) || rfn->mod.list.min) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5040 | for (parent = node->parent; |
| 5041 | parent && !(parent->nodetype & (LYS_CHOICE | LYS_GROUPING | LYS_ACTION | LYS_USES)); |
| 5042 | parent = parent->parent) { |
| 5043 | if (parent->nodetype == LYS_CONTAINER && ((struct lys_node_container *)parent)->presence) { |
| 5044 | /* stop also on presence containers */ |
| 5045 | break; |
| 5046 | } |
| 5047 | } |
| 5048 | /* and if it is a choice with the default case, check it for presence of a mandatory node in it */ |
| 5049 | if (parent && parent->nodetype == LYS_CHOICE && ((struct lys_node_choice *)parent)->dflt) { |
| 5050 | if (lyp_check_mandatory_choice(parent)) { |
| 5051 | goto fail; |
| 5052 | } |
| 5053 | } |
| 5054 | } |
| 5055 | } |
| 5056 | free(refine_nodes); |
| 5057 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5058 | return EXIT_SUCCESS; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 5059 | |
| 5060 | fail: |
| 5061 | LY_TREE_FOR_SAFE(uses->child, next, iter) { |
| 5062 | lys_node_free(iter, NULL, 0); |
| 5063 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5064 | free(refine_nodes); |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 5065 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5066 | } |
| 5067 | |
Radek Krejci | 83a4bac | 2017-02-07 15:53:04 +0100 | [diff] [blame] | 5068 | void |
| 5069 | resolve_identity_backlink_update(struct lys_ident *der, struct lys_ident *base) |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5070 | { |
| 5071 | int i; |
| 5072 | |
| 5073 | assert(der && base); |
| 5074 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5075 | if (!base->der) { |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5076 | /* create a set for backlinks if it does not exist */ |
| 5077 | base->der = ly_set_new(); |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5078 | } |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5079 | /* store backlink */ |
| 5080 | ly_set_add(base->der, der, LY_SET_OPT_USEASLIST); |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5081 | |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5082 | /* do it recursively */ |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5083 | for (i = 0; i < base->base_size; i++) { |
Radek Krejci | 83a4bac | 2017-02-07 15:53:04 +0100 | [diff] [blame] | 5084 | resolve_identity_backlink_update(der, base->base[i]); |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5085 | } |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5086 | } |
| 5087 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5088 | /** |
| 5089 | * @brief Resolve base identity recursively. Does not log. |
| 5090 | * |
| 5091 | * @param[in] module Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5092 | * @param[in] ident Identity to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5093 | * @param[in] basename Base name of the identity. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5094 | * @param[out] ret Pointer to the resolved identity. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5095 | * |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5096 | * @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] | 5097 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5098 | static int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 5099 | 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] | 5100 | struct unres_schema *unres, struct lys_ident **ret) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5101 | { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5102 | uint32_t i, j; |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5103 | struct lys_ident *base = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5104 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5105 | assert(ret); |
| 5106 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5107 | /* search module */ |
| 5108 | for (i = 0; i < module->ident_size; i++) { |
| 5109 | if (!strcmp(basename, module->ident[i].name)) { |
| 5110 | |
| 5111 | if (!ident) { |
| 5112 | /* just search for type, so do not modify anything, just return |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5113 | * the base identity pointer */ |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5114 | *ret = &module->ident[i]; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5115 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5116 | } |
| 5117 | |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5118 | base = &module->ident[i]; |
| 5119 | goto matchfound; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5120 | } |
| 5121 | } |
| 5122 | |
| 5123 | /* search submodules */ |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5124 | for (j = 0; j < module->inc_size && module->inc[j].submodule; j++) { |
| 5125 | for (i = 0; i < module->inc[j].submodule->ident_size; i++) { |
| 5126 | if (!strcmp(basename, module->inc[j].submodule->ident[i].name)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5127 | |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5128 | if (!ident) { |
| 5129 | *ret = &module->inc[j].submodule->ident[i]; |
| 5130 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5131 | } |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5132 | |
| 5133 | base = &module->inc[j].submodule->ident[i]; |
| 5134 | goto matchfound; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5135 | } |
| 5136 | } |
| 5137 | } |
| 5138 | |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5139 | matchfound: |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5140 | /* we found it somewhere */ |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 5141 | if (base) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5142 | /* is it already completely resolved? */ |
| 5143 | for (i = 0; i < unres->count; i++) { |
Radek Krejci | ba7b1cc | 2016-08-08 13:44:43 +0200 | [diff] [blame] | 5144 | if ((unres->item[i] == base) && (unres->type[i] == UNRES_IDENT)) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5145 | /* identity found, but not yet resolved, so do not return it in *res and try it again later */ |
| 5146 | |
| 5147 | /* simple check for circular reference, |
| 5148 | * the complete check is done as a side effect of using only completely |
| 5149 | * resolved identities (previous check of unres content) */ |
| 5150 | if (ly_strequal((const char *)unres->str_snode[i], ident->name, 1)) { |
| 5151 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, basename, "base"); |
| 5152 | 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] | 5153 | return -1; |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5154 | } |
| 5155 | |
Radek Krejci | 06f64ed | 2016-08-15 11:07:44 +0200 | [diff] [blame] | 5156 | return EXIT_FAILURE; |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 5157 | } |
| 5158 | } |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5159 | |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 5160 | /* checks done, store the result */ |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5161 | *ret = base; |
Pavol Vican | fdab9f9 | 2016-09-07 15:23:27 +0200 | [diff] [blame] | 5162 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5163 | } |
| 5164 | |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5165 | /* base not found (maybe a forward reference) */ |
| 5166 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5167 | } |
| 5168 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5169 | /** |
| 5170 | * @brief Resolve base identity. Logs directly. |
| 5171 | * |
| 5172 | * @param[in] module Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5173 | * @param[in] ident Identity to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5174 | * @param[in] basename Base name of the identity. |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 5175 | * @param[in] parent Either "type" or "identity". |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5176 | * @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] | 5177 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5178 | * @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] | 5179 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5180 | static int |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5181 | 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] | 5182 | struct lys_type *type, struct unres_schema *unres) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5183 | { |
| 5184 | const char *name; |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5185 | int mod_name_len = 0, rc; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5186 | struct lys_ident *target, **ret; |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 5187 | uint16_t flags; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5188 | struct lys_module *mod; |
| 5189 | |
| 5190 | assert((ident && !type) || (!ident && type)); |
| 5191 | |
| 5192 | if (!type) { |
| 5193 | /* have ident to resolve */ |
| 5194 | ret = ⌖ |
| 5195 | flags = ident->flags; |
| 5196 | mod = ident->module; |
| 5197 | } else { |
| 5198 | /* have type to fill */ |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5199 | ++type->info.ident.count; |
| 5200 | type->info.ident.ref = ly_realloc(type->info.ident.ref, type->info.ident.count * sizeof *type->info.ident.ref); |
| 5201 | if (!type->info.ident.ref) { |
| 5202 | LOGMEM; |
| 5203 | return -1; |
| 5204 | } |
| 5205 | |
| 5206 | ret = &type->info.ident.ref[type->info.ident.count - 1]; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5207 | flags = type->parent->flags; |
| 5208 | mod = type->parent->module; |
| 5209 | } |
Michal Vasko | f200600 | 2016-04-21 16:28:15 +0200 | [diff] [blame] | 5210 | *ret = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5211 | |
| 5212 | /* search for the base identity */ |
| 5213 | name = strchr(basename, ':'); |
| 5214 | if (name) { |
| 5215 | /* set name to correct position after colon */ |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 5216 | mod_name_len = name - basename; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5217 | name++; |
| 5218 | |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 5219 | 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] | 5220 | /* prefix refers to the current module, ignore it */ |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 5221 | mod_name_len = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5222 | } |
| 5223 | } else { |
| 5224 | name = basename; |
| 5225 | } |
| 5226 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 5227 | /* get module where to search */ |
| 5228 | module = lys_get_import_module(module, NULL, 0, mod_name_len ? basename : NULL, mod_name_len); |
| 5229 | if (!module) { |
| 5230 | /* identity refers unknown data model */ |
Radek Krejci | 02a0499 | 2016-03-17 16:06:37 +0100 | [diff] [blame] | 5231 | LOGVAL(LYE_INMOD, LY_VLOG_NONE, NULL, basename); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 5232 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5233 | } |
| 5234 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 5235 | /* search in the identified module ... */ |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5236 | rc = resolve_base_ident_sub(module, ident, name, unres, ret); |
| 5237 | if (!rc) { |
| 5238 | assert(*ret); |
| 5239 | |
| 5240 | /* check status */ |
| 5241 | if (lyp_check_status(flags, mod, ident ? ident->name : "of type", |
| 5242 | (*ret)->flags, (*ret)->module, (*ret)->name, NULL)) { |
| 5243 | rc = -1; |
Radek Krejci | 83a4bac | 2017-02-07 15:53:04 +0100 | [diff] [blame] | 5244 | } else if (ident) { |
| 5245 | ident->base[ident->base_size++] = *ret; |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5246 | if (lys_main_module(mod)->implemented) { |
| 5247 | /* in case of the implemented identity, maintain backlinks to it |
| 5248 | * from the base identities to make it available when resolving |
| 5249 | * data with the identity values (not implemented identity is not |
| 5250 | * allowed as an identityref value). */ |
| 5251 | resolve_identity_backlink_update(ident, *ret); |
| 5252 | } |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5253 | } |
| 5254 | } else if (rc == EXIT_FAILURE) { |
| 5255 | LOGVAL(LYE_INRESOLV, LY_VLOG_NONE, NULL, parent, basename); |
Pavol Vican | 053a288 | 2016-09-05 14:40:33 +0200 | [diff] [blame] | 5256 | if (type) { |
| 5257 | --type->info.ident.count; |
| 5258 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5259 | } |
| 5260 | |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5261 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5262 | } |
| 5263 | |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5264 | /* |
| 5265 | * 1 - true (der is derived from base) |
| 5266 | * 0 - false (der is not derived from base) |
| 5267 | */ |
| 5268 | static int |
| 5269 | search_base_identity(struct lys_ident *der, struct lys_ident *base) |
| 5270 | { |
| 5271 | int i; |
| 5272 | |
| 5273 | if (der == base) { |
| 5274 | return 1; |
| 5275 | } else { |
| 5276 | for(i = 0; i < der->base_size; i++) { |
| 5277 | if (search_base_identity(der->base[i], base) == 1) { |
| 5278 | return 1; |
| 5279 | } |
| 5280 | } |
| 5281 | } |
| 5282 | |
| 5283 | return 0; |
| 5284 | } |
| 5285 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5286 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 5287 | * @brief Resolve JSON data format identityref. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5288 | * |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5289 | * @param[in] type Identityref type. |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 5290 | * @param[in] ident_name Identityref name. |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 5291 | * @param[in] node Node where the identityref is being resolved |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5292 | * @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] | 5293 | * |
| 5294 | * @return Pointer to the identity resolvent, NULL on error. |
| 5295 | */ |
Radek Krejci | a52656e | 2015-08-05 13:41:50 +0200 | [diff] [blame] | 5296 | struct lys_ident * |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5297 | 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] | 5298 | { |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5299 | const char *mod_name, *name; |
| 5300 | int mod_name_len, rc, i, j; |
| 5301 | int make_implemented = 0; |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5302 | unsigned int u; |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5303 | struct lys_ident *der, *cur; |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5304 | struct lys_module *imod = NULL, *m; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5305 | |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5306 | assert(type && ident_name && node && mod); |
Radek Krejci | bb1ce0f | 2016-12-05 13:24:33 +0100 | [diff] [blame] | 5307 | |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5308 | if (!type || (!type->info.ident.count && !type->der) || !ident_name) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5309 | return NULL; |
| 5310 | } |
| 5311 | |
Michal Vasko | c633ca0 | 2015-08-21 14:03:51 +0200 | [diff] [blame] | 5312 | rc = parse_node_identifier(ident_name, &mod_name, &mod_name_len, &name, NULL); |
Michal Vasko | b43bb3c | 2016-03-17 15:00:27 +0100 | [diff] [blame] | 5313 | if (rc < 1) { |
Radek Krejci | bb1ce0f | 2016-12-05 13:24:33 +0100 | [diff] [blame] | 5314 | 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] | 5315 | return NULL; |
Michal Vasko | b43bb3c | 2016-03-17 15:00:27 +0100 | [diff] [blame] | 5316 | } else if (rc < (signed)strlen(ident_name)) { |
Radek Krejci | bb1ce0f | 2016-12-05 13:24:33 +0100 | [diff] [blame] | 5317 | 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] | 5318 | return NULL; |
| 5319 | } |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5320 | |
| 5321 | m = lys_main_module(mod); /* shortcut */ |
| 5322 | if (!mod_name || (!strncmp(mod_name, m->name, mod_name_len) && !m->name[mod_name_len])) { |
| 5323 | /* identity is defined in the same module as node */ |
| 5324 | imod = m; |
| 5325 | } else if (dflt) { |
| 5326 | /* solving identityref in default definition in schema - |
| 5327 | * find the identity's module in the imported modules list to have a correct revision */ |
| 5328 | for (i = 0; i < mod->imp_size; i++) { |
| 5329 | if (!strncmp(mod_name, mod->imp[i].module->name, mod_name_len) && !mod->imp[i].module->name[mod_name_len]) { |
| 5330 | imod = mod->imp[i].module; |
| 5331 | break; |
| 5332 | } |
| 5333 | } |
| 5334 | } else { |
| 5335 | /* solving identityref in data - get the (implemented) module from the context */ |
| 5336 | u = 0; |
| 5337 | while ((imod = (struct lys_module*)ly_ctx_get_module_iter(mod->ctx, &u))) { |
| 5338 | if (imod->implemented && !strncmp(mod_name, imod->name, mod_name_len) && !imod->name[mod_name_len]) { |
| 5339 | break; |
| 5340 | } |
| 5341 | } |
| 5342 | } |
| 5343 | if (!imod) { |
| 5344 | goto fail; |
| 5345 | } |
| 5346 | |
| 5347 | if (dflt && (m != imod || lys_main_module(type->parent->module) != mod)) { |
| 5348 | /* we are solving default statement in schema AND the type is not referencing the same schema, |
| 5349 | * THEN, we may need to make the module with the identity implemented, but only if it really |
| 5350 | * contains the identity */ |
| 5351 | if (!imod->implemented) { |
| 5352 | cur = NULL; |
| 5353 | /* get the identity in the module */ |
| 5354 | for (i = 0; i < imod->ident_size; i++) { |
| 5355 | if (!strcmp(name, imod->ident[i].name)) { |
| 5356 | cur = &imod->ident[i]; |
| 5357 | break; |
| 5358 | } |
| 5359 | } |
| 5360 | if (!cur) { |
| 5361 | /* go through includes */ |
| 5362 | for (j = 0; j < imod->inc_size; j++) { |
| 5363 | for (i = 0; i < imod->inc[j].submodule->ident_size; i++) { |
| 5364 | if (!strcmp(name, imod->inc[j].submodule->ident[i].name)) { |
| 5365 | cur = &imod->inc[j].submodule->ident[i]; |
| 5366 | break; |
| 5367 | } |
| 5368 | } |
| 5369 | } |
| 5370 | if (!cur) { |
| 5371 | goto fail; |
| 5372 | } |
| 5373 | } |
| 5374 | |
| 5375 | /* check that identity is derived from one of the type's base */ |
| 5376 | while (type->der) { |
| 5377 | for (i = 0; i < type->info.ident.count; i++) { |
| 5378 | if (search_base_identity(cur, type->info.ident.ref[i])) { |
| 5379 | /* cur's base matches the type's base */ |
| 5380 | make_implemented = 1; |
| 5381 | goto match; |
| 5382 | } |
| 5383 | } |
| 5384 | type = &type->der->type; |
| 5385 | } |
| 5386 | /* matching base not found */ |
| 5387 | LOGVAL(LYE_SPEC, LY_VLOG_LYD, node, "Identity used as identityref value is not implemented."); |
| 5388 | goto fail; |
| 5389 | } |
Radek Krejci | f32c5f6 | 2016-12-05 09:27:38 +0100 | [diff] [blame] | 5390 | } |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 5391 | |
Radek Krejci | 98a1e2d | 2017-04-26 14:34:52 +0200 | [diff] [blame] | 5392 | /* go through all the derived types of all the bases */ |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5393 | while (type->der) { |
| 5394 | for (i = 0; i < type->info.ident.count; ++i) { |
| 5395 | cur = type->info.ident.ref[i]; |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 5396 | |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5397 | if (cur->der) { |
Radek Krejci | 98a1e2d | 2017-04-26 14:34:52 +0200 | [diff] [blame] | 5398 | /* there are some derived identities */ |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5399 | for (u = 0; u < cur->der->number; u++) { |
| 5400 | der = (struct lys_ident *)cur->der->set.g[u]; /* shortcut */ |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5401 | if (!strcmp(der->name, name) && lys_main_module(der->module) == imod) { |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5402 | /* we have match */ |
| 5403 | cur = der; |
| 5404 | goto match; |
| 5405 | } |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5406 | } |
| 5407 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5408 | } |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5409 | type = &type->der->type; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5410 | } |
| 5411 | |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5412 | fail: |
Radek Krejci | bb1ce0f | 2016-12-05 13:24:33 +0100 | [diff] [blame] | 5413 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYD, node, "identityref", ident_name); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5414 | return NULL; |
Radek Krejci | f1ee2e2 | 2016-08-02 16:36:48 +0200 | [diff] [blame] | 5415 | |
| 5416 | match: |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5417 | for (i = 0; i < cur->iffeature_size; i++) { |
| 5418 | if (!resolve_iffeature(&cur->iffeature[i])) { |
Radek Krejci | bb1ce0f | 2016-12-05 13:24:33 +0100 | [diff] [blame] | 5419 | LOGVAL(LYE_INVAL, LY_VLOG_LYD, node, cur->name, node->schema->name); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 5420 | 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] | 5421 | return NULL; |
| 5422 | } |
| 5423 | } |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5424 | if (make_implemented) { |
| 5425 | LOGVRB("Making \"%s\" module implemented because of identityref default value \"%s\" used in the implemented \"%s\" module", |
| 5426 | imod->name, cur->name, mod->name); |
| 5427 | if (lys_set_implemented(imod)) { |
| 5428 | LOGERR(ly_errno, "Setting the module \"%s\" implemented because of used default identity \"%s\" failed.", |
| 5429 | imod->name, cur->name); |
| 5430 | LOGVAL(LYE_SPEC, LY_VLOG_LYD, node, "Identity used as identityref value is not implemented."); |
| 5431 | goto fail; |
| 5432 | } |
| 5433 | } |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5434 | return cur; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5435 | } |
| 5436 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5437 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5438 | * @brief Resolve unresolved uses. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5439 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5440 | * @param[in] uses Uses to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5441 | * @param[in] unres Specific unres item. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5442 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5443 | * @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] | 5444 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5445 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5446 | 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] | 5447 | { |
Michal Vasko | 5191755 | 2017-03-08 10:21:34 +0100 | [diff] [blame] | 5448 | int rc, endian_idx; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5449 | struct lys_node *par_grp; |
Michal Vasko | e91afce | 2015-08-12 12:21:00 +0200 | [diff] [blame] | 5450 | |
Michal Vasko | 5191755 | 2017-03-08 10:21:34 +0100 | [diff] [blame] | 5451 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
| 5452 | endian_idx = 1; |
| 5453 | #else |
| 5454 | endian_idx = 0; |
| 5455 | #endif |
| 5456 | |
Radek Krejci | 6ff885d | 2017-01-03 14:06:22 +0100 | [diff] [blame] | 5457 | /* HACK: when a grouping has uses inside, all such uses have to be resolved before the grouping itself is used |
| 5458 | * in some uses. When we see such a uses, the grouping's higher byte of the flags member (not used in |
| 5459 | * grouping) is used to store number of so far unresolved uses. The grouping cannot be used unless this |
| 5460 | * counter is decreased back to 0. To remember that the uses already increased grouping's counter, the |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5461 | * LYS_USESGRP flag is used. */ |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 5462 | 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] | 5463 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5464 | if (!uses->grp) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 5465 | rc = resolve_uses_schema_nodeid(uses->name, (const struct lys_node *)uses, (const struct lys_node_grp **)&uses->grp); |
| 5466 | if (rc == -1) { |
Michal Vasko | 92981a6 | 2016-10-14 10:25:16 +0200 | [diff] [blame] | 5467 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 5468 | return -1; |
| 5469 | } else if (rc > 0) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5470 | 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] | 5471 | return -1; |
| 5472 | } else if (!uses->grp) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5473 | if (par_grp && !(uses->flags & LYS_USESGRP)) { |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 5474 | /* hack - in contrast to lys_node, lys_node_grp has bigger nacm field |
| 5475 | * (and smaller flags - it uses only a limited set of flags) |
| 5476 | */ |
Michal Vasko | 5191755 | 2017-03-08 10:21:34 +0100 | [diff] [blame] | 5477 | ((uint8_t*)&((struct lys_node_grp *)par_grp)->flags)[endian_idx]++; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5478 | uses->flags |= LYS_USESGRP; |
Michal Vasko | 407f1bb | 2015-09-23 15:51:07 +0200 | [diff] [blame] | 5479 | } |
Michal Vasko | 92981a6 | 2016-10-14 10:25:16 +0200 | [diff] [blame] | 5480 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 5481 | return EXIT_FAILURE; |
Michal Vasko | 12e3084 | 2015-08-04 11:54:00 +0200 | [diff] [blame] | 5482 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5483 | } |
| 5484 | |
Michal Vasko | 5191755 | 2017-03-08 10:21:34 +0100 | [diff] [blame] | 5485 | if (((uint8_t*)&uses->grp->flags)[endian_idx]) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5486 | if (par_grp && !(uses->flags & LYS_USESGRP)) { |
Michal Vasko | 5191755 | 2017-03-08 10:21:34 +0100 | [diff] [blame] | 5487 | ((uint8_t*)&((struct lys_node_grp *)par_grp)->flags)[endian_idx]++; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5488 | uses->flags |= LYS_USESGRP; |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 5489 | } else { |
| 5490 | /* instantiate grouping only when it is completely resolved */ |
| 5491 | uses->grp = NULL; |
Michal Vasko | 407f1bb | 2015-09-23 15:51:07 +0200 | [diff] [blame] | 5492 | } |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5493 | return EXIT_FAILURE; |
| 5494 | } |
| 5495 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5496 | rc = resolve_uses(uses, unres); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5497 | if (!rc) { |
| 5498 | /* decrease unres count only if not first try */ |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5499 | if (par_grp && (uses->flags & LYS_USESGRP)) { |
Michal Vasko | 5191755 | 2017-03-08 10:21:34 +0100 | [diff] [blame] | 5500 | if (!((uint8_t*)&((struct lys_node_grp *)par_grp)->flags)[endian_idx]) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5501 | LOGINT; |
| 5502 | return -1; |
| 5503 | } |
Michal Vasko | 5191755 | 2017-03-08 10:21:34 +0100 | [diff] [blame] | 5504 | ((uint8_t*)&((struct lys_node_grp *)par_grp)->flags)[endian_idx]--; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5505 | uses->flags &= ~LYS_USESGRP; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5506 | } |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5507 | |
| 5508 | /* check status */ |
Radek Krejci | c655602 | 2016-01-27 15:16:45 +0100 | [diff] [blame] | 5509 | if (lyp_check_status(uses->flags, uses->module, "of uses", |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 5510 | uses->grp->flags, uses->grp->module, uses->grp->name, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5511 | (struct lys_node *)uses)) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5512 | return -1; |
| 5513 | } |
| 5514 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5515 | return EXIT_SUCCESS; |
| 5516 | } |
| 5517 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5518 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5519 | } |
| 5520 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5521 | /** |
Michal Vasko | 9957e59 | 2015-08-17 15:04:09 +0200 | [diff] [blame] | 5522 | * @brief Resolve list keys. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5523 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5524 | * @param[in] list List to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5525 | * @param[in] keys_str Keys node value. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5526 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5527 | * @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] | 5528 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5529 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5530 | resolve_list_keys(struct lys_node_list *list, const char *keys_str) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5531 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5532 | int i, len, rc; |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 5533 | const char *value; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5534 | |
| 5535 | for (i = 0; i < list->keys_size; ++i) { |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 5536 | if (!list->child) { |
| 5537 | /* no child, possible forward reference */ |
| 5538 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, list, "list keys", keys_str); |
| 5539 | return EXIT_FAILURE; |
| 5540 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5541 | /* get the key name */ |
| 5542 | if ((value = strpbrk(keys_str, " \t\n"))) { |
| 5543 | len = value - keys_str; |
| 5544 | while (isspace(value[0])) { |
| 5545 | value++; |
| 5546 | } |
| 5547 | } else { |
| 5548 | len = strlen(keys_str); |
| 5549 | } |
| 5550 | |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 5551 | rc = lys_getnext_data(lys_node_module((struct lys_node *)list), (struct lys_node *)list, keys_str, len, LYS_LEAF, |
| 5552 | (const struct lys_node **)&list->keys[i]); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5553 | if (rc) { |
Michal Vasko | 7a55bea | 2016-05-02 14:51:20 +0200 | [diff] [blame] | 5554 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, list, "list keys", keys_str); |
| 5555 | return EXIT_FAILURE; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5556 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5557 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5558 | if (check_key(list, i, keys_str, len)) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5559 | /* check_key logs */ |
| 5560 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5561 | } |
| 5562 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5563 | /* check status */ |
Radek Krejci | c655602 | 2016-01-27 15:16:45 +0100 | [diff] [blame] | 5564 | if (lyp_check_status(list->flags, list->module, list->name, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5565 | list->keys[i]->flags, list->keys[i]->module, list->keys[i]->name, |
| 5566 | (struct lys_node *)list->keys[i])) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5567 | return -1; |
| 5568 | } |
| 5569 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5570 | /* prepare for next iteration */ |
| 5571 | while (value && isspace(value[0])) { |
| 5572 | value++; |
| 5573 | } |
| 5574 | keys_str = value; |
| 5575 | } |
| 5576 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5577 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5578 | } |
| 5579 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5580 | /** |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5581 | * @brief Resolve (check) all must conditions of \p node. |
| 5582 | * Logs directly. |
| 5583 | * |
| 5584 | * @param[in] node Data node with optional must statements. |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5585 | * @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] | 5586 | * |
| 5587 | * @return EXIT_SUCCESS on pass, EXIT_FAILURE on fail, -1 on error. |
| 5588 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5589 | static int |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5590 | resolve_must(struct lyd_node *node, int inout_parent, int ignore_fail) |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5591 | { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 5592 | int node_flags; |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5593 | uint8_t i, must_size; |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5594 | struct lys_node *schema; |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5595 | struct lys_restr *must; |
| 5596 | struct lyxp_set set; |
| 5597 | |
| 5598 | assert(node); |
| 5599 | memset(&set, 0, sizeof set); |
| 5600 | |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5601 | if (inout_parent) { |
| 5602 | for (schema = lys_parent(node->schema); |
| 5603 | schema && (schema->nodetype & (LYS_CHOICE | LYS_CASE | LYS_USES)); |
| 5604 | schema = lys_parent(schema)); |
| 5605 | if (!schema || !(schema->nodetype & (LYS_INPUT | LYS_OUTPUT))) { |
| 5606 | LOGINT; |
| 5607 | return -1; |
| 5608 | } |
| 5609 | must_size = ((struct lys_node_inout *)schema)->must_size; |
| 5610 | must = ((struct lys_node_inout *)schema)->must; |
| 5611 | |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 5612 | node_flags = schema->flags; |
| 5613 | |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5614 | /* context node is the RPC/action */ |
| 5615 | node = node->parent; |
| 5616 | if (!(node->schema->nodetype & (LYS_RPC | LYS_ACTION))) { |
| 5617 | LOGINT; |
| 5618 | return -1; |
| 5619 | } |
| 5620 | } else { |
| 5621 | switch (node->schema->nodetype) { |
| 5622 | case LYS_CONTAINER: |
| 5623 | must_size = ((struct lys_node_container *)node->schema)->must_size; |
| 5624 | must = ((struct lys_node_container *)node->schema)->must; |
| 5625 | break; |
| 5626 | case LYS_LEAF: |
| 5627 | must_size = ((struct lys_node_leaf *)node->schema)->must_size; |
| 5628 | must = ((struct lys_node_leaf *)node->schema)->must; |
| 5629 | break; |
| 5630 | case LYS_LEAFLIST: |
| 5631 | must_size = ((struct lys_node_leaflist *)node->schema)->must_size; |
| 5632 | must = ((struct lys_node_leaflist *)node->schema)->must; |
| 5633 | break; |
| 5634 | case LYS_LIST: |
| 5635 | must_size = ((struct lys_node_list *)node->schema)->must_size; |
| 5636 | must = ((struct lys_node_list *)node->schema)->must; |
| 5637 | break; |
| 5638 | case LYS_ANYXML: |
| 5639 | case LYS_ANYDATA: |
| 5640 | must_size = ((struct lys_node_anydata *)node->schema)->must_size; |
| 5641 | must = ((struct lys_node_anydata *)node->schema)->must; |
| 5642 | break; |
| 5643 | case LYS_NOTIF: |
| 5644 | must_size = ((struct lys_node_notif *)node->schema)->must_size; |
| 5645 | must = ((struct lys_node_notif *)node->schema)->must; |
| 5646 | break; |
| 5647 | default: |
| 5648 | must_size = 0; |
| 5649 | break; |
| 5650 | } |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 5651 | |
| 5652 | node_flags = node->schema->flags; |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5653 | } |
| 5654 | |
| 5655 | for (i = 0; i < must_size; ++i) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5656 | 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] | 5657 | return -1; |
| 5658 | } |
| 5659 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5660 | 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] | 5661 | |
Michal Vasko | 8146d4c | 2016-05-09 15:50:29 +0200 | [diff] [blame] | 5662 | if (!set.val.bool) { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 5663 | if ((ignore_fail == 1) || ((node_flags & LYS_XPATH_DEP) && (ignore_fail == 2))) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5664 | LOGVRB("Must condition \"%s\" not satisfied, but it is not required.", must[i].expr); |
| 5665 | } else { |
| 5666 | LOGVAL(LYE_NOMUST, LY_VLOG_LYD, node, must[i].expr); |
| 5667 | if (must[i].emsg) { |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 5668 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, must[i].emsg); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5669 | } |
| 5670 | if (must[i].eapptag) { |
| 5671 | strncpy(((struct ly_err *)&ly_errno)->apptag, must[i].eapptag, LY_APPTAG_LEN - 1); |
| 5672 | } |
| 5673 | return 1; |
Michal Vasko | 6ac6828 | 2016-04-11 10:56:47 +0200 | [diff] [blame] | 5674 | } |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5675 | } |
| 5676 | } |
| 5677 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5678 | return EXIT_SUCCESS; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5679 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5680 | |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5681 | /** |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5682 | * @brief Resolve (find) when condition schema context node. Does not log. |
| 5683 | * |
| 5684 | * @param[in] schema Schema node with the when condition. |
| 5685 | * @param[out] ctx_snode When schema context node. |
| 5686 | * @param[out] ctx_snode_type Schema context node type. |
| 5687 | */ |
| 5688 | void |
| 5689 | resolve_when_ctx_snode(const struct lys_node *schema, struct lys_node **ctx_snode, enum lyxp_node_type *ctx_snode_type) |
| 5690 | { |
| 5691 | const struct lys_node *sparent; |
| 5692 | |
| 5693 | /* find a not schema-only node */ |
| 5694 | *ctx_snode_type = LYXP_NODE_ELEM; |
| 5695 | while (schema->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE | LYS_AUGMENT | LYS_INPUT | LYS_OUTPUT)) { |
| 5696 | if (schema->nodetype == LYS_AUGMENT) { |
| 5697 | sparent = ((struct lys_node_augment *)schema)->target; |
| 5698 | } else { |
| 5699 | sparent = schema->parent; |
| 5700 | } |
| 5701 | if (!sparent) { |
| 5702 | /* context node is the document root (fake root in our case) */ |
| 5703 | if (schema->flags & LYS_CONFIG_W) { |
| 5704 | *ctx_snode_type = LYXP_NODE_ROOT_CONFIG; |
| 5705 | } else { |
Michal Vasko | b94a5e4 | 2016-09-08 14:01:56 +0200 | [diff] [blame] | 5706 | *ctx_snode_type = LYXP_NODE_ROOT; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5707 | } |
Michal Vasko | 2d2aec1 | 2016-09-08 11:42:50 +0200 | [diff] [blame] | 5708 | /* we need the first top-level sibling, but no uses or groupings */ |
Michal Vasko | b94a5e4 | 2016-09-08 14:01:56 +0200 | [diff] [blame] | 5709 | schema = lys_getnext(NULL, NULL, lys_node_module(schema), 0); |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5710 | break; |
| 5711 | } |
| 5712 | schema = sparent; |
| 5713 | } |
| 5714 | |
| 5715 | *ctx_snode = (struct lys_node *)schema; |
| 5716 | } |
| 5717 | |
| 5718 | /** |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5719 | * @brief Resolve (find) when condition context node. Does not log. |
| 5720 | * |
| 5721 | * @param[in] node Data node, whose conditional definition is being decided. |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5722 | * @param[in] schema Schema node with the when condition. |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5723 | * @param[out] ctx_node Context node. |
| 5724 | * @param[out] ctx_node_type Context node type. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5725 | * |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5726 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5727 | */ |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5728 | static int |
| 5729 | resolve_when_ctx_node(struct lyd_node *node, struct lys_node *schema, struct lyd_node **ctx_node, |
| 5730 | enum lyxp_node_type *ctx_node_type) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5731 | { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5732 | struct lyd_node *parent; |
| 5733 | struct lys_node *sparent; |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5734 | enum lyxp_node_type node_type; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5735 | uint16_t i, data_depth, schema_depth; |
| 5736 | |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5737 | resolve_when_ctx_snode(schema, &schema, &node_type); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5738 | |
Michal Vasko | fe98975 | 2016-09-08 08:47:26 +0200 | [diff] [blame] | 5739 | if (node_type == LYXP_NODE_ELEM) { |
| 5740 | /* standard element context node */ |
| 5741 | for (parent = node, data_depth = 0; parent; parent = parent->parent, ++data_depth); |
| 5742 | for (sparent = schema, schema_depth = 0; |
| 5743 | sparent; |
| 5744 | sparent = (sparent->nodetype == LYS_AUGMENT ? ((struct lys_node_augment *)sparent)->target : sparent->parent)) { |
| 5745 | if (sparent->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_NOTIF | LYS_RPC)) { |
| 5746 | ++schema_depth; |
| 5747 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5748 | } |
Michal Vasko | fe98975 | 2016-09-08 08:47:26 +0200 | [diff] [blame] | 5749 | if (data_depth < schema_depth) { |
| 5750 | return -1; |
| 5751 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5752 | |
Michal Vasko | 956e854 | 2016-08-26 09:43:35 +0200 | [diff] [blame] | 5753 | /* find the corresponding data node */ |
| 5754 | for (i = 0; i < data_depth - schema_depth; ++i) { |
| 5755 | node = node->parent; |
| 5756 | } |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5757 | if (node->schema != schema) { |
| 5758 | return -1; |
| 5759 | } |
Michal Vasko | fe98975 | 2016-09-08 08:47:26 +0200 | [diff] [blame] | 5760 | } else { |
| 5761 | /* root context node */ |
| 5762 | while (node->parent) { |
| 5763 | node = node->parent; |
| 5764 | } |
| 5765 | while (node->prev->next) { |
| 5766 | node = node->prev; |
| 5767 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5768 | } |
| 5769 | |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5770 | *ctx_node = node; |
| 5771 | *ctx_node_type = node_type; |
| 5772 | return EXIT_SUCCESS; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5773 | } |
| 5774 | |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5775 | /** |
| 5776 | * @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] | 5777 | * The context node is adjusted if needed. |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5778 | * |
| 5779 | * @param[in] snode Schema node, whose children instances need to be unlinked. |
| 5780 | * @param[in,out] node Data siblings where to look for the children of \p snode. If it is unlinked, |
| 5781 | * it is moved to point to another sibling still in the original tree. |
| 5782 | * @param[in,out] ctx_node When context node, adjusted if needed. |
| 5783 | * @param[in] ctx_node_type Context node type, just for information to detect invalid situations. |
| 5784 | * @param[out] unlinked_nodes Unlinked siblings. Can be safely appended to \p node afterwards. |
| 5785 | * Ordering may change, but there will be no semantic change. |
| 5786 | * |
| 5787 | * @return EXIT_SUCCESS on success, -1 on error. |
| 5788 | */ |
| 5789 | static int |
| 5790 | resolve_when_unlink_nodes(struct lys_node *snode, struct lyd_node **node, struct lyd_node **ctx_node, |
| 5791 | enum lyxp_node_type ctx_node_type, struct lyd_node **unlinked_nodes) |
| 5792 | { |
| 5793 | struct lyd_node *next, *elem; |
Michal Vasko | eb81fb7 | 2017-02-06 12:11:08 +0100 | [diff] [blame] | 5794 | const struct lys_node *slast; |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5795 | |
| 5796 | switch (snode->nodetype) { |
| 5797 | case LYS_AUGMENT: |
| 5798 | case LYS_USES: |
| 5799 | case LYS_CHOICE: |
| 5800 | case LYS_CASE: |
Michal Vasko | eb81fb7 | 2017-02-06 12:11:08 +0100 | [diff] [blame] | 5801 | slast = NULL; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 5802 | while ((slast = lys_getnext(slast, snode, NULL, LYS_GETNEXT_PARENTUSES))) { |
Michal Vasko | eb81fb7 | 2017-02-06 12:11:08 +0100 | [diff] [blame] | 5803 | if (slast->nodetype & (LYS_ACTION | LYS_NOTIF)) { |
| 5804 | continue; |
| 5805 | } |
| 5806 | |
| 5807 | 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] | 5808 | return -1; |
| 5809 | } |
| 5810 | } |
| 5811 | break; |
| 5812 | case LYS_CONTAINER: |
| 5813 | case LYS_LIST: |
| 5814 | case LYS_LEAF: |
| 5815 | case LYS_LEAFLIST: |
| 5816 | case LYS_ANYXML: |
| 5817 | case LYS_ANYDATA: |
| 5818 | LY_TREE_FOR_SAFE(lyd_first_sibling(*node), next, elem) { |
| 5819 | if (elem->schema == snode) { |
| 5820 | |
| 5821 | if (elem == *ctx_node) { |
| 5822 | /* We are going to unlink our context node! This normally cannot happen, |
| 5823 | * but we use normal top-level data nodes for faking a document root node, |
| 5824 | * so if this is the context node, we just use the next top-level node. |
| 5825 | * Additionally, it can even happen that there are no top-level data nodes left, |
| 5826 | * all were unlinked, so in this case we pass NULL as the context node/data tree, |
| 5827 | * lyxp_eval() can handle this special situation. |
| 5828 | */ |
| 5829 | if (ctx_node_type == LYXP_NODE_ELEM) { |
| 5830 | LOGINT; |
| 5831 | return -1; |
| 5832 | } |
| 5833 | |
| 5834 | if (elem->prev == elem) { |
| 5835 | /* unlinking last top-level element, use an empty data tree */ |
| 5836 | *ctx_node = NULL; |
| 5837 | } else { |
| 5838 | /* in this case just use the previous/last top-level data node */ |
| 5839 | *ctx_node = elem->prev; |
| 5840 | } |
| 5841 | } else if (elem == *node) { |
| 5842 | /* We are going to unlink the currently processed node. This does not matter that |
| 5843 | * much, but we would lose access to the original data tree, so just move our |
| 5844 | * pointer somewhere still inside it. |
| 5845 | */ |
| 5846 | if ((*node)->prev != *node) { |
| 5847 | *node = (*node)->prev; |
| 5848 | } else { |
| 5849 | /* the processed node with sibings were all unlinked, oh well */ |
| 5850 | *node = NULL; |
| 5851 | } |
| 5852 | } |
| 5853 | |
| 5854 | /* temporarily unlink the node */ |
Michal Vasko | 2bce30c | 2017-02-06 12:16:39 +0100 | [diff] [blame] | 5855 | lyd_unlink_internal(elem, 0); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5856 | if (*unlinked_nodes) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5857 | if (lyd_insert_after((*unlinked_nodes)->prev, elem)) { |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5858 | LOGINT; |
| 5859 | return -1; |
| 5860 | } |
| 5861 | } else { |
| 5862 | *unlinked_nodes = elem; |
| 5863 | } |
| 5864 | |
| 5865 | if (snode->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_ANYDATA)) { |
| 5866 | /* there can be only one instance */ |
| 5867 | break; |
| 5868 | } |
| 5869 | } |
| 5870 | } |
| 5871 | break; |
| 5872 | default: |
| 5873 | LOGINT; |
| 5874 | return -1; |
| 5875 | } |
| 5876 | |
| 5877 | return EXIT_SUCCESS; |
| 5878 | } |
| 5879 | |
| 5880 | /** |
| 5881 | * @brief Relink the unlinked nodes back. |
| 5882 | * |
| 5883 | * @param[in] node Data node to link the nodes back to. It can actually be the adjusted context node, |
| 5884 | * we simply need a sibling from the original data tree. |
| 5885 | * @param[in] unlinked_nodes Unlinked nodes to relink to \p node. |
| 5886 | * @param[in] ctx_node_type Context node type to distinguish between \p node being the parent |
| 5887 | * or the sibling of \p unlinked_nodes. |
| 5888 | * |
| 5889 | * @return EXIT_SUCCESS on success, -1 on error. |
| 5890 | */ |
| 5891 | static int |
| 5892 | resolve_when_relink_nodes(struct lyd_node *node, struct lyd_node *unlinked_nodes, enum lyxp_node_type ctx_node_type) |
| 5893 | { |
| 5894 | struct lyd_node *elem; |
| 5895 | |
| 5896 | LY_TREE_FOR_SAFE(unlinked_nodes, unlinked_nodes, elem) { |
Michal Vasko | 2bce30c | 2017-02-06 12:16:39 +0100 | [diff] [blame] | 5897 | lyd_unlink_internal(elem, 0); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5898 | if (ctx_node_type == LYXP_NODE_ELEM) { |
Michal Vasko | 2bce30c | 2017-02-06 12:16:39 +0100 | [diff] [blame] | 5899 | if (lyd_insert_common(node, NULL, elem, 0)) { |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5900 | return -1; |
| 5901 | } |
| 5902 | } else { |
Michal Vasko | 2bce30c | 2017-02-06 12:16:39 +0100 | [diff] [blame] | 5903 | if (lyd_insert_nextto(node, elem, 0, 0)) { |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5904 | return -1; |
| 5905 | } |
| 5906 | } |
| 5907 | } |
| 5908 | |
| 5909 | return EXIT_SUCCESS; |
| 5910 | } |
| 5911 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5912 | int |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5913 | resolve_applies_must(const struct lyd_node *node) |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5914 | { |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5915 | int ret = 0; |
| 5916 | uint8_t must_size; |
| 5917 | struct lys_node *schema, *iter; |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5918 | |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5919 | assert(node); |
| 5920 | |
| 5921 | schema = node->schema; |
| 5922 | |
| 5923 | /* their own must */ |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5924 | switch (schema->nodetype) { |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5925 | case LYS_CONTAINER: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5926 | must_size = ((struct lys_node_container *)schema)->must_size; |
| 5927 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5928 | case LYS_LEAF: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5929 | must_size = ((struct lys_node_leaf *)schema)->must_size; |
| 5930 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5931 | case LYS_LEAFLIST: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5932 | must_size = ((struct lys_node_leaflist *)schema)->must_size; |
| 5933 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5934 | case LYS_LIST: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5935 | must_size = ((struct lys_node_list *)schema)->must_size; |
| 5936 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5937 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 5938 | case LYS_ANYDATA: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5939 | must_size = ((struct lys_node_anydata *)schema)->must_size; |
| 5940 | break; |
| 5941 | case LYS_NOTIF: |
| 5942 | must_size = ((struct lys_node_notif *)schema)->must_size; |
| 5943 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5944 | default: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5945 | must_size = 0; |
| 5946 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5947 | } |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5948 | |
| 5949 | if (must_size) { |
| 5950 | ++ret; |
| 5951 | } |
| 5952 | |
| 5953 | /* schema may be a direct data child of input/output with must (but it must be first, it needs to be evaluated only once) */ |
| 5954 | if (!node->prev->next) { |
| 5955 | for (iter = lys_parent(schema); iter && (iter->nodetype & (LYS_CHOICE | LYS_CASE | LYS_USES)); iter = lys_parent(iter)); |
| 5956 | if (iter && (iter->nodetype & (LYS_INPUT | LYS_OUTPUT))) { |
| 5957 | ret += 0x2; |
| 5958 | } |
| 5959 | } |
| 5960 | |
| 5961 | return ret; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5962 | } |
| 5963 | |
| 5964 | int |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5965 | 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] | 5966 | { |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5967 | const struct lys_node *parent; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5968 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5969 | assert(schema); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5970 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5971 | 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] | 5972 | return 1; |
| 5973 | } |
| 5974 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5975 | parent = schema; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5976 | goto check_augment; |
| 5977 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5978 | while (parent) { |
| 5979 | /* stop conditions */ |
| 5980 | if (!mode) { |
| 5981 | /* stop on node that can be instantiated in data tree */ |
| 5982 | if (!(parent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) { |
| 5983 | break; |
| 5984 | } |
| 5985 | } else { |
| 5986 | /* stop on the specified node */ |
| 5987 | if (parent == stop) { |
| 5988 | break; |
| 5989 | } |
| 5990 | } |
| 5991 | |
| 5992 | if (((const struct lys_node_uses *)parent)->when) { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5993 | return 1; |
| 5994 | } |
| 5995 | check_augment: |
| 5996 | |
| 5997 | if ((parent->parent && (parent->parent->nodetype == LYS_AUGMENT) && |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5998 | (((const struct lys_node_augment *)parent->parent)->when))) { |
Michal Vasko | e365556 | 2016-08-24 15:56:17 +0200 | [diff] [blame] | 5999 | return 1; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6000 | } |
| 6001 | parent = lys_parent(parent); |
| 6002 | } |
| 6003 | |
| 6004 | return 0; |
| 6005 | } |
| 6006 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6007 | /** |
| 6008 | * @brief Resolve (check) all when conditions relevant for \p node. |
| 6009 | * Logs directly. |
| 6010 | * |
| 6011 | * @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] | 6012 | * |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6013 | * @return |
| 6014 | * -1 - error, ly_errno is set |
| 6015 | * 0 - true "when" statement |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 6016 | * 0, ly_vecode = LYVE_NOWHEN - false "when" statement |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6017 | * 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] | 6018 | */ |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 6019 | int |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6020 | resolve_when(struct lyd_node *node, int *result, int ignore_fail) |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6021 | { |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6022 | struct lyd_node *ctx_node = NULL, *unlinked_nodes, *tmp_node; |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6023 | struct lys_node *sparent; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6024 | struct lyxp_set set; |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 6025 | enum lyxp_node_type ctx_node_type; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6026 | int rc = 0; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6027 | |
| 6028 | assert(node); |
| 6029 | memset(&set, 0, sizeof set); |
| 6030 | |
Michal Vasko | 78d97e2 | 2017-02-21 09:54:38 +0100 | [diff] [blame] | 6031 | 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] | 6032 | /* make the node dummy for the evaluation */ |
| 6033 | node->validity |= LYD_VAL_INUSE; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6034 | rc = lyxp_eval(((struct lys_node_container *)node->schema)->when->cond, node, LYXP_NODE_ELEM, lyd_node_module(node), |
| 6035 | &set, LYXP_WHEN); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6036 | node->validity &= ~LYD_VAL_INUSE; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6037 | if (rc) { |
| 6038 | if (rc == 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6039 | 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] | 6040 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6041 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6042 | } |
| 6043 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6044 | /* set boolean result of the condition */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6045 | 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] | 6046 | if (!set.val.bool) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6047 | node->when_status |= LYD_WHEN_FALSE; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 6048 | 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] | 6049 | LOGVRB("When condition \"%s\" is not satisfied, but it is not required.", |
| 6050 | ((struct lys_node_container *)node->schema)->when->cond); |
| 6051 | } else { |
| 6052 | LOGVAL(LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_container *)node->schema)->when->cond); |
| 6053 | goto cleanup; |
| 6054 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6055 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6056 | |
| 6057 | /* free xpath set content */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6058 | 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] | 6059 | } |
| 6060 | |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6061 | sparent = node->schema; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6062 | goto check_augment; |
| 6063 | |
| 6064 | /* check when in every schema node that affects node */ |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6065 | while (sparent && (sparent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) { |
| 6066 | if (((struct lys_node_uses *)sparent)->when) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6067 | if (!ctx_node) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6068 | rc = resolve_when_ctx_node(node, sparent, &ctx_node, &ctx_node_type); |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 6069 | if (rc) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6070 | LOGINT; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6071 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6072 | } |
| 6073 | } |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6074 | |
| 6075 | unlinked_nodes = NULL; |
| 6076 | /* we do not want our node pointer to change */ |
| 6077 | tmp_node = node; |
| 6078 | rc = resolve_when_unlink_nodes(sparent, &tmp_node, &ctx_node, ctx_node_type, &unlinked_nodes); |
| 6079 | if (rc) { |
| 6080 | goto cleanup; |
| 6081 | } |
| 6082 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6083 | rc = lyxp_eval(((struct lys_node_uses *)sparent)->when->cond, ctx_node, ctx_node_type, lys_node_module(sparent), |
| 6084 | &set, LYXP_WHEN); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6085 | |
| 6086 | if (unlinked_nodes && ctx_node) { |
| 6087 | if (resolve_when_relink_nodes(ctx_node, unlinked_nodes, ctx_node_type)) { |
| 6088 | rc = -1; |
| 6089 | goto cleanup; |
| 6090 | } |
| 6091 | } |
| 6092 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6093 | if (rc) { |
| 6094 | if (rc == 1) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6095 | 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] | 6096 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6097 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6098 | } |
| 6099 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6100 | 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] | 6101 | if (!set.val.bool) { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 6102 | if ((ignore_fail == 1) || ((sparent->flags & LYS_XPATH_DEP) || (ignore_fail == 2))) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6103 | LOGVRB("When condition \"%s\" is not satisfied, but it is not required.", |
| 6104 | ((struct lys_node_uses *)sparent)->when->cond); |
| 6105 | } else { |
Michal Vasko | 2cb18e7 | 2017-03-28 14:46:33 +0200 | [diff] [blame] | 6106 | node->when_status |= LYD_WHEN_FALSE; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6107 | LOGVAL(LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_uses *)sparent)->when->cond); |
| 6108 | goto cleanup; |
| 6109 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6110 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6111 | |
| 6112 | /* free xpath set content */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6113 | 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] | 6114 | } |
| 6115 | |
| 6116 | check_augment: |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6117 | 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] | 6118 | if (!ctx_node) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6119 | 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] | 6120 | if (rc) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6121 | LOGINT; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6122 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6123 | } |
| 6124 | } |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6125 | |
| 6126 | unlinked_nodes = NULL; |
| 6127 | tmp_node = node; |
| 6128 | rc = resolve_when_unlink_nodes(sparent->parent, &tmp_node, &ctx_node, ctx_node_type, &unlinked_nodes); |
| 6129 | if (rc) { |
| 6130 | goto cleanup; |
| 6131 | } |
| 6132 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6133 | rc = lyxp_eval(((struct lys_node_augment *)sparent->parent)->when->cond, ctx_node, ctx_node_type, |
| 6134 | lys_node_module(sparent->parent), &set, LYXP_WHEN); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6135 | |
| 6136 | /* reconnect nodes, if ctx_node is NULL then all the nodes were unlinked, but linked together, |
| 6137 | * so the tree did not actually change and there is nothing for us to do |
| 6138 | */ |
| 6139 | if (unlinked_nodes && ctx_node) { |
| 6140 | if (resolve_when_relink_nodes(ctx_node, unlinked_nodes, ctx_node_type)) { |
| 6141 | rc = -1; |
| 6142 | goto cleanup; |
| 6143 | } |
| 6144 | } |
| 6145 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6146 | if (rc) { |
| 6147 | if (rc == 1) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6148 | 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] | 6149 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6150 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6151 | } |
| 6152 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6153 | 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] | 6154 | if (!set.val.bool) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6155 | node->when_status |= LYD_WHEN_FALSE; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 6156 | 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] | 6157 | LOGVRB("When condition \"%s\" is not satisfied, but it is not required.", |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 6158 | ((struct lys_node_augment *)sparent->parent)->when->cond); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6159 | } else { |
| 6160 | LOGVAL(LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_augment *)sparent->parent)->when->cond); |
| 6161 | goto cleanup; |
| 6162 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6163 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6164 | |
| 6165 | /* free xpath set content */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6166 | 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] | 6167 | } |
| 6168 | |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6169 | sparent = lys_parent(sparent); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6170 | } |
| 6171 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6172 | node->when_status |= LYD_WHEN_TRUE; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6173 | |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6174 | cleanup: |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6175 | /* free xpath set content */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6176 | 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] | 6177 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 6178 | if (result) { |
| 6179 | if (node->when_status & LYD_WHEN_TRUE) { |
| 6180 | *result = 1; |
| 6181 | } else { |
| 6182 | *result = 0; |
| 6183 | } |
| 6184 | } |
| 6185 | |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6186 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6187 | } |
| 6188 | |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6189 | static int |
| 6190 | check_leafref_features(struct lys_type *type) |
| 6191 | { |
| 6192 | struct lys_node *iter; |
| 6193 | struct ly_set *src_parents, *trg_parents, *features; |
| 6194 | unsigned int i, j, size, x; |
| 6195 | int ret = EXIT_SUCCESS; |
| 6196 | |
| 6197 | assert(type->parent); |
| 6198 | |
| 6199 | src_parents = ly_set_new(); |
| 6200 | trg_parents = ly_set_new(); |
| 6201 | features = ly_set_new(); |
| 6202 | |
| 6203 | /* get parents chain of source (leafref) */ |
Radek Krejci | ecda01a | 2017-04-05 15:44:27 +0200 | [diff] [blame] | 6204 | for (iter = (struct lys_node *)type->parent; iter; iter = lys_parent(iter)) { |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6205 | if (iter->nodetype & (LYS_INPUT | LYS_OUTPUT)) { |
| 6206 | continue; |
| 6207 | } |
| 6208 | ly_set_add(src_parents, iter, LY_SET_OPT_USEASLIST); |
| 6209 | } |
| 6210 | /* get parents chain of target */ |
Radek Krejci | ecda01a | 2017-04-05 15:44:27 +0200 | [diff] [blame] | 6211 | 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] | 6212 | if (iter->nodetype & (LYS_INPUT | LYS_OUTPUT)) { |
| 6213 | continue; |
| 6214 | } |
| 6215 | ly_set_add(trg_parents, iter, LY_SET_OPT_USEASLIST); |
| 6216 | } |
| 6217 | |
| 6218 | /* compare the features used in if-feature statements in the rest of both |
| 6219 | * chains of parents. The set of features used for target must be a subset |
| 6220 | * of features used for the leafref. This is not a perfect, we should compare |
| 6221 | * the truth tables but it could require too much resources, so we simplify that */ |
| 6222 | for (i = 0; i < src_parents->number; i++) { |
| 6223 | iter = src_parents->set.s[i]; /* shortcut */ |
| 6224 | if (!iter->iffeature_size) { |
| 6225 | continue; |
| 6226 | } |
| 6227 | for (j = 0; j < iter->iffeature_size; j++) { |
| 6228 | resolve_iffeature_getsizes(&iter->iffeature[j], NULL, &size); |
| 6229 | for (; size; size--) { |
| 6230 | if (!iter->iffeature[j].features[size - 1]) { |
| 6231 | /* not yet resolved feature, postpone this check */ |
| 6232 | ret = EXIT_FAILURE; |
| 6233 | goto cleanup; |
| 6234 | } |
| 6235 | ly_set_add(features, iter->iffeature[j].features[size - 1], 0); |
| 6236 | } |
| 6237 | } |
| 6238 | } |
| 6239 | x = features->number; |
| 6240 | for (i = 0; i < trg_parents->number; i++) { |
| 6241 | iter = trg_parents->set.s[i]; /* shortcut */ |
| 6242 | if (!iter->iffeature_size) { |
| 6243 | continue; |
| 6244 | } |
| 6245 | for (j = 0; j < iter->iffeature_size; j++) { |
| 6246 | resolve_iffeature_getsizes(&iter->iffeature[j], NULL, &size); |
| 6247 | for (; size; size--) { |
| 6248 | if (!iter->iffeature[j].features[size - 1]) { |
| 6249 | /* not yet resolved feature, postpone this check */ |
| 6250 | ret = EXIT_FAILURE; |
| 6251 | goto cleanup; |
| 6252 | } |
| 6253 | if ((unsigned int)ly_set_add(features, iter->iffeature[j].features[size - 1], 0) >= x) { |
| 6254 | /* the feature is not present in features set of target's parents chain */ |
| 6255 | 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] | 6256 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6257 | "Leafref is not conditional based on \"%s\" feature as its target.", |
| 6258 | iter->iffeature[j].features[size - 1]->name); |
| 6259 | ret = -1; |
| 6260 | goto cleanup; |
| 6261 | } |
| 6262 | } |
| 6263 | } |
| 6264 | } |
| 6265 | |
| 6266 | cleanup: |
| 6267 | ly_set_free(features); |
| 6268 | ly_set_free(src_parents); |
| 6269 | ly_set_free(trg_parents); |
| 6270 | |
| 6271 | return ret; |
| 6272 | } |
| 6273 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6274 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6275 | * @brief Resolve a single unres schema item. Logs indirectly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6276 | * |
| 6277 | * @param[in] mod Main module. |
| 6278 | * @param[in] item Item to resolve. Type determined by \p type. |
| 6279 | * @param[in] type Type of the unresolved item. |
| 6280 | * @param[in] str_snode String, a schema node, or NULL. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6281 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 769f803 | 2017-01-24 13:11:55 +0100 | [diff] [blame] | 6282 | * @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] | 6283 | * |
| 6284 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 6285 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6286 | static int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 6287 | 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] | 6288 | struct unres_schema *unres, int final_fail) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6289 | { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6290 | /* 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] | 6291 | int rc = -1, has_str = 0, parent_type = 0, i, k; |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6292 | unsigned int j; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6293 | struct lys_node *root, *next, *node, *par_grp; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6294 | const char *expr; |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 6295 | uint8_t *u; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6296 | |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6297 | struct ly_set *refs, *procs; |
| 6298 | struct lys_feature *ref, *feat; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6299 | struct lys_ident *ident; |
| 6300 | struct lys_type *stype; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6301 | struct lys_node_choice *choic; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6302 | struct lyxml_elem *yin; |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6303 | struct yang_type *yang; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6304 | struct unres_list_uniq *unique_info; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6305 | struct unres_iffeat_data *iff_data; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6306 | struct unres_ext *ext_data; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6307 | struct lys_ext_instance *ext, **extlist; |
| 6308 | struct lyext_plugin *eplugin; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6309 | |
| 6310 | switch (type) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6311 | case UNRES_IDENT: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6312 | expr = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 6313 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6314 | ident = item; |
| 6315 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6316 | rc = resolve_base_ident(mod, ident, expr, "identity", NULL, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6317 | break; |
| 6318 | case UNRES_TYPE_IDENTREF: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6319 | expr = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 6320 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6321 | stype = item; |
| 6322 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6323 | rc = resolve_base_ident(mod, NULL, expr, "type", stype, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6324 | break; |
| 6325 | case UNRES_TYPE_LEAFREF: |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 6326 | node = str_snode; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6327 | stype = item; |
| 6328 | |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 6329 | rc = resolve_schema_leafref(stype->info.lref.path, node, (const struct lys_node **)&stype->info.lref.target); |
| 6330 | if (!rc) { |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 6331 | assert(stype->info.lref.target); |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6332 | /* check if leafref and its target are under a common if-features */ |
| 6333 | rc = check_leafref_features(stype); |
| 6334 | if (rc) { |
| 6335 | break; |
| 6336 | } |
| 6337 | |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 6338 | if (lys_node_module(node)->implemented) { |
| 6339 | /* make all the modules on the path implemented */ |
| 6340 | for (next = (struct lys_node *)stype->info.lref.target; next; next = lys_parent(next)) { |
| 6341 | if (!lys_node_module(next)->implemented) { |
| 6342 | if (lys_set_implemented(lys_node_module(next))) { |
| 6343 | rc = -1; |
| 6344 | break; |
| 6345 | } |
| 6346 | } |
| 6347 | } |
| 6348 | if (next) { |
| 6349 | break; |
| 6350 | } |
| 6351 | |
| 6352 | /* store the backlink from leafref target */ |
| 6353 | if (lys_leaf_add_leafref_target(stype->info.lref.target, (struct lys_node *)stype->parent)) { |
| 6354 | rc = -1; |
| 6355 | } |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 6356 | } |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 6357 | } |
| 6358 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6359 | break; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 6360 | case UNRES_TYPE_DER_EXT: |
| 6361 | parent_type++; |
| 6362 | /* no break */ |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 6363 | case UNRES_TYPE_DER_TPDF: |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 6364 | parent_type++; |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 6365 | /* no break */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6366 | case UNRES_TYPE_DER: |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6367 | /* parent */ |
| 6368 | node = str_snode; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6369 | stype = item; |
| 6370 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6371 | /* HACK type->der is temporarily unparsed type statement */ |
| 6372 | yin = (struct lyxml_elem *)stype->der; |
| 6373 | stype->der = NULL; |
| 6374 | |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6375 | if (yin->flags & LY_YANG_STRUCTURE_FLAG) { |
| 6376 | yang = (struct yang_type *)yin; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 6377 | rc = yang_check_type(mod, node, yang, stype, parent_type, unres); |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6378 | |
| 6379 | if (rc) { |
Pavol Vican | 8bd72e4 | 2016-08-29 09:53:05 +0200 | [diff] [blame] | 6380 | /* may try again later */ |
| 6381 | stype->der = (struct lys_tpdf *)yang; |
Pavol Vican | d01d8ae | 2016-03-01 10:45:59 +0100 | [diff] [blame] | 6382 | } else { |
| 6383 | /* 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] | 6384 | lydict_remove(mod->ctx, yang->name); |
Pavol Vican | d01d8ae | 2016-03-01 10:45:59 +0100 | [diff] [blame] | 6385 | free(yang); |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6386 | } |
| 6387 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6388 | } else { |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 6389 | rc = fill_yin_type(mod, node, yin, stype, parent_type, unres); |
Radek Krejci | 63fc096 | 2017-02-15 13:20:18 +0100 | [diff] [blame] | 6390 | if (!rc || rc == -1) { |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6391 | /* we need to always be able to free this, it's safe only in this case */ |
| 6392 | lyxml_free(mod->ctx, yin); |
| 6393 | } else { |
| 6394 | /* may try again later, put all back how it was */ |
| 6395 | stype->der = (struct lys_tpdf *)yin; |
| 6396 | } |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6397 | } |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 6398 | if (rc == EXIT_SUCCESS) { |
Radek Krejci | 2c2fce8 | 2016-08-01 13:52:26 +0200 | [diff] [blame] | 6399 | /* it does not make sense to have leaf-list of empty type */ |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 6400 | if (!parent_type && node->nodetype == LYS_LEAFLIST && stype->base == LY_TYPE_EMPTY) { |
Radek Krejci | 2c2fce8 | 2016-08-01 13:52:26 +0200 | [diff] [blame] | 6401 | LOGWRN("The leaf-list \"%s\" is of \"empty\" type, which does not make sense.", node->name); |
| 6402 | } |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6403 | } else if (rc == EXIT_FAILURE && stype->base != LY_TYPE_ERR) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 6404 | /* forward reference - in case the type is in grouping, we have to make the grouping unusable |
| 6405 | * by uses statement until the type is resolved. We do that the same way as uses statements inside |
| 6406 | * grouping - the grouping's nacm member (not used un grouping) is used to increase the number of |
| 6407 | * so far unresolved items (uses and types). The grouping cannot be used unless the nacm value is 0. |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6408 | * To remember that the grouping already increased grouping's nacm, the LY_TYPE_ERR is used as value |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 6409 | * of the type's base member. */ |
| 6410 | for (par_grp = node; par_grp && (par_grp->nodetype != LYS_GROUPING); par_grp = lys_parent(par_grp)); |
| 6411 | if (par_grp) { |
Radek Krejci | 6ff885d | 2017-01-03 14:06:22 +0100 | [diff] [blame] | 6412 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
| 6413 | ((uint8_t*)&((struct lys_node_grp *)par_grp)->flags)[1]++; |
| 6414 | #else |
| 6415 | ((uint8_t*)&((struct lys_node_grp *)par_grp)->flags)[0]++; |
| 6416 | #endif |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6417 | stype->base = LY_TYPE_ERR; |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 6418 | } |
Radek Krejci | 2c2fce8 | 2016-08-01 13:52:26 +0200 | [diff] [blame] | 6419 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6420 | break; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6421 | case UNRES_IFFEAT: |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6422 | iff_data = str_snode; |
| 6423 | 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] | 6424 | if (!rc) { |
| 6425 | /* success */ |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 6426 | if (iff_data->infeature) { |
| 6427 | /* store backlink into the target feature to allow reverse changes in case of changing feature status */ |
| 6428 | feat = *((struct lys_feature **)item); |
| 6429 | if (!feat->depfeatures) { |
| 6430 | feat->depfeatures = ly_set_new(); |
| 6431 | } |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 6432 | ly_set_add(feat->depfeatures, iff_data->node, LY_SET_OPT_USEASLIST); |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 6433 | } |
| 6434 | /* cleanup temporary data */ |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6435 | lydict_remove(mod->ctx, iff_data->fname); |
| 6436 | free(iff_data); |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6437 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6438 | break; |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6439 | case UNRES_FEATURE: |
| 6440 | feat = (struct lys_feature *)item; |
| 6441 | |
| 6442 | if (feat->iffeature_size) { |
| 6443 | refs = ly_set_new(); |
| 6444 | procs = ly_set_new(); |
| 6445 | ly_set_add(procs, feat, 0); |
| 6446 | |
| 6447 | while (procs->number) { |
| 6448 | ref = procs->set.g[procs->number - 1]; |
| 6449 | ly_set_rm_index(procs, procs->number - 1); |
| 6450 | |
| 6451 | for (i = 0; i < ref->iffeature_size; i++) { |
| 6452 | resolve_iffeature_getsizes(&ref->iffeature[i], NULL, &j); |
| 6453 | for (; j > 0 ; j--) { |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6454 | if (ref->iffeature[i].features[j - 1]) { |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6455 | if (ref->iffeature[i].features[j - 1] == feat) { |
| 6456 | LOGVAL(LYE_CIRC_FEATURES, LY_VLOG_NONE, NULL, feat->name); |
| 6457 | goto featurecheckdone; |
| 6458 | } |
| 6459 | |
| 6460 | if (ref->iffeature[i].features[j - 1]->iffeature_size) { |
| 6461 | k = refs->number; |
| 6462 | if (ly_set_add(refs, ref->iffeature[i].features[j - 1], 0) == k) { |
| 6463 | /* not yet seen feature, add it for processing */ |
| 6464 | ly_set_add(procs, ref->iffeature[i].features[j - 1], 0); |
| 6465 | } |
| 6466 | } |
| 6467 | } else { |
| 6468 | /* forward reference */ |
| 6469 | rc = EXIT_FAILURE; |
| 6470 | goto featurecheckdone; |
| 6471 | } |
| 6472 | } |
| 6473 | |
| 6474 | } |
| 6475 | } |
| 6476 | rc = EXIT_SUCCESS; |
| 6477 | |
| 6478 | featurecheckdone: |
| 6479 | ly_set_free(refs); |
| 6480 | ly_set_free(procs); |
| 6481 | } |
| 6482 | |
| 6483 | break; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6484 | case UNRES_USES: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6485 | rc = resolve_unres_schema_uses(item, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6486 | break; |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 6487 | case UNRES_TYPEDEF_DFLT: |
| 6488 | parent_type++; |
| 6489 | /* no break */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6490 | case UNRES_TYPE_DFLT: |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6491 | stype = item; |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 6492 | rc = check_default(stype, (const char **)str_snode, mod, parent_type); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6493 | break; |
| 6494 | case UNRES_CHOICE_DFLT: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6495 | expr = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 6496 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6497 | choic = item; |
| 6498 | |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 6499 | if (!choic->dflt) { |
| 6500 | choic->dflt = resolve_choice_dflt(choic, expr); |
| 6501 | } |
Michal Vasko | 7955b36 | 2015-09-04 14:18:15 +0200 | [diff] [blame] | 6502 | if (choic->dflt) { |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 6503 | rc = lyp_check_mandatory_choice((struct lys_node *)choic); |
Michal Vasko | 7955b36 | 2015-09-04 14:18:15 +0200 | [diff] [blame] | 6504 | } else { |
| 6505 | rc = EXIT_FAILURE; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6506 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6507 | break; |
| 6508 | case UNRES_LIST_KEYS: |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 6509 | rc = resolve_list_keys(item, ((struct lys_node_list *)item)->keys_str); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6510 | break; |
| 6511 | case UNRES_LIST_UNIQ: |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6512 | unique_info = (struct unres_list_uniq *)item; |
| 6513 | 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] | 6514 | break; |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 6515 | case UNRES_AUGMENT: |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 6516 | rc = resolve_augment(item, NULL, unres); |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 6517 | break; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6518 | case UNRES_XPATH: |
| 6519 | node = (struct lys_node *)item; |
Michal Vasko | 769f803 | 2017-01-24 13:11:55 +0100 | [diff] [blame] | 6520 | rc = lys_check_xpath(node, 1, final_fail); |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6521 | break; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6522 | case UNRES_EXT: |
| 6523 | ext_data = (struct unres_ext *)str_snode; |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 6524 | extlist = &(*(struct lys_ext_instance ***)item)[ext_data->ext_index]; |
Radek Krejci | a7db970 | 2017-01-20 12:55:14 +0100 | [diff] [blame] | 6525 | rc = resolve_extension(ext_data, extlist, unres); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6526 | if (!rc) { |
Radek Krejci | 79685c9 | 2017-02-17 10:49:43 +0100 | [diff] [blame] | 6527 | /* success */ |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6528 | /* is there a callback to be done to finalize the extension? */ |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 6529 | eplugin = extlist[0]->def->plugin; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6530 | if (eplugin) { |
| 6531 | if (eplugin->check_result || (eplugin->flags & LYEXT_OPT_INHERIT)) { |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 6532 | u = malloc(sizeof *u); |
| 6533 | (*u) = ext_data->ext_index; |
Radek Krejci | b08bc17 | 2017-02-27 13:17:14 +0100 | [diff] [blame] | 6534 | if (unres_schema_add_node(mod, unres, item, UNRES_EXT_FINALIZE, (struct lys_node *)u) == -1) { |
| 6535 | /* something really bad happend since the extension finalization is not actually |
| 6536 | * being resolved while adding into unres, so something more serious with the unres |
| 6537 | * list itself must happened */ |
| 6538 | return -1; |
| 6539 | } |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6540 | } |
| 6541 | } |
Radek Krejci | 79685c9 | 2017-02-17 10:49:43 +0100 | [diff] [blame] | 6542 | } |
| 6543 | if (!rc || rc == -1) { |
| 6544 | /* cleanup on success or fatal error */ |
| 6545 | if (ext_data->datatype == LYS_IN_YIN) { |
| 6546 | /* YIN */ |
| 6547 | lyxml_free(mod->ctx, ext_data->data.yin); |
| 6548 | } else { |
PavolVican | db0e817 | 2017-02-20 00:46:09 +0100 | [diff] [blame] | 6549 | /* YANG */ |
| 6550 | yang_free_ext_data(ext_data->data.yang); |
Radek Krejci | 79685c9 | 2017-02-17 10:49:43 +0100 | [diff] [blame] | 6551 | } |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 6552 | free(ext_data); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6553 | } |
| 6554 | break; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6555 | case UNRES_EXT_FINALIZE: |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 6556 | u = (uint8_t *)str_snode; |
| 6557 | ext = (*(struct lys_ext_instance ***)item)[*u]; |
| 6558 | free(u); |
| 6559 | |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6560 | eplugin = ext->def->plugin; |
| 6561 | |
| 6562 | /* inherit */ |
| 6563 | if ((eplugin->flags & LYEXT_OPT_INHERIT) && (ext->parent_type == LYEXT_PAR_NODE)) { |
| 6564 | root = (struct lys_node *)ext->parent; |
| 6565 | if (!(root->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) { |
| 6566 | LY_TREE_DFS_BEGIN(root->child, next, node) { |
| 6567 | /* first, check if the node already contain instance of the same extension, |
| 6568 | * in such a case we won't inherit. In case the node was actually defined as |
| 6569 | * augment data, we are supposed to check the same way also the augment node itself */ |
| 6570 | if (lys_ext_instance_presence(ext->def, node->ext, node->ext_size) != -1) { |
| 6571 | goto inherit_dfs_sibling; |
| 6572 | } else if (node->parent != root && node->parent->nodetype == LYS_AUGMENT && |
| 6573 | lys_ext_instance_presence(ext->def, node->parent->ext, node->parent->ext_size) != -1) { |
| 6574 | goto inherit_dfs_sibling; |
| 6575 | } |
| 6576 | |
| 6577 | if (eplugin->check_inherit) { |
| 6578 | /* we have a callback to check the inheritance, use it */ |
| 6579 | switch ((rc = (*eplugin->check_inherit)(ext, node))) { |
| 6580 | case 0: |
| 6581 | /* yes - continue with the inheriting code */ |
| 6582 | break; |
| 6583 | case 1: |
| 6584 | /* no - continue with the node's sibling */ |
| 6585 | goto inherit_dfs_sibling; |
| 6586 | case 2: |
| 6587 | /* no, but continue with the children, just skip the inheriting code for this node */ |
| 6588 | goto inherit_dfs_child; |
| 6589 | default: |
| 6590 | LOGERR(LY_EINT, "Plugin's (%s:%s) check_inherit callback returns invalid value (%d),", |
| 6591 | ext->def->module->name, ext->def->name, rc); |
| 6592 | } |
| 6593 | } |
| 6594 | |
| 6595 | /* inherit the extension */ |
| 6596 | extlist = realloc(node->ext, (node->ext_size + 1) * sizeof *node->ext); |
| 6597 | if (!extlist) { |
| 6598 | LOGMEM; |
| 6599 | return -1; |
| 6600 | } |
| 6601 | extlist[node->ext_size] = malloc(sizeof **extlist); |
| 6602 | if (!extlist[node->ext_size]) { |
| 6603 | LOGMEM; |
| 6604 | node->ext = extlist; |
| 6605 | return -1; |
| 6606 | } |
| 6607 | memcpy(extlist[node->ext_size], ext, sizeof *ext); |
| 6608 | extlist[node->ext_size]->flags |= LYEXT_OPT_INHERIT; |
| 6609 | |
| 6610 | node->ext = extlist; |
| 6611 | node->ext_size++; |
| 6612 | |
| 6613 | inherit_dfs_child: |
| 6614 | /* modification of - select element for the next run - children first */ |
| 6615 | if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
| 6616 | next = NULL; |
| 6617 | } else { |
| 6618 | next = node->child; |
| 6619 | } |
| 6620 | if (!next) { |
| 6621 | inherit_dfs_sibling: |
| 6622 | /* no children, try siblings */ |
| 6623 | next = node->next; |
| 6624 | } |
| 6625 | while (!next) { |
| 6626 | /* go to the parent */ |
| 6627 | node = lys_parent(node); |
| 6628 | |
| 6629 | /* we are done if we are back in the root (the starter's parent */ |
| 6630 | if (node == root) { |
| 6631 | break; |
| 6632 | } |
| 6633 | |
| 6634 | /* parent is already processed, go to its sibling */ |
| 6635 | next = node->next; |
| 6636 | } |
| 6637 | } |
| 6638 | } |
| 6639 | } |
| 6640 | |
| 6641 | /* final check */ |
| 6642 | if (eplugin->check_result) { |
| 6643 | if ((*eplugin->check_result)(ext)) { |
Radek Krejci | 2c121b3 | 2017-02-24 10:03:16 +0100 | [diff] [blame] | 6644 | ly_errno = LY_EEXT; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6645 | return -1; |
| 6646 | } |
| 6647 | } |
| 6648 | |
| 6649 | rc = 0; |
| 6650 | break; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6651 | default: |
| 6652 | LOGINT; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6653 | break; |
| 6654 | } |
| 6655 | |
Radek Krejci | 54081ce | 2016-08-12 15:21:47 +0200 | [diff] [blame] | 6656 | if (has_str && !rc) { |
| 6657 | /* the string is no more needed in case of success. |
| 6658 | * 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] | 6659 | lydict_remove(mod->ctx, str_snode); |
| 6660 | } |
| 6661 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6662 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6663 | } |
| 6664 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6665 | /* logs directly */ |
| 6666 | static void |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6667 | 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] | 6668 | { |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 6669 | struct lyxml_elem *xml; |
| 6670 | struct lyxml_attr *attr; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6671 | struct unres_iffeat_data *iff_data; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6672 | const char *name = NULL; |
| 6673 | struct unres_ext *extinfo; |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 6674 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6675 | switch (type) { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6676 | case UNRES_IDENT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6677 | 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] | 6678 | break; |
| 6679 | case UNRES_TYPE_IDENTREF: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6680 | 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] | 6681 | break; |
| 6682 | case UNRES_TYPE_LEAFREF: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6683 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "leafref", |
| 6684 | ((struct lys_type *)item)->info.lref.path); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6685 | break; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 6686 | case UNRES_TYPE_DER_EXT: |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 6687 | case UNRES_TYPE_DER_TPDF: |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6688 | case UNRES_TYPE_DER: |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 6689 | xml = (struct lyxml_elem *)((struct lys_type *)item)->der; |
| 6690 | if (xml->flags & LY_YANG_STRUCTURE_FLAG) { |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6691 | name = ((struct yang_type *)xml)->name; |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 6692 | } else { |
| 6693 | LY_TREE_FOR(xml->attr, attr) { |
| 6694 | if ((attr->type == LYXML_ATTR_STD) && !strcmp(attr->name, "name")) { |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6695 | name = attr->value; |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 6696 | break; |
| 6697 | } |
| 6698 | } |
| 6699 | assert(attr); |
| 6700 | } |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6701 | 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] | 6702 | break; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6703 | case UNRES_IFFEAT: |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6704 | iff_data = str_node; |
| 6705 | 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] | 6706 | break; |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6707 | case UNRES_FEATURE: |
| 6708 | LOGVRB("There are unresolved if-features for \"%s\" feature circular dependency check, it will be attempted later", |
| 6709 | ((struct lys_feature *)item)->name); |
| 6710 | break; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6711 | case UNRES_USES: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6712 | 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] | 6713 | break; |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 6714 | case UNRES_TYPEDEF_DFLT: |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6715 | case UNRES_TYPE_DFLT: |
Radek Krejci | 2e2de83 | 2016-10-13 16:12:26 +0200 | [diff] [blame] | 6716 | if (str_node) { |
| 6717 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "type default", (char *)str_node); |
| 6718 | } /* else no default value in the type itself, but we are checking some restrictions against |
| 6719 | * possible default value of some base type. The failure is caused by not resolved base type, |
| 6720 | * so it was already reported */ |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6721 | break; |
| 6722 | case UNRES_CHOICE_DFLT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6723 | 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] | 6724 | break; |
| 6725 | case UNRES_LIST_KEYS: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6726 | 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] | 6727 | break; |
| 6728 | case UNRES_LIST_UNIQ: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6729 | 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] | 6730 | break; |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 6731 | case UNRES_AUGMENT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6732 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "augment target", |
| 6733 | ((struct lys_node_augment *)item)->target_name); |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 6734 | break; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6735 | case UNRES_XPATH: |
Michal Vasko | 0d19837 | 2016-11-16 11:40:03 +0100 | [diff] [blame] | 6736 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "XPath expressions of", |
| 6737 | ((struct lys_node *)item)->name); |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6738 | break; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6739 | case UNRES_EXT: |
| 6740 | extinfo = (struct unres_ext *)str_node; |
| 6741 | name = extinfo->datatype == LYS_IN_YIN ? extinfo->data.yin->name : NULL; /* TODO YANG extension */ |
| 6742 | LOGVRB("Resolving extension \"%s\" failed, it will be attempted later.", name); |
| 6743 | break; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6744 | default: |
| 6745 | LOGINT; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6746 | break; |
| 6747 | } |
| 6748 | } |
| 6749 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6750 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6751 | * @brief Resolve every unres schema item in the structure. Logs directly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6752 | * |
| 6753 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6754 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6755 | * |
Michal Vasko | 92b8a38 | 2015-08-19 14:03:49 +0200 | [diff] [blame] | 6756 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6757 | */ |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6758 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 6759 | resolve_unres_schema(struct lys_module *mod, struct unres_schema *unres) |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6760 | { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6761 | uint32_t i, resolved = 0, unres_count, res_count; |
PavolVican | a0fdbf3 | 2017-02-15 17:59:02 +0100 | [diff] [blame] | 6762 | struct lyxml_elem *yin; |
| 6763 | struct yang_type *yang; |
Michal Vasko | 74a60c0 | 2017-03-08 10:19:48 +0100 | [diff] [blame] | 6764 | int rc, log_hidden; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6765 | |
| 6766 | assert(unres); |
| 6767 | |
Michal Vasko | e873426 | 2016-09-29 14:12:06 +0200 | [diff] [blame] | 6768 | LOGVRB("Resolving \"%s\" unresolved schema nodes and their constraints...", mod->name); |
Michal Vasko | 74a60c0 | 2017-03-08 10:19:48 +0100 | [diff] [blame] | 6769 | if (*ly_vlog_hide_location()) { |
| 6770 | log_hidden = 1; |
| 6771 | } else { |
| 6772 | log_hidden = 0; |
| 6773 | ly_vlog_hide(1); |
| 6774 | } |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6775 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6776 | /* uses */ |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6777 | do { |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6778 | unres_count = 0; |
| 6779 | res_count = 0; |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6780 | |
| 6781 | for (i = 0; i < unres->count; ++i) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6782 | /* UNRES_TYPE_LEAFREF must be resolved (for storing leafref target pointers); |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6783 | * if-features are resolved here to make sure that we will have all if-features for |
| 6784 | * later check of feature circular dependency */ |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6785 | if (unres->type[i] > UNRES_IDENT) { |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6786 | continue; |
| 6787 | } |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6788 | /* 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] | 6789 | * UNRES_AUGMENT, UNRES_CHOICE_DFLT and UNRES_IDENT */ |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6790 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6791 | ++unres_count; |
Radek Krejci | cd9a95a | 2017-03-25 12:02:35 -0500 | [diff] [blame] | 6792 | 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] | 6793 | if (!rc) { |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6794 | unres->type[i] = UNRES_RESOLVED; |
| 6795 | ++resolved; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6796 | ++res_count; |
Michal Vasko | 89e1532 | 2015-08-17 15:46:55 +0200 | [diff] [blame] | 6797 | } else if (rc == -1) { |
Michal Vasko | 74a60c0 | 2017-03-08 10:19:48 +0100 | [diff] [blame] | 6798 | if (!log_hidden) { |
| 6799 | ly_vlog_hide(0); |
| 6800 | } |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6801 | /* print the error */ |
Radek Krejci | 791f6c7 | 2017-02-22 15:23:39 +0100 | [diff] [blame] | 6802 | ly_err_repeat(); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6803 | return -1; |
Radek Krejci | c2a180f | 2016-06-22 13:28:16 +0200 | [diff] [blame] | 6804 | } else { |
| 6805 | /* forward reference, erase ly_errno */ |
Radek Krejci | 00a0e71 | 2016-10-26 10:24:46 +0200 | [diff] [blame] | 6806 | ly_err_clean(1); |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6807 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6808 | } |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6809 | } while (res_count && (res_count < unres_count)); |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6810 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6811 | if (res_count < unres_count) { |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6812 | /* just print the errors */ |
Michal Vasko | 74a60c0 | 2017-03-08 10:19:48 +0100 | [diff] [blame] | 6813 | if (!log_hidden) { |
| 6814 | ly_vlog_hide(0); |
| 6815 | } |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6816 | |
| 6817 | for (i = 0; i < unres->count; ++i) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6818 | if (unres->type[i] > UNRES_IDENT) { |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6819 | continue; |
| 6820 | } |
Radek Krejci | cd9a95a | 2017-03-25 12:02:35 -0500 | [diff] [blame] | 6821 | 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] | 6822 | if (unres->type[i] == UNRES_TYPE_DER_EXT) { |
PavolVican | a0fdbf3 | 2017-02-15 17:59:02 +0100 | [diff] [blame] | 6823 | yin = (struct lyxml_elem*)((struct lys_type *)unres->item[i])->der; |
| 6824 | if (yin->flags & LY_YANG_STRUCTURE_FLAG) { |
| 6825 | yang =(struct yang_type *)yin; |
| 6826 | ((struct lys_type *)unres->item[i])->base = yang->base; |
| 6827 | if (yang->base == LY_TYPE_UNION) { |
| 6828 | yang_free_type_union(mod->ctx, (struct lys_type *)unres->item[i]); |
| 6829 | } |
| 6830 | lydict_remove(mod->ctx, yang->name); |
| 6831 | free(yang); |
| 6832 | } else { |
| 6833 | lyxml_free(mod->ctx, yin); |
| 6834 | } |
Radek Krejci | 63fc096 | 2017-02-15 13:20:18 +0100 | [diff] [blame] | 6835 | } |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6836 | } |
Michal Vasko | 92b8a38 | 2015-08-19 14:03:49 +0200 | [diff] [blame] | 6837 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6838 | } |
| 6839 | |
Radek Krejci | 07d0fb9 | 2017-01-13 14:11:05 +0100 | [diff] [blame] | 6840 | /* the rest except finalizing extensions */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6841 | for (i = 0; i < unres->count; ++i) { |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6842 | if (unres->type[i] == UNRES_RESOLVED || unres->type[i] == UNRES_EXT_FINALIZE) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6843 | continue; |
| 6844 | } |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 6845 | |
Radek Krejci | cd9a95a | 2017-03-25 12:02:35 -0500 | [diff] [blame] | 6846 | 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] | 6847 | if (rc == 0) { |
Pavol Vican | 88e16c9 | 2016-09-07 15:41:50 +0200 | [diff] [blame] | 6848 | if (unres->type[i] == UNRES_LIST_UNIQ) { |
| 6849 | /* free the allocated structure */ |
| 6850 | free(unres->item[i]); |
| 6851 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6852 | unres->type[i] = UNRES_RESOLVED; |
| 6853 | ++resolved; |
| 6854 | } else if (rc == -1) { |
Michal Vasko | 74a60c0 | 2017-03-08 10:19:48 +0100 | [diff] [blame] | 6855 | if (!log_hidden) { |
| 6856 | ly_vlog_hide(0); |
| 6857 | } |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6858 | /* print the error */ |
Radek Krejci | 791f6c7 | 2017-02-22 15:23:39 +0100 | [diff] [blame] | 6859 | ly_err_repeat(); |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6860 | return -1; |
Radek Krejci | 791f6c7 | 2017-02-22 15:23:39 +0100 | [diff] [blame] | 6861 | } else { |
| 6862 | /* forward reference, erase ly_errno */ |
| 6863 | ly_err_clean(1); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6864 | } |
| 6865 | } |
| 6866 | |
Michal Vasko | 74a60c0 | 2017-03-08 10:19:48 +0100 | [diff] [blame] | 6867 | if (!log_hidden) { |
| 6868 | ly_vlog_hide(0); |
| 6869 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6870 | |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6871 | /* finalize extensions, keep it last to provide the complete schema tree information to the plugin's checkers */ |
| 6872 | for (i = 0; i < unres->count; ++i) { |
| 6873 | if (unres->type[i] != UNRES_EXT_FINALIZE) { |
| 6874 | continue; |
| 6875 | } |
| 6876 | |
Radek Krejci | cd9a95a | 2017-03-25 12:02:35 -0500 | [diff] [blame] | 6877 | 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] | 6878 | unres->type[i] = UNRES_RESOLVED; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6879 | if (rc == 0) { |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6880 | ++resolved; |
| 6881 | } |
Radek Krejci | 791f6c7 | 2017-02-22 15:23:39 +0100 | [diff] [blame] | 6882 | /* else error - it was already printed, but resolved was not increased, |
| 6883 | so this unres item will not be resolved again in the following code, |
| 6884 | but it will cause returning -1 at the end, this way we are able to |
| 6885 | print all the issues with unres */ |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6886 | } |
| 6887 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6888 | if (resolved < unres->count) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6889 | /* try to resolve the unresolved nodes again, it will not resolve anything, but it will print |
| 6890 | * all the validation errors |
| 6891 | */ |
| 6892 | for (i = 0; i < unres->count; ++i) { |
| 6893 | if (unres->type[i] == UNRES_RESOLVED) { |
| 6894 | continue; |
| 6895 | } |
Radek Krejci | cd9a95a | 2017-03-25 12:02:35 -0500 | [diff] [blame] | 6896 | 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] | 6897 | if (unres->type[i] == UNRES_XPATH) { |
Michal Vasko | 769f803 | 2017-01-24 13:11:55 +0100 | [diff] [blame] | 6898 | /* 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] | 6899 | unres->type[i] = UNRES_RESOLVED; |
| 6900 | resolved++; |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 6901 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6902 | } |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 6903 | if (resolved < unres->count) { |
| 6904 | return -1; |
| 6905 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6906 | } |
| 6907 | |
Michal Vasko | e873426 | 2016-09-29 14:12:06 +0200 | [diff] [blame] | 6908 | LOGVRB("All \"%s\" schema nodes and constraints resolved.", mod->name); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6909 | unres->count = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6910 | return EXIT_SUCCESS; |
| 6911 | } |
| 6912 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6913 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6914 | * @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] | 6915 | * |
| 6916 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6917 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6918 | * @param[in] item Item to resolve. Type determined by \p type. |
| 6919 | * @param[in] type Type of the unresolved item. |
| 6920 | * @param[in] str String argument. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6921 | * |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 6922 | * @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] | 6923 | */ |
| 6924 | int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6925 | unres_schema_add_str(struct lys_module *mod, struct unres_schema *unres, void *item, enum UNRES_ITEM type, |
| 6926 | const char *str) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6927 | { |
Radek Krejci | 54081ce | 2016-08-12 15:21:47 +0200 | [diff] [blame] | 6928 | int rc; |
| 6929 | const char *dictstr; |
| 6930 | |
| 6931 | dictstr = lydict_insert(mod->ctx, str, 0); |
| 6932 | rc = unres_schema_add_node(mod, unres, item, type, (struct lys_node *)dictstr); |
| 6933 | |
Radek Krejci | d9c0ce2 | 2017-01-20 15:20:16 +0100 | [diff] [blame] | 6934 | if (rc < 0) { |
Radek Krejci | 54081ce | 2016-08-12 15:21:47 +0200 | [diff] [blame] | 6935 | lydict_remove(mod->ctx, dictstr); |
| 6936 | } |
| 6937 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6938 | } |
| 6939 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6940 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6941 | * @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] | 6942 | * |
| 6943 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6944 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6945 | * @param[in] item Item to resolve. Type determined by \p type. |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6946 | * @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] | 6947 | * @param[in] snode Schema node argument. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6948 | * |
Radek Krejci | d9c0ce2 | 2017-01-20 15:20:16 +0100 | [diff] [blame] | 6949 | * @return EXIT_SUCCESS on success, EXIT_FIALURE on storing the item in unres, -1 on error, -2 if the unres item |
| 6950 | * is already in the unres list. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6951 | */ |
| 6952 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 6953 | 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] | 6954 | struct lys_node *snode) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6955 | { |
Michal Vasko | ef486d7 | 2016-09-27 12:10:44 +0200 | [diff] [blame] | 6956 | int rc, log_hidden; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6957 | struct lyxml_elem *yin; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6958 | |
Michal Vasko | 9bf425b | 2015-10-22 11:42:03 +0200 | [diff] [blame] | 6959 | assert(unres && item && ((type != UNRES_LEAFREF) && (type != UNRES_INSTID) && (type != UNRES_WHEN) |
| 6960 | && (type != UNRES_MUST))); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6961 | |
Michal Vasko | 9e862e8 | 2017-03-08 10:20:49 +0100 | [diff] [blame] | 6962 | #ifndef NDEBUG |
Radek Krejci | df056df | 2017-03-09 13:24:45 +0100 | [diff] [blame] | 6963 | uint32_t u; |
| 6964 | |
Radek Krejci | 850a5de | 2016-11-08 14:06:40 +0100 | [diff] [blame] | 6965 | /* check for duplicities in unres */ |
| 6966 | for (u = 0; u < unres->count; u++) { |
| 6967 | if (unres->type[u] == type && unres->item[u] == item && |
| 6968 | unres->str_snode[u] == snode && unres->module[u] == mod) { |
Michal Vasko | 9e862e8 | 2017-03-08 10:20:49 +0100 | [diff] [blame] | 6969 | /* duplication, should not happen */ |
| 6970 | assert(0); |
Radek Krejci | 850a5de | 2016-11-08 14:06:40 +0100 | [diff] [blame] | 6971 | } |
| 6972 | } |
Michal Vasko | 9e862e8 | 2017-03-08 10:20:49 +0100 | [diff] [blame] | 6973 | #endif |
Radek Krejci | 850a5de | 2016-11-08 14:06:40 +0100 | [diff] [blame] | 6974 | |
Radek Krejci | c293bac | 2017-02-27 11:25:28 +0100 | [diff] [blame] | 6975 | if (type == UNRES_EXT_FINALIZE) { |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6976 | /* 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] | 6977 | rc = EXIT_FAILURE; |
| 6978 | } else { |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6979 | if (*ly_vlog_hide_location()) { |
| 6980 | log_hidden = 1; |
| 6981 | } else { |
| 6982 | log_hidden = 0; |
| 6983 | ly_vlog_hide(1); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6984 | } |
Radek Krejci | cbba57c | 2017-01-24 13:43:20 +0100 | [diff] [blame] | 6985 | rc = resolve_unres_schema_item(mod, item, type, snode, unres, 0); |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6986 | if (!log_hidden) { |
| 6987 | ly_vlog_hide(0); |
| 6988 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6989 | |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6990 | if (rc != EXIT_FAILURE) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 6991 | if (rc == -1) { |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6992 | ly_err_repeat(); |
| 6993 | } |
| 6994 | if (type == UNRES_LIST_UNIQ) { |
| 6995 | /* free the allocated structure */ |
| 6996 | free(item); |
| 6997 | } else if (rc == -1 && type == UNRES_IFFEAT) { |
| 6998 | /* free the allocated resources */ |
| 6999 | free(*((char **)item)); |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 7000 | } |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7001 | return rc; |
| 7002 | } else { |
| 7003 | /* erase info about validation errors */ |
| 7004 | ly_err_clean(1); |
| 7005 | } |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7006 | |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7007 | print_unres_schema_item_fail(item, type, snode); |
| 7008 | |
| 7009 | /* HACK unlinking is performed here so that we do not do any (NS) copying in vain */ |
| 7010 | if (type == UNRES_TYPE_DER || type == UNRES_TYPE_DER_TPDF) { |
| 7011 | yin = (struct lyxml_elem *)((struct lys_type *)item)->der; |
| 7012 | if (!(yin->flags & LY_YANG_STRUCTURE_FLAG)) { |
| 7013 | lyxml_unlink_elem(mod->ctx, yin, 1); |
| 7014 | ((struct lys_type *)item)->der = (struct lys_tpdf *)yin; |
| 7015 | } |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 7016 | } |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7017 | } |
| 7018 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7019 | unres->count++; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 7020 | unres->item = ly_realloc(unres->item, unres->count*sizeof *unres->item); |
| 7021 | if (!unres->item) { |
| 7022 | LOGMEM; |
| 7023 | return -1; |
| 7024 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7025 | unres->item[unres->count-1] = item; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 7026 | unres->type = ly_realloc(unres->type, unres->count*sizeof *unres->type); |
| 7027 | if (!unres->type) { |
| 7028 | LOGMEM; |
| 7029 | return -1; |
| 7030 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7031 | unres->type[unres->count-1] = type; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 7032 | unres->str_snode = ly_realloc(unres->str_snode, unres->count*sizeof *unres->str_snode); |
| 7033 | if (!unres->str_snode) { |
| 7034 | LOGMEM; |
| 7035 | return -1; |
| 7036 | } |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7037 | unres->str_snode[unres->count-1] = snode; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7038 | unres->module = ly_realloc(unres->module, unres->count*sizeof *unres->module); |
| 7039 | if (!unres->module) { |
| 7040 | LOGMEM; |
| 7041 | return -1; |
| 7042 | } |
| 7043 | unres->module[unres->count-1] = mod; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7044 | |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 7045 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7046 | } |
| 7047 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7048 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 7049 | * @brief Duplicate an unres schema item. Logs indirectly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7050 | * |
| 7051 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 7052 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7053 | * @param[in] item Old item to be resolved. |
| 7054 | * @param[in] type Type of the old unresolved item. |
| 7055 | * @param[in] new_item New item to use in the duplicate. |
| 7056 | * |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 7057 | * @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] | 7058 | */ |
Michal Vasko | dad1940 | 2015-08-06 09:51:53 +0200 | [diff] [blame] | 7059 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 7060 | 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] | 7061 | { |
| 7062 | int i; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7063 | struct unres_list_uniq aux_uniq; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7064 | struct unres_iffeat_data *iff_data; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7065 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7066 | 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] | 7067 | |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7068 | /* hack for UNRES_LIST_UNIQ, which stores multiple items behind its item */ |
| 7069 | if (type == UNRES_LIST_UNIQ) { |
| 7070 | aux_uniq.list = item; |
| 7071 | aux_uniq.expr = ((struct unres_list_uniq *)new_item)->expr; |
| 7072 | item = &aux_uniq; |
| 7073 | } |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7074 | i = unres_schema_find(unres, -1, item, type); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7075 | |
| 7076 | if (i == -1) { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7077 | if (type == UNRES_LIST_UNIQ) { |
| 7078 | free(new_item); |
| 7079 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 7080 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7081 | } |
| 7082 | |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 7083 | if ((type == UNRES_TYPE_LEAFREF) || (type == UNRES_USES) || (type == UNRES_TYPE_DFLT) || |
Radek Krejci | b69f323 | 2016-09-16 17:41:07 +0200 | [diff] [blame] | 7084 | (type == UNRES_FEATURE) || (type == UNRES_LIST_UNIQ)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7085 | 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] | 7086 | LOGINT; |
| 7087 | return -1; |
| 7088 | } |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7089 | } else if (type == UNRES_IFFEAT) { |
| 7090 | /* duplicate unres_iffeature_data */ |
| 7091 | iff_data = malloc(sizeof *iff_data); |
| 7092 | iff_data->fname = lydict_insert(mod->ctx, ((struct unres_iffeat_data *)unres->str_snode[i])->fname, 0); |
| 7093 | iff_data->node = ((struct unres_iffeat_data *)unres->str_snode[i])->node; |
| 7094 | if (unres_schema_add_node(mod, unres, new_item, type, (struct lys_node *)iff_data) == -1) { |
| 7095 | LOGINT; |
| 7096 | return -1; |
| 7097 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7098 | } else { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7099 | 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] | 7100 | LOGINT; |
| 7101 | return -1; |
| 7102 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7103 | } |
Michal Vasko | dad1940 | 2015-08-06 09:51:53 +0200 | [diff] [blame] | 7104 | |
| 7105 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7106 | } |
| 7107 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7108 | /* does not log */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7109 | int |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7110 | 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] | 7111 | { |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7112 | int i; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7113 | struct unres_list_uniq *aux_uniq1, *aux_uniq2; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7114 | |
Radek Krejci | ddddd0d | 2017-01-20 15:20:46 +0100 | [diff] [blame] | 7115 | if (start_on_backwards >= 0) { |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7116 | i = start_on_backwards; |
| 7117 | } else { |
| 7118 | i = unres->count - 1; |
| 7119 | } |
| 7120 | for (; i > -1; i--) { |
| 7121 | if (unres->type[i] != type) { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7122 | continue; |
| 7123 | } |
| 7124 | if (type != UNRES_LIST_UNIQ) { |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7125 | if (unres->item[i] == item) { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7126 | break; |
| 7127 | } |
| 7128 | } else { |
| 7129 | aux_uniq1 = (struct unres_list_uniq *)unres->item[i - 1]; |
| 7130 | aux_uniq2 = (struct unres_list_uniq *)item; |
| 7131 | 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] | 7132 | break; |
| 7133 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7134 | } |
| 7135 | } |
| 7136 | |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7137 | return i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7138 | } |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7139 | |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7140 | static void |
| 7141 | unres_schema_free_item(struct ly_ctx *ctx, struct unres_schema *unres, uint32_t i) |
| 7142 | { |
| 7143 | struct lyxml_elem *yin; |
| 7144 | struct yang_type *yang; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7145 | struct unres_iffeat_data *iff_data; |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7146 | |
| 7147 | switch (unres->type[i]) { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 7148 | case UNRES_TYPE_DER_TPDF: |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7149 | case UNRES_TYPE_DER: |
| 7150 | yin = (struct lyxml_elem *)((struct lys_type *)unres->item[i])->der; |
| 7151 | if (yin->flags & LY_YANG_STRUCTURE_FLAG) { |
| 7152 | yang =(struct yang_type *)yin; |
Pavol Vican | cf2af4d | 2016-12-21 14:13:06 +0100 | [diff] [blame] | 7153 | ((struct lys_type *)unres->item[i])->base = yang->base; |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7154 | lydict_remove(ctx, yang->name); |
| 7155 | free(yang); |
Pavol Vican | cf2af4d | 2016-12-21 14:13:06 +0100 | [diff] [blame] | 7156 | if (((struct lys_type *)unres->item[i])->base == LY_TYPE_UNION) { |
| 7157 | yang_free_type_union(ctx, (struct lys_type *)unres->item[i]); |
| 7158 | } |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7159 | } else { |
| 7160 | lyxml_free(ctx, yin); |
| 7161 | } |
| 7162 | break; |
Pavol Vican | 88e16c9 | 2016-09-07 15:41:50 +0200 | [diff] [blame] | 7163 | case UNRES_IFFEAT: |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7164 | iff_data = (struct unres_iffeat_data *)unres->str_snode[i]; |
| 7165 | lydict_remove(ctx, iff_data->fname); |
| 7166 | free(unres->str_snode[i]); |
Pavol Vican | 88e16c9 | 2016-09-07 15:41:50 +0200 | [diff] [blame] | 7167 | break; |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7168 | case UNRES_IDENT: |
| 7169 | case UNRES_TYPE_IDENTREF: |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7170 | case UNRES_CHOICE_DFLT: |
| 7171 | case UNRES_LIST_KEYS: |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7172 | lydict_remove(ctx, (const char *)unres->str_snode[i]); |
| 7173 | break; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7174 | case UNRES_LIST_UNIQ: |
| 7175 | free(unres->item[i]); |
| 7176 | break; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 7177 | case UNRES_EXT: |
| 7178 | free(unres->str_snode[i]); |
| 7179 | break; |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7180 | default: |
| 7181 | break; |
| 7182 | } |
| 7183 | unres->type[i] = UNRES_RESOLVED; |
| 7184 | } |
| 7185 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7186 | void |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 7187 | 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] | 7188 | { |
| 7189 | uint32_t i; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7190 | unsigned int unresolved = 0; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7191 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7192 | if (!unres || !(*unres)) { |
| 7193 | return; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7194 | } |
| 7195 | |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 7196 | assert(module || ((*unres)->count == 0)); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7197 | |
| 7198 | for (i = 0; i < (*unres)->count; ++i) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 7199 | if (!all && ((*unres)->module[i] != module)) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7200 | if ((*unres)->type[i] != UNRES_RESOLVED) { |
| 7201 | unresolved++; |
| 7202 | } |
| 7203 | continue; |
| 7204 | } |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7205 | |
| 7206 | /* free heap memory for the specific item */ |
| 7207 | unres_schema_free_item(module->ctx, *unres, i); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7208 | } |
| 7209 | |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7210 | /* free it all */ |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 7211 | if (!module || all || (!unresolved && !module->type)) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7212 | free((*unres)->item); |
| 7213 | free((*unres)->type); |
| 7214 | free((*unres)->str_snode); |
| 7215 | free((*unres)->module); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7216 | free((*unres)); |
| 7217 | (*unres) = NULL; |
| 7218 | } |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7219 | } |
| 7220 | |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7221 | static int |
| 7222 | check_instid_ext_dep(const struct lys_node *sleaf, const char *json_instid) |
| 7223 | { |
| 7224 | struct ly_set *set; |
Michal Vasko | 3c77709 | 2017-01-17 14:10:09 +0100 | [diff] [blame] | 7225 | struct lys_node *op_node, *first_node; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7226 | char *buf; |
| 7227 | |
| 7228 | for (op_node = lys_parent(sleaf); |
| 7229 | op_node && !(op_node->nodetype & (LYS_NOTIF | LYS_RPC | LYS_ACTION)); |
| 7230 | op_node = lys_parent(op_node)); |
| 7231 | |
| 7232 | if (op_node && lys_parent(op_node)) { |
| 7233 | /* nested operation - any absolute path is external */ |
| 7234 | return 1; |
| 7235 | } |
| 7236 | |
| 7237 | /* get the first node from the instid */ |
| 7238 | buf = strndup(json_instid, strchr(json_instid + 1, '/') - json_instid); |
| 7239 | if (!buf) { |
| 7240 | LOGMEM; |
| 7241 | return -1; |
| 7242 | } |
| 7243 | |
| 7244 | /* there is a predicate, remove it */ |
| 7245 | if (buf[strlen(buf) - 1] == ']') { |
| 7246 | assert(strchr(buf, '[')); |
| 7247 | *strchr(buf, '[') = '\0'; |
| 7248 | } |
| 7249 | |
| 7250 | /* find the first schema node */ |
Michal Vasko | 2611e19 | 2017-01-23 10:33:21 +0100 | [diff] [blame] | 7251 | set = lys_find_xpath(NULL, sleaf, buf, 0); |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7252 | if (!set || !set->number) { |
| 7253 | free(buf); |
Michal Vasko | 29fd974 | 2017-01-23 09:55:44 +0100 | [diff] [blame] | 7254 | ly_set_free(set); |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7255 | return 1; |
| 7256 | } |
| 7257 | free(buf); |
| 7258 | |
Michal Vasko | 3c77709 | 2017-01-17 14:10:09 +0100 | [diff] [blame] | 7259 | first_node = set->set.s[0]; |
| 7260 | ly_set_free(set); |
| 7261 | |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7262 | /* based on the first schema node in the path we can decide whether it points to an external tree or not */ |
| 7263 | |
| 7264 | if (op_node) { |
| 7265 | /* it is an operation, so we're good if it points somewhere inside it */ |
Michal Vasko | 3c77709 | 2017-01-17 14:10:09 +0100 | [diff] [blame] | 7266 | if (op_node == first_node) { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7267 | assert(set->number == 1); |
| 7268 | return 0; |
| 7269 | } else { |
| 7270 | return 1; |
| 7271 | } |
| 7272 | } |
| 7273 | |
| 7274 | /* we cannot know whether it points to a tree that is going to be unlinked (application must handle |
| 7275 | * this itself), so we say it's not external */ |
| 7276 | return 0; |
| 7277 | } |
| 7278 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7279 | /** |
| 7280 | * @brief Resolve instance-identifier in JSON data format. Logs directly. |
| 7281 | * |
| 7282 | * @param[in] data Data node where the path is used |
| 7283 | * @param[in] path Instance-identifier node value. |
| 7284 | * @param[in,out] ret Resolved instance or NULL. |
| 7285 | * |
| 7286 | * @return 0 on success (even if unresolved and \p ret is NULL), -1 on error. |
| 7287 | */ |
| 7288 | static int |
| 7289 | resolve_instid(struct lyd_node *data, const char *path, int req_inst, struct lyd_node **ret) |
| 7290 | { |
| 7291 | int i = 0, j; |
| 7292 | const struct lys_module *mod; |
| 7293 | struct ly_ctx *ctx = data->schema->module->ctx; |
| 7294 | const char *model, *name; |
| 7295 | char *str; |
| 7296 | int mod_len, name_len, has_predicate; |
| 7297 | struct unres_data node_match; |
| 7298 | |
| 7299 | memset(&node_match, 0, sizeof node_match); |
| 7300 | *ret = NULL; |
| 7301 | |
| 7302 | /* we need root to resolve absolute path */ |
| 7303 | for (; data->parent; data = data->parent); |
| 7304 | /* we're still parsing it and the pointer is not correct yet */ |
| 7305 | if (data->prev) { |
| 7306 | for (; data->prev->next; data = data->prev); |
| 7307 | } |
| 7308 | |
| 7309 | /* search for the instance node */ |
| 7310 | while (path[i]) { |
| 7311 | j = parse_instance_identifier(&path[i], &model, &mod_len, &name, &name_len, &has_predicate); |
| 7312 | if (j <= 0) { |
| 7313 | LOGVAL(LYE_INCHAR, LY_VLOG_LYD, data, path[i-j], &path[i-j]); |
| 7314 | goto error; |
| 7315 | } |
| 7316 | i += j; |
| 7317 | |
| 7318 | str = strndup(model, mod_len); |
| 7319 | if (!str) { |
| 7320 | LOGMEM; |
| 7321 | goto error; |
| 7322 | } |
| 7323 | mod = ly_ctx_get_module(ctx, str, NULL); |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 7324 | if (ctx->data_clb) { |
| 7325 | if (!mod) { |
| 7326 | mod = ctx->data_clb(ctx, str, NULL, 0, ctx->data_clb_data); |
| 7327 | } else if (!mod->implemented) { |
| 7328 | mod = ctx->data_clb(ctx, mod->name, mod->ns, LY_MODCLB_NOT_IMPLEMENTED, ctx->data_clb_data); |
| 7329 | } |
| 7330 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7331 | free(str); |
| 7332 | |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 7333 | if (!mod || !mod->implemented || mod->disabled) { |
| 7334 | break; |
| 7335 | } |
| 7336 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7337 | if (resolve_data(mod, name, name_len, data, &node_match)) { |
| 7338 | /* no instance exists */ |
| 7339 | break; |
| 7340 | } |
| 7341 | |
| 7342 | if (has_predicate) { |
| 7343 | /* we have predicate, so the current results must be list or leaf-list */ |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 7344 | j = resolve_instid_predicate(&path[i], &node_match); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7345 | if (j < 1) { |
| 7346 | LOGVAL(LYE_INPRED, LY_VLOG_LYD, data, &path[i-j]); |
| 7347 | goto error; |
| 7348 | } |
| 7349 | i += j; |
| 7350 | |
| 7351 | if (!node_match.count) { |
| 7352 | /* no instance exists */ |
| 7353 | break; |
| 7354 | } |
Michal Vasko | 6f28e0f | 2017-04-18 15:14:13 +0200 | [diff] [blame] | 7355 | } else if (node_match.count) { |
| 7356 | /* check that we are not addressing lists */ |
| 7357 | for (j = 0; (unsigned)j < node_match.count; ++j) { |
| 7358 | if (node_match.node[j]->schema->nodetype == LYS_LIST) { |
| 7359 | unres_data_del(&node_match, j--); |
| 7360 | } |
| 7361 | } |
| 7362 | if (!node_match.count) { |
| 7363 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier is missing list keys."); |
| 7364 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7365 | } |
| 7366 | } |
| 7367 | |
| 7368 | if (!node_match.count) { |
| 7369 | /* no instance exists */ |
| 7370 | if (req_inst > -1) { |
| 7371 | LOGVAL(LYE_NOREQINS, LY_VLOG_NONE, NULL, path); |
| 7372 | return EXIT_FAILURE; |
| 7373 | } |
| 7374 | LOGVRB("There is no instance of \"%s\", but it is not required.", path); |
| 7375 | return EXIT_SUCCESS; |
| 7376 | } else if (node_match.count > 1) { |
| 7377 | /* instance identifier must resolve to a single node */ |
| 7378 | LOGVAL(LYE_TOOMANY, LY_VLOG_LYD, data, path, "data tree"); |
| 7379 | goto error; |
| 7380 | } else { |
| 7381 | /* we have required result, remember it and cleanup */ |
| 7382 | *ret = node_match.node[0]; |
| 7383 | free(node_match.node); |
| 7384 | return EXIT_SUCCESS; |
| 7385 | } |
| 7386 | |
| 7387 | error: |
| 7388 | /* cleanup */ |
| 7389 | free(node_match.node); |
| 7390 | return -1; |
| 7391 | } |
| 7392 | |
| 7393 | static int |
| 7394 | 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] | 7395 | { |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7396 | struct unres_data matches; |
| 7397 | uint32_t i; |
| 7398 | |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 7399 | /* init */ |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7400 | memset(&matches, 0, sizeof matches); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7401 | *ret = NULL; |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7402 | |
| 7403 | /* EXIT_FAILURE return keeps leaf->value.lefref NULL, handled later */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7404 | if (resolve_path_arg_data((struct lyd_node *)leaf, path, &matches) == -1) { |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7405 | return -1; |
| 7406 | } |
| 7407 | |
| 7408 | /* check that value matches */ |
| 7409 | for (i = 0; i < matches.count; ++i) { |
Radek Krejci | 1899d6a | 2016-11-03 13:48:07 +0100 | [diff] [blame] | 7410 | /* not that the value is already in canonical form since the parsers does the conversion, |
| 7411 | * so we can simply compare just the values */ |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7412 | if (ly_strequal(leaf->value_str, ((struct lyd_node_leaf_list *)matches.node[i])->value_str, 1)) { |
Radek Krejci | 1899d6a | 2016-11-03 13:48:07 +0100 | [diff] [blame] | 7413 | /* we have the match */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7414 | *ret = matches.node[i]; |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7415 | break; |
| 7416 | } |
| 7417 | } |
| 7418 | |
| 7419 | free(matches.node); |
| 7420 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7421 | if (!*ret) { |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7422 | /* reference not found */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7423 | if (req_inst > -1) { |
| 7424 | LOGVAL(LYE_NOLEAFREF, LY_VLOG_LYD, leaf, path, leaf->value_str); |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7425 | return EXIT_FAILURE; |
| 7426 | } else { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7427 | 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] | 7428 | } |
| 7429 | } |
| 7430 | |
| 7431 | return EXIT_SUCCESS; |
| 7432 | } |
| 7433 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7434 | /* 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] | 7435 | int |
| 7436 | resolve_union(struct lyd_node_leaf_list *leaf, struct lys_type *type, int store, int ignore_fail, |
| 7437 | struct lys_type **resolved_type) |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 7438 | { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7439 | struct lys_type *t; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7440 | struct lyd_node *ret; |
| 7441 | int found, hidden, success = 0, ext_dep, req_inst; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7442 | const char *json_val = NULL; |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 7443 | |
| 7444 | assert(type->base == LY_TYPE_UNION); |
| 7445 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7446 | if ((leaf->value_type == LY_TYPE_UNION) || (leaf->value_type == (LY_TYPE_INST | LY_TYPE_INST_UNRES))) { |
| 7447 | /* either NULL or instid previously converted to JSON */ |
| 7448 | json_val = leaf->value.string; |
| 7449 | } |
Michal Vasko | 1c8567a | 2017-01-05 13:42:27 +0100 | [diff] [blame] | 7450 | |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7451 | if (store) { |
| 7452 | if ((leaf->value_type & LY_DATA_TYPE_MASK) == LY_TYPE_BITS) { |
| 7453 | free(leaf->value.bit); |
| 7454 | } |
| 7455 | memset(&leaf->value, 0, sizeof leaf->value); |
Michal Vasko | 1c8567a | 2017-01-05 13:42:27 +0100 | [diff] [blame] | 7456 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7457 | |
| 7458 | /* turn logging off, we are going to try to validate the value with all the types in order */ |
| 7459 | hidden = *ly_vlog_hide_location(); |
| 7460 | ly_vlog_hide(1); |
| 7461 | |
| 7462 | t = NULL; |
| 7463 | found = 0; |
| 7464 | while ((t = lyp_get_next_union_type(type, t, &found))) { |
| 7465 | found = 0; |
| 7466 | |
| 7467 | switch (t->base) { |
| 7468 | case LY_TYPE_LEAFREF: |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7469 | if ((ignore_fail == 1) || ((leaf->schema->flags & LYS_LEAFREF_DEP) && (ignore_fail == 2))) { |
| 7470 | req_inst = -1; |
| 7471 | } else { |
| 7472 | req_inst = t->info.lref.req; |
| 7473 | } |
| 7474 | |
| 7475 | if (!resolve_leafref(leaf, t->info.lref.path, req_inst, &ret)) { |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7476 | if (store) { |
| 7477 | if (ret && !(leaf->schema->flags & LYS_LEAFREF_DEP)) { |
| 7478 | /* valid resolved */ |
| 7479 | leaf->value.leafref = ret; |
| 7480 | leaf->value_type = LY_TYPE_LEAFREF; |
| 7481 | } else { |
| 7482 | /* valid unresolved */ |
Radek Krejci | a571d94 | 2017-02-24 09:26:49 +0100 | [diff] [blame] | 7483 | 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] | 7484 | return -1; |
| 7485 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7486 | } |
| 7487 | } |
| 7488 | |
| 7489 | success = 1; |
| 7490 | } |
| 7491 | break; |
| 7492 | case LY_TYPE_INST: |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7493 | ext_dep = check_instid_ext_dep(leaf->schema, (json_val ? json_val : leaf->value_str)); |
| 7494 | if ((ignore_fail == 1) || (ext_dep && (ignore_fail == 2))) { |
| 7495 | req_inst = -1; |
| 7496 | } else { |
| 7497 | req_inst = t->info.inst.req; |
| 7498 | } |
| 7499 | |
Michal Vasko | d3a0311 | 2017-01-23 09:56:02 +0100 | [diff] [blame] | 7500 | 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] | 7501 | if (store) { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7502 | if (ret && !ext_dep) { |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7503 | /* valid resolved */ |
| 7504 | leaf->value.instance = ret; |
| 7505 | leaf->value_type = LY_TYPE_INST; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7506 | |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7507 | if (json_val) { |
| 7508 | lydict_remove(leaf->schema->module->ctx, leaf->value_str); |
| 7509 | leaf->value_str = json_val; |
| 7510 | json_val = NULL; |
| 7511 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7512 | } else { |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7513 | /* valid unresolved */ |
| 7514 | if (json_val) { |
| 7515 | /* put the JSON val back */ |
| 7516 | leaf->value.string = json_val; |
| 7517 | json_val = NULL; |
| 7518 | } else { |
| 7519 | leaf->value.instance = NULL; |
| 7520 | } |
| 7521 | leaf->value_type = LY_TYPE_INST | LY_TYPE_INST_UNRES; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7522 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7523 | } |
| 7524 | |
| 7525 | success = 1; |
| 7526 | } |
| 7527 | break; |
| 7528 | default: |
Radek Krejci | a571d94 | 2017-02-24 09:26:49 +0100 | [diff] [blame] | 7529 | 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] | 7530 | success = 1; |
| 7531 | } |
| 7532 | break; |
| 7533 | } |
| 7534 | |
| 7535 | if (success) { |
| 7536 | break; |
| 7537 | } |
| 7538 | |
| 7539 | /* erase information about errors - they are false or irrelevant |
| 7540 | * and will be replaced by a single error messages */ |
| 7541 | ly_err_clean(1); |
| 7542 | |
| 7543 | /* erase possible present and invalid value data */ |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7544 | if (store) { |
| 7545 | if (t->base == LY_TYPE_BITS) { |
| 7546 | free(leaf->value.bit); |
| 7547 | } |
| 7548 | memset(&leaf->value, 0, sizeof leaf->value); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7549 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7550 | } |
| 7551 | |
| 7552 | /* turn logging back on */ |
| 7553 | if (!hidden) { |
| 7554 | ly_vlog_hide(0); |
| 7555 | } |
| 7556 | |
| 7557 | if (json_val) { |
| 7558 | if (!success) { |
| 7559 | /* put the value back for now */ |
| 7560 | assert(leaf->value_type == LY_TYPE_UNION); |
| 7561 | leaf->value.string = json_val; |
| 7562 | } else { |
| 7563 | /* value was ultimately useless, but we could not have known */ |
| 7564 | lydict_remove(leaf->schema->module->ctx, json_val); |
| 7565 | } |
| 7566 | } |
| 7567 | |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7568 | if (success) { |
| 7569 | if (resolved_type) { |
| 7570 | *resolved_type = t; |
| 7571 | } |
| 7572 | } else if (!ignore_fail || !type->info.uni.has_ptr_type) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7573 | /* not found and it is required */ |
| 7574 | 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] | 7575 | return EXIT_FAILURE; |
| 7576 | } |
| 7577 | |
| 7578 | return EXIT_SUCCESS; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7579 | |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 7580 | } |
| 7581 | |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7582 | /** |
| 7583 | * @brief Resolve a single unres data item. Logs directly. |
| 7584 | * |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7585 | * @param[in] node Data node to resolve. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7586 | * @param[in] type Type of the unresolved item. |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7587 | * @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] | 7588 | * |
| 7589 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 7590 | */ |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 7591 | int |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7592 | 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] | 7593 | { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7594 | int rc, req_inst, ext_dep; |
Michal Vasko | 83a6c46 | 2015-10-08 16:43:53 +0200 | [diff] [blame] | 7595 | struct lyd_node_leaf_list *leaf; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7596 | struct lyd_node *ret; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7597 | struct lys_node_leaf *sleaf; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7598 | |
Michal Vasko | 83a6c46 | 2015-10-08 16:43:53 +0200 | [diff] [blame] | 7599 | leaf = (struct lyd_node_leaf_list *)node; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7600 | sleaf = (struct lys_node_leaf *)leaf->schema; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7601 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7602 | switch (type) { |
| 7603 | case UNRES_LEAFREF: |
| 7604 | assert(sleaf->type.base == LY_TYPE_LEAFREF); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7605 | assert(leaf->validity & LYD_VAL_LEAFREF); |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7606 | if ((ignore_fail == 1) || ((leaf->schema->flags & LYS_LEAFREF_DEP) && (ignore_fail == 2))) { |
| 7607 | req_inst = -1; |
| 7608 | } else { |
| 7609 | req_inst = sleaf->type.info.lref.req; |
| 7610 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7611 | rc = resolve_leafref(leaf, sleaf->type.info.lref.path, req_inst, &ret); |
| 7612 | if (!rc) { |
Michal Vasko | b1ac872 | 2017-01-02 13:04:25 +0100 | [diff] [blame] | 7613 | if (ret && !(leaf->schema->flags & LYS_LEAFREF_DEP)) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7614 | /* valid resolved */ |
Michal Vasko | 1c8567a | 2017-01-05 13:42:27 +0100 | [diff] [blame] | 7615 | if ((leaf->value_type & LY_DATA_TYPE_MASK) == LY_TYPE_BITS) { |
| 7616 | free(leaf->value.bit); |
| 7617 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7618 | leaf->value.leafref = ret; |
| 7619 | leaf->value_type = LY_TYPE_LEAFREF; |
| 7620 | } else { |
| 7621 | /* valid unresolved */ |
| 7622 | if (!(leaf->value_type & LY_TYPE_LEAFREF_UNRES)) { |
Radek Krejci | a571d94 | 2017-02-24 09:26:49 +0100 | [diff] [blame] | 7623 | 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] | 7624 | return -1; |
| 7625 | } |
| 7626 | } |
| 7627 | } |
| 7628 | leaf->validity &= ~LYD_VAL_LEAFREF; |
| 7629 | } else { |
| 7630 | return rc; |
| 7631 | } |
| 7632 | break; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7633 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7634 | case UNRES_INSTID: |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7635 | assert(sleaf->type.base == LY_TYPE_INST); |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7636 | ext_dep = check_instid_ext_dep(leaf->schema, leaf->value_str); |
| 7637 | if (ext_dep == -1) { |
| 7638 | return -1; |
| 7639 | } |
| 7640 | |
| 7641 | if ((ignore_fail == 1) || (ext_dep && (ignore_fail == 2))) { |
| 7642 | req_inst = -1; |
| 7643 | } else { |
| 7644 | req_inst = sleaf->type.info.inst.req; |
| 7645 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7646 | rc = resolve_instid(node, leaf->value_str, req_inst, &ret); |
| 7647 | if (!rc) { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7648 | if (ret && !ext_dep) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7649 | /* valid resolved */ |
| 7650 | leaf->value.instance = ret; |
| 7651 | leaf->value_type = LY_TYPE_INST; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7652 | } else { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7653 | /* valid unresolved */ |
| 7654 | leaf->value.instance = NULL; |
| 7655 | leaf->value_type = LY_TYPE_INST | LY_TYPE_INST_UNRES; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7656 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7657 | } else { |
| 7658 | return rc; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7659 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7660 | break; |
| 7661 | |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7662 | case UNRES_UNION: |
| 7663 | assert(sleaf->type.base == LY_TYPE_UNION); |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7664 | return resolve_union(leaf, &sleaf->type, 1, ignore_fail, NULL); |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7665 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7666 | case UNRES_WHEN: |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7667 | if ((rc = resolve_when(node, NULL, ignore_fail))) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7668 | return rc; |
| 7669 | } |
| 7670 | break; |
| 7671 | |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 7672 | case UNRES_MUST: |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7673 | if ((rc = resolve_must(node, 0, ignore_fail))) { |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 7674 | return rc; |
| 7675 | } |
| 7676 | break; |
| 7677 | |
| 7678 | case UNRES_MUST_INOUT: |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7679 | if ((rc = resolve_must(node, 1, ignore_fail))) { |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 7680 | return rc; |
| 7681 | } |
| 7682 | break; |
| 7683 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7684 | default: |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7685 | LOGINT; |
| 7686 | return -1; |
| 7687 | } |
| 7688 | |
| 7689 | return EXIT_SUCCESS; |
| 7690 | } |
| 7691 | |
| 7692 | /** |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7693 | * @brief add data unres item |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7694 | * |
| 7695 | * @param[in] unres Unres data structure to use. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7696 | * @param[in] node Data node to use. |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7697 | * |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7698 | * @return 0 on success, -1 on error. |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7699 | */ |
| 7700 | int |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7701 | 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] | 7702 | { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7703 | assert(unres && node); |
Michal Vasko | c428084 | 2016-04-19 16:10:42 +0200 | [diff] [blame] | 7704 | 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] | 7705 | || (type == UNRES_MUST_INOUT) || (type == UNRES_UNION)); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7706 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7707 | unres->count++; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 7708 | unres->node = ly_realloc(unres->node, unres->count * sizeof *unres->node); |
| 7709 | if (!unres->node) { |
| 7710 | LOGMEM; |
| 7711 | return -1; |
| 7712 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7713 | unres->node[unres->count - 1] = node; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 7714 | unres->type = ly_realloc(unres->type, unres->count * sizeof *unres->type); |
| 7715 | if (!unres->type) { |
| 7716 | LOGMEM; |
| 7717 | return -1; |
| 7718 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7719 | unres->type[unres->count - 1] = type; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7720 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7721 | if (type == UNRES_WHEN) { |
| 7722 | /* remove previous result */ |
| 7723 | node->when_status = LYD_WHEN; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7724 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7725 | |
| 7726 | return EXIT_SUCCESS; |
| 7727 | } |
| 7728 | |
| 7729 | /** |
| 7730 | * @brief Resolve every unres data item in the structure. Logs directly. |
| 7731 | * |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 7732 | * If options includes LYD_OPT_TRUSTED, the data are considered trusted (when, must conditions are not expected, |
| 7733 | * unresolved leafrefs/instids are accepted). |
| 7734 | * |
| 7735 | * If options includes LYD_OPT_NOAUTODEL, the false resulting when condition on non-default nodes, the error is raised. |
| 7736 | * |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7737 | * @param[in] unres Unres data structure to use. |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 7738 | * @param[in,out] root Root node of the data tree, can be changed due to autodeletion. |
| 7739 | * @param[in] options Data options as described above. |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7740 | * |
| 7741 | * @return EXIT_SUCCESS on success, -1 on error. |
| 7742 | */ |
| 7743 | int |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 7744 | 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] | 7745 | { |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 7746 | uint32_t i, j, first, resolved, del_items, stmt_count; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7747 | int rc, progress, ignore_fail; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7748 | struct lyd_node *parent; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7749 | |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 7750 | assert(root); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7751 | assert(unres); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7752 | |
| 7753 | if (!unres->count) { |
| 7754 | return EXIT_SUCCESS; |
| 7755 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7756 | |
Michal Vasko | ad2e44a | 2017-01-03 10:31:35 +0100 | [diff] [blame] | 7757 | 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] | 7758 | ignore_fail = 1; |
| 7759 | } else if (options & LYD_OPT_NOEXTDEPS) { |
| 7760 | ignore_fail = 2; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7761 | } else { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7762 | ignore_fail = 0; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7763 | } |
| 7764 | |
Michal Vasko | a0ffcab | 2016-05-02 14:52:08 +0200 | [diff] [blame] | 7765 | LOGVRB("Resolving unresolved data nodes and their constraints..."); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7766 | ly_vlog_hide(1); |
| 7767 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7768 | /* when-stmt first */ |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 7769 | first = 1; |
| 7770 | stmt_count = 0; |
| 7771 | resolved = 0; |
| 7772 | del_items = 0; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7773 | do { |
Radek Krejci | 00a0e71 | 2016-10-26 10:24:46 +0200 | [diff] [blame] | 7774 | ly_err_clean(1); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7775 | progress = 0; |
Michal Vasko | 6df9413 | 2016-09-22 11:08:09 +0200 | [diff] [blame] | 7776 | for (i = 0; i < unres->count; i++) { |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 7777 | if (unres->type[i] != UNRES_WHEN) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7778 | continue; |
| 7779 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7780 | if (first) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7781 | /* count when-stmt nodes in unres list */ |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 7782 | stmt_count++; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7783 | } |
| 7784 | |
| 7785 | /* resolve when condition only when all parent when conditions are already resolved */ |
| 7786 | for (parent = unres->node[i]->parent; |
| 7787 | parent && LYD_WHEN_DONE(parent->when_status); |
| 7788 | parent = parent->parent) { |
| 7789 | if (!parent->parent && (parent->when_status & LYD_WHEN_FALSE)) { |
| 7790 | /* the parent node was already unlinked, do not resolve this node, |
| 7791 | * it will be removed anyway, so just mark it as resolved |
| 7792 | */ |
| 7793 | unres->node[i]->when_status |= LYD_WHEN_FALSE; |
| 7794 | unres->type[i] = UNRES_RESOLVED; |
| 7795 | resolved++; |
| 7796 | break; |
| 7797 | } |
| 7798 | } |
| 7799 | if (parent) { |
| 7800 | continue; |
| 7801 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7802 | |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7803 | 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] | 7804 | if (!rc) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7805 | if (unres->node[i]->when_status & LYD_WHEN_FALSE) { |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 7806 | if ((options & LYD_OPT_NOAUTODEL) && !unres->node[i]->dflt) { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7807 | /* false when condition */ |
| 7808 | ly_vlog_hide(0); |
Radek Krejci | 2467a49 | 2016-10-24 15:16:59 +0200 | [diff] [blame] | 7809 | ly_err_repeat(); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7810 | return -1; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7811 | } /* follows else */ |
| 7812 | |
Michal Vasko | e31d34a | 2017-03-28 14:50:38 +0200 | [diff] [blame] | 7813 | /* auto-delete */ |
| 7814 | LOGVRB("auto-delete node \"%s\" due to when condition (%s)", ly_errpath(), |
| 7815 | ((struct lys_node_leaf *)unres->node[i]->schema)->when->cond); |
| 7816 | |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7817 | /* only unlink now, the subtree can contain another nodes stored in the unres list */ |
| 7818 | /* if it has parent non-presence containers that would be empty, we should actually |
| 7819 | * remove the container |
| 7820 | */ |
Radek Krejci | 2537fd3 | 2016-09-07 16:22:41 +0200 | [diff] [blame] | 7821 | for (parent = unres->node[i]; |
| 7822 | parent->parent && parent->parent->schema->nodetype == LYS_CONTAINER; |
| 7823 | parent = parent->parent) { |
| 7824 | if (((struct lys_node_container *)parent->parent->schema)->presence) { |
| 7825 | /* presence container */ |
| 7826 | break; |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7827 | } |
Radek Krejci | 2537fd3 | 2016-09-07 16:22:41 +0200 | [diff] [blame] | 7828 | if (parent->next || parent->prev != parent) { |
| 7829 | /* non empty (the child we are in and we are going to remove is not the only child) */ |
| 7830 | break; |
| 7831 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7832 | } |
Radek Krejci | 2537fd3 | 2016-09-07 16:22:41 +0200 | [diff] [blame] | 7833 | unres->node[i] = parent; |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7834 | |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7835 | if (*root && *root == unres->node[i]) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7836 | *root = (*root)->next; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7837 | } |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7838 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7839 | lyd_unlink(unres->node[i]); |
| 7840 | unres->type[i] = UNRES_DELETE; |
| 7841 | del_items++; |
Radek Krejci | 51fd816 | 2016-03-24 15:49:51 +0100 | [diff] [blame] | 7842 | |
| 7843 | /* update the rest of unres items */ |
| 7844 | for (j = 0; j < unres->count; j++) { |
Radek Krejci | 3db819b | 2016-03-24 16:29:48 +0100 | [diff] [blame] | 7845 | if (unres->type[j] == UNRES_RESOLVED || unres->type[j] == UNRES_DELETE) { |
Radek Krejci | 51fd816 | 2016-03-24 15:49:51 +0100 | [diff] [blame] | 7846 | continue; |
| 7847 | } |
| 7848 | |
| 7849 | /* test if the node is in subtree to be deleted */ |
| 7850 | for (parent = unres->node[j]; parent; parent = parent->parent) { |
| 7851 | if (parent == unres->node[i]) { |
| 7852 | /* yes, it is */ |
| 7853 | unres->type[j] = UNRES_RESOLVED; |
| 7854 | resolved++; |
| 7855 | break; |
| 7856 | } |
| 7857 | } |
| 7858 | } |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7859 | } else { |
| 7860 | unres->type[i] = UNRES_RESOLVED; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7861 | } |
Radek Krejci | 00a0e71 | 2016-10-26 10:24:46 +0200 | [diff] [blame] | 7862 | ly_err_clean(1); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7863 | resolved++; |
| 7864 | progress = 1; |
| 7865 | } else if (rc == -1) { |
| 7866 | ly_vlog_hide(0); |
Michal Vasko | 76e7340 | 2016-08-24 16:00:13 +0200 | [diff] [blame] | 7867 | /* print only this last error */ |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7868 | resolve_unres_data_item(unres->node[i], unres->type[i], ignore_fail); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7869 | return -1; |
Radek Krejci | 2467a49 | 2016-10-24 15:16:59 +0200 | [diff] [blame] | 7870 | } /* else forward reference */ |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7871 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7872 | first = 0; |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 7873 | } while (progress && resolved < stmt_count); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7874 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7875 | /* do we have some unresolved when-stmt? */ |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 7876 | if (stmt_count > resolved) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7877 | ly_vlog_hide(0); |
Radek Krejci | 2467a49 | 2016-10-24 15:16:59 +0200 | [diff] [blame] | 7878 | ly_err_repeat(); |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7879 | return -1; |
| 7880 | } |
| 7881 | |
| 7882 | for (i = 0; del_items && i < unres->count; i++) { |
| 7883 | /* we had some when-stmt resulted to false, so now we have to sanitize the unres list */ |
| 7884 | if (unres->type[i] != UNRES_DELETE) { |
| 7885 | continue; |
| 7886 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7887 | if (!unres->node[i]) { |
| 7888 | unres->type[i] = UNRES_RESOLVED; |
| 7889 | del_items--; |
| 7890 | continue; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7891 | } |
| 7892 | |
| 7893 | /* really remove the complete subtree */ |
| 7894 | lyd_free(unres->node[i]); |
| 7895 | unres->type[i] = UNRES_RESOLVED; |
| 7896 | del_items--; |
| 7897 | } |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 7898 | |
| 7899 | /* now leafrefs */ |
| 7900 | first = 1; |
| 7901 | stmt_count = 0; |
| 7902 | resolved = 0; |
| 7903 | do { |
| 7904 | progress = 0; |
| 7905 | for (i = 0; i < unres->count; i++) { |
| 7906 | if (unres->type[i] != UNRES_LEAFREF) { |
| 7907 | continue; |
| 7908 | } |
| 7909 | if (first) { |
| 7910 | /* count leafref nodes in unres list */ |
| 7911 | stmt_count++; |
| 7912 | } |
| 7913 | |
| 7914 | rc = resolve_unres_data_item(unres->node[i], unres->type[i], ignore_fail); |
| 7915 | if (!rc) { |
| 7916 | unres->type[i] = UNRES_RESOLVED; |
| 7917 | ly_err_clean(1); |
| 7918 | resolved++; |
| 7919 | progress = 1; |
| 7920 | } else if (rc == -1) { |
| 7921 | ly_vlog_hide(0); |
| 7922 | /* print only this last error */ |
| 7923 | resolve_unres_data_item(unres->node[i], unres->type[i], ignore_fail); |
| 7924 | return -1; |
| 7925 | } /* else forward reference */ |
| 7926 | } |
| 7927 | first = 0; |
| 7928 | } while (progress && resolved < stmt_count); |
| 7929 | |
| 7930 | /* do we have some unresolved leafrefs? */ |
| 7931 | if (stmt_count > resolved) { |
| 7932 | ly_vlog_hide(0); |
| 7933 | ly_err_repeat(); |
| 7934 | return -1; |
| 7935 | } |
| 7936 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7937 | ly_vlog_hide(0); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7938 | |
| 7939 | /* rest */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7940 | for (i = 0; i < unres->count; ++i) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7941 | if (unres->type[i] == UNRES_RESOLVED) { |
| 7942 | continue; |
| 7943 | } |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 7944 | 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] | 7945 | |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7946 | 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] | 7947 | if (rc) { |
| 7948 | /* since when was already resolved, a forward reference is an error */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7949 | return -1; |
| 7950 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7951 | |
| 7952 | unres->type[i] = UNRES_RESOLVED; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7953 | } |
| 7954 | |
Michal Vasko | a0ffcab | 2016-05-02 14:52:08 +0200 | [diff] [blame] | 7955 | LOGVRB("All data nodes and constraints resolved."); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7956 | unres->count = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7957 | return EXIT_SUCCESS; |
| 7958 | } |