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) { |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 558 | return -parsed+ret; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 559 | } else if (model && !*model) { |
| 560 | return -parsed; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | parsed += ret; |
| 564 | id += ret; |
| 565 | |
| 566 | if ((id[0] == '[') && has_predicate) { |
| 567 | *has_predicate = 1; |
| 568 | } |
| 569 | |
| 570 | return parsed; |
| 571 | } |
| 572 | |
| 573 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 574 | * @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] | 575 | * (which are mandatory) are actually model names. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 576 | * |
| 577 | * predicate = "[" *WSP (predicate-expr / pos) *WSP "]" |
| 578 | * predicate-expr = (node-identifier / ".") *WSP "=" *WSP |
| 579 | * ((DQUOTE string DQUOTE) / |
| 580 | * (SQUOTE string SQUOTE)) |
| 581 | * pos = non-negative-integer-value |
| 582 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 583 | * @param[in] id Identifier to use. |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 584 | * @param[out] model Points to the model name. |
| 585 | * @param[out] mod_len Length of the model name. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 586 | * @param[out] name Points to the node name. Can be identifier (from node-identifier), "." or pos. |
| 587 | * @param[out] nam_len Length of the node name. |
| 588 | * @param[out] value Value the node-identifier must have (string from the grammar), |
| 589 | * NULL if there is not any. |
| 590 | * @param[out] val_len Length of the value, 0 if there is not any. |
| 591 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 592 | * |
| 593 | * @return Number of characters successfully parsed, |
| 594 | * positive on success, negative on failure. |
| 595 | */ |
| 596 | static int |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 597 | parse_predicate(const char *id, const char **model, int *mod_len, const char **name, int *nam_len, |
| 598 | const char **value, int *val_len, int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 599 | { |
| 600 | const char *ptr; |
| 601 | int parsed = 0, ret; |
| 602 | char quote; |
| 603 | |
| 604 | assert(id); |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 605 | if (model) { |
| 606 | *model = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 607 | } |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 608 | if (mod_len) { |
| 609 | *mod_len = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 610 | } |
| 611 | if (name) { |
| 612 | *name = NULL; |
| 613 | } |
| 614 | if (nam_len) { |
| 615 | *nam_len = 0; |
| 616 | } |
| 617 | if (value) { |
| 618 | *value = NULL; |
| 619 | } |
| 620 | if (val_len) { |
| 621 | *val_len = 0; |
| 622 | } |
| 623 | if (has_predicate) { |
| 624 | *has_predicate = 0; |
| 625 | } |
| 626 | |
| 627 | if (id[0] != '[') { |
| 628 | return -parsed; |
| 629 | } |
| 630 | |
| 631 | ++parsed; |
| 632 | ++id; |
| 633 | |
| 634 | while (isspace(id[0])) { |
| 635 | ++parsed; |
| 636 | ++id; |
| 637 | } |
| 638 | |
| 639 | /* pos */ |
| 640 | if (isdigit(id[0])) { |
| 641 | if (name) { |
| 642 | *name = id; |
| 643 | } |
| 644 | |
| 645 | if (id[0] == '0') { |
| 646 | ++parsed; |
| 647 | ++id; |
| 648 | |
| 649 | if (isdigit(id[0])) { |
| 650 | return -parsed; |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | while (isdigit(id[0])) { |
| 655 | ++parsed; |
| 656 | ++id; |
| 657 | } |
| 658 | |
| 659 | if (nam_len) { |
| 660 | *nam_len = id-(*name); |
| 661 | } |
| 662 | |
| 663 | /* "." */ |
| 664 | } else if (id[0] == '.') { |
| 665 | if (name) { |
| 666 | *name = id; |
| 667 | } |
| 668 | if (nam_len) { |
| 669 | *nam_len = 1; |
| 670 | } |
| 671 | |
| 672 | ++parsed; |
| 673 | ++id; |
| 674 | |
| 675 | /* node-identifier */ |
| 676 | } else { |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 677 | 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] | 678 | return -parsed+ret; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 679 | } else if (model && !*model) { |
| 680 | return -parsed; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 681 | } |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 682 | |
| 683 | parsed += ret; |
| 684 | id += ret; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | while (isspace(id[0])) { |
| 688 | ++parsed; |
| 689 | ++id; |
| 690 | } |
| 691 | |
| 692 | if (id[0] != '=') { |
| 693 | return -parsed; |
| 694 | } |
| 695 | |
| 696 | ++parsed; |
| 697 | ++id; |
| 698 | |
| 699 | while (isspace(id[0])) { |
| 700 | ++parsed; |
| 701 | ++id; |
| 702 | } |
| 703 | |
| 704 | /* ((DQUOTE string DQUOTE) / (SQUOTE string SQUOTE)) */ |
| 705 | if ((id[0] == '\"') || (id[0] == '\'')) { |
| 706 | quote = id[0]; |
| 707 | |
| 708 | ++parsed; |
| 709 | ++id; |
| 710 | |
| 711 | if ((ptr = strchr(id, quote)) == NULL) { |
| 712 | return -parsed; |
| 713 | } |
| 714 | ret = ptr-id; |
| 715 | |
| 716 | if (value) { |
| 717 | *value = id; |
| 718 | } |
| 719 | if (val_len) { |
| 720 | *val_len = ret; |
| 721 | } |
| 722 | |
| 723 | parsed += ret+1; |
| 724 | id += ret+1; |
| 725 | } else { |
| 726 | return -parsed; |
| 727 | } |
| 728 | |
| 729 | while (isspace(id[0])) { |
| 730 | ++parsed; |
| 731 | ++id; |
| 732 | } |
| 733 | |
| 734 | if (id[0] != ']') { |
| 735 | return -parsed; |
| 736 | } |
| 737 | |
| 738 | ++parsed; |
| 739 | ++id; |
| 740 | |
| 741 | if ((id[0] == '[') && has_predicate) { |
| 742 | *has_predicate = 1; |
| 743 | } |
| 744 | |
| 745 | return parsed; |
| 746 | } |
| 747 | |
| 748 | /** |
| 749 | * @brief Parse schema-nodeid. |
| 750 | * |
| 751 | * schema-nodeid = absolute-schema-nodeid / |
| 752 | * descendant-schema-nodeid |
| 753 | * absolute-schema-nodeid = 1*("/" node-identifier) |
Michal Vasko | 4893535 | 2016-03-29 11:52:36 +0200 | [diff] [blame] | 754 | * descendant-schema-nodeid = ["." "/"] |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 755 | * node-identifier |
| 756 | * absolute-schema-nodeid |
| 757 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 758 | * @param[in] id Identifier to use. |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 759 | * @param[out] mod_name Points to the module name, NULL if there is not any. |
| 760 | * @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] | 761 | * @param[out] name Points to the node name. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 762 | * @param[out] nam_len Length of the node name. |
| 763 | * @param[out] is_relative Flag to mark whether the nodeid is absolute or descendant. Must be -1 |
| 764 | * on the first call, must not be changed between consecutive calls. |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 765 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. It cannot be |
| 766 | * based on the grammar, in those cases use NULL. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 767 | * |
| 768 | * @return Number of characters successfully parsed, |
| 769 | * positive on success, negative on failure. |
| 770 | */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 771 | int |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 772 | 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] | 773 | int *is_relative, int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 774 | { |
| 775 | int parsed = 0, ret; |
| 776 | |
| 777 | assert(id); |
| 778 | assert(is_relative); |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 779 | if (mod_name) { |
| 780 | *mod_name = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 781 | } |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 782 | if (mod_name_len) { |
| 783 | *mod_name_len = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 784 | } |
| 785 | if (name) { |
| 786 | *name = NULL; |
| 787 | } |
| 788 | if (nam_len) { |
| 789 | *nam_len = 0; |
| 790 | } |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 791 | if (has_predicate) { |
| 792 | *has_predicate = 0; |
| 793 | } |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 794 | |
| 795 | if (id[0] != '/') { |
| 796 | if (*is_relative != -1) { |
| 797 | return -parsed; |
| 798 | } else { |
| 799 | *is_relative = 1; |
| 800 | } |
Michal Vasko | 4893535 | 2016-03-29 11:52:36 +0200 | [diff] [blame] | 801 | if (!strncmp(id, "./", 2)) { |
| 802 | parsed += 2; |
| 803 | id += 2; |
| 804 | } |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 805 | } else { |
| 806 | if (*is_relative == -1) { |
| 807 | *is_relative = 0; |
| 808 | } |
| 809 | ++parsed; |
| 810 | ++id; |
| 811 | } |
| 812 | |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 813 | 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] | 814 | return -parsed+ret; |
| 815 | } |
| 816 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 817 | parsed += ret; |
| 818 | id += ret; |
| 819 | |
| 820 | if ((id[0] == '[') && has_predicate) { |
| 821 | *has_predicate = 1; |
| 822 | } |
| 823 | |
| 824 | return parsed; |
| 825 | } |
| 826 | |
| 827 | /** |
| 828 | * @brief Parse schema predicate (special format internally used). |
| 829 | * |
| 830 | * predicate = "[" *WSP predicate-expr *WSP "]" |
| 831 | * predicate-expr = identifier / key-with-value |
| 832 | * key-with-value = identifier *WSP "=" *WSP |
| 833 | * ((DQUOTE string DQUOTE) / |
| 834 | * (SQUOTE string SQUOTE)) |
| 835 | * |
| 836 | * @param[in] id Identifier to use. |
| 837 | * @param[out] name Points to the list key name. |
| 838 | * @param[out] nam_len Length of \p name. |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 839 | * @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] | 840 | * @param[out] val_len Length of \p value. |
| 841 | * @param[out] has_predicate Flag to mark whether there is another predicate specified. |
| 842 | */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 843 | int |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 844 | parse_schema_list_predicate(const char *id, const char **name, int *nam_len, const char **value, int *val_len, |
| 845 | int *has_predicate) |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 846 | { |
| 847 | const char *ptr; |
| 848 | int parsed = 0, ret; |
| 849 | char quote; |
| 850 | |
| 851 | assert(id); |
| 852 | if (name) { |
| 853 | *name = NULL; |
| 854 | } |
| 855 | if (nam_len) { |
| 856 | *nam_len = 0; |
| 857 | } |
| 858 | if (value) { |
| 859 | *value = NULL; |
| 860 | } |
| 861 | if (val_len) { |
| 862 | *val_len = 0; |
| 863 | } |
| 864 | if (has_predicate) { |
| 865 | *has_predicate = 0; |
| 866 | } |
| 867 | |
| 868 | if (id[0] != '[') { |
| 869 | return -parsed; |
| 870 | } |
| 871 | |
| 872 | ++parsed; |
| 873 | ++id; |
| 874 | |
| 875 | while (isspace(id[0])) { |
| 876 | ++parsed; |
| 877 | ++id; |
| 878 | } |
| 879 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 880 | /* identifier */ |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 881 | if ((ret = parse_identifier(id)) < 1) { |
| 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 | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 958 | * @brief Resolve (find) a data node based on a schema-nodeid. |
| 959 | * |
| 960 | * Used for resolving unique statements - so id is expected to be relative and local (without reference to a different |
| 961 | * module). |
| 962 | * |
| 963 | */ |
| 964 | struct lyd_node * |
| 965 | resolve_data_descendant_schema_nodeid(const char *nodeid, struct lyd_node *start) |
| 966 | { |
| 967 | char *str, *token, *p; |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 968 | struct lyd_node *result = NULL, *iter; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 969 | const struct lys_node *schema = NULL; |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 970 | int shorthand = 0; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 971 | |
| 972 | assert(nodeid && start); |
| 973 | |
| 974 | if (nodeid[0] == '/') { |
| 975 | return NULL; |
| 976 | } |
| 977 | |
| 978 | str = p = strdup(nodeid); |
| 979 | if (!str) { |
| 980 | LOGMEM; |
| 981 | return NULL; |
| 982 | } |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 983 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 984 | while (p) { |
| 985 | token = p; |
| 986 | p = strchr(p, '/'); |
| 987 | if (p) { |
| 988 | *p = '\0'; |
| 989 | p++; |
| 990 | } |
| 991 | |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 992 | if (p) { |
| 993 | /* inner node */ |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 994 | if (resolve_descendant_schema_nodeid(token, schema ? schema->child : start->schema, |
| 995 | LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_LEAF, &schema, 0) |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 996 | || !schema) { |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 997 | result = NULL; |
| 998 | break; |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 999 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1000 | |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1001 | if (schema->nodetype & (LYS_CHOICE | LYS_CASE)) { |
| 1002 | continue; |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1003 | } else if (schema->parent->nodetype == LYS_CHOICE) { |
| 1004 | /* shorthand case */ |
| 1005 | if (!shorthand) { |
| 1006 | shorthand = 1; |
| 1007 | schema = schema->parent; |
| 1008 | continue; |
| 1009 | } else { |
| 1010 | shorthand = 0; |
| 1011 | if (schema->nodetype == LYS_LEAF) { |
| 1012 | /* should not be here, since we have leaf, which is not a shorthand nor final node */ |
| 1013 | result = NULL; |
| 1014 | break; |
| 1015 | } |
| 1016 | } |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1017 | } |
| 1018 | } else { |
| 1019 | /* final node */ |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1020 | if (resolve_descendant_schema_nodeid(token, schema ? schema->child : start->schema, LYS_LEAF, &schema, shorthand ? 0 : 1) |
| 1021 | || !schema) { |
| 1022 | result = NULL; |
| 1023 | break; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1024 | } |
| 1025 | } |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1026 | LY_TREE_FOR(result ? result->child : start, iter) { |
| 1027 | if (iter->schema == schema) { |
| 1028 | /* move in data tree according to returned schema */ |
| 1029 | result = iter; |
| 1030 | break; |
| 1031 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1032 | } |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1033 | if (!iter) { |
| 1034 | /* instance not found */ |
| 1035 | result = NULL; |
| 1036 | break; |
| 1037 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1038 | } |
| 1039 | free(str); |
| 1040 | |
| 1041 | return result; |
| 1042 | } |
| 1043 | |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1044 | /* |
| 1045 | * 0 - ok (done) |
| 1046 | * 1 - continue |
| 1047 | * 2 - break |
| 1048 | * -1 - error |
| 1049 | */ |
| 1050 | static int |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1051 | 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] | 1052 | const struct lys_module *module, const char *mod_name, int mod_name_len, |
| 1053 | const struct lys_node **start) |
| 1054 | { |
| 1055 | const struct lys_module *prefix_mod; |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1056 | int sh = 0; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1057 | |
| 1058 | /* module check */ |
| 1059 | prefix_mod = lys_get_import_module(module, NULL, 0, mod_name, mod_name_len); |
| 1060 | if (!prefix_mod) { |
| 1061 | return -1; |
| 1062 | } |
| 1063 | if (prefix_mod != lys_node_module(sibling)) { |
| 1064 | return 1; |
| 1065 | } |
| 1066 | |
| 1067 | /* check for shorthand cases - then 'start' does not change */ |
| 1068 | if (sibling->parent && sibling->parent->nodetype == LYS_CHOICE && sibling->nodetype != LYS_CASE) { |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1069 | if (*shorthand != -1) { |
| 1070 | *shorthand = *shorthand ? 0 : 1; |
| 1071 | } |
| 1072 | sh = 1; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1073 | } |
| 1074 | |
| 1075 | /* the result node? */ |
| 1076 | if (!id[0]) { |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1077 | if (*shorthand == 1) { |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1078 | return 1; |
| 1079 | } |
| 1080 | return 0; |
| 1081 | } |
| 1082 | |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1083 | if (!sh) { |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1084 | /* move down the tree, if possible */ |
| 1085 | if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYXML)) { |
| 1086 | return -1; |
| 1087 | } |
| 1088 | *start = sibling->child; |
| 1089 | } |
| 1090 | |
| 1091 | return 2; |
| 1092 | } |
| 1093 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1094 | /* start - relative, module - absolute, -1 error, EXIT_SUCCESS ok (but ret can still be NULL), >0 unexpected char on ret - 1 */ |
| 1095 | int |
| 1096 | resolve_augment_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_module *module, |
| 1097 | const struct lys_node **ret) |
| 1098 | { |
| 1099 | const char *name, *mod_name, *id; |
| 1100 | const struct lys_node *sibling; |
| 1101 | int r, nam_len, mod_name_len, is_relative = -1; |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1102 | int8_t shorthand = 0; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1103 | /* 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] | 1104 | const struct lys_module *start_mod; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1105 | |
| 1106 | assert(nodeid && (start || module) && !(start && module) && ret); |
| 1107 | |
| 1108 | id = nodeid; |
| 1109 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1110 | 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] | 1111 | return ((id - nodeid) - r) + 1; |
| 1112 | } |
| 1113 | id += r; |
| 1114 | |
| 1115 | if ((is_relative && !start) || (!is_relative && !module)) { |
| 1116 | return -1; |
| 1117 | } |
| 1118 | |
| 1119 | /* descendant-schema-nodeid */ |
| 1120 | if (is_relative) { |
Michal Vasko | 4c06fd8 | 2016-02-17 13:43:38 +0100 | [diff] [blame] | 1121 | module = start_mod = start->module; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1122 | |
| 1123 | /* absolute-schema-nodeid */ |
| 1124 | } else { |
| 1125 | 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] | 1126 | if (!start_mod) { |
| 1127 | return -1; |
| 1128 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1129 | start = start_mod->data; |
| 1130 | } |
| 1131 | |
| 1132 | while (1) { |
| 1133 | sibling = NULL; |
| 1134 | while ((sibling = lys_getnext(sibling, lys_parent(start), start_mod, |
| 1135 | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT))) { |
| 1136 | /* name match */ |
| 1137 | if ((sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len]) |
Michal Vasko | 044697a | 2016-03-24 13:21:12 +0100 | [diff] [blame] | 1138 | || ((sibling->nodetype == LYS_INPUT) && !strncmp(name, "input", nam_len) && (nam_len == 5)) |
| 1139 | || ((sibling->nodetype == LYS_OUTPUT) && !strncmp(name, "output", nam_len) && (nam_len == 6))) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1140 | |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1141 | r = schema_nodeid_siblingcheck(sibling, &shorthand, id, module, mod_name, mod_name_len, &start); |
| 1142 | if (r == 0) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1143 | *ret = sibling; |
| 1144 | return EXIT_SUCCESS; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1145 | } else if (r == 1) { |
| 1146 | continue; |
| 1147 | } else if (r == 2) { |
| 1148 | break; |
| 1149 | } else { |
| 1150 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1151 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1152 | } |
| 1153 | } |
| 1154 | |
| 1155 | /* no match */ |
| 1156 | if (!sibling) { |
Michal Vasko | a426fef | 2016-03-07 10:47:31 +0100 | [diff] [blame] | 1157 | *ret = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1158 | return EXIT_SUCCESS; |
| 1159 | } |
| 1160 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1161 | 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] | 1162 | return ((id - nodeid) - r) + 1; |
| 1163 | } |
| 1164 | id += r; |
| 1165 | } |
| 1166 | |
| 1167 | /* cannot get here */ |
| 1168 | LOGINT; |
| 1169 | return -1; |
| 1170 | } |
| 1171 | |
| 1172 | /* unique, refine, -1 error, EXIT_SUCCESS ok (but ret can still be NULL), >0 unexpected char on ret - 1 */ |
| 1173 | int |
| 1174 | resolve_descendant_schema_nodeid(const char *nodeid, const struct lys_node *start, int ret_nodetype, |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1175 | const struct lys_node **ret, int check_shorthand) |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1176 | { |
| 1177 | const char *name, *mod_name, *id; |
| 1178 | const struct lys_node *sibling; |
| 1179 | int r, nam_len, mod_name_len, is_relative = -1; |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1180 | int8_t shorthand = check_shorthand ? 0 : -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1181 | /* 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] | 1182 | const struct lys_module *module; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1183 | |
| 1184 | assert(nodeid && start && ret); |
| 1185 | assert(!(ret_nodetype & (LYS_USES | LYS_AUGMENT)) && ((ret_nodetype == LYS_GROUPING) || !(ret_nodetype & LYS_GROUPING))); |
| 1186 | |
| 1187 | id = nodeid; |
| 1188 | module = start->module; |
| 1189 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1190 | 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] | 1191 | return ((id - nodeid) - r) + 1; |
| 1192 | } |
| 1193 | id += r; |
| 1194 | |
| 1195 | if (!is_relative) { |
| 1196 | return -1; |
| 1197 | } |
| 1198 | |
| 1199 | while (1) { |
| 1200 | sibling = NULL; |
| 1201 | while ((sibling = lys_getnext(sibling, lys_parent(start), module, |
| 1202 | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE))) { |
| 1203 | /* name match */ |
| 1204 | if (sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len]) { |
| 1205 | |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1206 | r = schema_nodeid_siblingcheck(sibling, &shorthand, id, module, mod_name, mod_name_len, &start); |
| 1207 | if (r == 0) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1208 | if (!(sibling->nodetype & ret_nodetype)) { |
| 1209 | /* wrong node type, too bad */ |
| 1210 | continue; |
| 1211 | } |
| 1212 | *ret = sibling; |
| 1213 | return EXIT_SUCCESS; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1214 | } else if (r == 1) { |
| 1215 | continue; |
| 1216 | } else if (r == 2) { |
| 1217 | break; |
| 1218 | } else { |
| 1219 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1220 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1221 | } |
| 1222 | } |
| 1223 | |
| 1224 | /* no match */ |
| 1225 | if (!sibling) { |
Michal Vasko | a426fef | 2016-03-07 10:47:31 +0100 | [diff] [blame] | 1226 | *ret = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1227 | return EXIT_SUCCESS; |
| 1228 | } |
| 1229 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1230 | 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] | 1231 | return ((id - nodeid) - r) + 1; |
| 1232 | } |
| 1233 | id += r; |
| 1234 | } |
| 1235 | |
| 1236 | /* cannot get here */ |
| 1237 | LOGINT; |
| 1238 | return -1; |
| 1239 | } |
| 1240 | |
| 1241 | /* choice default */ |
| 1242 | int |
| 1243 | resolve_choice_default_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_node **ret) |
| 1244 | { |
| 1245 | /* cannot actually be a path */ |
| 1246 | if (strchr(nodeid, '/')) { |
| 1247 | return -1; |
| 1248 | } |
| 1249 | |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1250 | return resolve_descendant_schema_nodeid(nodeid, start, LYS_NO_RPC_NOTIF_NODE, ret, 1); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1251 | } |
| 1252 | |
| 1253 | /* uses, -1 error, EXIT_SUCCESS ok (but ret can still be NULL), >0 unexpected char on ret - 1 */ |
| 1254 | static int |
| 1255 | resolve_uses_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_node_grp **ret) |
| 1256 | { |
| 1257 | const struct lys_module *module; |
| 1258 | const char *mod_prefix, *name; |
| 1259 | int i, mod_prefix_len, nam_len; |
| 1260 | |
| 1261 | /* parse the identifier, it must be parsed on one call */ |
| 1262 | if (((i = parse_node_identifier(nodeid, &mod_prefix, &mod_prefix_len, &name, &nam_len)) < 1) || nodeid[i]) { |
| 1263 | return -i + 1; |
| 1264 | } |
| 1265 | |
| 1266 | module = lys_get_import_module(start->module, mod_prefix, mod_prefix_len, NULL, 0); |
| 1267 | if (!module) { |
| 1268 | return -1; |
| 1269 | } |
| 1270 | if (module != start->module) { |
| 1271 | start = module->data; |
| 1272 | } |
| 1273 | |
| 1274 | *ret = lys_find_grouping_up(name, (struct lys_node *)start); |
| 1275 | |
| 1276 | return EXIT_SUCCESS; |
| 1277 | } |
| 1278 | |
| 1279 | int |
| 1280 | resolve_absolute_schema_nodeid(const char *nodeid, const struct lys_module *module, int ret_nodetype, |
| 1281 | const struct lys_node **ret) |
| 1282 | { |
| 1283 | const char *name, *mod_name, *id; |
| 1284 | const struct lys_node *sibling, *start; |
| 1285 | int r, nam_len, mod_name_len, is_relative = -1; |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1286 | int8_t shorthand = 0; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1287 | const struct lys_module *abs_start_mod; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1288 | |
| 1289 | assert(nodeid && module && ret); |
| 1290 | assert(!(ret_nodetype & (LYS_USES | LYS_AUGMENT)) && ((ret_nodetype == LYS_GROUPING) || !(ret_nodetype & LYS_GROUPING))); |
| 1291 | |
| 1292 | id = nodeid; |
| 1293 | start = module->data; |
| 1294 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1295 | 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] | 1296 | return ((id - nodeid) - r) + 1; |
| 1297 | } |
| 1298 | id += r; |
| 1299 | |
| 1300 | if (is_relative) { |
| 1301 | return -1; |
| 1302 | } |
| 1303 | |
| 1304 | 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] | 1305 | if (!abs_start_mod) { |
| 1306 | return -1; |
| 1307 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1308 | |
| 1309 | while (1) { |
| 1310 | sibling = NULL; |
| 1311 | while ((sibling = lys_getnext(sibling, lys_parent(start), abs_start_mod, LYS_GETNEXT_WITHCHOICE |
| 1312 | | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT | LYS_GETNEXT_WITHGROUPING))) { |
| 1313 | /* name match */ |
Michal Vasko | 044697a | 2016-03-24 13:21:12 +0100 | [diff] [blame] | 1314 | if ((sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len]) |
| 1315 | || ((sibling->nodetype == LYS_INPUT) && !strncmp(name, "input", nam_len) && (nam_len == 5)) |
| 1316 | || ((sibling->nodetype == LYS_OUTPUT) && !strncmp(name, "output", nam_len) && (nam_len == 6))) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1317 | |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1318 | r = schema_nodeid_siblingcheck(sibling, &shorthand, id, module, mod_name, mod_name_len, &start); |
| 1319 | if (r == 0) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1320 | if (!(sibling->nodetype & ret_nodetype)) { |
| 1321 | /* wrong node type, too bad */ |
| 1322 | continue; |
| 1323 | } |
| 1324 | *ret = sibling; |
| 1325 | return EXIT_SUCCESS; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1326 | } else if (r == 1) { |
| 1327 | continue; |
| 1328 | } else if (r == 2) { |
| 1329 | break; |
| 1330 | } else { |
| 1331 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1332 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1333 | } |
| 1334 | } |
| 1335 | |
| 1336 | /* no match */ |
| 1337 | if (!sibling) { |
Michal Vasko | a426fef | 2016-03-07 10:47:31 +0100 | [diff] [blame] | 1338 | *ret = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1339 | return EXIT_SUCCESS; |
| 1340 | } |
| 1341 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1342 | 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] | 1343 | return ((id - nodeid) - r) + 1; |
| 1344 | } |
| 1345 | id += r; |
| 1346 | } |
| 1347 | |
| 1348 | /* cannot get here */ |
| 1349 | LOGINT; |
| 1350 | return -1; |
| 1351 | } |
| 1352 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1353 | static int |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1354 | 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] | 1355 | { |
| 1356 | const char *name; |
| 1357 | int nam_len, has_predicate, i; |
| 1358 | |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1359 | if ((i = parse_schema_list_predicate(predicate, &name, &nam_len, NULL, NULL, &has_predicate)) < 1) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 1360 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-i], &predicate[-i]); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1361 | return -1; |
| 1362 | } |
| 1363 | |
| 1364 | predicate += i; |
| 1365 | *parsed += i; |
| 1366 | |
| 1367 | for (i = 0; i < list->keys_size; ++i) { |
| 1368 | if (!strncmp(list->keys[i]->name, name, nam_len) && !list->keys[i]->name[nam_len]) { |
| 1369 | break; |
| 1370 | } |
| 1371 | } |
| 1372 | |
| 1373 | if (i == list->keys_size) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 1374 | LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1375 | return -1; |
| 1376 | } |
| 1377 | |
| 1378 | /* more predicates? */ |
| 1379 | if (has_predicate) { |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1380 | return resolve_json_schema_list_predicate(predicate, list, parsed); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1381 | } |
| 1382 | |
| 1383 | return 0; |
| 1384 | } |
| 1385 | |
Michal Vasko | 9fd98e2 | 2016-04-07 15:44:19 +0200 | [diff] [blame] | 1386 | /* cannot return LYS_GROUPING, LYS_AUGMENT, LYS_USES, logs directly |
| 1387 | * 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] | 1388 | const struct lys_node * |
Michal Vasko | 9fd98e2 | 2016-04-07 15:44:19 +0200 | [diff] [blame] | 1389 | resolve_json_schema_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] | 1390 | { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 1391 | char *module_name = ly_buf(), *buf_backup = NULL, *str; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1392 | const char *name, *mod_name, *id; |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1393 | const struct lys_node *sibling; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1394 | 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] | 1395 | /* 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] | 1396 | const struct lys_module *prefix_mod, *module, *prev_mod; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1397 | |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1398 | assert(nodeid && (ctx || start)); |
| 1399 | if (!ctx) { |
| 1400 | ctx = start->module->ctx; |
| 1401 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1402 | |
| 1403 | id = nodeid; |
| 1404 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1405 | 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] | 1406 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1407 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1408 | } |
| 1409 | id += r; |
| 1410 | |
| 1411 | if (is_relative) { |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1412 | assert(start); |
| 1413 | start = start->child; |
| 1414 | if (!start) { |
| 1415 | /* no descendants, fail for sure */ |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 1416 | str = strndup(nodeid, (name + nam_len) - nodeid); |
| 1417 | LOGVAL(LYE_PATH_INNODE, LY_VLOG_STR, str); |
| 1418 | free(str); |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1419 | return NULL; |
| 1420 | } |
| 1421 | module = start->module; |
| 1422 | } else { |
| 1423 | if (!mod_name) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 1424 | str = strndup(nodeid, (name + nam_len) - nodeid); |
| 1425 | LOGVAL(LYE_PATH_MISSMOD, LY_VLOG_STR, nodeid); |
| 1426 | free(str); |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1427 | return NULL; |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 1428 | } else if (mod_name_len > LY_BUF_SIZE - 1) { |
| 1429 | LOGINT; |
| 1430 | return NULL; |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1431 | } |
| 1432 | |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 1433 | if (ly_buf_used && module_name[0]) { |
| 1434 | buf_backup = strndup(module_name, LY_BUF_SIZE - 1); |
| 1435 | } |
| 1436 | ly_buf_used++; |
| 1437 | |
Michal Vasko | df3f1ab | 2016-04-01 15:07:22 +0200 | [diff] [blame] | 1438 | memmove(module_name, mod_name, mod_name_len); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 1439 | module_name[mod_name_len] = '\0'; |
| 1440 | module = ly_ctx_get_module(ctx, module_name, NULL); |
| 1441 | |
| 1442 | if (buf_backup) { |
| 1443 | /* return previous internal buffer content */ |
| 1444 | strcpy(module_name, buf_backup); |
| 1445 | free(buf_backup); |
| 1446 | buf_backup = NULL; |
| 1447 | } |
| 1448 | ly_buf_used--; |
| 1449 | |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1450 | if (!module) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 1451 | str = strndup(nodeid, (mod_name + mod_name_len) - nodeid); |
| 1452 | LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str); |
| 1453 | free(str); |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1454 | return NULL; |
| 1455 | } |
| 1456 | start = module->data; |
| 1457 | |
| 1458 | /* now it's as if there was no module name */ |
| 1459 | mod_name = NULL; |
| 1460 | mod_name_len = 0; |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1461 | } |
| 1462 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1463 | prev_mod = module; |
| 1464 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1465 | while (1) { |
| 1466 | sibling = NULL; |
Michal Vasko | 9fd98e2 | 2016-04-07 15:44:19 +0200 | [diff] [blame] | 1467 | while ((sibling = lys_getnext(sibling, lys_parent(start), module, (data_nodeid ? |
| 1468 | 0 : LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT)))) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1469 | /* name match */ |
Michal Vasko | 044697a | 2016-03-24 13:21:12 +0100 | [diff] [blame] | 1470 | if ((sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len]) |
| 1471 | || ((sibling->nodetype == LYS_INPUT) && !strncmp(name, "input", nam_len) && (nam_len == 5)) |
| 1472 | || ((sibling->nodetype == LYS_OUTPUT) && !strncmp(name, "output", nam_len) && (nam_len == 6))) { |
Michal Vasko | 9fd98e2 | 2016-04-07 15:44:19 +0200 | [diff] [blame] | 1473 | |
| 1474 | /* data RPC input/output check */ |
| 1475 | if ((data_nodeid == 1) && sibling->parent && (sibling->parent->nodetype == LYS_OUTPUT)) { |
| 1476 | continue; |
| 1477 | } else if ((data_nodeid == 2) && sibling->parent && (sibling->parent->nodetype == LYS_INPUT)) { |
| 1478 | continue; |
| 1479 | } |
| 1480 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1481 | /* module check */ |
| 1482 | if (mod_name) { |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 1483 | if (mod_name_len > LY_BUF_SIZE - 1) { |
Michal Vasko | 8757e7c | 2016-03-15 10:41:30 +0100 | [diff] [blame] | 1484 | LOGINT; |
| 1485 | return NULL; |
| 1486 | } |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 1487 | |
| 1488 | if (ly_buf_used && module_name[0]) { |
| 1489 | buf_backup = strndup(module_name, LY_BUF_SIZE - 1); |
| 1490 | } |
| 1491 | ly_buf_used++; |
| 1492 | |
Michal Vasko | df3f1ab | 2016-04-01 15:07:22 +0200 | [diff] [blame] | 1493 | memmove(module_name, mod_name, mod_name_len); |
Michal Vasko | 8757e7c | 2016-03-15 10:41:30 +0100 | [diff] [blame] | 1494 | module_name[mod_name_len] = '\0'; |
| 1495 | /* will also find an augment module */ |
| 1496 | prefix_mod = ly_ctx_get_module(ctx, module_name, NULL); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 1497 | |
| 1498 | if (buf_backup) { |
| 1499 | /* return previous internal buffer content */ |
Michal Vasko | 888a129 | 2016-04-05 12:08:54 +0200 | [diff] [blame] | 1500 | strncpy(module_name, buf_backup, LY_BUF_SIZE - 1); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 1501 | free(buf_backup); |
| 1502 | buf_backup = NULL; |
| 1503 | } |
| 1504 | ly_buf_used--; |
| 1505 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1506 | if (!prefix_mod) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 1507 | str = strndup(nodeid, (mod_name + mod_name_len) - nodeid); |
| 1508 | LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str); |
| 1509 | free(str); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1510 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1511 | } |
| 1512 | } else { |
| 1513 | prefix_mod = prev_mod; |
| 1514 | } |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 1515 | if (prefix_mod != lys_node_module(sibling)) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1516 | continue; |
| 1517 | } |
| 1518 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1519 | /* do we have some predicates on it? */ |
| 1520 | if (has_predicate) { |
| 1521 | r = 0; |
| 1522 | if (sibling->nodetype != LYS_LIST) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 1523 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1524 | return NULL; |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1525 | } else if (resolve_json_schema_list_predicate(id, (const struct lys_node_list *)sibling, &r)) { |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1526 | return NULL; |
| 1527 | } |
| 1528 | id += r; |
| 1529 | } |
| 1530 | |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1531 | /* check for shorthand cases - then 'start' does not change */ |
| 1532 | if (sibling->parent && sibling->parent->nodetype == LYS_CHOICE && sibling->nodetype != LYS_CASE) { |
| 1533 | shorthand = ~shorthand; |
| 1534 | } |
| 1535 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1536 | /* the result node? */ |
| 1537 | if (!id[0]) { |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1538 | if (shorthand) { |
| 1539 | /* wrong path for shorthand */ |
| 1540 | sibling = NULL; |
| 1541 | break; |
| 1542 | } |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1543 | return sibling; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1544 | } |
| 1545 | |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1546 | if (!shorthand) { |
Michal Vasko | 7dc71d0 | 2016-03-15 10:42:28 +0100 | [diff] [blame] | 1547 | /* move down the tree, if possible */ |
| 1548 | if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYXML)) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 1549 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
Michal Vasko | 7dc71d0 | 2016-03-15 10:42:28 +0100 | [diff] [blame] | 1550 | return NULL; |
| 1551 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1552 | start = sibling->child; |
| 1553 | } |
| 1554 | |
| 1555 | /* update prev mod */ |
| 1556 | prev_mod = start->module; |
| 1557 | break; |
| 1558 | } |
| 1559 | } |
| 1560 | |
| 1561 | /* no match */ |
| 1562 | if (!sibling) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 1563 | str = strndup(nodeid, (name + nam_len) - nodeid); |
| 1564 | LOGVAL(LYE_PATH_INNODE, LY_VLOG_STR, str); |
| 1565 | free(str); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1566 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1567 | } |
| 1568 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1569 | 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] | 1570 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1571 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1572 | } |
| 1573 | id += r; |
| 1574 | } |
| 1575 | |
| 1576 | /* cannot get here */ |
| 1577 | LOGINT; |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1578 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1579 | } |
| 1580 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1581 | static int |
| 1582 | resolve_partial_json_data_list_predicate(const char *predicate, const char *node_name, struct lyd_node *node, int *parsed) |
| 1583 | { |
| 1584 | const char *name, *value; |
| 1585 | int nam_len, val_len, has_predicate = 1, r; |
| 1586 | uint16_t i; |
| 1587 | struct ly_set *keys; |
| 1588 | |
Radek Krejci | 61a86c6 | 2016-03-24 11:06:44 +0100 | [diff] [blame] | 1589 | assert(node); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1590 | assert(node->schema->nodetype == LYS_LIST); |
| 1591 | |
| 1592 | keys = lyd_get_list_keys(node); |
| 1593 | |
| 1594 | for (i = 0; i < keys->number; ++i) { |
| 1595 | if (!has_predicate) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 1596 | LOGVAL(LYE_PATH_MISSKEY, LY_VLOG_NONE, NULL, node_name); |
Radek Krejci | ba5cec6 | 2016-03-24 10:40:29 +0100 | [diff] [blame] | 1597 | goto error; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1598 | } |
| 1599 | |
| 1600 | if ((r = parse_schema_list_predicate(predicate, &name, &nam_len, &value, &val_len, &has_predicate)) < 1) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 1601 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-r], &predicate[-r]); |
Radek Krejci | ba5cec6 | 2016-03-24 10:40:29 +0100 | [diff] [blame] | 1602 | goto error; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1603 | } |
| 1604 | |
| 1605 | predicate += r; |
| 1606 | *parsed += r; |
| 1607 | |
Radek Krejci | 8f08df1 | 2016-03-21 11:11:30 +0100 | [diff] [blame] | 1608 | if (strncmp(keys->set.d[i]->schema->name, name, nam_len) || keys->set.d[i]->schema->name[nam_len]) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 1609 | LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name); |
Radek Krejci | ba5cec6 | 2016-03-24 10:40:29 +0100 | [diff] [blame] | 1610 | goto error; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1611 | } |
| 1612 | |
| 1613 | /* value does not match */ |
Radek Krejci | 8f08df1 | 2016-03-21 11:11:30 +0100 | [diff] [blame] | 1614 | if (strncmp(((struct lyd_node_leaf_list *)keys->set.d[i])->value_str, value, val_len) |
| 1615 | || ((struct lyd_node_leaf_list *)keys->set.d[i])->value_str[val_len]) { |
Radek Krejci | ba5cec6 | 2016-03-24 10:40:29 +0100 | [diff] [blame] | 1616 | ly_set_free(keys); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1617 | return 1; |
| 1618 | } |
| 1619 | } |
| 1620 | |
Radek Krejci | ba5cec6 | 2016-03-24 10:40:29 +0100 | [diff] [blame] | 1621 | ly_set_free(keys); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1622 | if (has_predicate) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 1623 | LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1624 | return -1; |
| 1625 | } |
| 1626 | |
| 1627 | return 0; |
Radek Krejci | ba5cec6 | 2016-03-24 10:40:29 +0100 | [diff] [blame] | 1628 | |
| 1629 | error: |
| 1630 | ly_set_free(keys); |
| 1631 | return -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1632 | } |
| 1633 | |
| 1634 | struct lyd_node * |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 1635 | resolve_partial_json_data_nodeid(const char *nodeid, const char *llist_value, struct lyd_node *start, int options, |
| 1636 | int *parsed) |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1637 | { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 1638 | char *module_name = ly_buf(), *buf_backup = NULL, *str; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1639 | const char *id, *mod_name, *name; |
| 1640 | 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] | 1641 | struct lyd_node *sibling, *last_match = NULL; |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 1642 | struct lyd_node_leaf_list *llist; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1643 | const struct lys_module *prefix_mod, *prev_mod; |
| 1644 | struct ly_ctx *ctx; |
| 1645 | |
| 1646 | assert(nodeid && start && parsed); |
| 1647 | |
| 1648 | ctx = start->schema->module->ctx; |
| 1649 | id = nodeid; |
| 1650 | |
| 1651 | 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] | 1652 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 1653 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1654 | return NULL; |
| 1655 | } |
| 1656 | id += r; |
| 1657 | /* add it to parsed only after the data node was actually found */ |
| 1658 | last_parsed = r; |
| 1659 | |
| 1660 | if (is_relative) { |
| 1661 | prev_mod = start->schema->module; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1662 | start = start->child; |
| 1663 | } else { |
| 1664 | for (; start->parent; start = start->parent); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1665 | prev_mod = start->schema->module; |
| 1666 | } |
| 1667 | |
| 1668 | while (1) { |
| 1669 | LY_TREE_FOR(start, sibling) { |
Michal Vasko | 2411b94 | 2016-03-23 13:50:03 +0100 | [diff] [blame] | 1670 | /* RPC data check, return simply invalid argument, because the data tree is invalid */ |
| 1671 | if (lys_parent(sibling->schema)) { |
| 1672 | if (options & LYD_PATH_OPT_OUTPUT) { |
| 1673 | if (lys_parent(sibling->schema)->nodetype == LYS_INPUT) { |
Michal Vasko | 9ffd899 | 2016-03-24 13:42:29 +0100 | [diff] [blame] | 1674 | LOGERR(LY_EINVAL, "%s: provided data tree includes some RPC input nodes."); |
Michal Vasko | 2411b94 | 2016-03-23 13:50:03 +0100 | [diff] [blame] | 1675 | *parsed = -1; |
| 1676 | return NULL; |
| 1677 | } |
| 1678 | } else { |
| 1679 | if (lys_parent(sibling->schema)->nodetype == LYS_OUTPUT) { |
Michal Vasko | 9ffd899 | 2016-03-24 13:42:29 +0100 | [diff] [blame] | 1680 | LOGERR(LY_EINVAL, "%s: provided data tree includes some RPC output nodes."); |
Michal Vasko | 2411b94 | 2016-03-23 13:50:03 +0100 | [diff] [blame] | 1681 | *parsed = -1; |
| 1682 | return NULL; |
| 1683 | } |
| 1684 | } |
| 1685 | } |
| 1686 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1687 | /* name match */ |
| 1688 | if (!strncmp(name, sibling->schema->name, nam_len) && !sibling->schema->name[nam_len]) { |
| 1689 | |
| 1690 | /* module check */ |
| 1691 | if (mod_name) { |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 1692 | if (mod_name_len > LY_BUF_SIZE - 1) { |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1693 | LOGINT; |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 1694 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1695 | return NULL; |
| 1696 | } |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 1697 | |
| 1698 | if (ly_buf_used && module_name[0]) { |
| 1699 | buf_backup = strndup(module_name, LY_BUF_SIZE - 1); |
| 1700 | } |
| 1701 | ly_buf_used++; |
| 1702 | |
Michal Vasko | df3f1ab | 2016-04-01 15:07:22 +0200 | [diff] [blame] | 1703 | memmove(module_name, mod_name, mod_name_len); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1704 | module_name[mod_name_len] = '\0'; |
| 1705 | /* will also find an augment module */ |
| 1706 | prefix_mod = ly_ctx_get_module(ctx, module_name, NULL); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 1707 | |
| 1708 | if (buf_backup) { |
| 1709 | /* return previous internal buffer content */ |
Michal Vasko | 888a129 | 2016-04-05 12:08:54 +0200 | [diff] [blame] | 1710 | strncpy(module_name, buf_backup, LY_BUF_SIZE - 1); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 1711 | free(buf_backup); |
| 1712 | buf_backup = NULL; |
| 1713 | } |
| 1714 | ly_buf_used--; |
| 1715 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1716 | if (!prefix_mod) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 1717 | str = strndup(nodeid, (mod_name + mod_name_len) - nodeid); |
| 1718 | LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str); |
| 1719 | free(str); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 1720 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1721 | return NULL; |
| 1722 | } |
| 1723 | } else { |
| 1724 | prefix_mod = prev_mod; |
| 1725 | } |
| 1726 | if (prefix_mod != lys_node_module(sibling->schema)) { |
| 1727 | continue; |
| 1728 | } |
| 1729 | |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 1730 | /* leaf-list, did we find it with the correct value or not? */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1731 | if (sibling->schema->nodetype == LYS_LEAFLIST) { |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 1732 | llist = (struct lyd_node_leaf_list *)sibling; |
| 1733 | if ((!llist_value && llist->value_str && llist->value_str[0]) |
| 1734 | || (llist_value && strcmp(llist_value, llist->value_str))) { |
| 1735 | continue; |
| 1736 | } |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1737 | } |
| 1738 | |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 1739 | /* list, we need predicates'n'stuff then */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1740 | if (sibling->schema->nodetype == LYS_LIST) { |
| 1741 | r = 0; |
| 1742 | if (!has_predicate) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 1743 | LOGVAL(LYE_PATH_MISSKEY, LY_VLOG_NONE, NULL, name); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 1744 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1745 | return NULL; |
| 1746 | } |
| 1747 | ret = resolve_partial_json_data_list_predicate(id, name, sibling, &r); |
| 1748 | if (ret == -1) { |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 1749 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1750 | return NULL; |
| 1751 | } else if (ret == 1) { |
| 1752 | /* this list instance does not match */ |
| 1753 | continue; |
| 1754 | } |
| 1755 | id += r; |
| 1756 | last_parsed += r; |
| 1757 | } |
| 1758 | |
| 1759 | *parsed += last_parsed; |
| 1760 | |
| 1761 | /* the result node? */ |
| 1762 | if (!id[0]) { |
| 1763 | return sibling; |
| 1764 | } |
| 1765 | |
| 1766 | /* move down the tree, if possible */ |
Michal Vasko | c6c52cf | 2016-03-29 11:53:04 +0200 | [diff] [blame] | 1767 | if (sibling->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYXML)) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 1768 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 1769 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1770 | return NULL; |
| 1771 | } |
Michal Vasko | c6c52cf | 2016-03-29 11:53:04 +0200 | [diff] [blame] | 1772 | last_match = sibling; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1773 | start = sibling->child; |
| 1774 | if (start) { |
| 1775 | prev_mod = start->schema->module; |
| 1776 | } |
| 1777 | break; |
| 1778 | } |
| 1779 | } |
| 1780 | |
| 1781 | /* no match, return last match */ |
| 1782 | if (!sibling) { |
| 1783 | return last_match; |
| 1784 | } |
| 1785 | |
| 1786 | 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] | 1787 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 1788 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1789 | return NULL; |
| 1790 | } |
| 1791 | id += r; |
| 1792 | last_parsed = r; |
| 1793 | } |
| 1794 | |
| 1795 | /* cannot get here */ |
| 1796 | LOGINT; |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 1797 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1798 | return NULL; |
| 1799 | } |
| 1800 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1801 | /** |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 1802 | * @brief Resolves length or range intervals. Does not log. |
| 1803 | * Syntax is assumed to be correct, *local_intv MUST be NULL. |
| 1804 | * |
| 1805 | * @param[in] str_restr The restriction as a string. |
| 1806 | * @param[in] type The type of the restriction. |
| 1807 | * @param[in] superior_restr Flag whether to check superior |
| 1808 | * types. |
| 1809 | * @param[out] local_intv The final interval structure. |
| 1810 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1811 | * @return EXIT_SUCCESS on succes, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 1812 | */ |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 1813 | int |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 1814 | resolve_len_ran_interval(const char *str_restr, struct lys_type *type, int superior_restr, |
| 1815 | struct len_ran_intv** local_intv) |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 1816 | { |
| 1817 | /* 0 - unsigned, 1 - signed, 2 - floating point */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1818 | int kind, rc = EXIT_SUCCESS; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 1819 | int64_t local_smin, local_smax; |
| 1820 | uint64_t local_umin, local_umax; |
| 1821 | long double local_fmin, local_fmax; |
| 1822 | const char *seg_ptr, *ptr; |
Michal Vasko | e01eca5 | 2015-08-13 14:42:02 +0200 | [diff] [blame] | 1823 | struct len_ran_intv *tmp_local_intv = NULL, *tmp_intv, *intv = NULL; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 1824 | |
| 1825 | switch (type->base) { |
| 1826 | case LY_TYPE_BINARY: |
| 1827 | kind = 0; |
| 1828 | local_umin = 0; |
| 1829 | local_umax = 18446744073709551615UL; |
| 1830 | |
| 1831 | if (!str_restr && type->info.binary.length) { |
| 1832 | str_restr = type->info.binary.length->expr; |
| 1833 | } |
| 1834 | break; |
| 1835 | case LY_TYPE_DEC64: |
| 1836 | kind = 2; |
| 1837 | local_fmin = -9223372036854775808.0; |
| 1838 | local_fmin /= 1 << type->info.dec64.dig; |
| 1839 | local_fmax = 9223372036854775807.0; |
| 1840 | local_fmax /= 1 << type->info.dec64.dig; |
| 1841 | |
| 1842 | if (!str_restr && type->info.dec64.range) { |
| 1843 | str_restr = type->info.dec64.range->expr; |
| 1844 | } |
| 1845 | break; |
| 1846 | case LY_TYPE_INT8: |
| 1847 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 1848 | local_smin = __INT64_C(-128); |
| 1849 | local_smax = __INT64_C(127); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 1850 | |
| 1851 | if (!str_restr && type->info.num.range) { |
| 1852 | str_restr = type->info.num.range->expr; |
| 1853 | } |
| 1854 | break; |
| 1855 | case LY_TYPE_INT16: |
| 1856 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 1857 | local_smin = __INT64_C(-32768); |
| 1858 | local_smax = __INT64_C(32767); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 1859 | |
| 1860 | if (!str_restr && type->info.num.range) { |
| 1861 | str_restr = type->info.num.range->expr; |
| 1862 | } |
| 1863 | break; |
| 1864 | case LY_TYPE_INT32: |
| 1865 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 1866 | local_smin = __INT64_C(-2147483648); |
| 1867 | local_smax = __INT64_C(2147483647); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 1868 | |
| 1869 | if (!str_restr && type->info.num.range) { |
| 1870 | str_restr = type->info.num.range->expr; |
| 1871 | } |
| 1872 | break; |
| 1873 | case LY_TYPE_INT64: |
| 1874 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 1875 | local_smin = __INT64_C(-9223372036854775807) - __INT64_C(1); |
| 1876 | local_smax = __INT64_C(9223372036854775807); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 1877 | |
| 1878 | if (!str_restr && type->info.num.range) { |
| 1879 | str_restr = type->info.num.range->expr; |
| 1880 | } |
| 1881 | break; |
| 1882 | case LY_TYPE_UINT8: |
| 1883 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 1884 | local_umin = __UINT64_C(0); |
| 1885 | local_umax = __UINT64_C(255); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 1886 | |
| 1887 | if (!str_restr && type->info.num.range) { |
| 1888 | str_restr = type->info.num.range->expr; |
| 1889 | } |
| 1890 | break; |
| 1891 | case LY_TYPE_UINT16: |
| 1892 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 1893 | local_umin = __UINT64_C(0); |
| 1894 | local_umax = __UINT64_C(65535); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 1895 | |
| 1896 | if (!str_restr && type->info.num.range) { |
| 1897 | str_restr = type->info.num.range->expr; |
| 1898 | } |
| 1899 | break; |
| 1900 | case LY_TYPE_UINT32: |
| 1901 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 1902 | local_umin = __UINT64_C(0); |
| 1903 | local_umax = __UINT64_C(4294967295); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 1904 | |
| 1905 | if (!str_restr && type->info.num.range) { |
| 1906 | str_restr = type->info.num.range->expr; |
| 1907 | } |
| 1908 | break; |
| 1909 | case LY_TYPE_UINT64: |
| 1910 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 1911 | local_umin = __UINT64_C(0); |
| 1912 | local_umax = __UINT64_C(18446744073709551615); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 1913 | |
| 1914 | if (!str_restr && type->info.num.range) { |
| 1915 | str_restr = type->info.num.range->expr; |
| 1916 | } |
| 1917 | break; |
| 1918 | case LY_TYPE_STRING: |
| 1919 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 1920 | local_umin = __UINT64_C(0); |
| 1921 | local_umax = __UINT64_C(18446744073709551615); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 1922 | |
| 1923 | if (!str_restr && type->info.str.length) { |
| 1924 | str_restr = type->info.str.length->expr; |
| 1925 | } |
| 1926 | break; |
| 1927 | default: |
| 1928 | LOGINT; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1929 | return -1; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 1930 | } |
| 1931 | |
| 1932 | /* process superior types */ |
| 1933 | if (type->der && superior_restr) { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 1934 | if (resolve_len_ran_interval(NULL, &type->der->type, superior_restr, &intv)) { |
| 1935 | LOGINT; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1936 | return -1; |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 1937 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 1938 | assert(!intv || (intv->kind == kind)); |
| 1939 | } |
| 1940 | |
| 1941 | if (!str_restr) { |
| 1942 | /* we are validating data and not have any restriction, but a superior type might have */ |
| 1943 | if (type->der && !superior_restr && !intv) { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 1944 | if (resolve_len_ran_interval(NULL, &type->der->type, superior_restr, &intv)) { |
| 1945 | LOGINT; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1946 | return -1; |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 1947 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 1948 | assert(!intv || (intv->kind == kind)); |
| 1949 | } |
| 1950 | *local_intv = intv; |
| 1951 | return EXIT_SUCCESS; |
| 1952 | } |
| 1953 | |
| 1954 | /* adjust local min and max */ |
| 1955 | if (intv) { |
| 1956 | tmp_intv = intv; |
| 1957 | |
| 1958 | if (kind == 0) { |
| 1959 | local_umin = tmp_intv->value.uval.min; |
| 1960 | } else if (kind == 1) { |
| 1961 | local_smin = tmp_intv->value.sval.min; |
| 1962 | } else if (kind == 2) { |
| 1963 | local_fmin = tmp_intv->value.fval.min; |
| 1964 | } |
| 1965 | |
| 1966 | while (tmp_intv->next) { |
| 1967 | tmp_intv = tmp_intv->next; |
| 1968 | } |
| 1969 | |
| 1970 | if (kind == 0) { |
| 1971 | local_umax = tmp_intv->value.uval.max; |
| 1972 | } else if (kind == 1) { |
| 1973 | local_smax = tmp_intv->value.sval.max; |
| 1974 | } else if (kind == 2) { |
| 1975 | local_fmax = tmp_intv->value.fval.max; |
| 1976 | } |
| 1977 | } |
| 1978 | |
| 1979 | /* finally parse our restriction */ |
| 1980 | seg_ptr = str_restr; |
| 1981 | while (1) { |
Michal Vasko | e01eca5 | 2015-08-13 14:42:02 +0200 | [diff] [blame] | 1982 | if (!*local_intv && !tmp_local_intv) { |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 1983 | *local_intv = malloc(sizeof **local_intv); |
| 1984 | tmp_local_intv = *local_intv; |
| 1985 | } else { |
| 1986 | tmp_local_intv->next = malloc(sizeof **local_intv); |
| 1987 | tmp_local_intv = tmp_local_intv->next; |
| 1988 | } |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 1989 | if (!tmp_local_intv) { |
| 1990 | LOGMEM; |
| 1991 | return -1; |
| 1992 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 1993 | |
| 1994 | tmp_local_intv->kind = kind; |
| 1995 | tmp_local_intv->next = NULL; |
| 1996 | |
| 1997 | /* min */ |
| 1998 | ptr = seg_ptr; |
| 1999 | while (isspace(ptr[0])) { |
| 2000 | ++ptr; |
| 2001 | } |
| 2002 | if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) { |
| 2003 | if (kind == 0) { |
| 2004 | tmp_local_intv->value.uval.min = atoll(ptr); |
| 2005 | } else if (kind == 1) { |
| 2006 | tmp_local_intv->value.sval.min = atoll(ptr); |
| 2007 | } else if (kind == 2) { |
| 2008 | tmp_local_intv->value.fval.min = atoll(ptr); |
| 2009 | } |
| 2010 | |
| 2011 | if ((ptr[0] == '+') || (ptr[0] == '-')) { |
| 2012 | ++ptr; |
| 2013 | } |
| 2014 | while (isdigit(ptr[0])) { |
| 2015 | ++ptr; |
| 2016 | } |
| 2017 | } else if (!strncmp(ptr, "min", 3)) { |
| 2018 | if (kind == 0) { |
| 2019 | tmp_local_intv->value.uval.min = local_umin; |
| 2020 | } else if (kind == 1) { |
| 2021 | tmp_local_intv->value.sval.min = local_smin; |
| 2022 | } else if (kind == 2) { |
| 2023 | tmp_local_intv->value.fval.min = local_fmin; |
| 2024 | } |
| 2025 | |
| 2026 | ptr += 3; |
| 2027 | } else if (!strncmp(ptr, "max", 3)) { |
| 2028 | if (kind == 0) { |
| 2029 | tmp_local_intv->value.uval.min = local_umax; |
| 2030 | } else if (kind == 1) { |
| 2031 | tmp_local_intv->value.sval.min = local_smax; |
| 2032 | } else if (kind == 2) { |
| 2033 | tmp_local_intv->value.fval.min = local_fmax; |
| 2034 | } |
| 2035 | |
| 2036 | ptr += 3; |
| 2037 | } else { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2038 | LOGINT; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2039 | rc = -1; |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2040 | goto cleanup; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2041 | } |
| 2042 | |
| 2043 | while (isspace(ptr[0])) { |
| 2044 | ptr++; |
| 2045 | } |
| 2046 | |
| 2047 | /* no interval or interval */ |
| 2048 | if ((ptr[0] == '|') || !ptr[0]) { |
| 2049 | if (kind == 0) { |
| 2050 | tmp_local_intv->value.uval.max = tmp_local_intv->value.uval.min; |
| 2051 | } else if (kind == 1) { |
| 2052 | tmp_local_intv->value.sval.max = tmp_local_intv->value.sval.min; |
| 2053 | } else if (kind == 2) { |
| 2054 | tmp_local_intv->value.fval.max = tmp_local_intv->value.fval.min; |
| 2055 | } |
| 2056 | } else if (!strncmp(ptr, "..", 2)) { |
| 2057 | /* skip ".." */ |
| 2058 | ptr += 2; |
| 2059 | while (isspace(ptr[0])) { |
| 2060 | ++ptr; |
| 2061 | } |
| 2062 | |
| 2063 | /* max */ |
| 2064 | if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) { |
| 2065 | if (kind == 0) { |
| 2066 | tmp_local_intv->value.uval.max = atoll(ptr); |
| 2067 | } else if (kind == 1) { |
| 2068 | tmp_local_intv->value.sval.max = atoll(ptr); |
| 2069 | } else if (kind == 2) { |
| 2070 | tmp_local_intv->value.fval.max = atoll(ptr); |
| 2071 | } |
| 2072 | } else if (!strncmp(ptr, "max", 3)) { |
| 2073 | if (kind == 0) { |
| 2074 | tmp_local_intv->value.uval.max = local_umax; |
| 2075 | } else if (kind == 1) { |
| 2076 | tmp_local_intv->value.sval.max = local_smax; |
| 2077 | } else if (kind == 2) { |
| 2078 | tmp_local_intv->value.fval.max = local_fmax; |
| 2079 | } |
| 2080 | } else { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2081 | LOGINT; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2082 | rc = -1; |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2083 | goto cleanup; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2084 | } |
| 2085 | } else { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2086 | LOGINT; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2087 | rc = -1; |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2088 | goto cleanup; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2089 | } |
| 2090 | |
| 2091 | /* next segment (next OR) */ |
| 2092 | seg_ptr = strchr(seg_ptr, '|'); |
| 2093 | if (!seg_ptr) { |
| 2094 | break; |
| 2095 | } |
| 2096 | seg_ptr++; |
| 2097 | } |
| 2098 | |
| 2099 | /* check local restrictions against superior ones */ |
| 2100 | if (intv) { |
| 2101 | tmp_intv = intv; |
| 2102 | tmp_local_intv = *local_intv; |
| 2103 | |
| 2104 | while (tmp_local_intv && tmp_intv) { |
| 2105 | /* reuse local variables */ |
| 2106 | if (kind == 0) { |
| 2107 | local_umin = tmp_local_intv->value.uval.min; |
| 2108 | local_umax = tmp_local_intv->value.uval.max; |
| 2109 | |
| 2110 | /* it must be in this interval */ |
| 2111 | if ((local_umin >= tmp_intv->value.uval.min) && (local_umin <= tmp_intv->value.uval.max)) { |
| 2112 | /* this interval is covered, next one */ |
| 2113 | if (local_umax <= tmp_intv->value.uval.max) { |
| 2114 | tmp_local_intv = tmp_local_intv->next; |
| 2115 | continue; |
| 2116 | /* ascending order of restrictions -> fail */ |
| 2117 | } else { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2118 | rc = -1; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2119 | goto cleanup; |
| 2120 | } |
| 2121 | } |
| 2122 | } else if (kind == 1) { |
| 2123 | local_smin = tmp_local_intv->value.sval.min; |
| 2124 | local_smax = tmp_local_intv->value.sval.max; |
| 2125 | |
| 2126 | if ((local_smin >= tmp_intv->value.sval.min) && (local_smin <= tmp_intv->value.sval.max)) { |
| 2127 | if (local_smax <= tmp_intv->value.sval.max) { |
| 2128 | tmp_local_intv = tmp_local_intv->next; |
| 2129 | continue; |
| 2130 | } else { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2131 | rc = -1; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2132 | goto cleanup; |
| 2133 | } |
| 2134 | } |
| 2135 | } else if (kind == 2) { |
| 2136 | local_fmin = tmp_local_intv->value.fval.min; |
| 2137 | local_fmax = tmp_local_intv->value.fval.max; |
| 2138 | |
| 2139 | if ((local_fmin >= tmp_intv->value.fval.min) && (local_fmin <= tmp_intv->value.fval.max)) { |
| 2140 | if (local_fmax <= tmp_intv->value.fval.max) { |
| 2141 | tmp_local_intv = tmp_local_intv->next; |
| 2142 | continue; |
| 2143 | } else { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2144 | rc = -1; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2145 | goto cleanup; |
| 2146 | } |
| 2147 | } |
| 2148 | } |
| 2149 | |
| 2150 | tmp_intv = tmp_intv->next; |
| 2151 | } |
| 2152 | |
| 2153 | /* some interval left uncovered -> fail */ |
| 2154 | if (tmp_local_intv) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2155 | rc = -1; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2156 | } |
| 2157 | |
| 2158 | } |
| 2159 | |
| 2160 | cleanup: |
| 2161 | while (intv) { |
| 2162 | tmp_intv = intv->next; |
| 2163 | free(intv); |
| 2164 | intv = tmp_intv; |
| 2165 | } |
| 2166 | |
| 2167 | /* fail */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2168 | if (rc) { |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2169 | while (*local_intv) { |
| 2170 | tmp_local_intv = (*local_intv)->next; |
| 2171 | free(*local_intv); |
| 2172 | *local_intv = tmp_local_intv; |
| 2173 | } |
| 2174 | } |
| 2175 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2176 | return rc; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2177 | } |
| 2178 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2179 | /** |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 2180 | * @brief Resolve a typedef, return only resolved typedefs if derived. Does not log. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2181 | * |
| 2182 | * @param[in] name Typedef name. |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2183 | * @param[in] mod_name Typedef name module name. |
| 2184 | * @param[in] module Main module. |
| 2185 | * @param[in] parent Parent of the resolved type definition. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2186 | * @param[out] ret Pointer to the resolved typedef. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2187 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2188 | * @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] | 2189 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2190 | int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 2191 | resolve_superior_type(const char *name, const char *mod_name, const struct lys_module *module, |
| 2192 | const struct lys_node *parent, struct lys_tpdf **ret) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2193 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2194 | int i, j; |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 2195 | struct lys_tpdf *tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2196 | int tpdf_size; |
| 2197 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2198 | if (!mod_name) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2199 | /* no prefix, try built-in types */ |
| 2200 | for (i = 1; i < LY_DATA_TYPE_COUNT; i++) { |
| 2201 | if (!strcmp(ly_types[i].def->name, name)) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2202 | if (ret) { |
| 2203 | *ret = ly_types[i].def; |
| 2204 | } |
| 2205 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2206 | } |
| 2207 | } |
| 2208 | } else { |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2209 | if (!strcmp(mod_name, module->name)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2210 | /* prefix refers to the current module, ignore it */ |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2211 | mod_name = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2212 | } |
| 2213 | } |
| 2214 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2215 | if (!mod_name && parent) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2216 | /* search in local typedefs */ |
| 2217 | while (parent) { |
| 2218 | switch (parent->nodetype) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2219 | case LYS_CONTAINER: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 2220 | tpdf_size = ((struct lys_node_container *)parent)->tpdf_size; |
| 2221 | tpdf = ((struct lys_node_container *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2222 | break; |
| 2223 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2224 | case LYS_LIST: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 2225 | tpdf_size = ((struct lys_node_list *)parent)->tpdf_size; |
| 2226 | tpdf = ((struct lys_node_list *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2227 | break; |
| 2228 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2229 | case LYS_GROUPING: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 2230 | tpdf_size = ((struct lys_node_grp *)parent)->tpdf_size; |
| 2231 | tpdf = ((struct lys_node_grp *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2232 | break; |
| 2233 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2234 | case LYS_RPC: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 2235 | tpdf_size = ((struct lys_node_rpc *)parent)->tpdf_size; |
| 2236 | tpdf = ((struct lys_node_rpc *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2237 | break; |
| 2238 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2239 | case LYS_NOTIF: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 2240 | tpdf_size = ((struct lys_node_notif *)parent)->tpdf_size; |
| 2241 | tpdf = ((struct lys_node_notif *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2242 | break; |
| 2243 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2244 | case LYS_INPUT: |
| 2245 | case LYS_OUTPUT: |
Radek Krejci | 4608ada | 2015-08-05 16:04:37 +0200 | [diff] [blame] | 2246 | tpdf_size = ((struct lys_node_rpc_inout *)parent)->tpdf_size; |
| 2247 | tpdf = ((struct lys_node_rpc_inout *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2248 | break; |
| 2249 | |
| 2250 | default: |
| 2251 | parent = parent->parent; |
| 2252 | continue; |
| 2253 | } |
| 2254 | |
| 2255 | for (i = 0; i < tpdf_size; i++) { |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 2256 | if (!strcmp(tpdf[i].name, name) && tpdf[i].type.base) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2257 | if (ret) { |
| 2258 | *ret = &tpdf[i]; |
| 2259 | } |
| 2260 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2261 | } |
| 2262 | } |
| 2263 | |
| 2264 | parent = parent->parent; |
| 2265 | } |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2266 | } else { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2267 | /* get module where to search */ |
Michal Vasko | b6729c6 | 2015-10-21 12:09:47 +0200 | [diff] [blame] | 2268 | module = lys_get_import_module(module, NULL, 0, mod_name, 0); |
Michal Vasko | c935fff | 2015-08-17 14:02:06 +0200 | [diff] [blame] | 2269 | if (!module) { |
| 2270 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2271 | } |
| 2272 | } |
| 2273 | |
| 2274 | /* search in top level typedefs */ |
| 2275 | for (i = 0; i < module->tpdf_size; i++) { |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 2276 | if (!strcmp(module->tpdf[i].name, name) && module->tpdf[i].type.base) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2277 | if (ret) { |
| 2278 | *ret = &module->tpdf[i]; |
| 2279 | } |
| 2280 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2281 | } |
| 2282 | } |
| 2283 | |
| 2284 | /* search in submodules */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2285 | for (i = 0; i < module->inc_size && module->inc[i].submodule; i++) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2286 | for (j = 0; j < module->inc[i].submodule->tpdf_size; j++) { |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 2287 | if (!strcmp(module->inc[i].submodule->tpdf[j].name, name) && module->inc[i].submodule->tpdf[j].type.base) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2288 | if (ret) { |
| 2289 | *ret = &module->inc[i].submodule->tpdf[j]; |
| 2290 | } |
| 2291 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2292 | } |
| 2293 | } |
| 2294 | } |
| 2295 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2296 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2297 | } |
| 2298 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2299 | /** |
| 2300 | * @brief Check the default \p value of the \p type. Logs directly. |
| 2301 | * |
| 2302 | * @param[in] type Type definition to use. |
| 2303 | * @param[in] value Default value to check. |
Michal Vasko | 5682640 | 2016-03-02 11:11:37 +0100 | [diff] [blame] | 2304 | * @param[in] module Type module. |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2305 | * |
| 2306 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 2307 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2308 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2309 | 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] | 2310 | { |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2311 | struct lyd_node_leaf_list node; |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 2312 | int ret = EXIT_SUCCESS; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2313 | |
| 2314 | /* dummy leaf */ |
Radek Krejci | 4fe36bd | 2016-03-17 16:47:16 +0100 | [diff] [blame] | 2315 | memset(&node, 0, sizeof node); |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2316 | node.value_str = value; |
| 2317 | node.value_type = type->base; |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 2318 | node.schema = calloc(1, sizeof (struct lys_node_leaf)); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2319 | if (!node.schema) { |
| 2320 | LOGMEM; |
| 2321 | return -1; |
| 2322 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2323 | node.schema->name = strdup("default"); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2324 | if (!node.schema->name) { |
| 2325 | LOGMEM; |
| 2326 | return -1; |
| 2327 | } |
Michal Vasko | 5682640 | 2016-03-02 11:11:37 +0100 | [diff] [blame] | 2328 | node.schema->module = module; |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 2329 | memcpy(&((struct lys_node_leaf *)node.schema)->type, type, sizeof *type); |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2330 | |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 2331 | if (type->base == LY_TYPE_LEAFREF) { |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2332 | if (!type->info.lref.target) { |
| 2333 | ret = EXIT_FAILURE; |
| 2334 | goto finish; |
| 2335 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2336 | ret = check_default(&type->info.lref.target->type, value, module); |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2337 | |
| 2338 | } else if ((type->base == LY_TYPE_INST) || (type->base == LY_TYPE_IDENT)) { |
| 2339 | /* it was converted to JSON format before, nothing else sensible we can do */ |
| 2340 | |
| 2341 | } else { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 2342 | ret = lyp_parse_value(&node, NULL, 1); |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2343 | } |
| 2344 | |
| 2345 | finish: |
| 2346 | if (node.value_type == LY_TYPE_BITS) { |
| 2347 | free(node.value.bit); |
| 2348 | } |
| 2349 | free((char *)node.schema->name); |
| 2350 | free(node.schema); |
| 2351 | |
| 2352 | return ret; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2353 | } |
| 2354 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2355 | /** |
| 2356 | * @brief Check a key for mandatory attributes. Logs directly. |
| 2357 | * |
| 2358 | * @param[in] key The key to check. |
| 2359 | * @param[in] flags What flags to check. |
| 2360 | * @param[in] list The list of all the keys. |
| 2361 | * @param[in] index Index of the key in the key list. |
| 2362 | * @param[in] name The name of the keys. |
| 2363 | * @param[in] len The name length. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2364 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2365 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2366 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2367 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2368 | 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] | 2369 | { |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 2370 | struct lys_node_leaf *key = list->keys[index]; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2371 | char *dup = NULL; |
| 2372 | int j; |
| 2373 | |
| 2374 | /* existence */ |
| 2375 | if (!key) { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 2376 | if (name[len] != '\0') { |
| 2377 | dup = strdup(name); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2378 | if (!dup) { |
| 2379 | LOGMEM; |
| 2380 | return -1; |
| 2381 | } |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 2382 | dup[len] = '\0'; |
| 2383 | name = dup; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2384 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2385 | LOGVAL(LYE_KEY_MISS, LY_VLOG_LYS, list, name); |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 2386 | free(dup); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2387 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2388 | } |
| 2389 | |
| 2390 | /* uniqueness */ |
| 2391 | for (j = index - 1; j >= 0; j--) { |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 2392 | if (key == list->keys[j]) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2393 | LOGVAL(LYE_KEY_DUP, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2394 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2395 | } |
| 2396 | } |
| 2397 | |
| 2398 | /* key is a leaf */ |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2399 | if (key->nodetype != LYS_LEAF) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2400 | LOGVAL(LYE_KEY_NLEAF, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2401 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2402 | } |
| 2403 | |
| 2404 | /* type of the leaf is not built-in empty */ |
| 2405 | if (key->type.base == LY_TYPE_EMPTY) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2406 | LOGVAL(LYE_KEY_TYPE, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2407 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2408 | } |
| 2409 | |
| 2410 | /* config attribute is the same as of the list */ |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 2411 | if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2412 | LOGVAL(LYE_KEY_CONFIG, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2413 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2414 | } |
| 2415 | |
Radek Krejci | 55e2cdc | 2016-03-11 13:51:09 +0100 | [diff] [blame] | 2416 | /* key is not placed from augment */ |
| 2417 | if (key->parent->nodetype == LYS_AUGMENT) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2418 | LOGVAL(LYE_KEY_MISS, LY_VLOG_LYS, key, key->name); |
| 2419 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, key, "Key inserted from augment."); |
Radek Krejci | 55e2cdc | 2016-03-11 13:51:09 +0100 | [diff] [blame] | 2420 | return -1; |
| 2421 | } |
| 2422 | |
Michal Vasko | cca4784 | 2016-03-17 10:31:07 +0100 | [diff] [blame] | 2423 | /* key is not when-conditional */ |
| 2424 | if (key->when) { |
Radek Krejci | 02a0499 | 2016-03-17 16:06:37 +0100 | [diff] [blame] | 2425 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, key, "when", "leaf"); |
| 2426 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, key, "Key definition cannot depend on a \"when\" condition."); |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2427 | return -1; |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2428 | } |
| 2429 | |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 2430 | return EXIT_SUCCESS; |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 2431 | } |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2432 | |
| 2433 | /** |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2434 | * @brief Resolve (test the target exists) unique. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2435 | * |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2436 | * @param[in] parent The parent node of the unique structure. |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 2437 | * @param[in] uniq_str_path One path from the unique string. |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2438 | * |
| 2439 | * @return EXIT_SUCCESS on succes, EXIT_FAILURE on forward reference, -1 on error. |
| 2440 | */ |
| 2441 | int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2442 | resolve_unique(struct lys_node *parent, const char *uniq_str_path) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2443 | { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2444 | int rc; |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 2445 | const struct lys_node *leaf = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2446 | |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 2447 | rc = resolve_descendant_schema_nodeid(uniq_str_path, parent->child, LYS_LEAF, &leaf, 1); |
Michal Vasko | 9bb061b | 2016-02-12 11:00:19 +0100 | [diff] [blame] | 2448 | if (rc || !leaf) { |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 2449 | if (rc) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2450 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 2451 | if (rc > 0) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2452 | LOGVAL(LYE_INCHAR, LY_VLOG_LYS, parent, uniq_str_path[rc - 1], &uniq_str_path[rc - 1]); |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2453 | } |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 2454 | rc = -1; |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 2455 | } else { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2456 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
| 2457 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, parent, "Target leaf not found."); |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 2458 | rc = EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2459 | } |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2460 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2461 | } |
Michal Vasko | 9bb061b | 2016-02-12 11:00:19 +0100 | [diff] [blame] | 2462 | if (leaf->nodetype != LYS_LEAF) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2463 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
| 2464 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, parent, "Target is not a leaf."); |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2465 | rc = -1; |
| 2466 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2467 | } |
| 2468 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 2469 | /* check status */ |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2470 | 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] | 2471 | return -1; |
| 2472 | } |
| 2473 | |
Radek Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame] | 2474 | /* set leaf's unique flag */ |
| 2475 | ((struct lys_node_leaf *)leaf)->flags |= LYS_UNIQUE; |
| 2476 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2477 | return EXIT_SUCCESS; |
| 2478 | |
| 2479 | error: |
| 2480 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2481 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2482 | } |
| 2483 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2484 | /** |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2485 | * @brief Resolve (find) a feature definition. Logs directly. |
| 2486 | * |
| 2487 | * @param[in] name Feature name. |
| 2488 | * @param[in] module Module to search in. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2489 | * @param[out] ret Pointer to the resolved feature. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2490 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2491 | * @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] | 2492 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2493 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2494 | resolve_feature(const char *id, const struct lys_module *module, struct lys_feature **ret) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2495 | { |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 2496 | const char *mod_name, *name; |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 2497 | int mod_name_len, nam_len, i, j; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 2498 | struct lys_node *node; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2499 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2500 | assert(id); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2501 | assert(module); |
| 2502 | |
| 2503 | /* check prefix */ |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 2504 | if ((i = parse_node_identifier(id, &mod_name, &mod_name_len, &name, &nam_len)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2505 | LOGVAL(LYE_INCHAR, LY_VLOG_NONE, NULL, id[-i], &id[-i]); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2506 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2507 | } |
| 2508 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2509 | module = lys_get_import_module(module, NULL, 0, mod_name, mod_name_len); |
| 2510 | if (!module) { |
| 2511 | /* identity refers unknown data model */ |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2512 | LOGVAL(LYE_INMOD_LEN, LY_VLOG_NONE, NULL, mod_name_len, mod_name); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2513 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2514 | } |
| 2515 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2516 | /* search in the identified module ... */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2517 | for (j = 0; j < module->features_size; j++) { |
| 2518 | if (!strcmp(name, module->features[j].name)) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2519 | if (ret) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 2520 | /* check status */ |
| 2521 | node = (struct lys_node *)*ret; |
Radek Krejci | c655602 | 2016-01-27 15:16:45 +0100 | [diff] [blame] | 2522 | if (lyp_check_status(node->flags, node->module, node->name, module->features[j].flags, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2523 | module->features[j].module, module->features[j].name, node)) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 2524 | return -1; |
| 2525 | } |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2526 | *ret = &module->features[j]; |
| 2527 | } |
| 2528 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2529 | } |
| 2530 | } |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2531 | /* ... and all its submodules */ |
Michal Vasko | 27ab822 | 2016-02-12 09:33:52 +0100 | [diff] [blame] | 2532 | for (i = 0; i < module->inc_size; i++) { |
| 2533 | if (!module->inc[i].submodule) { |
| 2534 | /* not yet resolved */ |
| 2535 | continue; |
| 2536 | } |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2537 | for (j = 0; j < module->inc[i].submodule->features_size; j++) { |
| 2538 | if (!strcmp(name, module->inc[i].submodule->features[j].name)) { |
| 2539 | if (ret) { |
| 2540 | /* check status */ |
| 2541 | node = (struct lys_node *)*ret; |
Radek Krejci | c655602 | 2016-01-27 15:16:45 +0100 | [diff] [blame] | 2542 | if (lyp_check_status(node->flags, node->module, node->name, |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2543 | module->inc[i].submodule->features[j].flags, |
| 2544 | module->inc[i].submodule->features[j].module, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2545 | module->inc[i].submodule->features[j].name, node)) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2546 | return -1; |
| 2547 | } |
| 2548 | *ret = &(module->inc[i].submodule->features[j]); |
| 2549 | } |
| 2550 | return EXIT_SUCCESS; |
| 2551 | } |
| 2552 | } |
| 2553 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2554 | |
| 2555 | /* not found */ |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2556 | LOGVAL(LYE_INRESOLV, LY_VLOG_NONE, NULL, "feature", id); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2557 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2558 | } |
| 2559 | |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 2560 | void |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2561 | unres_data_del(struct unres_data *unres, uint32_t i) |
| 2562 | { |
| 2563 | /* there are items after the one deleted */ |
| 2564 | if (i+1 < unres->count) { |
| 2565 | /* we only move the data, memory is left allocated, why bother */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2566 | 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] | 2567 | |
| 2568 | /* deleting the last item */ |
| 2569 | } else if (i == 0) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2570 | free(unres->node); |
| 2571 | unres->node = NULL; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2572 | } |
| 2573 | |
| 2574 | /* if there are no items after and it is not the last one, just move the counter */ |
| 2575 | --unres->count; |
| 2576 | } |
| 2577 | |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 2578 | /** |
| 2579 | * @brief Resolve (find) a data node from a specific module. Does not log. |
| 2580 | * |
| 2581 | * @param[in] mod Module to search in. |
| 2582 | * @param[in] name Name of the data node. |
| 2583 | * @param[in] nam_len Length of the name. |
| 2584 | * @param[in] start Data node to start the search from. |
| 2585 | * @param[in,out] parents Resolved nodes. If there are some parents, |
| 2586 | * they are replaced (!!) with the resolvents. |
| 2587 | * |
| 2588 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference. |
| 2589 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2590 | static int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 2591 | 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] | 2592 | { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2593 | struct lyd_node *node; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 2594 | int flag; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2595 | uint32_t i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2596 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2597 | if (!parents->count) { |
| 2598 | parents->count = 1; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2599 | parents->node = malloc(sizeof *parents->node); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2600 | if (!parents->node) { |
| 2601 | LOGMEM; |
| 2602 | return EXIT_FAILURE; |
| 2603 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2604 | parents->node[0] = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2605 | } |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2606 | for (i = 0; i < parents->count;) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2607 | 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] | 2608 | /* skip */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2609 | ++i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2610 | continue; |
| 2611 | } |
| 2612 | flag = 0; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2613 | LY_TREE_FOR(parents->node[i] ? parents->node[i]->child : start, node) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2614 | if (node->schema->module == mod && !strncmp(node->schema->name, name, nam_len) |
| 2615 | && node->schema->name[nam_len] == '\0') { |
| 2616 | /* matching target */ |
| 2617 | if (!flag) { |
Michal Vasko | 9a47e12 | 2015-09-03 14:26:32 +0200 | [diff] [blame] | 2618 | /* put node instead of the current parent */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2619 | parents->node[i] = node; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2620 | flag = 1; |
| 2621 | } else { |
Michal Vasko | 9a47e12 | 2015-09-03 14:26:32 +0200 | [diff] [blame] | 2622 | /* multiple matching, so create a new node */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2623 | ++parents->count; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2624 | parents->node = ly_realloc(parents->node, parents->count * sizeof *parents->node); |
| 2625 | if (!parents->node) { |
| 2626 | return EXIT_FAILURE; |
| 2627 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2628 | parents->node[parents->count-1] = node; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2629 | ++i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2630 | } |
| 2631 | } |
| 2632 | } |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 2633 | |
| 2634 | if (!flag) { |
| 2635 | /* remove item from the parents list */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2636 | unres_data_del(parents, i); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 2637 | } else { |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2638 | ++i; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 2639 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2640 | } |
| 2641 | |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 2642 | return parents->count ? EXIT_SUCCESS : EXIT_FAILURE; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 2643 | } |
| 2644 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2645 | /** |
| 2646 | * @brief Resolve (find) a data node. Does not log. |
| 2647 | * |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2648 | * @param[in] mod_name Module name of the data node. |
| 2649 | * @param[in] mod_name_len Length of the module name. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2650 | * @param[in] name Name of the data node. |
| 2651 | * @param[in] nam_len Length of the name. |
| 2652 | * @param[in] start Data node to start the search from. |
| 2653 | * @param[in,out] parents Resolved nodes. If there are some parents, |
| 2654 | * they are replaced (!!) with the resolvents. |
| 2655 | * |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 2656 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 otherwise. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2657 | */ |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 2658 | static int |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2659 | 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] | 2660 | struct unres_data *parents) |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 2661 | { |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 2662 | const struct lys_module *mod; |
Michal Vasko | 31fc367 | 2015-10-21 12:08:13 +0200 | [diff] [blame] | 2663 | char *str; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 2664 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2665 | assert(start); |
| 2666 | |
Michal Vasko | 31fc367 | 2015-10-21 12:08:13 +0200 | [diff] [blame] | 2667 | if (mod_name) { |
| 2668 | /* we have mod_name, find appropriate module */ |
| 2669 | str = strndup(mod_name, mod_name_len); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2670 | if (!str) { |
| 2671 | LOGMEM; |
| 2672 | return -1; |
| 2673 | } |
Michal Vasko | 31fc367 | 2015-10-21 12:08:13 +0200 | [diff] [blame] | 2674 | mod = ly_ctx_get_module(start->schema->module->ctx, str, NULL); |
| 2675 | free(str); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 2676 | if (!mod) { |
| 2677 | /* invalid prefix */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2678 | return -1; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 2679 | } |
| 2680 | } else { |
| 2681 | /* no prefix, module is the same as of current node */ |
| 2682 | mod = start->schema->module; |
| 2683 | } |
| 2684 | |
| 2685 | return resolve_data(mod, name, name_len, start, parents); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2686 | } |
| 2687 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2688 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 2689 | * @brief Resolve a path predicate (leafref) in JSON data context. Logs directly |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2690 | * only specific errors, general no-resolvent error is left to the caller. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2691 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 2692 | * @param[in] pred Predicate to use. |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 2693 | * @param[in] node Node from which the predicate is being resolved |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2694 | * @param[in,out] node_match Nodes satisfying the restriction |
| 2695 | * without the predicate. Nodes not |
| 2696 | * satisfying the predicate are removed. |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 2697 | * @param[out] parsed Number of characters parsed, negative on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2698 | * |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 2699 | * @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] | 2700 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2701 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2702 | 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] | 2703 | int *parsed) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2704 | { |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2705 | /* ... /node[source = destination] ... */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2706 | struct unres_data source_match, dest_match; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2707 | const char *path_key_expr, *source, *sour_pref, *dest, *dest_pref; |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 2708 | int pke_len, sour_len, sour_pref_len, dest_len, dest_pref_len, parsed_loc = 0, pke_parsed = 0; |
| 2709 | int has_predicate, dest_parent_times, i, rc; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2710 | uint32_t j; |
| 2711 | |
| 2712 | source_match.count = 1; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2713 | source_match.node = malloc(sizeof *source_match.node); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2714 | if (!source_match.node) { |
| 2715 | LOGMEM; |
| 2716 | return -1; |
| 2717 | } |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2718 | dest_match.count = 1; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2719 | dest_match.node = malloc(sizeof *dest_match.node); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2720 | if (!dest_match.node) { |
| 2721 | LOGMEM; |
| 2722 | return -1; |
| 2723 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2724 | |
| 2725 | do { |
| 2726 | if ((i = parse_path_predicate(pred, &sour_pref, &sour_pref_len, &source, &sour_len, &path_key_expr, |
| 2727 | &pke_len, &has_predicate)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2728 | LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, pred[-i], &pred[-i]); |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 2729 | rc = -1; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2730 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2731 | } |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 2732 | parsed_loc += i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2733 | pred += i; |
| 2734 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2735 | for (j = 0; j < node_match->count;) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2736 | /* source */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2737 | source_match.node[0] = node_match->node[j]; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2738 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2739 | /* must be leaf (key of a list) */ |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2740 | 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] | 2741 | &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] | 2742 | i = 0; |
| 2743 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2744 | } |
| 2745 | |
| 2746 | /* destination */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2747 | dest_match.node[0] = node_match->node[j]; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2748 | dest_parent_times = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2749 | if ((i = parse_path_key_expr(path_key_expr, &dest_pref, &dest_pref_len, &dest, &dest_len, |
| 2750 | &dest_parent_times)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2751 | 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] | 2752 | rc = -1; |
| 2753 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2754 | } |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2755 | pke_parsed = i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2756 | for (i = 0; i < dest_parent_times; ++i) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2757 | dest_match.node[0] = dest_match.node[0]->parent; |
| 2758 | if (!dest_match.node[0]) { |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2759 | i = 0; |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 2760 | rc = EXIT_FAILURE; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2761 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2762 | } |
| 2763 | } |
| 2764 | while (1) { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2765 | 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] | 2766 | &dest_match)) || (dest_match.count != 1)) { |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2767 | i = 0; |
| 2768 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2769 | } |
| 2770 | |
| 2771 | if (pke_len == pke_parsed) { |
| 2772 | break; |
| 2773 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 2774 | 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] | 2775 | &dest_parent_times)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2776 | 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] | 2777 | rc = -1; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2778 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2779 | } |
| 2780 | pke_parsed += i; |
| 2781 | } |
| 2782 | |
| 2783 | /* check match between source and destination nodes */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2784 | if (((struct lys_node_leaf *)source_match.node[0]->schema)->type.base |
| 2785 | != ((struct lys_node_leaf *)dest_match.node[0]->schema)->type.base) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2786 | goto remove_leafref; |
| 2787 | } |
| 2788 | |
Radek Krejci | c1ffa4d | 2016-02-17 13:11:11 +0100 | [diff] [blame] | 2789 | if (!ly_strequal(((struct lyd_node_leaf_list *)source_match.node[0])->value_str, |
Radek Krejci | 749190d | 2016-02-18 16:26:25 +0100 | [diff] [blame] | 2790 | ((struct lyd_node_leaf_list *)dest_match.node[0])->value_str, 1)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2791 | goto remove_leafref; |
| 2792 | } |
| 2793 | |
| 2794 | /* leafref is ok, continue check with next leafref */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2795 | ++j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2796 | continue; |
| 2797 | |
| 2798 | remove_leafref: |
| 2799 | /* does not fulfill conditions, remove leafref record */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2800 | unres_data_del(node_match, j); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2801 | } |
| 2802 | } while (has_predicate); |
| 2803 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2804 | free(source_match.node); |
| 2805 | free(dest_match.node); |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 2806 | if (parsed) { |
| 2807 | *parsed = parsed_loc; |
| 2808 | } |
| 2809 | return EXIT_SUCCESS; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2810 | |
| 2811 | error: |
| 2812 | |
| 2813 | if (source_match.count) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2814 | free(source_match.node); |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2815 | } |
| 2816 | if (dest_match.count) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2817 | free(dest_match.node); |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2818 | } |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 2819 | if (parsed) { |
| 2820 | *parsed = -parsed_loc+i; |
| 2821 | } |
| 2822 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2823 | } |
| 2824 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2825 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 2826 | * @brief Resolve a path (leafref) in JSON data context. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2827 | * |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2828 | * @param[in] node Leafref data node. |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2829 | * @param[in] path Path of the leafref. |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2830 | * @param[out] ret Matching nodes. Expects an empty, but allocated structure. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2831 | * |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 2832 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 otherwise. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2833 | */ |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 2834 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2835 | 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] | 2836 | { |
Radek Krejci | 71b795b | 2015-08-10 16:20:39 +0200 | [diff] [blame] | 2837 | struct lyd_node *data = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2838 | const char *prefix, *name; |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 2839 | int pref_len, nam_len, has_predicate, parent_times, i, parsed, rc; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2840 | uint32_t j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2841 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2842 | assert(node && path && ret && !ret->count); |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2843 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2844 | parent_times = 0; |
Michal Vasko | d917334 | 2015-08-17 14:35:36 +0200 | [diff] [blame] | 2845 | parsed = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2846 | |
| 2847 | /* searching for nodeset */ |
| 2848 | do { |
| 2849 | 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] | 2850 | LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, path[-i], &path[-i]); |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 2851 | rc = -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2852 | goto error; |
| 2853 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2854 | path += i; |
Michal Vasko | d917334 | 2015-08-17 14:35:36 +0200 | [diff] [blame] | 2855 | parsed += i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2856 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2857 | if (!ret->count) { |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 2858 | if (parent_times != -1) { |
| 2859 | ret->count = 1; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2860 | ret->node = calloc(1, sizeof *ret->node); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2861 | if (!ret->node) { |
| 2862 | LOGMEM; |
Radek Krejci | 5050173 | 2016-01-07 13:06:39 +0100 | [diff] [blame] | 2863 | rc = -1; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2864 | goto error; |
| 2865 | } |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 2866 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2867 | for (i = 0; i < parent_times; ++i) { |
| 2868 | /* relative path */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2869 | if (!ret->count) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2870 | /* error, too many .. */ |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2871 | LOGVAL(LYE_INVAL, LY_VLOG_LYD, node, path, node->schema->name); |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 2872 | rc = -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2873 | goto error; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2874 | } else if (!ret->node[0]) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2875 | /* first .. */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2876 | data = ret->node[0] = node->parent; |
| 2877 | } else if (!ret->node[0]->parent) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2878 | /* we are in root */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2879 | ret->count = 0; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2880 | free(ret->node); |
| 2881 | ret->node = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2882 | } else { |
| 2883 | /* multiple .. */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2884 | data = ret->node[0] = ret->node[0]->parent; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2885 | } |
| 2886 | } |
| 2887 | |
| 2888 | /* absolute path */ |
| 2889 | if (parent_times == -1) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2890 | for (data = node; data->parent; data = data->parent); |
Michal Vasko | db9323c | 2015-10-16 10:32:44 +0200 | [diff] [blame] | 2891 | /* we're still parsing it and the pointer is not correct yet */ |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 2892 | if (data->prev) { |
| 2893 | for (; data->prev->next; data = data->prev); |
| 2894 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2895 | } |
| 2896 | } |
| 2897 | |
| 2898 | /* node identifier */ |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2899 | 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] | 2900 | if (rc == -1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2901 | LOGVAL(LYE_INELEM_LEN, LY_VLOG_LYD, node, nam_len, name); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 2902 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2903 | goto error; |
| 2904 | } |
| 2905 | |
| 2906 | if (has_predicate) { |
| 2907 | /* we have predicate, so the current results must be lists */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2908 | for (j = 0; j < ret->count;) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2909 | if (ret->node[j]->schema->nodetype == LYS_LIST && |
| 2910 | ((struct lys_node_list *)ret->node[0]->schema)->keys) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2911 | /* leafref is ok, continue check with next leafref */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2912 | ++j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2913 | continue; |
| 2914 | } |
| 2915 | |
| 2916 | /* does not fulfill conditions, remove leafref record */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2917 | unres_data_del(ret, j); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2918 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2919 | if ((rc = resolve_path_predicate_data(path, node, ret, &i))) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 2920 | if (rc == -1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2921 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYD, node, path); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 2922 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2923 | goto error; |
| 2924 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2925 | path += i; |
Michal Vasko | d917334 | 2015-08-17 14:35:36 +0200 | [diff] [blame] | 2926 | parsed += i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2927 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2928 | if (!ret->count) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2929 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYD, node, path-parsed); |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 2930 | rc = EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2931 | goto error; |
| 2932 | } |
| 2933 | } |
| 2934 | } while (path[0] != '\0'); |
| 2935 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 2936 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2937 | |
| 2938 | error: |
| 2939 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2940 | free(ret->node); |
| 2941 | ret->node = NULL; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2942 | ret->count = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2943 | |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 2944 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2945 | } |
| 2946 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2947 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 2948 | * @brief Resolve a path (leafref) predicate in JSON schema context. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2949 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 2950 | * @param[in] path Path to use. |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 2951 | * @param[in] context_node Predicate context node (where the predicate is placed). |
| 2952 | * @param[in] parent Path context node (where the path begins/is placed). |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2953 | * |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 2954 | * @return 0 on forward reference, otherwise the number |
| 2955 | * of characters successfully parsed, |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2956 | * positive on success, negative on failure. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2957 | */ |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 2958 | static int |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 2959 | 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] | 2960 | struct lys_node *parent) |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 2961 | { |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 2962 | const struct lys_node *src_node, *dst_node; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 2963 | const char *path_key_expr, *source, *sour_pref, *dest, *dest_pref; |
| 2964 | 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] | 2965 | int has_predicate, dest_parent_times = 0, i, rc; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 2966 | |
| 2967 | do { |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2968 | 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] | 2969 | &pke_len, &has_predicate)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2970 | 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] | 2971 | return -parsed+i; |
| 2972 | } |
| 2973 | parsed += i; |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2974 | path += i; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 2975 | |
Michal Vasko | 5809090 | 2015-08-13 14:04:15 +0200 | [diff] [blame] | 2976 | /* source (must be leaf) */ |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 2977 | if (!sour_pref) { |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 2978 | sour_pref = context_node->module->name; |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 2979 | } |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 2980 | 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] | 2981 | LYS_LEAF | LYS_AUGMENT, &src_node); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2982 | if (rc) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 2983 | if (rc == -1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2984 | LOGVAL(LYE_NORESOLV, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, path-parsed); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 2985 | } |
| 2986 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 2987 | } |
| 2988 | |
| 2989 | /* destination */ |
| 2990 | if ((i = parse_path_key_expr(path_key_expr, &dest_pref, &dest_pref_len, &dest, &dest_len, |
| 2991 | &dest_parent_times)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2992 | 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] | 2993 | return -parsed; |
| 2994 | } |
| 2995 | pke_parsed += i; |
| 2996 | |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 2997 | /* parent is actually the parent of this leaf, so skip the first ".." */ |
| 2998 | for (i = 0, dst_node = parent; i < dest_parent_times; ++i) { |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 2999 | if (!dst_node) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3000 | LOGVAL(LYE_NORESOLV, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, path_key_expr); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3001 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3002 | } |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3003 | dst_node = dst_node->parent; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3004 | } |
| 3005 | while (1) { |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3006 | if (!dest_pref) { |
| 3007 | dest_pref = dst_node->module->name; |
| 3008 | } |
| 3009 | 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] | 3010 | LYS_CONTAINER | LYS_LIST | LYS_LEAF | LYS_AUGMENT, &dst_node); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3011 | if (rc) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 3012 | if (rc == -1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3013 | LOGVAL(LYE_NORESOLV, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, path_key_expr); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3014 | } |
| 3015 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3016 | } |
| 3017 | |
| 3018 | if (pke_len == pke_parsed) { |
| 3019 | break; |
| 3020 | } |
| 3021 | |
| 3022 | if ((i = parse_path_key_expr(path_key_expr+pke_parsed, &dest_pref, &dest_pref_len, &dest, &dest_len, |
| 3023 | &dest_parent_times)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3024 | LOGVAL(LYE_INCHAR, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3025 | (path_key_expr+pke_parsed)[-i], (path_key_expr+pke_parsed)-i); |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3026 | return -parsed; |
| 3027 | } |
| 3028 | pke_parsed += i; |
| 3029 | } |
| 3030 | |
| 3031 | /* check source - dest match */ |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3032 | if (dst_node->nodetype != LYS_LEAF) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3033 | LOGVAL(LYE_NORESOLV, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, path-parsed); |
| 3034 | LOGVAL(LYE_SPEC, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, |
| 3035 | "Destination node is not a leaf, but %s.", strnodetype(dst_node->nodetype)); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3036 | return -parsed; |
| 3037 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3038 | } while (has_predicate); |
| 3039 | |
| 3040 | return parsed; |
| 3041 | } |
| 3042 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3043 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3044 | * @brief Resolve a path (leafref) in JSON schema context. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3045 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3046 | * @param[in] path Path to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3047 | * @param[in] parent_node Parent of the leafref. |
Radek Krejci | 2f12f85 | 2016-01-08 12:59:57 +0100 | [diff] [blame] | 3048 | * @param[in] parent_tpdf Flag if the parent node is actually typedef, in that case the path |
| 3049 | * has to contain absolute path |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3050 | * @param[out] ret Pointer to the resolved schema node. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3051 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3052 | * @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] | 3053 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3054 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3055 | 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] | 3056 | const struct lys_node **ret) |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3057 | { |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3058 | const struct lys_node *node; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 3059 | const struct lys_module *mod; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3060 | const char *id, *prefix, *name; |
| 3061 | int pref_len, nam_len, parent_times, has_predicate; |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3062 | int i, first_iter, rc; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3063 | |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3064 | first_iter = 1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3065 | parent_times = 0; |
| 3066 | id = path; |
| 3067 | |
| 3068 | do { |
| 3069 | 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] | 3070 | 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] | 3071 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3072 | } |
| 3073 | id += i; |
| 3074 | |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3075 | if (first_iter) { |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3076 | if (parent_times == -1) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 3077 | /* resolve prefix of the module */ |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3078 | mod = lys_get_import_module(parent->module, NULL, 0, prefix, pref_len); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 3079 | /* get start node */ |
| 3080 | node = mod ? mod->data : NULL; |
Michal Vasko | 5809090 | 2015-08-13 14:04:15 +0200 | [diff] [blame] | 3081 | if (!node) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3082 | LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, path); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3083 | return EXIT_FAILURE; |
Michal Vasko | 5809090 | 2015-08-13 14:04:15 +0200 | [diff] [blame] | 3084 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3085 | } else if (parent_times > 0) { |
Michal Vasko | 73ae256 | 2015-08-06 11:58:13 +0200 | [diff] [blame] | 3086 | /* node is the parent already, skip one ".." */ |
Radek Krejci | 2f12f85 | 2016-01-08 12:59:57 +0100 | [diff] [blame] | 3087 | if (parent_tpdf) { |
| 3088 | /* the path is not allowed to contain relative path since we are in top level typedef */ |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3089 | LOGVAL(LYE_NORESOLV, 0, NULL, path); |
Radek Krejci | 2f12f85 | 2016-01-08 12:59:57 +0100 | [diff] [blame] | 3090 | return -1; |
| 3091 | } |
| 3092 | |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3093 | node = parent; |
Michal Vasko | 5809090 | 2015-08-13 14:04:15 +0200 | [diff] [blame] | 3094 | i = 0; |
| 3095 | while (1) { |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3096 | if (!node) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3097 | LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, path); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3098 | return EXIT_FAILURE; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3099 | } |
Michal Vasko | 5809090 | 2015-08-13 14:04:15 +0200 | [diff] [blame] | 3100 | |
| 3101 | /* this node is a wrong node, we actually need the augment target */ |
| 3102 | if (node->nodetype == LYS_AUGMENT) { |
| 3103 | node = ((struct lys_node_augment *)node)->target; |
| 3104 | if (!node) { |
| 3105 | continue; |
| 3106 | } |
| 3107 | } |
| 3108 | |
| 3109 | ++i; |
| 3110 | if (i == parent_times) { |
| 3111 | break; |
| 3112 | } |
| 3113 | node = node->parent; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3114 | } |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3115 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3116 | node = node->child; |
Michal Vasko | e01eca5 | 2015-08-13 14:42:02 +0200 | [diff] [blame] | 3117 | } else { |
| 3118 | LOGINT; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3119 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3120 | } |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3121 | |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3122 | first_iter = 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3123 | } else { |
Michal Vasko | 7dc71d0 | 2016-03-15 10:42:28 +0100 | [diff] [blame] | 3124 | /* move down the tree, if possible */ |
| 3125 | if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYXML)) { |
Michal Vasko | 2f5aceb | 2016-03-22 10:24:14 +0100 | [diff] [blame] | 3126 | 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] | 3127 | return -1; |
| 3128 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3129 | node = node->child; |
| 3130 | } |
| 3131 | |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 3132 | if (!prefix) { |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3133 | prefix = lys_node_module(parent)->name; |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 3134 | } |
| 3135 | |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3136 | 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] | 3137 | if (rc) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 3138 | if (rc == -1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3139 | LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, path); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3140 | } |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3141 | return rc; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3142 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3143 | |
| 3144 | if (has_predicate) { |
| 3145 | /* we have predicate, so the current result must be list */ |
| 3146 | if (node->nodetype != LYS_LIST) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3147 | LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, path); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3148 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3149 | } |
| 3150 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3151 | i = resolve_path_predicate_schema(id, node, parent); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3152 | if (!i) { |
Michal Vasko | f9664da | 2015-08-24 15:03:30 +0200 | [diff] [blame] | 3153 | return EXIT_FAILURE; |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3154 | } else if (i < 0) { |
| 3155 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3156 | } |
| 3157 | id += i; |
| 3158 | } |
| 3159 | } while (id[0]); |
| 3160 | |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 3161 | /* the target must be leaf or leaf-list */ |
| 3162 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3163 | LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, path); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3164 | return -1; |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 3165 | } |
| 3166 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3167 | /* check status */ |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3168 | if (lyp_check_status(parent->flags, parent->module, parent->name, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3169 | node->flags, node->module, node->name, node)) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3170 | return -1; |
| 3171 | } |
| 3172 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3173 | if (ret) { |
| 3174 | *ret = node; |
| 3175 | } |
| 3176 | return EXIT_SUCCESS; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3177 | } |
| 3178 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3179 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3180 | * @brief Resolve instance-identifier predicate in JSON data format. |
| 3181 | * Does not log. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3182 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3183 | * @param[in] pred Predicate to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3184 | * @param[in,out] node_match Nodes matching the restriction without |
| 3185 | * the predicate. Nodes not satisfying |
| 3186 | * the predicate are removed. |
| 3187 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3188 | * @return Number of characters successfully parsed, |
| 3189 | * positive on success, negative on failure. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3190 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3191 | static int |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3192 | resolve_predicate(const char *pred, struct unres_data *node_match) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3193 | { |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3194 | /* ... /node[target = value] ... */ |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3195 | struct unres_data target_match; |
| 3196 | struct ly_ctx *ctx; |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3197 | const struct lys_module *mod; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3198 | const char *model, *name, *value; |
| 3199 | char *str; |
| 3200 | int mod_len, nam_len, val_len, i, has_predicate, cur_idx, idx, parsed; |
| 3201 | uint32_t j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3202 | |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3203 | assert(pred && node_match->count); |
| 3204 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3205 | ctx = node_match->node[0]->schema->module->ctx; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3206 | idx = -1; |
| 3207 | parsed = 0; |
| 3208 | |
| 3209 | do { |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3210 | 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] | 3211 | return -parsed+i; |
| 3212 | } |
| 3213 | parsed += i; |
| 3214 | pred += i; |
| 3215 | |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3216 | /* pos */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3217 | if (isdigit(name[0])) { |
| 3218 | idx = atoi(name); |
| 3219 | } |
| 3220 | |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3221 | for (cur_idx = 0, j = 0; j < node_match->count; ++cur_idx) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3222 | /* target */ |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3223 | memset(&target_match, 0, sizeof target_match); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3224 | if ((name[0] == '.') || !value) { |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3225 | target_match.count = 1; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3226 | target_match.node = malloc(sizeof *target_match.node); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3227 | if (!target_match.node) { |
| 3228 | LOGMEM; |
| 3229 | return -1; |
| 3230 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3231 | target_match.node[0] = node_match->node[j]; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3232 | } else { |
| 3233 | str = strndup(model, mod_len); |
| 3234 | mod = ly_ctx_get_module(ctx, str, NULL); |
| 3235 | free(str); |
| 3236 | |
Radek Krejci | 804836a | 2016-02-03 10:39:55 +0100 | [diff] [blame] | 3237 | 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] | 3238 | goto remove_instid; |
| 3239 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3240 | } |
| 3241 | |
| 3242 | /* check that we have the correct type */ |
| 3243 | if (name[0] == '.') { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3244 | if (node_match->node[j]->schema->nodetype != LYS_LEAFLIST) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3245 | goto remove_instid; |
| 3246 | } |
| 3247 | } else if (value) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3248 | if (node_match->node[j]->schema->nodetype != LYS_LIST) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3249 | goto remove_instid; |
| 3250 | } |
| 3251 | } |
| 3252 | |
Michal Vasko | 83a6c46 | 2015-10-08 16:43:53 +0200 | [diff] [blame] | 3253 | if ((value && (strncmp(((struct lyd_node_leaf_list *)target_match.node[0])->value_str, value, val_len) |
| 3254 | || ((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] | 3255 | || (!value && (idx != cur_idx))) { |
| 3256 | goto remove_instid; |
| 3257 | } |
| 3258 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3259 | free(target_match.node); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3260 | |
| 3261 | /* leafref is ok, continue check with next leafref */ |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3262 | ++j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3263 | continue; |
| 3264 | |
| 3265 | remove_instid: |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3266 | free(target_match.node); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3267 | |
| 3268 | /* does not fulfill conditions, remove leafref record */ |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3269 | unres_data_del(node_match, j); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3270 | } |
| 3271 | } while (has_predicate); |
| 3272 | |
| 3273 | return parsed; |
| 3274 | } |
| 3275 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3276 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3277 | * @brief Resolve instance-identifier in JSON data format. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3278 | * |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3279 | * @param[in] data Data node where the path is used |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3280 | * @param[in] path Instance-identifier node value. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3281 | * |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3282 | * @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] | 3283 | */ |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3284 | static struct lyd_node * |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3285 | resolve_instid(struct lyd_node *data, const char *path) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3286 | { |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3287 | int i = 0, j; |
| 3288 | struct lyd_node *result = NULL; |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3289 | const struct lys_module *mod = NULL; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3290 | struct ly_ctx *ctx = data->schema->module->ctx; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3291 | const char *model, *name; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3292 | char *str; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3293 | int mod_len, name_len, has_predicate; |
| 3294 | struct unres_data node_match; |
| 3295 | uint32_t k; |
| 3296 | |
| 3297 | memset(&node_match, 0, sizeof node_match); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3298 | |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3299 | /* we need root to resolve absolute path */ |
| 3300 | for (; data->parent; data = data->parent); |
Michal Vasko | db9323c | 2015-10-16 10:32:44 +0200 | [diff] [blame] | 3301 | /* we're still parsing it and the pointer is not correct yet */ |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3302 | if (data->prev) { |
| 3303 | for (; data->prev->next; data = data->prev); |
| 3304 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3305 | |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3306 | /* search for the instance node */ |
| 3307 | while (path[i]) { |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3308 | 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] | 3309 | if (j <= 0) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3310 | 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] | 3311 | goto error; |
| 3312 | } |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3313 | i += j; |
Michal Vasko | b387c48 | 2015-08-12 09:32:59 +0200 | [diff] [blame] | 3314 | |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3315 | str = strndup(model, mod_len); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3316 | if (!str) { |
| 3317 | LOGMEM; |
| 3318 | goto error; |
| 3319 | } |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3320 | mod = ly_ctx_get_module(ctx, str, NULL); |
| 3321 | free(str); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3322 | |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3323 | if (!mod) { |
| 3324 | /* no instance exists */ |
| 3325 | return NULL; |
| 3326 | } |
| 3327 | |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3328 | if (resolve_data(mod, name, name_len, data, &node_match)) { |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3329 | /* no instance exists */ |
| 3330 | return NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3331 | } |
| 3332 | |
| 3333 | if (has_predicate) { |
| 3334 | /* 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] | 3335 | for (k = 0; k < node_match.count;) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3336 | if ((node_match.node[k]->schema->nodetype == LYS_LIST && |
| 3337 | ((struct lys_node_list *)node_match.node[k]->schema)->keys) |
| 3338 | || (node_match.node[k]->schema->nodetype == LYS_LEAFLIST)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3339 | /* instid is ok, continue check with next instid */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3340 | ++k; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3341 | continue; |
| 3342 | } |
| 3343 | |
| 3344 | /* does not fulfill conditions, remove inst record */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3345 | unres_data_del(&node_match, k); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3346 | } |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3347 | |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3348 | j = resolve_predicate(&path[i], &node_match); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3349 | if (j < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3350 | LOGVAL(LYE_INPRED, LY_VLOG_LYD, data, &path[i-j]); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3351 | goto error; |
| 3352 | } |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3353 | i += j; |
Michal Vasko | b387c48 | 2015-08-12 09:32:59 +0200 | [diff] [blame] | 3354 | |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3355 | if (!node_match.count) { |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3356 | /* no instance exists */ |
| 3357 | return NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3358 | } |
| 3359 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3360 | } |
| 3361 | |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3362 | if (!node_match.count) { |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3363 | /* no instance exists */ |
| 3364 | return NULL; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3365 | } else if (node_match.count > 1) { |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3366 | /* instance identifier must resolve to a single node */ |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3367 | LOGVAL(LYE_TOOMANY, LY_VLOG_LYD, data, path, "data tree"); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3368 | |
| 3369 | /* cleanup */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3370 | free(node_match.node); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3371 | |
| 3372 | return NULL; |
| 3373 | } else { |
| 3374 | /* we have required result, remember it and cleanup */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3375 | result = node_match.node[0]; |
| 3376 | free(node_match.node); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3377 | |
| 3378 | return result; |
| 3379 | } |
| 3380 | |
| 3381 | error: |
| 3382 | |
| 3383 | /* cleanup */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3384 | free(node_match.node); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3385 | |
| 3386 | return NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3387 | } |
| 3388 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3389 | /** |
| 3390 | * @brief Passes config flag down to children. Does not log. |
| 3391 | * |
| 3392 | * @param[in] node Parent node. |
| 3393 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3394 | static void |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 3395 | inherit_config_flag(struct lys_node *node) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3396 | { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 3397 | LY_TREE_FOR(node, node) { |
| 3398 | node->flags |= node->parent->flags & LYS_CONFIG_MASK; |
| 3399 | inherit_config_flag(node->child); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3400 | } |
| 3401 | } |
| 3402 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3403 | /** |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 3404 | * @brief Resolve augment target. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3405 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3406 | * @param[in] aug Augment to use. |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 3407 | * @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] | 3408 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3409 | * @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] | 3410 | */ |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 3411 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3412 | resolve_augment(struct lys_node_augment *aug, struct lys_node *siblings) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3413 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3414 | int rc; |
Michal Vasko | 1d87a92 | 2015-08-21 12:57:16 +0200 | [diff] [blame] | 3415 | struct lys_node *sub; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3416 | |
Michal Vasko | 1d87a92 | 2015-08-21 12:57:16 +0200 | [diff] [blame] | 3417 | assert(aug); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3418 | |
| 3419 | /* resolve target node */ |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 3420 | rc = resolve_augment_schema_nodeid(aug->target_name, siblings, (siblings ? NULL : aug->module), (const struct lys_node **)&aug->target); |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 3421 | if (rc == -1) { |
| 3422 | return -1; |
| 3423 | } |
| 3424 | if (rc > 0) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3425 | LOGVAL(LYE_INCHAR, LY_VLOG_LYS, aug, aug->target_name[rc - 1], &aug->target_name[rc - 1]); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 3426 | return -1; |
| 3427 | } |
| 3428 | if (!aug->target) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3429 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, aug, "augment", aug->target_name); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 3430 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3431 | } |
| 3432 | |
| 3433 | if (!aug->child) { |
| 3434 | /* nothing to do */ |
Michal Vasko | 1d87a92 | 2015-08-21 12:57:16 +0200 | [diff] [blame] | 3435 | LOGWRN("Augment \"%s\" without children.", aug->target_name); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3436 | return EXIT_SUCCESS; |
| 3437 | } |
| 3438 | |
Michal Vasko | d58d596 | 2016-03-02 14:29:41 +0100 | [diff] [blame] | 3439 | /* check for mandatory nodes - if the target node is in another module |
| 3440 | * the added nodes cannot be mandatory |
| 3441 | */ |
| 3442 | if (!aug->parent && (lys_node_module((struct lys_node *)aug) != lys_node_module(aug->target)) |
| 3443 | && lyp_check_mandatory((struct lys_node *)aug)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3444 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, aug, "mandatory", "augment node"); |
| 3445 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, aug, "When augmenting data in another module, mandatory nodes are not allowed."); |
Michal Vasko | d58d596 | 2016-03-02 14:29:41 +0100 | [diff] [blame] | 3446 | return -1; |
| 3447 | } |
| 3448 | |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 3449 | /* check augment target type and then augment nodes type */ |
| 3450 | if (aug->target->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_CASE | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF)) { |
| 3451 | LY_TREE_FOR(aug->child, sub) { |
| 3452 | 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] | 3453 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment"); |
| 3454 | 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] | 3455 | strnodetype(aug->target->nodetype), strnodetype(sub->nodetype)); |
| 3456 | return -1; |
| 3457 | } |
| 3458 | } |
| 3459 | } else if (aug->target->nodetype == LYS_CHOICE) { |
| 3460 | LY_TREE_FOR(aug->child, sub) { |
| 3461 | 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] | 3462 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment"); |
| 3463 | 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] | 3464 | strnodetype(aug->target->nodetype), strnodetype(sub->nodetype)); |
| 3465 | return -1; |
| 3466 | } |
| 3467 | } |
| 3468 | } else { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3469 | LOGVAL(LYE_INARG, LY_VLOG_LYS, aug, aug->target_name, "target-node"); |
| 3470 | 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] | 3471 | return -1; |
| 3472 | } |
| 3473 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3474 | /* inherit config information from parent, augment does not have |
| 3475 | * config property, but we need to keep the information for subelements |
| 3476 | */ |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 3477 | aug->flags |= aug->target->flags & LYS_CONFIG_MASK; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3478 | LY_TREE_FOR(aug->child, sub) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3479 | inherit_config_flag(sub); |
| 3480 | } |
| 3481 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 3482 | /* check identifier uniqueness as in lys_node_addchild() */ |
Michal Vasko | 1d87a92 | 2015-08-21 12:57:16 +0200 | [diff] [blame] | 3483 | LY_TREE_FOR(aug->child, sub) { |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 3484 | if (lys_check_id(sub, aug->parent, lys_module(aug->module))) { |
Michal Vasko | 3e6665f | 2015-08-17 14:00:38 +0200 | [diff] [blame] | 3485 | return -1; |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 3486 | } |
| 3487 | } |
Radek Krejci | 0acbe1b | 2015-08-04 09:33:49 +0200 | [diff] [blame] | 3488 | /* reconnect augmenting data into the target - add them to the target child list */ |
| 3489 | if (aug->target->child) { |
Michal Vasko | 1d87a92 | 2015-08-21 12:57:16 +0200 | [diff] [blame] | 3490 | sub = aug->target->child->prev; /* remember current target's last node */ |
| 3491 | sub->next = aug->child; /* connect augmenting data after target's last node */ |
Radek Krejci | 0acbe1b | 2015-08-04 09:33:49 +0200 | [diff] [blame] | 3492 | 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] | 3493 | aug->child->prev = sub; /* finish connecting of both child lists */ |
Radek Krejci | 0acbe1b | 2015-08-04 09:33:49 +0200 | [diff] [blame] | 3494 | } else { |
| 3495 | aug->target->child = aug->child; |
| 3496 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3497 | |
| 3498 | return EXIT_SUCCESS; |
| 3499 | } |
| 3500 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3501 | /** |
| 3502 | * @brief Resolve uses, apply augments, refines. Logs directly. |
| 3503 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3504 | * @param[in] uses Uses to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3505 | * @param[in,out] unres List of unresolved items. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3506 | * |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 3507 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3508 | */ |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3509 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3510 | resolve_uses(struct lys_node_uses *uses, struct unres_schema *unres) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3511 | { |
| 3512 | struct ly_ctx *ctx; |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3513 | struct lys_node *node = NULL; |
| 3514 | const struct lys_node *node_aux; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3515 | struct lys_refine *rfn; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 3516 | struct lys_restr *must, **old_must; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3517 | int i, j, rc; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 3518 | uint8_t size, *old_size; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3519 | |
Michal Vasko | 71e1aa8 | 2015-08-12 12:17:51 +0200 | [diff] [blame] | 3520 | assert(uses->grp); |
Michal Vasko | e770851 | 2016-03-11 10:26:55 +0100 | [diff] [blame] | 3521 | /* HACK just check that the grouping is resolved */ |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 3522 | assert(!uses->grp->nacm); |
Michal Vasko | 71e1aa8 | 2015-08-12 12:17:51 +0200 | [diff] [blame] | 3523 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3524 | /* copy the data nodes from grouping into the uses context */ |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3525 | LY_TREE_FOR(uses->grp->child, node_aux) { |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 3526 | node = lys_node_dup(uses->module, (struct lys_node *)uses, node_aux, uses->flags, uses->nacm, unres, 0); |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3527 | if (!node) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3528 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, uses->grp->name, "uses"); |
| 3529 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, "Copying data from grouping failed."); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3530 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3531 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3532 | } |
| 3533 | ctx = uses->module->ctx; |
| 3534 | |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 3535 | /* we managed to copy the grouping, the rest must be possible to resolve */ |
| 3536 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3537 | /* apply refines */ |
| 3538 | for (i = 0; i < uses->refine_size; i++) { |
| 3539 | rfn = &uses->refine[i]; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 3540 | rc = resolve_descendant_schema_nodeid(rfn->target_name, uses->child, LYS_NO_RPC_NOTIF_NODE, |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 3541 | (const struct lys_node **)&node, 1); |
Michal Vasko | 9bb061b | 2016-02-12 11:00:19 +0100 | [diff] [blame] | 3542 | if (rc || !node) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3543 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine"); |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 3544 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3545 | } |
| 3546 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 3547 | if (rfn->target_type && !(node->nodetype & rfn->target_type)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3548 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine"); |
| 3549 | 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] | 3550 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3551 | } |
| 3552 | |
| 3553 | /* description on any nodetype */ |
| 3554 | if (rfn->dsc) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 3555 | lydict_remove(ctx, node->dsc); |
| 3556 | node->dsc = lydict_insert(ctx, rfn->dsc, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3557 | } |
| 3558 | |
| 3559 | /* reference on any nodetype */ |
| 3560 | if (rfn->ref) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 3561 | lydict_remove(ctx, node->ref); |
| 3562 | node->ref = lydict_insert(ctx, rfn->ref, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3563 | } |
| 3564 | |
| 3565 | /* config on any nodetype */ |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 3566 | if (rfn->flags & LYS_CONFIG_MASK) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 3567 | node->flags &= ~LYS_CONFIG_MASK; |
| 3568 | node->flags |= (rfn->flags & LYS_CONFIG_MASK); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3569 | } |
| 3570 | |
| 3571 | /* default value ... */ |
| 3572 | if (rfn->mod.dflt) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 3573 | if (node->nodetype == LYS_LEAF) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3574 | /* leaf */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 3575 | lydict_remove(ctx, ((struct lys_node_leaf *)node)->dflt); |
| 3576 | ((struct lys_node_leaf *)node)->dflt = lydict_insert(ctx, rfn->mod.dflt, 0); |
| 3577 | } else if (node->nodetype == LYS_CHOICE) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3578 | /* choice */ |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 3579 | rc = resolve_choice_default_schema_nodeid(rfn->mod.dflt, node->child, |
| 3580 | (const struct lys_node **)&((struct lys_node_choice *)node)->dflt); |
Michal Vasko | 9bb061b | 2016-02-12 11:00:19 +0100 | [diff] [blame] | 3581 | if (rc || !((struct lys_node_choice *)node)->dflt) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3582 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->mod.dflt, "default"); |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 3583 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3584 | } |
| 3585 | } |
| 3586 | } |
| 3587 | |
| 3588 | /* mandatory on leaf, anyxml or choice */ |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 3589 | if (rfn->flags & LYS_MAND_MASK) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 3590 | if (node->nodetype & (LYS_LEAF | LYS_ANYXML | LYS_CHOICE)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3591 | /* remove current value */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 3592 | node->flags &= ~LYS_MAND_MASK; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3593 | |
| 3594 | /* set new value */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 3595 | node->flags |= (rfn->flags & LYS_MAND_MASK); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3596 | } |
| 3597 | } |
| 3598 | |
| 3599 | /* presence on container */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 3600 | if ((node->nodetype & LYS_CONTAINER) && rfn->mod.presence) { |
| 3601 | lydict_remove(ctx, ((struct lys_node_container *)node)->presence); |
| 3602 | ((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] | 3603 | } |
| 3604 | |
| 3605 | /* min/max-elements on list or leaf-list */ |
| 3606 | /* magic - bit 3 in flags means min set, bit 4 says max set */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 3607 | if (node->nodetype == LYS_LIST) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3608 | if (rfn->flags & 0x04) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 3609 | ((struct lys_node_list *)node)->min = rfn->mod.list.min; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3610 | } |
| 3611 | if (rfn->flags & 0x08) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 3612 | ((struct lys_node_list *)node)->max = rfn->mod.list.max; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3613 | } |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 3614 | } else if (node->nodetype == LYS_LEAFLIST) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3615 | if (rfn->flags & 0x04) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 3616 | ((struct lys_node_leaflist *)node)->min = rfn->mod.list.min; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3617 | } |
| 3618 | if (rfn->flags & 0x08) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 3619 | ((struct lys_node_leaflist *)node)->max = rfn->mod.list.max; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3620 | } |
| 3621 | } |
| 3622 | |
| 3623 | /* must in leaf, leaf-list, list, container or anyxml */ |
| 3624 | if (rfn->must_size) { |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 3625 | switch (node->nodetype) { |
| 3626 | case LYS_LEAF: |
| 3627 | old_size = &((struct lys_node_leaf *)node)->must_size; |
| 3628 | old_must = &((struct lys_node_leaf *)node)->must; |
| 3629 | break; |
| 3630 | case LYS_LEAFLIST: |
| 3631 | old_size = &((struct lys_node_leaflist *)node)->must_size; |
| 3632 | old_must = &((struct lys_node_leaflist *)node)->must; |
| 3633 | break; |
| 3634 | case LYS_LIST: |
| 3635 | old_size = &((struct lys_node_list *)node)->must_size; |
| 3636 | old_must = &((struct lys_node_list *)node)->must; |
| 3637 | break; |
| 3638 | case LYS_CONTAINER: |
| 3639 | old_size = &((struct lys_node_container *)node)->must_size; |
| 3640 | old_must = &((struct lys_node_container *)node)->must; |
| 3641 | break; |
| 3642 | case LYS_ANYXML: |
| 3643 | old_size = &((struct lys_node_anyxml *)node)->must_size; |
| 3644 | old_must = &((struct lys_node_anyxml *)node)->must; |
| 3645 | break; |
| 3646 | default: |
| 3647 | LOGINT; |
Radek Krejci | e4e4d72 | 2015-10-05 16:53:50 +0200 | [diff] [blame] | 3648 | return -1; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 3649 | } |
| 3650 | |
| 3651 | size = *old_size + rfn->must_size; |
| 3652 | must = realloc(*old_must, size * sizeof *rfn->must); |
| 3653 | if (!must) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3654 | LOGMEM; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3655 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3656 | } |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 3657 | for (i = 0, j = *old_size; i < rfn->must_size; i++, j++) { |
| 3658 | must[j].expr = lydict_insert(ctx, rfn->must[i].expr, 0); |
| 3659 | must[j].dsc = lydict_insert(ctx, rfn->must[i].dsc, 0); |
| 3660 | must[j].ref = lydict_insert(ctx, rfn->must[i].ref, 0); |
| 3661 | must[j].eapptag = lydict_insert(ctx, rfn->must[i].eapptag, 0); |
| 3662 | must[j].emsg = lydict_insert(ctx, rfn->must[i].emsg, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3663 | } |
| 3664 | |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 3665 | *old_must = must; |
| 3666 | *old_size = size; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3667 | } |
| 3668 | } |
| 3669 | |
| 3670 | /* apply augments */ |
| 3671 | for (i = 0; i < uses->augment_size; i++) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3672 | rc = resolve_augment(&uses->augment[i], uses->child); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3673 | if (rc) { |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 3674 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3675 | } |
| 3676 | } |
| 3677 | |
| 3678 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3679 | } |
| 3680 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3681 | /** |
| 3682 | * @brief Resolve base identity recursively. Does not log. |
| 3683 | * |
| 3684 | * @param[in] module Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3685 | * @param[in] ident Identity to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3686 | * @param[in] basename Base name of the identity. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3687 | * @param[out] ret Pointer to the resolved identity. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3688 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3689 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3690 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3691 | static int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3692 | resolve_base_ident_sub(const struct lys_module *module, struct lys_ident *ident, const char *basename, |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3693 | struct lys_ident **ret) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3694 | { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3695 | uint32_t i, j; |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 3696 | struct lys_ident *base = NULL, *base_iter; |
Radek Krejci | a52656e | 2015-08-05 13:41:50 +0200 | [diff] [blame] | 3697 | struct lys_ident_der *der; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3698 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3699 | assert(ret); |
| 3700 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3701 | /* search module */ |
| 3702 | for (i = 0; i < module->ident_size; i++) { |
| 3703 | if (!strcmp(basename, module->ident[i].name)) { |
| 3704 | |
| 3705 | if (!ident) { |
| 3706 | /* just search for type, so do not modify anything, just return |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 3707 | * the base identity pointer */ |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3708 | *ret = &module->ident[i]; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3709 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3710 | } |
| 3711 | |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 3712 | base = &module->ident[i]; |
| 3713 | goto matchfound; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3714 | } |
| 3715 | } |
| 3716 | |
| 3717 | /* search submodules */ |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 3718 | for (j = 0; j < module->inc_size && module->inc[j].submodule; j++) { |
| 3719 | for (i = 0; i < module->inc[j].submodule->ident_size; i++) { |
| 3720 | if (!strcmp(basename, module->inc[j].submodule->ident[i].name)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3721 | |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 3722 | if (!ident) { |
| 3723 | *ret = &module->inc[j].submodule->ident[i]; |
| 3724 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3725 | } |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 3726 | |
| 3727 | base = &module->inc[j].submodule->ident[i]; |
| 3728 | goto matchfound; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3729 | } |
| 3730 | } |
| 3731 | } |
| 3732 | |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 3733 | matchfound: |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3734 | /* we found it somewhere */ |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 3735 | if (base) { |
| 3736 | /* check for circular reference */ |
| 3737 | for (base_iter = base; base_iter; base_iter = base_iter->base) { |
| 3738 | if (ident == base_iter) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3739 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, base_iter->name, "base"); |
| 3740 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Circular reference of \"%s\" identity.", basename); |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 3741 | return EXIT_FAILURE; |
| 3742 | } |
| 3743 | } |
| 3744 | /* checks done, store the result */ |
| 3745 | ident->base = base; |
| 3746 | |
| 3747 | /* maintain backlinks to the derived identitise */ |
| 3748 | while (base) { |
| 3749 | for (der = base->der; der && der->next; der = der->next); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3750 | if (der) { |
| 3751 | der->next = malloc(sizeof *der); |
| 3752 | der = der->next; |
| 3753 | } else { |
| 3754 | ident->base->der = der = malloc(sizeof *der); |
| 3755 | } |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3756 | if (!der) { |
| 3757 | LOGMEM; |
| 3758 | return EXIT_FAILURE; |
| 3759 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3760 | der->next = NULL; |
| 3761 | der->ident = ident; |
| 3762 | |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 3763 | base = base->base; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3764 | } |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3765 | *ret = ident->base; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3766 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3767 | } |
| 3768 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3769 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3770 | } |
| 3771 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3772 | /** |
| 3773 | * @brief Resolve base identity. Logs directly. |
| 3774 | * |
| 3775 | * @param[in] module Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3776 | * @param[in] ident Identity to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3777 | * @param[in] basename Base name of the identity. |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 3778 | * @param[in] parent Either "type" or "identity". |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3779 | * @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] | 3780 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3781 | * @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] | 3782 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3783 | static int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3784 | resolve_base_ident(const struct lys_module *module, struct lys_ident *ident, const char *basename, const char* parent, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3785 | struct lys_type *type) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3786 | { |
| 3787 | const char *name; |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 3788 | int i, mod_name_len = 0; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3789 | struct lys_ident *target, **ret; |
| 3790 | uint8_t flags; |
| 3791 | struct lys_module *mod; |
| 3792 | |
| 3793 | assert((ident && !type) || (!ident && type)); |
| 3794 | |
| 3795 | if (!type) { |
| 3796 | /* have ident to resolve */ |
| 3797 | ret = ⌖ |
| 3798 | flags = ident->flags; |
| 3799 | mod = ident->module; |
| 3800 | } else { |
| 3801 | /* have type to fill */ |
| 3802 | ret = &type->info.ident.ref; |
| 3803 | flags = type->parent->flags; |
| 3804 | mod = type->parent->module; |
| 3805 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3806 | |
| 3807 | /* search for the base identity */ |
| 3808 | name = strchr(basename, ':'); |
| 3809 | if (name) { |
| 3810 | /* set name to correct position after colon */ |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 3811 | mod_name_len = name - basename; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3812 | name++; |
| 3813 | |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 3814 | 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] | 3815 | /* prefix refers to the current module, ignore it */ |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 3816 | mod_name_len = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3817 | } |
| 3818 | } else { |
| 3819 | name = basename; |
| 3820 | } |
| 3821 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 3822 | /* get module where to search */ |
| 3823 | module = lys_get_import_module(module, NULL, 0, mod_name_len ? basename : NULL, mod_name_len); |
| 3824 | if (!module) { |
| 3825 | /* identity refers unknown data model */ |
Radek Krejci | 02a0499 | 2016-03-17 16:06:37 +0100 | [diff] [blame] | 3826 | LOGVAL(LYE_INMOD, LY_VLOG_NONE, NULL, basename); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 3827 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3828 | } |
| 3829 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 3830 | /* search in the identified module ... */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3831 | if (!resolve_base_ident_sub(module, ident, name, ret)) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3832 | goto success; |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 3833 | } else if (ly_errno) { |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 3834 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3835 | } |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 3836 | /* and all its submodules */ |
| 3837 | for (i = 0; i < module->inc_size && module->inc[i].submodule; i++) { |
| 3838 | if (!resolve_base_ident_sub((struct lys_module *)module->inc[i].submodule, ident, name, ret)) { |
| 3839 | goto success; |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 3840 | } else if (ly_errno) { |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 3841 | return EXIT_FAILURE; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 3842 | } |
| 3843 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3844 | |
Radek Krejci | 02a0499 | 2016-03-17 16:06:37 +0100 | [diff] [blame] | 3845 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, basename, parent); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3846 | return EXIT_FAILURE; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3847 | |
| 3848 | success: |
| 3849 | /* check status */ |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3850 | if (lyp_check_status(flags, mod, ident ? ident->name : "of type", |
| 3851 | (*ret)->flags, (*ret)->module, (*ret)->name, NULL)) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3852 | return -1; |
| 3853 | } |
| 3854 | |
| 3855 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3856 | } |
| 3857 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3858 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3859 | * @brief Resolve JSON data format identityref. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3860 | * |
| 3861 | * @param[in] base Base identity. |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 3862 | * @param[in] ident_name Identityref name. |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3863 | * @param[in] node Node where the identityref is being resolved |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3864 | * |
| 3865 | * @return Pointer to the identity resolvent, NULL on error. |
| 3866 | */ |
Radek Krejci | a52656e | 2015-08-05 13:41:50 +0200 | [diff] [blame] | 3867 | struct lys_ident * |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3868 | 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] | 3869 | { |
Michal Vasko | c633ca0 | 2015-08-21 14:03:51 +0200 | [diff] [blame] | 3870 | const char *mod_name, *name; |
| 3871 | int mod_name_len, rc; |
Radek Krejci | a52656e | 2015-08-05 13:41:50 +0200 | [diff] [blame] | 3872 | struct lys_ident_der *der; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3873 | |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 3874 | if (!base || !ident_name) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3875 | return NULL; |
| 3876 | } |
| 3877 | |
Michal Vasko | c633ca0 | 2015-08-21 14:03:51 +0200 | [diff] [blame] | 3878 | 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] | 3879 | if (rc < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3880 | 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] | 3881 | return NULL; |
Michal Vasko | b43bb3c | 2016-03-17 15:00:27 +0100 | [diff] [blame] | 3882 | } else if (rc < (signed)strlen(ident_name)) { |
Radek Krejci | 02a0499 | 2016-03-17 16:06:37 +0100 | [diff] [blame] | 3883 | 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] | 3884 | return NULL; |
| 3885 | } |
| 3886 | |
Michal Vasko | c633ca0 | 2015-08-21 14:03:51 +0200 | [diff] [blame] | 3887 | if (!strcmp(base->name, name) && (!mod_name |
| 3888 | || (!strncmp(base->module->name, mod_name, mod_name_len) && !base->module->name[mod_name_len]))) { |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 3889 | return base; |
| 3890 | } |
| 3891 | |
| 3892 | for (der = base->der; der; der = der->next) { |
Michal Vasko | c633ca0 | 2015-08-21 14:03:51 +0200 | [diff] [blame] | 3893 | if (!strcmp(der->ident->name, name) && (!mod_name |
| 3894 | || (!strncmp(der->ident->module->name, mod_name, mod_name_len) |
| 3895 | && !der->ident->module->name[mod_name_len]))) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3896 | /* we have match */ |
| 3897 | return der->ident; |
| 3898 | } |
| 3899 | } |
| 3900 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3901 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYD, node, "identityref", ident_name); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3902 | return NULL; |
| 3903 | } |
| 3904 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3905 | /** |
Michal Vasko | 7955b36 | 2015-09-04 14:18:15 +0200 | [diff] [blame] | 3906 | * @brief Resolve (find) choice default case. Does not log. |
| 3907 | * |
| 3908 | * @param[in] choic Choice to use. |
| 3909 | * @param[in] dflt Name of the default case. |
| 3910 | * |
| 3911 | * @return Pointer to the default node or NULL. |
| 3912 | */ |
| 3913 | static struct lys_node * |
| 3914 | resolve_choice_dflt(struct lys_node_choice *choic, const char *dflt) |
| 3915 | { |
| 3916 | struct lys_node *child, *ret; |
| 3917 | |
| 3918 | LY_TREE_FOR(choic->child, child) { |
| 3919 | if (child->nodetype == LYS_USES) { |
| 3920 | ret = resolve_choice_dflt((struct lys_node_choice *)child, dflt); |
| 3921 | if (ret) { |
| 3922 | return ret; |
| 3923 | } |
| 3924 | } |
| 3925 | |
Radek Krejci | 749190d | 2016-02-18 16:26:25 +0100 | [diff] [blame] | 3926 | 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] | 3927 | | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST))) { |
| 3928 | return child; |
| 3929 | } |
| 3930 | } |
| 3931 | |
| 3932 | return NULL; |
| 3933 | } |
| 3934 | |
| 3935 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3936 | * @brief Resolve unresolved uses. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3937 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3938 | * @param[in] uses Uses to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3939 | * @param[in] unres Specific unres item. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3940 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3941 | * @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] | 3942 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3943 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3944 | 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] | 3945 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3946 | int rc; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 3947 | struct lys_node *par_grp; |
Michal Vasko | e91afce | 2015-08-12 12:21:00 +0200 | [diff] [blame] | 3948 | |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 3949 | /* HACK: when a grouping has uses inside, all such uses have to be resolved before the grouping itself |
| 3950 | * is used in some uses. When we see such a uses, the grouping's nacm member (not used in grouping) |
| 3951 | * is used to store number of so far unresolved uses. The grouping cannot be used unless the nacm |
| 3952 | * value is decreased back to 0. To remember that the uses already increased grouping's nacm, the |
| 3953 | * LYS_USESGRP flag is used. */ |
| 3954 | for (par_grp = uses->parent; par_grp && (par_grp->nodetype != LYS_GROUPING); par_grp = par_grp->parent); |
Michal Vasko | e91afce | 2015-08-12 12:21:00 +0200 | [diff] [blame] | 3955 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3956 | if (!uses->grp) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 3957 | rc = resolve_uses_schema_nodeid(uses->name, (const struct lys_node *)uses, (const struct lys_node_grp **)&uses->grp); |
| 3958 | if (rc == -1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3959 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, uses, "grouping", uses->name); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 3960 | return -1; |
| 3961 | } else if (rc > 0) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3962 | 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] | 3963 | return -1; |
| 3964 | } else if (!uses->grp) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 3965 | if (par_grp && !(uses->flags & LYS_USESGRP)) { |
| 3966 | par_grp->nacm++; |
| 3967 | uses->flags |= LYS_USESGRP; |
Michal Vasko | 407f1bb | 2015-09-23 15:51:07 +0200 | [diff] [blame] | 3968 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 3969 | return EXIT_FAILURE; |
Michal Vasko | 12e3084 | 2015-08-04 11:54:00 +0200 | [diff] [blame] | 3970 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3971 | } |
| 3972 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3973 | if (uses->grp->nacm) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 3974 | if (par_grp && !(uses->flags & LYS_USESGRP)) { |
| 3975 | par_grp->nacm++; |
| 3976 | uses->flags |= LYS_USESGRP; |
Michal Vasko | 407f1bb | 2015-09-23 15:51:07 +0200 | [diff] [blame] | 3977 | } |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3978 | return EXIT_FAILURE; |
| 3979 | } |
| 3980 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3981 | rc = resolve_uses(uses, unres); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3982 | if (!rc) { |
| 3983 | /* decrease unres count only if not first try */ |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 3984 | if (par_grp && (uses->flags & LYS_USESGRP)) { |
| 3985 | if (!par_grp->nacm) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3986 | LOGINT; |
| 3987 | return -1; |
| 3988 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 3989 | par_grp->nacm--; |
| 3990 | uses->flags &= ~LYS_USESGRP; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3991 | } |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3992 | |
| 3993 | /* check status */ |
Radek Krejci | c655602 | 2016-01-27 15:16:45 +0100 | [diff] [blame] | 3994 | if (lyp_check_status(uses->flags, uses->module, "of uses", |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3995 | uses->grp->flags, uses->grp->module, uses->grp->name, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3996 | (struct lys_node *)uses)) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3997 | return -1; |
| 3998 | } |
| 3999 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4000 | return EXIT_SUCCESS; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 4001 | } else if ((rc == EXIT_FAILURE) && par_grp && !(uses->flags & LYS_USESGRP)) { |
| 4002 | par_grp->nacm++; |
| 4003 | uses->flags |= LYS_USESGRP; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4004 | } |
| 4005 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4006 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4007 | } |
| 4008 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4009 | /** |
Michal Vasko | 9957e59 | 2015-08-17 15:04:09 +0200 | [diff] [blame] | 4010 | * @brief Resolve list keys. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4011 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4012 | * @param[in] list List to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4013 | * @param[in] keys_str Keys node value. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4014 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4015 | * @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] | 4016 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4017 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4018 | resolve_list_keys(struct lys_node_list *list, const char *keys_str) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4019 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4020 | int i, len, rc; |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 4021 | const char *value; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4022 | |
| 4023 | for (i = 0; i < list->keys_size; ++i) { |
| 4024 | /* get the key name */ |
| 4025 | if ((value = strpbrk(keys_str, " \t\n"))) { |
| 4026 | len = value - keys_str; |
| 4027 | while (isspace(value[0])) { |
| 4028 | value++; |
| 4029 | } |
| 4030 | } else { |
| 4031 | len = strlen(keys_str); |
| 4032 | } |
| 4033 | |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 4034 | rc = lys_get_sibling(list->child, lys_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] | 4035 | if (rc) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 4036 | if (rc == -1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4037 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, list, "list keys", keys_str); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4038 | } |
| 4039 | return rc; |
| 4040 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4041 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4042 | if (check_key(list, i, keys_str, len)) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4043 | /* check_key logs */ |
| 4044 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4045 | } |
| 4046 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 4047 | /* check status */ |
Radek Krejci | c655602 | 2016-01-27 15:16:45 +0100 | [diff] [blame] | 4048 | if (lyp_check_status(list->flags, list->module, list->name, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4049 | list->keys[i]->flags, list->keys[i]->module, list->keys[i]->name, |
| 4050 | (struct lys_node *)list->keys[i])) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 4051 | return -1; |
| 4052 | } |
| 4053 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4054 | /* prepare for next iteration */ |
| 4055 | while (value && isspace(value[0])) { |
| 4056 | value++; |
| 4057 | } |
| 4058 | keys_str = value; |
| 4059 | } |
| 4060 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 4061 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4062 | } |
| 4063 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4064 | /** |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 4065 | * @brief Resolve (check) all must conditions of \p node. |
| 4066 | * Logs directly. |
| 4067 | * |
| 4068 | * @param[in] node Data node with optional must statements. |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 4069 | * |
| 4070 | * @return EXIT_SUCCESS on pass, EXIT_FAILURE on fail, -1 on error. |
| 4071 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4072 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4073 | resolve_must(struct lyd_node *node) |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 4074 | { |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 4075 | uint8_t i, must_size; |
| 4076 | struct lys_restr *must; |
| 4077 | struct lyxp_set set; |
| 4078 | |
| 4079 | assert(node); |
| 4080 | memset(&set, 0, sizeof set); |
| 4081 | |
| 4082 | switch (node->schema->nodetype) { |
| 4083 | case LYS_CONTAINER: |
| 4084 | must_size = ((struct lys_node_container *)node->schema)->must_size; |
| 4085 | must = ((struct lys_node_container *)node->schema)->must; |
| 4086 | break; |
| 4087 | case LYS_LEAF: |
| 4088 | must_size = ((struct lys_node_leaf *)node->schema)->must_size; |
| 4089 | must = ((struct lys_node_leaf *)node->schema)->must; |
| 4090 | break; |
| 4091 | case LYS_LEAFLIST: |
| 4092 | must_size = ((struct lys_node_leaflist *)node->schema)->must_size; |
| 4093 | must = ((struct lys_node_leaflist *)node->schema)->must; |
| 4094 | break; |
| 4095 | case LYS_LIST: |
| 4096 | must_size = ((struct lys_node_list *)node->schema)->must_size; |
| 4097 | must = ((struct lys_node_list *)node->schema)->must; |
| 4098 | break; |
| 4099 | case LYS_ANYXML: |
| 4100 | must_size = ((struct lys_node_anyxml *)node->schema)->must_size; |
| 4101 | must = ((struct lys_node_anyxml *)node->schema)->must; |
| 4102 | break; |
| 4103 | default: |
| 4104 | must_size = 0; |
| 4105 | break; |
| 4106 | } |
| 4107 | |
| 4108 | for (i = 0; i < must_size; ++i) { |
Michal Vasko | 944a564 | 2016-03-21 11:48:58 +0100 | [diff] [blame] | 4109 | if (lyxp_eval(must[i].expr, node, &set, LYXP_MUST)) { |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 4110 | return -1; |
| 4111 | } |
| 4112 | |
Michal Vasko | 944a564 | 2016-03-21 11:48:58 +0100 | [diff] [blame] | 4113 | lyxp_set_cast(&set, LYXP_SET_BOOLEAN, node, LYXP_MUST); |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 4114 | |
| 4115 | if (!set.value.bool) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4116 | LOGVAL(LYE_NOCOND, LY_VLOG_LYD, node, "Must", must[i].expr); |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 4117 | return 1; |
| 4118 | } |
| 4119 | } |
| 4120 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4121 | return EXIT_SUCCESS; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 4122 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4123 | |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 4124 | /** |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4125 | * @brief Resolve (find) when condition context node. Does not log. |
| 4126 | * |
| 4127 | * @param[in] node Data node, whose conditional definition is being decided. |
| 4128 | * @param[in] schema Schema node with a when condition. |
| 4129 | * |
| 4130 | * @return Context node. |
| 4131 | */ |
| 4132 | static struct lyd_node * |
| 4133 | resolve_when_ctx_node(struct lyd_node *node, struct lys_node *schema) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4134 | { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4135 | struct lyd_node *parent; |
| 4136 | struct lys_node *sparent; |
| 4137 | uint16_t i, data_depth, schema_depth; |
| 4138 | |
| 4139 | /* find a not schema-only node */ |
| 4140 | while (schema->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE | LYS_AUGMENT | LYS_INPUT | LYS_OUTPUT)) { |
| 4141 | schema = lys_parent(schema); |
| 4142 | if (!schema) { |
| 4143 | return NULL; |
| 4144 | } |
| 4145 | } |
| 4146 | |
| 4147 | /* get node depths */ |
| 4148 | for (parent = node, data_depth = 0; parent; parent = parent->parent, ++data_depth); |
| 4149 | for (sparent = lys_parent(schema), schema_depth = 1; sparent; sparent = lys_parent(sparent)) { |
| 4150 | if (sparent->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYXML | LYS_NOTIF | LYS_RPC)) { |
| 4151 | ++schema_depth; |
| 4152 | } |
| 4153 | } |
| 4154 | if (data_depth < schema_depth) { |
| 4155 | return NULL; |
| 4156 | } |
| 4157 | |
| 4158 | /* find the corresponding data node */ |
| 4159 | for (i = 0; i < data_depth - schema_depth; ++i) { |
| 4160 | node = node->parent; |
| 4161 | } |
| 4162 | if (node->schema != schema) { |
| 4163 | return NULL; |
| 4164 | } |
| 4165 | |
| 4166 | return node; |
| 4167 | } |
| 4168 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 4169 | int |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 4170 | resolve_applies_must(const struct lyd_node *node) |
| 4171 | { |
| 4172 | switch (node->schema->nodetype) { |
| 4173 | case LYS_CONTAINER: |
| 4174 | return ((struct lys_node_container *)node->schema)->must_size; |
| 4175 | case LYS_LEAF: |
| 4176 | return ((struct lys_node_leaf *)node->schema)->must_size; |
| 4177 | case LYS_LEAFLIST: |
| 4178 | return ((struct lys_node_leaflist *)node->schema)->must_size; |
| 4179 | case LYS_LIST: |
| 4180 | return ((struct lys_node_list *)node->schema)->must_size; |
| 4181 | case LYS_ANYXML: |
| 4182 | return ((struct lys_node_anyxml *)node->schema)->must_size; |
| 4183 | default: |
| 4184 | return 0; |
| 4185 | } |
| 4186 | } |
| 4187 | |
| 4188 | int |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 4189 | resolve_applies_when(const struct lyd_node *node) |
| 4190 | { |
| 4191 | struct lys_node *parent; |
| 4192 | |
| 4193 | assert(node); |
| 4194 | |
| 4195 | if (!(node->schema->nodetype & (LYS_NOTIF | LYS_RPC)) && (((struct lys_node_container *)node->schema)->when)) { |
| 4196 | return 1; |
| 4197 | } |
| 4198 | |
| 4199 | parent = node->schema; |
| 4200 | goto check_augment; |
| 4201 | |
| 4202 | while (parent && (parent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) { |
| 4203 | if (((struct lys_node_uses *)parent)->when) { |
| 4204 | return 1; |
| 4205 | } |
| 4206 | check_augment: |
| 4207 | |
| 4208 | if ((parent->parent && (parent->parent->nodetype == LYS_AUGMENT) && |
| 4209 | (((struct lys_node_augment *)parent->parent)->when))) { |
| 4210 | |
| 4211 | } |
| 4212 | parent = lys_parent(parent); |
| 4213 | } |
| 4214 | |
| 4215 | return 0; |
| 4216 | } |
| 4217 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4218 | /** |
| 4219 | * @brief Resolve (check) all when conditions relevant for \p node. |
| 4220 | * Logs directly. |
| 4221 | * |
| 4222 | * @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] | 4223 | * |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 4224 | * @return |
| 4225 | * -1 - error, ly_errno is set |
| 4226 | * 0 - true "when" statement |
| 4227 | * 0, ly_vecode = LYVE_NOCOND - false "when" statement |
| 4228 | * 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] | 4229 | */ |
| 4230 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4231 | resolve_when(struct lyd_node *node) |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4232 | { |
| 4233 | struct lyd_node *ctx_node = NULL; |
| 4234 | struct lys_node *parent; |
| 4235 | struct lyxp_set set; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 4236 | int rc = 0; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4237 | |
| 4238 | assert(node); |
| 4239 | memset(&set, 0, sizeof set); |
| 4240 | |
| 4241 | 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] | 4242 | 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] | 4243 | if (rc) { |
| 4244 | if (rc == 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4245 | 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] | 4246 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 4247 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4248 | } |
| 4249 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 4250 | /* set boolean result of the condition */ |
Michal Vasko | 944a564 | 2016-03-21 11:48:58 +0100 | [diff] [blame] | 4251 | lyxp_set_cast(&set, LYXP_SET_BOOLEAN, node, LYXP_WHEN); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4252 | if (!set.value.bool) { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 4253 | ly_vlog_hide(1); |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4254 | LOGVAL(LYE_NOCOND, LY_VLOG_LYD, node, "When", ((struct lys_node_container *)node->schema)->when->cond); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 4255 | ly_vlog_hide(0); |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 4256 | node->when_status |= LYD_WHEN_FALSE; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 4257 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4258 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 4259 | |
| 4260 | /* free xpath set content */ |
| 4261 | lyxp_set_cast(&set, LYXP_SET_EMPTY, node, 0); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4262 | } |
| 4263 | |
| 4264 | parent = node->schema; |
| 4265 | goto check_augment; |
| 4266 | |
| 4267 | /* check when in every schema node that affects node */ |
| 4268 | while (parent && (parent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) { |
| 4269 | if (((struct lys_node_uses *)parent)->when) { |
| 4270 | if (!ctx_node) { |
| 4271 | ctx_node = resolve_when_ctx_node(node, parent); |
| 4272 | if (!ctx_node) { |
| 4273 | LOGINT; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 4274 | rc = -1; |
| 4275 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4276 | } |
| 4277 | } |
Michal Vasko | 944a564 | 2016-03-21 11:48:58 +0100 | [diff] [blame] | 4278 | 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] | 4279 | if (rc) { |
| 4280 | if (rc == 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4281 | 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] | 4282 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 4283 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4284 | } |
| 4285 | |
Michal Vasko | 944a564 | 2016-03-21 11:48:58 +0100 | [diff] [blame] | 4286 | lyxp_set_cast(&set, LYXP_SET_BOOLEAN, ctx_node, LYXP_WHEN); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4287 | if (!set.value.bool) { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 4288 | ly_vlog_hide(1); |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4289 | LOGVAL(LYE_NOCOND, LY_VLOG_LYD, node, "When", ((struct lys_node_uses *)parent)->when->cond); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 4290 | ly_vlog_hide(0); |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 4291 | node->when_status |= LYD_WHEN_FALSE; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 4292 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4293 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 4294 | |
| 4295 | /* free xpath set content */ |
| 4296 | lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node, 0); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4297 | } |
| 4298 | |
| 4299 | check_augment: |
| 4300 | if ((parent->parent && (parent->parent->nodetype == LYS_AUGMENT) && (((struct lys_node_augment *)parent->parent)->when))) { |
| 4301 | if (!ctx_node) { |
| 4302 | ctx_node = resolve_when_ctx_node(node, parent->parent); |
| 4303 | if (!ctx_node) { |
| 4304 | LOGINT; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 4305 | rc = -1; |
| 4306 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4307 | } |
| 4308 | } |
Michal Vasko | 944a564 | 2016-03-21 11:48:58 +0100 | [diff] [blame] | 4309 | 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] | 4310 | if (rc) { |
| 4311 | if (rc == 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4312 | 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] | 4313 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 4314 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4315 | } |
| 4316 | |
Michal Vasko | 944a564 | 2016-03-21 11:48:58 +0100 | [diff] [blame] | 4317 | lyxp_set_cast(&set, LYXP_SET_BOOLEAN, ctx_node, LYXP_WHEN); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4318 | |
| 4319 | if (!set.value.bool) { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 4320 | ly_vlog_hide(1); |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4321 | LOGVAL(LYE_NOCOND, LY_VLOG_LYD, node, "When", ((struct lys_node_augment *)parent->parent)->when->cond); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 4322 | ly_vlog_hide(0); |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 4323 | node->when_status |= LYD_WHEN_FALSE; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 4324 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4325 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 4326 | |
| 4327 | /* free xpath set content */ |
| 4328 | lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node, 0); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4329 | } |
| 4330 | |
| 4331 | parent = lys_parent(parent); |
| 4332 | } |
| 4333 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 4334 | node->when_status |= LYD_WHEN_TRUE; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 4335 | |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 4336 | cleanup: |
| 4337 | |
| 4338 | /* free xpath set content */ |
| 4339 | lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node ? ctx_node : node, 0); |
| 4340 | |
| 4341 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4342 | } |
| 4343 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4344 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4345 | * @brief Resolve a single unres schema item. Logs indirectly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4346 | * |
| 4347 | * @param[in] mod Main module. |
| 4348 | * @param[in] item Item to resolve. Type determined by \p type. |
| 4349 | * @param[in] type Type of the unresolved item. |
| 4350 | * @param[in] str_snode String, a schema node, or NULL. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4351 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4352 | * |
| 4353 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 4354 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4355 | static int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 4356 | 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] | 4357 | struct unres_schema *unres) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4358 | { |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 4359 | int rc = -1, has_str = 0, tpdf_flag = 0; |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 4360 | struct lys_node *node; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 4361 | const char *base_name; |
| 4362 | |
| 4363 | struct lys_ident *ident; |
| 4364 | struct lys_type *stype; |
| 4365 | struct lys_feature **feat_ptr; |
| 4366 | struct lys_node_choice *choic; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 4367 | struct lyxml_elem *yin; |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 4368 | struct yang_type *yang; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4369 | |
| 4370 | switch (type) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4371 | case UNRES_IDENT: |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 4372 | base_name = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 4373 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 4374 | ident = item; |
| 4375 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4376 | rc = resolve_base_ident(mod, ident, base_name, "identity", NULL); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4377 | break; |
| 4378 | case UNRES_TYPE_IDENTREF: |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 4379 | base_name = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 4380 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 4381 | stype = item; |
| 4382 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4383 | rc = resolve_base_ident(mod, NULL, base_name, "type", stype); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4384 | break; |
| 4385 | case UNRES_TYPE_LEAFREF: |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 4386 | node = str_snode; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 4387 | stype = item; |
| 4388 | |
Radek Krejci | 2f12f85 | 2016-01-08 12:59:57 +0100 | [diff] [blame] | 4389 | /* HACK - when there is no parent, we are in top level typedef and in that |
| 4390 | * case, the path has to contain absolute path, so we let the resolve_path_arg_schema() |
| 4391 | * know it via tpdf_flag */ |
| 4392 | if (!node) { |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 4393 | tpdf_flag = 1; |
Radek Krejci | 2f12f85 | 2016-01-08 12:59:57 +0100 | [diff] [blame] | 4394 | node = (struct lys_node *)stype->parent; |
| 4395 | } |
| 4396 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4397 | rc = resolve_path_arg_schema(stype->info.lref.path, node, tpdf_flag, |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 4398 | (const struct lys_node **)&stype->info.lref.target); |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 4399 | if (stype->info.lref.target) { |
| 4400 | /* store the backlink from leafref target */ |
| 4401 | if (!stype->info.lref.target->child) { |
| 4402 | stype->info.lref.target->child = (void*)ly_set_new(); |
| 4403 | if (!stype->info.lref.target->child) { |
| 4404 | LOGMEM; |
| 4405 | return -1; |
| 4406 | } |
| 4407 | } |
| 4408 | ly_set_add((struct ly_set *)stype->info.lref.target->child, stype->parent); |
| 4409 | } |
| 4410 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4411 | break; |
| 4412 | case UNRES_TYPE_DER: |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 4413 | /* parent */ |
| 4414 | node = str_snode; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 4415 | stype = item; |
| 4416 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 4417 | /* HACK type->der is temporarily unparsed type statement */ |
| 4418 | yin = (struct lyxml_elem *)stype->der; |
| 4419 | stype->der = NULL; |
| 4420 | |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 4421 | if (yin->flags & LY_YANG_STRUCTURE_FLAG) { |
| 4422 | yang = (struct yang_type *)yin; |
| 4423 | rc = yang_check_type(mod, node, yang, unres); |
| 4424 | |
| 4425 | if (rc) { |
Pavol Vican | 933aa5a | 2016-04-09 21:05:46 +0200 | [diff] [blame] | 4426 | if (rc == -1) { |
| 4427 | yang->type->base = yang->base; |
| 4428 | lydict_remove(mod->ctx, yang->name); |
| 4429 | free(yang); |
| 4430 | stype->der = NULL; |
| 4431 | } else { |
| 4432 | /* may try again later */ |
| 4433 | stype->der = (struct lys_tpdf *)yang; |
| 4434 | } |
Pavol Vican | d01d8ae | 2016-03-01 10:45:59 +0100 | [diff] [blame] | 4435 | } else { |
| 4436 | /* 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] | 4437 | lydict_remove(mod->ctx, yang->name); |
Pavol Vican | d01d8ae | 2016-03-01 10:45:59 +0100 | [diff] [blame] | 4438 | free(yang); |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 4439 | } |
| 4440 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 4441 | } else { |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 4442 | rc = fill_yin_type(mod, node, yin, stype, unres); |
| 4443 | if (!rc) { |
| 4444 | /* we need to always be able to free this, it's safe only in this case */ |
| 4445 | lyxml_free(mod->ctx, yin); |
| 4446 | } else { |
| 4447 | /* may try again later, put all back how it was */ |
| 4448 | stype->der = (struct lys_tpdf *)yin; |
| 4449 | } |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 4450 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4451 | break; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4452 | case UNRES_IFFEAT: |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 4453 | base_name = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 4454 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 4455 | feat_ptr = item; |
| 4456 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4457 | rc = resolve_feature(base_name, mod, feat_ptr); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4458 | break; |
| 4459 | case UNRES_USES: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4460 | rc = resolve_unres_schema_uses(item, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4461 | break; |
| 4462 | case UNRES_TYPE_DFLT: |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 4463 | base_name = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 4464 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 4465 | stype = item; |
| 4466 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4467 | rc = check_default(stype, base_name, mod); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4468 | break; |
| 4469 | case UNRES_CHOICE_DFLT: |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 4470 | base_name = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 4471 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 4472 | choic = item; |
| 4473 | |
Michal Vasko | 7955b36 | 2015-09-04 14:18:15 +0200 | [diff] [blame] | 4474 | choic->dflt = resolve_choice_dflt(choic, base_name); |
| 4475 | if (choic->dflt) { |
| 4476 | rc = EXIT_SUCCESS; |
| 4477 | } else { |
| 4478 | rc = EXIT_FAILURE; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 4479 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4480 | break; |
| 4481 | case UNRES_LIST_KEYS: |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 4482 | has_str = 1; |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4483 | rc = resolve_list_keys(item, str_snode); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4484 | break; |
| 4485 | case UNRES_LIST_UNIQ: |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 4486 | has_str = 1; |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4487 | rc = resolve_unique(item, str_snode); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4488 | break; |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 4489 | case UNRES_AUGMENT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4490 | rc = resolve_augment(item, NULL); |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 4491 | break; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4492 | default: |
| 4493 | LOGINT; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4494 | break; |
| 4495 | } |
| 4496 | |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 4497 | if (has_str && !rc) { |
| 4498 | lydict_remove(mod->ctx, str_snode); |
| 4499 | } |
| 4500 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4501 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4502 | } |
| 4503 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 4504 | /* logs directly */ |
| 4505 | static void |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4506 | 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] | 4507 | { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 4508 | switch (type) { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 4509 | case UNRES_IDENT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4510 | 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] | 4511 | break; |
| 4512 | case UNRES_TYPE_IDENTREF: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4513 | 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] | 4514 | break; |
| 4515 | case UNRES_TYPE_LEAFREF: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4516 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "leafref", |
| 4517 | ((struct lys_type *)item)->info.lref.path); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 4518 | break; |
| 4519 | case UNRES_TYPE_DER: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4520 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "derived type", |
| 4521 | ((struct lyxml_elem *)((struct lys_type *)item)->der)->attr->value); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 4522 | break; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 4523 | case UNRES_IFFEAT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4524 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "if-feature", (char *)str_node); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 4525 | break; |
| 4526 | case UNRES_USES: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4527 | 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] | 4528 | break; |
| 4529 | case UNRES_TYPE_DFLT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4530 | 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] | 4531 | break; |
| 4532 | case UNRES_CHOICE_DFLT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4533 | 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] | 4534 | break; |
| 4535 | case UNRES_LIST_KEYS: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4536 | 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] | 4537 | break; |
| 4538 | case UNRES_LIST_UNIQ: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4539 | 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] | 4540 | break; |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 4541 | case UNRES_AUGMENT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4542 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "augment target", |
| 4543 | ((struct lys_node_augment *)item)->target_name); |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 4544 | break; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4545 | default: |
| 4546 | LOGINT; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 4547 | break; |
| 4548 | } |
| 4549 | } |
| 4550 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4551 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4552 | * @brief Resolve every unres schema item in the structure. Logs directly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4553 | * |
| 4554 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4555 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4556 | * |
Michal Vasko | 92b8a38 | 2015-08-19 14:03:49 +0200 | [diff] [blame] | 4557 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4558 | */ |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 4559 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 4560 | resolve_unres_schema(struct lys_module *mod, struct unres_schema *unres) |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 4561 | { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 4562 | uint32_t i, resolved = 0, unres_count, res_count; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4563 | int rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4564 | |
| 4565 | assert(unres); |
| 4566 | |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 4567 | LOGVRB("Resolving unresolved schema nodes and their constraints."); |
| 4568 | ly_vlog_hide(1); |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 4569 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4570 | /* uses */ |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 4571 | do { |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 4572 | unres_count = 0; |
| 4573 | res_count = 0; |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 4574 | |
| 4575 | for (i = 0; i < unres->count; ++i) { |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 4576 | /* we do not need to have UNRES_TYPE_IDENTREF or UNRES_TYPE_LEAFREF resolved, |
| 4577 | * we need every type's base only */ |
| 4578 | if ((unres->type[i] != UNRES_USES) && (unres->type[i] != UNRES_TYPE_DER)) { |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 4579 | continue; |
| 4580 | } |
| 4581 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 4582 | ++unres_count; |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4583 | 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] | 4584 | if (!rc) { |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 4585 | unres->type[i] = UNRES_RESOLVED; |
| 4586 | ++resolved; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 4587 | ++res_count; |
Michal Vasko | 89e1532 | 2015-08-17 15:46:55 +0200 | [diff] [blame] | 4588 | } else if (rc == -1) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 4589 | ly_vlog_hide(0); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4590 | return -1; |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 4591 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4592 | } |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 4593 | } while (res_count && (res_count < unres_count)); |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 4594 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 4595 | if (res_count < unres_count) { |
Michal Vasko | 92b8a38 | 2015-08-19 14:03:49 +0200 | [diff] [blame] | 4596 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4597 | } |
| 4598 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4599 | /* the rest */ |
| 4600 | for (i = 0; i < unres->count; ++i) { |
| 4601 | if (unres->type[i] == UNRES_RESOLVED) { |
| 4602 | continue; |
| 4603 | } |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 4604 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4605 | 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] | 4606 | if (rc == 0) { |
| 4607 | unres->type[i] = UNRES_RESOLVED; |
| 4608 | ++resolved; |
| 4609 | } else if (rc == -1) { |
| 4610 | ly_vlog_hide(0); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 4611 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4612 | } |
| 4613 | } |
| 4614 | |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 4615 | ly_vlog_hide(0); |
| 4616 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4617 | if (resolved < unres->count) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 4618 | /* try to resolve the unresolved nodes again, it will not resolve anything, but it will print |
| 4619 | * all the validation errors |
| 4620 | */ |
| 4621 | for (i = 0; i < unres->count; ++i) { |
| 4622 | if (unres->type[i] == UNRES_RESOLVED) { |
| 4623 | continue; |
| 4624 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4625 | 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] | 4626 | } |
Michal Vasko | 92b8a38 | 2015-08-19 14:03:49 +0200 | [diff] [blame] | 4627 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4628 | } |
| 4629 | |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 4630 | LOGVRB("Resolving unresolved schema nodes and their constraints."); |
| 4631 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 4632 | unres->count = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4633 | return EXIT_SUCCESS; |
| 4634 | } |
| 4635 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4636 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4637 | * @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] | 4638 | * |
| 4639 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4640 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4641 | * @param[in] item Item to resolve. Type determined by \p type. |
| 4642 | * @param[in] type Type of the unresolved item. |
| 4643 | * @param[in] str String argument. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4644 | * |
| 4645 | * @return EXIT_SUCCESS on success or storing the item in unres, -1 on error. |
| 4646 | */ |
| 4647 | int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4648 | unres_schema_add_str(struct lys_module *mod, struct unres_schema *unres, void *item, enum UNRES_ITEM type, |
| 4649 | const char *str) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4650 | { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4651 | return unres_schema_add_node(mod, unres, item, type, (struct lys_node *)lydict_insert(mod->ctx, str, 0)); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4652 | } |
| 4653 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4654 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4655 | * @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] | 4656 | * |
| 4657 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4658 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4659 | * @param[in] item Item to resolve. Type determined by \p type. |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 4660 | * @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] | 4661 | * @param[in] snode Schema node argument. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4662 | * |
| 4663 | * @return EXIT_SUCCESS on success or storing the item in unres, -1 on error. |
| 4664 | */ |
| 4665 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 4666 | 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] | 4667 | struct lys_node *snode) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4668 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4669 | int rc; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 4670 | struct lyxml_elem *yin; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 4671 | char *path, *msg; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4672 | |
Michal Vasko | 9bf425b | 2015-10-22 11:42:03 +0200 | [diff] [blame] | 4673 | assert(unres && item && ((type != UNRES_LEAFREF) && (type != UNRES_INSTID) && (type != UNRES_WHEN) |
| 4674 | && (type != UNRES_MUST))); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4675 | |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 4676 | ly_vlog_hide(1); |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4677 | rc = resolve_unres_schema_item(mod, item, type, snode, unres); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 4678 | ly_vlog_hide(0); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4679 | if (rc != EXIT_FAILURE) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 4680 | if (rc == -1 && ly_errno == LY_EVALID) { |
| 4681 | path = strdup(ly_errpath()); |
| 4682 | LOGERR(LY_EVALID, "%s%s%s%s", msg = strdup(ly_errmsg()), |
| 4683 | path[0] ? " (path: " : "", path[0] ? path : "", path[0] ? ")" : ""); |
| 4684 | free(path); |
| 4685 | free(msg); |
| 4686 | } |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4687 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4688 | } |
| 4689 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4690 | print_unres_schema_item_fail(item, type, snode); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 4691 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 4692 | /* HACK unlinking is performed here so that we do not do any (NS) copying in vain */ |
| 4693 | if (type == UNRES_TYPE_DER) { |
| 4694 | yin = (struct lyxml_elem *)((struct lys_type *)item)->der; |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 4695 | if (!(yin->flags & LY_YANG_STRUCTURE_FLAG)) { |
| 4696 | lyxml_unlink_elem(mod->ctx, yin, 1); |
| 4697 | ((struct lys_type *)item)->der = (struct lys_tpdf *)yin; |
| 4698 | } |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 4699 | } |
| 4700 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4701 | unres->count++; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 4702 | unres->item = ly_realloc(unres->item, unres->count*sizeof *unres->item); |
| 4703 | if (!unres->item) { |
| 4704 | LOGMEM; |
| 4705 | return -1; |
| 4706 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4707 | unres->item[unres->count-1] = item; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 4708 | unres->type = ly_realloc(unres->type, unres->count*sizeof *unres->type); |
| 4709 | if (!unres->type) { |
| 4710 | LOGMEM; |
| 4711 | return -1; |
| 4712 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4713 | unres->type[unres->count-1] = type; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 4714 | unres->str_snode = ly_realloc(unres->str_snode, unres->count*sizeof *unres->str_snode); |
| 4715 | if (!unres->str_snode) { |
| 4716 | LOGMEM; |
| 4717 | return -1; |
| 4718 | } |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4719 | unres->str_snode[unres->count-1] = snode; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 4720 | unres->module = ly_realloc(unres->module, unres->count*sizeof *unres->module); |
| 4721 | if (!unres->module) { |
| 4722 | LOGMEM; |
| 4723 | return -1; |
| 4724 | } |
| 4725 | unres->module[unres->count-1] = mod; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4726 | |
| 4727 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4728 | } |
| 4729 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4730 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4731 | * @brief Duplicate an unres schema item. Logs indirectly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4732 | * |
| 4733 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4734 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4735 | * @param[in] item Old item to be resolved. |
| 4736 | * @param[in] type Type of the old unresolved item. |
| 4737 | * @param[in] new_item New item to use in the duplicate. |
| 4738 | * |
| 4739 | * @return EXIT_SUCCESS on success, -1 on error. |
| 4740 | */ |
Michal Vasko | dad1940 | 2015-08-06 09:51:53 +0200 | [diff] [blame] | 4741 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 4742 | 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] | 4743 | { |
| 4744 | int i; |
| 4745 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4746 | 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] | 4747 | |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 4748 | i = unres_schema_find(unres, item, type); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4749 | |
| 4750 | if (i == -1) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4751 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4752 | } |
| 4753 | |
Michal Vasko | 0d20459 | 2015-10-07 09:50:04 +0200 | [diff] [blame] | 4754 | if ((type == UNRES_TYPE_LEAFREF) || (type == UNRES_USES) || (type == UNRES_TYPE_DFLT)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4755 | 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] | 4756 | LOGINT; |
| 4757 | return -1; |
| 4758 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4759 | } else { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4760 | 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] | 4761 | LOGINT; |
| 4762 | return -1; |
| 4763 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4764 | } |
Michal Vasko | dad1940 | 2015-08-06 09:51:53 +0200 | [diff] [blame] | 4765 | |
| 4766 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4767 | } |
| 4768 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 4769 | /* does not log */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4770 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 4771 | 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] | 4772 | { |
| 4773 | uint32_t ret = -1, i; |
| 4774 | |
| 4775 | for (i = 0; i < unres->count; ++i) { |
| 4776 | if ((unres->item[i] == item) && (unres->type[i] == type)) { |
| 4777 | ret = i; |
| 4778 | break; |
| 4779 | } |
| 4780 | } |
| 4781 | |
| 4782 | return ret; |
| 4783 | } |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 4784 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 4785 | void |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 4786 | unres_schema_free(struct lys_module *module, struct unres_schema **unres) |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 4787 | { |
| 4788 | uint32_t i; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 4789 | unsigned int unresolved = 0; |
Pavol Vican | d01d8ae | 2016-03-01 10:45:59 +0100 | [diff] [blame] | 4790 | struct lyxml_elem *yin; |
| 4791 | struct yang_type *yang; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 4792 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 4793 | if (!unres || !(*unres)) { |
| 4794 | return; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 4795 | } |
| 4796 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 4797 | assert(module || (*unres)->count == 0); |
| 4798 | |
| 4799 | for (i = 0; i < (*unres)->count; ++i) { |
| 4800 | if ((*unres)->module[i] != module) { |
| 4801 | if ((*unres)->type[i] != UNRES_RESOLVED) { |
| 4802 | unresolved++; |
| 4803 | } |
| 4804 | continue; |
| 4805 | } |
| 4806 | if ((*unres)->type[i] == UNRES_TYPE_DER) { |
Pavol Vican | d01d8ae | 2016-03-01 10:45:59 +0100 | [diff] [blame] | 4807 | yin = (struct lyxml_elem *)((struct lys_type *)(*unres)->item[i])->der; |
| 4808 | if (yin->flags & LY_YANG_STRUCTURE_FLAG) { |
| 4809 | yang =(struct yang_type *)yin; |
Pavol Vican | 6b07251 | 2016-04-04 10:50:21 +0200 | [diff] [blame] | 4810 | yang->type->base = yang->base; |
Pavol Vican | 5f0316a | 2016-04-05 21:21:11 +0200 | [diff] [blame] | 4811 | lydict_remove(module->ctx, yang->name); |
Pavol Vican | d01d8ae | 2016-03-01 10:45:59 +0100 | [diff] [blame] | 4812 | free(yang); |
| 4813 | } else { |
| 4814 | lyxml_free(module->ctx, yin); |
| 4815 | } |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 4816 | } |
| 4817 | (*unres)->type[i] = UNRES_RESOLVED; |
| 4818 | } |
| 4819 | |
| 4820 | if (!module || (!unresolved && !module->type)) { |
| 4821 | free((*unres)->item); |
| 4822 | free((*unres)->type); |
| 4823 | free((*unres)->str_snode); |
| 4824 | free((*unres)->module); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 4825 | free((*unres)); |
| 4826 | (*unres) = NULL; |
| 4827 | } |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 4828 | } |
| 4829 | |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 4830 | /** |
| 4831 | * @brief Resolve a single unres data item. Logs directly. |
| 4832 | * |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4833 | * @param[in] node Data node to resolve. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4834 | * @param[in] type Type of the unresolved item. |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 4835 | * |
| 4836 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 4837 | */ |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 4838 | int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4839 | resolve_unres_data_item(struct lyd_node *node, enum UNRES_ITEM type) |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 4840 | { |
| 4841 | uint32_t i; |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 4842 | int rc; |
Michal Vasko | 83a6c46 | 2015-10-08 16:43:53 +0200 | [diff] [blame] | 4843 | struct lyd_node_leaf_list *leaf; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 4844 | struct lys_node_leaf *sleaf; |
| 4845 | struct unres_data matches; |
| 4846 | |
| 4847 | memset(&matches, 0, sizeof matches); |
Michal Vasko | 83a6c46 | 2015-10-08 16:43:53 +0200 | [diff] [blame] | 4848 | leaf = (struct lyd_node_leaf_list *)node; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4849 | sleaf = (struct lys_node_leaf *)leaf->schema; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 4850 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4851 | switch (type) { |
| 4852 | case UNRES_LEAFREF: |
| 4853 | assert(sleaf->type.base == LY_TYPE_LEAFREF); |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4854 | if ((rc = resolve_path_arg_data(node, sleaf->type.info.lref.path, &matches))) { |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 4855 | return rc; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 4856 | } |
| 4857 | |
| 4858 | /* check that value matches */ |
| 4859 | for (i = 0; i < matches.count; ++i) { |
Radek Krejci | 749190d | 2016-02-18 16:26:25 +0100 | [diff] [blame] | 4860 | 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] | 4861 | leaf->value.leafref = matches.node[i]; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 4862 | break; |
| 4863 | } |
| 4864 | } |
| 4865 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4866 | free(matches.node); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 4867 | memset(&matches, 0, sizeof matches); |
| 4868 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4869 | if (!leaf->value.leafref) { |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 4870 | /* reference not found */ |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4871 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYD, leaf, sleaf->type.info.lref.path); |
| 4872 | LOGVAL(LYE_SPEC, LY_VLOG_LYD, leaf, "Leafref value \"%s\" did not match any node value.", leaf->value_str); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 4873 | return EXIT_FAILURE; |
| 4874 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4875 | break; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 4876 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4877 | case UNRES_INSTID: |
| 4878 | assert(sleaf->type.base == LY_TYPE_INST); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 4879 | ly_errno = 0; |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4880 | leaf->value.instance = resolve_instid(node, leaf->value_str); |
Radek Krejci | 40f17b9 | 2016-02-03 14:30:43 +0100 | [diff] [blame] | 4881 | if (!leaf->value.instance) { |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 4882 | if (ly_errno) { |
| 4883 | return -1; |
| 4884 | } else if (sleaf->type.info.inst.req > -1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4885 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYD, leaf, leaf->value_str); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 4886 | return EXIT_FAILURE; |
| 4887 | } else { |
Radek Krejci | 4ce42be | 2016-02-03 13:04:41 +0100 | [diff] [blame] | 4888 | 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] | 4889 | } |
| 4890 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4891 | break; |
| 4892 | |
| 4893 | case UNRES_WHEN: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4894 | if ((rc = resolve_when(node))) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4895 | return rc; |
| 4896 | } |
| 4897 | break; |
| 4898 | |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 4899 | case UNRES_MUST: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4900 | if ((rc = resolve_must(node))) { |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 4901 | return rc; |
| 4902 | } |
| 4903 | break; |
| 4904 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4905 | default: |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 4906 | LOGINT; |
| 4907 | return -1; |
| 4908 | } |
| 4909 | |
| 4910 | return EXIT_SUCCESS; |
| 4911 | } |
| 4912 | |
| 4913 | /** |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 4914 | * @brief add data unres item |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 4915 | * |
| 4916 | * @param[in] unres Unres data structure to use. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4917 | * @param[in] node Data node to use. |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 4918 | * |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 4919 | * @return 0 on success, -1 on error. |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 4920 | */ |
| 4921 | int |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 4922 | 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] | 4923 | { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 4924 | assert(unres && node); |
| 4925 | assert((type == UNRES_LEAFREF) || (type == UNRES_INSTID) || (type == UNRES_WHEN) || (type == UNRES_MUST)); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 4926 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 4927 | unres->count++; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 4928 | unres->node = ly_realloc(unres->node, unres->count * sizeof *unres->node); |
| 4929 | if (!unres->node) { |
| 4930 | LOGMEM; |
| 4931 | return -1; |
| 4932 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4933 | unres->node[unres->count - 1] = node; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 4934 | unres->type = ly_realloc(unres->type, unres->count * sizeof *unres->type); |
| 4935 | if (!unres->type) { |
| 4936 | LOGMEM; |
| 4937 | return -1; |
| 4938 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4939 | unres->type[unres->count - 1] = type; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 4940 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 4941 | if (type == UNRES_WHEN) { |
| 4942 | /* remove previous result */ |
| 4943 | node->when_status = LYD_WHEN; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4944 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4945 | |
| 4946 | return EXIT_SUCCESS; |
| 4947 | } |
| 4948 | |
| 4949 | /** |
| 4950 | * @brief Resolve every unres data item in the structure. Logs directly. |
| 4951 | * |
| 4952 | * @param[in] unres Unres data structure to use. |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 4953 | * @param[in,out] root Root node of the data tree. If not NULL, auto-delete is performed on false when condition. If |
| 4954 | * NULL and when condition is false the error is raised. |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 4955 | * @param[in] options Parer options |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4956 | * |
| 4957 | * @return EXIT_SUCCESS on success, -1 on error. |
| 4958 | */ |
| 4959 | int |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 4960 | 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] | 4961 | { |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 4962 | 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] | 4963 | int rc, progress; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 4964 | char *msg, *path; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 4965 | struct lyd_node *parent; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4966 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 4967 | assert(unres); |
Radek Krejci | 8ee1b56 | 2016-03-31 10:58:31 +0200 | [diff] [blame] | 4968 | assert((root && (*root)) || (options & LYD_OPT_NOAUTODEL)); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 4969 | |
| 4970 | if (!unres->count) { |
| 4971 | return EXIT_SUCCESS; |
| 4972 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4973 | |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 4974 | LOGVRB("Resolving unresolved data nodes and their constraints."); |
| 4975 | ly_vlog_hide(1); |
| 4976 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 4977 | /* when-stmt first */ |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 4978 | ly_errno = LY_SUCCESS; |
| 4979 | ly_vecode = LYVE_SUCCESS; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 4980 | do { |
| 4981 | progress = 0; |
| 4982 | for(i = 0; i < unres->count; i++) { |
| 4983 | if (unres->type[i] != UNRES_WHEN) { |
| 4984 | continue; |
| 4985 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 4986 | if (first) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 4987 | /* count when-stmt nodes in unres list */ |
| 4988 | when_stmt++; |
| 4989 | } |
| 4990 | |
| 4991 | /* resolve when condition only when all parent when conditions are already resolved */ |
| 4992 | for (parent = unres->node[i]->parent; |
| 4993 | parent && LYD_WHEN_DONE(parent->when_status); |
| 4994 | parent = parent->parent) { |
| 4995 | if (!parent->parent && (parent->when_status & LYD_WHEN_FALSE)) { |
| 4996 | /* the parent node was already unlinked, do not resolve this node, |
| 4997 | * it will be removed anyway, so just mark it as resolved |
| 4998 | */ |
| 4999 | unres->node[i]->when_status |= LYD_WHEN_FALSE; |
| 5000 | unres->type[i] = UNRES_RESOLVED; |
| 5001 | resolved++; |
| 5002 | break; |
| 5003 | } |
| 5004 | } |
| 5005 | if (parent) { |
| 5006 | continue; |
| 5007 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5008 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5009 | rc = resolve_unres_data_item(unres->node[i], unres->type[i]); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5010 | if (!rc) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5011 | if (unres->node[i]->when_status & LYD_WHEN_FALSE) { |
| 5012 | if (!root) { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5013 | /* false when condition */ |
| 5014 | ly_vlog_hide(0); |
| 5015 | path = strdup(ly_errpath()); |
| 5016 | LOGERR(LY_EVALID, "%s%s%s%s", msg = strdup(ly_errmsg()), path[0] ? " (path: " : "", |
| 5017 | path[0] ? path : "", path[0] ? ")" : ""); |
| 5018 | free(path); |
| 5019 | free(msg); |
| 5020 | return -1; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5021 | } /* follows else */ |
| 5022 | |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 5023 | /* only unlink now, the subtree can contain another nodes stored in the unres list */ |
| 5024 | /* if it has parent non-presence containers that would be empty, we should actually |
| 5025 | * remove the container |
| 5026 | */ |
| 5027 | if (!(options & LYD_OPT_KEEPEMPTYCONT)) { |
| 5028 | for (parent = unres->node[i]; |
| 5029 | parent->parent && parent->parent->schema->nodetype == LYS_CONTAINER; |
| 5030 | parent = parent->parent) { |
| 5031 | if (((struct lys_node_container *)parent->parent->schema)->presence) { |
| 5032 | /* presence container */ |
| 5033 | break; |
| 5034 | } |
| 5035 | if (parent->next || parent->prev != parent) { |
| 5036 | /* non empty (the child we are in and we are going to remove is not the only child) */ |
| 5037 | break; |
| 5038 | } |
| 5039 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 5040 | unres->node[i] = parent; |
| 5041 | } |
| 5042 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5043 | /* auto-delete */ |
| 5044 | LOGVRB("auto-delete node \"%s\" due to when condition (%s)", ly_errpath(), |
| 5045 | ((struct lys_node_leaf *)unres->node[i]->schema)->when->cond); |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 5046 | if (*root && *root == unres->node[i]) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5047 | *root = (*root)->next; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5048 | } |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5049 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5050 | lyd_unlink(unres->node[i]); |
| 5051 | unres->type[i] = UNRES_DELETE; |
| 5052 | del_items++; |
Radek Krejci | 51fd816 | 2016-03-24 15:49:51 +0100 | [diff] [blame] | 5053 | |
| 5054 | /* update the rest of unres items */ |
| 5055 | for (j = 0; j < unres->count; j++) { |
Radek Krejci | 3db819b | 2016-03-24 16:29:48 +0100 | [diff] [blame] | 5056 | if (unres->type[j] == UNRES_RESOLVED || unres->type[j] == UNRES_DELETE) { |
Radek Krejci | 51fd816 | 2016-03-24 15:49:51 +0100 | [diff] [blame] | 5057 | continue; |
| 5058 | } |
| 5059 | |
| 5060 | /* test if the node is in subtree to be deleted */ |
| 5061 | for (parent = unres->node[j]; parent; parent = parent->parent) { |
| 5062 | if (parent == unres->node[i]) { |
| 5063 | /* yes, it is */ |
| 5064 | unres->type[j] = UNRES_RESOLVED; |
| 5065 | resolved++; |
| 5066 | break; |
| 5067 | } |
| 5068 | } |
| 5069 | } |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5070 | } else { |
| 5071 | unres->type[i] = UNRES_RESOLVED; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5072 | } |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5073 | ly_errno = LY_SUCCESS; |
| 5074 | ly_vecode = LYVE_SUCCESS; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5075 | resolved++; |
| 5076 | progress = 1; |
| 5077 | } else if (rc == -1) { |
| 5078 | ly_vlog_hide(0); |
| 5079 | return -1; |
| 5080 | } |
| 5081 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 5082 | first = 0; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5083 | } while (progress && resolved < when_stmt); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5084 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5085 | /* do we have some unresolved when-stmt? */ |
Radek Krejci | d940d73 | 2016-03-24 16:02:28 +0100 | [diff] [blame] | 5086 | if (when_stmt > resolved) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5087 | ly_vlog_hide(0); |
| 5088 | path = strdup(ly_errpath()); |
| 5089 | LOGERR(LY_EVALID, "%s%s%s%s", msg = strdup(ly_errmsg()), path[0] ? " (path: " : "", |
| 5090 | path[0] ? path : "", path[0] ? ")" : ""); |
| 5091 | free(path); |
| 5092 | free(msg); |
| 5093 | return -1; |
| 5094 | } |
| 5095 | |
| 5096 | for (i = 0; del_items && i < unres->count; i++) { |
| 5097 | /* we had some when-stmt resulted to false, so now we have to sanitize the unres list */ |
| 5098 | if (unres->type[i] != UNRES_DELETE) { |
| 5099 | continue; |
| 5100 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 5101 | if (!unres->node[i]) { |
| 5102 | unres->type[i] = UNRES_RESOLVED; |
| 5103 | del_items--; |
| 5104 | continue; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5105 | } |
| 5106 | |
| 5107 | /* really remove the complete subtree */ |
| 5108 | lyd_free(unres->node[i]); |
| 5109 | unres->type[i] = UNRES_RESOLVED; |
| 5110 | del_items--; |
| 5111 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5112 | |
| 5113 | /* rest */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5114 | for (i = 0; i < unres->count; ++i) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5115 | if (unres->type[i] == UNRES_RESOLVED) { |
| 5116 | continue; |
| 5117 | } |
| 5118 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5119 | rc = resolve_unres_data_item(unres->node[i], unres->type[i]); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5120 | if (rc == 0) { |
| 5121 | unres->type[i] = UNRES_RESOLVED; |
| 5122 | resolved++; |
| 5123 | } else if (rc == -1) { |
| 5124 | ly_vlog_hide(0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5125 | return -1; |
| 5126 | } |
| 5127 | } |
| 5128 | |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5129 | ly_vlog_hide(0); |
| 5130 | if (resolved < unres->count) { |
| 5131 | /* try to resolve the unresolved data again, it will not resolve anything, but it will print |
| 5132 | * all the validation errors |
| 5133 | */ |
| 5134 | for (i = 0; i < unres->count; ++i) { |
| 5135 | if (unres->type[i] == UNRES_RESOLVED) { |
| 5136 | continue; |
| 5137 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5138 | resolve_unres_data_item(unres->node[i], unres->type[i]); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5139 | } |
| 5140 | return -1; |
| 5141 | } |
| 5142 | |
| 5143 | LOGVRB("All data nodes and constraints resolved"); |
| 5144 | unres->count = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5145 | return EXIT_SUCCESS; |
| 5146 | } |