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, |
| 997 | node->module->features[j].module, node->module->features[j].name, node)) { |
| 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, |
| 1011 | module->features[j].module, module->features[j].name, node)) { |
| 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, |
| 1031 | module->inc[i].submodule->features[j].name, node)) { |
| 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 */ |
| 1539 | if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYXML)) { |
| 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 */ |
| 2011 | if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYXML)) { |
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 | |
| 2098 | struct lyd_node * |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2099 | resolve_partial_json_data_nodeid(const char *nodeid, const char *llist_value, struct lyd_node *start, int options, |
| 2100 | int *parsed) |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2101 | { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2102 | char *module_name = ly_buf(), *buf_backup = NULL, *str; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2103 | const char *id, *mod_name, *name; |
| 2104 | 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] | 2105 | struct lyd_node *sibling, *last_match = NULL; |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2106 | struct lyd_node_leaf_list *llist; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2107 | const struct lys_module *prefix_mod, *prev_mod; |
| 2108 | struct ly_ctx *ctx; |
| 2109 | |
| 2110 | assert(nodeid && start && parsed); |
| 2111 | |
| 2112 | ctx = start->schema->module->ctx; |
| 2113 | id = nodeid; |
| 2114 | |
| 2115 | 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] | 2116 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2117 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2118 | return NULL; |
| 2119 | } |
| 2120 | id += r; |
| 2121 | /* add it to parsed only after the data node was actually found */ |
| 2122 | last_parsed = r; |
| 2123 | |
| 2124 | if (is_relative) { |
| 2125 | prev_mod = start->schema->module; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2126 | start = start->child; |
| 2127 | } else { |
| 2128 | for (; start->parent; start = start->parent); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2129 | prev_mod = start->schema->module; |
| 2130 | } |
| 2131 | |
| 2132 | while (1) { |
| 2133 | LY_TREE_FOR(start, sibling) { |
Michal Vasko | 2411b94 | 2016-03-23 13:50:03 +0100 | [diff] [blame] | 2134 | /* RPC data check, return simply invalid argument, because the data tree is invalid */ |
| 2135 | if (lys_parent(sibling->schema)) { |
| 2136 | if (options & LYD_PATH_OPT_OUTPUT) { |
| 2137 | if (lys_parent(sibling->schema)->nodetype == LYS_INPUT) { |
Michal Vasko | b724689 | 2016-04-19 10:37:35 +0200 | [diff] [blame] | 2138 | 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] | 2139 | *parsed = -1; |
| 2140 | return NULL; |
| 2141 | } |
| 2142 | } else { |
| 2143 | if (lys_parent(sibling->schema)->nodetype == LYS_OUTPUT) { |
Michal Vasko | b724689 | 2016-04-19 10:37:35 +0200 | [diff] [blame] | 2144 | 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] | 2145 | *parsed = -1; |
| 2146 | return NULL; |
| 2147 | } |
| 2148 | } |
| 2149 | } |
| 2150 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2151 | /* name match */ |
| 2152 | if (!strncmp(name, sibling->schema->name, nam_len) && !sibling->schema->name[nam_len]) { |
| 2153 | |
| 2154 | /* module check */ |
| 2155 | if (mod_name) { |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2156 | if (mod_name_len > LY_BUF_SIZE - 1) { |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2157 | LOGINT; |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2158 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2159 | return NULL; |
| 2160 | } |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2161 | |
| 2162 | if (ly_buf_used && module_name[0]) { |
| 2163 | buf_backup = strndup(module_name, LY_BUF_SIZE - 1); |
| 2164 | } |
| 2165 | ly_buf_used++; |
| 2166 | |
Michal Vasko | df3f1ab | 2016-04-01 15:07:22 +0200 | [diff] [blame] | 2167 | memmove(module_name, mod_name, mod_name_len); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2168 | module_name[mod_name_len] = '\0'; |
| 2169 | /* will also find an augment module */ |
| 2170 | prefix_mod = ly_ctx_get_module(ctx, module_name, NULL); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2171 | |
| 2172 | if (buf_backup) { |
| 2173 | /* return previous internal buffer content */ |
Michal Vasko | 888a129 | 2016-04-05 12:08:54 +0200 | [diff] [blame] | 2174 | strncpy(module_name, buf_backup, LY_BUF_SIZE - 1); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2175 | free(buf_backup); |
| 2176 | buf_backup = NULL; |
| 2177 | } |
| 2178 | ly_buf_used--; |
| 2179 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2180 | if (!prefix_mod) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2181 | str = strndup(nodeid, (mod_name + mod_name_len) - nodeid); |
| 2182 | LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str); |
| 2183 | free(str); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2184 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2185 | return NULL; |
| 2186 | } |
| 2187 | } else { |
| 2188 | prefix_mod = prev_mod; |
| 2189 | } |
| 2190 | if (prefix_mod != lys_node_module(sibling->schema)) { |
| 2191 | continue; |
| 2192 | } |
| 2193 | |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2194 | /* leaf-list, did we find it with the correct value or not? */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2195 | if (sibling->schema->nodetype == LYS_LEAFLIST) { |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2196 | llist = (struct lyd_node_leaf_list *)sibling; |
| 2197 | if ((!llist_value && llist->value_str && llist->value_str[0]) |
| 2198 | || (llist_value && strcmp(llist_value, llist->value_str))) { |
| 2199 | continue; |
| 2200 | } |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2201 | } |
| 2202 | |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2203 | /* list, we need predicates'n'stuff then */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2204 | if (sibling->schema->nodetype == LYS_LIST) { |
| 2205 | r = 0; |
| 2206 | if (!has_predicate) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2207 | LOGVAL(LYE_PATH_MISSKEY, LY_VLOG_NONE, NULL, name); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2208 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2209 | return NULL; |
| 2210 | } |
| 2211 | ret = resolve_partial_json_data_list_predicate(id, name, sibling, &r); |
| 2212 | if (ret == -1) { |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2213 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2214 | return NULL; |
| 2215 | } else if (ret == 1) { |
| 2216 | /* this list instance does not match */ |
| 2217 | continue; |
| 2218 | } |
| 2219 | id += r; |
| 2220 | last_parsed += r; |
| 2221 | } |
| 2222 | |
| 2223 | *parsed += last_parsed; |
| 2224 | |
| 2225 | /* the result node? */ |
| 2226 | if (!id[0]) { |
| 2227 | return sibling; |
| 2228 | } |
| 2229 | |
| 2230 | /* move down the tree, if possible */ |
Michal Vasko | c6c52cf | 2016-03-29 11:53:04 +0200 | [diff] [blame] | 2231 | if (sibling->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYXML)) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2232 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2233 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2234 | return NULL; |
| 2235 | } |
Michal Vasko | c6c52cf | 2016-03-29 11:53:04 +0200 | [diff] [blame] | 2236 | last_match = sibling; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2237 | start = sibling->child; |
| 2238 | if (start) { |
| 2239 | prev_mod = start->schema->module; |
| 2240 | } |
| 2241 | break; |
| 2242 | } |
| 2243 | } |
| 2244 | |
| 2245 | /* no match, return last match */ |
| 2246 | if (!sibling) { |
| 2247 | return last_match; |
| 2248 | } |
| 2249 | |
| 2250 | 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] | 2251 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2252 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2253 | return NULL; |
| 2254 | } |
| 2255 | id += r; |
| 2256 | last_parsed = r; |
| 2257 | } |
| 2258 | |
| 2259 | /* cannot get here */ |
| 2260 | LOGINT; |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2261 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2262 | return NULL; |
| 2263 | } |
| 2264 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2265 | /** |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2266 | * @brief Resolves length or range intervals. Does not log. |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2267 | * Syntax is assumed to be correct, *ret MUST be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2268 | * |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2269 | * @param[in] str_restr Restriction as a string. |
| 2270 | * @param[in] type Type of the restriction. |
| 2271 | * @param[out] ret Final interval structure that starts with |
| 2272 | * the interval of the initial type, continues with intervals |
| 2273 | * of any superior types derived from the initial one, and |
| 2274 | * finishes with intervals from our \p type. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2275 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2276 | * @return EXIT_SUCCESS on succes, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2277 | */ |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2278 | int |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2279 | 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] | 2280 | { |
| 2281 | /* 0 - unsigned, 1 - signed, 2 - floating point */ |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2282 | int kind; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2283 | int64_t local_smin, local_smax; |
| 2284 | uint64_t local_umin, local_umax; |
| 2285 | long double local_fmin, local_fmax; |
| 2286 | const char *seg_ptr, *ptr; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2287 | 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] | 2288 | |
| 2289 | switch (type->base) { |
| 2290 | case LY_TYPE_BINARY: |
| 2291 | kind = 0; |
| 2292 | local_umin = 0; |
| 2293 | local_umax = 18446744073709551615UL; |
| 2294 | |
| 2295 | if (!str_restr && type->info.binary.length) { |
| 2296 | str_restr = type->info.binary.length->expr; |
| 2297 | } |
| 2298 | break; |
| 2299 | case LY_TYPE_DEC64: |
| 2300 | kind = 2; |
Radek Krejci | 8c3b4b6 | 2016-06-17 14:32:12 +0200 | [diff] [blame] | 2301 | local_fmin = ((long double)-9223372036854775808.0) / type->info.dec64.div; |
| 2302 | local_fmax = ((long double)9223372036854775807.0) / type->info.dec64.div; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2303 | |
| 2304 | if (!str_restr && type->info.dec64.range) { |
| 2305 | str_restr = type->info.dec64.range->expr; |
| 2306 | } |
| 2307 | break; |
| 2308 | case LY_TYPE_INT8: |
| 2309 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2310 | local_smin = __INT64_C(-128); |
| 2311 | local_smax = __INT64_C(127); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2312 | |
| 2313 | if (!str_restr && type->info.num.range) { |
| 2314 | str_restr = type->info.num.range->expr; |
| 2315 | } |
| 2316 | break; |
| 2317 | case LY_TYPE_INT16: |
| 2318 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2319 | local_smin = __INT64_C(-32768); |
| 2320 | local_smax = __INT64_C(32767); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2321 | |
| 2322 | if (!str_restr && type->info.num.range) { |
| 2323 | str_restr = type->info.num.range->expr; |
| 2324 | } |
| 2325 | break; |
| 2326 | case LY_TYPE_INT32: |
| 2327 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2328 | local_smin = __INT64_C(-2147483648); |
| 2329 | local_smax = __INT64_C(2147483647); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2330 | |
| 2331 | if (!str_restr && type->info.num.range) { |
| 2332 | str_restr = type->info.num.range->expr; |
| 2333 | } |
| 2334 | break; |
| 2335 | case LY_TYPE_INT64: |
| 2336 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2337 | local_smin = __INT64_C(-9223372036854775807) - __INT64_C(1); |
| 2338 | local_smax = __INT64_C(9223372036854775807); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2339 | |
| 2340 | if (!str_restr && type->info.num.range) { |
| 2341 | str_restr = type->info.num.range->expr; |
| 2342 | } |
| 2343 | break; |
| 2344 | case LY_TYPE_UINT8: |
| 2345 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2346 | local_umin = __UINT64_C(0); |
| 2347 | local_umax = __UINT64_C(255); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2348 | |
| 2349 | if (!str_restr && type->info.num.range) { |
| 2350 | str_restr = type->info.num.range->expr; |
| 2351 | } |
| 2352 | break; |
| 2353 | case LY_TYPE_UINT16: |
| 2354 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2355 | local_umin = __UINT64_C(0); |
| 2356 | local_umax = __UINT64_C(65535); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2357 | |
| 2358 | if (!str_restr && type->info.num.range) { |
| 2359 | str_restr = type->info.num.range->expr; |
| 2360 | } |
| 2361 | break; |
| 2362 | case LY_TYPE_UINT32: |
| 2363 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2364 | local_umin = __UINT64_C(0); |
| 2365 | local_umax = __UINT64_C(4294967295); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2366 | |
| 2367 | if (!str_restr && type->info.num.range) { |
| 2368 | str_restr = type->info.num.range->expr; |
| 2369 | } |
| 2370 | break; |
| 2371 | case LY_TYPE_UINT64: |
| 2372 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2373 | local_umin = __UINT64_C(0); |
| 2374 | local_umax = __UINT64_C(18446744073709551615); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2375 | |
| 2376 | if (!str_restr && type->info.num.range) { |
| 2377 | str_restr = type->info.num.range->expr; |
| 2378 | } |
| 2379 | break; |
| 2380 | case LY_TYPE_STRING: |
| 2381 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2382 | local_umin = __UINT64_C(0); |
| 2383 | local_umax = __UINT64_C(18446744073709551615); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2384 | |
| 2385 | if (!str_restr && type->info.str.length) { |
| 2386 | str_restr = type->info.str.length->expr; |
| 2387 | } |
| 2388 | break; |
| 2389 | default: |
| 2390 | LOGINT; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2391 | return -1; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2392 | } |
| 2393 | |
| 2394 | /* process superior types */ |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2395 | if (type->der) { |
| 2396 | if (resolve_len_ran_interval(NULL, &type->der->type, &intv)) { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2397 | LOGINT; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2398 | return -1; |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2399 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2400 | assert(!intv || (intv->kind == kind)); |
| 2401 | } |
| 2402 | |
| 2403 | if (!str_restr) { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2404 | /* we do not have any restriction, return superior ones */ |
| 2405 | *ret = intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2406 | return EXIT_SUCCESS; |
| 2407 | } |
| 2408 | |
| 2409 | /* adjust local min and max */ |
| 2410 | if (intv) { |
| 2411 | tmp_intv = intv; |
| 2412 | |
| 2413 | if (kind == 0) { |
| 2414 | local_umin = tmp_intv->value.uval.min; |
| 2415 | } else if (kind == 1) { |
| 2416 | local_smin = tmp_intv->value.sval.min; |
| 2417 | } else if (kind == 2) { |
| 2418 | local_fmin = tmp_intv->value.fval.min; |
| 2419 | } |
| 2420 | |
| 2421 | while (tmp_intv->next) { |
| 2422 | tmp_intv = tmp_intv->next; |
| 2423 | } |
| 2424 | |
| 2425 | if (kind == 0) { |
| 2426 | local_umax = tmp_intv->value.uval.max; |
| 2427 | } else if (kind == 1) { |
| 2428 | local_smax = tmp_intv->value.sval.max; |
| 2429 | } else if (kind == 2) { |
| 2430 | local_fmax = tmp_intv->value.fval.max; |
| 2431 | } |
| 2432 | } |
| 2433 | |
| 2434 | /* finally parse our restriction */ |
| 2435 | seg_ptr = str_restr; |
| 2436 | while (1) { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2437 | if (!tmp_local_intv) { |
| 2438 | assert(!local_intv); |
| 2439 | local_intv = malloc(sizeof *local_intv); |
| 2440 | tmp_local_intv = local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2441 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2442 | tmp_local_intv->next = malloc(sizeof *tmp_local_intv); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2443 | tmp_local_intv = tmp_local_intv->next; |
| 2444 | } |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2445 | if (!tmp_local_intv) { |
| 2446 | LOGMEM; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2447 | goto error; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2448 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2449 | |
| 2450 | tmp_local_intv->kind = kind; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2451 | tmp_local_intv->type = type; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2452 | tmp_local_intv->next = NULL; |
| 2453 | |
| 2454 | /* min */ |
| 2455 | ptr = seg_ptr; |
| 2456 | while (isspace(ptr[0])) { |
| 2457 | ++ptr; |
| 2458 | } |
| 2459 | if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) { |
| 2460 | if (kind == 0) { |
| 2461 | tmp_local_intv->value.uval.min = atoll(ptr); |
| 2462 | } else if (kind == 1) { |
| 2463 | tmp_local_intv->value.sval.min = atoll(ptr); |
| 2464 | } else if (kind == 2) { |
| 2465 | tmp_local_intv->value.fval.min = atoll(ptr); |
| 2466 | } |
| 2467 | |
| 2468 | if ((ptr[0] == '+') || (ptr[0] == '-')) { |
| 2469 | ++ptr; |
| 2470 | } |
| 2471 | while (isdigit(ptr[0])) { |
| 2472 | ++ptr; |
| 2473 | } |
| 2474 | } else if (!strncmp(ptr, "min", 3)) { |
| 2475 | if (kind == 0) { |
| 2476 | tmp_local_intv->value.uval.min = local_umin; |
| 2477 | } else if (kind == 1) { |
| 2478 | tmp_local_intv->value.sval.min = local_smin; |
| 2479 | } else if (kind == 2) { |
| 2480 | tmp_local_intv->value.fval.min = local_fmin; |
| 2481 | } |
| 2482 | |
| 2483 | ptr += 3; |
| 2484 | } else if (!strncmp(ptr, "max", 3)) { |
| 2485 | if (kind == 0) { |
| 2486 | tmp_local_intv->value.uval.min = local_umax; |
| 2487 | } else if (kind == 1) { |
| 2488 | tmp_local_intv->value.sval.min = local_smax; |
| 2489 | } else if (kind == 2) { |
| 2490 | tmp_local_intv->value.fval.min = local_fmax; |
| 2491 | } |
| 2492 | |
| 2493 | ptr += 3; |
| 2494 | } else { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2495 | LOGINT; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2496 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2497 | } |
| 2498 | |
| 2499 | while (isspace(ptr[0])) { |
| 2500 | ptr++; |
| 2501 | } |
| 2502 | |
| 2503 | /* no interval or interval */ |
| 2504 | if ((ptr[0] == '|') || !ptr[0]) { |
| 2505 | if (kind == 0) { |
| 2506 | tmp_local_intv->value.uval.max = tmp_local_intv->value.uval.min; |
| 2507 | } else if (kind == 1) { |
| 2508 | tmp_local_intv->value.sval.max = tmp_local_intv->value.sval.min; |
| 2509 | } else if (kind == 2) { |
| 2510 | tmp_local_intv->value.fval.max = tmp_local_intv->value.fval.min; |
| 2511 | } |
| 2512 | } else if (!strncmp(ptr, "..", 2)) { |
| 2513 | /* skip ".." */ |
| 2514 | ptr += 2; |
| 2515 | while (isspace(ptr[0])) { |
| 2516 | ++ptr; |
| 2517 | } |
| 2518 | |
| 2519 | /* max */ |
| 2520 | if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) { |
| 2521 | if (kind == 0) { |
| 2522 | tmp_local_intv->value.uval.max = atoll(ptr); |
| 2523 | } else if (kind == 1) { |
| 2524 | tmp_local_intv->value.sval.max = atoll(ptr); |
| 2525 | } else if (kind == 2) { |
| 2526 | tmp_local_intv->value.fval.max = atoll(ptr); |
| 2527 | } |
| 2528 | } else if (!strncmp(ptr, "max", 3)) { |
| 2529 | if (kind == 0) { |
| 2530 | tmp_local_intv->value.uval.max = local_umax; |
| 2531 | } else if (kind == 1) { |
| 2532 | tmp_local_intv->value.sval.max = local_smax; |
| 2533 | } else if (kind == 2) { |
| 2534 | tmp_local_intv->value.fval.max = local_fmax; |
| 2535 | } |
| 2536 | } else { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2537 | LOGINT; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2538 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2539 | } |
| 2540 | } else { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2541 | LOGINT; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2542 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2543 | } |
| 2544 | |
| 2545 | /* next segment (next OR) */ |
| 2546 | seg_ptr = strchr(seg_ptr, '|'); |
| 2547 | if (!seg_ptr) { |
| 2548 | break; |
| 2549 | } |
| 2550 | seg_ptr++; |
| 2551 | } |
| 2552 | |
| 2553 | /* check local restrictions against superior ones */ |
| 2554 | if (intv) { |
| 2555 | tmp_intv = intv; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2556 | tmp_local_intv = local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2557 | |
| 2558 | while (tmp_local_intv && tmp_intv) { |
| 2559 | /* reuse local variables */ |
| 2560 | if (kind == 0) { |
| 2561 | local_umin = tmp_local_intv->value.uval.min; |
| 2562 | local_umax = tmp_local_intv->value.uval.max; |
| 2563 | |
| 2564 | /* it must be in this interval */ |
| 2565 | if ((local_umin >= tmp_intv->value.uval.min) && (local_umin <= tmp_intv->value.uval.max)) { |
| 2566 | /* this interval is covered, next one */ |
| 2567 | if (local_umax <= tmp_intv->value.uval.max) { |
| 2568 | tmp_local_intv = tmp_local_intv->next; |
| 2569 | continue; |
| 2570 | /* ascending order of restrictions -> fail */ |
| 2571 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2572 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2573 | } |
| 2574 | } |
| 2575 | } else if (kind == 1) { |
| 2576 | local_smin = tmp_local_intv->value.sval.min; |
| 2577 | local_smax = tmp_local_intv->value.sval.max; |
| 2578 | |
| 2579 | if ((local_smin >= tmp_intv->value.sval.min) && (local_smin <= tmp_intv->value.sval.max)) { |
| 2580 | if (local_smax <= tmp_intv->value.sval.max) { |
| 2581 | tmp_local_intv = tmp_local_intv->next; |
| 2582 | continue; |
| 2583 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2584 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2585 | } |
| 2586 | } |
| 2587 | } else if (kind == 2) { |
| 2588 | local_fmin = tmp_local_intv->value.fval.min; |
| 2589 | local_fmax = tmp_local_intv->value.fval.max; |
| 2590 | |
| 2591 | if ((local_fmin >= tmp_intv->value.fval.min) && (local_fmin <= tmp_intv->value.fval.max)) { |
| 2592 | if (local_fmax <= tmp_intv->value.fval.max) { |
| 2593 | tmp_local_intv = tmp_local_intv->next; |
| 2594 | continue; |
| 2595 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2596 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2597 | } |
| 2598 | } |
| 2599 | } |
| 2600 | |
| 2601 | tmp_intv = tmp_intv->next; |
| 2602 | } |
| 2603 | |
| 2604 | /* some interval left uncovered -> fail */ |
| 2605 | if (tmp_local_intv) { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2606 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2607 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2608 | } |
| 2609 | |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2610 | /* append the local intervals to all the intervals of the superior types, return it all */ |
| 2611 | if (intv) { |
| 2612 | for (tmp_intv = intv; tmp_intv->next; tmp_intv = tmp_intv->next); |
| 2613 | tmp_intv->next = local_intv; |
| 2614 | } else { |
| 2615 | intv = local_intv; |
| 2616 | } |
| 2617 | *ret = intv; |
| 2618 | |
| 2619 | return EXIT_SUCCESS; |
| 2620 | |
| 2621 | error: |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2622 | while (intv) { |
| 2623 | tmp_intv = intv->next; |
| 2624 | free(intv); |
| 2625 | intv = tmp_intv; |
| 2626 | } |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2627 | while (local_intv) { |
| 2628 | tmp_local_intv = local_intv->next; |
| 2629 | free(local_intv); |
| 2630 | local_intv = tmp_local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2631 | } |
| 2632 | |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2633 | return -1; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2634 | } |
| 2635 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2636 | /** |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2637 | * @brief Resolve a typedef, return only resolved typedefs if derived. If leafref, it must be |
| 2638 | * resolved for this function to return it. Does not log. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2639 | * |
| 2640 | * @param[in] name Typedef name. |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2641 | * @param[in] mod_name Typedef name module name. |
| 2642 | * @param[in] module Main module. |
| 2643 | * @param[in] parent Parent of the resolved type definition. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2644 | * @param[out] ret Pointer to the resolved typedef. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2645 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2646 | * @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] | 2647 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2648 | int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 2649 | resolve_superior_type(const char *name, const char *mod_name, const struct lys_module *module, |
| 2650 | const struct lys_node *parent, struct lys_tpdf **ret) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2651 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2652 | int i, j; |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2653 | struct lys_tpdf *tpdf, *match; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2654 | int tpdf_size; |
| 2655 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2656 | if (!mod_name) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2657 | /* no prefix, try built-in types */ |
| 2658 | for (i = 1; i < LY_DATA_TYPE_COUNT; i++) { |
| 2659 | if (!strcmp(ly_types[i].def->name, name)) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2660 | if (ret) { |
| 2661 | *ret = ly_types[i].def; |
| 2662 | } |
| 2663 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2664 | } |
| 2665 | } |
| 2666 | } else { |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2667 | if (!strcmp(mod_name, module->name)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2668 | /* prefix refers to the current module, ignore it */ |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2669 | mod_name = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2670 | } |
| 2671 | } |
| 2672 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2673 | if (!mod_name && parent) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2674 | /* search in local typedefs */ |
| 2675 | while (parent) { |
| 2676 | switch (parent->nodetype) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2677 | case LYS_CONTAINER: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 2678 | tpdf_size = ((struct lys_node_container *)parent)->tpdf_size; |
| 2679 | tpdf = ((struct lys_node_container *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2680 | break; |
| 2681 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2682 | case LYS_LIST: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 2683 | tpdf_size = ((struct lys_node_list *)parent)->tpdf_size; |
| 2684 | tpdf = ((struct lys_node_list *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2685 | break; |
| 2686 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2687 | case LYS_GROUPING: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 2688 | tpdf_size = ((struct lys_node_grp *)parent)->tpdf_size; |
| 2689 | tpdf = ((struct lys_node_grp *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2690 | break; |
| 2691 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2692 | case LYS_RPC: |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 2693 | case LYS_ACTION: |
| 2694 | tpdf_size = ((struct lys_node_rpc_action *)parent)->tpdf_size; |
| 2695 | tpdf = ((struct lys_node_rpc_action *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2696 | break; |
| 2697 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2698 | case LYS_NOTIF: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 2699 | tpdf_size = ((struct lys_node_notif *)parent)->tpdf_size; |
| 2700 | tpdf = ((struct lys_node_notif *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2701 | break; |
| 2702 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2703 | case LYS_INPUT: |
| 2704 | case LYS_OUTPUT: |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 2705 | tpdf_size = ((struct lys_node_inout *)parent)->tpdf_size; |
| 2706 | tpdf = ((struct lys_node_inout *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2707 | break; |
| 2708 | |
| 2709 | default: |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 2710 | parent = lys_parent(parent); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2711 | continue; |
| 2712 | } |
| 2713 | |
| 2714 | for (i = 0; i < tpdf_size; i++) { |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 2715 | if (!strcmp(tpdf[i].name, name) && tpdf[i].type.base) { |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2716 | match = &tpdf[i]; |
| 2717 | goto check_leafref; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2718 | } |
| 2719 | } |
| 2720 | |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 2721 | parent = lys_parent(parent); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2722 | } |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2723 | } else { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2724 | /* get module where to search */ |
Michal Vasko | b6729c6 | 2015-10-21 12:09:47 +0200 | [diff] [blame] | 2725 | module = lys_get_import_module(module, NULL, 0, mod_name, 0); |
Michal Vasko | c935fff | 2015-08-17 14:02:06 +0200 | [diff] [blame] | 2726 | if (!module) { |
| 2727 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2728 | } |
| 2729 | } |
| 2730 | |
| 2731 | /* search in top level typedefs */ |
| 2732 | for (i = 0; i < module->tpdf_size; i++) { |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 2733 | if (!strcmp(module->tpdf[i].name, name) && module->tpdf[i].type.base) { |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2734 | match = &module->tpdf[i]; |
| 2735 | goto check_leafref; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2736 | } |
| 2737 | } |
| 2738 | |
| 2739 | /* search in submodules */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2740 | for (i = 0; i < module->inc_size && module->inc[i].submodule; i++) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2741 | for (j = 0; j < module->inc[i].submodule->tpdf_size; j++) { |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 2742 | if (!strcmp(module->inc[i].submodule->tpdf[j].name, name) && module->inc[i].submodule->tpdf[j].type.base) { |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2743 | match = &module->inc[i].submodule->tpdf[j]; |
| 2744 | goto check_leafref; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2745 | } |
| 2746 | } |
| 2747 | } |
| 2748 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2749 | return EXIT_FAILURE; |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2750 | |
| 2751 | check_leafref: |
| 2752 | if (ret) { |
| 2753 | *ret = match; |
| 2754 | } |
| 2755 | if (match->type.base == LY_TYPE_LEAFREF) { |
| 2756 | while (!match->type.info.lref.path) { |
| 2757 | match = match->type.der; |
| 2758 | assert(match); |
| 2759 | } |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2760 | } |
| 2761 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2762 | } |
| 2763 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2764 | /** |
| 2765 | * @brief Check the default \p value of the \p type. Logs directly. |
| 2766 | * |
| 2767 | * @param[in] type Type definition to use. |
| 2768 | * @param[in] value Default value to check. |
Michal Vasko | 5682640 | 2016-03-02 11:11:37 +0100 | [diff] [blame] | 2769 | * @param[in] module Type module. |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2770 | * |
| 2771 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 2772 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2773 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2774 | 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] | 2775 | { |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 2776 | struct lys_tpdf *base_tpdf = NULL; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2777 | struct lyd_node_leaf_list node; |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 2778 | int ret = EXIT_SUCCESS; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2779 | |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 2780 | if (type->base == LY_TYPE_DER) { |
| 2781 | /* the type was not resolved yet, nothing to do for now */ |
| 2782 | return EXIT_FAILURE; |
| 2783 | } |
| 2784 | |
| 2785 | if (!value) { |
| 2786 | /* we do not have a new default value, so is there any to check even, in some base type? */ |
| 2787 | for (base_tpdf = type->der; base_tpdf->type.der; base_tpdf = base_tpdf->type.der) { |
| 2788 | if (base_tpdf->dflt) { |
| 2789 | value = base_tpdf->dflt; |
| 2790 | break; |
| 2791 | } |
| 2792 | } |
| 2793 | |
| 2794 | if (!value) { |
| 2795 | /* no default value, nothing to check, all is well */ |
| 2796 | return EXIT_SUCCESS; |
| 2797 | } |
| 2798 | |
| 2799 | /* 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)? */ |
| 2800 | switch (type->base) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 2801 | case LY_TYPE_IDENT: |
| 2802 | case LY_TYPE_INST: |
| 2803 | case LY_TYPE_LEAFREF: |
| 2804 | case LY_TYPE_BOOL: |
| 2805 | case LY_TYPE_EMPTY: |
| 2806 | /* these have no restrictions, so we would do the exact same work as the unres in the base typedef */ |
| 2807 | return EXIT_SUCCESS; |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 2808 | case LY_TYPE_BITS: |
| 2809 | /* the default value must match the restricted list of values, if the type was restricted */ |
| 2810 | if (type->info.bits.count) { |
| 2811 | break; |
| 2812 | } |
| 2813 | return EXIT_SUCCESS; |
| 2814 | case LY_TYPE_ENUM: |
| 2815 | /* the default value must match the restricted list of values, if the type was restricted */ |
| 2816 | if (type->info.enums.count) { |
| 2817 | break; |
| 2818 | } |
| 2819 | return EXIT_SUCCESS; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 2820 | case LY_TYPE_DEC64: |
| 2821 | if (type->info.dec64.range) { |
| 2822 | break; |
| 2823 | } |
| 2824 | return EXIT_SUCCESS; |
| 2825 | case LY_TYPE_BINARY: |
| 2826 | if (type->info.binary.length) { |
| 2827 | break; |
| 2828 | } |
| 2829 | return EXIT_SUCCESS; |
| 2830 | case LY_TYPE_INT8: |
| 2831 | case LY_TYPE_INT16: |
| 2832 | case LY_TYPE_INT32: |
| 2833 | case LY_TYPE_INT64: |
| 2834 | case LY_TYPE_UINT8: |
| 2835 | case LY_TYPE_UINT16: |
| 2836 | case LY_TYPE_UINT32: |
| 2837 | case LY_TYPE_UINT64: |
| 2838 | if (type->info.num.range) { |
| 2839 | break; |
| 2840 | } |
| 2841 | return EXIT_SUCCESS; |
| 2842 | case LY_TYPE_STRING: |
| 2843 | if (type->info.str.length || type->info.str.patterns) { |
| 2844 | break; |
| 2845 | } |
| 2846 | return EXIT_SUCCESS; |
| 2847 | case LY_TYPE_UNION: |
| 2848 | /* way too much trouble learning whether we need to check the default again, so just do it */ |
| 2849 | break; |
| 2850 | default: |
| 2851 | LOGINT; |
| 2852 | return -1; |
| 2853 | } |
| 2854 | } |
| 2855 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2856 | /* dummy leaf */ |
Radek Krejci | 4fe36bd | 2016-03-17 16:47:16 +0100 | [diff] [blame] | 2857 | memset(&node, 0, sizeof node); |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2858 | node.value_str = value; |
| 2859 | node.value_type = type->base; |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 2860 | node.schema = calloc(1, sizeof (struct lys_node_leaf)); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2861 | if (!node.schema) { |
| 2862 | LOGMEM; |
| 2863 | return -1; |
| 2864 | } |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 2865 | node.schema->name = strdup("fake-default"); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2866 | if (!node.schema->name) { |
| 2867 | LOGMEM; |
Michal Vasko | f49eda0 | 2016-07-21 12:17:01 +0200 | [diff] [blame] | 2868 | free(node.schema); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2869 | return -1; |
| 2870 | } |
Michal Vasko | 5682640 | 2016-03-02 11:11:37 +0100 | [diff] [blame] | 2871 | node.schema->module = module; |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 2872 | memcpy(&((struct lys_node_leaf *)node.schema)->type, type, sizeof *type); |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2873 | |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 2874 | if (type->base == LY_TYPE_LEAFREF) { |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2875 | if (!type->info.lref.target) { |
| 2876 | ret = EXIT_FAILURE; |
| 2877 | goto finish; |
| 2878 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2879 | ret = check_default(&type->info.lref.target->type, value, module); |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2880 | |
| 2881 | } else if ((type->base == LY_TYPE_INST) || (type->base == LY_TYPE_IDENT)) { |
| 2882 | /* it was converted to JSON format before, nothing else sensible we can do */ |
| 2883 | |
| 2884 | } else { |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 2885 | if (lyp_parse_value(&node, NULL, 1)) { |
| 2886 | ret = -1; |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 2887 | if (base_tpdf) { |
| 2888 | /* default value was is defined in some base typedef */ |
| 2889 | if ((type->base == LY_TYPE_BITS && type->der->type.der) || |
| 2890 | (type->base == LY_TYPE_ENUM && type->der->type.der)) { |
| 2891 | /* we have refined bits/enums */ |
| 2892 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, |
| 2893 | "Invalid value \"%s\" of the default statement inherited to \"%s\" from \"%s\" base type.", |
| 2894 | value, type->parent->name, base_tpdf->name); |
| 2895 | } |
| 2896 | } |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 2897 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2898 | } |
| 2899 | |
| 2900 | finish: |
| 2901 | if (node.value_type == LY_TYPE_BITS) { |
| 2902 | free(node.value.bit); |
| 2903 | } |
| 2904 | free((char *)node.schema->name); |
| 2905 | free(node.schema); |
| 2906 | |
| 2907 | return ret; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2908 | } |
| 2909 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2910 | /** |
| 2911 | * @brief Check a key for mandatory attributes. Logs directly. |
| 2912 | * |
| 2913 | * @param[in] key The key to check. |
| 2914 | * @param[in] flags What flags to check. |
| 2915 | * @param[in] list The list of all the keys. |
| 2916 | * @param[in] index Index of the key in the key list. |
| 2917 | * @param[in] name The name of the keys. |
| 2918 | * @param[in] len The name length. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2919 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2920 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2921 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2922 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2923 | 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] | 2924 | { |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 2925 | struct lys_node_leaf *key = list->keys[index]; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2926 | char *dup = NULL; |
| 2927 | int j; |
| 2928 | |
| 2929 | /* existence */ |
| 2930 | if (!key) { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 2931 | if (name[len] != '\0') { |
| 2932 | dup = strdup(name); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2933 | if (!dup) { |
| 2934 | LOGMEM; |
| 2935 | return -1; |
| 2936 | } |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 2937 | dup[len] = '\0'; |
| 2938 | name = dup; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2939 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2940 | LOGVAL(LYE_KEY_MISS, LY_VLOG_LYS, list, name); |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 2941 | free(dup); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2942 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2943 | } |
| 2944 | |
| 2945 | /* uniqueness */ |
| 2946 | for (j = index - 1; j >= 0; j--) { |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 2947 | if (key == list->keys[j]) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2948 | LOGVAL(LYE_KEY_DUP, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2949 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2950 | } |
| 2951 | } |
| 2952 | |
| 2953 | /* key is a leaf */ |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2954 | if (key->nodetype != LYS_LEAF) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2955 | LOGVAL(LYE_KEY_NLEAF, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2956 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2957 | } |
| 2958 | |
| 2959 | /* type of the leaf is not built-in empty */ |
| 2960 | if (key->type.base == LY_TYPE_EMPTY) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2961 | LOGVAL(LYE_KEY_TYPE, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2962 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2963 | } |
| 2964 | |
| 2965 | /* config attribute is the same as of the list */ |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 2966 | if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2967 | LOGVAL(LYE_KEY_CONFIG, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2968 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2969 | } |
| 2970 | |
Radek Krejci | 55e2cdc | 2016-03-11 13:51:09 +0100 | [diff] [blame] | 2971 | /* key is not placed from augment */ |
| 2972 | if (key->parent->nodetype == LYS_AUGMENT) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2973 | LOGVAL(LYE_KEY_MISS, LY_VLOG_LYS, key, key->name); |
| 2974 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, key, "Key inserted from augment."); |
Radek Krejci | 55e2cdc | 2016-03-11 13:51:09 +0100 | [diff] [blame] | 2975 | return -1; |
| 2976 | } |
| 2977 | |
Radek Krejci | 3f21ada | 2016-08-01 13:34:31 +0200 | [diff] [blame] | 2978 | /* key is not when/if-feature -conditional */ |
| 2979 | j = 0; |
| 2980 | if (key->when || (key->iffeature_size && (j = 1))) { |
| 2981 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, key, j ? "if-feature" : "when", "leaf"); |
| 2982 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, key, "Key definition cannot depend on a \"%s\" condition.", |
| 2983 | j ? "if-feature" : "when"); |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2984 | return -1; |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2985 | } |
| 2986 | |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 2987 | return EXIT_SUCCESS; |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 2988 | } |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2989 | |
| 2990 | /** |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2991 | * @brief Resolve (test the target exists) unique. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2992 | * |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2993 | * @param[in] parent The parent node of the unique structure. |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 2994 | * @param[in] uniq_str_path One path from the unique string. |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2995 | * |
| 2996 | * @return EXIT_SUCCESS on succes, EXIT_FAILURE on forward reference, -1 on error. |
| 2997 | */ |
| 2998 | int |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 2999 | 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] | 3000 | { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3001 | int rc; |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3002 | const struct lys_node *leaf = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3003 | |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 3004 | 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] | 3005 | if (rc || !leaf) { |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3006 | if (rc) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3007 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3008 | if (rc > 0) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3009 | 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] | 3010 | } else if (rc == -2) { |
Michal Vasko | c66c6d8 | 2016-04-12 11:37:31 +0200 | [diff] [blame] | 3011 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, parent, "Unique argument references list."); |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3012 | } |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3013 | rc = -1; |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3014 | } else { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3015 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
| 3016 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, parent, "Target leaf not found."); |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3017 | rc = EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3018 | } |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3019 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3020 | } |
Michal Vasko | 9bb061b | 2016-02-12 11:00:19 +0100 | [diff] [blame] | 3021 | if (leaf->nodetype != LYS_LEAF) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3022 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
| 3023 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, parent, "Target is not a leaf."); |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3024 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3025 | } |
| 3026 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3027 | /* check status */ |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3028 | 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] | 3029 | return -1; |
| 3030 | } |
| 3031 | |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3032 | /* check that all unique's targets are of the same config type */ |
| 3033 | if (*trg_type) { |
| 3034 | if (((*trg_type == 1) && (leaf->flags & LYS_CONFIG_R)) || ((*trg_type == 2) && (leaf->flags & LYS_CONFIG_W))) { |
| 3035 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
| 3036 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, parent, |
| 3037 | "Leaf \"%s\" referenced in unique statement is config %s, but previous referenced leaf is config %s.", |
| 3038 | uniq_str_path, *trg_type == 1 ? "false" : "true", *trg_type == 1 ? "true" : "false"); |
| 3039 | return -1; |
| 3040 | } |
| 3041 | } else { |
| 3042 | /* first unique */ |
| 3043 | if (leaf->flags & LYS_CONFIG_W) { |
| 3044 | *trg_type = 1; |
| 3045 | } else { |
| 3046 | *trg_type = 2; |
| 3047 | } |
| 3048 | } |
| 3049 | |
Radek Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame] | 3050 | /* set leaf's unique flag */ |
| 3051 | ((struct lys_node_leaf *)leaf)->flags |= LYS_UNIQUE; |
| 3052 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3053 | return EXIT_SUCCESS; |
| 3054 | |
| 3055 | error: |
| 3056 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3057 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3058 | } |
| 3059 | |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 3060 | void |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3061 | unres_data_del(struct unres_data *unres, uint32_t i) |
| 3062 | { |
| 3063 | /* there are items after the one deleted */ |
| 3064 | if (i+1 < unres->count) { |
| 3065 | /* we only move the data, memory is left allocated, why bother */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3066 | 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] | 3067 | |
| 3068 | /* deleting the last item */ |
| 3069 | } else if (i == 0) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3070 | free(unres->node); |
| 3071 | unres->node = NULL; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3072 | } |
| 3073 | |
| 3074 | /* if there are no items after and it is not the last one, just move the counter */ |
| 3075 | --unres->count; |
| 3076 | } |
| 3077 | |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3078 | /** |
| 3079 | * @brief Resolve (find) a data node from a specific module. Does not log. |
| 3080 | * |
| 3081 | * @param[in] mod Module to search in. |
| 3082 | * @param[in] name Name of the data node. |
| 3083 | * @param[in] nam_len Length of the name. |
| 3084 | * @param[in] start Data node to start the search from. |
| 3085 | * @param[in,out] parents Resolved nodes. If there are some parents, |
| 3086 | * they are replaced (!!) with the resolvents. |
| 3087 | * |
Michal Vasko | 2471e7f | 2016-04-11 11:00:15 +0200 | [diff] [blame] | 3088 | * @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] | 3089 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3090 | static int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3091 | 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] | 3092 | { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3093 | struct lyd_node *node; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3094 | int flag; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3095 | uint32_t i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3096 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3097 | if (!parents->count) { |
| 3098 | parents->count = 1; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3099 | parents->node = malloc(sizeof *parents->node); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3100 | if (!parents->node) { |
| 3101 | LOGMEM; |
Michal Vasko | 2471e7f | 2016-04-11 11:00:15 +0200 | [diff] [blame] | 3102 | return -1; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3103 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3104 | parents->node[0] = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3105 | } |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3106 | for (i = 0; i < parents->count;) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3107 | if (parents->node[i] && (parents->node[i]->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYXML))) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3108 | /* skip */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3109 | ++i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3110 | continue; |
| 3111 | } |
| 3112 | flag = 0; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3113 | LY_TREE_FOR(parents->node[i] ? parents->node[i]->child : start, node) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3114 | if (node->schema->module == mod && !strncmp(node->schema->name, name, nam_len) |
| 3115 | && node->schema->name[nam_len] == '\0') { |
| 3116 | /* matching target */ |
| 3117 | if (!flag) { |
Michal Vasko | 9a47e12 | 2015-09-03 14:26:32 +0200 | [diff] [blame] | 3118 | /* put node instead of the current parent */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3119 | parents->node[i] = node; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3120 | flag = 1; |
| 3121 | } else { |
Michal Vasko | 9a47e12 | 2015-09-03 14:26:32 +0200 | [diff] [blame] | 3122 | /* multiple matching, so create a new node */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3123 | ++parents->count; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3124 | parents->node = ly_realloc(parents->node, parents->count * sizeof *parents->node); |
| 3125 | if (!parents->node) { |
| 3126 | return EXIT_FAILURE; |
| 3127 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3128 | parents->node[parents->count-1] = node; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3129 | ++i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3130 | } |
| 3131 | } |
| 3132 | } |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3133 | |
| 3134 | if (!flag) { |
| 3135 | /* remove item from the parents list */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3136 | unres_data_del(parents, i); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3137 | } else { |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3138 | ++i; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3139 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3140 | } |
| 3141 | |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3142 | return parents->count ? EXIT_SUCCESS : EXIT_FAILURE; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3143 | } |
| 3144 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3145 | /** |
| 3146 | * @brief Resolve (find) a data node. Does not log. |
| 3147 | * |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3148 | * @param[in] mod_name Module name of the data node. |
| 3149 | * @param[in] mod_name_len Length of the module name. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3150 | * @param[in] name Name of the data node. |
| 3151 | * @param[in] nam_len Length of the name. |
| 3152 | * @param[in] start Data node to start the search from. |
| 3153 | * @param[in,out] parents Resolved nodes. If there are some parents, |
| 3154 | * they are replaced (!!) with the resolvents. |
| 3155 | * |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3156 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 otherwise. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3157 | */ |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3158 | static int |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3159 | 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] | 3160 | struct unres_data *parents) |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3161 | { |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3162 | const struct lys_module *mod; |
Michal Vasko | 31fc367 | 2015-10-21 12:08:13 +0200 | [diff] [blame] | 3163 | char *str; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3164 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3165 | assert(start); |
| 3166 | |
Michal Vasko | 31fc367 | 2015-10-21 12:08:13 +0200 | [diff] [blame] | 3167 | if (mod_name) { |
| 3168 | /* we have mod_name, find appropriate module */ |
| 3169 | str = strndup(mod_name, mod_name_len); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3170 | if (!str) { |
| 3171 | LOGMEM; |
| 3172 | return -1; |
| 3173 | } |
Michal Vasko | 31fc367 | 2015-10-21 12:08:13 +0200 | [diff] [blame] | 3174 | mod = ly_ctx_get_module(start->schema->module->ctx, str, NULL); |
| 3175 | free(str); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3176 | if (!mod) { |
| 3177 | /* invalid prefix */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3178 | return -1; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3179 | } |
| 3180 | } else { |
| 3181 | /* no prefix, module is the same as of current node */ |
| 3182 | mod = start->schema->module; |
| 3183 | } |
| 3184 | |
| 3185 | return resolve_data(mod, name, name_len, start, parents); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3186 | } |
| 3187 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3188 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3189 | * @brief Resolve a path predicate (leafref) in JSON data context. Logs directly |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3190 | * only specific errors, general no-resolvent error is left to the caller. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3191 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3192 | * @param[in] pred Predicate to use. |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3193 | * @param[in] node Node from which the predicate is being resolved |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3194 | * @param[in,out] node_match Nodes satisfying the restriction |
| 3195 | * without the predicate. Nodes not |
| 3196 | * satisfying the predicate are removed. |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3197 | * @param[out] parsed Number of characters parsed, negative on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3198 | * |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3199 | * @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] | 3200 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3201 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3202 | 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] | 3203 | int *parsed) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3204 | { |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3205 | /* ... /node[source = destination] ... */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3206 | struct unres_data source_match, dest_match; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3207 | const char *path_key_expr, *source, *sour_pref, *dest, *dest_pref; |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3208 | int pke_len, sour_len, sour_pref_len, dest_len, dest_pref_len, parsed_loc = 0, pke_parsed = 0; |
| 3209 | int has_predicate, dest_parent_times, i, rc; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3210 | uint32_t j; |
Radek Krejci | 0c2fa3a | 2016-06-13 15:18:03 +0200 | [diff] [blame] | 3211 | struct lyd_node_leaf_list *leaf_dst, *leaf_src; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3212 | |
| 3213 | source_match.count = 1; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3214 | source_match.node = malloc(sizeof *source_match.node); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3215 | if (!source_match.node) { |
| 3216 | LOGMEM; |
| 3217 | return -1; |
| 3218 | } |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3219 | dest_match.count = 1; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3220 | dest_match.node = malloc(sizeof *dest_match.node); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3221 | if (!dest_match.node) { |
| 3222 | LOGMEM; |
| 3223 | return -1; |
| 3224 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3225 | |
| 3226 | do { |
| 3227 | if ((i = parse_path_predicate(pred, &sour_pref, &sour_pref_len, &source, &sour_len, &path_key_expr, |
| 3228 | &pke_len, &has_predicate)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3229 | LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, pred[-i], &pred[-i]); |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3230 | rc = -1; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3231 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3232 | } |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3233 | parsed_loc += i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3234 | pred += i; |
| 3235 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3236 | for (j = 0; j < node_match->count;) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3237 | /* source */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3238 | source_match.node[0] = node_match->node[j]; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3239 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3240 | /* must be leaf (key of a list) */ |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3241 | 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] | 3242 | &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] | 3243 | i = 0; |
| 3244 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3245 | } |
| 3246 | |
| 3247 | /* destination */ |
Radek Krejci | 0c2fa3a | 2016-06-13 15:18:03 +0200 | [diff] [blame] | 3248 | dest_match.node[0] = node; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3249 | dest_parent_times = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3250 | if ((i = parse_path_key_expr(path_key_expr, &dest_pref, &dest_pref_len, &dest, &dest_len, |
| 3251 | &dest_parent_times)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3252 | 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] | 3253 | rc = -1; |
| 3254 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3255 | } |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3256 | pke_parsed = i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3257 | for (i = 0; i < dest_parent_times; ++i) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3258 | dest_match.node[0] = dest_match.node[0]->parent; |
| 3259 | if (!dest_match.node[0]) { |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3260 | i = 0; |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3261 | rc = EXIT_FAILURE; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3262 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3263 | } |
| 3264 | } |
| 3265 | while (1) { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3266 | 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] | 3267 | &dest_match)) || (dest_match.count != 1)) { |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3268 | i = 0; |
| 3269 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3270 | } |
| 3271 | |
| 3272 | if (pke_len == pke_parsed) { |
| 3273 | break; |
| 3274 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3275 | 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] | 3276 | &dest_parent_times)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3277 | 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] | 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 | } |
| 3281 | pke_parsed += i; |
| 3282 | } |
| 3283 | |
| 3284 | /* check match between source and destination nodes */ |
Radek Krejci | 0c2fa3a | 2016-06-13 15:18:03 +0200 | [diff] [blame] | 3285 | leaf_dst = (struct lyd_node_leaf_list *)dest_match.node[0]; |
| 3286 | while (leaf_dst->value_type == LY_TYPE_LEAFREF) { |
| 3287 | leaf_dst = (struct lyd_node_leaf_list *)leaf_dst->value.leafref; |
| 3288 | } |
| 3289 | leaf_src = (struct lyd_node_leaf_list *)source_match.node[0]; |
| 3290 | while (leaf_src->value_type == LY_TYPE_LEAFREF) { |
| 3291 | leaf_src = (struct lyd_node_leaf_list *)leaf_src->value.leafref; |
| 3292 | } |
| 3293 | if (leaf_src->value_type != leaf_dst->value_type) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3294 | goto remove_leafref; |
| 3295 | } |
| 3296 | |
Radek Krejci | 0c2fa3a | 2016-06-13 15:18:03 +0200 | [diff] [blame] | 3297 | if (!ly_strequal(leaf_src->value_str, leaf_dst->value_str, 1)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3298 | goto remove_leafref; |
| 3299 | } |
| 3300 | |
| 3301 | /* leafref is ok, continue check with next leafref */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3302 | ++j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3303 | continue; |
| 3304 | |
| 3305 | remove_leafref: |
| 3306 | /* does not fulfill conditions, remove leafref record */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3307 | unres_data_del(node_match, j); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3308 | } |
| 3309 | } while (has_predicate); |
| 3310 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3311 | free(source_match.node); |
| 3312 | free(dest_match.node); |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3313 | if (parsed) { |
| 3314 | *parsed = parsed_loc; |
| 3315 | } |
| 3316 | return EXIT_SUCCESS; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3317 | |
| 3318 | error: |
| 3319 | |
| 3320 | if (source_match.count) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3321 | free(source_match.node); |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3322 | } |
| 3323 | if (dest_match.count) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3324 | free(dest_match.node); |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3325 | } |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3326 | if (parsed) { |
| 3327 | *parsed = -parsed_loc+i; |
| 3328 | } |
| 3329 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3330 | } |
| 3331 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3332 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3333 | * @brief Resolve a path (leafref) in JSON data context. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3334 | * |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3335 | * @param[in] node Leafref data node. |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3336 | * @param[in] path Path of the leafref. |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3337 | * @param[out] ret Matching nodes. Expects an empty, but allocated structure. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3338 | * |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3339 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 otherwise. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3340 | */ |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3341 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3342 | 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] | 3343 | { |
Radek Krejci | 71b795b | 2015-08-10 16:20:39 +0200 | [diff] [blame] | 3344 | struct lyd_node *data = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3345 | const char *prefix, *name; |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3346 | int pref_len, nam_len, has_predicate, parent_times, i, parsed, rc; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3347 | uint32_t j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3348 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3349 | assert(node && path && ret && !ret->count); |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3350 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3351 | parent_times = 0; |
Michal Vasko | d917334 | 2015-08-17 14:35:36 +0200 | [diff] [blame] | 3352 | parsed = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3353 | |
| 3354 | /* searching for nodeset */ |
| 3355 | do { |
| 3356 | 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] | 3357 | LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, path[-i], &path[-i]); |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3358 | rc = -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3359 | goto error; |
| 3360 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3361 | path += i; |
Michal Vasko | d917334 | 2015-08-17 14:35:36 +0200 | [diff] [blame] | 3362 | parsed += i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3363 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3364 | if (!ret->count) { |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 3365 | if (parent_times != -1) { |
| 3366 | ret->count = 1; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3367 | ret->node = calloc(1, sizeof *ret->node); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3368 | if (!ret->node) { |
| 3369 | LOGMEM; |
Radek Krejci | 5050173 | 2016-01-07 13:06:39 +0100 | [diff] [blame] | 3370 | rc = -1; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3371 | goto error; |
| 3372 | } |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 3373 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3374 | for (i = 0; i < parent_times; ++i) { |
| 3375 | /* relative path */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3376 | if (!ret->count) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3377 | /* error, too many .. */ |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3378 | LOGVAL(LYE_INVAL, LY_VLOG_LYD, node, path, node->schema->name); |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3379 | rc = -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3380 | goto error; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3381 | } else if (!ret->node[0]) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3382 | /* first .. */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3383 | data = ret->node[0] = node->parent; |
| 3384 | } else if (!ret->node[0]->parent) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3385 | /* we are in root */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3386 | ret->count = 0; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3387 | free(ret->node); |
| 3388 | ret->node = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3389 | } else { |
| 3390 | /* multiple .. */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3391 | data = ret->node[0] = ret->node[0]->parent; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3392 | } |
| 3393 | } |
| 3394 | |
| 3395 | /* absolute path */ |
| 3396 | if (parent_times == -1) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3397 | for (data = node; data->parent; data = data->parent); |
Michal Vasko | db9323c | 2015-10-16 10:32:44 +0200 | [diff] [blame] | 3398 | /* we're still parsing it and the pointer is not correct yet */ |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 3399 | if (data->prev) { |
| 3400 | for (; data->prev->next; data = data->prev); |
| 3401 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3402 | } |
| 3403 | } |
| 3404 | |
| 3405 | /* node identifier */ |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3406 | 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] | 3407 | if (rc == -1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3408 | LOGVAL(LYE_INELEM_LEN, LY_VLOG_LYD, node, nam_len, name); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3409 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3410 | goto error; |
| 3411 | } |
| 3412 | |
| 3413 | if (has_predicate) { |
| 3414 | /* we have predicate, so the current results must be lists */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3415 | for (j = 0; j < ret->count;) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3416 | if (ret->node[j]->schema->nodetype == LYS_LIST && |
| 3417 | ((struct lys_node_list *)ret->node[0]->schema)->keys) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3418 | /* leafref is ok, continue check with next leafref */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3419 | ++j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3420 | continue; |
| 3421 | } |
| 3422 | |
| 3423 | /* does not fulfill conditions, remove leafref record */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3424 | unres_data_del(ret, j); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3425 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3426 | if ((rc = resolve_path_predicate_data(path, node, ret, &i))) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 3427 | if (rc == -1) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3428 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYD, node, "leafref", path); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3429 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3430 | goto error; |
| 3431 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3432 | path += i; |
Michal Vasko | d917334 | 2015-08-17 14:35:36 +0200 | [diff] [blame] | 3433 | parsed += i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3434 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3435 | if (!ret->count) { |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3436 | rc = EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3437 | goto error; |
| 3438 | } |
| 3439 | } |
| 3440 | } while (path[0] != '\0'); |
| 3441 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3442 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3443 | |
| 3444 | error: |
| 3445 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3446 | free(ret->node); |
| 3447 | ret->node = NULL; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3448 | ret->count = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3449 | |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3450 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3451 | } |
| 3452 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3453 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3454 | * @brief Resolve a path (leafref) predicate in JSON schema context. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3455 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3456 | * @param[in] path Path to use. |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3457 | * @param[in] context_node Predicate context node (where the predicate is placed). |
| 3458 | * @param[in] parent Path context node (where the path begins/is placed). |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3459 | * |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3460 | * @return 0 on forward reference, otherwise the number |
| 3461 | * of characters successfully parsed, |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3462 | * positive on success, negative on failure. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3463 | */ |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3464 | static int |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3465 | 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] | 3466 | struct lys_node *parent) |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3467 | { |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3468 | const struct lys_node *src_node, *dst_node; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3469 | const char *path_key_expr, *source, *sour_pref, *dest, *dest_pref; |
| 3470 | 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] | 3471 | int has_predicate, dest_parent_times = 0, i, rc; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3472 | |
| 3473 | do { |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3474 | 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] | 3475 | &pke_len, &has_predicate)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3476 | 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] | 3477 | return -parsed+i; |
| 3478 | } |
| 3479 | parsed += i; |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3480 | path += i; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3481 | |
Michal Vasko | 5809090 | 2015-08-13 14:04:15 +0200 | [diff] [blame] | 3482 | /* source (must be leaf) */ |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3483 | if (!sour_pref) { |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3484 | sour_pref = context_node->module->name; |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3485 | } |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3486 | 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] | 3487 | LYS_LEAF | LYS_AUGMENT, &src_node); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3488 | if (rc) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3489 | 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] | 3490 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3491 | } |
| 3492 | |
| 3493 | /* destination */ |
| 3494 | if ((i = parse_path_key_expr(path_key_expr, &dest_pref, &dest_pref_len, &dest, &dest_len, |
| 3495 | &dest_parent_times)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3496 | 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] | 3497 | return -parsed; |
| 3498 | } |
| 3499 | pke_parsed += i; |
| 3500 | |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3501 | for (i = 0, dst_node = parent; i < dest_parent_times; ++i) { |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3502 | if (!dst_node) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3503 | 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] | 3504 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3505 | } |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 3506 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 3507 | * all schema nodes that cannot be instantiated in data tree */ |
| 3508 | for (dst_node = lys_parent(dst_node); |
| 3509 | dst_node && !(dst_node->nodetype & (LYS_CONTAINER | LYS_LIST)); |
| 3510 | dst_node = lys_parent(dst_node)); |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3511 | } |
| 3512 | while (1) { |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3513 | if (!dest_pref) { |
| 3514 | dest_pref = dst_node->module->name; |
| 3515 | } |
| 3516 | 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] | 3517 | LYS_CONTAINER | LYS_LIST | LYS_LEAF | LYS_AUGMENT, &dst_node); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3518 | if (rc) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3519 | 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] | 3520 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3521 | } |
| 3522 | |
| 3523 | if (pke_len == pke_parsed) { |
| 3524 | break; |
| 3525 | } |
| 3526 | |
| 3527 | if ((i = parse_path_key_expr(path_key_expr+pke_parsed, &dest_pref, &dest_pref_len, &dest, &dest_len, |
| 3528 | &dest_parent_times)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3529 | LOGVAL(LYE_INCHAR, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3530 | (path_key_expr+pke_parsed)[-i], (path_key_expr+pke_parsed)-i); |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3531 | return -parsed; |
| 3532 | } |
| 3533 | pke_parsed += i; |
| 3534 | } |
| 3535 | |
| 3536 | /* check source - dest match */ |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3537 | if (dst_node->nodetype != LYS_LEAF) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3538 | 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] | 3539 | LOGVAL(LYE_SPEC, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, |
| 3540 | "Destination node is not a leaf, but %s.", strnodetype(dst_node->nodetype)); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3541 | return -parsed; |
| 3542 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3543 | } while (has_predicate); |
| 3544 | |
| 3545 | return parsed; |
| 3546 | } |
| 3547 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3548 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3549 | * @brief Resolve a path (leafref) in JSON schema context. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3550 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3551 | * @param[in] path Path to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3552 | * @param[in] parent_node Parent of the leafref. |
Radek Krejci | 2f12f85 | 2016-01-08 12:59:57 +0100 | [diff] [blame] | 3553 | * @param[in] parent_tpdf Flag if the parent node is actually typedef, in that case the path |
| 3554 | * has to contain absolute path |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3555 | * @param[out] ret Pointer to the resolved schema node. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3556 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3557 | * @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] | 3558 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3559 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3560 | 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] | 3561 | const struct lys_node **ret) |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3562 | { |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3563 | const struct lys_node *node; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 3564 | const struct lys_module *mod; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3565 | const char *id, *prefix, *name; |
| 3566 | int pref_len, nam_len, parent_times, has_predicate; |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3567 | int i, first_iter, rc; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3568 | |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3569 | first_iter = 1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3570 | parent_times = 0; |
| 3571 | id = path; |
| 3572 | |
| 3573 | do { |
| 3574 | 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] | 3575 | 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] | 3576 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3577 | } |
| 3578 | id += i; |
| 3579 | |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3580 | if (first_iter) { |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3581 | if (parent_times == -1) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 3582 | /* resolve prefix of the module */ |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3583 | mod = lys_get_import_module(parent->module, NULL, 0, prefix, pref_len); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 3584 | /* get start node */ |
| 3585 | node = mod ? mod->data : NULL; |
Michal Vasko | 5809090 | 2015-08-13 14:04:15 +0200 | [diff] [blame] | 3586 | if (!node) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3587 | LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, |
| 3588 | "leafref", path); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3589 | return EXIT_FAILURE; |
Michal Vasko | 5809090 | 2015-08-13 14:04:15 +0200 | [diff] [blame] | 3590 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3591 | } else if (parent_times > 0) { |
Radek Krejci | 2f12f85 | 2016-01-08 12:59:57 +0100 | [diff] [blame] | 3592 | if (parent_tpdf) { |
| 3593 | /* 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] | 3594 | LOGVAL(LYE_NORESOLV, 0, NULL, "leafref", path); |
Radek Krejci | 2f12f85 | 2016-01-08 12:59:57 +0100 | [diff] [blame] | 3595 | return -1; |
| 3596 | } |
| 3597 | |
Michal Vasko | df053d1 | 2016-07-21 13:33:31 +0200 | [diff] [blame] | 3598 | /* node is the parent already, skip one ".." */ |
| 3599 | for (i = 1, node = parent; i < parent_times; i++) { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 3600 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 3601 | * all schema nodes that cannot be instantiated in data tree */ |
| 3602 | for (node = lys_parent(node); |
| 3603 | node && !(node->nodetype & (LYS_CONTAINER | LYS_LIST)); |
| 3604 | node = lys_parent(node)); |
| 3605 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3606 | if (!node) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3607 | LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, |
| 3608 | "leafref", path); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3609 | return EXIT_FAILURE; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3610 | } |
Michal Vasko | 5809090 | 2015-08-13 14:04:15 +0200 | [diff] [blame] | 3611 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3612 | } |
Michal Vasko | e01eca5 | 2015-08-13 14:42:02 +0200 | [diff] [blame] | 3613 | } else { |
| 3614 | LOGINT; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3615 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3616 | } |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3617 | |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3618 | first_iter = 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3619 | } else { |
Michal Vasko | 7dc71d0 | 2016-03-15 10:42:28 +0100 | [diff] [blame] | 3620 | /* move down the tree, if possible */ |
| 3621 | if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYXML)) { |
Michal Vasko | 2f5aceb | 2016-03-22 10:24:14 +0100 | [diff] [blame] | 3622 | 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] | 3623 | return -1; |
| 3624 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3625 | node = node->child; |
| 3626 | } |
| 3627 | |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 3628 | if (!prefix) { |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3629 | prefix = lys_node_module(parent)->name; |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 3630 | } |
| 3631 | |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3632 | 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] | 3633 | if (rc) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3634 | 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] | 3635 | return EXIT_FAILURE; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3636 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3637 | |
| 3638 | if (has_predicate) { |
| 3639 | /* we have predicate, so the current result must be list */ |
| 3640 | if (node->nodetype != LYS_LIST) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3641 | 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] | 3642 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3643 | } |
| 3644 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3645 | i = resolve_path_predicate_schema(id, node, parent); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3646 | if (!i) { |
Michal Vasko | f9664da | 2015-08-24 15:03:30 +0200 | [diff] [blame] | 3647 | return EXIT_FAILURE; |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3648 | } else if (i < 0) { |
| 3649 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3650 | } |
| 3651 | id += i; |
| 3652 | } |
| 3653 | } while (id[0]); |
| 3654 | |
Michal Vasko | ca91768 | 2016-07-25 11:00:37 +0200 | [diff] [blame] | 3655 | /* the target must be leaf or leaf-list (in YANG 1.1 only) */ |
| 3656 | 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] | 3657 | 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] | 3658 | return -1; |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 3659 | } |
| 3660 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3661 | /* check status */ |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3662 | if (lyp_check_status(parent->flags, parent->module, parent->name, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3663 | node->flags, node->module, node->name, node)) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3664 | return -1; |
| 3665 | } |
| 3666 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3667 | if (ret) { |
| 3668 | *ret = node; |
| 3669 | } |
| 3670 | return EXIT_SUCCESS; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3671 | } |
| 3672 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3673 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3674 | * @brief Resolve instance-identifier predicate in JSON data format. |
| 3675 | * Does not log. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3676 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3677 | * @param[in] pred Predicate to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3678 | * @param[in,out] node_match Nodes matching the restriction without |
| 3679 | * the predicate. Nodes not satisfying |
| 3680 | * the predicate are removed. |
| 3681 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3682 | * @return Number of characters successfully parsed, |
| 3683 | * positive on success, negative on failure. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3684 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3685 | static int |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3686 | resolve_predicate(const char *pred, struct unres_data *node_match) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3687 | { |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3688 | /* ... /node[target = value] ... */ |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3689 | struct unres_data target_match; |
| 3690 | struct ly_ctx *ctx; |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3691 | const struct lys_module *mod; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3692 | const char *model, *name, *value; |
| 3693 | char *str; |
| 3694 | int mod_len, nam_len, val_len, i, has_predicate, cur_idx, idx, parsed; |
| 3695 | uint32_t j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3696 | |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3697 | assert(pred && node_match->count); |
| 3698 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3699 | ctx = node_match->node[0]->schema->module->ctx; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3700 | idx = -1; |
| 3701 | parsed = 0; |
| 3702 | |
| 3703 | do { |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3704 | 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] | 3705 | return -parsed+i; |
| 3706 | } |
| 3707 | parsed += i; |
| 3708 | pred += i; |
| 3709 | |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3710 | /* pos */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3711 | if (isdigit(name[0])) { |
| 3712 | idx = atoi(name); |
| 3713 | } |
| 3714 | |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3715 | for (cur_idx = 0, j = 0; j < node_match->count; ++cur_idx) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3716 | /* target */ |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3717 | memset(&target_match, 0, sizeof target_match); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3718 | if ((name[0] == '.') || !value) { |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3719 | target_match.count = 1; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3720 | target_match.node = malloc(sizeof *target_match.node); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3721 | if (!target_match.node) { |
| 3722 | LOGMEM; |
| 3723 | return -1; |
| 3724 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3725 | target_match.node[0] = node_match->node[j]; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3726 | } else { |
| 3727 | str = strndup(model, mod_len); |
| 3728 | mod = ly_ctx_get_module(ctx, str, NULL); |
| 3729 | free(str); |
| 3730 | |
Radek Krejci | 804836a | 2016-02-03 10:39:55 +0100 | [diff] [blame] | 3731 | 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] | 3732 | goto remove_instid; |
| 3733 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3734 | } |
| 3735 | |
| 3736 | /* check that we have the correct type */ |
| 3737 | if (name[0] == '.') { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3738 | if (node_match->node[j]->schema->nodetype != LYS_LEAFLIST) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3739 | goto remove_instid; |
| 3740 | } |
| 3741 | } else if (value) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3742 | if (node_match->node[j]->schema->nodetype != LYS_LIST) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3743 | goto remove_instid; |
| 3744 | } |
| 3745 | } |
| 3746 | |
Michal Vasko | 83a6c46 | 2015-10-08 16:43:53 +0200 | [diff] [blame] | 3747 | if ((value && (strncmp(((struct lyd_node_leaf_list *)target_match.node[0])->value_str, value, val_len) |
| 3748 | || ((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] | 3749 | || (!value && (idx != cur_idx))) { |
| 3750 | goto remove_instid; |
| 3751 | } |
| 3752 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3753 | free(target_match.node); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3754 | |
| 3755 | /* leafref is ok, continue check with next leafref */ |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3756 | ++j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3757 | continue; |
| 3758 | |
| 3759 | remove_instid: |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3760 | free(target_match.node); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3761 | |
| 3762 | /* does not fulfill conditions, remove leafref record */ |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3763 | unres_data_del(node_match, j); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3764 | } |
| 3765 | } while (has_predicate); |
| 3766 | |
| 3767 | return parsed; |
| 3768 | } |
| 3769 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3770 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3771 | * @brief Resolve instance-identifier in JSON data format. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3772 | * |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3773 | * @param[in] data Data node where the path is used |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3774 | * @param[in] path Instance-identifier node value. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3775 | * |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3776 | * @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] | 3777 | */ |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3778 | static struct lyd_node * |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3779 | resolve_instid(struct lyd_node *data, const char *path) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3780 | { |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3781 | int i = 0, j; |
| 3782 | struct lyd_node *result = NULL; |
Michal Vasko | bea08e9 | 2016-05-18 13:28:08 +0200 | [diff] [blame] | 3783 | const struct lys_module *mod, *prev_mod; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3784 | struct ly_ctx *ctx = data->schema->module->ctx; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3785 | const char *model, *name; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3786 | char *str; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3787 | int mod_len, name_len, has_predicate; |
| 3788 | struct unres_data node_match; |
| 3789 | uint32_t k; |
| 3790 | |
| 3791 | memset(&node_match, 0, sizeof node_match); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3792 | |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3793 | /* we need root to resolve absolute path */ |
| 3794 | for (; data->parent; data = data->parent); |
Michal Vasko | db9323c | 2015-10-16 10:32:44 +0200 | [diff] [blame] | 3795 | /* we're still parsing it and the pointer is not correct yet */ |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3796 | if (data->prev) { |
| 3797 | for (; data->prev->next; data = data->prev); |
| 3798 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3799 | |
Michal Vasko | bea08e9 | 2016-05-18 13:28:08 +0200 | [diff] [blame] | 3800 | prev_mod = lyd_node_module(data); |
| 3801 | |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3802 | /* search for the instance node */ |
| 3803 | while (path[i]) { |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3804 | 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] | 3805 | if (j <= 0) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3806 | 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] | 3807 | goto error; |
| 3808 | } |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3809 | i += j; |
Michal Vasko | b387c48 | 2015-08-12 09:32:59 +0200 | [diff] [blame] | 3810 | |
Michal Vasko | bea08e9 | 2016-05-18 13:28:08 +0200 | [diff] [blame] | 3811 | if (model) { |
| 3812 | str = strndup(model, mod_len); |
| 3813 | if (!str) { |
| 3814 | LOGMEM; |
| 3815 | goto error; |
| 3816 | } |
| 3817 | mod = ly_ctx_get_module(ctx, str, NULL); |
| 3818 | free(str); |
| 3819 | } else { |
| 3820 | mod = prev_mod; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3821 | } |
| 3822 | |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3823 | if (resolve_data(mod, name, name_len, data, &node_match)) { |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3824 | /* no instance exists */ |
| 3825 | return NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3826 | } |
| 3827 | |
| 3828 | if (has_predicate) { |
| 3829 | /* 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] | 3830 | for (k = 0; k < node_match.count;) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3831 | if ((node_match.node[k]->schema->nodetype == LYS_LIST && |
| 3832 | ((struct lys_node_list *)node_match.node[k]->schema)->keys) |
| 3833 | || (node_match.node[k]->schema->nodetype == LYS_LEAFLIST)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3834 | /* instid is ok, continue check with next instid */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3835 | ++k; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3836 | continue; |
| 3837 | } |
| 3838 | |
| 3839 | /* does not fulfill conditions, remove inst record */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3840 | unres_data_del(&node_match, k); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3841 | } |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3842 | |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3843 | j = resolve_predicate(&path[i], &node_match); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3844 | if (j < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3845 | LOGVAL(LYE_INPRED, LY_VLOG_LYD, data, &path[i-j]); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3846 | goto error; |
| 3847 | } |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3848 | i += j; |
Michal Vasko | b387c48 | 2015-08-12 09:32:59 +0200 | [diff] [blame] | 3849 | |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3850 | if (!node_match.count) { |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3851 | /* no instance exists */ |
| 3852 | return NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3853 | } |
| 3854 | } |
Michal Vasko | bea08e9 | 2016-05-18 13:28:08 +0200 | [diff] [blame] | 3855 | |
| 3856 | prev_mod = mod; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3857 | } |
| 3858 | |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3859 | if (!node_match.count) { |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3860 | /* no instance exists */ |
| 3861 | return NULL; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3862 | } else if (node_match.count > 1) { |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3863 | /* instance identifier must resolve to a single node */ |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3864 | LOGVAL(LYE_TOOMANY, LY_VLOG_LYD, data, path, "data tree"); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3865 | |
Michal Vasko | d6adbaa | 2016-04-11 11:01:09 +0200 | [diff] [blame] | 3866 | goto error; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3867 | } else { |
| 3868 | /* we have required result, remember it and cleanup */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3869 | result = node_match.node[0]; |
| 3870 | free(node_match.node); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3871 | |
| 3872 | return result; |
| 3873 | } |
| 3874 | |
| 3875 | error: |
| 3876 | |
| 3877 | /* cleanup */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3878 | free(node_match.node); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3879 | |
| 3880 | return NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3881 | } |
| 3882 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3883 | /** |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 3884 | * @brief Passes config flag down to children, skips nodes without config flags. |
| 3885 | * Does not log. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3886 | * |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 3887 | * @param[in] node Siblings and their children to have flags changed. |
| 3888 | * @param[in] flags Flags to assign to all the nodes. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3889 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3890 | static void |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 3891 | inherit_config_flag(struct lys_node *node, int flags) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3892 | { |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 3893 | assert(!(flags ^ (flags & LYS_CONFIG_MASK))); |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 3894 | LY_TREE_FOR(node, node) { |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 3895 | if (node->flags & LYS_CONFIG_SET) { |
| 3896 | /* skip nodes with an explicit config value */ |
| 3897 | continue; |
| 3898 | } |
| 3899 | if (!(node->nodetype & (LYS_USES | LYS_GROUPING))) { |
Radek Krejci | 43f548d | 2016-06-16 11:06:38 +0200 | [diff] [blame] | 3900 | node->flags = (node->flags & ~LYS_CONFIG_MASK) | flags; |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 3901 | } |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 3902 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYXML))) { |
| 3903 | inherit_config_flag(node->child, flags); |
| 3904 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3905 | } |
| 3906 | } |
| 3907 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3908 | /** |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 3909 | * @brief Resolve augment target. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3910 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3911 | * @param[in] aug Augment to use. |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 3912 | * @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] | 3913 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3914 | * @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] | 3915 | */ |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 3916 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3917 | resolve_augment(struct lys_node_augment *aug, struct lys_node *siblings) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3918 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3919 | int rc; |
Michal Vasko | 1d87a92 | 2015-08-21 12:57:16 +0200 | [diff] [blame] | 3920 | struct lys_node *sub; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3921 | |
Michal Vasko | 1d87a92 | 2015-08-21 12:57:16 +0200 | [diff] [blame] | 3922 | assert(aug); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3923 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 3924 | if (!aug->target) { |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 3925 | /* resolve target node */ |
| 3926 | rc = resolve_augment_schema_nodeid(aug->target_name, siblings, (siblings ? NULL : aug->module), (const struct lys_node **)&aug->target); |
| 3927 | if (rc == -1) { |
| 3928 | return -1; |
| 3929 | } |
| 3930 | if (rc > 0) { |
| 3931 | LOGVAL(LYE_INCHAR, LY_VLOG_LYS, aug, aug->target_name[rc - 1], &aug->target_name[rc - 1]); |
| 3932 | return -1; |
| 3933 | } |
| 3934 | if (!aug->target) { |
| 3935 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, aug, "augment", aug->target_name); |
| 3936 | return EXIT_FAILURE; |
| 3937 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3938 | |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 3939 | if (!aug->child) { |
| 3940 | /* nothing to do */ |
| 3941 | LOGWRN("Augment \"%s\" without children.", aug->target_name); |
| 3942 | return EXIT_SUCCESS; |
| 3943 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3944 | } |
| 3945 | |
Michal Vasko | d58d596 | 2016-03-02 14:29:41 +0100 | [diff] [blame] | 3946 | /* check for mandatory nodes - if the target node is in another module |
| 3947 | * the added nodes cannot be mandatory |
| 3948 | */ |
| 3949 | 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] | 3950 | && (rc = lyp_check_mandatory_augment(aug))) { |
| 3951 | return rc; |
Michal Vasko | d58d596 | 2016-03-02 14:29:41 +0100 | [diff] [blame] | 3952 | } |
| 3953 | |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 3954 | /* check augment target type and then augment nodes type */ |
| 3955 | if (aug->target->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_CASE | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF)) { |
| 3956 | LY_TREE_FOR(aug->child, sub) { |
| 3957 | if (!(sub->nodetype & (LYS_ANYXML | LYS_CONTAINER | LYS_LEAF | LYS_LIST | LYS_LEAFLIST | LYS_USES | LYS_CHOICE))) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3958 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment"); |
| 3959 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, aug, "Cannot augment \"%s\" with a \"%s\".", |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 3960 | strnodetype(aug->target->nodetype), strnodetype(sub->nodetype)); |
| 3961 | return -1; |
| 3962 | } |
| 3963 | } |
| 3964 | } else if (aug->target->nodetype == LYS_CHOICE) { |
| 3965 | LY_TREE_FOR(aug->child, sub) { |
| 3966 | if (!(sub->nodetype & (LYS_CASE | LYS_ANYXML | LYS_CONTAINER | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3967 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment"); |
| 3968 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, aug, "Cannot augment \"%s\" with a \"%s\".", |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 3969 | strnodetype(aug->target->nodetype), strnodetype(sub->nodetype)); |
| 3970 | return -1; |
| 3971 | } |
| 3972 | } |
| 3973 | } else { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3974 | LOGVAL(LYE_INARG, LY_VLOG_LYS, aug, aug->target_name, "target-node"); |
| 3975 | 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] | 3976 | return -1; |
| 3977 | } |
| 3978 | |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 3979 | /* inherit config information from actual parent */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3980 | LY_TREE_FOR(aug->child, sub) { |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 3981 | inherit_config_flag(sub, aug->target->flags & LYS_CONFIG_MASK); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3982 | } |
| 3983 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 3984 | /* check identifier uniqueness as in lys_node_addchild() */ |
Michal Vasko | 1d87a92 | 2015-08-21 12:57:16 +0200 | [diff] [blame] | 3985 | LY_TREE_FOR(aug->child, sub) { |
Michal Vasko | 2afc1ca | 2016-05-03 11:38:53 +0200 | [diff] [blame] | 3986 | if (lys_check_id(sub, aug->target, NULL)) { |
Michal Vasko | 3e6665f | 2015-08-17 14:00:38 +0200 | [diff] [blame] | 3987 | return -1; |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 3988 | } |
| 3989 | } |
Radek Krejci | 0acbe1b | 2015-08-04 09:33:49 +0200 | [diff] [blame] | 3990 | /* reconnect augmenting data into the target - add them to the target child list */ |
| 3991 | if (aug->target->child) { |
Michal Vasko | 1d87a92 | 2015-08-21 12:57:16 +0200 | [diff] [blame] | 3992 | sub = aug->target->child->prev; /* remember current target's last node */ |
| 3993 | sub->next = aug->child; /* connect augmenting data after target's last node */ |
Radek Krejci | 0acbe1b | 2015-08-04 09:33:49 +0200 | [diff] [blame] | 3994 | aug->target->child->prev = aug->child->prev; /* new target's last node is last augmenting node */ |
Michal Vasko | 1d87a92 | 2015-08-21 12:57:16 +0200 | [diff] [blame] | 3995 | aug->child->prev = sub; /* finish connecting of both child lists */ |
Radek Krejci | 0acbe1b | 2015-08-04 09:33:49 +0200 | [diff] [blame] | 3996 | } else { |
| 3997 | aug->target->child = aug->child; |
| 3998 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3999 | |
| 4000 | return EXIT_SUCCESS; |
| 4001 | } |
| 4002 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4003 | /** |
| 4004 | * @brief Resolve uses, apply augments, refines. Logs directly. |
| 4005 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4006 | * @param[in] uses Uses to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4007 | * @param[in,out] unres List of unresolved items. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4008 | * |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 4009 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4010 | */ |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 4011 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4012 | resolve_uses(struct lys_node_uses *uses, struct unres_schema *unres) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4013 | { |
| 4014 | struct ly_ctx *ctx; |
Radek Krejci | 989790c | 2016-04-14 17:49:41 +0200 | [diff] [blame] | 4015 | struct lys_node *node = NULL, *next, *iter; |
Pavol Vican | 55abd33 | 2016-07-12 15:54:49 +0200 | [diff] [blame] | 4016 | struct lys_node *node_aux, *parent, *tmp; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 4017 | struct lys_refine *rfn; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4018 | struct lys_restr *must, **old_must; |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4019 | struct lys_iffeature *iff, **old_iff; |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4020 | int i, j, rc, parent_flags; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4021 | uint8_t size, *old_size; |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4022 | unsigned int usize, usize1, usize2; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4023 | |
Michal Vasko | 71e1aa8 | 2015-08-12 12:17:51 +0200 | [diff] [blame] | 4024 | assert(uses->grp); |
Michal Vasko | e770851 | 2016-03-11 10:26:55 +0100 | [diff] [blame] | 4025 | /* HACK just check that the grouping is resolved */ |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 4026 | assert(!uses->grp->nacm); |
Michal Vasko | 71e1aa8 | 2015-08-12 12:17:51 +0200 | [diff] [blame] | 4027 | |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4028 | if (!uses->grp->child) { |
| 4029 | /* grouping without children, warning was already displayed */ |
| 4030 | return EXIT_SUCCESS; |
| 4031 | } |
| 4032 | |
| 4033 | /* get proper parent (config) flags */ |
| 4034 | for (node_aux = lys_parent((struct lys_node *)uses); node_aux && (node_aux->nodetype == LYS_USES); node_aux = lys_parent(node_aux)); |
| 4035 | if (node_aux) { |
| 4036 | parent_flags = node_aux->flags & LYS_CONFIG_MASK; |
| 4037 | } else { |
| 4038 | /* default */ |
| 4039 | parent_flags = LYS_CONFIG_W; |
| 4040 | } |
| 4041 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4042 | /* copy the data nodes from grouping into the uses context */ |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 4043 | LY_TREE_FOR(uses->grp->child, node_aux) { |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4044 | 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] | 4045 | if (!node) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4046 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, uses->grp->name, "uses"); |
| 4047 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, "Copying data from grouping failed."); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4048 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4049 | } |
Pavol Vican | 55abd33 | 2016-07-12 15:54:49 +0200 | [diff] [blame] | 4050 | /* test the name of siblings */ |
| 4051 | 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] | 4052 | if (!(tmp->nodetype & (LYS_USES | LYS_GROUPING | LYS_CASE)) && ly_strequal(tmp->name, node_aux->name, 1)) { |
Pavol Vican | 55abd33 | 2016-07-12 15:54:49 +0200 | [diff] [blame] | 4053 | return -1; |
| 4054 | } |
| 4055 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4056 | } |
| 4057 | ctx = uses->module->ctx; |
| 4058 | |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4059 | if (parent_flags) { |
| 4060 | assert(uses->child); |
| 4061 | inherit_config_flag(uses->child, parent_flags); |
| 4062 | } |
| 4063 | |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 4064 | /* we managed to copy the grouping, the rest must be possible to resolve */ |
| 4065 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4066 | /* apply refines */ |
| 4067 | for (i = 0; i < uses->refine_size; i++) { |
| 4068 | rfn = &uses->refine[i]; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 4069 | 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] | 4070 | 1, 0, (const struct lys_node **)&node); |
Michal Vasko | 9bb061b | 2016-02-12 11:00:19 +0100 | [diff] [blame] | 4071 | if (rc || !node) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4072 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine"); |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 4073 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4074 | } |
| 4075 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4076 | if (rfn->target_type && !(node->nodetype & rfn->target_type)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4077 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine"); |
| 4078 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, "Refine substatements not applicable to the target-node."); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4079 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4080 | } |
| 4081 | |
| 4082 | /* description on any nodetype */ |
| 4083 | if (rfn->dsc) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4084 | lydict_remove(ctx, node->dsc); |
| 4085 | node->dsc = lydict_insert(ctx, rfn->dsc, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4086 | } |
| 4087 | |
| 4088 | /* reference on any nodetype */ |
| 4089 | if (rfn->ref) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4090 | lydict_remove(ctx, node->ref); |
| 4091 | node->ref = lydict_insert(ctx, rfn->ref, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4092 | } |
| 4093 | |
| 4094 | /* config on any nodetype */ |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 4095 | if (rfn->flags & LYS_CONFIG_MASK) { |
Radek Krejci | a881e3e | 2016-06-23 17:02:39 +0200 | [diff] [blame] | 4096 | for (parent = lys_parent(node); parent && parent->nodetype == LYS_USES; parent = lys_parent(parent)); |
| 4097 | if (parent && parent->nodetype != LYS_GROUPING && |
| 4098 | ((parent->flags & LYS_CONFIG_MASK) != (rfn->flags & LYS_CONFIG_MASK)) && |
Radek Krejci | 989790c | 2016-04-14 17:49:41 +0200 | [diff] [blame] | 4099 | (rfn->flags & LYS_CONFIG_W)) { |
| 4100 | /* setting config true under config false is prohibited */ |
| 4101 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, "config", "refine"); |
| 4102 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, |
| 4103 | "changing config from 'false' to 'true' is prohibited while " |
| 4104 | "the target's parent is still config 'false'."); |
| 4105 | return -1; |
| 4106 | } |
| 4107 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4108 | node->flags &= ~LYS_CONFIG_MASK; |
| 4109 | node->flags |= (rfn->flags & LYS_CONFIG_MASK); |
Radek Krejci | 989790c | 2016-04-14 17:49:41 +0200 | [diff] [blame] | 4110 | |
| 4111 | /* inherit config change to the target children */ |
| 4112 | LY_TREE_DFS_BEGIN(node->child, next, iter) { |
| 4113 | if (rfn->flags & LYS_CONFIG_W) { |
| 4114 | if (iter->flags & LYS_CONFIG_SET) { |
| 4115 | /* config is set explicitely, go to next sibling */ |
| 4116 | next = NULL; |
| 4117 | goto nextsibling; |
| 4118 | } |
| 4119 | } else { /* LYS_CONFIG_R */ |
| 4120 | if ((iter->flags & LYS_CONFIG_SET) && (iter->flags & LYS_CONFIG_W)) { |
| 4121 | /* error - we would have config data under status data */ |
| 4122 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, "config", "refine"); |
| 4123 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, |
| 4124 | "changing config from 'true' to 'false' is prohibited while the target " |
| 4125 | "has still a children with explicit config 'true'."); |
| 4126 | return -1; |
| 4127 | } |
| 4128 | } |
| 4129 | /* change config */ |
| 4130 | iter->flags &= ~LYS_CONFIG_MASK; |
| 4131 | iter->flags |= (rfn->flags & LYS_CONFIG_MASK); |
| 4132 | |
| 4133 | /* select next iter - modified LY_TREE_DFS_END */ |
| 4134 | if (iter->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYXML)) { |
| 4135 | next = NULL; |
| 4136 | } else { |
| 4137 | next = iter->child; |
| 4138 | } |
| 4139 | nextsibling: |
| 4140 | if (!next) { |
| 4141 | /* no children */ |
| 4142 | if (iter == node->child) { |
| 4143 | /* we are done, (START) has no children */ |
| 4144 | break; |
| 4145 | } |
| 4146 | /* try siblings */ |
| 4147 | next = iter->next; |
| 4148 | } |
| 4149 | while (!next) { |
| 4150 | /* parent is already processed, go to its sibling */ |
| 4151 | iter = lys_parent(iter); |
| 4152 | |
| 4153 | /* no siblings, go back through parents */ |
| 4154 | if (iter == node) { |
| 4155 | /* we are done, no next element to process */ |
| 4156 | break; |
| 4157 | } |
| 4158 | next = iter->next; |
| 4159 | } |
| 4160 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4161 | } |
| 4162 | |
| 4163 | /* default value ... */ |
| 4164 | if (rfn->mod.dflt) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4165 | if (node->nodetype == LYS_LEAF) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4166 | /* leaf */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4167 | lydict_remove(ctx, ((struct lys_node_leaf *)node)->dflt); |
| 4168 | ((struct lys_node_leaf *)node)->dflt = lydict_insert(ctx, rfn->mod.dflt, 0); |
| 4169 | } else if (node->nodetype == LYS_CHOICE) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4170 | /* choice */ |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 4171 | rc = resolve_choice_default_schema_nodeid(rfn->mod.dflt, node->child, |
| 4172 | (const struct lys_node **)&((struct lys_node_choice *)node)->dflt); |
Michal Vasko | 9bb061b | 2016-02-12 11:00:19 +0100 | [diff] [blame] | 4173 | if (rc || !((struct lys_node_choice *)node)->dflt) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4174 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->mod.dflt, "default"); |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 4175 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4176 | } |
| 4177 | } |
| 4178 | } |
| 4179 | |
| 4180 | /* mandatory on leaf, anyxml or choice */ |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 4181 | if (rfn->flags & LYS_MAND_MASK) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4182 | if (node->nodetype & (LYS_LEAF | LYS_ANYXML | LYS_CHOICE)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4183 | /* remove current value */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4184 | node->flags &= ~LYS_MAND_MASK; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4185 | |
| 4186 | /* set new value */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4187 | node->flags |= (rfn->flags & LYS_MAND_MASK); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4188 | } |
| 4189 | } |
| 4190 | |
| 4191 | /* presence on container */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4192 | if ((node->nodetype & LYS_CONTAINER) && rfn->mod.presence) { |
| 4193 | lydict_remove(ctx, ((struct lys_node_container *)node)->presence); |
| 4194 | ((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] | 4195 | } |
| 4196 | |
| 4197 | /* min/max-elements on list or leaf-list */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4198 | if (node->nodetype == LYS_LIST) { |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 4199 | if (rfn->flags & LYS_RFN_MINSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4200 | ((struct lys_node_list *)node)->min = rfn->mod.list.min; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4201 | } |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 4202 | if (rfn->flags & LYS_RFN_MAXSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4203 | ((struct lys_node_list *)node)->max = rfn->mod.list.max; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4204 | } |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4205 | } else if (node->nodetype == LYS_LEAFLIST) { |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 4206 | if (rfn->flags & LYS_RFN_MINSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4207 | ((struct lys_node_leaflist *)node)->min = rfn->mod.list.min; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4208 | } |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 4209 | if (rfn->flags & LYS_RFN_MAXSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4210 | ((struct lys_node_leaflist *)node)->max = rfn->mod.list.max; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4211 | } |
| 4212 | } |
| 4213 | |
| 4214 | /* must in leaf, leaf-list, list, container or anyxml */ |
| 4215 | if (rfn->must_size) { |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4216 | switch (node->nodetype) { |
| 4217 | case LYS_LEAF: |
| 4218 | old_size = &((struct lys_node_leaf *)node)->must_size; |
| 4219 | old_must = &((struct lys_node_leaf *)node)->must; |
| 4220 | break; |
| 4221 | case LYS_LEAFLIST: |
| 4222 | old_size = &((struct lys_node_leaflist *)node)->must_size; |
| 4223 | old_must = &((struct lys_node_leaflist *)node)->must; |
| 4224 | break; |
| 4225 | case LYS_LIST: |
| 4226 | old_size = &((struct lys_node_list *)node)->must_size; |
| 4227 | old_must = &((struct lys_node_list *)node)->must; |
| 4228 | break; |
| 4229 | case LYS_CONTAINER: |
| 4230 | old_size = &((struct lys_node_container *)node)->must_size; |
| 4231 | old_must = &((struct lys_node_container *)node)->must; |
| 4232 | break; |
| 4233 | case LYS_ANYXML: |
| 4234 | old_size = &((struct lys_node_anyxml *)node)->must_size; |
| 4235 | old_must = &((struct lys_node_anyxml *)node)->must; |
| 4236 | break; |
| 4237 | default: |
| 4238 | LOGINT; |
Radek Krejci | e4e4d72 | 2015-10-05 16:53:50 +0200 | [diff] [blame] | 4239 | return -1; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4240 | } |
| 4241 | |
| 4242 | size = *old_size + rfn->must_size; |
| 4243 | must = realloc(*old_must, size * sizeof *rfn->must); |
| 4244 | if (!must) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4245 | LOGMEM; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4246 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4247 | } |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4248 | for (i = 0, j = *old_size; i < rfn->must_size; i++, j++) { |
| 4249 | must[j].expr = lydict_insert(ctx, rfn->must[i].expr, 0); |
| 4250 | must[j].dsc = lydict_insert(ctx, rfn->must[i].dsc, 0); |
| 4251 | must[j].ref = lydict_insert(ctx, rfn->must[i].ref, 0); |
| 4252 | must[j].eapptag = lydict_insert(ctx, rfn->must[i].eapptag, 0); |
| 4253 | must[j].emsg = lydict_insert(ctx, rfn->must[i].emsg, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4254 | } |
| 4255 | |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4256 | *old_must = must; |
| 4257 | *old_size = size; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4258 | } |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4259 | |
| 4260 | /* if-feature in leaf, leaf-list, list, container or anyxml */ |
| 4261 | if (rfn->iffeature_size) { |
| 4262 | old_size = &node->iffeature_size; |
| 4263 | old_iff = &node->iffeature; |
| 4264 | |
| 4265 | size = *old_size + rfn->iffeature_size; |
| 4266 | iff = realloc(*old_iff, size * sizeof *rfn->iffeature); |
| 4267 | if (!iff) { |
| 4268 | LOGMEM; |
| 4269 | return -1; |
| 4270 | } |
| 4271 | for (i = 0, j = *old_size; i < rfn->iffeature_size; i++, j++) { |
| 4272 | resolve_iffeature_getsizes(&rfn->iffeature[i], &usize1, &usize2); |
| 4273 | if (usize1) { |
| 4274 | /* there is something to duplicate */ |
| 4275 | /* duplicate compiled expression */ |
| 4276 | usize = (usize1 / 4) + (usize1 % 4) ? 1 : 0; |
| 4277 | iff[j].expr = malloc(usize * sizeof *iff[j].expr); |
| 4278 | memcpy(iff[j].expr, rfn->iffeature[i].expr, usize * sizeof *iff[j].expr); |
| 4279 | |
| 4280 | /* duplicate list of feature pointers */ |
| 4281 | iff[j].features = malloc(usize2 * sizeof *iff[i].features); |
| 4282 | memcpy(iff[j].features, rfn->iffeature[i].features, usize2 * sizeof *iff[j].features); |
| 4283 | } |
| 4284 | } |
| 4285 | |
| 4286 | *old_iff = iff; |
| 4287 | *old_size = size; |
| 4288 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4289 | } |
| 4290 | |
| 4291 | /* apply augments */ |
| 4292 | for (i = 0; i < uses->augment_size; i++) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4293 | rc = resolve_augment(&uses->augment[i], uses->child); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4294 | if (rc) { |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 4295 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4296 | } |
| 4297 | } |
| 4298 | |
| 4299 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4300 | } |
| 4301 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4302 | static int |
| 4303 | identity_backlink_update(struct lys_ident *der, struct lys_ident *base) |
| 4304 | { |
| 4305 | int i; |
| 4306 | |
| 4307 | assert(der && base); |
| 4308 | |
| 4309 | base->der = ly_realloc(base->der, (base->der_size + 1) * sizeof *(base->der)); |
| 4310 | if (!base->der) { |
| 4311 | LOGMEM; |
| 4312 | return EXIT_FAILURE; |
| 4313 | } |
| 4314 | base->der[base->der_size++] = der; |
| 4315 | |
| 4316 | for (i = 0; i < base->base_size; i++) { |
| 4317 | if (identity_backlink_update(der, base->base[i])) { |
| 4318 | return EXIT_FAILURE; |
| 4319 | } |
| 4320 | } |
| 4321 | |
| 4322 | return EXIT_SUCCESS; |
| 4323 | } |
| 4324 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4325 | /** |
| 4326 | * @brief Resolve base identity recursively. Does not log. |
| 4327 | * |
| 4328 | * @param[in] module Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4329 | * @param[in] ident Identity to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4330 | * @param[in] basename Base name of the identity. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4331 | * @param[out] ret Pointer to the resolved identity. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4332 | * |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 4333 | * @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] | 4334 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4335 | static int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 4336 | 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] | 4337 | struct unres_schema *unres, struct lys_ident **ret) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4338 | { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 4339 | uint32_t i, j; |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4340 | struct lys_ident *base = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4341 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 4342 | assert(ret); |
| 4343 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4344 | /* search module */ |
| 4345 | for (i = 0; i < module->ident_size; i++) { |
| 4346 | if (!strcmp(basename, module->ident[i].name)) { |
| 4347 | |
| 4348 | if (!ident) { |
| 4349 | /* just search for type, so do not modify anything, just return |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 4350 | * the base identity pointer */ |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 4351 | *ret = &module->ident[i]; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4352 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4353 | } |
| 4354 | |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 4355 | base = &module->ident[i]; |
| 4356 | goto matchfound; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4357 | } |
| 4358 | } |
| 4359 | |
| 4360 | /* search submodules */ |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 4361 | for (j = 0; j < module->inc_size && module->inc[j].submodule; j++) { |
| 4362 | for (i = 0; i < module->inc[j].submodule->ident_size; i++) { |
| 4363 | if (!strcmp(basename, module->inc[j].submodule->ident[i].name)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4364 | |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 4365 | if (!ident) { |
| 4366 | *ret = &module->inc[j].submodule->ident[i]; |
| 4367 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4368 | } |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 4369 | |
| 4370 | base = &module->inc[j].submodule->ident[i]; |
| 4371 | goto matchfound; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4372 | } |
| 4373 | } |
| 4374 | } |
| 4375 | |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 4376 | matchfound: |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4377 | /* we found it somewhere */ |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 4378 | if (base) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4379 | /* is it already completely resolved? */ |
| 4380 | for (i = 0; i < unres->count; i++) { |
Radek Krejci | ba7b1cc | 2016-08-08 13:44:43 +0200 | [diff] [blame] | 4381 | if ((unres->item[i] == base) && (unres->type[i] == UNRES_IDENT)) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4382 | /* identity found, but not yet resolved, so do not return it in *res and try it again later */ |
| 4383 | |
| 4384 | /* simple check for circular reference, |
| 4385 | * the complete check is done as a side effect of using only completely |
| 4386 | * resolved identities (previous check of unres content) */ |
| 4387 | if (ly_strequal((const char *)unres->str_snode[i], ident->name, 1)) { |
| 4388 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, basename, "base"); |
| 4389 | 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] | 4390 | return -1; |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4391 | } |
| 4392 | |
Radek Krejci | 06f64ed | 2016-08-15 11:07:44 +0200 | [diff] [blame^] | 4393 | return EXIT_FAILURE; |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 4394 | } |
| 4395 | } |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4396 | |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 4397 | /* checks done, store the result */ |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4398 | ident->base[ident->base_size++] = base; |
| 4399 | *ret = base; |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 4400 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4401 | /* maintain backlinks to the derived identities */ |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 4402 | return identity_backlink_update(ident, base) ? -1 : EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4403 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4404 | } |
| 4405 | |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 4406 | /* base not found (maybe a forward reference) */ |
| 4407 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4408 | } |
| 4409 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4410 | /** |
| 4411 | * @brief Resolve base identity. Logs directly. |
| 4412 | * |
| 4413 | * @param[in] module Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4414 | * @param[in] ident Identity to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4415 | * @param[in] basename Base name of the identity. |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 4416 | * @param[in] parent Either "type" or "identity". |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 4417 | * @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] | 4418 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4419 | * @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] | 4420 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4421 | static int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 4422 | 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] | 4423 | struct lys_type *type, struct unres_schema *unres) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4424 | { |
| 4425 | const char *name; |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 4426 | int mod_name_len = 0, rc; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 4427 | struct lys_ident *target, **ret; |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 4428 | uint16_t flags; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 4429 | struct lys_module *mod; |
| 4430 | |
| 4431 | assert((ident && !type) || (!ident && type)); |
| 4432 | |
| 4433 | if (!type) { |
| 4434 | /* have ident to resolve */ |
| 4435 | ret = ⌖ |
| 4436 | flags = ident->flags; |
| 4437 | mod = ident->module; |
| 4438 | } else { |
| 4439 | /* have type to fill */ |
| 4440 | ret = &type->info.ident.ref; |
| 4441 | flags = type->parent->flags; |
| 4442 | mod = type->parent->module; |
| 4443 | } |
Michal Vasko | f200600 | 2016-04-21 16:28:15 +0200 | [diff] [blame] | 4444 | *ret = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4445 | |
| 4446 | /* search for the base identity */ |
| 4447 | name = strchr(basename, ':'); |
| 4448 | if (name) { |
| 4449 | /* set name to correct position after colon */ |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 4450 | mod_name_len = name - basename; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4451 | name++; |
| 4452 | |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 4453 | 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] | 4454 | /* prefix refers to the current module, ignore it */ |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 4455 | mod_name_len = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4456 | } |
| 4457 | } else { |
| 4458 | name = basename; |
| 4459 | } |
| 4460 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 4461 | /* get module where to search */ |
| 4462 | module = lys_get_import_module(module, NULL, 0, mod_name_len ? basename : NULL, mod_name_len); |
| 4463 | if (!module) { |
| 4464 | /* identity refers unknown data model */ |
Radek Krejci | 02a0499 | 2016-03-17 16:06:37 +0100 | [diff] [blame] | 4465 | LOGVAL(LYE_INMOD, LY_VLOG_NONE, NULL, basename); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 4466 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4467 | } |
| 4468 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 4469 | /* search in the identified module ... */ |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 4470 | rc = resolve_base_ident_sub(module, ident, name, unres, ret); |
| 4471 | if (!rc) { |
| 4472 | assert(*ret); |
| 4473 | |
| 4474 | /* check status */ |
| 4475 | if (lyp_check_status(flags, mod, ident ? ident->name : "of type", |
| 4476 | (*ret)->flags, (*ret)->module, (*ret)->name, NULL)) { |
| 4477 | rc = -1; |
| 4478 | } |
| 4479 | } else if (rc == EXIT_FAILURE) { |
| 4480 | LOGVAL(LYE_INRESOLV, LY_VLOG_NONE, NULL, parent, basename); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4481 | } |
| 4482 | |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 4483 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4484 | } |
| 4485 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4486 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 4487 | * @brief Resolve JSON data format identityref. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4488 | * |
| 4489 | * @param[in] base Base identity. |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 4490 | * @param[in] ident_name Identityref name. |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 4491 | * @param[in] node Node where the identityref is being resolved |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4492 | * |
| 4493 | * @return Pointer to the identity resolvent, NULL on error. |
| 4494 | */ |
Radek Krejci | a52656e | 2015-08-05 13:41:50 +0200 | [diff] [blame] | 4495 | struct lys_ident * |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4496 | resolve_identref(struct lys_ident *base, const char *ident_name, struct lyd_node *node) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4497 | { |
Michal Vasko | c633ca0 | 2015-08-21 14:03:51 +0200 | [diff] [blame] | 4498 | const char *mod_name, *name; |
| 4499 | int mod_name_len, rc; |
Radek Krejci | 1b61d0e | 2016-04-15 13:55:44 +0200 | [diff] [blame] | 4500 | int i; |
| 4501 | struct lys_ident *der; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4502 | |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 4503 | if (!base || !ident_name) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4504 | return NULL; |
| 4505 | } |
| 4506 | |
Michal Vasko | c633ca0 | 2015-08-21 14:03:51 +0200 | [diff] [blame] | 4507 | 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] | 4508 | if (rc < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4509 | 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] | 4510 | return NULL; |
Michal Vasko | b43bb3c | 2016-03-17 15:00:27 +0100 | [diff] [blame] | 4511 | } else if (rc < (signed)strlen(ident_name)) { |
Radek Krejci | 02a0499 | 2016-03-17 16:06:37 +0100 | [diff] [blame] | 4512 | 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] | 4513 | return NULL; |
| 4514 | } |
| 4515 | |
Michal Vasko | c633ca0 | 2015-08-21 14:03:51 +0200 | [diff] [blame] | 4516 | if (!strcmp(base->name, name) && (!mod_name |
| 4517 | || (!strncmp(base->module->name, mod_name, mod_name_len) && !base->module->name[mod_name_len]))) { |
Radek Krejci | f1ee2e2 | 2016-08-02 16:36:48 +0200 | [diff] [blame] | 4518 | der = base; |
| 4519 | goto match; |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 4520 | } |
| 4521 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4522 | for (i = 0; i < base->der_size; i++) { |
| 4523 | der = base->der[i]; /* shortcut */ |
| 4524 | if (!strcmp(der->name, name) && |
| 4525 | (!mod_name || (!strncmp(der->module->name, mod_name, mod_name_len) && !der->module->name[mod_name_len]))) { |
| 4526 | /* we have match */ |
| 4527 | goto match; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4528 | } |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4529 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4530 | } |
| 4531 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4532 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYD, node, "identityref", ident_name); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4533 | return NULL; |
Radek Krejci | f1ee2e2 | 2016-08-02 16:36:48 +0200 | [diff] [blame] | 4534 | |
| 4535 | match: |
| 4536 | for (i = 0; i < der->iffeature_size; i++) { |
| 4537 | if (!resolve_iffeature(&der->iffeature[i])) { |
| 4538 | LOGVAL(LYE_INVAL, LY_VLOG_LYD, node, der->name, node->schema->name); |
| 4539 | LOGVAL(LYE_SPEC, LY_VLOG_LYD, node, "Identity \"%s\" is disabled by its if-feature condition.", |
| 4540 | der->name); |
| 4541 | return NULL; |
| 4542 | } |
| 4543 | } |
| 4544 | return der; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4545 | } |
| 4546 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4547 | /** |
Michal Vasko | 7955b36 | 2015-09-04 14:18:15 +0200 | [diff] [blame] | 4548 | * @brief Resolve (find) choice default case. Does not log. |
| 4549 | * |
| 4550 | * @param[in] choic Choice to use. |
| 4551 | * @param[in] dflt Name of the default case. |
| 4552 | * |
| 4553 | * @return Pointer to the default node or NULL. |
| 4554 | */ |
| 4555 | static struct lys_node * |
| 4556 | resolve_choice_dflt(struct lys_node_choice *choic, const char *dflt) |
| 4557 | { |
| 4558 | struct lys_node *child, *ret; |
| 4559 | |
| 4560 | LY_TREE_FOR(choic->child, child) { |
| 4561 | if (child->nodetype == LYS_USES) { |
| 4562 | ret = resolve_choice_dflt((struct lys_node_choice *)child, dflt); |
| 4563 | if (ret) { |
| 4564 | return ret; |
| 4565 | } |
| 4566 | } |
| 4567 | |
Radek Krejci | 749190d | 2016-02-18 16:26:25 +0100 | [diff] [blame] | 4568 | if (ly_strequal(child->name, dflt, 1) && (child->nodetype & (LYS_ANYXML | LYS_CASE |
Michal Vasko | 7955b36 | 2015-09-04 14:18:15 +0200 | [diff] [blame] | 4569 | | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST))) { |
| 4570 | return child; |
| 4571 | } |
| 4572 | } |
| 4573 | |
| 4574 | return NULL; |
| 4575 | } |
| 4576 | |
| 4577 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4578 | * @brief Resolve unresolved uses. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4579 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4580 | * @param[in] uses Uses to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4581 | * @param[in] unres Specific unres item. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4582 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4583 | * @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] | 4584 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4585 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4586 | 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] | 4587 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4588 | int rc; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 4589 | struct lys_node *par_grp; |
Michal Vasko | e91afce | 2015-08-12 12:21:00 +0200 | [diff] [blame] | 4590 | |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 4591 | /* HACK: when a grouping has uses inside, all such uses have to be resolved before the grouping itself |
| 4592 | * is used in some uses. When we see such a uses, the grouping's nacm member (not used in grouping) |
| 4593 | * is used to store number of so far unresolved uses. The grouping cannot be used unless the nacm |
| 4594 | * value is decreased back to 0. To remember that the uses already increased grouping's nacm, the |
| 4595 | * LYS_USESGRP flag is used. */ |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 4596 | 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] | 4597 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4598 | if (!uses->grp) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 4599 | rc = resolve_uses_schema_nodeid(uses->name, (const struct lys_node *)uses, (const struct lys_node_grp **)&uses->grp); |
| 4600 | if (rc == -1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4601 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, uses, "grouping", uses->name); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 4602 | return -1; |
| 4603 | } else if (rc > 0) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4604 | 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] | 4605 | return -1; |
| 4606 | } else if (!uses->grp) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 4607 | if (par_grp && !(uses->flags & LYS_USESGRP)) { |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 4608 | /* hack - in contrast to lys_node, lys_node_grp has bigger nacm field |
| 4609 | * (and smaller flags - it uses only a limited set of flags) |
| 4610 | */ |
| 4611 | ((struct lys_node_grp *)par_grp)->nacm++; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 4612 | uses->flags |= LYS_USESGRP; |
Michal Vasko | 407f1bb | 2015-09-23 15:51:07 +0200 | [diff] [blame] | 4613 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 4614 | return EXIT_FAILURE; |
Michal Vasko | 12e3084 | 2015-08-04 11:54:00 +0200 | [diff] [blame] | 4615 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4616 | } |
| 4617 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4618 | if (uses->grp->nacm) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 4619 | if (par_grp && !(uses->flags & LYS_USESGRP)) { |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 4620 | ((struct lys_node_grp *)par_grp)->nacm++; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 4621 | uses->flags |= LYS_USESGRP; |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 4622 | } else { |
| 4623 | /* instantiate grouping only when it is completely resolved */ |
| 4624 | uses->grp = NULL; |
Michal Vasko | 407f1bb | 2015-09-23 15:51:07 +0200 | [diff] [blame] | 4625 | } |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4626 | return EXIT_FAILURE; |
| 4627 | } |
| 4628 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4629 | rc = resolve_uses(uses, unres); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4630 | if (!rc) { |
| 4631 | /* decrease unres count only if not first try */ |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 4632 | if (par_grp && (uses->flags & LYS_USESGRP)) { |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 4633 | if (!((struct lys_node_grp *)par_grp)->nacm) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4634 | LOGINT; |
| 4635 | return -1; |
| 4636 | } |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 4637 | ((struct lys_node_grp *)par_grp)->nacm--; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 4638 | uses->flags &= ~LYS_USESGRP; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4639 | } |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 4640 | |
| 4641 | /* check status */ |
Radek Krejci | c655602 | 2016-01-27 15:16:45 +0100 | [diff] [blame] | 4642 | if (lyp_check_status(uses->flags, uses->module, "of uses", |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 4643 | uses->grp->flags, uses->grp->module, uses->grp->name, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4644 | (struct lys_node *)uses)) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 4645 | return -1; |
| 4646 | } |
| 4647 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4648 | return EXIT_SUCCESS; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 4649 | } else if ((rc == EXIT_FAILURE) && par_grp && !(uses->flags & LYS_USESGRP)) { |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 4650 | ((struct lys_node_grp *)par_grp)->nacm++; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 4651 | uses->flags |= LYS_USESGRP; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4652 | } |
| 4653 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4654 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4655 | } |
| 4656 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4657 | /** |
Michal Vasko | 9957e59 | 2015-08-17 15:04:09 +0200 | [diff] [blame] | 4658 | * @brief Resolve list keys. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4659 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4660 | * @param[in] list List to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4661 | * @param[in] keys_str Keys node value. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4662 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4663 | * @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] | 4664 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4665 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4666 | resolve_list_keys(struct lys_node_list *list, const char *keys_str) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4667 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4668 | int i, len, rc; |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 4669 | const char *value; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4670 | |
| 4671 | for (i = 0; i < list->keys_size; ++i) { |
| 4672 | /* get the key name */ |
| 4673 | if ((value = strpbrk(keys_str, " \t\n"))) { |
| 4674 | len = value - keys_str; |
| 4675 | while (isspace(value[0])) { |
| 4676 | value++; |
| 4677 | } |
| 4678 | } else { |
| 4679 | len = strlen(keys_str); |
| 4680 | } |
| 4681 | |
Radek Krejci | c428344 | 2016-04-22 09:19:27 +0200 | [diff] [blame] | 4682 | 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] | 4683 | if (rc) { |
Michal Vasko | 7a55bea | 2016-05-02 14:51:20 +0200 | [diff] [blame] | 4684 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, list, "list keys", keys_str); |
| 4685 | return EXIT_FAILURE; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4686 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4687 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4688 | if (check_key(list, i, keys_str, len)) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4689 | /* check_key logs */ |
| 4690 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4691 | } |
| 4692 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 4693 | /* check status */ |
Radek Krejci | c655602 | 2016-01-27 15:16:45 +0100 | [diff] [blame] | 4694 | if (lyp_check_status(list->flags, list->module, list->name, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4695 | list->keys[i]->flags, list->keys[i]->module, list->keys[i]->name, |
| 4696 | (struct lys_node *)list->keys[i])) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 4697 | return -1; |
| 4698 | } |
| 4699 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4700 | /* prepare for next iteration */ |
| 4701 | while (value && isspace(value[0])) { |
| 4702 | value++; |
| 4703 | } |
| 4704 | keys_str = value; |
| 4705 | } |
| 4706 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 4707 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4708 | } |
| 4709 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4710 | /** |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 4711 | * @brief Resolve (check) all must conditions of \p node. |
| 4712 | * Logs directly. |
| 4713 | * |
| 4714 | * @param[in] node Data node with optional must statements. |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 4715 | * |
| 4716 | * @return EXIT_SUCCESS on pass, EXIT_FAILURE on fail, -1 on error. |
| 4717 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4718 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4719 | resolve_must(struct lyd_node *node) |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 4720 | { |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 4721 | uint8_t i, must_size; |
| 4722 | struct lys_restr *must; |
| 4723 | struct lyxp_set set; |
| 4724 | |
| 4725 | assert(node); |
| 4726 | memset(&set, 0, sizeof set); |
| 4727 | |
| 4728 | switch (node->schema->nodetype) { |
| 4729 | case LYS_CONTAINER: |
| 4730 | must_size = ((struct lys_node_container *)node->schema)->must_size; |
| 4731 | must = ((struct lys_node_container *)node->schema)->must; |
| 4732 | break; |
| 4733 | case LYS_LEAF: |
| 4734 | must_size = ((struct lys_node_leaf *)node->schema)->must_size; |
| 4735 | must = ((struct lys_node_leaf *)node->schema)->must; |
| 4736 | break; |
| 4737 | case LYS_LEAFLIST: |
| 4738 | must_size = ((struct lys_node_leaflist *)node->schema)->must_size; |
| 4739 | must = ((struct lys_node_leaflist *)node->schema)->must; |
| 4740 | break; |
| 4741 | case LYS_LIST: |
| 4742 | must_size = ((struct lys_node_list *)node->schema)->must_size; |
| 4743 | must = ((struct lys_node_list *)node->schema)->must; |
| 4744 | break; |
| 4745 | case LYS_ANYXML: |
| 4746 | must_size = ((struct lys_node_anyxml *)node->schema)->must_size; |
| 4747 | must = ((struct lys_node_anyxml *)node->schema)->must; |
| 4748 | break; |
| 4749 | default: |
| 4750 | must_size = 0; |
| 4751 | break; |
| 4752 | } |
| 4753 | |
| 4754 | for (i = 0; i < must_size; ++i) { |
Michal Vasko | 944a564 | 2016-03-21 11:48:58 +0100 | [diff] [blame] | 4755 | if (lyxp_eval(must[i].expr, node, &set, LYXP_MUST)) { |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 4756 | return -1; |
| 4757 | } |
| 4758 | |
Michal Vasko | 944a564 | 2016-03-21 11:48:58 +0100 | [diff] [blame] | 4759 | lyxp_set_cast(&set, LYXP_SET_BOOLEAN, node, LYXP_MUST); |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 4760 | |
Michal Vasko | 8146d4c | 2016-05-09 15:50:29 +0200 | [diff] [blame] | 4761 | if (!set.val.bool) { |
Michal Vasko | 6ac6828 | 2016-04-11 10:56:47 +0200 | [diff] [blame] | 4762 | LOGVAL(LYE_NOMUST, LY_VLOG_LYD, node, must[i].expr); |
| 4763 | if (must[i].emsg) { |
| 4764 | LOGVAL(LYE_SPEC, LY_VLOG_LYD, node, must[i].emsg); |
| 4765 | } |
| 4766 | if (must[i].eapptag) { |
| 4767 | strncpy(((struct ly_err *)&ly_errno)->apptag, must[i].eapptag, LY_APPTAG_LEN - 1); |
| 4768 | } |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 4769 | return 1; |
| 4770 | } |
| 4771 | } |
| 4772 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4773 | return EXIT_SUCCESS; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 4774 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4775 | |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 4776 | /** |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4777 | * @brief Resolve (find) when condition context node. Does not log. |
| 4778 | * |
| 4779 | * @param[in] node Data node, whose conditional definition is being decided. |
| 4780 | * @param[in] schema Schema node with a when condition. |
| 4781 | * |
| 4782 | * @return Context node. |
| 4783 | */ |
| 4784 | static struct lyd_node * |
| 4785 | resolve_when_ctx_node(struct lyd_node *node, struct lys_node *schema) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4786 | { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4787 | struct lyd_node *parent; |
| 4788 | struct lys_node *sparent; |
| 4789 | uint16_t i, data_depth, schema_depth; |
| 4790 | |
| 4791 | /* find a not schema-only node */ |
| 4792 | while (schema->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE | LYS_AUGMENT | LYS_INPUT | LYS_OUTPUT)) { |
| 4793 | schema = lys_parent(schema); |
| 4794 | if (!schema) { |
| 4795 | return NULL; |
| 4796 | } |
| 4797 | } |
| 4798 | |
| 4799 | /* get node depths */ |
| 4800 | for (parent = node, data_depth = 0; parent; parent = parent->parent, ++data_depth); |
| 4801 | for (sparent = lys_parent(schema), schema_depth = 1; sparent; sparent = lys_parent(sparent)) { |
| 4802 | if (sparent->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYXML | LYS_NOTIF | LYS_RPC)) { |
| 4803 | ++schema_depth; |
| 4804 | } |
| 4805 | } |
| 4806 | if (data_depth < schema_depth) { |
| 4807 | return NULL; |
| 4808 | } |
| 4809 | |
| 4810 | /* find the corresponding data node */ |
| 4811 | for (i = 0; i < data_depth - schema_depth; ++i) { |
| 4812 | node = node->parent; |
| 4813 | } |
| 4814 | if (node->schema != schema) { |
| 4815 | return NULL; |
| 4816 | } |
| 4817 | |
| 4818 | return node; |
| 4819 | } |
| 4820 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 4821 | int |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 4822 | resolve_applies_must(const struct lyd_node *node) |
| 4823 | { |
| 4824 | switch (node->schema->nodetype) { |
| 4825 | case LYS_CONTAINER: |
| 4826 | return ((struct lys_node_container *)node->schema)->must_size; |
| 4827 | case LYS_LEAF: |
| 4828 | return ((struct lys_node_leaf *)node->schema)->must_size; |
| 4829 | case LYS_LEAFLIST: |
| 4830 | return ((struct lys_node_leaflist *)node->schema)->must_size; |
| 4831 | case LYS_LIST: |
| 4832 | return ((struct lys_node_list *)node->schema)->must_size; |
| 4833 | case LYS_ANYXML: |
| 4834 | return ((struct lys_node_anyxml *)node->schema)->must_size; |
| 4835 | default: |
| 4836 | return 0; |
| 4837 | } |
| 4838 | } |
| 4839 | |
| 4840 | int |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 4841 | resolve_applies_when(const struct lyd_node *node) |
| 4842 | { |
| 4843 | struct lys_node *parent; |
| 4844 | |
| 4845 | assert(node); |
| 4846 | |
| 4847 | if (!(node->schema->nodetype & (LYS_NOTIF | LYS_RPC)) && (((struct lys_node_container *)node->schema)->when)) { |
| 4848 | return 1; |
| 4849 | } |
| 4850 | |
| 4851 | parent = node->schema; |
| 4852 | goto check_augment; |
| 4853 | |
| 4854 | while (parent && (parent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) { |
| 4855 | if (((struct lys_node_uses *)parent)->when) { |
| 4856 | return 1; |
| 4857 | } |
| 4858 | check_augment: |
| 4859 | |
| 4860 | if ((parent->parent && (parent->parent->nodetype == LYS_AUGMENT) && |
| 4861 | (((struct lys_node_augment *)parent->parent)->when))) { |
| 4862 | |
| 4863 | } |
| 4864 | parent = lys_parent(parent); |
| 4865 | } |
| 4866 | |
| 4867 | return 0; |
| 4868 | } |
| 4869 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4870 | /** |
| 4871 | * @brief Resolve (check) all when conditions relevant for \p node. |
| 4872 | * Logs directly. |
| 4873 | * |
| 4874 | * @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] | 4875 | * |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 4876 | * @return |
| 4877 | * -1 - error, ly_errno is set |
| 4878 | * 0 - true "when" statement |
| 4879 | * 0, ly_vecode = LYVE_NOCOND - false "when" statement |
| 4880 | * 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] | 4881 | */ |
| 4882 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4883 | resolve_when(struct lyd_node *node) |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4884 | { |
| 4885 | struct lyd_node *ctx_node = NULL; |
| 4886 | struct lys_node *parent; |
| 4887 | struct lyxp_set set; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 4888 | int rc = 0; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4889 | |
| 4890 | assert(node); |
| 4891 | memset(&set, 0, sizeof set); |
| 4892 | |
| 4893 | if (!(node->schema->nodetype & (LYS_NOTIF | LYS_RPC)) && (((struct lys_node_container *)node->schema)->when)) { |
Michal Vasko | 944a564 | 2016-03-21 11:48:58 +0100 | [diff] [blame] | 4894 | rc = lyxp_eval(((struct lys_node_container *)node->schema)->when->cond, node, &set, LYXP_WHEN); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 4895 | if (rc) { |
| 4896 | if (rc == 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4897 | 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] | 4898 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 4899 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4900 | } |
| 4901 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 4902 | /* set boolean result of the condition */ |
Michal Vasko | 944a564 | 2016-03-21 11:48:58 +0100 | [diff] [blame] | 4903 | lyxp_set_cast(&set, LYXP_SET_BOOLEAN, node, LYXP_WHEN); |
Michal Vasko | 8146d4c | 2016-05-09 15:50:29 +0200 | [diff] [blame] | 4904 | if (!set.val.bool) { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 4905 | ly_vlog_hide(1); |
Michal Vasko | 6ac6828 | 2016-04-11 10:56:47 +0200 | [diff] [blame] | 4906 | LOGVAL(LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_container *)node->schema)->when->cond); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 4907 | ly_vlog_hide(0); |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 4908 | node->when_status |= LYD_WHEN_FALSE; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 4909 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4910 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 4911 | |
| 4912 | /* free xpath set content */ |
| 4913 | lyxp_set_cast(&set, LYXP_SET_EMPTY, node, 0); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4914 | } |
| 4915 | |
| 4916 | parent = node->schema; |
| 4917 | goto check_augment; |
| 4918 | |
| 4919 | /* check when in every schema node that affects node */ |
| 4920 | while (parent && (parent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) { |
| 4921 | if (((struct lys_node_uses *)parent)->when) { |
| 4922 | if (!ctx_node) { |
| 4923 | ctx_node = resolve_when_ctx_node(node, parent); |
| 4924 | if (!ctx_node) { |
| 4925 | LOGINT; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 4926 | rc = -1; |
| 4927 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4928 | } |
| 4929 | } |
Michal Vasko | 944a564 | 2016-03-21 11:48:58 +0100 | [diff] [blame] | 4930 | rc = lyxp_eval(((struct lys_node_uses *)parent)->when->cond, ctx_node, &set, LYXP_WHEN); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 4931 | if (rc) { |
| 4932 | if (rc == 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4933 | LOGVAL(LYE_INWHEN, LY_VLOG_LYD, node, ((struct lys_node_uses *)parent)->when->cond); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 4934 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 4935 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4936 | } |
| 4937 | |
Michal Vasko | 944a564 | 2016-03-21 11:48:58 +0100 | [diff] [blame] | 4938 | lyxp_set_cast(&set, LYXP_SET_BOOLEAN, ctx_node, LYXP_WHEN); |
Michal Vasko | 8146d4c | 2016-05-09 15:50:29 +0200 | [diff] [blame] | 4939 | if (!set.val.bool) { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 4940 | ly_vlog_hide(1); |
Michal Vasko | 6ac6828 | 2016-04-11 10:56:47 +0200 | [diff] [blame] | 4941 | LOGVAL(LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_uses *)parent)->when->cond); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 4942 | ly_vlog_hide(0); |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 4943 | node->when_status |= LYD_WHEN_FALSE; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 4944 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4945 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 4946 | |
| 4947 | /* free xpath set content */ |
| 4948 | lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node, 0); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4949 | } |
| 4950 | |
| 4951 | check_augment: |
| 4952 | if ((parent->parent && (parent->parent->nodetype == LYS_AUGMENT) && (((struct lys_node_augment *)parent->parent)->when))) { |
| 4953 | if (!ctx_node) { |
| 4954 | ctx_node = resolve_when_ctx_node(node, parent->parent); |
| 4955 | if (!ctx_node) { |
| 4956 | LOGINT; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 4957 | rc = -1; |
| 4958 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4959 | } |
| 4960 | } |
Michal Vasko | 944a564 | 2016-03-21 11:48:58 +0100 | [diff] [blame] | 4961 | rc = lyxp_eval(((struct lys_node_augment *)parent->parent)->when->cond, ctx_node, &set, LYXP_WHEN); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 4962 | if (rc) { |
| 4963 | if (rc == 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4964 | LOGVAL(LYE_INWHEN, LY_VLOG_LYD, node, ((struct lys_node_augment *)parent->parent)->when->cond); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 4965 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 4966 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4967 | } |
| 4968 | |
Michal Vasko | 944a564 | 2016-03-21 11:48:58 +0100 | [diff] [blame] | 4969 | lyxp_set_cast(&set, LYXP_SET_BOOLEAN, ctx_node, LYXP_WHEN); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4970 | |
Michal Vasko | 8146d4c | 2016-05-09 15:50:29 +0200 | [diff] [blame] | 4971 | if (!set.val.bool) { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 4972 | ly_vlog_hide(1); |
Michal Vasko | 6ac6828 | 2016-04-11 10:56:47 +0200 | [diff] [blame] | 4973 | LOGVAL(LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_augment *)parent->parent)->when->cond); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 4974 | ly_vlog_hide(0); |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 4975 | node->when_status |= LYD_WHEN_FALSE; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 4976 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4977 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 4978 | |
| 4979 | /* free xpath set content */ |
| 4980 | lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node, 0); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4981 | } |
| 4982 | |
| 4983 | parent = lys_parent(parent); |
| 4984 | } |
| 4985 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 4986 | node->when_status |= LYD_WHEN_TRUE; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 4987 | |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 4988 | cleanup: |
| 4989 | |
| 4990 | /* free xpath set content */ |
| 4991 | lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node ? ctx_node : node, 0); |
| 4992 | |
| 4993 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4994 | } |
| 4995 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4996 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4997 | * @brief Resolve a single unres schema item. Logs indirectly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4998 | * |
| 4999 | * @param[in] mod Main module. |
| 5000 | * @param[in] item Item to resolve. Type determined by \p type. |
| 5001 | * @param[in] type Type of the unresolved item. |
| 5002 | * @param[in] str_snode String, a schema node, or NULL. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5003 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5004 | * |
| 5005 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 5006 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5007 | static int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 5008 | 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] | 5009 | struct unres_schema *unres) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5010 | { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 5011 | /* 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] | 5012 | int rc = -1, has_str = 0, tpdf_flag = 0, i, k; |
| 5013 | unsigned int j; |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 5014 | struct lys_node *node; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 5015 | const char *expr; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 5016 | |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 5017 | struct ly_set *refs, *procs; |
| 5018 | struct lys_feature *ref, *feat; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 5019 | struct lys_ident *ident; |
| 5020 | struct lys_type *stype; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 5021 | struct lys_node_choice *choic; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 5022 | struct lyxml_elem *yin; |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 5023 | struct yang_type *yang; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 5024 | struct unres_list_uniq *unique_info; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5025 | |
| 5026 | switch (type) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5027 | case UNRES_IDENT: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 5028 | expr = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 5029 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 5030 | ident = item; |
| 5031 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5032 | rc = resolve_base_ident(mod, ident, expr, "identity", NULL, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5033 | break; |
| 5034 | case UNRES_TYPE_IDENTREF: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 5035 | expr = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 5036 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 5037 | stype = item; |
| 5038 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5039 | rc = resolve_base_ident(mod, NULL, expr, "type", stype, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5040 | break; |
| 5041 | case UNRES_TYPE_LEAFREF: |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 5042 | node = str_snode; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 5043 | stype = item; |
| 5044 | |
Radek Krejci | 2f12f85 | 2016-01-08 12:59:57 +0100 | [diff] [blame] | 5045 | /* HACK - when there is no parent, we are in top level typedef and in that |
| 5046 | * case, the path has to contain absolute path, so we let the resolve_path_arg_schema() |
| 5047 | * know it via tpdf_flag */ |
| 5048 | if (!node) { |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 5049 | tpdf_flag = 1; |
Radek Krejci | 2f12f85 | 2016-01-08 12:59:57 +0100 | [diff] [blame] | 5050 | node = (struct lys_node *)stype->parent; |
| 5051 | } |
| 5052 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5053 | rc = resolve_path_arg_schema(stype->info.lref.path, node, tpdf_flag, |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 5054 | (const struct lys_node **)&stype->info.lref.target); |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 5055 | if (!tpdf_flag && !rc) { |
| 5056 | assert(stype->info.lref.target); |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 5057 | /* store the backlink from leafref target */ |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 5058 | if (lys_leaf_add_leafref_target(stype->info.lref.target, (struct lys_node *)stype->parent)) { |
| 5059 | rc = -1; |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 5060 | } |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 5061 | } |
| 5062 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5063 | break; |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 5064 | case UNRES_TYPE_DER_TPDF: |
| 5065 | tpdf_flag = 1; |
| 5066 | /* no break */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5067 | case UNRES_TYPE_DER: |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 5068 | /* parent */ |
| 5069 | node = str_snode; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 5070 | stype = item; |
| 5071 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 5072 | /* HACK type->der is temporarily unparsed type statement */ |
| 5073 | yin = (struct lyxml_elem *)stype->der; |
| 5074 | stype->der = NULL; |
| 5075 | |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 5076 | if (yin->flags & LY_YANG_STRUCTURE_FLAG) { |
| 5077 | yang = (struct yang_type *)yin; |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 5078 | rc = yang_check_type(mod, node, yang, tpdf_flag, unres); |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 5079 | |
| 5080 | if (rc) { |
Pavol Vican | 933aa5a | 2016-04-09 21:05:46 +0200 | [diff] [blame] | 5081 | if (rc == -1) { |
| 5082 | yang->type->base = yang->base; |
| 5083 | lydict_remove(mod->ctx, yang->name); |
| 5084 | free(yang); |
| 5085 | stype->der = NULL; |
| 5086 | } else { |
| 5087 | /* may try again later */ |
| 5088 | stype->der = (struct lys_tpdf *)yang; |
| 5089 | } |
Pavol Vican | d01d8ae | 2016-03-01 10:45:59 +0100 | [diff] [blame] | 5090 | } else { |
| 5091 | /* 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] | 5092 | lydict_remove(mod->ctx, yang->name); |
Pavol Vican | d01d8ae | 2016-03-01 10:45:59 +0100 | [diff] [blame] | 5093 | free(yang); |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 5094 | } |
| 5095 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 5096 | } else { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 5097 | rc = fill_yin_type(mod, node, yin, stype, tpdf_flag, unres); |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 5098 | if (!rc) { |
| 5099 | /* we need to always be able to free this, it's safe only in this case */ |
| 5100 | lyxml_free(mod->ctx, yin); |
| 5101 | } else { |
| 5102 | /* may try again later, put all back how it was */ |
| 5103 | stype->der = (struct lys_tpdf *)yin; |
| 5104 | } |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 5105 | } |
Radek Krejci | 2c2fce8 | 2016-08-01 13:52:26 +0200 | [diff] [blame] | 5106 | if (!rc) { |
| 5107 | /* it does not make sense to have leaf-list of empty type */ |
| 5108 | if (!tpdf_flag && node->nodetype == LYS_LEAFLIST && stype->base == LY_TYPE_EMPTY) { |
| 5109 | LOGWRN("The leaf-list \"%s\" is of \"empty\" type, which does not make sense.", node->name); |
| 5110 | } |
| 5111 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5112 | break; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5113 | case UNRES_IFFEAT: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 5114 | node = str_snode; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 5115 | expr = *((const char **)item); |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 5116 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 5117 | rc = resolve_feature(expr, strlen(expr), node, item); |
| 5118 | if (!rc) { |
| 5119 | /* success */ |
| 5120 | free((char *)expr); |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 5121 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5122 | break; |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 5123 | case UNRES_FEATURE: |
| 5124 | feat = (struct lys_feature *)item; |
| 5125 | |
| 5126 | if (feat->iffeature_size) { |
| 5127 | refs = ly_set_new(); |
| 5128 | procs = ly_set_new(); |
| 5129 | ly_set_add(procs, feat, 0); |
| 5130 | |
| 5131 | while (procs->number) { |
| 5132 | ref = procs->set.g[procs->number - 1]; |
| 5133 | ly_set_rm_index(procs, procs->number - 1); |
| 5134 | |
| 5135 | for (i = 0; i < ref->iffeature_size; i++) { |
| 5136 | resolve_iffeature_getsizes(&ref->iffeature[i], NULL, &j); |
| 5137 | for (; j > 0 ; j--) { |
| 5138 | if (unres_schema_find(unres, &ref->iffeature[i].features[j - 1], UNRES_IFFEAT) == -1) { |
| 5139 | if (ref->iffeature[i].features[j - 1] == feat) { |
| 5140 | LOGVAL(LYE_CIRC_FEATURES, LY_VLOG_NONE, NULL, feat->name); |
| 5141 | goto featurecheckdone; |
| 5142 | } |
| 5143 | |
| 5144 | if (ref->iffeature[i].features[j - 1]->iffeature_size) { |
| 5145 | k = refs->number; |
| 5146 | if (ly_set_add(refs, ref->iffeature[i].features[j - 1], 0) == k) { |
| 5147 | /* not yet seen feature, add it for processing */ |
| 5148 | ly_set_add(procs, ref->iffeature[i].features[j - 1], 0); |
| 5149 | } |
| 5150 | } |
| 5151 | } else { |
| 5152 | /* forward reference */ |
| 5153 | rc = EXIT_FAILURE; |
| 5154 | goto featurecheckdone; |
| 5155 | } |
| 5156 | } |
| 5157 | |
| 5158 | } |
| 5159 | } |
| 5160 | rc = EXIT_SUCCESS; |
| 5161 | |
| 5162 | featurecheckdone: |
| 5163 | ly_set_free(refs); |
| 5164 | ly_set_free(procs); |
| 5165 | } |
| 5166 | |
| 5167 | break; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5168 | case UNRES_USES: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5169 | rc = resolve_unres_schema_uses(item, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5170 | break; |
| 5171 | case UNRES_TYPE_DFLT: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 5172 | expr = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 5173 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 5174 | stype = item; |
| 5175 | |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 5176 | rc = check_default(stype, expr, mod); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5177 | break; |
| 5178 | case UNRES_CHOICE_DFLT: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 5179 | expr = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 5180 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 5181 | choic = item; |
| 5182 | |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 5183 | if (!choic->dflt) { |
| 5184 | choic->dflt = resolve_choice_dflt(choic, expr); |
| 5185 | } |
Michal Vasko | 7955b36 | 2015-09-04 14:18:15 +0200 | [diff] [blame] | 5186 | if (choic->dflt) { |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 5187 | rc = lyp_check_mandatory_choice((struct lys_node *)choic); |
Michal Vasko | 7955b36 | 2015-09-04 14:18:15 +0200 | [diff] [blame] | 5188 | } else { |
| 5189 | rc = EXIT_FAILURE; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 5190 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5191 | break; |
| 5192 | case UNRES_LIST_KEYS: |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 5193 | has_str = 1; |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5194 | rc = resolve_list_keys(item, str_snode); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5195 | break; |
| 5196 | case UNRES_LIST_UNIQ: |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 5197 | unique_info = (struct unres_list_uniq *)item; |
| 5198 | 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] | 5199 | break; |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 5200 | case UNRES_AUGMENT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5201 | rc = resolve_augment(item, NULL); |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 5202 | break; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5203 | default: |
| 5204 | LOGINT; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5205 | break; |
| 5206 | } |
| 5207 | |
Radek Krejci | 54081ce | 2016-08-12 15:21:47 +0200 | [diff] [blame] | 5208 | if (has_str && !rc) { |
| 5209 | /* the string is no more needed in case of success. |
| 5210 | * 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] | 5211 | lydict_remove(mod->ctx, str_snode); |
| 5212 | } |
| 5213 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5214 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5215 | } |
| 5216 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5217 | /* logs directly */ |
| 5218 | static void |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5219 | 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] | 5220 | { |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 5221 | struct lyxml_elem *xml; |
| 5222 | struct lyxml_attr *attr; |
Radek Krejci | 76e15e1 | 2016-06-22 11:02:24 +0200 | [diff] [blame] | 5223 | const char *type_name = NULL; |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 5224 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5225 | switch (type) { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5226 | case UNRES_IDENT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5227 | 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] | 5228 | break; |
| 5229 | case UNRES_TYPE_IDENTREF: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5230 | 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] | 5231 | break; |
| 5232 | case UNRES_TYPE_LEAFREF: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5233 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "leafref", |
| 5234 | ((struct lys_type *)item)->info.lref.path); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5235 | break; |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 5236 | case UNRES_TYPE_DER_TPDF: |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5237 | case UNRES_TYPE_DER: |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 5238 | xml = (struct lyxml_elem *)((struct lys_type *)item)->der; |
| 5239 | if (xml->flags & LY_YANG_STRUCTURE_FLAG) { |
| 5240 | type_name = ((struct yang_type *)xml)->name; |
| 5241 | } else { |
| 5242 | LY_TREE_FOR(xml->attr, attr) { |
| 5243 | if ((attr->type == LYXML_ATTR_STD) && !strcmp(attr->name, "name")) { |
| 5244 | type_name = attr->value; |
| 5245 | break; |
| 5246 | } |
| 5247 | } |
| 5248 | assert(attr); |
| 5249 | } |
| 5250 | 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] | 5251 | break; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5252 | case UNRES_IFFEAT: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 5253 | 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] | 5254 | break; |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 5255 | case UNRES_FEATURE: |
| 5256 | LOGVRB("There are unresolved if-features for \"%s\" feature circular dependency check, it will be attempted later", |
| 5257 | ((struct lys_feature *)item)->name); |
| 5258 | break; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5259 | case UNRES_USES: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5260 | 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] | 5261 | break; |
| 5262 | case UNRES_TYPE_DFLT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5263 | 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] | 5264 | break; |
| 5265 | case UNRES_CHOICE_DFLT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5266 | 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] | 5267 | break; |
| 5268 | case UNRES_LIST_KEYS: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5269 | 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] | 5270 | break; |
| 5271 | case UNRES_LIST_UNIQ: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5272 | 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] | 5273 | break; |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 5274 | case UNRES_AUGMENT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5275 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "augment target", |
| 5276 | ((struct lys_node_augment *)item)->target_name); |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 5277 | break; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5278 | default: |
| 5279 | LOGINT; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5280 | break; |
| 5281 | } |
| 5282 | } |
| 5283 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5284 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5285 | * @brief Resolve every unres schema item in the structure. Logs directly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5286 | * |
| 5287 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5288 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5289 | * |
Michal Vasko | 92b8a38 | 2015-08-19 14:03:49 +0200 | [diff] [blame] | 5290 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5291 | */ |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5292 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 5293 | resolve_unres_schema(struct lys_module *mod, struct unres_schema *unres) |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5294 | { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5295 | uint32_t i, resolved = 0, unres_count, res_count; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5296 | int rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5297 | |
| 5298 | assert(unres); |
| 5299 | |
Michal Vasko | a0ffcab | 2016-05-02 14:52:08 +0200 | [diff] [blame] | 5300 | LOGVRB("Resolving unresolved schema nodes and their constraints..."); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5301 | ly_vlog_hide(1); |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 5302 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5303 | /* uses */ |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 5304 | do { |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 5305 | unres_count = 0; |
| 5306 | res_count = 0; |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 5307 | |
| 5308 | for (i = 0; i < unres->count; ++i) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5309 | /* UNRES_TYPE_LEAFREF must be resolved (for storing leafref target pointers); |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 5310 | * if-features are resolved here to make sure that we will have all if-features for |
| 5311 | * later check of feature circular dependency */ |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5312 | if (unres->type[i] > UNRES_IDENT) { |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 5313 | continue; |
| 5314 | } |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5315 | /* 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] | 5316 | * UNRES_CHOICE_DFLT and UNRES_IDENT */ |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 5317 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 5318 | ++unres_count; |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5319 | 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] | 5320 | if (!rc) { |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 5321 | unres->type[i] = UNRES_RESOLVED; |
| 5322 | ++resolved; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 5323 | ++res_count; |
Michal Vasko | 89e1532 | 2015-08-17 15:46:55 +0200 | [diff] [blame] | 5324 | } else if (rc == -1) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5325 | ly_vlog_hide(0); |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 5326 | /* print the error */ |
| 5327 | 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] | 5328 | return -1; |
Radek Krejci | c2a180f | 2016-06-22 13:28:16 +0200 | [diff] [blame] | 5329 | } else { |
| 5330 | /* forward reference, erase ly_errno */ |
| 5331 | ly_errno = LY_SUCCESS; |
| 5332 | ly_vecode = LYVE_SUCCESS; |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 5333 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5334 | } |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 5335 | } while (res_count && (res_count < unres_count)); |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 5336 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 5337 | if (res_count < unres_count) { |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 5338 | /* just print the errors */ |
| 5339 | ly_vlog_hide(0); |
| 5340 | |
| 5341 | for (i = 0; i < unres->count; ++i) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5342 | if (unres->type[i] > UNRES_IDENT) { |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 5343 | continue; |
| 5344 | } |
| 5345 | 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] | 5346 | |
| 5347 | /* free the allocated resources */ |
| 5348 | if (unres->type[i] == UNRES_IFFEAT) { |
| 5349 | free(*((char **)unres->item[i])); |
| 5350 | } |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 5351 | } |
Michal Vasko | 92b8a38 | 2015-08-19 14:03:49 +0200 | [diff] [blame] | 5352 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5353 | } |
| 5354 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5355 | /* the rest */ |
| 5356 | for (i = 0; i < unres->count; ++i) { |
| 5357 | if (unres->type[i] == UNRES_RESOLVED) { |
| 5358 | continue; |
| 5359 | } |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 5360 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5361 | 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] | 5362 | if (rc == 0) { |
| 5363 | unres->type[i] = UNRES_RESOLVED; |
| 5364 | ++resolved; |
| 5365 | } else if (rc == -1) { |
| 5366 | ly_vlog_hide(0); |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 5367 | /* print the error */ |
| 5368 | resolve_unres_schema_item(mod, unres->item[i], unres->type[i], unres->str_snode[i], unres); |
| 5369 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5370 | } |
| 5371 | } |
| 5372 | |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5373 | ly_vlog_hide(0); |
| 5374 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5375 | if (resolved < unres->count) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5376 | /* try to resolve the unresolved nodes again, it will not resolve anything, but it will print |
| 5377 | * all the validation errors |
| 5378 | */ |
| 5379 | for (i = 0; i < unres->count; ++i) { |
| 5380 | if (unres->type[i] == UNRES_RESOLVED) { |
| 5381 | continue; |
| 5382 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5383 | 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] | 5384 | } |
Michal Vasko | 92b8a38 | 2015-08-19 14:03:49 +0200 | [diff] [blame] | 5385 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5386 | } |
| 5387 | |
Michal Vasko | a0ffcab | 2016-05-02 14:52:08 +0200 | [diff] [blame] | 5388 | LOGVRB("All schema nodes and constraints resolved."); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 5389 | unres->count = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5390 | return EXIT_SUCCESS; |
| 5391 | } |
| 5392 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5393 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5394 | * @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] | 5395 | * |
| 5396 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5397 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5398 | * @param[in] item Item to resolve. Type determined by \p type. |
| 5399 | * @param[in] type Type of the unresolved item. |
| 5400 | * @param[in] str String argument. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5401 | * |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 5402 | * @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] | 5403 | */ |
| 5404 | int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5405 | unres_schema_add_str(struct lys_module *mod, struct unres_schema *unres, void *item, enum UNRES_ITEM type, |
| 5406 | const char *str) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5407 | { |
Radek Krejci | 54081ce | 2016-08-12 15:21:47 +0200 | [diff] [blame] | 5408 | int rc; |
| 5409 | const char *dictstr; |
| 5410 | |
| 5411 | dictstr = lydict_insert(mod->ctx, str, 0); |
| 5412 | rc = unres_schema_add_node(mod, unres, item, type, (struct lys_node *)dictstr); |
| 5413 | |
| 5414 | if (rc == -1) { |
| 5415 | lydict_remove(mod->ctx, dictstr); |
| 5416 | } |
| 5417 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5418 | } |
| 5419 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5420 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5421 | * @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] | 5422 | * |
| 5423 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5424 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5425 | * @param[in] item Item to resolve. Type determined by \p type. |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 5426 | * @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] | 5427 | * @param[in] snode Schema node argument. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5428 | * |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 5429 | * @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] | 5430 | */ |
| 5431 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 5432 | 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] | 5433 | struct lys_node *snode) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5434 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5435 | int rc; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 5436 | struct lyxml_elem *yin; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5437 | char *path, *msg; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5438 | |
Michal Vasko | 9bf425b | 2015-10-22 11:42:03 +0200 | [diff] [blame] | 5439 | assert(unres && item && ((type != UNRES_LEAFREF) && (type != UNRES_INSTID) && (type != UNRES_WHEN) |
| 5440 | && (type != UNRES_MUST))); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5441 | |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5442 | ly_vlog_hide(1); |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5443 | rc = resolve_unres_schema_item(mod, item, type, snode, unres); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5444 | ly_vlog_hide(0); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5445 | if (rc != EXIT_FAILURE) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5446 | if (rc == -1 && ly_errno == LY_EVALID) { |
Radek Krejci | 76e15e1 | 2016-06-22 11:02:24 +0200 | [diff] [blame] | 5447 | if (ly_log_level >= LY_LLERR) { |
| 5448 | path = strdup(ly_errpath()); |
| 5449 | msg = strdup(ly_errmsg()); |
| 5450 | LOGERR(LY_EVALID, "%s%s%s%s", msg, path[0] ? " (path: " : "", path[0] ? path : "", path[0] ? ")" : ""); |
| 5451 | free(path); |
| 5452 | free(msg); |
| 5453 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5454 | } |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 5455 | if (type == UNRES_LIST_UNIQ) { |
| 5456 | /* free the allocated structure */ |
| 5457 | free(item); |
| 5458 | } |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5459 | return rc; |
Radek Krejci | f347abc | 2016-06-22 10:18:47 +0200 | [diff] [blame] | 5460 | } else { |
| 5461 | /* erase info about validation errors */ |
| 5462 | ly_errno = LY_SUCCESS; |
| 5463 | ly_vecode = LYVE_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5464 | } |
| 5465 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5466 | print_unres_schema_item_fail(item, type, snode); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5467 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 5468 | /* 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] | 5469 | if (type == UNRES_TYPE_DER || type == UNRES_TYPE_DER_TPDF) { |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 5470 | yin = (struct lyxml_elem *)((struct lys_type *)item)->der; |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 5471 | if (!(yin->flags & LY_YANG_STRUCTURE_FLAG)) { |
| 5472 | lyxml_unlink_elem(mod->ctx, yin, 1); |
| 5473 | ((struct lys_type *)item)->der = (struct lys_tpdf *)yin; |
| 5474 | } |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 5475 | } |
| 5476 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5477 | unres->count++; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 5478 | unres->item = ly_realloc(unres->item, unres->count*sizeof *unres->item); |
| 5479 | if (!unres->item) { |
| 5480 | LOGMEM; |
| 5481 | return -1; |
| 5482 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5483 | unres->item[unres->count-1] = item; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 5484 | unres->type = ly_realloc(unres->type, unres->count*sizeof *unres->type); |
| 5485 | if (!unres->type) { |
| 5486 | LOGMEM; |
| 5487 | return -1; |
| 5488 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5489 | unres->type[unres->count-1] = type; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 5490 | unres->str_snode = ly_realloc(unres->str_snode, unres->count*sizeof *unres->str_snode); |
| 5491 | if (!unres->str_snode) { |
| 5492 | LOGMEM; |
| 5493 | return -1; |
| 5494 | } |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5495 | unres->str_snode[unres->count-1] = snode; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 5496 | unres->module = ly_realloc(unres->module, unres->count*sizeof *unres->module); |
| 5497 | if (!unres->module) { |
| 5498 | LOGMEM; |
| 5499 | return -1; |
| 5500 | } |
| 5501 | unres->module[unres->count-1] = mod; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5502 | |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 5503 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5504 | } |
| 5505 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5506 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5507 | * @brief Duplicate an unres schema item. Logs indirectly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5508 | * |
| 5509 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5510 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5511 | * @param[in] item Old item to be resolved. |
| 5512 | * @param[in] type Type of the old unresolved item. |
| 5513 | * @param[in] new_item New item to use in the duplicate. |
| 5514 | * |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 5515 | * @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] | 5516 | */ |
Michal Vasko | dad1940 | 2015-08-06 09:51:53 +0200 | [diff] [blame] | 5517 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 5518 | 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] | 5519 | { |
| 5520 | int i; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 5521 | struct unres_list_uniq aux_uniq; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5522 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5523 | 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] | 5524 | |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 5525 | /* hack for UNRES_LIST_UNIQ, which stores multiple items behind its item */ |
| 5526 | if (type == UNRES_LIST_UNIQ) { |
| 5527 | aux_uniq.list = item; |
| 5528 | aux_uniq.expr = ((struct unres_list_uniq *)new_item)->expr; |
| 5529 | item = &aux_uniq; |
| 5530 | } |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 5531 | i = unres_schema_find(unres, item, type); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5532 | |
| 5533 | if (i == -1) { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 5534 | if (type == UNRES_LIST_UNIQ) { |
| 5535 | free(new_item); |
| 5536 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 5537 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5538 | } |
| 5539 | |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 5540 | if ((type == UNRES_TYPE_LEAFREF) || (type == UNRES_USES) || (type == UNRES_TYPE_DFLT) || |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 5541 | (type == UNRES_IFFEAT) || (type == UNRES_FEATURE) || (type == UNRES_LIST_UNIQ)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5542 | 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] | 5543 | LOGINT; |
| 5544 | return -1; |
| 5545 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5546 | } else { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5547 | 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] | 5548 | LOGINT; |
| 5549 | return -1; |
| 5550 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5551 | } |
Michal Vasko | dad1940 | 2015-08-06 09:51:53 +0200 | [diff] [blame] | 5552 | |
| 5553 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5554 | } |
| 5555 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5556 | /* does not log */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5557 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 5558 | unres_schema_find(struct unres_schema *unres, void *item, enum UNRES_ITEM type) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5559 | { |
| 5560 | uint32_t ret = -1, i; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 5561 | struct unres_list_uniq *aux_uniq1, *aux_uniq2; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5562 | |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 5563 | for (i = unres->count; i > 0; i--) { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 5564 | if (unres->type[i - 1] != type) { |
| 5565 | continue; |
| 5566 | } |
| 5567 | if (type != UNRES_LIST_UNIQ) { |
| 5568 | if (unres->item[i - 1] == item) { |
| 5569 | ret = i - 1; |
| 5570 | break; |
| 5571 | } |
| 5572 | } else { |
| 5573 | aux_uniq1 = (struct unres_list_uniq *)unres->item[i - 1]; |
| 5574 | aux_uniq2 = (struct unres_list_uniq *)item; |
| 5575 | if ((aux_uniq1->list == aux_uniq2->list) && ly_strequal(aux_uniq1->expr, aux_uniq2->expr, 0)) { |
| 5576 | ret = i - 1; |
| 5577 | break; |
| 5578 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5579 | } |
| 5580 | } |
| 5581 | |
| 5582 | return ret; |
| 5583 | } |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 5584 | |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 5585 | static void |
| 5586 | unres_schema_free_item(struct ly_ctx *ctx, struct unres_schema *unres, uint32_t i) |
| 5587 | { |
| 5588 | struct lyxml_elem *yin; |
| 5589 | struct yang_type *yang; |
| 5590 | |
| 5591 | switch (unres->type[i]) { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 5592 | case UNRES_TYPE_DER_TPDF: |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 5593 | case UNRES_TYPE_DER: |
| 5594 | yin = (struct lyxml_elem *)((struct lys_type *)unres->item[i])->der; |
| 5595 | if (yin->flags & LY_YANG_STRUCTURE_FLAG) { |
| 5596 | yang =(struct yang_type *)yin; |
| 5597 | yang->type->base = yang->base; |
| 5598 | lydict_remove(ctx, yang->name); |
| 5599 | free(yang); |
| 5600 | } else { |
| 5601 | lyxml_free(ctx, yin); |
| 5602 | } |
| 5603 | break; |
| 5604 | case UNRES_IDENT: |
| 5605 | case UNRES_TYPE_IDENTREF: |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 5606 | case UNRES_TYPE_DFLT: |
| 5607 | case UNRES_CHOICE_DFLT: |
| 5608 | case UNRES_LIST_KEYS: |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 5609 | lydict_remove(ctx, (const char *)unres->str_snode[i]); |
| 5610 | break; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 5611 | case UNRES_LIST_UNIQ: |
| 5612 | free(unres->item[i]); |
| 5613 | break; |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 5614 | default: |
| 5615 | break; |
| 5616 | } |
| 5617 | unres->type[i] = UNRES_RESOLVED; |
| 5618 | } |
| 5619 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 5620 | void |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 5621 | unres_schema_free(struct lys_module *module, struct unres_schema **unres) |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 5622 | { |
| 5623 | uint32_t i; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 5624 | unsigned int unresolved = 0; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 5625 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 5626 | if (!unres || !(*unres)) { |
| 5627 | return; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 5628 | } |
| 5629 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 5630 | assert(module || (*unres)->count == 0); |
| 5631 | |
| 5632 | for (i = 0; i < (*unres)->count; ++i) { |
| 5633 | if ((*unres)->module[i] != module) { |
| 5634 | if ((*unres)->type[i] != UNRES_RESOLVED) { |
| 5635 | unresolved++; |
| 5636 | } |
| 5637 | continue; |
| 5638 | } |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 5639 | |
| 5640 | /* free heap memory for the specific item */ |
| 5641 | unres_schema_free_item(module->ctx, *unres, i); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 5642 | } |
| 5643 | |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 5644 | /* free it all */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 5645 | if (!module || (!unresolved && !module->type)) { |
| 5646 | free((*unres)->item); |
| 5647 | free((*unres)->type); |
| 5648 | free((*unres)->str_snode); |
| 5649 | free((*unres)->module); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 5650 | free((*unres)); |
| 5651 | (*unres) = NULL; |
| 5652 | } |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 5653 | } |
| 5654 | |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 5655 | /** |
| 5656 | * @brief Resolve a single unres data item. Logs directly. |
| 5657 | * |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5658 | * @param[in] node Data node to resolve. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5659 | * @param[in] type Type of the unresolved item. |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 5660 | * |
| 5661 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 5662 | */ |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 5663 | int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5664 | resolve_unres_data_item(struct lyd_node *node, enum UNRES_ITEM type) |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 5665 | { |
| 5666 | uint32_t i; |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 5667 | int rc; |
Michal Vasko | 83a6c46 | 2015-10-08 16:43:53 +0200 | [diff] [blame] | 5668 | struct lyd_node_leaf_list *leaf; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 5669 | struct lys_node_leaf *sleaf; |
Michal Vasko | c428084 | 2016-04-19 16:10:42 +0200 | [diff] [blame] | 5670 | struct lyd_node *parent; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 5671 | struct unres_data matches; |
| 5672 | |
| 5673 | memset(&matches, 0, sizeof matches); |
Michal Vasko | 83a6c46 | 2015-10-08 16:43:53 +0200 | [diff] [blame] | 5674 | leaf = (struct lyd_node_leaf_list *)node; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5675 | sleaf = (struct lys_node_leaf *)leaf->schema; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 5676 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5677 | switch (type) { |
| 5678 | case UNRES_LEAFREF: |
| 5679 | assert(sleaf->type.base == LY_TYPE_LEAFREF); |
Michal Vasko | 2471e7f | 2016-04-11 11:00:15 +0200 | [diff] [blame] | 5680 | /* EXIT_FAILURE return keeps leaf->value.lefref NULL, handled later */ |
| 5681 | if (resolve_path_arg_data(node, sleaf->type.info.lref.path, &matches) == -1) { |
| 5682 | return -1; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 5683 | } |
| 5684 | |
| 5685 | /* check that value matches */ |
| 5686 | for (i = 0; i < matches.count; ++i) { |
Radek Krejci | 749190d | 2016-02-18 16:26:25 +0100 | [diff] [blame] | 5687 | 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] | 5688 | leaf->value.leafref = matches.node[i]; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 5689 | break; |
| 5690 | } |
| 5691 | } |
| 5692 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5693 | free(matches.node); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 5694 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5695 | if (!leaf->value.leafref) { |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 5696 | /* reference not found */ |
Michal Vasko | 6ac6828 | 2016-04-11 10:56:47 +0200 | [diff] [blame] | 5697 | LOGVAL(LYE_NOLEAFREF, LY_VLOG_LYD, leaf, sleaf->type.info.lref.path, leaf->value_str); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 5698 | return EXIT_FAILURE; |
| 5699 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5700 | break; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 5701 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5702 | case UNRES_INSTID: |
Michal Vasko | 976a5f2 | 2016-05-18 13:28:42 +0200 | [diff] [blame] | 5703 | 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] | 5704 | ly_errno = 0; |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5705 | leaf->value.instance = resolve_instid(node, leaf->value_str); |
Radek Krejci | 40f17b9 | 2016-02-03 14:30:43 +0100 | [diff] [blame] | 5706 | if (!leaf->value.instance) { |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 5707 | if (ly_errno) { |
| 5708 | return -1; |
| 5709 | } else if (sleaf->type.info.inst.req > -1) { |
Michal Vasko | 6ac6828 | 2016-04-11 10:56:47 +0200 | [diff] [blame] | 5710 | LOGVAL(LYE_NOREQINS, LY_VLOG_LYD, leaf, leaf->value_str); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 5711 | return EXIT_FAILURE; |
| 5712 | } else { |
Radek Krejci | 4ce42be | 2016-02-03 13:04:41 +0100 | [diff] [blame] | 5713 | LOGVRB("There is no instance of \"%s\", but it is not required.", leaf->value_str); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 5714 | } |
| 5715 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5716 | break; |
| 5717 | |
| 5718 | case UNRES_WHEN: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5719 | if ((rc = resolve_when(node))) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5720 | return rc; |
| 5721 | } |
| 5722 | break; |
| 5723 | |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5724 | case UNRES_MUST: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5725 | if ((rc = resolve_must(node))) { |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5726 | return rc; |
| 5727 | } |
| 5728 | break; |
| 5729 | |
Michal Vasko | c428084 | 2016-04-19 16:10:42 +0200 | [diff] [blame] | 5730 | case UNRES_EMPTYCONT: |
| 5731 | do { |
| 5732 | parent = node->parent; |
| 5733 | lyd_free(node); |
| 5734 | node = parent; |
| 5735 | } while (node && (node->schema->nodetype == LYS_CONTAINER) && !node->child |
| 5736 | && !((struct lys_node_container *)node->schema)->presence); |
| 5737 | break; |
| 5738 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5739 | default: |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 5740 | LOGINT; |
| 5741 | return -1; |
| 5742 | } |
| 5743 | |
| 5744 | return EXIT_SUCCESS; |
| 5745 | } |
| 5746 | |
| 5747 | /** |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5748 | * @brief add data unres item |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 5749 | * |
| 5750 | * @param[in] unres Unres data structure to use. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5751 | * @param[in] node Data node to use. |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 5752 | * |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5753 | * @return 0 on success, -1 on error. |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 5754 | */ |
| 5755 | int |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5756 | 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] | 5757 | { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5758 | assert(unres && node); |
Michal Vasko | c428084 | 2016-04-19 16:10:42 +0200 | [diff] [blame] | 5759 | assert((type == UNRES_LEAFREF) || (type == UNRES_INSTID) || (type == UNRES_WHEN) || (type == UNRES_MUST) |
| 5760 | || (type == UNRES_EMPTYCONT)); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 5761 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5762 | unres->count++; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 5763 | unres->node = ly_realloc(unres->node, unres->count * sizeof *unres->node); |
| 5764 | if (!unres->node) { |
| 5765 | LOGMEM; |
| 5766 | return -1; |
| 5767 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5768 | unres->node[unres->count - 1] = node; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 5769 | unres->type = ly_realloc(unres->type, unres->count * sizeof *unres->type); |
| 5770 | if (!unres->type) { |
| 5771 | LOGMEM; |
| 5772 | return -1; |
| 5773 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5774 | unres->type[unres->count - 1] = type; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 5775 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5776 | if (type == UNRES_WHEN) { |
| 5777 | /* remove previous result */ |
| 5778 | node->when_status = LYD_WHEN; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5779 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5780 | |
| 5781 | return EXIT_SUCCESS; |
| 5782 | } |
| 5783 | |
| 5784 | /** |
| 5785 | * @brief Resolve every unres data item in the structure. Logs directly. |
| 5786 | * |
| 5787 | * @param[in] unres Unres data structure to use. |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5788 | * @param[in,out] root Root node of the data tree. If not NULL, auto-delete is performed on false when condition. If |
| 5789 | * NULL and when condition is false the error is raised. |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 5790 | * @param[in] options Parer options |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5791 | * |
| 5792 | * @return EXIT_SUCCESS on success, -1 on error. |
| 5793 | */ |
| 5794 | int |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 5795 | 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] | 5796 | { |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 5797 | 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] | 5798 | int rc, progress; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5799 | char *msg, *path; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5800 | struct lyd_node *parent; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5801 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5802 | assert(unres); |
Radek Krejci | 8ee1b56 | 2016-03-31 10:58:31 +0200 | [diff] [blame] | 5803 | assert((root && (*root)) || (options & LYD_OPT_NOAUTODEL)); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5804 | |
| 5805 | if (!unres->count) { |
| 5806 | return EXIT_SUCCESS; |
| 5807 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5808 | |
Michal Vasko | a0ffcab | 2016-05-02 14:52:08 +0200 | [diff] [blame] | 5809 | LOGVRB("Resolving unresolved data nodes and their constraints..."); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5810 | ly_vlog_hide(1); |
| 5811 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5812 | /* when-stmt first */ |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5813 | ly_errno = LY_SUCCESS; |
| 5814 | ly_vecode = LYVE_SUCCESS; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5815 | do { |
| 5816 | progress = 0; |
| 5817 | for(i = 0; i < unres->count; i++) { |
| 5818 | if (unres->type[i] != UNRES_WHEN) { |
| 5819 | continue; |
| 5820 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 5821 | if (first) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5822 | /* count when-stmt nodes in unres list */ |
| 5823 | when_stmt++; |
| 5824 | } |
| 5825 | |
| 5826 | /* resolve when condition only when all parent when conditions are already resolved */ |
| 5827 | for (parent = unres->node[i]->parent; |
| 5828 | parent && LYD_WHEN_DONE(parent->when_status); |
| 5829 | parent = parent->parent) { |
| 5830 | if (!parent->parent && (parent->when_status & LYD_WHEN_FALSE)) { |
| 5831 | /* the parent node was already unlinked, do not resolve this node, |
| 5832 | * it will be removed anyway, so just mark it as resolved |
| 5833 | */ |
| 5834 | unres->node[i]->when_status |= LYD_WHEN_FALSE; |
| 5835 | unres->type[i] = UNRES_RESOLVED; |
| 5836 | resolved++; |
| 5837 | break; |
| 5838 | } |
| 5839 | } |
| 5840 | if (parent) { |
| 5841 | continue; |
| 5842 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5843 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5844 | rc = resolve_unres_data_item(unres->node[i], unres->type[i]); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5845 | if (!rc) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5846 | if (unres->node[i]->when_status & LYD_WHEN_FALSE) { |
| 5847 | if (!root) { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5848 | /* false when condition */ |
| 5849 | ly_vlog_hide(0); |
Radek Krejci | 76e15e1 | 2016-06-22 11:02:24 +0200 | [diff] [blame] | 5850 | if (ly_log_level >= LY_LLERR) { |
| 5851 | path = strdup(ly_errpath()); |
| 5852 | msg = strdup(ly_errmsg()); |
| 5853 | LOGERR(LY_EVALID, "%s%s%s%s", msg, |
| 5854 | path[0] ? " (path: " : "", path[0] ? path : "", path[0] ? ")" : ""); |
| 5855 | free(path); |
| 5856 | free(msg); |
| 5857 | } |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5858 | return -1; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5859 | } /* follows else */ |
| 5860 | |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 5861 | /* only unlink now, the subtree can contain another nodes stored in the unres list */ |
| 5862 | /* if it has parent non-presence containers that would be empty, we should actually |
| 5863 | * remove the container |
| 5864 | */ |
| 5865 | if (!(options & LYD_OPT_KEEPEMPTYCONT)) { |
| 5866 | for (parent = unres->node[i]; |
| 5867 | parent->parent && parent->parent->schema->nodetype == LYS_CONTAINER; |
| 5868 | parent = parent->parent) { |
| 5869 | if (((struct lys_node_container *)parent->parent->schema)->presence) { |
| 5870 | /* presence container */ |
| 5871 | break; |
| 5872 | } |
| 5873 | if (parent->next || parent->prev != parent) { |
| 5874 | /* non empty (the child we are in and we are going to remove is not the only child) */ |
| 5875 | break; |
| 5876 | } |
| 5877 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 5878 | unres->node[i] = parent; |
| 5879 | } |
| 5880 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5881 | /* auto-delete */ |
| 5882 | LOGVRB("auto-delete node \"%s\" due to when condition (%s)", ly_errpath(), |
| 5883 | ((struct lys_node_leaf *)unres->node[i]->schema)->when->cond); |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 5884 | if (*root && *root == unres->node[i]) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5885 | *root = (*root)->next; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5886 | } |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5887 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5888 | lyd_unlink(unres->node[i]); |
| 5889 | unres->type[i] = UNRES_DELETE; |
| 5890 | del_items++; |
Radek Krejci | 51fd816 | 2016-03-24 15:49:51 +0100 | [diff] [blame] | 5891 | |
| 5892 | /* update the rest of unres items */ |
| 5893 | for (j = 0; j < unres->count; j++) { |
Radek Krejci | 3db819b | 2016-03-24 16:29:48 +0100 | [diff] [blame] | 5894 | if (unres->type[j] == UNRES_RESOLVED || unres->type[j] == UNRES_DELETE) { |
Radek Krejci | 51fd816 | 2016-03-24 15:49:51 +0100 | [diff] [blame] | 5895 | continue; |
| 5896 | } |
| 5897 | |
| 5898 | /* test if the node is in subtree to be deleted */ |
| 5899 | for (parent = unres->node[j]; parent; parent = parent->parent) { |
| 5900 | if (parent == unres->node[i]) { |
| 5901 | /* yes, it is */ |
| 5902 | unres->type[j] = UNRES_RESOLVED; |
| 5903 | resolved++; |
| 5904 | break; |
| 5905 | } |
| 5906 | } |
| 5907 | } |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5908 | } else { |
| 5909 | unres->type[i] = UNRES_RESOLVED; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5910 | } |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5911 | ly_errno = LY_SUCCESS; |
| 5912 | ly_vecode = LYVE_SUCCESS; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5913 | resolved++; |
| 5914 | progress = 1; |
| 5915 | } else if (rc == -1) { |
| 5916 | ly_vlog_hide(0); |
| 5917 | return -1; |
Radek Krejci | c2a180f | 2016-06-22 13:28:16 +0200 | [diff] [blame] | 5918 | } else { |
| 5919 | /* forward reference, erase ly_errno */ |
| 5920 | ly_errno = LY_SUCCESS; |
| 5921 | ly_vecode = LYVE_SUCCESS; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5922 | } |
| 5923 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 5924 | first = 0; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5925 | } while (progress && resolved < when_stmt); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5926 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5927 | /* do we have some unresolved when-stmt? */ |
Radek Krejci | d940d73 | 2016-03-24 16:02:28 +0100 | [diff] [blame] | 5928 | if (when_stmt > resolved) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5929 | ly_vlog_hide(0); |
Radek Krejci | 76e15e1 | 2016-06-22 11:02:24 +0200 | [diff] [blame] | 5930 | if (ly_log_level >= LY_LLERR) { |
| 5931 | path = strdup(ly_errpath()); |
| 5932 | msg = strdup(ly_errmsg()); |
| 5933 | LOGERR(LY_EVALID, "%s%s%s%s", msg, path[0] ? " (path: " : "", path[0] ? path : "", path[0] ? ")" : ""); |
| 5934 | free(path); |
| 5935 | free(msg); |
| 5936 | } |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5937 | return -1; |
| 5938 | } |
| 5939 | |
| 5940 | for (i = 0; del_items && i < unres->count; i++) { |
| 5941 | /* we had some when-stmt resulted to false, so now we have to sanitize the unres list */ |
| 5942 | if (unres->type[i] != UNRES_DELETE) { |
| 5943 | continue; |
| 5944 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 5945 | if (!unres->node[i]) { |
| 5946 | unres->type[i] = UNRES_RESOLVED; |
| 5947 | del_items--; |
| 5948 | continue; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5949 | } |
| 5950 | |
| 5951 | /* really remove the complete subtree */ |
| 5952 | lyd_free(unres->node[i]); |
| 5953 | unres->type[i] = UNRES_RESOLVED; |
| 5954 | del_items--; |
| 5955 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5956 | |
| 5957 | /* rest */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5958 | for (i = 0; i < unres->count; ++i) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5959 | if (unres->type[i] == UNRES_RESOLVED) { |
| 5960 | continue; |
| 5961 | } |
| 5962 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5963 | rc = resolve_unres_data_item(unres->node[i], unres->type[i]); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5964 | if (rc == 0) { |
| 5965 | unres->type[i] = UNRES_RESOLVED; |
| 5966 | resolved++; |
| 5967 | } else if (rc == -1) { |
| 5968 | ly_vlog_hide(0); |
Michal Vasko | 96b846c | 2016-05-18 13:28:58 +0200 | [diff] [blame] | 5969 | /* print only this last error */ |
| 5970 | resolve_unres_data_item(unres->node[i], unres->type[i]); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5971 | return -1; |
| 5972 | } |
| 5973 | } |
| 5974 | |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5975 | ly_vlog_hide(0); |
| 5976 | if (resolved < unres->count) { |
| 5977 | /* try to resolve the unresolved data again, it will not resolve anything, but it will print |
| 5978 | * all the validation errors |
| 5979 | */ |
| 5980 | for (i = 0; i < unres->count; ++i) { |
| 5981 | if (unres->type[i] == UNRES_RESOLVED) { |
| 5982 | continue; |
| 5983 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5984 | resolve_unres_data_item(unres->node[i], unres->type[i]); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5985 | } |
| 5986 | return -1; |
| 5987 | } |
| 5988 | |
Michal Vasko | a0ffcab | 2016-05-02 14:52:08 +0200 | [diff] [blame] | 5989 | LOGVRB("All data nodes and constraints resolved."); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5990 | unres->count = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5991 | return EXIT_SUCCESS; |
| 5992 | } |