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 | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 33 | int |
| 34 | parse_range_dec64(const char **str_num, uint8_t dig, int64_t *num) |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 35 | { |
| 36 | const char *ptr; |
| 37 | int minus = 0; |
| 38 | int64_t ret = 0; |
| 39 | int8_t str_exp, str_dig = -1; |
| 40 | |
| 41 | ptr = *str_num; |
| 42 | |
| 43 | if (ptr[0] == '-') { |
| 44 | minus = 1; |
| 45 | ++ptr; |
| 46 | } |
| 47 | |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 48 | if (!isdigit(ptr[0])) { |
| 49 | /* there must be at least one */ |
| 50 | return 1; |
| 51 | } |
| 52 | |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 53 | for (str_exp = 0; isdigit(ptr[0]) || ((ptr[0] == '.') && (str_dig < 0)); ++ptr) { |
| 54 | if (str_exp > 18) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 55 | return 1; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | if (ptr[0] == '.') { |
| 59 | if (ptr[1] == '.') { |
| 60 | /* it's the next interval */ |
| 61 | break; |
| 62 | } |
| 63 | ++str_dig; |
| 64 | } else { |
| 65 | ret = ret * 10 + (ptr[0] - 48); |
| 66 | if (str_dig > -1) { |
| 67 | ++str_dig; |
| 68 | } |
| 69 | ++str_exp; |
| 70 | } |
| 71 | } |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 72 | if (str_dig == 0) { |
| 73 | /* no digits after '.' */ |
| 74 | return 1; |
| 75 | } else if (str_dig == -1) { |
| 76 | /* there are 0 numbers after the floating point */ |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 77 | str_dig = 0; |
| 78 | } |
| 79 | |
| 80 | /* it's parsed, now adjust the number based on fraction-digits, if needed */ |
| 81 | if (str_dig < dig) { |
| 82 | if ((str_exp - 1) + (dig - str_dig) > 18) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 83 | return 1; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 84 | } |
| 85 | ret *= dec_pow(dig - str_dig); |
| 86 | } |
| 87 | if (str_dig > dig) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 88 | return 1; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | if (minus) { |
| 92 | ret *= -1; |
| 93 | } |
| 94 | *str_num = ptr; |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 95 | *num = ret; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 96 | |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 97 | return 0; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | /** |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 101 | * @brief Parse an identifier. |
| 102 | * |
| 103 | * ;; An identifier MUST NOT start with (('X'|'x') ('M'|'m') ('L'|'l')) |
| 104 | * identifier = (ALPHA / "_") |
| 105 | * *(ALPHA / DIGIT / "_" / "-" / ".") |
| 106 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 107 | * @param[in] id Identifier to use. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 108 | * |
| 109 | * @return Number of characters successfully parsed. |
| 110 | */ |
Michal Vasko | 249e6b5 | 2015-08-19 11:08:52 +0200 | [diff] [blame] | 111 | int |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 112 | parse_identifier(const char *id) |
| 113 | { |
| 114 | int parsed = 0; |
| 115 | |
Michal Vasko | 1ab90bc | 2016-03-15 10:40:22 +0100 | [diff] [blame] | 116 | assert(id); |
| 117 | |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 118 | if (!isalpha(id[0]) && (id[0] != '_')) { |
| 119 | return -parsed; |
| 120 | } |
| 121 | |
| 122 | ++parsed; |
| 123 | ++id; |
| 124 | |
| 125 | while (isalnum(id[0]) || (id[0] == '_') || (id[0] == '-') || (id[0] == '.')) { |
| 126 | ++parsed; |
| 127 | ++id; |
| 128 | } |
| 129 | |
| 130 | return parsed; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * @brief Parse a node-identifier. |
| 135 | * |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 136 | * node-identifier = [module-name ":"] identifier |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 137 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 138 | * @param[in] id Identifier to use. |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 139 | * @param[out] mod_name Points to the module name, NULL if there is not any. |
| 140 | * @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] | 141 | * @param[out] name Points to the node name. |
| 142 | * @param[out] nam_len Length of the node name. |
| 143 | * |
| 144 | * @return Number of characters successfully parsed, |
| 145 | * positive on success, negative on failure. |
| 146 | */ |
| 147 | static int |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 148 | 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] | 149 | { |
| 150 | int parsed = 0, ret; |
| 151 | |
| 152 | assert(id); |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 153 | if (mod_name) { |
| 154 | *mod_name = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 155 | } |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 156 | if (mod_name_len) { |
| 157 | *mod_name_len = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 158 | } |
| 159 | if (name) { |
| 160 | *name = NULL; |
| 161 | } |
| 162 | if (nam_len) { |
| 163 | *nam_len = 0; |
| 164 | } |
| 165 | |
| 166 | if ((ret = parse_identifier(id)) < 1) { |
| 167 | return ret; |
| 168 | } |
| 169 | |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 170 | if (mod_name) { |
| 171 | *mod_name = id; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 172 | } |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 173 | if (mod_name_len) { |
| 174 | *mod_name_len = ret; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | parsed += ret; |
| 178 | id += ret; |
| 179 | |
| 180 | /* there is prefix */ |
| 181 | if (id[0] == ':') { |
| 182 | ++parsed; |
| 183 | ++id; |
| 184 | |
| 185 | /* there isn't */ |
| 186 | } else { |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 187 | if (name && mod_name) { |
| 188 | *name = *mod_name; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 189 | } |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 190 | if (mod_name) { |
| 191 | *mod_name = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 192 | } |
| 193 | |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 194 | if (nam_len && mod_name_len) { |
| 195 | *nam_len = *mod_name_len; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 196 | } |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 197 | if (mod_name_len) { |
| 198 | *mod_name_len = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | return parsed; |
| 202 | } |
| 203 | |
| 204 | /* identifier (node name) */ |
| 205 | if ((ret = parse_identifier(id)) < 1) { |
| 206 | return -parsed+ret; |
| 207 | } |
| 208 | |
| 209 | if (name) { |
| 210 | *name = id; |
| 211 | } |
| 212 | if (nam_len) { |
| 213 | *nam_len = ret; |
| 214 | } |
| 215 | |
| 216 | return parsed+ret; |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * @brief Parse a path-predicate (leafref). |
| 221 | * |
| 222 | * path-predicate = "[" *WSP path-equality-expr *WSP "]" |
| 223 | * path-equality-expr = node-identifier *WSP "=" *WSP path-key-expr |
| 224 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 225 | * @param[in] id Identifier to use. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 226 | * @param[out] prefix Points to the prefix, NULL if there is not any. |
| 227 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 228 | * @param[out] name Points to the node name. |
| 229 | * @param[out] nam_len Length of the node name. |
| 230 | * @param[out] path_key_expr Points to the path-key-expr. |
| 231 | * @param[out] pke_len Length of the path-key-expr. |
| 232 | * @param[out] has_predicate Flag to mark whether there is another predicate following. |
| 233 | * |
| 234 | * @return Number of characters successfully parsed, |
| 235 | * positive on success, negative on failure. |
| 236 | */ |
| 237 | static int |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 238 | parse_path_predicate(const char *id, const char **prefix, int *pref_len, const char **name, int *nam_len, |
| 239 | const char **path_key_expr, int *pke_len, int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 240 | { |
| 241 | const char *ptr; |
| 242 | int parsed = 0, ret; |
| 243 | |
| 244 | assert(id); |
| 245 | if (prefix) { |
| 246 | *prefix = NULL; |
| 247 | } |
| 248 | if (pref_len) { |
| 249 | *pref_len = 0; |
| 250 | } |
| 251 | if (name) { |
| 252 | *name = NULL; |
| 253 | } |
| 254 | if (nam_len) { |
| 255 | *nam_len = 0; |
| 256 | } |
| 257 | if (path_key_expr) { |
| 258 | *path_key_expr = NULL; |
| 259 | } |
| 260 | if (pke_len) { |
| 261 | *pke_len = 0; |
| 262 | } |
| 263 | if (has_predicate) { |
| 264 | *has_predicate = 0; |
| 265 | } |
| 266 | |
| 267 | if (id[0] != '[') { |
| 268 | return -parsed; |
| 269 | } |
| 270 | |
| 271 | ++parsed; |
| 272 | ++id; |
| 273 | |
| 274 | while (isspace(id[0])) { |
| 275 | ++parsed; |
| 276 | ++id; |
| 277 | } |
| 278 | |
| 279 | if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len)) < 1) { |
| 280 | return -parsed+ret; |
| 281 | } |
| 282 | |
| 283 | parsed += ret; |
| 284 | id += ret; |
| 285 | |
| 286 | while (isspace(id[0])) { |
| 287 | ++parsed; |
| 288 | ++id; |
| 289 | } |
| 290 | |
| 291 | if (id[0] != '=') { |
| 292 | return -parsed; |
| 293 | } |
| 294 | |
| 295 | ++parsed; |
| 296 | ++id; |
| 297 | |
| 298 | while (isspace(id[0])) { |
| 299 | ++parsed; |
| 300 | ++id; |
| 301 | } |
| 302 | |
| 303 | if ((ptr = strchr(id, ']')) == NULL) { |
| 304 | return -parsed; |
| 305 | } |
| 306 | |
| 307 | --ptr; |
| 308 | while (isspace(ptr[0])) { |
| 309 | --ptr; |
| 310 | } |
| 311 | ++ptr; |
| 312 | |
| 313 | ret = ptr-id; |
| 314 | if (path_key_expr) { |
| 315 | *path_key_expr = id; |
| 316 | } |
| 317 | if (pke_len) { |
| 318 | *pke_len = ret; |
| 319 | } |
| 320 | |
| 321 | parsed += ret; |
| 322 | id += ret; |
| 323 | |
| 324 | while (isspace(id[0])) { |
| 325 | ++parsed; |
| 326 | ++id; |
| 327 | } |
| 328 | |
| 329 | assert(id[0] == ']'); |
| 330 | |
| 331 | if (id[1] == '[') { |
| 332 | *has_predicate = 1; |
| 333 | } |
| 334 | |
| 335 | return parsed+1; |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * @brief Parse a path-key-expr (leafref). First call parses "current()", all |
| 340 | * the ".." and the first node-identifier, other calls parse a single |
| 341 | * node-identifier each. |
| 342 | * |
| 343 | * path-key-expr = current-function-invocation *WSP "/" *WSP |
| 344 | * rel-path-keyexpr |
| 345 | * rel-path-keyexpr = 1*(".." *WSP "/" *WSP) |
| 346 | * *(node-identifier *WSP "/" *WSP) |
| 347 | * node-identifier |
| 348 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 349 | * @param[in] id Identifier to use. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 350 | * @param[out] prefix Points to the prefix, NULL if there is not any. |
| 351 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 352 | * @param[out] name Points to the node name. |
| 353 | * @param[out] nam_len Length of the node name. |
| 354 | * @param[out] parent_times Number of ".." in the path. Must be 0 on the first call, |
| 355 | * must not be changed between consecutive calls. |
| 356 | * @return Number of characters successfully parsed, |
| 357 | * positive on success, negative on failure. |
| 358 | */ |
| 359 | static int |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 360 | parse_path_key_expr(const char *id, const char **prefix, int *pref_len, const char **name, int *nam_len, |
| 361 | int *parent_times) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 362 | { |
| 363 | int parsed = 0, ret, par_times = 0; |
| 364 | |
| 365 | assert(id); |
| 366 | assert(parent_times); |
| 367 | if (prefix) { |
| 368 | *prefix = NULL; |
| 369 | } |
| 370 | if (pref_len) { |
| 371 | *pref_len = 0; |
| 372 | } |
| 373 | if (name) { |
| 374 | *name = NULL; |
| 375 | } |
| 376 | if (nam_len) { |
| 377 | *nam_len = 0; |
| 378 | } |
| 379 | |
| 380 | if (!*parent_times) { |
| 381 | /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */ |
| 382 | if (strncmp(id, "current()", 9)) { |
| 383 | return -parsed; |
| 384 | } |
| 385 | |
| 386 | parsed += 9; |
| 387 | id += 9; |
| 388 | |
| 389 | while (isspace(id[0])) { |
| 390 | ++parsed; |
| 391 | ++id; |
| 392 | } |
| 393 | |
| 394 | if (id[0] != '/') { |
| 395 | return -parsed; |
| 396 | } |
| 397 | |
| 398 | ++parsed; |
| 399 | ++id; |
| 400 | |
| 401 | while (isspace(id[0])) { |
| 402 | ++parsed; |
| 403 | ++id; |
| 404 | } |
| 405 | |
| 406 | /* rel-path-keyexpr */ |
| 407 | if (strncmp(id, "..", 2)) { |
| 408 | return -parsed; |
| 409 | } |
| 410 | ++par_times; |
| 411 | |
| 412 | parsed += 2; |
| 413 | id += 2; |
| 414 | |
| 415 | while (isspace(id[0])) { |
| 416 | ++parsed; |
| 417 | ++id; |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | /* 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier |
| 422 | * |
| 423 | * first parent reference with whitespaces already parsed |
| 424 | */ |
| 425 | if (id[0] != '/') { |
| 426 | return -parsed; |
| 427 | } |
| 428 | |
| 429 | ++parsed; |
| 430 | ++id; |
| 431 | |
| 432 | while (isspace(id[0])) { |
| 433 | ++parsed; |
| 434 | ++id; |
| 435 | } |
| 436 | |
| 437 | while (!strncmp(id, "..", 2) && !*parent_times) { |
| 438 | ++par_times; |
| 439 | |
| 440 | parsed += 2; |
| 441 | id += 2; |
| 442 | |
| 443 | while (isspace(id[0])) { |
| 444 | ++parsed; |
| 445 | ++id; |
| 446 | } |
| 447 | |
| 448 | if (id[0] != '/') { |
| 449 | return -parsed; |
| 450 | } |
| 451 | |
| 452 | ++parsed; |
| 453 | ++id; |
| 454 | |
| 455 | while (isspace(id[0])) { |
| 456 | ++parsed; |
| 457 | ++id; |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | if (!*parent_times) { |
| 462 | *parent_times = par_times; |
| 463 | } |
| 464 | |
| 465 | /* all parent references must be parsed at this point */ |
| 466 | if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len)) < 1) { |
| 467 | return -parsed+ret; |
| 468 | } |
| 469 | |
| 470 | parsed += ret; |
| 471 | id += ret; |
| 472 | |
| 473 | return parsed; |
| 474 | } |
| 475 | |
| 476 | /** |
| 477 | * @brief Parse path-arg (leafref). |
| 478 | * |
| 479 | * path-arg = absolute-path / relative-path |
| 480 | * absolute-path = 1*("/" (node-identifier *path-predicate)) |
| 481 | * relative-path = 1*(".." "/") descendant-path |
| 482 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 483 | * @param[in] id Identifier to use. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 484 | * @param[out] prefix Points to the prefix, NULL if there is not any. |
| 485 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 486 | * @param[out] name Points to the node name. |
| 487 | * @param[out] nam_len Length of the node name. |
| 488 | * @param[out] parent_times Number of ".." in the path. Must be 0 on the first call, |
| 489 | * must not be changed between consecutive calls. -1 if the |
| 490 | * path is relative. |
| 491 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 492 | * |
| 493 | * @return Number of characters successfully parsed, |
| 494 | * positive on success, negative on failure. |
| 495 | */ |
| 496 | static int |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 497 | parse_path_arg(const char *id, const char **prefix, int *pref_len, const char **name, int *nam_len, int *parent_times, |
| 498 | int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 499 | { |
| 500 | int parsed = 0, ret, par_times = 0; |
| 501 | |
| 502 | assert(id); |
| 503 | assert(parent_times); |
| 504 | if (prefix) { |
| 505 | *prefix = NULL; |
| 506 | } |
| 507 | if (pref_len) { |
| 508 | *pref_len = 0; |
| 509 | } |
| 510 | if (name) { |
| 511 | *name = NULL; |
| 512 | } |
| 513 | if (nam_len) { |
| 514 | *nam_len = 0; |
| 515 | } |
| 516 | if (has_predicate) { |
| 517 | *has_predicate = 0; |
| 518 | } |
| 519 | |
| 520 | if (!*parent_times && !strncmp(id, "..", 2)) { |
| 521 | ++par_times; |
| 522 | |
| 523 | parsed += 2; |
| 524 | id += 2; |
| 525 | |
| 526 | while (!strncmp(id, "/..", 3)) { |
| 527 | ++par_times; |
| 528 | |
| 529 | parsed += 3; |
| 530 | id += 3; |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | if (!*parent_times) { |
| 535 | if (par_times) { |
| 536 | *parent_times = par_times; |
| 537 | } else { |
| 538 | *parent_times = -1; |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | if (id[0] != '/') { |
| 543 | return -parsed; |
| 544 | } |
| 545 | |
| 546 | /* skip '/' */ |
| 547 | ++parsed; |
| 548 | ++id; |
| 549 | |
| 550 | /* node-identifier ([prefix:]identifier) */ |
| 551 | if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len)) < 1) { |
| 552 | return -parsed-ret; |
| 553 | } |
| 554 | |
| 555 | parsed += ret; |
| 556 | id += ret; |
| 557 | |
| 558 | /* there is no predicate */ |
| 559 | if ((id[0] == '/') || !id[0]) { |
| 560 | return parsed; |
| 561 | } else if (id[0] != '[') { |
| 562 | return -parsed; |
| 563 | } |
| 564 | |
| 565 | if (has_predicate) { |
| 566 | *has_predicate = 1; |
| 567 | } |
| 568 | |
| 569 | return parsed; |
| 570 | } |
| 571 | |
| 572 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 573 | * @brief Parse instance-identifier in JSON data format. That means that prefixes |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 574 | * (which are mandatory for every node-identifier) are actually model names. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 575 | * |
| 576 | * instance-identifier = 1*("/" (node-identifier *predicate)) |
| 577 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 578 | * @param[in] id Identifier to use. |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 579 | * @param[out] model Points to the model name. |
| 580 | * @param[out] mod_len Length of the model name. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 581 | * @param[out] name Points to the node name. |
| 582 | * @param[out] nam_len Length of the node name. |
| 583 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 584 | * |
| 585 | * @return Number of characters successfully parsed, |
| 586 | * positive on success, negative on failure. |
| 587 | */ |
| 588 | static int |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 589 | parse_instance_identifier(const char *id, const char **model, int *mod_len, const char **name, int *nam_len, |
| 590 | int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 591 | { |
| 592 | int parsed = 0, ret; |
| 593 | |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 594 | if (has_predicate) { |
| 595 | *has_predicate = 0; |
| 596 | } |
| 597 | |
| 598 | if (id[0] != '/') { |
| 599 | return -parsed; |
| 600 | } |
| 601 | |
| 602 | ++parsed; |
| 603 | ++id; |
| 604 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 605 | if ((ret = parse_identifier(id)) < 1) { |
| 606 | return ret; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 607 | } |
| 608 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 609 | *model = id; |
| 610 | *mod_len = ret; |
| 611 | |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 612 | parsed += ret; |
| 613 | id += ret; |
| 614 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 615 | if (id[0] != ':') { |
| 616 | return -parsed; |
| 617 | } |
| 618 | |
| 619 | ++parsed; |
| 620 | ++id; |
| 621 | |
| 622 | if ((ret = parse_identifier(id)) < 1) { |
| 623 | return ret; |
| 624 | } |
| 625 | |
| 626 | *name = id; |
| 627 | *nam_len = ret; |
| 628 | |
| 629 | parsed += ret; |
| 630 | id += ret; |
| 631 | |
Radek Krejci | 4967cb6 | 2016-09-14 16:40:28 +0200 | [diff] [blame] | 632 | if (id[0] == '[' && has_predicate) { |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 633 | *has_predicate = 1; |
| 634 | } |
| 635 | |
| 636 | return parsed; |
| 637 | } |
| 638 | |
| 639 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 640 | * @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] | 641 | * (which are mandatory) are actually model names. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 642 | * |
| 643 | * predicate = "[" *WSP (predicate-expr / pos) *WSP "]" |
| 644 | * predicate-expr = (node-identifier / ".") *WSP "=" *WSP |
| 645 | * ((DQUOTE string DQUOTE) / |
| 646 | * (SQUOTE string SQUOTE)) |
| 647 | * pos = non-negative-integer-value |
| 648 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 649 | * @param[in] id Identifier to use. |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 650 | * @param[out] model Points to the model name. |
| 651 | * @param[out] mod_len Length of the model name. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 652 | * @param[out] name Points to the node name. Can be identifier (from node-identifier), "." or pos. |
| 653 | * @param[out] nam_len Length of the node name. |
| 654 | * @param[out] value Value the node-identifier must have (string from the grammar), |
| 655 | * NULL if there is not any. |
| 656 | * @param[out] val_len Length of the value, 0 if there is not any. |
| 657 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 658 | * |
| 659 | * @return Number of characters successfully parsed, |
| 660 | * positive on success, negative on failure. |
| 661 | */ |
| 662 | static int |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 663 | parse_predicate(const char *id, const char **model, int *mod_len, const char **name, int *nam_len, |
| 664 | const char **value, int *val_len, int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 665 | { |
| 666 | const char *ptr; |
| 667 | int parsed = 0, ret; |
| 668 | char quote; |
| 669 | |
| 670 | assert(id); |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 671 | if (model) { |
| 672 | *model = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 673 | } |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 674 | if (mod_len) { |
| 675 | *mod_len = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 676 | } |
| 677 | if (name) { |
| 678 | *name = NULL; |
| 679 | } |
| 680 | if (nam_len) { |
| 681 | *nam_len = 0; |
| 682 | } |
| 683 | if (value) { |
| 684 | *value = NULL; |
| 685 | } |
| 686 | if (val_len) { |
| 687 | *val_len = 0; |
| 688 | } |
| 689 | if (has_predicate) { |
| 690 | *has_predicate = 0; |
| 691 | } |
| 692 | |
| 693 | if (id[0] != '[') { |
| 694 | return -parsed; |
| 695 | } |
| 696 | |
| 697 | ++parsed; |
| 698 | ++id; |
| 699 | |
| 700 | while (isspace(id[0])) { |
| 701 | ++parsed; |
| 702 | ++id; |
| 703 | } |
| 704 | |
| 705 | /* pos */ |
| 706 | if (isdigit(id[0])) { |
| 707 | if (name) { |
| 708 | *name = id; |
| 709 | } |
| 710 | |
| 711 | if (id[0] == '0') { |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 712 | return -parsed; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | while (isdigit(id[0])) { |
| 716 | ++parsed; |
| 717 | ++id; |
| 718 | } |
| 719 | |
| 720 | if (nam_len) { |
| 721 | *nam_len = id-(*name); |
| 722 | } |
| 723 | |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 724 | /* "." or node-identifier */ |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 725 | } else { |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 726 | if (id[0] == '.') { |
| 727 | if (name) { |
| 728 | *name = id; |
| 729 | } |
| 730 | if (nam_len) { |
| 731 | *nam_len = 1; |
| 732 | } |
| 733 | |
| 734 | ++parsed; |
| 735 | ++id; |
| 736 | |
| 737 | } else { |
| 738 | if ((ret = parse_node_identifier(id, model, mod_len, name, nam_len)) < 1) { |
| 739 | return -parsed+ret; |
| 740 | } else if (model && !*model) { |
| 741 | return -parsed; |
| 742 | } |
| 743 | |
| 744 | parsed += ret; |
| 745 | id += ret; |
| 746 | } |
| 747 | |
| 748 | while (isspace(id[0])) { |
| 749 | ++parsed; |
| 750 | ++id; |
| 751 | } |
| 752 | |
| 753 | if (id[0] != '=') { |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 754 | return -parsed; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 755 | } |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 756 | |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 757 | ++parsed; |
| 758 | ++id; |
| 759 | |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 760 | while (isspace(id[0])) { |
| 761 | ++parsed; |
| 762 | ++id; |
| 763 | } |
| 764 | |
| 765 | /* ((DQUOTE string DQUOTE) / (SQUOTE string SQUOTE)) */ |
| 766 | if ((id[0] == '\"') || (id[0] == '\'')) { |
| 767 | quote = id[0]; |
| 768 | |
| 769 | ++parsed; |
| 770 | ++id; |
| 771 | |
| 772 | if ((ptr = strchr(id, quote)) == NULL) { |
| 773 | return -parsed; |
| 774 | } |
| 775 | ret = ptr-id; |
| 776 | |
| 777 | if (value) { |
| 778 | *value = id; |
| 779 | } |
| 780 | if (val_len) { |
| 781 | *val_len = ret; |
| 782 | } |
| 783 | |
| 784 | parsed += ret+1; |
| 785 | id += ret+1; |
| 786 | } else { |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 787 | return -parsed; |
| 788 | } |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 789 | } |
| 790 | |
| 791 | while (isspace(id[0])) { |
| 792 | ++parsed; |
| 793 | ++id; |
| 794 | } |
| 795 | |
| 796 | if (id[0] != ']') { |
| 797 | return -parsed; |
| 798 | } |
| 799 | |
| 800 | ++parsed; |
| 801 | ++id; |
| 802 | |
| 803 | if ((id[0] == '[') && has_predicate) { |
| 804 | *has_predicate = 1; |
| 805 | } |
| 806 | |
| 807 | return parsed; |
| 808 | } |
| 809 | |
| 810 | /** |
| 811 | * @brief Parse schema-nodeid. |
| 812 | * |
| 813 | * schema-nodeid = absolute-schema-nodeid / |
| 814 | * descendant-schema-nodeid |
| 815 | * absolute-schema-nodeid = 1*("/" node-identifier) |
Michal Vasko | 4893535 | 2016-03-29 11:52:36 +0200 | [diff] [blame] | 816 | * descendant-schema-nodeid = ["." "/"] |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 817 | * node-identifier |
| 818 | * absolute-schema-nodeid |
| 819 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 820 | * @param[in] id Identifier to use. |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 821 | * @param[out] mod_name Points to the module name, NULL if there is not any. |
| 822 | * @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] | 823 | * @param[out] name Points to the node name. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 824 | * @param[out] nam_len Length of the node name. |
| 825 | * @param[out] is_relative Flag to mark whether the nodeid is absolute or descendant. Must be -1 |
| 826 | * on the first call, must not be changed between consecutive calls. |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 827 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. It cannot be |
| 828 | * based on the grammar, in those cases use NULL. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 829 | * |
| 830 | * @return Number of characters successfully parsed, |
| 831 | * positive on success, negative on failure. |
| 832 | */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 833 | int |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 834 | 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] | 835 | int *is_relative, int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 836 | { |
| 837 | int parsed = 0, ret; |
| 838 | |
| 839 | assert(id); |
| 840 | assert(is_relative); |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 841 | if (mod_name) { |
| 842 | *mod_name = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 843 | } |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 844 | if (mod_name_len) { |
| 845 | *mod_name_len = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 846 | } |
| 847 | if (name) { |
| 848 | *name = NULL; |
| 849 | } |
| 850 | if (nam_len) { |
| 851 | *nam_len = 0; |
| 852 | } |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 853 | if (has_predicate) { |
| 854 | *has_predicate = 0; |
| 855 | } |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 856 | |
| 857 | if (id[0] != '/') { |
| 858 | if (*is_relative != -1) { |
| 859 | return -parsed; |
| 860 | } else { |
| 861 | *is_relative = 1; |
| 862 | } |
Michal Vasko | 4893535 | 2016-03-29 11:52:36 +0200 | [diff] [blame] | 863 | if (!strncmp(id, "./", 2)) { |
| 864 | parsed += 2; |
| 865 | id += 2; |
| 866 | } |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 867 | } else { |
| 868 | if (*is_relative == -1) { |
| 869 | *is_relative = 0; |
| 870 | } |
| 871 | ++parsed; |
| 872 | ++id; |
| 873 | } |
| 874 | |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 875 | 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] | 876 | return -parsed+ret; |
| 877 | } |
| 878 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 879 | parsed += ret; |
| 880 | id += ret; |
| 881 | |
| 882 | if ((id[0] == '[') && has_predicate) { |
| 883 | *has_predicate = 1; |
| 884 | } |
| 885 | |
| 886 | return parsed; |
| 887 | } |
| 888 | |
| 889 | /** |
| 890 | * @brief Parse schema predicate (special format internally used). |
| 891 | * |
| 892 | * predicate = "[" *WSP predicate-expr *WSP "]" |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 893 | * predicate-expr = "." / identifier / key-with-value |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 894 | * key-with-value = identifier *WSP "=" *WSP |
| 895 | * ((DQUOTE string DQUOTE) / |
| 896 | * (SQUOTE string SQUOTE)) |
| 897 | * |
| 898 | * @param[in] id Identifier to use. |
| 899 | * @param[out] name Points to the list key name. |
| 900 | * @param[out] nam_len Length of \p name. |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 901 | * @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] | 902 | * @param[out] val_len Length of \p value. |
| 903 | * @param[out] has_predicate Flag to mark whether there is another predicate specified. |
| 904 | */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 905 | int |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 906 | parse_schema_json_predicate(const char *id, const char **name, int *nam_len, const char **value, int *val_len, |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 907 | int *has_predicate) |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 908 | { |
| 909 | const char *ptr; |
| 910 | int parsed = 0, ret; |
| 911 | char quote; |
| 912 | |
| 913 | assert(id); |
| 914 | if (name) { |
| 915 | *name = NULL; |
| 916 | } |
| 917 | if (nam_len) { |
| 918 | *nam_len = 0; |
| 919 | } |
| 920 | if (value) { |
| 921 | *value = NULL; |
| 922 | } |
| 923 | if (val_len) { |
| 924 | *val_len = 0; |
| 925 | } |
| 926 | if (has_predicate) { |
| 927 | *has_predicate = 0; |
| 928 | } |
| 929 | |
| 930 | if (id[0] != '[') { |
| 931 | return -parsed; |
| 932 | } |
| 933 | |
| 934 | ++parsed; |
| 935 | ++id; |
| 936 | |
| 937 | while (isspace(id[0])) { |
| 938 | ++parsed; |
| 939 | ++id; |
| 940 | } |
| 941 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 942 | /* identifier */ |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 943 | if (id[0] == '.') { |
| 944 | ret = 1; |
| 945 | } else if ((ret = parse_identifier(id)) < 1) { |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 946 | return -parsed + ret; |
| 947 | } |
| 948 | if (name) { |
| 949 | *name = id; |
| 950 | } |
| 951 | if (nam_len) { |
| 952 | *nam_len = ret; |
| 953 | } |
| 954 | |
| 955 | parsed += ret; |
| 956 | id += ret; |
| 957 | |
| 958 | while (isspace(id[0])) { |
| 959 | ++parsed; |
| 960 | ++id; |
| 961 | } |
| 962 | |
| 963 | /* there is value as well */ |
| 964 | if (id[0] == '=') { |
| 965 | ++parsed; |
| 966 | ++id; |
| 967 | |
| 968 | while (isspace(id[0])) { |
| 969 | ++parsed; |
| 970 | ++id; |
| 971 | } |
| 972 | |
| 973 | /* ((DQUOTE string DQUOTE) / (SQUOTE string SQUOTE)) */ |
| 974 | if ((id[0] == '\"') || (id[0] == '\'')) { |
| 975 | quote = id[0]; |
| 976 | |
| 977 | ++parsed; |
| 978 | ++id; |
| 979 | |
| 980 | if ((ptr = strchr(id, quote)) == NULL) { |
| 981 | return -parsed; |
| 982 | } |
| 983 | ret = ptr - id; |
| 984 | |
| 985 | if (value) { |
| 986 | *value = id; |
| 987 | } |
| 988 | if (val_len) { |
| 989 | *val_len = ret; |
| 990 | } |
| 991 | |
| 992 | parsed += ret + 1; |
| 993 | id += ret + 1; |
| 994 | } else { |
| 995 | return -parsed; |
| 996 | } |
| 997 | |
| 998 | while (isspace(id[0])) { |
| 999 | ++parsed; |
| 1000 | ++id; |
| 1001 | } |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1002 | } else if (value) { |
| 1003 | /* if value was expected, it's mandatory */ |
| 1004 | return -parsed; |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1005 | } |
| 1006 | |
| 1007 | if (id[0] != ']') { |
| 1008 | return -parsed; |
| 1009 | } |
| 1010 | |
| 1011 | ++parsed; |
| 1012 | ++id; |
| 1013 | |
| 1014 | if ((id[0] == '[') && has_predicate) { |
| 1015 | *has_predicate = 1; |
| 1016 | } |
| 1017 | |
| 1018 | return parsed; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 1019 | } |
| 1020 | |
| 1021 | /** |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1022 | * @brief Resolve (find) a feature definition. Logs directly. |
| 1023 | * |
| 1024 | * @param[in] feat_name Feature name to resolve. |
| 1025 | * @param[in] len Length of \p feat_name. |
| 1026 | * @param[in] node Node with the if-feature expression. |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1027 | * @param[out] feature Pointer to be set to point to the feature definition, if feature not found |
| 1028 | * (return code 1), the pointer is untouched. |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1029 | * |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1030 | * @return 0 on success, 1 on forward reference, -1 on error. |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1031 | */ |
| 1032 | static int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1033 | resolve_feature(const char *feat_name, uint16_t len, const struct lys_node *node, struct lys_feature **feature) |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1034 | { |
| 1035 | char *str; |
| 1036 | const char *mod_name, *name; |
| 1037 | int mod_name_len, nam_len, i, j; |
| 1038 | const struct lys_module *module; |
| 1039 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1040 | assert(feature); |
| 1041 | |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1042 | /* check prefix */ |
| 1043 | if ((i = parse_node_identifier(feat_name, &mod_name, &mod_name_len, &name, &nam_len)) < 1) { |
| 1044 | LOGVAL(LYE_INCHAR, LY_VLOG_NONE, NULL, feat_name[-i], &feat_name[-i]); |
| 1045 | return -1; |
| 1046 | } |
| 1047 | |
| 1048 | module = lys_get_import_module(lys_node_module(node), NULL, 0, mod_name, mod_name_len); |
| 1049 | if (!module) { |
| 1050 | /* identity refers unknown data model */ |
| 1051 | LOGVAL(LYE_INMOD_LEN, LY_VLOG_NONE, NULL, mod_name_len, mod_name); |
| 1052 | return -1; |
| 1053 | } |
| 1054 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1055 | if (module != node->module && module == lys_node_module(node)) { |
| 1056 | /* first, try to search directly in submodule where the feature was mentioned */ |
| 1057 | for (j = 0; j < node->module->features_size; j++) { |
| 1058 | if (!strncmp(name, node->module->features[j].name, nam_len) && !node->module->features[j].name[nam_len]) { |
| 1059 | /* check status */ |
| 1060 | if (lyp_check_status(node->flags, lys_node_module(node), node->name, node->module->features[j].flags, |
Pavol Vican | fdab9f9 | 2016-09-07 15:23:27 +0200 | [diff] [blame] | 1061 | node->module->features[j].module, node->module->features[j].name, NULL)) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1062 | return -1; |
| 1063 | } |
| 1064 | *feature = &node->module->features[j]; |
| 1065 | return 0; |
| 1066 | } |
| 1067 | } |
| 1068 | } |
| 1069 | |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1070 | /* search in the identified module ... */ |
| 1071 | for (j = 0; j < module->features_size; j++) { |
Michal Vasko | 3def867 | 2016-07-01 11:43:09 +0200 | [diff] [blame] | 1072 | if (!strncmp(name, module->features[j].name, nam_len) && !module->features[j].name[nam_len]) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1073 | /* check status */ |
| 1074 | if (lyp_check_status(node->flags, lys_node_module(node), node->name, module->features[j].flags, |
Pavol Vican | fdab9f9 | 2016-09-07 15:23:27 +0200 | [diff] [blame] | 1075 | module->features[j].module, module->features[j].name, NULL)) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1076 | return -1; |
| 1077 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1078 | *feature = &module->features[j]; |
| 1079 | return 0; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1080 | } |
| 1081 | } |
| 1082 | /* ... and all its submodules */ |
| 1083 | for (i = 0; i < module->inc_size; i++) { |
| 1084 | if (!module->inc[i].submodule) { |
| 1085 | /* not yet resolved */ |
| 1086 | continue; |
| 1087 | } |
| 1088 | for (j = 0; j < module->inc[i].submodule->features_size; j++) { |
Michal Vasko | 3def867 | 2016-07-01 11:43:09 +0200 | [diff] [blame] | 1089 | if (!strncmp(name, module->inc[i].submodule->features[j].name, nam_len) |
| 1090 | && !module->inc[i].submodule->features[j].name[nam_len]) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1091 | /* check status */ |
| 1092 | if (lyp_check_status(node->flags, lys_node_module(node), node->name, |
| 1093 | module->inc[i].submodule->features[j].flags, |
| 1094 | module->inc[i].submodule->features[j].module, |
Pavol Vican | fdab9f9 | 2016-09-07 15:23:27 +0200 | [diff] [blame] | 1095 | module->inc[i].submodule->features[j].name, NULL)) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1096 | return -1; |
| 1097 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1098 | *feature = &module->inc[i].submodule->features[j]; |
| 1099 | return 0; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1100 | } |
| 1101 | } |
| 1102 | } |
| 1103 | |
| 1104 | /* not found */ |
| 1105 | str = strndup(feat_name, len); |
| 1106 | LOGVAL(LYE_INRESOLV, LY_VLOG_NONE, NULL, "feature", str); |
| 1107 | free(str); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1108 | return 1; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1109 | } |
| 1110 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1111 | /* |
| 1112 | * @return |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1113 | * - 1 if enabled |
| 1114 | * - 0 if disabled |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1115 | * - -1 if not usable by its if-feature expression |
| 1116 | */ |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1117 | static int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1118 | resolve_feature_value(const struct lys_feature *feat) |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1119 | { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1120 | int i; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1121 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1122 | for (i = 0; i < feat->iffeature_size; i++) { |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1123 | if (!resolve_iffeature(&feat->iffeature[i])) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1124 | return -1; |
| 1125 | } |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1126 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1127 | |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1128 | return feat->flags & LYS_FENABLED ? 1 : 0; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1129 | } |
| 1130 | |
| 1131 | static int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1132 | resolve_iffeature_recursive(struct lys_iffeature *expr, int *index_e, int *index_f) |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1133 | { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1134 | uint8_t op; |
| 1135 | int rc, a, b; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1136 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1137 | op = iff_getop(expr->expr, *index_e); |
| 1138 | (*index_e)++; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1139 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1140 | switch (op) { |
| 1141 | case LYS_IFF_F: |
| 1142 | /* resolve feature */ |
| 1143 | return resolve_feature_value(expr->features[(*index_f)++]); |
| 1144 | case LYS_IFF_NOT: |
| 1145 | rc = resolve_iffeature_recursive(expr, index_e, index_f); |
| 1146 | if (rc == -1) { |
| 1147 | /* one of the referenced feature is hidden by its if-feature, |
| 1148 | * so this if-feature expression is always false */ |
| 1149 | return -1; |
| 1150 | } else { |
| 1151 | /* invert result */ |
| 1152 | return rc ? 0 : 1; |
| 1153 | } |
| 1154 | case LYS_IFF_AND: |
| 1155 | case LYS_IFF_OR: |
| 1156 | a = resolve_iffeature_recursive(expr, index_e, index_f); |
| 1157 | b = resolve_iffeature_recursive(expr, index_e, index_f); |
| 1158 | if (a == -1 || b == -1) { |
| 1159 | /* one of the referenced feature is hidden by its if-feature, |
| 1160 | * so this if-feature expression is always false */ |
| 1161 | return -1; |
| 1162 | } else if (op == LYS_IFF_AND) { |
| 1163 | return a && b; |
| 1164 | } else { /* LYS_IFF_OR */ |
| 1165 | return a || b; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1166 | } |
| 1167 | } |
| 1168 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1169 | return -1; |
| 1170 | } |
| 1171 | |
| 1172 | int |
| 1173 | resolve_iffeature(struct lys_iffeature *expr) |
| 1174 | { |
| 1175 | int rc = -1; |
| 1176 | int index_e = 0, index_f = 0; |
| 1177 | |
| 1178 | if (expr->expr) { |
| 1179 | rc = resolve_iffeature_recursive(expr, &index_e, &index_f); |
| 1180 | } |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1181 | return (rc == 1) ? 1 : 0; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1182 | } |
| 1183 | |
| 1184 | struct iff_stack { |
| 1185 | int size; |
| 1186 | int index; /* first empty item */ |
| 1187 | uint8_t *stack; |
| 1188 | }; |
| 1189 | |
| 1190 | static int |
| 1191 | iff_stack_push(struct iff_stack *stack, uint8_t value) |
| 1192 | { |
| 1193 | if (stack->index == stack->size) { |
| 1194 | stack->size += 4; |
| 1195 | stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack); |
| 1196 | if (!stack->stack) { |
| 1197 | LOGMEM; |
| 1198 | stack->size = 0; |
| 1199 | return EXIT_FAILURE; |
| 1200 | } |
| 1201 | } |
| 1202 | |
| 1203 | stack->stack[stack->index++] = value; |
| 1204 | return EXIT_SUCCESS; |
| 1205 | } |
| 1206 | |
| 1207 | static uint8_t |
| 1208 | iff_stack_pop(struct iff_stack *stack) |
| 1209 | { |
| 1210 | stack->index--; |
| 1211 | return stack->stack[stack->index]; |
| 1212 | } |
| 1213 | |
| 1214 | static void |
| 1215 | iff_stack_clean(struct iff_stack *stack) |
| 1216 | { |
| 1217 | stack->size = 0; |
| 1218 | free(stack->stack); |
| 1219 | } |
| 1220 | |
| 1221 | static void |
| 1222 | iff_setop(uint8_t *list, uint8_t op, int pos) |
| 1223 | { |
| 1224 | uint8_t *item; |
| 1225 | uint8_t mask = 3; |
| 1226 | |
| 1227 | assert(pos >= 0); |
| 1228 | assert(op <= 3); /* max 2 bits */ |
| 1229 | |
| 1230 | item = &list[pos / 4]; |
| 1231 | mask = mask << 2 * (pos % 4); |
| 1232 | *item = (*item) & ~mask; |
| 1233 | *item = (*item) | (op << 2 * (pos % 4)); |
| 1234 | } |
| 1235 | |
| 1236 | uint8_t |
| 1237 | iff_getop(uint8_t *list, int pos) |
| 1238 | { |
| 1239 | uint8_t *item; |
| 1240 | uint8_t mask = 3, result; |
| 1241 | |
| 1242 | assert(pos >= 0); |
| 1243 | |
| 1244 | item = &list[pos / 4]; |
| 1245 | result = (*item) & (mask << 2 * (pos % 4)); |
| 1246 | return result >> 2 * (pos % 4); |
| 1247 | } |
| 1248 | |
| 1249 | #define LYS_IFF_LP 0x04 /* ( */ |
| 1250 | #define LYS_IFF_RP 0x08 /* ) */ |
| 1251 | |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1252 | /* internal structure for passing data for UNRES_IFFEAT */ |
| 1253 | struct unres_iffeat_data { |
| 1254 | struct lys_node *node; |
| 1255 | const char *fname; |
| 1256 | }; |
| 1257 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1258 | void |
| 1259 | resolve_iffeature_getsizes(struct lys_iffeature *iffeat, unsigned int *expr_size, unsigned int *feat_size) |
| 1260 | { |
| 1261 | unsigned int e = 0, f = 0, r = 0; |
| 1262 | uint8_t op; |
| 1263 | |
| 1264 | assert(iffeat); |
| 1265 | |
| 1266 | if (!iffeat->expr) { |
| 1267 | goto result; |
| 1268 | } |
| 1269 | |
| 1270 | do { |
| 1271 | op = iff_getop(iffeat->expr, e++); |
| 1272 | switch (op) { |
| 1273 | case LYS_IFF_NOT: |
| 1274 | if (!r) { |
| 1275 | r += 1; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1276 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1277 | break; |
| 1278 | case LYS_IFF_AND: |
| 1279 | case LYS_IFF_OR: |
| 1280 | if (!r) { |
| 1281 | r += 2; |
| 1282 | } else { |
| 1283 | r += 1; |
| 1284 | } |
| 1285 | break; |
| 1286 | case LYS_IFF_F: |
| 1287 | f++; |
| 1288 | if (r) { |
| 1289 | r--; |
| 1290 | } |
| 1291 | break; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1292 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1293 | } while(r); |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1294 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1295 | result: |
| 1296 | if (expr_size) { |
| 1297 | *expr_size = e; |
| 1298 | } |
| 1299 | if (feat_size) { |
| 1300 | *feat_size = f; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1301 | } |
| 1302 | } |
| 1303 | |
| 1304 | int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1305 | resolve_iffeature_compile(struct lys_iffeature *iffeat_expr, const char *value, struct lys_node *node, |
| 1306 | struct unres_schema *unres) |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1307 | { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1308 | const char *c = value; |
| 1309 | int r, rc = EXIT_FAILURE; |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1310 | int i, j, last_not, checkversion = 0; |
| 1311 | unsigned int f_size = 0, expr_size = 0, f_exp = 1; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1312 | uint8_t op; |
| 1313 | struct iff_stack stack = {0, 0, NULL}; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1314 | struct unres_iffeat_data *iff_data; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1315 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1316 | assert(c); |
| 1317 | |
| 1318 | if (isspace(c[0])) { |
| 1319 | LOGVAL(LYE_INCHAR, LY_VLOG_NONE, NULL, c[0], c); |
| 1320 | return EXIT_FAILURE; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1321 | } |
| 1322 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1323 | /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */ |
| 1324 | for (i = j = last_not = 0; c[i]; i++) { |
| 1325 | if (c[i] == '(') { |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1326 | checkversion = 1; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1327 | j++; |
| 1328 | continue; |
| 1329 | } else if (c[i] == ')') { |
| 1330 | j--; |
| 1331 | continue; |
| 1332 | } else if (isspace(c[i])) { |
| 1333 | continue; |
| 1334 | } |
| 1335 | |
| 1336 | if (!strncmp(&c[i], "not", r = 3) || !strncmp(&c[i], "and", r = 3) || !strncmp(&c[i], "or", r = 2)) { |
| 1337 | if (c[i + r] == '\0') { |
Radek Krejci | a98da3f | 2016-07-27 14:05:22 +0200 | [diff] [blame] | 1338 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1339 | return EXIT_FAILURE; |
| 1340 | } else if (!isspace(c[i + r])) { |
| 1341 | /* feature name starting with the not/and/or */ |
| 1342 | last_not = 0; |
| 1343 | f_size++; |
| 1344 | } else if (c[i] == 'n') { /* not operation */ |
| 1345 | if (last_not) { |
| 1346 | /* double not */ |
| 1347 | expr_size = expr_size - 2; |
| 1348 | last_not = 0; |
| 1349 | } else { |
| 1350 | last_not = 1; |
| 1351 | } |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1352 | } else { /* and, or */ |
| 1353 | f_exp++; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1354 | /* not a not operation */ |
| 1355 | last_not = 0; |
| 1356 | } |
| 1357 | i += r; |
| 1358 | } else { |
| 1359 | f_size++; |
| 1360 | last_not = 0; |
| 1361 | } |
| 1362 | expr_size++; |
| 1363 | |
| 1364 | while (!isspace(c[i])) { |
| 1365 | if (!c[i] || c[i] == ')') { |
| 1366 | i--; |
| 1367 | break; |
| 1368 | } |
| 1369 | i++; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1370 | } |
| 1371 | } |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1372 | if (j || f_exp != f_size) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1373 | /* not matching count of ( and ) */ |
| 1374 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
| 1375 | return EXIT_FAILURE; |
| 1376 | } |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1377 | |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1378 | if (checkversion || expr_size > 1) { |
| 1379 | /* check that we have 1.1 module */ |
| 1380 | if (node->module->version != 2) { |
| 1381 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
| 1382 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "YANG 1.1 if-feature expression found in 1.0 module."); |
| 1383 | return EXIT_FAILURE; |
| 1384 | } |
| 1385 | } |
| 1386 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1387 | /* allocate the memory */ |
| 1388 | iffeat_expr->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iffeat_expr->expr); |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1389 | iffeat_expr->features = calloc(f_size, sizeof *iffeat_expr->features); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1390 | stack.size = expr_size; |
| 1391 | stack.stack = malloc(expr_size * sizeof *stack.stack); |
| 1392 | if (!stack.stack || !iffeat_expr->expr || !iffeat_expr->features) { |
| 1393 | LOGMEM; |
| 1394 | goto error; |
| 1395 | } |
| 1396 | f_size--; expr_size--; /* used as indexes from now */ |
| 1397 | |
| 1398 | for (i--; i >= 0; i--) { |
| 1399 | if (c[i] == ')') { |
| 1400 | /* push it on stack */ |
| 1401 | iff_stack_push(&stack, LYS_IFF_RP); |
| 1402 | continue; |
| 1403 | } else if (c[i] == '(') { |
| 1404 | /* pop from the stack into result all operators until ) */ |
| 1405 | while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) { |
| 1406 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1407 | } |
| 1408 | continue; |
| 1409 | } else if (isspace(c[i])) { |
| 1410 | continue; |
| 1411 | } |
| 1412 | |
| 1413 | /* end operator or operand -> find beginning and get what is it */ |
| 1414 | j = i + 1; |
| 1415 | while (i >= 0 && !isspace(c[i]) && c[i] != '(') { |
| 1416 | i--; |
| 1417 | } |
| 1418 | i++; /* get back by one step */ |
| 1419 | |
| 1420 | if (!strncmp(&c[i], "not ", 4)) { |
| 1421 | if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) { |
| 1422 | /* double not */ |
| 1423 | iff_stack_pop(&stack); |
| 1424 | } else { |
| 1425 | /* not has the highest priority, so do not pop from the stack |
| 1426 | * as in case of AND and OR */ |
| 1427 | iff_stack_push(&stack, LYS_IFF_NOT); |
| 1428 | } |
| 1429 | } else if (!strncmp(&c[i], "and ", 4)) { |
| 1430 | /* as for OR - pop from the stack all operators with the same or higher |
| 1431 | * priority and store them to the result, then push the AND to the stack */ |
| 1432 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) { |
| 1433 | op = iff_stack_pop(&stack); |
| 1434 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1435 | } |
| 1436 | iff_stack_push(&stack, LYS_IFF_AND); |
| 1437 | } else if (!strncmp(&c[i], "or ", 3)) { |
| 1438 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) { |
| 1439 | op = iff_stack_pop(&stack); |
| 1440 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1441 | } |
| 1442 | iff_stack_push(&stack, LYS_IFF_OR); |
| 1443 | } else { |
| 1444 | /* feature name, length is j - i */ |
| 1445 | |
| 1446 | /* add it to the result */ |
| 1447 | iff_setop(iffeat_expr->expr, LYS_IFF_F, expr_size--); |
| 1448 | |
| 1449 | /* now get the link to the feature definition. Since it can be |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1450 | * forward referenced, we have to keep the feature name in auxiliary |
| 1451 | * structure passed into unres */ |
| 1452 | iff_data = malloc(sizeof *iff_data); |
| 1453 | iff_data->node = node; |
| 1454 | iff_data->fname = lydict_insert(node->module->ctx, &c[i], j - i); |
| 1455 | r = unres_schema_add_node(node->module, unres, &iffeat_expr->features[f_size], UNRES_IFFEAT, |
| 1456 | (struct lys_node *)iff_data); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1457 | f_size--; |
| 1458 | |
| 1459 | if (r == -1) { |
Pavol Vican | 4d08451 | 2016-09-29 16:38:12 +0200 | [diff] [blame] | 1460 | free(iff_data); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1461 | goto error; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1462 | } |
| 1463 | } |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1464 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1465 | while (stack.index) { |
| 1466 | op = iff_stack_pop(&stack); |
| 1467 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1468 | } |
| 1469 | |
| 1470 | if (++expr_size || ++f_size) { |
| 1471 | /* not all expected operators and operands found */ |
| 1472 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
| 1473 | rc = EXIT_FAILURE; |
| 1474 | } else { |
| 1475 | rc = EXIT_SUCCESS; |
| 1476 | } |
| 1477 | |
| 1478 | error: |
| 1479 | /* cleanup */ |
| 1480 | iff_stack_clean(&stack); |
| 1481 | |
| 1482 | return rc; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1483 | } |
| 1484 | |
| 1485 | /** |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1486 | * @brief Resolve (find) a data node based on a schema-nodeid. |
| 1487 | * |
| 1488 | * Used for resolving unique statements - so id is expected to be relative and local (without reference to a different |
| 1489 | * module). |
| 1490 | * |
| 1491 | */ |
| 1492 | struct lyd_node * |
| 1493 | resolve_data_descendant_schema_nodeid(const char *nodeid, struct lyd_node *start) |
| 1494 | { |
| 1495 | char *str, *token, *p; |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1496 | struct lyd_node *result = NULL, *iter; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1497 | const struct lys_node *schema = NULL; |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1498 | int shorthand = 0; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1499 | |
| 1500 | assert(nodeid && start); |
| 1501 | |
| 1502 | if (nodeid[0] == '/') { |
| 1503 | return NULL; |
| 1504 | } |
| 1505 | |
| 1506 | str = p = strdup(nodeid); |
| 1507 | if (!str) { |
| 1508 | LOGMEM; |
| 1509 | return NULL; |
| 1510 | } |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1511 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1512 | while (p) { |
| 1513 | token = p; |
| 1514 | p = strchr(p, '/'); |
| 1515 | if (p) { |
| 1516 | *p = '\0'; |
| 1517 | p++; |
| 1518 | } |
| 1519 | |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1520 | if (p) { |
| 1521 | /* inner node */ |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1522 | if (resolve_descendant_schema_nodeid(token, schema ? schema->child : start->schema, |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 1523 | LYS_CONTAINER | LYS_CHOICE | LYS_CASE | LYS_LEAF, 0, 0, &schema) |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1524 | || !schema) { |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1525 | result = NULL; |
| 1526 | break; |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1527 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1528 | |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1529 | if (schema->nodetype & (LYS_CHOICE | LYS_CASE)) { |
| 1530 | continue; |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 1531 | } else if (lys_parent(schema)->nodetype == LYS_CHOICE) { |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1532 | /* shorthand case */ |
| 1533 | if (!shorthand) { |
| 1534 | shorthand = 1; |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 1535 | schema = lys_parent(schema); |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1536 | continue; |
| 1537 | } else { |
| 1538 | shorthand = 0; |
| 1539 | if (schema->nodetype == LYS_LEAF) { |
| 1540 | /* should not be here, since we have leaf, which is not a shorthand nor final node */ |
| 1541 | result = NULL; |
| 1542 | break; |
| 1543 | } |
| 1544 | } |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1545 | } |
| 1546 | } else { |
| 1547 | /* final node */ |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 1548 | if (resolve_descendant_schema_nodeid(token, schema ? schema->child : start->schema, LYS_LEAF, |
| 1549 | shorthand ? 0 : 1, 0, &schema) |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1550 | || !schema) { |
| 1551 | result = NULL; |
| 1552 | break; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1553 | } |
| 1554 | } |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1555 | LY_TREE_FOR(result ? result->child : start, iter) { |
| 1556 | if (iter->schema == schema) { |
| 1557 | /* move in data tree according to returned schema */ |
| 1558 | result = iter; |
| 1559 | break; |
| 1560 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1561 | } |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1562 | if (!iter) { |
| 1563 | /* instance not found */ |
| 1564 | result = NULL; |
| 1565 | break; |
| 1566 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1567 | } |
| 1568 | free(str); |
| 1569 | |
| 1570 | return result; |
| 1571 | } |
| 1572 | |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1573 | /* |
| 1574 | * 0 - ok (done) |
| 1575 | * 1 - continue |
| 1576 | * 2 - break |
| 1577 | * -1 - error |
| 1578 | */ |
| 1579 | static int |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1580 | 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] | 1581 | const struct lys_module *module, const char *mod_name, int mod_name_len, |
Radek Krejci | 0fa54e9 | 2016-09-14 14:01:05 +0200 | [diff] [blame] | 1582 | int implemented_mod, const struct lys_node **start) |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1583 | { |
| 1584 | const struct lys_module *prefix_mod; |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1585 | int sh = 0; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1586 | |
| 1587 | /* module check */ |
| 1588 | prefix_mod = lys_get_import_module(module, NULL, 0, mod_name, mod_name_len); |
Radek Krejci | 0fa54e9 | 2016-09-14 14:01:05 +0200 | [diff] [blame] | 1589 | if (implemented_mod) { |
| 1590 | prefix_mod = lys_get_implemented_module(prefix_mod); |
| 1591 | } |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1592 | if (!prefix_mod) { |
| 1593 | return -1; |
| 1594 | } |
| 1595 | if (prefix_mod != lys_node_module(sibling)) { |
| 1596 | return 1; |
| 1597 | } |
| 1598 | |
| 1599 | /* check for shorthand cases - then 'start' does not change */ |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 1600 | if (lys_parent(sibling) && (lys_parent(sibling)->nodetype == LYS_CHOICE) && (sibling->nodetype != LYS_CASE)) { |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1601 | if (*shorthand != -1) { |
| 1602 | *shorthand = *shorthand ? 0 : 1; |
| 1603 | } |
| 1604 | sh = 1; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1605 | } |
| 1606 | |
| 1607 | /* the result node? */ |
| 1608 | if (!id[0]) { |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1609 | if (*shorthand == 1) { |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1610 | return 1; |
| 1611 | } |
| 1612 | return 0; |
| 1613 | } |
| 1614 | |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1615 | if (!sh) { |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1616 | /* move down the tree, if possible */ |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 1617 | if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1618 | return -1; |
| 1619 | } |
| 1620 | *start = sibling->child; |
| 1621 | } |
| 1622 | |
| 1623 | return 2; |
| 1624 | } |
| 1625 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1626 | /* start - relative, module - absolute, -1 error, EXIT_SUCCESS ok (but ret can still be NULL), >0 unexpected char on ret - 1 */ |
| 1627 | int |
| 1628 | resolve_augment_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_module *module, |
| 1629 | const struct lys_node **ret) |
| 1630 | { |
| 1631 | const char *name, *mod_name, *id; |
| 1632 | const struct lys_node *sibling; |
| 1633 | int r, nam_len, mod_name_len, is_relative = -1; |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1634 | int8_t shorthand = 0; |
Radek Krejci | 0fa54e9 | 2016-09-14 14:01:05 +0200 | [diff] [blame] | 1635 | int implemented_search = 0; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1636 | /* resolved import module from the start module, it must match the next node-name-match sibling */ |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1637 | const struct lys_module *start_mod; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1638 | |
| 1639 | assert(nodeid && (start || module) && !(start && module) && ret); |
| 1640 | |
| 1641 | id = nodeid; |
| 1642 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1643 | 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] | 1644 | return ((id - nodeid) - r) + 1; |
| 1645 | } |
| 1646 | id += r; |
| 1647 | |
| 1648 | if ((is_relative && !start) || (!is_relative && !module)) { |
| 1649 | return -1; |
| 1650 | } |
| 1651 | |
| 1652 | /* descendant-schema-nodeid */ |
| 1653 | if (is_relative) { |
Michal Vasko | 4c06fd8 | 2016-02-17 13:43:38 +0100 | [diff] [blame] | 1654 | module = start_mod = start->module; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1655 | |
| 1656 | /* absolute-schema-nodeid */ |
| 1657 | } else { |
| 1658 | start_mod = lys_get_import_module(module, NULL, 0, mod_name, mod_name_len); |
Radek Krejci | 0fa54e9 | 2016-09-14 14:01:05 +0200 | [diff] [blame] | 1659 | if (start_mod != lys_main_module(module)) { |
| 1660 | /* if the submodule augments the mainmodule (or in general a module augments |
| 1661 | * itself, we don't want to search for the implemented module but augments |
| 1662 | * the module anyway. But when augmenting another module, we need the implemented |
| 1663 | * revision of the module if any */ |
| 1664 | start_mod = lys_get_implemented_module(start_mod); |
| 1665 | implemented_search = 1; |
| 1666 | } |
Michal Vasko | e290563 | 2016-02-11 15:42:24 +0100 | [diff] [blame] | 1667 | if (!start_mod) { |
| 1668 | return -1; |
| 1669 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1670 | start = start_mod->data; |
| 1671 | } |
| 1672 | |
| 1673 | while (1) { |
| 1674 | sibling = NULL; |
| 1675 | while ((sibling = lys_getnext(sibling, lys_parent(start), start_mod, |
| 1676 | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT))) { |
| 1677 | /* name match */ |
Michal Vasko | 0a1aaa4 | 2016-04-19 09:48:25 +0200 | [diff] [blame] | 1678 | if (sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len]) { |
Radek Krejci | 0fa54e9 | 2016-09-14 14:01:05 +0200 | [diff] [blame] | 1679 | r = schema_nodeid_siblingcheck(sibling, &shorthand, id, module, mod_name, mod_name_len, |
| 1680 | implemented_search, &start); |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1681 | if (r == 0) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1682 | *ret = sibling; |
| 1683 | return EXIT_SUCCESS; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1684 | } else if (r == 1) { |
| 1685 | continue; |
| 1686 | } else if (r == 2) { |
| 1687 | break; |
| 1688 | } else { |
| 1689 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1690 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1691 | } |
| 1692 | } |
| 1693 | |
| 1694 | /* no match */ |
| 1695 | if (!sibling) { |
Michal Vasko | a426fef | 2016-03-07 10:47:31 +0100 | [diff] [blame] | 1696 | *ret = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1697 | return EXIT_SUCCESS; |
| 1698 | } |
| 1699 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1700 | 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] | 1701 | return ((id - nodeid) - r) + 1; |
| 1702 | } |
| 1703 | id += r; |
| 1704 | } |
| 1705 | |
| 1706 | /* cannot get here */ |
| 1707 | LOGINT; |
| 1708 | return -1; |
| 1709 | } |
| 1710 | |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 1711 | /* unique, refine, |
| 1712 | * >0 - unexpected char on position (ret - 1), |
| 1713 | * 0 - ok (but ret can still be NULL), |
| 1714 | * -1 - error, |
| 1715 | * -2 - violated no_innerlist */ |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1716 | int |
| 1717 | resolve_descendant_schema_nodeid(const char *nodeid, const struct lys_node *start, int ret_nodetype, |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 1718 | int check_shorthand, int no_innerlist, const struct lys_node **ret) |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1719 | { |
| 1720 | const char *name, *mod_name, *id; |
| 1721 | const struct lys_node *sibling; |
| 1722 | int r, nam_len, mod_name_len, is_relative = -1; |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1723 | int8_t shorthand = check_shorthand ? 0 : -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1724 | /* resolved import module from the start module, it must match the next node-name-match sibling */ |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1725 | const struct lys_module *module; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1726 | |
| 1727 | assert(nodeid && start && ret); |
| 1728 | assert(!(ret_nodetype & (LYS_USES | LYS_AUGMENT)) && ((ret_nodetype == LYS_GROUPING) || !(ret_nodetype & LYS_GROUPING))); |
| 1729 | |
| 1730 | id = nodeid; |
| 1731 | module = start->module; |
| 1732 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1733 | 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] | 1734 | return ((id - nodeid) - r) + 1; |
| 1735 | } |
| 1736 | id += r; |
| 1737 | |
| 1738 | if (!is_relative) { |
| 1739 | return -1; |
| 1740 | } |
| 1741 | |
| 1742 | while (1) { |
| 1743 | sibling = NULL; |
| 1744 | while ((sibling = lys_getnext(sibling, lys_parent(start), module, |
| 1745 | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE))) { |
| 1746 | /* name match */ |
| 1747 | if (sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len]) { |
Radek Krejci | 0fa54e9 | 2016-09-14 14:01:05 +0200 | [diff] [blame] | 1748 | r = schema_nodeid_siblingcheck(sibling, &shorthand, id, module, mod_name, mod_name_len, 0, &start); |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1749 | if (r == 0) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1750 | if (!(sibling->nodetype & ret_nodetype)) { |
| 1751 | /* wrong node type, too bad */ |
| 1752 | continue; |
| 1753 | } |
| 1754 | *ret = sibling; |
| 1755 | return EXIT_SUCCESS; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1756 | } else if (r == 1) { |
| 1757 | continue; |
| 1758 | } else if (r == 2) { |
| 1759 | break; |
| 1760 | } else { |
| 1761 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1762 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1763 | } |
| 1764 | } |
| 1765 | |
| 1766 | /* no match */ |
| 1767 | if (!sibling) { |
Michal Vasko | a426fef | 2016-03-07 10:47:31 +0100 | [diff] [blame] | 1768 | *ret = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1769 | return EXIT_SUCCESS; |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 1770 | } else if (no_innerlist && sibling->nodetype == LYS_LIST) { |
| 1771 | *ret = NULL; |
| 1772 | return -2; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1773 | } |
| 1774 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1775 | 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] | 1776 | return ((id - nodeid) - r) + 1; |
| 1777 | } |
| 1778 | id += r; |
| 1779 | } |
| 1780 | |
| 1781 | /* cannot get here */ |
| 1782 | LOGINT; |
| 1783 | return -1; |
| 1784 | } |
| 1785 | |
| 1786 | /* choice default */ |
| 1787 | int |
| 1788 | resolve_choice_default_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_node **ret) |
| 1789 | { |
| 1790 | /* cannot actually be a path */ |
| 1791 | if (strchr(nodeid, '/')) { |
| 1792 | return -1; |
| 1793 | } |
| 1794 | |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 1795 | return resolve_descendant_schema_nodeid(nodeid, start, LYS_NO_RPC_NOTIF_NODE, 1, 0, ret); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1796 | } |
| 1797 | |
| 1798 | /* uses, -1 error, EXIT_SUCCESS ok (but ret can still be NULL), >0 unexpected char on ret - 1 */ |
| 1799 | static int |
| 1800 | resolve_uses_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_node_grp **ret) |
| 1801 | { |
| 1802 | const struct lys_module *module; |
| 1803 | const char *mod_prefix, *name; |
| 1804 | int i, mod_prefix_len, nam_len; |
| 1805 | |
| 1806 | /* parse the identifier, it must be parsed on one call */ |
| 1807 | if (((i = parse_node_identifier(nodeid, &mod_prefix, &mod_prefix_len, &name, &nam_len)) < 1) || nodeid[i]) { |
| 1808 | return -i + 1; |
| 1809 | } |
| 1810 | |
| 1811 | module = lys_get_import_module(start->module, mod_prefix, mod_prefix_len, NULL, 0); |
| 1812 | if (!module) { |
| 1813 | return -1; |
| 1814 | } |
| 1815 | if (module != start->module) { |
| 1816 | start = module->data; |
| 1817 | } |
| 1818 | |
| 1819 | *ret = lys_find_grouping_up(name, (struct lys_node *)start); |
| 1820 | |
| 1821 | return EXIT_SUCCESS; |
| 1822 | } |
| 1823 | |
| 1824 | int |
| 1825 | resolve_absolute_schema_nodeid(const char *nodeid, const struct lys_module *module, int ret_nodetype, |
| 1826 | const struct lys_node **ret) |
| 1827 | { |
| 1828 | const char *name, *mod_name, *id; |
| 1829 | const struct lys_node *sibling, *start; |
| 1830 | int r, nam_len, mod_name_len, is_relative = -1; |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1831 | int8_t shorthand = 0; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1832 | const struct lys_module *abs_start_mod; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1833 | |
| 1834 | assert(nodeid && module && ret); |
| 1835 | assert(!(ret_nodetype & (LYS_USES | LYS_AUGMENT)) && ((ret_nodetype == LYS_GROUPING) || !(ret_nodetype & LYS_GROUPING))); |
| 1836 | |
| 1837 | id = nodeid; |
| 1838 | start = module->data; |
| 1839 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1840 | 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] | 1841 | return ((id - nodeid) - r) + 1; |
| 1842 | } |
| 1843 | id += r; |
| 1844 | |
| 1845 | if (is_relative) { |
| 1846 | return -1; |
| 1847 | } |
| 1848 | |
| 1849 | 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] | 1850 | if (!abs_start_mod) { |
| 1851 | return -1; |
| 1852 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1853 | |
| 1854 | while (1) { |
| 1855 | sibling = NULL; |
| 1856 | while ((sibling = lys_getnext(sibling, lys_parent(start), abs_start_mod, LYS_GETNEXT_WITHCHOICE |
| 1857 | | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT | LYS_GETNEXT_WITHGROUPING))) { |
| 1858 | /* name match */ |
Michal Vasko | 0a1aaa4 | 2016-04-19 09:48:25 +0200 | [diff] [blame] | 1859 | if (sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len]) { |
Radek Krejci | 0fa54e9 | 2016-09-14 14:01:05 +0200 | [diff] [blame] | 1860 | r = schema_nodeid_siblingcheck(sibling, &shorthand, id, module, mod_name, mod_name_len, 0, &start); |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1861 | if (r == 0) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1862 | if (!(sibling->nodetype & ret_nodetype)) { |
| 1863 | /* wrong node type, too bad */ |
| 1864 | continue; |
| 1865 | } |
| 1866 | *ret = sibling; |
| 1867 | return EXIT_SUCCESS; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1868 | } else if (r == 1) { |
| 1869 | continue; |
| 1870 | } else if (r == 2) { |
| 1871 | break; |
| 1872 | } else { |
| 1873 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1874 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1875 | } |
| 1876 | } |
| 1877 | |
| 1878 | /* no match */ |
| 1879 | if (!sibling) { |
Michal Vasko | a426fef | 2016-03-07 10:47:31 +0100 | [diff] [blame] | 1880 | *ret = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1881 | return EXIT_SUCCESS; |
| 1882 | } |
| 1883 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1884 | 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] | 1885 | return ((id - nodeid) - r) + 1; |
| 1886 | } |
| 1887 | id += r; |
| 1888 | } |
| 1889 | |
| 1890 | /* cannot get here */ |
| 1891 | LOGINT; |
| 1892 | return -1; |
| 1893 | } |
| 1894 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1895 | static int |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1896 | 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] | 1897 | { |
| 1898 | const char *name; |
| 1899 | int nam_len, has_predicate, i; |
| 1900 | |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 1901 | if (((i = parse_schema_json_predicate(predicate, &name, &nam_len, NULL, NULL, &has_predicate)) < 1) |
| 1902 | || !strncmp(name, ".", nam_len)) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 1903 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-i], &predicate[-i]); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1904 | return -1; |
| 1905 | } |
| 1906 | |
| 1907 | predicate += i; |
| 1908 | *parsed += i; |
| 1909 | |
| 1910 | for (i = 0; i < list->keys_size; ++i) { |
| 1911 | if (!strncmp(list->keys[i]->name, name, nam_len) && !list->keys[i]->name[nam_len]) { |
| 1912 | break; |
| 1913 | } |
| 1914 | } |
| 1915 | |
| 1916 | if (i == list->keys_size) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 1917 | LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1918 | return -1; |
| 1919 | } |
| 1920 | |
| 1921 | /* more predicates? */ |
| 1922 | if (has_predicate) { |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1923 | return resolve_json_schema_list_predicate(predicate, list, parsed); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1924 | } |
| 1925 | |
| 1926 | return 0; |
| 1927 | } |
| 1928 | |
Michal Vasko | 8d26e5c | 2016-09-08 10:03:49 +0200 | [diff] [blame] | 1929 | /* cannot return LYS_GROUPING, LYS_AUGMENT, LYS_USES, logs directly */ |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1930 | const struct lys_node * |
Michal Vasko | 8d26e5c | 2016-09-08 10:03:49 +0200 | [diff] [blame] | 1931 | resolve_json_nodeid(const char *nodeid, struct ly_ctx *ctx, const struct lys_node *start) |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1932 | { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 1933 | char *module_name = ly_buf(), *buf_backup = NULL, *str; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1934 | const char *name, *mod_name, *id; |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1935 | const struct lys_node *sibling; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1936 | 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] | 1937 | /* 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] | 1938 | const struct lys_module *prefix_mod, *module, *prev_mod; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1939 | |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1940 | assert(nodeid && (ctx || start)); |
| 1941 | if (!ctx) { |
| 1942 | ctx = start->module->ctx; |
| 1943 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1944 | |
| 1945 | id = nodeid; |
| 1946 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1947 | 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] | 1948 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1949 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1950 | } |
| 1951 | id += r; |
| 1952 | |
| 1953 | if (is_relative) { |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1954 | assert(start); |
| 1955 | start = start->child; |
| 1956 | if (!start) { |
| 1957 | /* no descendants, fail for sure */ |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 1958 | str = strndup(nodeid, (name + nam_len) - nodeid); |
| 1959 | LOGVAL(LYE_PATH_INNODE, LY_VLOG_STR, str); |
| 1960 | free(str); |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1961 | return NULL; |
| 1962 | } |
| 1963 | module = start->module; |
| 1964 | } else { |
| 1965 | if (!mod_name) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 1966 | str = strndup(nodeid, (name + nam_len) - nodeid); |
| 1967 | LOGVAL(LYE_PATH_MISSMOD, LY_VLOG_STR, nodeid); |
| 1968 | free(str); |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1969 | return NULL; |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 1970 | } else if (mod_name_len > LY_BUF_SIZE - 1) { |
| 1971 | LOGINT; |
| 1972 | return NULL; |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1973 | } |
| 1974 | |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 1975 | if (ly_buf_used && module_name[0]) { |
| 1976 | buf_backup = strndup(module_name, LY_BUF_SIZE - 1); |
| 1977 | } |
| 1978 | ly_buf_used++; |
| 1979 | |
Michal Vasko | df3f1ab | 2016-04-01 15:07:22 +0200 | [diff] [blame] | 1980 | memmove(module_name, mod_name, mod_name_len); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 1981 | module_name[mod_name_len] = '\0'; |
| 1982 | module = ly_ctx_get_module(ctx, module_name, NULL); |
| 1983 | |
| 1984 | if (buf_backup) { |
| 1985 | /* return previous internal buffer content */ |
| 1986 | strcpy(module_name, buf_backup); |
| 1987 | free(buf_backup); |
| 1988 | buf_backup = NULL; |
| 1989 | } |
| 1990 | ly_buf_used--; |
| 1991 | |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1992 | if (!module) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 1993 | str = strndup(nodeid, (mod_name + mod_name_len) - nodeid); |
| 1994 | LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str); |
| 1995 | free(str); |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1996 | return NULL; |
| 1997 | } |
| 1998 | start = module->data; |
| 1999 | |
| 2000 | /* now it's as if there was no module name */ |
| 2001 | mod_name = NULL; |
| 2002 | mod_name_len = 0; |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2003 | } |
| 2004 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2005 | prev_mod = module; |
| 2006 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2007 | while (1) { |
| 2008 | sibling = NULL; |
Michal Vasko | 8d26e5c | 2016-09-08 10:03:49 +0200 | [diff] [blame] | 2009 | while ((sibling = lys_getnext(sibling, lys_parent(start), module, |
| 2010 | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT))) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2011 | /* name match */ |
Michal Vasko | 0a1aaa4 | 2016-04-19 09:48:25 +0200 | [diff] [blame] | 2012 | if (sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len]) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2013 | /* module check */ |
| 2014 | if (mod_name) { |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2015 | if (mod_name_len > LY_BUF_SIZE - 1) { |
Michal Vasko | 8757e7c | 2016-03-15 10:41:30 +0100 | [diff] [blame] | 2016 | LOGINT; |
| 2017 | return NULL; |
| 2018 | } |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2019 | |
| 2020 | if (ly_buf_used && module_name[0]) { |
| 2021 | buf_backup = strndup(module_name, LY_BUF_SIZE - 1); |
| 2022 | } |
| 2023 | ly_buf_used++; |
| 2024 | |
Michal Vasko | df3f1ab | 2016-04-01 15:07:22 +0200 | [diff] [blame] | 2025 | memmove(module_name, mod_name, mod_name_len); |
Michal Vasko | 8757e7c | 2016-03-15 10:41:30 +0100 | [diff] [blame] | 2026 | module_name[mod_name_len] = '\0'; |
| 2027 | /* will also find an augment module */ |
| 2028 | prefix_mod = ly_ctx_get_module(ctx, module_name, NULL); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2029 | |
| 2030 | if (buf_backup) { |
| 2031 | /* return previous internal buffer content */ |
Michal Vasko | 888a129 | 2016-04-05 12:08:54 +0200 | [diff] [blame] | 2032 | strncpy(module_name, buf_backup, LY_BUF_SIZE - 1); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2033 | free(buf_backup); |
| 2034 | buf_backup = NULL; |
| 2035 | } |
| 2036 | ly_buf_used--; |
| 2037 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2038 | if (!prefix_mod) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2039 | str = strndup(nodeid, (mod_name + mod_name_len) - nodeid); |
| 2040 | LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str); |
| 2041 | free(str); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2042 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2043 | } |
| 2044 | } else { |
| 2045 | prefix_mod = prev_mod; |
| 2046 | } |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 2047 | if (prefix_mod != lys_node_module(sibling)) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2048 | continue; |
| 2049 | } |
| 2050 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2051 | /* do we have some predicates on it? */ |
| 2052 | if (has_predicate) { |
| 2053 | r = 0; |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 2054 | if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 2055 | if ((r = parse_schema_json_predicate(id, NULL, NULL, NULL, NULL, &has_predicate)) < 1) { |
| 2056 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
| 2057 | return NULL; |
| 2058 | } |
| 2059 | } else if (sibling->nodetype == LYS_LIST) { |
| 2060 | if (resolve_json_schema_list_predicate(id, (const struct lys_node_list *)sibling, &r)) { |
| 2061 | return NULL; |
| 2062 | } |
| 2063 | } else { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2064 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2065 | return NULL; |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2066 | } |
| 2067 | id += r; |
| 2068 | } |
| 2069 | |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 2070 | /* check for shorthand cases - then 'start' does not change */ |
Michal Vasko | 8d26e5c | 2016-09-08 10:03:49 +0200 | [diff] [blame] | 2071 | if (lys_parent(sibling) && (lys_parent(sibling)->nodetype == LYS_CHOICE) && (sibling->nodetype != LYS_CASE)) { |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 2072 | shorthand = ~shorthand; |
| 2073 | } |
| 2074 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2075 | /* the result node? */ |
| 2076 | if (!id[0]) { |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 2077 | if (shorthand) { |
| 2078 | /* wrong path for shorthand */ |
Michal Vasko | 025e045 | 2016-05-17 16:14:20 +0200 | [diff] [blame] | 2079 | str = strndup(nodeid, (name + nam_len) - nodeid); |
| 2080 | LOGVAL(LYE_PATH_INNODE, LY_VLOG_STR, str); |
Michal Vasko | 3c0f9f5 | 2016-05-17 16:38:10 +0200 | [diff] [blame] | 2081 | LOGVAL(LYE_SPEC, LY_VLOG_STR, str, "Schema shorthand case path must include the virtual case statement."); |
Radek Krejci | 9a5fccc | 2016-05-18 15:44:58 +0200 | [diff] [blame] | 2082 | free(str); |
Michal Vasko | 025e045 | 2016-05-17 16:14:20 +0200 | [diff] [blame] | 2083 | return NULL; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 2084 | } |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2085 | return sibling; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2086 | } |
| 2087 | |
Michal Vasko | 8d26e5c | 2016-09-08 10:03:49 +0200 | [diff] [blame] | 2088 | if (!shorthand) { |
Michal Vasko | 7dc71d0 | 2016-03-15 10:42:28 +0100 | [diff] [blame] | 2089 | /* move down the tree, if possible */ |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 2090 | if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2091 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
Michal Vasko | 7dc71d0 | 2016-03-15 10:42:28 +0100 | [diff] [blame] | 2092 | return NULL; |
| 2093 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2094 | start = sibling->child; |
| 2095 | } |
| 2096 | |
| 2097 | /* update prev mod */ |
| 2098 | prev_mod = start->module; |
| 2099 | break; |
| 2100 | } |
| 2101 | } |
| 2102 | |
| 2103 | /* no match */ |
| 2104 | if (!sibling) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2105 | str = strndup(nodeid, (name + nam_len) - nodeid); |
| 2106 | LOGVAL(LYE_PATH_INNODE, LY_VLOG_STR, str); |
| 2107 | free(str); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2108 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2109 | } |
| 2110 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2111 | 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] | 2112 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2113 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2114 | } |
| 2115 | id += r; |
| 2116 | } |
| 2117 | |
| 2118 | /* cannot get here */ |
| 2119 | LOGINT; |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2120 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2121 | } |
| 2122 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2123 | static int |
| 2124 | resolve_partial_json_data_list_predicate(const char *predicate, const char *node_name, struct lyd_node *node, int *parsed) |
| 2125 | { |
| 2126 | const char *name, *value; |
| 2127 | int nam_len, val_len, has_predicate = 1, r; |
| 2128 | uint16_t i; |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2129 | struct lyd_node_leaf_list *key; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2130 | |
Radek Krejci | 61a86c6 | 2016-03-24 11:06:44 +0100 | [diff] [blame] | 2131 | assert(node); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2132 | assert(node->schema->nodetype == LYS_LIST); |
| 2133 | |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2134 | key = (struct lyd_node_leaf_list *)node->child; |
| 2135 | for (i = 0; i < ((struct lys_node_list *)node->schema)->keys_size; ++i) { |
| 2136 | if (!key) { |
| 2137 | /* invalid data */ |
| 2138 | LOGINT; |
| 2139 | return -1; |
| 2140 | } |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2141 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2142 | if (!has_predicate) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2143 | LOGVAL(LYE_PATH_MISSKEY, LY_VLOG_NONE, NULL, node_name); |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2144 | return -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2145 | } |
| 2146 | |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 2147 | if (((r = parse_schema_json_predicate(predicate, &name, &nam_len, &value, &val_len, &has_predicate)) < 1) |
| 2148 | || !strncmp(name, ".", nam_len)) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2149 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-r], &predicate[-r]); |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2150 | return -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2151 | } |
| 2152 | |
| 2153 | predicate += r; |
| 2154 | *parsed += r; |
| 2155 | |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2156 | if (strncmp(key->schema->name, name, nam_len) || key->schema->name[nam_len]) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2157 | LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name); |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2158 | return -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2159 | } |
| 2160 | |
| 2161 | /* value does not match */ |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2162 | if (strncmp(key->value_str, value, val_len) || key->value_str[val_len]) { |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2163 | return 1; |
| 2164 | } |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2165 | |
| 2166 | key = (struct lyd_node_leaf_list *)key->next; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2167 | } |
| 2168 | |
| 2169 | if (has_predicate) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2170 | LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2171 | return -1; |
| 2172 | } |
| 2173 | |
| 2174 | return 0; |
| 2175 | } |
| 2176 | |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 2177 | /** |
| 2178 | * @brief get the closest parent of the node (or the node itself) identified by the nodeid (path) |
| 2179 | * |
| 2180 | * @param[in] nodeid Node data path to find |
| 2181 | * @param[in] llist_value If the \p nodeid identifies leaf-list, this is expected value of the leaf-list instance. |
| 2182 | * @param[in] options Bitmask of options flags, see @ref pathoptions. |
| 2183 | * @param[out] parsed Number of characters processed in \p id |
| 2184 | * @return The closes parent (or the node itself) from the path |
| 2185 | */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2186 | struct lyd_node * |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2187 | resolve_partial_json_data_nodeid(const char *nodeid, const char *llist_value, struct lyd_node *start, int options, |
| 2188 | int *parsed) |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2189 | { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2190 | char *module_name = ly_buf(), *buf_backup = NULL, *str; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2191 | const char *id, *mod_name, *name; |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame^] | 2192 | int r, ret, mod_name_len, nam_len, is_relative = -1, has_predicate, last_parsed, val_len; |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2193 | struct lyd_node *sibling, *last_match = NULL; |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2194 | struct lyd_node_leaf_list *llist; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2195 | const struct lys_module *prefix_mod, *prev_mod; |
| 2196 | struct ly_ctx *ctx; |
| 2197 | |
| 2198 | assert(nodeid && start && parsed); |
| 2199 | |
| 2200 | ctx = start->schema->module->ctx; |
| 2201 | id = nodeid; |
| 2202 | |
| 2203 | 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] | 2204 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2205 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2206 | return NULL; |
| 2207 | } |
| 2208 | id += r; |
| 2209 | /* add it to parsed only after the data node was actually found */ |
| 2210 | last_parsed = r; |
| 2211 | |
| 2212 | if (is_relative) { |
| 2213 | prev_mod = start->schema->module; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2214 | start = start->child; |
| 2215 | } else { |
| 2216 | for (; start->parent; start = start->parent); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2217 | prev_mod = start->schema->module; |
| 2218 | } |
| 2219 | |
| 2220 | while (1) { |
| 2221 | LY_TREE_FOR(start, sibling) { |
Michal Vasko | 2411b94 | 2016-03-23 13:50:03 +0100 | [diff] [blame] | 2222 | /* RPC data check, return simply invalid argument, because the data tree is invalid */ |
| 2223 | if (lys_parent(sibling->schema)) { |
| 2224 | if (options & LYD_PATH_OPT_OUTPUT) { |
| 2225 | if (lys_parent(sibling->schema)->nodetype == LYS_INPUT) { |
Michal Vasko | b724689 | 2016-04-19 10:37:35 +0200 | [diff] [blame] | 2226 | LOGERR(LY_EINVAL, "Provided data tree includes some RPC input nodes (%s).", sibling->schema->name); |
Michal Vasko | 2411b94 | 2016-03-23 13:50:03 +0100 | [diff] [blame] | 2227 | *parsed = -1; |
| 2228 | return NULL; |
| 2229 | } |
| 2230 | } else { |
| 2231 | if (lys_parent(sibling->schema)->nodetype == LYS_OUTPUT) { |
Michal Vasko | b724689 | 2016-04-19 10:37:35 +0200 | [diff] [blame] | 2232 | LOGERR(LY_EINVAL, "Provided data tree includes some RPC output nodes (%s).", sibling->schema->name); |
Michal Vasko | 2411b94 | 2016-03-23 13:50:03 +0100 | [diff] [blame] | 2233 | *parsed = -1; |
| 2234 | return NULL; |
| 2235 | } |
| 2236 | } |
| 2237 | } |
| 2238 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2239 | /* name match */ |
| 2240 | if (!strncmp(name, sibling->schema->name, nam_len) && !sibling->schema->name[nam_len]) { |
| 2241 | |
| 2242 | /* module check */ |
| 2243 | if (mod_name) { |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2244 | if (mod_name_len > LY_BUF_SIZE - 1) { |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2245 | LOGINT; |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2246 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2247 | return NULL; |
| 2248 | } |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2249 | |
| 2250 | if (ly_buf_used && module_name[0]) { |
| 2251 | buf_backup = strndup(module_name, LY_BUF_SIZE - 1); |
| 2252 | } |
| 2253 | ly_buf_used++; |
| 2254 | |
Michal Vasko | df3f1ab | 2016-04-01 15:07:22 +0200 | [diff] [blame] | 2255 | memmove(module_name, mod_name, mod_name_len); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2256 | module_name[mod_name_len] = '\0'; |
| 2257 | /* will also find an augment module */ |
| 2258 | prefix_mod = ly_ctx_get_module(ctx, module_name, NULL); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2259 | |
| 2260 | if (buf_backup) { |
| 2261 | /* return previous internal buffer content */ |
Michal Vasko | 888a129 | 2016-04-05 12:08:54 +0200 | [diff] [blame] | 2262 | strncpy(module_name, buf_backup, LY_BUF_SIZE - 1); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2263 | free(buf_backup); |
| 2264 | buf_backup = NULL; |
| 2265 | } |
| 2266 | ly_buf_used--; |
| 2267 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2268 | if (!prefix_mod) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2269 | str = strndup(nodeid, (mod_name + mod_name_len) - nodeid); |
| 2270 | LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str); |
| 2271 | free(str); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2272 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2273 | return NULL; |
| 2274 | } |
| 2275 | } else { |
| 2276 | prefix_mod = prev_mod; |
| 2277 | } |
| 2278 | if (prefix_mod != lys_node_module(sibling->schema)) { |
| 2279 | continue; |
| 2280 | } |
| 2281 | |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2282 | /* leaf-list, did we find it with the correct value or not? */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2283 | if (sibling->schema->nodetype == LYS_LEAFLIST) { |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame^] | 2284 | if (has_predicate) { |
| 2285 | if ((r = parse_schema_json_predicate(id, &name, &nam_len, &llist_value, &val_len, &has_predicate)) < 1) { |
| 2286 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
| 2287 | *parsed = -1; |
| 2288 | return NULL; |
| 2289 | } |
| 2290 | } else { |
| 2291 | r = 0; |
| 2292 | if (llist_value) { |
| 2293 | val_len = strlen(llist_value); |
| 2294 | } else { |
| 2295 | val_len = 0; |
| 2296 | } |
| 2297 | } |
| 2298 | |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2299 | llist = (struct lyd_node_leaf_list *)sibling; |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame^] | 2300 | if ((!val_len && llist->value_str && llist->value_str[0]) |
| 2301 | || (val_len && (strncmp(llist_value, llist->value_str, val_len) || llist->value_str[val_len]))) { |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2302 | continue; |
| 2303 | } |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame^] | 2304 | id += r; |
| 2305 | last_parsed += r; |
| 2306 | |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 2307 | } else if (sibling->schema->nodetype == LYS_LIST) { |
| 2308 | /* list, we need predicates'n'stuff then */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2309 | r = 0; |
| 2310 | if (!has_predicate) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2311 | LOGVAL(LYE_PATH_MISSKEY, LY_VLOG_NONE, NULL, name); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2312 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2313 | return NULL; |
| 2314 | } |
| 2315 | ret = resolve_partial_json_data_list_predicate(id, name, sibling, &r); |
| 2316 | if (ret == -1) { |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2317 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2318 | return NULL; |
| 2319 | } else if (ret == 1) { |
| 2320 | /* this list instance does not match */ |
| 2321 | continue; |
| 2322 | } |
| 2323 | id += r; |
| 2324 | last_parsed += r; |
| 2325 | } |
| 2326 | |
| 2327 | *parsed += last_parsed; |
| 2328 | |
| 2329 | /* the result node? */ |
| 2330 | if (!id[0]) { |
| 2331 | return sibling; |
| 2332 | } |
| 2333 | |
| 2334 | /* move down the tree, if possible */ |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 2335 | if (sibling->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2336 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2337 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2338 | return NULL; |
| 2339 | } |
Michal Vasko | c6c52cf | 2016-03-29 11:53:04 +0200 | [diff] [blame] | 2340 | last_match = sibling; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2341 | start = sibling->child; |
| 2342 | if (start) { |
| 2343 | prev_mod = start->schema->module; |
| 2344 | } |
| 2345 | break; |
| 2346 | } |
| 2347 | } |
| 2348 | |
| 2349 | /* no match, return last match */ |
| 2350 | if (!sibling) { |
| 2351 | return last_match; |
| 2352 | } |
| 2353 | |
| 2354 | 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] | 2355 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2356 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2357 | return NULL; |
| 2358 | } |
| 2359 | id += r; |
| 2360 | last_parsed = r; |
| 2361 | } |
| 2362 | |
| 2363 | /* cannot get here */ |
| 2364 | LOGINT; |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2365 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2366 | return NULL; |
| 2367 | } |
| 2368 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2369 | /** |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2370 | * @brief Resolves length or range intervals. Does not log. |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2371 | * Syntax is assumed to be correct, *ret MUST be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2372 | * |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2373 | * @param[in] str_restr Restriction as a string. |
| 2374 | * @param[in] type Type of the restriction. |
| 2375 | * @param[out] ret Final interval structure that starts with |
| 2376 | * the interval of the initial type, continues with intervals |
| 2377 | * of any superior types derived from the initial one, and |
| 2378 | * finishes with intervals from our \p type. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2379 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2380 | * @return EXIT_SUCCESS on succes, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2381 | */ |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2382 | int |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2383 | resolve_len_ran_interval(const char *str_restr, struct lys_type *type, struct len_ran_intv **ret) |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2384 | { |
| 2385 | /* 0 - unsigned, 1 - signed, 2 - floating point */ |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2386 | int kind; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2387 | int64_t local_smin, local_smax, local_fmin, local_fmax; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2388 | uint64_t local_umin, local_umax; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2389 | uint8_t local_fdig; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2390 | const char *seg_ptr, *ptr; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2391 | struct len_ran_intv *local_intv = NULL, *tmp_local_intv = NULL, *tmp_intv, *intv = NULL; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2392 | |
| 2393 | switch (type->base) { |
| 2394 | case LY_TYPE_BINARY: |
| 2395 | kind = 0; |
| 2396 | local_umin = 0; |
| 2397 | local_umax = 18446744073709551615UL; |
| 2398 | |
| 2399 | if (!str_restr && type->info.binary.length) { |
| 2400 | str_restr = type->info.binary.length->expr; |
| 2401 | } |
| 2402 | break; |
| 2403 | case LY_TYPE_DEC64: |
| 2404 | kind = 2; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2405 | local_fmin = __INT64_C(-9223372036854775807) - __INT64_C(1); |
| 2406 | local_fmax = __INT64_C(9223372036854775807); |
| 2407 | local_fdig = type->info.dec64.dig; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2408 | |
| 2409 | if (!str_restr && type->info.dec64.range) { |
| 2410 | str_restr = type->info.dec64.range->expr; |
| 2411 | } |
| 2412 | break; |
| 2413 | case LY_TYPE_INT8: |
| 2414 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2415 | local_smin = __INT64_C(-128); |
| 2416 | local_smax = __INT64_C(127); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2417 | |
| 2418 | if (!str_restr && type->info.num.range) { |
| 2419 | str_restr = type->info.num.range->expr; |
| 2420 | } |
| 2421 | break; |
| 2422 | case LY_TYPE_INT16: |
| 2423 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2424 | local_smin = __INT64_C(-32768); |
| 2425 | local_smax = __INT64_C(32767); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2426 | |
| 2427 | if (!str_restr && type->info.num.range) { |
| 2428 | str_restr = type->info.num.range->expr; |
| 2429 | } |
| 2430 | break; |
| 2431 | case LY_TYPE_INT32: |
| 2432 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2433 | local_smin = __INT64_C(-2147483648); |
| 2434 | local_smax = __INT64_C(2147483647); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2435 | |
| 2436 | if (!str_restr && type->info.num.range) { |
| 2437 | str_restr = type->info.num.range->expr; |
| 2438 | } |
| 2439 | break; |
| 2440 | case LY_TYPE_INT64: |
| 2441 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2442 | local_smin = __INT64_C(-9223372036854775807) - __INT64_C(1); |
| 2443 | local_smax = __INT64_C(9223372036854775807); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2444 | |
| 2445 | if (!str_restr && type->info.num.range) { |
| 2446 | str_restr = type->info.num.range->expr; |
| 2447 | } |
| 2448 | break; |
| 2449 | case LY_TYPE_UINT8: |
| 2450 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2451 | local_umin = __UINT64_C(0); |
| 2452 | local_umax = __UINT64_C(255); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2453 | |
| 2454 | if (!str_restr && type->info.num.range) { |
| 2455 | str_restr = type->info.num.range->expr; |
| 2456 | } |
| 2457 | break; |
| 2458 | case LY_TYPE_UINT16: |
| 2459 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2460 | local_umin = __UINT64_C(0); |
| 2461 | local_umax = __UINT64_C(65535); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2462 | |
| 2463 | if (!str_restr && type->info.num.range) { |
| 2464 | str_restr = type->info.num.range->expr; |
| 2465 | } |
| 2466 | break; |
| 2467 | case LY_TYPE_UINT32: |
| 2468 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2469 | local_umin = __UINT64_C(0); |
| 2470 | local_umax = __UINT64_C(4294967295); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2471 | |
| 2472 | if (!str_restr && type->info.num.range) { |
| 2473 | str_restr = type->info.num.range->expr; |
| 2474 | } |
| 2475 | break; |
| 2476 | case LY_TYPE_UINT64: |
| 2477 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2478 | local_umin = __UINT64_C(0); |
| 2479 | local_umax = __UINT64_C(18446744073709551615); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2480 | |
| 2481 | if (!str_restr && type->info.num.range) { |
| 2482 | str_restr = type->info.num.range->expr; |
| 2483 | } |
| 2484 | break; |
| 2485 | case LY_TYPE_STRING: |
| 2486 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2487 | local_umin = __UINT64_C(0); |
| 2488 | local_umax = __UINT64_C(18446744073709551615); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2489 | |
| 2490 | if (!str_restr && type->info.str.length) { |
| 2491 | str_restr = type->info.str.length->expr; |
| 2492 | } |
| 2493 | break; |
| 2494 | default: |
| 2495 | LOGINT; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2496 | return -1; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2497 | } |
| 2498 | |
| 2499 | /* process superior types */ |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2500 | if (type->der) { |
| 2501 | if (resolve_len_ran_interval(NULL, &type->der->type, &intv)) { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2502 | LOGINT; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2503 | return -1; |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2504 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2505 | assert(!intv || (intv->kind == kind)); |
| 2506 | } |
| 2507 | |
| 2508 | if (!str_restr) { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2509 | /* we do not have any restriction, return superior ones */ |
| 2510 | *ret = intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2511 | return EXIT_SUCCESS; |
| 2512 | } |
| 2513 | |
| 2514 | /* adjust local min and max */ |
| 2515 | if (intv) { |
| 2516 | tmp_intv = intv; |
| 2517 | |
| 2518 | if (kind == 0) { |
| 2519 | local_umin = tmp_intv->value.uval.min; |
| 2520 | } else if (kind == 1) { |
| 2521 | local_smin = tmp_intv->value.sval.min; |
| 2522 | } else if (kind == 2) { |
| 2523 | local_fmin = tmp_intv->value.fval.min; |
| 2524 | } |
| 2525 | |
| 2526 | while (tmp_intv->next) { |
| 2527 | tmp_intv = tmp_intv->next; |
| 2528 | } |
| 2529 | |
| 2530 | if (kind == 0) { |
| 2531 | local_umax = tmp_intv->value.uval.max; |
| 2532 | } else if (kind == 1) { |
| 2533 | local_smax = tmp_intv->value.sval.max; |
| 2534 | } else if (kind == 2) { |
| 2535 | local_fmax = tmp_intv->value.fval.max; |
| 2536 | } |
| 2537 | } |
| 2538 | |
| 2539 | /* finally parse our restriction */ |
| 2540 | seg_ptr = str_restr; |
Pavol Vican | 9e7c01d | 2016-08-29 09:36:17 +0200 | [diff] [blame] | 2541 | tmp_intv = NULL; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2542 | while (1) { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2543 | if (!tmp_local_intv) { |
| 2544 | assert(!local_intv); |
| 2545 | local_intv = malloc(sizeof *local_intv); |
| 2546 | tmp_local_intv = local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2547 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2548 | tmp_local_intv->next = malloc(sizeof *tmp_local_intv); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2549 | tmp_local_intv = tmp_local_intv->next; |
| 2550 | } |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2551 | if (!tmp_local_intv) { |
| 2552 | LOGMEM; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2553 | goto error; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2554 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2555 | |
| 2556 | tmp_local_intv->kind = kind; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2557 | tmp_local_intv->type = type; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2558 | tmp_local_intv->next = NULL; |
| 2559 | |
| 2560 | /* min */ |
| 2561 | ptr = seg_ptr; |
| 2562 | while (isspace(ptr[0])) { |
| 2563 | ++ptr; |
| 2564 | } |
| 2565 | if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) { |
| 2566 | if (kind == 0) { |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2567 | tmp_local_intv->value.uval.min = strtol(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2568 | } else if (kind == 1) { |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2569 | tmp_local_intv->value.sval.min = strtol(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2570 | } else if (kind == 2) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 2571 | if (parse_range_dec64(&ptr, local_fdig, &tmp_local_intv->value.fval.min)) { |
| 2572 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, ptr, "range"); |
| 2573 | goto error; |
| 2574 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2575 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2576 | } else if (!strncmp(ptr, "min", 3)) { |
| 2577 | if (kind == 0) { |
| 2578 | tmp_local_intv->value.uval.min = local_umin; |
| 2579 | } else if (kind == 1) { |
| 2580 | tmp_local_intv->value.sval.min = local_smin; |
| 2581 | } else if (kind == 2) { |
| 2582 | tmp_local_intv->value.fval.min = local_fmin; |
| 2583 | } |
| 2584 | |
| 2585 | ptr += 3; |
| 2586 | } else if (!strncmp(ptr, "max", 3)) { |
| 2587 | if (kind == 0) { |
| 2588 | tmp_local_intv->value.uval.min = local_umax; |
| 2589 | } else if (kind == 1) { |
| 2590 | tmp_local_intv->value.sval.min = local_smax; |
| 2591 | } else if (kind == 2) { |
| 2592 | tmp_local_intv->value.fval.min = local_fmax; |
| 2593 | } |
| 2594 | |
| 2595 | ptr += 3; |
| 2596 | } else { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2597 | LOGINT; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2598 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2599 | } |
| 2600 | |
| 2601 | while (isspace(ptr[0])) { |
| 2602 | ptr++; |
| 2603 | } |
| 2604 | |
| 2605 | /* no interval or interval */ |
| 2606 | if ((ptr[0] == '|') || !ptr[0]) { |
| 2607 | if (kind == 0) { |
| 2608 | tmp_local_intv->value.uval.max = tmp_local_intv->value.uval.min; |
| 2609 | } else if (kind == 1) { |
| 2610 | tmp_local_intv->value.sval.max = tmp_local_intv->value.sval.min; |
| 2611 | } else if (kind == 2) { |
| 2612 | tmp_local_intv->value.fval.max = tmp_local_intv->value.fval.min; |
| 2613 | } |
| 2614 | } else if (!strncmp(ptr, "..", 2)) { |
| 2615 | /* skip ".." */ |
| 2616 | ptr += 2; |
| 2617 | while (isspace(ptr[0])) { |
| 2618 | ++ptr; |
| 2619 | } |
| 2620 | |
| 2621 | /* max */ |
| 2622 | if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) { |
| 2623 | if (kind == 0) { |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2624 | tmp_local_intv->value.uval.max = strtol(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2625 | } else if (kind == 1) { |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2626 | tmp_local_intv->value.sval.max = strtol(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2627 | } else if (kind == 2) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 2628 | if (parse_range_dec64(&ptr, local_fdig, &tmp_local_intv->value.fval.max)) { |
| 2629 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, ptr, "range"); |
| 2630 | goto error; |
| 2631 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2632 | } |
| 2633 | } else if (!strncmp(ptr, "max", 3)) { |
| 2634 | if (kind == 0) { |
| 2635 | tmp_local_intv->value.uval.max = local_umax; |
| 2636 | } else if (kind == 1) { |
| 2637 | tmp_local_intv->value.sval.max = local_smax; |
| 2638 | } else if (kind == 2) { |
| 2639 | tmp_local_intv->value.fval.max = local_fmax; |
| 2640 | } |
| 2641 | } else { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2642 | LOGINT; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2643 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2644 | } |
| 2645 | } else { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2646 | LOGINT; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2647 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2648 | } |
| 2649 | |
Pavol Vican | 9e7c01d | 2016-08-29 09:36:17 +0200 | [diff] [blame] | 2650 | /* check min and max in correct order*/ |
| 2651 | if (kind == 0) { |
| 2652 | /* current segment */ |
| 2653 | if (tmp_local_intv->value.uval.min > tmp_local_intv->value.uval.max) { |
| 2654 | goto error; |
| 2655 | } |
| 2656 | if (tmp_local_intv->value.uval.min < local_umin || tmp_local_intv->value.uval.max > local_umax) { |
| 2657 | goto error; |
| 2658 | } |
| 2659 | /* segments sholud be ascending order */ |
Pavol Vican | 69f62c9 | 2016-08-30 09:06:25 +0200 | [diff] [blame] | 2660 | if (tmp_intv && (tmp_intv->value.uval.max >= tmp_local_intv->value.uval.min)) { |
Pavol Vican | 9e7c01d | 2016-08-29 09:36:17 +0200 | [diff] [blame] | 2661 | goto error; |
| 2662 | } |
| 2663 | } else if (kind == 1) { |
| 2664 | if (tmp_local_intv->value.sval.min > tmp_local_intv->value.sval.max) { |
| 2665 | goto error; |
| 2666 | } |
| 2667 | if (tmp_local_intv->value.sval.min < local_smin || tmp_local_intv->value.sval.max > local_smax) { |
| 2668 | goto error; |
| 2669 | } |
Pavol Vican | 69f62c9 | 2016-08-30 09:06:25 +0200 | [diff] [blame] | 2670 | if (tmp_intv && (tmp_intv->value.sval.max >= tmp_local_intv->value.sval.min)) { |
Pavol Vican | 9e7c01d | 2016-08-29 09:36:17 +0200 | [diff] [blame] | 2671 | goto error; |
| 2672 | } |
| 2673 | } else if (kind == 2) { |
| 2674 | if (tmp_local_intv->value.fval.min > tmp_local_intv->value.fval.max) { |
| 2675 | goto error; |
| 2676 | } |
| 2677 | if (tmp_local_intv->value.fval.min < local_fmin || tmp_local_intv->value.fval.max > local_fmax) { |
| 2678 | goto error; |
| 2679 | } |
Pavol Vican | 69f62c9 | 2016-08-30 09:06:25 +0200 | [diff] [blame] | 2680 | if (tmp_intv && (tmp_intv->value.fval.max >= tmp_local_intv->value.fval.min)) { |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2681 | /* fraction-digits value is always the same (it cannot be changed in derived types) */ |
Pavol Vican | 9e7c01d | 2016-08-29 09:36:17 +0200 | [diff] [blame] | 2682 | goto error; |
| 2683 | } |
| 2684 | } |
| 2685 | |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2686 | /* next segment (next OR) */ |
| 2687 | seg_ptr = strchr(seg_ptr, '|'); |
| 2688 | if (!seg_ptr) { |
| 2689 | break; |
| 2690 | } |
| 2691 | seg_ptr++; |
Pavol Vican | 9e7c01d | 2016-08-29 09:36:17 +0200 | [diff] [blame] | 2692 | tmp_intv = tmp_local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2693 | } |
| 2694 | |
| 2695 | /* check local restrictions against superior ones */ |
| 2696 | if (intv) { |
| 2697 | tmp_intv = intv; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2698 | tmp_local_intv = local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2699 | |
| 2700 | while (tmp_local_intv && tmp_intv) { |
| 2701 | /* reuse local variables */ |
| 2702 | if (kind == 0) { |
| 2703 | local_umin = tmp_local_intv->value.uval.min; |
| 2704 | local_umax = tmp_local_intv->value.uval.max; |
| 2705 | |
| 2706 | /* it must be in this interval */ |
| 2707 | if ((local_umin >= tmp_intv->value.uval.min) && (local_umin <= tmp_intv->value.uval.max)) { |
| 2708 | /* this interval is covered, next one */ |
| 2709 | if (local_umax <= tmp_intv->value.uval.max) { |
| 2710 | tmp_local_intv = tmp_local_intv->next; |
| 2711 | continue; |
| 2712 | /* ascending order of restrictions -> fail */ |
| 2713 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2714 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2715 | } |
| 2716 | } |
| 2717 | } else if (kind == 1) { |
| 2718 | local_smin = tmp_local_intv->value.sval.min; |
| 2719 | local_smax = tmp_local_intv->value.sval.max; |
| 2720 | |
| 2721 | if ((local_smin >= tmp_intv->value.sval.min) && (local_smin <= tmp_intv->value.sval.max)) { |
| 2722 | if (local_smax <= tmp_intv->value.sval.max) { |
| 2723 | tmp_local_intv = tmp_local_intv->next; |
| 2724 | continue; |
| 2725 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2726 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2727 | } |
| 2728 | } |
| 2729 | } else if (kind == 2) { |
| 2730 | local_fmin = tmp_local_intv->value.fval.min; |
| 2731 | local_fmax = tmp_local_intv->value.fval.max; |
| 2732 | |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2733 | if ((dec64cmp(local_fmin, local_fdig, tmp_intv->value.fval.min, local_fdig) > -1) |
Pavol Vican | 3c8ee2b | 2016-09-29 13:18:13 +0200 | [diff] [blame] | 2734 | && (dec64cmp(local_fmin, local_fdig, tmp_intv->value.fval.max, local_fdig) < 1)) { |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2735 | if (dec64cmp(local_fmax, local_fdig, tmp_intv->value.fval.max, local_fdig) < 1) { |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2736 | tmp_local_intv = tmp_local_intv->next; |
| 2737 | continue; |
| 2738 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2739 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2740 | } |
| 2741 | } |
| 2742 | } |
| 2743 | |
| 2744 | tmp_intv = tmp_intv->next; |
| 2745 | } |
| 2746 | |
| 2747 | /* some interval left uncovered -> fail */ |
| 2748 | if (tmp_local_intv) { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2749 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2750 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2751 | } |
| 2752 | |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2753 | /* append the local intervals to all the intervals of the superior types, return it all */ |
| 2754 | if (intv) { |
| 2755 | for (tmp_intv = intv; tmp_intv->next; tmp_intv = tmp_intv->next); |
| 2756 | tmp_intv->next = local_intv; |
| 2757 | } else { |
| 2758 | intv = local_intv; |
| 2759 | } |
| 2760 | *ret = intv; |
| 2761 | |
| 2762 | return EXIT_SUCCESS; |
| 2763 | |
| 2764 | error: |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2765 | while (intv) { |
| 2766 | tmp_intv = intv->next; |
| 2767 | free(intv); |
| 2768 | intv = tmp_intv; |
| 2769 | } |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2770 | while (local_intv) { |
| 2771 | tmp_local_intv = local_intv->next; |
| 2772 | free(local_intv); |
| 2773 | local_intv = tmp_local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2774 | } |
| 2775 | |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2776 | return -1; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2777 | } |
| 2778 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2779 | /** |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2780 | * @brief Resolve a typedef, return only resolved typedefs if derived. If leafref, it must be |
| 2781 | * resolved for this function to return it. Does not log. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2782 | * |
| 2783 | * @param[in] name Typedef name. |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2784 | * @param[in] mod_name Typedef name module name. |
| 2785 | * @param[in] module Main module. |
| 2786 | * @param[in] parent Parent of the resolved type definition. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2787 | * @param[out] ret Pointer to the resolved typedef. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2788 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2789 | * @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] | 2790 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2791 | int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 2792 | resolve_superior_type(const char *name, const char *mod_name, const struct lys_module *module, |
| 2793 | const struct lys_node *parent, struct lys_tpdf **ret) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2794 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2795 | int i, j; |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2796 | struct lys_tpdf *tpdf, *match; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2797 | int tpdf_size; |
| 2798 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2799 | if (!mod_name) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2800 | /* no prefix, try built-in types */ |
| 2801 | for (i = 1; i < LY_DATA_TYPE_COUNT; i++) { |
| 2802 | if (!strcmp(ly_types[i].def->name, name)) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2803 | if (ret) { |
| 2804 | *ret = ly_types[i].def; |
| 2805 | } |
| 2806 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2807 | } |
| 2808 | } |
| 2809 | } else { |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2810 | if (!strcmp(mod_name, module->name)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2811 | /* prefix refers to the current module, ignore it */ |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2812 | mod_name = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2813 | } |
| 2814 | } |
| 2815 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2816 | if (!mod_name && parent) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2817 | /* search in local typedefs */ |
| 2818 | while (parent) { |
| 2819 | switch (parent->nodetype) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2820 | case LYS_CONTAINER: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 2821 | tpdf_size = ((struct lys_node_container *)parent)->tpdf_size; |
| 2822 | tpdf = ((struct lys_node_container *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2823 | break; |
| 2824 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2825 | case LYS_LIST: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 2826 | tpdf_size = ((struct lys_node_list *)parent)->tpdf_size; |
| 2827 | tpdf = ((struct lys_node_list *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2828 | break; |
| 2829 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2830 | case LYS_GROUPING: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 2831 | tpdf_size = ((struct lys_node_grp *)parent)->tpdf_size; |
| 2832 | tpdf = ((struct lys_node_grp *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2833 | break; |
| 2834 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2835 | case LYS_RPC: |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 2836 | case LYS_ACTION: |
| 2837 | tpdf_size = ((struct lys_node_rpc_action *)parent)->tpdf_size; |
| 2838 | tpdf = ((struct lys_node_rpc_action *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2839 | break; |
| 2840 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2841 | case LYS_NOTIF: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 2842 | tpdf_size = ((struct lys_node_notif *)parent)->tpdf_size; |
| 2843 | tpdf = ((struct lys_node_notif *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2844 | break; |
| 2845 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2846 | case LYS_INPUT: |
| 2847 | case LYS_OUTPUT: |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 2848 | tpdf_size = ((struct lys_node_inout *)parent)->tpdf_size; |
| 2849 | tpdf = ((struct lys_node_inout *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2850 | break; |
| 2851 | |
| 2852 | default: |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 2853 | parent = lys_parent(parent); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2854 | continue; |
| 2855 | } |
| 2856 | |
| 2857 | for (i = 0; i < tpdf_size; i++) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 2858 | if (!strcmp(tpdf[i].name, name) && tpdf[i].type.base > 0) { |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2859 | match = &tpdf[i]; |
| 2860 | goto check_leafref; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2861 | } |
| 2862 | } |
| 2863 | |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 2864 | parent = lys_parent(parent); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2865 | } |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2866 | } else { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2867 | /* get module where to search */ |
Michal Vasko | b6729c6 | 2015-10-21 12:09:47 +0200 | [diff] [blame] | 2868 | module = lys_get_import_module(module, NULL, 0, mod_name, 0); |
Michal Vasko | c935fff | 2015-08-17 14:02:06 +0200 | [diff] [blame] | 2869 | if (!module) { |
| 2870 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2871 | } |
| 2872 | } |
| 2873 | |
| 2874 | /* search in top level typedefs */ |
| 2875 | for (i = 0; i < module->tpdf_size; i++) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 2876 | if (!strcmp(module->tpdf[i].name, name) && module->tpdf[i].type.base > 0) { |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2877 | match = &module->tpdf[i]; |
| 2878 | goto check_leafref; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2879 | } |
| 2880 | } |
| 2881 | |
| 2882 | /* search in submodules */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2883 | for (i = 0; i < module->inc_size && module->inc[i].submodule; i++) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2884 | for (j = 0; j < module->inc[i].submodule->tpdf_size; j++) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 2885 | if (!strcmp(module->inc[i].submodule->tpdf[j].name, name) && module->inc[i].submodule->tpdf[j].type.base > 0) { |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2886 | match = &module->inc[i].submodule->tpdf[j]; |
| 2887 | goto check_leafref; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2888 | } |
| 2889 | } |
| 2890 | } |
| 2891 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2892 | return EXIT_FAILURE; |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2893 | |
| 2894 | check_leafref: |
| 2895 | if (ret) { |
| 2896 | *ret = match; |
| 2897 | } |
| 2898 | if (match->type.base == LY_TYPE_LEAFREF) { |
| 2899 | while (!match->type.info.lref.path) { |
| 2900 | match = match->type.der; |
| 2901 | assert(match); |
| 2902 | } |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2903 | } |
| 2904 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2905 | } |
| 2906 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2907 | /** |
| 2908 | * @brief Check the default \p value of the \p type. Logs directly. |
| 2909 | * |
| 2910 | * @param[in] type Type definition to use. |
| 2911 | * @param[in] value Default value to check. |
Michal Vasko | 5682640 | 2016-03-02 11:11:37 +0100 | [diff] [blame] | 2912 | * @param[in] module Type module. |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2913 | * |
| 2914 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 2915 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2916 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2917 | 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] | 2918 | { |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 2919 | struct lys_tpdf *base_tpdf = NULL; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2920 | struct lyd_node_leaf_list node; |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 2921 | int ret = EXIT_SUCCESS; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2922 | |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 2923 | if (type->base <= LY_TYPE_DER) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 2924 | /* the type was not resolved yet, nothing to do for now */ |
| 2925 | return EXIT_FAILURE; |
| 2926 | } |
| 2927 | |
| 2928 | if (!value) { |
| 2929 | /* we do not have a new default value, so is there any to check even, in some base type? */ |
| 2930 | for (base_tpdf = type->der; base_tpdf->type.der; base_tpdf = base_tpdf->type.der) { |
| 2931 | if (base_tpdf->dflt) { |
| 2932 | value = base_tpdf->dflt; |
| 2933 | break; |
| 2934 | } |
| 2935 | } |
| 2936 | |
| 2937 | if (!value) { |
| 2938 | /* no default value, nothing to check, all is well */ |
| 2939 | return EXIT_SUCCESS; |
| 2940 | } |
| 2941 | |
| 2942 | /* so there is a default value in a base type, but can the default value be no longer valid (did we define some new restrictions)? */ |
| 2943 | switch (type->base) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 2944 | case LY_TYPE_IDENT: |
| 2945 | case LY_TYPE_INST: |
| 2946 | case LY_TYPE_LEAFREF: |
| 2947 | case LY_TYPE_BOOL: |
| 2948 | case LY_TYPE_EMPTY: |
| 2949 | /* these have no restrictions, so we would do the exact same work as the unres in the base typedef */ |
| 2950 | return EXIT_SUCCESS; |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 2951 | case LY_TYPE_BITS: |
| 2952 | /* the default value must match the restricted list of values, if the type was restricted */ |
| 2953 | if (type->info.bits.count) { |
| 2954 | break; |
| 2955 | } |
| 2956 | return EXIT_SUCCESS; |
| 2957 | case LY_TYPE_ENUM: |
| 2958 | /* the default value must match the restricted list of values, if the type was restricted */ |
| 2959 | if (type->info.enums.count) { |
| 2960 | break; |
| 2961 | } |
| 2962 | return EXIT_SUCCESS; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 2963 | case LY_TYPE_DEC64: |
| 2964 | if (type->info.dec64.range) { |
| 2965 | break; |
| 2966 | } |
| 2967 | return EXIT_SUCCESS; |
| 2968 | case LY_TYPE_BINARY: |
| 2969 | if (type->info.binary.length) { |
| 2970 | break; |
| 2971 | } |
| 2972 | return EXIT_SUCCESS; |
| 2973 | case LY_TYPE_INT8: |
| 2974 | case LY_TYPE_INT16: |
| 2975 | case LY_TYPE_INT32: |
| 2976 | case LY_TYPE_INT64: |
| 2977 | case LY_TYPE_UINT8: |
| 2978 | case LY_TYPE_UINT16: |
| 2979 | case LY_TYPE_UINT32: |
| 2980 | case LY_TYPE_UINT64: |
| 2981 | if (type->info.num.range) { |
| 2982 | break; |
| 2983 | } |
| 2984 | return EXIT_SUCCESS; |
| 2985 | case LY_TYPE_STRING: |
| 2986 | if (type->info.str.length || type->info.str.patterns) { |
| 2987 | break; |
| 2988 | } |
| 2989 | return EXIT_SUCCESS; |
| 2990 | case LY_TYPE_UNION: |
| 2991 | /* way too much trouble learning whether we need to check the default again, so just do it */ |
| 2992 | break; |
| 2993 | default: |
| 2994 | LOGINT; |
| 2995 | return -1; |
| 2996 | } |
Radek Krejci | 55a161c | 2016-09-05 17:13:25 +0200 | [diff] [blame] | 2997 | } else if (type->base == LY_TYPE_EMPTY) { |
| 2998 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "default", type->parent->name); |
| 2999 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The \"empty\" data type cannot have a default value."); |
| 3000 | return -1; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3001 | } |
| 3002 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3003 | /* dummy leaf */ |
Radek Krejci | 4fe36bd | 2016-03-17 16:47:16 +0100 | [diff] [blame] | 3004 | memset(&node, 0, sizeof node); |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3005 | node.value_str = value; |
| 3006 | node.value_type = type->base; |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 3007 | node.schema = calloc(1, sizeof (struct lys_node_leaf)); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3008 | if (!node.schema) { |
| 3009 | LOGMEM; |
| 3010 | return -1; |
| 3011 | } |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3012 | node.schema->name = strdup("fake-default"); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3013 | if (!node.schema->name) { |
| 3014 | LOGMEM; |
Michal Vasko | f49eda0 | 2016-07-21 12:17:01 +0200 | [diff] [blame] | 3015 | free(node.schema); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3016 | return -1; |
| 3017 | } |
Michal Vasko | 5682640 | 2016-03-02 11:11:37 +0100 | [diff] [blame] | 3018 | node.schema->module = module; |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 3019 | memcpy(&((struct lys_node_leaf *)node.schema)->type, type, sizeof *type); |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3020 | |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 3021 | if (type->base == LY_TYPE_LEAFREF) { |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3022 | if (!type->info.lref.target) { |
| 3023 | ret = EXIT_FAILURE; |
| 3024 | goto finish; |
| 3025 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3026 | ret = check_default(&type->info.lref.target->type, value, module); |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3027 | |
| 3028 | } else if ((type->base == LY_TYPE_INST) || (type->base == LY_TYPE_IDENT)) { |
| 3029 | /* it was converted to JSON format before, nothing else sensible we can do */ |
| 3030 | |
| 3031 | } else { |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 3032 | if (lyp_parse_value(&node, NULL, 1)) { |
| 3033 | ret = -1; |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3034 | if (base_tpdf) { |
| 3035 | /* default value was is defined in some base typedef */ |
| 3036 | if ((type->base == LY_TYPE_BITS && type->der->type.der) || |
| 3037 | (type->base == LY_TYPE_ENUM && type->der->type.der)) { |
| 3038 | /* we have refined bits/enums */ |
| 3039 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, |
| 3040 | "Invalid value \"%s\" of the default statement inherited to \"%s\" from \"%s\" base type.", |
| 3041 | value, type->parent->name, base_tpdf->name); |
| 3042 | } |
| 3043 | } |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 3044 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3045 | } |
| 3046 | |
| 3047 | finish: |
| 3048 | if (node.value_type == LY_TYPE_BITS) { |
| 3049 | free(node.value.bit); |
| 3050 | } |
| 3051 | free((char *)node.schema->name); |
| 3052 | free(node.schema); |
| 3053 | |
| 3054 | return ret; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3055 | } |
| 3056 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3057 | /** |
| 3058 | * @brief Check a key for mandatory attributes. Logs directly. |
| 3059 | * |
| 3060 | * @param[in] key The key to check. |
| 3061 | * @param[in] flags What flags to check. |
| 3062 | * @param[in] list The list of all the keys. |
| 3063 | * @param[in] index Index of the key in the key list. |
| 3064 | * @param[in] name The name of the keys. |
| 3065 | * @param[in] len The name length. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3066 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3067 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3068 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3069 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3070 | 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] | 3071 | { |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3072 | struct lys_node_leaf *key = list->keys[index]; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3073 | char *dup = NULL; |
| 3074 | int j; |
| 3075 | |
| 3076 | /* existence */ |
| 3077 | if (!key) { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3078 | if (name[len] != '\0') { |
| 3079 | dup = strdup(name); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3080 | if (!dup) { |
| 3081 | LOGMEM; |
| 3082 | return -1; |
| 3083 | } |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3084 | dup[len] = '\0'; |
| 3085 | name = dup; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3086 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3087 | LOGVAL(LYE_KEY_MISS, LY_VLOG_LYS, list, name); |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 3088 | free(dup); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3089 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3090 | } |
| 3091 | |
| 3092 | /* uniqueness */ |
| 3093 | for (j = index - 1; j >= 0; j--) { |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3094 | if (key == list->keys[j]) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3095 | LOGVAL(LYE_KEY_DUP, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3096 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3097 | } |
| 3098 | } |
| 3099 | |
| 3100 | /* key is a leaf */ |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3101 | if (key->nodetype != LYS_LEAF) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3102 | LOGVAL(LYE_KEY_NLEAF, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3103 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3104 | } |
| 3105 | |
| 3106 | /* type of the leaf is not built-in empty */ |
Radek Krejci | c3738db | 2016-09-15 15:51:21 +0200 | [diff] [blame] | 3107 | if (key->type.base == LY_TYPE_EMPTY && key->module->version < 2) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3108 | LOGVAL(LYE_KEY_TYPE, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3109 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3110 | } |
| 3111 | |
| 3112 | /* config attribute is the same as of the list */ |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3113 | if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3114 | LOGVAL(LYE_KEY_CONFIG, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3115 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3116 | } |
| 3117 | |
Radek Krejci | 55e2cdc | 2016-03-11 13:51:09 +0100 | [diff] [blame] | 3118 | /* key is not placed from augment */ |
| 3119 | if (key->parent->nodetype == LYS_AUGMENT) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3120 | LOGVAL(LYE_KEY_MISS, LY_VLOG_LYS, key, key->name); |
| 3121 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, key, "Key inserted from augment."); |
Radek Krejci | 55e2cdc | 2016-03-11 13:51:09 +0100 | [diff] [blame] | 3122 | return -1; |
| 3123 | } |
| 3124 | |
Radek Krejci | 3f21ada | 2016-08-01 13:34:31 +0200 | [diff] [blame] | 3125 | /* key is not when/if-feature -conditional */ |
| 3126 | j = 0; |
| 3127 | if (key->when || (key->iffeature_size && (j = 1))) { |
| 3128 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, key, j ? "if-feature" : "when", "leaf"); |
| 3129 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, key, "Key definition cannot depend on a \"%s\" condition.", |
| 3130 | j ? "if-feature" : "when"); |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3131 | return -1; |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3132 | } |
| 3133 | |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3134 | return EXIT_SUCCESS; |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3135 | } |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3136 | |
| 3137 | /** |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3138 | * @brief Resolve (test the target exists) unique. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3139 | * |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3140 | * @param[in] parent The parent node of the unique structure. |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3141 | * @param[in] uniq_str_path One path from the unique string. |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3142 | * |
| 3143 | * @return EXIT_SUCCESS on succes, EXIT_FAILURE on forward reference, -1 on error. |
| 3144 | */ |
| 3145 | int |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3146 | resolve_unique(struct lys_node *parent, const char *uniq_str_path, uint8_t *trg_type) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3147 | { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3148 | int rc; |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3149 | const struct lys_node *leaf = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3150 | |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 3151 | rc = resolve_descendant_schema_nodeid(uniq_str_path, parent->child, LYS_LEAF, 1, 1, &leaf); |
Michal Vasko | 9bb061b | 2016-02-12 11:00:19 +0100 | [diff] [blame] | 3152 | if (rc || !leaf) { |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3153 | if (rc) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3154 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3155 | if (rc > 0) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3156 | LOGVAL(LYE_INCHAR, LY_VLOG_LYS, parent, uniq_str_path[rc - 1], &uniq_str_path[rc - 1]); |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 3157 | } else if (rc == -2) { |
Michal Vasko | c66c6d8 | 2016-04-12 11:37:31 +0200 | [diff] [blame] | 3158 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, parent, "Unique argument references list."); |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3159 | } |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3160 | rc = -1; |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3161 | } else { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3162 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
| 3163 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, parent, "Target leaf not found."); |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3164 | rc = EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3165 | } |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3166 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3167 | } |
Michal Vasko | 9bb061b | 2016-02-12 11:00:19 +0100 | [diff] [blame] | 3168 | if (leaf->nodetype != LYS_LEAF) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3169 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
| 3170 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, parent, "Target is not a leaf."); |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3171 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3172 | } |
| 3173 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3174 | /* check status */ |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3175 | 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] | 3176 | return -1; |
| 3177 | } |
| 3178 | |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3179 | /* check that all unique's targets are of the same config type */ |
| 3180 | if (*trg_type) { |
| 3181 | if (((*trg_type == 1) && (leaf->flags & LYS_CONFIG_R)) || ((*trg_type == 2) && (leaf->flags & LYS_CONFIG_W))) { |
| 3182 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
| 3183 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, parent, |
| 3184 | "Leaf \"%s\" referenced in unique statement is config %s, but previous referenced leaf is config %s.", |
| 3185 | uniq_str_path, *trg_type == 1 ? "false" : "true", *trg_type == 1 ? "true" : "false"); |
| 3186 | return -1; |
| 3187 | } |
| 3188 | } else { |
| 3189 | /* first unique */ |
| 3190 | if (leaf->flags & LYS_CONFIG_W) { |
| 3191 | *trg_type = 1; |
| 3192 | } else { |
| 3193 | *trg_type = 2; |
| 3194 | } |
| 3195 | } |
| 3196 | |
Radek Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame] | 3197 | /* set leaf's unique flag */ |
| 3198 | ((struct lys_node_leaf *)leaf)->flags |= LYS_UNIQUE; |
| 3199 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3200 | return EXIT_SUCCESS; |
| 3201 | |
| 3202 | error: |
| 3203 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3204 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3205 | } |
| 3206 | |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 3207 | void |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3208 | unres_data_del(struct unres_data *unres, uint32_t i) |
| 3209 | { |
| 3210 | /* there are items after the one deleted */ |
| 3211 | if (i+1 < unres->count) { |
| 3212 | /* we only move the data, memory is left allocated, why bother */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3213 | 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] | 3214 | |
| 3215 | /* deleting the last item */ |
| 3216 | } else if (i == 0) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3217 | free(unres->node); |
| 3218 | unres->node = NULL; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3219 | } |
| 3220 | |
| 3221 | /* if there are no items after and it is not the last one, just move the counter */ |
| 3222 | --unres->count; |
| 3223 | } |
| 3224 | |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3225 | /** |
| 3226 | * @brief Resolve (find) a data node from a specific module. Does not log. |
| 3227 | * |
| 3228 | * @param[in] mod Module to search in. |
| 3229 | * @param[in] name Name of the data node. |
| 3230 | * @param[in] nam_len Length of the name. |
| 3231 | * @param[in] start Data node to start the search from. |
| 3232 | * @param[in,out] parents Resolved nodes. If there are some parents, |
| 3233 | * they are replaced (!!) with the resolvents. |
| 3234 | * |
Michal Vasko | 2471e7f | 2016-04-11 11:00:15 +0200 | [diff] [blame] | 3235 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3236 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3237 | static int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3238 | 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] | 3239 | { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3240 | struct lyd_node *node; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3241 | int flag; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3242 | uint32_t i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3243 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3244 | if (!parents->count) { |
| 3245 | parents->count = 1; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3246 | parents->node = malloc(sizeof *parents->node); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3247 | if (!parents->node) { |
| 3248 | LOGMEM; |
Michal Vasko | 2471e7f | 2016-04-11 11:00:15 +0200 | [diff] [blame] | 3249 | return -1; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3250 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3251 | parents->node[0] = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3252 | } |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3253 | for (i = 0; i < parents->count;) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 3254 | if (parents->node[i] && (parents->node[i]->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3255 | /* skip */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3256 | ++i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3257 | continue; |
| 3258 | } |
| 3259 | flag = 0; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3260 | LY_TREE_FOR(parents->node[i] ? parents->node[i]->child : start, node) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3261 | if (node->schema->module == mod && !strncmp(node->schema->name, name, nam_len) |
| 3262 | && node->schema->name[nam_len] == '\0') { |
| 3263 | /* matching target */ |
| 3264 | if (!flag) { |
Michal Vasko | 9a47e12 | 2015-09-03 14:26:32 +0200 | [diff] [blame] | 3265 | /* put node instead of the current parent */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3266 | parents->node[i] = node; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3267 | flag = 1; |
| 3268 | } else { |
Michal Vasko | 9a47e12 | 2015-09-03 14:26:32 +0200 | [diff] [blame] | 3269 | /* multiple matching, so create a new node */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3270 | ++parents->count; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3271 | parents->node = ly_realloc(parents->node, parents->count * sizeof *parents->node); |
| 3272 | if (!parents->node) { |
| 3273 | return EXIT_FAILURE; |
| 3274 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3275 | parents->node[parents->count-1] = node; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3276 | ++i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3277 | } |
| 3278 | } |
| 3279 | } |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3280 | |
| 3281 | if (!flag) { |
| 3282 | /* remove item from the parents list */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3283 | unres_data_del(parents, i); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3284 | } else { |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3285 | ++i; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3286 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3287 | } |
| 3288 | |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3289 | return parents->count ? EXIT_SUCCESS : EXIT_FAILURE; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3290 | } |
| 3291 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3292 | /** |
| 3293 | * @brief Resolve (find) a data node. Does not log. |
| 3294 | * |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3295 | * @param[in] mod_name Module name of the data node. |
| 3296 | * @param[in] mod_name_len Length of the module name. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3297 | * @param[in] name Name of the data node. |
| 3298 | * @param[in] nam_len Length of the name. |
| 3299 | * @param[in] start Data node to start the search from. |
| 3300 | * @param[in,out] parents Resolved nodes. If there are some parents, |
| 3301 | * they are replaced (!!) with the resolvents. |
| 3302 | * |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3303 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 otherwise. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3304 | */ |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3305 | static int |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3306 | 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] | 3307 | struct unres_data *parents) |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3308 | { |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3309 | const struct lys_module *mod; |
Michal Vasko | 31fc367 | 2015-10-21 12:08:13 +0200 | [diff] [blame] | 3310 | char *str; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3311 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3312 | assert(start); |
| 3313 | |
Michal Vasko | 31fc367 | 2015-10-21 12:08:13 +0200 | [diff] [blame] | 3314 | if (mod_name) { |
| 3315 | /* we have mod_name, find appropriate module */ |
| 3316 | str = strndup(mod_name, mod_name_len); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3317 | if (!str) { |
| 3318 | LOGMEM; |
| 3319 | return -1; |
| 3320 | } |
Michal Vasko | 31fc367 | 2015-10-21 12:08:13 +0200 | [diff] [blame] | 3321 | mod = ly_ctx_get_module(start->schema->module->ctx, str, NULL); |
| 3322 | free(str); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3323 | if (!mod) { |
| 3324 | /* invalid prefix */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3325 | return -1; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3326 | } |
| 3327 | } else { |
| 3328 | /* no prefix, module is the same as of current node */ |
| 3329 | mod = start->schema->module; |
| 3330 | } |
| 3331 | |
| 3332 | return resolve_data(mod, name, name_len, start, parents); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3333 | } |
| 3334 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3335 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3336 | * @brief Resolve a path predicate (leafref) in JSON data context. Logs directly |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3337 | * only specific errors, general no-resolvent error is left to the caller. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3338 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3339 | * @param[in] pred Predicate to use. |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3340 | * @param[in] node Node from which the predicate is being resolved |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3341 | * @param[in,out] node_match Nodes satisfying the restriction |
| 3342 | * without the predicate. Nodes not |
| 3343 | * satisfying the predicate are removed. |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3344 | * @param[out] parsed Number of characters parsed, negative on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3345 | * |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3346 | * @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] | 3347 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3348 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3349 | 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] | 3350 | int *parsed) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3351 | { |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3352 | /* ... /node[source = destination] ... */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3353 | struct unres_data source_match, dest_match; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3354 | const char *path_key_expr, *source, *sour_pref, *dest, *dest_pref; |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3355 | int pke_len, sour_len, sour_pref_len, dest_len, dest_pref_len, parsed_loc = 0, pke_parsed = 0; |
| 3356 | int has_predicate, dest_parent_times, i, rc; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3357 | uint32_t j; |
Radek Krejci | 0c2fa3a | 2016-06-13 15:18:03 +0200 | [diff] [blame] | 3358 | struct lyd_node_leaf_list *leaf_dst, *leaf_src; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3359 | |
| 3360 | source_match.count = 1; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3361 | source_match.node = malloc(sizeof *source_match.node); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3362 | if (!source_match.node) { |
| 3363 | LOGMEM; |
| 3364 | return -1; |
| 3365 | } |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3366 | dest_match.count = 1; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3367 | dest_match.node = malloc(sizeof *dest_match.node); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3368 | if (!dest_match.node) { |
| 3369 | LOGMEM; |
| 3370 | return -1; |
| 3371 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3372 | |
| 3373 | do { |
| 3374 | if ((i = parse_path_predicate(pred, &sour_pref, &sour_pref_len, &source, &sour_len, &path_key_expr, |
| 3375 | &pke_len, &has_predicate)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3376 | LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, pred[-i], &pred[-i]); |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3377 | rc = -1; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3378 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3379 | } |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3380 | parsed_loc += i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3381 | pred += i; |
| 3382 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3383 | for (j = 0; j < node_match->count;) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3384 | /* source */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3385 | source_match.node[0] = node_match->node[j]; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3386 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3387 | /* must be leaf (key of a list) */ |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3388 | 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] | 3389 | &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] | 3390 | i = 0; |
| 3391 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3392 | } |
| 3393 | |
| 3394 | /* destination */ |
Radek Krejci | 0c2fa3a | 2016-06-13 15:18:03 +0200 | [diff] [blame] | 3395 | dest_match.node[0] = node; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3396 | dest_parent_times = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3397 | if ((i = parse_path_key_expr(path_key_expr, &dest_pref, &dest_pref_len, &dest, &dest_len, |
| 3398 | &dest_parent_times)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3399 | 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] | 3400 | rc = -1; |
| 3401 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3402 | } |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3403 | pke_parsed = i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3404 | for (i = 0; i < dest_parent_times; ++i) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3405 | dest_match.node[0] = dest_match.node[0]->parent; |
| 3406 | if (!dest_match.node[0]) { |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3407 | i = 0; |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3408 | rc = EXIT_FAILURE; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3409 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3410 | } |
| 3411 | } |
| 3412 | while (1) { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3413 | 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] | 3414 | &dest_match)) || (dest_match.count != 1)) { |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3415 | i = 0; |
| 3416 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3417 | } |
| 3418 | |
| 3419 | if (pke_len == pke_parsed) { |
| 3420 | break; |
| 3421 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3422 | 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] | 3423 | &dest_parent_times)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3424 | 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] | 3425 | rc = -1; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3426 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3427 | } |
| 3428 | pke_parsed += i; |
| 3429 | } |
| 3430 | |
| 3431 | /* check match between source and destination nodes */ |
Radek Krejci | 0c2fa3a | 2016-06-13 15:18:03 +0200 | [diff] [blame] | 3432 | leaf_dst = (struct lyd_node_leaf_list *)dest_match.node[0]; |
| 3433 | while (leaf_dst->value_type == LY_TYPE_LEAFREF) { |
| 3434 | leaf_dst = (struct lyd_node_leaf_list *)leaf_dst->value.leafref; |
| 3435 | } |
| 3436 | leaf_src = (struct lyd_node_leaf_list *)source_match.node[0]; |
| 3437 | while (leaf_src->value_type == LY_TYPE_LEAFREF) { |
| 3438 | leaf_src = (struct lyd_node_leaf_list *)leaf_src->value.leafref; |
| 3439 | } |
| 3440 | if (leaf_src->value_type != leaf_dst->value_type) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3441 | goto remove_leafref; |
| 3442 | } |
| 3443 | |
Radek Krejci | 0c2fa3a | 2016-06-13 15:18:03 +0200 | [diff] [blame] | 3444 | if (!ly_strequal(leaf_src->value_str, leaf_dst->value_str, 1)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3445 | goto remove_leafref; |
| 3446 | } |
| 3447 | |
| 3448 | /* leafref is ok, continue check with next leafref */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3449 | ++j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3450 | continue; |
| 3451 | |
| 3452 | remove_leafref: |
| 3453 | /* does not fulfill conditions, remove leafref record */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3454 | unres_data_del(node_match, j); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3455 | } |
| 3456 | } while (has_predicate); |
| 3457 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3458 | free(source_match.node); |
| 3459 | free(dest_match.node); |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3460 | if (parsed) { |
| 3461 | *parsed = parsed_loc; |
| 3462 | } |
| 3463 | return EXIT_SUCCESS; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3464 | |
| 3465 | error: |
| 3466 | |
| 3467 | if (source_match.count) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3468 | free(source_match.node); |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3469 | } |
| 3470 | if (dest_match.count) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3471 | free(dest_match.node); |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3472 | } |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3473 | if (parsed) { |
| 3474 | *parsed = -parsed_loc+i; |
| 3475 | } |
| 3476 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3477 | } |
| 3478 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3479 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3480 | * @brief Resolve a path (leafref) in JSON data context. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3481 | * |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3482 | * @param[in] node Leafref data node. |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3483 | * @param[in] path Path of the leafref. |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3484 | * @param[out] ret Matching nodes. Expects an empty, but allocated structure. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3485 | * |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3486 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 otherwise. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3487 | */ |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3488 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3489 | 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] | 3490 | { |
Radek Krejci | 71b795b | 2015-08-10 16:20:39 +0200 | [diff] [blame] | 3491 | struct lyd_node *data = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3492 | const char *prefix, *name; |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3493 | int pref_len, nam_len, has_predicate, parent_times, i, parsed, rc; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3494 | uint32_t j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3495 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3496 | assert(node && path && ret && !ret->count); |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3497 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3498 | parent_times = 0; |
Michal Vasko | d917334 | 2015-08-17 14:35:36 +0200 | [diff] [blame] | 3499 | parsed = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3500 | |
| 3501 | /* searching for nodeset */ |
| 3502 | do { |
| 3503 | 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] | 3504 | LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, path[-i], &path[-i]); |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3505 | rc = -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3506 | goto error; |
| 3507 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3508 | path += i; |
Michal Vasko | d917334 | 2015-08-17 14:35:36 +0200 | [diff] [blame] | 3509 | parsed += i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3510 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3511 | if (!ret->count) { |
Michal Vasko | bfd98e6 | 2016-09-02 09:50:05 +0200 | [diff] [blame] | 3512 | if (parent_times > 0) { |
| 3513 | data = node; |
| 3514 | for (i = 1; i < parent_times; ++i) { |
| 3515 | data = data->parent; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3516 | } |
Michal Vasko | bfd98e6 | 2016-09-02 09:50:05 +0200 | [diff] [blame] | 3517 | } else if (!parent_times) { |
| 3518 | data = node->child; |
| 3519 | } else { |
| 3520 | /* absolute path */ |
| 3521 | for (data = node; data->parent; data = data->parent); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3522 | } |
| 3523 | |
Michal Vasko | bfd98e6 | 2016-09-02 09:50:05 +0200 | [diff] [blame] | 3524 | /* we may still be parsing it and the pointer is not correct yet */ |
| 3525 | if (data->prev) { |
| 3526 | while (data->prev->next) { |
| 3527 | data = data->prev; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 3528 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3529 | } |
| 3530 | } |
| 3531 | |
| 3532 | /* node identifier */ |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3533 | 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] | 3534 | if (rc == -1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3535 | LOGVAL(LYE_INELEM_LEN, LY_VLOG_LYD, node, nam_len, name); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3536 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3537 | goto error; |
| 3538 | } |
| 3539 | |
| 3540 | if (has_predicate) { |
| 3541 | /* we have predicate, so the current results must be lists */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3542 | for (j = 0; j < ret->count;) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3543 | if (ret->node[j]->schema->nodetype == LYS_LIST && |
| 3544 | ((struct lys_node_list *)ret->node[0]->schema)->keys) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3545 | /* leafref is ok, continue check with next leafref */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3546 | ++j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3547 | continue; |
| 3548 | } |
| 3549 | |
| 3550 | /* does not fulfill conditions, remove leafref record */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3551 | unres_data_del(ret, j); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3552 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3553 | if ((rc = resolve_path_predicate_data(path, node, ret, &i))) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 3554 | if (rc == -1) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3555 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYD, node, "leafref", path); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3556 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3557 | goto error; |
| 3558 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3559 | path += i; |
Michal Vasko | d917334 | 2015-08-17 14:35:36 +0200 | [diff] [blame] | 3560 | parsed += i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3561 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3562 | if (!ret->count) { |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3563 | rc = EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3564 | goto error; |
| 3565 | } |
| 3566 | } |
| 3567 | } while (path[0] != '\0'); |
| 3568 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3569 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3570 | |
| 3571 | error: |
| 3572 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3573 | free(ret->node); |
| 3574 | ret->node = NULL; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3575 | ret->count = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3576 | |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3577 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3578 | } |
| 3579 | |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3580 | static int |
| 3581 | resolve_path_arg_schema_valid_dep_flag(const struct lys_node *op_node, const struct lys_node *first_node, int abs_path) |
| 3582 | { |
| 3583 | int dep1, dep2; |
| 3584 | const struct lys_node *node; |
| 3585 | |
| 3586 | if (lys_parent(op_node)) { |
| 3587 | /* inner operation (notif/action) */ |
| 3588 | if (abs_path) { |
| 3589 | return 1; |
| 3590 | } else { |
| 3591 | /* compare depth of both nodes */ |
| 3592 | for (dep1 = 0, node = op_node; lys_parent(node); node = lys_parent(node)); |
| 3593 | for (dep2 = 0, node = first_node; lys_parent(node); node = lys_parent(node)); |
| 3594 | if ((dep2 > dep1) || ((dep2 == dep1) && (op_node != first_node))) { |
| 3595 | return 1; |
| 3596 | } |
| 3597 | } |
| 3598 | } else { |
| 3599 | /* top-level operation (notif/rpc) */ |
| 3600 | if (op_node != first_node) { |
| 3601 | return 1; |
| 3602 | } |
| 3603 | } |
| 3604 | |
| 3605 | return 0; |
| 3606 | } |
| 3607 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3608 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3609 | * @brief Resolve a path (leafref) predicate in JSON schema context. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3610 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3611 | * @param[in] path Path to use. |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3612 | * @param[in] context_node Predicate context node (where the predicate is placed). |
| 3613 | * @param[in] parent Path context node (where the path begins/is placed). |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3614 | * @param[in] op_node Optional node if the leafref is in an operation (action/rpc/notif). |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3615 | * |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3616 | * @return 0 on forward reference, otherwise the number |
| 3617 | * of characters successfully parsed, |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3618 | * positive on success, negative on failure. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3619 | */ |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3620 | static int |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3621 | resolve_path_predicate_schema(const char *path, const struct lys_node *context_node, |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3622 | struct lys_node *parent, const struct lys_node *op_node) |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3623 | { |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3624 | const struct lys_node *src_node, *dst_node; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3625 | const char *path_key_expr, *source, *sour_pref, *dest, *dest_pref; |
| 3626 | int pke_len, sour_len, sour_pref_len, dest_len, dest_pref_len, parsed = 0, pke_parsed = 0; |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3627 | int has_predicate, dest_parent_times = 0, i, rc, first_iter; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3628 | |
| 3629 | do { |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3630 | 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] | 3631 | &pke_len, &has_predicate)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3632 | 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] | 3633 | return -parsed+i; |
| 3634 | } |
| 3635 | parsed += i; |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3636 | path += i; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3637 | |
Michal Vasko | 5809090 | 2015-08-13 14:04:15 +0200 | [diff] [blame] | 3638 | /* source (must be leaf) */ |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3639 | if (!sour_pref) { |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3640 | sour_pref = context_node->module->name; |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3641 | } |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3642 | rc = lys_get_sibling(context_node->child, sour_pref, sour_pref_len, source, sour_len, |
Michal Vasko | 59ad458 | 2016-09-16 13:15:41 +0200 | [diff] [blame] | 3643 | LYS_LEAF | LYS_LEAFLIST | LYS_AUGMENT, &src_node); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3644 | if (rc) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3645 | LOGVAL(LYE_NORESOLV, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, "leafref predicate", path-parsed); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3646 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3647 | } |
| 3648 | |
| 3649 | /* destination */ |
| 3650 | if ((i = parse_path_key_expr(path_key_expr, &dest_pref, &dest_pref_len, &dest, &dest_len, |
| 3651 | &dest_parent_times)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3652 | 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] | 3653 | return -parsed; |
| 3654 | } |
| 3655 | pke_parsed += i; |
| 3656 | |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3657 | for (i = 0, dst_node = parent; i < dest_parent_times; ++i) { |
Michal Vasko | fbaead7 | 2016-10-07 10:54:48 +0200 | [diff] [blame] | 3658 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 3659 | * all schema nodes that cannot be instantiated in data tree */ |
| 3660 | for (dst_node = lys_parent(dst_node); |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3661 | dst_node && !(dst_node->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_ACTION | LYS_NOTIF | LYS_RPC)); |
Michal Vasko | fbaead7 | 2016-10-07 10:54:48 +0200 | [diff] [blame] | 3662 | dst_node = lys_parent(dst_node)); |
| 3663 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3664 | if (!dst_node) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3665 | LOGVAL(LYE_NORESOLV, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, "leafref predicate", path_key_expr); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3666 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3667 | } |
| 3668 | } |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3669 | first_iter = 1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3670 | while (1) { |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3671 | if (!dest_pref) { |
| 3672 | dest_pref = dst_node->module->name; |
| 3673 | } |
| 3674 | 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] | 3675 | LYS_CONTAINER | LYS_LIST | LYS_LEAF | LYS_AUGMENT, &dst_node); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3676 | if (rc) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3677 | LOGVAL(LYE_NORESOLV, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, "leafref predicate", path_key_expr); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3678 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3679 | } |
| 3680 | |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3681 | if (first_iter) { |
| 3682 | if (resolve_path_arg_schema_valid_dep_flag(op_node, dst_node, 0)) { |
| 3683 | parent->flags |= LYS_VALID_DEP; |
| 3684 | } |
| 3685 | first_iter = 0; |
| 3686 | } |
| 3687 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3688 | if (pke_len == pke_parsed) { |
| 3689 | break; |
| 3690 | } |
| 3691 | |
| 3692 | if ((i = parse_path_key_expr(path_key_expr+pke_parsed, &dest_pref, &dest_pref_len, &dest, &dest_len, |
| 3693 | &dest_parent_times)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3694 | LOGVAL(LYE_INCHAR, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3695 | (path_key_expr+pke_parsed)[-i], (path_key_expr+pke_parsed)-i); |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3696 | return -parsed; |
| 3697 | } |
| 3698 | pke_parsed += i; |
| 3699 | } |
| 3700 | |
| 3701 | /* check source - dest match */ |
Michal Vasko | 59ad458 | 2016-09-16 13:15:41 +0200 | [diff] [blame] | 3702 | if (dst_node->nodetype != src_node->nodetype) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3703 | LOGVAL(LYE_NORESOLV, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, "leafref predicate", path-parsed); |
Michal Vasko | 59ad458 | 2016-09-16 13:15:41 +0200 | [diff] [blame] | 3704 | LOGVAL(LYE_SPEC, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, "Destination node is not a %s, but a %s.", |
| 3705 | strnodetype(src_node->nodetype), strnodetype(dst_node->nodetype)); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3706 | return -parsed; |
| 3707 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3708 | } while (has_predicate); |
| 3709 | |
| 3710 | return parsed; |
| 3711 | } |
| 3712 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3713 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3714 | * @brief Resolve a path (leafref) in JSON schema context. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3715 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3716 | * @param[in] path Path to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3717 | * @param[in] parent_node Parent of the leafref. |
Radek Krejci | 2f12f85 | 2016-01-08 12:59:57 +0100 | [diff] [blame] | 3718 | * @param[in] parent_tpdf Flag if the parent node is actually typedef, in that case the path |
| 3719 | * has to contain absolute path |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3720 | * @param[out] ret Pointer to the resolved schema node. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3721 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3722 | * @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] | 3723 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3724 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3725 | 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] | 3726 | const struct lys_node **ret) |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3727 | { |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3728 | const struct lys_node *node, *op_node = NULL; |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 3729 | const struct lys_module *mod, *mod2; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3730 | const char *id, *prefix, *name; |
| 3731 | int pref_len, nam_len, parent_times, has_predicate; |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3732 | int i, first_iter, rc; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3733 | |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3734 | first_iter = 1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3735 | parent_times = 0; |
| 3736 | id = path; |
| 3737 | |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3738 | /* find operation schema we are in, if applicable */ |
Michal Vasko | e9914d1 | 2016-10-07 14:32:37 +0200 | [diff] [blame] | 3739 | if (!parent_tpdf) { |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3740 | for (op_node = lys_parent(parent); |
| 3741 | op_node && !(op_node->nodetype & (LYS_ACTION | LYS_NOTIF | LYS_RPC)); |
| 3742 | op_node = lys_parent(op_node)); |
Michal Vasko | e9914d1 | 2016-10-07 14:32:37 +0200 | [diff] [blame] | 3743 | } |
| 3744 | |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 3745 | mod2 = lys_node_module(parent); |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3746 | do { |
| 3747 | 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] | 3748 | 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] | 3749 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3750 | } |
| 3751 | id += i; |
| 3752 | |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3753 | if (first_iter) { |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3754 | if (parent_times == -1) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 3755 | /* resolve prefix of the module */ |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3756 | mod = lys_get_import_module(parent->module, NULL, 0, prefix, pref_len); |
Radek Krejci | 0fa54e9 | 2016-09-14 14:01:05 +0200 | [diff] [blame] | 3757 | mod = lys_get_implemented_module(mod); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 3758 | /* get start node */ |
| 3759 | node = mod ? mod->data : NULL; |
Michal Vasko | 5809090 | 2015-08-13 14:04:15 +0200 | [diff] [blame] | 3760 | if (!node) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3761 | LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, |
| 3762 | "leafref", path); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3763 | return EXIT_FAILURE; |
Michal Vasko | 5809090 | 2015-08-13 14:04:15 +0200 | [diff] [blame] | 3764 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3765 | } else if (parent_times > 0) { |
Radek Krejci | 2f12f85 | 2016-01-08 12:59:57 +0100 | [diff] [blame] | 3766 | if (parent_tpdf) { |
| 3767 | /* the path is not allowed to contain relative path since we are in top level typedef */ |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3768 | LOGVAL(LYE_NORESOLV, 0, NULL, "leafref", path); |
Radek Krejci | 2f12f85 | 2016-01-08 12:59:57 +0100 | [diff] [blame] | 3769 | return -1; |
| 3770 | } |
| 3771 | |
Michal Vasko | 9445808 | 2016-10-07 14:34:36 +0200 | [diff] [blame] | 3772 | /* we are looking for a sibling of a node, node it's parent (that is why parent_times - 1) */ |
| 3773 | for (i = 0, node = parent; i < parent_times - 1; i++) { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 3774 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 3775 | * all schema nodes that cannot be instantiated in data tree */ |
| 3776 | for (node = lys_parent(node); |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3777 | node && !(node->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_ACTION | LYS_NOTIF | LYS_RPC)); |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 3778 | node = lys_parent(node)); |
| 3779 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3780 | if (!node) { |
Michal Vasko | e9914d1 | 2016-10-07 14:32:37 +0200 | [diff] [blame] | 3781 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3782 | return EXIT_FAILURE; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3783 | } |
| 3784 | } |
Michal Vasko | e01eca5 | 2015-08-13 14:42:02 +0200 | [diff] [blame] | 3785 | } else { |
| 3786 | LOGINT; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3787 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3788 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3789 | } else { |
Michal Vasko | 7dc71d0 | 2016-03-15 10:42:28 +0100 | [diff] [blame] | 3790 | /* move down the tree, if possible */ |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 3791 | if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
Michal Vasko | 2f5aceb | 2016-03-22 10:24:14 +0100 | [diff] [blame] | 3792 | 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] | 3793 | return -1; |
| 3794 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3795 | node = node->child; |
Radek Krejci | 43ccc4c | 2016-10-18 20:40:06 +0200 | [diff] [blame] | 3796 | if (!node) { |
| 3797 | LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, |
| 3798 | "leafref", path); |
| 3799 | return EXIT_FAILURE; |
| 3800 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3801 | } |
| 3802 | |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 3803 | if (!prefix) { |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3804 | prefix = lys_node_module(parent)->name; |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 3805 | } |
| 3806 | |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3807 | 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] | 3808 | if (rc) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3809 | LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, "leafref", path); |
Michal Vasko | 7a55bea | 2016-05-02 14:51:20 +0200 | [diff] [blame] | 3810 | return EXIT_FAILURE; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3811 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3812 | |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3813 | if (first_iter) { |
| 3814 | /* set external dependency flag, we can decide based on the first found node */ |
| 3815 | if (!parent_tpdf && op_node && parent_times && |
| 3816 | resolve_path_arg_schema_valid_dep_flag(op_node, node, (parent_times == -1 ? 1 : 0))) { |
| 3817 | parent->flags |= LYS_VALID_DEP; |
| 3818 | } |
| 3819 | first_iter = 0; |
| 3820 | } |
| 3821 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3822 | if (has_predicate) { |
| 3823 | /* we have predicate, so the current result must be list */ |
| 3824 | if (node->nodetype != LYS_LIST) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3825 | LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, "leafref", path); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3826 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3827 | } |
| 3828 | |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3829 | i = resolve_path_predicate_schema(id, node, parent, op_node); |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 3830 | if (i <= 0) { |
| 3831 | if (i == 0) { |
| 3832 | return EXIT_FAILURE; |
| 3833 | } else { /* i < 0 */ |
| 3834 | return -1; |
| 3835 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3836 | } |
| 3837 | id += i; |
| 3838 | } |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 3839 | mod = lys_node_module(node); |
| 3840 | if (!mod->implemented && mod != mod2) { |
| 3841 | /* set the module implemented */ |
| 3842 | if (lys_set_implemented(mod)) { |
| 3843 | return -1; |
| 3844 | } |
| 3845 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3846 | } while (id[0]); |
| 3847 | |
Michal Vasko | ca91768 | 2016-07-25 11:00:37 +0200 | [diff] [blame] | 3848 | /* the target must be leaf or leaf-list (in YANG 1.1 only) */ |
| 3849 | if ((node->nodetype != LYS_LEAF) && ((lys_node_module(parent)->version != 2) || (node->nodetype != LYS_LEAFLIST))) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3850 | LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, "leafref", path); |
Radek Krejci | d47daf6 | 2016-08-22 16:23:38 +0200 | [diff] [blame] | 3851 | LOGVAL(LYE_SPEC, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, |
| 3852 | "Leafref target \"%s\" is not a leaf%s.", path, |
| 3853 | lys_node_module(parent)->version != 2 ? "" : " nor a leaf-list"); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3854 | return -1; |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 3855 | } |
| 3856 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3857 | /* check status */ |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3858 | if (lyp_check_status(parent->flags, parent->module, parent->name, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3859 | node->flags, node->module, node->name, node)) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3860 | return -1; |
| 3861 | } |
| 3862 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3863 | if (ret) { |
| 3864 | *ret = node; |
| 3865 | } |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 3866 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3867 | return EXIT_SUCCESS; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3868 | } |
| 3869 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3870 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3871 | * @brief Resolve instance-identifier predicate in JSON data format. |
| 3872 | * Does not log. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3873 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3874 | * @param[in] pred Predicate to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3875 | * @param[in,out] node_match Nodes matching the restriction without |
| 3876 | * the predicate. Nodes not satisfying |
| 3877 | * the predicate are removed. |
| 3878 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3879 | * @return Number of characters successfully parsed, |
| 3880 | * positive on success, negative on failure. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3881 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3882 | static int |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3883 | resolve_predicate(const char *pred, struct unres_data *node_match) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3884 | { |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3885 | /* ... /node[target = value] ... */ |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3886 | struct lyd_node *target; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3887 | const char *model, *name, *value; |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3888 | int mod_len, nam_len, val_len, i, has_predicate, cur_idx, idx, parsed, pred_iter, k; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3889 | uint32_t j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3890 | |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3891 | assert(pred && node_match->count); |
| 3892 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3893 | idx = -1; |
| 3894 | parsed = 0; |
| 3895 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3896 | pred_iter = -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3897 | do { |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3898 | 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] | 3899 | return -parsed+i; |
| 3900 | } |
| 3901 | parsed += i; |
| 3902 | pred += i; |
| 3903 | |
| 3904 | if (isdigit(name[0])) { |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3905 | /* pos */ |
| 3906 | assert(!value); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3907 | idx = atoi(name); |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3908 | } else if (name[0] != '.') { |
| 3909 | /* list keys */ |
| 3910 | if (pred_iter < 0) { |
| 3911 | pred_iter = 1; |
| 3912 | } else { |
| 3913 | ++pred_iter; |
| 3914 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3915 | } |
| 3916 | |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 3917 | for (cur_idx = 1, j = 0; j < node_match->count; ++cur_idx) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3918 | /* target */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3919 | if (name[0] == '.') { |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3920 | /* leaf-list value */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3921 | if (node_match->node[j]->schema->nodetype != LYS_LEAFLIST) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3922 | goto remove_instid; |
| 3923 | } |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3924 | |
| 3925 | target = node_match->node[j]; |
| 3926 | /* check the value */ |
| 3927 | if (strncmp(((struct lyd_node_leaf_list *)target)->value_str, value, val_len) |
| 3928 | || ((struct lyd_node_leaf_list *)target)->value_str[val_len]) { |
| 3929 | goto remove_instid; |
| 3930 | } |
| 3931 | |
| 3932 | } else if (!value) { |
| 3933 | /* keyless list position */ |
| 3934 | if ((node_match->node[j]->schema->nodetype != LYS_LIST) |
| 3935 | || ((struct lys_node_list *)node_match->node[j]->schema)->keys) { |
| 3936 | goto remove_instid; |
| 3937 | } |
| 3938 | |
| 3939 | if (idx != cur_idx) { |
| 3940 | goto remove_instid; |
| 3941 | } |
| 3942 | |
| 3943 | } else { |
| 3944 | /* list key value */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3945 | if (node_match->node[j]->schema->nodetype != LYS_LIST) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3946 | goto remove_instid; |
| 3947 | } |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3948 | |
| 3949 | /* key module must match the list module */ |
| 3950 | if (strncmp(node_match->node[j]->schema->module->name, model, mod_len) |
| 3951 | || node_match->node[j]->schema->module->name[mod_len]) { |
| 3952 | goto remove_instid; |
| 3953 | } |
| 3954 | /* find the key leaf */ |
Michal Vasko | 045182c | 2016-09-09 12:44:07 +0200 | [diff] [blame] | 3955 | for (k = 1, target = node_match->node[j]->child; target && (k < pred_iter); k++, target = target->next); |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3956 | if (!target) { |
| 3957 | goto remove_instid; |
| 3958 | } |
| 3959 | if ((struct lys_node_leaf *)target->schema != |
| 3960 | ((struct lys_node_list *)node_match->node[j]->schema)->keys[pred_iter - 1]) { |
| 3961 | goto remove_instid; |
| 3962 | } |
| 3963 | |
| 3964 | /* check the value */ |
| 3965 | if (strncmp(((struct lyd_node_leaf_list *)target)->value_str, value, val_len) |
| 3966 | || ((struct lyd_node_leaf_list *)target)->value_str[val_len]) { |
| 3967 | goto remove_instid; |
| 3968 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3969 | } |
| 3970 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3971 | /* instid is ok, continue check with the next one */ |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3972 | ++j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3973 | continue; |
| 3974 | |
| 3975 | remove_instid: |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3976 | /* does not fulfill conditions, remove instid record */ |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3977 | unres_data_del(node_match, j); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3978 | } |
| 3979 | } while (has_predicate); |
| 3980 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3981 | /* check that all list keys were specified */ |
| 3982 | if ((pred_iter > 0) && node_match->count) { |
Michal Vasko | 045182c | 2016-09-09 12:44:07 +0200 | [diff] [blame] | 3983 | j = 0; |
| 3984 | while (j < node_match->count) { |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3985 | assert(node_match->node[j]->schema->nodetype == LYS_LIST); |
| 3986 | if (pred_iter < ((struct lys_node_list *)node_match->node[j]->schema)->keys_size) { |
| 3987 | /* not enough predicates, just remove the list instance */ |
| 3988 | unres_data_del(node_match, j); |
Michal Vasko | 045182c | 2016-09-09 12:44:07 +0200 | [diff] [blame] | 3989 | } else { |
| 3990 | ++j; |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3991 | } |
| 3992 | } |
| 3993 | |
| 3994 | if (!node_match->count) { |
| 3995 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier is missing some list keys."); |
| 3996 | } |
| 3997 | } |
| 3998 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3999 | return parsed; |
| 4000 | } |
| 4001 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4002 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 4003 | * @brief Resolve instance-identifier in JSON data format. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4004 | * |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 4005 | * @param[in] data Data node where the path is used |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4006 | * @param[in] path Instance-identifier node value. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4007 | * |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4008 | * @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] | 4009 | */ |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 4010 | static struct lyd_node * |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4011 | resolve_instid(struct lyd_node *data, const char *path) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4012 | { |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4013 | int i = 0, j; |
| 4014 | struct lyd_node *result = NULL; |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4015 | const struct lys_module *mod; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4016 | struct ly_ctx *ctx = data->schema->module->ctx; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4017 | const char *model, *name; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4018 | char *str; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4019 | int mod_len, name_len, has_predicate; |
| 4020 | struct unres_data node_match; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4021 | |
| 4022 | memset(&node_match, 0, sizeof node_match); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4023 | |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4024 | /* we need root to resolve absolute path */ |
| 4025 | for (; data->parent; data = data->parent); |
Michal Vasko | db9323c | 2015-10-16 10:32:44 +0200 | [diff] [blame] | 4026 | /* we're still parsing it and the pointer is not correct yet */ |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 4027 | if (data->prev) { |
| 4028 | for (; data->prev->next; data = data->prev); |
| 4029 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4030 | |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4031 | /* search for the instance node */ |
| 4032 | while (path[i]) { |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 4033 | 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] | 4034 | if (j <= 0) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4035 | 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] | 4036 | goto error; |
| 4037 | } |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4038 | i += j; |
Michal Vasko | b387c48 | 2015-08-12 09:32:59 +0200 | [diff] [blame] | 4039 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4040 | str = strndup(model, mod_len); |
| 4041 | if (!str) { |
| 4042 | LOGMEM; |
| 4043 | goto error; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4044 | } |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4045 | mod = ly_ctx_get_module(ctx, str, NULL); |
| 4046 | free(str); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4047 | |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4048 | if (resolve_data(mod, name, name_len, data, &node_match)) { |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4049 | /* no instance exists */ |
| 4050 | return NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4051 | } |
| 4052 | |
| 4053 | if (has_predicate) { |
| 4054 | /* we have predicate, so the current results must be list or leaf-list */ |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 4055 | j = resolve_predicate(&path[i], &node_match); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4056 | if (j < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4057 | LOGVAL(LYE_INPRED, LY_VLOG_LYD, data, &path[i-j]); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4058 | goto error; |
| 4059 | } |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4060 | i += j; |
Michal Vasko | b387c48 | 2015-08-12 09:32:59 +0200 | [diff] [blame] | 4061 | |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4062 | if (!node_match.count) { |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4063 | /* no instance exists */ |
| 4064 | return NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4065 | } |
| 4066 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4067 | } |
| 4068 | |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4069 | if (!node_match.count) { |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4070 | /* no instance exists */ |
| 4071 | return NULL; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 4072 | } else if (node_match.count > 1) { |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4073 | /* instance identifier must resolve to a single node */ |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4074 | LOGVAL(LYE_TOOMANY, LY_VLOG_LYD, data, path, "data tree"); |
Michal Vasko | d6adbaa | 2016-04-11 11:01:09 +0200 | [diff] [blame] | 4075 | goto error; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4076 | } else { |
| 4077 | /* we have required result, remember it and cleanup */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4078 | result = node_match.node[0]; |
| 4079 | free(node_match.node); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4080 | return result; |
| 4081 | } |
| 4082 | |
| 4083 | error: |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4084 | /* cleanup */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4085 | free(node_match.node); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4086 | return NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4087 | } |
| 4088 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4089 | /** |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4090 | * @brief Check all XPath expressions of a node (when and must), set LYS_XPATH_DEP flag if required. |
| 4091 | * |
| 4092 | * @param[in] node Node to examine. |
| 4093 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 4094 | */ |
| 4095 | static int |
| 4096 | check_node_xpath(struct lys_node *node) |
| 4097 | { |
| 4098 | struct lys_node *parent, *elem; |
| 4099 | struct lyxp_set set; |
| 4100 | uint32_t i; |
| 4101 | int rc; |
| 4102 | |
| 4103 | parent = node; |
| 4104 | while (parent) { |
| 4105 | if (parent->nodetype == LYS_GROUPING) { |
| 4106 | /* unresolved grouping, skip for now (will be checked later) */ |
| 4107 | return EXIT_SUCCESS; |
| 4108 | } |
| 4109 | if (parent->nodetype == LYS_AUGMENT) { |
| 4110 | if (!((struct lys_node_augment *)parent)->target) { |
| 4111 | /* uresolved augment, skip for now (will be checked later) */ |
| 4112 | return EXIT_SUCCESS; |
| 4113 | } else { |
| 4114 | parent = ((struct lys_node_augment *)parent)->target; |
| 4115 | continue; |
| 4116 | } |
| 4117 | } |
| 4118 | parent = parent->parent; |
| 4119 | } |
| 4120 | |
| 4121 | rc = lyxp_node_atomize(node, &set); |
| 4122 | if (rc) { |
| 4123 | return rc; |
| 4124 | } |
| 4125 | |
| 4126 | for (parent = node; parent && !(parent->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); parent = lys_parent(parent)); |
| 4127 | |
| 4128 | for (i = 0; i < set.used; ++i) { |
| 4129 | /* skip roots'n'stuff */ |
| 4130 | if (set.val.snodes[i].type == LYXP_NODE_ELEM) { |
| 4131 | /* XPath expression cannot reference "lower" status than the node that has the definition */ |
| 4132 | if (lyp_check_status(node->flags, lys_node_module(node), node->name, set.val.snodes[i].snode->flags, |
| 4133 | lys_node_module(set.val.snodes[i].snode), set.val.snodes[i].snode->name, node)) { |
| 4134 | return -1; |
| 4135 | } |
| 4136 | |
| 4137 | if (parent) { |
| 4138 | for (elem = set.val.snodes[i].snode; elem && (elem != parent); elem = lys_parent(elem)); |
| 4139 | if (!elem) { |
| 4140 | /* not in node's RPC or notification subtree, set the flag */ |
| 4141 | node->flags |= LYS_VALID_DEP; |
| 4142 | break; |
| 4143 | } |
| 4144 | } |
| 4145 | } |
| 4146 | } |
| 4147 | |
| 4148 | free(set.val.snodes); |
| 4149 | return EXIT_SUCCESS; |
| 4150 | } |
| 4151 | |
| 4152 | /** |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4153 | * @brief Passes config flag down to children, skips nodes without config flags. |
| 4154 | * Does not log. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4155 | * |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4156 | * @param[in] node Siblings and their children to have flags changed. |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4157 | * @param[in] clear Flag to clear all config flags if parent is LYS_NOTIF, LYS_INPUT, LYS_OUTPUT, LYS_RPC. |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4158 | * @param[in] flags Flags to assign to all the nodes. |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4159 | * |
| 4160 | * @return 0 on success, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4161 | */ |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4162 | static int |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4163 | inherit_config_flag(struct lys_node *node, int flags, int clear, int check_list, int check_xpath) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4164 | { |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4165 | assert(!(flags ^ (flags & LYS_CONFIG_MASK))); |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4166 | LY_TREE_FOR(node, node) { |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4167 | if (check_xpath && check_node_xpath(node)) { |
| 4168 | return -1; |
| 4169 | } |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4170 | if (clear) { |
| 4171 | node->flags &= ~LYS_CONFIG_MASK; |
Michal Vasko | c2a8d36 | 2016-09-29 08:50:13 +0200 | [diff] [blame] | 4172 | node->flags &= ~LYS_CONFIG_SET; |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4173 | } else { |
| 4174 | if (node->flags & LYS_CONFIG_SET) { |
| 4175 | /* skip nodes with an explicit config value */ |
| 4176 | if ((flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) { |
| 4177 | LOGVAL(LYE_INARG, LY_VLOG_LYS, node, "true", "config"); |
| 4178 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, node, "State nodes cannot have configuration nodes as children."); |
| 4179 | return -1; |
| 4180 | } |
| 4181 | continue; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4182 | } |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4183 | |
| 4184 | if (!(node->nodetype & (LYS_USES | LYS_GROUPING))) { |
| 4185 | node->flags = (node->flags & ~LYS_CONFIG_MASK) | flags; |
| 4186 | /* check that configuration lists have keys */ |
| 4187 | if (check_list && (node->nodetype == LYS_LIST) |
| 4188 | && (node->flags & LYS_CONFIG_W) && !((struct lys_node_list *)node)->keys_size) { |
| 4189 | LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_LYS, node, "key", "list"); |
| 4190 | return -1; |
| 4191 | } |
| 4192 | } |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4193 | } |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4194 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) { |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4195 | if (inherit_config_flag(node->child, flags, clear, check_list, check_xpath)) { |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4196 | return -1; |
| 4197 | } |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 4198 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4199 | } |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4200 | |
| 4201 | return 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4202 | } |
| 4203 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4204 | /** |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 4205 | * @brief Resolve augment target. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4206 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4207 | * @param[in] aug Augment to use. |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 4208 | * @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] | 4209 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4210 | * @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] | 4211 | */ |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 4212 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4213 | resolve_augment(struct lys_node_augment *aug, struct lys_node *siblings) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4214 | { |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4215 | int rc, clear_config; |
Michal Vasko | 1d87a92 | 2015-08-21 12:57:16 +0200 | [diff] [blame] | 4216 | struct lys_node *sub; |
Pavol Vican | 4731993 | 2016-08-29 09:14:47 +0200 | [diff] [blame] | 4217 | const struct lys_node *aug_target, *parent; |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4218 | struct lys_module *mod; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4219 | |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4220 | assert(aug && !aug->target); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4221 | |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4222 | /* resolve target node */ |
| 4223 | rc = resolve_augment_schema_nodeid(aug->target_name, siblings, (siblings ? NULL : aug->module), &aug_target); |
| 4224 | if (rc == -1) { |
| 4225 | return -1; |
| 4226 | } |
| 4227 | if (rc > 0) { |
| 4228 | LOGVAL(LYE_INCHAR, LY_VLOG_LYS, aug, aug->target_name[rc - 1], &aug->target_name[rc - 1]); |
| 4229 | return -1; |
| 4230 | } |
| 4231 | if (!aug_target) { |
| 4232 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, aug, "augment", aug->target_name); |
| 4233 | return EXIT_FAILURE; |
| 4234 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4235 | |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4236 | /* check that we want to connect augment into its target */ |
| 4237 | mod = lys_main_module(aug->module); |
| 4238 | if (!mod->implemented) { |
| 4239 | /* it must be augment only to the same module, |
| 4240 | * otherwise we do not apply augment in not-implemented |
| 4241 | * module. If the module is set to be implemented in future, |
| 4242 | * the augment is being resolved and checked again */ |
| 4243 | for (sub = aug->target; sub; sub = lys_parent(sub)) { |
| 4244 | if (lys_node_module(sub) != mod) { |
| 4245 | /* this is not an implemented module and the augment |
| 4246 | * target some other module, so avoid its connecting |
| 4247 | * to the target */ |
| 4248 | return EXIT_SUCCESS; |
| 4249 | } |
| 4250 | } |
| 4251 | } |
| 4252 | |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4253 | if (!aug->child) { |
| 4254 | /* nothing to do */ |
| 4255 | LOGWRN("Augment \"%s\" without children.", aug->target_name); |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4256 | goto success; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4257 | } |
| 4258 | |
Michal Vasko | d58d596 | 2016-03-02 14:29:41 +0100 | [diff] [blame] | 4259 | /* check for mandatory nodes - if the target node is in another module |
| 4260 | * the added nodes cannot be mandatory |
| 4261 | */ |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4262 | if (!aug->parent && (lys_node_module((struct lys_node *)aug) != lys_node_module(aug_target)) |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 4263 | && (rc = lyp_check_mandatory_augment(aug))) { |
| 4264 | return rc; |
Michal Vasko | d58d596 | 2016-03-02 14:29:41 +0100 | [diff] [blame] | 4265 | } |
| 4266 | |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4267 | /* check augment target type and then augment nodes type */ |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4268 | if (aug_target->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_CASE | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF)) { |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4269 | LY_TREE_FOR(aug->child, sub) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4270 | if (!(sub->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_LEAF | LYS_LIST | LYS_LEAFLIST | LYS_USES | LYS_CHOICE))) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4271 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment"); |
| 4272 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, aug, "Cannot augment \"%s\" with a \"%s\".", |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4273 | strnodetype(aug_target->nodetype), strnodetype(sub->nodetype)); |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4274 | return -1; |
| 4275 | } |
| 4276 | } |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4277 | } else if (aug_target->nodetype == LYS_CHOICE) { |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4278 | LY_TREE_FOR(aug->child, sub) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4279 | if (!(sub->nodetype & (LYS_CASE | LYS_ANYDATA | LYS_CONTAINER | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4280 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment"); |
| 4281 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, aug, "Cannot augment \"%s\" with a \"%s\".", |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4282 | strnodetype(aug_target->nodetype), strnodetype(sub->nodetype)); |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4283 | return -1; |
| 4284 | } |
| 4285 | } |
| 4286 | } else { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4287 | LOGVAL(LYE_INARG, LY_VLOG_LYS, aug, aug->target_name, "target-node"); |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4288 | 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] | 4289 | return -1; |
| 4290 | } |
| 4291 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 4292 | /* check identifier uniqueness as in lys_node_addchild() */ |
Michal Vasko | 1d87a92 | 2015-08-21 12:57:16 +0200 | [diff] [blame] | 4293 | LY_TREE_FOR(aug->child, sub) { |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4294 | if (lys_check_id(sub, (struct lys_node *)aug_target, NULL)) { |
Michal Vasko | 3e6665f | 2015-08-17 14:00:38 +0200 | [diff] [blame] | 4295 | return -1; |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 4296 | } |
| 4297 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4298 | |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4299 | /* finally reconnect augmenting data into the target - add them to the target child list, |
| 4300 | * by setting aug->target we know the augment is fully resolved now */ |
| 4301 | aug->target = (struct lys_node *)aug_target; |
| 4302 | if (aug->target->child) { |
| 4303 | sub = aug->target->child->prev; /* remember current target's last node */ |
| 4304 | sub->next = aug->child; /* connect augmenting data after target's last node */ |
| 4305 | aug->target->child->prev = aug->child->prev; /* new target's last node is last augmenting node */ |
| 4306 | aug->child->prev = sub; /* finish connecting of both child lists */ |
| 4307 | } else { |
| 4308 | aug->target->child = aug->child; |
| 4309 | } |
| 4310 | |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4311 | /* inherit config information from actual parent */ |
| 4312 | for(parent = aug_target; parent && !(parent->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT | LYS_RPC)); parent = lys_parent(parent)); |
| 4313 | clear_config = (parent) ? 1 : 0; |
| 4314 | LY_TREE_FOR(aug->child, sub) { |
| 4315 | if (inherit_config_flag(sub, aug_target->flags & LYS_CONFIG_MASK, clear_config, 1, 1)) { |
| 4316 | return -1; |
| 4317 | } |
| 4318 | } |
| 4319 | |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4320 | success: |
| 4321 | if (mod->implemented) { |
| 4322 | /* make target modules also implemented */ |
| 4323 | for (sub = aug->target; sub; sub = lys_parent(sub)) { |
| 4324 | if (lys_set_implemented(sub->module)) { |
| 4325 | return -1; |
| 4326 | } |
| 4327 | } |
| 4328 | } |
| 4329 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4330 | return EXIT_SUCCESS; |
| 4331 | } |
| 4332 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4333 | /** |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4334 | * @brief Resolve (find) choice default case. Does not log. |
| 4335 | * |
| 4336 | * @param[in] choic Choice to use. |
| 4337 | * @param[in] dflt Name of the default case. |
| 4338 | * |
| 4339 | * @return Pointer to the default node or NULL. |
| 4340 | */ |
| 4341 | static struct lys_node * |
| 4342 | resolve_choice_dflt(struct lys_node_choice *choic, const char *dflt) |
| 4343 | { |
| 4344 | struct lys_node *child, *ret; |
| 4345 | |
| 4346 | LY_TREE_FOR(choic->child, child) { |
| 4347 | if (child->nodetype == LYS_USES) { |
| 4348 | ret = resolve_choice_dflt((struct lys_node_choice *)child, dflt); |
| 4349 | if (ret) { |
| 4350 | return ret; |
| 4351 | } |
| 4352 | } |
| 4353 | |
| 4354 | if (ly_strequal(child->name, dflt, 1) && (child->nodetype & (LYS_ANYDATA | LYS_CASE |
Radek Krejci | 2f792db | 2016-09-12 10:52:33 +0200 | [diff] [blame] | 4355 | | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CHOICE))) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4356 | return child; |
| 4357 | } |
| 4358 | } |
| 4359 | |
| 4360 | return NULL; |
| 4361 | } |
| 4362 | |
| 4363 | /** |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4364 | * @brief Resolve uses, apply augments, refines. Logs directly. |
| 4365 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4366 | * @param[in] uses Uses to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4367 | * @param[in,out] unres List of unresolved items. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4368 | * |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 4369 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4370 | */ |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 4371 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4372 | resolve_uses(struct lys_node_uses *uses, struct unres_schema *unres) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4373 | { |
| 4374 | struct ly_ctx *ctx; |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4375 | struct lys_node *node = NULL, *next, *iter, **refine_nodes = NULL; |
Pavol Vican | 55abd33 | 2016-07-12 15:54:49 +0200 | [diff] [blame] | 4376 | struct lys_node *node_aux, *parent, *tmp; |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4377 | struct lys_node_leaflist *llist; |
| 4378 | struct lys_node_leaf *leaf; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 4379 | struct lys_refine *rfn; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4380 | struct lys_restr *must, **old_must; |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4381 | struct lys_iffeature *iff, **old_iff; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4382 | int i, j, k, rc, parent_config, clear_config, check_list, check_xpath; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4383 | uint8_t size, *old_size; |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4384 | unsigned int usize, usize1, usize2; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4385 | |
Michal Vasko | 71e1aa8 | 2015-08-12 12:17:51 +0200 | [diff] [blame] | 4386 | assert(uses->grp); |
Michal Vasko | e770851 | 2016-03-11 10:26:55 +0100 | [diff] [blame] | 4387 | /* HACK just check that the grouping is resolved */ |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 4388 | assert(!uses->grp->nacm); |
Michal Vasko | 71e1aa8 | 2015-08-12 12:17:51 +0200 | [diff] [blame] | 4389 | |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4390 | if (!uses->grp->child) { |
| 4391 | /* grouping without children, warning was already displayed */ |
| 4392 | return EXIT_SUCCESS; |
| 4393 | } |
| 4394 | |
| 4395 | /* get proper parent (config) flags */ |
| 4396 | for (node_aux = lys_parent((struct lys_node *)uses); node_aux && (node_aux->nodetype == LYS_USES); node_aux = lys_parent(node_aux)); |
| 4397 | if (node_aux) { |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4398 | parent_config = node_aux->flags & LYS_CONFIG_MASK; |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4399 | } else { |
| 4400 | /* default */ |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4401 | parent_config = LYS_CONFIG_W; |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4402 | } |
| 4403 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4404 | /* copy the data nodes from grouping into the uses context */ |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 4405 | LY_TREE_FOR(uses->grp->child, node_aux) { |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4406 | node = lys_node_dup(uses->module, (struct lys_node *)uses, node_aux, uses->nacm, unres, 0); |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 4407 | if (!node) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4408 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, uses->grp->name, "uses"); |
| 4409 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, "Copying data from grouping failed."); |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4410 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4411 | } |
Pavol Vican | 55abd33 | 2016-07-12 15:54:49 +0200 | [diff] [blame] | 4412 | /* test the name of siblings */ |
| 4413 | LY_TREE_FOR((uses->parent) ? uses->parent->child : lys_main_module(uses->module)->data, tmp) { |
Pavol Vican | 2510ddc | 2016-07-18 16:23:44 +0200 | [diff] [blame] | 4414 | if (!(tmp->nodetype & (LYS_USES | LYS_GROUPING | LYS_CASE)) && ly_strequal(tmp->name, node_aux->name, 1)) { |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4415 | goto fail; |
Pavol Vican | 55abd33 | 2016-07-12 15:54:49 +0200 | [diff] [blame] | 4416 | } |
| 4417 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4418 | } |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4419 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4420 | ctx = uses->module->ctx; |
Michal Vasko | 4bc590c | 2016-09-30 12:19:51 +0200 | [diff] [blame] | 4421 | |
| 4422 | parent = node; |
| 4423 | while (parent && !(parent->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT | LYS_RPC | LYS_GROUPING))) { |
| 4424 | if (parent->nodetype == LYS_AUGMENT) { |
| 4425 | if (!((struct lys_node_augment *)parent)->target) { |
| 4426 | break; |
| 4427 | } else { |
| 4428 | parent = ((struct lys_node_augment *)parent)->target; |
| 4429 | } |
| 4430 | } else { |
| 4431 | parent = parent->parent; |
| 4432 | } |
| 4433 | } |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4434 | if (parent) { |
Michal Vasko | 4bc590c | 2016-09-30 12:19:51 +0200 | [diff] [blame] | 4435 | if (parent->nodetype & (LYS_GROUPING | LYS_AUGMENT)) { |
| 4436 | /* we are still in some other unresolved grouping or augment, unable to check lists */ |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4437 | check_list = 0; |
| 4438 | clear_config = 0; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4439 | check_xpath = 0; |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4440 | } else { |
| 4441 | check_list = 0; |
| 4442 | clear_config = 1; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4443 | check_xpath = 1; |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4444 | } |
| 4445 | } else { |
| 4446 | check_list = 1; |
| 4447 | clear_config = 0; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4448 | check_xpath = 1; |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4449 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4450 | |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4451 | if (parent_config) { |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4452 | assert(uses->child); |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4453 | if (inherit_config_flag(uses->child, parent_config, clear_config, check_list, check_xpath)) { |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4454 | goto fail; |
| 4455 | } |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4456 | } |
| 4457 | |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 4458 | /* we managed to copy the grouping, the rest must be possible to resolve */ |
| 4459 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4460 | if (uses->refine_size) { |
| 4461 | refine_nodes = malloc(uses->refine_size * sizeof *refine_nodes); |
| 4462 | if (!refine_nodes) { |
| 4463 | LOGMEM; |
| 4464 | goto fail; |
| 4465 | } |
| 4466 | } |
| 4467 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4468 | /* apply refines */ |
| 4469 | for (i = 0; i < uses->refine_size; i++) { |
| 4470 | rfn = &uses->refine[i]; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 4471 | rc = resolve_descendant_schema_nodeid(rfn->target_name, uses->child, LYS_NO_RPC_NOTIF_NODE, |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 4472 | 1, 0, (const struct lys_node **)&node); |
Michal Vasko | 9bb061b | 2016-02-12 11:00:19 +0100 | [diff] [blame] | 4473 | if (rc || !node) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4474 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine"); |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4475 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4476 | } |
| 4477 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4478 | if (rfn->target_type && !(node->nodetype & rfn->target_type)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4479 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine"); |
| 4480 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, "Refine substatements not applicable to the target-node."); |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4481 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4482 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4483 | refine_nodes[i] = node; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4484 | |
| 4485 | /* description on any nodetype */ |
| 4486 | if (rfn->dsc) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4487 | lydict_remove(ctx, node->dsc); |
| 4488 | node->dsc = lydict_insert(ctx, rfn->dsc, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4489 | } |
| 4490 | |
| 4491 | /* reference on any nodetype */ |
| 4492 | if (rfn->ref) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4493 | lydict_remove(ctx, node->ref); |
| 4494 | node->ref = lydict_insert(ctx, rfn->ref, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4495 | } |
| 4496 | |
| 4497 | /* config on any nodetype */ |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4498 | if ((rfn->flags & LYS_CONFIG_MASK) && !clear_config) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4499 | node->flags &= ~LYS_CONFIG_MASK; |
| 4500 | node->flags |= (rfn->flags & LYS_CONFIG_MASK); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4501 | } |
| 4502 | |
| 4503 | /* default value ... */ |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4504 | if (rfn->dflt_size) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4505 | if (node->nodetype == LYS_LEAF) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4506 | /* leaf */ |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4507 | leaf = (struct lys_node_leaf *)node; |
| 4508 | |
| 4509 | lydict_remove(ctx, leaf->dflt); |
| 4510 | leaf->dflt = lydict_insert(ctx, rfn->dflt[0], 0); |
| 4511 | |
| 4512 | /* check the default value */ |
| 4513 | if (unres_schema_add_str(leaf->module, unres, &leaf->type, UNRES_TYPE_DFLT, leaf->dflt) == -1) { |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4514 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4515 | } |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4516 | } else if (node->nodetype == LYS_LEAFLIST) { |
| 4517 | /* leaf-list */ |
| 4518 | llist = (struct lys_node_leaflist *)node; |
| 4519 | |
| 4520 | /* remove complete set of defaults in target */ |
| 4521 | for (i = 0; i < llist->dflt_size; i++) { |
| 4522 | lydict_remove(ctx, llist->dflt[i]); |
| 4523 | } |
| 4524 | free(llist->dflt); |
| 4525 | |
| 4526 | /* copy the default set from refine */ |
| 4527 | llist->dflt_size = rfn->dflt_size; |
| 4528 | llist->dflt = malloc(llist->dflt_size * sizeof *llist->dflt); |
| 4529 | for (i = 0; i < llist->dflt_size; i++) { |
| 4530 | llist->dflt[i] = lydict_insert(ctx, rfn->dflt[i], 0); |
| 4531 | } |
| 4532 | |
| 4533 | /* check default value */ |
| 4534 | for (i = 0; i < llist->dflt_size; i++) { |
| 4535 | if (unres_schema_add_str(llist->module, unres, &llist->type, UNRES_TYPE_DFLT, llist->dflt[i]) == -1) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4536 | goto fail; |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4537 | } |
| 4538 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4539 | } |
| 4540 | } |
| 4541 | |
| 4542 | /* mandatory on leaf, anyxml or choice */ |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 4543 | if (rfn->flags & LYS_MAND_MASK) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4544 | if (node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_CHOICE)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4545 | /* remove current value */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4546 | node->flags &= ~LYS_MAND_MASK; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4547 | |
| 4548 | /* set new value */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4549 | node->flags |= (rfn->flags & LYS_MAND_MASK); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4550 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4551 | if (rfn->flags & LYS_MAND_TRUE) { |
| 4552 | /* check if node has default value */ |
| 4553 | if ((node->nodetype & LYS_LEAF) && ((struct lys_node_leaf *)node)->dflt) { |
| 4554 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The \"mandatory\" statement is forbidden on leaf with \"default\"."); |
| 4555 | goto fail; |
| 4556 | } |
| 4557 | if ((node->nodetype & LYS_CHOICE) && ((struct lys_node_choice *)node)->dflt) { |
| 4558 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The \"mandatory\" statement is forbidden on choices with \"default\"."); |
| 4559 | goto fail; |
| 4560 | } |
| 4561 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4562 | } |
| 4563 | |
| 4564 | /* presence on container */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4565 | if ((node->nodetype & LYS_CONTAINER) && rfn->mod.presence) { |
| 4566 | lydict_remove(ctx, ((struct lys_node_container *)node)->presence); |
| 4567 | ((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] | 4568 | } |
| 4569 | |
| 4570 | /* min/max-elements on list or leaf-list */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4571 | if (node->nodetype == LYS_LIST) { |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 4572 | if (rfn->flags & LYS_RFN_MINSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4573 | ((struct lys_node_list *)node)->min = rfn->mod.list.min; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4574 | } |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 4575 | if (rfn->flags & LYS_RFN_MAXSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4576 | ((struct lys_node_list *)node)->max = rfn->mod.list.max; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4577 | } |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4578 | } else if (node->nodetype == LYS_LEAFLIST) { |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 4579 | if (rfn->flags & LYS_RFN_MINSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4580 | ((struct lys_node_leaflist *)node)->min = rfn->mod.list.min; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4581 | } |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 4582 | if (rfn->flags & LYS_RFN_MAXSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4583 | ((struct lys_node_leaflist *)node)->max = rfn->mod.list.max; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4584 | } |
| 4585 | } |
| 4586 | |
| 4587 | /* must in leaf, leaf-list, list, container or anyxml */ |
| 4588 | if (rfn->must_size) { |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4589 | switch (node->nodetype) { |
| 4590 | case LYS_LEAF: |
| 4591 | old_size = &((struct lys_node_leaf *)node)->must_size; |
| 4592 | old_must = &((struct lys_node_leaf *)node)->must; |
| 4593 | break; |
| 4594 | case LYS_LEAFLIST: |
| 4595 | old_size = &((struct lys_node_leaflist *)node)->must_size; |
| 4596 | old_must = &((struct lys_node_leaflist *)node)->must; |
| 4597 | break; |
| 4598 | case LYS_LIST: |
| 4599 | old_size = &((struct lys_node_list *)node)->must_size; |
| 4600 | old_must = &((struct lys_node_list *)node)->must; |
| 4601 | break; |
| 4602 | case LYS_CONTAINER: |
| 4603 | old_size = &((struct lys_node_container *)node)->must_size; |
| 4604 | old_must = &((struct lys_node_container *)node)->must; |
| 4605 | break; |
| 4606 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4607 | case LYS_ANYDATA: |
| 4608 | old_size = &((struct lys_node_anydata *)node)->must_size; |
| 4609 | old_must = &((struct lys_node_anydata *)node)->must; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4610 | break; |
| 4611 | default: |
| 4612 | LOGINT; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4613 | goto fail; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4614 | } |
| 4615 | |
| 4616 | size = *old_size + rfn->must_size; |
| 4617 | must = realloc(*old_must, size * sizeof *rfn->must); |
| 4618 | if (!must) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4619 | LOGMEM; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4620 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4621 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4622 | for (k = 0, j = *old_size; k < rfn->must_size; k++, j++) { |
| 4623 | must[j].expr = lydict_insert(ctx, rfn->must[k].expr, 0); |
| 4624 | must[j].dsc = lydict_insert(ctx, rfn->must[k].dsc, 0); |
| 4625 | must[j].ref = lydict_insert(ctx, rfn->must[k].ref, 0); |
| 4626 | must[j].eapptag = lydict_insert(ctx, rfn->must[k].eapptag, 0); |
| 4627 | must[j].emsg = lydict_insert(ctx, rfn->must[k].emsg, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4628 | } |
| 4629 | |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4630 | *old_must = must; |
| 4631 | *old_size = size; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 4632 | |
| 4633 | /* check XPath dependencies again */ |
| 4634 | if (unres_schema_add_node(node->module, unres, node, UNRES_XPATH, NULL) == -1) { |
| 4635 | goto fail; |
| 4636 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4637 | } |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4638 | |
| 4639 | /* if-feature in leaf, leaf-list, list, container or anyxml */ |
| 4640 | if (rfn->iffeature_size) { |
| 4641 | old_size = &node->iffeature_size; |
| 4642 | old_iff = &node->iffeature; |
| 4643 | |
| 4644 | size = *old_size + rfn->iffeature_size; |
| 4645 | iff = realloc(*old_iff, size * sizeof *rfn->iffeature); |
| 4646 | if (!iff) { |
| 4647 | LOGMEM; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4648 | goto fail; |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4649 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4650 | for (k = 0, j = *old_size; k < rfn->iffeature_size; k++, j++) { |
| 4651 | resolve_iffeature_getsizes(&rfn->iffeature[k], &usize1, &usize2); |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4652 | if (usize1) { |
| 4653 | /* there is something to duplicate */ |
| 4654 | /* duplicate compiled expression */ |
| 4655 | usize = (usize1 / 4) + (usize1 % 4) ? 1 : 0; |
| 4656 | iff[j].expr = malloc(usize * sizeof *iff[j].expr); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4657 | memcpy(iff[j].expr, rfn->iffeature[k].expr, usize * sizeof *iff[j].expr); |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4658 | |
| 4659 | /* duplicate list of feature pointers */ |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4660 | iff[j].features = malloc(usize2 * sizeof *iff[k].features); |
| 4661 | memcpy(iff[j].features, rfn->iffeature[k].features, usize2 * sizeof *iff[j].features); |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4662 | } |
| 4663 | } |
| 4664 | |
| 4665 | *old_iff = iff; |
| 4666 | *old_size = size; |
| 4667 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4668 | } |
| 4669 | |
| 4670 | /* apply augments */ |
| 4671 | for (i = 0; i < uses->augment_size; i++) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4672 | rc = resolve_augment(&uses->augment[i], uses->child); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4673 | if (rc) { |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4674 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4675 | } |
| 4676 | } |
| 4677 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4678 | /* check refines */ |
| 4679 | for (i = 0; i < uses->refine_size; i++) { |
| 4680 | node = refine_nodes[i]; |
| 4681 | rfn = &uses->refine[i]; |
| 4682 | |
| 4683 | /* config on any nodetype */ |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4684 | if ((rfn->flags & LYS_CONFIG_MASK) && !clear_config) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4685 | for (parent = lys_parent(node); parent && parent->nodetype == LYS_USES; parent = lys_parent(parent)); |
| 4686 | if (parent && parent->nodetype != LYS_GROUPING && |
| 4687 | ((parent->flags & LYS_CONFIG_MASK) != (rfn->flags & LYS_CONFIG_MASK)) && |
| 4688 | (rfn->flags & LYS_CONFIG_W)) { |
| 4689 | /* setting config true under config false is prohibited */ |
| 4690 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, "config", "refine"); |
| 4691 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, |
| 4692 | "changing config from 'false' to 'true' is prohibited while " |
| 4693 | "the target's parent is still config 'false'."); |
| 4694 | goto fail; |
| 4695 | } |
| 4696 | |
| 4697 | /* inherit config change to the target children */ |
| 4698 | LY_TREE_DFS_BEGIN(node->child, next, iter) { |
| 4699 | if (rfn->flags & LYS_CONFIG_W) { |
| 4700 | if (iter->flags & LYS_CONFIG_SET) { |
| 4701 | /* config is set explicitely, go to next sibling */ |
| 4702 | next = NULL; |
| 4703 | goto nextsibling; |
| 4704 | } |
| 4705 | } else { /* LYS_CONFIG_R */ |
| 4706 | if ((iter->flags & LYS_CONFIG_SET) && (iter->flags & LYS_CONFIG_W)) { |
| 4707 | /* error - we would have config data under status data */ |
| 4708 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, "config", "refine"); |
| 4709 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, |
| 4710 | "changing config from 'true' to 'false' is prohibited while the target " |
| 4711 | "has still a children with explicit config 'true'."); |
| 4712 | goto fail; |
| 4713 | } |
| 4714 | } |
| 4715 | /* change config */ |
| 4716 | iter->flags &= ~LYS_CONFIG_MASK; |
| 4717 | iter->flags |= (rfn->flags & LYS_CONFIG_MASK); |
| 4718 | |
| 4719 | /* select next iter - modified LY_TREE_DFS_END */ |
| 4720 | if (iter->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
| 4721 | next = NULL; |
| 4722 | } else { |
| 4723 | next = iter->child; |
| 4724 | } |
| 4725 | nextsibling: |
| 4726 | if (!next) { |
| 4727 | /* try siblings */ |
| 4728 | next = iter->next; |
| 4729 | } |
| 4730 | while (!next) { |
| 4731 | /* parent is already processed, go to its sibling */ |
| 4732 | iter = lys_parent(iter); |
| 4733 | |
| 4734 | /* no siblings, go back through parents */ |
| 4735 | if (iter == node) { |
| 4736 | /* we are done, no next element to process */ |
| 4737 | break; |
| 4738 | } |
| 4739 | next = iter->next; |
| 4740 | } |
| 4741 | } |
| 4742 | } |
| 4743 | |
| 4744 | /* default value */ |
| 4745 | if (rfn->dflt_size && node->nodetype == LYS_CHOICE) { |
| 4746 | /* choice */ |
| 4747 | |
| 4748 | ((struct lys_node_choice *)node)->dflt = resolve_choice_dflt((struct lys_node_choice *)node, |
| 4749 | rfn->dflt[0]); |
| 4750 | if (!((struct lys_node_choice *)node)->dflt) { |
| 4751 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->dflt[0], "default"); |
| 4752 | goto fail; |
| 4753 | } |
| 4754 | if (lyp_check_mandatory_choice(node)) { |
| 4755 | goto fail; |
| 4756 | } |
| 4757 | } |
| 4758 | |
| 4759 | /* min/max-elements on list or leaf-list */ |
| 4760 | if (node->nodetype == LYS_LIST) { |
| 4761 | if (((struct lys_node_list *)node)->min > ((struct lys_node_list *)node)->max) { |
| 4762 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid value \"%d\" of \"%s\".", rfn->mod.list.min, "min-elements"); |
| 4763 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"min-elements\" is bigger than \"max-elements\"."); |
| 4764 | goto fail; |
| 4765 | } |
| 4766 | } else if (node->nodetype == LYS_LEAFLIST) { |
| 4767 | if (((struct lys_node_leaflist *)node)->min > ((struct lys_node_leaflist *)node)->max) { |
| 4768 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid value \"%d\" of \"%s\".", rfn->mod.list.min, "min-elements"); |
| 4769 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"min-elements\" is bigger than \"max-elements\"."); |
| 4770 | goto fail; |
| 4771 | } |
| 4772 | } |
| 4773 | |
| 4774 | /* additional checks */ |
| 4775 | if (node->nodetype == LYS_LEAFLIST) { |
| 4776 | llist = (struct lys_node_leaflist *)node; |
| 4777 | if (llist->dflt_size && llist->min) { |
| 4778 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, rfn->dflt_size ? "default" : "min-elements", "refine"); |
| 4779 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, |
| 4780 | "The \"min-elements\" statement with non-zero value is forbidden on leaf-lists with the \"default\" statement."); |
| 4781 | goto fail; |
| 4782 | } |
| 4783 | } |
| 4784 | if ((rfn->flags & LYS_MAND_TRUE) || rfn->mod.list.min) { |
| 4785 | /* check for mandatory node in default case, first find the closest parent choice to the changed node */ |
| 4786 | for (parent = node->parent; |
| 4787 | parent && !(parent->nodetype & (LYS_CHOICE | LYS_GROUPING | LYS_ACTION | LYS_USES)); |
| 4788 | parent = parent->parent) { |
| 4789 | if (parent->nodetype == LYS_CONTAINER && ((struct lys_node_container *)parent)->presence) { |
| 4790 | /* stop also on presence containers */ |
| 4791 | break; |
| 4792 | } |
| 4793 | } |
| 4794 | /* and if it is a choice with the default case, check it for presence of a mandatory node in it */ |
| 4795 | if (parent && parent->nodetype == LYS_CHOICE && ((struct lys_node_choice *)parent)->dflt) { |
| 4796 | if (lyp_check_mandatory_choice(parent)) { |
| 4797 | goto fail; |
| 4798 | } |
| 4799 | } |
| 4800 | } |
| 4801 | } |
| 4802 | free(refine_nodes); |
| 4803 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4804 | return EXIT_SUCCESS; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4805 | |
| 4806 | fail: |
| 4807 | LY_TREE_FOR_SAFE(uses->child, next, iter) { |
| 4808 | lys_node_free(iter, NULL, 0); |
| 4809 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4810 | free(refine_nodes); |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4811 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4812 | } |
| 4813 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4814 | static int |
| 4815 | identity_backlink_update(struct lys_ident *der, struct lys_ident *base) |
| 4816 | { |
| 4817 | int i; |
| 4818 | |
| 4819 | assert(der && base); |
| 4820 | |
| 4821 | base->der = ly_realloc(base->der, (base->der_size + 1) * sizeof *(base->der)); |
| 4822 | if (!base->der) { |
| 4823 | LOGMEM; |
| 4824 | return EXIT_FAILURE; |
| 4825 | } |
| 4826 | base->der[base->der_size++] = der; |
| 4827 | |
| 4828 | for (i = 0; i < base->base_size; i++) { |
| 4829 | if (identity_backlink_update(der, base->base[i])) { |
| 4830 | return EXIT_FAILURE; |
| 4831 | } |
| 4832 | } |
| 4833 | |
| 4834 | return EXIT_SUCCESS; |
| 4835 | } |
| 4836 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4837 | /** |
| 4838 | * @brief Resolve base identity recursively. Does not log. |
| 4839 | * |
| 4840 | * @param[in] module Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4841 | * @param[in] ident Identity to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4842 | * @param[in] basename Base name of the identity. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4843 | * @param[out] ret Pointer to the resolved identity. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4844 | * |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 4845 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on crucial error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4846 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4847 | static int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 4848 | resolve_base_ident_sub(const struct lys_module *module, struct lys_ident *ident, const char *basename, |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4849 | struct unres_schema *unres, struct lys_ident **ret) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4850 | { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 4851 | uint32_t i, j; |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4852 | struct lys_ident *base = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4853 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 4854 | assert(ret); |
| 4855 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4856 | /* search module */ |
| 4857 | for (i = 0; i < module->ident_size; i++) { |
| 4858 | if (!strcmp(basename, module->ident[i].name)) { |
| 4859 | |
| 4860 | if (!ident) { |
| 4861 | /* just search for type, so do not modify anything, just return |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 4862 | * the base identity pointer */ |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 4863 | *ret = &module->ident[i]; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4864 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4865 | } |
| 4866 | |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 4867 | base = &module->ident[i]; |
| 4868 | goto matchfound; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4869 | } |
| 4870 | } |
| 4871 | |
| 4872 | /* search submodules */ |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 4873 | for (j = 0; j < module->inc_size && module->inc[j].submodule; j++) { |
| 4874 | for (i = 0; i < module->inc[j].submodule->ident_size; i++) { |
| 4875 | if (!strcmp(basename, module->inc[j].submodule->ident[i].name)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4876 | |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 4877 | if (!ident) { |
| 4878 | *ret = &module->inc[j].submodule->ident[i]; |
| 4879 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4880 | } |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 4881 | |
| 4882 | base = &module->inc[j].submodule->ident[i]; |
| 4883 | goto matchfound; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4884 | } |
| 4885 | } |
| 4886 | } |
| 4887 | |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 4888 | matchfound: |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4889 | /* we found it somewhere */ |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 4890 | if (base) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4891 | /* is it already completely resolved? */ |
| 4892 | for (i = 0; i < unres->count; i++) { |
Radek Krejci | ba7b1cc | 2016-08-08 13:44:43 +0200 | [diff] [blame] | 4893 | if ((unres->item[i] == base) && (unres->type[i] == UNRES_IDENT)) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4894 | /* identity found, but not yet resolved, so do not return it in *res and try it again later */ |
| 4895 | |
| 4896 | /* simple check for circular reference, |
| 4897 | * the complete check is done as a side effect of using only completely |
| 4898 | * resolved identities (previous check of unres content) */ |
| 4899 | if (ly_strequal((const char *)unres->str_snode[i], ident->name, 1)) { |
| 4900 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, basename, "base"); |
| 4901 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Circular reference of \"%s\" identity.", basename); |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 4902 | return -1; |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4903 | } |
| 4904 | |
Radek Krejci | 06f64ed | 2016-08-15 11:07:44 +0200 | [diff] [blame] | 4905 | return EXIT_FAILURE; |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 4906 | } |
| 4907 | } |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4908 | |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 4909 | /* checks done, store the result */ |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4910 | *ret = base; |
Pavol Vican | fdab9f9 | 2016-09-07 15:23:27 +0200 | [diff] [blame] | 4911 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4912 | } |
| 4913 | |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 4914 | /* base not found (maybe a forward reference) */ |
| 4915 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4916 | } |
| 4917 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4918 | /** |
| 4919 | * @brief Resolve base identity. Logs directly. |
| 4920 | * |
| 4921 | * @param[in] module Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4922 | * @param[in] ident Identity to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4923 | * @param[in] basename Base name of the identity. |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 4924 | * @param[in] parent Either "type" or "identity". |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 4925 | * @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] | 4926 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4927 | * @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] | 4928 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4929 | static int |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 4930 | resolve_base_ident(const struct lys_module *module, struct lys_ident *ident, const char *basename, const char *parent, |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4931 | struct lys_type *type, struct unres_schema *unres) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4932 | { |
| 4933 | const char *name; |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 4934 | int mod_name_len = 0, rc; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 4935 | struct lys_ident *target, **ret; |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 4936 | uint16_t flags; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 4937 | struct lys_module *mod; |
| 4938 | |
| 4939 | assert((ident && !type) || (!ident && type)); |
| 4940 | |
| 4941 | if (!type) { |
| 4942 | /* have ident to resolve */ |
| 4943 | ret = ⌖ |
| 4944 | flags = ident->flags; |
| 4945 | mod = ident->module; |
| 4946 | } else { |
| 4947 | /* have type to fill */ |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 4948 | ++type->info.ident.count; |
| 4949 | type->info.ident.ref = ly_realloc(type->info.ident.ref, type->info.ident.count * sizeof *type->info.ident.ref); |
| 4950 | if (!type->info.ident.ref) { |
| 4951 | LOGMEM; |
| 4952 | return -1; |
| 4953 | } |
| 4954 | |
| 4955 | ret = &type->info.ident.ref[type->info.ident.count - 1]; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 4956 | flags = type->parent->flags; |
| 4957 | mod = type->parent->module; |
| 4958 | } |
Michal Vasko | f200600 | 2016-04-21 16:28:15 +0200 | [diff] [blame] | 4959 | *ret = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4960 | |
| 4961 | /* search for the base identity */ |
| 4962 | name = strchr(basename, ':'); |
| 4963 | if (name) { |
| 4964 | /* set name to correct position after colon */ |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 4965 | mod_name_len = name - basename; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4966 | name++; |
| 4967 | |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 4968 | 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] | 4969 | /* prefix refers to the current module, ignore it */ |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 4970 | mod_name_len = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4971 | } |
| 4972 | } else { |
| 4973 | name = basename; |
| 4974 | } |
| 4975 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 4976 | /* get module where to search */ |
| 4977 | module = lys_get_import_module(module, NULL, 0, mod_name_len ? basename : NULL, mod_name_len); |
| 4978 | if (!module) { |
| 4979 | /* identity refers unknown data model */ |
Radek Krejci | 02a0499 | 2016-03-17 16:06:37 +0100 | [diff] [blame] | 4980 | LOGVAL(LYE_INMOD, LY_VLOG_NONE, NULL, basename); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 4981 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4982 | } |
| 4983 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 4984 | /* search in the identified module ... */ |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 4985 | rc = resolve_base_ident_sub(module, ident, name, unres, ret); |
| 4986 | if (!rc) { |
| 4987 | assert(*ret); |
| 4988 | |
| 4989 | /* check status */ |
| 4990 | if (lyp_check_status(flags, mod, ident ? ident->name : "of type", |
| 4991 | (*ret)->flags, (*ret)->module, (*ret)->name, NULL)) { |
| 4992 | rc = -1; |
Pavol Vican | fdab9f9 | 2016-09-07 15:23:27 +0200 | [diff] [blame] | 4993 | } else { |
| 4994 | if (ident) { |
| 4995 | ident->base[ident->base_size++] = *ret; |
| 4996 | |
| 4997 | /* maintain backlinks to the derived identities */ |
| 4998 | rc = identity_backlink_update(ident, *ret) ? -1 : EXIT_SUCCESS; |
| 4999 | } |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5000 | } |
| 5001 | } else if (rc == EXIT_FAILURE) { |
| 5002 | LOGVAL(LYE_INRESOLV, LY_VLOG_NONE, NULL, parent, basename); |
Pavol Vican | 053a288 | 2016-09-05 14:40:33 +0200 | [diff] [blame] | 5003 | if (type) { |
| 5004 | --type->info.ident.count; |
| 5005 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5006 | } |
| 5007 | |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5008 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5009 | } |
| 5010 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5011 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 5012 | * @brief Resolve JSON data format identityref. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5013 | * |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5014 | * @param[in] type Identityref type. |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 5015 | * @param[in] ident_name Identityref name. |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 5016 | * @param[in] node Node where the identityref is being resolved |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5017 | * |
| 5018 | * @return Pointer to the identity resolvent, NULL on error. |
| 5019 | */ |
Radek Krejci | a52656e | 2015-08-05 13:41:50 +0200 | [diff] [blame] | 5020 | struct lys_ident * |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5021 | resolve_identref(struct lys_type *type, const char *ident_name, struct lyd_node *node) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5022 | { |
Michal Vasko | c633ca0 | 2015-08-21 14:03:51 +0200 | [diff] [blame] | 5023 | const char *mod_name, *name; |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5024 | int mod_name_len, rc, i, j; |
| 5025 | struct lys_ident *der, *cur; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5026 | |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5027 | if (!type || (!type->info.ident.count && !type->der) || !ident_name) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5028 | return NULL; |
| 5029 | } |
| 5030 | |
Michal Vasko | c633ca0 | 2015-08-21 14:03:51 +0200 | [diff] [blame] | 5031 | 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] | 5032 | if (rc < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5033 | 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] | 5034 | return NULL; |
Michal Vasko | b43bb3c | 2016-03-17 15:00:27 +0100 | [diff] [blame] | 5035 | } else if (rc < (signed)strlen(ident_name)) { |
Radek Krejci | 02a0499 | 2016-03-17 16:06:37 +0100 | [diff] [blame] | 5036 | 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] | 5037 | return NULL; |
| 5038 | } |
| 5039 | |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5040 | /* go through all the bases in all the derived types */ |
| 5041 | while (type->der) { |
| 5042 | for (i = 0; i < type->info.ident.count; ++i) { |
| 5043 | cur = type->info.ident.ref[i]; |
| 5044 | if (!strcmp(cur->name, name) && (!mod_name |
| 5045 | || (!strncmp(cur->module->name, mod_name, mod_name_len) && !cur->module->name[mod_name_len]))) { |
| 5046 | goto match; |
| 5047 | } |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 5048 | |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5049 | for (j = 0; j < cur->der_size; j++) { |
| 5050 | der = cur->der[j]; /* shortcut */ |
| 5051 | if (!strcmp(der->name, name) && |
| 5052 | (!mod_name || (!strncmp(der->module->name, mod_name, mod_name_len) && !der->module->name[mod_name_len]))) { |
| 5053 | /* we have match */ |
| 5054 | cur = der; |
| 5055 | goto match; |
| 5056 | } |
| 5057 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5058 | } |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5059 | type = &type->der->type; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5060 | } |
| 5061 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5062 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYD, node, "identityref", ident_name); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5063 | return NULL; |
Radek Krejci | f1ee2e2 | 2016-08-02 16:36:48 +0200 | [diff] [blame] | 5064 | |
| 5065 | match: |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5066 | for (i = 0; i < cur->iffeature_size; i++) { |
| 5067 | if (!resolve_iffeature(&cur->iffeature[i])) { |
| 5068 | LOGVAL(LYE_INVAL, LY_VLOG_LYD, node, cur->name, node->schema->name); |
Radek Krejci | f1ee2e2 | 2016-08-02 16:36:48 +0200 | [diff] [blame] | 5069 | LOGVAL(LYE_SPEC, LY_VLOG_LYD, node, "Identity \"%s\" is disabled by its if-feature condition.", |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5070 | cur->name); |
Radek Krejci | f1ee2e2 | 2016-08-02 16:36:48 +0200 | [diff] [blame] | 5071 | return NULL; |
| 5072 | } |
| 5073 | } |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5074 | return cur; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5075 | } |
| 5076 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5077 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5078 | * @brief Resolve unresolved uses. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5079 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5080 | * @param[in] uses Uses to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5081 | * @param[in] unres Specific unres item. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5082 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5083 | * @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] | 5084 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5085 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5086 | 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] | 5087 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5088 | int rc; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5089 | struct lys_node *par_grp; |
Michal Vasko | e91afce | 2015-08-12 12:21:00 +0200 | [diff] [blame] | 5090 | |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5091 | /* HACK: when a grouping has uses inside, all such uses have to be resolved before the grouping itself |
| 5092 | * is used in some uses. When we see such a uses, the grouping's nacm member (not used in grouping) |
| 5093 | * is used to store number of so far unresolved uses. The grouping cannot be used unless the nacm |
| 5094 | * value is decreased back to 0. To remember that the uses already increased grouping's nacm, the |
| 5095 | * LYS_USESGRP flag is used. */ |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 5096 | for (par_grp = lys_parent((struct lys_node *)uses); par_grp && (par_grp->nodetype != LYS_GROUPING); par_grp = lys_parent(par_grp)); |
Michal Vasko | e91afce | 2015-08-12 12:21:00 +0200 | [diff] [blame] | 5097 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5098 | if (!uses->grp) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 5099 | rc = resolve_uses_schema_nodeid(uses->name, (const struct lys_node *)uses, (const struct lys_node_grp **)&uses->grp); |
| 5100 | if (rc == -1) { |
Michal Vasko | 92981a6 | 2016-10-14 10:25:16 +0200 | [diff] [blame] | 5101 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 5102 | return -1; |
| 5103 | } else if (rc > 0) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5104 | 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] | 5105 | return -1; |
| 5106 | } else if (!uses->grp) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5107 | if (par_grp && !(uses->flags & LYS_USESGRP)) { |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 5108 | /* hack - in contrast to lys_node, lys_node_grp has bigger nacm field |
| 5109 | * (and smaller flags - it uses only a limited set of flags) |
| 5110 | */ |
| 5111 | ((struct lys_node_grp *)par_grp)->nacm++; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5112 | uses->flags |= LYS_USESGRP; |
Michal Vasko | 407f1bb | 2015-09-23 15:51:07 +0200 | [diff] [blame] | 5113 | } |
Michal Vasko | 92981a6 | 2016-10-14 10:25:16 +0200 | [diff] [blame] | 5114 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 5115 | return EXIT_FAILURE; |
Michal Vasko | 12e3084 | 2015-08-04 11:54:00 +0200 | [diff] [blame] | 5116 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5117 | } |
| 5118 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5119 | if (uses->grp->nacm) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5120 | if (par_grp && !(uses->flags & LYS_USESGRP)) { |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 5121 | ((struct lys_node_grp *)par_grp)->nacm++; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5122 | uses->flags |= LYS_USESGRP; |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 5123 | } else { |
| 5124 | /* instantiate grouping only when it is completely resolved */ |
| 5125 | uses->grp = NULL; |
Michal Vasko | 407f1bb | 2015-09-23 15:51:07 +0200 | [diff] [blame] | 5126 | } |
Michal Vasko | 92981a6 | 2016-10-14 10:25:16 +0200 | [diff] [blame] | 5127 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5128 | return EXIT_FAILURE; |
| 5129 | } |
| 5130 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5131 | rc = resolve_uses(uses, unres); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5132 | if (!rc) { |
| 5133 | /* decrease unres count only if not first try */ |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5134 | if (par_grp && (uses->flags & LYS_USESGRP)) { |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 5135 | if (!((struct lys_node_grp *)par_grp)->nacm) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5136 | LOGINT; |
| 5137 | return -1; |
| 5138 | } |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 5139 | ((struct lys_node_grp *)par_grp)->nacm--; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5140 | uses->flags &= ~LYS_USESGRP; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5141 | } |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5142 | |
| 5143 | /* check status */ |
Radek Krejci | c655602 | 2016-01-27 15:16:45 +0100 | [diff] [blame] | 5144 | if (lyp_check_status(uses->flags, uses->module, "of uses", |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 5145 | uses->grp->flags, uses->grp->module, uses->grp->name, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5146 | (struct lys_node *)uses)) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5147 | return -1; |
| 5148 | } |
| 5149 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5150 | return EXIT_SUCCESS; |
| 5151 | } |
| 5152 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5153 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5154 | } |
| 5155 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5156 | /** |
Michal Vasko | 9957e59 | 2015-08-17 15:04:09 +0200 | [diff] [blame] | 5157 | * @brief Resolve list keys. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5158 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5159 | * @param[in] list List to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5160 | * @param[in] keys_str Keys node value. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5161 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5162 | * @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] | 5163 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5164 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5165 | resolve_list_keys(struct lys_node_list *list, const char *keys_str) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5166 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5167 | int i, len, rc; |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 5168 | const char *value; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5169 | |
| 5170 | for (i = 0; i < list->keys_size; ++i) { |
| 5171 | /* get the key name */ |
| 5172 | if ((value = strpbrk(keys_str, " \t\n"))) { |
| 5173 | len = value - keys_str; |
| 5174 | while (isspace(value[0])) { |
| 5175 | value++; |
| 5176 | } |
| 5177 | } else { |
| 5178 | len = strlen(keys_str); |
| 5179 | } |
| 5180 | |
Radek Krejci | c428344 | 2016-04-22 09:19:27 +0200 | [diff] [blame] | 5181 | rc = lys_get_sibling(list->child, lys_main_module(list->module)->name, 0, keys_str, len, LYS_LEAF, (const struct lys_node **)&list->keys[i]); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5182 | if (rc) { |
Michal Vasko | 7a55bea | 2016-05-02 14:51:20 +0200 | [diff] [blame] | 5183 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, list, "list keys", keys_str); |
| 5184 | return EXIT_FAILURE; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5185 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5186 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5187 | if (check_key(list, i, keys_str, len)) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5188 | /* check_key logs */ |
| 5189 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5190 | } |
| 5191 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5192 | /* check status */ |
Radek Krejci | c655602 | 2016-01-27 15:16:45 +0100 | [diff] [blame] | 5193 | if (lyp_check_status(list->flags, list->module, list->name, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5194 | list->keys[i]->flags, list->keys[i]->module, list->keys[i]->name, |
| 5195 | (struct lys_node *)list->keys[i])) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5196 | return -1; |
| 5197 | } |
| 5198 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5199 | /* prepare for next iteration */ |
| 5200 | while (value && isspace(value[0])) { |
| 5201 | value++; |
| 5202 | } |
| 5203 | keys_str = value; |
| 5204 | } |
| 5205 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5206 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5207 | } |
| 5208 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5209 | /** |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5210 | * @brief Resolve (check) all must conditions of \p node. |
| 5211 | * Logs directly. |
| 5212 | * |
| 5213 | * @param[in] node Data node with optional must statements. |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5214 | * @param[in] inout_parent If set, must in input or output parent of node->schema will be resolved. |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5215 | * |
| 5216 | * @return EXIT_SUCCESS on pass, EXIT_FAILURE on fail, -1 on error. |
| 5217 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5218 | static int |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5219 | resolve_must(struct lyd_node *node, int inout_parent) |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5220 | { |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5221 | uint8_t i, must_size; |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5222 | struct lys_node *schema; |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5223 | struct lys_restr *must; |
| 5224 | struct lyxp_set set; |
| 5225 | |
| 5226 | assert(node); |
| 5227 | memset(&set, 0, sizeof set); |
| 5228 | |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5229 | if (inout_parent) { |
| 5230 | for (schema = lys_parent(node->schema); |
| 5231 | schema && (schema->nodetype & (LYS_CHOICE | LYS_CASE | LYS_USES)); |
| 5232 | schema = lys_parent(schema)); |
| 5233 | if (!schema || !(schema->nodetype & (LYS_INPUT | LYS_OUTPUT))) { |
| 5234 | LOGINT; |
| 5235 | return -1; |
| 5236 | } |
| 5237 | must_size = ((struct lys_node_inout *)schema)->must_size; |
| 5238 | must = ((struct lys_node_inout *)schema)->must; |
| 5239 | |
| 5240 | /* context node is the RPC/action */ |
| 5241 | node = node->parent; |
| 5242 | if (!(node->schema->nodetype & (LYS_RPC | LYS_ACTION))) { |
| 5243 | LOGINT; |
| 5244 | return -1; |
| 5245 | } |
| 5246 | } else { |
| 5247 | switch (node->schema->nodetype) { |
| 5248 | case LYS_CONTAINER: |
| 5249 | must_size = ((struct lys_node_container *)node->schema)->must_size; |
| 5250 | must = ((struct lys_node_container *)node->schema)->must; |
| 5251 | break; |
| 5252 | case LYS_LEAF: |
| 5253 | must_size = ((struct lys_node_leaf *)node->schema)->must_size; |
| 5254 | must = ((struct lys_node_leaf *)node->schema)->must; |
| 5255 | break; |
| 5256 | case LYS_LEAFLIST: |
| 5257 | must_size = ((struct lys_node_leaflist *)node->schema)->must_size; |
| 5258 | must = ((struct lys_node_leaflist *)node->schema)->must; |
| 5259 | break; |
| 5260 | case LYS_LIST: |
| 5261 | must_size = ((struct lys_node_list *)node->schema)->must_size; |
| 5262 | must = ((struct lys_node_list *)node->schema)->must; |
| 5263 | break; |
| 5264 | case LYS_ANYXML: |
| 5265 | case LYS_ANYDATA: |
| 5266 | must_size = ((struct lys_node_anydata *)node->schema)->must_size; |
| 5267 | must = ((struct lys_node_anydata *)node->schema)->must; |
| 5268 | break; |
| 5269 | case LYS_NOTIF: |
| 5270 | must_size = ((struct lys_node_notif *)node->schema)->must_size; |
| 5271 | must = ((struct lys_node_notif *)node->schema)->must; |
| 5272 | break; |
| 5273 | default: |
| 5274 | must_size = 0; |
| 5275 | break; |
| 5276 | } |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5277 | } |
| 5278 | |
| 5279 | for (i = 0; i < must_size; ++i) { |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5280 | if (lyxp_eval(must[i].expr, node, LYXP_NODE_ELEM, &set, LYXP_MUST)) { |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5281 | return -1; |
| 5282 | } |
| 5283 | |
Michal Vasko | 944a564 | 2016-03-21 11:48:58 +0100 | [diff] [blame] | 5284 | lyxp_set_cast(&set, LYXP_SET_BOOLEAN, node, LYXP_MUST); |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5285 | |
Michal Vasko | 8146d4c | 2016-05-09 15:50:29 +0200 | [diff] [blame] | 5286 | if (!set.val.bool) { |
Michal Vasko | 6ac6828 | 2016-04-11 10:56:47 +0200 | [diff] [blame] | 5287 | LOGVAL(LYE_NOMUST, LY_VLOG_LYD, node, must[i].expr); |
| 5288 | if (must[i].emsg) { |
| 5289 | LOGVAL(LYE_SPEC, LY_VLOG_LYD, node, must[i].emsg); |
| 5290 | } |
| 5291 | if (must[i].eapptag) { |
| 5292 | strncpy(((struct ly_err *)&ly_errno)->apptag, must[i].eapptag, LY_APPTAG_LEN - 1); |
| 5293 | } |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5294 | return 1; |
| 5295 | } |
| 5296 | } |
| 5297 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5298 | return EXIT_SUCCESS; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5299 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5300 | |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5301 | /** |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5302 | * @brief Resolve (find) when condition schema context node. Does not log. |
| 5303 | * |
| 5304 | * @param[in] schema Schema node with the when condition. |
| 5305 | * @param[out] ctx_snode When schema context node. |
| 5306 | * @param[out] ctx_snode_type Schema context node type. |
| 5307 | */ |
| 5308 | void |
| 5309 | resolve_when_ctx_snode(const struct lys_node *schema, struct lys_node **ctx_snode, enum lyxp_node_type *ctx_snode_type) |
| 5310 | { |
| 5311 | const struct lys_node *sparent; |
| 5312 | |
| 5313 | /* find a not schema-only node */ |
| 5314 | *ctx_snode_type = LYXP_NODE_ELEM; |
| 5315 | while (schema->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE | LYS_AUGMENT | LYS_INPUT | LYS_OUTPUT)) { |
| 5316 | if (schema->nodetype == LYS_AUGMENT) { |
| 5317 | sparent = ((struct lys_node_augment *)schema)->target; |
| 5318 | } else { |
| 5319 | sparent = schema->parent; |
| 5320 | } |
| 5321 | if (!sparent) { |
| 5322 | /* context node is the document root (fake root in our case) */ |
| 5323 | if (schema->flags & LYS_CONFIG_W) { |
| 5324 | *ctx_snode_type = LYXP_NODE_ROOT_CONFIG; |
| 5325 | } else { |
Michal Vasko | b94a5e4 | 2016-09-08 14:01:56 +0200 | [diff] [blame] | 5326 | *ctx_snode_type = LYXP_NODE_ROOT; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5327 | } |
Michal Vasko | 2d2aec1 | 2016-09-08 11:42:50 +0200 | [diff] [blame] | 5328 | /* we need the first top-level sibling, but no uses or groupings */ |
Michal Vasko | b94a5e4 | 2016-09-08 14:01:56 +0200 | [diff] [blame] | 5329 | schema = lys_getnext(NULL, NULL, lys_node_module(schema), 0); |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5330 | break; |
| 5331 | } |
| 5332 | schema = sparent; |
| 5333 | } |
| 5334 | |
| 5335 | *ctx_snode = (struct lys_node *)schema; |
| 5336 | } |
| 5337 | |
| 5338 | /** |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5339 | * @brief Resolve (find) when condition context node. Does not log. |
| 5340 | * |
| 5341 | * @param[in] node Data node, whose conditional definition is being decided. |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5342 | * @param[in] schema Schema node with the when condition. |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5343 | * @param[out] ctx_node Context node. |
| 5344 | * @param[out] ctx_node_type Context node type. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5345 | * |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5346 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5347 | */ |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5348 | static int |
| 5349 | resolve_when_ctx_node(struct lyd_node *node, struct lys_node *schema, struct lyd_node **ctx_node, |
| 5350 | enum lyxp_node_type *ctx_node_type) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5351 | { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5352 | struct lyd_node *parent; |
| 5353 | struct lys_node *sparent; |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5354 | enum lyxp_node_type node_type; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5355 | uint16_t i, data_depth, schema_depth; |
| 5356 | |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5357 | resolve_when_ctx_snode(schema, &schema, &node_type); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5358 | |
Michal Vasko | fe98975 | 2016-09-08 08:47:26 +0200 | [diff] [blame] | 5359 | if (node_type == LYXP_NODE_ELEM) { |
| 5360 | /* standard element context node */ |
| 5361 | for (parent = node, data_depth = 0; parent; parent = parent->parent, ++data_depth); |
| 5362 | for (sparent = schema, schema_depth = 0; |
| 5363 | sparent; |
| 5364 | sparent = (sparent->nodetype == LYS_AUGMENT ? ((struct lys_node_augment *)sparent)->target : sparent->parent)) { |
| 5365 | if (sparent->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_NOTIF | LYS_RPC)) { |
| 5366 | ++schema_depth; |
| 5367 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5368 | } |
Michal Vasko | fe98975 | 2016-09-08 08:47:26 +0200 | [diff] [blame] | 5369 | if (data_depth < schema_depth) { |
| 5370 | return -1; |
| 5371 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5372 | |
Michal Vasko | 956e854 | 2016-08-26 09:43:35 +0200 | [diff] [blame] | 5373 | /* find the corresponding data node */ |
| 5374 | for (i = 0; i < data_depth - schema_depth; ++i) { |
| 5375 | node = node->parent; |
| 5376 | } |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5377 | if (node->schema != schema) { |
| 5378 | return -1; |
| 5379 | } |
Michal Vasko | fe98975 | 2016-09-08 08:47:26 +0200 | [diff] [blame] | 5380 | } else { |
| 5381 | /* root context node */ |
| 5382 | while (node->parent) { |
| 5383 | node = node->parent; |
| 5384 | } |
| 5385 | while (node->prev->next) { |
| 5386 | node = node->prev; |
| 5387 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5388 | } |
| 5389 | |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5390 | *ctx_node = node; |
| 5391 | *ctx_node_type = node_type; |
| 5392 | return EXIT_SUCCESS; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5393 | } |
| 5394 | |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5395 | /** |
| 5396 | * @brief Temporarily unlink nodes as per YANG 1.1 RFC section 7.21.5 for when XPath evaluation. |
| 5397 | * The context nodes is adjusted if needed. |
| 5398 | * |
| 5399 | * @param[in] snode Schema node, whose children instances need to be unlinked. |
| 5400 | * @param[in,out] node Data siblings where to look for the children of \p snode. If it is unlinked, |
| 5401 | * it is moved to point to another sibling still in the original tree. |
| 5402 | * @param[in,out] ctx_node When context node, adjusted if needed. |
| 5403 | * @param[in] ctx_node_type Context node type, just for information to detect invalid situations. |
| 5404 | * @param[out] unlinked_nodes Unlinked siblings. Can be safely appended to \p node afterwards. |
| 5405 | * Ordering may change, but there will be no semantic change. |
| 5406 | * |
| 5407 | * @return EXIT_SUCCESS on success, -1 on error. |
| 5408 | */ |
| 5409 | static int |
| 5410 | resolve_when_unlink_nodes(struct lys_node *snode, struct lyd_node **node, struct lyd_node **ctx_node, |
| 5411 | enum lyxp_node_type ctx_node_type, struct lyd_node **unlinked_nodes) |
| 5412 | { |
| 5413 | struct lyd_node *next, *elem; |
| 5414 | |
| 5415 | switch (snode->nodetype) { |
| 5416 | case LYS_AUGMENT: |
| 5417 | case LYS_USES: |
| 5418 | case LYS_CHOICE: |
| 5419 | case LYS_CASE: |
| 5420 | LY_TREE_FOR(snode->child, snode) { |
| 5421 | if (resolve_when_unlink_nodes(snode, node, ctx_node, ctx_node_type, unlinked_nodes)) { |
| 5422 | return -1; |
| 5423 | } |
| 5424 | } |
| 5425 | break; |
| 5426 | case LYS_CONTAINER: |
| 5427 | case LYS_LIST: |
| 5428 | case LYS_LEAF: |
| 5429 | case LYS_LEAFLIST: |
| 5430 | case LYS_ANYXML: |
| 5431 | case LYS_ANYDATA: |
| 5432 | LY_TREE_FOR_SAFE(lyd_first_sibling(*node), next, elem) { |
| 5433 | if (elem->schema == snode) { |
| 5434 | |
| 5435 | if (elem == *ctx_node) { |
| 5436 | /* We are going to unlink our context node! This normally cannot happen, |
| 5437 | * but we use normal top-level data nodes for faking a document root node, |
| 5438 | * so if this is the context node, we just use the next top-level node. |
| 5439 | * Additionally, it can even happen that there are no top-level data nodes left, |
| 5440 | * all were unlinked, so in this case we pass NULL as the context node/data tree, |
| 5441 | * lyxp_eval() can handle this special situation. |
| 5442 | */ |
| 5443 | if (ctx_node_type == LYXP_NODE_ELEM) { |
| 5444 | LOGINT; |
| 5445 | return -1; |
| 5446 | } |
| 5447 | |
| 5448 | if (elem->prev == elem) { |
| 5449 | /* unlinking last top-level element, use an empty data tree */ |
| 5450 | *ctx_node = NULL; |
| 5451 | } else { |
| 5452 | /* in this case just use the previous/last top-level data node */ |
| 5453 | *ctx_node = elem->prev; |
| 5454 | } |
| 5455 | } else if (elem == *node) { |
| 5456 | /* We are going to unlink the currently processed node. This does not matter that |
| 5457 | * much, but we would lose access to the original data tree, so just move our |
| 5458 | * pointer somewhere still inside it. |
| 5459 | */ |
| 5460 | if ((*node)->prev != *node) { |
| 5461 | *node = (*node)->prev; |
| 5462 | } else { |
| 5463 | /* the processed node with sibings were all unlinked, oh well */ |
| 5464 | *node = NULL; |
| 5465 | } |
| 5466 | } |
| 5467 | |
| 5468 | /* temporarily unlink the node */ |
| 5469 | lyd_unlink(elem); |
| 5470 | if (*unlinked_nodes) { |
| 5471 | if (lyd_insert_after(*unlinked_nodes, elem)) { |
| 5472 | LOGINT; |
| 5473 | return -1; |
| 5474 | } |
| 5475 | } else { |
| 5476 | *unlinked_nodes = elem; |
| 5477 | } |
| 5478 | |
| 5479 | if (snode->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_ANYDATA)) { |
| 5480 | /* there can be only one instance */ |
| 5481 | break; |
| 5482 | } |
| 5483 | } |
| 5484 | } |
| 5485 | break; |
| 5486 | default: |
| 5487 | LOGINT; |
| 5488 | return -1; |
| 5489 | } |
| 5490 | |
| 5491 | return EXIT_SUCCESS; |
| 5492 | } |
| 5493 | |
| 5494 | /** |
| 5495 | * @brief Relink the unlinked nodes back. |
| 5496 | * |
| 5497 | * @param[in] node Data node to link the nodes back to. It can actually be the adjusted context node, |
| 5498 | * we simply need a sibling from the original data tree. |
| 5499 | * @param[in] unlinked_nodes Unlinked nodes to relink to \p node. |
| 5500 | * @param[in] ctx_node_type Context node type to distinguish between \p node being the parent |
| 5501 | * or the sibling of \p unlinked_nodes. |
| 5502 | * |
| 5503 | * @return EXIT_SUCCESS on success, -1 on error. |
| 5504 | */ |
| 5505 | static int |
| 5506 | resolve_when_relink_nodes(struct lyd_node *node, struct lyd_node *unlinked_nodes, enum lyxp_node_type ctx_node_type) |
| 5507 | { |
| 5508 | struct lyd_node *elem; |
| 5509 | |
| 5510 | LY_TREE_FOR_SAFE(unlinked_nodes, unlinked_nodes, elem) { |
| 5511 | if (ctx_node_type == LYXP_NODE_ELEM) { |
| 5512 | if (lyd_insert(node, elem)) { |
| 5513 | return -1; |
| 5514 | } |
| 5515 | } else { |
| 5516 | if (lyd_insert_after(node, elem)) { |
| 5517 | return -1; |
| 5518 | } |
| 5519 | } |
| 5520 | } |
| 5521 | |
| 5522 | return EXIT_SUCCESS; |
| 5523 | } |
| 5524 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5525 | int |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5526 | resolve_applies_must(const struct lyd_node *node) |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5527 | { |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5528 | int ret = 0; |
| 5529 | uint8_t must_size; |
| 5530 | struct lys_node *schema, *iter; |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5531 | |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5532 | assert(node); |
| 5533 | |
| 5534 | schema = node->schema; |
| 5535 | |
| 5536 | /* their own must */ |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5537 | switch (schema->nodetype) { |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5538 | case LYS_CONTAINER: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5539 | must_size = ((struct lys_node_container *)schema)->must_size; |
| 5540 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5541 | case LYS_LEAF: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5542 | must_size = ((struct lys_node_leaf *)schema)->must_size; |
| 5543 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5544 | case LYS_LEAFLIST: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5545 | must_size = ((struct lys_node_leaflist *)schema)->must_size; |
| 5546 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5547 | case LYS_LIST: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5548 | must_size = ((struct lys_node_list *)schema)->must_size; |
| 5549 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5550 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 5551 | case LYS_ANYDATA: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5552 | must_size = ((struct lys_node_anydata *)schema)->must_size; |
| 5553 | break; |
| 5554 | case LYS_NOTIF: |
| 5555 | must_size = ((struct lys_node_notif *)schema)->must_size; |
| 5556 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5557 | default: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5558 | must_size = 0; |
| 5559 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5560 | } |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5561 | |
| 5562 | if (must_size) { |
| 5563 | ++ret; |
| 5564 | } |
| 5565 | |
| 5566 | /* schema may be a direct data child of input/output with must (but it must be first, it needs to be evaluated only once) */ |
| 5567 | if (!node->prev->next) { |
| 5568 | for (iter = lys_parent(schema); iter && (iter->nodetype & (LYS_CHOICE | LYS_CASE | LYS_USES)); iter = lys_parent(iter)); |
| 5569 | if (iter && (iter->nodetype & (LYS_INPUT | LYS_OUTPUT))) { |
| 5570 | ret += 0x2; |
| 5571 | } |
| 5572 | } |
| 5573 | |
| 5574 | return ret; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5575 | } |
| 5576 | |
| 5577 | int |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5578 | resolve_applies_when(const struct lys_node *schema, int mode, const struct lys_node *stop) |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5579 | { |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5580 | const struct lys_node *parent; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5581 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5582 | assert(schema); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5583 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5584 | if (!(schema->nodetype & (LYS_NOTIF | LYS_RPC)) && (((struct lys_node_container *)schema)->when)) { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5585 | return 1; |
| 5586 | } |
| 5587 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5588 | parent = schema; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5589 | goto check_augment; |
| 5590 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5591 | while (parent) { |
| 5592 | /* stop conditions */ |
| 5593 | if (!mode) { |
| 5594 | /* stop on node that can be instantiated in data tree */ |
| 5595 | if (!(parent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) { |
| 5596 | break; |
| 5597 | } |
| 5598 | } else { |
| 5599 | /* stop on the specified node */ |
| 5600 | if (parent == stop) { |
| 5601 | break; |
| 5602 | } |
| 5603 | } |
| 5604 | |
| 5605 | if (((const struct lys_node_uses *)parent)->when) { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5606 | return 1; |
| 5607 | } |
| 5608 | check_augment: |
| 5609 | |
| 5610 | if ((parent->parent && (parent->parent->nodetype == LYS_AUGMENT) && |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5611 | (((const struct lys_node_augment *)parent->parent)->when))) { |
Michal Vasko | e365556 | 2016-08-24 15:56:17 +0200 | [diff] [blame] | 5612 | return 1; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5613 | } |
| 5614 | parent = lys_parent(parent); |
| 5615 | } |
| 5616 | |
| 5617 | return 0; |
| 5618 | } |
| 5619 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5620 | /** |
| 5621 | * @brief Resolve (check) all when conditions relevant for \p node. |
| 5622 | * Logs directly. |
| 5623 | * |
| 5624 | * @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] | 5625 | * |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5626 | * @return |
| 5627 | * -1 - error, ly_errno is set |
| 5628 | * 0 - true "when" statement |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5629 | * 0, ly_vecode = LYVE_NOWHEN - false "when" statement |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5630 | * 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] | 5631 | */ |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5632 | int |
| 5633 | resolve_when(struct lyd_node *node, int *result) |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5634 | { |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5635 | struct lyd_node *ctx_node = NULL, *unlinked_nodes, *tmp_node; |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5636 | struct lys_node *sparent; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5637 | struct lyxp_set set; |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5638 | enum lyxp_node_type ctx_node_type; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5639 | int rc = 0; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5640 | |
| 5641 | assert(node); |
| 5642 | memset(&set, 0, sizeof set); |
| 5643 | |
| 5644 | if (!(node->schema->nodetype & (LYS_NOTIF | LYS_RPC)) && (((struct lys_node_container *)node->schema)->when)) { |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5645 | /* make the node dummy for the evaluation */ |
| 5646 | node->validity |= LYD_VAL_INUSE; |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5647 | rc = lyxp_eval(((struct lys_node_container *)node->schema)->when->cond, node, LYXP_NODE_ELEM, &set, LYXP_WHEN); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5648 | node->validity &= ~LYD_VAL_INUSE; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5649 | if (rc) { |
| 5650 | if (rc == 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5651 | 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] | 5652 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5653 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5654 | } |
| 5655 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5656 | /* set boolean result of the condition */ |
Michal Vasko | 944a564 | 2016-03-21 11:48:58 +0100 | [diff] [blame] | 5657 | lyxp_set_cast(&set, LYXP_SET_BOOLEAN, node, LYXP_WHEN); |
Michal Vasko | 8146d4c | 2016-05-09 15:50:29 +0200 | [diff] [blame] | 5658 | if (!set.val.bool) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5659 | LOGVAL(LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_container *)node->schema)->when->cond); |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5660 | node->when_status |= LYD_WHEN_FALSE; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5661 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5662 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5663 | |
| 5664 | /* free xpath set content */ |
| 5665 | lyxp_set_cast(&set, LYXP_SET_EMPTY, node, 0); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5666 | } |
| 5667 | |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5668 | sparent = node->schema; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5669 | goto check_augment; |
| 5670 | |
| 5671 | /* check when in every schema node that affects node */ |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5672 | while (sparent && (sparent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) { |
| 5673 | if (((struct lys_node_uses *)sparent)->when) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5674 | if (!ctx_node) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5675 | rc = resolve_when_ctx_node(node, sparent, &ctx_node, &ctx_node_type); |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5676 | if (rc) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5677 | LOGINT; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5678 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5679 | } |
| 5680 | } |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5681 | |
| 5682 | unlinked_nodes = NULL; |
| 5683 | /* we do not want our node pointer to change */ |
| 5684 | tmp_node = node; |
| 5685 | rc = resolve_when_unlink_nodes(sparent, &tmp_node, &ctx_node, ctx_node_type, &unlinked_nodes); |
| 5686 | if (rc) { |
| 5687 | goto cleanup; |
| 5688 | } |
| 5689 | |
| 5690 | rc = lyxp_eval(((struct lys_node_uses *)sparent)->when->cond, ctx_node, ctx_node_type, &set, LYXP_WHEN); |
| 5691 | |
| 5692 | if (unlinked_nodes && ctx_node) { |
| 5693 | if (resolve_when_relink_nodes(ctx_node, unlinked_nodes, ctx_node_type)) { |
| 5694 | rc = -1; |
| 5695 | goto cleanup; |
| 5696 | } |
| 5697 | } |
| 5698 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5699 | if (rc) { |
| 5700 | if (rc == 1) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5701 | LOGVAL(LYE_INWHEN, LY_VLOG_LYD, node, ((struct lys_node_uses *)sparent)->when->cond); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5702 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5703 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5704 | } |
| 5705 | |
Michal Vasko | 944a564 | 2016-03-21 11:48:58 +0100 | [diff] [blame] | 5706 | lyxp_set_cast(&set, LYXP_SET_BOOLEAN, ctx_node, LYXP_WHEN); |
Michal Vasko | 8146d4c | 2016-05-09 15:50:29 +0200 | [diff] [blame] | 5707 | if (!set.val.bool) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5708 | LOGVAL(LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_uses *)sparent)->when->cond); |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5709 | node->when_status |= LYD_WHEN_FALSE; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5710 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5711 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5712 | |
| 5713 | /* free xpath set content */ |
| 5714 | lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node, 0); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5715 | } |
| 5716 | |
| 5717 | check_augment: |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5718 | if ((sparent->parent && (sparent->parent->nodetype == LYS_AUGMENT) && (((struct lys_node_augment *)sparent->parent)->when))) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5719 | if (!ctx_node) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5720 | rc = resolve_when_ctx_node(node, sparent->parent, &ctx_node, &ctx_node_type); |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5721 | if (rc) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5722 | LOGINT; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5723 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5724 | } |
| 5725 | } |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5726 | |
| 5727 | unlinked_nodes = NULL; |
| 5728 | tmp_node = node; |
| 5729 | rc = resolve_when_unlink_nodes(sparent->parent, &tmp_node, &ctx_node, ctx_node_type, &unlinked_nodes); |
| 5730 | if (rc) { |
| 5731 | goto cleanup; |
| 5732 | } |
| 5733 | |
| 5734 | rc = lyxp_eval(((struct lys_node_augment *)sparent->parent)->when->cond, ctx_node, ctx_node_type, &set, LYXP_WHEN); |
| 5735 | |
| 5736 | /* reconnect nodes, if ctx_node is NULL then all the nodes were unlinked, but linked together, |
| 5737 | * so the tree did not actually change and there is nothing for us to do |
| 5738 | */ |
| 5739 | if (unlinked_nodes && ctx_node) { |
| 5740 | if (resolve_when_relink_nodes(ctx_node, unlinked_nodes, ctx_node_type)) { |
| 5741 | rc = -1; |
| 5742 | goto cleanup; |
| 5743 | } |
| 5744 | } |
| 5745 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5746 | if (rc) { |
| 5747 | if (rc == 1) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5748 | LOGVAL(LYE_INWHEN, LY_VLOG_LYD, node, ((struct lys_node_augment *)sparent->parent)->when->cond); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5749 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5750 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5751 | } |
| 5752 | |
Michal Vasko | 944a564 | 2016-03-21 11:48:58 +0100 | [diff] [blame] | 5753 | lyxp_set_cast(&set, LYXP_SET_BOOLEAN, ctx_node, LYXP_WHEN); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5754 | |
Michal Vasko | 8146d4c | 2016-05-09 15:50:29 +0200 | [diff] [blame] | 5755 | if (!set.val.bool) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5756 | LOGVAL(LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_augment *)sparent->parent)->when->cond); |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5757 | node->when_status |= LYD_WHEN_FALSE; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5758 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5759 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5760 | |
| 5761 | /* free xpath set content */ |
| 5762 | lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node, 0); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5763 | } |
| 5764 | |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5765 | sparent = lys_parent(sparent); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5766 | } |
| 5767 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5768 | node->when_status |= LYD_WHEN_TRUE; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5769 | |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5770 | cleanup: |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5771 | /* free xpath set content */ |
| 5772 | lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node ? ctx_node : node, 0); |
| 5773 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5774 | if (result) { |
| 5775 | if (node->when_status & LYD_WHEN_TRUE) { |
| 5776 | *result = 1; |
| 5777 | } else { |
| 5778 | *result = 0; |
| 5779 | } |
| 5780 | } |
| 5781 | |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5782 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5783 | } |
| 5784 | |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 5785 | static int |
| 5786 | check_leafref_features(struct lys_type *type) |
| 5787 | { |
| 5788 | struct lys_node *iter; |
| 5789 | struct ly_set *src_parents, *trg_parents, *features; |
| 5790 | unsigned int i, j, size, x; |
| 5791 | int ret = EXIT_SUCCESS; |
| 5792 | |
| 5793 | assert(type->parent); |
| 5794 | |
| 5795 | src_parents = ly_set_new(); |
| 5796 | trg_parents = ly_set_new(); |
| 5797 | features = ly_set_new(); |
| 5798 | |
| 5799 | /* get parents chain of source (leafref) */ |
| 5800 | for (iter = (struct lys_node *)type->parent; iter; iter = iter->parent) { |
| 5801 | if (iter->nodetype & (LYS_INPUT | LYS_OUTPUT)) { |
| 5802 | continue; |
| 5803 | } |
| 5804 | ly_set_add(src_parents, iter, LY_SET_OPT_USEASLIST); |
| 5805 | } |
| 5806 | /* get parents chain of target */ |
| 5807 | for (iter = (struct lys_node *)type->info.lref.target; iter; iter = iter->parent) { |
| 5808 | if (iter->nodetype & (LYS_INPUT | LYS_OUTPUT)) { |
| 5809 | continue; |
| 5810 | } |
| 5811 | ly_set_add(trg_parents, iter, LY_SET_OPT_USEASLIST); |
| 5812 | } |
| 5813 | |
| 5814 | /* compare the features used in if-feature statements in the rest of both |
| 5815 | * chains of parents. The set of features used for target must be a subset |
| 5816 | * of features used for the leafref. This is not a perfect, we should compare |
| 5817 | * the truth tables but it could require too much resources, so we simplify that */ |
| 5818 | for (i = 0; i < src_parents->number; i++) { |
| 5819 | iter = src_parents->set.s[i]; /* shortcut */ |
| 5820 | if (!iter->iffeature_size) { |
| 5821 | continue; |
| 5822 | } |
| 5823 | for (j = 0; j < iter->iffeature_size; j++) { |
| 5824 | resolve_iffeature_getsizes(&iter->iffeature[j], NULL, &size); |
| 5825 | for (; size; size--) { |
| 5826 | if (!iter->iffeature[j].features[size - 1]) { |
| 5827 | /* not yet resolved feature, postpone this check */ |
| 5828 | ret = EXIT_FAILURE; |
| 5829 | goto cleanup; |
| 5830 | } |
| 5831 | ly_set_add(features, iter->iffeature[j].features[size - 1], 0); |
| 5832 | } |
| 5833 | } |
| 5834 | } |
| 5835 | x = features->number; |
| 5836 | for (i = 0; i < trg_parents->number; i++) { |
| 5837 | iter = trg_parents->set.s[i]; /* shortcut */ |
| 5838 | if (!iter->iffeature_size) { |
| 5839 | continue; |
| 5840 | } |
| 5841 | for (j = 0; j < iter->iffeature_size; j++) { |
| 5842 | resolve_iffeature_getsizes(&iter->iffeature[j], NULL, &size); |
| 5843 | for (; size; size--) { |
| 5844 | if (!iter->iffeature[j].features[size - 1]) { |
| 5845 | /* not yet resolved feature, postpone this check */ |
| 5846 | ret = EXIT_FAILURE; |
| 5847 | goto cleanup; |
| 5848 | } |
| 5849 | if ((unsigned int)ly_set_add(features, iter->iffeature[j].features[size - 1], 0) >= x) { |
| 5850 | /* the feature is not present in features set of target's parents chain */ |
| 5851 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, type->parent, "leafref", type->info.lref.path); |
| 5852 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, type->parent, |
| 5853 | "Leafref is not conditional based on \"%s\" feature as its target.", |
| 5854 | iter->iffeature[j].features[size - 1]->name); |
| 5855 | ret = -1; |
| 5856 | goto cleanup; |
| 5857 | } |
| 5858 | } |
| 5859 | } |
| 5860 | } |
| 5861 | |
| 5862 | cleanup: |
| 5863 | ly_set_free(features); |
| 5864 | ly_set_free(src_parents); |
| 5865 | ly_set_free(trg_parents); |
| 5866 | |
| 5867 | return ret; |
| 5868 | } |
| 5869 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5870 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5871 | * @brief Resolve a single unres schema item. Logs indirectly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5872 | * |
| 5873 | * @param[in] mod Main module. |
| 5874 | * @param[in] item Item to resolve. Type determined by \p type. |
| 5875 | * @param[in] type Type of the unresolved item. |
| 5876 | * @param[in] str_snode String, a schema node, or NULL. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5877 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5878 | * |
| 5879 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 5880 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5881 | static int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 5882 | 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] | 5883 | struct unres_schema *unres) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5884 | { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 5885 | /* has_str - whether the str_snode is a string in a dictionary that needs to be freed */ |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 5886 | int rc = -1, has_str = 0, tpdf_flag = 0, i, k; |
| 5887 | unsigned int j; |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 5888 | struct lys_node *node, *par_grp; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 5889 | const char *expr; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 5890 | |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 5891 | struct ly_set *refs, *procs; |
| 5892 | struct lys_feature *ref, *feat; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 5893 | struct lys_ident *ident; |
| 5894 | struct lys_type *stype; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 5895 | struct lys_node_choice *choic; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 5896 | struct lyxml_elem *yin; |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 5897 | struct yang_type *yang; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 5898 | struct unres_list_uniq *unique_info; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 5899 | struct unres_iffeat_data *iff_data; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5900 | |
| 5901 | switch (type) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5902 | case UNRES_IDENT: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 5903 | expr = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 5904 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 5905 | ident = item; |
| 5906 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5907 | rc = resolve_base_ident(mod, ident, expr, "identity", NULL, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5908 | break; |
| 5909 | case UNRES_TYPE_IDENTREF: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 5910 | expr = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 5911 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 5912 | stype = item; |
| 5913 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5914 | rc = resolve_base_ident(mod, NULL, expr, "type", stype, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5915 | break; |
| 5916 | case UNRES_TYPE_LEAFREF: |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 5917 | node = str_snode; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 5918 | stype = item; |
| 5919 | |
Radek Krejci | 2f12f85 | 2016-01-08 12:59:57 +0100 | [diff] [blame] | 5920 | /* HACK - when there is no parent, we are in top level typedef and in that |
| 5921 | * case, the path has to contain absolute path, so we let the resolve_path_arg_schema() |
| 5922 | * know it via tpdf_flag */ |
| 5923 | if (!node) { |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 5924 | tpdf_flag = 1; |
Radek Krejci | 2f12f85 | 2016-01-08 12:59:57 +0100 | [diff] [blame] | 5925 | node = (struct lys_node *)stype->parent; |
| 5926 | } |
| 5927 | |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 5928 | if (!lys_node_module(node)->implemented) { |
| 5929 | /* not implemented module, don't bother with resolving the leafref |
| 5930 | * if the module is set to be implemented, tha path will be resolved then */ |
| 5931 | rc = 0; |
| 5932 | break; |
| 5933 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5934 | rc = resolve_path_arg_schema(stype->info.lref.path, node, tpdf_flag, |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 5935 | (const struct lys_node **)&stype->info.lref.target); |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 5936 | if (!tpdf_flag && !rc) { |
| 5937 | assert(stype->info.lref.target); |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 5938 | /* check if leafref and its target are under a common if-features */ |
| 5939 | rc = check_leafref_features(stype); |
| 5940 | if (rc) { |
| 5941 | break; |
| 5942 | } |
| 5943 | |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 5944 | /* store the backlink from leafref target */ |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 5945 | if (lys_leaf_add_leafref_target(stype->info.lref.target, (struct lys_node *)stype->parent)) { |
| 5946 | rc = -1; |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 5947 | } |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 5948 | } |
| 5949 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5950 | break; |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 5951 | case UNRES_TYPE_DER_TPDF: |
| 5952 | tpdf_flag = 1; |
| 5953 | /* no break */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5954 | case UNRES_TYPE_DER: |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 5955 | /* parent */ |
| 5956 | node = str_snode; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 5957 | stype = item; |
| 5958 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 5959 | /* HACK type->der is temporarily unparsed type statement */ |
| 5960 | yin = (struct lyxml_elem *)stype->der; |
| 5961 | stype->der = NULL; |
| 5962 | |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 5963 | if (yin->flags & LY_YANG_STRUCTURE_FLAG) { |
| 5964 | yang = (struct yang_type *)yin; |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 5965 | rc = yang_check_type(mod, node, yang, tpdf_flag, unres); |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 5966 | |
| 5967 | if (rc) { |
Pavol Vican | 8bd72e4 | 2016-08-29 09:53:05 +0200 | [diff] [blame] | 5968 | /* may try again later */ |
| 5969 | stype->der = (struct lys_tpdf *)yang; |
Pavol Vican | d01d8ae | 2016-03-01 10:45:59 +0100 | [diff] [blame] | 5970 | } else { |
| 5971 | /* 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] | 5972 | lydict_remove(mod->ctx, yang->name); |
Pavol Vican | d01d8ae | 2016-03-01 10:45:59 +0100 | [diff] [blame] | 5973 | free(yang); |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 5974 | } |
| 5975 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 5976 | } else { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 5977 | rc = fill_yin_type(mod, node, yin, stype, tpdf_flag, unres); |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 5978 | if (!rc) { |
| 5979 | /* we need to always be able to free this, it's safe only in this case */ |
| 5980 | lyxml_free(mod->ctx, yin); |
| 5981 | } else { |
| 5982 | /* may try again later, put all back how it was */ |
| 5983 | stype->der = (struct lys_tpdf *)yin; |
| 5984 | } |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 5985 | } |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 5986 | if (rc == EXIT_SUCCESS) { |
Radek Krejci | 2c2fce8 | 2016-08-01 13:52:26 +0200 | [diff] [blame] | 5987 | /* it does not make sense to have leaf-list of empty type */ |
| 5988 | if (!tpdf_flag && node->nodetype == LYS_LEAFLIST && stype->base == LY_TYPE_EMPTY) { |
| 5989 | LOGWRN("The leaf-list \"%s\" is of \"empty\" type, which does not make sense.", node->name); |
| 5990 | } |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 5991 | } else if (rc == EXIT_FAILURE && stype->base != LY_TYPE_ERR) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 5992 | /* forward reference - in case the type is in grouping, we have to make the grouping unusable |
| 5993 | * by uses statement until the type is resolved. We do that the same way as uses statements inside |
| 5994 | * grouping - the grouping's nacm member (not used un grouping) is used to increase the number of |
| 5995 | * so far unresolved items (uses and types). The grouping cannot be used unless the nacm value is 0. |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 5996 | * To remember that the grouping already increased grouping's nacm, the LY_TYPE_ERR is used as value |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 5997 | * of the type's base member. */ |
| 5998 | for (par_grp = node; par_grp && (par_grp->nodetype != LYS_GROUPING); par_grp = lys_parent(par_grp)); |
| 5999 | if (par_grp) { |
| 6000 | ((struct lys_node_grp *)par_grp)->nacm++; |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6001 | stype->base = LY_TYPE_ERR; |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 6002 | } |
Radek Krejci | 2c2fce8 | 2016-08-01 13:52:26 +0200 | [diff] [blame] | 6003 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6004 | break; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6005 | case UNRES_IFFEAT: |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6006 | iff_data = str_snode; |
| 6007 | rc = resolve_feature(iff_data->fname, strlen(iff_data->fname), iff_data->node, item); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 6008 | if (!rc) { |
| 6009 | /* success */ |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6010 | lydict_remove(mod->ctx, iff_data->fname); |
| 6011 | free(iff_data); |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6012 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6013 | break; |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6014 | case UNRES_FEATURE: |
| 6015 | feat = (struct lys_feature *)item; |
| 6016 | |
| 6017 | if (feat->iffeature_size) { |
| 6018 | refs = ly_set_new(); |
| 6019 | procs = ly_set_new(); |
| 6020 | ly_set_add(procs, feat, 0); |
| 6021 | |
| 6022 | while (procs->number) { |
| 6023 | ref = procs->set.g[procs->number - 1]; |
| 6024 | ly_set_rm_index(procs, procs->number - 1); |
| 6025 | |
| 6026 | for (i = 0; i < ref->iffeature_size; i++) { |
| 6027 | resolve_iffeature_getsizes(&ref->iffeature[i], NULL, &j); |
| 6028 | for (; j > 0 ; j--) { |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6029 | if (ref->iffeature[i].features[j - 1]) { |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6030 | if (ref->iffeature[i].features[j - 1] == feat) { |
| 6031 | LOGVAL(LYE_CIRC_FEATURES, LY_VLOG_NONE, NULL, feat->name); |
| 6032 | goto featurecheckdone; |
| 6033 | } |
| 6034 | |
| 6035 | if (ref->iffeature[i].features[j - 1]->iffeature_size) { |
| 6036 | k = refs->number; |
| 6037 | if (ly_set_add(refs, ref->iffeature[i].features[j - 1], 0) == k) { |
| 6038 | /* not yet seen feature, add it for processing */ |
| 6039 | ly_set_add(procs, ref->iffeature[i].features[j - 1], 0); |
| 6040 | } |
| 6041 | } |
| 6042 | } else { |
| 6043 | /* forward reference */ |
| 6044 | rc = EXIT_FAILURE; |
| 6045 | goto featurecheckdone; |
| 6046 | } |
| 6047 | } |
| 6048 | |
| 6049 | } |
| 6050 | } |
| 6051 | rc = EXIT_SUCCESS; |
| 6052 | |
| 6053 | featurecheckdone: |
| 6054 | ly_set_free(refs); |
| 6055 | ly_set_free(procs); |
| 6056 | } |
| 6057 | |
| 6058 | break; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6059 | case UNRES_USES: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6060 | rc = resolve_unres_schema_uses(item, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6061 | break; |
| 6062 | case UNRES_TYPE_DFLT: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6063 | expr = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 6064 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6065 | stype = item; |
| 6066 | |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6067 | rc = check_default(stype, expr, mod); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6068 | break; |
| 6069 | case UNRES_CHOICE_DFLT: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6070 | expr = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 6071 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6072 | choic = item; |
| 6073 | |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 6074 | if (!choic->dflt) { |
| 6075 | choic->dflt = resolve_choice_dflt(choic, expr); |
| 6076 | } |
Michal Vasko | 7955b36 | 2015-09-04 14:18:15 +0200 | [diff] [blame] | 6077 | if (choic->dflt) { |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 6078 | rc = lyp_check_mandatory_choice((struct lys_node *)choic); |
Michal Vasko | 7955b36 | 2015-09-04 14:18:15 +0200 | [diff] [blame] | 6079 | } else { |
| 6080 | rc = EXIT_FAILURE; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6081 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6082 | break; |
| 6083 | case UNRES_LIST_KEYS: |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 6084 | has_str = 1; |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6085 | rc = resolve_list_keys(item, str_snode); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6086 | break; |
| 6087 | case UNRES_LIST_UNIQ: |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6088 | unique_info = (struct unres_list_uniq *)item; |
| 6089 | rc = resolve_unique(unique_info->list, unique_info->expr, unique_info->trg_type); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6090 | break; |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 6091 | case UNRES_AUGMENT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6092 | rc = resolve_augment(item, NULL); |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 6093 | break; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6094 | case UNRES_XPATH: |
| 6095 | node = (struct lys_node *)item; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 6096 | rc = check_node_xpath(node); |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6097 | break; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6098 | default: |
| 6099 | LOGINT; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6100 | break; |
| 6101 | } |
| 6102 | |
Radek Krejci | 54081ce | 2016-08-12 15:21:47 +0200 | [diff] [blame] | 6103 | if (has_str && !rc) { |
| 6104 | /* the string is no more needed in case of success. |
| 6105 | * In case of forward reference, we will try to resolve the string later */ |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 6106 | lydict_remove(mod->ctx, str_snode); |
| 6107 | } |
| 6108 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6109 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6110 | } |
| 6111 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6112 | /* logs directly */ |
| 6113 | static void |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6114 | 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] | 6115 | { |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 6116 | struct lyxml_elem *xml; |
| 6117 | struct lyxml_attr *attr; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6118 | struct unres_iffeat_data *iff_data; |
Radek Krejci | 76e15e1 | 2016-06-22 11:02:24 +0200 | [diff] [blame] | 6119 | const char *type_name = NULL; |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 6120 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6121 | switch (type) { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6122 | case UNRES_IDENT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6123 | 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] | 6124 | break; |
| 6125 | case UNRES_TYPE_IDENTREF: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6126 | 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] | 6127 | break; |
| 6128 | case UNRES_TYPE_LEAFREF: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6129 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "leafref", |
| 6130 | ((struct lys_type *)item)->info.lref.path); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6131 | break; |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 6132 | case UNRES_TYPE_DER_TPDF: |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6133 | case UNRES_TYPE_DER: |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 6134 | xml = (struct lyxml_elem *)((struct lys_type *)item)->der; |
| 6135 | if (xml->flags & LY_YANG_STRUCTURE_FLAG) { |
| 6136 | type_name = ((struct yang_type *)xml)->name; |
| 6137 | } else { |
| 6138 | LY_TREE_FOR(xml->attr, attr) { |
| 6139 | if ((attr->type == LYXML_ATTR_STD) && !strcmp(attr->name, "name")) { |
| 6140 | type_name = attr->value; |
| 6141 | break; |
| 6142 | } |
| 6143 | } |
| 6144 | assert(attr); |
| 6145 | } |
| 6146 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "derived type", type_name); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6147 | break; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6148 | case UNRES_IFFEAT: |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6149 | iff_data = str_node; |
| 6150 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "if-feature", iff_data->fname); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6151 | break; |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6152 | case UNRES_FEATURE: |
| 6153 | LOGVRB("There are unresolved if-features for \"%s\" feature circular dependency check, it will be attempted later", |
| 6154 | ((struct lys_feature *)item)->name); |
| 6155 | break; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6156 | case UNRES_USES: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6157 | 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] | 6158 | break; |
| 6159 | case UNRES_TYPE_DFLT: |
Radek Krejci | 2e2de83 | 2016-10-13 16:12:26 +0200 | [diff] [blame] | 6160 | if (str_node) { |
| 6161 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "type default", (char *)str_node); |
| 6162 | } /* else no default value in the type itself, but we are checking some restrictions against |
| 6163 | * possible default value of some base type. The failure is caused by not resolved base type, |
| 6164 | * so it was already reported */ |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6165 | break; |
| 6166 | case UNRES_CHOICE_DFLT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6167 | 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] | 6168 | break; |
| 6169 | case UNRES_LIST_KEYS: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6170 | 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] | 6171 | break; |
| 6172 | case UNRES_LIST_UNIQ: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6173 | 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] | 6174 | break; |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 6175 | case UNRES_AUGMENT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6176 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "augment target", |
| 6177 | ((struct lys_node_augment *)item)->target_name); |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 6178 | break; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6179 | case UNRES_XPATH: |
| 6180 | LOGVRB("Resolving %s \"%s\" with the context node \"%s\" failed, it will be attempted later.", "XPath", |
| 6181 | (char *)str_node, ((struct lys_node *)item)->name); |
| 6182 | break; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6183 | default: |
| 6184 | LOGINT; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6185 | break; |
| 6186 | } |
| 6187 | } |
| 6188 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6189 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6190 | * @brief Resolve every unres schema item in the structure. Logs directly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6191 | * |
| 6192 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6193 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6194 | * |
Michal Vasko | 92b8a38 | 2015-08-19 14:03:49 +0200 | [diff] [blame] | 6195 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6196 | */ |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6197 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 6198 | resolve_unres_schema(struct lys_module *mod, struct unres_schema *unres) |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6199 | { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6200 | uint32_t i, resolved = 0, unres_count, res_count; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6201 | int rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6202 | |
| 6203 | assert(unres); |
| 6204 | |
Michal Vasko | e873426 | 2016-09-29 14:12:06 +0200 | [diff] [blame] | 6205 | LOGVRB("Resolving \"%s\" unresolved schema nodes and their constraints...", mod->name); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6206 | ly_vlog_hide(1); |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6207 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6208 | /* uses */ |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6209 | do { |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6210 | unres_count = 0; |
| 6211 | res_count = 0; |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6212 | |
| 6213 | for (i = 0; i < unres->count; ++i) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6214 | /* UNRES_TYPE_LEAFREF must be resolved (for storing leafref target pointers); |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6215 | * if-features are resolved here to make sure that we will have all if-features for |
| 6216 | * later check of feature circular dependency */ |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6217 | if (unres->type[i] > UNRES_IDENT) { |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6218 | continue; |
| 6219 | } |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6220 | /* processes UNRES_USES, UNRES_IFFEAT, UNRES_TYPE_DER, UNRES_TYPE_DER_TPDF, UNRES_TYPE_LEAFREF, |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 6221 | * UNRES_CHOICE_DFLT and UNRES_IDENT */ |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6222 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6223 | ++unres_count; |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6224 | 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] | 6225 | if (!rc) { |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6226 | unres->type[i] = UNRES_RESOLVED; |
| 6227 | ++resolved; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6228 | ++res_count; |
Michal Vasko | 89e1532 | 2015-08-17 15:46:55 +0200 | [diff] [blame] | 6229 | } else if (rc == -1) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6230 | ly_vlog_hide(0); |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6231 | /* print the error */ |
| 6232 | 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] | 6233 | return -1; |
Radek Krejci | c2a180f | 2016-06-22 13:28:16 +0200 | [diff] [blame] | 6234 | } else { |
| 6235 | /* forward reference, erase ly_errno */ |
| 6236 | ly_errno = LY_SUCCESS; |
| 6237 | ly_vecode = LYVE_SUCCESS; |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6238 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6239 | } |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6240 | } while (res_count && (res_count < unres_count)); |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6241 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6242 | if (res_count < unres_count) { |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6243 | /* just print the errors */ |
| 6244 | ly_vlog_hide(0); |
| 6245 | |
| 6246 | for (i = 0; i < unres->count; ++i) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6247 | if (unres->type[i] > UNRES_IDENT) { |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6248 | continue; |
| 6249 | } |
| 6250 | resolve_unres_schema_item(mod, unres->item[i], unres->type[i], unres->str_snode[i], unres); |
| 6251 | } |
Michal Vasko | 92b8a38 | 2015-08-19 14:03:49 +0200 | [diff] [blame] | 6252 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6253 | } |
| 6254 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6255 | /* the rest */ |
| 6256 | for (i = 0; i < unres->count; ++i) { |
| 6257 | if (unres->type[i] == UNRES_RESOLVED) { |
| 6258 | continue; |
| 6259 | } |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 6260 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6261 | 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] | 6262 | if (rc == 0) { |
Pavol Vican | 88e16c9 | 2016-09-07 15:41:50 +0200 | [diff] [blame] | 6263 | if (unres->type[i] == UNRES_LIST_UNIQ) { |
| 6264 | /* free the allocated structure */ |
| 6265 | free(unres->item[i]); |
| 6266 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6267 | unres->type[i] = UNRES_RESOLVED; |
| 6268 | ++resolved; |
| 6269 | } else if (rc == -1) { |
| 6270 | ly_vlog_hide(0); |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6271 | /* print the error */ |
| 6272 | resolve_unres_schema_item(mod, unres->item[i], unres->type[i], unres->str_snode[i], unres); |
| 6273 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6274 | } |
| 6275 | } |
| 6276 | |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6277 | ly_vlog_hide(0); |
| 6278 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6279 | if (resolved < unres->count) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6280 | /* try to resolve the unresolved nodes again, it will not resolve anything, but it will print |
| 6281 | * all the validation errors |
| 6282 | */ |
| 6283 | for (i = 0; i < unres->count; ++i) { |
| 6284 | if (unres->type[i] == UNRES_RESOLVED) { |
| 6285 | continue; |
| 6286 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6287 | 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] | 6288 | } |
Michal Vasko | 92b8a38 | 2015-08-19 14:03:49 +0200 | [diff] [blame] | 6289 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6290 | } |
| 6291 | |
Michal Vasko | e873426 | 2016-09-29 14:12:06 +0200 | [diff] [blame] | 6292 | LOGVRB("All \"%s\" schema nodes and constraints resolved.", mod->name); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6293 | unres->count = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6294 | return EXIT_SUCCESS; |
| 6295 | } |
| 6296 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6297 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6298 | * @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] | 6299 | * |
| 6300 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6301 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6302 | * @param[in] item Item to resolve. Type determined by \p type. |
| 6303 | * @param[in] type Type of the unresolved item. |
| 6304 | * @param[in] str String argument. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6305 | * |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 6306 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on storing the item in unres, -1 on error. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6307 | */ |
| 6308 | int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6309 | unres_schema_add_str(struct lys_module *mod, struct unres_schema *unres, void *item, enum UNRES_ITEM type, |
| 6310 | const char *str) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6311 | { |
Radek Krejci | 54081ce | 2016-08-12 15:21:47 +0200 | [diff] [blame] | 6312 | int rc; |
| 6313 | const char *dictstr; |
| 6314 | |
| 6315 | dictstr = lydict_insert(mod->ctx, str, 0); |
| 6316 | rc = unres_schema_add_node(mod, unres, item, type, (struct lys_node *)dictstr); |
| 6317 | |
| 6318 | if (rc == -1) { |
| 6319 | lydict_remove(mod->ctx, dictstr); |
| 6320 | } |
| 6321 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6322 | } |
| 6323 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6324 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6325 | * @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] | 6326 | * |
| 6327 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6328 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6329 | * @param[in] item Item to resolve. Type determined by \p type. |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6330 | * @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] | 6331 | * @param[in] snode Schema node argument. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6332 | * |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 6333 | * @return EXIT_SUCCESS on success, EXIT_FIALURE on storing the item in unres, -1 on error. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6334 | */ |
| 6335 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 6336 | 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] | 6337 | struct lys_node *snode) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6338 | { |
Michal Vasko | ef486d7 | 2016-09-27 12:10:44 +0200 | [diff] [blame] | 6339 | int rc, log_hidden; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6340 | struct lyxml_elem *yin; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6341 | char *path, *msg; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6342 | |
Michal Vasko | 9bf425b | 2015-10-22 11:42:03 +0200 | [diff] [blame] | 6343 | assert(unres && item && ((type != UNRES_LEAFREF) && (type != UNRES_INSTID) && (type != UNRES_WHEN) |
| 6344 | && (type != UNRES_MUST))); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6345 | |
Michal Vasko | ef486d7 | 2016-09-27 12:10:44 +0200 | [diff] [blame] | 6346 | if (*ly_vlog_hide_location()) { |
| 6347 | log_hidden = 1; |
| 6348 | } else { |
| 6349 | log_hidden = 0; |
| 6350 | ly_vlog_hide(1); |
| 6351 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6352 | rc = resolve_unres_schema_item(mod, item, type, snode, unres); |
Michal Vasko | ef486d7 | 2016-09-27 12:10:44 +0200 | [diff] [blame] | 6353 | if (!log_hidden) { |
| 6354 | ly_vlog_hide(0); |
| 6355 | } |
| 6356 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6357 | if (rc != EXIT_FAILURE) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6358 | if (rc == -1 && ly_errno == LY_EVALID) { |
Radek Krejci | 76e15e1 | 2016-06-22 11:02:24 +0200 | [diff] [blame] | 6359 | if (ly_log_level >= LY_LLERR) { |
| 6360 | path = strdup(ly_errpath()); |
| 6361 | msg = strdup(ly_errmsg()); |
| 6362 | LOGERR(LY_EVALID, "%s%s%s%s", msg, path[0] ? " (path: " : "", path[0] ? path : "", path[0] ? ")" : ""); |
| 6363 | free(path); |
| 6364 | free(msg); |
| 6365 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6366 | } |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6367 | if (type == UNRES_LIST_UNIQ) { |
| 6368 | /* free the allocated structure */ |
| 6369 | free(item); |
Pavol Vican | 88e16c9 | 2016-09-07 15:41:50 +0200 | [diff] [blame] | 6370 | } else if (rc == -1 && type == UNRES_IFFEAT) { |
| 6371 | /* free the allocated resources */ |
| 6372 | free(*((char **)item)); |
| 6373 | } |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6374 | return rc; |
Radek Krejci | f347abc | 2016-06-22 10:18:47 +0200 | [diff] [blame] | 6375 | } else { |
| 6376 | /* erase info about validation errors */ |
| 6377 | ly_errno = LY_SUCCESS; |
| 6378 | ly_vecode = LYVE_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6379 | } |
| 6380 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6381 | print_unres_schema_item_fail(item, type, snode); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6382 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6383 | /* HACK unlinking is performed here so that we do not do any (NS) copying in vain */ |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 6384 | if (type == UNRES_TYPE_DER || type == UNRES_TYPE_DER_TPDF) { |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6385 | yin = (struct lyxml_elem *)((struct lys_type *)item)->der; |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6386 | if (!(yin->flags & LY_YANG_STRUCTURE_FLAG)) { |
| 6387 | lyxml_unlink_elem(mod->ctx, yin, 1); |
| 6388 | ((struct lys_type *)item)->der = (struct lys_tpdf *)yin; |
| 6389 | } |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6390 | } |
| 6391 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6392 | unres->count++; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 6393 | unres->item = ly_realloc(unres->item, unres->count*sizeof *unres->item); |
| 6394 | if (!unres->item) { |
| 6395 | LOGMEM; |
| 6396 | return -1; |
| 6397 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6398 | unres->item[unres->count-1] = item; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 6399 | unres->type = ly_realloc(unres->type, unres->count*sizeof *unres->type); |
| 6400 | if (!unres->type) { |
| 6401 | LOGMEM; |
| 6402 | return -1; |
| 6403 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6404 | unres->type[unres->count-1] = type; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 6405 | unres->str_snode = ly_realloc(unres->str_snode, unres->count*sizeof *unres->str_snode); |
| 6406 | if (!unres->str_snode) { |
| 6407 | LOGMEM; |
| 6408 | return -1; |
| 6409 | } |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6410 | unres->str_snode[unres->count-1] = snode; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6411 | unres->module = ly_realloc(unres->module, unres->count*sizeof *unres->module); |
| 6412 | if (!unres->module) { |
| 6413 | LOGMEM; |
| 6414 | return -1; |
| 6415 | } |
| 6416 | unres->module[unres->count-1] = mod; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6417 | |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 6418 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6419 | } |
| 6420 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6421 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6422 | * @brief Duplicate an unres schema item. Logs indirectly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6423 | * |
| 6424 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6425 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6426 | * @param[in] item Old item to be resolved. |
| 6427 | * @param[in] type Type of the old unresolved item. |
| 6428 | * @param[in] new_item New item to use in the duplicate. |
| 6429 | * |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 6430 | * @return EXIT_SUCCESS on success, EXIT_FAILURE if item is not in unres, -1 on error. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6431 | */ |
Michal Vasko | dad1940 | 2015-08-06 09:51:53 +0200 | [diff] [blame] | 6432 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 6433 | 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] | 6434 | { |
| 6435 | int i; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6436 | struct unres_list_uniq aux_uniq; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6437 | struct unres_iffeat_data *iff_data; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6438 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6439 | 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] | 6440 | |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6441 | /* hack for UNRES_LIST_UNIQ, which stores multiple items behind its item */ |
| 6442 | if (type == UNRES_LIST_UNIQ) { |
| 6443 | aux_uniq.list = item; |
| 6444 | aux_uniq.expr = ((struct unres_list_uniq *)new_item)->expr; |
| 6445 | item = &aux_uniq; |
| 6446 | } |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 6447 | i = unres_schema_find(unres, -1, item, type); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6448 | |
| 6449 | if (i == -1) { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6450 | if (type == UNRES_LIST_UNIQ) { |
| 6451 | free(new_item); |
| 6452 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 6453 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6454 | } |
| 6455 | |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6456 | if ((type == UNRES_TYPE_LEAFREF) || (type == UNRES_USES) || (type == UNRES_TYPE_DFLT) || |
Radek Krejci | b69f323 | 2016-09-16 17:41:07 +0200 | [diff] [blame] | 6457 | (type == UNRES_FEATURE) || (type == UNRES_LIST_UNIQ)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6458 | 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] | 6459 | LOGINT; |
| 6460 | return -1; |
| 6461 | } |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6462 | } else if (type == UNRES_IFFEAT) { |
| 6463 | /* duplicate unres_iffeature_data */ |
| 6464 | iff_data = malloc(sizeof *iff_data); |
| 6465 | iff_data->fname = lydict_insert(mod->ctx, ((struct unres_iffeat_data *)unres->str_snode[i])->fname, 0); |
| 6466 | iff_data->node = ((struct unres_iffeat_data *)unres->str_snode[i])->node; |
| 6467 | if (unres_schema_add_node(mod, unres, new_item, type, (struct lys_node *)iff_data) == -1) { |
| 6468 | LOGINT; |
| 6469 | return -1; |
| 6470 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6471 | } else { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6472 | 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] | 6473 | LOGINT; |
| 6474 | return -1; |
| 6475 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6476 | } |
Michal Vasko | dad1940 | 2015-08-06 09:51:53 +0200 | [diff] [blame] | 6477 | |
| 6478 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6479 | } |
| 6480 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6481 | /* does not log */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6482 | int |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 6483 | unres_schema_find(struct unres_schema *unres, int start_on_backwards, void *item, enum UNRES_ITEM type) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6484 | { |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 6485 | int i; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6486 | struct unres_list_uniq *aux_uniq1, *aux_uniq2; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6487 | |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 6488 | if (start_on_backwards > 0) { |
| 6489 | i = start_on_backwards; |
| 6490 | } else { |
| 6491 | i = unres->count - 1; |
| 6492 | } |
| 6493 | for (; i > -1; i--) { |
| 6494 | if (unres->type[i] != type) { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6495 | continue; |
| 6496 | } |
| 6497 | if (type != UNRES_LIST_UNIQ) { |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 6498 | if (unres->item[i] == item) { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6499 | break; |
| 6500 | } |
| 6501 | } else { |
| 6502 | aux_uniq1 = (struct unres_list_uniq *)unres->item[i - 1]; |
| 6503 | aux_uniq2 = (struct unres_list_uniq *)item; |
| 6504 | if ((aux_uniq1->list == aux_uniq2->list) && ly_strequal(aux_uniq1->expr, aux_uniq2->expr, 0)) { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6505 | break; |
| 6506 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6507 | } |
| 6508 | } |
| 6509 | |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 6510 | return i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6511 | } |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6512 | |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 6513 | static void |
| 6514 | unres_schema_free_item(struct ly_ctx *ctx, struct unres_schema *unres, uint32_t i) |
| 6515 | { |
| 6516 | struct lyxml_elem *yin; |
| 6517 | struct yang_type *yang; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6518 | struct unres_iffeat_data *iff_data; |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 6519 | |
| 6520 | switch (unres->type[i]) { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 6521 | case UNRES_TYPE_DER_TPDF: |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 6522 | case UNRES_TYPE_DER: |
| 6523 | yin = (struct lyxml_elem *)((struct lys_type *)unres->item[i])->der; |
| 6524 | if (yin->flags & LY_YANG_STRUCTURE_FLAG) { |
| 6525 | yang =(struct yang_type *)yin; |
| 6526 | yang->type->base = yang->base; |
| 6527 | lydict_remove(ctx, yang->name); |
| 6528 | free(yang); |
| 6529 | } else { |
| 6530 | lyxml_free(ctx, yin); |
| 6531 | } |
| 6532 | break; |
Pavol Vican | 88e16c9 | 2016-09-07 15:41:50 +0200 | [diff] [blame] | 6533 | case UNRES_IFFEAT: |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6534 | iff_data = (struct unres_iffeat_data *)unres->str_snode[i]; |
| 6535 | lydict_remove(ctx, iff_data->fname); |
| 6536 | free(unres->str_snode[i]); |
Pavol Vican | 88e16c9 | 2016-09-07 15:41:50 +0200 | [diff] [blame] | 6537 | break; |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 6538 | case UNRES_IDENT: |
| 6539 | case UNRES_TYPE_IDENTREF: |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 6540 | case UNRES_TYPE_DFLT: |
| 6541 | case UNRES_CHOICE_DFLT: |
| 6542 | case UNRES_LIST_KEYS: |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 6543 | lydict_remove(ctx, (const char *)unres->str_snode[i]); |
| 6544 | break; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6545 | case UNRES_LIST_UNIQ: |
| 6546 | free(unres->item[i]); |
| 6547 | break; |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 6548 | default: |
| 6549 | break; |
| 6550 | } |
| 6551 | unres->type[i] = UNRES_RESOLVED; |
| 6552 | } |
| 6553 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6554 | void |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6555 | unres_schema_free(struct lys_module *module, struct unres_schema **unres) |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6556 | { |
| 6557 | uint32_t i; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6558 | unsigned int unresolved = 0; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6559 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6560 | if (!unres || !(*unres)) { |
| 6561 | return; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6562 | } |
| 6563 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6564 | assert(module || (*unres)->count == 0); |
| 6565 | |
| 6566 | for (i = 0; i < (*unres)->count; ++i) { |
| 6567 | if ((*unres)->module[i] != module) { |
| 6568 | if ((*unres)->type[i] != UNRES_RESOLVED) { |
| 6569 | unresolved++; |
| 6570 | } |
| 6571 | continue; |
| 6572 | } |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 6573 | |
| 6574 | /* free heap memory for the specific item */ |
| 6575 | unres_schema_free_item(module->ctx, *unres, i); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6576 | } |
| 6577 | |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 6578 | /* free it all */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6579 | if (!module || (!unresolved && !module->type)) { |
| 6580 | free((*unres)->item); |
| 6581 | free((*unres)->type); |
| 6582 | free((*unres)->str_snode); |
| 6583 | free((*unres)->module); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6584 | free((*unres)); |
| 6585 | (*unres) = NULL; |
| 6586 | } |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6587 | } |
| 6588 | |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6589 | static int resolve_union(struct lyd_node_leaf_list *leaf, struct lys_type *type); |
| 6590 | |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 6591 | static int |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6592 | resolve_leafref(struct lyd_node_leaf_list *leaf, struct lys_type *type) |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 6593 | { |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 6594 | struct unres_data matches; |
| 6595 | uint32_t i; |
| 6596 | |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6597 | assert(type->base == LY_TYPE_LEAFREF); |
| 6598 | |
| 6599 | /* init */ |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 6600 | memset(&matches, 0, sizeof matches); |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 6601 | |
| 6602 | /* EXIT_FAILURE return keeps leaf->value.lefref NULL, handled later */ |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6603 | if (resolve_path_arg_data((struct lyd_node *)leaf, type->info.lref.path, &matches) == -1) { |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 6604 | return -1; |
| 6605 | } |
| 6606 | |
| 6607 | /* check that value matches */ |
| 6608 | for (i = 0; i < matches.count; ++i) { |
| 6609 | if (ly_strequal(leaf->value_str, ((struct lyd_node_leaf_list *)matches.node[i])->value_str, 1)) { |
| 6610 | leaf->value.leafref = matches.node[i]; |
| 6611 | break; |
| 6612 | } |
| 6613 | } |
| 6614 | |
| 6615 | free(matches.node); |
| 6616 | |
| 6617 | if (!leaf->value.leafref) { |
| 6618 | /* reference not found */ |
| 6619 | if (type->info.lref.req > -1) { |
| 6620 | LOGVAL(LYE_NOLEAFREF, LY_VLOG_LYD, leaf, type->info.lref.path, leaf->value_str); |
| 6621 | return EXIT_FAILURE; |
| 6622 | } else { |
| 6623 | LOGVRB("There is no leafref with the value \"%s\", but it is not required.", leaf->value_str); |
| 6624 | } |
| 6625 | } |
| 6626 | |
| 6627 | return EXIT_SUCCESS; |
| 6628 | } |
| 6629 | |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6630 | API LY_DATA_TYPE |
| 6631 | lyd_leaf_type(const struct lyd_node_leaf_list *leaf) |
| 6632 | { |
| 6633 | struct lyd_node *node; |
| 6634 | struct lys_type *type, *type_iter; |
| 6635 | lyd_val value; |
| 6636 | int f = 0, r; |
| 6637 | |
| 6638 | if (!leaf || !(leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 6639 | return LY_TYPE_ERR; |
| 6640 | } |
| 6641 | |
| 6642 | if (leaf->value_type > 0 && (leaf->value_type & LY_DATA_TYPE_MASK) != LY_TYPE_UNION && |
| 6643 | (leaf->value_type & LY_DATA_TYPE_MASK) != LY_TYPE_LEAFREF) { |
| 6644 | /* we can get the type directly from the data node (it was already resolved) */ |
| 6645 | return leaf->value_type & LY_DATA_TYPE_MASK; |
| 6646 | } |
| 6647 | |
| 6648 | /* init */ |
| 6649 | type = &((struct lys_node_leaf *)leaf->schema)->type; |
| 6650 | value = leaf->value; |
| 6651 | ly_vlog_hide(1); |
| 6652 | |
| 6653 | /* resolve until we get the real data type */ |
| 6654 | while (1) { |
| 6655 | /* get the correct data type from schema */ |
| 6656 | switch (type->base) { |
| 6657 | case LY_TYPE_LEAFREF: |
| 6658 | type = &type->info.lref.target->type; |
| 6659 | break; /* continue in while loop */ |
| 6660 | case LY_TYPE_UNION: |
| 6661 | type_iter = NULL; |
| 6662 | while ((type_iter = lyp_get_next_union_type(type, type_iter, &f))) { |
| 6663 | if (type_iter->base == LY_TYPE_LEAFREF) { |
| 6664 | if (type_iter->info.lref.req == -1) { |
| 6665 | /* target not required, so it always succeeds */ |
| 6666 | break; |
| 6667 | } else { |
| 6668 | /* try to resolve leafref */ |
| 6669 | memset(&((struct lyd_node_leaf_list *)leaf)->value, 0, sizeof leaf->value); |
| 6670 | r = resolve_leafref((struct lyd_node_leaf_list *)leaf, type_iter); |
| 6671 | /* revert leaf's content affected by resolve_leafref */ |
| 6672 | ((struct lyd_node_leaf_list *)leaf)->value = value; |
| 6673 | if (!r) { |
| 6674 | /* success, we can continue with the leafref type */ |
| 6675 | break; |
| 6676 | } |
| 6677 | } |
| 6678 | } else if (type_iter->base == LY_TYPE_INST) { |
| 6679 | if (type_iter->info.inst.req == -1) { |
| 6680 | /* target not required, so it always succeeds */ |
| 6681 | return LY_TYPE_INST; |
| 6682 | } else { |
| 6683 | /* try to resolve instance-identifier */ |
| 6684 | ly_errno = 0; |
| 6685 | node = resolve_instid((struct lyd_node *)leaf, leaf->value_str); |
| 6686 | if (!ly_errno && node) { |
| 6687 | /* the real type is instance-identifier */ |
| 6688 | return LY_TYPE_INST; |
| 6689 | } |
| 6690 | } |
| 6691 | } else { |
| 6692 | r = lyp_parse_value_type((struct lyd_node_leaf_list *)leaf, type_iter, 1); |
| 6693 | /* revert leaf's content affected by resolve_leafref */ |
| 6694 | ((struct lyd_node_leaf_list *)leaf)->value = value; |
| 6695 | if (!r) { |
| 6696 | /* we have the real type */ |
| 6697 | return type_iter->base; |
| 6698 | } |
| 6699 | } |
| 6700 | f = 0; |
| 6701 | } |
| 6702 | /* erase ly_errno and ly_vecode */ |
| 6703 | ly_errno = LY_SUCCESS; |
| 6704 | ly_vecode = LYVE_SUCCESS; |
| 6705 | |
| 6706 | if (!type_iter) { |
| 6707 | LOGERR(LY_EINVAL, "Unable to get type from union \"%s\" with no valid type.", type->parent->name) |
| 6708 | return LY_TYPE_ERR; |
| 6709 | } |
| 6710 | type = type_iter; |
| 6711 | break; |
| 6712 | default: |
| 6713 | /* we have the real type */ |
| 6714 | ly_vlog_hide(0); |
| 6715 | return type->base; |
| 6716 | } |
| 6717 | } |
| 6718 | |
| 6719 | ly_vlog_hide(0); |
| 6720 | return LY_TYPE_ERR; |
| 6721 | } |
| 6722 | |
| 6723 | static int |
| 6724 | resolve_union(struct lyd_node_leaf_list *leaf, struct lys_type *type) |
| 6725 | { |
| 6726 | struct lys_type *datatype = NULL; |
| 6727 | int f = 0; |
| 6728 | |
| 6729 | assert(type->base == LY_TYPE_UNION); |
| 6730 | |
| 6731 | memset(&leaf->value, 0, sizeof leaf->value); |
| 6732 | while ((datatype = lyp_get_next_union_type(type, datatype, &f))) { |
| 6733 | leaf->value_type = datatype->base; |
| 6734 | |
| 6735 | if (datatype->base == LY_TYPE_LEAFREF) { |
| 6736 | /* try to resolve leafref */ |
| 6737 | if (!resolve_leafref(leaf, datatype)) { |
| 6738 | /* success */ |
| 6739 | break; |
| 6740 | } |
| 6741 | } else if (datatype->base == LY_TYPE_INST) { |
| 6742 | /* try to resolve instance-identifier */ |
| 6743 | ly_errno = 0; |
| 6744 | leaf->value.instance = resolve_instid((struct lyd_node *)leaf, leaf->value_str); |
| 6745 | if (!ly_errno && (leaf->value.instance || datatype->info.inst.req == -1)) { |
| 6746 | /* success */ |
| 6747 | break; |
| 6748 | } |
| 6749 | } else { |
| 6750 | if (!lyp_parse_value_type(leaf, datatype, 1)) { |
| 6751 | /* success */ |
| 6752 | break; |
| 6753 | } |
| 6754 | } |
| 6755 | f = 0; |
| 6756 | } |
| 6757 | /* erase ly_errno and ly_vecode */ |
| 6758 | ly_errno = LY_SUCCESS; |
| 6759 | ly_vecode = LYVE_SUCCESS; |
| 6760 | |
| 6761 | if (!datatype) { |
| 6762 | /* failure */ |
| 6763 | LOGVAL(LYE_INVAL, LY_VLOG_LYD, leaf, (leaf->value_str ? leaf->value_str : ""), leaf->schema->name); |
| 6764 | return EXIT_FAILURE; |
| 6765 | } |
| 6766 | |
| 6767 | return EXIT_SUCCESS; |
| 6768 | } |
| 6769 | |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6770 | /** |
| 6771 | * @brief Resolve a single unres data item. Logs directly. |
| 6772 | * |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6773 | * @param[in] node Data node to resolve. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6774 | * @param[in] type Type of the unresolved item. |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6775 | * |
| 6776 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 6777 | */ |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 6778 | int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6779 | resolve_unres_data_item(struct lyd_node *node, enum UNRES_ITEM type) |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6780 | { |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 6781 | int rc; |
Michal Vasko | 83a6c46 | 2015-10-08 16:43:53 +0200 | [diff] [blame] | 6782 | struct lyd_node_leaf_list *leaf; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6783 | struct lys_node_leaf *sleaf; |
Michal Vasko | c428084 | 2016-04-19 16:10:42 +0200 | [diff] [blame] | 6784 | struct lyd_node *parent; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6785 | |
Michal Vasko | 83a6c46 | 2015-10-08 16:43:53 +0200 | [diff] [blame] | 6786 | leaf = (struct lyd_node_leaf_list *)node; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6787 | sleaf = (struct lys_node_leaf *)leaf->schema; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6788 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6789 | switch (type) { |
| 6790 | case UNRES_LEAFREF: |
| 6791 | assert(sleaf->type.base == LY_TYPE_LEAFREF); |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6792 | return resolve_leafref(leaf, &sleaf->type); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6793 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6794 | case UNRES_INSTID: |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 6795 | assert(sleaf->type.base == LY_TYPE_INST); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6796 | ly_errno = 0; |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6797 | leaf->value.instance = resolve_instid(node, leaf->value_str); |
Radek Krejci | 40f17b9 | 2016-02-03 14:30:43 +0100 | [diff] [blame] | 6798 | if (!leaf->value.instance) { |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6799 | if (ly_errno) { |
| 6800 | return -1; |
| 6801 | } else if (sleaf->type.info.inst.req > -1) { |
Michal Vasko | 6ac6828 | 2016-04-11 10:56:47 +0200 | [diff] [blame] | 6802 | LOGVAL(LYE_NOREQINS, LY_VLOG_LYD, leaf, leaf->value_str); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6803 | return EXIT_FAILURE; |
| 6804 | } else { |
Michal Vasko | 9925e28 | 2016-09-02 12:45:14 +0200 | [diff] [blame] | 6805 | LOGVRB("There is no instance identifier \"%s\", but it is not required.", leaf->value_str); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6806 | } |
| 6807 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6808 | break; |
| 6809 | |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 6810 | case UNRES_UNION: |
| 6811 | assert(sleaf->type.base == LY_TYPE_UNION); |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6812 | return resolve_union(leaf, &sleaf->type); |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 6813 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6814 | case UNRES_WHEN: |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 6815 | if ((rc = resolve_when(node, NULL))) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6816 | return rc; |
| 6817 | } |
| 6818 | break; |
| 6819 | |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 6820 | case UNRES_MUST: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6821 | if ((rc = resolve_must(node, 0))) { |
| 6822 | return rc; |
| 6823 | } |
| 6824 | break; |
| 6825 | |
| 6826 | case UNRES_MUST_INOUT: |
| 6827 | if ((rc = resolve_must(node, 1))) { |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 6828 | return rc; |
| 6829 | } |
| 6830 | break; |
| 6831 | |
Michal Vasko | c428084 | 2016-04-19 16:10:42 +0200 | [diff] [blame] | 6832 | case UNRES_EMPTYCONT: |
| 6833 | do { |
| 6834 | parent = node->parent; |
| 6835 | lyd_free(node); |
| 6836 | node = parent; |
| 6837 | } while (node && (node->schema->nodetype == LYS_CONTAINER) && !node->child |
| 6838 | && !((struct lys_node_container *)node->schema)->presence); |
| 6839 | break; |
| 6840 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6841 | default: |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6842 | LOGINT; |
| 6843 | return -1; |
| 6844 | } |
| 6845 | |
| 6846 | return EXIT_SUCCESS; |
| 6847 | } |
| 6848 | |
| 6849 | /** |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6850 | * @brief add data unres item |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6851 | * |
| 6852 | * @param[in] unres Unres data structure to use. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6853 | * @param[in] node Data node to use. |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6854 | * |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6855 | * @return 0 on success, -1 on error. |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6856 | */ |
| 6857 | int |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6858 | 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] | 6859 | { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6860 | assert(unres && node); |
Michal Vasko | c428084 | 2016-04-19 16:10:42 +0200 | [diff] [blame] | 6861 | assert((type == UNRES_LEAFREF) || (type == UNRES_INSTID) || (type == UNRES_WHEN) || (type == UNRES_MUST) |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6862 | || (type == UNRES_MUST_INOUT) || (type == UNRES_UNION) || (type == UNRES_EMPTYCONT)); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6863 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6864 | unres->count++; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 6865 | unres->node = ly_realloc(unres->node, unres->count * sizeof *unres->node); |
| 6866 | if (!unres->node) { |
| 6867 | LOGMEM; |
| 6868 | return -1; |
| 6869 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6870 | unres->node[unres->count - 1] = node; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 6871 | unres->type = ly_realloc(unres->type, unres->count * sizeof *unres->type); |
| 6872 | if (!unres->type) { |
| 6873 | LOGMEM; |
| 6874 | return -1; |
| 6875 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6876 | unres->type[unres->count - 1] = type; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6877 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6878 | if (type == UNRES_WHEN) { |
| 6879 | /* remove previous result */ |
| 6880 | node->when_status = LYD_WHEN; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6881 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6882 | |
| 6883 | return EXIT_SUCCESS; |
| 6884 | } |
| 6885 | |
| 6886 | /** |
| 6887 | * @brief Resolve every unres data item in the structure. Logs directly. |
| 6888 | * |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 6889 | * If options includes LYD_OPT_TRUSTED, the data are considered trusted (when, must conditions are not expected, |
| 6890 | * unresolved leafrefs/instids are accepted). |
| 6891 | * |
| 6892 | * If options includes LYD_OPT_NOAUTODEL, the false resulting when condition on non-default nodes, the error is raised. |
| 6893 | * |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6894 | * @param[in] unres Unres data structure to use. |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 6895 | * @param[in,out] root Root node of the data tree, can be changed due to autodeletion. |
| 6896 | * @param[in] options Data options as described above. |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6897 | * |
| 6898 | * @return EXIT_SUCCESS on success, -1 on error. |
| 6899 | */ |
| 6900 | int |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 6901 | 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] | 6902 | { |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 6903 | 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] | 6904 | int rc, progress; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6905 | char *msg, *path; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6906 | struct lyd_node *parent; |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 6907 | struct lyd_node_leaf_list *leaf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6908 | |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 6909 | assert(root); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6910 | assert(unres); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6911 | |
| 6912 | if (!unres->count) { |
| 6913 | return EXIT_SUCCESS; |
| 6914 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6915 | |
Michal Vasko | a0ffcab | 2016-05-02 14:52:08 +0200 | [diff] [blame] | 6916 | LOGVRB("Resolving unresolved data nodes and their constraints..."); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6917 | ly_vlog_hide(1); |
| 6918 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6919 | /* when-stmt first */ |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6920 | ly_errno = LY_SUCCESS; |
| 6921 | ly_vecode = LYVE_SUCCESS; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6922 | do { |
| 6923 | progress = 0; |
Michal Vasko | 6df9413 | 2016-09-22 11:08:09 +0200 | [diff] [blame] | 6924 | for (i = 0; i < unres->count; i++) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6925 | if (unres->type[i] != UNRES_WHEN) { |
| 6926 | continue; |
| 6927 | } |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 6928 | assert(!(options & LYD_OPT_TRUSTED)); |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 6929 | if (first) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6930 | /* count when-stmt nodes in unres list */ |
| 6931 | when_stmt++; |
| 6932 | } |
| 6933 | |
| 6934 | /* resolve when condition only when all parent when conditions are already resolved */ |
| 6935 | for (parent = unres->node[i]->parent; |
| 6936 | parent && LYD_WHEN_DONE(parent->when_status); |
| 6937 | parent = parent->parent) { |
| 6938 | if (!parent->parent && (parent->when_status & LYD_WHEN_FALSE)) { |
| 6939 | /* the parent node was already unlinked, do not resolve this node, |
| 6940 | * it will be removed anyway, so just mark it as resolved |
| 6941 | */ |
| 6942 | unres->node[i]->when_status |= LYD_WHEN_FALSE; |
| 6943 | unres->type[i] = UNRES_RESOLVED; |
| 6944 | resolved++; |
| 6945 | break; |
| 6946 | } |
| 6947 | } |
| 6948 | if (parent) { |
| 6949 | continue; |
| 6950 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6951 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6952 | rc = resolve_unres_data_item(unres->node[i], unres->type[i]); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6953 | if (!rc) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6954 | if (unres->node[i]->when_status & LYD_WHEN_FALSE) { |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 6955 | if ((options & LYD_OPT_NOAUTODEL) && !unres->node[i]->dflt) { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6956 | /* false when condition */ |
| 6957 | ly_vlog_hide(0); |
Radek Krejci | 76e15e1 | 2016-06-22 11:02:24 +0200 | [diff] [blame] | 6958 | if (ly_log_level >= LY_LLERR) { |
| 6959 | path = strdup(ly_errpath()); |
| 6960 | msg = strdup(ly_errmsg()); |
| 6961 | LOGERR(LY_EVALID, "%s%s%s%s", msg, |
| 6962 | path[0] ? " (path: " : "", path[0] ? path : "", path[0] ? ")" : ""); |
| 6963 | free(path); |
| 6964 | free(msg); |
| 6965 | } |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6966 | return -1; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6967 | } /* follows else */ |
| 6968 | |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 6969 | /* only unlink now, the subtree can contain another nodes stored in the unres list */ |
| 6970 | /* if it has parent non-presence containers that would be empty, we should actually |
| 6971 | * remove the container |
| 6972 | */ |
Radek Krejci | 2537fd3 | 2016-09-07 16:22:41 +0200 | [diff] [blame] | 6973 | for (parent = unres->node[i]; |
| 6974 | parent->parent && parent->parent->schema->nodetype == LYS_CONTAINER; |
| 6975 | parent = parent->parent) { |
| 6976 | if (((struct lys_node_container *)parent->parent->schema)->presence) { |
| 6977 | /* presence container */ |
| 6978 | break; |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 6979 | } |
Radek Krejci | 2537fd3 | 2016-09-07 16:22:41 +0200 | [diff] [blame] | 6980 | if (parent->next || parent->prev != parent) { |
| 6981 | /* non empty (the child we are in and we are going to remove is not the only child) */ |
| 6982 | break; |
| 6983 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 6984 | } |
Radek Krejci | 2537fd3 | 2016-09-07 16:22:41 +0200 | [diff] [blame] | 6985 | unres->node[i] = parent; |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 6986 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6987 | /* auto-delete */ |
| 6988 | LOGVRB("auto-delete node \"%s\" due to when condition (%s)", ly_errpath(), |
| 6989 | ((struct lys_node_leaf *)unres->node[i]->schema)->when->cond); |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 6990 | if (*root && *root == unres->node[i]) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6991 | *root = (*root)->next; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6992 | } |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6993 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6994 | lyd_unlink(unres->node[i]); |
| 6995 | unres->type[i] = UNRES_DELETE; |
| 6996 | del_items++; |
Radek Krejci | 51fd816 | 2016-03-24 15:49:51 +0100 | [diff] [blame] | 6997 | |
| 6998 | /* update the rest of unres items */ |
| 6999 | for (j = 0; j < unres->count; j++) { |
Radek Krejci | 3db819b | 2016-03-24 16:29:48 +0100 | [diff] [blame] | 7000 | if (unres->type[j] == UNRES_RESOLVED || unres->type[j] == UNRES_DELETE) { |
Radek Krejci | 51fd816 | 2016-03-24 15:49:51 +0100 | [diff] [blame] | 7001 | continue; |
| 7002 | } |
| 7003 | |
| 7004 | /* test if the node is in subtree to be deleted */ |
| 7005 | for (parent = unres->node[j]; parent; parent = parent->parent) { |
| 7006 | if (parent == unres->node[i]) { |
| 7007 | /* yes, it is */ |
| 7008 | unres->type[j] = UNRES_RESOLVED; |
| 7009 | resolved++; |
| 7010 | break; |
| 7011 | } |
| 7012 | } |
| 7013 | } |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7014 | } else { |
| 7015 | unres->type[i] = UNRES_RESOLVED; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7016 | } |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7017 | ly_errno = LY_SUCCESS; |
| 7018 | ly_vecode = LYVE_SUCCESS; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7019 | resolved++; |
| 7020 | progress = 1; |
| 7021 | } else if (rc == -1) { |
| 7022 | ly_vlog_hide(0); |
Michal Vasko | 76e7340 | 2016-08-24 16:00:13 +0200 | [diff] [blame] | 7023 | /* print only this last error */ |
| 7024 | resolve_unres_data_item(unres->node[i], unres->type[i]); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7025 | return -1; |
Radek Krejci | c2a180f | 2016-06-22 13:28:16 +0200 | [diff] [blame] | 7026 | } else { |
| 7027 | /* forward reference, erase ly_errno */ |
| 7028 | ly_errno = LY_SUCCESS; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7029 | } |
| 7030 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7031 | first = 0; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7032 | } while (progress && resolved < when_stmt); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7033 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7034 | /* do we have some unresolved when-stmt? */ |
Radek Krejci | d940d73 | 2016-03-24 16:02:28 +0100 | [diff] [blame] | 7035 | if (when_stmt > resolved) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7036 | ly_vlog_hide(0); |
Radek Krejci | 76e15e1 | 2016-06-22 11:02:24 +0200 | [diff] [blame] | 7037 | if (ly_log_level >= LY_LLERR) { |
| 7038 | path = strdup(ly_errpath()); |
| 7039 | msg = strdup(ly_errmsg()); |
| 7040 | LOGERR(LY_EVALID, "%s%s%s%s", msg, path[0] ? " (path: " : "", path[0] ? path : "", path[0] ? ")" : ""); |
| 7041 | free(path); |
| 7042 | free(msg); |
| 7043 | } |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7044 | return -1; |
| 7045 | } |
| 7046 | |
| 7047 | for (i = 0; del_items && i < unres->count; i++) { |
| 7048 | /* we had some when-stmt resulted to false, so now we have to sanitize the unres list */ |
| 7049 | if (unres->type[i] != UNRES_DELETE) { |
| 7050 | continue; |
| 7051 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7052 | if (!unres->node[i]) { |
| 7053 | unres->type[i] = UNRES_RESOLVED; |
| 7054 | del_items--; |
| 7055 | continue; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7056 | } |
| 7057 | |
| 7058 | /* really remove the complete subtree */ |
| 7059 | lyd_free(unres->node[i]); |
| 7060 | unres->type[i] = UNRES_RESOLVED; |
| 7061 | del_items--; |
| 7062 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7063 | |
| 7064 | /* rest */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7065 | for (i = 0; i < unres->count; ++i) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7066 | if (unres->type[i] == UNRES_RESOLVED) { |
| 7067 | continue; |
| 7068 | } |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 7069 | assert(!(options & LYD_OPT_TRUSTED) || ((unres->type[i] != UNRES_MUST) && (unres->type[i] != UNRES_MUST_INOUT))); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7070 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7071 | rc = resolve_unres_data_item(unres->node[i], unres->type[i]); |
Michal Vasko | 6df9413 | 2016-09-22 11:08:09 +0200 | [diff] [blame] | 7072 | if (rc == -1) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7073 | ly_vlog_hide(0); |
Michal Vasko | 96b846c | 2016-05-18 13:28:58 +0200 | [diff] [blame] | 7074 | /* print only this last error */ |
| 7075 | resolve_unres_data_item(unres->node[i], unres->type[i]); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7076 | return -1; |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 7077 | } else if ((rc == 0) || ((options & LYD_OPT_TRUSTED) && ((unres->type[i] == UNRES_LEAFREF) || (unres->type[i] == UNRES_INSTID)))) { |
Michal Vasko | 6df9413 | 2016-09-22 11:08:09 +0200 | [diff] [blame] | 7078 | unres->type[i] = UNRES_RESOLVED; |
| 7079 | resolved++; |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 7080 | if (options & LYD_OPT_TRUSTED) { |
Michal Vasko | 6df9413 | 2016-09-22 11:08:09 +0200 | [diff] [blame] | 7081 | /* accept it in this case */ |
| 7082 | if (unres->type[i] == UNRES_LEAFREF) { |
| 7083 | LOGVRB("Leafref \"%s\" with value \"%s\" failed to be resolved.", |
| 7084 | ((struct lys_node_leaf *)unres->node[i]->schema)->type.info.lref.path, |
| 7085 | ((struct lyd_node_leaf_list *)unres->node[i])->value_str); |
| 7086 | } else { |
| 7087 | LOGVRB("Instance identifier \"%s\" failed to be resolved.", |
| 7088 | ((struct lyd_node_leaf_list *)unres->node[i])->value_str); |
| 7089 | } |
| 7090 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7091 | } |
| 7092 | } |
| 7093 | |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7094 | ly_vlog_hide(0); |
| 7095 | if (resolved < unres->count) { |
| 7096 | /* try to resolve the unresolved data again, it will not resolve anything, but it will print |
| 7097 | * all the validation errors |
| 7098 | */ |
| 7099 | for (i = 0; i < unres->count; ++i) { |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7100 | if (unres->type[i] == UNRES_UNION) { |
| 7101 | /* does not make sense to print specific errors for all |
| 7102 | * the data types, just print that the value is invalid */ |
| 7103 | leaf = (struct lyd_node_leaf_list *)unres->node[i]; |
| 7104 | LOGVAL(LYE_INVAL, LY_VLOG_LYD, unres->node[i], (leaf->value_str ? leaf->value_str : ""), |
| 7105 | leaf->schema->name); |
| 7106 | } else if (unres->type[i] != UNRES_RESOLVED) { |
| 7107 | resolve_unres_data_item(unres->node[i], unres->type[i]); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7108 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7109 | } |
| 7110 | return -1; |
| 7111 | } |
| 7112 | |
Michal Vasko | a0ffcab | 2016-05-02 14:52:08 +0200 | [diff] [blame] | 7113 | LOGVRB("All data nodes and constraints resolved."); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7114 | unres->count = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7115 | return EXIT_SUCCESS; |
| 7116 | } |