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" |
| 32 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 33 | /** |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 34 | * @brief Parse an identifier. |
| 35 | * |
| 36 | * ;; An identifier MUST NOT start with (('X'|'x') ('M'|'m') ('L'|'l')) |
| 37 | * identifier = (ALPHA / "_") |
| 38 | * *(ALPHA / DIGIT / "_" / "-" / ".") |
| 39 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 40 | * @param[in] id Identifier to use. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 41 | * |
| 42 | * @return Number of characters successfully parsed. |
| 43 | */ |
Michal Vasko | 249e6b5 | 2015-08-19 11:08:52 +0200 | [diff] [blame] | 44 | int |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 45 | parse_identifier(const char *id) |
| 46 | { |
| 47 | int parsed = 0; |
| 48 | |
Michal Vasko | 1ab90bc | 2016-03-15 10:40:22 +0100 | [diff] [blame] | 49 | assert(id); |
| 50 | |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 51 | if (((id[0] == 'x') || (id[0] == 'X')) |
Michal Vasko | 1ab90bc | 2016-03-15 10:40:22 +0100 | [diff] [blame] | 52 | && (id[0] && ((id[1] == 'm') || (id[0] == 'M'))) |
| 53 | && (id[1] && ((id[2] == 'l') || (id[2] == 'L')))) { |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 54 | return -parsed; |
| 55 | } |
| 56 | |
| 57 | if (!isalpha(id[0]) && (id[0] != '_')) { |
| 58 | return -parsed; |
| 59 | } |
| 60 | |
| 61 | ++parsed; |
| 62 | ++id; |
| 63 | |
| 64 | while (isalnum(id[0]) || (id[0] == '_') || (id[0] == '-') || (id[0] == '.')) { |
| 65 | ++parsed; |
| 66 | ++id; |
| 67 | } |
| 68 | |
| 69 | return parsed; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * @brief Parse a node-identifier. |
| 74 | * |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 75 | * node-identifier = [module-name ":"] identifier |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 76 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 77 | * @param[in] id Identifier to use. |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 78 | * @param[out] mod_name Points to the module name, NULL if there is not any. |
| 79 | * @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] | 80 | * @param[out] name Points to the node name. |
| 81 | * @param[out] nam_len Length of the node name. |
| 82 | * |
| 83 | * @return Number of characters successfully parsed, |
| 84 | * positive on success, negative on failure. |
| 85 | */ |
| 86 | static int |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 87 | 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] | 88 | { |
| 89 | int parsed = 0, ret; |
| 90 | |
| 91 | assert(id); |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 92 | if (mod_name) { |
| 93 | *mod_name = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 94 | } |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 95 | if (mod_name_len) { |
| 96 | *mod_name_len = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 97 | } |
| 98 | if (name) { |
| 99 | *name = NULL; |
| 100 | } |
| 101 | if (nam_len) { |
| 102 | *nam_len = 0; |
| 103 | } |
| 104 | |
| 105 | if ((ret = parse_identifier(id)) < 1) { |
| 106 | return ret; |
| 107 | } |
| 108 | |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 109 | if (mod_name) { |
| 110 | *mod_name = id; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 111 | } |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 112 | if (mod_name_len) { |
| 113 | *mod_name_len = ret; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | parsed += ret; |
| 117 | id += ret; |
| 118 | |
| 119 | /* there is prefix */ |
| 120 | if (id[0] == ':') { |
| 121 | ++parsed; |
| 122 | ++id; |
| 123 | |
| 124 | /* there isn't */ |
| 125 | } else { |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 126 | if (name && mod_name) { |
| 127 | *name = *mod_name; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 128 | } |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 129 | if (mod_name) { |
| 130 | *mod_name = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 131 | } |
| 132 | |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 133 | if (nam_len && mod_name_len) { |
| 134 | *nam_len = *mod_name_len; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 135 | } |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 136 | if (mod_name_len) { |
| 137 | *mod_name_len = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | return parsed; |
| 141 | } |
| 142 | |
| 143 | /* identifier (node name) */ |
| 144 | if ((ret = parse_identifier(id)) < 1) { |
| 145 | return -parsed+ret; |
| 146 | } |
| 147 | |
| 148 | if (name) { |
| 149 | *name = id; |
| 150 | } |
| 151 | if (nam_len) { |
| 152 | *nam_len = ret; |
| 153 | } |
| 154 | |
| 155 | return parsed+ret; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * @brief Parse a path-predicate (leafref). |
| 160 | * |
| 161 | * path-predicate = "[" *WSP path-equality-expr *WSP "]" |
| 162 | * path-equality-expr = node-identifier *WSP "=" *WSP path-key-expr |
| 163 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 164 | * @param[in] id Identifier to use. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 165 | * @param[out] prefix Points to the prefix, NULL if there is not any. |
| 166 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 167 | * @param[out] name Points to the node name. |
| 168 | * @param[out] nam_len Length of the node name. |
| 169 | * @param[out] path_key_expr Points to the path-key-expr. |
| 170 | * @param[out] pke_len Length of the path-key-expr. |
| 171 | * @param[out] has_predicate Flag to mark whether there is another predicate following. |
| 172 | * |
| 173 | * @return Number of characters successfully parsed, |
| 174 | * positive on success, negative on failure. |
| 175 | */ |
| 176 | static int |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 177 | parse_path_predicate(const char *id, const char **prefix, int *pref_len, const char **name, int *nam_len, |
| 178 | const char **path_key_expr, int *pke_len, int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 179 | { |
| 180 | const char *ptr; |
| 181 | int parsed = 0, ret; |
| 182 | |
| 183 | assert(id); |
| 184 | if (prefix) { |
| 185 | *prefix = NULL; |
| 186 | } |
| 187 | if (pref_len) { |
| 188 | *pref_len = 0; |
| 189 | } |
| 190 | if (name) { |
| 191 | *name = NULL; |
| 192 | } |
| 193 | if (nam_len) { |
| 194 | *nam_len = 0; |
| 195 | } |
| 196 | if (path_key_expr) { |
| 197 | *path_key_expr = NULL; |
| 198 | } |
| 199 | if (pke_len) { |
| 200 | *pke_len = 0; |
| 201 | } |
| 202 | if (has_predicate) { |
| 203 | *has_predicate = 0; |
| 204 | } |
| 205 | |
| 206 | if (id[0] != '[') { |
| 207 | return -parsed; |
| 208 | } |
| 209 | |
| 210 | ++parsed; |
| 211 | ++id; |
| 212 | |
| 213 | while (isspace(id[0])) { |
| 214 | ++parsed; |
| 215 | ++id; |
| 216 | } |
| 217 | |
| 218 | if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len)) < 1) { |
| 219 | return -parsed+ret; |
| 220 | } |
| 221 | |
| 222 | parsed += ret; |
| 223 | id += ret; |
| 224 | |
| 225 | while (isspace(id[0])) { |
| 226 | ++parsed; |
| 227 | ++id; |
| 228 | } |
| 229 | |
| 230 | if (id[0] != '=') { |
| 231 | return -parsed; |
| 232 | } |
| 233 | |
| 234 | ++parsed; |
| 235 | ++id; |
| 236 | |
| 237 | while (isspace(id[0])) { |
| 238 | ++parsed; |
| 239 | ++id; |
| 240 | } |
| 241 | |
| 242 | if ((ptr = strchr(id, ']')) == NULL) { |
| 243 | return -parsed; |
| 244 | } |
| 245 | |
| 246 | --ptr; |
| 247 | while (isspace(ptr[0])) { |
| 248 | --ptr; |
| 249 | } |
| 250 | ++ptr; |
| 251 | |
| 252 | ret = ptr-id; |
| 253 | if (path_key_expr) { |
| 254 | *path_key_expr = id; |
| 255 | } |
| 256 | if (pke_len) { |
| 257 | *pke_len = ret; |
| 258 | } |
| 259 | |
| 260 | parsed += ret; |
| 261 | id += ret; |
| 262 | |
| 263 | while (isspace(id[0])) { |
| 264 | ++parsed; |
| 265 | ++id; |
| 266 | } |
| 267 | |
| 268 | assert(id[0] == ']'); |
| 269 | |
| 270 | if (id[1] == '[') { |
| 271 | *has_predicate = 1; |
| 272 | } |
| 273 | |
| 274 | return parsed+1; |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * @brief Parse a path-key-expr (leafref). First call parses "current()", all |
| 279 | * the ".." and the first node-identifier, other calls parse a single |
| 280 | * node-identifier each. |
| 281 | * |
| 282 | * path-key-expr = current-function-invocation *WSP "/" *WSP |
| 283 | * rel-path-keyexpr |
| 284 | * rel-path-keyexpr = 1*(".." *WSP "/" *WSP) |
| 285 | * *(node-identifier *WSP "/" *WSP) |
| 286 | * node-identifier |
| 287 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 288 | * @param[in] id Identifier to use. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 289 | * @param[out] prefix Points to the prefix, NULL if there is not any. |
| 290 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 291 | * @param[out] name Points to the node name. |
| 292 | * @param[out] nam_len Length of the node name. |
| 293 | * @param[out] parent_times Number of ".." in the path. Must be 0 on the first call, |
| 294 | * must not be changed between consecutive calls. |
| 295 | * @return Number of characters successfully parsed, |
| 296 | * positive on success, negative on failure. |
| 297 | */ |
| 298 | static int |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 299 | parse_path_key_expr(const char *id, const char **prefix, int *pref_len, const char **name, int *nam_len, |
| 300 | int *parent_times) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 301 | { |
| 302 | int parsed = 0, ret, par_times = 0; |
| 303 | |
| 304 | assert(id); |
| 305 | assert(parent_times); |
| 306 | if (prefix) { |
| 307 | *prefix = NULL; |
| 308 | } |
| 309 | if (pref_len) { |
| 310 | *pref_len = 0; |
| 311 | } |
| 312 | if (name) { |
| 313 | *name = NULL; |
| 314 | } |
| 315 | if (nam_len) { |
| 316 | *nam_len = 0; |
| 317 | } |
| 318 | |
| 319 | if (!*parent_times) { |
| 320 | /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */ |
| 321 | if (strncmp(id, "current()", 9)) { |
| 322 | return -parsed; |
| 323 | } |
| 324 | |
| 325 | parsed += 9; |
| 326 | id += 9; |
| 327 | |
| 328 | while (isspace(id[0])) { |
| 329 | ++parsed; |
| 330 | ++id; |
| 331 | } |
| 332 | |
| 333 | if (id[0] != '/') { |
| 334 | return -parsed; |
| 335 | } |
| 336 | |
| 337 | ++parsed; |
| 338 | ++id; |
| 339 | |
| 340 | while (isspace(id[0])) { |
| 341 | ++parsed; |
| 342 | ++id; |
| 343 | } |
| 344 | |
| 345 | /* rel-path-keyexpr */ |
| 346 | if (strncmp(id, "..", 2)) { |
| 347 | return -parsed; |
| 348 | } |
| 349 | ++par_times; |
| 350 | |
| 351 | parsed += 2; |
| 352 | id += 2; |
| 353 | |
| 354 | while (isspace(id[0])) { |
| 355 | ++parsed; |
| 356 | ++id; |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | /* 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier |
| 361 | * |
| 362 | * first parent reference with whitespaces already parsed |
| 363 | */ |
| 364 | if (id[0] != '/') { |
| 365 | return -parsed; |
| 366 | } |
| 367 | |
| 368 | ++parsed; |
| 369 | ++id; |
| 370 | |
| 371 | while (isspace(id[0])) { |
| 372 | ++parsed; |
| 373 | ++id; |
| 374 | } |
| 375 | |
| 376 | while (!strncmp(id, "..", 2) && !*parent_times) { |
| 377 | ++par_times; |
| 378 | |
| 379 | parsed += 2; |
| 380 | id += 2; |
| 381 | |
| 382 | while (isspace(id[0])) { |
| 383 | ++parsed; |
| 384 | ++id; |
| 385 | } |
| 386 | |
| 387 | if (id[0] != '/') { |
| 388 | return -parsed; |
| 389 | } |
| 390 | |
| 391 | ++parsed; |
| 392 | ++id; |
| 393 | |
| 394 | while (isspace(id[0])) { |
| 395 | ++parsed; |
| 396 | ++id; |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | if (!*parent_times) { |
| 401 | *parent_times = par_times; |
| 402 | } |
| 403 | |
| 404 | /* all parent references must be parsed at this point */ |
| 405 | if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len)) < 1) { |
| 406 | return -parsed+ret; |
| 407 | } |
| 408 | |
| 409 | parsed += ret; |
| 410 | id += ret; |
| 411 | |
| 412 | return parsed; |
| 413 | } |
| 414 | |
| 415 | /** |
| 416 | * @brief Parse path-arg (leafref). |
| 417 | * |
| 418 | * path-arg = absolute-path / relative-path |
| 419 | * absolute-path = 1*("/" (node-identifier *path-predicate)) |
| 420 | * relative-path = 1*(".." "/") descendant-path |
| 421 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 422 | * @param[in] id Identifier to use. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 423 | * @param[out] prefix Points to the prefix, NULL if there is not any. |
| 424 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 425 | * @param[out] name Points to the node name. |
| 426 | * @param[out] nam_len Length of the node name. |
| 427 | * @param[out] parent_times Number of ".." in the path. Must be 0 on the first call, |
| 428 | * must not be changed between consecutive calls. -1 if the |
| 429 | * path is relative. |
| 430 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 431 | * |
| 432 | * @return Number of characters successfully parsed, |
| 433 | * positive on success, negative on failure. |
| 434 | */ |
| 435 | static int |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 436 | parse_path_arg(const char *id, const char **prefix, int *pref_len, const char **name, int *nam_len, int *parent_times, |
| 437 | int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 438 | { |
| 439 | int parsed = 0, ret, par_times = 0; |
| 440 | |
| 441 | assert(id); |
| 442 | assert(parent_times); |
| 443 | if (prefix) { |
| 444 | *prefix = NULL; |
| 445 | } |
| 446 | if (pref_len) { |
| 447 | *pref_len = 0; |
| 448 | } |
| 449 | if (name) { |
| 450 | *name = NULL; |
| 451 | } |
| 452 | if (nam_len) { |
| 453 | *nam_len = 0; |
| 454 | } |
| 455 | if (has_predicate) { |
| 456 | *has_predicate = 0; |
| 457 | } |
| 458 | |
| 459 | if (!*parent_times && !strncmp(id, "..", 2)) { |
| 460 | ++par_times; |
| 461 | |
| 462 | parsed += 2; |
| 463 | id += 2; |
| 464 | |
| 465 | while (!strncmp(id, "/..", 3)) { |
| 466 | ++par_times; |
| 467 | |
| 468 | parsed += 3; |
| 469 | id += 3; |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | if (!*parent_times) { |
| 474 | if (par_times) { |
| 475 | *parent_times = par_times; |
| 476 | } else { |
| 477 | *parent_times = -1; |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | if (id[0] != '/') { |
| 482 | return -parsed; |
| 483 | } |
| 484 | |
| 485 | /* skip '/' */ |
| 486 | ++parsed; |
| 487 | ++id; |
| 488 | |
| 489 | /* node-identifier ([prefix:]identifier) */ |
| 490 | if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len)) < 1) { |
| 491 | return -parsed-ret; |
| 492 | } |
| 493 | |
| 494 | parsed += ret; |
| 495 | id += ret; |
| 496 | |
| 497 | /* there is no predicate */ |
| 498 | if ((id[0] == '/') || !id[0]) { |
| 499 | return parsed; |
| 500 | } else if (id[0] != '[') { |
| 501 | return -parsed; |
| 502 | } |
| 503 | |
| 504 | if (has_predicate) { |
| 505 | *has_predicate = 1; |
| 506 | } |
| 507 | |
| 508 | return parsed; |
| 509 | } |
| 510 | |
| 511 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 512 | * @brief Parse instance-identifier in JSON data format. That means that prefixes |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 513 | * (which are mandatory) are actually model names. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 514 | * |
| 515 | * instance-identifier = 1*("/" (node-identifier *predicate)) |
| 516 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 517 | * @param[in] id Identifier to use. |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 518 | * @param[out] model Points to the model name. |
| 519 | * @param[out] mod_len Length of the model name. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 520 | * @param[out] name Points to the node name. |
| 521 | * @param[out] nam_len Length of the node name. |
| 522 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 523 | * |
| 524 | * @return Number of characters successfully parsed, |
| 525 | * positive on success, negative on failure. |
| 526 | */ |
| 527 | static int |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 528 | parse_instance_identifier(const char *id, const char **model, int *mod_len, const char **name, int *nam_len, |
| 529 | int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 530 | { |
| 531 | int parsed = 0, ret; |
| 532 | |
| 533 | assert(id); |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 534 | if (model) { |
| 535 | *model = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 536 | } |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 537 | if (mod_len) { |
| 538 | *mod_len = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 539 | } |
| 540 | if (name) { |
| 541 | *name = NULL; |
| 542 | } |
| 543 | if (nam_len) { |
| 544 | *nam_len = 0; |
| 545 | } |
| 546 | if (has_predicate) { |
| 547 | *has_predicate = 0; |
| 548 | } |
| 549 | |
| 550 | if (id[0] != '/') { |
| 551 | return -parsed; |
| 552 | } |
| 553 | |
| 554 | ++parsed; |
| 555 | ++id; |
| 556 | |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 557 | if ((ret = parse_node_identifier(id, model, mod_len, name, nam_len)) < 1) { |
Michal Vasko | bea08e9 | 2016-05-18 13:28:08 +0200 | [diff] [blame] | 558 | return -parsed + ret; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | parsed += ret; |
| 562 | id += ret; |
| 563 | |
| 564 | if ((id[0] == '[') && has_predicate) { |
| 565 | *has_predicate = 1; |
| 566 | } |
| 567 | |
| 568 | return parsed; |
| 569 | } |
| 570 | |
| 571 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 572 | * @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] | 573 | * (which are mandatory) are actually model names. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 574 | * |
| 575 | * predicate = "[" *WSP (predicate-expr / pos) *WSP "]" |
| 576 | * predicate-expr = (node-identifier / ".") *WSP "=" *WSP |
| 577 | * ((DQUOTE string DQUOTE) / |
| 578 | * (SQUOTE string SQUOTE)) |
| 579 | * pos = non-negative-integer-value |
| 580 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 581 | * @param[in] id Identifier to use. |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 582 | * @param[out] model Points to the model name. |
| 583 | * @param[out] mod_len Length of the model name. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 584 | * @param[out] name Points to the node name. Can be identifier (from node-identifier), "." or pos. |
| 585 | * @param[out] nam_len Length of the node name. |
| 586 | * @param[out] value Value the node-identifier must have (string from the grammar), |
| 587 | * NULL if there is not any. |
| 588 | * @param[out] val_len Length of the value, 0 if there is not any. |
| 589 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 590 | * |
| 591 | * @return Number of characters successfully parsed, |
| 592 | * positive on success, negative on failure. |
| 593 | */ |
| 594 | static int |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 595 | parse_predicate(const char *id, const char **model, int *mod_len, const char **name, int *nam_len, |
| 596 | const char **value, int *val_len, int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 597 | { |
| 598 | const char *ptr; |
| 599 | int parsed = 0, ret; |
| 600 | char quote; |
| 601 | |
| 602 | assert(id); |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 603 | if (model) { |
| 604 | *model = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 605 | } |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 606 | if (mod_len) { |
| 607 | *mod_len = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 608 | } |
| 609 | if (name) { |
| 610 | *name = NULL; |
| 611 | } |
| 612 | if (nam_len) { |
| 613 | *nam_len = 0; |
| 614 | } |
| 615 | if (value) { |
| 616 | *value = NULL; |
| 617 | } |
| 618 | if (val_len) { |
| 619 | *val_len = 0; |
| 620 | } |
| 621 | if (has_predicate) { |
| 622 | *has_predicate = 0; |
| 623 | } |
| 624 | |
| 625 | if (id[0] != '[') { |
| 626 | return -parsed; |
| 627 | } |
| 628 | |
| 629 | ++parsed; |
| 630 | ++id; |
| 631 | |
| 632 | while (isspace(id[0])) { |
| 633 | ++parsed; |
| 634 | ++id; |
| 635 | } |
| 636 | |
| 637 | /* pos */ |
| 638 | if (isdigit(id[0])) { |
| 639 | if (name) { |
| 640 | *name = id; |
| 641 | } |
| 642 | |
| 643 | if (id[0] == '0') { |
| 644 | ++parsed; |
| 645 | ++id; |
| 646 | |
| 647 | if (isdigit(id[0])) { |
| 648 | return -parsed; |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | while (isdigit(id[0])) { |
| 653 | ++parsed; |
| 654 | ++id; |
| 655 | } |
| 656 | |
| 657 | if (nam_len) { |
| 658 | *nam_len = id-(*name); |
| 659 | } |
| 660 | |
| 661 | /* "." */ |
| 662 | } else if (id[0] == '.') { |
| 663 | if (name) { |
| 664 | *name = id; |
| 665 | } |
| 666 | if (nam_len) { |
| 667 | *nam_len = 1; |
| 668 | } |
| 669 | |
| 670 | ++parsed; |
| 671 | ++id; |
| 672 | |
| 673 | /* node-identifier */ |
| 674 | } else { |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 675 | if ((ret = parse_node_identifier(id, model, mod_len, name, nam_len)) < 1) { |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 676 | return -parsed+ret; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 677 | } else if (model && !*model) { |
| 678 | return -parsed; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 679 | } |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 680 | |
| 681 | parsed += ret; |
| 682 | id += ret; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | while (isspace(id[0])) { |
| 686 | ++parsed; |
| 687 | ++id; |
| 688 | } |
| 689 | |
| 690 | if (id[0] != '=') { |
| 691 | return -parsed; |
| 692 | } |
| 693 | |
| 694 | ++parsed; |
| 695 | ++id; |
| 696 | |
| 697 | while (isspace(id[0])) { |
| 698 | ++parsed; |
| 699 | ++id; |
| 700 | } |
| 701 | |
| 702 | /* ((DQUOTE string DQUOTE) / (SQUOTE string SQUOTE)) */ |
| 703 | if ((id[0] == '\"') || (id[0] == '\'')) { |
| 704 | quote = id[0]; |
| 705 | |
| 706 | ++parsed; |
| 707 | ++id; |
| 708 | |
| 709 | if ((ptr = strchr(id, quote)) == NULL) { |
| 710 | return -parsed; |
| 711 | } |
| 712 | ret = ptr-id; |
| 713 | |
| 714 | if (value) { |
| 715 | *value = id; |
| 716 | } |
| 717 | if (val_len) { |
| 718 | *val_len = ret; |
| 719 | } |
| 720 | |
| 721 | parsed += ret+1; |
| 722 | id += ret+1; |
| 723 | } else { |
| 724 | return -parsed; |
| 725 | } |
| 726 | |
| 727 | while (isspace(id[0])) { |
| 728 | ++parsed; |
| 729 | ++id; |
| 730 | } |
| 731 | |
| 732 | if (id[0] != ']') { |
| 733 | return -parsed; |
| 734 | } |
| 735 | |
| 736 | ++parsed; |
| 737 | ++id; |
| 738 | |
| 739 | if ((id[0] == '[') && has_predicate) { |
| 740 | *has_predicate = 1; |
| 741 | } |
| 742 | |
| 743 | return parsed; |
| 744 | } |
| 745 | |
| 746 | /** |
| 747 | * @brief Parse schema-nodeid. |
| 748 | * |
| 749 | * schema-nodeid = absolute-schema-nodeid / |
| 750 | * descendant-schema-nodeid |
| 751 | * absolute-schema-nodeid = 1*("/" node-identifier) |
Michal Vasko | 4893535 | 2016-03-29 11:52:36 +0200 | [diff] [blame] | 752 | * descendant-schema-nodeid = ["." "/"] |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 753 | * node-identifier |
| 754 | * absolute-schema-nodeid |
| 755 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 756 | * @param[in] id Identifier to use. |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 757 | * @param[out] mod_name Points to the module name, NULL if there is not any. |
| 758 | * @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] | 759 | * @param[out] name Points to the node name. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 760 | * @param[out] nam_len Length of the node name. |
| 761 | * @param[out] is_relative Flag to mark whether the nodeid is absolute or descendant. Must be -1 |
| 762 | * on the first call, must not be changed between consecutive calls. |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 763 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. It cannot be |
| 764 | * based on the grammar, in those cases use NULL. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 765 | * |
| 766 | * @return Number of characters successfully parsed, |
| 767 | * positive on success, negative on failure. |
| 768 | */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 769 | int |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 770 | 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] | 771 | int *is_relative, int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 772 | { |
| 773 | int parsed = 0, ret; |
| 774 | |
| 775 | assert(id); |
| 776 | assert(is_relative); |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 777 | if (mod_name) { |
| 778 | *mod_name = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 779 | } |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 780 | if (mod_name_len) { |
| 781 | *mod_name_len = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 782 | } |
| 783 | if (name) { |
| 784 | *name = NULL; |
| 785 | } |
| 786 | if (nam_len) { |
| 787 | *nam_len = 0; |
| 788 | } |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 789 | if (has_predicate) { |
| 790 | *has_predicate = 0; |
| 791 | } |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 792 | |
| 793 | if (id[0] != '/') { |
| 794 | if (*is_relative != -1) { |
| 795 | return -parsed; |
| 796 | } else { |
| 797 | *is_relative = 1; |
| 798 | } |
Michal Vasko | 4893535 | 2016-03-29 11:52:36 +0200 | [diff] [blame] | 799 | if (!strncmp(id, "./", 2)) { |
| 800 | parsed += 2; |
| 801 | id += 2; |
| 802 | } |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 803 | } else { |
| 804 | if (*is_relative == -1) { |
| 805 | *is_relative = 0; |
| 806 | } |
| 807 | ++parsed; |
| 808 | ++id; |
| 809 | } |
| 810 | |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 811 | 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] | 812 | return -parsed+ret; |
| 813 | } |
| 814 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 815 | parsed += ret; |
| 816 | id += ret; |
| 817 | |
| 818 | if ((id[0] == '[') && has_predicate) { |
| 819 | *has_predicate = 1; |
| 820 | } |
| 821 | |
| 822 | return parsed; |
| 823 | } |
| 824 | |
| 825 | /** |
| 826 | * @brief Parse schema predicate (special format internally used). |
| 827 | * |
| 828 | * predicate = "[" *WSP predicate-expr *WSP "]" |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 829 | * predicate-expr = "." / identifier / key-with-value |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 830 | * key-with-value = identifier *WSP "=" *WSP |
| 831 | * ((DQUOTE string DQUOTE) / |
| 832 | * (SQUOTE string SQUOTE)) |
| 833 | * |
| 834 | * @param[in] id Identifier to use. |
| 835 | * @param[out] name Points to the list key name. |
| 836 | * @param[out] nam_len Length of \p name. |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 837 | * @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] | 838 | * @param[out] val_len Length of \p value. |
| 839 | * @param[out] has_predicate Flag to mark whether there is another predicate specified. |
| 840 | */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 841 | int |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 842 | 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] | 843 | int *has_predicate) |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 844 | { |
| 845 | const char *ptr; |
| 846 | int parsed = 0, ret; |
| 847 | char quote; |
| 848 | |
| 849 | assert(id); |
| 850 | if (name) { |
| 851 | *name = NULL; |
| 852 | } |
| 853 | if (nam_len) { |
| 854 | *nam_len = 0; |
| 855 | } |
| 856 | if (value) { |
| 857 | *value = NULL; |
| 858 | } |
| 859 | if (val_len) { |
| 860 | *val_len = 0; |
| 861 | } |
| 862 | if (has_predicate) { |
| 863 | *has_predicate = 0; |
| 864 | } |
| 865 | |
| 866 | if (id[0] != '[') { |
| 867 | return -parsed; |
| 868 | } |
| 869 | |
| 870 | ++parsed; |
| 871 | ++id; |
| 872 | |
| 873 | while (isspace(id[0])) { |
| 874 | ++parsed; |
| 875 | ++id; |
| 876 | } |
| 877 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 878 | /* identifier */ |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 879 | if (id[0] == '.') { |
| 880 | ret = 1; |
| 881 | } else if ((ret = parse_identifier(id)) < 1) { |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 882 | return -parsed + ret; |
| 883 | } |
| 884 | if (name) { |
| 885 | *name = id; |
| 886 | } |
| 887 | if (nam_len) { |
| 888 | *nam_len = ret; |
| 889 | } |
| 890 | |
| 891 | parsed += ret; |
| 892 | id += ret; |
| 893 | |
| 894 | while (isspace(id[0])) { |
| 895 | ++parsed; |
| 896 | ++id; |
| 897 | } |
| 898 | |
| 899 | /* there is value as well */ |
| 900 | if (id[0] == '=') { |
| 901 | ++parsed; |
| 902 | ++id; |
| 903 | |
| 904 | while (isspace(id[0])) { |
| 905 | ++parsed; |
| 906 | ++id; |
| 907 | } |
| 908 | |
| 909 | /* ((DQUOTE string DQUOTE) / (SQUOTE string SQUOTE)) */ |
| 910 | if ((id[0] == '\"') || (id[0] == '\'')) { |
| 911 | quote = id[0]; |
| 912 | |
| 913 | ++parsed; |
| 914 | ++id; |
| 915 | |
| 916 | if ((ptr = strchr(id, quote)) == NULL) { |
| 917 | return -parsed; |
| 918 | } |
| 919 | ret = ptr - id; |
| 920 | |
| 921 | if (value) { |
| 922 | *value = id; |
| 923 | } |
| 924 | if (val_len) { |
| 925 | *val_len = ret; |
| 926 | } |
| 927 | |
| 928 | parsed += ret + 1; |
| 929 | id += ret + 1; |
| 930 | } else { |
| 931 | return -parsed; |
| 932 | } |
| 933 | |
| 934 | while (isspace(id[0])) { |
| 935 | ++parsed; |
| 936 | ++id; |
| 937 | } |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 938 | } else if (value) { |
| 939 | /* if value was expected, it's mandatory */ |
| 940 | return -parsed; |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | if (id[0] != ']') { |
| 944 | return -parsed; |
| 945 | } |
| 946 | |
| 947 | ++parsed; |
| 948 | ++id; |
| 949 | |
| 950 | if ((id[0] == '[') && has_predicate) { |
| 951 | *has_predicate = 1; |
| 952 | } |
| 953 | |
| 954 | return parsed; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 955 | } |
| 956 | |
| 957 | /** |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 958 | * @brief Resolve (find) a feature definition. Logs directly. |
| 959 | * |
| 960 | * @param[in] feat_name Feature name to resolve. |
| 961 | * @param[in] len Length of \p feat_name. |
| 962 | * @param[in] node Node with the if-feature expression. |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 963 | * @param[out] feature Pointer to be set to point to the feature definition, if feature not found |
| 964 | * (return code 1), the pointer is untouched. |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 965 | * |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 966 | * @return 0 on success, 1 on forward reference, -1 on error. |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 967 | */ |
| 968 | static int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 969 | 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] | 970 | { |
| 971 | char *str; |
| 972 | const char *mod_name, *name; |
| 973 | int mod_name_len, nam_len, i, j; |
| 974 | const struct lys_module *module; |
| 975 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 976 | assert(feature); |
| 977 | |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 978 | /* check prefix */ |
| 979 | if ((i = parse_node_identifier(feat_name, &mod_name, &mod_name_len, &name, &nam_len)) < 1) { |
| 980 | LOGVAL(LYE_INCHAR, LY_VLOG_NONE, NULL, feat_name[-i], &feat_name[-i]); |
| 981 | return -1; |
| 982 | } |
| 983 | |
| 984 | module = lys_get_import_module(lys_node_module(node), NULL, 0, mod_name, mod_name_len); |
| 985 | if (!module) { |
| 986 | /* identity refers unknown data model */ |
| 987 | LOGVAL(LYE_INMOD_LEN, LY_VLOG_NONE, NULL, mod_name_len, mod_name); |
| 988 | return -1; |
| 989 | } |
| 990 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 991 | if (module != node->module && module == lys_node_module(node)) { |
| 992 | /* first, try to search directly in submodule where the feature was mentioned */ |
| 993 | for (j = 0; j < node->module->features_size; j++) { |
| 994 | if (!strncmp(name, node->module->features[j].name, nam_len) && !node->module->features[j].name[nam_len]) { |
| 995 | /* check status */ |
| 996 | 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^] | 997 | node->module->features[j].module, node->module->features[j].name, NULL)) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 998 | return -1; |
| 999 | } |
| 1000 | *feature = &node->module->features[j]; |
| 1001 | return 0; |
| 1002 | } |
| 1003 | } |
| 1004 | } |
| 1005 | |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1006 | /* search in the identified module ... */ |
| 1007 | for (j = 0; j < module->features_size; j++) { |
Michal Vasko | 3def867 | 2016-07-01 11:43:09 +0200 | [diff] [blame] | 1008 | 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] | 1009 | /* check status */ |
| 1010 | 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^] | 1011 | module->features[j].module, module->features[j].name, NULL)) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1012 | return -1; |
| 1013 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1014 | *feature = &module->features[j]; |
| 1015 | return 0; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1016 | } |
| 1017 | } |
| 1018 | /* ... and all its submodules */ |
| 1019 | for (i = 0; i < module->inc_size; i++) { |
| 1020 | if (!module->inc[i].submodule) { |
| 1021 | /* not yet resolved */ |
| 1022 | continue; |
| 1023 | } |
| 1024 | for (j = 0; j < module->inc[i].submodule->features_size; j++) { |
Michal Vasko | 3def867 | 2016-07-01 11:43:09 +0200 | [diff] [blame] | 1025 | if (!strncmp(name, module->inc[i].submodule->features[j].name, nam_len) |
| 1026 | && !module->inc[i].submodule->features[j].name[nam_len]) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1027 | /* check status */ |
| 1028 | if (lyp_check_status(node->flags, lys_node_module(node), node->name, |
| 1029 | module->inc[i].submodule->features[j].flags, |
| 1030 | module->inc[i].submodule->features[j].module, |
Pavol Vican | fdab9f9 | 2016-09-07 15:23:27 +0200 | [diff] [blame^] | 1031 | module->inc[i].submodule->features[j].name, NULL)) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1032 | return -1; |
| 1033 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1034 | *feature = &module->inc[i].submodule->features[j]; |
| 1035 | return 0; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1036 | } |
| 1037 | } |
| 1038 | } |
| 1039 | |
| 1040 | /* not found */ |
| 1041 | str = strndup(feat_name, len); |
| 1042 | LOGVAL(LYE_INRESOLV, LY_VLOG_NONE, NULL, "feature", str); |
| 1043 | free(str); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1044 | return 1; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1045 | } |
| 1046 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1047 | /* |
| 1048 | * @return |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1049 | * - 1 if enabled |
| 1050 | * - 0 if disabled |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1051 | * - -1 if not usable by its if-feature expression |
| 1052 | */ |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1053 | static int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1054 | resolve_feature_value(const struct lys_feature *feat) |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1055 | { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1056 | int i; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1057 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1058 | for (i = 0; i < feat->iffeature_size; i++) { |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1059 | if (!resolve_iffeature(&feat->iffeature[i])) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1060 | return -1; |
| 1061 | } |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1062 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1063 | |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1064 | return feat->flags & LYS_FENABLED ? 1 : 0; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1065 | } |
| 1066 | |
| 1067 | static int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1068 | 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] | 1069 | { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1070 | uint8_t op; |
| 1071 | int rc, a, b; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1072 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1073 | op = iff_getop(expr->expr, *index_e); |
| 1074 | (*index_e)++; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1075 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1076 | switch (op) { |
| 1077 | case LYS_IFF_F: |
| 1078 | /* resolve feature */ |
| 1079 | return resolve_feature_value(expr->features[(*index_f)++]); |
| 1080 | case LYS_IFF_NOT: |
| 1081 | rc = resolve_iffeature_recursive(expr, index_e, index_f); |
| 1082 | if (rc == -1) { |
| 1083 | /* one of the referenced feature is hidden by its if-feature, |
| 1084 | * so this if-feature expression is always false */ |
| 1085 | return -1; |
| 1086 | } else { |
| 1087 | /* invert result */ |
| 1088 | return rc ? 0 : 1; |
| 1089 | } |
| 1090 | case LYS_IFF_AND: |
| 1091 | case LYS_IFF_OR: |
| 1092 | a = resolve_iffeature_recursive(expr, index_e, index_f); |
| 1093 | b = resolve_iffeature_recursive(expr, index_e, index_f); |
| 1094 | if (a == -1 || b == -1) { |
| 1095 | /* one of the referenced feature is hidden by its if-feature, |
| 1096 | * so this if-feature expression is always false */ |
| 1097 | return -1; |
| 1098 | } else if (op == LYS_IFF_AND) { |
| 1099 | return a && b; |
| 1100 | } else { /* LYS_IFF_OR */ |
| 1101 | return a || b; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1102 | } |
| 1103 | } |
| 1104 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1105 | return -1; |
| 1106 | } |
| 1107 | |
| 1108 | int |
| 1109 | resolve_iffeature(struct lys_iffeature *expr) |
| 1110 | { |
| 1111 | int rc = -1; |
| 1112 | int index_e = 0, index_f = 0; |
| 1113 | |
| 1114 | if (expr->expr) { |
| 1115 | rc = resolve_iffeature_recursive(expr, &index_e, &index_f); |
| 1116 | } |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1117 | return (rc == 1) ? 1 : 0; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1118 | } |
| 1119 | |
| 1120 | struct iff_stack { |
| 1121 | int size; |
| 1122 | int index; /* first empty item */ |
| 1123 | uint8_t *stack; |
| 1124 | }; |
| 1125 | |
| 1126 | static int |
| 1127 | iff_stack_push(struct iff_stack *stack, uint8_t value) |
| 1128 | { |
| 1129 | if (stack->index == stack->size) { |
| 1130 | stack->size += 4; |
| 1131 | stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack); |
| 1132 | if (!stack->stack) { |
| 1133 | LOGMEM; |
| 1134 | stack->size = 0; |
| 1135 | return EXIT_FAILURE; |
| 1136 | } |
| 1137 | } |
| 1138 | |
| 1139 | stack->stack[stack->index++] = value; |
| 1140 | return EXIT_SUCCESS; |
| 1141 | } |
| 1142 | |
| 1143 | static uint8_t |
| 1144 | iff_stack_pop(struct iff_stack *stack) |
| 1145 | { |
| 1146 | stack->index--; |
| 1147 | return stack->stack[stack->index]; |
| 1148 | } |
| 1149 | |
| 1150 | static void |
| 1151 | iff_stack_clean(struct iff_stack *stack) |
| 1152 | { |
| 1153 | stack->size = 0; |
| 1154 | free(stack->stack); |
| 1155 | } |
| 1156 | |
| 1157 | static void |
| 1158 | iff_setop(uint8_t *list, uint8_t op, int pos) |
| 1159 | { |
| 1160 | uint8_t *item; |
| 1161 | uint8_t mask = 3; |
| 1162 | |
| 1163 | assert(pos >= 0); |
| 1164 | assert(op <= 3); /* max 2 bits */ |
| 1165 | |
| 1166 | item = &list[pos / 4]; |
| 1167 | mask = mask << 2 * (pos % 4); |
| 1168 | *item = (*item) & ~mask; |
| 1169 | *item = (*item) | (op << 2 * (pos % 4)); |
| 1170 | } |
| 1171 | |
| 1172 | uint8_t |
| 1173 | iff_getop(uint8_t *list, int pos) |
| 1174 | { |
| 1175 | uint8_t *item; |
| 1176 | uint8_t mask = 3, result; |
| 1177 | |
| 1178 | assert(pos >= 0); |
| 1179 | |
| 1180 | item = &list[pos / 4]; |
| 1181 | result = (*item) & (mask << 2 * (pos % 4)); |
| 1182 | return result >> 2 * (pos % 4); |
| 1183 | } |
| 1184 | |
| 1185 | #define LYS_IFF_LP 0x04 /* ( */ |
| 1186 | #define LYS_IFF_RP 0x08 /* ) */ |
| 1187 | |
| 1188 | void |
| 1189 | resolve_iffeature_getsizes(struct lys_iffeature *iffeat, unsigned int *expr_size, unsigned int *feat_size) |
| 1190 | { |
| 1191 | unsigned int e = 0, f = 0, r = 0; |
| 1192 | uint8_t op; |
| 1193 | |
| 1194 | assert(iffeat); |
| 1195 | |
| 1196 | if (!iffeat->expr) { |
| 1197 | goto result; |
| 1198 | } |
| 1199 | |
| 1200 | do { |
| 1201 | op = iff_getop(iffeat->expr, e++); |
| 1202 | switch (op) { |
| 1203 | case LYS_IFF_NOT: |
| 1204 | if (!r) { |
| 1205 | r += 1; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1206 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1207 | break; |
| 1208 | case LYS_IFF_AND: |
| 1209 | case LYS_IFF_OR: |
| 1210 | if (!r) { |
| 1211 | r += 2; |
| 1212 | } else { |
| 1213 | r += 1; |
| 1214 | } |
| 1215 | break; |
| 1216 | case LYS_IFF_F: |
| 1217 | f++; |
| 1218 | if (r) { |
| 1219 | r--; |
| 1220 | } |
| 1221 | break; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1222 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1223 | } while(r); |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1224 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1225 | result: |
| 1226 | if (expr_size) { |
| 1227 | *expr_size = e; |
| 1228 | } |
| 1229 | if (feat_size) { |
| 1230 | *feat_size = f; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1231 | } |
| 1232 | } |
| 1233 | |
| 1234 | int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1235 | resolve_iffeature_compile(struct lys_iffeature *iffeat_expr, const char *value, struct lys_node *node, |
| 1236 | struct unres_schema *unres) |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1237 | { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1238 | const char *c = value; |
| 1239 | int r, rc = EXIT_FAILURE; |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1240 | int i, j, last_not, checkversion = 0; |
| 1241 | unsigned int f_size = 0, expr_size = 0, f_exp = 1; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1242 | uint8_t op; |
| 1243 | struct iff_stack stack = {0, 0, NULL}; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1244 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1245 | assert(c); |
| 1246 | |
| 1247 | if (isspace(c[0])) { |
| 1248 | LOGVAL(LYE_INCHAR, LY_VLOG_NONE, NULL, c[0], c); |
| 1249 | return EXIT_FAILURE; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1250 | } |
| 1251 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1252 | /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */ |
| 1253 | for (i = j = last_not = 0; c[i]; i++) { |
| 1254 | if (c[i] == '(') { |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1255 | checkversion = 1; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1256 | j++; |
| 1257 | continue; |
| 1258 | } else if (c[i] == ')') { |
| 1259 | j--; |
| 1260 | continue; |
| 1261 | } else if (isspace(c[i])) { |
| 1262 | continue; |
| 1263 | } |
| 1264 | |
| 1265 | if (!strncmp(&c[i], "not", r = 3) || !strncmp(&c[i], "and", r = 3) || !strncmp(&c[i], "or", r = 2)) { |
| 1266 | if (c[i + r] == '\0') { |
Radek Krejci | a98da3f | 2016-07-27 14:05:22 +0200 | [diff] [blame] | 1267 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1268 | return EXIT_FAILURE; |
| 1269 | } else if (!isspace(c[i + r])) { |
| 1270 | /* feature name starting with the not/and/or */ |
| 1271 | last_not = 0; |
| 1272 | f_size++; |
| 1273 | } else if (c[i] == 'n') { /* not operation */ |
| 1274 | if (last_not) { |
| 1275 | /* double not */ |
| 1276 | expr_size = expr_size - 2; |
| 1277 | last_not = 0; |
| 1278 | } else { |
| 1279 | last_not = 1; |
| 1280 | } |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1281 | } else { /* and, or */ |
| 1282 | f_exp++; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1283 | /* not a not operation */ |
| 1284 | last_not = 0; |
| 1285 | } |
| 1286 | i += r; |
| 1287 | } else { |
| 1288 | f_size++; |
| 1289 | last_not = 0; |
| 1290 | } |
| 1291 | expr_size++; |
| 1292 | |
| 1293 | while (!isspace(c[i])) { |
| 1294 | if (!c[i] || c[i] == ')') { |
| 1295 | i--; |
| 1296 | break; |
| 1297 | } |
| 1298 | i++; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1299 | } |
| 1300 | } |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1301 | if (j || f_exp != f_size) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1302 | /* not matching count of ( and ) */ |
| 1303 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
| 1304 | return EXIT_FAILURE; |
| 1305 | } |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1306 | |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1307 | if (checkversion || expr_size > 1) { |
| 1308 | /* check that we have 1.1 module */ |
| 1309 | if (node->module->version != 2) { |
| 1310 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
| 1311 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "YANG 1.1 if-feature expression found in 1.0 module."); |
| 1312 | return EXIT_FAILURE; |
| 1313 | } |
| 1314 | } |
| 1315 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1316 | /* allocate the memory */ |
| 1317 | iffeat_expr->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iffeat_expr->expr); |
| 1318 | iffeat_expr->features = malloc(f_size * sizeof *iffeat_expr->features); |
| 1319 | stack.size = expr_size; |
| 1320 | stack.stack = malloc(expr_size * sizeof *stack.stack); |
| 1321 | if (!stack.stack || !iffeat_expr->expr || !iffeat_expr->features) { |
| 1322 | LOGMEM; |
| 1323 | goto error; |
| 1324 | } |
| 1325 | f_size--; expr_size--; /* used as indexes from now */ |
| 1326 | |
| 1327 | for (i--; i >= 0; i--) { |
| 1328 | if (c[i] == ')') { |
| 1329 | /* push it on stack */ |
| 1330 | iff_stack_push(&stack, LYS_IFF_RP); |
| 1331 | continue; |
| 1332 | } else if (c[i] == '(') { |
| 1333 | /* pop from the stack into result all operators until ) */ |
| 1334 | while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) { |
| 1335 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1336 | } |
| 1337 | continue; |
| 1338 | } else if (isspace(c[i])) { |
| 1339 | continue; |
| 1340 | } |
| 1341 | |
| 1342 | /* end operator or operand -> find beginning and get what is it */ |
| 1343 | j = i + 1; |
| 1344 | while (i >= 0 && !isspace(c[i]) && c[i] != '(') { |
| 1345 | i--; |
| 1346 | } |
| 1347 | i++; /* get back by one step */ |
| 1348 | |
| 1349 | if (!strncmp(&c[i], "not ", 4)) { |
| 1350 | if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) { |
| 1351 | /* double not */ |
| 1352 | iff_stack_pop(&stack); |
| 1353 | } else { |
| 1354 | /* not has the highest priority, so do not pop from the stack |
| 1355 | * as in case of AND and OR */ |
| 1356 | iff_stack_push(&stack, LYS_IFF_NOT); |
| 1357 | } |
| 1358 | } else if (!strncmp(&c[i], "and ", 4)) { |
| 1359 | /* as for OR - pop from the stack all operators with the same or higher |
| 1360 | * priority and store them to the result, then push the AND to the stack */ |
| 1361 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) { |
| 1362 | op = iff_stack_pop(&stack); |
| 1363 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1364 | } |
| 1365 | iff_stack_push(&stack, LYS_IFF_AND); |
| 1366 | } else if (!strncmp(&c[i], "or ", 3)) { |
| 1367 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) { |
| 1368 | op = iff_stack_pop(&stack); |
| 1369 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1370 | } |
| 1371 | iff_stack_push(&stack, LYS_IFF_OR); |
| 1372 | } else { |
| 1373 | /* feature name, length is j - i */ |
| 1374 | |
| 1375 | /* add it to the result */ |
| 1376 | iff_setop(iffeat_expr->expr, LYS_IFF_F, expr_size--); |
| 1377 | |
| 1378 | /* now get the link to the feature definition. Since it can be |
| 1379 | * forward referenced, we have hack for unres - until resolved, |
| 1380 | * the feature name is stored instead of link to the lys_feature */ |
| 1381 | iffeat_expr->features[f_size] = (void*)strndup(&c[i], j - i); |
| 1382 | r = unres_schema_add_node(node->module, unres, &iffeat_expr->features[f_size], UNRES_IFFEAT, node); |
| 1383 | f_size--; |
| 1384 | |
| 1385 | if (r == -1) { |
| 1386 | goto error; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1387 | } |
| 1388 | } |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1389 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1390 | while (stack.index) { |
| 1391 | op = iff_stack_pop(&stack); |
| 1392 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1393 | } |
| 1394 | |
| 1395 | if (++expr_size || ++f_size) { |
| 1396 | /* not all expected operators and operands found */ |
| 1397 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
| 1398 | rc = EXIT_FAILURE; |
| 1399 | } else { |
| 1400 | rc = EXIT_SUCCESS; |
| 1401 | } |
| 1402 | |
| 1403 | error: |
| 1404 | /* cleanup */ |
| 1405 | iff_stack_clean(&stack); |
| 1406 | |
| 1407 | return rc; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1408 | } |
| 1409 | |
| 1410 | /** |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1411 | * @brief Resolve (find) a data node based on a schema-nodeid. |
| 1412 | * |
| 1413 | * Used for resolving unique statements - so id is expected to be relative and local (without reference to a different |
| 1414 | * module). |
| 1415 | * |
| 1416 | */ |
| 1417 | struct lyd_node * |
| 1418 | resolve_data_descendant_schema_nodeid(const char *nodeid, struct lyd_node *start) |
| 1419 | { |
| 1420 | char *str, *token, *p; |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1421 | struct lyd_node *result = NULL, *iter; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1422 | const struct lys_node *schema = NULL; |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1423 | int shorthand = 0; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1424 | |
| 1425 | assert(nodeid && start); |
| 1426 | |
| 1427 | if (nodeid[0] == '/') { |
| 1428 | return NULL; |
| 1429 | } |
| 1430 | |
| 1431 | str = p = strdup(nodeid); |
| 1432 | if (!str) { |
| 1433 | LOGMEM; |
| 1434 | return NULL; |
| 1435 | } |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1436 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1437 | while (p) { |
| 1438 | token = p; |
| 1439 | p = strchr(p, '/'); |
| 1440 | if (p) { |
| 1441 | *p = '\0'; |
| 1442 | p++; |
| 1443 | } |
| 1444 | |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1445 | if (p) { |
| 1446 | /* inner node */ |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1447 | if (resolve_descendant_schema_nodeid(token, schema ? schema->child : start->schema, |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 1448 | LYS_CONTAINER | LYS_CHOICE | LYS_CASE | LYS_LEAF, 0, 0, &schema) |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1449 | || !schema) { |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1450 | result = NULL; |
| 1451 | break; |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1452 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1453 | |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1454 | if (schema->nodetype & (LYS_CHOICE | LYS_CASE)) { |
| 1455 | continue; |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 1456 | } else if (lys_parent(schema)->nodetype == LYS_CHOICE) { |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1457 | /* shorthand case */ |
| 1458 | if (!shorthand) { |
| 1459 | shorthand = 1; |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 1460 | schema = lys_parent(schema); |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1461 | continue; |
| 1462 | } else { |
| 1463 | shorthand = 0; |
| 1464 | if (schema->nodetype == LYS_LEAF) { |
| 1465 | /* should not be here, since we have leaf, which is not a shorthand nor final node */ |
| 1466 | result = NULL; |
| 1467 | break; |
| 1468 | } |
| 1469 | } |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1470 | } |
| 1471 | } else { |
| 1472 | /* final node */ |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 1473 | if (resolve_descendant_schema_nodeid(token, schema ? schema->child : start->schema, LYS_LEAF, |
| 1474 | shorthand ? 0 : 1, 0, &schema) |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1475 | || !schema) { |
| 1476 | result = NULL; |
| 1477 | break; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1478 | } |
| 1479 | } |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1480 | LY_TREE_FOR(result ? result->child : start, iter) { |
| 1481 | if (iter->schema == schema) { |
| 1482 | /* move in data tree according to returned schema */ |
| 1483 | result = iter; |
| 1484 | break; |
| 1485 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1486 | } |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1487 | if (!iter) { |
| 1488 | /* instance not found */ |
| 1489 | result = NULL; |
| 1490 | break; |
| 1491 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1492 | } |
| 1493 | free(str); |
| 1494 | |
| 1495 | return result; |
| 1496 | } |
| 1497 | |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1498 | /* |
| 1499 | * 0 - ok (done) |
| 1500 | * 1 - continue |
| 1501 | * 2 - break |
| 1502 | * -1 - error |
| 1503 | */ |
| 1504 | static int |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1505 | schema_nodeid_siblingcheck(const struct lys_node *sibling, int8_t *shorthand, const char *id, |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1506 | const struct lys_module *module, const char *mod_name, int mod_name_len, |
| 1507 | const struct lys_node **start) |
| 1508 | { |
| 1509 | const struct lys_module *prefix_mod; |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1510 | int sh = 0; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1511 | |
| 1512 | /* module check */ |
| 1513 | prefix_mod = lys_get_import_module(module, NULL, 0, mod_name, mod_name_len); |
| 1514 | if (!prefix_mod) { |
| 1515 | return -1; |
| 1516 | } |
| 1517 | if (prefix_mod != lys_node_module(sibling)) { |
| 1518 | return 1; |
| 1519 | } |
| 1520 | |
| 1521 | /* check for shorthand cases - then 'start' does not change */ |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 1522 | if (lys_parent(sibling) && (lys_parent(sibling)->nodetype == LYS_CHOICE) && (sibling->nodetype != LYS_CASE)) { |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1523 | if (*shorthand != -1) { |
| 1524 | *shorthand = *shorthand ? 0 : 1; |
| 1525 | } |
| 1526 | sh = 1; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1527 | } |
| 1528 | |
| 1529 | /* the result node? */ |
| 1530 | if (!id[0]) { |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1531 | if (*shorthand == 1) { |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1532 | return 1; |
| 1533 | } |
| 1534 | return 0; |
| 1535 | } |
| 1536 | |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1537 | if (!sh) { |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1538 | /* move down the tree, if possible */ |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 1539 | if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1540 | return -1; |
| 1541 | } |
| 1542 | *start = sibling->child; |
| 1543 | } |
| 1544 | |
| 1545 | return 2; |
| 1546 | } |
| 1547 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1548 | /* start - relative, module - absolute, -1 error, EXIT_SUCCESS ok (but ret can still be NULL), >0 unexpected char on ret - 1 */ |
| 1549 | int |
| 1550 | resolve_augment_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_module *module, |
| 1551 | const struct lys_node **ret) |
| 1552 | { |
| 1553 | const char *name, *mod_name, *id; |
| 1554 | const struct lys_node *sibling; |
| 1555 | int r, nam_len, mod_name_len, is_relative = -1; |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1556 | int8_t shorthand = 0; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1557 | /* 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] | 1558 | const struct lys_module *start_mod; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1559 | |
| 1560 | assert(nodeid && (start || module) && !(start && module) && ret); |
| 1561 | |
| 1562 | id = nodeid; |
| 1563 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1564 | 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] | 1565 | return ((id - nodeid) - r) + 1; |
| 1566 | } |
| 1567 | id += r; |
| 1568 | |
| 1569 | if ((is_relative && !start) || (!is_relative && !module)) { |
| 1570 | return -1; |
| 1571 | } |
| 1572 | |
| 1573 | /* descendant-schema-nodeid */ |
| 1574 | if (is_relative) { |
Michal Vasko | 4c06fd8 | 2016-02-17 13:43:38 +0100 | [diff] [blame] | 1575 | module = start_mod = start->module; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1576 | |
| 1577 | /* absolute-schema-nodeid */ |
| 1578 | } else { |
| 1579 | 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] | 1580 | if (!start_mod) { |
| 1581 | return -1; |
| 1582 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1583 | start = start_mod->data; |
| 1584 | } |
| 1585 | |
| 1586 | while (1) { |
| 1587 | sibling = NULL; |
| 1588 | while ((sibling = lys_getnext(sibling, lys_parent(start), start_mod, |
| 1589 | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT))) { |
| 1590 | /* name match */ |
Michal Vasko | 0a1aaa4 | 2016-04-19 09:48:25 +0200 | [diff] [blame] | 1591 | if (sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len]) { |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1592 | r = schema_nodeid_siblingcheck(sibling, &shorthand, id, module, mod_name, mod_name_len, &start); |
| 1593 | if (r == 0) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1594 | *ret = sibling; |
| 1595 | return EXIT_SUCCESS; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1596 | } else if (r == 1) { |
| 1597 | continue; |
| 1598 | } else if (r == 2) { |
| 1599 | break; |
| 1600 | } else { |
| 1601 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1602 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1603 | } |
| 1604 | } |
| 1605 | |
| 1606 | /* no match */ |
| 1607 | if (!sibling) { |
Michal Vasko | a426fef | 2016-03-07 10:47:31 +0100 | [diff] [blame] | 1608 | *ret = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1609 | return EXIT_SUCCESS; |
| 1610 | } |
| 1611 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1612 | 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] | 1613 | return ((id - nodeid) - r) + 1; |
| 1614 | } |
| 1615 | id += r; |
| 1616 | } |
| 1617 | |
| 1618 | /* cannot get here */ |
| 1619 | LOGINT; |
| 1620 | return -1; |
| 1621 | } |
| 1622 | |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 1623 | /* unique, refine, |
| 1624 | * >0 - unexpected char on position (ret - 1), |
| 1625 | * 0 - ok (but ret can still be NULL), |
| 1626 | * -1 - error, |
| 1627 | * -2 - violated no_innerlist */ |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1628 | int |
| 1629 | resolve_descendant_schema_nodeid(const char *nodeid, const struct lys_node *start, int ret_nodetype, |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 1630 | int check_shorthand, int no_innerlist, const struct lys_node **ret) |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1631 | { |
| 1632 | const char *name, *mod_name, *id; |
| 1633 | const struct lys_node *sibling; |
| 1634 | int r, nam_len, mod_name_len, is_relative = -1; |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1635 | int8_t shorthand = check_shorthand ? 0 : -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1636 | /* 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] | 1637 | const struct lys_module *module; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1638 | |
| 1639 | assert(nodeid && start && ret); |
| 1640 | assert(!(ret_nodetype & (LYS_USES | LYS_AUGMENT)) && ((ret_nodetype == LYS_GROUPING) || !(ret_nodetype & LYS_GROUPING))); |
| 1641 | |
| 1642 | id = nodeid; |
| 1643 | module = start->module; |
| 1644 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1645 | 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] | 1646 | return ((id - nodeid) - r) + 1; |
| 1647 | } |
| 1648 | id += r; |
| 1649 | |
| 1650 | if (!is_relative) { |
| 1651 | return -1; |
| 1652 | } |
| 1653 | |
| 1654 | while (1) { |
| 1655 | sibling = NULL; |
| 1656 | while ((sibling = lys_getnext(sibling, lys_parent(start), module, |
| 1657 | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE))) { |
| 1658 | /* name match */ |
| 1659 | if (sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len]) { |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1660 | r = schema_nodeid_siblingcheck(sibling, &shorthand, id, module, mod_name, mod_name_len, &start); |
| 1661 | if (r == 0) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1662 | if (!(sibling->nodetype & ret_nodetype)) { |
| 1663 | /* wrong node type, too bad */ |
| 1664 | continue; |
| 1665 | } |
| 1666 | *ret = sibling; |
| 1667 | return EXIT_SUCCESS; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1668 | } else if (r == 1) { |
| 1669 | continue; |
| 1670 | } else if (r == 2) { |
| 1671 | break; |
| 1672 | } else { |
| 1673 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1674 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1675 | } |
| 1676 | } |
| 1677 | |
| 1678 | /* no match */ |
| 1679 | if (!sibling) { |
Michal Vasko | a426fef | 2016-03-07 10:47:31 +0100 | [diff] [blame] | 1680 | *ret = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1681 | return EXIT_SUCCESS; |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 1682 | } else if (no_innerlist && sibling->nodetype == LYS_LIST) { |
| 1683 | *ret = NULL; |
| 1684 | return -2; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1685 | } |
| 1686 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1687 | 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] | 1688 | return ((id - nodeid) - r) + 1; |
| 1689 | } |
| 1690 | id += r; |
| 1691 | } |
| 1692 | |
| 1693 | /* cannot get here */ |
| 1694 | LOGINT; |
| 1695 | return -1; |
| 1696 | } |
| 1697 | |
| 1698 | /* choice default */ |
| 1699 | int |
| 1700 | resolve_choice_default_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_node **ret) |
| 1701 | { |
| 1702 | /* cannot actually be a path */ |
| 1703 | if (strchr(nodeid, '/')) { |
| 1704 | return -1; |
| 1705 | } |
| 1706 | |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 1707 | return resolve_descendant_schema_nodeid(nodeid, start, LYS_NO_RPC_NOTIF_NODE, 1, 0, ret); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1708 | } |
| 1709 | |
| 1710 | /* uses, -1 error, EXIT_SUCCESS ok (but ret can still be NULL), >0 unexpected char on ret - 1 */ |
| 1711 | static int |
| 1712 | resolve_uses_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_node_grp **ret) |
| 1713 | { |
| 1714 | const struct lys_module *module; |
| 1715 | const char *mod_prefix, *name; |
| 1716 | int i, mod_prefix_len, nam_len; |
| 1717 | |
| 1718 | /* parse the identifier, it must be parsed on one call */ |
| 1719 | if (((i = parse_node_identifier(nodeid, &mod_prefix, &mod_prefix_len, &name, &nam_len)) < 1) || nodeid[i]) { |
| 1720 | return -i + 1; |
| 1721 | } |
| 1722 | |
| 1723 | module = lys_get_import_module(start->module, mod_prefix, mod_prefix_len, NULL, 0); |
| 1724 | if (!module) { |
| 1725 | return -1; |
| 1726 | } |
| 1727 | if (module != start->module) { |
| 1728 | start = module->data; |
| 1729 | } |
| 1730 | |
| 1731 | *ret = lys_find_grouping_up(name, (struct lys_node *)start); |
| 1732 | |
| 1733 | return EXIT_SUCCESS; |
| 1734 | } |
| 1735 | |
| 1736 | int |
| 1737 | resolve_absolute_schema_nodeid(const char *nodeid, const struct lys_module *module, int ret_nodetype, |
| 1738 | const struct lys_node **ret) |
| 1739 | { |
| 1740 | const char *name, *mod_name, *id; |
| 1741 | const struct lys_node *sibling, *start; |
| 1742 | int r, nam_len, mod_name_len, is_relative = -1; |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1743 | int8_t shorthand = 0; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1744 | const struct lys_module *abs_start_mod; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1745 | |
| 1746 | assert(nodeid && module && ret); |
| 1747 | assert(!(ret_nodetype & (LYS_USES | LYS_AUGMENT)) && ((ret_nodetype == LYS_GROUPING) || !(ret_nodetype & LYS_GROUPING))); |
| 1748 | |
| 1749 | id = nodeid; |
| 1750 | start = module->data; |
| 1751 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1752 | 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] | 1753 | return ((id - nodeid) - r) + 1; |
| 1754 | } |
| 1755 | id += r; |
| 1756 | |
| 1757 | if (is_relative) { |
| 1758 | return -1; |
| 1759 | } |
| 1760 | |
| 1761 | 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] | 1762 | if (!abs_start_mod) { |
| 1763 | return -1; |
| 1764 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1765 | |
| 1766 | while (1) { |
| 1767 | sibling = NULL; |
| 1768 | while ((sibling = lys_getnext(sibling, lys_parent(start), abs_start_mod, LYS_GETNEXT_WITHCHOICE |
| 1769 | | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT | LYS_GETNEXT_WITHGROUPING))) { |
| 1770 | /* name match */ |
Michal Vasko | 0a1aaa4 | 2016-04-19 09:48:25 +0200 | [diff] [blame] | 1771 | if (sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len]) { |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1772 | r = schema_nodeid_siblingcheck(sibling, &shorthand, id, module, mod_name, mod_name_len, &start); |
| 1773 | if (r == 0) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1774 | if (!(sibling->nodetype & ret_nodetype)) { |
| 1775 | /* wrong node type, too bad */ |
| 1776 | continue; |
| 1777 | } |
| 1778 | *ret = sibling; |
| 1779 | return EXIT_SUCCESS; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1780 | } else if (r == 1) { |
| 1781 | continue; |
| 1782 | } else if (r == 2) { |
| 1783 | break; |
| 1784 | } else { |
| 1785 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1786 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1787 | } |
| 1788 | } |
| 1789 | |
| 1790 | /* no match */ |
| 1791 | if (!sibling) { |
Michal Vasko | a426fef | 2016-03-07 10:47:31 +0100 | [diff] [blame] | 1792 | *ret = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1793 | return EXIT_SUCCESS; |
| 1794 | } |
| 1795 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1796 | 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] | 1797 | return ((id - nodeid) - r) + 1; |
| 1798 | } |
| 1799 | id += r; |
| 1800 | } |
| 1801 | |
| 1802 | /* cannot get here */ |
| 1803 | LOGINT; |
| 1804 | return -1; |
| 1805 | } |
| 1806 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1807 | static int |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1808 | 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] | 1809 | { |
| 1810 | const char *name; |
| 1811 | int nam_len, has_predicate, i; |
| 1812 | |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 1813 | if (((i = parse_schema_json_predicate(predicate, &name, &nam_len, NULL, NULL, &has_predicate)) < 1) |
| 1814 | || !strncmp(name, ".", nam_len)) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 1815 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-i], &predicate[-i]); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1816 | return -1; |
| 1817 | } |
| 1818 | |
| 1819 | predicate += i; |
| 1820 | *parsed += i; |
| 1821 | |
| 1822 | for (i = 0; i < list->keys_size; ++i) { |
| 1823 | if (!strncmp(list->keys[i]->name, name, nam_len) && !list->keys[i]->name[nam_len]) { |
| 1824 | break; |
| 1825 | } |
| 1826 | } |
| 1827 | |
| 1828 | if (i == list->keys_size) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 1829 | LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1830 | return -1; |
| 1831 | } |
| 1832 | |
| 1833 | /* more predicates? */ |
| 1834 | if (has_predicate) { |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1835 | return resolve_json_schema_list_predicate(predicate, list, parsed); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1836 | } |
| 1837 | |
| 1838 | return 0; |
| 1839 | } |
| 1840 | |
Michal Vasko | 9fd98e2 | 2016-04-07 15:44:19 +0200 | [diff] [blame] | 1841 | /* cannot return LYS_GROUPING, LYS_AUGMENT, LYS_USES, logs directly |
| 1842 | * data_nodeid - 0 schema nodeid, 1 - data nodeid with RPC input, 2 - data nodeid with RPC output */ |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1843 | const struct lys_node * |
Michal Vasko | a9c8e99 | 2016-07-26 16:27:05 +0200 | [diff] [blame] | 1844 | resolve_json_nodeid(const char *nodeid, struct ly_ctx *ctx, const struct lys_node *start, int data_nodeid) |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1845 | { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 1846 | char *module_name = ly_buf(), *buf_backup = NULL, *str; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1847 | const char *name, *mod_name, *id; |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1848 | const struct lys_node *sibling; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1849 | int r, nam_len, mod_name_len, is_relative = -1, has_predicate, shorthand = 0; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1850 | /* 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] | 1851 | const struct lys_module *prefix_mod, *module, *prev_mod; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1852 | |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1853 | assert(nodeid && (ctx || start)); |
| 1854 | if (!ctx) { |
| 1855 | ctx = start->module->ctx; |
| 1856 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1857 | |
| 1858 | id = nodeid; |
| 1859 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1860 | 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] | 1861 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1862 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1863 | } |
| 1864 | id += r; |
| 1865 | |
| 1866 | if (is_relative) { |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1867 | assert(start); |
| 1868 | start = start->child; |
| 1869 | if (!start) { |
| 1870 | /* no descendants, fail for sure */ |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 1871 | str = strndup(nodeid, (name + nam_len) - nodeid); |
| 1872 | LOGVAL(LYE_PATH_INNODE, LY_VLOG_STR, str); |
| 1873 | free(str); |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1874 | return NULL; |
| 1875 | } |
| 1876 | module = start->module; |
| 1877 | } else { |
| 1878 | if (!mod_name) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 1879 | str = strndup(nodeid, (name + nam_len) - nodeid); |
| 1880 | LOGVAL(LYE_PATH_MISSMOD, LY_VLOG_STR, nodeid); |
| 1881 | free(str); |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1882 | return NULL; |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 1883 | } else if (mod_name_len > LY_BUF_SIZE - 1) { |
| 1884 | LOGINT; |
| 1885 | return NULL; |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1886 | } |
| 1887 | |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 1888 | if (ly_buf_used && module_name[0]) { |
| 1889 | buf_backup = strndup(module_name, LY_BUF_SIZE - 1); |
| 1890 | } |
| 1891 | ly_buf_used++; |
| 1892 | |
Michal Vasko | df3f1ab | 2016-04-01 15:07:22 +0200 | [diff] [blame] | 1893 | memmove(module_name, mod_name, mod_name_len); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 1894 | module_name[mod_name_len] = '\0'; |
| 1895 | module = ly_ctx_get_module(ctx, module_name, NULL); |
| 1896 | |
| 1897 | if (buf_backup) { |
| 1898 | /* return previous internal buffer content */ |
| 1899 | strcpy(module_name, buf_backup); |
| 1900 | free(buf_backup); |
| 1901 | buf_backup = NULL; |
| 1902 | } |
| 1903 | ly_buf_used--; |
| 1904 | |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1905 | if (!module) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 1906 | str = strndup(nodeid, (mod_name + mod_name_len) - nodeid); |
| 1907 | LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str); |
| 1908 | free(str); |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1909 | return NULL; |
| 1910 | } |
| 1911 | start = module->data; |
| 1912 | |
| 1913 | /* now it's as if there was no module name */ |
| 1914 | mod_name = NULL; |
| 1915 | mod_name_len = 0; |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1916 | } |
| 1917 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1918 | prev_mod = module; |
| 1919 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1920 | while (1) { |
| 1921 | sibling = NULL; |
Michal Vasko | 9fd98e2 | 2016-04-07 15:44:19 +0200 | [diff] [blame] | 1922 | while ((sibling = lys_getnext(sibling, lys_parent(start), module, (data_nodeid ? |
| 1923 | 0 : LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT)))) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1924 | /* name match */ |
Michal Vasko | 0a1aaa4 | 2016-04-19 09:48:25 +0200 | [diff] [blame] | 1925 | if (sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len]) { |
Michal Vasko | 9fd98e2 | 2016-04-07 15:44:19 +0200 | [diff] [blame] | 1926 | |
| 1927 | /* data RPC input/output check */ |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 1928 | if ((data_nodeid == 1) && lys_parent(sibling) && (lys_parent(sibling)->nodetype == LYS_OUTPUT)) { |
Michal Vasko | 9fd98e2 | 2016-04-07 15:44:19 +0200 | [diff] [blame] | 1929 | continue; |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 1930 | } else if ((data_nodeid == 2) && lys_parent(sibling) && (lys_parent(sibling)->nodetype == LYS_INPUT)) { |
Michal Vasko | 9fd98e2 | 2016-04-07 15:44:19 +0200 | [diff] [blame] | 1931 | continue; |
| 1932 | } |
| 1933 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1934 | /* module check */ |
| 1935 | if (mod_name) { |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 1936 | if (mod_name_len > LY_BUF_SIZE - 1) { |
Michal Vasko | 8757e7c | 2016-03-15 10:41:30 +0100 | [diff] [blame] | 1937 | LOGINT; |
| 1938 | return NULL; |
| 1939 | } |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 1940 | |
| 1941 | if (ly_buf_used && module_name[0]) { |
| 1942 | buf_backup = strndup(module_name, LY_BUF_SIZE - 1); |
| 1943 | } |
| 1944 | ly_buf_used++; |
| 1945 | |
Michal Vasko | df3f1ab | 2016-04-01 15:07:22 +0200 | [diff] [blame] | 1946 | memmove(module_name, mod_name, mod_name_len); |
Michal Vasko | 8757e7c | 2016-03-15 10:41:30 +0100 | [diff] [blame] | 1947 | module_name[mod_name_len] = '\0'; |
| 1948 | /* will also find an augment module */ |
| 1949 | prefix_mod = ly_ctx_get_module(ctx, module_name, NULL); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 1950 | |
| 1951 | if (buf_backup) { |
| 1952 | /* return previous internal buffer content */ |
Michal Vasko | 888a129 | 2016-04-05 12:08:54 +0200 | [diff] [blame] | 1953 | strncpy(module_name, buf_backup, LY_BUF_SIZE - 1); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 1954 | free(buf_backup); |
| 1955 | buf_backup = NULL; |
| 1956 | } |
| 1957 | ly_buf_used--; |
| 1958 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1959 | if (!prefix_mod) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 1960 | str = strndup(nodeid, (mod_name + mod_name_len) - nodeid); |
| 1961 | LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str); |
| 1962 | free(str); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1963 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1964 | } |
| 1965 | } else { |
| 1966 | prefix_mod = prev_mod; |
| 1967 | } |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 1968 | if (prefix_mod != lys_node_module(sibling)) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1969 | continue; |
| 1970 | } |
| 1971 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1972 | /* do we have some predicates on it? */ |
| 1973 | if (has_predicate) { |
| 1974 | r = 0; |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 1975 | if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 1976 | if ((r = parse_schema_json_predicate(id, NULL, NULL, NULL, NULL, &has_predicate)) < 1) { |
| 1977 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
| 1978 | return NULL; |
| 1979 | } |
| 1980 | } else if (sibling->nodetype == LYS_LIST) { |
| 1981 | if (resolve_json_schema_list_predicate(id, (const struct lys_node_list *)sibling, &r)) { |
| 1982 | return NULL; |
| 1983 | } |
| 1984 | } else { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 1985 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1986 | return NULL; |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1987 | } |
| 1988 | id += r; |
| 1989 | } |
| 1990 | |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1991 | /* check for shorthand cases - then 'start' does not change */ |
Michal Vasko | 3c0f9f5 | 2016-05-17 16:38:10 +0200 | [diff] [blame] | 1992 | if (!data_nodeid && lys_parent(sibling) && (lys_parent(sibling)->nodetype == LYS_CHOICE) && (sibling->nodetype != LYS_CASE)) { |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1993 | shorthand = ~shorthand; |
| 1994 | } |
| 1995 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1996 | /* the result node? */ |
| 1997 | if (!id[0]) { |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1998 | if (shorthand) { |
| 1999 | /* wrong path for shorthand */ |
Michal Vasko | 025e045 | 2016-05-17 16:14:20 +0200 | [diff] [blame] | 2000 | str = strndup(nodeid, (name + nam_len) - nodeid); |
| 2001 | LOGVAL(LYE_PATH_INNODE, LY_VLOG_STR, str); |
Michal Vasko | 3c0f9f5 | 2016-05-17 16:38:10 +0200 | [diff] [blame] | 2002 | LOGVAL(LYE_SPEC, LY_VLOG_STR, str, "Schema shorthand case path must include the virtual case statement."); |
Radek Krejci | 9a5fccc | 2016-05-18 15:44:58 +0200 | [diff] [blame] | 2003 | free(str); |
Michal Vasko | 025e045 | 2016-05-17 16:14:20 +0200 | [diff] [blame] | 2004 | return NULL; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 2005 | } |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2006 | return sibling; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2007 | } |
| 2008 | |
Michal Vasko | 3c0f9f5 | 2016-05-17 16:38:10 +0200 | [diff] [blame] | 2009 | if (data_nodeid || !shorthand) { |
Michal Vasko | 7dc71d0 | 2016-03-15 10:42:28 +0100 | [diff] [blame] | 2010 | /* move down the tree, if possible */ |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 2011 | if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2012 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
Michal Vasko | 7dc71d0 | 2016-03-15 10:42:28 +0100 | [diff] [blame] | 2013 | return NULL; |
| 2014 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2015 | start = sibling->child; |
| 2016 | } |
| 2017 | |
| 2018 | /* update prev mod */ |
| 2019 | prev_mod = start->module; |
| 2020 | break; |
| 2021 | } |
| 2022 | } |
| 2023 | |
| 2024 | /* no match */ |
| 2025 | if (!sibling) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2026 | str = strndup(nodeid, (name + nam_len) - nodeid); |
| 2027 | LOGVAL(LYE_PATH_INNODE, LY_VLOG_STR, str); |
| 2028 | free(str); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2029 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2030 | } |
| 2031 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2032 | 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] | 2033 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2034 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2035 | } |
| 2036 | id += r; |
| 2037 | } |
| 2038 | |
| 2039 | /* cannot get here */ |
| 2040 | LOGINT; |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2041 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2042 | } |
| 2043 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2044 | static int |
| 2045 | resolve_partial_json_data_list_predicate(const char *predicate, const char *node_name, struct lyd_node *node, int *parsed) |
| 2046 | { |
| 2047 | const char *name, *value; |
| 2048 | int nam_len, val_len, has_predicate = 1, r; |
| 2049 | uint16_t i; |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2050 | struct lyd_node_leaf_list *key; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2051 | |
Radek Krejci | 61a86c6 | 2016-03-24 11:06:44 +0100 | [diff] [blame] | 2052 | assert(node); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2053 | assert(node->schema->nodetype == LYS_LIST); |
| 2054 | |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2055 | key = (struct lyd_node_leaf_list *)node->child; |
| 2056 | for (i = 0; i < ((struct lys_node_list *)node->schema)->keys_size; ++i) { |
| 2057 | if (!key) { |
| 2058 | /* invalid data */ |
| 2059 | LOGINT; |
| 2060 | return -1; |
| 2061 | } |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2062 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2063 | if (!has_predicate) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2064 | LOGVAL(LYE_PATH_MISSKEY, LY_VLOG_NONE, NULL, node_name); |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2065 | return -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2066 | } |
| 2067 | |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 2068 | if (((r = parse_schema_json_predicate(predicate, &name, &nam_len, &value, &val_len, &has_predicate)) < 1) |
| 2069 | || !strncmp(name, ".", nam_len)) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2070 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-r], &predicate[-r]); |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2071 | return -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2072 | } |
| 2073 | |
| 2074 | predicate += r; |
| 2075 | *parsed += r; |
| 2076 | |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2077 | 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] | 2078 | LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name); |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2079 | return -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2080 | } |
| 2081 | |
| 2082 | /* value does not match */ |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2083 | if (strncmp(key->value_str, value, val_len) || key->value_str[val_len]) { |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2084 | return 1; |
| 2085 | } |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2086 | |
| 2087 | key = (struct lyd_node_leaf_list *)key->next; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2088 | } |
| 2089 | |
| 2090 | if (has_predicate) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2091 | LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2092 | return -1; |
| 2093 | } |
| 2094 | |
| 2095 | return 0; |
| 2096 | } |
| 2097 | |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 2098 | /** |
| 2099 | * @brief get the closest parent of the node (or the node itself) identified by the nodeid (path) |
| 2100 | * |
| 2101 | * @param[in] nodeid Node data path to find |
| 2102 | * @param[in] llist_value If the \p nodeid identifies leaf-list, this is expected value of the leaf-list instance. |
| 2103 | * @param[in] options Bitmask of options flags, see @ref pathoptions. |
| 2104 | * @param[out] parsed Number of characters processed in \p id |
| 2105 | * @return The closes parent (or the node itself) from the path |
| 2106 | */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2107 | struct lyd_node * |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2108 | resolve_partial_json_data_nodeid(const char *nodeid, const char *llist_value, struct lyd_node *start, int options, |
| 2109 | int *parsed) |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2110 | { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2111 | char *module_name = ly_buf(), *buf_backup = NULL, *str; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2112 | const char *id, *mod_name, *name; |
| 2113 | int r, ret, mod_name_len, nam_len, is_relative = -1, has_predicate, last_parsed; |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2114 | struct lyd_node *sibling, *last_match = NULL; |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2115 | struct lyd_node_leaf_list *llist; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2116 | const struct lys_module *prefix_mod, *prev_mod; |
| 2117 | struct ly_ctx *ctx; |
| 2118 | |
| 2119 | assert(nodeid && start && parsed); |
| 2120 | |
| 2121 | ctx = start->schema->module->ctx; |
| 2122 | id = nodeid; |
| 2123 | |
| 2124 | 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] | 2125 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2126 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2127 | return NULL; |
| 2128 | } |
| 2129 | id += r; |
| 2130 | /* add it to parsed only after the data node was actually found */ |
| 2131 | last_parsed = r; |
| 2132 | |
| 2133 | if (is_relative) { |
| 2134 | prev_mod = start->schema->module; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2135 | start = start->child; |
| 2136 | } else { |
| 2137 | for (; start->parent; start = start->parent); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2138 | prev_mod = start->schema->module; |
| 2139 | } |
| 2140 | |
| 2141 | while (1) { |
| 2142 | LY_TREE_FOR(start, sibling) { |
Michal Vasko | 2411b94 | 2016-03-23 13:50:03 +0100 | [diff] [blame] | 2143 | /* RPC data check, return simply invalid argument, because the data tree is invalid */ |
| 2144 | if (lys_parent(sibling->schema)) { |
| 2145 | if (options & LYD_PATH_OPT_OUTPUT) { |
| 2146 | if (lys_parent(sibling->schema)->nodetype == LYS_INPUT) { |
Michal Vasko | b724689 | 2016-04-19 10:37:35 +0200 | [diff] [blame] | 2147 | 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] | 2148 | *parsed = -1; |
| 2149 | return NULL; |
| 2150 | } |
| 2151 | } else { |
| 2152 | if (lys_parent(sibling->schema)->nodetype == LYS_OUTPUT) { |
Michal Vasko | b724689 | 2016-04-19 10:37:35 +0200 | [diff] [blame] | 2153 | 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] | 2154 | *parsed = -1; |
| 2155 | return NULL; |
| 2156 | } |
| 2157 | } |
| 2158 | } |
| 2159 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2160 | /* name match */ |
| 2161 | if (!strncmp(name, sibling->schema->name, nam_len) && !sibling->schema->name[nam_len]) { |
| 2162 | |
| 2163 | /* module check */ |
| 2164 | if (mod_name) { |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2165 | if (mod_name_len > LY_BUF_SIZE - 1) { |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2166 | LOGINT; |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2167 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2168 | return NULL; |
| 2169 | } |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2170 | |
| 2171 | if (ly_buf_used && module_name[0]) { |
| 2172 | buf_backup = strndup(module_name, LY_BUF_SIZE - 1); |
| 2173 | } |
| 2174 | ly_buf_used++; |
| 2175 | |
Michal Vasko | df3f1ab | 2016-04-01 15:07:22 +0200 | [diff] [blame] | 2176 | memmove(module_name, mod_name, mod_name_len); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2177 | module_name[mod_name_len] = '\0'; |
| 2178 | /* will also find an augment module */ |
| 2179 | prefix_mod = ly_ctx_get_module(ctx, module_name, NULL); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2180 | |
| 2181 | if (buf_backup) { |
| 2182 | /* return previous internal buffer content */ |
Michal Vasko | 888a129 | 2016-04-05 12:08:54 +0200 | [diff] [blame] | 2183 | strncpy(module_name, buf_backup, LY_BUF_SIZE - 1); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2184 | free(buf_backup); |
| 2185 | buf_backup = NULL; |
| 2186 | } |
| 2187 | ly_buf_used--; |
| 2188 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2189 | if (!prefix_mod) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2190 | str = strndup(nodeid, (mod_name + mod_name_len) - nodeid); |
| 2191 | LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str); |
| 2192 | free(str); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2193 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2194 | return NULL; |
| 2195 | } |
| 2196 | } else { |
| 2197 | prefix_mod = prev_mod; |
| 2198 | } |
| 2199 | if (prefix_mod != lys_node_module(sibling->schema)) { |
| 2200 | continue; |
| 2201 | } |
| 2202 | |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2203 | /* leaf-list, did we find it with the correct value or not? */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2204 | if (sibling->schema->nodetype == LYS_LEAFLIST) { |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2205 | llist = (struct lyd_node_leaf_list *)sibling; |
| 2206 | if ((!llist_value && llist->value_str && llist->value_str[0]) |
| 2207 | || (llist_value && strcmp(llist_value, llist->value_str))) { |
| 2208 | continue; |
| 2209 | } |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 2210 | } else if (sibling->schema->nodetype == LYS_LIST) { |
| 2211 | /* list, we need predicates'n'stuff then */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2212 | r = 0; |
| 2213 | if (!has_predicate) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2214 | LOGVAL(LYE_PATH_MISSKEY, LY_VLOG_NONE, NULL, name); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2215 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2216 | return NULL; |
| 2217 | } |
| 2218 | ret = resolve_partial_json_data_list_predicate(id, name, sibling, &r); |
| 2219 | if (ret == -1) { |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2220 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2221 | return NULL; |
| 2222 | } else if (ret == 1) { |
| 2223 | /* this list instance does not match */ |
| 2224 | continue; |
| 2225 | } |
| 2226 | id += r; |
| 2227 | last_parsed += r; |
| 2228 | } |
| 2229 | |
| 2230 | *parsed += last_parsed; |
| 2231 | |
| 2232 | /* the result node? */ |
| 2233 | if (!id[0]) { |
| 2234 | return sibling; |
| 2235 | } |
| 2236 | |
| 2237 | /* move down the tree, if possible */ |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 2238 | if (sibling->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2239 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2240 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2241 | return NULL; |
| 2242 | } |
Michal Vasko | c6c52cf | 2016-03-29 11:53:04 +0200 | [diff] [blame] | 2243 | last_match = sibling; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2244 | start = sibling->child; |
| 2245 | if (start) { |
| 2246 | prev_mod = start->schema->module; |
| 2247 | } |
| 2248 | break; |
| 2249 | } |
| 2250 | } |
| 2251 | |
| 2252 | /* no match, return last match */ |
| 2253 | if (!sibling) { |
| 2254 | return last_match; |
| 2255 | } |
| 2256 | |
| 2257 | 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] | 2258 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2259 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2260 | return NULL; |
| 2261 | } |
| 2262 | id += r; |
| 2263 | last_parsed = r; |
| 2264 | } |
| 2265 | |
| 2266 | /* cannot get here */ |
| 2267 | LOGINT; |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2268 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2269 | return NULL; |
| 2270 | } |
| 2271 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2272 | /** |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2273 | * @brief Resolves length or range intervals. Does not log. |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2274 | * Syntax is assumed to be correct, *ret MUST be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2275 | * |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2276 | * @param[in] str_restr Restriction as a string. |
| 2277 | * @param[in] type Type of the restriction. |
| 2278 | * @param[out] ret Final interval structure that starts with |
| 2279 | * the interval of the initial type, continues with intervals |
| 2280 | * of any superior types derived from the initial one, and |
| 2281 | * finishes with intervals from our \p type. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2282 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2283 | * @return EXIT_SUCCESS on succes, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2284 | */ |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2285 | int |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2286 | 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] | 2287 | { |
| 2288 | /* 0 - unsigned, 1 - signed, 2 - floating point */ |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2289 | int kind; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2290 | int64_t local_smin, local_smax; |
| 2291 | uint64_t local_umin, local_umax; |
| 2292 | long double local_fmin, local_fmax; |
| 2293 | const char *seg_ptr, *ptr; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2294 | 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] | 2295 | |
| 2296 | switch (type->base) { |
| 2297 | case LY_TYPE_BINARY: |
| 2298 | kind = 0; |
| 2299 | local_umin = 0; |
| 2300 | local_umax = 18446744073709551615UL; |
| 2301 | |
| 2302 | if (!str_restr && type->info.binary.length) { |
| 2303 | str_restr = type->info.binary.length->expr; |
| 2304 | } |
| 2305 | break; |
| 2306 | case LY_TYPE_DEC64: |
| 2307 | kind = 2; |
Radek Krejci | 8c3b4b6 | 2016-06-17 14:32:12 +0200 | [diff] [blame] | 2308 | local_fmin = ((long double)-9223372036854775808.0) / type->info.dec64.div; |
| 2309 | local_fmax = ((long double)9223372036854775807.0) / type->info.dec64.div; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2310 | |
| 2311 | if (!str_restr && type->info.dec64.range) { |
| 2312 | str_restr = type->info.dec64.range->expr; |
| 2313 | } |
| 2314 | break; |
| 2315 | case LY_TYPE_INT8: |
| 2316 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2317 | local_smin = __INT64_C(-128); |
| 2318 | local_smax = __INT64_C(127); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2319 | |
| 2320 | if (!str_restr && type->info.num.range) { |
| 2321 | str_restr = type->info.num.range->expr; |
| 2322 | } |
| 2323 | break; |
| 2324 | case LY_TYPE_INT16: |
| 2325 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2326 | local_smin = __INT64_C(-32768); |
| 2327 | local_smax = __INT64_C(32767); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2328 | |
| 2329 | if (!str_restr && type->info.num.range) { |
| 2330 | str_restr = type->info.num.range->expr; |
| 2331 | } |
| 2332 | break; |
| 2333 | case LY_TYPE_INT32: |
| 2334 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2335 | local_smin = __INT64_C(-2147483648); |
| 2336 | local_smax = __INT64_C(2147483647); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2337 | |
| 2338 | if (!str_restr && type->info.num.range) { |
| 2339 | str_restr = type->info.num.range->expr; |
| 2340 | } |
| 2341 | break; |
| 2342 | case LY_TYPE_INT64: |
| 2343 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2344 | local_smin = __INT64_C(-9223372036854775807) - __INT64_C(1); |
| 2345 | local_smax = __INT64_C(9223372036854775807); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2346 | |
| 2347 | if (!str_restr && type->info.num.range) { |
| 2348 | str_restr = type->info.num.range->expr; |
| 2349 | } |
| 2350 | break; |
| 2351 | case LY_TYPE_UINT8: |
| 2352 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2353 | local_umin = __UINT64_C(0); |
| 2354 | local_umax = __UINT64_C(255); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2355 | |
| 2356 | if (!str_restr && type->info.num.range) { |
| 2357 | str_restr = type->info.num.range->expr; |
| 2358 | } |
| 2359 | break; |
| 2360 | case LY_TYPE_UINT16: |
| 2361 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2362 | local_umin = __UINT64_C(0); |
| 2363 | local_umax = __UINT64_C(65535); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2364 | |
| 2365 | if (!str_restr && type->info.num.range) { |
| 2366 | str_restr = type->info.num.range->expr; |
| 2367 | } |
| 2368 | break; |
| 2369 | case LY_TYPE_UINT32: |
| 2370 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2371 | local_umin = __UINT64_C(0); |
| 2372 | local_umax = __UINT64_C(4294967295); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2373 | |
| 2374 | if (!str_restr && type->info.num.range) { |
| 2375 | str_restr = type->info.num.range->expr; |
| 2376 | } |
| 2377 | break; |
| 2378 | case LY_TYPE_UINT64: |
| 2379 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2380 | local_umin = __UINT64_C(0); |
| 2381 | local_umax = __UINT64_C(18446744073709551615); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2382 | |
| 2383 | if (!str_restr && type->info.num.range) { |
| 2384 | str_restr = type->info.num.range->expr; |
| 2385 | } |
| 2386 | break; |
| 2387 | case LY_TYPE_STRING: |
| 2388 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2389 | local_umin = __UINT64_C(0); |
| 2390 | local_umax = __UINT64_C(18446744073709551615); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2391 | |
| 2392 | if (!str_restr && type->info.str.length) { |
| 2393 | str_restr = type->info.str.length->expr; |
| 2394 | } |
| 2395 | break; |
| 2396 | default: |
| 2397 | LOGINT; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2398 | return -1; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2399 | } |
| 2400 | |
| 2401 | /* process superior types */ |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2402 | if (type->der) { |
| 2403 | if (resolve_len_ran_interval(NULL, &type->der->type, &intv)) { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2404 | LOGINT; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2405 | return -1; |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2406 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2407 | assert(!intv || (intv->kind == kind)); |
| 2408 | } |
| 2409 | |
| 2410 | if (!str_restr) { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2411 | /* we do not have any restriction, return superior ones */ |
| 2412 | *ret = intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2413 | return EXIT_SUCCESS; |
| 2414 | } |
| 2415 | |
| 2416 | /* adjust local min and max */ |
| 2417 | if (intv) { |
| 2418 | tmp_intv = intv; |
| 2419 | |
| 2420 | if (kind == 0) { |
| 2421 | local_umin = tmp_intv->value.uval.min; |
| 2422 | } else if (kind == 1) { |
| 2423 | local_smin = tmp_intv->value.sval.min; |
| 2424 | } else if (kind == 2) { |
| 2425 | local_fmin = tmp_intv->value.fval.min; |
| 2426 | } |
| 2427 | |
| 2428 | while (tmp_intv->next) { |
| 2429 | tmp_intv = tmp_intv->next; |
| 2430 | } |
| 2431 | |
| 2432 | if (kind == 0) { |
| 2433 | local_umax = tmp_intv->value.uval.max; |
| 2434 | } else if (kind == 1) { |
| 2435 | local_smax = tmp_intv->value.sval.max; |
| 2436 | } else if (kind == 2) { |
| 2437 | local_fmax = tmp_intv->value.fval.max; |
| 2438 | } |
| 2439 | } |
| 2440 | |
| 2441 | /* finally parse our restriction */ |
| 2442 | seg_ptr = str_restr; |
Pavol Vican | 9e7c01d | 2016-08-29 09:36:17 +0200 | [diff] [blame] | 2443 | tmp_intv = NULL; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2444 | while (1) { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2445 | if (!tmp_local_intv) { |
| 2446 | assert(!local_intv); |
| 2447 | local_intv = malloc(sizeof *local_intv); |
| 2448 | tmp_local_intv = local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2449 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2450 | tmp_local_intv->next = malloc(sizeof *tmp_local_intv); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2451 | tmp_local_intv = tmp_local_intv->next; |
| 2452 | } |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2453 | if (!tmp_local_intv) { |
| 2454 | LOGMEM; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2455 | goto error; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2456 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2457 | |
| 2458 | tmp_local_intv->kind = kind; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2459 | tmp_local_intv->type = type; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2460 | tmp_local_intv->next = NULL; |
| 2461 | |
| 2462 | /* min */ |
| 2463 | ptr = seg_ptr; |
| 2464 | while (isspace(ptr[0])) { |
| 2465 | ++ptr; |
| 2466 | } |
| 2467 | if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) { |
| 2468 | if (kind == 0) { |
| 2469 | tmp_local_intv->value.uval.min = atoll(ptr); |
| 2470 | } else if (kind == 1) { |
| 2471 | tmp_local_intv->value.sval.min = atoll(ptr); |
| 2472 | } else if (kind == 2) { |
| 2473 | tmp_local_intv->value.fval.min = atoll(ptr); |
| 2474 | } |
| 2475 | |
| 2476 | if ((ptr[0] == '+') || (ptr[0] == '-')) { |
| 2477 | ++ptr; |
| 2478 | } |
| 2479 | while (isdigit(ptr[0])) { |
| 2480 | ++ptr; |
| 2481 | } |
| 2482 | } else if (!strncmp(ptr, "min", 3)) { |
| 2483 | if (kind == 0) { |
| 2484 | tmp_local_intv->value.uval.min = local_umin; |
| 2485 | } else if (kind == 1) { |
| 2486 | tmp_local_intv->value.sval.min = local_smin; |
| 2487 | } else if (kind == 2) { |
| 2488 | tmp_local_intv->value.fval.min = local_fmin; |
| 2489 | } |
| 2490 | |
| 2491 | ptr += 3; |
| 2492 | } else if (!strncmp(ptr, "max", 3)) { |
| 2493 | if (kind == 0) { |
| 2494 | tmp_local_intv->value.uval.min = local_umax; |
| 2495 | } else if (kind == 1) { |
| 2496 | tmp_local_intv->value.sval.min = local_smax; |
| 2497 | } else if (kind == 2) { |
| 2498 | tmp_local_intv->value.fval.min = local_fmax; |
| 2499 | } |
| 2500 | |
| 2501 | ptr += 3; |
| 2502 | } else { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2503 | LOGINT; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2504 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2505 | } |
| 2506 | |
| 2507 | while (isspace(ptr[0])) { |
| 2508 | ptr++; |
| 2509 | } |
| 2510 | |
| 2511 | /* no interval or interval */ |
| 2512 | if ((ptr[0] == '|') || !ptr[0]) { |
| 2513 | if (kind == 0) { |
| 2514 | tmp_local_intv->value.uval.max = tmp_local_intv->value.uval.min; |
| 2515 | } else if (kind == 1) { |
| 2516 | tmp_local_intv->value.sval.max = tmp_local_intv->value.sval.min; |
| 2517 | } else if (kind == 2) { |
| 2518 | tmp_local_intv->value.fval.max = tmp_local_intv->value.fval.min; |
| 2519 | } |
| 2520 | } else if (!strncmp(ptr, "..", 2)) { |
| 2521 | /* skip ".." */ |
| 2522 | ptr += 2; |
| 2523 | while (isspace(ptr[0])) { |
| 2524 | ++ptr; |
| 2525 | } |
| 2526 | |
| 2527 | /* max */ |
| 2528 | if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) { |
| 2529 | if (kind == 0) { |
| 2530 | tmp_local_intv->value.uval.max = atoll(ptr); |
| 2531 | } else if (kind == 1) { |
| 2532 | tmp_local_intv->value.sval.max = atoll(ptr); |
| 2533 | } else if (kind == 2) { |
| 2534 | tmp_local_intv->value.fval.max = atoll(ptr); |
| 2535 | } |
| 2536 | } else if (!strncmp(ptr, "max", 3)) { |
| 2537 | if (kind == 0) { |
| 2538 | tmp_local_intv->value.uval.max = local_umax; |
| 2539 | } else if (kind == 1) { |
| 2540 | tmp_local_intv->value.sval.max = local_smax; |
| 2541 | } else if (kind == 2) { |
| 2542 | tmp_local_intv->value.fval.max = local_fmax; |
| 2543 | } |
| 2544 | } else { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2545 | LOGINT; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2546 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2547 | } |
| 2548 | } else { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2549 | LOGINT; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2550 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2551 | } |
| 2552 | |
Pavol Vican | 9e7c01d | 2016-08-29 09:36:17 +0200 | [diff] [blame] | 2553 | /* check min and max in correct order*/ |
| 2554 | if (kind == 0) { |
| 2555 | /* current segment */ |
| 2556 | if (tmp_local_intv->value.uval.min > tmp_local_intv->value.uval.max) { |
| 2557 | goto error; |
| 2558 | } |
| 2559 | if (tmp_local_intv->value.uval.min < local_umin || tmp_local_intv->value.uval.max > local_umax) { |
| 2560 | goto error; |
| 2561 | } |
| 2562 | /* segments sholud be ascending order */ |
Pavol Vican | 69f62c9 | 2016-08-30 09:06:25 +0200 | [diff] [blame] | 2563 | 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] | 2564 | goto error; |
| 2565 | } |
| 2566 | } else if (kind == 1) { |
| 2567 | if (tmp_local_intv->value.sval.min > tmp_local_intv->value.sval.max) { |
| 2568 | goto error; |
| 2569 | } |
| 2570 | if (tmp_local_intv->value.sval.min < local_smin || tmp_local_intv->value.sval.max > local_smax) { |
| 2571 | goto error; |
| 2572 | } |
Pavol Vican | 69f62c9 | 2016-08-30 09:06:25 +0200 | [diff] [blame] | 2573 | 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] | 2574 | goto error; |
| 2575 | } |
| 2576 | } else if (kind == 2) { |
| 2577 | if (tmp_local_intv->value.fval.min > tmp_local_intv->value.fval.max) { |
| 2578 | goto error; |
| 2579 | } |
| 2580 | if (tmp_local_intv->value.fval.min < local_fmin || tmp_local_intv->value.fval.max > local_fmax) { |
| 2581 | goto error; |
| 2582 | } |
Pavol Vican | 69f62c9 | 2016-08-30 09:06:25 +0200 | [diff] [blame] | 2583 | if (tmp_intv && (tmp_intv->value.fval.max >= tmp_local_intv->value.fval.min)) { |
Pavol Vican | 9e7c01d | 2016-08-29 09:36:17 +0200 | [diff] [blame] | 2584 | goto error; |
| 2585 | } |
| 2586 | } |
| 2587 | |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2588 | /* next segment (next OR) */ |
| 2589 | seg_ptr = strchr(seg_ptr, '|'); |
| 2590 | if (!seg_ptr) { |
| 2591 | break; |
| 2592 | } |
| 2593 | seg_ptr++; |
Pavol Vican | 9e7c01d | 2016-08-29 09:36:17 +0200 | [diff] [blame] | 2594 | tmp_intv = tmp_local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2595 | } |
| 2596 | |
| 2597 | /* check local restrictions against superior ones */ |
| 2598 | if (intv) { |
| 2599 | tmp_intv = intv; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2600 | tmp_local_intv = local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2601 | |
| 2602 | while (tmp_local_intv && tmp_intv) { |
| 2603 | /* reuse local variables */ |
| 2604 | if (kind == 0) { |
| 2605 | local_umin = tmp_local_intv->value.uval.min; |
| 2606 | local_umax = tmp_local_intv->value.uval.max; |
| 2607 | |
| 2608 | /* it must be in this interval */ |
| 2609 | if ((local_umin >= tmp_intv->value.uval.min) && (local_umin <= tmp_intv->value.uval.max)) { |
| 2610 | /* this interval is covered, next one */ |
| 2611 | if (local_umax <= tmp_intv->value.uval.max) { |
| 2612 | tmp_local_intv = tmp_local_intv->next; |
| 2613 | continue; |
| 2614 | /* ascending order of restrictions -> fail */ |
| 2615 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2616 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2617 | } |
| 2618 | } |
| 2619 | } else if (kind == 1) { |
| 2620 | local_smin = tmp_local_intv->value.sval.min; |
| 2621 | local_smax = tmp_local_intv->value.sval.max; |
| 2622 | |
| 2623 | if ((local_smin >= tmp_intv->value.sval.min) && (local_smin <= tmp_intv->value.sval.max)) { |
| 2624 | if (local_smax <= tmp_intv->value.sval.max) { |
| 2625 | tmp_local_intv = tmp_local_intv->next; |
| 2626 | continue; |
| 2627 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2628 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2629 | } |
| 2630 | } |
| 2631 | } else if (kind == 2) { |
| 2632 | local_fmin = tmp_local_intv->value.fval.min; |
| 2633 | local_fmax = tmp_local_intv->value.fval.max; |
| 2634 | |
| 2635 | if ((local_fmin >= tmp_intv->value.fval.min) && (local_fmin <= tmp_intv->value.fval.max)) { |
| 2636 | if (local_fmax <= tmp_intv->value.fval.max) { |
| 2637 | tmp_local_intv = tmp_local_intv->next; |
| 2638 | continue; |
| 2639 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2640 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2641 | } |
| 2642 | } |
| 2643 | } |
| 2644 | |
| 2645 | tmp_intv = tmp_intv->next; |
| 2646 | } |
| 2647 | |
| 2648 | /* some interval left uncovered -> fail */ |
| 2649 | if (tmp_local_intv) { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2650 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2651 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2652 | } |
| 2653 | |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2654 | /* append the local intervals to all the intervals of the superior types, return it all */ |
| 2655 | if (intv) { |
| 2656 | for (tmp_intv = intv; tmp_intv->next; tmp_intv = tmp_intv->next); |
| 2657 | tmp_intv->next = local_intv; |
| 2658 | } else { |
| 2659 | intv = local_intv; |
| 2660 | } |
| 2661 | *ret = intv; |
| 2662 | |
| 2663 | return EXIT_SUCCESS; |
| 2664 | |
| 2665 | error: |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2666 | while (intv) { |
| 2667 | tmp_intv = intv->next; |
| 2668 | free(intv); |
| 2669 | intv = tmp_intv; |
| 2670 | } |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2671 | while (local_intv) { |
| 2672 | tmp_local_intv = local_intv->next; |
| 2673 | free(local_intv); |
| 2674 | local_intv = tmp_local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2675 | } |
| 2676 | |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2677 | return -1; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2678 | } |
| 2679 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2680 | /** |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2681 | * @brief Resolve a typedef, return only resolved typedefs if derived. If leafref, it must be |
| 2682 | * resolved for this function to return it. Does not log. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2683 | * |
| 2684 | * @param[in] name Typedef name. |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2685 | * @param[in] mod_name Typedef name module name. |
| 2686 | * @param[in] module Main module. |
| 2687 | * @param[in] parent Parent of the resolved type definition. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2688 | * @param[out] ret Pointer to the resolved typedef. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2689 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2690 | * @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] | 2691 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2692 | int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 2693 | resolve_superior_type(const char *name, const char *mod_name, const struct lys_module *module, |
| 2694 | const struct lys_node *parent, struct lys_tpdf **ret) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2695 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2696 | int i, j; |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2697 | struct lys_tpdf *tpdf, *match; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2698 | int tpdf_size; |
| 2699 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2700 | if (!mod_name) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2701 | /* no prefix, try built-in types */ |
| 2702 | for (i = 1; i < LY_DATA_TYPE_COUNT; i++) { |
| 2703 | if (!strcmp(ly_types[i].def->name, name)) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2704 | if (ret) { |
| 2705 | *ret = ly_types[i].def; |
| 2706 | } |
| 2707 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2708 | } |
| 2709 | } |
| 2710 | } else { |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2711 | if (!strcmp(mod_name, module->name)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2712 | /* prefix refers to the current module, ignore it */ |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2713 | mod_name = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2714 | } |
| 2715 | } |
| 2716 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2717 | if (!mod_name && parent) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2718 | /* search in local typedefs */ |
| 2719 | while (parent) { |
| 2720 | switch (parent->nodetype) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2721 | case LYS_CONTAINER: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 2722 | tpdf_size = ((struct lys_node_container *)parent)->tpdf_size; |
| 2723 | tpdf = ((struct lys_node_container *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2724 | break; |
| 2725 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2726 | case LYS_LIST: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 2727 | tpdf_size = ((struct lys_node_list *)parent)->tpdf_size; |
| 2728 | tpdf = ((struct lys_node_list *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2729 | break; |
| 2730 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2731 | case LYS_GROUPING: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 2732 | tpdf_size = ((struct lys_node_grp *)parent)->tpdf_size; |
| 2733 | tpdf = ((struct lys_node_grp *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2734 | break; |
| 2735 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2736 | case LYS_RPC: |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 2737 | case LYS_ACTION: |
| 2738 | tpdf_size = ((struct lys_node_rpc_action *)parent)->tpdf_size; |
| 2739 | tpdf = ((struct lys_node_rpc_action *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2740 | break; |
| 2741 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2742 | case LYS_NOTIF: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 2743 | tpdf_size = ((struct lys_node_notif *)parent)->tpdf_size; |
| 2744 | tpdf = ((struct lys_node_notif *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2745 | break; |
| 2746 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2747 | case LYS_INPUT: |
| 2748 | case LYS_OUTPUT: |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 2749 | tpdf_size = ((struct lys_node_inout *)parent)->tpdf_size; |
| 2750 | tpdf = ((struct lys_node_inout *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2751 | break; |
| 2752 | |
| 2753 | default: |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 2754 | parent = lys_parent(parent); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2755 | continue; |
| 2756 | } |
| 2757 | |
| 2758 | for (i = 0; i < tpdf_size; i++) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 2759 | if (!strcmp(tpdf[i].name, name) && tpdf[i].type.base > 0) { |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2760 | match = &tpdf[i]; |
| 2761 | goto check_leafref; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2762 | } |
| 2763 | } |
| 2764 | |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 2765 | parent = lys_parent(parent); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2766 | } |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2767 | } else { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2768 | /* get module where to search */ |
Michal Vasko | b6729c6 | 2015-10-21 12:09:47 +0200 | [diff] [blame] | 2769 | module = lys_get_import_module(module, NULL, 0, mod_name, 0); |
Michal Vasko | c935fff | 2015-08-17 14:02:06 +0200 | [diff] [blame] | 2770 | if (!module) { |
| 2771 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2772 | } |
| 2773 | } |
| 2774 | |
| 2775 | /* search in top level typedefs */ |
| 2776 | for (i = 0; i < module->tpdf_size; i++) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 2777 | 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] | 2778 | match = &module->tpdf[i]; |
| 2779 | goto check_leafref; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2780 | } |
| 2781 | } |
| 2782 | |
| 2783 | /* search in submodules */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2784 | for (i = 0; i < module->inc_size && module->inc[i].submodule; i++) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2785 | for (j = 0; j < module->inc[i].submodule->tpdf_size; j++) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 2786 | 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] | 2787 | match = &module->inc[i].submodule->tpdf[j]; |
| 2788 | goto check_leafref; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2789 | } |
| 2790 | } |
| 2791 | } |
| 2792 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2793 | return EXIT_FAILURE; |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2794 | |
| 2795 | check_leafref: |
| 2796 | if (ret) { |
| 2797 | *ret = match; |
| 2798 | } |
| 2799 | if (match->type.base == LY_TYPE_LEAFREF) { |
| 2800 | while (!match->type.info.lref.path) { |
| 2801 | match = match->type.der; |
| 2802 | assert(match); |
| 2803 | } |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2804 | } |
| 2805 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2806 | } |
| 2807 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2808 | /** |
| 2809 | * @brief Check the default \p value of the \p type. Logs directly. |
| 2810 | * |
| 2811 | * @param[in] type Type definition to use. |
| 2812 | * @param[in] value Default value to check. |
Michal Vasko | 5682640 | 2016-03-02 11:11:37 +0100 | [diff] [blame] | 2813 | * @param[in] module Type module. |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2814 | * |
| 2815 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 2816 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2817 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2818 | check_default(struct lys_type *type, const char *value, struct lys_module *module) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2819 | { |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 2820 | struct lys_tpdf *base_tpdf = NULL; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2821 | struct lyd_node_leaf_list node; |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 2822 | int ret = EXIT_SUCCESS; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2823 | |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 2824 | if (type->base <= LY_TYPE_DER) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 2825 | /* the type was not resolved yet, nothing to do for now */ |
| 2826 | return EXIT_FAILURE; |
| 2827 | } |
| 2828 | |
| 2829 | if (!value) { |
| 2830 | /* we do not have a new default value, so is there any to check even, in some base type? */ |
| 2831 | for (base_tpdf = type->der; base_tpdf->type.der; base_tpdf = base_tpdf->type.der) { |
| 2832 | if (base_tpdf->dflt) { |
| 2833 | value = base_tpdf->dflt; |
| 2834 | break; |
| 2835 | } |
| 2836 | } |
| 2837 | |
| 2838 | if (!value) { |
| 2839 | /* no default value, nothing to check, all is well */ |
| 2840 | return EXIT_SUCCESS; |
| 2841 | } |
| 2842 | |
| 2843 | /* 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)? */ |
| 2844 | switch (type->base) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 2845 | case LY_TYPE_IDENT: |
| 2846 | case LY_TYPE_INST: |
| 2847 | case LY_TYPE_LEAFREF: |
| 2848 | case LY_TYPE_BOOL: |
| 2849 | case LY_TYPE_EMPTY: |
| 2850 | /* these have no restrictions, so we would do the exact same work as the unres in the base typedef */ |
| 2851 | return EXIT_SUCCESS; |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 2852 | case LY_TYPE_BITS: |
| 2853 | /* the default value must match the restricted list of values, if the type was restricted */ |
| 2854 | if (type->info.bits.count) { |
| 2855 | break; |
| 2856 | } |
| 2857 | return EXIT_SUCCESS; |
| 2858 | case LY_TYPE_ENUM: |
| 2859 | /* the default value must match the restricted list of values, if the type was restricted */ |
| 2860 | if (type->info.enums.count) { |
| 2861 | break; |
| 2862 | } |
| 2863 | return EXIT_SUCCESS; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 2864 | case LY_TYPE_DEC64: |
| 2865 | if (type->info.dec64.range) { |
| 2866 | break; |
| 2867 | } |
| 2868 | return EXIT_SUCCESS; |
| 2869 | case LY_TYPE_BINARY: |
| 2870 | if (type->info.binary.length) { |
| 2871 | break; |
| 2872 | } |
| 2873 | return EXIT_SUCCESS; |
| 2874 | case LY_TYPE_INT8: |
| 2875 | case LY_TYPE_INT16: |
| 2876 | case LY_TYPE_INT32: |
| 2877 | case LY_TYPE_INT64: |
| 2878 | case LY_TYPE_UINT8: |
| 2879 | case LY_TYPE_UINT16: |
| 2880 | case LY_TYPE_UINT32: |
| 2881 | case LY_TYPE_UINT64: |
| 2882 | if (type->info.num.range) { |
| 2883 | break; |
| 2884 | } |
| 2885 | return EXIT_SUCCESS; |
| 2886 | case LY_TYPE_STRING: |
| 2887 | if (type->info.str.length || type->info.str.patterns) { |
| 2888 | break; |
| 2889 | } |
| 2890 | return EXIT_SUCCESS; |
| 2891 | case LY_TYPE_UNION: |
| 2892 | /* way too much trouble learning whether we need to check the default again, so just do it */ |
| 2893 | break; |
| 2894 | default: |
| 2895 | LOGINT; |
| 2896 | return -1; |
| 2897 | } |
Radek Krejci | 55a161c | 2016-09-05 17:13:25 +0200 | [diff] [blame] | 2898 | } else if (type->base == LY_TYPE_EMPTY) { |
| 2899 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "default", type->parent->name); |
| 2900 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The \"empty\" data type cannot have a default value."); |
| 2901 | return -1; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 2902 | } |
| 2903 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2904 | /* dummy leaf */ |
Radek Krejci | 4fe36bd | 2016-03-17 16:47:16 +0100 | [diff] [blame] | 2905 | memset(&node, 0, sizeof node); |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2906 | node.value_str = value; |
| 2907 | node.value_type = type->base; |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 2908 | node.schema = calloc(1, sizeof (struct lys_node_leaf)); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2909 | if (!node.schema) { |
| 2910 | LOGMEM; |
| 2911 | return -1; |
| 2912 | } |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 2913 | node.schema->name = strdup("fake-default"); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2914 | if (!node.schema->name) { |
| 2915 | LOGMEM; |
Michal Vasko | f49eda0 | 2016-07-21 12:17:01 +0200 | [diff] [blame] | 2916 | free(node.schema); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2917 | return -1; |
| 2918 | } |
Michal Vasko | 5682640 | 2016-03-02 11:11:37 +0100 | [diff] [blame] | 2919 | node.schema->module = module; |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 2920 | memcpy(&((struct lys_node_leaf *)node.schema)->type, type, sizeof *type); |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2921 | |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 2922 | if (type->base == LY_TYPE_LEAFREF) { |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2923 | if (!type->info.lref.target) { |
| 2924 | ret = EXIT_FAILURE; |
| 2925 | goto finish; |
| 2926 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2927 | ret = check_default(&type->info.lref.target->type, value, module); |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2928 | |
| 2929 | } else if ((type->base == LY_TYPE_INST) || (type->base == LY_TYPE_IDENT)) { |
| 2930 | /* it was converted to JSON format before, nothing else sensible we can do */ |
| 2931 | |
| 2932 | } else { |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 2933 | if (lyp_parse_value(&node, NULL, 1)) { |
| 2934 | ret = -1; |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 2935 | if (base_tpdf) { |
| 2936 | /* default value was is defined in some base typedef */ |
| 2937 | if ((type->base == LY_TYPE_BITS && type->der->type.der) || |
| 2938 | (type->base == LY_TYPE_ENUM && type->der->type.der)) { |
| 2939 | /* we have refined bits/enums */ |
| 2940 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, |
| 2941 | "Invalid value \"%s\" of the default statement inherited to \"%s\" from \"%s\" base type.", |
| 2942 | value, type->parent->name, base_tpdf->name); |
| 2943 | } |
| 2944 | } |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 2945 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2946 | } |
| 2947 | |
| 2948 | finish: |
| 2949 | if (node.value_type == LY_TYPE_BITS) { |
| 2950 | free(node.value.bit); |
| 2951 | } |
| 2952 | free((char *)node.schema->name); |
| 2953 | free(node.schema); |
| 2954 | |
| 2955 | return ret; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2956 | } |
| 2957 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2958 | /** |
| 2959 | * @brief Check a key for mandatory attributes. Logs directly. |
| 2960 | * |
| 2961 | * @param[in] key The key to check. |
| 2962 | * @param[in] flags What flags to check. |
| 2963 | * @param[in] list The list of all the keys. |
| 2964 | * @param[in] index Index of the key in the key list. |
| 2965 | * @param[in] name The name of the keys. |
| 2966 | * @param[in] len The name length. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2967 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2968 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2969 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2970 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2971 | 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] | 2972 | { |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 2973 | struct lys_node_leaf *key = list->keys[index]; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2974 | char *dup = NULL; |
| 2975 | int j; |
| 2976 | |
| 2977 | /* existence */ |
| 2978 | if (!key) { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 2979 | if (name[len] != '\0') { |
| 2980 | dup = strdup(name); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2981 | if (!dup) { |
| 2982 | LOGMEM; |
| 2983 | return -1; |
| 2984 | } |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 2985 | dup[len] = '\0'; |
| 2986 | name = dup; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2987 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2988 | LOGVAL(LYE_KEY_MISS, LY_VLOG_LYS, list, name); |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 2989 | free(dup); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2990 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2991 | } |
| 2992 | |
| 2993 | /* uniqueness */ |
| 2994 | for (j = index - 1; j >= 0; j--) { |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 2995 | if (key == list->keys[j]) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2996 | LOGVAL(LYE_KEY_DUP, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2997 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2998 | } |
| 2999 | } |
| 3000 | |
| 3001 | /* key is a leaf */ |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3002 | if (key->nodetype != LYS_LEAF) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3003 | LOGVAL(LYE_KEY_NLEAF, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3004 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3005 | } |
| 3006 | |
| 3007 | /* type of the leaf is not built-in empty */ |
| 3008 | if (key->type.base == LY_TYPE_EMPTY) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3009 | LOGVAL(LYE_KEY_TYPE, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3010 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3011 | } |
| 3012 | |
| 3013 | /* config attribute is the same as of the list */ |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3014 | if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3015 | LOGVAL(LYE_KEY_CONFIG, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3016 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3017 | } |
| 3018 | |
Radek Krejci | 55e2cdc | 2016-03-11 13:51:09 +0100 | [diff] [blame] | 3019 | /* key is not placed from augment */ |
| 3020 | if (key->parent->nodetype == LYS_AUGMENT) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3021 | LOGVAL(LYE_KEY_MISS, LY_VLOG_LYS, key, key->name); |
| 3022 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, key, "Key inserted from augment."); |
Radek Krejci | 55e2cdc | 2016-03-11 13:51:09 +0100 | [diff] [blame] | 3023 | return -1; |
| 3024 | } |
| 3025 | |
Radek Krejci | 3f21ada | 2016-08-01 13:34:31 +0200 | [diff] [blame] | 3026 | /* key is not when/if-feature -conditional */ |
| 3027 | j = 0; |
| 3028 | if (key->when || (key->iffeature_size && (j = 1))) { |
| 3029 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, key, j ? "if-feature" : "when", "leaf"); |
| 3030 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, key, "Key definition cannot depend on a \"%s\" condition.", |
| 3031 | j ? "if-feature" : "when"); |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3032 | return -1; |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3033 | } |
| 3034 | |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3035 | return EXIT_SUCCESS; |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3036 | } |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3037 | |
| 3038 | /** |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3039 | * @brief Resolve (test the target exists) unique. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3040 | * |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3041 | * @param[in] parent The parent node of the unique structure. |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3042 | * @param[in] uniq_str_path One path from the unique string. |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3043 | * |
| 3044 | * @return EXIT_SUCCESS on succes, EXIT_FAILURE on forward reference, -1 on error. |
| 3045 | */ |
| 3046 | int |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3047 | 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] | 3048 | { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3049 | int rc; |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3050 | const struct lys_node *leaf = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3051 | |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 3052 | rc = resolve_descendant_schema_nodeid(uniq_str_path, parent->child, LYS_LEAF, 1, 1, &leaf); |
Michal Vasko | 9bb061b | 2016-02-12 11:00:19 +0100 | [diff] [blame] | 3053 | if (rc || !leaf) { |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3054 | if (rc) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3055 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3056 | if (rc > 0) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3057 | LOGVAL(LYE_INCHAR, LY_VLOG_LYS, parent, uniq_str_path[rc - 1], &uniq_str_path[rc - 1]); |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 3058 | } else if (rc == -2) { |
Michal Vasko | c66c6d8 | 2016-04-12 11:37:31 +0200 | [diff] [blame] | 3059 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, parent, "Unique argument references list."); |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3060 | } |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3061 | rc = -1; |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3062 | } else { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3063 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
| 3064 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, parent, "Target leaf not found."); |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3065 | rc = EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3066 | } |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3067 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3068 | } |
Michal Vasko | 9bb061b | 2016-02-12 11:00:19 +0100 | [diff] [blame] | 3069 | if (leaf->nodetype != LYS_LEAF) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3070 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
| 3071 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, parent, "Target is not a leaf."); |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3072 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3073 | } |
| 3074 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3075 | /* check status */ |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3076 | if (lyp_check_status(parent->flags, parent->module, parent->name, leaf->flags, leaf->module, leaf->name, leaf)) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3077 | return -1; |
| 3078 | } |
| 3079 | |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3080 | /* check that all unique's targets are of the same config type */ |
| 3081 | if (*trg_type) { |
| 3082 | if (((*trg_type == 1) && (leaf->flags & LYS_CONFIG_R)) || ((*trg_type == 2) && (leaf->flags & LYS_CONFIG_W))) { |
| 3083 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
| 3084 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, parent, |
| 3085 | "Leaf \"%s\" referenced in unique statement is config %s, but previous referenced leaf is config %s.", |
| 3086 | uniq_str_path, *trg_type == 1 ? "false" : "true", *trg_type == 1 ? "true" : "false"); |
| 3087 | return -1; |
| 3088 | } |
| 3089 | } else { |
| 3090 | /* first unique */ |
| 3091 | if (leaf->flags & LYS_CONFIG_W) { |
| 3092 | *trg_type = 1; |
| 3093 | } else { |
| 3094 | *trg_type = 2; |
| 3095 | } |
| 3096 | } |
| 3097 | |
Radek Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame] | 3098 | /* set leaf's unique flag */ |
| 3099 | ((struct lys_node_leaf *)leaf)->flags |= LYS_UNIQUE; |
| 3100 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3101 | return EXIT_SUCCESS; |
| 3102 | |
| 3103 | error: |
| 3104 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3105 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3106 | } |
| 3107 | |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 3108 | void |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3109 | unres_data_del(struct unres_data *unres, uint32_t i) |
| 3110 | { |
| 3111 | /* there are items after the one deleted */ |
| 3112 | if (i+1 < unres->count) { |
| 3113 | /* we only move the data, memory is left allocated, why bother */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3114 | 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] | 3115 | |
| 3116 | /* deleting the last item */ |
| 3117 | } else if (i == 0) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3118 | free(unres->node); |
| 3119 | unres->node = NULL; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3120 | } |
| 3121 | |
| 3122 | /* if there are no items after and it is not the last one, just move the counter */ |
| 3123 | --unres->count; |
| 3124 | } |
| 3125 | |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3126 | /** |
| 3127 | * @brief Resolve (find) a data node from a specific module. Does not log. |
| 3128 | * |
| 3129 | * @param[in] mod Module to search in. |
| 3130 | * @param[in] name Name of the data node. |
| 3131 | * @param[in] nam_len Length of the name. |
| 3132 | * @param[in] start Data node to start the search from. |
| 3133 | * @param[in,out] parents Resolved nodes. If there are some parents, |
| 3134 | * they are replaced (!!) with the resolvents. |
| 3135 | * |
Michal Vasko | 2471e7f | 2016-04-11 11:00:15 +0200 | [diff] [blame] | 3136 | * @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] | 3137 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3138 | static int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3139 | 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] | 3140 | { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3141 | struct lyd_node *node; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3142 | int flag; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3143 | uint32_t i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3144 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3145 | if (!parents->count) { |
| 3146 | parents->count = 1; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3147 | parents->node = malloc(sizeof *parents->node); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3148 | if (!parents->node) { |
| 3149 | LOGMEM; |
Michal Vasko | 2471e7f | 2016-04-11 11:00:15 +0200 | [diff] [blame] | 3150 | return -1; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3151 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3152 | parents->node[0] = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3153 | } |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3154 | for (i = 0; i < parents->count;) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 3155 | 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] | 3156 | /* skip */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3157 | ++i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3158 | continue; |
| 3159 | } |
| 3160 | flag = 0; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3161 | LY_TREE_FOR(parents->node[i] ? parents->node[i]->child : start, node) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3162 | if (node->schema->module == mod && !strncmp(node->schema->name, name, nam_len) |
| 3163 | && node->schema->name[nam_len] == '\0') { |
| 3164 | /* matching target */ |
| 3165 | if (!flag) { |
Michal Vasko | 9a47e12 | 2015-09-03 14:26:32 +0200 | [diff] [blame] | 3166 | /* put node instead of the current parent */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3167 | parents->node[i] = node; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3168 | flag = 1; |
| 3169 | } else { |
Michal Vasko | 9a47e12 | 2015-09-03 14:26:32 +0200 | [diff] [blame] | 3170 | /* multiple matching, so create a new node */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3171 | ++parents->count; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3172 | parents->node = ly_realloc(parents->node, parents->count * sizeof *parents->node); |
| 3173 | if (!parents->node) { |
| 3174 | return EXIT_FAILURE; |
| 3175 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3176 | parents->node[parents->count-1] = node; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3177 | ++i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3178 | } |
| 3179 | } |
| 3180 | } |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3181 | |
| 3182 | if (!flag) { |
| 3183 | /* remove item from the parents list */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3184 | unres_data_del(parents, i); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3185 | } else { |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3186 | ++i; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3187 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3188 | } |
| 3189 | |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3190 | return parents->count ? EXIT_SUCCESS : EXIT_FAILURE; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3191 | } |
| 3192 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3193 | /** |
| 3194 | * @brief Resolve (find) a data node. Does not log. |
| 3195 | * |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3196 | * @param[in] mod_name Module name of the data node. |
| 3197 | * @param[in] mod_name_len Length of the module name. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3198 | * @param[in] name Name of the data node. |
| 3199 | * @param[in] nam_len Length of the name. |
| 3200 | * @param[in] start Data node to start the search from. |
| 3201 | * @param[in,out] parents Resolved nodes. If there are some parents, |
| 3202 | * they are replaced (!!) with the resolvents. |
| 3203 | * |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3204 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 otherwise. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3205 | */ |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3206 | static int |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3207 | 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] | 3208 | struct unres_data *parents) |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3209 | { |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3210 | const struct lys_module *mod; |
Michal Vasko | 31fc367 | 2015-10-21 12:08:13 +0200 | [diff] [blame] | 3211 | char *str; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3212 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3213 | assert(start); |
| 3214 | |
Michal Vasko | 31fc367 | 2015-10-21 12:08:13 +0200 | [diff] [blame] | 3215 | if (mod_name) { |
| 3216 | /* we have mod_name, find appropriate module */ |
| 3217 | str = strndup(mod_name, mod_name_len); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3218 | if (!str) { |
| 3219 | LOGMEM; |
| 3220 | return -1; |
| 3221 | } |
Michal Vasko | 31fc367 | 2015-10-21 12:08:13 +0200 | [diff] [blame] | 3222 | mod = ly_ctx_get_module(start->schema->module->ctx, str, NULL); |
| 3223 | free(str); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3224 | if (!mod) { |
| 3225 | /* invalid prefix */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3226 | return -1; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3227 | } |
| 3228 | } else { |
| 3229 | /* no prefix, module is the same as of current node */ |
| 3230 | mod = start->schema->module; |
| 3231 | } |
| 3232 | |
| 3233 | return resolve_data(mod, name, name_len, start, parents); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3234 | } |
| 3235 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3236 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3237 | * @brief Resolve a path predicate (leafref) in JSON data context. Logs directly |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3238 | * only specific errors, general no-resolvent error is left to the caller. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3239 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3240 | * @param[in] pred Predicate to use. |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3241 | * @param[in] node Node from which the predicate is being resolved |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3242 | * @param[in,out] node_match Nodes satisfying the restriction |
| 3243 | * without the predicate. Nodes not |
| 3244 | * satisfying the predicate are removed. |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3245 | * @param[out] parsed Number of characters parsed, negative on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3246 | * |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3247 | * @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] | 3248 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3249 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3250 | 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] | 3251 | int *parsed) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3252 | { |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3253 | /* ... /node[source = destination] ... */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3254 | struct unres_data source_match, dest_match; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3255 | const char *path_key_expr, *source, *sour_pref, *dest, *dest_pref; |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3256 | int pke_len, sour_len, sour_pref_len, dest_len, dest_pref_len, parsed_loc = 0, pke_parsed = 0; |
| 3257 | int has_predicate, dest_parent_times, i, rc; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3258 | uint32_t j; |
Radek Krejci | 0c2fa3a | 2016-06-13 15:18:03 +0200 | [diff] [blame] | 3259 | struct lyd_node_leaf_list *leaf_dst, *leaf_src; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3260 | |
| 3261 | source_match.count = 1; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3262 | source_match.node = malloc(sizeof *source_match.node); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3263 | if (!source_match.node) { |
| 3264 | LOGMEM; |
| 3265 | return -1; |
| 3266 | } |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3267 | dest_match.count = 1; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3268 | dest_match.node = malloc(sizeof *dest_match.node); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3269 | if (!dest_match.node) { |
| 3270 | LOGMEM; |
| 3271 | return -1; |
| 3272 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3273 | |
| 3274 | do { |
| 3275 | if ((i = parse_path_predicate(pred, &sour_pref, &sour_pref_len, &source, &sour_len, &path_key_expr, |
| 3276 | &pke_len, &has_predicate)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3277 | LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, pred[-i], &pred[-i]); |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3278 | rc = -1; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3279 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3280 | } |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3281 | parsed_loc += i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3282 | pred += i; |
| 3283 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3284 | for (j = 0; j < node_match->count;) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3285 | /* source */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3286 | source_match.node[0] = node_match->node[j]; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3287 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3288 | /* must be leaf (key of a list) */ |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3289 | 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] | 3290 | &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] | 3291 | i = 0; |
| 3292 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3293 | } |
| 3294 | |
| 3295 | /* destination */ |
Radek Krejci | 0c2fa3a | 2016-06-13 15:18:03 +0200 | [diff] [blame] | 3296 | dest_match.node[0] = node; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3297 | dest_parent_times = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3298 | if ((i = parse_path_key_expr(path_key_expr, &dest_pref, &dest_pref_len, &dest, &dest_len, |
| 3299 | &dest_parent_times)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3300 | 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] | 3301 | rc = -1; |
| 3302 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3303 | } |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3304 | pke_parsed = i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3305 | for (i = 0; i < dest_parent_times; ++i) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3306 | dest_match.node[0] = dest_match.node[0]->parent; |
| 3307 | if (!dest_match.node[0]) { |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3308 | i = 0; |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3309 | rc = EXIT_FAILURE; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3310 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3311 | } |
| 3312 | } |
| 3313 | while (1) { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3314 | 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] | 3315 | &dest_match)) || (dest_match.count != 1)) { |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3316 | i = 0; |
| 3317 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3318 | } |
| 3319 | |
| 3320 | if (pke_len == pke_parsed) { |
| 3321 | break; |
| 3322 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3323 | 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] | 3324 | &dest_parent_times)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3325 | 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] | 3326 | rc = -1; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3327 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3328 | } |
| 3329 | pke_parsed += i; |
| 3330 | } |
| 3331 | |
| 3332 | /* check match between source and destination nodes */ |
Radek Krejci | 0c2fa3a | 2016-06-13 15:18:03 +0200 | [diff] [blame] | 3333 | leaf_dst = (struct lyd_node_leaf_list *)dest_match.node[0]; |
| 3334 | while (leaf_dst->value_type == LY_TYPE_LEAFREF) { |
| 3335 | leaf_dst = (struct lyd_node_leaf_list *)leaf_dst->value.leafref; |
| 3336 | } |
| 3337 | leaf_src = (struct lyd_node_leaf_list *)source_match.node[0]; |
| 3338 | while (leaf_src->value_type == LY_TYPE_LEAFREF) { |
| 3339 | leaf_src = (struct lyd_node_leaf_list *)leaf_src->value.leafref; |
| 3340 | } |
| 3341 | if (leaf_src->value_type != leaf_dst->value_type) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3342 | goto remove_leafref; |
| 3343 | } |
| 3344 | |
Radek Krejci | 0c2fa3a | 2016-06-13 15:18:03 +0200 | [diff] [blame] | 3345 | if (!ly_strequal(leaf_src->value_str, leaf_dst->value_str, 1)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3346 | goto remove_leafref; |
| 3347 | } |
| 3348 | |
| 3349 | /* leafref is ok, continue check with next leafref */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3350 | ++j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3351 | continue; |
| 3352 | |
| 3353 | remove_leafref: |
| 3354 | /* does not fulfill conditions, remove leafref record */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3355 | unres_data_del(node_match, j); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3356 | } |
| 3357 | } while (has_predicate); |
| 3358 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3359 | free(source_match.node); |
| 3360 | free(dest_match.node); |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3361 | if (parsed) { |
| 3362 | *parsed = parsed_loc; |
| 3363 | } |
| 3364 | return EXIT_SUCCESS; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3365 | |
| 3366 | error: |
| 3367 | |
| 3368 | if (source_match.count) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3369 | free(source_match.node); |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3370 | } |
| 3371 | if (dest_match.count) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3372 | free(dest_match.node); |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3373 | } |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3374 | if (parsed) { |
| 3375 | *parsed = -parsed_loc+i; |
| 3376 | } |
| 3377 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3378 | } |
| 3379 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3380 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3381 | * @brief Resolve a path (leafref) in JSON data context. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3382 | * |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3383 | * @param[in] node Leafref data node. |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3384 | * @param[in] path Path of the leafref. |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3385 | * @param[out] ret Matching nodes. Expects an empty, but allocated structure. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3386 | * |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3387 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 otherwise. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3388 | */ |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3389 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3390 | 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] | 3391 | { |
Radek Krejci | 71b795b | 2015-08-10 16:20:39 +0200 | [diff] [blame] | 3392 | struct lyd_node *data = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3393 | const char *prefix, *name; |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3394 | int pref_len, nam_len, has_predicate, parent_times, i, parsed, rc; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3395 | uint32_t j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3396 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3397 | assert(node && path && ret && !ret->count); |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3398 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3399 | parent_times = 0; |
Michal Vasko | d917334 | 2015-08-17 14:35:36 +0200 | [diff] [blame] | 3400 | parsed = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3401 | |
| 3402 | /* searching for nodeset */ |
| 3403 | do { |
| 3404 | if ((i = parse_path_arg(path, &prefix, &pref_len, &name, &nam_len, &parent_times, &has_predicate)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3405 | LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, path[-i], &path[-i]); |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3406 | rc = -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3407 | goto error; |
| 3408 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3409 | path += i; |
Michal Vasko | d917334 | 2015-08-17 14:35:36 +0200 | [diff] [blame] | 3410 | parsed += i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3411 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3412 | if (!ret->count) { |
Michal Vasko | bfd98e6 | 2016-09-02 09:50:05 +0200 | [diff] [blame] | 3413 | if (parent_times > 0) { |
| 3414 | data = node; |
| 3415 | for (i = 1; i < parent_times; ++i) { |
| 3416 | data = data->parent; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3417 | } |
Michal Vasko | bfd98e6 | 2016-09-02 09:50:05 +0200 | [diff] [blame] | 3418 | } else if (!parent_times) { |
| 3419 | data = node->child; |
| 3420 | } else { |
| 3421 | /* absolute path */ |
| 3422 | for (data = node; data->parent; data = data->parent); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3423 | } |
| 3424 | |
Michal Vasko | bfd98e6 | 2016-09-02 09:50:05 +0200 | [diff] [blame] | 3425 | /* we may still be parsing it and the pointer is not correct yet */ |
| 3426 | if (data->prev) { |
| 3427 | while (data->prev->next) { |
| 3428 | data = data->prev; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 3429 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3430 | } |
| 3431 | } |
| 3432 | |
| 3433 | /* node identifier */ |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3434 | 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] | 3435 | if (rc == -1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3436 | LOGVAL(LYE_INELEM_LEN, LY_VLOG_LYD, node, nam_len, name); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3437 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3438 | goto error; |
| 3439 | } |
| 3440 | |
| 3441 | if (has_predicate) { |
| 3442 | /* we have predicate, so the current results must be lists */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3443 | for (j = 0; j < ret->count;) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3444 | if (ret->node[j]->schema->nodetype == LYS_LIST && |
| 3445 | ((struct lys_node_list *)ret->node[0]->schema)->keys) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3446 | /* leafref is ok, continue check with next leafref */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3447 | ++j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3448 | continue; |
| 3449 | } |
| 3450 | |
| 3451 | /* does not fulfill conditions, remove leafref record */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3452 | unres_data_del(ret, j); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3453 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3454 | if ((rc = resolve_path_predicate_data(path, node, ret, &i))) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 3455 | if (rc == -1) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3456 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYD, node, "leafref", path); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3457 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3458 | goto error; |
| 3459 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3460 | path += i; |
Michal Vasko | d917334 | 2015-08-17 14:35:36 +0200 | [diff] [blame] | 3461 | parsed += i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3462 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3463 | if (!ret->count) { |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3464 | rc = EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3465 | goto error; |
| 3466 | } |
| 3467 | } |
| 3468 | } while (path[0] != '\0'); |
| 3469 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3470 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3471 | |
| 3472 | error: |
| 3473 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3474 | free(ret->node); |
| 3475 | ret->node = NULL; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3476 | ret->count = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3477 | |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3478 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3479 | } |
| 3480 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3481 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3482 | * @brief Resolve a path (leafref) predicate in JSON schema context. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3483 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3484 | * @param[in] path Path to use. |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3485 | * @param[in] context_node Predicate context node (where the predicate is placed). |
| 3486 | * @param[in] parent Path context node (where the path begins/is placed). |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3487 | * |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3488 | * @return 0 on forward reference, otherwise the number |
| 3489 | * of characters successfully parsed, |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3490 | * positive on success, negative on failure. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3491 | */ |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3492 | static int |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3493 | resolve_path_predicate_schema(const char *path, const struct lys_node *context_node, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3494 | struct lys_node *parent) |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3495 | { |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3496 | const struct lys_node *src_node, *dst_node; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3497 | const char *path_key_expr, *source, *sour_pref, *dest, *dest_pref; |
| 3498 | int pke_len, sour_len, sour_pref_len, dest_len, dest_pref_len, parsed = 0, pke_parsed = 0; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3499 | int has_predicate, dest_parent_times = 0, i, rc; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3500 | |
| 3501 | do { |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3502 | 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] | 3503 | &pke_len, &has_predicate)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3504 | 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] | 3505 | return -parsed+i; |
| 3506 | } |
| 3507 | parsed += i; |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3508 | path += i; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3509 | |
Michal Vasko | 5809090 | 2015-08-13 14:04:15 +0200 | [diff] [blame] | 3510 | /* source (must be leaf) */ |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3511 | if (!sour_pref) { |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3512 | sour_pref = context_node->module->name; |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3513 | } |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3514 | rc = lys_get_sibling(context_node->child, sour_pref, sour_pref_len, source, sour_len, |
Michal Vasko | 165dc4a | 2015-10-23 09:44:27 +0200 | [diff] [blame] | 3515 | LYS_LEAF | LYS_AUGMENT, &src_node); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3516 | if (rc) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3517 | 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] | 3518 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3519 | } |
| 3520 | |
| 3521 | /* destination */ |
| 3522 | if ((i = parse_path_key_expr(path_key_expr, &dest_pref, &dest_pref_len, &dest, &dest_len, |
| 3523 | &dest_parent_times)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3524 | 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] | 3525 | return -parsed; |
| 3526 | } |
| 3527 | pke_parsed += i; |
| 3528 | |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3529 | for (i = 0, dst_node = parent; i < dest_parent_times; ++i) { |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3530 | if (!dst_node) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3531 | 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] | 3532 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3533 | } |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 3534 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 3535 | * all schema nodes that cannot be instantiated in data tree */ |
| 3536 | for (dst_node = lys_parent(dst_node); |
| 3537 | dst_node && !(dst_node->nodetype & (LYS_CONTAINER | LYS_LIST)); |
| 3538 | dst_node = lys_parent(dst_node)); |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3539 | } |
| 3540 | while (1) { |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3541 | if (!dest_pref) { |
| 3542 | dest_pref = dst_node->module->name; |
| 3543 | } |
| 3544 | rc = lys_get_sibling(dst_node->child, dest_pref, dest_pref_len, dest, dest_len, |
Michal Vasko | 165dc4a | 2015-10-23 09:44:27 +0200 | [diff] [blame] | 3545 | LYS_CONTAINER | LYS_LIST | LYS_LEAF | LYS_AUGMENT, &dst_node); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3546 | if (rc) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3547 | 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] | 3548 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3549 | } |
| 3550 | |
| 3551 | if (pke_len == pke_parsed) { |
| 3552 | break; |
| 3553 | } |
| 3554 | |
| 3555 | if ((i = parse_path_key_expr(path_key_expr+pke_parsed, &dest_pref, &dest_pref_len, &dest, &dest_len, |
| 3556 | &dest_parent_times)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3557 | LOGVAL(LYE_INCHAR, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3558 | (path_key_expr+pke_parsed)[-i], (path_key_expr+pke_parsed)-i); |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3559 | return -parsed; |
| 3560 | } |
| 3561 | pke_parsed += i; |
| 3562 | } |
| 3563 | |
| 3564 | /* check source - dest match */ |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3565 | if (dst_node->nodetype != LYS_LEAF) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3566 | LOGVAL(LYE_NORESOLV, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, "leafref predicate", path-parsed); |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3567 | LOGVAL(LYE_SPEC, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, |
| 3568 | "Destination node is not a leaf, but %s.", strnodetype(dst_node->nodetype)); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3569 | return -parsed; |
| 3570 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3571 | } while (has_predicate); |
| 3572 | |
| 3573 | return parsed; |
| 3574 | } |
| 3575 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3576 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3577 | * @brief Resolve a path (leafref) in JSON schema context. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3578 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3579 | * @param[in] path Path to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3580 | * @param[in] parent_node Parent of the leafref. |
Radek Krejci | 2f12f85 | 2016-01-08 12:59:57 +0100 | [diff] [blame] | 3581 | * @param[in] parent_tpdf Flag if the parent node is actually typedef, in that case the path |
| 3582 | * has to contain absolute path |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3583 | * @param[out] ret Pointer to the resolved schema node. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3584 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3585 | * @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] | 3586 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3587 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3588 | resolve_path_arg_schema(const char *path, struct lys_node *parent, int parent_tpdf, |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3589 | const struct lys_node **ret) |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3590 | { |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3591 | const struct lys_node *node; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 3592 | const struct lys_module *mod; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3593 | const char *id, *prefix, *name; |
| 3594 | int pref_len, nam_len, parent_times, has_predicate; |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3595 | int i, first_iter, rc; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3596 | |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3597 | first_iter = 1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3598 | parent_times = 0; |
| 3599 | id = path; |
| 3600 | |
| 3601 | do { |
| 3602 | if ((i = parse_path_arg(id, &prefix, &pref_len, &name, &nam_len, &parent_times, &has_predicate)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3603 | LOGVAL(LYE_INCHAR, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, id[-i], &id[-i]); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3604 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3605 | } |
| 3606 | id += i; |
| 3607 | |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3608 | if (first_iter) { |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3609 | if (parent_times == -1) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 3610 | /* resolve prefix of the module */ |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3611 | mod = lys_get_import_module(parent->module, NULL, 0, prefix, pref_len); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 3612 | /* get start node */ |
| 3613 | node = mod ? mod->data : NULL; |
Michal Vasko | 5809090 | 2015-08-13 14:04:15 +0200 | [diff] [blame] | 3614 | if (!node) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3615 | LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, |
| 3616 | "leafref", path); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3617 | return EXIT_FAILURE; |
Michal Vasko | 5809090 | 2015-08-13 14:04:15 +0200 | [diff] [blame] | 3618 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3619 | } else if (parent_times > 0) { |
Radek Krejci | 2f12f85 | 2016-01-08 12:59:57 +0100 | [diff] [blame] | 3620 | if (parent_tpdf) { |
| 3621 | /* the path is not allowed to contain relative path since we are in top level typedef */ |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3622 | LOGVAL(LYE_NORESOLV, 0, NULL, "leafref", path); |
Radek Krejci | 2f12f85 | 2016-01-08 12:59:57 +0100 | [diff] [blame] | 3623 | return -1; |
| 3624 | } |
| 3625 | |
Michal Vasko | df053d1 | 2016-07-21 13:33:31 +0200 | [diff] [blame] | 3626 | /* node is the parent already, skip one ".." */ |
| 3627 | for (i = 1, node = parent; i < parent_times; i++) { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 3628 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 3629 | * all schema nodes that cannot be instantiated in data tree */ |
| 3630 | for (node = lys_parent(node); |
| 3631 | node && !(node->nodetype & (LYS_CONTAINER | LYS_LIST)); |
| 3632 | node = lys_parent(node)); |
| 3633 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3634 | if (!node) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3635 | LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, |
| 3636 | "leafref", path); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3637 | return EXIT_FAILURE; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3638 | } |
Michal Vasko | 5809090 | 2015-08-13 14:04:15 +0200 | [diff] [blame] | 3639 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3640 | } |
Michal Vasko | e01eca5 | 2015-08-13 14:42:02 +0200 | [diff] [blame] | 3641 | } else { |
| 3642 | LOGINT; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3643 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3644 | } |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3645 | |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3646 | first_iter = 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3647 | } else { |
Michal Vasko | 7dc71d0 | 2016-03-15 10:42:28 +0100 | [diff] [blame] | 3648 | /* move down the tree, if possible */ |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 3649 | if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
Michal Vasko | 2f5aceb | 2016-03-22 10:24:14 +0100 | [diff] [blame] | 3650 | LOGVAL(LYE_INCHAR, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, name[0], name); |
Michal Vasko | 7dc71d0 | 2016-03-15 10:42:28 +0100 | [diff] [blame] | 3651 | return -1; |
| 3652 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3653 | node = node->child; |
| 3654 | } |
| 3655 | |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 3656 | if (!prefix) { |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3657 | prefix = lys_node_module(parent)->name; |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 3658 | } |
| 3659 | |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3660 | rc = lys_get_sibling(node, prefix, pref_len, name, nam_len, LYS_ANY & ~(LYS_USES | LYS_GROUPING), &node); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3661 | if (rc) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3662 | LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, "leafref", path); |
Michal Vasko | 7a55bea | 2016-05-02 14:51:20 +0200 | [diff] [blame] | 3663 | return EXIT_FAILURE; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3664 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3665 | |
| 3666 | if (has_predicate) { |
| 3667 | /* we have predicate, so the current result must be list */ |
| 3668 | if (node->nodetype != LYS_LIST) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3669 | LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, "leafref", path); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3670 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3671 | } |
| 3672 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3673 | i = resolve_path_predicate_schema(id, node, parent); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3674 | if (!i) { |
Michal Vasko | f9664da | 2015-08-24 15:03:30 +0200 | [diff] [blame] | 3675 | return EXIT_FAILURE; |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3676 | } else if (i < 0) { |
| 3677 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3678 | } |
| 3679 | id += i; |
| 3680 | } |
| 3681 | } while (id[0]); |
| 3682 | |
Michal Vasko | ca91768 | 2016-07-25 11:00:37 +0200 | [diff] [blame] | 3683 | /* the target must be leaf or leaf-list (in YANG 1.1 only) */ |
| 3684 | if ((node->nodetype != LYS_LEAF) && ((lys_node_module(parent)->version != 2) || (node->nodetype != LYS_LEAFLIST))) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3685 | LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, "leafref", path); |
Radek Krejci | d47daf6 | 2016-08-22 16:23:38 +0200 | [diff] [blame] | 3686 | LOGVAL(LYE_SPEC, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, |
| 3687 | "Leafref target \"%s\" is not a leaf%s.", path, |
| 3688 | lys_node_module(parent)->version != 2 ? "" : " nor a leaf-list"); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3689 | return -1; |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 3690 | } |
| 3691 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3692 | /* check status */ |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3693 | if (lyp_check_status(parent->flags, parent->module, parent->name, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3694 | node->flags, node->module, node->name, node)) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3695 | return -1; |
| 3696 | } |
| 3697 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3698 | if (ret) { |
| 3699 | *ret = node; |
| 3700 | } |
| 3701 | return EXIT_SUCCESS; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3702 | } |
| 3703 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3704 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3705 | * @brief Resolve instance-identifier predicate in JSON data format. |
| 3706 | * Does not log. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3707 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3708 | * @param[in] pred Predicate to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3709 | * @param[in,out] node_match Nodes matching the restriction without |
| 3710 | * the predicate. Nodes not satisfying |
| 3711 | * the predicate are removed. |
| 3712 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3713 | * @return Number of characters successfully parsed, |
| 3714 | * positive on success, negative on failure. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3715 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3716 | static int |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3717 | resolve_predicate(const char *pred, struct unres_data *node_match) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3718 | { |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3719 | /* ... /node[target = value] ... */ |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3720 | struct unres_data target_match; |
| 3721 | struct ly_ctx *ctx; |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3722 | const struct lys_module *mod; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3723 | const char *model, *name, *value; |
| 3724 | char *str; |
| 3725 | int mod_len, nam_len, val_len, i, has_predicate, cur_idx, idx, parsed; |
| 3726 | uint32_t j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3727 | |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3728 | assert(pred && node_match->count); |
| 3729 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3730 | ctx = node_match->node[0]->schema->module->ctx; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3731 | idx = -1; |
| 3732 | parsed = 0; |
| 3733 | |
| 3734 | do { |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3735 | 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] | 3736 | return -parsed+i; |
| 3737 | } |
| 3738 | parsed += i; |
| 3739 | pred += i; |
| 3740 | |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3741 | /* pos */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3742 | if (isdigit(name[0])) { |
| 3743 | idx = atoi(name); |
| 3744 | } |
| 3745 | |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3746 | for (cur_idx = 0, j = 0; j < node_match->count; ++cur_idx) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3747 | /* target */ |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3748 | memset(&target_match, 0, sizeof target_match); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3749 | if ((name[0] == '.') || !value) { |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3750 | target_match.count = 1; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3751 | target_match.node = malloc(sizeof *target_match.node); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3752 | if (!target_match.node) { |
| 3753 | LOGMEM; |
| 3754 | return -1; |
| 3755 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3756 | target_match.node[0] = node_match->node[j]; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3757 | } else { |
| 3758 | str = strndup(model, mod_len); |
| 3759 | mod = ly_ctx_get_module(ctx, str, NULL); |
| 3760 | free(str); |
| 3761 | |
Radek Krejci | 804836a | 2016-02-03 10:39:55 +0100 | [diff] [blame] | 3762 | if (resolve_data(mod, name, nam_len, node_match->node[j]->child, &target_match)) { |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3763 | goto remove_instid; |
| 3764 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3765 | } |
| 3766 | |
| 3767 | /* check that we have the correct type */ |
| 3768 | if (name[0] == '.') { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3769 | if (node_match->node[j]->schema->nodetype != LYS_LEAFLIST) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3770 | goto remove_instid; |
| 3771 | } |
| 3772 | } else if (value) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3773 | if (node_match->node[j]->schema->nodetype != LYS_LIST) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3774 | goto remove_instid; |
| 3775 | } |
| 3776 | } |
| 3777 | |
Michal Vasko | 83a6c46 | 2015-10-08 16:43:53 +0200 | [diff] [blame] | 3778 | if ((value && (strncmp(((struct lyd_node_leaf_list *)target_match.node[0])->value_str, value, val_len) |
| 3779 | || ((struct lyd_node_leaf_list *)target_match.node[0])->value_str[val_len])) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3780 | || (!value && (idx != cur_idx))) { |
| 3781 | goto remove_instid; |
| 3782 | } |
| 3783 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3784 | free(target_match.node); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3785 | |
| 3786 | /* leafref is ok, continue check with next leafref */ |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3787 | ++j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3788 | continue; |
| 3789 | |
| 3790 | remove_instid: |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3791 | free(target_match.node); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3792 | |
| 3793 | /* does not fulfill conditions, remove leafref record */ |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3794 | unres_data_del(node_match, j); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3795 | } |
| 3796 | } while (has_predicate); |
| 3797 | |
| 3798 | return parsed; |
| 3799 | } |
| 3800 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3801 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3802 | * @brief Resolve instance-identifier in JSON data format. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3803 | * |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3804 | * @param[in] data Data node where the path is used |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3805 | * @param[in] path Instance-identifier node value. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3806 | * |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3807 | * @return Matching node or NULL if no such a node exists. If error occurs, NULL is returned and ly_errno is set. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3808 | */ |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3809 | static struct lyd_node * |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3810 | resolve_instid(struct lyd_node *data, const char *path) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3811 | { |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3812 | int i = 0, j; |
| 3813 | struct lyd_node *result = NULL; |
Michal Vasko | bea08e9 | 2016-05-18 13:28:08 +0200 | [diff] [blame] | 3814 | const struct lys_module *mod, *prev_mod; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3815 | struct ly_ctx *ctx = data->schema->module->ctx; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3816 | const char *model, *name; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3817 | char *str; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3818 | int mod_len, name_len, has_predicate; |
| 3819 | struct unres_data node_match; |
| 3820 | uint32_t k; |
| 3821 | |
| 3822 | memset(&node_match, 0, sizeof node_match); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3823 | |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3824 | /* we need root to resolve absolute path */ |
| 3825 | for (; data->parent; data = data->parent); |
Michal Vasko | db9323c | 2015-10-16 10:32:44 +0200 | [diff] [blame] | 3826 | /* we're still parsing it and the pointer is not correct yet */ |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3827 | if (data->prev) { |
| 3828 | for (; data->prev->next; data = data->prev); |
| 3829 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3830 | |
Michal Vasko | bea08e9 | 2016-05-18 13:28:08 +0200 | [diff] [blame] | 3831 | prev_mod = lyd_node_module(data); |
| 3832 | |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3833 | /* search for the instance node */ |
| 3834 | while (path[i]) { |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3835 | j = parse_instance_identifier(&path[i], &model, &mod_len, &name, &name_len, &has_predicate); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3836 | if (j <= 0) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3837 | LOGVAL(LYE_INCHAR, LY_VLOG_LYD, data, path[i-j], &path[i-j]); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3838 | goto error; |
| 3839 | } |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3840 | i += j; |
Michal Vasko | b387c48 | 2015-08-12 09:32:59 +0200 | [diff] [blame] | 3841 | |
Michal Vasko | bea08e9 | 2016-05-18 13:28:08 +0200 | [diff] [blame] | 3842 | if (model) { |
| 3843 | str = strndup(model, mod_len); |
| 3844 | if (!str) { |
| 3845 | LOGMEM; |
| 3846 | goto error; |
| 3847 | } |
| 3848 | mod = ly_ctx_get_module(ctx, str, NULL); |
| 3849 | free(str); |
| 3850 | } else { |
| 3851 | mod = prev_mod; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3852 | } |
| 3853 | |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3854 | if (resolve_data(mod, name, name_len, data, &node_match)) { |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3855 | /* no instance exists */ |
| 3856 | return NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3857 | } |
| 3858 | |
| 3859 | if (has_predicate) { |
| 3860 | /* we have predicate, so the current results must be list or leaf-list */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3861 | for (k = 0; k < node_match.count;) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3862 | if ((node_match.node[k]->schema->nodetype == LYS_LIST && |
| 3863 | ((struct lys_node_list *)node_match.node[k]->schema)->keys) |
| 3864 | || (node_match.node[k]->schema->nodetype == LYS_LEAFLIST)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3865 | /* instid is ok, continue check with next instid */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3866 | ++k; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3867 | continue; |
| 3868 | } |
| 3869 | |
| 3870 | /* does not fulfill conditions, remove inst record */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3871 | unres_data_del(&node_match, k); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3872 | } |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3873 | |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3874 | j = resolve_predicate(&path[i], &node_match); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3875 | if (j < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3876 | LOGVAL(LYE_INPRED, LY_VLOG_LYD, data, &path[i-j]); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3877 | goto error; |
| 3878 | } |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3879 | i += j; |
Michal Vasko | b387c48 | 2015-08-12 09:32:59 +0200 | [diff] [blame] | 3880 | |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3881 | if (!node_match.count) { |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3882 | /* no instance exists */ |
| 3883 | return NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3884 | } |
| 3885 | } |
Michal Vasko | bea08e9 | 2016-05-18 13:28:08 +0200 | [diff] [blame] | 3886 | |
| 3887 | prev_mod = mod; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3888 | } |
| 3889 | |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3890 | if (!node_match.count) { |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3891 | /* no instance exists */ |
| 3892 | return NULL; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3893 | } else if (node_match.count > 1) { |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3894 | /* instance identifier must resolve to a single node */ |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3895 | LOGVAL(LYE_TOOMANY, LY_VLOG_LYD, data, path, "data tree"); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3896 | |
Michal Vasko | d6adbaa | 2016-04-11 11:01:09 +0200 | [diff] [blame] | 3897 | goto error; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3898 | } else { |
| 3899 | /* we have required result, remember it and cleanup */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3900 | result = node_match.node[0]; |
| 3901 | free(node_match.node); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3902 | |
| 3903 | return result; |
| 3904 | } |
| 3905 | |
| 3906 | error: |
| 3907 | |
| 3908 | /* cleanup */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3909 | free(node_match.node); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3910 | |
| 3911 | return NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3912 | } |
| 3913 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3914 | /** |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 3915 | * @brief Passes config flag down to children, skips nodes without config flags. |
| 3916 | * Does not log. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3917 | * |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 3918 | * @param[in] node Siblings and their children to have flags changed. |
Pavol Vican | 4731993 | 2016-08-29 09:14:47 +0200 | [diff] [blame] | 3919 | * @param[in] ignore Flag to ignore check if parent is LYS_NOTIF, LYS_INPUT, LYS_OUTPUT, LYS_RPC. |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 3920 | * @param[in] flags Flags to assign to all the nodes. |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 3921 | * |
| 3922 | * @return 0 on success, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3923 | */ |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 3924 | static int |
Pavol Vican | 4731993 | 2016-08-29 09:14:47 +0200 | [diff] [blame] | 3925 | inherit_config_flag(struct lys_node *node, int ignore, int flags) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3926 | { |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 3927 | assert(!(flags ^ (flags & LYS_CONFIG_MASK))); |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 3928 | LY_TREE_FOR(node, node) { |
Pavol Vican | 4731993 | 2016-08-29 09:14:47 +0200 | [diff] [blame] | 3929 | if (!ignore && (node->flags & LYS_CONFIG_SET)) { |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 3930 | /* skip nodes with an explicit config value */ |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 3931 | if ((flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) { |
| 3932 | LOGVAL(LYE_INARG, LY_VLOG_LYS, node, "true", "config"); |
| 3933 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, node, "State nodes cannot have configuration nodes as children."); |
| 3934 | return -1; |
| 3935 | } |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 3936 | continue; |
| 3937 | } |
| 3938 | if (!(node->nodetype & (LYS_USES | LYS_GROUPING))) { |
Radek Krejci | 43f548d | 2016-06-16 11:06:38 +0200 | [diff] [blame] | 3939 | node->flags = (node->flags & ~LYS_CONFIG_MASK) | flags; |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 3940 | } |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 3941 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) { |
Pavol Vican | 4731993 | 2016-08-29 09:14:47 +0200 | [diff] [blame] | 3942 | if (inherit_config_flag(node->child, ignore, flags)) { |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 3943 | return -1; |
| 3944 | } |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 3945 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3946 | } |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 3947 | |
| 3948 | return 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3949 | } |
| 3950 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3951 | /** |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 3952 | * @brief Resolve augment target. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3953 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3954 | * @param[in] aug Augment to use. |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 3955 | * @param[in] siblings Nodes where to start the search in. If set, uses augment, if not, standalone augment. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3956 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3957 | * @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] | 3958 | */ |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 3959 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3960 | resolve_augment(struct lys_node_augment *aug, struct lys_node *siblings) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3961 | { |
Pavol Vican | 4731993 | 2016-08-29 09:14:47 +0200 | [diff] [blame] | 3962 | int rc, ignore_config; |
Michal Vasko | 1d87a92 | 2015-08-21 12:57:16 +0200 | [diff] [blame] | 3963 | struct lys_node *sub; |
Pavol Vican | 4731993 | 2016-08-29 09:14:47 +0200 | [diff] [blame] | 3964 | const struct lys_node *aug_target, *parent; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3965 | |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 3966 | assert(aug && !aug->target); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3967 | |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 3968 | /* resolve target node */ |
| 3969 | rc = resolve_augment_schema_nodeid(aug->target_name, siblings, (siblings ? NULL : aug->module), &aug_target); |
| 3970 | if (rc == -1) { |
| 3971 | return -1; |
| 3972 | } |
| 3973 | if (rc > 0) { |
| 3974 | LOGVAL(LYE_INCHAR, LY_VLOG_LYS, aug, aug->target_name[rc - 1], &aug->target_name[rc - 1]); |
| 3975 | return -1; |
| 3976 | } |
| 3977 | if (!aug_target) { |
| 3978 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, aug, "augment", aug->target_name); |
| 3979 | return EXIT_FAILURE; |
| 3980 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3981 | |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 3982 | if (!aug->child) { |
| 3983 | /* nothing to do */ |
| 3984 | LOGWRN("Augment \"%s\" without children.", aug->target_name); |
| 3985 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3986 | } |
| 3987 | |
Michal Vasko | d58d596 | 2016-03-02 14:29:41 +0100 | [diff] [blame] | 3988 | /* check for mandatory nodes - if the target node is in another module |
| 3989 | * the added nodes cannot be mandatory |
| 3990 | */ |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 3991 | if (!aug->parent && (lys_node_module((struct lys_node *)aug) != lys_node_module(aug_target)) |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 3992 | && (rc = lyp_check_mandatory_augment(aug))) { |
| 3993 | return rc; |
Michal Vasko | d58d596 | 2016-03-02 14:29:41 +0100 | [diff] [blame] | 3994 | } |
| 3995 | |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 3996 | /* check augment target type and then augment nodes type */ |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 3997 | if (aug_target->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_CASE | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF)) { |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 3998 | LY_TREE_FOR(aug->child, sub) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 3999 | 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] | 4000 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment"); |
| 4001 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, aug, "Cannot augment \"%s\" with a \"%s\".", |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4002 | strnodetype(aug_target->nodetype), strnodetype(sub->nodetype)); |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4003 | return -1; |
| 4004 | } |
| 4005 | } |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4006 | } else if (aug_target->nodetype == LYS_CHOICE) { |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4007 | LY_TREE_FOR(aug->child, sub) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4008 | 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] | 4009 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment"); |
| 4010 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, aug, "Cannot augment \"%s\" with a \"%s\".", |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4011 | strnodetype(aug_target->nodetype), strnodetype(sub->nodetype)); |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4012 | return -1; |
| 4013 | } |
| 4014 | } |
| 4015 | } else { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4016 | LOGVAL(LYE_INARG, LY_VLOG_LYS, aug, aug->target_name, "target-node"); |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4017 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, aug, "Invalid augment target node type \"%s\".", strnodetype(aug_target->nodetype)); |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4018 | return -1; |
| 4019 | } |
| 4020 | |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4021 | /* inherit config information from actual parent */ |
Pavol Vican | 4731993 | 2016-08-29 09:14:47 +0200 | [diff] [blame] | 4022 | for(parent = aug_target; parent && !(parent->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT | LYS_RPC)); parent = parent->parent); |
| 4023 | ignore_config = (parent) ? 1 : 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4024 | LY_TREE_FOR(aug->child, sub) { |
Pavol Vican | 4731993 | 2016-08-29 09:14:47 +0200 | [diff] [blame] | 4025 | if (inherit_config_flag(sub, ignore_config, aug_target->flags & LYS_CONFIG_MASK)) { |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4026 | return -1; |
| 4027 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4028 | } |
| 4029 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 4030 | /* check identifier uniqueness as in lys_node_addchild() */ |
Michal Vasko | 1d87a92 | 2015-08-21 12:57:16 +0200 | [diff] [blame] | 4031 | LY_TREE_FOR(aug->child, sub) { |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4032 | if (lys_check_id(sub, (struct lys_node *)aug_target, NULL)) { |
Michal Vasko | 3e6665f | 2015-08-17 14:00:38 +0200 | [diff] [blame] | 4033 | return -1; |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 4034 | } |
| 4035 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4036 | |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4037 | /* finally reconnect augmenting data into the target - add them to the target child list, |
| 4038 | * by setting aug->target we know the augment is fully resolved now */ |
| 4039 | aug->target = (struct lys_node *)aug_target; |
| 4040 | if (aug->target->child) { |
| 4041 | sub = aug->target->child->prev; /* remember current target's last node */ |
| 4042 | sub->next = aug->child; /* connect augmenting data after target's last node */ |
| 4043 | aug->target->child->prev = aug->child->prev; /* new target's last node is last augmenting node */ |
| 4044 | aug->child->prev = sub; /* finish connecting of both child lists */ |
| 4045 | } else { |
| 4046 | aug->target->child = aug->child; |
| 4047 | } |
| 4048 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4049 | return EXIT_SUCCESS; |
| 4050 | } |
| 4051 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4052 | /** |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4053 | * @brief Resolve (find) choice default case. Does not log. |
| 4054 | * |
| 4055 | * @param[in] choic Choice to use. |
| 4056 | * @param[in] dflt Name of the default case. |
| 4057 | * |
| 4058 | * @return Pointer to the default node or NULL. |
| 4059 | */ |
| 4060 | static struct lys_node * |
| 4061 | resolve_choice_dflt(struct lys_node_choice *choic, const char *dflt) |
| 4062 | { |
| 4063 | struct lys_node *child, *ret; |
| 4064 | |
| 4065 | LY_TREE_FOR(choic->child, child) { |
| 4066 | if (child->nodetype == LYS_USES) { |
| 4067 | ret = resolve_choice_dflt((struct lys_node_choice *)child, dflt); |
| 4068 | if (ret) { |
| 4069 | return ret; |
| 4070 | } |
| 4071 | } |
| 4072 | |
| 4073 | if (ly_strequal(child->name, dflt, 1) && (child->nodetype & (LYS_ANYDATA | LYS_CASE |
| 4074 | | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST))) { |
| 4075 | return child; |
| 4076 | } |
| 4077 | } |
| 4078 | |
| 4079 | return NULL; |
| 4080 | } |
| 4081 | |
| 4082 | /** |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4083 | * @brief Resolve uses, apply augments, refines. Logs directly. |
| 4084 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4085 | * @param[in] uses Uses to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4086 | * @param[in,out] unres List of unresolved items. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4087 | * |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 4088 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4089 | */ |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 4090 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4091 | resolve_uses(struct lys_node_uses *uses, struct unres_schema *unres) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4092 | { |
| 4093 | struct ly_ctx *ctx; |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4094 | struct lys_node *node = NULL, *next, *iter, **refine_nodes = NULL; |
Pavol Vican | 55abd33 | 2016-07-12 15:54:49 +0200 | [diff] [blame] | 4095 | struct lys_node *node_aux, *parent, *tmp; |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4096 | struct lys_node_leaflist *llist; |
| 4097 | struct lys_node_leaf *leaf; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 4098 | struct lys_refine *rfn; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4099 | struct lys_restr *must, **old_must; |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4100 | struct lys_iffeature *iff, **old_iff; |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4101 | int i, j, k, rc, parent_config, ignore_config; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4102 | uint8_t size, *old_size; |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4103 | unsigned int usize, usize1, usize2; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4104 | |
Michal Vasko | 71e1aa8 | 2015-08-12 12:17:51 +0200 | [diff] [blame] | 4105 | assert(uses->grp); |
Michal Vasko | e770851 | 2016-03-11 10:26:55 +0100 | [diff] [blame] | 4106 | /* HACK just check that the grouping is resolved */ |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 4107 | assert(!uses->grp->nacm); |
Michal Vasko | 71e1aa8 | 2015-08-12 12:17:51 +0200 | [diff] [blame] | 4108 | |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4109 | if (!uses->grp->child) { |
| 4110 | /* grouping without children, warning was already displayed */ |
| 4111 | return EXIT_SUCCESS; |
| 4112 | } |
| 4113 | |
| 4114 | /* get proper parent (config) flags */ |
| 4115 | for (node_aux = lys_parent((struct lys_node *)uses); node_aux && (node_aux->nodetype == LYS_USES); node_aux = lys_parent(node_aux)); |
| 4116 | if (node_aux) { |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4117 | parent_config = node_aux->flags & LYS_CONFIG_MASK; |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4118 | } else { |
| 4119 | /* default */ |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4120 | parent_config = LYS_CONFIG_W; |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4121 | } |
| 4122 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4123 | /* copy the data nodes from grouping into the uses context */ |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 4124 | LY_TREE_FOR(uses->grp->child, node_aux) { |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4125 | node = lys_node_dup(uses->module, (struct lys_node *)uses, node_aux, 0, uses->nacm, unres, 0); |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 4126 | if (!node) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4127 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, uses->grp->name, "uses"); |
| 4128 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, "Copying data from grouping failed."); |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4129 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4130 | } |
Pavol Vican | 55abd33 | 2016-07-12 15:54:49 +0200 | [diff] [blame] | 4131 | /* test the name of siblings */ |
| 4132 | LY_TREE_FOR((uses->parent) ? uses->parent->child : lys_main_module(uses->module)->data, tmp) { |
Pavol Vican | 2510ddc | 2016-07-18 16:23:44 +0200 | [diff] [blame] | 4133 | 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] | 4134 | goto fail; |
Pavol Vican | 55abd33 | 2016-07-12 15:54:49 +0200 | [diff] [blame] | 4135 | } |
| 4136 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4137 | } |
| 4138 | ctx = uses->module->ctx; |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4139 | for(parent = node; parent && !(parent->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT | LYS_RPC)); parent = parent->parent); |
| 4140 | ignore_config = (parent) ? 1 : 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4141 | |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4142 | if (parent_config) { |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4143 | assert(uses->child); |
Pavol Vican | 4731993 | 2016-08-29 09:14:47 +0200 | [diff] [blame] | 4144 | if (inherit_config_flag(uses->child, ignore_config, parent_config)) { |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4145 | goto fail; |
| 4146 | } |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4147 | } |
| 4148 | |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 4149 | /* we managed to copy the grouping, the rest must be possible to resolve */ |
| 4150 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4151 | if (uses->refine_size) { |
| 4152 | refine_nodes = malloc(uses->refine_size * sizeof *refine_nodes); |
| 4153 | if (!refine_nodes) { |
| 4154 | LOGMEM; |
| 4155 | goto fail; |
| 4156 | } |
| 4157 | } |
| 4158 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4159 | /* apply refines */ |
| 4160 | for (i = 0; i < uses->refine_size; i++) { |
| 4161 | rfn = &uses->refine[i]; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 4162 | rc = resolve_descendant_schema_nodeid(rfn->target_name, uses->child, LYS_NO_RPC_NOTIF_NODE, |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 4163 | 1, 0, (const struct lys_node **)&node); |
Michal Vasko | 9bb061b | 2016-02-12 11:00:19 +0100 | [diff] [blame] | 4164 | if (rc || !node) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4165 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine"); |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4166 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4167 | } |
| 4168 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4169 | if (rfn->target_type && !(node->nodetype & rfn->target_type)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4170 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine"); |
| 4171 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, "Refine substatements not applicable to the target-node."); |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4172 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4173 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4174 | refine_nodes[i] = node; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4175 | |
| 4176 | /* description on any nodetype */ |
| 4177 | if (rfn->dsc) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4178 | lydict_remove(ctx, node->dsc); |
| 4179 | node->dsc = lydict_insert(ctx, rfn->dsc, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4180 | } |
| 4181 | |
| 4182 | /* reference on any nodetype */ |
| 4183 | if (rfn->ref) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4184 | lydict_remove(ctx, node->ref); |
| 4185 | node->ref = lydict_insert(ctx, rfn->ref, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4186 | } |
| 4187 | |
| 4188 | /* config on any nodetype */ |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4189 | if ((rfn->flags & LYS_CONFIG_MASK) && !ignore_config) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4190 | node->flags &= ~LYS_CONFIG_MASK; |
| 4191 | node->flags |= (rfn->flags & LYS_CONFIG_MASK); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4192 | } |
| 4193 | |
| 4194 | /* default value ... */ |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4195 | if (rfn->dflt_size) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4196 | if (node->nodetype == LYS_LEAF) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4197 | /* leaf */ |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4198 | leaf = (struct lys_node_leaf *)node; |
| 4199 | |
| 4200 | lydict_remove(ctx, leaf->dflt); |
| 4201 | leaf->dflt = lydict_insert(ctx, rfn->dflt[0], 0); |
| 4202 | |
| 4203 | /* check the default value */ |
| 4204 | if (unres_schema_add_str(leaf->module, unres, &leaf->type, UNRES_TYPE_DFLT, leaf->dflt) == -1) { |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4205 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4206 | } |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4207 | } else if (node->nodetype == LYS_LEAFLIST) { |
| 4208 | /* leaf-list */ |
| 4209 | llist = (struct lys_node_leaflist *)node; |
| 4210 | |
| 4211 | /* remove complete set of defaults in target */ |
| 4212 | for (i = 0; i < llist->dflt_size; i++) { |
| 4213 | lydict_remove(ctx, llist->dflt[i]); |
| 4214 | } |
| 4215 | free(llist->dflt); |
| 4216 | |
| 4217 | /* copy the default set from refine */ |
| 4218 | llist->dflt_size = rfn->dflt_size; |
| 4219 | llist->dflt = malloc(llist->dflt_size * sizeof *llist->dflt); |
| 4220 | for (i = 0; i < llist->dflt_size; i++) { |
| 4221 | llist->dflt[i] = lydict_insert(ctx, rfn->dflt[i], 0); |
| 4222 | } |
| 4223 | |
| 4224 | /* check default value */ |
| 4225 | for (i = 0; i < llist->dflt_size; i++) { |
| 4226 | if (unres_schema_add_str(llist->module, unres, &llist->type, UNRES_TYPE_DFLT, llist->dflt[i]) == -1) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4227 | goto fail; |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4228 | } |
| 4229 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4230 | } |
| 4231 | } |
| 4232 | |
| 4233 | /* mandatory on leaf, anyxml or choice */ |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 4234 | if (rfn->flags & LYS_MAND_MASK) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4235 | if (node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_CHOICE)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4236 | /* remove current value */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4237 | node->flags &= ~LYS_MAND_MASK; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4238 | |
| 4239 | /* set new value */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4240 | node->flags |= (rfn->flags & LYS_MAND_MASK); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4241 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4242 | if (rfn->flags & LYS_MAND_TRUE) { |
| 4243 | /* check if node has default value */ |
| 4244 | if ((node->nodetype & LYS_LEAF) && ((struct lys_node_leaf *)node)->dflt) { |
| 4245 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The \"mandatory\" statement is forbidden on leaf with \"default\"."); |
| 4246 | goto fail; |
| 4247 | } |
| 4248 | if ((node->nodetype & LYS_CHOICE) && ((struct lys_node_choice *)node)->dflt) { |
| 4249 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The \"mandatory\" statement is forbidden on choices with \"default\"."); |
| 4250 | goto fail; |
| 4251 | } |
| 4252 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4253 | } |
| 4254 | |
| 4255 | /* presence on container */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4256 | if ((node->nodetype & LYS_CONTAINER) && rfn->mod.presence) { |
| 4257 | lydict_remove(ctx, ((struct lys_node_container *)node)->presence); |
| 4258 | ((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] | 4259 | } |
| 4260 | |
| 4261 | /* min/max-elements on list or leaf-list */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4262 | if (node->nodetype == LYS_LIST) { |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 4263 | if (rfn->flags & LYS_RFN_MINSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4264 | ((struct lys_node_list *)node)->min = rfn->mod.list.min; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4265 | } |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 4266 | if (rfn->flags & LYS_RFN_MAXSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4267 | ((struct lys_node_list *)node)->max = rfn->mod.list.max; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4268 | } |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4269 | } else if (node->nodetype == LYS_LEAFLIST) { |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 4270 | if (rfn->flags & LYS_RFN_MINSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4271 | ((struct lys_node_leaflist *)node)->min = rfn->mod.list.min; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4272 | } |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 4273 | if (rfn->flags & LYS_RFN_MAXSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4274 | ((struct lys_node_leaflist *)node)->max = rfn->mod.list.max; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4275 | } |
| 4276 | } |
| 4277 | |
| 4278 | /* must in leaf, leaf-list, list, container or anyxml */ |
| 4279 | if (rfn->must_size) { |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4280 | switch (node->nodetype) { |
| 4281 | case LYS_LEAF: |
| 4282 | old_size = &((struct lys_node_leaf *)node)->must_size; |
| 4283 | old_must = &((struct lys_node_leaf *)node)->must; |
| 4284 | break; |
| 4285 | case LYS_LEAFLIST: |
| 4286 | old_size = &((struct lys_node_leaflist *)node)->must_size; |
| 4287 | old_must = &((struct lys_node_leaflist *)node)->must; |
| 4288 | break; |
| 4289 | case LYS_LIST: |
| 4290 | old_size = &((struct lys_node_list *)node)->must_size; |
| 4291 | old_must = &((struct lys_node_list *)node)->must; |
| 4292 | break; |
| 4293 | case LYS_CONTAINER: |
| 4294 | old_size = &((struct lys_node_container *)node)->must_size; |
| 4295 | old_must = &((struct lys_node_container *)node)->must; |
| 4296 | break; |
| 4297 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4298 | case LYS_ANYDATA: |
| 4299 | old_size = &((struct lys_node_anydata *)node)->must_size; |
| 4300 | old_must = &((struct lys_node_anydata *)node)->must; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4301 | break; |
| 4302 | default: |
| 4303 | LOGINT; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4304 | goto fail; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4305 | } |
| 4306 | |
| 4307 | size = *old_size + rfn->must_size; |
| 4308 | must = realloc(*old_must, size * sizeof *rfn->must); |
| 4309 | if (!must) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4310 | LOGMEM; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4311 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4312 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4313 | for (k = 0, j = *old_size; k < rfn->must_size; k++, j++) { |
| 4314 | must[j].expr = lydict_insert(ctx, rfn->must[k].expr, 0); |
| 4315 | must[j].dsc = lydict_insert(ctx, rfn->must[k].dsc, 0); |
| 4316 | must[j].ref = lydict_insert(ctx, rfn->must[k].ref, 0); |
| 4317 | must[j].eapptag = lydict_insert(ctx, rfn->must[k].eapptag, 0); |
| 4318 | must[j].emsg = lydict_insert(ctx, rfn->must[k].emsg, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4319 | } |
| 4320 | |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4321 | *old_must = must; |
| 4322 | *old_size = size; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 4323 | |
| 4324 | /* check XPath dependencies again */ |
| 4325 | if (unres_schema_add_node(node->module, unres, node, UNRES_XPATH, NULL) == -1) { |
| 4326 | goto fail; |
| 4327 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4328 | } |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4329 | |
| 4330 | /* if-feature in leaf, leaf-list, list, container or anyxml */ |
| 4331 | if (rfn->iffeature_size) { |
| 4332 | old_size = &node->iffeature_size; |
| 4333 | old_iff = &node->iffeature; |
| 4334 | |
| 4335 | size = *old_size + rfn->iffeature_size; |
| 4336 | iff = realloc(*old_iff, size * sizeof *rfn->iffeature); |
| 4337 | if (!iff) { |
| 4338 | LOGMEM; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4339 | goto fail; |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4340 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4341 | for (k = 0, j = *old_size; k < rfn->iffeature_size; k++, j++) { |
| 4342 | resolve_iffeature_getsizes(&rfn->iffeature[k], &usize1, &usize2); |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4343 | if (usize1) { |
| 4344 | /* there is something to duplicate */ |
| 4345 | /* duplicate compiled expression */ |
| 4346 | usize = (usize1 / 4) + (usize1 % 4) ? 1 : 0; |
| 4347 | iff[j].expr = malloc(usize * sizeof *iff[j].expr); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4348 | 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] | 4349 | |
| 4350 | /* duplicate list of feature pointers */ |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4351 | iff[j].features = malloc(usize2 * sizeof *iff[k].features); |
| 4352 | 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] | 4353 | } |
| 4354 | } |
| 4355 | |
| 4356 | *old_iff = iff; |
| 4357 | *old_size = size; |
| 4358 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4359 | } |
| 4360 | |
| 4361 | /* apply augments */ |
| 4362 | for (i = 0; i < uses->augment_size; i++) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4363 | rc = resolve_augment(&uses->augment[i], uses->child); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4364 | if (rc) { |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4365 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4366 | } |
| 4367 | } |
| 4368 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4369 | /* check refines */ |
| 4370 | for (i = 0; i < uses->refine_size; i++) { |
| 4371 | node = refine_nodes[i]; |
| 4372 | rfn = &uses->refine[i]; |
| 4373 | |
| 4374 | /* config on any nodetype */ |
| 4375 | if ((rfn->flags & LYS_CONFIG_MASK) && !ignore_config) { |
| 4376 | for (parent = lys_parent(node); parent && parent->nodetype == LYS_USES; parent = lys_parent(parent)); |
| 4377 | if (parent && parent->nodetype != LYS_GROUPING && |
| 4378 | ((parent->flags & LYS_CONFIG_MASK) != (rfn->flags & LYS_CONFIG_MASK)) && |
| 4379 | (rfn->flags & LYS_CONFIG_W)) { |
| 4380 | /* setting config true under config false is prohibited */ |
| 4381 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, "config", "refine"); |
| 4382 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, |
| 4383 | "changing config from 'false' to 'true' is prohibited while " |
| 4384 | "the target's parent is still config 'false'."); |
| 4385 | goto fail; |
| 4386 | } |
| 4387 | |
| 4388 | /* inherit config change to the target children */ |
| 4389 | LY_TREE_DFS_BEGIN(node->child, next, iter) { |
| 4390 | if (rfn->flags & LYS_CONFIG_W) { |
| 4391 | if (iter->flags & LYS_CONFIG_SET) { |
| 4392 | /* config is set explicitely, go to next sibling */ |
| 4393 | next = NULL; |
| 4394 | goto nextsibling; |
| 4395 | } |
| 4396 | } else { /* LYS_CONFIG_R */ |
| 4397 | if ((iter->flags & LYS_CONFIG_SET) && (iter->flags & LYS_CONFIG_W)) { |
| 4398 | /* error - we would have config data under status data */ |
| 4399 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, "config", "refine"); |
| 4400 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, |
| 4401 | "changing config from 'true' to 'false' is prohibited while the target " |
| 4402 | "has still a children with explicit config 'true'."); |
| 4403 | goto fail; |
| 4404 | } |
| 4405 | } |
| 4406 | /* change config */ |
| 4407 | iter->flags &= ~LYS_CONFIG_MASK; |
| 4408 | iter->flags |= (rfn->flags & LYS_CONFIG_MASK); |
| 4409 | |
| 4410 | /* select next iter - modified LY_TREE_DFS_END */ |
| 4411 | if (iter->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
| 4412 | next = NULL; |
| 4413 | } else { |
| 4414 | next = iter->child; |
| 4415 | } |
| 4416 | nextsibling: |
| 4417 | if (!next) { |
| 4418 | /* try siblings */ |
| 4419 | next = iter->next; |
| 4420 | } |
| 4421 | while (!next) { |
| 4422 | /* parent is already processed, go to its sibling */ |
| 4423 | iter = lys_parent(iter); |
| 4424 | |
| 4425 | /* no siblings, go back through parents */ |
| 4426 | if (iter == node) { |
| 4427 | /* we are done, no next element to process */ |
| 4428 | break; |
| 4429 | } |
| 4430 | next = iter->next; |
| 4431 | } |
| 4432 | } |
| 4433 | } |
| 4434 | |
| 4435 | /* default value */ |
| 4436 | if (rfn->dflt_size && node->nodetype == LYS_CHOICE) { |
| 4437 | /* choice */ |
| 4438 | |
| 4439 | ((struct lys_node_choice *)node)->dflt = resolve_choice_dflt((struct lys_node_choice *)node, |
| 4440 | rfn->dflt[0]); |
| 4441 | if (!((struct lys_node_choice *)node)->dflt) { |
| 4442 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->dflt[0], "default"); |
| 4443 | goto fail; |
| 4444 | } |
| 4445 | if (lyp_check_mandatory_choice(node)) { |
| 4446 | goto fail; |
| 4447 | } |
| 4448 | } |
| 4449 | |
| 4450 | /* min/max-elements on list or leaf-list */ |
| 4451 | if (node->nodetype == LYS_LIST) { |
| 4452 | if (((struct lys_node_list *)node)->min > ((struct lys_node_list *)node)->max) { |
| 4453 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid value \"%d\" of \"%s\".", rfn->mod.list.min, "min-elements"); |
| 4454 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"min-elements\" is bigger than \"max-elements\"."); |
| 4455 | goto fail; |
| 4456 | } |
| 4457 | } else if (node->nodetype == LYS_LEAFLIST) { |
| 4458 | if (((struct lys_node_leaflist *)node)->min > ((struct lys_node_leaflist *)node)->max) { |
| 4459 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid value \"%d\" of \"%s\".", rfn->mod.list.min, "min-elements"); |
| 4460 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"min-elements\" is bigger than \"max-elements\"."); |
| 4461 | goto fail; |
| 4462 | } |
| 4463 | } |
| 4464 | |
| 4465 | /* additional checks */ |
| 4466 | if (node->nodetype == LYS_LEAFLIST) { |
| 4467 | llist = (struct lys_node_leaflist *)node; |
| 4468 | if (llist->dflt_size && llist->min) { |
| 4469 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, rfn->dflt_size ? "default" : "min-elements", "refine"); |
| 4470 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, |
| 4471 | "The \"min-elements\" statement with non-zero value is forbidden on leaf-lists with the \"default\" statement."); |
| 4472 | goto fail; |
| 4473 | } |
| 4474 | } |
| 4475 | if ((rfn->flags & LYS_MAND_TRUE) || rfn->mod.list.min) { |
| 4476 | /* check for mandatory node in default case, first find the closest parent choice to the changed node */ |
| 4477 | for (parent = node->parent; |
| 4478 | parent && !(parent->nodetype & (LYS_CHOICE | LYS_GROUPING | LYS_ACTION | LYS_USES)); |
| 4479 | parent = parent->parent) { |
| 4480 | if (parent->nodetype == LYS_CONTAINER && ((struct lys_node_container *)parent)->presence) { |
| 4481 | /* stop also on presence containers */ |
| 4482 | break; |
| 4483 | } |
| 4484 | } |
| 4485 | /* and if it is a choice with the default case, check it for presence of a mandatory node in it */ |
| 4486 | if (parent && parent->nodetype == LYS_CHOICE && ((struct lys_node_choice *)parent)->dflt) { |
| 4487 | if (lyp_check_mandatory_choice(parent)) { |
| 4488 | goto fail; |
| 4489 | } |
| 4490 | } |
| 4491 | } |
| 4492 | } |
| 4493 | free(refine_nodes); |
| 4494 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4495 | return EXIT_SUCCESS; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4496 | |
| 4497 | fail: |
| 4498 | LY_TREE_FOR_SAFE(uses->child, next, iter) { |
| 4499 | lys_node_free(iter, NULL, 0); |
| 4500 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4501 | free(refine_nodes); |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4502 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4503 | } |
| 4504 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4505 | static int |
| 4506 | identity_backlink_update(struct lys_ident *der, struct lys_ident *base) |
| 4507 | { |
| 4508 | int i; |
| 4509 | |
| 4510 | assert(der && base); |
| 4511 | |
| 4512 | base->der = ly_realloc(base->der, (base->der_size + 1) * sizeof *(base->der)); |
| 4513 | if (!base->der) { |
| 4514 | LOGMEM; |
| 4515 | return EXIT_FAILURE; |
| 4516 | } |
| 4517 | base->der[base->der_size++] = der; |
| 4518 | |
| 4519 | for (i = 0; i < base->base_size; i++) { |
| 4520 | if (identity_backlink_update(der, base->base[i])) { |
| 4521 | return EXIT_FAILURE; |
| 4522 | } |
| 4523 | } |
| 4524 | |
| 4525 | return EXIT_SUCCESS; |
| 4526 | } |
| 4527 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4528 | /** |
| 4529 | * @brief Resolve base identity recursively. Does not log. |
| 4530 | * |
| 4531 | * @param[in] module Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4532 | * @param[in] ident Identity to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4533 | * @param[in] basename Base name of the identity. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4534 | * @param[out] ret Pointer to the resolved identity. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4535 | * |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 4536 | * @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] | 4537 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4538 | static int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 4539 | 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] | 4540 | struct unres_schema *unres, struct lys_ident **ret) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4541 | { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 4542 | uint32_t i, j; |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4543 | struct lys_ident *base = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4544 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 4545 | assert(ret); |
| 4546 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4547 | /* search module */ |
| 4548 | for (i = 0; i < module->ident_size; i++) { |
| 4549 | if (!strcmp(basename, module->ident[i].name)) { |
| 4550 | |
| 4551 | if (!ident) { |
| 4552 | /* just search for type, so do not modify anything, just return |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 4553 | * the base identity pointer */ |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 4554 | *ret = &module->ident[i]; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4555 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4556 | } |
| 4557 | |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 4558 | base = &module->ident[i]; |
| 4559 | goto matchfound; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4560 | } |
| 4561 | } |
| 4562 | |
| 4563 | /* search submodules */ |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 4564 | for (j = 0; j < module->inc_size && module->inc[j].submodule; j++) { |
| 4565 | for (i = 0; i < module->inc[j].submodule->ident_size; i++) { |
| 4566 | if (!strcmp(basename, module->inc[j].submodule->ident[i].name)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4567 | |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 4568 | if (!ident) { |
| 4569 | *ret = &module->inc[j].submodule->ident[i]; |
| 4570 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4571 | } |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 4572 | |
| 4573 | base = &module->inc[j].submodule->ident[i]; |
| 4574 | goto matchfound; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4575 | } |
| 4576 | } |
| 4577 | } |
| 4578 | |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 4579 | matchfound: |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4580 | /* we found it somewhere */ |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 4581 | if (base) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4582 | /* is it already completely resolved? */ |
| 4583 | for (i = 0; i < unres->count; i++) { |
Radek Krejci | ba7b1cc | 2016-08-08 13:44:43 +0200 | [diff] [blame] | 4584 | if ((unres->item[i] == base) && (unres->type[i] == UNRES_IDENT)) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4585 | /* identity found, but not yet resolved, so do not return it in *res and try it again later */ |
| 4586 | |
| 4587 | /* simple check for circular reference, |
| 4588 | * the complete check is done as a side effect of using only completely |
| 4589 | * resolved identities (previous check of unres content) */ |
| 4590 | if (ly_strequal((const char *)unres->str_snode[i], ident->name, 1)) { |
| 4591 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, basename, "base"); |
| 4592 | 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] | 4593 | return -1; |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4594 | } |
| 4595 | |
Radek Krejci | 06f64ed | 2016-08-15 11:07:44 +0200 | [diff] [blame] | 4596 | return EXIT_FAILURE; |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 4597 | } |
| 4598 | } |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4599 | |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 4600 | /* checks done, store the result */ |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4601 | *ret = base; |
Pavol Vican | fdab9f9 | 2016-09-07 15:23:27 +0200 | [diff] [blame^] | 4602 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4603 | } |
| 4604 | |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 4605 | /* base not found (maybe a forward reference) */ |
| 4606 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4607 | } |
| 4608 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4609 | /** |
| 4610 | * @brief Resolve base identity. Logs directly. |
| 4611 | * |
| 4612 | * @param[in] module Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4613 | * @param[in] ident Identity to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4614 | * @param[in] basename Base name of the identity. |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 4615 | * @param[in] parent Either "type" or "identity". |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 4616 | * @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] | 4617 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4618 | * @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] | 4619 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4620 | static int |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 4621 | 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] | 4622 | struct lys_type *type, struct unres_schema *unres) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4623 | { |
| 4624 | const char *name; |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 4625 | int mod_name_len = 0, rc; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 4626 | struct lys_ident *target, **ret; |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 4627 | uint16_t flags; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 4628 | struct lys_module *mod; |
| 4629 | |
| 4630 | assert((ident && !type) || (!ident && type)); |
| 4631 | |
| 4632 | if (!type) { |
| 4633 | /* have ident to resolve */ |
| 4634 | ret = ⌖ |
| 4635 | flags = ident->flags; |
| 4636 | mod = ident->module; |
| 4637 | } else { |
| 4638 | /* have type to fill */ |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 4639 | ++type->info.ident.count; |
| 4640 | type->info.ident.ref = ly_realloc(type->info.ident.ref, type->info.ident.count * sizeof *type->info.ident.ref); |
| 4641 | if (!type->info.ident.ref) { |
| 4642 | LOGMEM; |
| 4643 | return -1; |
| 4644 | } |
| 4645 | |
| 4646 | ret = &type->info.ident.ref[type->info.ident.count - 1]; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 4647 | flags = type->parent->flags; |
| 4648 | mod = type->parent->module; |
| 4649 | } |
Michal Vasko | f200600 | 2016-04-21 16:28:15 +0200 | [diff] [blame] | 4650 | *ret = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4651 | |
| 4652 | /* search for the base identity */ |
| 4653 | name = strchr(basename, ':'); |
| 4654 | if (name) { |
| 4655 | /* set name to correct position after colon */ |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 4656 | mod_name_len = name - basename; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4657 | name++; |
| 4658 | |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 4659 | 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] | 4660 | /* prefix refers to the current module, ignore it */ |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 4661 | mod_name_len = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4662 | } |
| 4663 | } else { |
| 4664 | name = basename; |
| 4665 | } |
| 4666 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 4667 | /* get module where to search */ |
| 4668 | module = lys_get_import_module(module, NULL, 0, mod_name_len ? basename : NULL, mod_name_len); |
| 4669 | if (!module) { |
| 4670 | /* identity refers unknown data model */ |
Radek Krejci | 02a0499 | 2016-03-17 16:06:37 +0100 | [diff] [blame] | 4671 | LOGVAL(LYE_INMOD, LY_VLOG_NONE, NULL, basename); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 4672 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4673 | } |
| 4674 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 4675 | /* search in the identified module ... */ |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 4676 | rc = resolve_base_ident_sub(module, ident, name, unres, ret); |
| 4677 | if (!rc) { |
| 4678 | assert(*ret); |
| 4679 | |
| 4680 | /* check status */ |
| 4681 | if (lyp_check_status(flags, mod, ident ? ident->name : "of type", |
| 4682 | (*ret)->flags, (*ret)->module, (*ret)->name, NULL)) { |
| 4683 | rc = -1; |
Pavol Vican | fdab9f9 | 2016-09-07 15:23:27 +0200 | [diff] [blame^] | 4684 | } else { |
| 4685 | if (ident) { |
| 4686 | ident->base[ident->base_size++] = *ret; |
| 4687 | |
| 4688 | /* maintain backlinks to the derived identities */ |
| 4689 | rc = identity_backlink_update(ident, *ret) ? -1 : EXIT_SUCCESS; |
| 4690 | } |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 4691 | } |
| 4692 | } else if (rc == EXIT_FAILURE) { |
| 4693 | LOGVAL(LYE_INRESOLV, LY_VLOG_NONE, NULL, parent, basename); |
Pavol Vican | 053a288 | 2016-09-05 14:40:33 +0200 | [diff] [blame] | 4694 | if (type) { |
| 4695 | --type->info.ident.count; |
| 4696 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4697 | } |
| 4698 | |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 4699 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4700 | } |
| 4701 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4702 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 4703 | * @brief Resolve JSON data format identityref. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4704 | * |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 4705 | * @param[in] type Identityref type. |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 4706 | * @param[in] ident_name Identityref name. |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 4707 | * @param[in] node Node where the identityref is being resolved |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4708 | * |
| 4709 | * @return Pointer to the identity resolvent, NULL on error. |
| 4710 | */ |
Radek Krejci | a52656e | 2015-08-05 13:41:50 +0200 | [diff] [blame] | 4711 | struct lys_ident * |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 4712 | resolve_identref(struct lys_type *type, const char *ident_name, struct lyd_node *node) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4713 | { |
Michal Vasko | c633ca0 | 2015-08-21 14:03:51 +0200 | [diff] [blame] | 4714 | const char *mod_name, *name; |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 4715 | int mod_name_len, rc, i, j; |
| 4716 | struct lys_ident *der, *cur; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4717 | |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 4718 | if (!type || (!type->info.ident.count && !type->der) || !ident_name) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4719 | return NULL; |
| 4720 | } |
| 4721 | |
Michal Vasko | c633ca0 | 2015-08-21 14:03:51 +0200 | [diff] [blame] | 4722 | 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] | 4723 | if (rc < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4724 | 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] | 4725 | return NULL; |
Michal Vasko | b43bb3c | 2016-03-17 15:00:27 +0100 | [diff] [blame] | 4726 | } else if (rc < (signed)strlen(ident_name)) { |
Radek Krejci | 02a0499 | 2016-03-17 16:06:37 +0100 | [diff] [blame] | 4727 | 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] | 4728 | return NULL; |
| 4729 | } |
| 4730 | |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 4731 | /* go through all the bases in all the derived types */ |
| 4732 | while (type->der) { |
| 4733 | for (i = 0; i < type->info.ident.count; ++i) { |
| 4734 | cur = type->info.ident.ref[i]; |
| 4735 | if (!strcmp(cur->name, name) && (!mod_name |
| 4736 | || (!strncmp(cur->module->name, mod_name, mod_name_len) && !cur->module->name[mod_name_len]))) { |
| 4737 | goto match; |
| 4738 | } |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 4739 | |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 4740 | for (j = 0; j < cur->der_size; j++) { |
| 4741 | der = cur->der[j]; /* shortcut */ |
| 4742 | if (!strcmp(der->name, name) && |
| 4743 | (!mod_name || (!strncmp(der->module->name, mod_name, mod_name_len) && !der->module->name[mod_name_len]))) { |
| 4744 | /* we have match */ |
| 4745 | cur = der; |
| 4746 | goto match; |
| 4747 | } |
| 4748 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4749 | } |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 4750 | type = &type->der->type; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4751 | } |
| 4752 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4753 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYD, node, "identityref", ident_name); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4754 | return NULL; |
Radek Krejci | f1ee2e2 | 2016-08-02 16:36:48 +0200 | [diff] [blame] | 4755 | |
| 4756 | match: |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 4757 | for (i = 0; i < cur->iffeature_size; i++) { |
| 4758 | if (!resolve_iffeature(&cur->iffeature[i])) { |
| 4759 | LOGVAL(LYE_INVAL, LY_VLOG_LYD, node, cur->name, node->schema->name); |
Radek Krejci | f1ee2e2 | 2016-08-02 16:36:48 +0200 | [diff] [blame] | 4760 | LOGVAL(LYE_SPEC, LY_VLOG_LYD, node, "Identity \"%s\" is disabled by its if-feature condition.", |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 4761 | cur->name); |
Radek Krejci | f1ee2e2 | 2016-08-02 16:36:48 +0200 | [diff] [blame] | 4762 | return NULL; |
| 4763 | } |
| 4764 | } |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 4765 | return cur; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4766 | } |
| 4767 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4768 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4769 | * @brief Resolve unresolved uses. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4770 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4771 | * @param[in] uses Uses to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4772 | * @param[in] unres Specific unres item. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4773 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4774 | * @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] | 4775 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4776 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4777 | 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] | 4778 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4779 | int rc; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 4780 | struct lys_node *par_grp; |
Michal Vasko | e91afce | 2015-08-12 12:21:00 +0200 | [diff] [blame] | 4781 | |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 4782 | /* HACK: when a grouping has uses inside, all such uses have to be resolved before the grouping itself |
| 4783 | * is used in some uses. When we see such a uses, the grouping's nacm member (not used in grouping) |
| 4784 | * is used to store number of so far unresolved uses. The grouping cannot be used unless the nacm |
| 4785 | * value is decreased back to 0. To remember that the uses already increased grouping's nacm, the |
| 4786 | * LYS_USESGRP flag is used. */ |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 4787 | 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] | 4788 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4789 | if (!uses->grp) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 4790 | rc = resolve_uses_schema_nodeid(uses->name, (const struct lys_node *)uses, (const struct lys_node_grp **)&uses->grp); |
| 4791 | if (rc == -1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4792 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, uses, "grouping", uses->name); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 4793 | return -1; |
| 4794 | } else if (rc > 0) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4795 | 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] | 4796 | return -1; |
| 4797 | } else if (!uses->grp) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 4798 | if (par_grp && !(uses->flags & LYS_USESGRP)) { |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 4799 | /* hack - in contrast to lys_node, lys_node_grp has bigger nacm field |
| 4800 | * (and smaller flags - it uses only a limited set of flags) |
| 4801 | */ |
| 4802 | ((struct lys_node_grp *)par_grp)->nacm++; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 4803 | uses->flags |= LYS_USESGRP; |
Michal Vasko | 407f1bb | 2015-09-23 15:51:07 +0200 | [diff] [blame] | 4804 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 4805 | return EXIT_FAILURE; |
Michal Vasko | 12e3084 | 2015-08-04 11:54:00 +0200 | [diff] [blame] | 4806 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4807 | } |
| 4808 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4809 | if (uses->grp->nacm) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 4810 | if (par_grp && !(uses->flags & LYS_USESGRP)) { |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 4811 | ((struct lys_node_grp *)par_grp)->nacm++; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 4812 | uses->flags |= LYS_USESGRP; |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 4813 | } else { |
| 4814 | /* instantiate grouping only when it is completely resolved */ |
| 4815 | uses->grp = NULL; |
Michal Vasko | 407f1bb | 2015-09-23 15:51:07 +0200 | [diff] [blame] | 4816 | } |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4817 | return EXIT_FAILURE; |
| 4818 | } |
| 4819 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4820 | rc = resolve_uses(uses, unres); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4821 | if (!rc) { |
| 4822 | /* decrease unres count only if not first try */ |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 4823 | if (par_grp && (uses->flags & LYS_USESGRP)) { |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 4824 | if (!((struct lys_node_grp *)par_grp)->nacm) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4825 | LOGINT; |
| 4826 | return -1; |
| 4827 | } |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 4828 | ((struct lys_node_grp *)par_grp)->nacm--; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 4829 | uses->flags &= ~LYS_USESGRP; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4830 | } |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 4831 | |
| 4832 | /* check status */ |
Radek Krejci | c655602 | 2016-01-27 15:16:45 +0100 | [diff] [blame] | 4833 | if (lyp_check_status(uses->flags, uses->module, "of uses", |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 4834 | uses->grp->flags, uses->grp->module, uses->grp->name, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4835 | (struct lys_node *)uses)) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 4836 | return -1; |
| 4837 | } |
| 4838 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4839 | return EXIT_SUCCESS; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 4840 | } else if ((rc == EXIT_FAILURE) && par_grp && !(uses->flags & LYS_USESGRP)) { |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 4841 | ((struct lys_node_grp *)par_grp)->nacm++; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 4842 | uses->flags |= LYS_USESGRP; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4843 | } |
| 4844 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4845 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4846 | } |
| 4847 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4848 | /** |
Michal Vasko | 9957e59 | 2015-08-17 15:04:09 +0200 | [diff] [blame] | 4849 | * @brief Resolve list keys. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4850 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4851 | * @param[in] list List to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4852 | * @param[in] keys_str Keys node value. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4853 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4854 | * @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] | 4855 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4856 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4857 | resolve_list_keys(struct lys_node_list *list, const char *keys_str) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4858 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4859 | int i, len, rc; |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 4860 | const char *value; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4861 | |
| 4862 | for (i = 0; i < list->keys_size; ++i) { |
| 4863 | /* get the key name */ |
| 4864 | if ((value = strpbrk(keys_str, " \t\n"))) { |
| 4865 | len = value - keys_str; |
| 4866 | while (isspace(value[0])) { |
| 4867 | value++; |
| 4868 | } |
| 4869 | } else { |
| 4870 | len = strlen(keys_str); |
| 4871 | } |
| 4872 | |
Radek Krejci | c428344 | 2016-04-22 09:19:27 +0200 | [diff] [blame] | 4873 | rc = lys_get_sibling(list->child, lys_main_module(list->module)->name, 0, keys_str, len, LYS_LEAF, (const struct lys_node **)&list->keys[i]); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4874 | if (rc) { |
Michal Vasko | 7a55bea | 2016-05-02 14:51:20 +0200 | [diff] [blame] | 4875 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, list, "list keys", keys_str); |
| 4876 | return EXIT_FAILURE; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4877 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4878 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4879 | if (check_key(list, i, keys_str, len)) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4880 | /* check_key logs */ |
| 4881 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4882 | } |
| 4883 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 4884 | /* check status */ |
Radek Krejci | c655602 | 2016-01-27 15:16:45 +0100 | [diff] [blame] | 4885 | if (lyp_check_status(list->flags, list->module, list->name, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4886 | list->keys[i]->flags, list->keys[i]->module, list->keys[i]->name, |
| 4887 | (struct lys_node *)list->keys[i])) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 4888 | return -1; |
| 4889 | } |
| 4890 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4891 | /* prepare for next iteration */ |
| 4892 | while (value && isspace(value[0])) { |
| 4893 | value++; |
| 4894 | } |
| 4895 | keys_str = value; |
| 4896 | } |
| 4897 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 4898 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4899 | } |
| 4900 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4901 | /** |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 4902 | * @brief Resolve (check) all must conditions of \p node. |
| 4903 | * Logs directly. |
| 4904 | * |
| 4905 | * @param[in] node Data node with optional must statements. |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 4906 | * |
| 4907 | * @return EXIT_SUCCESS on pass, EXIT_FAILURE on fail, -1 on error. |
| 4908 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4909 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4910 | resolve_must(struct lyd_node *node) |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 4911 | { |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 4912 | uint8_t i, must_size; |
| 4913 | struct lys_restr *must; |
| 4914 | struct lyxp_set set; |
| 4915 | |
| 4916 | assert(node); |
| 4917 | memset(&set, 0, sizeof set); |
| 4918 | |
| 4919 | switch (node->schema->nodetype) { |
| 4920 | case LYS_CONTAINER: |
| 4921 | must_size = ((struct lys_node_container *)node->schema)->must_size; |
| 4922 | must = ((struct lys_node_container *)node->schema)->must; |
| 4923 | break; |
| 4924 | case LYS_LEAF: |
| 4925 | must_size = ((struct lys_node_leaf *)node->schema)->must_size; |
| 4926 | must = ((struct lys_node_leaf *)node->schema)->must; |
| 4927 | break; |
| 4928 | case LYS_LEAFLIST: |
| 4929 | must_size = ((struct lys_node_leaflist *)node->schema)->must_size; |
| 4930 | must = ((struct lys_node_leaflist *)node->schema)->must; |
| 4931 | break; |
| 4932 | case LYS_LIST: |
| 4933 | must_size = ((struct lys_node_list *)node->schema)->must_size; |
| 4934 | must = ((struct lys_node_list *)node->schema)->must; |
| 4935 | break; |
| 4936 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4937 | case LYS_ANYDATA: |
| 4938 | must_size = ((struct lys_node_anydata *)node->schema)->must_size; |
| 4939 | must = ((struct lys_node_anydata *)node->schema)->must; |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 4940 | break; |
| 4941 | default: |
| 4942 | must_size = 0; |
| 4943 | break; |
| 4944 | } |
| 4945 | |
| 4946 | for (i = 0; i < must_size; ++i) { |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 4947 | if (lyxp_eval(must[i].expr, node, LYXP_NODE_ELEM, &set, LYXP_MUST)) { |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 4948 | return -1; |
| 4949 | } |
| 4950 | |
Michal Vasko | 944a564 | 2016-03-21 11:48:58 +0100 | [diff] [blame] | 4951 | lyxp_set_cast(&set, LYXP_SET_BOOLEAN, node, LYXP_MUST); |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 4952 | |
Michal Vasko | 8146d4c | 2016-05-09 15:50:29 +0200 | [diff] [blame] | 4953 | if (!set.val.bool) { |
Michal Vasko | 6ac6828 | 2016-04-11 10:56:47 +0200 | [diff] [blame] | 4954 | LOGVAL(LYE_NOMUST, LY_VLOG_LYD, node, must[i].expr); |
| 4955 | if (must[i].emsg) { |
| 4956 | LOGVAL(LYE_SPEC, LY_VLOG_LYD, node, must[i].emsg); |
| 4957 | } |
| 4958 | if (must[i].eapptag) { |
| 4959 | strncpy(((struct ly_err *)&ly_errno)->apptag, must[i].eapptag, LY_APPTAG_LEN - 1); |
| 4960 | } |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 4961 | return 1; |
| 4962 | } |
| 4963 | } |
| 4964 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4965 | return EXIT_SUCCESS; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 4966 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4967 | |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 4968 | /** |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 4969 | * @brief Resolve (find) when condition schema context node. Does not log. |
| 4970 | * |
| 4971 | * @param[in] schema Schema node with the when condition. |
| 4972 | * @param[out] ctx_snode When schema context node. |
| 4973 | * @param[out] ctx_snode_type Schema context node type. |
| 4974 | */ |
| 4975 | void |
| 4976 | resolve_when_ctx_snode(const struct lys_node *schema, struct lys_node **ctx_snode, enum lyxp_node_type *ctx_snode_type) |
| 4977 | { |
| 4978 | const struct lys_node *sparent; |
| 4979 | |
| 4980 | /* find a not schema-only node */ |
| 4981 | *ctx_snode_type = LYXP_NODE_ELEM; |
| 4982 | while (schema->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE | LYS_AUGMENT | LYS_INPUT | LYS_OUTPUT)) { |
| 4983 | if (schema->nodetype == LYS_AUGMENT) { |
| 4984 | sparent = ((struct lys_node_augment *)schema)->target; |
| 4985 | } else { |
| 4986 | sparent = schema->parent; |
| 4987 | } |
| 4988 | if (!sparent) { |
| 4989 | /* context node is the document root (fake root in our case) */ |
| 4990 | if (schema->flags & LYS_CONFIG_W) { |
| 4991 | *ctx_snode_type = LYXP_NODE_ROOT_CONFIG; |
| 4992 | } else { |
| 4993 | *ctx_snode_type = LYXP_NODE_ROOT_STATE; |
| 4994 | } |
| 4995 | /* move the fake root to the beginning of the list, if any */ |
| 4996 | while (schema->prev->next) { |
| 4997 | schema = schema->prev; |
| 4998 | } |
| 4999 | break; |
| 5000 | } |
| 5001 | schema = sparent; |
| 5002 | } |
| 5003 | |
| 5004 | *ctx_snode = (struct lys_node *)schema; |
| 5005 | } |
| 5006 | |
| 5007 | /** |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5008 | * @brief Resolve (find) when condition context node. Does not log. |
| 5009 | * |
| 5010 | * @param[in] node Data node, whose conditional definition is being decided. |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5011 | * @param[in] schema Schema node with the when condition. |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5012 | * @param[out] ctx_node Context node. |
| 5013 | * @param[out] ctx_node_type Context node type. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5014 | * |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5015 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5016 | */ |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5017 | static int |
| 5018 | resolve_when_ctx_node(struct lyd_node *node, struct lys_node *schema, struct lyd_node **ctx_node, |
| 5019 | enum lyxp_node_type *ctx_node_type) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5020 | { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5021 | struct lyd_node *parent; |
| 5022 | struct lys_node *sparent; |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5023 | enum lyxp_node_type node_type; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5024 | uint16_t i, data_depth, schema_depth; |
| 5025 | |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5026 | resolve_when_ctx_snode(schema, &schema, &node_type); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5027 | |
| 5028 | /* get node depths */ |
| 5029 | for (parent = node, data_depth = 0; parent; parent = parent->parent, ++data_depth); |
Michal Vasko | e365556 | 2016-08-24 15:56:17 +0200 | [diff] [blame] | 5030 | for (sparent = schema, schema_depth = 0; |
| 5031 | sparent; |
| 5032 | sparent = (sparent->nodetype == LYS_AUGMENT ? ((struct lys_node_augment *)sparent)->target : sparent->parent)) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 5033 | if (sparent->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_NOTIF | LYS_RPC)) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5034 | ++schema_depth; |
| 5035 | } |
| 5036 | } |
| 5037 | if (data_depth < schema_depth) { |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5038 | return -1; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5039 | } |
| 5040 | |
Michal Vasko | 956e854 | 2016-08-26 09:43:35 +0200 | [diff] [blame] | 5041 | if (schema_depth) { |
| 5042 | /* find the corresponding data node */ |
| 5043 | for (i = 0; i < data_depth - schema_depth; ++i) { |
| 5044 | node = node->parent; |
| 5045 | } |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5046 | if (node->schema != schema) { |
| 5047 | return -1; |
| 5048 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5049 | } |
| 5050 | |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5051 | *ctx_node = node; |
| 5052 | *ctx_node_type = node_type; |
| 5053 | return EXIT_SUCCESS; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5054 | } |
| 5055 | |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5056 | /** |
| 5057 | * @brief Temporarily unlink nodes as per YANG 1.1 RFC section 7.21.5 for when XPath evaluation. |
| 5058 | * The context nodes is adjusted if needed. |
| 5059 | * |
| 5060 | * @param[in] snode Schema node, whose children instances need to be unlinked. |
| 5061 | * @param[in,out] node Data siblings where to look for the children of \p snode. If it is unlinked, |
| 5062 | * it is moved to point to another sibling still in the original tree. |
| 5063 | * @param[in,out] ctx_node When context node, adjusted if needed. |
| 5064 | * @param[in] ctx_node_type Context node type, just for information to detect invalid situations. |
| 5065 | * @param[out] unlinked_nodes Unlinked siblings. Can be safely appended to \p node afterwards. |
| 5066 | * Ordering may change, but there will be no semantic change. |
| 5067 | * |
| 5068 | * @return EXIT_SUCCESS on success, -1 on error. |
| 5069 | */ |
| 5070 | static int |
| 5071 | resolve_when_unlink_nodes(struct lys_node *snode, struct lyd_node **node, struct lyd_node **ctx_node, |
| 5072 | enum lyxp_node_type ctx_node_type, struct lyd_node **unlinked_nodes) |
| 5073 | { |
| 5074 | struct lyd_node *next, *elem; |
| 5075 | |
| 5076 | switch (snode->nodetype) { |
| 5077 | case LYS_AUGMENT: |
| 5078 | case LYS_USES: |
| 5079 | case LYS_CHOICE: |
| 5080 | case LYS_CASE: |
| 5081 | LY_TREE_FOR(snode->child, snode) { |
| 5082 | if (resolve_when_unlink_nodes(snode, node, ctx_node, ctx_node_type, unlinked_nodes)) { |
| 5083 | return -1; |
| 5084 | } |
| 5085 | } |
| 5086 | break; |
| 5087 | case LYS_CONTAINER: |
| 5088 | case LYS_LIST: |
| 5089 | case LYS_LEAF: |
| 5090 | case LYS_LEAFLIST: |
| 5091 | case LYS_ANYXML: |
| 5092 | case LYS_ANYDATA: |
| 5093 | LY_TREE_FOR_SAFE(lyd_first_sibling(*node), next, elem) { |
| 5094 | if (elem->schema == snode) { |
| 5095 | |
| 5096 | if (elem == *ctx_node) { |
| 5097 | /* We are going to unlink our context node! This normally cannot happen, |
| 5098 | * but we use normal top-level data nodes for faking a document root node, |
| 5099 | * so if this is the context node, we just use the next top-level node. |
| 5100 | * Additionally, it can even happen that there are no top-level data nodes left, |
| 5101 | * all were unlinked, so in this case we pass NULL as the context node/data tree, |
| 5102 | * lyxp_eval() can handle this special situation. |
| 5103 | */ |
| 5104 | if (ctx_node_type == LYXP_NODE_ELEM) { |
| 5105 | LOGINT; |
| 5106 | return -1; |
| 5107 | } |
| 5108 | |
| 5109 | if (elem->prev == elem) { |
| 5110 | /* unlinking last top-level element, use an empty data tree */ |
| 5111 | *ctx_node = NULL; |
| 5112 | } else { |
| 5113 | /* in this case just use the previous/last top-level data node */ |
| 5114 | *ctx_node = elem->prev; |
| 5115 | } |
| 5116 | } else if (elem == *node) { |
| 5117 | /* We are going to unlink the currently processed node. This does not matter that |
| 5118 | * much, but we would lose access to the original data tree, so just move our |
| 5119 | * pointer somewhere still inside it. |
| 5120 | */ |
| 5121 | if ((*node)->prev != *node) { |
| 5122 | *node = (*node)->prev; |
| 5123 | } else { |
| 5124 | /* the processed node with sibings were all unlinked, oh well */ |
| 5125 | *node = NULL; |
| 5126 | } |
| 5127 | } |
| 5128 | |
| 5129 | /* temporarily unlink the node */ |
| 5130 | lyd_unlink(elem); |
| 5131 | if (*unlinked_nodes) { |
| 5132 | if (lyd_insert_after(*unlinked_nodes, elem)) { |
| 5133 | LOGINT; |
| 5134 | return -1; |
| 5135 | } |
| 5136 | } else { |
| 5137 | *unlinked_nodes = elem; |
| 5138 | } |
| 5139 | |
| 5140 | if (snode->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_ANYDATA)) { |
| 5141 | /* there can be only one instance */ |
| 5142 | break; |
| 5143 | } |
| 5144 | } |
| 5145 | } |
| 5146 | break; |
| 5147 | default: |
| 5148 | LOGINT; |
| 5149 | return -1; |
| 5150 | } |
| 5151 | |
| 5152 | return EXIT_SUCCESS; |
| 5153 | } |
| 5154 | |
| 5155 | /** |
| 5156 | * @brief Relink the unlinked nodes back. |
| 5157 | * |
| 5158 | * @param[in] node Data node to link the nodes back to. It can actually be the adjusted context node, |
| 5159 | * we simply need a sibling from the original data tree. |
| 5160 | * @param[in] unlinked_nodes Unlinked nodes to relink to \p node. |
| 5161 | * @param[in] ctx_node_type Context node type to distinguish between \p node being the parent |
| 5162 | * or the sibling of \p unlinked_nodes. |
| 5163 | * |
| 5164 | * @return EXIT_SUCCESS on success, -1 on error. |
| 5165 | */ |
| 5166 | static int |
| 5167 | resolve_when_relink_nodes(struct lyd_node *node, struct lyd_node *unlinked_nodes, enum lyxp_node_type ctx_node_type) |
| 5168 | { |
| 5169 | struct lyd_node *elem; |
| 5170 | |
| 5171 | LY_TREE_FOR_SAFE(unlinked_nodes, unlinked_nodes, elem) { |
| 5172 | if (ctx_node_type == LYXP_NODE_ELEM) { |
| 5173 | if (lyd_insert(node, elem)) { |
| 5174 | return -1; |
| 5175 | } |
| 5176 | } else { |
| 5177 | if (lyd_insert_after(node, elem)) { |
| 5178 | return -1; |
| 5179 | } |
| 5180 | } |
| 5181 | } |
| 5182 | |
| 5183 | return EXIT_SUCCESS; |
| 5184 | } |
| 5185 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5186 | int |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5187 | resolve_applies_must(const struct lys_node *schema) |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5188 | { |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5189 | assert(schema); |
| 5190 | |
| 5191 | switch (schema->nodetype) { |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5192 | case LYS_CONTAINER: |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5193 | return ((struct lys_node_container *)schema)->must_size; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5194 | case LYS_LEAF: |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5195 | return ((struct lys_node_leaf *)schema)->must_size; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5196 | case LYS_LEAFLIST: |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5197 | return ((struct lys_node_leaflist *)schema)->must_size; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5198 | case LYS_LIST: |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5199 | return ((struct lys_node_list *)schema)->must_size; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5200 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 5201 | case LYS_ANYDATA: |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5202 | return ((struct lys_node_anydata *)schema)->must_size; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5203 | default: |
| 5204 | return 0; |
| 5205 | } |
| 5206 | } |
| 5207 | |
| 5208 | int |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5209 | 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] | 5210 | { |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5211 | const struct lys_node *parent; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5212 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5213 | assert(schema); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5214 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5215 | 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] | 5216 | return 1; |
| 5217 | } |
| 5218 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5219 | parent = schema; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5220 | goto check_augment; |
| 5221 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5222 | while (parent) { |
| 5223 | /* stop conditions */ |
| 5224 | if (!mode) { |
| 5225 | /* stop on node that can be instantiated in data tree */ |
| 5226 | if (!(parent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) { |
| 5227 | break; |
| 5228 | } |
| 5229 | } else { |
| 5230 | /* stop on the specified node */ |
| 5231 | if (parent == stop) { |
| 5232 | break; |
| 5233 | } |
| 5234 | } |
| 5235 | |
| 5236 | if (((const struct lys_node_uses *)parent)->when) { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5237 | return 1; |
| 5238 | } |
| 5239 | check_augment: |
| 5240 | |
| 5241 | if ((parent->parent && (parent->parent->nodetype == LYS_AUGMENT) && |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5242 | (((const struct lys_node_augment *)parent->parent)->when))) { |
Michal Vasko | e365556 | 2016-08-24 15:56:17 +0200 | [diff] [blame] | 5243 | return 1; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5244 | } |
| 5245 | parent = lys_parent(parent); |
| 5246 | } |
| 5247 | |
| 5248 | return 0; |
| 5249 | } |
| 5250 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5251 | /** |
| 5252 | * @brief Resolve (check) all when conditions relevant for \p node. |
| 5253 | * Logs directly. |
| 5254 | * |
| 5255 | * @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] | 5256 | * |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5257 | * @return |
| 5258 | * -1 - error, ly_errno is set |
| 5259 | * 0 - true "when" statement |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5260 | * 0, ly_vecode = LYVE_NOWHEN - false "when" statement |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5261 | * 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] | 5262 | */ |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5263 | int |
| 5264 | resolve_when(struct lyd_node *node, int *result) |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5265 | { |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5266 | struct lyd_node *ctx_node = NULL, *unlinked_nodes, *tmp_node; |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5267 | struct lys_node *sparent; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5268 | struct lyxp_set set; |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5269 | enum lyxp_node_type ctx_node_type; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5270 | int rc = 0; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5271 | |
| 5272 | assert(node); |
| 5273 | memset(&set, 0, sizeof set); |
| 5274 | |
| 5275 | if (!(node->schema->nodetype & (LYS_NOTIF | LYS_RPC)) && (((struct lys_node_container *)node->schema)->when)) { |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5276 | /* make the node dummy for the evaluation */ |
| 5277 | node->validity |= LYD_VAL_INUSE; |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5278 | rc = lyxp_eval(((struct lys_node_container *)node->schema)->when->cond, node, LYXP_NODE_ELEM, &set, LYXP_WHEN); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5279 | node->validity &= ~LYD_VAL_INUSE; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5280 | if (rc) { |
| 5281 | if (rc == 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5282 | 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] | 5283 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5284 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5285 | } |
| 5286 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5287 | /* set boolean result of the condition */ |
Michal Vasko | 944a564 | 2016-03-21 11:48:58 +0100 | [diff] [blame] | 5288 | lyxp_set_cast(&set, LYXP_SET_BOOLEAN, node, LYXP_WHEN); |
Michal Vasko | 8146d4c | 2016-05-09 15:50:29 +0200 | [diff] [blame] | 5289 | if (!set.val.bool) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5290 | LOGVAL(LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_container *)node->schema)->when->cond); |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5291 | node->when_status |= LYD_WHEN_FALSE; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5292 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5293 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5294 | |
| 5295 | /* free xpath set content */ |
| 5296 | lyxp_set_cast(&set, LYXP_SET_EMPTY, node, 0); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5297 | } |
| 5298 | |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5299 | sparent = node->schema; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5300 | goto check_augment; |
| 5301 | |
| 5302 | /* check when in every schema node that affects node */ |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5303 | while (sparent && (sparent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) { |
| 5304 | if (((struct lys_node_uses *)sparent)->when) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5305 | if (!ctx_node) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5306 | rc = resolve_when_ctx_node(node, sparent, &ctx_node, &ctx_node_type); |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5307 | if (rc) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5308 | LOGINT; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5309 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5310 | } |
| 5311 | } |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5312 | |
| 5313 | unlinked_nodes = NULL; |
| 5314 | /* we do not want our node pointer to change */ |
| 5315 | tmp_node = node; |
| 5316 | rc = resolve_when_unlink_nodes(sparent, &tmp_node, &ctx_node, ctx_node_type, &unlinked_nodes); |
| 5317 | if (rc) { |
| 5318 | goto cleanup; |
| 5319 | } |
| 5320 | |
| 5321 | rc = lyxp_eval(((struct lys_node_uses *)sparent)->when->cond, ctx_node, ctx_node_type, &set, LYXP_WHEN); |
| 5322 | |
| 5323 | if (unlinked_nodes && ctx_node) { |
| 5324 | if (resolve_when_relink_nodes(ctx_node, unlinked_nodes, ctx_node_type)) { |
| 5325 | rc = -1; |
| 5326 | goto cleanup; |
| 5327 | } |
| 5328 | } |
| 5329 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5330 | if (rc) { |
| 5331 | if (rc == 1) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5332 | 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] | 5333 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5334 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5335 | } |
| 5336 | |
Michal Vasko | 944a564 | 2016-03-21 11:48:58 +0100 | [diff] [blame] | 5337 | lyxp_set_cast(&set, LYXP_SET_BOOLEAN, ctx_node, LYXP_WHEN); |
Michal Vasko | 8146d4c | 2016-05-09 15:50:29 +0200 | [diff] [blame] | 5338 | if (!set.val.bool) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5339 | LOGVAL(LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_uses *)sparent)->when->cond); |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5340 | node->when_status |= LYD_WHEN_FALSE; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5341 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5342 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5343 | |
| 5344 | /* free xpath set content */ |
| 5345 | lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node, 0); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5346 | } |
| 5347 | |
| 5348 | check_augment: |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5349 | 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] | 5350 | if (!ctx_node) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5351 | 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] | 5352 | if (rc) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5353 | LOGINT; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5354 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5355 | } |
| 5356 | } |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5357 | |
| 5358 | unlinked_nodes = NULL; |
| 5359 | tmp_node = node; |
| 5360 | rc = resolve_when_unlink_nodes(sparent->parent, &tmp_node, &ctx_node, ctx_node_type, &unlinked_nodes); |
| 5361 | if (rc) { |
| 5362 | goto cleanup; |
| 5363 | } |
| 5364 | |
| 5365 | rc = lyxp_eval(((struct lys_node_augment *)sparent->parent)->when->cond, ctx_node, ctx_node_type, &set, LYXP_WHEN); |
| 5366 | |
| 5367 | /* reconnect nodes, if ctx_node is NULL then all the nodes were unlinked, but linked together, |
| 5368 | * so the tree did not actually change and there is nothing for us to do |
| 5369 | */ |
| 5370 | if (unlinked_nodes && ctx_node) { |
| 5371 | if (resolve_when_relink_nodes(ctx_node, unlinked_nodes, ctx_node_type)) { |
| 5372 | rc = -1; |
| 5373 | goto cleanup; |
| 5374 | } |
| 5375 | } |
| 5376 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5377 | if (rc) { |
| 5378 | if (rc == 1) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5379 | 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] | 5380 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5381 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5382 | } |
| 5383 | |
Michal Vasko | 944a564 | 2016-03-21 11:48:58 +0100 | [diff] [blame] | 5384 | lyxp_set_cast(&set, LYXP_SET_BOOLEAN, ctx_node, LYXP_WHEN); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5385 | |
Michal Vasko | 8146d4c | 2016-05-09 15:50:29 +0200 | [diff] [blame] | 5386 | if (!set.val.bool) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5387 | LOGVAL(LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_augment *)sparent->parent)->when->cond); |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5388 | node->when_status |= LYD_WHEN_FALSE; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5389 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5390 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5391 | |
| 5392 | /* free xpath set content */ |
| 5393 | lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node, 0); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5394 | } |
| 5395 | |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5396 | sparent = lys_parent(sparent); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5397 | } |
| 5398 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5399 | node->when_status |= LYD_WHEN_TRUE; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5400 | |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5401 | cleanup: |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5402 | /* free xpath set content */ |
| 5403 | lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node ? ctx_node : node, 0); |
| 5404 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5405 | if (result) { |
| 5406 | if (node->when_status & LYD_WHEN_TRUE) { |
| 5407 | *result = 1; |
| 5408 | } else { |
| 5409 | *result = 0; |
| 5410 | } |
| 5411 | } |
| 5412 | |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5413 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5414 | } |
| 5415 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5416 | /** |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5417 | * @brief Check all XPath expressions of a node (when and must), set LYS_XPATH_DEP flag if required. |
| 5418 | * |
| 5419 | * @param[in] node Node to examine. |
| 5420 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 5421 | */ |
| 5422 | static int |
| 5423 | check_xpath(struct lys_node *node) |
| 5424 | { |
| 5425 | struct lys_node *parent, *elem; |
| 5426 | struct lyxp_set set; |
| 5427 | uint32_t i; |
| 5428 | int rc; |
| 5429 | |
| 5430 | parent = node; |
| 5431 | while (parent) { |
| 5432 | if (parent->nodetype == LYS_GROUPING) { |
| 5433 | /* unresolved grouping, skip for now */ |
| 5434 | return 0; |
| 5435 | } |
| 5436 | if (parent->nodetype == LYS_AUGMENT) { |
| 5437 | if (!((struct lys_node_augment *)parent)->target) { |
| 5438 | /* uresolved augment skip for now */ |
| 5439 | return 0; |
| 5440 | } else { |
| 5441 | parent = ((struct lys_node_augment *)parent)->target; |
| 5442 | continue; |
| 5443 | } |
| 5444 | } |
| 5445 | parent = parent->parent; |
| 5446 | } |
| 5447 | |
| 5448 | rc = lyxp_node_atomize(node, &set); |
| 5449 | if (rc) { |
| 5450 | return rc; |
| 5451 | } |
| 5452 | |
| 5453 | /* RPC, action can have neither must nor when */ |
| 5454 | for (parent = node; parent && !(parent->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT)); parent = lys_parent(parent)); |
| 5455 | |
| 5456 | if (parent) { |
| 5457 | for (i = 0; i < set.used; ++i) { |
| 5458 | for (elem = set.val.snodes[i].snode; elem && (elem != parent); elem = lys_parent(elem)); |
| 5459 | if (!elem) { |
| 5460 | /* not in node's input, output, or, notification subtree, set the flag */ |
| 5461 | node->flags |= LYS_XPATH_DEP; |
| 5462 | break; |
| 5463 | } |
| 5464 | } |
| 5465 | } |
| 5466 | |
| 5467 | free(set.val.snodes); |
| 5468 | return EXIT_SUCCESS; |
| 5469 | } |
| 5470 | |
| 5471 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5472 | * @brief Resolve a single unres schema item. Logs indirectly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5473 | * |
| 5474 | * @param[in] mod Main module. |
| 5475 | * @param[in] item Item to resolve. Type determined by \p type. |
| 5476 | * @param[in] type Type of the unresolved item. |
| 5477 | * @param[in] str_snode String, a schema node, or NULL. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5478 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5479 | * |
| 5480 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 5481 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5482 | static int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 5483 | resolve_unres_schema_item(struct lys_module *mod, void *item, enum UNRES_ITEM type, void *str_snode, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5484 | struct unres_schema *unres) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5485 | { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 5486 | /* has_str - whether the str_snode is a string in a dictionary that needs to be freed */ |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 5487 | int rc = -1, has_str = 0, tpdf_flag = 0, i, k; |
| 5488 | unsigned int j; |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 5489 | struct lys_node *node, *par_grp; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 5490 | const char *expr; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 5491 | |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 5492 | struct ly_set *refs, *procs; |
| 5493 | struct lys_feature *ref, *feat; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 5494 | struct lys_ident *ident; |
| 5495 | struct lys_type *stype; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 5496 | struct lys_node_choice *choic; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 5497 | struct lyxml_elem *yin; |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 5498 | struct yang_type *yang; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 5499 | struct unres_list_uniq *unique_info; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5500 | |
| 5501 | switch (type) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5502 | case UNRES_IDENT: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 5503 | expr = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 5504 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 5505 | ident = item; |
| 5506 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5507 | rc = resolve_base_ident(mod, ident, expr, "identity", NULL, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5508 | break; |
| 5509 | case UNRES_TYPE_IDENTREF: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 5510 | expr = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 5511 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 5512 | stype = item; |
| 5513 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5514 | rc = resolve_base_ident(mod, NULL, expr, "type", stype, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5515 | break; |
| 5516 | case UNRES_TYPE_LEAFREF: |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 5517 | node = str_snode; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 5518 | stype = item; |
| 5519 | |
Radek Krejci | 2f12f85 | 2016-01-08 12:59:57 +0100 | [diff] [blame] | 5520 | /* HACK - when there is no parent, we are in top level typedef and in that |
| 5521 | * case, the path has to contain absolute path, so we let the resolve_path_arg_schema() |
| 5522 | * know it via tpdf_flag */ |
| 5523 | if (!node) { |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 5524 | tpdf_flag = 1; |
Radek Krejci | 2f12f85 | 2016-01-08 12:59:57 +0100 | [diff] [blame] | 5525 | node = (struct lys_node *)stype->parent; |
| 5526 | } |
| 5527 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5528 | rc = resolve_path_arg_schema(stype->info.lref.path, node, tpdf_flag, |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 5529 | (const struct lys_node **)&stype->info.lref.target); |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 5530 | if (!tpdf_flag && !rc) { |
| 5531 | assert(stype->info.lref.target); |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 5532 | /* store the backlink from leafref target */ |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 5533 | if (lys_leaf_add_leafref_target(stype->info.lref.target, (struct lys_node *)stype->parent)) { |
| 5534 | rc = -1; |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 5535 | } |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 5536 | } |
| 5537 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5538 | break; |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 5539 | case UNRES_TYPE_DER_TPDF: |
| 5540 | tpdf_flag = 1; |
| 5541 | /* no break */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5542 | case UNRES_TYPE_DER: |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 5543 | /* parent */ |
| 5544 | node = str_snode; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 5545 | stype = item; |
| 5546 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 5547 | /* HACK type->der is temporarily unparsed type statement */ |
| 5548 | yin = (struct lyxml_elem *)stype->der; |
| 5549 | stype->der = NULL; |
| 5550 | |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 5551 | if (yin->flags & LY_YANG_STRUCTURE_FLAG) { |
| 5552 | yang = (struct yang_type *)yin; |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 5553 | rc = yang_check_type(mod, node, yang, tpdf_flag, unres); |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 5554 | |
| 5555 | if (rc) { |
Pavol Vican | 8bd72e4 | 2016-08-29 09:53:05 +0200 | [diff] [blame] | 5556 | /* may try again later */ |
| 5557 | stype->der = (struct lys_tpdf *)yang; |
Pavol Vican | d01d8ae | 2016-03-01 10:45:59 +0100 | [diff] [blame] | 5558 | } else { |
| 5559 | /* 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] | 5560 | lydict_remove(mod->ctx, yang->name); |
Pavol Vican | d01d8ae | 2016-03-01 10:45:59 +0100 | [diff] [blame] | 5561 | free(yang); |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 5562 | } |
| 5563 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 5564 | } else { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 5565 | rc = fill_yin_type(mod, node, yin, stype, tpdf_flag, unres); |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 5566 | if (!rc) { |
| 5567 | /* we need to always be able to free this, it's safe only in this case */ |
| 5568 | lyxml_free(mod->ctx, yin); |
| 5569 | } else { |
| 5570 | /* may try again later, put all back how it was */ |
| 5571 | stype->der = (struct lys_tpdf *)yin; |
| 5572 | } |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 5573 | } |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 5574 | if (rc == EXIT_SUCCESS) { |
Radek Krejci | 2c2fce8 | 2016-08-01 13:52:26 +0200 | [diff] [blame] | 5575 | /* it does not make sense to have leaf-list of empty type */ |
| 5576 | if (!tpdf_flag && node->nodetype == LYS_LEAFLIST && stype->base == LY_TYPE_EMPTY) { |
| 5577 | LOGWRN("The leaf-list \"%s\" is of \"empty\" type, which does not make sense.", node->name); |
| 5578 | } |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 5579 | } else if (rc == EXIT_FAILURE && stype->base != LY_TYPE_INGRP) { |
| 5580 | /* forward reference - in case the type is in grouping, we have to make the grouping unusable |
| 5581 | * by uses statement until the type is resolved. We do that the same way as uses statements inside |
| 5582 | * grouping - the grouping's nacm member (not used un grouping) is used to increase the number of |
| 5583 | * so far unresolved items (uses and types). The grouping cannot be used unless the nacm value is 0. |
| 5584 | * To remember that the grouping already increased grouping's nacm, the LY_TYPE_INGRP is used as value |
| 5585 | * of the type's base member. */ |
| 5586 | for (par_grp = node; par_grp && (par_grp->nodetype != LYS_GROUPING); par_grp = lys_parent(par_grp)); |
| 5587 | if (par_grp) { |
| 5588 | ((struct lys_node_grp *)par_grp)->nacm++; |
| 5589 | stype->base = LY_TYPE_INGRP; |
| 5590 | } |
Radek Krejci | 2c2fce8 | 2016-08-01 13:52:26 +0200 | [diff] [blame] | 5591 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5592 | break; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5593 | case UNRES_IFFEAT: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 5594 | node = str_snode; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 5595 | expr = *((const char **)item); |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 5596 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 5597 | rc = resolve_feature(expr, strlen(expr), node, item); |
| 5598 | if (!rc) { |
| 5599 | /* success */ |
| 5600 | free((char *)expr); |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 5601 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5602 | break; |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 5603 | case UNRES_FEATURE: |
| 5604 | feat = (struct lys_feature *)item; |
| 5605 | |
| 5606 | if (feat->iffeature_size) { |
| 5607 | refs = ly_set_new(); |
| 5608 | procs = ly_set_new(); |
| 5609 | ly_set_add(procs, feat, 0); |
| 5610 | |
| 5611 | while (procs->number) { |
| 5612 | ref = procs->set.g[procs->number - 1]; |
| 5613 | ly_set_rm_index(procs, procs->number - 1); |
| 5614 | |
| 5615 | for (i = 0; i < ref->iffeature_size; i++) { |
| 5616 | resolve_iffeature_getsizes(&ref->iffeature[i], NULL, &j); |
| 5617 | for (; j > 0 ; j--) { |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 5618 | if (unres_schema_find(unres, -1, &ref->iffeature[i].features[j - 1], UNRES_IFFEAT) == -1) { |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 5619 | if (ref->iffeature[i].features[j - 1] == feat) { |
| 5620 | LOGVAL(LYE_CIRC_FEATURES, LY_VLOG_NONE, NULL, feat->name); |
| 5621 | goto featurecheckdone; |
| 5622 | } |
| 5623 | |
| 5624 | if (ref->iffeature[i].features[j - 1]->iffeature_size) { |
| 5625 | k = refs->number; |
| 5626 | if (ly_set_add(refs, ref->iffeature[i].features[j - 1], 0) == k) { |
| 5627 | /* not yet seen feature, add it for processing */ |
| 5628 | ly_set_add(procs, ref->iffeature[i].features[j - 1], 0); |
| 5629 | } |
| 5630 | } |
| 5631 | } else { |
| 5632 | /* forward reference */ |
| 5633 | rc = EXIT_FAILURE; |
| 5634 | goto featurecheckdone; |
| 5635 | } |
| 5636 | } |
| 5637 | |
| 5638 | } |
| 5639 | } |
| 5640 | rc = EXIT_SUCCESS; |
| 5641 | |
| 5642 | featurecheckdone: |
| 5643 | ly_set_free(refs); |
| 5644 | ly_set_free(procs); |
| 5645 | } |
| 5646 | |
| 5647 | break; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5648 | case UNRES_USES: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5649 | rc = resolve_unres_schema_uses(item, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5650 | break; |
| 5651 | case UNRES_TYPE_DFLT: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 5652 | expr = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 5653 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 5654 | stype = item; |
| 5655 | |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 5656 | rc = check_default(stype, expr, mod); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5657 | break; |
| 5658 | case UNRES_CHOICE_DFLT: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 5659 | expr = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 5660 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 5661 | choic = item; |
| 5662 | |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 5663 | if (!choic->dflt) { |
| 5664 | choic->dflt = resolve_choice_dflt(choic, expr); |
| 5665 | } |
Michal Vasko | 7955b36 | 2015-09-04 14:18:15 +0200 | [diff] [blame] | 5666 | if (choic->dflt) { |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 5667 | rc = lyp_check_mandatory_choice((struct lys_node *)choic); |
Michal Vasko | 7955b36 | 2015-09-04 14:18:15 +0200 | [diff] [blame] | 5668 | } else { |
| 5669 | rc = EXIT_FAILURE; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 5670 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5671 | break; |
| 5672 | case UNRES_LIST_KEYS: |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 5673 | has_str = 1; |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5674 | rc = resolve_list_keys(item, str_snode); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5675 | break; |
| 5676 | case UNRES_LIST_UNIQ: |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 5677 | unique_info = (struct unres_list_uniq *)item; |
| 5678 | 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] | 5679 | break; |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 5680 | case UNRES_AUGMENT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5681 | rc = resolve_augment(item, NULL); |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 5682 | break; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5683 | case UNRES_XPATH: |
| 5684 | node = (struct lys_node *)item; |
| 5685 | rc = check_xpath(node); |
| 5686 | break; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5687 | default: |
| 5688 | LOGINT; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5689 | break; |
| 5690 | } |
| 5691 | |
Radek Krejci | 54081ce | 2016-08-12 15:21:47 +0200 | [diff] [blame] | 5692 | if (has_str && !rc) { |
| 5693 | /* the string is no more needed in case of success. |
| 5694 | * 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] | 5695 | lydict_remove(mod->ctx, str_snode); |
| 5696 | } |
| 5697 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5698 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5699 | } |
| 5700 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5701 | /* logs directly */ |
| 5702 | static void |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5703 | 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] | 5704 | { |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 5705 | struct lyxml_elem *xml; |
| 5706 | struct lyxml_attr *attr; |
Radek Krejci | 76e15e1 | 2016-06-22 11:02:24 +0200 | [diff] [blame] | 5707 | const char *type_name = NULL; |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 5708 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5709 | switch (type) { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5710 | case UNRES_IDENT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5711 | 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] | 5712 | break; |
| 5713 | case UNRES_TYPE_IDENTREF: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5714 | 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] | 5715 | break; |
| 5716 | case UNRES_TYPE_LEAFREF: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5717 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "leafref", |
| 5718 | ((struct lys_type *)item)->info.lref.path); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5719 | break; |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 5720 | case UNRES_TYPE_DER_TPDF: |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5721 | case UNRES_TYPE_DER: |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 5722 | xml = (struct lyxml_elem *)((struct lys_type *)item)->der; |
| 5723 | if (xml->flags & LY_YANG_STRUCTURE_FLAG) { |
| 5724 | type_name = ((struct yang_type *)xml)->name; |
| 5725 | } else { |
| 5726 | LY_TREE_FOR(xml->attr, attr) { |
| 5727 | if ((attr->type == LYXML_ATTR_STD) && !strcmp(attr->name, "name")) { |
| 5728 | type_name = attr->value; |
| 5729 | break; |
| 5730 | } |
| 5731 | } |
| 5732 | assert(attr); |
| 5733 | } |
| 5734 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "derived type", type_name); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5735 | break; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5736 | case UNRES_IFFEAT: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 5737 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "if-feature", (char *)item); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5738 | break; |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 5739 | case UNRES_FEATURE: |
| 5740 | LOGVRB("There are unresolved if-features for \"%s\" feature circular dependency check, it will be attempted later", |
| 5741 | ((struct lys_feature *)item)->name); |
| 5742 | break; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5743 | case UNRES_USES: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5744 | 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] | 5745 | break; |
| 5746 | case UNRES_TYPE_DFLT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5747 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "type default", (char *)str_node); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5748 | break; |
| 5749 | case UNRES_CHOICE_DFLT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5750 | 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] | 5751 | break; |
| 5752 | case UNRES_LIST_KEYS: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5753 | 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] | 5754 | break; |
| 5755 | case UNRES_LIST_UNIQ: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5756 | 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] | 5757 | break; |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 5758 | case UNRES_AUGMENT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5759 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "augment target", |
| 5760 | ((struct lys_node_augment *)item)->target_name); |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 5761 | break; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5762 | case UNRES_XPATH: |
| 5763 | LOGVRB("Resolving %s \"%s\" with the context node \"%s\" failed, it will be attempted later.", "XPath", |
| 5764 | (char *)str_node, ((struct lys_node *)item)->name); |
| 5765 | break; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5766 | default: |
| 5767 | LOGINT; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5768 | break; |
| 5769 | } |
| 5770 | } |
| 5771 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5772 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5773 | * @brief Resolve every unres schema item in the structure. Logs directly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5774 | * |
| 5775 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5776 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5777 | * |
Michal Vasko | 92b8a38 | 2015-08-19 14:03:49 +0200 | [diff] [blame] | 5778 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5779 | */ |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5780 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 5781 | resolve_unres_schema(struct lys_module *mod, struct unres_schema *unres) |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5782 | { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5783 | uint32_t i, resolved = 0, unres_count, res_count; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5784 | int rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5785 | |
| 5786 | assert(unres); |
| 5787 | |
Michal Vasko | a0ffcab | 2016-05-02 14:52:08 +0200 | [diff] [blame] | 5788 | LOGVRB("Resolving unresolved schema nodes and their constraints..."); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5789 | ly_vlog_hide(1); |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 5790 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5791 | /* uses */ |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 5792 | do { |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 5793 | unres_count = 0; |
| 5794 | res_count = 0; |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 5795 | |
| 5796 | for (i = 0; i < unres->count; ++i) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5797 | /* UNRES_TYPE_LEAFREF must be resolved (for storing leafref target pointers); |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 5798 | * if-features are resolved here to make sure that we will have all if-features for |
| 5799 | * later check of feature circular dependency */ |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5800 | if (unres->type[i] > UNRES_IDENT) { |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 5801 | continue; |
| 5802 | } |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5803 | /* processes UNRES_USES, UNRES_IFFEAT, UNRES_TYPE_DER, UNRES_TYPE_DER_TPDF, UNRES_TYPE_LEAFREF, |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 5804 | * UNRES_CHOICE_DFLT and UNRES_IDENT */ |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 5805 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 5806 | ++unres_count; |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5807 | rc = resolve_unres_schema_item(mod, unres->item[i], unres->type[i], unres->str_snode[i], unres); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5808 | if (!rc) { |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 5809 | unres->type[i] = UNRES_RESOLVED; |
| 5810 | ++resolved; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 5811 | ++res_count; |
Michal Vasko | 89e1532 | 2015-08-17 15:46:55 +0200 | [diff] [blame] | 5812 | } else if (rc == -1) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5813 | ly_vlog_hide(0); |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 5814 | /* print the error */ |
| 5815 | resolve_unres_schema_item(mod, unres->item[i], unres->type[i], unres->str_snode[i], unres); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5816 | return -1; |
Radek Krejci | c2a180f | 2016-06-22 13:28:16 +0200 | [diff] [blame] | 5817 | } else { |
| 5818 | /* forward reference, erase ly_errno */ |
| 5819 | ly_errno = LY_SUCCESS; |
| 5820 | ly_vecode = LYVE_SUCCESS; |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 5821 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5822 | } |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 5823 | } while (res_count && (res_count < unres_count)); |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 5824 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 5825 | if (res_count < unres_count) { |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 5826 | /* just print the errors */ |
| 5827 | ly_vlog_hide(0); |
| 5828 | |
| 5829 | for (i = 0; i < unres->count; ++i) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5830 | if (unres->type[i] > UNRES_IDENT) { |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 5831 | continue; |
| 5832 | } |
| 5833 | resolve_unres_schema_item(mod, unres->item[i], unres->type[i], unres->str_snode[i], unres); |
Radek Krejci | 8a8d5ff | 2016-07-27 14:08:38 +0200 | [diff] [blame] | 5834 | |
| 5835 | /* free the allocated resources */ |
| 5836 | if (unres->type[i] == UNRES_IFFEAT) { |
| 5837 | free(*((char **)unres->item[i])); |
| 5838 | } |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 5839 | } |
Michal Vasko | 92b8a38 | 2015-08-19 14:03:49 +0200 | [diff] [blame] | 5840 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5841 | } |
| 5842 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5843 | /* the rest */ |
| 5844 | for (i = 0; i < unres->count; ++i) { |
| 5845 | if (unres->type[i] == UNRES_RESOLVED) { |
| 5846 | continue; |
| 5847 | } |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 5848 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5849 | rc = resolve_unres_schema_item(mod, unres->item[i], unres->type[i], unres->str_snode[i], unres); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5850 | if (rc == 0) { |
| 5851 | unres->type[i] = UNRES_RESOLVED; |
| 5852 | ++resolved; |
| 5853 | } else if (rc == -1) { |
| 5854 | ly_vlog_hide(0); |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 5855 | /* print the error */ |
| 5856 | resolve_unres_schema_item(mod, unres->item[i], unres->type[i], unres->str_snode[i], unres); |
| 5857 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5858 | } |
| 5859 | } |
| 5860 | |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5861 | ly_vlog_hide(0); |
| 5862 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5863 | if (resolved < unres->count) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5864 | /* try to resolve the unresolved nodes again, it will not resolve anything, but it will print |
| 5865 | * all the validation errors |
| 5866 | */ |
| 5867 | for (i = 0; i < unres->count; ++i) { |
| 5868 | if (unres->type[i] == UNRES_RESOLVED) { |
| 5869 | continue; |
| 5870 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5871 | resolve_unres_schema_item(mod, unres->item[i], unres->type[i], unres->str_snode[i], unres); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5872 | } |
Michal Vasko | 92b8a38 | 2015-08-19 14:03:49 +0200 | [diff] [blame] | 5873 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5874 | } |
| 5875 | |
Michal Vasko | a0ffcab | 2016-05-02 14:52:08 +0200 | [diff] [blame] | 5876 | LOGVRB("All schema nodes and constraints resolved."); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 5877 | unres->count = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5878 | return EXIT_SUCCESS; |
| 5879 | } |
| 5880 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5881 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5882 | * @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] | 5883 | * |
| 5884 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5885 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5886 | * @param[in] item Item to resolve. Type determined by \p type. |
| 5887 | * @param[in] type Type of the unresolved item. |
| 5888 | * @param[in] str String argument. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5889 | * |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 5890 | * @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] | 5891 | */ |
| 5892 | int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5893 | unres_schema_add_str(struct lys_module *mod, struct unres_schema *unres, void *item, enum UNRES_ITEM type, |
| 5894 | const char *str) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5895 | { |
Radek Krejci | 54081ce | 2016-08-12 15:21:47 +0200 | [diff] [blame] | 5896 | int rc; |
| 5897 | const char *dictstr; |
| 5898 | |
| 5899 | dictstr = lydict_insert(mod->ctx, str, 0); |
| 5900 | rc = unres_schema_add_node(mod, unres, item, type, (struct lys_node *)dictstr); |
| 5901 | |
| 5902 | if (rc == -1) { |
| 5903 | lydict_remove(mod->ctx, dictstr); |
| 5904 | } |
| 5905 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5906 | } |
| 5907 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5908 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5909 | * @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] | 5910 | * |
| 5911 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5912 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5913 | * @param[in] item Item to resolve. Type determined by \p type. |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 5914 | * @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] | 5915 | * @param[in] snode Schema node argument. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5916 | * |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 5917 | * @return EXIT_SUCCESS on success, EXIT_FIALURE on storing the item in unres, -1 on error. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5918 | */ |
| 5919 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 5920 | 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] | 5921 | struct lys_node *snode) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5922 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5923 | int rc; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 5924 | struct lyxml_elem *yin; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5925 | char *path, *msg; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5926 | |
Michal Vasko | 9bf425b | 2015-10-22 11:42:03 +0200 | [diff] [blame] | 5927 | assert(unres && item && ((type != UNRES_LEAFREF) && (type != UNRES_INSTID) && (type != UNRES_WHEN) |
| 5928 | && (type != UNRES_MUST))); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5929 | |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5930 | ly_vlog_hide(1); |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5931 | rc = resolve_unres_schema_item(mod, item, type, snode, unres); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5932 | ly_vlog_hide(0); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5933 | if (rc != EXIT_FAILURE) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5934 | if (rc == -1 && ly_errno == LY_EVALID) { |
Radek Krejci | 76e15e1 | 2016-06-22 11:02:24 +0200 | [diff] [blame] | 5935 | if (ly_log_level >= LY_LLERR) { |
| 5936 | path = strdup(ly_errpath()); |
| 5937 | msg = strdup(ly_errmsg()); |
| 5938 | LOGERR(LY_EVALID, "%s%s%s%s", msg, path[0] ? " (path: " : "", path[0] ? path : "", path[0] ? ")" : ""); |
| 5939 | free(path); |
| 5940 | free(msg); |
| 5941 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5942 | } |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 5943 | if (type == UNRES_LIST_UNIQ) { |
| 5944 | /* free the allocated structure */ |
| 5945 | free(item); |
| 5946 | } |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5947 | return rc; |
Radek Krejci | f347abc | 2016-06-22 10:18:47 +0200 | [diff] [blame] | 5948 | } else { |
| 5949 | /* erase info about validation errors */ |
| 5950 | ly_errno = LY_SUCCESS; |
| 5951 | ly_vecode = LYVE_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5952 | } |
| 5953 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5954 | print_unres_schema_item_fail(item, type, snode); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5955 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 5956 | /* HACK unlinking is performed here so that we do not do any (NS) copying in vain */ |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 5957 | if (type == UNRES_TYPE_DER || type == UNRES_TYPE_DER_TPDF) { |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 5958 | yin = (struct lyxml_elem *)((struct lys_type *)item)->der; |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 5959 | if (!(yin->flags & LY_YANG_STRUCTURE_FLAG)) { |
| 5960 | lyxml_unlink_elem(mod->ctx, yin, 1); |
| 5961 | ((struct lys_type *)item)->der = (struct lys_tpdf *)yin; |
| 5962 | } |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 5963 | } |
| 5964 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5965 | unres->count++; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 5966 | unres->item = ly_realloc(unres->item, unres->count*sizeof *unres->item); |
| 5967 | if (!unres->item) { |
| 5968 | LOGMEM; |
| 5969 | return -1; |
| 5970 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5971 | unres->item[unres->count-1] = item; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 5972 | unres->type = ly_realloc(unres->type, unres->count*sizeof *unres->type); |
| 5973 | if (!unres->type) { |
| 5974 | LOGMEM; |
| 5975 | return -1; |
| 5976 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5977 | unres->type[unres->count-1] = type; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 5978 | unres->str_snode = ly_realloc(unres->str_snode, unres->count*sizeof *unres->str_snode); |
| 5979 | if (!unres->str_snode) { |
| 5980 | LOGMEM; |
| 5981 | return -1; |
| 5982 | } |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5983 | unres->str_snode[unres->count-1] = snode; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 5984 | unres->module = ly_realloc(unres->module, unres->count*sizeof *unres->module); |
| 5985 | if (!unres->module) { |
| 5986 | LOGMEM; |
| 5987 | return -1; |
| 5988 | } |
| 5989 | unres->module[unres->count-1] = mod; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5990 | |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 5991 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5992 | } |
| 5993 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5994 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5995 | * @brief Duplicate an unres schema item. Logs indirectly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5996 | * |
| 5997 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5998 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5999 | * @param[in] item Old item to be resolved. |
| 6000 | * @param[in] type Type of the old unresolved item. |
| 6001 | * @param[in] new_item New item to use in the duplicate. |
| 6002 | * |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 6003 | * @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] | 6004 | */ |
Michal Vasko | dad1940 | 2015-08-06 09:51:53 +0200 | [diff] [blame] | 6005 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 6006 | 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] | 6007 | { |
| 6008 | int i; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6009 | struct unres_list_uniq aux_uniq; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6010 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6011 | 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] | 6012 | |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6013 | /* hack for UNRES_LIST_UNIQ, which stores multiple items behind its item */ |
| 6014 | if (type == UNRES_LIST_UNIQ) { |
| 6015 | aux_uniq.list = item; |
| 6016 | aux_uniq.expr = ((struct unres_list_uniq *)new_item)->expr; |
| 6017 | item = &aux_uniq; |
| 6018 | } |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 6019 | i = unres_schema_find(unres, -1, item, type); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6020 | |
| 6021 | if (i == -1) { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6022 | if (type == UNRES_LIST_UNIQ) { |
| 6023 | free(new_item); |
| 6024 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 6025 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6026 | } |
| 6027 | |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6028 | if ((type == UNRES_TYPE_LEAFREF) || (type == UNRES_USES) || (type == UNRES_TYPE_DFLT) || |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6029 | (type == UNRES_IFFEAT) || (type == UNRES_FEATURE) || (type == UNRES_LIST_UNIQ)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6030 | 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] | 6031 | LOGINT; |
| 6032 | return -1; |
| 6033 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6034 | } else { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6035 | 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] | 6036 | LOGINT; |
| 6037 | return -1; |
| 6038 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6039 | } |
Michal Vasko | dad1940 | 2015-08-06 09:51:53 +0200 | [diff] [blame] | 6040 | |
| 6041 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6042 | } |
| 6043 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6044 | /* does not log */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6045 | int |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 6046 | 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] | 6047 | { |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 6048 | int i; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6049 | struct unres_list_uniq *aux_uniq1, *aux_uniq2; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6050 | |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 6051 | if (start_on_backwards > 0) { |
| 6052 | i = start_on_backwards; |
| 6053 | } else { |
| 6054 | i = unres->count - 1; |
| 6055 | } |
| 6056 | for (; i > -1; i--) { |
| 6057 | if (unres->type[i] != type) { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6058 | continue; |
| 6059 | } |
| 6060 | if (type != UNRES_LIST_UNIQ) { |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 6061 | if (unres->item[i] == item) { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6062 | break; |
| 6063 | } |
| 6064 | } else { |
| 6065 | aux_uniq1 = (struct unres_list_uniq *)unres->item[i - 1]; |
| 6066 | aux_uniq2 = (struct unres_list_uniq *)item; |
| 6067 | 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] | 6068 | break; |
| 6069 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6070 | } |
| 6071 | } |
| 6072 | |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 6073 | return i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6074 | } |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6075 | |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 6076 | static void |
| 6077 | unres_schema_free_item(struct ly_ctx *ctx, struct unres_schema *unres, uint32_t i) |
| 6078 | { |
| 6079 | struct lyxml_elem *yin; |
| 6080 | struct yang_type *yang; |
| 6081 | |
| 6082 | switch (unres->type[i]) { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 6083 | case UNRES_TYPE_DER_TPDF: |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 6084 | case UNRES_TYPE_DER: |
| 6085 | yin = (struct lyxml_elem *)((struct lys_type *)unres->item[i])->der; |
| 6086 | if (yin->flags & LY_YANG_STRUCTURE_FLAG) { |
| 6087 | yang =(struct yang_type *)yin; |
| 6088 | yang->type->base = yang->base; |
| 6089 | lydict_remove(ctx, yang->name); |
| 6090 | free(yang); |
| 6091 | } else { |
| 6092 | lyxml_free(ctx, yin); |
| 6093 | } |
| 6094 | break; |
| 6095 | case UNRES_IDENT: |
| 6096 | case UNRES_TYPE_IDENTREF: |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 6097 | case UNRES_TYPE_DFLT: |
| 6098 | case UNRES_CHOICE_DFLT: |
| 6099 | case UNRES_LIST_KEYS: |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 6100 | lydict_remove(ctx, (const char *)unres->str_snode[i]); |
| 6101 | break; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6102 | case UNRES_LIST_UNIQ: |
| 6103 | free(unres->item[i]); |
| 6104 | break; |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 6105 | default: |
| 6106 | break; |
| 6107 | } |
| 6108 | unres->type[i] = UNRES_RESOLVED; |
| 6109 | } |
| 6110 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6111 | void |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6112 | unres_schema_free(struct lys_module *module, struct unres_schema **unres) |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6113 | { |
| 6114 | uint32_t i; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6115 | unsigned int unresolved = 0; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6116 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6117 | if (!unres || !(*unres)) { |
| 6118 | return; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6119 | } |
| 6120 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6121 | assert(module || (*unres)->count == 0); |
| 6122 | |
| 6123 | for (i = 0; i < (*unres)->count; ++i) { |
| 6124 | if ((*unres)->module[i] != module) { |
| 6125 | if ((*unres)->type[i] != UNRES_RESOLVED) { |
| 6126 | unresolved++; |
| 6127 | } |
| 6128 | continue; |
| 6129 | } |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 6130 | |
| 6131 | /* free heap memory for the specific item */ |
| 6132 | unres_schema_free_item(module->ctx, *unres, i); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6133 | } |
| 6134 | |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 6135 | /* free it all */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6136 | if (!module || (!unresolved && !module->type)) { |
| 6137 | free((*unres)->item); |
| 6138 | free((*unres)->type); |
| 6139 | free((*unres)->str_snode); |
| 6140 | free((*unres)->module); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6141 | free((*unres)); |
| 6142 | (*unres) = NULL; |
| 6143 | } |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6144 | } |
| 6145 | |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6146 | /** |
| 6147 | * @brief Resolve a single unres data item. Logs directly. |
| 6148 | * |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6149 | * @param[in] node Data node to resolve. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6150 | * @param[in] type Type of the unresolved item. |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6151 | * |
| 6152 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 6153 | */ |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 6154 | int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6155 | resolve_unres_data_item(struct lyd_node *node, enum UNRES_ITEM type) |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6156 | { |
| 6157 | uint32_t i; |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 6158 | int rc; |
Michal Vasko | 83a6c46 | 2015-10-08 16:43:53 +0200 | [diff] [blame] | 6159 | struct lyd_node_leaf_list *leaf; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6160 | struct lys_node_leaf *sleaf; |
Michal Vasko | c428084 | 2016-04-19 16:10:42 +0200 | [diff] [blame] | 6161 | struct lyd_node *parent; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6162 | struct unres_data matches; |
| 6163 | |
| 6164 | memset(&matches, 0, sizeof matches); |
Michal Vasko | 83a6c46 | 2015-10-08 16:43:53 +0200 | [diff] [blame] | 6165 | leaf = (struct lyd_node_leaf_list *)node; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6166 | sleaf = (struct lys_node_leaf *)leaf->schema; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6167 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6168 | switch (type) { |
| 6169 | case UNRES_LEAFREF: |
| 6170 | assert(sleaf->type.base == LY_TYPE_LEAFREF); |
Michal Vasko | 2471e7f | 2016-04-11 11:00:15 +0200 | [diff] [blame] | 6171 | /* EXIT_FAILURE return keeps leaf->value.lefref NULL, handled later */ |
| 6172 | if (resolve_path_arg_data(node, sleaf->type.info.lref.path, &matches) == -1) { |
| 6173 | return -1; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6174 | } |
| 6175 | |
| 6176 | /* check that value matches */ |
| 6177 | for (i = 0; i < matches.count; ++i) { |
Radek Krejci | 749190d | 2016-02-18 16:26:25 +0100 | [diff] [blame] | 6178 | if (ly_strequal(leaf->value_str, ((struct lyd_node_leaf_list *)matches.node[i])->value_str, 1)) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6179 | leaf->value.leafref = matches.node[i]; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6180 | break; |
| 6181 | } |
| 6182 | } |
| 6183 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6184 | free(matches.node); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6185 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6186 | if (!leaf->value.leafref) { |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6187 | /* reference not found */ |
Michal Vasko | 9925e28 | 2016-09-02 12:45:14 +0200 | [diff] [blame] | 6188 | if (sleaf->type.info.lref.req > -1) { |
| 6189 | LOGVAL(LYE_NOLEAFREF, LY_VLOG_LYD, leaf, sleaf->type.info.lref.path, leaf->value_str); |
| 6190 | return EXIT_FAILURE; |
| 6191 | } else { |
| 6192 | LOGVRB("There is no leafref with the value \"%s\", but it is not required.", leaf->value_str); |
| 6193 | } |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6194 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6195 | break; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6196 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6197 | case UNRES_INSTID: |
Michal Vasko | 976a5f2 | 2016-05-18 13:28:42 +0200 | [diff] [blame] | 6198 | assert((sleaf->type.base == LY_TYPE_INST) || (sleaf->type.base == LY_TYPE_UNION)); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6199 | ly_errno = 0; |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6200 | leaf->value.instance = resolve_instid(node, leaf->value_str); |
Radek Krejci | 40f17b9 | 2016-02-03 14:30:43 +0100 | [diff] [blame] | 6201 | if (!leaf->value.instance) { |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6202 | if (ly_errno) { |
| 6203 | return -1; |
| 6204 | } else if (sleaf->type.info.inst.req > -1) { |
Michal Vasko | 6ac6828 | 2016-04-11 10:56:47 +0200 | [diff] [blame] | 6205 | LOGVAL(LYE_NOREQINS, LY_VLOG_LYD, leaf, leaf->value_str); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6206 | return EXIT_FAILURE; |
| 6207 | } else { |
Michal Vasko | 9925e28 | 2016-09-02 12:45:14 +0200 | [diff] [blame] | 6208 | LOGVRB("There is no instance identifier \"%s\", but it is not required.", leaf->value_str); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6209 | } |
| 6210 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6211 | break; |
| 6212 | |
| 6213 | case UNRES_WHEN: |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 6214 | if ((rc = resolve_when(node, NULL))) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6215 | return rc; |
| 6216 | } |
| 6217 | break; |
| 6218 | |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 6219 | case UNRES_MUST: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6220 | if ((rc = resolve_must(node))) { |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 6221 | return rc; |
| 6222 | } |
| 6223 | break; |
| 6224 | |
Michal Vasko | c428084 | 2016-04-19 16:10:42 +0200 | [diff] [blame] | 6225 | case UNRES_EMPTYCONT: |
| 6226 | do { |
| 6227 | parent = node->parent; |
| 6228 | lyd_free(node); |
| 6229 | node = parent; |
| 6230 | } while (node && (node->schema->nodetype == LYS_CONTAINER) && !node->child |
| 6231 | && !((struct lys_node_container *)node->schema)->presence); |
| 6232 | break; |
| 6233 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6234 | default: |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6235 | LOGINT; |
| 6236 | return -1; |
| 6237 | } |
| 6238 | |
| 6239 | return EXIT_SUCCESS; |
| 6240 | } |
| 6241 | |
| 6242 | /** |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6243 | * @brief add data unres item |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6244 | * |
| 6245 | * @param[in] unres Unres data structure to use. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6246 | * @param[in] node Data node to use. |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6247 | * |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6248 | * @return 0 on success, -1 on error. |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6249 | */ |
| 6250 | int |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6251 | 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] | 6252 | { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6253 | assert(unres && node); |
Michal Vasko | c428084 | 2016-04-19 16:10:42 +0200 | [diff] [blame] | 6254 | assert((type == UNRES_LEAFREF) || (type == UNRES_INSTID) || (type == UNRES_WHEN) || (type == UNRES_MUST) |
| 6255 | || (type == UNRES_EMPTYCONT)); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6256 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6257 | unres->count++; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 6258 | unres->node = ly_realloc(unres->node, unres->count * sizeof *unres->node); |
| 6259 | if (!unres->node) { |
| 6260 | LOGMEM; |
| 6261 | return -1; |
| 6262 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6263 | unres->node[unres->count - 1] = node; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 6264 | unres->type = ly_realloc(unres->type, unres->count * sizeof *unres->type); |
| 6265 | if (!unres->type) { |
| 6266 | LOGMEM; |
| 6267 | return -1; |
| 6268 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6269 | unres->type[unres->count - 1] = type; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6270 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6271 | if (type == UNRES_WHEN) { |
| 6272 | /* remove previous result */ |
| 6273 | node->when_status = LYD_WHEN; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6274 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6275 | |
| 6276 | return EXIT_SUCCESS; |
| 6277 | } |
| 6278 | |
| 6279 | /** |
| 6280 | * @brief Resolve every unres data item in the structure. Logs directly. |
| 6281 | * |
| 6282 | * @param[in] unres Unres data structure to use. |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6283 | * @param[in,out] root Root node of the data tree. If not NULL, auto-delete is performed on false when condition. If |
| 6284 | * NULL and when condition is false the error is raised. |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 6285 | * @param[in] options Parer options |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6286 | * |
| 6287 | * @return EXIT_SUCCESS on success, -1 on error. |
| 6288 | */ |
| 6289 | int |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 6290 | 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] | 6291 | { |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 6292 | uint32_t i, j, first = 1, resolved = 0, del_items = 0, when_stmt = 0; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6293 | int rc, progress; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6294 | char *msg, *path; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6295 | struct lyd_node *parent; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6296 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6297 | assert(unres); |
Radek Krejci | 8ee1b56 | 2016-03-31 10:58:31 +0200 | [diff] [blame] | 6298 | assert((root && (*root)) || (options & LYD_OPT_NOAUTODEL)); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6299 | |
| 6300 | if (!unres->count) { |
| 6301 | return EXIT_SUCCESS; |
| 6302 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6303 | |
Michal Vasko | a0ffcab | 2016-05-02 14:52:08 +0200 | [diff] [blame] | 6304 | LOGVRB("Resolving unresolved data nodes and their constraints..."); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6305 | ly_vlog_hide(1); |
| 6306 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6307 | /* when-stmt first */ |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6308 | ly_errno = LY_SUCCESS; |
| 6309 | ly_vecode = LYVE_SUCCESS; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6310 | do { |
| 6311 | progress = 0; |
| 6312 | for(i = 0; i < unres->count; i++) { |
| 6313 | if (unres->type[i] != UNRES_WHEN) { |
| 6314 | continue; |
| 6315 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 6316 | if (first) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6317 | /* count when-stmt nodes in unres list */ |
| 6318 | when_stmt++; |
| 6319 | } |
| 6320 | |
| 6321 | /* resolve when condition only when all parent when conditions are already resolved */ |
| 6322 | for (parent = unres->node[i]->parent; |
| 6323 | parent && LYD_WHEN_DONE(parent->when_status); |
| 6324 | parent = parent->parent) { |
| 6325 | if (!parent->parent && (parent->when_status & LYD_WHEN_FALSE)) { |
| 6326 | /* the parent node was already unlinked, do not resolve this node, |
| 6327 | * it will be removed anyway, so just mark it as resolved |
| 6328 | */ |
| 6329 | unres->node[i]->when_status |= LYD_WHEN_FALSE; |
| 6330 | unres->type[i] = UNRES_RESOLVED; |
| 6331 | resolved++; |
| 6332 | break; |
| 6333 | } |
| 6334 | } |
| 6335 | if (parent) { |
| 6336 | continue; |
| 6337 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6338 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6339 | rc = resolve_unres_data_item(unres->node[i], unres->type[i]); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6340 | if (!rc) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6341 | if (unres->node[i]->when_status & LYD_WHEN_FALSE) { |
| 6342 | if (!root) { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6343 | /* false when condition */ |
| 6344 | ly_vlog_hide(0); |
Radek Krejci | 76e15e1 | 2016-06-22 11:02:24 +0200 | [diff] [blame] | 6345 | if (ly_log_level >= LY_LLERR) { |
| 6346 | path = strdup(ly_errpath()); |
| 6347 | msg = strdup(ly_errmsg()); |
| 6348 | LOGERR(LY_EVALID, "%s%s%s%s", msg, |
| 6349 | path[0] ? " (path: " : "", path[0] ? path : "", path[0] ? ")" : ""); |
| 6350 | free(path); |
| 6351 | free(msg); |
| 6352 | } |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6353 | return -1; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6354 | } /* follows else */ |
| 6355 | |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 6356 | /* only unlink now, the subtree can contain another nodes stored in the unres list */ |
| 6357 | /* if it has parent non-presence containers that would be empty, we should actually |
| 6358 | * remove the container |
| 6359 | */ |
| 6360 | if (!(options & LYD_OPT_KEEPEMPTYCONT)) { |
| 6361 | for (parent = unres->node[i]; |
| 6362 | parent->parent && parent->parent->schema->nodetype == LYS_CONTAINER; |
| 6363 | parent = parent->parent) { |
| 6364 | if (((struct lys_node_container *)parent->parent->schema)->presence) { |
| 6365 | /* presence container */ |
| 6366 | break; |
| 6367 | } |
| 6368 | if (parent->next || parent->prev != parent) { |
| 6369 | /* non empty (the child we are in and we are going to remove is not the only child) */ |
| 6370 | break; |
| 6371 | } |
| 6372 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 6373 | unres->node[i] = parent; |
| 6374 | } |
| 6375 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6376 | /* auto-delete */ |
| 6377 | LOGVRB("auto-delete node \"%s\" due to when condition (%s)", ly_errpath(), |
| 6378 | ((struct lys_node_leaf *)unres->node[i]->schema)->when->cond); |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 6379 | if (*root && *root == unres->node[i]) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6380 | *root = (*root)->next; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6381 | } |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6382 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6383 | lyd_unlink(unres->node[i]); |
| 6384 | unres->type[i] = UNRES_DELETE; |
| 6385 | del_items++; |
Radek Krejci | 51fd816 | 2016-03-24 15:49:51 +0100 | [diff] [blame] | 6386 | |
| 6387 | /* update the rest of unres items */ |
| 6388 | for (j = 0; j < unres->count; j++) { |
Radek Krejci | 3db819b | 2016-03-24 16:29:48 +0100 | [diff] [blame] | 6389 | if (unres->type[j] == UNRES_RESOLVED || unres->type[j] == UNRES_DELETE) { |
Radek Krejci | 51fd816 | 2016-03-24 15:49:51 +0100 | [diff] [blame] | 6390 | continue; |
| 6391 | } |
| 6392 | |
| 6393 | /* test if the node is in subtree to be deleted */ |
| 6394 | for (parent = unres->node[j]; parent; parent = parent->parent) { |
| 6395 | if (parent == unres->node[i]) { |
| 6396 | /* yes, it is */ |
| 6397 | unres->type[j] = UNRES_RESOLVED; |
| 6398 | resolved++; |
| 6399 | break; |
| 6400 | } |
| 6401 | } |
| 6402 | } |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6403 | } else { |
| 6404 | unres->type[i] = UNRES_RESOLVED; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6405 | } |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6406 | ly_errno = LY_SUCCESS; |
| 6407 | ly_vecode = LYVE_SUCCESS; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6408 | resolved++; |
| 6409 | progress = 1; |
| 6410 | } else if (rc == -1) { |
| 6411 | ly_vlog_hide(0); |
Michal Vasko | 76e7340 | 2016-08-24 16:00:13 +0200 | [diff] [blame] | 6412 | /* print only this last error */ |
| 6413 | resolve_unres_data_item(unres->node[i], unres->type[i]); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6414 | return -1; |
Radek Krejci | c2a180f | 2016-06-22 13:28:16 +0200 | [diff] [blame] | 6415 | } else { |
| 6416 | /* forward reference, erase ly_errno */ |
| 6417 | ly_errno = LY_SUCCESS; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6418 | } |
| 6419 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 6420 | first = 0; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6421 | } while (progress && resolved < when_stmt); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6422 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6423 | /* do we have some unresolved when-stmt? */ |
Radek Krejci | d940d73 | 2016-03-24 16:02:28 +0100 | [diff] [blame] | 6424 | if (when_stmt > resolved) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6425 | ly_vlog_hide(0); |
Radek Krejci | 76e15e1 | 2016-06-22 11:02:24 +0200 | [diff] [blame] | 6426 | if (ly_log_level >= LY_LLERR) { |
| 6427 | path = strdup(ly_errpath()); |
| 6428 | msg = strdup(ly_errmsg()); |
| 6429 | LOGERR(LY_EVALID, "%s%s%s%s", msg, path[0] ? " (path: " : "", path[0] ? path : "", path[0] ? ")" : ""); |
| 6430 | free(path); |
| 6431 | free(msg); |
| 6432 | } |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6433 | return -1; |
| 6434 | } |
| 6435 | |
| 6436 | for (i = 0; del_items && i < unres->count; i++) { |
| 6437 | /* we had some when-stmt resulted to false, so now we have to sanitize the unres list */ |
| 6438 | if (unres->type[i] != UNRES_DELETE) { |
| 6439 | continue; |
| 6440 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 6441 | if (!unres->node[i]) { |
| 6442 | unres->type[i] = UNRES_RESOLVED; |
| 6443 | del_items--; |
| 6444 | continue; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6445 | } |
| 6446 | |
| 6447 | /* really remove the complete subtree */ |
| 6448 | lyd_free(unres->node[i]); |
| 6449 | unres->type[i] = UNRES_RESOLVED; |
| 6450 | del_items--; |
| 6451 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6452 | |
| 6453 | /* rest */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6454 | for (i = 0; i < unres->count; ++i) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6455 | if (unres->type[i] == UNRES_RESOLVED) { |
| 6456 | continue; |
| 6457 | } |
| 6458 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6459 | rc = resolve_unres_data_item(unres->node[i], unres->type[i]); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6460 | if (rc == 0) { |
| 6461 | unres->type[i] = UNRES_RESOLVED; |
| 6462 | resolved++; |
| 6463 | } else if (rc == -1) { |
| 6464 | ly_vlog_hide(0); |
Michal Vasko | 96b846c | 2016-05-18 13:28:58 +0200 | [diff] [blame] | 6465 | /* print only this last error */ |
| 6466 | resolve_unres_data_item(unres->node[i], unres->type[i]); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6467 | return -1; |
| 6468 | } |
| 6469 | } |
| 6470 | |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6471 | ly_vlog_hide(0); |
| 6472 | if (resolved < unres->count) { |
| 6473 | /* try to resolve the unresolved data again, it will not resolve anything, but it will print |
| 6474 | * all the validation errors |
| 6475 | */ |
| 6476 | for (i = 0; i < unres->count; ++i) { |
| 6477 | if (unres->type[i] == UNRES_RESOLVED) { |
| 6478 | continue; |
| 6479 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6480 | resolve_unres_data_item(unres->node[i], unres->type[i]); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6481 | } |
| 6482 | return -1; |
| 6483 | } |
| 6484 | |
Michal Vasko | a0ffcab | 2016-05-02 14:52:08 +0200 | [diff] [blame] | 6485 | LOGVRB("All data nodes and constraints resolved."); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6486 | unres->count = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6487 | return EXIT_SUCCESS; |
| 6488 | } |