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 | f9b35d9 | 2016-10-21 15:19:30 +0200 | [diff] [blame] | 33 | static int resolve_union(struct lyd_node_leaf_list *leaf, struct lys_type *type); |
| 34 | |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 35 | int |
| 36 | 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] | 37 | { |
| 38 | const char *ptr; |
| 39 | int minus = 0; |
| 40 | int64_t ret = 0; |
| 41 | int8_t str_exp, str_dig = -1; |
| 42 | |
| 43 | ptr = *str_num; |
| 44 | |
| 45 | if (ptr[0] == '-') { |
| 46 | minus = 1; |
| 47 | ++ptr; |
| 48 | } |
| 49 | |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 50 | if (!isdigit(ptr[0])) { |
| 51 | /* there must be at least one */ |
| 52 | return 1; |
| 53 | } |
| 54 | |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 55 | for (str_exp = 0; isdigit(ptr[0]) || ((ptr[0] == '.') && (str_dig < 0)); ++ptr) { |
| 56 | if (str_exp > 18) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 57 | return 1; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | if (ptr[0] == '.') { |
| 61 | if (ptr[1] == '.') { |
| 62 | /* it's the next interval */ |
| 63 | break; |
| 64 | } |
| 65 | ++str_dig; |
| 66 | } else { |
| 67 | ret = ret * 10 + (ptr[0] - 48); |
| 68 | if (str_dig > -1) { |
| 69 | ++str_dig; |
| 70 | } |
| 71 | ++str_exp; |
| 72 | } |
| 73 | } |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 74 | if (str_dig == 0) { |
| 75 | /* no digits after '.' */ |
| 76 | return 1; |
| 77 | } else if (str_dig == -1) { |
| 78 | /* there are 0 numbers after the floating point */ |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 79 | str_dig = 0; |
| 80 | } |
| 81 | |
| 82 | /* it's parsed, now adjust the number based on fraction-digits, if needed */ |
| 83 | if (str_dig < dig) { |
| 84 | if ((str_exp - 1) + (dig - str_dig) > 18) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 85 | return 1; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 86 | } |
| 87 | ret *= dec_pow(dig - str_dig); |
| 88 | } |
| 89 | if (str_dig > dig) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 90 | return 1; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | if (minus) { |
| 94 | ret *= -1; |
| 95 | } |
| 96 | *str_num = ptr; |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 97 | *num = ret; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 98 | |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 99 | return 0; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | /** |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 103 | * @brief Parse an identifier. |
| 104 | * |
| 105 | * ;; An identifier MUST NOT start with (('X'|'x') ('M'|'m') ('L'|'l')) |
| 106 | * identifier = (ALPHA / "_") |
| 107 | * *(ALPHA / DIGIT / "_" / "-" / ".") |
| 108 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 109 | * @param[in] id Identifier to use. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 110 | * |
| 111 | * @return Number of characters successfully parsed. |
| 112 | */ |
Michal Vasko | 249e6b5 | 2015-08-19 11:08:52 +0200 | [diff] [blame] | 113 | int |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 114 | parse_identifier(const char *id) |
| 115 | { |
| 116 | int parsed = 0; |
| 117 | |
Michal Vasko | 1ab90bc | 2016-03-15 10:40:22 +0100 | [diff] [blame] | 118 | assert(id); |
| 119 | |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 120 | if (!isalpha(id[0]) && (id[0] != '_')) { |
| 121 | return -parsed; |
| 122 | } |
| 123 | |
| 124 | ++parsed; |
| 125 | ++id; |
| 126 | |
| 127 | while (isalnum(id[0]) || (id[0] == '_') || (id[0] == '-') || (id[0] == '.')) { |
| 128 | ++parsed; |
| 129 | ++id; |
| 130 | } |
| 131 | |
| 132 | return parsed; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * @brief Parse a node-identifier. |
| 137 | * |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 138 | * node-identifier = [module-name ":"] identifier |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 139 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 140 | * @param[in] id Identifier to use. |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 141 | * @param[out] mod_name Points to the module name, NULL if there is not any. |
| 142 | * @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] | 143 | * @param[out] name Points to the node name. |
| 144 | * @param[out] nam_len Length of the node name. |
| 145 | * |
| 146 | * @return Number of characters successfully parsed, |
| 147 | * positive on success, negative on failure. |
| 148 | */ |
| 149 | static int |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 150 | 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] | 151 | { |
| 152 | int parsed = 0, ret; |
| 153 | |
| 154 | assert(id); |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 155 | if (mod_name) { |
| 156 | *mod_name = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 157 | } |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 158 | if (mod_name_len) { |
| 159 | *mod_name_len = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 160 | } |
| 161 | if (name) { |
| 162 | *name = NULL; |
| 163 | } |
| 164 | if (nam_len) { |
| 165 | *nam_len = 0; |
| 166 | } |
| 167 | |
| 168 | if ((ret = parse_identifier(id)) < 1) { |
| 169 | return ret; |
| 170 | } |
| 171 | |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 172 | if (mod_name) { |
| 173 | *mod_name = id; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 174 | } |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 175 | if (mod_name_len) { |
| 176 | *mod_name_len = ret; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | parsed += ret; |
| 180 | id += ret; |
| 181 | |
| 182 | /* there is prefix */ |
| 183 | if (id[0] == ':') { |
| 184 | ++parsed; |
| 185 | ++id; |
| 186 | |
| 187 | /* there isn't */ |
| 188 | } else { |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 189 | if (name && mod_name) { |
| 190 | *name = *mod_name; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 191 | } |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 192 | if (mod_name) { |
| 193 | *mod_name = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 194 | } |
| 195 | |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 196 | if (nam_len && mod_name_len) { |
| 197 | *nam_len = *mod_name_len; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 198 | } |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 199 | if (mod_name_len) { |
| 200 | *mod_name_len = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | return parsed; |
| 204 | } |
| 205 | |
| 206 | /* identifier (node name) */ |
| 207 | if ((ret = parse_identifier(id)) < 1) { |
| 208 | return -parsed+ret; |
| 209 | } |
| 210 | |
| 211 | if (name) { |
| 212 | *name = id; |
| 213 | } |
| 214 | if (nam_len) { |
| 215 | *nam_len = ret; |
| 216 | } |
| 217 | |
| 218 | return parsed+ret; |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * @brief Parse a path-predicate (leafref). |
| 223 | * |
| 224 | * path-predicate = "[" *WSP path-equality-expr *WSP "]" |
| 225 | * path-equality-expr = node-identifier *WSP "=" *WSP path-key-expr |
| 226 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 227 | * @param[in] id Identifier to use. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 228 | * @param[out] prefix Points to the prefix, NULL if there is not any. |
| 229 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 230 | * @param[out] name Points to the node name. |
| 231 | * @param[out] nam_len Length of the node name. |
| 232 | * @param[out] path_key_expr Points to the path-key-expr. |
| 233 | * @param[out] pke_len Length of the path-key-expr. |
| 234 | * @param[out] has_predicate Flag to mark whether there is another predicate following. |
| 235 | * |
| 236 | * @return Number of characters successfully parsed, |
| 237 | * positive on success, negative on failure. |
| 238 | */ |
| 239 | static int |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 240 | parse_path_predicate(const char *id, const char **prefix, int *pref_len, const char **name, int *nam_len, |
| 241 | const char **path_key_expr, int *pke_len, int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 242 | { |
| 243 | const char *ptr; |
| 244 | int parsed = 0, ret; |
| 245 | |
| 246 | assert(id); |
| 247 | if (prefix) { |
| 248 | *prefix = NULL; |
| 249 | } |
| 250 | if (pref_len) { |
| 251 | *pref_len = 0; |
| 252 | } |
| 253 | if (name) { |
| 254 | *name = NULL; |
| 255 | } |
| 256 | if (nam_len) { |
| 257 | *nam_len = 0; |
| 258 | } |
| 259 | if (path_key_expr) { |
| 260 | *path_key_expr = NULL; |
| 261 | } |
| 262 | if (pke_len) { |
| 263 | *pke_len = 0; |
| 264 | } |
| 265 | if (has_predicate) { |
| 266 | *has_predicate = 0; |
| 267 | } |
| 268 | |
| 269 | if (id[0] != '[') { |
| 270 | return -parsed; |
| 271 | } |
| 272 | |
| 273 | ++parsed; |
| 274 | ++id; |
| 275 | |
| 276 | while (isspace(id[0])) { |
| 277 | ++parsed; |
| 278 | ++id; |
| 279 | } |
| 280 | |
| 281 | if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len)) < 1) { |
| 282 | return -parsed+ret; |
| 283 | } |
| 284 | |
| 285 | parsed += ret; |
| 286 | id += ret; |
| 287 | |
| 288 | while (isspace(id[0])) { |
| 289 | ++parsed; |
| 290 | ++id; |
| 291 | } |
| 292 | |
| 293 | if (id[0] != '=') { |
| 294 | return -parsed; |
| 295 | } |
| 296 | |
| 297 | ++parsed; |
| 298 | ++id; |
| 299 | |
| 300 | while (isspace(id[0])) { |
| 301 | ++parsed; |
| 302 | ++id; |
| 303 | } |
| 304 | |
| 305 | if ((ptr = strchr(id, ']')) == NULL) { |
| 306 | return -parsed; |
| 307 | } |
| 308 | |
| 309 | --ptr; |
| 310 | while (isspace(ptr[0])) { |
| 311 | --ptr; |
| 312 | } |
| 313 | ++ptr; |
| 314 | |
| 315 | ret = ptr-id; |
| 316 | if (path_key_expr) { |
| 317 | *path_key_expr = id; |
| 318 | } |
| 319 | if (pke_len) { |
| 320 | *pke_len = ret; |
| 321 | } |
| 322 | |
| 323 | parsed += ret; |
| 324 | id += ret; |
| 325 | |
| 326 | while (isspace(id[0])) { |
| 327 | ++parsed; |
| 328 | ++id; |
| 329 | } |
| 330 | |
| 331 | assert(id[0] == ']'); |
| 332 | |
| 333 | if (id[1] == '[') { |
| 334 | *has_predicate = 1; |
| 335 | } |
| 336 | |
| 337 | return parsed+1; |
| 338 | } |
| 339 | |
| 340 | /** |
| 341 | * @brief Parse a path-key-expr (leafref). First call parses "current()", all |
| 342 | * the ".." and the first node-identifier, other calls parse a single |
| 343 | * node-identifier each. |
| 344 | * |
| 345 | * path-key-expr = current-function-invocation *WSP "/" *WSP |
| 346 | * rel-path-keyexpr |
| 347 | * rel-path-keyexpr = 1*(".." *WSP "/" *WSP) |
| 348 | * *(node-identifier *WSP "/" *WSP) |
| 349 | * node-identifier |
| 350 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 351 | * @param[in] id Identifier to use. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 352 | * @param[out] prefix Points to the prefix, NULL if there is not any. |
| 353 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 354 | * @param[out] name Points to the node name. |
| 355 | * @param[out] nam_len Length of the node name. |
| 356 | * @param[out] parent_times Number of ".." in the path. Must be 0 on the first call, |
| 357 | * must not be changed between consecutive calls. |
| 358 | * @return Number of characters successfully parsed, |
| 359 | * positive on success, negative on failure. |
| 360 | */ |
| 361 | static int |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 362 | parse_path_key_expr(const char *id, const char **prefix, int *pref_len, const char **name, int *nam_len, |
| 363 | int *parent_times) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 364 | { |
| 365 | int parsed = 0, ret, par_times = 0; |
| 366 | |
| 367 | assert(id); |
| 368 | assert(parent_times); |
| 369 | if (prefix) { |
| 370 | *prefix = NULL; |
| 371 | } |
| 372 | if (pref_len) { |
| 373 | *pref_len = 0; |
| 374 | } |
| 375 | if (name) { |
| 376 | *name = NULL; |
| 377 | } |
| 378 | if (nam_len) { |
| 379 | *nam_len = 0; |
| 380 | } |
| 381 | |
| 382 | if (!*parent_times) { |
| 383 | /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */ |
| 384 | if (strncmp(id, "current()", 9)) { |
| 385 | return -parsed; |
| 386 | } |
| 387 | |
| 388 | parsed += 9; |
| 389 | id += 9; |
| 390 | |
| 391 | while (isspace(id[0])) { |
| 392 | ++parsed; |
| 393 | ++id; |
| 394 | } |
| 395 | |
| 396 | if (id[0] != '/') { |
| 397 | return -parsed; |
| 398 | } |
| 399 | |
| 400 | ++parsed; |
| 401 | ++id; |
| 402 | |
| 403 | while (isspace(id[0])) { |
| 404 | ++parsed; |
| 405 | ++id; |
| 406 | } |
| 407 | |
| 408 | /* rel-path-keyexpr */ |
| 409 | if (strncmp(id, "..", 2)) { |
| 410 | return -parsed; |
| 411 | } |
| 412 | ++par_times; |
| 413 | |
| 414 | parsed += 2; |
| 415 | id += 2; |
| 416 | |
| 417 | while (isspace(id[0])) { |
| 418 | ++parsed; |
| 419 | ++id; |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | /* 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier |
| 424 | * |
| 425 | * first parent reference with whitespaces already parsed |
| 426 | */ |
| 427 | if (id[0] != '/') { |
| 428 | return -parsed; |
| 429 | } |
| 430 | |
| 431 | ++parsed; |
| 432 | ++id; |
| 433 | |
| 434 | while (isspace(id[0])) { |
| 435 | ++parsed; |
| 436 | ++id; |
| 437 | } |
| 438 | |
| 439 | while (!strncmp(id, "..", 2) && !*parent_times) { |
| 440 | ++par_times; |
| 441 | |
| 442 | parsed += 2; |
| 443 | id += 2; |
| 444 | |
| 445 | while (isspace(id[0])) { |
| 446 | ++parsed; |
| 447 | ++id; |
| 448 | } |
| 449 | |
| 450 | if (id[0] != '/') { |
| 451 | return -parsed; |
| 452 | } |
| 453 | |
| 454 | ++parsed; |
| 455 | ++id; |
| 456 | |
| 457 | while (isspace(id[0])) { |
| 458 | ++parsed; |
| 459 | ++id; |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | if (!*parent_times) { |
| 464 | *parent_times = par_times; |
| 465 | } |
| 466 | |
| 467 | /* all parent references must be parsed at this point */ |
| 468 | if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len)) < 1) { |
| 469 | return -parsed+ret; |
| 470 | } |
| 471 | |
| 472 | parsed += ret; |
| 473 | id += ret; |
| 474 | |
| 475 | return parsed; |
| 476 | } |
| 477 | |
| 478 | /** |
| 479 | * @brief Parse path-arg (leafref). |
| 480 | * |
| 481 | * path-arg = absolute-path / relative-path |
| 482 | * absolute-path = 1*("/" (node-identifier *path-predicate)) |
| 483 | * relative-path = 1*(".." "/") descendant-path |
| 484 | * |
Radek Krejci | f7ed4c3 | 2016-10-27 16:20:03 +0200 | [diff] [blame^] | 485 | * @param[in] mod Module of the context node to get correct prefix in case it is not explicitly specified |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 486 | * @param[in] id Identifier to use. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 487 | * @param[out] prefix Points to the prefix, NULL if there is not any. |
| 488 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 489 | * @param[out] name Points to the node name. |
| 490 | * @param[out] nam_len Length of the node name. |
| 491 | * @param[out] parent_times Number of ".." in the path. Must be 0 on the first call, |
| 492 | * must not be changed between consecutive calls. -1 if the |
| 493 | * path is relative. |
| 494 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 495 | * |
| 496 | * @return Number of characters successfully parsed, |
| 497 | * positive on success, negative on failure. |
| 498 | */ |
| 499 | static int |
Radek Krejci | f7ed4c3 | 2016-10-27 16:20:03 +0200 | [diff] [blame^] | 500 | parse_path_arg(struct lys_module *mod, const char *id, const char **prefix, int *pref_len, |
| 501 | const char **name, int *nam_len, int *parent_times, int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 502 | { |
| 503 | int parsed = 0, ret, par_times = 0; |
| 504 | |
| 505 | assert(id); |
| 506 | assert(parent_times); |
| 507 | if (prefix) { |
| 508 | *prefix = NULL; |
| 509 | } |
| 510 | if (pref_len) { |
| 511 | *pref_len = 0; |
| 512 | } |
| 513 | if (name) { |
| 514 | *name = NULL; |
| 515 | } |
| 516 | if (nam_len) { |
| 517 | *nam_len = 0; |
| 518 | } |
| 519 | if (has_predicate) { |
| 520 | *has_predicate = 0; |
| 521 | } |
| 522 | |
| 523 | if (!*parent_times && !strncmp(id, "..", 2)) { |
| 524 | ++par_times; |
| 525 | |
| 526 | parsed += 2; |
| 527 | id += 2; |
| 528 | |
| 529 | while (!strncmp(id, "/..", 3)) { |
| 530 | ++par_times; |
| 531 | |
| 532 | parsed += 3; |
| 533 | id += 3; |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | if (!*parent_times) { |
| 538 | if (par_times) { |
| 539 | *parent_times = par_times; |
| 540 | } else { |
| 541 | *parent_times = -1; |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | if (id[0] != '/') { |
| 546 | return -parsed; |
| 547 | } |
| 548 | |
| 549 | /* skip '/' */ |
| 550 | ++parsed; |
| 551 | ++id; |
| 552 | |
| 553 | /* node-identifier ([prefix:]identifier) */ |
| 554 | if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len)) < 1) { |
| 555 | return -parsed-ret; |
| 556 | } |
Radek Krejci | f7ed4c3 | 2016-10-27 16:20:03 +0200 | [diff] [blame^] | 557 | if (!(*prefix)) { |
| 558 | /* actually we always need prefix even it is not specified */ |
| 559 | *prefix = lys_main_module(mod)->name; |
| 560 | *pref_len = strlen(*prefix); |
| 561 | } |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 562 | |
| 563 | parsed += ret; |
| 564 | id += ret; |
| 565 | |
| 566 | /* there is no predicate */ |
| 567 | if ((id[0] == '/') || !id[0]) { |
| 568 | return parsed; |
| 569 | } else if (id[0] != '[') { |
| 570 | return -parsed; |
| 571 | } |
| 572 | |
| 573 | if (has_predicate) { |
| 574 | *has_predicate = 1; |
| 575 | } |
| 576 | |
| 577 | return parsed; |
| 578 | } |
| 579 | |
| 580 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 581 | * @brief Parse instance-identifier in JSON data format. That means that prefixes |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 582 | * (which are mandatory for every node-identifier) are actually model names. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 583 | * |
| 584 | * instance-identifier = 1*("/" (node-identifier *predicate)) |
| 585 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 586 | * @param[in] id Identifier to use. |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 587 | * @param[out] model Points to the model name. |
| 588 | * @param[out] mod_len Length of the model name. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 589 | * @param[out] name Points to the node name. |
| 590 | * @param[out] nam_len Length of the node name. |
| 591 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 592 | * |
| 593 | * @return Number of characters successfully parsed, |
| 594 | * positive on success, negative on failure. |
| 595 | */ |
| 596 | static int |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 597 | parse_instance_identifier(const char *id, const char **model, int *mod_len, const char **name, int *nam_len, |
| 598 | int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 599 | { |
| 600 | int parsed = 0, ret; |
| 601 | |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 602 | if (has_predicate) { |
| 603 | *has_predicate = 0; |
| 604 | } |
| 605 | |
| 606 | if (id[0] != '/') { |
| 607 | return -parsed; |
| 608 | } |
| 609 | |
| 610 | ++parsed; |
| 611 | ++id; |
| 612 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 613 | if ((ret = parse_identifier(id)) < 1) { |
| 614 | return ret; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 615 | } |
| 616 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 617 | *model = id; |
| 618 | *mod_len = ret; |
| 619 | |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 620 | parsed += ret; |
| 621 | id += ret; |
| 622 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 623 | if (id[0] != ':') { |
| 624 | return -parsed; |
| 625 | } |
| 626 | |
| 627 | ++parsed; |
| 628 | ++id; |
| 629 | |
| 630 | if ((ret = parse_identifier(id)) < 1) { |
| 631 | return ret; |
| 632 | } |
| 633 | |
| 634 | *name = id; |
| 635 | *nam_len = ret; |
| 636 | |
| 637 | parsed += ret; |
| 638 | id += ret; |
| 639 | |
Radek Krejci | 4967cb6 | 2016-09-14 16:40:28 +0200 | [diff] [blame] | 640 | if (id[0] == '[' && has_predicate) { |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 641 | *has_predicate = 1; |
| 642 | } |
| 643 | |
| 644 | return parsed; |
| 645 | } |
| 646 | |
| 647 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 648 | * @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] | 649 | * (which are mandatory) are actually model names. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 650 | * |
| 651 | * predicate = "[" *WSP (predicate-expr / pos) *WSP "]" |
| 652 | * predicate-expr = (node-identifier / ".") *WSP "=" *WSP |
| 653 | * ((DQUOTE string DQUOTE) / |
| 654 | * (SQUOTE string SQUOTE)) |
| 655 | * pos = non-negative-integer-value |
| 656 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 657 | * @param[in] id Identifier to use. |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 658 | * @param[out] model Points to the model name. |
| 659 | * @param[out] mod_len Length of the model name. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 660 | * @param[out] name Points to the node name. Can be identifier (from node-identifier), "." or pos. |
| 661 | * @param[out] nam_len Length of the node name. |
| 662 | * @param[out] value Value the node-identifier must have (string from the grammar), |
| 663 | * NULL if there is not any. |
| 664 | * @param[out] val_len Length of the value, 0 if there is not any. |
| 665 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 666 | * |
| 667 | * @return Number of characters successfully parsed, |
| 668 | * positive on success, negative on failure. |
| 669 | */ |
| 670 | static int |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 671 | parse_predicate(const char *id, const char **model, int *mod_len, const char **name, int *nam_len, |
| 672 | const char **value, int *val_len, int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 673 | { |
| 674 | const char *ptr; |
| 675 | int parsed = 0, ret; |
| 676 | char quote; |
| 677 | |
| 678 | assert(id); |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 679 | if (model) { |
| 680 | *model = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 681 | } |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 682 | if (mod_len) { |
| 683 | *mod_len = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 684 | } |
| 685 | if (name) { |
| 686 | *name = NULL; |
| 687 | } |
| 688 | if (nam_len) { |
| 689 | *nam_len = 0; |
| 690 | } |
| 691 | if (value) { |
| 692 | *value = NULL; |
| 693 | } |
| 694 | if (val_len) { |
| 695 | *val_len = 0; |
| 696 | } |
| 697 | if (has_predicate) { |
| 698 | *has_predicate = 0; |
| 699 | } |
| 700 | |
| 701 | if (id[0] != '[') { |
| 702 | return -parsed; |
| 703 | } |
| 704 | |
| 705 | ++parsed; |
| 706 | ++id; |
| 707 | |
| 708 | while (isspace(id[0])) { |
| 709 | ++parsed; |
| 710 | ++id; |
| 711 | } |
| 712 | |
| 713 | /* pos */ |
| 714 | if (isdigit(id[0])) { |
| 715 | if (name) { |
| 716 | *name = id; |
| 717 | } |
| 718 | |
| 719 | if (id[0] == '0') { |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 720 | return -parsed; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | while (isdigit(id[0])) { |
| 724 | ++parsed; |
| 725 | ++id; |
| 726 | } |
| 727 | |
| 728 | if (nam_len) { |
| 729 | *nam_len = id-(*name); |
| 730 | } |
| 731 | |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 732 | /* "." or node-identifier */ |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 733 | } else { |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 734 | if (id[0] == '.') { |
| 735 | if (name) { |
| 736 | *name = id; |
| 737 | } |
| 738 | if (nam_len) { |
| 739 | *nam_len = 1; |
| 740 | } |
| 741 | |
| 742 | ++parsed; |
| 743 | ++id; |
| 744 | |
| 745 | } else { |
| 746 | if ((ret = parse_node_identifier(id, model, mod_len, name, nam_len)) < 1) { |
| 747 | return -parsed+ret; |
| 748 | } else if (model && !*model) { |
| 749 | return -parsed; |
| 750 | } |
| 751 | |
| 752 | parsed += ret; |
| 753 | id += ret; |
| 754 | } |
| 755 | |
| 756 | while (isspace(id[0])) { |
| 757 | ++parsed; |
| 758 | ++id; |
| 759 | } |
| 760 | |
| 761 | if (id[0] != '=') { |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 762 | return -parsed; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 763 | } |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 764 | |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 765 | ++parsed; |
| 766 | ++id; |
| 767 | |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 768 | while (isspace(id[0])) { |
| 769 | ++parsed; |
| 770 | ++id; |
| 771 | } |
| 772 | |
| 773 | /* ((DQUOTE string DQUOTE) / (SQUOTE string SQUOTE)) */ |
| 774 | if ((id[0] == '\"') || (id[0] == '\'')) { |
| 775 | quote = id[0]; |
| 776 | |
| 777 | ++parsed; |
| 778 | ++id; |
| 779 | |
| 780 | if ((ptr = strchr(id, quote)) == NULL) { |
| 781 | return -parsed; |
| 782 | } |
| 783 | ret = ptr-id; |
| 784 | |
| 785 | if (value) { |
| 786 | *value = id; |
| 787 | } |
| 788 | if (val_len) { |
| 789 | *val_len = ret; |
| 790 | } |
| 791 | |
| 792 | parsed += ret+1; |
| 793 | id += ret+1; |
| 794 | } else { |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 795 | return -parsed; |
| 796 | } |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | while (isspace(id[0])) { |
| 800 | ++parsed; |
| 801 | ++id; |
| 802 | } |
| 803 | |
| 804 | if (id[0] != ']') { |
| 805 | return -parsed; |
| 806 | } |
| 807 | |
| 808 | ++parsed; |
| 809 | ++id; |
| 810 | |
| 811 | if ((id[0] == '[') && has_predicate) { |
| 812 | *has_predicate = 1; |
| 813 | } |
| 814 | |
| 815 | return parsed; |
| 816 | } |
| 817 | |
| 818 | /** |
| 819 | * @brief Parse schema-nodeid. |
| 820 | * |
| 821 | * schema-nodeid = absolute-schema-nodeid / |
| 822 | * descendant-schema-nodeid |
| 823 | * absolute-schema-nodeid = 1*("/" node-identifier) |
Michal Vasko | 4893535 | 2016-03-29 11:52:36 +0200 | [diff] [blame] | 824 | * descendant-schema-nodeid = ["." "/"] |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 825 | * node-identifier |
| 826 | * absolute-schema-nodeid |
| 827 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 828 | * @param[in] id Identifier to use. |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 829 | * @param[out] mod_name Points to the module name, NULL if there is not any. |
| 830 | * @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] | 831 | * @param[out] name Points to the node name. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 832 | * @param[out] nam_len Length of the node name. |
| 833 | * @param[out] is_relative Flag to mark whether the nodeid is absolute or descendant. Must be -1 |
| 834 | * on the first call, must not be changed between consecutive calls. |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 835 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. It cannot be |
| 836 | * based on the grammar, in those cases use NULL. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 837 | * |
| 838 | * @return Number of characters successfully parsed, |
| 839 | * positive on success, negative on failure. |
| 840 | */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 841 | int |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 842 | 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] | 843 | int *is_relative, int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 844 | { |
| 845 | int parsed = 0, ret; |
| 846 | |
| 847 | assert(id); |
| 848 | assert(is_relative); |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 849 | if (mod_name) { |
| 850 | *mod_name = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 851 | } |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 852 | if (mod_name_len) { |
| 853 | *mod_name_len = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 854 | } |
| 855 | if (name) { |
| 856 | *name = NULL; |
| 857 | } |
| 858 | if (nam_len) { |
| 859 | *nam_len = 0; |
| 860 | } |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 861 | if (has_predicate) { |
| 862 | *has_predicate = 0; |
| 863 | } |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 864 | |
| 865 | if (id[0] != '/') { |
| 866 | if (*is_relative != -1) { |
| 867 | return -parsed; |
| 868 | } else { |
| 869 | *is_relative = 1; |
| 870 | } |
Michal Vasko | 4893535 | 2016-03-29 11:52:36 +0200 | [diff] [blame] | 871 | if (!strncmp(id, "./", 2)) { |
| 872 | parsed += 2; |
| 873 | id += 2; |
| 874 | } |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 875 | } else { |
| 876 | if (*is_relative == -1) { |
| 877 | *is_relative = 0; |
| 878 | } |
| 879 | ++parsed; |
| 880 | ++id; |
| 881 | } |
| 882 | |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 883 | 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] | 884 | return -parsed+ret; |
| 885 | } |
| 886 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 887 | parsed += ret; |
| 888 | id += ret; |
| 889 | |
| 890 | if ((id[0] == '[') && has_predicate) { |
| 891 | *has_predicate = 1; |
| 892 | } |
| 893 | |
| 894 | return parsed; |
| 895 | } |
| 896 | |
| 897 | /** |
| 898 | * @brief Parse schema predicate (special format internally used). |
| 899 | * |
| 900 | * predicate = "[" *WSP predicate-expr *WSP "]" |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 901 | * predicate-expr = "." / identifier / key-with-value |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 902 | * key-with-value = identifier *WSP "=" *WSP |
| 903 | * ((DQUOTE string DQUOTE) / |
| 904 | * (SQUOTE string SQUOTE)) |
| 905 | * |
| 906 | * @param[in] id Identifier to use. |
| 907 | * @param[out] name Points to the list key name. |
| 908 | * @param[out] nam_len Length of \p name. |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 909 | * @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] | 910 | * @param[out] val_len Length of \p value. |
| 911 | * @param[out] has_predicate Flag to mark whether there is another predicate specified. |
| 912 | */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 913 | int |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 914 | 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] | 915 | int *has_predicate) |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 916 | { |
| 917 | const char *ptr; |
| 918 | int parsed = 0, ret; |
| 919 | char quote; |
| 920 | |
| 921 | assert(id); |
| 922 | if (name) { |
| 923 | *name = NULL; |
| 924 | } |
| 925 | if (nam_len) { |
| 926 | *nam_len = 0; |
| 927 | } |
| 928 | if (value) { |
| 929 | *value = NULL; |
| 930 | } |
| 931 | if (val_len) { |
| 932 | *val_len = 0; |
| 933 | } |
| 934 | if (has_predicate) { |
| 935 | *has_predicate = 0; |
| 936 | } |
| 937 | |
| 938 | if (id[0] != '[') { |
| 939 | return -parsed; |
| 940 | } |
| 941 | |
| 942 | ++parsed; |
| 943 | ++id; |
| 944 | |
| 945 | while (isspace(id[0])) { |
| 946 | ++parsed; |
| 947 | ++id; |
| 948 | } |
| 949 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 950 | /* identifier */ |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 951 | if (id[0] == '.') { |
| 952 | ret = 1; |
| 953 | } else if ((ret = parse_identifier(id)) < 1) { |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 954 | return -parsed + ret; |
| 955 | } |
| 956 | if (name) { |
| 957 | *name = id; |
| 958 | } |
| 959 | if (nam_len) { |
| 960 | *nam_len = ret; |
| 961 | } |
| 962 | |
| 963 | parsed += ret; |
| 964 | id += ret; |
| 965 | |
| 966 | while (isspace(id[0])) { |
| 967 | ++parsed; |
| 968 | ++id; |
| 969 | } |
| 970 | |
| 971 | /* there is value as well */ |
| 972 | if (id[0] == '=') { |
| 973 | ++parsed; |
| 974 | ++id; |
| 975 | |
| 976 | while (isspace(id[0])) { |
| 977 | ++parsed; |
| 978 | ++id; |
| 979 | } |
| 980 | |
| 981 | /* ((DQUOTE string DQUOTE) / (SQUOTE string SQUOTE)) */ |
| 982 | if ((id[0] == '\"') || (id[0] == '\'')) { |
| 983 | quote = id[0]; |
| 984 | |
| 985 | ++parsed; |
| 986 | ++id; |
| 987 | |
| 988 | if ((ptr = strchr(id, quote)) == NULL) { |
| 989 | return -parsed; |
| 990 | } |
| 991 | ret = ptr - id; |
| 992 | |
| 993 | if (value) { |
| 994 | *value = id; |
| 995 | } |
| 996 | if (val_len) { |
| 997 | *val_len = ret; |
| 998 | } |
| 999 | |
| 1000 | parsed += ret + 1; |
| 1001 | id += ret + 1; |
| 1002 | } else { |
| 1003 | return -parsed; |
| 1004 | } |
| 1005 | |
| 1006 | while (isspace(id[0])) { |
| 1007 | ++parsed; |
| 1008 | ++id; |
| 1009 | } |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1010 | } else if (value) { |
| 1011 | /* if value was expected, it's mandatory */ |
| 1012 | return -parsed; |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1013 | } |
| 1014 | |
| 1015 | if (id[0] != ']') { |
| 1016 | return -parsed; |
| 1017 | } |
| 1018 | |
| 1019 | ++parsed; |
| 1020 | ++id; |
| 1021 | |
| 1022 | if ((id[0] == '[') && has_predicate) { |
| 1023 | *has_predicate = 1; |
| 1024 | } |
| 1025 | |
| 1026 | return parsed; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 1027 | } |
| 1028 | |
| 1029 | /** |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1030 | * @brief Resolve (find) a feature definition. Logs directly. |
| 1031 | * |
| 1032 | * @param[in] feat_name Feature name to resolve. |
| 1033 | * @param[in] len Length of \p feat_name. |
| 1034 | * @param[in] node Node with the if-feature expression. |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1035 | * @param[out] feature Pointer to be set to point to the feature definition, if feature not found |
| 1036 | * (return code 1), the pointer is untouched. |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1037 | * |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1038 | * @return 0 on success, 1 on forward reference, -1 on error. |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1039 | */ |
| 1040 | static int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1041 | 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] | 1042 | { |
| 1043 | char *str; |
| 1044 | const char *mod_name, *name; |
| 1045 | int mod_name_len, nam_len, i, j; |
| 1046 | const struct lys_module *module; |
| 1047 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1048 | assert(feature); |
| 1049 | |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1050 | /* check prefix */ |
| 1051 | if ((i = parse_node_identifier(feat_name, &mod_name, &mod_name_len, &name, &nam_len)) < 1) { |
| 1052 | LOGVAL(LYE_INCHAR, LY_VLOG_NONE, NULL, feat_name[-i], &feat_name[-i]); |
| 1053 | return -1; |
| 1054 | } |
| 1055 | |
| 1056 | module = lys_get_import_module(lys_node_module(node), NULL, 0, mod_name, mod_name_len); |
| 1057 | if (!module) { |
| 1058 | /* identity refers unknown data model */ |
| 1059 | LOGVAL(LYE_INMOD_LEN, LY_VLOG_NONE, NULL, mod_name_len, mod_name); |
| 1060 | return -1; |
| 1061 | } |
| 1062 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1063 | if (module != node->module && module == lys_node_module(node)) { |
| 1064 | /* first, try to search directly in submodule where the feature was mentioned */ |
| 1065 | for (j = 0; j < node->module->features_size; j++) { |
| 1066 | if (!strncmp(name, node->module->features[j].name, nam_len) && !node->module->features[j].name[nam_len]) { |
| 1067 | /* check status */ |
| 1068 | 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] | 1069 | node->module->features[j].module, node->module->features[j].name, NULL)) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1070 | return -1; |
| 1071 | } |
| 1072 | *feature = &node->module->features[j]; |
| 1073 | return 0; |
| 1074 | } |
| 1075 | } |
| 1076 | } |
| 1077 | |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1078 | /* search in the identified module ... */ |
| 1079 | for (j = 0; j < module->features_size; j++) { |
Michal Vasko | 3def867 | 2016-07-01 11:43:09 +0200 | [diff] [blame] | 1080 | 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] | 1081 | /* check status */ |
| 1082 | 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] | 1083 | module->features[j].module, module->features[j].name, NULL)) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1084 | return -1; |
| 1085 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1086 | *feature = &module->features[j]; |
| 1087 | return 0; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1088 | } |
| 1089 | } |
| 1090 | /* ... and all its submodules */ |
| 1091 | for (i = 0; i < module->inc_size; i++) { |
| 1092 | if (!module->inc[i].submodule) { |
| 1093 | /* not yet resolved */ |
| 1094 | continue; |
| 1095 | } |
| 1096 | for (j = 0; j < module->inc[i].submodule->features_size; j++) { |
Michal Vasko | 3def867 | 2016-07-01 11:43:09 +0200 | [diff] [blame] | 1097 | if (!strncmp(name, module->inc[i].submodule->features[j].name, nam_len) |
| 1098 | && !module->inc[i].submodule->features[j].name[nam_len]) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1099 | /* check status */ |
| 1100 | if (lyp_check_status(node->flags, lys_node_module(node), node->name, |
| 1101 | module->inc[i].submodule->features[j].flags, |
| 1102 | module->inc[i].submodule->features[j].module, |
Pavol Vican | fdab9f9 | 2016-09-07 15:23:27 +0200 | [diff] [blame] | 1103 | module->inc[i].submodule->features[j].name, NULL)) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1104 | return -1; |
| 1105 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1106 | *feature = &module->inc[i].submodule->features[j]; |
| 1107 | return 0; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1108 | } |
| 1109 | } |
| 1110 | } |
| 1111 | |
| 1112 | /* not found */ |
| 1113 | str = strndup(feat_name, len); |
| 1114 | LOGVAL(LYE_INRESOLV, LY_VLOG_NONE, NULL, "feature", str); |
| 1115 | free(str); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1116 | return 1; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1117 | } |
| 1118 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1119 | /* |
| 1120 | * @return |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1121 | * - 1 if enabled |
| 1122 | * - 0 if disabled |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1123 | * - -1 if not usable by its if-feature expression |
| 1124 | */ |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1125 | static int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1126 | resolve_feature_value(const struct lys_feature *feat) |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1127 | { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1128 | int i; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1129 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1130 | for (i = 0; i < feat->iffeature_size; i++) { |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1131 | if (!resolve_iffeature(&feat->iffeature[i])) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1132 | return -1; |
| 1133 | } |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1134 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1135 | |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1136 | return feat->flags & LYS_FENABLED ? 1 : 0; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1137 | } |
| 1138 | |
| 1139 | static int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1140 | 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] | 1141 | { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1142 | uint8_t op; |
| 1143 | int rc, a, b; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1144 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1145 | op = iff_getop(expr->expr, *index_e); |
| 1146 | (*index_e)++; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1147 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1148 | switch (op) { |
| 1149 | case LYS_IFF_F: |
| 1150 | /* resolve feature */ |
| 1151 | return resolve_feature_value(expr->features[(*index_f)++]); |
| 1152 | case LYS_IFF_NOT: |
| 1153 | rc = resolve_iffeature_recursive(expr, index_e, index_f); |
| 1154 | if (rc == -1) { |
| 1155 | /* one of the referenced feature is hidden by its if-feature, |
| 1156 | * so this if-feature expression is always false */ |
| 1157 | return -1; |
| 1158 | } else { |
| 1159 | /* invert result */ |
| 1160 | return rc ? 0 : 1; |
| 1161 | } |
| 1162 | case LYS_IFF_AND: |
| 1163 | case LYS_IFF_OR: |
| 1164 | a = resolve_iffeature_recursive(expr, index_e, index_f); |
| 1165 | b = resolve_iffeature_recursive(expr, index_e, index_f); |
| 1166 | if (a == -1 || b == -1) { |
| 1167 | /* one of the referenced feature is hidden by its if-feature, |
| 1168 | * so this if-feature expression is always false */ |
| 1169 | return -1; |
| 1170 | } else if (op == LYS_IFF_AND) { |
| 1171 | return a && b; |
| 1172 | } else { /* LYS_IFF_OR */ |
| 1173 | return a || b; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1174 | } |
| 1175 | } |
| 1176 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1177 | return -1; |
| 1178 | } |
| 1179 | |
| 1180 | int |
| 1181 | resolve_iffeature(struct lys_iffeature *expr) |
| 1182 | { |
| 1183 | int rc = -1; |
| 1184 | int index_e = 0, index_f = 0; |
| 1185 | |
| 1186 | if (expr->expr) { |
| 1187 | rc = resolve_iffeature_recursive(expr, &index_e, &index_f); |
| 1188 | } |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1189 | return (rc == 1) ? 1 : 0; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1190 | } |
| 1191 | |
| 1192 | struct iff_stack { |
| 1193 | int size; |
| 1194 | int index; /* first empty item */ |
| 1195 | uint8_t *stack; |
| 1196 | }; |
| 1197 | |
| 1198 | static int |
| 1199 | iff_stack_push(struct iff_stack *stack, uint8_t value) |
| 1200 | { |
| 1201 | if (stack->index == stack->size) { |
| 1202 | stack->size += 4; |
| 1203 | stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack); |
| 1204 | if (!stack->stack) { |
| 1205 | LOGMEM; |
| 1206 | stack->size = 0; |
| 1207 | return EXIT_FAILURE; |
| 1208 | } |
| 1209 | } |
| 1210 | |
| 1211 | stack->stack[stack->index++] = value; |
| 1212 | return EXIT_SUCCESS; |
| 1213 | } |
| 1214 | |
| 1215 | static uint8_t |
| 1216 | iff_stack_pop(struct iff_stack *stack) |
| 1217 | { |
| 1218 | stack->index--; |
| 1219 | return stack->stack[stack->index]; |
| 1220 | } |
| 1221 | |
| 1222 | static void |
| 1223 | iff_stack_clean(struct iff_stack *stack) |
| 1224 | { |
| 1225 | stack->size = 0; |
| 1226 | free(stack->stack); |
| 1227 | } |
| 1228 | |
| 1229 | static void |
| 1230 | iff_setop(uint8_t *list, uint8_t op, int pos) |
| 1231 | { |
| 1232 | uint8_t *item; |
| 1233 | uint8_t mask = 3; |
| 1234 | |
| 1235 | assert(pos >= 0); |
| 1236 | assert(op <= 3); /* max 2 bits */ |
| 1237 | |
| 1238 | item = &list[pos / 4]; |
| 1239 | mask = mask << 2 * (pos % 4); |
| 1240 | *item = (*item) & ~mask; |
| 1241 | *item = (*item) | (op << 2 * (pos % 4)); |
| 1242 | } |
| 1243 | |
| 1244 | uint8_t |
| 1245 | iff_getop(uint8_t *list, int pos) |
| 1246 | { |
| 1247 | uint8_t *item; |
| 1248 | uint8_t mask = 3, result; |
| 1249 | |
| 1250 | assert(pos >= 0); |
| 1251 | |
| 1252 | item = &list[pos / 4]; |
| 1253 | result = (*item) & (mask << 2 * (pos % 4)); |
| 1254 | return result >> 2 * (pos % 4); |
| 1255 | } |
| 1256 | |
| 1257 | #define LYS_IFF_LP 0x04 /* ( */ |
| 1258 | #define LYS_IFF_RP 0x08 /* ) */ |
| 1259 | |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1260 | /* internal structure for passing data for UNRES_IFFEAT */ |
| 1261 | struct unres_iffeat_data { |
| 1262 | struct lys_node *node; |
| 1263 | const char *fname; |
| 1264 | }; |
| 1265 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1266 | void |
| 1267 | resolve_iffeature_getsizes(struct lys_iffeature *iffeat, unsigned int *expr_size, unsigned int *feat_size) |
| 1268 | { |
| 1269 | unsigned int e = 0, f = 0, r = 0; |
| 1270 | uint8_t op; |
| 1271 | |
| 1272 | assert(iffeat); |
| 1273 | |
| 1274 | if (!iffeat->expr) { |
| 1275 | goto result; |
| 1276 | } |
| 1277 | |
| 1278 | do { |
| 1279 | op = iff_getop(iffeat->expr, e++); |
| 1280 | switch (op) { |
| 1281 | case LYS_IFF_NOT: |
| 1282 | if (!r) { |
| 1283 | r += 1; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1284 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1285 | break; |
| 1286 | case LYS_IFF_AND: |
| 1287 | case LYS_IFF_OR: |
| 1288 | if (!r) { |
| 1289 | r += 2; |
| 1290 | } else { |
| 1291 | r += 1; |
| 1292 | } |
| 1293 | break; |
| 1294 | case LYS_IFF_F: |
| 1295 | f++; |
| 1296 | if (r) { |
| 1297 | r--; |
| 1298 | } |
| 1299 | break; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1300 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1301 | } while(r); |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1302 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1303 | result: |
| 1304 | if (expr_size) { |
| 1305 | *expr_size = e; |
| 1306 | } |
| 1307 | if (feat_size) { |
| 1308 | *feat_size = f; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1309 | } |
| 1310 | } |
| 1311 | |
| 1312 | int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1313 | resolve_iffeature_compile(struct lys_iffeature *iffeat_expr, const char *value, struct lys_node *node, |
| 1314 | struct unres_schema *unres) |
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 | const char *c = value; |
| 1317 | int r, rc = EXIT_FAILURE; |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1318 | int i, j, last_not, checkversion = 0; |
| 1319 | unsigned int f_size = 0, expr_size = 0, f_exp = 1; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1320 | uint8_t op; |
| 1321 | struct iff_stack stack = {0, 0, NULL}; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1322 | struct unres_iffeat_data *iff_data; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1323 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1324 | assert(c); |
| 1325 | |
| 1326 | if (isspace(c[0])) { |
| 1327 | LOGVAL(LYE_INCHAR, LY_VLOG_NONE, NULL, c[0], c); |
| 1328 | return EXIT_FAILURE; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1329 | } |
| 1330 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1331 | /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */ |
| 1332 | for (i = j = last_not = 0; c[i]; i++) { |
| 1333 | if (c[i] == '(') { |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1334 | checkversion = 1; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1335 | j++; |
| 1336 | continue; |
| 1337 | } else if (c[i] == ')') { |
| 1338 | j--; |
| 1339 | continue; |
| 1340 | } else if (isspace(c[i])) { |
| 1341 | continue; |
| 1342 | } |
| 1343 | |
| 1344 | if (!strncmp(&c[i], "not", r = 3) || !strncmp(&c[i], "and", r = 3) || !strncmp(&c[i], "or", r = 2)) { |
| 1345 | if (c[i + r] == '\0') { |
Radek Krejci | a98da3f | 2016-07-27 14:05:22 +0200 | [diff] [blame] | 1346 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1347 | return EXIT_FAILURE; |
| 1348 | } else if (!isspace(c[i + r])) { |
| 1349 | /* feature name starting with the not/and/or */ |
| 1350 | last_not = 0; |
| 1351 | f_size++; |
| 1352 | } else if (c[i] == 'n') { /* not operation */ |
| 1353 | if (last_not) { |
| 1354 | /* double not */ |
| 1355 | expr_size = expr_size - 2; |
| 1356 | last_not = 0; |
| 1357 | } else { |
| 1358 | last_not = 1; |
| 1359 | } |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1360 | } else { /* and, or */ |
| 1361 | f_exp++; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1362 | /* not a not operation */ |
| 1363 | last_not = 0; |
| 1364 | } |
| 1365 | i += r; |
| 1366 | } else { |
| 1367 | f_size++; |
| 1368 | last_not = 0; |
| 1369 | } |
| 1370 | expr_size++; |
| 1371 | |
| 1372 | while (!isspace(c[i])) { |
| 1373 | if (!c[i] || c[i] == ')') { |
| 1374 | i--; |
| 1375 | break; |
| 1376 | } |
| 1377 | i++; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1378 | } |
| 1379 | } |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1380 | if (j || f_exp != f_size) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1381 | /* not matching count of ( and ) */ |
| 1382 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
| 1383 | return EXIT_FAILURE; |
| 1384 | } |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1385 | |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1386 | if (checkversion || expr_size > 1) { |
| 1387 | /* check that we have 1.1 module */ |
| 1388 | if (node->module->version != 2) { |
| 1389 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
| 1390 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "YANG 1.1 if-feature expression found in 1.0 module."); |
| 1391 | return EXIT_FAILURE; |
| 1392 | } |
| 1393 | } |
| 1394 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1395 | /* allocate the memory */ |
| 1396 | 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] | 1397 | iffeat_expr->features = calloc(f_size, sizeof *iffeat_expr->features); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1398 | stack.size = expr_size; |
| 1399 | stack.stack = malloc(expr_size * sizeof *stack.stack); |
| 1400 | if (!stack.stack || !iffeat_expr->expr || !iffeat_expr->features) { |
| 1401 | LOGMEM; |
| 1402 | goto error; |
| 1403 | } |
| 1404 | f_size--; expr_size--; /* used as indexes from now */ |
| 1405 | |
| 1406 | for (i--; i >= 0; i--) { |
| 1407 | if (c[i] == ')') { |
| 1408 | /* push it on stack */ |
| 1409 | iff_stack_push(&stack, LYS_IFF_RP); |
| 1410 | continue; |
| 1411 | } else if (c[i] == '(') { |
| 1412 | /* pop from the stack into result all operators until ) */ |
| 1413 | while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) { |
| 1414 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1415 | } |
| 1416 | continue; |
| 1417 | } else if (isspace(c[i])) { |
| 1418 | continue; |
| 1419 | } |
| 1420 | |
| 1421 | /* end operator or operand -> find beginning and get what is it */ |
| 1422 | j = i + 1; |
| 1423 | while (i >= 0 && !isspace(c[i]) && c[i] != '(') { |
| 1424 | i--; |
| 1425 | } |
| 1426 | i++; /* get back by one step */ |
| 1427 | |
| 1428 | if (!strncmp(&c[i], "not ", 4)) { |
| 1429 | if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) { |
| 1430 | /* double not */ |
| 1431 | iff_stack_pop(&stack); |
| 1432 | } else { |
| 1433 | /* not has the highest priority, so do not pop from the stack |
| 1434 | * as in case of AND and OR */ |
| 1435 | iff_stack_push(&stack, LYS_IFF_NOT); |
| 1436 | } |
| 1437 | } else if (!strncmp(&c[i], "and ", 4)) { |
| 1438 | /* as for OR - pop from the stack all operators with the same or higher |
| 1439 | * priority and store them to the result, then push the AND to the stack */ |
| 1440 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) { |
| 1441 | op = iff_stack_pop(&stack); |
| 1442 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1443 | } |
| 1444 | iff_stack_push(&stack, LYS_IFF_AND); |
| 1445 | } else if (!strncmp(&c[i], "or ", 3)) { |
| 1446 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) { |
| 1447 | op = iff_stack_pop(&stack); |
| 1448 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1449 | } |
| 1450 | iff_stack_push(&stack, LYS_IFF_OR); |
| 1451 | } else { |
| 1452 | /* feature name, length is j - i */ |
| 1453 | |
| 1454 | /* add it to the result */ |
| 1455 | iff_setop(iffeat_expr->expr, LYS_IFF_F, expr_size--); |
| 1456 | |
| 1457 | /* now get the link to the feature definition. Since it can be |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1458 | * forward referenced, we have to keep the feature name in auxiliary |
| 1459 | * structure passed into unres */ |
| 1460 | iff_data = malloc(sizeof *iff_data); |
| 1461 | iff_data->node = node; |
| 1462 | iff_data->fname = lydict_insert(node->module->ctx, &c[i], j - i); |
| 1463 | r = unres_schema_add_node(node->module, unres, &iffeat_expr->features[f_size], UNRES_IFFEAT, |
| 1464 | (struct lys_node *)iff_data); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1465 | f_size--; |
| 1466 | |
| 1467 | if (r == -1) { |
Pavol Vican | 4d08451 | 2016-09-29 16:38:12 +0200 | [diff] [blame] | 1468 | free(iff_data); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1469 | goto error; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1470 | } |
| 1471 | } |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1472 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1473 | while (stack.index) { |
| 1474 | op = iff_stack_pop(&stack); |
| 1475 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1476 | } |
| 1477 | |
| 1478 | if (++expr_size || ++f_size) { |
| 1479 | /* not all expected operators and operands found */ |
| 1480 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
| 1481 | rc = EXIT_FAILURE; |
| 1482 | } else { |
| 1483 | rc = EXIT_SUCCESS; |
| 1484 | } |
| 1485 | |
| 1486 | error: |
| 1487 | /* cleanup */ |
| 1488 | iff_stack_clean(&stack); |
| 1489 | |
| 1490 | return rc; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1491 | } |
| 1492 | |
| 1493 | /** |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1494 | * @brief Resolve (find) a data node based on a schema-nodeid. |
| 1495 | * |
| 1496 | * Used for resolving unique statements - so id is expected to be relative and local (without reference to a different |
| 1497 | * module). |
| 1498 | * |
| 1499 | */ |
| 1500 | struct lyd_node * |
| 1501 | resolve_data_descendant_schema_nodeid(const char *nodeid, struct lyd_node *start) |
| 1502 | { |
| 1503 | char *str, *token, *p; |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1504 | struct lyd_node *result = NULL, *iter; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1505 | const struct lys_node *schema = NULL; |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1506 | int shorthand = 0; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1507 | |
| 1508 | assert(nodeid && start); |
| 1509 | |
| 1510 | if (nodeid[0] == '/') { |
| 1511 | return NULL; |
| 1512 | } |
| 1513 | |
| 1514 | str = p = strdup(nodeid); |
| 1515 | if (!str) { |
| 1516 | LOGMEM; |
| 1517 | return NULL; |
| 1518 | } |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1519 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1520 | while (p) { |
| 1521 | token = p; |
| 1522 | p = strchr(p, '/'); |
| 1523 | if (p) { |
| 1524 | *p = '\0'; |
| 1525 | p++; |
| 1526 | } |
| 1527 | |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1528 | if (p) { |
| 1529 | /* inner node */ |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1530 | if (resolve_descendant_schema_nodeid(token, schema ? schema->child : start->schema, |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 1531 | LYS_CONTAINER | LYS_CHOICE | LYS_CASE | LYS_LEAF, 0, 0, &schema) |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1532 | || !schema) { |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1533 | result = NULL; |
| 1534 | break; |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1535 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1536 | |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1537 | if (schema->nodetype & (LYS_CHOICE | LYS_CASE)) { |
| 1538 | continue; |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 1539 | } else if (lys_parent(schema)->nodetype == LYS_CHOICE) { |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1540 | /* shorthand case */ |
| 1541 | if (!shorthand) { |
| 1542 | shorthand = 1; |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 1543 | schema = lys_parent(schema); |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1544 | continue; |
| 1545 | } else { |
| 1546 | shorthand = 0; |
| 1547 | if (schema->nodetype == LYS_LEAF) { |
| 1548 | /* should not be here, since we have leaf, which is not a shorthand nor final node */ |
| 1549 | result = NULL; |
| 1550 | break; |
| 1551 | } |
| 1552 | } |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1553 | } |
| 1554 | } else { |
| 1555 | /* final node */ |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 1556 | if (resolve_descendant_schema_nodeid(token, schema ? schema->child : start->schema, LYS_LEAF, |
| 1557 | shorthand ? 0 : 1, 0, &schema) |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1558 | || !schema) { |
| 1559 | result = NULL; |
| 1560 | break; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1561 | } |
| 1562 | } |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1563 | LY_TREE_FOR(result ? result->child : start, iter) { |
| 1564 | if (iter->schema == schema) { |
| 1565 | /* move in data tree according to returned schema */ |
| 1566 | result = iter; |
| 1567 | break; |
| 1568 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1569 | } |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1570 | if (!iter) { |
| 1571 | /* instance not found */ |
| 1572 | result = NULL; |
| 1573 | break; |
| 1574 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1575 | } |
| 1576 | free(str); |
| 1577 | |
| 1578 | return result; |
| 1579 | } |
| 1580 | |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1581 | /* |
| 1582 | * 0 - ok (done) |
| 1583 | * 1 - continue |
| 1584 | * 2 - break |
| 1585 | * -1 - error |
| 1586 | */ |
| 1587 | static int |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1588 | 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] | 1589 | 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] | 1590 | int implemented_mod, const struct lys_node **start) |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1591 | { |
| 1592 | const struct lys_module *prefix_mod; |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1593 | int sh = 0; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1594 | |
| 1595 | /* module check */ |
| 1596 | 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] | 1597 | if (implemented_mod) { |
| 1598 | prefix_mod = lys_get_implemented_module(prefix_mod); |
| 1599 | } |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1600 | if (!prefix_mod) { |
| 1601 | return -1; |
| 1602 | } |
| 1603 | if (prefix_mod != lys_node_module(sibling)) { |
| 1604 | return 1; |
| 1605 | } |
| 1606 | |
| 1607 | /* check for shorthand cases - then 'start' does not change */ |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 1608 | 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] | 1609 | if (*shorthand != -1) { |
| 1610 | *shorthand = *shorthand ? 0 : 1; |
| 1611 | } |
| 1612 | sh = 1; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1613 | } |
| 1614 | |
| 1615 | /* the result node? */ |
| 1616 | if (!id[0]) { |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1617 | if (*shorthand == 1) { |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1618 | return 1; |
| 1619 | } |
| 1620 | return 0; |
| 1621 | } |
| 1622 | |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1623 | if (!sh) { |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1624 | /* move down the tree, if possible */ |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 1625 | if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1626 | return -1; |
| 1627 | } |
| 1628 | *start = sibling->child; |
| 1629 | } |
| 1630 | |
| 1631 | return 2; |
| 1632 | } |
| 1633 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1634 | /* start - relative, module - absolute, -1 error, EXIT_SUCCESS ok (but ret can still be NULL), >0 unexpected char on ret - 1 */ |
| 1635 | int |
| 1636 | resolve_augment_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_module *module, |
| 1637 | const struct lys_node **ret) |
| 1638 | { |
| 1639 | const char *name, *mod_name, *id; |
| 1640 | const struct lys_node *sibling; |
| 1641 | int r, nam_len, mod_name_len, is_relative = -1; |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1642 | int8_t shorthand = 0; |
Radek Krejci | 0fa54e9 | 2016-09-14 14:01:05 +0200 | [diff] [blame] | 1643 | int implemented_search = 0; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1644 | /* 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] | 1645 | const struct lys_module *start_mod; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1646 | |
| 1647 | assert(nodeid && (start || module) && !(start && module) && ret); |
| 1648 | |
| 1649 | id = nodeid; |
| 1650 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1651 | 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] | 1652 | return ((id - nodeid) - r) + 1; |
| 1653 | } |
| 1654 | id += r; |
| 1655 | |
| 1656 | if ((is_relative && !start) || (!is_relative && !module)) { |
| 1657 | return -1; |
| 1658 | } |
| 1659 | |
| 1660 | /* descendant-schema-nodeid */ |
| 1661 | if (is_relative) { |
Michal Vasko | 4c06fd8 | 2016-02-17 13:43:38 +0100 | [diff] [blame] | 1662 | module = start_mod = start->module; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1663 | |
| 1664 | /* absolute-schema-nodeid */ |
| 1665 | } else { |
| 1666 | 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] | 1667 | if (start_mod != lys_main_module(module)) { |
| 1668 | /* if the submodule augments the mainmodule (or in general a module augments |
| 1669 | * itself, we don't want to search for the implemented module but augments |
| 1670 | * the module anyway. But when augmenting another module, we need the implemented |
| 1671 | * revision of the module if any */ |
| 1672 | start_mod = lys_get_implemented_module(start_mod); |
| 1673 | implemented_search = 1; |
| 1674 | } |
Michal Vasko | e290563 | 2016-02-11 15:42:24 +0100 | [diff] [blame] | 1675 | if (!start_mod) { |
| 1676 | return -1; |
| 1677 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1678 | start = start_mod->data; |
| 1679 | } |
| 1680 | |
| 1681 | while (1) { |
| 1682 | sibling = NULL; |
| 1683 | while ((sibling = lys_getnext(sibling, lys_parent(start), start_mod, |
| 1684 | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT))) { |
| 1685 | /* name match */ |
Michal Vasko | 0a1aaa4 | 2016-04-19 09:48:25 +0200 | [diff] [blame] | 1686 | 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] | 1687 | r = schema_nodeid_siblingcheck(sibling, &shorthand, id, module, mod_name, mod_name_len, |
| 1688 | implemented_search, &start); |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1689 | if (r == 0) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1690 | *ret = sibling; |
| 1691 | return EXIT_SUCCESS; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1692 | } else if (r == 1) { |
| 1693 | continue; |
| 1694 | } else if (r == 2) { |
| 1695 | break; |
| 1696 | } else { |
| 1697 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1698 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1699 | } |
| 1700 | } |
| 1701 | |
| 1702 | /* no match */ |
| 1703 | if (!sibling) { |
Michal Vasko | a426fef | 2016-03-07 10:47:31 +0100 | [diff] [blame] | 1704 | *ret = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1705 | return EXIT_SUCCESS; |
| 1706 | } |
| 1707 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1708 | 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] | 1709 | return ((id - nodeid) - r) + 1; |
| 1710 | } |
| 1711 | id += r; |
| 1712 | } |
| 1713 | |
| 1714 | /* cannot get here */ |
| 1715 | LOGINT; |
| 1716 | return -1; |
| 1717 | } |
| 1718 | |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 1719 | /* unique, refine, |
| 1720 | * >0 - unexpected char on position (ret - 1), |
| 1721 | * 0 - ok (but ret can still be NULL), |
| 1722 | * -1 - error, |
| 1723 | * -2 - violated no_innerlist */ |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1724 | int |
| 1725 | 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] | 1726 | int check_shorthand, int no_innerlist, const struct lys_node **ret) |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1727 | { |
| 1728 | const char *name, *mod_name, *id; |
| 1729 | const struct lys_node *sibling; |
| 1730 | int r, nam_len, mod_name_len, is_relative = -1; |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1731 | int8_t shorthand = check_shorthand ? 0 : -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1732 | /* 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] | 1733 | const struct lys_module *module; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1734 | |
| 1735 | assert(nodeid && start && ret); |
| 1736 | assert(!(ret_nodetype & (LYS_USES | LYS_AUGMENT)) && ((ret_nodetype == LYS_GROUPING) || !(ret_nodetype & LYS_GROUPING))); |
| 1737 | |
| 1738 | id = nodeid; |
| 1739 | module = start->module; |
| 1740 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1741 | 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] | 1742 | return ((id - nodeid) - r) + 1; |
| 1743 | } |
| 1744 | id += r; |
| 1745 | |
| 1746 | if (!is_relative) { |
| 1747 | return -1; |
| 1748 | } |
| 1749 | |
| 1750 | while (1) { |
| 1751 | sibling = NULL; |
| 1752 | while ((sibling = lys_getnext(sibling, lys_parent(start), module, |
| 1753 | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE))) { |
| 1754 | /* name match */ |
| 1755 | 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] | 1756 | 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] | 1757 | if (r == 0) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1758 | if (!(sibling->nodetype & ret_nodetype)) { |
| 1759 | /* wrong node type, too bad */ |
| 1760 | continue; |
| 1761 | } |
| 1762 | *ret = sibling; |
| 1763 | return EXIT_SUCCESS; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1764 | } else if (r == 1) { |
| 1765 | continue; |
| 1766 | } else if (r == 2) { |
| 1767 | break; |
| 1768 | } else { |
| 1769 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1770 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1771 | } |
| 1772 | } |
| 1773 | |
| 1774 | /* no match */ |
| 1775 | if (!sibling) { |
Michal Vasko | a426fef | 2016-03-07 10:47:31 +0100 | [diff] [blame] | 1776 | *ret = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1777 | return EXIT_SUCCESS; |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 1778 | } else if (no_innerlist && sibling->nodetype == LYS_LIST) { |
| 1779 | *ret = NULL; |
| 1780 | return -2; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1781 | } |
| 1782 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1783 | 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] | 1784 | return ((id - nodeid) - r) + 1; |
| 1785 | } |
| 1786 | id += r; |
| 1787 | } |
| 1788 | |
| 1789 | /* cannot get here */ |
| 1790 | LOGINT; |
| 1791 | return -1; |
| 1792 | } |
| 1793 | |
| 1794 | /* choice default */ |
| 1795 | int |
| 1796 | resolve_choice_default_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_node **ret) |
| 1797 | { |
| 1798 | /* cannot actually be a path */ |
| 1799 | if (strchr(nodeid, '/')) { |
| 1800 | return -1; |
| 1801 | } |
| 1802 | |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 1803 | 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] | 1804 | } |
| 1805 | |
| 1806 | /* uses, -1 error, EXIT_SUCCESS ok (but ret can still be NULL), >0 unexpected char on ret - 1 */ |
| 1807 | static int |
| 1808 | resolve_uses_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_node_grp **ret) |
| 1809 | { |
| 1810 | const struct lys_module *module; |
| 1811 | const char *mod_prefix, *name; |
| 1812 | int i, mod_prefix_len, nam_len; |
| 1813 | |
| 1814 | /* parse the identifier, it must be parsed on one call */ |
| 1815 | if (((i = parse_node_identifier(nodeid, &mod_prefix, &mod_prefix_len, &name, &nam_len)) < 1) || nodeid[i]) { |
| 1816 | return -i + 1; |
| 1817 | } |
| 1818 | |
| 1819 | module = lys_get_import_module(start->module, mod_prefix, mod_prefix_len, NULL, 0); |
| 1820 | if (!module) { |
| 1821 | return -1; |
| 1822 | } |
| 1823 | if (module != start->module) { |
| 1824 | start = module->data; |
| 1825 | } |
| 1826 | |
| 1827 | *ret = lys_find_grouping_up(name, (struct lys_node *)start); |
| 1828 | |
| 1829 | return EXIT_SUCCESS; |
| 1830 | } |
| 1831 | |
| 1832 | int |
| 1833 | resolve_absolute_schema_nodeid(const char *nodeid, const struct lys_module *module, int ret_nodetype, |
| 1834 | const struct lys_node **ret) |
| 1835 | { |
| 1836 | const char *name, *mod_name, *id; |
| 1837 | const struct lys_node *sibling, *start; |
| 1838 | int r, nam_len, mod_name_len, is_relative = -1; |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1839 | int8_t shorthand = 0; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1840 | const struct lys_module *abs_start_mod; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1841 | |
| 1842 | assert(nodeid && module && ret); |
| 1843 | assert(!(ret_nodetype & (LYS_USES | LYS_AUGMENT)) && ((ret_nodetype == LYS_GROUPING) || !(ret_nodetype & LYS_GROUPING))); |
| 1844 | |
| 1845 | id = nodeid; |
| 1846 | start = module->data; |
| 1847 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1848 | 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] | 1849 | return ((id - nodeid) - r) + 1; |
| 1850 | } |
| 1851 | id += r; |
| 1852 | |
| 1853 | if (is_relative) { |
| 1854 | return -1; |
| 1855 | } |
| 1856 | |
| 1857 | 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] | 1858 | if (!abs_start_mod) { |
| 1859 | return -1; |
| 1860 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1861 | |
| 1862 | while (1) { |
| 1863 | sibling = NULL; |
| 1864 | while ((sibling = lys_getnext(sibling, lys_parent(start), abs_start_mod, LYS_GETNEXT_WITHCHOICE |
| 1865 | | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT | LYS_GETNEXT_WITHGROUPING))) { |
| 1866 | /* name match */ |
Michal Vasko | 0a1aaa4 | 2016-04-19 09:48:25 +0200 | [diff] [blame] | 1867 | 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] | 1868 | 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] | 1869 | if (r == 0) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1870 | if (!(sibling->nodetype & ret_nodetype)) { |
| 1871 | /* wrong node type, too bad */ |
| 1872 | continue; |
| 1873 | } |
| 1874 | *ret = sibling; |
| 1875 | return EXIT_SUCCESS; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1876 | } else if (r == 1) { |
| 1877 | continue; |
| 1878 | } else if (r == 2) { |
| 1879 | break; |
| 1880 | } else { |
| 1881 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1882 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1883 | } |
| 1884 | } |
| 1885 | |
| 1886 | /* no match */ |
| 1887 | if (!sibling) { |
Michal Vasko | a426fef | 2016-03-07 10:47:31 +0100 | [diff] [blame] | 1888 | *ret = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1889 | return EXIT_SUCCESS; |
| 1890 | } |
| 1891 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1892 | 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] | 1893 | return ((id - nodeid) - r) + 1; |
| 1894 | } |
| 1895 | id += r; |
| 1896 | } |
| 1897 | |
| 1898 | /* cannot get here */ |
| 1899 | LOGINT; |
| 1900 | return -1; |
| 1901 | } |
| 1902 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1903 | static int |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1904 | 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] | 1905 | { |
| 1906 | const char *name; |
| 1907 | int nam_len, has_predicate, i; |
| 1908 | |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 1909 | if (((i = parse_schema_json_predicate(predicate, &name, &nam_len, NULL, NULL, &has_predicate)) < 1) |
| 1910 | || !strncmp(name, ".", nam_len)) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 1911 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-i], &predicate[-i]); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1912 | return -1; |
| 1913 | } |
| 1914 | |
| 1915 | predicate += i; |
| 1916 | *parsed += i; |
| 1917 | |
| 1918 | for (i = 0; i < list->keys_size; ++i) { |
| 1919 | if (!strncmp(list->keys[i]->name, name, nam_len) && !list->keys[i]->name[nam_len]) { |
| 1920 | break; |
| 1921 | } |
| 1922 | } |
| 1923 | |
| 1924 | if (i == list->keys_size) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 1925 | LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1926 | return -1; |
| 1927 | } |
| 1928 | |
| 1929 | /* more predicates? */ |
| 1930 | if (has_predicate) { |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1931 | return resolve_json_schema_list_predicate(predicate, list, parsed); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1932 | } |
| 1933 | |
| 1934 | return 0; |
| 1935 | } |
| 1936 | |
Michal Vasko | 8d26e5c | 2016-09-08 10:03:49 +0200 | [diff] [blame] | 1937 | /* cannot return LYS_GROUPING, LYS_AUGMENT, LYS_USES, logs directly */ |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1938 | const struct lys_node * |
Michal Vasko | 8d26e5c | 2016-09-08 10:03:49 +0200 | [diff] [blame] | 1939 | 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] | 1940 | { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 1941 | char *module_name = ly_buf(), *buf_backup = NULL, *str; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1942 | const char *name, *mod_name, *id; |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1943 | const struct lys_node *sibling; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1944 | 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] | 1945 | /* 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] | 1946 | const struct lys_module *prefix_mod, *module, *prev_mod; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1947 | |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1948 | assert(nodeid && (ctx || start)); |
| 1949 | if (!ctx) { |
| 1950 | ctx = start->module->ctx; |
| 1951 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1952 | |
| 1953 | id = nodeid; |
| 1954 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1955 | 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] | 1956 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1957 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1958 | } |
| 1959 | id += r; |
| 1960 | |
| 1961 | if (is_relative) { |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1962 | assert(start); |
| 1963 | start = start->child; |
| 1964 | if (!start) { |
| 1965 | /* no descendants, fail for sure */ |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 1966 | str = strndup(nodeid, (name + nam_len) - nodeid); |
| 1967 | LOGVAL(LYE_PATH_INNODE, LY_VLOG_STR, str); |
| 1968 | free(str); |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1969 | return NULL; |
| 1970 | } |
| 1971 | module = start->module; |
| 1972 | } else { |
| 1973 | if (!mod_name) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 1974 | str = strndup(nodeid, (name + nam_len) - nodeid); |
| 1975 | LOGVAL(LYE_PATH_MISSMOD, LY_VLOG_STR, nodeid); |
| 1976 | free(str); |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1977 | return NULL; |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 1978 | } else if (mod_name_len > LY_BUF_SIZE - 1) { |
| 1979 | LOGINT; |
| 1980 | return NULL; |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1981 | } |
| 1982 | |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 1983 | if (ly_buf_used && module_name[0]) { |
| 1984 | buf_backup = strndup(module_name, LY_BUF_SIZE - 1); |
| 1985 | } |
| 1986 | ly_buf_used++; |
| 1987 | |
Michal Vasko | df3f1ab | 2016-04-01 15:07:22 +0200 | [diff] [blame] | 1988 | memmove(module_name, mod_name, mod_name_len); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 1989 | module_name[mod_name_len] = '\0'; |
| 1990 | module = ly_ctx_get_module(ctx, module_name, NULL); |
| 1991 | |
| 1992 | if (buf_backup) { |
| 1993 | /* return previous internal buffer content */ |
| 1994 | strcpy(module_name, buf_backup); |
| 1995 | free(buf_backup); |
| 1996 | buf_backup = NULL; |
| 1997 | } |
| 1998 | ly_buf_used--; |
| 1999 | |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2000 | if (!module) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2001 | str = strndup(nodeid, (mod_name + mod_name_len) - nodeid); |
| 2002 | LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str); |
| 2003 | free(str); |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2004 | return NULL; |
| 2005 | } |
| 2006 | start = module->data; |
| 2007 | |
| 2008 | /* now it's as if there was no module name */ |
| 2009 | mod_name = NULL; |
| 2010 | mod_name_len = 0; |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2011 | } |
| 2012 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2013 | prev_mod = module; |
| 2014 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2015 | while (1) { |
| 2016 | sibling = NULL; |
Michal Vasko | 8d26e5c | 2016-09-08 10:03:49 +0200 | [diff] [blame] | 2017 | while ((sibling = lys_getnext(sibling, lys_parent(start), module, |
| 2018 | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT))) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2019 | /* name match */ |
Michal Vasko | 0a1aaa4 | 2016-04-19 09:48:25 +0200 | [diff] [blame] | 2020 | 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] | 2021 | /* module check */ |
| 2022 | if (mod_name) { |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2023 | if (mod_name_len > LY_BUF_SIZE - 1) { |
Michal Vasko | 8757e7c | 2016-03-15 10:41:30 +0100 | [diff] [blame] | 2024 | LOGINT; |
| 2025 | return NULL; |
| 2026 | } |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2027 | |
| 2028 | if (ly_buf_used && module_name[0]) { |
| 2029 | buf_backup = strndup(module_name, LY_BUF_SIZE - 1); |
| 2030 | } |
| 2031 | ly_buf_used++; |
| 2032 | |
Michal Vasko | df3f1ab | 2016-04-01 15:07:22 +0200 | [diff] [blame] | 2033 | memmove(module_name, mod_name, mod_name_len); |
Michal Vasko | 8757e7c | 2016-03-15 10:41:30 +0100 | [diff] [blame] | 2034 | module_name[mod_name_len] = '\0'; |
| 2035 | /* will also find an augment module */ |
| 2036 | prefix_mod = ly_ctx_get_module(ctx, module_name, NULL); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2037 | |
| 2038 | if (buf_backup) { |
| 2039 | /* return previous internal buffer content */ |
Michal Vasko | 888a129 | 2016-04-05 12:08:54 +0200 | [diff] [blame] | 2040 | strncpy(module_name, buf_backup, LY_BUF_SIZE - 1); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2041 | free(buf_backup); |
| 2042 | buf_backup = NULL; |
| 2043 | } |
| 2044 | ly_buf_used--; |
| 2045 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2046 | if (!prefix_mod) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2047 | str = strndup(nodeid, (mod_name + mod_name_len) - nodeid); |
| 2048 | LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str); |
| 2049 | free(str); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2050 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2051 | } |
| 2052 | } else { |
| 2053 | prefix_mod = prev_mod; |
| 2054 | } |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 2055 | if (prefix_mod != lys_node_module(sibling)) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2056 | continue; |
| 2057 | } |
| 2058 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2059 | /* do we have some predicates on it? */ |
| 2060 | if (has_predicate) { |
| 2061 | r = 0; |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 2062 | if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 2063 | if ((r = parse_schema_json_predicate(id, NULL, NULL, NULL, NULL, &has_predicate)) < 1) { |
| 2064 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
| 2065 | return NULL; |
| 2066 | } |
| 2067 | } else if (sibling->nodetype == LYS_LIST) { |
| 2068 | if (resolve_json_schema_list_predicate(id, (const struct lys_node_list *)sibling, &r)) { |
| 2069 | return NULL; |
| 2070 | } |
| 2071 | } else { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2072 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2073 | return NULL; |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2074 | } |
| 2075 | id += r; |
| 2076 | } |
| 2077 | |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 2078 | /* check for shorthand cases - then 'start' does not change */ |
Michal Vasko | 8d26e5c | 2016-09-08 10:03:49 +0200 | [diff] [blame] | 2079 | 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] | 2080 | shorthand = ~shorthand; |
| 2081 | } |
| 2082 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2083 | /* the result node? */ |
| 2084 | if (!id[0]) { |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 2085 | if (shorthand) { |
| 2086 | /* wrong path for shorthand */ |
Michal Vasko | 025e045 | 2016-05-17 16:14:20 +0200 | [diff] [blame] | 2087 | str = strndup(nodeid, (name + nam_len) - nodeid); |
| 2088 | LOGVAL(LYE_PATH_INNODE, LY_VLOG_STR, str); |
Michal Vasko | 3c0f9f5 | 2016-05-17 16:38:10 +0200 | [diff] [blame] | 2089 | 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] | 2090 | free(str); |
Michal Vasko | 025e045 | 2016-05-17 16:14:20 +0200 | [diff] [blame] | 2091 | return NULL; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 2092 | } |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2093 | return sibling; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2094 | } |
| 2095 | |
Michal Vasko | 8d26e5c | 2016-09-08 10:03:49 +0200 | [diff] [blame] | 2096 | if (!shorthand) { |
Michal Vasko | 7dc71d0 | 2016-03-15 10:42:28 +0100 | [diff] [blame] | 2097 | /* move down the tree, if possible */ |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 2098 | if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2099 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
Michal Vasko | 7dc71d0 | 2016-03-15 10:42:28 +0100 | [diff] [blame] | 2100 | return NULL; |
| 2101 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2102 | start = sibling->child; |
| 2103 | } |
| 2104 | |
| 2105 | /* update prev mod */ |
| 2106 | prev_mod = start->module; |
| 2107 | break; |
| 2108 | } |
| 2109 | } |
| 2110 | |
| 2111 | /* no match */ |
| 2112 | if (!sibling) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2113 | str = strndup(nodeid, (name + nam_len) - nodeid); |
| 2114 | LOGVAL(LYE_PATH_INNODE, LY_VLOG_STR, str); |
| 2115 | free(str); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2116 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2117 | } |
| 2118 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2119 | 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] | 2120 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2121 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2122 | } |
| 2123 | id += r; |
| 2124 | } |
| 2125 | |
| 2126 | /* cannot get here */ |
| 2127 | LOGINT; |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2128 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2129 | } |
| 2130 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2131 | static int |
| 2132 | resolve_partial_json_data_list_predicate(const char *predicate, const char *node_name, struct lyd_node *node, int *parsed) |
| 2133 | { |
| 2134 | const char *name, *value; |
| 2135 | int nam_len, val_len, has_predicate = 1, r; |
| 2136 | uint16_t i; |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2137 | struct lyd_node_leaf_list *key; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2138 | |
Radek Krejci | 61a86c6 | 2016-03-24 11:06:44 +0100 | [diff] [blame] | 2139 | assert(node); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2140 | assert(node->schema->nodetype == LYS_LIST); |
| 2141 | |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2142 | key = (struct lyd_node_leaf_list *)node->child; |
| 2143 | for (i = 0; i < ((struct lys_node_list *)node->schema)->keys_size; ++i) { |
| 2144 | if (!key) { |
| 2145 | /* invalid data */ |
| 2146 | LOGINT; |
| 2147 | return -1; |
| 2148 | } |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2149 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2150 | if (!has_predicate) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2151 | LOGVAL(LYE_PATH_MISSKEY, LY_VLOG_NONE, NULL, node_name); |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2152 | return -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2153 | } |
| 2154 | |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 2155 | if (((r = parse_schema_json_predicate(predicate, &name, &nam_len, &value, &val_len, &has_predicate)) < 1) |
| 2156 | || !strncmp(name, ".", nam_len)) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2157 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-r], &predicate[-r]); |
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 | predicate += r; |
| 2162 | *parsed += r; |
| 2163 | |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2164 | 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] | 2165 | LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name); |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2166 | return -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2167 | } |
| 2168 | |
| 2169 | /* value does not match */ |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2170 | 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] | 2171 | return 1; |
| 2172 | } |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2173 | |
| 2174 | key = (struct lyd_node_leaf_list *)key->next; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2175 | } |
| 2176 | |
| 2177 | if (has_predicate) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2178 | LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2179 | return -1; |
| 2180 | } |
| 2181 | |
| 2182 | return 0; |
| 2183 | } |
| 2184 | |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 2185 | /** |
| 2186 | * @brief get the closest parent of the node (or the node itself) identified by the nodeid (path) |
| 2187 | * |
| 2188 | * @param[in] nodeid Node data path to find |
| 2189 | * @param[in] llist_value If the \p nodeid identifies leaf-list, this is expected value of the leaf-list instance. |
| 2190 | * @param[in] options Bitmask of options flags, see @ref pathoptions. |
| 2191 | * @param[out] parsed Number of characters processed in \p id |
| 2192 | * @return The closes parent (or the node itself) from the path |
| 2193 | */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2194 | struct lyd_node * |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2195 | resolve_partial_json_data_nodeid(const char *nodeid, const char *llist_value, struct lyd_node *start, int options, |
| 2196 | int *parsed) |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2197 | { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2198 | char *module_name = ly_buf(), *buf_backup = NULL, *str; |
Michal Vasko | 0a8d17f | 2016-10-19 15:51:36 +0200 | [diff] [blame] | 2199 | const char *id, *mod_name, *name, *pred_name; |
| 2200 | int r, ret, mod_name_len, nam_len, is_relative = -1; |
| 2201 | int has_predicate, last_parsed, val_len, pred_name_len, last_has_pred; |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2202 | struct lyd_node *sibling, *last_match = NULL; |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2203 | struct lyd_node_leaf_list *llist; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2204 | const struct lys_module *prefix_mod, *prev_mod; |
| 2205 | struct ly_ctx *ctx; |
| 2206 | |
| 2207 | assert(nodeid && start && parsed); |
| 2208 | |
| 2209 | ctx = start->schema->module->ctx; |
| 2210 | id = nodeid; |
| 2211 | |
| 2212 | 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] | 2213 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2214 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2215 | return NULL; |
| 2216 | } |
| 2217 | id += r; |
| 2218 | /* add it to parsed only after the data node was actually found */ |
| 2219 | last_parsed = r; |
| 2220 | |
| 2221 | if (is_relative) { |
| 2222 | prev_mod = start->schema->module; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2223 | start = start->child; |
| 2224 | } else { |
| 2225 | for (; start->parent; start = start->parent); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2226 | prev_mod = start->schema->module; |
| 2227 | } |
| 2228 | |
| 2229 | while (1) { |
| 2230 | LY_TREE_FOR(start, sibling) { |
Michal Vasko | 2411b94 | 2016-03-23 13:50:03 +0100 | [diff] [blame] | 2231 | /* RPC data check, return simply invalid argument, because the data tree is invalid */ |
| 2232 | if (lys_parent(sibling->schema)) { |
| 2233 | if (options & LYD_PATH_OPT_OUTPUT) { |
| 2234 | if (lys_parent(sibling->schema)->nodetype == LYS_INPUT) { |
Michal Vasko | b724689 | 2016-04-19 10:37:35 +0200 | [diff] [blame] | 2235 | 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] | 2236 | *parsed = -1; |
| 2237 | return NULL; |
| 2238 | } |
| 2239 | } else { |
| 2240 | if (lys_parent(sibling->schema)->nodetype == LYS_OUTPUT) { |
Michal Vasko | b724689 | 2016-04-19 10:37:35 +0200 | [diff] [blame] | 2241 | 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] | 2242 | *parsed = -1; |
| 2243 | return NULL; |
| 2244 | } |
| 2245 | } |
| 2246 | } |
| 2247 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2248 | /* name match */ |
| 2249 | if (!strncmp(name, sibling->schema->name, nam_len) && !sibling->schema->name[nam_len]) { |
| 2250 | |
| 2251 | /* module check */ |
| 2252 | if (mod_name) { |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2253 | if (mod_name_len > LY_BUF_SIZE - 1) { |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2254 | LOGINT; |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2255 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2256 | return NULL; |
| 2257 | } |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2258 | |
| 2259 | if (ly_buf_used && module_name[0]) { |
| 2260 | buf_backup = strndup(module_name, LY_BUF_SIZE - 1); |
| 2261 | } |
| 2262 | ly_buf_used++; |
| 2263 | |
Michal Vasko | df3f1ab | 2016-04-01 15:07:22 +0200 | [diff] [blame] | 2264 | memmove(module_name, mod_name, mod_name_len); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2265 | module_name[mod_name_len] = '\0'; |
| 2266 | /* will also find an augment module */ |
| 2267 | prefix_mod = ly_ctx_get_module(ctx, module_name, NULL); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2268 | |
| 2269 | if (buf_backup) { |
| 2270 | /* return previous internal buffer content */ |
Michal Vasko | 888a129 | 2016-04-05 12:08:54 +0200 | [diff] [blame] | 2271 | strncpy(module_name, buf_backup, LY_BUF_SIZE - 1); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2272 | free(buf_backup); |
| 2273 | buf_backup = NULL; |
| 2274 | } |
| 2275 | ly_buf_used--; |
| 2276 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2277 | if (!prefix_mod) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2278 | str = strndup(nodeid, (mod_name + mod_name_len) - nodeid); |
| 2279 | LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str); |
| 2280 | free(str); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2281 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2282 | return NULL; |
| 2283 | } |
| 2284 | } else { |
| 2285 | prefix_mod = prev_mod; |
| 2286 | } |
| 2287 | if (prefix_mod != lys_node_module(sibling->schema)) { |
| 2288 | continue; |
| 2289 | } |
| 2290 | |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2291 | /* leaf-list, did we find it with the correct value or not? */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2292 | if (sibling->schema->nodetype == LYS_LEAFLIST) { |
Michal Vasko | 0a8d17f | 2016-10-19 15:51:36 +0200 | [diff] [blame] | 2293 | last_has_pred = 0; |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2294 | if (has_predicate) { |
Michal Vasko | 0a8d17f | 2016-10-19 15:51:36 +0200 | [diff] [blame] | 2295 | if ((r = parse_schema_json_predicate(id, &pred_name, &pred_name_len, &llist_value, &val_len, &last_has_pred)) < 1) { |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2296 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
| 2297 | *parsed = -1; |
| 2298 | return NULL; |
| 2299 | } |
Michal Vasko | 0a8d17f | 2016-10-19 15:51:36 +0200 | [diff] [blame] | 2300 | if ((pred_name[0] != '.') || (pred_name_len != 1)) { |
| 2301 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[1], id + 1); |
| 2302 | *parsed = -1; |
| 2303 | return NULL; |
| 2304 | } |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2305 | } else { |
| 2306 | r = 0; |
| 2307 | if (llist_value) { |
| 2308 | val_len = strlen(llist_value); |
| 2309 | } else { |
| 2310 | val_len = 0; |
| 2311 | } |
| 2312 | } |
| 2313 | |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2314 | llist = (struct lyd_node_leaf_list *)sibling; |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2315 | if ((!val_len && llist->value_str && llist->value_str[0]) |
| 2316 | || (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] | 2317 | continue; |
| 2318 | } |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2319 | id += r; |
| 2320 | last_parsed += r; |
Michal Vasko | 0a8d17f | 2016-10-19 15:51:36 +0200 | [diff] [blame] | 2321 | has_predicate = last_has_pred; |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2322 | |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 2323 | } else if (sibling->schema->nodetype == LYS_LIST) { |
| 2324 | /* list, we need predicates'n'stuff then */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2325 | r = 0; |
| 2326 | if (!has_predicate) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2327 | LOGVAL(LYE_PATH_MISSKEY, LY_VLOG_NONE, NULL, name); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2328 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2329 | return NULL; |
| 2330 | } |
| 2331 | ret = resolve_partial_json_data_list_predicate(id, name, sibling, &r); |
| 2332 | if (ret == -1) { |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2333 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2334 | return NULL; |
| 2335 | } else if (ret == 1) { |
| 2336 | /* this list instance does not match */ |
| 2337 | continue; |
| 2338 | } |
| 2339 | id += r; |
| 2340 | last_parsed += r; |
| 2341 | } |
| 2342 | |
| 2343 | *parsed += last_parsed; |
| 2344 | |
| 2345 | /* the result node? */ |
| 2346 | if (!id[0]) { |
| 2347 | return sibling; |
| 2348 | } |
| 2349 | |
| 2350 | /* move down the tree, if possible */ |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 2351 | if (sibling->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2352 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2353 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2354 | return NULL; |
| 2355 | } |
Michal Vasko | c6c52cf | 2016-03-29 11:53:04 +0200 | [diff] [blame] | 2356 | last_match = sibling; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2357 | start = sibling->child; |
| 2358 | if (start) { |
| 2359 | prev_mod = start->schema->module; |
| 2360 | } |
| 2361 | break; |
| 2362 | } |
| 2363 | } |
| 2364 | |
| 2365 | /* no match, return last match */ |
| 2366 | if (!sibling) { |
| 2367 | return last_match; |
| 2368 | } |
| 2369 | |
| 2370 | 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] | 2371 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2372 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2373 | return NULL; |
| 2374 | } |
| 2375 | id += r; |
| 2376 | last_parsed = r; |
| 2377 | } |
| 2378 | |
| 2379 | /* cannot get here */ |
| 2380 | LOGINT; |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2381 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2382 | return NULL; |
| 2383 | } |
| 2384 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2385 | /** |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2386 | * @brief Resolves length or range intervals. Does not log. |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2387 | * Syntax is assumed to be correct, *ret MUST be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2388 | * |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2389 | * @param[in] str_restr Restriction as a string. |
| 2390 | * @param[in] type Type of the restriction. |
| 2391 | * @param[out] ret Final interval structure that starts with |
| 2392 | * the interval of the initial type, continues with intervals |
| 2393 | * of any superior types derived from the initial one, and |
| 2394 | * finishes with intervals from our \p type. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2395 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2396 | * @return EXIT_SUCCESS on succes, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2397 | */ |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2398 | int |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2399 | 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] | 2400 | { |
| 2401 | /* 0 - unsigned, 1 - signed, 2 - floating point */ |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2402 | int kind; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2403 | int64_t local_smin, local_smax, local_fmin, local_fmax; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2404 | uint64_t local_umin, local_umax; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2405 | uint8_t local_fdig; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2406 | const char *seg_ptr, *ptr; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2407 | 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] | 2408 | |
| 2409 | switch (type->base) { |
| 2410 | case LY_TYPE_BINARY: |
| 2411 | kind = 0; |
| 2412 | local_umin = 0; |
| 2413 | local_umax = 18446744073709551615UL; |
| 2414 | |
| 2415 | if (!str_restr && type->info.binary.length) { |
| 2416 | str_restr = type->info.binary.length->expr; |
| 2417 | } |
| 2418 | break; |
| 2419 | case LY_TYPE_DEC64: |
| 2420 | kind = 2; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2421 | local_fmin = __INT64_C(-9223372036854775807) - __INT64_C(1); |
| 2422 | local_fmax = __INT64_C(9223372036854775807); |
| 2423 | local_fdig = type->info.dec64.dig; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2424 | |
| 2425 | if (!str_restr && type->info.dec64.range) { |
| 2426 | str_restr = type->info.dec64.range->expr; |
| 2427 | } |
| 2428 | break; |
| 2429 | case LY_TYPE_INT8: |
| 2430 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2431 | local_smin = __INT64_C(-128); |
| 2432 | local_smax = __INT64_C(127); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2433 | |
| 2434 | if (!str_restr && type->info.num.range) { |
| 2435 | str_restr = type->info.num.range->expr; |
| 2436 | } |
| 2437 | break; |
| 2438 | case LY_TYPE_INT16: |
| 2439 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2440 | local_smin = __INT64_C(-32768); |
| 2441 | local_smax = __INT64_C(32767); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2442 | |
| 2443 | if (!str_restr && type->info.num.range) { |
| 2444 | str_restr = type->info.num.range->expr; |
| 2445 | } |
| 2446 | break; |
| 2447 | case LY_TYPE_INT32: |
| 2448 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2449 | local_smin = __INT64_C(-2147483648); |
| 2450 | local_smax = __INT64_C(2147483647); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2451 | |
| 2452 | if (!str_restr && type->info.num.range) { |
| 2453 | str_restr = type->info.num.range->expr; |
| 2454 | } |
| 2455 | break; |
| 2456 | case LY_TYPE_INT64: |
| 2457 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2458 | local_smin = __INT64_C(-9223372036854775807) - __INT64_C(1); |
| 2459 | local_smax = __INT64_C(9223372036854775807); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2460 | |
| 2461 | if (!str_restr && type->info.num.range) { |
| 2462 | str_restr = type->info.num.range->expr; |
| 2463 | } |
| 2464 | break; |
| 2465 | case LY_TYPE_UINT8: |
| 2466 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2467 | local_umin = __UINT64_C(0); |
| 2468 | local_umax = __UINT64_C(255); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2469 | |
| 2470 | if (!str_restr && type->info.num.range) { |
| 2471 | str_restr = type->info.num.range->expr; |
| 2472 | } |
| 2473 | break; |
| 2474 | case LY_TYPE_UINT16: |
| 2475 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2476 | local_umin = __UINT64_C(0); |
| 2477 | local_umax = __UINT64_C(65535); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2478 | |
| 2479 | if (!str_restr && type->info.num.range) { |
| 2480 | str_restr = type->info.num.range->expr; |
| 2481 | } |
| 2482 | break; |
| 2483 | case LY_TYPE_UINT32: |
| 2484 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2485 | local_umin = __UINT64_C(0); |
| 2486 | local_umax = __UINT64_C(4294967295); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2487 | |
| 2488 | if (!str_restr && type->info.num.range) { |
| 2489 | str_restr = type->info.num.range->expr; |
| 2490 | } |
| 2491 | break; |
| 2492 | case LY_TYPE_UINT64: |
| 2493 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2494 | local_umin = __UINT64_C(0); |
| 2495 | local_umax = __UINT64_C(18446744073709551615); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2496 | |
| 2497 | if (!str_restr && type->info.num.range) { |
| 2498 | str_restr = type->info.num.range->expr; |
| 2499 | } |
| 2500 | break; |
| 2501 | case LY_TYPE_STRING: |
| 2502 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2503 | local_umin = __UINT64_C(0); |
| 2504 | local_umax = __UINT64_C(18446744073709551615); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2505 | |
| 2506 | if (!str_restr && type->info.str.length) { |
| 2507 | str_restr = type->info.str.length->expr; |
| 2508 | } |
| 2509 | break; |
| 2510 | default: |
| 2511 | LOGINT; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2512 | return -1; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2513 | } |
| 2514 | |
| 2515 | /* process superior types */ |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2516 | if (type->der) { |
| 2517 | if (resolve_len_ran_interval(NULL, &type->der->type, &intv)) { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2518 | LOGINT; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2519 | return -1; |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2520 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2521 | assert(!intv || (intv->kind == kind)); |
| 2522 | } |
| 2523 | |
| 2524 | if (!str_restr) { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2525 | /* we do not have any restriction, return superior ones */ |
| 2526 | *ret = intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2527 | return EXIT_SUCCESS; |
| 2528 | } |
| 2529 | |
| 2530 | /* adjust local min and max */ |
| 2531 | if (intv) { |
| 2532 | tmp_intv = intv; |
| 2533 | |
| 2534 | if (kind == 0) { |
| 2535 | local_umin = tmp_intv->value.uval.min; |
| 2536 | } else if (kind == 1) { |
| 2537 | local_smin = tmp_intv->value.sval.min; |
| 2538 | } else if (kind == 2) { |
| 2539 | local_fmin = tmp_intv->value.fval.min; |
| 2540 | } |
| 2541 | |
| 2542 | while (tmp_intv->next) { |
| 2543 | tmp_intv = tmp_intv->next; |
| 2544 | } |
| 2545 | |
| 2546 | if (kind == 0) { |
| 2547 | local_umax = tmp_intv->value.uval.max; |
| 2548 | } else if (kind == 1) { |
| 2549 | local_smax = tmp_intv->value.sval.max; |
| 2550 | } else if (kind == 2) { |
| 2551 | local_fmax = tmp_intv->value.fval.max; |
| 2552 | } |
| 2553 | } |
| 2554 | |
| 2555 | /* finally parse our restriction */ |
| 2556 | seg_ptr = str_restr; |
Pavol Vican | 9e7c01d | 2016-08-29 09:36:17 +0200 | [diff] [blame] | 2557 | tmp_intv = NULL; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2558 | while (1) { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2559 | if (!tmp_local_intv) { |
| 2560 | assert(!local_intv); |
| 2561 | local_intv = malloc(sizeof *local_intv); |
| 2562 | tmp_local_intv = local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2563 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2564 | tmp_local_intv->next = malloc(sizeof *tmp_local_intv); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2565 | tmp_local_intv = tmp_local_intv->next; |
| 2566 | } |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2567 | if (!tmp_local_intv) { |
| 2568 | LOGMEM; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2569 | goto error; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2570 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2571 | |
| 2572 | tmp_local_intv->kind = kind; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2573 | tmp_local_intv->type = type; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2574 | tmp_local_intv->next = NULL; |
| 2575 | |
| 2576 | /* min */ |
| 2577 | ptr = seg_ptr; |
| 2578 | while (isspace(ptr[0])) { |
| 2579 | ++ptr; |
| 2580 | } |
| 2581 | if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) { |
| 2582 | if (kind == 0) { |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2583 | tmp_local_intv->value.uval.min = strtol(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2584 | } else if (kind == 1) { |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2585 | tmp_local_intv->value.sval.min = strtol(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2586 | } else if (kind == 2) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 2587 | if (parse_range_dec64(&ptr, local_fdig, &tmp_local_intv->value.fval.min)) { |
| 2588 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, ptr, "range"); |
| 2589 | goto error; |
| 2590 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2591 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2592 | } else if (!strncmp(ptr, "min", 3)) { |
| 2593 | if (kind == 0) { |
| 2594 | tmp_local_intv->value.uval.min = local_umin; |
| 2595 | } else if (kind == 1) { |
| 2596 | tmp_local_intv->value.sval.min = local_smin; |
| 2597 | } else if (kind == 2) { |
| 2598 | tmp_local_intv->value.fval.min = local_fmin; |
| 2599 | } |
| 2600 | |
| 2601 | ptr += 3; |
| 2602 | } else if (!strncmp(ptr, "max", 3)) { |
| 2603 | if (kind == 0) { |
| 2604 | tmp_local_intv->value.uval.min = local_umax; |
| 2605 | } else if (kind == 1) { |
| 2606 | tmp_local_intv->value.sval.min = local_smax; |
| 2607 | } else if (kind == 2) { |
| 2608 | tmp_local_intv->value.fval.min = local_fmax; |
| 2609 | } |
| 2610 | |
| 2611 | ptr += 3; |
| 2612 | } else { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2613 | LOGINT; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2614 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2615 | } |
| 2616 | |
| 2617 | while (isspace(ptr[0])) { |
| 2618 | ptr++; |
| 2619 | } |
| 2620 | |
| 2621 | /* no interval or interval */ |
| 2622 | if ((ptr[0] == '|') || !ptr[0]) { |
| 2623 | if (kind == 0) { |
| 2624 | tmp_local_intv->value.uval.max = tmp_local_intv->value.uval.min; |
| 2625 | } else if (kind == 1) { |
| 2626 | tmp_local_intv->value.sval.max = tmp_local_intv->value.sval.min; |
| 2627 | } else if (kind == 2) { |
| 2628 | tmp_local_intv->value.fval.max = tmp_local_intv->value.fval.min; |
| 2629 | } |
| 2630 | } else if (!strncmp(ptr, "..", 2)) { |
| 2631 | /* skip ".." */ |
| 2632 | ptr += 2; |
| 2633 | while (isspace(ptr[0])) { |
| 2634 | ++ptr; |
| 2635 | } |
| 2636 | |
| 2637 | /* max */ |
| 2638 | if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) { |
| 2639 | if (kind == 0) { |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2640 | tmp_local_intv->value.uval.max = strtol(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2641 | } else if (kind == 1) { |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2642 | tmp_local_intv->value.sval.max = strtol(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2643 | } else if (kind == 2) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 2644 | if (parse_range_dec64(&ptr, local_fdig, &tmp_local_intv->value.fval.max)) { |
| 2645 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, ptr, "range"); |
| 2646 | goto error; |
| 2647 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2648 | } |
| 2649 | } else if (!strncmp(ptr, "max", 3)) { |
| 2650 | if (kind == 0) { |
| 2651 | tmp_local_intv->value.uval.max = local_umax; |
| 2652 | } else if (kind == 1) { |
| 2653 | tmp_local_intv->value.sval.max = local_smax; |
| 2654 | } else if (kind == 2) { |
| 2655 | tmp_local_intv->value.fval.max = local_fmax; |
| 2656 | } |
| 2657 | } else { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2658 | LOGINT; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2659 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2660 | } |
| 2661 | } else { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2662 | LOGINT; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2663 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2664 | } |
| 2665 | |
Pavol Vican | 9e7c01d | 2016-08-29 09:36:17 +0200 | [diff] [blame] | 2666 | /* check min and max in correct order*/ |
| 2667 | if (kind == 0) { |
| 2668 | /* current segment */ |
| 2669 | if (tmp_local_intv->value.uval.min > tmp_local_intv->value.uval.max) { |
| 2670 | goto error; |
| 2671 | } |
| 2672 | if (tmp_local_intv->value.uval.min < local_umin || tmp_local_intv->value.uval.max > local_umax) { |
| 2673 | goto error; |
| 2674 | } |
| 2675 | /* segments sholud be ascending order */ |
Pavol Vican | 69f62c9 | 2016-08-30 09:06:25 +0200 | [diff] [blame] | 2676 | 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] | 2677 | goto error; |
| 2678 | } |
| 2679 | } else if (kind == 1) { |
| 2680 | if (tmp_local_intv->value.sval.min > tmp_local_intv->value.sval.max) { |
| 2681 | goto error; |
| 2682 | } |
| 2683 | if (tmp_local_intv->value.sval.min < local_smin || tmp_local_intv->value.sval.max > local_smax) { |
| 2684 | goto error; |
| 2685 | } |
Pavol Vican | 69f62c9 | 2016-08-30 09:06:25 +0200 | [diff] [blame] | 2686 | 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] | 2687 | goto error; |
| 2688 | } |
| 2689 | } else if (kind == 2) { |
| 2690 | if (tmp_local_intv->value.fval.min > tmp_local_intv->value.fval.max) { |
| 2691 | goto error; |
| 2692 | } |
| 2693 | if (tmp_local_intv->value.fval.min < local_fmin || tmp_local_intv->value.fval.max > local_fmax) { |
| 2694 | goto error; |
| 2695 | } |
Pavol Vican | 69f62c9 | 2016-08-30 09:06:25 +0200 | [diff] [blame] | 2696 | 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] | 2697 | /* 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] | 2698 | goto error; |
| 2699 | } |
| 2700 | } |
| 2701 | |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2702 | /* next segment (next OR) */ |
| 2703 | seg_ptr = strchr(seg_ptr, '|'); |
| 2704 | if (!seg_ptr) { |
| 2705 | break; |
| 2706 | } |
| 2707 | seg_ptr++; |
Pavol Vican | 9e7c01d | 2016-08-29 09:36:17 +0200 | [diff] [blame] | 2708 | tmp_intv = tmp_local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2709 | } |
| 2710 | |
| 2711 | /* check local restrictions against superior ones */ |
| 2712 | if (intv) { |
| 2713 | tmp_intv = intv; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2714 | tmp_local_intv = local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2715 | |
| 2716 | while (tmp_local_intv && tmp_intv) { |
| 2717 | /* reuse local variables */ |
| 2718 | if (kind == 0) { |
| 2719 | local_umin = tmp_local_intv->value.uval.min; |
| 2720 | local_umax = tmp_local_intv->value.uval.max; |
| 2721 | |
| 2722 | /* it must be in this interval */ |
| 2723 | if ((local_umin >= tmp_intv->value.uval.min) && (local_umin <= tmp_intv->value.uval.max)) { |
| 2724 | /* this interval is covered, next one */ |
| 2725 | if (local_umax <= tmp_intv->value.uval.max) { |
| 2726 | tmp_local_intv = tmp_local_intv->next; |
| 2727 | continue; |
| 2728 | /* ascending order of restrictions -> fail */ |
| 2729 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2730 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2731 | } |
| 2732 | } |
| 2733 | } else if (kind == 1) { |
| 2734 | local_smin = tmp_local_intv->value.sval.min; |
| 2735 | local_smax = tmp_local_intv->value.sval.max; |
| 2736 | |
| 2737 | if ((local_smin >= tmp_intv->value.sval.min) && (local_smin <= tmp_intv->value.sval.max)) { |
| 2738 | if (local_smax <= tmp_intv->value.sval.max) { |
| 2739 | tmp_local_intv = tmp_local_intv->next; |
| 2740 | continue; |
| 2741 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2742 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2743 | } |
| 2744 | } |
| 2745 | } else if (kind == 2) { |
| 2746 | local_fmin = tmp_local_intv->value.fval.min; |
| 2747 | local_fmax = tmp_local_intv->value.fval.max; |
| 2748 | |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2749 | 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] | 2750 | && (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] | 2751 | 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] | 2752 | tmp_local_intv = tmp_local_intv->next; |
| 2753 | continue; |
| 2754 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2755 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2756 | } |
| 2757 | } |
| 2758 | } |
| 2759 | |
| 2760 | tmp_intv = tmp_intv->next; |
| 2761 | } |
| 2762 | |
| 2763 | /* some interval left uncovered -> fail */ |
| 2764 | if (tmp_local_intv) { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2765 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2766 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2767 | } |
| 2768 | |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2769 | /* append the local intervals to all the intervals of the superior types, return it all */ |
| 2770 | if (intv) { |
| 2771 | for (tmp_intv = intv; tmp_intv->next; tmp_intv = tmp_intv->next); |
| 2772 | tmp_intv->next = local_intv; |
| 2773 | } else { |
| 2774 | intv = local_intv; |
| 2775 | } |
| 2776 | *ret = intv; |
| 2777 | |
| 2778 | return EXIT_SUCCESS; |
| 2779 | |
| 2780 | error: |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2781 | while (intv) { |
| 2782 | tmp_intv = intv->next; |
| 2783 | free(intv); |
| 2784 | intv = tmp_intv; |
| 2785 | } |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2786 | while (local_intv) { |
| 2787 | tmp_local_intv = local_intv->next; |
| 2788 | free(local_intv); |
| 2789 | local_intv = tmp_local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2790 | } |
| 2791 | |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2792 | return -1; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2793 | } |
| 2794 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2795 | /** |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2796 | * @brief Resolve a typedef, return only resolved typedefs if derived. If leafref, it must be |
| 2797 | * resolved for this function to return it. Does not log. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2798 | * |
| 2799 | * @param[in] name Typedef name. |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2800 | * @param[in] mod_name Typedef name module name. |
| 2801 | * @param[in] module Main module. |
| 2802 | * @param[in] parent Parent of the resolved type definition. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2803 | * @param[out] ret Pointer to the resolved typedef. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2804 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2805 | * @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] | 2806 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2807 | int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 2808 | resolve_superior_type(const char *name, const char *mod_name, const struct lys_module *module, |
| 2809 | const struct lys_node *parent, struct lys_tpdf **ret) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2810 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2811 | int i, j; |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2812 | struct lys_tpdf *tpdf, *match; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2813 | int tpdf_size; |
| 2814 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2815 | if (!mod_name) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2816 | /* no prefix, try built-in types */ |
| 2817 | for (i = 1; i < LY_DATA_TYPE_COUNT; i++) { |
| 2818 | if (!strcmp(ly_types[i].def->name, name)) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2819 | if (ret) { |
| 2820 | *ret = ly_types[i].def; |
| 2821 | } |
| 2822 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2823 | } |
| 2824 | } |
| 2825 | } else { |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2826 | if (!strcmp(mod_name, module->name)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2827 | /* prefix refers to the current module, ignore it */ |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2828 | mod_name = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2829 | } |
| 2830 | } |
| 2831 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2832 | if (!mod_name && parent) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2833 | /* search in local typedefs */ |
| 2834 | while (parent) { |
| 2835 | switch (parent->nodetype) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2836 | case LYS_CONTAINER: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 2837 | tpdf_size = ((struct lys_node_container *)parent)->tpdf_size; |
| 2838 | tpdf = ((struct lys_node_container *)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_LIST: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 2842 | tpdf_size = ((struct lys_node_list *)parent)->tpdf_size; |
| 2843 | tpdf = ((struct lys_node_list *)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_GROUPING: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 2847 | tpdf_size = ((struct lys_node_grp *)parent)->tpdf_size; |
| 2848 | tpdf = ((struct lys_node_grp *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2849 | break; |
| 2850 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2851 | case LYS_RPC: |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 2852 | case LYS_ACTION: |
| 2853 | tpdf_size = ((struct lys_node_rpc_action *)parent)->tpdf_size; |
| 2854 | tpdf = ((struct lys_node_rpc_action *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2855 | break; |
| 2856 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2857 | case LYS_NOTIF: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 2858 | tpdf_size = ((struct lys_node_notif *)parent)->tpdf_size; |
| 2859 | tpdf = ((struct lys_node_notif *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2860 | break; |
| 2861 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2862 | case LYS_INPUT: |
| 2863 | case LYS_OUTPUT: |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 2864 | tpdf_size = ((struct lys_node_inout *)parent)->tpdf_size; |
| 2865 | tpdf = ((struct lys_node_inout *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2866 | break; |
| 2867 | |
| 2868 | default: |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 2869 | parent = lys_parent(parent); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2870 | continue; |
| 2871 | } |
| 2872 | |
| 2873 | for (i = 0; i < tpdf_size; i++) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 2874 | if (!strcmp(tpdf[i].name, name) && tpdf[i].type.base > 0) { |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2875 | match = &tpdf[i]; |
| 2876 | goto check_leafref; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2877 | } |
| 2878 | } |
| 2879 | |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 2880 | parent = lys_parent(parent); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2881 | } |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2882 | } else { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2883 | /* get module where to search */ |
Michal Vasko | b6729c6 | 2015-10-21 12:09:47 +0200 | [diff] [blame] | 2884 | module = lys_get_import_module(module, NULL, 0, mod_name, 0); |
Michal Vasko | c935fff | 2015-08-17 14:02:06 +0200 | [diff] [blame] | 2885 | if (!module) { |
| 2886 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2887 | } |
| 2888 | } |
| 2889 | |
| 2890 | /* search in top level typedefs */ |
| 2891 | for (i = 0; i < module->tpdf_size; i++) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 2892 | 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] | 2893 | match = &module->tpdf[i]; |
| 2894 | goto check_leafref; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2895 | } |
| 2896 | } |
| 2897 | |
| 2898 | /* search in submodules */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2899 | for (i = 0; i < module->inc_size && module->inc[i].submodule; i++) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2900 | for (j = 0; j < module->inc[i].submodule->tpdf_size; j++) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 2901 | 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] | 2902 | match = &module->inc[i].submodule->tpdf[j]; |
| 2903 | goto check_leafref; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2904 | } |
| 2905 | } |
| 2906 | } |
| 2907 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2908 | return EXIT_FAILURE; |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2909 | |
| 2910 | check_leafref: |
| 2911 | if (ret) { |
| 2912 | *ret = match; |
| 2913 | } |
| 2914 | if (match->type.base == LY_TYPE_LEAFREF) { |
| 2915 | while (!match->type.info.lref.path) { |
| 2916 | match = match->type.der; |
| 2917 | assert(match); |
| 2918 | } |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2919 | } |
| 2920 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2921 | } |
| 2922 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2923 | /** |
| 2924 | * @brief Check the default \p value of the \p type. Logs directly. |
| 2925 | * |
| 2926 | * @param[in] type Type definition to use. |
| 2927 | * @param[in] value Default value to check. |
Michal Vasko | 5682640 | 2016-03-02 11:11:37 +0100 | [diff] [blame] | 2928 | * @param[in] module Type module. |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2929 | * |
| 2930 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 2931 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2932 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2933 | 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] | 2934 | { |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 2935 | struct lys_tpdf *base_tpdf = NULL; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2936 | struct lyd_node_leaf_list node; |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 2937 | int ret = EXIT_SUCCESS; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2938 | |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 2939 | if (type->base <= LY_TYPE_DER) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 2940 | /* the type was not resolved yet, nothing to do for now */ |
| 2941 | return EXIT_FAILURE; |
| 2942 | } |
| 2943 | |
| 2944 | if (!value) { |
| 2945 | /* we do not have a new default value, so is there any to check even, in some base type? */ |
| 2946 | for (base_tpdf = type->der; base_tpdf->type.der; base_tpdf = base_tpdf->type.der) { |
| 2947 | if (base_tpdf->dflt) { |
| 2948 | value = base_tpdf->dflt; |
| 2949 | break; |
| 2950 | } |
| 2951 | } |
| 2952 | |
| 2953 | if (!value) { |
| 2954 | /* no default value, nothing to check, all is well */ |
| 2955 | return EXIT_SUCCESS; |
| 2956 | } |
| 2957 | |
| 2958 | /* 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)? */ |
| 2959 | switch (type->base) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 2960 | case LY_TYPE_IDENT: |
| 2961 | case LY_TYPE_INST: |
| 2962 | case LY_TYPE_LEAFREF: |
| 2963 | case LY_TYPE_BOOL: |
| 2964 | case LY_TYPE_EMPTY: |
| 2965 | /* these have no restrictions, so we would do the exact same work as the unres in the base typedef */ |
| 2966 | return EXIT_SUCCESS; |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 2967 | case LY_TYPE_BITS: |
| 2968 | /* the default value must match the restricted list of values, if the type was restricted */ |
| 2969 | if (type->info.bits.count) { |
| 2970 | break; |
| 2971 | } |
| 2972 | return EXIT_SUCCESS; |
| 2973 | case LY_TYPE_ENUM: |
| 2974 | /* the default value must match the restricted list of values, if the type was restricted */ |
| 2975 | if (type->info.enums.count) { |
| 2976 | break; |
| 2977 | } |
| 2978 | return EXIT_SUCCESS; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 2979 | case LY_TYPE_DEC64: |
| 2980 | if (type->info.dec64.range) { |
| 2981 | break; |
| 2982 | } |
| 2983 | return EXIT_SUCCESS; |
| 2984 | case LY_TYPE_BINARY: |
| 2985 | if (type->info.binary.length) { |
| 2986 | break; |
| 2987 | } |
| 2988 | return EXIT_SUCCESS; |
| 2989 | case LY_TYPE_INT8: |
| 2990 | case LY_TYPE_INT16: |
| 2991 | case LY_TYPE_INT32: |
| 2992 | case LY_TYPE_INT64: |
| 2993 | case LY_TYPE_UINT8: |
| 2994 | case LY_TYPE_UINT16: |
| 2995 | case LY_TYPE_UINT32: |
| 2996 | case LY_TYPE_UINT64: |
| 2997 | if (type->info.num.range) { |
| 2998 | break; |
| 2999 | } |
| 3000 | return EXIT_SUCCESS; |
| 3001 | case LY_TYPE_STRING: |
| 3002 | if (type->info.str.length || type->info.str.patterns) { |
| 3003 | break; |
| 3004 | } |
| 3005 | return EXIT_SUCCESS; |
| 3006 | case LY_TYPE_UNION: |
| 3007 | /* way too much trouble learning whether we need to check the default again, so just do it */ |
| 3008 | break; |
| 3009 | default: |
| 3010 | LOGINT; |
| 3011 | return -1; |
| 3012 | } |
Radek Krejci | 55a161c | 2016-09-05 17:13:25 +0200 | [diff] [blame] | 3013 | } else if (type->base == LY_TYPE_EMPTY) { |
| 3014 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "default", type->parent->name); |
| 3015 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The \"empty\" data type cannot have a default value."); |
| 3016 | return -1; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3017 | } |
| 3018 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3019 | /* dummy leaf */ |
Radek Krejci | 4fe36bd | 2016-03-17 16:47:16 +0100 | [diff] [blame] | 3020 | memset(&node, 0, sizeof node); |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3021 | node.value_str = value; |
| 3022 | node.value_type = type->base; |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 3023 | node.schema = calloc(1, sizeof (struct lys_node_leaf)); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3024 | if (!node.schema) { |
| 3025 | LOGMEM; |
| 3026 | return -1; |
| 3027 | } |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3028 | node.schema->name = strdup("fake-default"); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3029 | if (!node.schema->name) { |
| 3030 | LOGMEM; |
Michal Vasko | f49eda0 | 2016-07-21 12:17:01 +0200 | [diff] [blame] | 3031 | free(node.schema); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3032 | return -1; |
| 3033 | } |
Michal Vasko | 5682640 | 2016-03-02 11:11:37 +0100 | [diff] [blame] | 3034 | node.schema->module = module; |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 3035 | memcpy(&((struct lys_node_leaf *)node.schema)->type, type, sizeof *type); |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3036 | |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 3037 | if (type->base == LY_TYPE_LEAFREF) { |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3038 | if (!type->info.lref.target) { |
| 3039 | ret = EXIT_FAILURE; |
| 3040 | goto finish; |
| 3041 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3042 | ret = check_default(&type->info.lref.target->type, value, module); |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3043 | |
| 3044 | } else if ((type->base == LY_TYPE_INST) || (type->base == LY_TYPE_IDENT)) { |
| 3045 | /* it was converted to JSON format before, nothing else sensible we can do */ |
| 3046 | |
| 3047 | } else { |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 3048 | if (lyp_parse_value(&node, NULL, 1)) { |
| 3049 | ret = -1; |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3050 | if (base_tpdf) { |
| 3051 | /* default value was is defined in some base typedef */ |
| 3052 | if ((type->base == LY_TYPE_BITS && type->der->type.der) || |
| 3053 | (type->base == LY_TYPE_ENUM && type->der->type.der)) { |
| 3054 | /* we have refined bits/enums */ |
| 3055 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, |
| 3056 | "Invalid value \"%s\" of the default statement inherited to \"%s\" from \"%s\" base type.", |
| 3057 | value, type->parent->name, base_tpdf->name); |
| 3058 | } |
| 3059 | } |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 3060 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3061 | } |
| 3062 | |
| 3063 | finish: |
| 3064 | if (node.value_type == LY_TYPE_BITS) { |
| 3065 | free(node.value.bit); |
| 3066 | } |
| 3067 | free((char *)node.schema->name); |
| 3068 | free(node.schema); |
| 3069 | |
| 3070 | return ret; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3071 | } |
| 3072 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3073 | /** |
| 3074 | * @brief Check a key for mandatory attributes. Logs directly. |
| 3075 | * |
| 3076 | * @param[in] key The key to check. |
| 3077 | * @param[in] flags What flags to check. |
| 3078 | * @param[in] list The list of all the keys. |
| 3079 | * @param[in] index Index of the key in the key list. |
| 3080 | * @param[in] name The name of the keys. |
| 3081 | * @param[in] len The name length. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3082 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3083 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3084 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3085 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3086 | 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] | 3087 | { |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3088 | struct lys_node_leaf *key = list->keys[index]; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3089 | char *dup = NULL; |
| 3090 | int j; |
| 3091 | |
| 3092 | /* existence */ |
| 3093 | if (!key) { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3094 | if (name[len] != '\0') { |
| 3095 | dup = strdup(name); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3096 | if (!dup) { |
| 3097 | LOGMEM; |
| 3098 | return -1; |
| 3099 | } |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3100 | dup[len] = '\0'; |
| 3101 | name = dup; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3102 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3103 | LOGVAL(LYE_KEY_MISS, LY_VLOG_LYS, list, name); |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 3104 | free(dup); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3105 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3106 | } |
| 3107 | |
| 3108 | /* uniqueness */ |
| 3109 | for (j = index - 1; j >= 0; j--) { |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3110 | if (key == list->keys[j]) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3111 | LOGVAL(LYE_KEY_DUP, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3112 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3113 | } |
| 3114 | } |
| 3115 | |
| 3116 | /* key is a leaf */ |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3117 | if (key->nodetype != LYS_LEAF) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3118 | LOGVAL(LYE_KEY_NLEAF, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3119 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3120 | } |
| 3121 | |
| 3122 | /* type of the leaf is not built-in empty */ |
Radek Krejci | c3738db | 2016-09-15 15:51:21 +0200 | [diff] [blame] | 3123 | if (key->type.base == LY_TYPE_EMPTY && key->module->version < 2) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3124 | LOGVAL(LYE_KEY_TYPE, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3125 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3126 | } |
| 3127 | |
| 3128 | /* config attribute is the same as of the list */ |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3129 | if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3130 | LOGVAL(LYE_KEY_CONFIG, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3131 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3132 | } |
| 3133 | |
Radek Krejci | 55e2cdc | 2016-03-11 13:51:09 +0100 | [diff] [blame] | 3134 | /* key is not placed from augment */ |
| 3135 | if (key->parent->nodetype == LYS_AUGMENT) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3136 | LOGVAL(LYE_KEY_MISS, LY_VLOG_LYS, key, key->name); |
| 3137 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, key, "Key inserted from augment."); |
Radek Krejci | 55e2cdc | 2016-03-11 13:51:09 +0100 | [diff] [blame] | 3138 | return -1; |
| 3139 | } |
| 3140 | |
Radek Krejci | 3f21ada | 2016-08-01 13:34:31 +0200 | [diff] [blame] | 3141 | /* key is not when/if-feature -conditional */ |
| 3142 | j = 0; |
| 3143 | if (key->when || (key->iffeature_size && (j = 1))) { |
| 3144 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, key, j ? "if-feature" : "when", "leaf"); |
| 3145 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, key, "Key definition cannot depend on a \"%s\" condition.", |
| 3146 | j ? "if-feature" : "when"); |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3147 | return -1; |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3148 | } |
| 3149 | |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3150 | return EXIT_SUCCESS; |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3151 | } |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3152 | |
| 3153 | /** |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3154 | * @brief Resolve (test the target exists) unique. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3155 | * |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3156 | * @param[in] parent The parent node of the unique structure. |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3157 | * @param[in] uniq_str_path One path from the unique string. |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3158 | * |
| 3159 | * @return EXIT_SUCCESS on succes, EXIT_FAILURE on forward reference, -1 on error. |
| 3160 | */ |
| 3161 | int |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3162 | 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] | 3163 | { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3164 | int rc; |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3165 | const struct lys_node *leaf = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3166 | |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 3167 | 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] | 3168 | if (rc || !leaf) { |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3169 | if (rc) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3170 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3171 | if (rc > 0) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3172 | 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] | 3173 | } else if (rc == -2) { |
Michal Vasko | c66c6d8 | 2016-04-12 11:37:31 +0200 | [diff] [blame] | 3174 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, parent, "Unique argument references list."); |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3175 | } |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3176 | rc = -1; |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3177 | } else { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3178 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
| 3179 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, parent, "Target leaf not found."); |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3180 | rc = EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3181 | } |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3182 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3183 | } |
Michal Vasko | 9bb061b | 2016-02-12 11:00:19 +0100 | [diff] [blame] | 3184 | if (leaf->nodetype != LYS_LEAF) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3185 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
| 3186 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, parent, "Target is not a leaf."); |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3187 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3188 | } |
| 3189 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3190 | /* check status */ |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3191 | 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] | 3192 | return -1; |
| 3193 | } |
| 3194 | |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3195 | /* check that all unique's targets are of the same config type */ |
| 3196 | if (*trg_type) { |
| 3197 | if (((*trg_type == 1) && (leaf->flags & LYS_CONFIG_R)) || ((*trg_type == 2) && (leaf->flags & LYS_CONFIG_W))) { |
| 3198 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
| 3199 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, parent, |
| 3200 | "Leaf \"%s\" referenced in unique statement is config %s, but previous referenced leaf is config %s.", |
| 3201 | uniq_str_path, *trg_type == 1 ? "false" : "true", *trg_type == 1 ? "true" : "false"); |
| 3202 | return -1; |
| 3203 | } |
| 3204 | } else { |
| 3205 | /* first unique */ |
| 3206 | if (leaf->flags & LYS_CONFIG_W) { |
| 3207 | *trg_type = 1; |
| 3208 | } else { |
| 3209 | *trg_type = 2; |
| 3210 | } |
| 3211 | } |
| 3212 | |
Radek Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame] | 3213 | /* set leaf's unique flag */ |
| 3214 | ((struct lys_node_leaf *)leaf)->flags |= LYS_UNIQUE; |
| 3215 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3216 | return EXIT_SUCCESS; |
| 3217 | |
| 3218 | error: |
| 3219 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3220 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3221 | } |
| 3222 | |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 3223 | void |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3224 | unres_data_del(struct unres_data *unres, uint32_t i) |
| 3225 | { |
| 3226 | /* there are items after the one deleted */ |
| 3227 | if (i+1 < unres->count) { |
| 3228 | /* we only move the data, memory is left allocated, why bother */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3229 | 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] | 3230 | |
| 3231 | /* deleting the last item */ |
| 3232 | } else if (i == 0) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3233 | free(unres->node); |
| 3234 | unres->node = NULL; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3235 | } |
| 3236 | |
| 3237 | /* if there are no items after and it is not the last one, just move the counter */ |
| 3238 | --unres->count; |
| 3239 | } |
| 3240 | |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3241 | /** |
| 3242 | * @brief Resolve (find) a data node from a specific module. Does not log. |
| 3243 | * |
| 3244 | * @param[in] mod Module to search in. |
| 3245 | * @param[in] name Name of the data node. |
| 3246 | * @param[in] nam_len Length of the name. |
| 3247 | * @param[in] start Data node to start the search from. |
| 3248 | * @param[in,out] parents Resolved nodes. If there are some parents, |
| 3249 | * they are replaced (!!) with the resolvents. |
| 3250 | * |
Michal Vasko | 2471e7f | 2016-04-11 11:00:15 +0200 | [diff] [blame] | 3251 | * @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] | 3252 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3253 | static int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3254 | 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] | 3255 | { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3256 | struct lyd_node *node; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3257 | int flag; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3258 | uint32_t i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3259 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3260 | if (!parents->count) { |
| 3261 | parents->count = 1; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3262 | parents->node = malloc(sizeof *parents->node); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3263 | if (!parents->node) { |
| 3264 | LOGMEM; |
Michal Vasko | 2471e7f | 2016-04-11 11:00:15 +0200 | [diff] [blame] | 3265 | return -1; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3266 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3267 | parents->node[0] = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3268 | } |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3269 | for (i = 0; i < parents->count;) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 3270 | 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] | 3271 | /* skip */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3272 | ++i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3273 | continue; |
| 3274 | } |
| 3275 | flag = 0; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3276 | LY_TREE_FOR(parents->node[i] ? parents->node[i]->child : start, node) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3277 | if (node->schema->module == mod && !strncmp(node->schema->name, name, nam_len) |
| 3278 | && node->schema->name[nam_len] == '\0') { |
| 3279 | /* matching target */ |
| 3280 | if (!flag) { |
Michal Vasko | 9a47e12 | 2015-09-03 14:26:32 +0200 | [diff] [blame] | 3281 | /* put node instead of the current parent */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3282 | parents->node[i] = node; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3283 | flag = 1; |
| 3284 | } else { |
Michal Vasko | 9a47e12 | 2015-09-03 14:26:32 +0200 | [diff] [blame] | 3285 | /* multiple matching, so create a new node */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3286 | ++parents->count; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3287 | parents->node = ly_realloc(parents->node, parents->count * sizeof *parents->node); |
| 3288 | if (!parents->node) { |
| 3289 | return EXIT_FAILURE; |
| 3290 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3291 | parents->node[parents->count-1] = node; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3292 | ++i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3293 | } |
| 3294 | } |
| 3295 | } |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3296 | |
| 3297 | if (!flag) { |
| 3298 | /* remove item from the parents list */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3299 | unres_data_del(parents, i); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3300 | } else { |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3301 | ++i; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3302 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3303 | } |
| 3304 | |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3305 | return parents->count ? EXIT_SUCCESS : EXIT_FAILURE; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3306 | } |
| 3307 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3308 | /** |
| 3309 | * @brief Resolve (find) a data node. Does not log. |
| 3310 | * |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3311 | * @param[in] mod_name Module name of the data node. |
| 3312 | * @param[in] mod_name_len Length of the module name. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3313 | * @param[in] name Name of the data node. |
| 3314 | * @param[in] nam_len Length of the name. |
| 3315 | * @param[in] start Data node to start the search from. |
| 3316 | * @param[in,out] parents Resolved nodes. If there are some parents, |
| 3317 | * they are replaced (!!) with the resolvents. |
| 3318 | * |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3319 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 otherwise. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3320 | */ |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3321 | static int |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3322 | 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] | 3323 | struct unres_data *parents) |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3324 | { |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3325 | const struct lys_module *mod; |
Michal Vasko | 31fc367 | 2015-10-21 12:08:13 +0200 | [diff] [blame] | 3326 | char *str; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3327 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3328 | assert(start); |
| 3329 | |
Michal Vasko | 31fc367 | 2015-10-21 12:08:13 +0200 | [diff] [blame] | 3330 | if (mod_name) { |
| 3331 | /* we have mod_name, find appropriate module */ |
| 3332 | str = strndup(mod_name, mod_name_len); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3333 | if (!str) { |
| 3334 | LOGMEM; |
| 3335 | return -1; |
| 3336 | } |
Michal Vasko | 31fc367 | 2015-10-21 12:08:13 +0200 | [diff] [blame] | 3337 | mod = ly_ctx_get_module(start->schema->module->ctx, str, NULL); |
| 3338 | free(str); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3339 | if (!mod) { |
| 3340 | /* invalid prefix */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3341 | return -1; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3342 | } |
| 3343 | } else { |
| 3344 | /* no prefix, module is the same as of current node */ |
| 3345 | mod = start->schema->module; |
| 3346 | } |
| 3347 | |
| 3348 | return resolve_data(mod, name, name_len, start, parents); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3349 | } |
| 3350 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3351 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3352 | * @brief Resolve a path predicate (leafref) in JSON data context. Logs directly |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3353 | * only specific errors, general no-resolvent error is left to the caller. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3354 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3355 | * @param[in] pred Predicate to use. |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3356 | * @param[in] node Node from which the predicate is being resolved |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3357 | * @param[in,out] node_match Nodes satisfying the restriction |
| 3358 | * without the predicate. Nodes not |
| 3359 | * satisfying the predicate are removed. |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3360 | * @param[out] parsed Number of characters parsed, negative on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3361 | * |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3362 | * @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] | 3363 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3364 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3365 | 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] | 3366 | int *parsed) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3367 | { |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3368 | /* ... /node[source = destination] ... */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3369 | struct unres_data source_match, dest_match; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3370 | const char *path_key_expr, *source, *sour_pref, *dest, *dest_pref; |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3371 | int pke_len, sour_len, sour_pref_len, dest_len, dest_pref_len, parsed_loc = 0, pke_parsed = 0; |
| 3372 | int has_predicate, dest_parent_times, i, rc; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3373 | uint32_t j; |
Radek Krejci | 0c2fa3a | 2016-06-13 15:18:03 +0200 | [diff] [blame] | 3374 | struct lyd_node_leaf_list *leaf_dst, *leaf_src; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3375 | |
| 3376 | source_match.count = 1; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3377 | source_match.node = malloc(sizeof *source_match.node); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3378 | if (!source_match.node) { |
| 3379 | LOGMEM; |
| 3380 | return -1; |
| 3381 | } |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3382 | dest_match.count = 1; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3383 | dest_match.node = malloc(sizeof *dest_match.node); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3384 | if (!dest_match.node) { |
| 3385 | LOGMEM; |
| 3386 | return -1; |
| 3387 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3388 | |
| 3389 | do { |
| 3390 | if ((i = parse_path_predicate(pred, &sour_pref, &sour_pref_len, &source, &sour_len, &path_key_expr, |
| 3391 | &pke_len, &has_predicate)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3392 | LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, pred[-i], &pred[-i]); |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3393 | rc = -1; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3394 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3395 | } |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3396 | parsed_loc += i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3397 | pred += i; |
| 3398 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3399 | for (j = 0; j < node_match->count;) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3400 | /* source */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3401 | source_match.node[0] = node_match->node[j]; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3402 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3403 | /* must be leaf (key of a list) */ |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3404 | 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] | 3405 | &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] | 3406 | i = 0; |
| 3407 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3408 | } |
| 3409 | |
| 3410 | /* destination */ |
Radek Krejci | 0c2fa3a | 2016-06-13 15:18:03 +0200 | [diff] [blame] | 3411 | dest_match.node[0] = node; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3412 | dest_parent_times = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3413 | if ((i = parse_path_key_expr(path_key_expr, &dest_pref, &dest_pref_len, &dest, &dest_len, |
| 3414 | &dest_parent_times)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3415 | 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] | 3416 | rc = -1; |
| 3417 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3418 | } |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3419 | pke_parsed = i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3420 | for (i = 0; i < dest_parent_times; ++i) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3421 | dest_match.node[0] = dest_match.node[0]->parent; |
| 3422 | if (!dest_match.node[0]) { |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3423 | i = 0; |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3424 | rc = EXIT_FAILURE; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3425 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3426 | } |
| 3427 | } |
| 3428 | while (1) { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3429 | 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] | 3430 | &dest_match)) || (dest_match.count != 1)) { |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3431 | i = 0; |
| 3432 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3433 | } |
| 3434 | |
| 3435 | if (pke_len == pke_parsed) { |
| 3436 | break; |
| 3437 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3438 | 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] | 3439 | &dest_parent_times)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3440 | 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] | 3441 | rc = -1; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3442 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3443 | } |
| 3444 | pke_parsed += i; |
| 3445 | } |
| 3446 | |
| 3447 | /* check match between source and destination nodes */ |
Radek Krejci | 0c2fa3a | 2016-06-13 15:18:03 +0200 | [diff] [blame] | 3448 | leaf_dst = (struct lyd_node_leaf_list *)dest_match.node[0]; |
| 3449 | while (leaf_dst->value_type == LY_TYPE_LEAFREF) { |
| 3450 | leaf_dst = (struct lyd_node_leaf_list *)leaf_dst->value.leafref; |
| 3451 | } |
| 3452 | leaf_src = (struct lyd_node_leaf_list *)source_match.node[0]; |
| 3453 | while (leaf_src->value_type == LY_TYPE_LEAFREF) { |
| 3454 | leaf_src = (struct lyd_node_leaf_list *)leaf_src->value.leafref; |
| 3455 | } |
| 3456 | if (leaf_src->value_type != leaf_dst->value_type) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3457 | goto remove_leafref; |
| 3458 | } |
| 3459 | |
Radek Krejci | 0c2fa3a | 2016-06-13 15:18:03 +0200 | [diff] [blame] | 3460 | if (!ly_strequal(leaf_src->value_str, leaf_dst->value_str, 1)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3461 | goto remove_leafref; |
| 3462 | } |
| 3463 | |
| 3464 | /* leafref is ok, continue check with next leafref */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3465 | ++j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3466 | continue; |
| 3467 | |
| 3468 | remove_leafref: |
| 3469 | /* does not fulfill conditions, remove leafref record */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3470 | unres_data_del(node_match, j); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3471 | } |
| 3472 | } while (has_predicate); |
| 3473 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3474 | free(source_match.node); |
| 3475 | free(dest_match.node); |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3476 | if (parsed) { |
| 3477 | *parsed = parsed_loc; |
| 3478 | } |
| 3479 | return EXIT_SUCCESS; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3480 | |
| 3481 | error: |
| 3482 | |
| 3483 | if (source_match.count) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3484 | free(source_match.node); |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3485 | } |
| 3486 | if (dest_match.count) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3487 | free(dest_match.node); |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3488 | } |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3489 | if (parsed) { |
| 3490 | *parsed = -parsed_loc+i; |
| 3491 | } |
| 3492 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3493 | } |
| 3494 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3495 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3496 | * @brief Resolve a path (leafref) in JSON data context. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3497 | * |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3498 | * @param[in] node Leafref data node. |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3499 | * @param[in] path Path of the leafref. |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3500 | * @param[out] ret Matching nodes. Expects an empty, but allocated structure. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3501 | * |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3502 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 otherwise. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3503 | */ |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3504 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3505 | 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] | 3506 | { |
Radek Krejci | 71b795b | 2015-08-10 16:20:39 +0200 | [diff] [blame] | 3507 | struct lyd_node *data = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3508 | const char *prefix, *name; |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3509 | int pref_len, nam_len, has_predicate, parent_times, i, parsed, rc; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3510 | uint32_t j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3511 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3512 | assert(node && path && ret && !ret->count); |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3513 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3514 | parent_times = 0; |
Michal Vasko | d917334 | 2015-08-17 14:35:36 +0200 | [diff] [blame] | 3515 | parsed = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3516 | |
| 3517 | /* searching for nodeset */ |
| 3518 | do { |
Radek Krejci | f7ed4c3 | 2016-10-27 16:20:03 +0200 | [diff] [blame^] | 3519 | if ((i = parse_path_arg(node->schema->module, path, &prefix, &pref_len, &name, &nam_len, &parent_times, &has_predicate)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3520 | LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, path[-i], &path[-i]); |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3521 | rc = -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3522 | goto error; |
| 3523 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3524 | path += i; |
Michal Vasko | d917334 | 2015-08-17 14:35:36 +0200 | [diff] [blame] | 3525 | parsed += i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3526 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3527 | if (!ret->count) { |
Michal Vasko | bfd98e6 | 2016-09-02 09:50:05 +0200 | [diff] [blame] | 3528 | if (parent_times > 0) { |
| 3529 | data = node; |
| 3530 | for (i = 1; i < parent_times; ++i) { |
| 3531 | data = data->parent; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3532 | } |
Michal Vasko | bfd98e6 | 2016-09-02 09:50:05 +0200 | [diff] [blame] | 3533 | } else if (!parent_times) { |
| 3534 | data = node->child; |
| 3535 | } else { |
| 3536 | /* absolute path */ |
| 3537 | for (data = node; data->parent; data = data->parent); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3538 | } |
| 3539 | |
Michal Vasko | bfd98e6 | 2016-09-02 09:50:05 +0200 | [diff] [blame] | 3540 | /* we may still be parsing it and the pointer is not correct yet */ |
| 3541 | if (data->prev) { |
| 3542 | while (data->prev->next) { |
| 3543 | data = data->prev; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 3544 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3545 | } |
| 3546 | } |
| 3547 | |
| 3548 | /* node identifier */ |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3549 | 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] | 3550 | if (rc == -1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3551 | LOGVAL(LYE_INELEM_LEN, LY_VLOG_LYD, node, nam_len, name); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3552 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3553 | goto error; |
| 3554 | } |
| 3555 | |
| 3556 | if (has_predicate) { |
| 3557 | /* we have predicate, so the current results must be lists */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3558 | for (j = 0; j < ret->count;) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3559 | if (ret->node[j]->schema->nodetype == LYS_LIST && |
| 3560 | ((struct lys_node_list *)ret->node[0]->schema)->keys) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3561 | /* leafref is ok, continue check with next leafref */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3562 | ++j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3563 | continue; |
| 3564 | } |
| 3565 | |
| 3566 | /* does not fulfill conditions, remove leafref record */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3567 | unres_data_del(ret, j); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3568 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3569 | if ((rc = resolve_path_predicate_data(path, node, ret, &i))) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 3570 | if (rc == -1) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3571 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYD, node, "leafref", path); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3572 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3573 | goto error; |
| 3574 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3575 | path += i; |
Michal Vasko | d917334 | 2015-08-17 14:35:36 +0200 | [diff] [blame] | 3576 | parsed += i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3577 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3578 | if (!ret->count) { |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3579 | rc = EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3580 | goto error; |
| 3581 | } |
| 3582 | } |
| 3583 | } while (path[0] != '\0'); |
| 3584 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3585 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3586 | |
| 3587 | error: |
| 3588 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3589 | free(ret->node); |
| 3590 | ret->node = NULL; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3591 | ret->count = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3592 | |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3593 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3594 | } |
| 3595 | |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3596 | static int |
| 3597 | resolve_path_arg_schema_valid_dep_flag(const struct lys_node *op_node, const struct lys_node *first_node, int abs_path) |
| 3598 | { |
| 3599 | int dep1, dep2; |
| 3600 | const struct lys_node *node; |
| 3601 | |
| 3602 | if (lys_parent(op_node)) { |
| 3603 | /* inner operation (notif/action) */ |
| 3604 | if (abs_path) { |
| 3605 | return 1; |
| 3606 | } else { |
| 3607 | /* compare depth of both nodes */ |
| 3608 | for (dep1 = 0, node = op_node; lys_parent(node); node = lys_parent(node)); |
| 3609 | for (dep2 = 0, node = first_node; lys_parent(node); node = lys_parent(node)); |
| 3610 | if ((dep2 > dep1) || ((dep2 == dep1) && (op_node != first_node))) { |
| 3611 | return 1; |
| 3612 | } |
| 3613 | } |
| 3614 | } else { |
| 3615 | /* top-level operation (notif/rpc) */ |
| 3616 | if (op_node != first_node) { |
| 3617 | return 1; |
| 3618 | } |
| 3619 | } |
| 3620 | |
| 3621 | return 0; |
| 3622 | } |
| 3623 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3624 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3625 | * @brief Resolve a path (leafref) predicate in JSON schema context. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3626 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3627 | * @param[in] path Path to use. |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3628 | * @param[in] context_node Predicate context node (where the predicate is placed). |
| 3629 | * @param[in] parent Path context node (where the path begins/is placed). |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3630 | * @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] | 3631 | * |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3632 | * @return 0 on forward reference, otherwise the number |
| 3633 | * of characters successfully parsed, |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3634 | * positive on success, negative on failure. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3635 | */ |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3636 | static int |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3637 | 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] | 3638 | struct lys_node *parent, const struct lys_node *op_node) |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3639 | { |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3640 | const struct lys_node *src_node, *dst_node; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3641 | const char *path_key_expr, *source, *sour_pref, *dest, *dest_pref; |
Michal Vasko | f9b35d9 | 2016-10-21 15:19:30 +0200 | [diff] [blame] | 3642 | int pke_len, sour_len, sour_pref_len, dest_len, dest_pref_len, pke_parsed, parsed = 0; |
| 3643 | int has_predicate, dest_parent_times, i, rc, first_iter; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3644 | |
| 3645 | do { |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3646 | 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] | 3647 | &pke_len, &has_predicate)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3648 | 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] | 3649 | return -parsed+i; |
| 3650 | } |
| 3651 | parsed += i; |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3652 | path += i; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3653 | |
Michal Vasko | 5809090 | 2015-08-13 14:04:15 +0200 | [diff] [blame] | 3654 | /* source (must be leaf) */ |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3655 | if (!sour_pref) { |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3656 | sour_pref = context_node->module->name; |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3657 | } |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3658 | 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] | 3659 | LYS_LEAF | LYS_LEAFLIST | LYS_AUGMENT, &src_node); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3660 | if (rc) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3661 | 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] | 3662 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3663 | } |
| 3664 | |
| 3665 | /* destination */ |
Michal Vasko | f9b35d9 | 2016-10-21 15:19:30 +0200 | [diff] [blame] | 3666 | dest_parent_times = 0; |
| 3667 | pke_parsed = 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3668 | if ((i = parse_path_key_expr(path_key_expr, &dest_pref, &dest_pref_len, &dest, &dest_len, |
| 3669 | &dest_parent_times)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3670 | 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] | 3671 | return -parsed; |
| 3672 | } |
| 3673 | pke_parsed += i; |
| 3674 | |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3675 | for (i = 0, dst_node = parent; i < dest_parent_times; ++i) { |
Michal Vasko | fbaead7 | 2016-10-07 10:54:48 +0200 | [diff] [blame] | 3676 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 3677 | * all schema nodes that cannot be instantiated in data tree */ |
| 3678 | for (dst_node = lys_parent(dst_node); |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3679 | 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] | 3680 | dst_node = lys_parent(dst_node)); |
| 3681 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3682 | if (!dst_node) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3683 | 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] | 3684 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3685 | } |
| 3686 | } |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3687 | first_iter = 1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3688 | while (1) { |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3689 | if (!dest_pref) { |
| 3690 | dest_pref = dst_node->module->name; |
| 3691 | } |
| 3692 | 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] | 3693 | LYS_CONTAINER | LYS_LIST | LYS_LEAF | LYS_AUGMENT, &dst_node); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3694 | if (rc) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3695 | 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] | 3696 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3697 | } |
| 3698 | |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3699 | if (first_iter) { |
| 3700 | if (resolve_path_arg_schema_valid_dep_flag(op_node, dst_node, 0)) { |
| 3701 | parent->flags |= LYS_VALID_DEP; |
| 3702 | } |
| 3703 | first_iter = 0; |
| 3704 | } |
| 3705 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3706 | if (pke_len == pke_parsed) { |
| 3707 | break; |
| 3708 | } |
| 3709 | |
| 3710 | if ((i = parse_path_key_expr(path_key_expr+pke_parsed, &dest_pref, &dest_pref_len, &dest, &dest_len, |
| 3711 | &dest_parent_times)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3712 | LOGVAL(LYE_INCHAR, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3713 | (path_key_expr+pke_parsed)[-i], (path_key_expr+pke_parsed)-i); |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3714 | return -parsed; |
| 3715 | } |
| 3716 | pke_parsed += i; |
| 3717 | } |
| 3718 | |
| 3719 | /* check source - dest match */ |
Michal Vasko | 59ad458 | 2016-09-16 13:15:41 +0200 | [diff] [blame] | 3720 | if (dst_node->nodetype != src_node->nodetype) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3721 | 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] | 3722 | LOGVAL(LYE_SPEC, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, "Destination node is not a %s, but a %s.", |
| 3723 | strnodetype(src_node->nodetype), strnodetype(dst_node->nodetype)); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3724 | return -parsed; |
| 3725 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3726 | } while (has_predicate); |
| 3727 | |
| 3728 | return parsed; |
| 3729 | } |
| 3730 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3731 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3732 | * @brief Resolve a path (leafref) in JSON schema context. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3733 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3734 | * @param[in] path Path to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3735 | * @param[in] parent_node Parent of the leafref. |
Radek Krejci | 2f12f85 | 2016-01-08 12:59:57 +0100 | [diff] [blame] | 3736 | * @param[in] parent_tpdf Flag if the parent node is actually typedef, in that case the path |
| 3737 | * has to contain absolute path |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3738 | * @param[out] ret Pointer to the resolved schema node. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3739 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3740 | * @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] | 3741 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3742 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3743 | 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] | 3744 | const struct lys_node **ret) |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3745 | { |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3746 | const struct lys_node *node, *op_node = NULL; |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 3747 | const struct lys_module *mod, *mod2; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3748 | const char *id, *prefix, *name; |
| 3749 | int pref_len, nam_len, parent_times, has_predicate; |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3750 | int i, first_iter, rc; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3751 | |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3752 | first_iter = 1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3753 | parent_times = 0; |
| 3754 | id = path; |
| 3755 | |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3756 | /* find operation schema we are in, if applicable */ |
Michal Vasko | e9914d1 | 2016-10-07 14:32:37 +0200 | [diff] [blame] | 3757 | if (!parent_tpdf) { |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3758 | for (op_node = lys_parent(parent); |
| 3759 | op_node && !(op_node->nodetype & (LYS_ACTION | LYS_NOTIF | LYS_RPC)); |
| 3760 | op_node = lys_parent(op_node)); |
Michal Vasko | e9914d1 | 2016-10-07 14:32:37 +0200 | [diff] [blame] | 3761 | } |
| 3762 | |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 3763 | mod2 = lys_node_module(parent); |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3764 | do { |
Radek Krejci | f7ed4c3 | 2016-10-27 16:20:03 +0200 | [diff] [blame^] | 3765 | if ((i = parse_path_arg(parent->module, id, &prefix, &pref_len, &name, &nam_len, &parent_times, &has_predicate)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3766 | 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] | 3767 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3768 | } |
| 3769 | id += i; |
| 3770 | |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3771 | if (first_iter) { |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3772 | if (parent_times == -1) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 3773 | /* resolve prefix of the module */ |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3774 | mod = lys_get_import_module(parent->module, NULL, 0, prefix, pref_len); |
Radek Krejci | 0fa54e9 | 2016-09-14 14:01:05 +0200 | [diff] [blame] | 3775 | mod = lys_get_implemented_module(mod); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 3776 | /* get start node */ |
| 3777 | node = mod ? mod->data : NULL; |
Michal Vasko | 5809090 | 2015-08-13 14:04:15 +0200 | [diff] [blame] | 3778 | if (!node) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3779 | LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, |
| 3780 | "leafref", path); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3781 | return EXIT_FAILURE; |
Michal Vasko | 5809090 | 2015-08-13 14:04:15 +0200 | [diff] [blame] | 3782 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3783 | } else if (parent_times > 0) { |
Radek Krejci | 2f12f85 | 2016-01-08 12:59:57 +0100 | [diff] [blame] | 3784 | if (parent_tpdf) { |
| 3785 | /* 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] | 3786 | LOGVAL(LYE_NORESOLV, 0, NULL, "leafref", path); |
Radek Krejci | 2f12f85 | 2016-01-08 12:59:57 +0100 | [diff] [blame] | 3787 | return -1; |
| 3788 | } |
| 3789 | |
Michal Vasko | 9445808 | 2016-10-07 14:34:36 +0200 | [diff] [blame] | 3790 | /* we are looking for a sibling of a node, node it's parent (that is why parent_times - 1) */ |
| 3791 | for (i = 0, node = parent; i < parent_times - 1; i++) { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 3792 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 3793 | * all schema nodes that cannot be instantiated in data tree */ |
| 3794 | for (node = lys_parent(node); |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3795 | 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] | 3796 | node = lys_parent(node)); |
| 3797 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3798 | if (!node) { |
Michal Vasko | e9914d1 | 2016-10-07 14:32:37 +0200 | [diff] [blame] | 3799 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3800 | return EXIT_FAILURE; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3801 | } |
| 3802 | } |
Michal Vasko | e01eca5 | 2015-08-13 14:42:02 +0200 | [diff] [blame] | 3803 | } else { |
| 3804 | LOGINT; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3805 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3806 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3807 | } else { |
Michal Vasko | 7dc71d0 | 2016-03-15 10:42:28 +0100 | [diff] [blame] | 3808 | /* move down the tree, if possible */ |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 3809 | if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
Michal Vasko | 2f5aceb | 2016-03-22 10:24:14 +0100 | [diff] [blame] | 3810 | 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] | 3811 | return -1; |
| 3812 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3813 | node = node->child; |
Radek Krejci | 43ccc4c | 2016-10-18 20:40:06 +0200 | [diff] [blame] | 3814 | if (!node) { |
| 3815 | LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, |
| 3816 | "leafref", path); |
| 3817 | return EXIT_FAILURE; |
| 3818 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3819 | } |
| 3820 | |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 3821 | if (!prefix) { |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3822 | prefix = lys_node_module(parent)->name; |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 3823 | } |
| 3824 | |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3825 | 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] | 3826 | if (rc) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3827 | 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] | 3828 | return EXIT_FAILURE; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3829 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3830 | |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3831 | if (first_iter) { |
| 3832 | /* set external dependency flag, we can decide based on the first found node */ |
| 3833 | if (!parent_tpdf && op_node && parent_times && |
| 3834 | resolve_path_arg_schema_valid_dep_flag(op_node, node, (parent_times == -1 ? 1 : 0))) { |
| 3835 | parent->flags |= LYS_VALID_DEP; |
| 3836 | } |
| 3837 | first_iter = 0; |
| 3838 | } |
| 3839 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3840 | if (has_predicate) { |
| 3841 | /* we have predicate, so the current result must be list */ |
| 3842 | if (node->nodetype != LYS_LIST) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3843 | 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] | 3844 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3845 | } |
| 3846 | |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3847 | i = resolve_path_predicate_schema(id, node, parent, op_node); |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 3848 | if (i <= 0) { |
| 3849 | if (i == 0) { |
| 3850 | return EXIT_FAILURE; |
| 3851 | } else { /* i < 0 */ |
| 3852 | return -1; |
| 3853 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3854 | } |
| 3855 | id += i; |
Michal Vasko | f9b35d9 | 2016-10-21 15:19:30 +0200 | [diff] [blame] | 3856 | has_predicate = 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3857 | } |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 3858 | mod = lys_node_module(node); |
| 3859 | if (!mod->implemented && mod != mod2) { |
| 3860 | /* set the module implemented */ |
| 3861 | if (lys_set_implemented(mod)) { |
| 3862 | return -1; |
| 3863 | } |
| 3864 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3865 | } while (id[0]); |
| 3866 | |
Michal Vasko | ca91768 | 2016-07-25 11:00:37 +0200 | [diff] [blame] | 3867 | /* the target must be leaf or leaf-list (in YANG 1.1 only) */ |
| 3868 | 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] | 3869 | 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] | 3870 | LOGVAL(LYE_SPEC, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, |
| 3871 | "Leafref target \"%s\" is not a leaf%s.", path, |
| 3872 | lys_node_module(parent)->version != 2 ? "" : " nor a leaf-list"); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3873 | return -1; |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 3874 | } |
| 3875 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3876 | /* check status */ |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3877 | if (lyp_check_status(parent->flags, parent->module, parent->name, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3878 | node->flags, node->module, node->name, node)) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3879 | return -1; |
| 3880 | } |
| 3881 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3882 | if (ret) { |
| 3883 | *ret = node; |
| 3884 | } |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 3885 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3886 | return EXIT_SUCCESS; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3887 | } |
| 3888 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3889 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3890 | * @brief Resolve instance-identifier predicate in JSON data format. |
| 3891 | * Does not log. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3892 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3893 | * @param[in] pred Predicate to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3894 | * @param[in,out] node_match Nodes matching the restriction without |
| 3895 | * the predicate. Nodes not satisfying |
| 3896 | * the predicate are removed. |
| 3897 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3898 | * @return Number of characters successfully parsed, |
| 3899 | * positive on success, negative on failure. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3900 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3901 | static int |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3902 | resolve_predicate(const char *pred, struct unres_data *node_match) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3903 | { |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3904 | /* ... /node[target = value] ... */ |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3905 | struct lyd_node *target; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3906 | const char *model, *name, *value; |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3907 | 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] | 3908 | uint32_t j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3909 | |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3910 | assert(pred && node_match->count); |
| 3911 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3912 | idx = -1; |
| 3913 | parsed = 0; |
| 3914 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3915 | pred_iter = -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3916 | do { |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3917 | 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] | 3918 | return -parsed+i; |
| 3919 | } |
| 3920 | parsed += i; |
| 3921 | pred += i; |
| 3922 | |
| 3923 | if (isdigit(name[0])) { |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3924 | /* pos */ |
| 3925 | assert(!value); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3926 | idx = atoi(name); |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3927 | } else if (name[0] != '.') { |
| 3928 | /* list keys */ |
| 3929 | if (pred_iter < 0) { |
| 3930 | pred_iter = 1; |
| 3931 | } else { |
| 3932 | ++pred_iter; |
| 3933 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3934 | } |
| 3935 | |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 3936 | for (cur_idx = 1, j = 0; j < node_match->count; ++cur_idx) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3937 | /* target */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3938 | if (name[0] == '.') { |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3939 | /* leaf-list value */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3940 | if (node_match->node[j]->schema->nodetype != LYS_LEAFLIST) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3941 | goto remove_instid; |
| 3942 | } |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3943 | |
| 3944 | target = node_match->node[j]; |
| 3945 | /* check the value */ |
| 3946 | if (strncmp(((struct lyd_node_leaf_list *)target)->value_str, value, val_len) |
| 3947 | || ((struct lyd_node_leaf_list *)target)->value_str[val_len]) { |
| 3948 | goto remove_instid; |
| 3949 | } |
| 3950 | |
| 3951 | } else if (!value) { |
| 3952 | /* keyless list position */ |
| 3953 | if ((node_match->node[j]->schema->nodetype != LYS_LIST) |
| 3954 | || ((struct lys_node_list *)node_match->node[j]->schema)->keys) { |
| 3955 | goto remove_instid; |
| 3956 | } |
| 3957 | |
| 3958 | if (idx != cur_idx) { |
| 3959 | goto remove_instid; |
| 3960 | } |
| 3961 | |
| 3962 | } else { |
| 3963 | /* list key value */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3964 | if (node_match->node[j]->schema->nodetype != LYS_LIST) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3965 | goto remove_instid; |
| 3966 | } |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3967 | |
| 3968 | /* key module must match the list module */ |
| 3969 | if (strncmp(node_match->node[j]->schema->module->name, model, mod_len) |
| 3970 | || node_match->node[j]->schema->module->name[mod_len]) { |
| 3971 | goto remove_instid; |
| 3972 | } |
| 3973 | /* find the key leaf */ |
Michal Vasko | 045182c | 2016-09-09 12:44:07 +0200 | [diff] [blame] | 3974 | 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] | 3975 | if (!target) { |
| 3976 | goto remove_instid; |
| 3977 | } |
| 3978 | if ((struct lys_node_leaf *)target->schema != |
| 3979 | ((struct lys_node_list *)node_match->node[j]->schema)->keys[pred_iter - 1]) { |
| 3980 | goto remove_instid; |
| 3981 | } |
| 3982 | |
| 3983 | /* check the value */ |
| 3984 | if (strncmp(((struct lyd_node_leaf_list *)target)->value_str, value, val_len) |
| 3985 | || ((struct lyd_node_leaf_list *)target)->value_str[val_len]) { |
| 3986 | goto remove_instid; |
| 3987 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3988 | } |
| 3989 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3990 | /* instid is ok, continue check with the next one */ |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3991 | ++j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3992 | continue; |
| 3993 | |
| 3994 | remove_instid: |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3995 | /* does not fulfill conditions, remove instid record */ |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3996 | unres_data_del(node_match, j); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3997 | } |
| 3998 | } while (has_predicate); |
| 3999 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4000 | /* check that all list keys were specified */ |
| 4001 | if ((pred_iter > 0) && node_match->count) { |
Michal Vasko | 045182c | 2016-09-09 12:44:07 +0200 | [diff] [blame] | 4002 | j = 0; |
| 4003 | while (j < node_match->count) { |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4004 | assert(node_match->node[j]->schema->nodetype == LYS_LIST); |
| 4005 | if (pred_iter < ((struct lys_node_list *)node_match->node[j]->schema)->keys_size) { |
| 4006 | /* not enough predicates, just remove the list instance */ |
| 4007 | unres_data_del(node_match, j); |
Michal Vasko | 045182c | 2016-09-09 12:44:07 +0200 | [diff] [blame] | 4008 | } else { |
| 4009 | ++j; |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4010 | } |
| 4011 | } |
| 4012 | |
| 4013 | if (!node_match->count) { |
| 4014 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier is missing some list keys."); |
| 4015 | } |
| 4016 | } |
| 4017 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4018 | return parsed; |
| 4019 | } |
| 4020 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4021 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 4022 | * @brief Resolve instance-identifier in JSON data format. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4023 | * |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 4024 | * @param[in] data Data node where the path is used |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4025 | * @param[in] path Instance-identifier node value. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4026 | * |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4027 | * @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] | 4028 | */ |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 4029 | static struct lyd_node * |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4030 | resolve_instid(struct lyd_node *data, const char *path) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4031 | { |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4032 | int i = 0, j; |
| 4033 | struct lyd_node *result = NULL; |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4034 | const struct lys_module *mod; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4035 | struct ly_ctx *ctx = data->schema->module->ctx; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4036 | const char *model, *name; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4037 | char *str; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4038 | int mod_len, name_len, has_predicate; |
| 4039 | struct unres_data node_match; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4040 | |
| 4041 | memset(&node_match, 0, sizeof node_match); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4042 | |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4043 | /* we need root to resolve absolute path */ |
| 4044 | for (; data->parent; data = data->parent); |
Michal Vasko | db9323c | 2015-10-16 10:32:44 +0200 | [diff] [blame] | 4045 | /* we're still parsing it and the pointer is not correct yet */ |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 4046 | if (data->prev) { |
| 4047 | for (; data->prev->next; data = data->prev); |
| 4048 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4049 | |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4050 | /* search for the instance node */ |
| 4051 | while (path[i]) { |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 4052 | 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] | 4053 | if (j <= 0) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4054 | 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] | 4055 | goto error; |
| 4056 | } |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4057 | i += j; |
Michal Vasko | b387c48 | 2015-08-12 09:32:59 +0200 | [diff] [blame] | 4058 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4059 | str = strndup(model, mod_len); |
| 4060 | if (!str) { |
| 4061 | LOGMEM; |
| 4062 | goto error; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4063 | } |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4064 | mod = ly_ctx_get_module(ctx, str, NULL); |
| 4065 | free(str); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4066 | |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4067 | if (resolve_data(mod, name, name_len, data, &node_match)) { |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4068 | /* no instance exists */ |
| 4069 | return NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4070 | } |
| 4071 | |
| 4072 | if (has_predicate) { |
| 4073 | /* 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] | 4074 | j = resolve_predicate(&path[i], &node_match); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4075 | if (j < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4076 | LOGVAL(LYE_INPRED, LY_VLOG_LYD, data, &path[i-j]); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4077 | goto error; |
| 4078 | } |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4079 | i += j; |
Michal Vasko | b387c48 | 2015-08-12 09:32:59 +0200 | [diff] [blame] | 4080 | |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4081 | if (!node_match.count) { |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4082 | /* no instance exists */ |
| 4083 | return NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4084 | } |
| 4085 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4086 | } |
| 4087 | |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4088 | if (!node_match.count) { |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4089 | /* no instance exists */ |
| 4090 | return NULL; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 4091 | } else if (node_match.count > 1) { |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4092 | /* instance identifier must resolve to a single node */ |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4093 | LOGVAL(LYE_TOOMANY, LY_VLOG_LYD, data, path, "data tree"); |
Michal Vasko | d6adbaa | 2016-04-11 11:01:09 +0200 | [diff] [blame] | 4094 | goto error; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4095 | } else { |
| 4096 | /* we have required result, remember it and cleanup */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4097 | result = node_match.node[0]; |
| 4098 | free(node_match.node); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4099 | return result; |
| 4100 | } |
| 4101 | |
| 4102 | error: |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4103 | /* cleanup */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4104 | free(node_match.node); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4105 | return NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4106 | } |
| 4107 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4108 | /** |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4109 | * @brief Check all XPath expressions of a node (when and must), set LYS_XPATH_DEP flag if required. |
| 4110 | * |
| 4111 | * @param[in] node Node to examine. |
| 4112 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 4113 | */ |
| 4114 | static int |
| 4115 | check_node_xpath(struct lys_node *node) |
| 4116 | { |
| 4117 | struct lys_node *parent, *elem; |
| 4118 | struct lyxp_set set; |
| 4119 | uint32_t i; |
| 4120 | int rc; |
| 4121 | |
| 4122 | parent = node; |
| 4123 | while (parent) { |
| 4124 | if (parent->nodetype == LYS_GROUPING) { |
| 4125 | /* unresolved grouping, skip for now (will be checked later) */ |
| 4126 | return EXIT_SUCCESS; |
| 4127 | } |
| 4128 | if (parent->nodetype == LYS_AUGMENT) { |
| 4129 | if (!((struct lys_node_augment *)parent)->target) { |
| 4130 | /* uresolved augment, skip for now (will be checked later) */ |
| 4131 | return EXIT_SUCCESS; |
| 4132 | } else { |
| 4133 | parent = ((struct lys_node_augment *)parent)->target; |
| 4134 | continue; |
| 4135 | } |
| 4136 | } |
| 4137 | parent = parent->parent; |
| 4138 | } |
| 4139 | |
| 4140 | rc = lyxp_node_atomize(node, &set); |
| 4141 | if (rc) { |
| 4142 | return rc; |
| 4143 | } |
| 4144 | |
| 4145 | for (parent = node; parent && !(parent->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); parent = lys_parent(parent)); |
| 4146 | |
| 4147 | for (i = 0; i < set.used; ++i) { |
| 4148 | /* skip roots'n'stuff */ |
| 4149 | if (set.val.snodes[i].type == LYXP_NODE_ELEM) { |
| 4150 | /* XPath expression cannot reference "lower" status than the node that has the definition */ |
| 4151 | if (lyp_check_status(node->flags, lys_node_module(node), node->name, set.val.snodes[i].snode->flags, |
| 4152 | lys_node_module(set.val.snodes[i].snode), set.val.snodes[i].snode->name, node)) { |
| 4153 | return -1; |
| 4154 | } |
| 4155 | |
| 4156 | if (parent) { |
| 4157 | for (elem = set.val.snodes[i].snode; elem && (elem != parent); elem = lys_parent(elem)); |
| 4158 | if (!elem) { |
| 4159 | /* not in node's RPC or notification subtree, set the flag */ |
| 4160 | node->flags |= LYS_VALID_DEP; |
| 4161 | break; |
| 4162 | } |
| 4163 | } |
| 4164 | } |
| 4165 | } |
| 4166 | |
| 4167 | free(set.val.snodes); |
| 4168 | return EXIT_SUCCESS; |
| 4169 | } |
| 4170 | |
| 4171 | /** |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4172 | * @brief Passes config flag down to children, skips nodes without config flags. |
| 4173 | * Does not log. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4174 | * |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4175 | * @param[in] node Siblings and their children to have flags changed. |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4176 | * @param[in] clear Flag to clear all config flags if parent is LYS_NOTIF, LYS_INPUT, LYS_OUTPUT, LYS_RPC. |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4177 | * @param[in] flags Flags to assign to all the nodes. |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4178 | * |
| 4179 | * @return 0 on success, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4180 | */ |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4181 | static int |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4182 | 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] | 4183 | { |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4184 | assert(!(flags ^ (flags & LYS_CONFIG_MASK))); |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4185 | LY_TREE_FOR(node, node) { |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4186 | if (check_xpath && check_node_xpath(node)) { |
| 4187 | return -1; |
| 4188 | } |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4189 | if (clear) { |
| 4190 | node->flags &= ~LYS_CONFIG_MASK; |
Michal Vasko | c2a8d36 | 2016-09-29 08:50:13 +0200 | [diff] [blame] | 4191 | node->flags &= ~LYS_CONFIG_SET; |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4192 | } else { |
| 4193 | if (node->flags & LYS_CONFIG_SET) { |
| 4194 | /* skip nodes with an explicit config value */ |
| 4195 | if ((flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) { |
| 4196 | LOGVAL(LYE_INARG, LY_VLOG_LYS, node, "true", "config"); |
| 4197 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, node, "State nodes cannot have configuration nodes as children."); |
| 4198 | return -1; |
| 4199 | } |
| 4200 | continue; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4201 | } |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4202 | |
| 4203 | if (!(node->nodetype & (LYS_USES | LYS_GROUPING))) { |
| 4204 | node->flags = (node->flags & ~LYS_CONFIG_MASK) | flags; |
| 4205 | /* check that configuration lists have keys */ |
| 4206 | if (check_list && (node->nodetype == LYS_LIST) |
| 4207 | && (node->flags & LYS_CONFIG_W) && !((struct lys_node_list *)node)->keys_size) { |
| 4208 | LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_LYS, node, "key", "list"); |
| 4209 | return -1; |
| 4210 | } |
| 4211 | } |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4212 | } |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4213 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) { |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4214 | if (inherit_config_flag(node->child, flags, clear, check_list, check_xpath)) { |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4215 | return -1; |
| 4216 | } |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 4217 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4218 | } |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4219 | |
| 4220 | return 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4221 | } |
| 4222 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4223 | /** |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 4224 | * @brief Resolve augment target. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4225 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4226 | * @param[in] aug Augment to use. |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 4227 | * @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] | 4228 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4229 | * @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] | 4230 | */ |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 4231 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4232 | resolve_augment(struct lys_node_augment *aug, struct lys_node *siblings) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4233 | { |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4234 | int rc, clear_config; |
Michal Vasko | 1d87a92 | 2015-08-21 12:57:16 +0200 | [diff] [blame] | 4235 | struct lys_node *sub; |
Pavol Vican | 4731993 | 2016-08-29 09:14:47 +0200 | [diff] [blame] | 4236 | const struct lys_node *aug_target, *parent; |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4237 | struct lys_module *mod; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4238 | |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4239 | assert(aug && !aug->target); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4240 | |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4241 | /* resolve target node */ |
| 4242 | rc = resolve_augment_schema_nodeid(aug->target_name, siblings, (siblings ? NULL : aug->module), &aug_target); |
| 4243 | if (rc == -1) { |
| 4244 | return -1; |
| 4245 | } |
| 4246 | if (rc > 0) { |
| 4247 | LOGVAL(LYE_INCHAR, LY_VLOG_LYS, aug, aug->target_name[rc - 1], &aug->target_name[rc - 1]); |
| 4248 | return -1; |
| 4249 | } |
| 4250 | if (!aug_target) { |
| 4251 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, aug, "augment", aug->target_name); |
| 4252 | return EXIT_FAILURE; |
| 4253 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4254 | |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4255 | /* check that we want to connect augment into its target */ |
| 4256 | mod = lys_main_module(aug->module); |
| 4257 | if (!mod->implemented) { |
| 4258 | /* it must be augment only to the same module, |
| 4259 | * otherwise we do not apply augment in not-implemented |
| 4260 | * module. If the module is set to be implemented in future, |
| 4261 | * the augment is being resolved and checked again */ |
| 4262 | for (sub = aug->target; sub; sub = lys_parent(sub)) { |
| 4263 | if (lys_node_module(sub) != mod) { |
| 4264 | /* this is not an implemented module and the augment |
| 4265 | * target some other module, so avoid its connecting |
| 4266 | * to the target */ |
| 4267 | return EXIT_SUCCESS; |
| 4268 | } |
| 4269 | } |
| 4270 | } |
| 4271 | |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4272 | if (!aug->child) { |
| 4273 | /* nothing to do */ |
| 4274 | LOGWRN("Augment \"%s\" without children.", aug->target_name); |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4275 | goto success; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4276 | } |
| 4277 | |
Michal Vasko | d58d596 | 2016-03-02 14:29:41 +0100 | [diff] [blame] | 4278 | /* check for mandatory nodes - if the target node is in another module |
| 4279 | * the added nodes cannot be mandatory |
| 4280 | */ |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4281 | 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] | 4282 | && (rc = lyp_check_mandatory_augment(aug))) { |
| 4283 | return rc; |
Michal Vasko | d58d596 | 2016-03-02 14:29:41 +0100 | [diff] [blame] | 4284 | } |
| 4285 | |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4286 | /* check augment target type and then augment nodes type */ |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4287 | 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] | 4288 | LY_TREE_FOR(aug->child, sub) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4289 | 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] | 4290 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment"); |
| 4291 | 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] | 4292 | strnodetype(aug_target->nodetype), strnodetype(sub->nodetype)); |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4293 | return -1; |
| 4294 | } |
| 4295 | } |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4296 | } else if (aug_target->nodetype == LYS_CHOICE) { |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4297 | LY_TREE_FOR(aug->child, sub) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4298 | 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] | 4299 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment"); |
| 4300 | 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] | 4301 | strnodetype(aug_target->nodetype), strnodetype(sub->nodetype)); |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4302 | return -1; |
| 4303 | } |
| 4304 | } |
| 4305 | } else { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4306 | LOGVAL(LYE_INARG, LY_VLOG_LYS, aug, aug->target_name, "target-node"); |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4307 | 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] | 4308 | return -1; |
| 4309 | } |
| 4310 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 4311 | /* check identifier uniqueness as in lys_node_addchild() */ |
Michal Vasko | 1d87a92 | 2015-08-21 12:57:16 +0200 | [diff] [blame] | 4312 | LY_TREE_FOR(aug->child, sub) { |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4313 | if (lys_check_id(sub, (struct lys_node *)aug_target, NULL)) { |
Michal Vasko | 3e6665f | 2015-08-17 14:00:38 +0200 | [diff] [blame] | 4314 | return -1; |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 4315 | } |
| 4316 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4317 | |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4318 | /* finally reconnect augmenting data into the target - add them to the target child list, |
| 4319 | * by setting aug->target we know the augment is fully resolved now */ |
| 4320 | aug->target = (struct lys_node *)aug_target; |
| 4321 | if (aug->target->child) { |
| 4322 | sub = aug->target->child->prev; /* remember current target's last node */ |
| 4323 | sub->next = aug->child; /* connect augmenting data after target's last node */ |
| 4324 | aug->target->child->prev = aug->child->prev; /* new target's last node is last augmenting node */ |
| 4325 | aug->child->prev = sub; /* finish connecting of both child lists */ |
| 4326 | } else { |
| 4327 | aug->target->child = aug->child; |
| 4328 | } |
| 4329 | |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4330 | /* inherit config information from actual parent */ |
| 4331 | for(parent = aug_target; parent && !(parent->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT | LYS_RPC)); parent = lys_parent(parent)); |
| 4332 | clear_config = (parent) ? 1 : 0; |
| 4333 | LY_TREE_FOR(aug->child, sub) { |
| 4334 | if (inherit_config_flag(sub, aug_target->flags & LYS_CONFIG_MASK, clear_config, 1, 1)) { |
| 4335 | return -1; |
| 4336 | } |
| 4337 | } |
| 4338 | |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4339 | success: |
| 4340 | if (mod->implemented) { |
| 4341 | /* make target modules also implemented */ |
| 4342 | for (sub = aug->target; sub; sub = lys_parent(sub)) { |
| 4343 | if (lys_set_implemented(sub->module)) { |
| 4344 | return -1; |
| 4345 | } |
| 4346 | } |
| 4347 | } |
| 4348 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4349 | return EXIT_SUCCESS; |
| 4350 | } |
| 4351 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4352 | /** |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4353 | * @brief Resolve (find) choice default case. Does not log. |
| 4354 | * |
| 4355 | * @param[in] choic Choice to use. |
| 4356 | * @param[in] dflt Name of the default case. |
| 4357 | * |
| 4358 | * @return Pointer to the default node or NULL. |
| 4359 | */ |
| 4360 | static struct lys_node * |
| 4361 | resolve_choice_dflt(struct lys_node_choice *choic, const char *dflt) |
| 4362 | { |
| 4363 | struct lys_node *child, *ret; |
| 4364 | |
| 4365 | LY_TREE_FOR(choic->child, child) { |
| 4366 | if (child->nodetype == LYS_USES) { |
| 4367 | ret = resolve_choice_dflt((struct lys_node_choice *)child, dflt); |
| 4368 | if (ret) { |
| 4369 | return ret; |
| 4370 | } |
| 4371 | } |
| 4372 | |
| 4373 | 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] | 4374 | | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CHOICE))) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4375 | return child; |
| 4376 | } |
| 4377 | } |
| 4378 | |
| 4379 | return NULL; |
| 4380 | } |
| 4381 | |
| 4382 | /** |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4383 | * @brief Resolve uses, apply augments, refines. Logs directly. |
| 4384 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4385 | * @param[in] uses Uses to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4386 | * @param[in,out] unres List of unresolved items. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4387 | * |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 4388 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4389 | */ |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 4390 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4391 | resolve_uses(struct lys_node_uses *uses, struct unres_schema *unres) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4392 | { |
| 4393 | struct ly_ctx *ctx; |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4394 | struct lys_node *node = NULL, *next, *iter, **refine_nodes = NULL; |
Pavol Vican | 55abd33 | 2016-07-12 15:54:49 +0200 | [diff] [blame] | 4395 | struct lys_node *node_aux, *parent, *tmp; |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4396 | struct lys_node_leaflist *llist; |
| 4397 | struct lys_node_leaf *leaf; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 4398 | struct lys_refine *rfn; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4399 | struct lys_restr *must, **old_must; |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4400 | struct lys_iffeature *iff, **old_iff; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4401 | 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] | 4402 | uint8_t size, *old_size; |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4403 | unsigned int usize, usize1, usize2; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4404 | |
Michal Vasko | 71e1aa8 | 2015-08-12 12:17:51 +0200 | [diff] [blame] | 4405 | assert(uses->grp); |
Michal Vasko | e770851 | 2016-03-11 10:26:55 +0100 | [diff] [blame] | 4406 | /* HACK just check that the grouping is resolved */ |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 4407 | assert(!uses->grp->nacm); |
Michal Vasko | 71e1aa8 | 2015-08-12 12:17:51 +0200 | [diff] [blame] | 4408 | |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4409 | if (!uses->grp->child) { |
| 4410 | /* grouping without children, warning was already displayed */ |
| 4411 | return EXIT_SUCCESS; |
| 4412 | } |
| 4413 | |
| 4414 | /* get proper parent (config) flags */ |
| 4415 | for (node_aux = lys_parent((struct lys_node *)uses); node_aux && (node_aux->nodetype == LYS_USES); node_aux = lys_parent(node_aux)); |
| 4416 | if (node_aux) { |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4417 | parent_config = node_aux->flags & LYS_CONFIG_MASK; |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4418 | } else { |
| 4419 | /* default */ |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4420 | parent_config = LYS_CONFIG_W; |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4421 | } |
| 4422 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4423 | /* copy the data nodes from grouping into the uses context */ |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 4424 | LY_TREE_FOR(uses->grp->child, node_aux) { |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4425 | 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] | 4426 | if (!node) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4427 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, uses->grp->name, "uses"); |
| 4428 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, "Copying data from grouping failed."); |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4429 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4430 | } |
Pavol Vican | 55abd33 | 2016-07-12 15:54:49 +0200 | [diff] [blame] | 4431 | /* test the name of siblings */ |
| 4432 | 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] | 4433 | 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] | 4434 | goto fail; |
Pavol Vican | 55abd33 | 2016-07-12 15:54:49 +0200 | [diff] [blame] | 4435 | } |
| 4436 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4437 | } |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4438 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4439 | ctx = uses->module->ctx; |
Michal Vasko | 4bc590c | 2016-09-30 12:19:51 +0200 | [diff] [blame] | 4440 | |
| 4441 | parent = node; |
| 4442 | while (parent && !(parent->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT | LYS_RPC | LYS_GROUPING))) { |
| 4443 | if (parent->nodetype == LYS_AUGMENT) { |
| 4444 | if (!((struct lys_node_augment *)parent)->target) { |
| 4445 | break; |
| 4446 | } else { |
| 4447 | parent = ((struct lys_node_augment *)parent)->target; |
| 4448 | } |
| 4449 | } else { |
| 4450 | parent = parent->parent; |
| 4451 | } |
| 4452 | } |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4453 | if (parent) { |
Michal Vasko | 4bc590c | 2016-09-30 12:19:51 +0200 | [diff] [blame] | 4454 | if (parent->nodetype & (LYS_GROUPING | LYS_AUGMENT)) { |
| 4455 | /* 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] | 4456 | check_list = 0; |
| 4457 | clear_config = 0; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4458 | check_xpath = 0; |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4459 | } else { |
| 4460 | check_list = 0; |
| 4461 | clear_config = 1; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4462 | check_xpath = 1; |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4463 | } |
| 4464 | } else { |
| 4465 | check_list = 1; |
| 4466 | clear_config = 0; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4467 | check_xpath = 1; |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4468 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4469 | |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4470 | if (parent_config) { |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4471 | assert(uses->child); |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4472 | 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] | 4473 | goto fail; |
| 4474 | } |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4475 | } |
| 4476 | |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 4477 | /* we managed to copy the grouping, the rest must be possible to resolve */ |
| 4478 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4479 | if (uses->refine_size) { |
| 4480 | refine_nodes = malloc(uses->refine_size * sizeof *refine_nodes); |
| 4481 | if (!refine_nodes) { |
| 4482 | LOGMEM; |
| 4483 | goto fail; |
| 4484 | } |
| 4485 | } |
| 4486 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4487 | /* apply refines */ |
| 4488 | for (i = 0; i < uses->refine_size; i++) { |
| 4489 | rfn = &uses->refine[i]; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 4490 | 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] | 4491 | 1, 0, (const struct lys_node **)&node); |
Michal Vasko | 9bb061b | 2016-02-12 11:00:19 +0100 | [diff] [blame] | 4492 | if (rc || !node) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4493 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine"); |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4494 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4495 | } |
| 4496 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4497 | if (rfn->target_type && !(node->nodetype & rfn->target_type)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4498 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine"); |
| 4499 | 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] | 4500 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4501 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4502 | refine_nodes[i] = node; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4503 | |
| 4504 | /* description on any nodetype */ |
| 4505 | if (rfn->dsc) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4506 | lydict_remove(ctx, node->dsc); |
| 4507 | node->dsc = lydict_insert(ctx, rfn->dsc, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4508 | } |
| 4509 | |
| 4510 | /* reference on any nodetype */ |
| 4511 | if (rfn->ref) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4512 | lydict_remove(ctx, node->ref); |
| 4513 | node->ref = lydict_insert(ctx, rfn->ref, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4514 | } |
| 4515 | |
| 4516 | /* config on any nodetype */ |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4517 | if ((rfn->flags & LYS_CONFIG_MASK) && !clear_config) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4518 | node->flags &= ~LYS_CONFIG_MASK; |
| 4519 | node->flags |= (rfn->flags & LYS_CONFIG_MASK); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4520 | } |
| 4521 | |
| 4522 | /* default value ... */ |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4523 | if (rfn->dflt_size) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4524 | if (node->nodetype == LYS_LEAF) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4525 | /* leaf */ |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4526 | leaf = (struct lys_node_leaf *)node; |
| 4527 | |
| 4528 | lydict_remove(ctx, leaf->dflt); |
| 4529 | leaf->dflt = lydict_insert(ctx, rfn->dflt[0], 0); |
| 4530 | |
| 4531 | /* check the default value */ |
| 4532 | 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] | 4533 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4534 | } |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4535 | } else if (node->nodetype == LYS_LEAFLIST) { |
| 4536 | /* leaf-list */ |
| 4537 | llist = (struct lys_node_leaflist *)node; |
| 4538 | |
| 4539 | /* remove complete set of defaults in target */ |
| 4540 | for (i = 0; i < llist->dflt_size; i++) { |
| 4541 | lydict_remove(ctx, llist->dflt[i]); |
| 4542 | } |
| 4543 | free(llist->dflt); |
| 4544 | |
| 4545 | /* copy the default set from refine */ |
| 4546 | llist->dflt_size = rfn->dflt_size; |
| 4547 | llist->dflt = malloc(llist->dflt_size * sizeof *llist->dflt); |
| 4548 | for (i = 0; i < llist->dflt_size; i++) { |
| 4549 | llist->dflt[i] = lydict_insert(ctx, rfn->dflt[i], 0); |
| 4550 | } |
| 4551 | |
| 4552 | /* check default value */ |
| 4553 | for (i = 0; i < llist->dflt_size; i++) { |
| 4554 | 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] | 4555 | goto fail; |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4556 | } |
| 4557 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4558 | } |
| 4559 | } |
| 4560 | |
| 4561 | /* mandatory on leaf, anyxml or choice */ |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 4562 | if (rfn->flags & LYS_MAND_MASK) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4563 | if (node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_CHOICE)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4564 | /* remove current value */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4565 | node->flags &= ~LYS_MAND_MASK; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4566 | |
| 4567 | /* set new value */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4568 | node->flags |= (rfn->flags & LYS_MAND_MASK); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4569 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4570 | if (rfn->flags & LYS_MAND_TRUE) { |
| 4571 | /* check if node has default value */ |
| 4572 | if ((node->nodetype & LYS_LEAF) && ((struct lys_node_leaf *)node)->dflt) { |
| 4573 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The \"mandatory\" statement is forbidden on leaf with \"default\"."); |
| 4574 | goto fail; |
| 4575 | } |
| 4576 | if ((node->nodetype & LYS_CHOICE) && ((struct lys_node_choice *)node)->dflt) { |
| 4577 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The \"mandatory\" statement is forbidden on choices with \"default\"."); |
| 4578 | goto fail; |
| 4579 | } |
| 4580 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4581 | } |
| 4582 | |
| 4583 | /* presence on container */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4584 | if ((node->nodetype & LYS_CONTAINER) && rfn->mod.presence) { |
| 4585 | lydict_remove(ctx, ((struct lys_node_container *)node)->presence); |
| 4586 | ((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] | 4587 | } |
| 4588 | |
| 4589 | /* min/max-elements on list or leaf-list */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4590 | if (node->nodetype == LYS_LIST) { |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 4591 | if (rfn->flags & LYS_RFN_MINSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4592 | ((struct lys_node_list *)node)->min = rfn->mod.list.min; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4593 | } |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 4594 | if (rfn->flags & LYS_RFN_MAXSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4595 | ((struct lys_node_list *)node)->max = rfn->mod.list.max; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4596 | } |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4597 | } else if (node->nodetype == LYS_LEAFLIST) { |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 4598 | if (rfn->flags & LYS_RFN_MINSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4599 | ((struct lys_node_leaflist *)node)->min = rfn->mod.list.min; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4600 | } |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 4601 | if (rfn->flags & LYS_RFN_MAXSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4602 | ((struct lys_node_leaflist *)node)->max = rfn->mod.list.max; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4603 | } |
| 4604 | } |
| 4605 | |
| 4606 | /* must in leaf, leaf-list, list, container or anyxml */ |
| 4607 | if (rfn->must_size) { |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4608 | switch (node->nodetype) { |
| 4609 | case LYS_LEAF: |
| 4610 | old_size = &((struct lys_node_leaf *)node)->must_size; |
| 4611 | old_must = &((struct lys_node_leaf *)node)->must; |
| 4612 | break; |
| 4613 | case LYS_LEAFLIST: |
| 4614 | old_size = &((struct lys_node_leaflist *)node)->must_size; |
| 4615 | old_must = &((struct lys_node_leaflist *)node)->must; |
| 4616 | break; |
| 4617 | case LYS_LIST: |
| 4618 | old_size = &((struct lys_node_list *)node)->must_size; |
| 4619 | old_must = &((struct lys_node_list *)node)->must; |
| 4620 | break; |
| 4621 | case LYS_CONTAINER: |
| 4622 | old_size = &((struct lys_node_container *)node)->must_size; |
| 4623 | old_must = &((struct lys_node_container *)node)->must; |
| 4624 | break; |
| 4625 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4626 | case LYS_ANYDATA: |
| 4627 | old_size = &((struct lys_node_anydata *)node)->must_size; |
| 4628 | old_must = &((struct lys_node_anydata *)node)->must; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4629 | break; |
| 4630 | default: |
| 4631 | LOGINT; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4632 | goto fail; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4633 | } |
| 4634 | |
| 4635 | size = *old_size + rfn->must_size; |
| 4636 | must = realloc(*old_must, size * sizeof *rfn->must); |
| 4637 | if (!must) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4638 | LOGMEM; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4639 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4640 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4641 | for (k = 0, j = *old_size; k < rfn->must_size; k++, j++) { |
| 4642 | must[j].expr = lydict_insert(ctx, rfn->must[k].expr, 0); |
| 4643 | must[j].dsc = lydict_insert(ctx, rfn->must[k].dsc, 0); |
| 4644 | must[j].ref = lydict_insert(ctx, rfn->must[k].ref, 0); |
| 4645 | must[j].eapptag = lydict_insert(ctx, rfn->must[k].eapptag, 0); |
| 4646 | must[j].emsg = lydict_insert(ctx, rfn->must[k].emsg, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4647 | } |
| 4648 | |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4649 | *old_must = must; |
| 4650 | *old_size = size; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 4651 | |
| 4652 | /* check XPath dependencies again */ |
| 4653 | if (unres_schema_add_node(node->module, unres, node, UNRES_XPATH, NULL) == -1) { |
| 4654 | goto fail; |
| 4655 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4656 | } |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4657 | |
| 4658 | /* if-feature in leaf, leaf-list, list, container or anyxml */ |
| 4659 | if (rfn->iffeature_size) { |
| 4660 | old_size = &node->iffeature_size; |
| 4661 | old_iff = &node->iffeature; |
| 4662 | |
| 4663 | size = *old_size + rfn->iffeature_size; |
| 4664 | iff = realloc(*old_iff, size * sizeof *rfn->iffeature); |
| 4665 | if (!iff) { |
| 4666 | LOGMEM; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4667 | goto fail; |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4668 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4669 | for (k = 0, j = *old_size; k < rfn->iffeature_size; k++, j++) { |
| 4670 | resolve_iffeature_getsizes(&rfn->iffeature[k], &usize1, &usize2); |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4671 | if (usize1) { |
| 4672 | /* there is something to duplicate */ |
| 4673 | /* duplicate compiled expression */ |
| 4674 | usize = (usize1 / 4) + (usize1 % 4) ? 1 : 0; |
| 4675 | iff[j].expr = malloc(usize * sizeof *iff[j].expr); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4676 | 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] | 4677 | |
| 4678 | /* duplicate list of feature pointers */ |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4679 | iff[j].features = malloc(usize2 * sizeof *iff[k].features); |
| 4680 | 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] | 4681 | } |
| 4682 | } |
| 4683 | |
| 4684 | *old_iff = iff; |
| 4685 | *old_size = size; |
| 4686 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4687 | } |
| 4688 | |
| 4689 | /* apply augments */ |
| 4690 | for (i = 0; i < uses->augment_size; i++) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4691 | rc = resolve_augment(&uses->augment[i], uses->child); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4692 | if (rc) { |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4693 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4694 | } |
| 4695 | } |
| 4696 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4697 | /* check refines */ |
| 4698 | for (i = 0; i < uses->refine_size; i++) { |
| 4699 | node = refine_nodes[i]; |
| 4700 | rfn = &uses->refine[i]; |
| 4701 | |
| 4702 | /* config on any nodetype */ |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4703 | if ((rfn->flags & LYS_CONFIG_MASK) && !clear_config) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4704 | for (parent = lys_parent(node); parent && parent->nodetype == LYS_USES; parent = lys_parent(parent)); |
| 4705 | if (parent && parent->nodetype != LYS_GROUPING && |
| 4706 | ((parent->flags & LYS_CONFIG_MASK) != (rfn->flags & LYS_CONFIG_MASK)) && |
| 4707 | (rfn->flags & LYS_CONFIG_W)) { |
| 4708 | /* setting config true under config false is prohibited */ |
| 4709 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, "config", "refine"); |
| 4710 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, |
| 4711 | "changing config from 'false' to 'true' is prohibited while " |
| 4712 | "the target's parent is still config 'false'."); |
| 4713 | goto fail; |
| 4714 | } |
| 4715 | |
| 4716 | /* inherit config change to the target children */ |
| 4717 | LY_TREE_DFS_BEGIN(node->child, next, iter) { |
| 4718 | if (rfn->flags & LYS_CONFIG_W) { |
| 4719 | if (iter->flags & LYS_CONFIG_SET) { |
| 4720 | /* config is set explicitely, go to next sibling */ |
| 4721 | next = NULL; |
| 4722 | goto nextsibling; |
| 4723 | } |
| 4724 | } else { /* LYS_CONFIG_R */ |
| 4725 | if ((iter->flags & LYS_CONFIG_SET) && (iter->flags & LYS_CONFIG_W)) { |
| 4726 | /* error - we would have config data under status data */ |
| 4727 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, "config", "refine"); |
| 4728 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, |
| 4729 | "changing config from 'true' to 'false' is prohibited while the target " |
| 4730 | "has still a children with explicit config 'true'."); |
| 4731 | goto fail; |
| 4732 | } |
| 4733 | } |
| 4734 | /* change config */ |
| 4735 | iter->flags &= ~LYS_CONFIG_MASK; |
| 4736 | iter->flags |= (rfn->flags & LYS_CONFIG_MASK); |
| 4737 | |
| 4738 | /* select next iter - modified LY_TREE_DFS_END */ |
| 4739 | if (iter->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
| 4740 | next = NULL; |
| 4741 | } else { |
| 4742 | next = iter->child; |
| 4743 | } |
| 4744 | nextsibling: |
| 4745 | if (!next) { |
| 4746 | /* try siblings */ |
| 4747 | next = iter->next; |
| 4748 | } |
| 4749 | while (!next) { |
| 4750 | /* parent is already processed, go to its sibling */ |
| 4751 | iter = lys_parent(iter); |
| 4752 | |
| 4753 | /* no siblings, go back through parents */ |
| 4754 | if (iter == node) { |
| 4755 | /* we are done, no next element to process */ |
| 4756 | break; |
| 4757 | } |
| 4758 | next = iter->next; |
| 4759 | } |
| 4760 | } |
| 4761 | } |
| 4762 | |
| 4763 | /* default value */ |
| 4764 | if (rfn->dflt_size && node->nodetype == LYS_CHOICE) { |
| 4765 | /* choice */ |
| 4766 | |
| 4767 | ((struct lys_node_choice *)node)->dflt = resolve_choice_dflt((struct lys_node_choice *)node, |
| 4768 | rfn->dflt[0]); |
| 4769 | if (!((struct lys_node_choice *)node)->dflt) { |
| 4770 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->dflt[0], "default"); |
| 4771 | goto fail; |
| 4772 | } |
| 4773 | if (lyp_check_mandatory_choice(node)) { |
| 4774 | goto fail; |
| 4775 | } |
| 4776 | } |
| 4777 | |
| 4778 | /* min/max-elements on list or leaf-list */ |
| 4779 | if (node->nodetype == LYS_LIST) { |
| 4780 | if (((struct lys_node_list *)node)->min > ((struct lys_node_list *)node)->max) { |
| 4781 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid value \"%d\" of \"%s\".", rfn->mod.list.min, "min-elements"); |
| 4782 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"min-elements\" is bigger than \"max-elements\"."); |
| 4783 | goto fail; |
| 4784 | } |
| 4785 | } else if (node->nodetype == LYS_LEAFLIST) { |
| 4786 | if (((struct lys_node_leaflist *)node)->min > ((struct lys_node_leaflist *)node)->max) { |
| 4787 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid value \"%d\" of \"%s\".", rfn->mod.list.min, "min-elements"); |
| 4788 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"min-elements\" is bigger than \"max-elements\"."); |
| 4789 | goto fail; |
| 4790 | } |
| 4791 | } |
| 4792 | |
| 4793 | /* additional checks */ |
| 4794 | if (node->nodetype == LYS_LEAFLIST) { |
| 4795 | llist = (struct lys_node_leaflist *)node; |
| 4796 | if (llist->dflt_size && llist->min) { |
| 4797 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, rfn->dflt_size ? "default" : "min-elements", "refine"); |
| 4798 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, |
| 4799 | "The \"min-elements\" statement with non-zero value is forbidden on leaf-lists with the \"default\" statement."); |
| 4800 | goto fail; |
| 4801 | } |
| 4802 | } |
| 4803 | if ((rfn->flags & LYS_MAND_TRUE) || rfn->mod.list.min) { |
| 4804 | /* check for mandatory node in default case, first find the closest parent choice to the changed node */ |
| 4805 | for (parent = node->parent; |
| 4806 | parent && !(parent->nodetype & (LYS_CHOICE | LYS_GROUPING | LYS_ACTION | LYS_USES)); |
| 4807 | parent = parent->parent) { |
| 4808 | if (parent->nodetype == LYS_CONTAINER && ((struct lys_node_container *)parent)->presence) { |
| 4809 | /* stop also on presence containers */ |
| 4810 | break; |
| 4811 | } |
| 4812 | } |
| 4813 | /* and if it is a choice with the default case, check it for presence of a mandatory node in it */ |
| 4814 | if (parent && parent->nodetype == LYS_CHOICE && ((struct lys_node_choice *)parent)->dflt) { |
| 4815 | if (lyp_check_mandatory_choice(parent)) { |
| 4816 | goto fail; |
| 4817 | } |
| 4818 | } |
| 4819 | } |
| 4820 | } |
| 4821 | free(refine_nodes); |
| 4822 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4823 | return EXIT_SUCCESS; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4824 | |
| 4825 | fail: |
| 4826 | LY_TREE_FOR_SAFE(uses->child, next, iter) { |
| 4827 | lys_node_free(iter, NULL, 0); |
| 4828 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4829 | free(refine_nodes); |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4830 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4831 | } |
| 4832 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4833 | static int |
| 4834 | identity_backlink_update(struct lys_ident *der, struct lys_ident *base) |
| 4835 | { |
| 4836 | int i; |
| 4837 | |
| 4838 | assert(der && base); |
| 4839 | |
| 4840 | base->der = ly_realloc(base->der, (base->der_size + 1) * sizeof *(base->der)); |
| 4841 | if (!base->der) { |
| 4842 | LOGMEM; |
| 4843 | return EXIT_FAILURE; |
| 4844 | } |
| 4845 | base->der[base->der_size++] = der; |
| 4846 | |
| 4847 | for (i = 0; i < base->base_size; i++) { |
| 4848 | if (identity_backlink_update(der, base->base[i])) { |
| 4849 | return EXIT_FAILURE; |
| 4850 | } |
| 4851 | } |
| 4852 | |
| 4853 | return EXIT_SUCCESS; |
| 4854 | } |
| 4855 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4856 | /** |
| 4857 | * @brief Resolve base identity recursively. Does not log. |
| 4858 | * |
| 4859 | * @param[in] module Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4860 | * @param[in] ident Identity to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4861 | * @param[in] basename Base name of the identity. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4862 | * @param[out] ret Pointer to the resolved identity. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4863 | * |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 4864 | * @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] | 4865 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4866 | static int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 4867 | 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] | 4868 | struct unres_schema *unres, struct lys_ident **ret) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4869 | { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 4870 | uint32_t i, j; |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4871 | struct lys_ident *base = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4872 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 4873 | assert(ret); |
| 4874 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4875 | /* search module */ |
| 4876 | for (i = 0; i < module->ident_size; i++) { |
| 4877 | if (!strcmp(basename, module->ident[i].name)) { |
| 4878 | |
| 4879 | if (!ident) { |
| 4880 | /* just search for type, so do not modify anything, just return |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 4881 | * the base identity pointer */ |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 4882 | *ret = &module->ident[i]; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4883 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4884 | } |
| 4885 | |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 4886 | base = &module->ident[i]; |
| 4887 | goto matchfound; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4888 | } |
| 4889 | } |
| 4890 | |
| 4891 | /* search submodules */ |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 4892 | for (j = 0; j < module->inc_size && module->inc[j].submodule; j++) { |
| 4893 | for (i = 0; i < module->inc[j].submodule->ident_size; i++) { |
| 4894 | if (!strcmp(basename, module->inc[j].submodule->ident[i].name)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4895 | |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 4896 | if (!ident) { |
| 4897 | *ret = &module->inc[j].submodule->ident[i]; |
| 4898 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4899 | } |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 4900 | |
| 4901 | base = &module->inc[j].submodule->ident[i]; |
| 4902 | goto matchfound; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4903 | } |
| 4904 | } |
| 4905 | } |
| 4906 | |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 4907 | matchfound: |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4908 | /* we found it somewhere */ |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 4909 | if (base) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4910 | /* is it already completely resolved? */ |
| 4911 | for (i = 0; i < unres->count; i++) { |
Radek Krejci | ba7b1cc | 2016-08-08 13:44:43 +0200 | [diff] [blame] | 4912 | if ((unres->item[i] == base) && (unres->type[i] == UNRES_IDENT)) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4913 | /* identity found, but not yet resolved, so do not return it in *res and try it again later */ |
| 4914 | |
| 4915 | /* simple check for circular reference, |
| 4916 | * the complete check is done as a side effect of using only completely |
| 4917 | * resolved identities (previous check of unres content) */ |
| 4918 | if (ly_strequal((const char *)unres->str_snode[i], ident->name, 1)) { |
| 4919 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, basename, "base"); |
| 4920 | 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] | 4921 | return -1; |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4922 | } |
| 4923 | |
Radek Krejci | 06f64ed | 2016-08-15 11:07:44 +0200 | [diff] [blame] | 4924 | return EXIT_FAILURE; |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 4925 | } |
| 4926 | } |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4927 | |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 4928 | /* checks done, store the result */ |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4929 | *ret = base; |
Pavol Vican | fdab9f9 | 2016-09-07 15:23:27 +0200 | [diff] [blame] | 4930 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4931 | } |
| 4932 | |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 4933 | /* base not found (maybe a forward reference) */ |
| 4934 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4935 | } |
| 4936 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4937 | /** |
| 4938 | * @brief Resolve base identity. Logs directly. |
| 4939 | * |
| 4940 | * @param[in] module Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4941 | * @param[in] ident Identity to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4942 | * @param[in] basename Base name of the identity. |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 4943 | * @param[in] parent Either "type" or "identity". |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 4944 | * @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] | 4945 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4946 | * @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] | 4947 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4948 | static int |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 4949 | 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] | 4950 | struct lys_type *type, struct unres_schema *unres) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4951 | { |
| 4952 | const char *name; |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 4953 | int mod_name_len = 0, rc; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 4954 | struct lys_ident *target, **ret; |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 4955 | uint16_t flags; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 4956 | struct lys_module *mod; |
| 4957 | |
| 4958 | assert((ident && !type) || (!ident && type)); |
| 4959 | |
| 4960 | if (!type) { |
| 4961 | /* have ident to resolve */ |
| 4962 | ret = ⌖ |
| 4963 | flags = ident->flags; |
| 4964 | mod = ident->module; |
| 4965 | } else { |
| 4966 | /* have type to fill */ |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 4967 | ++type->info.ident.count; |
| 4968 | type->info.ident.ref = ly_realloc(type->info.ident.ref, type->info.ident.count * sizeof *type->info.ident.ref); |
| 4969 | if (!type->info.ident.ref) { |
| 4970 | LOGMEM; |
| 4971 | return -1; |
| 4972 | } |
| 4973 | |
| 4974 | ret = &type->info.ident.ref[type->info.ident.count - 1]; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 4975 | flags = type->parent->flags; |
| 4976 | mod = type->parent->module; |
| 4977 | } |
Michal Vasko | f200600 | 2016-04-21 16:28:15 +0200 | [diff] [blame] | 4978 | *ret = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4979 | |
| 4980 | /* search for the base identity */ |
| 4981 | name = strchr(basename, ':'); |
| 4982 | if (name) { |
| 4983 | /* set name to correct position after colon */ |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 4984 | mod_name_len = name - basename; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4985 | name++; |
| 4986 | |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 4987 | 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] | 4988 | /* prefix refers to the current module, ignore it */ |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 4989 | mod_name_len = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4990 | } |
| 4991 | } else { |
| 4992 | name = basename; |
| 4993 | } |
| 4994 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 4995 | /* get module where to search */ |
| 4996 | module = lys_get_import_module(module, NULL, 0, mod_name_len ? basename : NULL, mod_name_len); |
| 4997 | if (!module) { |
| 4998 | /* identity refers unknown data model */ |
Radek Krejci | 02a0499 | 2016-03-17 16:06:37 +0100 | [diff] [blame] | 4999 | LOGVAL(LYE_INMOD, LY_VLOG_NONE, NULL, basename); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 5000 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5001 | } |
| 5002 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 5003 | /* search in the identified module ... */ |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5004 | rc = resolve_base_ident_sub(module, ident, name, unres, ret); |
| 5005 | if (!rc) { |
| 5006 | assert(*ret); |
| 5007 | |
| 5008 | /* check status */ |
| 5009 | if (lyp_check_status(flags, mod, ident ? ident->name : "of type", |
| 5010 | (*ret)->flags, (*ret)->module, (*ret)->name, NULL)) { |
| 5011 | rc = -1; |
Pavol Vican | fdab9f9 | 2016-09-07 15:23:27 +0200 | [diff] [blame] | 5012 | } else { |
| 5013 | if (ident) { |
| 5014 | ident->base[ident->base_size++] = *ret; |
| 5015 | |
| 5016 | /* maintain backlinks to the derived identities */ |
| 5017 | rc = identity_backlink_update(ident, *ret) ? -1 : EXIT_SUCCESS; |
| 5018 | } |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5019 | } |
| 5020 | } else if (rc == EXIT_FAILURE) { |
| 5021 | LOGVAL(LYE_INRESOLV, LY_VLOG_NONE, NULL, parent, basename); |
Pavol Vican | 053a288 | 2016-09-05 14:40:33 +0200 | [diff] [blame] | 5022 | if (type) { |
| 5023 | --type->info.ident.count; |
| 5024 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5025 | } |
| 5026 | |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5027 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5028 | } |
| 5029 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5030 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 5031 | * @brief Resolve JSON data format identityref. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5032 | * |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5033 | * @param[in] type Identityref type. |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 5034 | * @param[in] ident_name Identityref name. |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 5035 | * @param[in] node Node where the identityref is being resolved |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5036 | * |
| 5037 | * @return Pointer to the identity resolvent, NULL on error. |
| 5038 | */ |
Radek Krejci | a52656e | 2015-08-05 13:41:50 +0200 | [diff] [blame] | 5039 | struct lys_ident * |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5040 | 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] | 5041 | { |
Michal Vasko | c633ca0 | 2015-08-21 14:03:51 +0200 | [diff] [blame] | 5042 | const char *mod_name, *name; |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5043 | int mod_name_len, rc, i, j; |
| 5044 | struct lys_ident *der, *cur; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5045 | |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5046 | if (!type || (!type->info.ident.count && !type->der) || !ident_name) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5047 | return NULL; |
| 5048 | } |
| 5049 | |
Michal Vasko | c633ca0 | 2015-08-21 14:03:51 +0200 | [diff] [blame] | 5050 | 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] | 5051 | if (rc < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5052 | 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] | 5053 | return NULL; |
Michal Vasko | b43bb3c | 2016-03-17 15:00:27 +0100 | [diff] [blame] | 5054 | } else if (rc < (signed)strlen(ident_name)) { |
Radek Krejci | 02a0499 | 2016-03-17 16:06:37 +0100 | [diff] [blame] | 5055 | 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] | 5056 | return NULL; |
| 5057 | } |
| 5058 | |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5059 | /* go through all the bases in all the derived types */ |
| 5060 | while (type->der) { |
| 5061 | for (i = 0; i < type->info.ident.count; ++i) { |
| 5062 | cur = type->info.ident.ref[i]; |
| 5063 | if (!strcmp(cur->name, name) && (!mod_name |
| 5064 | || (!strncmp(cur->module->name, mod_name, mod_name_len) && !cur->module->name[mod_name_len]))) { |
| 5065 | goto match; |
| 5066 | } |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 5067 | |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5068 | for (j = 0; j < cur->der_size; j++) { |
| 5069 | der = cur->der[j]; /* shortcut */ |
| 5070 | if (!strcmp(der->name, name) && |
| 5071 | (!mod_name || (!strncmp(der->module->name, mod_name, mod_name_len) && !der->module->name[mod_name_len]))) { |
| 5072 | /* we have match */ |
| 5073 | cur = der; |
| 5074 | goto match; |
| 5075 | } |
| 5076 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5077 | } |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5078 | type = &type->der->type; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5079 | } |
| 5080 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5081 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYD, node, "identityref", ident_name); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5082 | return NULL; |
Radek Krejci | f1ee2e2 | 2016-08-02 16:36:48 +0200 | [diff] [blame] | 5083 | |
| 5084 | match: |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5085 | for (i = 0; i < cur->iffeature_size; i++) { |
| 5086 | if (!resolve_iffeature(&cur->iffeature[i])) { |
| 5087 | LOGVAL(LYE_INVAL, LY_VLOG_LYD, node, cur->name, node->schema->name); |
Radek Krejci | f1ee2e2 | 2016-08-02 16:36:48 +0200 | [diff] [blame] | 5088 | 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] | 5089 | cur->name); |
Radek Krejci | f1ee2e2 | 2016-08-02 16:36:48 +0200 | [diff] [blame] | 5090 | return NULL; |
| 5091 | } |
| 5092 | } |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5093 | return cur; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5094 | } |
| 5095 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5096 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5097 | * @brief Resolve unresolved uses. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5098 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5099 | * @param[in] uses Uses to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5100 | * @param[in] unres Specific unres item. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5101 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5102 | * @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] | 5103 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5104 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5105 | 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] | 5106 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5107 | int rc; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5108 | struct lys_node *par_grp; |
Michal Vasko | e91afce | 2015-08-12 12:21:00 +0200 | [diff] [blame] | 5109 | |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5110 | /* HACK: when a grouping has uses inside, all such uses have to be resolved before the grouping itself |
| 5111 | * is used in some uses. When we see such a uses, the grouping's nacm member (not used in grouping) |
| 5112 | * is used to store number of so far unresolved uses. The grouping cannot be used unless the nacm |
| 5113 | * value is decreased back to 0. To remember that the uses already increased grouping's nacm, the |
| 5114 | * LYS_USESGRP flag is used. */ |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 5115 | 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] | 5116 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5117 | if (!uses->grp) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 5118 | rc = resolve_uses_schema_nodeid(uses->name, (const struct lys_node *)uses, (const struct lys_node_grp **)&uses->grp); |
| 5119 | if (rc == -1) { |
Michal Vasko | 92981a6 | 2016-10-14 10:25:16 +0200 | [diff] [blame] | 5120 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 5121 | return -1; |
| 5122 | } else if (rc > 0) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5123 | 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] | 5124 | return -1; |
| 5125 | } else if (!uses->grp) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5126 | if (par_grp && !(uses->flags & LYS_USESGRP)) { |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 5127 | /* hack - in contrast to lys_node, lys_node_grp has bigger nacm field |
| 5128 | * (and smaller flags - it uses only a limited set of flags) |
| 5129 | */ |
| 5130 | ((struct lys_node_grp *)par_grp)->nacm++; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5131 | uses->flags |= LYS_USESGRP; |
Michal Vasko | 407f1bb | 2015-09-23 15:51:07 +0200 | [diff] [blame] | 5132 | } |
Michal Vasko | 92981a6 | 2016-10-14 10:25:16 +0200 | [diff] [blame] | 5133 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 5134 | return EXIT_FAILURE; |
Michal Vasko | 12e3084 | 2015-08-04 11:54:00 +0200 | [diff] [blame] | 5135 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5136 | } |
| 5137 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5138 | if (uses->grp->nacm) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5139 | if (par_grp && !(uses->flags & LYS_USESGRP)) { |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 5140 | ((struct lys_node_grp *)par_grp)->nacm++; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5141 | uses->flags |= LYS_USESGRP; |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 5142 | } else { |
| 5143 | /* instantiate grouping only when it is completely resolved */ |
| 5144 | uses->grp = NULL; |
Michal Vasko | 407f1bb | 2015-09-23 15:51:07 +0200 | [diff] [blame] | 5145 | } |
Michal Vasko | 92981a6 | 2016-10-14 10:25:16 +0200 | [diff] [blame] | 5146 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5147 | return EXIT_FAILURE; |
| 5148 | } |
| 5149 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5150 | rc = resolve_uses(uses, unres); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5151 | if (!rc) { |
| 5152 | /* decrease unres count only if not first try */ |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5153 | if (par_grp && (uses->flags & LYS_USESGRP)) { |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 5154 | if (!((struct lys_node_grp *)par_grp)->nacm) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5155 | LOGINT; |
| 5156 | return -1; |
| 5157 | } |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 5158 | ((struct lys_node_grp *)par_grp)->nacm--; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5159 | uses->flags &= ~LYS_USESGRP; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5160 | } |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5161 | |
| 5162 | /* check status */ |
Radek Krejci | c655602 | 2016-01-27 15:16:45 +0100 | [diff] [blame] | 5163 | if (lyp_check_status(uses->flags, uses->module, "of uses", |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 5164 | uses->grp->flags, uses->grp->module, uses->grp->name, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5165 | (struct lys_node *)uses)) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5166 | return -1; |
| 5167 | } |
| 5168 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5169 | return EXIT_SUCCESS; |
| 5170 | } |
| 5171 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5172 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5173 | } |
| 5174 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5175 | /** |
Michal Vasko | 9957e59 | 2015-08-17 15:04:09 +0200 | [diff] [blame] | 5176 | * @brief Resolve list keys. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5177 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5178 | * @param[in] list List to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5179 | * @param[in] keys_str Keys node value. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5180 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5181 | * @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] | 5182 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5183 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5184 | resolve_list_keys(struct lys_node_list *list, const char *keys_str) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5185 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5186 | int i, len, rc; |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 5187 | const char *value; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5188 | |
| 5189 | for (i = 0; i < list->keys_size; ++i) { |
| 5190 | /* get the key name */ |
| 5191 | if ((value = strpbrk(keys_str, " \t\n"))) { |
| 5192 | len = value - keys_str; |
| 5193 | while (isspace(value[0])) { |
| 5194 | value++; |
| 5195 | } |
| 5196 | } else { |
| 5197 | len = strlen(keys_str); |
| 5198 | } |
| 5199 | |
Radek Krejci | c428344 | 2016-04-22 09:19:27 +0200 | [diff] [blame] | 5200 | 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] | 5201 | if (rc) { |
Michal Vasko | 7a55bea | 2016-05-02 14:51:20 +0200 | [diff] [blame] | 5202 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, list, "list keys", keys_str); |
| 5203 | return EXIT_FAILURE; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5204 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5205 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5206 | if (check_key(list, i, keys_str, len)) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5207 | /* check_key logs */ |
| 5208 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5209 | } |
| 5210 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5211 | /* check status */ |
Radek Krejci | c655602 | 2016-01-27 15:16:45 +0100 | [diff] [blame] | 5212 | if (lyp_check_status(list->flags, list->module, list->name, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5213 | list->keys[i]->flags, list->keys[i]->module, list->keys[i]->name, |
| 5214 | (struct lys_node *)list->keys[i])) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5215 | return -1; |
| 5216 | } |
| 5217 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5218 | /* prepare for next iteration */ |
| 5219 | while (value && isspace(value[0])) { |
| 5220 | value++; |
| 5221 | } |
| 5222 | keys_str = value; |
| 5223 | } |
| 5224 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5225 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5226 | } |
| 5227 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5228 | /** |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5229 | * @brief Resolve (check) all must conditions of \p node. |
| 5230 | * Logs directly. |
| 5231 | * |
| 5232 | * @param[in] node Data node with optional must statements. |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5233 | * @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] | 5234 | * |
| 5235 | * @return EXIT_SUCCESS on pass, EXIT_FAILURE on fail, -1 on error. |
| 5236 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5237 | static int |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5238 | resolve_must(struct lyd_node *node, int inout_parent) |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5239 | { |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5240 | uint8_t i, must_size; |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5241 | struct lys_node *schema; |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5242 | struct lys_restr *must; |
| 5243 | struct lyxp_set set; |
| 5244 | |
| 5245 | assert(node); |
| 5246 | memset(&set, 0, sizeof set); |
| 5247 | |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5248 | if (inout_parent) { |
| 5249 | for (schema = lys_parent(node->schema); |
| 5250 | schema && (schema->nodetype & (LYS_CHOICE | LYS_CASE | LYS_USES)); |
| 5251 | schema = lys_parent(schema)); |
| 5252 | if (!schema || !(schema->nodetype & (LYS_INPUT | LYS_OUTPUT))) { |
| 5253 | LOGINT; |
| 5254 | return -1; |
| 5255 | } |
| 5256 | must_size = ((struct lys_node_inout *)schema)->must_size; |
| 5257 | must = ((struct lys_node_inout *)schema)->must; |
| 5258 | |
| 5259 | /* context node is the RPC/action */ |
| 5260 | node = node->parent; |
| 5261 | if (!(node->schema->nodetype & (LYS_RPC | LYS_ACTION))) { |
| 5262 | LOGINT; |
| 5263 | return -1; |
| 5264 | } |
| 5265 | } else { |
| 5266 | switch (node->schema->nodetype) { |
| 5267 | case LYS_CONTAINER: |
| 5268 | must_size = ((struct lys_node_container *)node->schema)->must_size; |
| 5269 | must = ((struct lys_node_container *)node->schema)->must; |
| 5270 | break; |
| 5271 | case LYS_LEAF: |
| 5272 | must_size = ((struct lys_node_leaf *)node->schema)->must_size; |
| 5273 | must = ((struct lys_node_leaf *)node->schema)->must; |
| 5274 | break; |
| 5275 | case LYS_LEAFLIST: |
| 5276 | must_size = ((struct lys_node_leaflist *)node->schema)->must_size; |
| 5277 | must = ((struct lys_node_leaflist *)node->schema)->must; |
| 5278 | break; |
| 5279 | case LYS_LIST: |
| 5280 | must_size = ((struct lys_node_list *)node->schema)->must_size; |
| 5281 | must = ((struct lys_node_list *)node->schema)->must; |
| 5282 | break; |
| 5283 | case LYS_ANYXML: |
| 5284 | case LYS_ANYDATA: |
| 5285 | must_size = ((struct lys_node_anydata *)node->schema)->must_size; |
| 5286 | must = ((struct lys_node_anydata *)node->schema)->must; |
| 5287 | break; |
| 5288 | case LYS_NOTIF: |
| 5289 | must_size = ((struct lys_node_notif *)node->schema)->must_size; |
| 5290 | must = ((struct lys_node_notif *)node->schema)->must; |
| 5291 | break; |
| 5292 | default: |
| 5293 | must_size = 0; |
| 5294 | break; |
| 5295 | } |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5296 | } |
| 5297 | |
| 5298 | for (i = 0; i < must_size; ++i) { |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5299 | 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] | 5300 | return -1; |
| 5301 | } |
| 5302 | |
Michal Vasko | 944a564 | 2016-03-21 11:48:58 +0100 | [diff] [blame] | 5303 | lyxp_set_cast(&set, LYXP_SET_BOOLEAN, node, LYXP_MUST); |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5304 | |
Michal Vasko | 8146d4c | 2016-05-09 15:50:29 +0200 | [diff] [blame] | 5305 | if (!set.val.bool) { |
Michal Vasko | 6ac6828 | 2016-04-11 10:56:47 +0200 | [diff] [blame] | 5306 | LOGVAL(LYE_NOMUST, LY_VLOG_LYD, node, must[i].expr); |
| 5307 | if (must[i].emsg) { |
| 5308 | LOGVAL(LYE_SPEC, LY_VLOG_LYD, node, must[i].emsg); |
| 5309 | } |
| 5310 | if (must[i].eapptag) { |
| 5311 | strncpy(((struct ly_err *)&ly_errno)->apptag, must[i].eapptag, LY_APPTAG_LEN - 1); |
| 5312 | } |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5313 | return 1; |
| 5314 | } |
| 5315 | } |
| 5316 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5317 | return EXIT_SUCCESS; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5318 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5319 | |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5320 | /** |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5321 | * @brief Resolve (find) when condition schema context node. Does not log. |
| 5322 | * |
| 5323 | * @param[in] schema Schema node with the when condition. |
| 5324 | * @param[out] ctx_snode When schema context node. |
| 5325 | * @param[out] ctx_snode_type Schema context node type. |
| 5326 | */ |
| 5327 | void |
| 5328 | resolve_when_ctx_snode(const struct lys_node *schema, struct lys_node **ctx_snode, enum lyxp_node_type *ctx_snode_type) |
| 5329 | { |
| 5330 | const struct lys_node *sparent; |
| 5331 | |
| 5332 | /* find a not schema-only node */ |
| 5333 | *ctx_snode_type = LYXP_NODE_ELEM; |
| 5334 | while (schema->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE | LYS_AUGMENT | LYS_INPUT | LYS_OUTPUT)) { |
| 5335 | if (schema->nodetype == LYS_AUGMENT) { |
| 5336 | sparent = ((struct lys_node_augment *)schema)->target; |
| 5337 | } else { |
| 5338 | sparent = schema->parent; |
| 5339 | } |
| 5340 | if (!sparent) { |
| 5341 | /* context node is the document root (fake root in our case) */ |
| 5342 | if (schema->flags & LYS_CONFIG_W) { |
| 5343 | *ctx_snode_type = LYXP_NODE_ROOT_CONFIG; |
| 5344 | } else { |
Michal Vasko | b94a5e4 | 2016-09-08 14:01:56 +0200 | [diff] [blame] | 5345 | *ctx_snode_type = LYXP_NODE_ROOT; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5346 | } |
Michal Vasko | 2d2aec1 | 2016-09-08 11:42:50 +0200 | [diff] [blame] | 5347 | /* we need the first top-level sibling, but no uses or groupings */ |
Michal Vasko | b94a5e4 | 2016-09-08 14:01:56 +0200 | [diff] [blame] | 5348 | schema = lys_getnext(NULL, NULL, lys_node_module(schema), 0); |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5349 | break; |
| 5350 | } |
| 5351 | schema = sparent; |
| 5352 | } |
| 5353 | |
| 5354 | *ctx_snode = (struct lys_node *)schema; |
| 5355 | } |
| 5356 | |
| 5357 | /** |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5358 | * @brief Resolve (find) when condition context node. Does not log. |
| 5359 | * |
| 5360 | * @param[in] node Data node, whose conditional definition is being decided. |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5361 | * @param[in] schema Schema node with the when condition. |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5362 | * @param[out] ctx_node Context node. |
| 5363 | * @param[out] ctx_node_type Context node type. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5364 | * |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5365 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5366 | */ |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5367 | static int |
| 5368 | resolve_when_ctx_node(struct lyd_node *node, struct lys_node *schema, struct lyd_node **ctx_node, |
| 5369 | enum lyxp_node_type *ctx_node_type) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5370 | { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5371 | struct lyd_node *parent; |
| 5372 | struct lys_node *sparent; |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5373 | enum lyxp_node_type node_type; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5374 | uint16_t i, data_depth, schema_depth; |
| 5375 | |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5376 | resolve_when_ctx_snode(schema, &schema, &node_type); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5377 | |
Michal Vasko | fe98975 | 2016-09-08 08:47:26 +0200 | [diff] [blame] | 5378 | if (node_type == LYXP_NODE_ELEM) { |
| 5379 | /* standard element context node */ |
| 5380 | for (parent = node, data_depth = 0; parent; parent = parent->parent, ++data_depth); |
| 5381 | for (sparent = schema, schema_depth = 0; |
| 5382 | sparent; |
| 5383 | sparent = (sparent->nodetype == LYS_AUGMENT ? ((struct lys_node_augment *)sparent)->target : sparent->parent)) { |
| 5384 | if (sparent->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_NOTIF | LYS_RPC)) { |
| 5385 | ++schema_depth; |
| 5386 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5387 | } |
Michal Vasko | fe98975 | 2016-09-08 08:47:26 +0200 | [diff] [blame] | 5388 | if (data_depth < schema_depth) { |
| 5389 | return -1; |
| 5390 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5391 | |
Michal Vasko | 956e854 | 2016-08-26 09:43:35 +0200 | [diff] [blame] | 5392 | /* find the corresponding data node */ |
| 5393 | for (i = 0; i < data_depth - schema_depth; ++i) { |
| 5394 | node = node->parent; |
| 5395 | } |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5396 | if (node->schema != schema) { |
| 5397 | return -1; |
| 5398 | } |
Michal Vasko | fe98975 | 2016-09-08 08:47:26 +0200 | [diff] [blame] | 5399 | } else { |
| 5400 | /* root context node */ |
| 5401 | while (node->parent) { |
| 5402 | node = node->parent; |
| 5403 | } |
| 5404 | while (node->prev->next) { |
| 5405 | node = node->prev; |
| 5406 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5407 | } |
| 5408 | |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5409 | *ctx_node = node; |
| 5410 | *ctx_node_type = node_type; |
| 5411 | return EXIT_SUCCESS; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5412 | } |
| 5413 | |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5414 | /** |
| 5415 | * @brief Temporarily unlink nodes as per YANG 1.1 RFC section 7.21.5 for when XPath evaluation. |
| 5416 | * The context nodes is adjusted if needed. |
| 5417 | * |
| 5418 | * @param[in] snode Schema node, whose children instances need to be unlinked. |
| 5419 | * @param[in,out] node Data siblings where to look for the children of \p snode. If it is unlinked, |
| 5420 | * it is moved to point to another sibling still in the original tree. |
| 5421 | * @param[in,out] ctx_node When context node, adjusted if needed. |
| 5422 | * @param[in] ctx_node_type Context node type, just for information to detect invalid situations. |
| 5423 | * @param[out] unlinked_nodes Unlinked siblings. Can be safely appended to \p node afterwards. |
| 5424 | * Ordering may change, but there will be no semantic change. |
| 5425 | * |
| 5426 | * @return EXIT_SUCCESS on success, -1 on error. |
| 5427 | */ |
| 5428 | static int |
| 5429 | resolve_when_unlink_nodes(struct lys_node *snode, struct lyd_node **node, struct lyd_node **ctx_node, |
| 5430 | enum lyxp_node_type ctx_node_type, struct lyd_node **unlinked_nodes) |
| 5431 | { |
| 5432 | struct lyd_node *next, *elem; |
| 5433 | |
| 5434 | switch (snode->nodetype) { |
| 5435 | case LYS_AUGMENT: |
| 5436 | case LYS_USES: |
| 5437 | case LYS_CHOICE: |
| 5438 | case LYS_CASE: |
| 5439 | LY_TREE_FOR(snode->child, snode) { |
| 5440 | if (resolve_when_unlink_nodes(snode, node, ctx_node, ctx_node_type, unlinked_nodes)) { |
| 5441 | return -1; |
| 5442 | } |
| 5443 | } |
| 5444 | break; |
| 5445 | case LYS_CONTAINER: |
| 5446 | case LYS_LIST: |
| 5447 | case LYS_LEAF: |
| 5448 | case LYS_LEAFLIST: |
| 5449 | case LYS_ANYXML: |
| 5450 | case LYS_ANYDATA: |
| 5451 | LY_TREE_FOR_SAFE(lyd_first_sibling(*node), next, elem) { |
| 5452 | if (elem->schema == snode) { |
| 5453 | |
| 5454 | if (elem == *ctx_node) { |
| 5455 | /* We are going to unlink our context node! This normally cannot happen, |
| 5456 | * but we use normal top-level data nodes for faking a document root node, |
| 5457 | * so if this is the context node, we just use the next top-level node. |
| 5458 | * Additionally, it can even happen that there are no top-level data nodes left, |
| 5459 | * all were unlinked, so in this case we pass NULL as the context node/data tree, |
| 5460 | * lyxp_eval() can handle this special situation. |
| 5461 | */ |
| 5462 | if (ctx_node_type == LYXP_NODE_ELEM) { |
| 5463 | LOGINT; |
| 5464 | return -1; |
| 5465 | } |
| 5466 | |
| 5467 | if (elem->prev == elem) { |
| 5468 | /* unlinking last top-level element, use an empty data tree */ |
| 5469 | *ctx_node = NULL; |
| 5470 | } else { |
| 5471 | /* in this case just use the previous/last top-level data node */ |
| 5472 | *ctx_node = elem->prev; |
| 5473 | } |
| 5474 | } else if (elem == *node) { |
| 5475 | /* We are going to unlink the currently processed node. This does not matter that |
| 5476 | * much, but we would lose access to the original data tree, so just move our |
| 5477 | * pointer somewhere still inside it. |
| 5478 | */ |
| 5479 | if ((*node)->prev != *node) { |
| 5480 | *node = (*node)->prev; |
| 5481 | } else { |
| 5482 | /* the processed node with sibings were all unlinked, oh well */ |
| 5483 | *node = NULL; |
| 5484 | } |
| 5485 | } |
| 5486 | |
| 5487 | /* temporarily unlink the node */ |
| 5488 | lyd_unlink(elem); |
| 5489 | if (*unlinked_nodes) { |
| 5490 | if (lyd_insert_after(*unlinked_nodes, elem)) { |
| 5491 | LOGINT; |
| 5492 | return -1; |
| 5493 | } |
| 5494 | } else { |
| 5495 | *unlinked_nodes = elem; |
| 5496 | } |
| 5497 | |
| 5498 | if (snode->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_ANYDATA)) { |
| 5499 | /* there can be only one instance */ |
| 5500 | break; |
| 5501 | } |
| 5502 | } |
| 5503 | } |
| 5504 | break; |
| 5505 | default: |
| 5506 | LOGINT; |
| 5507 | return -1; |
| 5508 | } |
| 5509 | |
| 5510 | return EXIT_SUCCESS; |
| 5511 | } |
| 5512 | |
| 5513 | /** |
| 5514 | * @brief Relink the unlinked nodes back. |
| 5515 | * |
| 5516 | * @param[in] node Data node to link the nodes back to. It can actually be the adjusted context node, |
| 5517 | * we simply need a sibling from the original data tree. |
| 5518 | * @param[in] unlinked_nodes Unlinked nodes to relink to \p node. |
| 5519 | * @param[in] ctx_node_type Context node type to distinguish between \p node being the parent |
| 5520 | * or the sibling of \p unlinked_nodes. |
| 5521 | * |
| 5522 | * @return EXIT_SUCCESS on success, -1 on error. |
| 5523 | */ |
| 5524 | static int |
| 5525 | resolve_when_relink_nodes(struct lyd_node *node, struct lyd_node *unlinked_nodes, enum lyxp_node_type ctx_node_type) |
| 5526 | { |
| 5527 | struct lyd_node *elem; |
| 5528 | |
| 5529 | LY_TREE_FOR_SAFE(unlinked_nodes, unlinked_nodes, elem) { |
| 5530 | if (ctx_node_type == LYXP_NODE_ELEM) { |
| 5531 | if (lyd_insert(node, elem)) { |
| 5532 | return -1; |
| 5533 | } |
| 5534 | } else { |
| 5535 | if (lyd_insert_after(node, elem)) { |
| 5536 | return -1; |
| 5537 | } |
| 5538 | } |
| 5539 | } |
| 5540 | |
| 5541 | return EXIT_SUCCESS; |
| 5542 | } |
| 5543 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5544 | int |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5545 | resolve_applies_must(const struct lyd_node *node) |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5546 | { |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5547 | int ret = 0; |
| 5548 | uint8_t must_size; |
| 5549 | struct lys_node *schema, *iter; |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5550 | |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5551 | assert(node); |
| 5552 | |
| 5553 | schema = node->schema; |
| 5554 | |
| 5555 | /* their own must */ |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5556 | switch (schema->nodetype) { |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5557 | case LYS_CONTAINER: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5558 | must_size = ((struct lys_node_container *)schema)->must_size; |
| 5559 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5560 | case LYS_LEAF: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5561 | must_size = ((struct lys_node_leaf *)schema)->must_size; |
| 5562 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5563 | case LYS_LEAFLIST: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5564 | must_size = ((struct lys_node_leaflist *)schema)->must_size; |
| 5565 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5566 | case LYS_LIST: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5567 | must_size = ((struct lys_node_list *)schema)->must_size; |
| 5568 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5569 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 5570 | case LYS_ANYDATA: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5571 | must_size = ((struct lys_node_anydata *)schema)->must_size; |
| 5572 | break; |
| 5573 | case LYS_NOTIF: |
| 5574 | must_size = ((struct lys_node_notif *)schema)->must_size; |
| 5575 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5576 | default: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5577 | must_size = 0; |
| 5578 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5579 | } |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5580 | |
| 5581 | if (must_size) { |
| 5582 | ++ret; |
| 5583 | } |
| 5584 | |
| 5585 | /* schema may be a direct data child of input/output with must (but it must be first, it needs to be evaluated only once) */ |
| 5586 | if (!node->prev->next) { |
| 5587 | for (iter = lys_parent(schema); iter && (iter->nodetype & (LYS_CHOICE | LYS_CASE | LYS_USES)); iter = lys_parent(iter)); |
| 5588 | if (iter && (iter->nodetype & (LYS_INPUT | LYS_OUTPUT))) { |
| 5589 | ret += 0x2; |
| 5590 | } |
| 5591 | } |
| 5592 | |
| 5593 | return ret; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5594 | } |
| 5595 | |
| 5596 | int |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5597 | 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] | 5598 | { |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5599 | const struct lys_node *parent; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5600 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5601 | assert(schema); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5602 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5603 | 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] | 5604 | return 1; |
| 5605 | } |
| 5606 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5607 | parent = schema; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5608 | goto check_augment; |
| 5609 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5610 | while (parent) { |
| 5611 | /* stop conditions */ |
| 5612 | if (!mode) { |
| 5613 | /* stop on node that can be instantiated in data tree */ |
| 5614 | if (!(parent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) { |
| 5615 | break; |
| 5616 | } |
| 5617 | } else { |
| 5618 | /* stop on the specified node */ |
| 5619 | if (parent == stop) { |
| 5620 | break; |
| 5621 | } |
| 5622 | } |
| 5623 | |
| 5624 | if (((const struct lys_node_uses *)parent)->when) { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5625 | return 1; |
| 5626 | } |
| 5627 | check_augment: |
| 5628 | |
| 5629 | if ((parent->parent && (parent->parent->nodetype == LYS_AUGMENT) && |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5630 | (((const struct lys_node_augment *)parent->parent)->when))) { |
Michal Vasko | e365556 | 2016-08-24 15:56:17 +0200 | [diff] [blame] | 5631 | return 1; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5632 | } |
| 5633 | parent = lys_parent(parent); |
| 5634 | } |
| 5635 | |
| 5636 | return 0; |
| 5637 | } |
| 5638 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5639 | /** |
| 5640 | * @brief Resolve (check) all when conditions relevant for \p node. |
| 5641 | * Logs directly. |
| 5642 | * |
| 5643 | * @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] | 5644 | * |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5645 | * @return |
| 5646 | * -1 - error, ly_errno is set |
| 5647 | * 0 - true "when" statement |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5648 | * 0, ly_vecode = LYVE_NOWHEN - false "when" statement |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5649 | * 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] | 5650 | */ |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5651 | int |
| 5652 | resolve_when(struct lyd_node *node, int *result) |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5653 | { |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5654 | struct lyd_node *ctx_node = NULL, *unlinked_nodes, *tmp_node; |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5655 | struct lys_node *sparent; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5656 | struct lyxp_set set; |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5657 | enum lyxp_node_type ctx_node_type; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5658 | int rc = 0; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5659 | |
| 5660 | assert(node); |
| 5661 | memset(&set, 0, sizeof set); |
| 5662 | |
| 5663 | 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] | 5664 | /* make the node dummy for the evaluation */ |
| 5665 | node->validity |= LYD_VAL_INUSE; |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5666 | 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] | 5667 | node->validity &= ~LYD_VAL_INUSE; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5668 | if (rc) { |
| 5669 | if (rc == 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5670 | 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] | 5671 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5672 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5673 | } |
| 5674 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5675 | /* set boolean result of the condition */ |
Michal Vasko | 944a564 | 2016-03-21 11:48:58 +0100 | [diff] [blame] | 5676 | lyxp_set_cast(&set, LYXP_SET_BOOLEAN, node, LYXP_WHEN); |
Michal Vasko | 8146d4c | 2016-05-09 15:50:29 +0200 | [diff] [blame] | 5677 | if (!set.val.bool) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5678 | 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] | 5679 | node->when_status |= LYD_WHEN_FALSE; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5680 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5681 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5682 | |
| 5683 | /* free xpath set content */ |
| 5684 | lyxp_set_cast(&set, LYXP_SET_EMPTY, node, 0); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5685 | } |
| 5686 | |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5687 | sparent = node->schema; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5688 | goto check_augment; |
| 5689 | |
| 5690 | /* check when in every schema node that affects node */ |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5691 | while (sparent && (sparent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) { |
| 5692 | if (((struct lys_node_uses *)sparent)->when) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5693 | if (!ctx_node) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5694 | rc = resolve_when_ctx_node(node, sparent, &ctx_node, &ctx_node_type); |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5695 | if (rc) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5696 | LOGINT; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5697 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5698 | } |
| 5699 | } |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5700 | |
| 5701 | unlinked_nodes = NULL; |
| 5702 | /* we do not want our node pointer to change */ |
| 5703 | tmp_node = node; |
| 5704 | rc = resolve_when_unlink_nodes(sparent, &tmp_node, &ctx_node, ctx_node_type, &unlinked_nodes); |
| 5705 | if (rc) { |
| 5706 | goto cleanup; |
| 5707 | } |
| 5708 | |
| 5709 | rc = lyxp_eval(((struct lys_node_uses *)sparent)->when->cond, ctx_node, ctx_node_type, &set, LYXP_WHEN); |
| 5710 | |
| 5711 | if (unlinked_nodes && ctx_node) { |
| 5712 | if (resolve_when_relink_nodes(ctx_node, unlinked_nodes, ctx_node_type)) { |
| 5713 | rc = -1; |
| 5714 | goto cleanup; |
| 5715 | } |
| 5716 | } |
| 5717 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5718 | if (rc) { |
| 5719 | if (rc == 1) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5720 | 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] | 5721 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5722 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5723 | } |
| 5724 | |
Michal Vasko | 944a564 | 2016-03-21 11:48:58 +0100 | [diff] [blame] | 5725 | lyxp_set_cast(&set, LYXP_SET_BOOLEAN, ctx_node, LYXP_WHEN); |
Michal Vasko | 8146d4c | 2016-05-09 15:50:29 +0200 | [diff] [blame] | 5726 | if (!set.val.bool) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5727 | 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] | 5728 | node->when_status |= LYD_WHEN_FALSE; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5729 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5730 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5731 | |
| 5732 | /* free xpath set content */ |
| 5733 | lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node, 0); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5734 | } |
| 5735 | |
| 5736 | check_augment: |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5737 | 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] | 5738 | if (!ctx_node) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5739 | 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] | 5740 | if (rc) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5741 | LOGINT; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5742 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5743 | } |
| 5744 | } |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5745 | |
| 5746 | unlinked_nodes = NULL; |
| 5747 | tmp_node = node; |
| 5748 | rc = resolve_when_unlink_nodes(sparent->parent, &tmp_node, &ctx_node, ctx_node_type, &unlinked_nodes); |
| 5749 | if (rc) { |
| 5750 | goto cleanup; |
| 5751 | } |
| 5752 | |
| 5753 | rc = lyxp_eval(((struct lys_node_augment *)sparent->parent)->when->cond, ctx_node, ctx_node_type, &set, LYXP_WHEN); |
| 5754 | |
| 5755 | /* reconnect nodes, if ctx_node is NULL then all the nodes were unlinked, but linked together, |
| 5756 | * so the tree did not actually change and there is nothing for us to do |
| 5757 | */ |
| 5758 | if (unlinked_nodes && ctx_node) { |
| 5759 | if (resolve_when_relink_nodes(ctx_node, unlinked_nodes, ctx_node_type)) { |
| 5760 | rc = -1; |
| 5761 | goto cleanup; |
| 5762 | } |
| 5763 | } |
| 5764 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5765 | if (rc) { |
| 5766 | if (rc == 1) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5767 | 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] | 5768 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5769 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5770 | } |
| 5771 | |
Michal Vasko | 944a564 | 2016-03-21 11:48:58 +0100 | [diff] [blame] | 5772 | lyxp_set_cast(&set, LYXP_SET_BOOLEAN, ctx_node, LYXP_WHEN); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5773 | |
Michal Vasko | 8146d4c | 2016-05-09 15:50:29 +0200 | [diff] [blame] | 5774 | if (!set.val.bool) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5775 | 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] | 5776 | node->when_status |= LYD_WHEN_FALSE; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5777 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5778 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5779 | |
| 5780 | /* free xpath set content */ |
| 5781 | lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node, 0); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5782 | } |
| 5783 | |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5784 | sparent = lys_parent(sparent); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5785 | } |
| 5786 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5787 | node->when_status |= LYD_WHEN_TRUE; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5788 | |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5789 | cleanup: |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5790 | /* free xpath set content */ |
| 5791 | lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node ? ctx_node : node, 0); |
| 5792 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5793 | if (result) { |
| 5794 | if (node->when_status & LYD_WHEN_TRUE) { |
| 5795 | *result = 1; |
| 5796 | } else { |
| 5797 | *result = 0; |
| 5798 | } |
| 5799 | } |
| 5800 | |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5801 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5802 | } |
| 5803 | |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 5804 | static int |
| 5805 | check_leafref_features(struct lys_type *type) |
| 5806 | { |
| 5807 | struct lys_node *iter; |
| 5808 | struct ly_set *src_parents, *trg_parents, *features; |
| 5809 | unsigned int i, j, size, x; |
| 5810 | int ret = EXIT_SUCCESS; |
| 5811 | |
| 5812 | assert(type->parent); |
| 5813 | |
| 5814 | src_parents = ly_set_new(); |
| 5815 | trg_parents = ly_set_new(); |
| 5816 | features = ly_set_new(); |
| 5817 | |
| 5818 | /* get parents chain of source (leafref) */ |
| 5819 | for (iter = (struct lys_node *)type->parent; iter; iter = iter->parent) { |
| 5820 | if (iter->nodetype & (LYS_INPUT | LYS_OUTPUT)) { |
| 5821 | continue; |
| 5822 | } |
| 5823 | ly_set_add(src_parents, iter, LY_SET_OPT_USEASLIST); |
| 5824 | } |
| 5825 | /* get parents chain of target */ |
| 5826 | for (iter = (struct lys_node *)type->info.lref.target; iter; iter = iter->parent) { |
| 5827 | if (iter->nodetype & (LYS_INPUT | LYS_OUTPUT)) { |
| 5828 | continue; |
| 5829 | } |
| 5830 | ly_set_add(trg_parents, iter, LY_SET_OPT_USEASLIST); |
| 5831 | } |
| 5832 | |
| 5833 | /* compare the features used in if-feature statements in the rest of both |
| 5834 | * chains of parents. The set of features used for target must be a subset |
| 5835 | * of features used for the leafref. This is not a perfect, we should compare |
| 5836 | * the truth tables but it could require too much resources, so we simplify that */ |
| 5837 | for (i = 0; i < src_parents->number; i++) { |
| 5838 | iter = src_parents->set.s[i]; /* shortcut */ |
| 5839 | if (!iter->iffeature_size) { |
| 5840 | continue; |
| 5841 | } |
| 5842 | for (j = 0; j < iter->iffeature_size; j++) { |
| 5843 | resolve_iffeature_getsizes(&iter->iffeature[j], NULL, &size); |
| 5844 | for (; size; size--) { |
| 5845 | if (!iter->iffeature[j].features[size - 1]) { |
| 5846 | /* not yet resolved feature, postpone this check */ |
| 5847 | ret = EXIT_FAILURE; |
| 5848 | goto cleanup; |
| 5849 | } |
| 5850 | ly_set_add(features, iter->iffeature[j].features[size - 1], 0); |
| 5851 | } |
| 5852 | } |
| 5853 | } |
| 5854 | x = features->number; |
| 5855 | for (i = 0; i < trg_parents->number; i++) { |
| 5856 | iter = trg_parents->set.s[i]; /* shortcut */ |
| 5857 | if (!iter->iffeature_size) { |
| 5858 | continue; |
| 5859 | } |
| 5860 | for (j = 0; j < iter->iffeature_size; j++) { |
| 5861 | resolve_iffeature_getsizes(&iter->iffeature[j], NULL, &size); |
| 5862 | for (; size; size--) { |
| 5863 | if (!iter->iffeature[j].features[size - 1]) { |
| 5864 | /* not yet resolved feature, postpone this check */ |
| 5865 | ret = EXIT_FAILURE; |
| 5866 | goto cleanup; |
| 5867 | } |
| 5868 | if ((unsigned int)ly_set_add(features, iter->iffeature[j].features[size - 1], 0) >= x) { |
| 5869 | /* the feature is not present in features set of target's parents chain */ |
| 5870 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, type->parent, "leafref", type->info.lref.path); |
| 5871 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, type->parent, |
| 5872 | "Leafref is not conditional based on \"%s\" feature as its target.", |
| 5873 | iter->iffeature[j].features[size - 1]->name); |
| 5874 | ret = -1; |
| 5875 | goto cleanup; |
| 5876 | } |
| 5877 | } |
| 5878 | } |
| 5879 | } |
| 5880 | |
| 5881 | cleanup: |
| 5882 | ly_set_free(features); |
| 5883 | ly_set_free(src_parents); |
| 5884 | ly_set_free(trg_parents); |
| 5885 | |
| 5886 | return ret; |
| 5887 | } |
| 5888 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5889 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5890 | * @brief Resolve a single unres schema item. Logs indirectly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5891 | * |
| 5892 | * @param[in] mod Main module. |
| 5893 | * @param[in] item Item to resolve. Type determined by \p type. |
| 5894 | * @param[in] type Type of the unresolved item. |
| 5895 | * @param[in] str_snode String, a schema node, or NULL. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5896 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5897 | * |
| 5898 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 5899 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5900 | static int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 5901 | 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] | 5902 | struct unres_schema *unres) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5903 | { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 5904 | /* 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] | 5905 | int rc = -1, has_str = 0, tpdf_flag = 0, i, k; |
| 5906 | unsigned int j; |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 5907 | struct lys_node *node, *par_grp; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 5908 | const char *expr; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 5909 | |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 5910 | struct ly_set *refs, *procs; |
| 5911 | struct lys_feature *ref, *feat; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 5912 | struct lys_ident *ident; |
| 5913 | struct lys_type *stype; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 5914 | struct lys_node_choice *choic; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 5915 | struct lyxml_elem *yin; |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 5916 | struct yang_type *yang; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 5917 | struct unres_list_uniq *unique_info; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 5918 | struct unres_iffeat_data *iff_data; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5919 | |
| 5920 | switch (type) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5921 | case UNRES_IDENT: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 5922 | expr = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 5923 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 5924 | ident = item; |
| 5925 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5926 | rc = resolve_base_ident(mod, ident, expr, "identity", NULL, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5927 | break; |
| 5928 | case UNRES_TYPE_IDENTREF: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 5929 | expr = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 5930 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 5931 | stype = item; |
| 5932 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5933 | rc = resolve_base_ident(mod, NULL, expr, "type", stype, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5934 | break; |
| 5935 | case UNRES_TYPE_LEAFREF: |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 5936 | node = str_snode; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 5937 | stype = item; |
| 5938 | |
Radek Krejci | 2f12f85 | 2016-01-08 12:59:57 +0100 | [diff] [blame] | 5939 | /* HACK - when there is no parent, we are in top level typedef and in that |
| 5940 | * case, the path has to contain absolute path, so we let the resolve_path_arg_schema() |
| 5941 | * know it via tpdf_flag */ |
| 5942 | if (!node) { |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 5943 | tpdf_flag = 1; |
Radek Krejci | 2f12f85 | 2016-01-08 12:59:57 +0100 | [diff] [blame] | 5944 | node = (struct lys_node *)stype->parent; |
| 5945 | } |
| 5946 | |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 5947 | if (!lys_node_module(node)->implemented) { |
| 5948 | /* not implemented module, don't bother with resolving the leafref |
| 5949 | * if the module is set to be implemented, tha path will be resolved then */ |
| 5950 | rc = 0; |
| 5951 | break; |
| 5952 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5953 | rc = resolve_path_arg_schema(stype->info.lref.path, node, tpdf_flag, |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 5954 | (const struct lys_node **)&stype->info.lref.target); |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 5955 | if (!tpdf_flag && !rc) { |
| 5956 | assert(stype->info.lref.target); |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 5957 | /* check if leafref and its target are under a common if-features */ |
| 5958 | rc = check_leafref_features(stype); |
| 5959 | if (rc) { |
| 5960 | break; |
| 5961 | } |
| 5962 | |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 5963 | /* store the backlink from leafref target */ |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 5964 | if (lys_leaf_add_leafref_target(stype->info.lref.target, (struct lys_node *)stype->parent)) { |
| 5965 | rc = -1; |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 5966 | } |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 5967 | } |
| 5968 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5969 | break; |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 5970 | case UNRES_TYPE_DER_TPDF: |
| 5971 | tpdf_flag = 1; |
| 5972 | /* no break */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5973 | case UNRES_TYPE_DER: |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 5974 | /* parent */ |
| 5975 | node = str_snode; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 5976 | stype = item; |
| 5977 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 5978 | /* HACK type->der is temporarily unparsed type statement */ |
| 5979 | yin = (struct lyxml_elem *)stype->der; |
| 5980 | stype->der = NULL; |
| 5981 | |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 5982 | if (yin->flags & LY_YANG_STRUCTURE_FLAG) { |
| 5983 | yang = (struct yang_type *)yin; |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 5984 | rc = yang_check_type(mod, node, yang, tpdf_flag, unres); |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 5985 | |
| 5986 | if (rc) { |
Pavol Vican | 8bd72e4 | 2016-08-29 09:53:05 +0200 | [diff] [blame] | 5987 | /* may try again later */ |
| 5988 | stype->der = (struct lys_tpdf *)yang; |
Pavol Vican | d01d8ae | 2016-03-01 10:45:59 +0100 | [diff] [blame] | 5989 | } else { |
| 5990 | /* 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] | 5991 | lydict_remove(mod->ctx, yang->name); |
Pavol Vican | d01d8ae | 2016-03-01 10:45:59 +0100 | [diff] [blame] | 5992 | free(yang); |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 5993 | } |
| 5994 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 5995 | } else { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 5996 | rc = fill_yin_type(mod, node, yin, stype, tpdf_flag, unres); |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 5997 | if (!rc) { |
| 5998 | /* we need to always be able to free this, it's safe only in this case */ |
| 5999 | lyxml_free(mod->ctx, yin); |
| 6000 | } else { |
| 6001 | /* may try again later, put all back how it was */ |
| 6002 | stype->der = (struct lys_tpdf *)yin; |
| 6003 | } |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6004 | } |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 6005 | if (rc == EXIT_SUCCESS) { |
Radek Krejci | 2c2fce8 | 2016-08-01 13:52:26 +0200 | [diff] [blame] | 6006 | /* it does not make sense to have leaf-list of empty type */ |
| 6007 | if (!tpdf_flag && node->nodetype == LYS_LEAFLIST && stype->base == LY_TYPE_EMPTY) { |
| 6008 | LOGWRN("The leaf-list \"%s\" is of \"empty\" type, which does not make sense.", node->name); |
| 6009 | } |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6010 | } else if (rc == EXIT_FAILURE && stype->base != LY_TYPE_ERR) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 6011 | /* forward reference - in case the type is in grouping, we have to make the grouping unusable |
| 6012 | * by uses statement until the type is resolved. We do that the same way as uses statements inside |
| 6013 | * grouping - the grouping's nacm member (not used un grouping) is used to increase the number of |
| 6014 | * 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] | 6015 | * 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] | 6016 | * of the type's base member. */ |
| 6017 | for (par_grp = node; par_grp && (par_grp->nodetype != LYS_GROUPING); par_grp = lys_parent(par_grp)); |
| 6018 | if (par_grp) { |
| 6019 | ((struct lys_node_grp *)par_grp)->nacm++; |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6020 | stype->base = LY_TYPE_ERR; |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 6021 | } |
Radek Krejci | 2c2fce8 | 2016-08-01 13:52:26 +0200 | [diff] [blame] | 6022 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6023 | break; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6024 | case UNRES_IFFEAT: |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6025 | iff_data = str_snode; |
| 6026 | 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] | 6027 | if (!rc) { |
| 6028 | /* success */ |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6029 | lydict_remove(mod->ctx, iff_data->fname); |
| 6030 | free(iff_data); |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6031 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6032 | break; |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6033 | case UNRES_FEATURE: |
| 6034 | feat = (struct lys_feature *)item; |
| 6035 | |
| 6036 | if (feat->iffeature_size) { |
| 6037 | refs = ly_set_new(); |
| 6038 | procs = ly_set_new(); |
| 6039 | ly_set_add(procs, feat, 0); |
| 6040 | |
| 6041 | while (procs->number) { |
| 6042 | ref = procs->set.g[procs->number - 1]; |
| 6043 | ly_set_rm_index(procs, procs->number - 1); |
| 6044 | |
| 6045 | for (i = 0; i < ref->iffeature_size; i++) { |
| 6046 | resolve_iffeature_getsizes(&ref->iffeature[i], NULL, &j); |
| 6047 | for (; j > 0 ; j--) { |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6048 | if (ref->iffeature[i].features[j - 1]) { |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6049 | if (ref->iffeature[i].features[j - 1] == feat) { |
| 6050 | LOGVAL(LYE_CIRC_FEATURES, LY_VLOG_NONE, NULL, feat->name); |
| 6051 | goto featurecheckdone; |
| 6052 | } |
| 6053 | |
| 6054 | if (ref->iffeature[i].features[j - 1]->iffeature_size) { |
| 6055 | k = refs->number; |
| 6056 | if (ly_set_add(refs, ref->iffeature[i].features[j - 1], 0) == k) { |
| 6057 | /* not yet seen feature, add it for processing */ |
| 6058 | ly_set_add(procs, ref->iffeature[i].features[j - 1], 0); |
| 6059 | } |
| 6060 | } |
| 6061 | } else { |
| 6062 | /* forward reference */ |
| 6063 | rc = EXIT_FAILURE; |
| 6064 | goto featurecheckdone; |
| 6065 | } |
| 6066 | } |
| 6067 | |
| 6068 | } |
| 6069 | } |
| 6070 | rc = EXIT_SUCCESS; |
| 6071 | |
| 6072 | featurecheckdone: |
| 6073 | ly_set_free(refs); |
| 6074 | ly_set_free(procs); |
| 6075 | } |
| 6076 | |
| 6077 | break; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6078 | case UNRES_USES: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6079 | rc = resolve_unres_schema_uses(item, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6080 | break; |
| 6081 | case UNRES_TYPE_DFLT: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6082 | expr = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 6083 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6084 | stype = item; |
| 6085 | |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6086 | rc = check_default(stype, expr, mod); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6087 | break; |
| 6088 | case UNRES_CHOICE_DFLT: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6089 | expr = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 6090 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6091 | choic = item; |
| 6092 | |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 6093 | if (!choic->dflt) { |
| 6094 | choic->dflt = resolve_choice_dflt(choic, expr); |
| 6095 | } |
Michal Vasko | 7955b36 | 2015-09-04 14:18:15 +0200 | [diff] [blame] | 6096 | if (choic->dflt) { |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 6097 | rc = lyp_check_mandatory_choice((struct lys_node *)choic); |
Michal Vasko | 7955b36 | 2015-09-04 14:18:15 +0200 | [diff] [blame] | 6098 | } else { |
| 6099 | rc = EXIT_FAILURE; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6100 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6101 | break; |
| 6102 | case UNRES_LIST_KEYS: |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 6103 | has_str = 1; |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6104 | rc = resolve_list_keys(item, str_snode); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6105 | break; |
| 6106 | case UNRES_LIST_UNIQ: |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6107 | unique_info = (struct unres_list_uniq *)item; |
| 6108 | 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] | 6109 | break; |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 6110 | case UNRES_AUGMENT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6111 | rc = resolve_augment(item, NULL); |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 6112 | break; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6113 | case UNRES_XPATH: |
| 6114 | node = (struct lys_node *)item; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 6115 | rc = check_node_xpath(node); |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6116 | break; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6117 | default: |
| 6118 | LOGINT; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6119 | break; |
| 6120 | } |
| 6121 | |
Radek Krejci | 54081ce | 2016-08-12 15:21:47 +0200 | [diff] [blame] | 6122 | if (has_str && !rc) { |
| 6123 | /* the string is no more needed in case of success. |
| 6124 | * 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] | 6125 | lydict_remove(mod->ctx, str_snode); |
| 6126 | } |
| 6127 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6128 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6129 | } |
| 6130 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6131 | /* logs directly */ |
| 6132 | static void |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6133 | 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] | 6134 | { |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 6135 | struct lyxml_elem *xml; |
| 6136 | struct lyxml_attr *attr; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6137 | struct unres_iffeat_data *iff_data; |
Radek Krejci | 76e15e1 | 2016-06-22 11:02:24 +0200 | [diff] [blame] | 6138 | const char *type_name = NULL; |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 6139 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6140 | switch (type) { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6141 | case UNRES_IDENT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6142 | 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] | 6143 | break; |
| 6144 | case UNRES_TYPE_IDENTREF: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6145 | 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] | 6146 | break; |
| 6147 | case UNRES_TYPE_LEAFREF: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6148 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "leafref", |
| 6149 | ((struct lys_type *)item)->info.lref.path); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6150 | break; |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 6151 | case UNRES_TYPE_DER_TPDF: |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6152 | case UNRES_TYPE_DER: |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 6153 | xml = (struct lyxml_elem *)((struct lys_type *)item)->der; |
| 6154 | if (xml->flags & LY_YANG_STRUCTURE_FLAG) { |
| 6155 | type_name = ((struct yang_type *)xml)->name; |
| 6156 | } else { |
| 6157 | LY_TREE_FOR(xml->attr, attr) { |
| 6158 | if ((attr->type == LYXML_ATTR_STD) && !strcmp(attr->name, "name")) { |
| 6159 | type_name = attr->value; |
| 6160 | break; |
| 6161 | } |
| 6162 | } |
| 6163 | assert(attr); |
| 6164 | } |
| 6165 | 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] | 6166 | break; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6167 | case UNRES_IFFEAT: |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6168 | iff_data = str_node; |
| 6169 | 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] | 6170 | break; |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6171 | case UNRES_FEATURE: |
| 6172 | LOGVRB("There are unresolved if-features for \"%s\" feature circular dependency check, it will be attempted later", |
| 6173 | ((struct lys_feature *)item)->name); |
| 6174 | break; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6175 | case UNRES_USES: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6176 | 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] | 6177 | break; |
| 6178 | case UNRES_TYPE_DFLT: |
Radek Krejci | 2e2de83 | 2016-10-13 16:12:26 +0200 | [diff] [blame] | 6179 | if (str_node) { |
| 6180 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "type default", (char *)str_node); |
| 6181 | } /* else no default value in the type itself, but we are checking some restrictions against |
| 6182 | * possible default value of some base type. The failure is caused by not resolved base type, |
| 6183 | * so it was already reported */ |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6184 | break; |
| 6185 | case UNRES_CHOICE_DFLT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6186 | 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] | 6187 | break; |
| 6188 | case UNRES_LIST_KEYS: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6189 | 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] | 6190 | break; |
| 6191 | case UNRES_LIST_UNIQ: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6192 | 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] | 6193 | break; |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 6194 | case UNRES_AUGMENT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6195 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "augment target", |
| 6196 | ((struct lys_node_augment *)item)->target_name); |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 6197 | break; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6198 | case UNRES_XPATH: |
| 6199 | LOGVRB("Resolving %s \"%s\" with the context node \"%s\" failed, it will be attempted later.", "XPath", |
| 6200 | (char *)str_node, ((struct lys_node *)item)->name); |
| 6201 | break; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6202 | default: |
| 6203 | LOGINT; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6204 | break; |
| 6205 | } |
| 6206 | } |
| 6207 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6208 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6209 | * @brief Resolve every unres schema item in the structure. Logs directly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6210 | * |
| 6211 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6212 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6213 | * |
Michal Vasko | 92b8a38 | 2015-08-19 14:03:49 +0200 | [diff] [blame] | 6214 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6215 | */ |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6216 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 6217 | resolve_unres_schema(struct lys_module *mod, struct unres_schema *unres) |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6218 | { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6219 | uint32_t i, resolved = 0, unres_count, res_count; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6220 | int rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6221 | |
| 6222 | assert(unres); |
| 6223 | |
Michal Vasko | e873426 | 2016-09-29 14:12:06 +0200 | [diff] [blame] | 6224 | LOGVRB("Resolving \"%s\" unresolved schema nodes and their constraints...", mod->name); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6225 | ly_vlog_hide(1); |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6226 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6227 | /* uses */ |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6228 | do { |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6229 | unres_count = 0; |
| 6230 | res_count = 0; |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6231 | |
| 6232 | for (i = 0; i < unres->count; ++i) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6233 | /* UNRES_TYPE_LEAFREF must be resolved (for storing leafref target pointers); |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6234 | * if-features are resolved here to make sure that we will have all if-features for |
| 6235 | * later check of feature circular dependency */ |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6236 | if (unres->type[i] > UNRES_IDENT) { |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6237 | continue; |
| 6238 | } |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6239 | /* 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] | 6240 | * UNRES_CHOICE_DFLT and UNRES_IDENT */ |
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 | ++unres_count; |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6243 | 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] | 6244 | if (!rc) { |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6245 | unres->type[i] = UNRES_RESOLVED; |
| 6246 | ++resolved; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6247 | ++res_count; |
Michal Vasko | 89e1532 | 2015-08-17 15:46:55 +0200 | [diff] [blame] | 6248 | } else if (rc == -1) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6249 | ly_vlog_hide(0); |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6250 | /* print the error */ |
| 6251 | 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] | 6252 | return -1; |
Radek Krejci | c2a180f | 2016-06-22 13:28:16 +0200 | [diff] [blame] | 6253 | } else { |
| 6254 | /* forward reference, erase ly_errno */ |
| 6255 | ly_errno = LY_SUCCESS; |
| 6256 | ly_vecode = LYVE_SUCCESS; |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6257 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6258 | } |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6259 | } while (res_count && (res_count < unres_count)); |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6260 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6261 | if (res_count < unres_count) { |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6262 | /* just print the errors */ |
| 6263 | ly_vlog_hide(0); |
| 6264 | |
| 6265 | for (i = 0; i < unres->count; ++i) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6266 | if (unres->type[i] > UNRES_IDENT) { |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6267 | continue; |
| 6268 | } |
| 6269 | resolve_unres_schema_item(mod, unres->item[i], unres->type[i], unres->str_snode[i], unres); |
| 6270 | } |
Michal Vasko | 92b8a38 | 2015-08-19 14:03:49 +0200 | [diff] [blame] | 6271 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6272 | } |
| 6273 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6274 | /* the rest */ |
| 6275 | for (i = 0; i < unres->count; ++i) { |
| 6276 | if (unres->type[i] == UNRES_RESOLVED) { |
| 6277 | continue; |
| 6278 | } |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 6279 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6280 | 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] | 6281 | if (rc == 0) { |
Pavol Vican | 88e16c9 | 2016-09-07 15:41:50 +0200 | [diff] [blame] | 6282 | if (unres->type[i] == UNRES_LIST_UNIQ) { |
| 6283 | /* free the allocated structure */ |
| 6284 | free(unres->item[i]); |
| 6285 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6286 | unres->type[i] = UNRES_RESOLVED; |
| 6287 | ++resolved; |
| 6288 | } else if (rc == -1) { |
| 6289 | ly_vlog_hide(0); |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6290 | /* print the error */ |
| 6291 | resolve_unres_schema_item(mod, unres->item[i], unres->type[i], unres->str_snode[i], unres); |
| 6292 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6293 | } |
| 6294 | } |
| 6295 | |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6296 | ly_vlog_hide(0); |
| 6297 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6298 | if (resolved < unres->count) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6299 | /* try to resolve the unresolved nodes again, it will not resolve anything, but it will print |
| 6300 | * all the validation errors |
| 6301 | */ |
| 6302 | for (i = 0; i < unres->count; ++i) { |
| 6303 | if (unres->type[i] == UNRES_RESOLVED) { |
| 6304 | continue; |
| 6305 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6306 | 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] | 6307 | } |
Michal Vasko | 92b8a38 | 2015-08-19 14:03:49 +0200 | [diff] [blame] | 6308 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6309 | } |
| 6310 | |
Michal Vasko | e873426 | 2016-09-29 14:12:06 +0200 | [diff] [blame] | 6311 | LOGVRB("All \"%s\" schema nodes and constraints resolved.", mod->name); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6312 | unres->count = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6313 | return EXIT_SUCCESS; |
| 6314 | } |
| 6315 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6316 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6317 | * @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] | 6318 | * |
| 6319 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6320 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6321 | * @param[in] item Item to resolve. Type determined by \p type. |
| 6322 | * @param[in] type Type of the unresolved item. |
| 6323 | * @param[in] str String argument. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6324 | * |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 6325 | * @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] | 6326 | */ |
| 6327 | int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6328 | unres_schema_add_str(struct lys_module *mod, struct unres_schema *unres, void *item, enum UNRES_ITEM type, |
| 6329 | const char *str) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6330 | { |
Radek Krejci | 54081ce | 2016-08-12 15:21:47 +0200 | [diff] [blame] | 6331 | int rc; |
| 6332 | const char *dictstr; |
| 6333 | |
| 6334 | dictstr = lydict_insert(mod->ctx, str, 0); |
| 6335 | rc = unres_schema_add_node(mod, unres, item, type, (struct lys_node *)dictstr); |
| 6336 | |
| 6337 | if (rc == -1) { |
| 6338 | lydict_remove(mod->ctx, dictstr); |
| 6339 | } |
| 6340 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6341 | } |
| 6342 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6343 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6344 | * @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] | 6345 | * |
| 6346 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6347 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6348 | * @param[in] item Item to resolve. Type determined by \p type. |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6349 | * @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] | 6350 | * @param[in] snode Schema node argument. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6351 | * |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 6352 | * @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] | 6353 | */ |
| 6354 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 6355 | 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] | 6356 | struct lys_node *snode) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6357 | { |
Michal Vasko | ef486d7 | 2016-09-27 12:10:44 +0200 | [diff] [blame] | 6358 | int rc, log_hidden; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6359 | struct lyxml_elem *yin; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6360 | char *path, *msg; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6361 | |
Michal Vasko | 9bf425b | 2015-10-22 11:42:03 +0200 | [diff] [blame] | 6362 | assert(unres && item && ((type != UNRES_LEAFREF) && (type != UNRES_INSTID) && (type != UNRES_WHEN) |
| 6363 | && (type != UNRES_MUST))); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6364 | |
Michal Vasko | ef486d7 | 2016-09-27 12:10:44 +0200 | [diff] [blame] | 6365 | if (*ly_vlog_hide_location()) { |
| 6366 | log_hidden = 1; |
| 6367 | } else { |
| 6368 | log_hidden = 0; |
| 6369 | ly_vlog_hide(1); |
| 6370 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6371 | rc = resolve_unres_schema_item(mod, item, type, snode, unres); |
Michal Vasko | ef486d7 | 2016-09-27 12:10:44 +0200 | [diff] [blame] | 6372 | if (!log_hidden) { |
| 6373 | ly_vlog_hide(0); |
| 6374 | } |
| 6375 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6376 | if (rc != EXIT_FAILURE) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6377 | if (rc == -1 && ly_errno == LY_EVALID) { |
Radek Krejci | 76e15e1 | 2016-06-22 11:02:24 +0200 | [diff] [blame] | 6378 | if (ly_log_level >= LY_LLERR) { |
| 6379 | path = strdup(ly_errpath()); |
| 6380 | msg = strdup(ly_errmsg()); |
| 6381 | LOGERR(LY_EVALID, "%s%s%s%s", msg, path[0] ? " (path: " : "", path[0] ? path : "", path[0] ? ")" : ""); |
| 6382 | free(path); |
| 6383 | free(msg); |
| 6384 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6385 | } |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6386 | if (type == UNRES_LIST_UNIQ) { |
| 6387 | /* free the allocated structure */ |
| 6388 | free(item); |
Pavol Vican | 88e16c9 | 2016-09-07 15:41:50 +0200 | [diff] [blame] | 6389 | } else if (rc == -1 && type == UNRES_IFFEAT) { |
| 6390 | /* free the allocated resources */ |
| 6391 | free(*((char **)item)); |
| 6392 | } |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6393 | return rc; |
Radek Krejci | f347abc | 2016-06-22 10:18:47 +0200 | [diff] [blame] | 6394 | } else { |
| 6395 | /* erase info about validation errors */ |
| 6396 | ly_errno = LY_SUCCESS; |
| 6397 | ly_vecode = LYVE_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6398 | } |
| 6399 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6400 | print_unres_schema_item_fail(item, type, snode); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6401 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6402 | /* 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] | 6403 | if (type == UNRES_TYPE_DER || type == UNRES_TYPE_DER_TPDF) { |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6404 | yin = (struct lyxml_elem *)((struct lys_type *)item)->der; |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6405 | if (!(yin->flags & LY_YANG_STRUCTURE_FLAG)) { |
| 6406 | lyxml_unlink_elem(mod->ctx, yin, 1); |
| 6407 | ((struct lys_type *)item)->der = (struct lys_tpdf *)yin; |
| 6408 | } |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6409 | } |
| 6410 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6411 | unres->count++; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 6412 | unres->item = ly_realloc(unres->item, unres->count*sizeof *unres->item); |
| 6413 | if (!unres->item) { |
| 6414 | LOGMEM; |
| 6415 | return -1; |
| 6416 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6417 | unres->item[unres->count-1] = item; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 6418 | unres->type = ly_realloc(unres->type, unres->count*sizeof *unres->type); |
| 6419 | if (!unres->type) { |
| 6420 | LOGMEM; |
| 6421 | return -1; |
| 6422 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6423 | unres->type[unres->count-1] = type; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 6424 | unres->str_snode = ly_realloc(unres->str_snode, unres->count*sizeof *unres->str_snode); |
| 6425 | if (!unres->str_snode) { |
| 6426 | LOGMEM; |
| 6427 | return -1; |
| 6428 | } |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6429 | unres->str_snode[unres->count-1] = snode; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6430 | unres->module = ly_realloc(unres->module, unres->count*sizeof *unres->module); |
| 6431 | if (!unres->module) { |
| 6432 | LOGMEM; |
| 6433 | return -1; |
| 6434 | } |
| 6435 | unres->module[unres->count-1] = mod; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6436 | |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 6437 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6438 | } |
| 6439 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6440 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6441 | * @brief Duplicate an unres schema item. Logs indirectly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6442 | * |
| 6443 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6444 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6445 | * @param[in] item Old item to be resolved. |
| 6446 | * @param[in] type Type of the old unresolved item. |
| 6447 | * @param[in] new_item New item to use in the duplicate. |
| 6448 | * |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 6449 | * @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] | 6450 | */ |
Michal Vasko | dad1940 | 2015-08-06 09:51:53 +0200 | [diff] [blame] | 6451 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 6452 | 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] | 6453 | { |
| 6454 | int i; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6455 | struct unres_list_uniq aux_uniq; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6456 | struct unres_iffeat_data *iff_data; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6457 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6458 | 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] | 6459 | |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6460 | /* hack for UNRES_LIST_UNIQ, which stores multiple items behind its item */ |
| 6461 | if (type == UNRES_LIST_UNIQ) { |
| 6462 | aux_uniq.list = item; |
| 6463 | aux_uniq.expr = ((struct unres_list_uniq *)new_item)->expr; |
| 6464 | item = &aux_uniq; |
| 6465 | } |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 6466 | i = unres_schema_find(unres, -1, item, type); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6467 | |
| 6468 | if (i == -1) { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6469 | if (type == UNRES_LIST_UNIQ) { |
| 6470 | free(new_item); |
| 6471 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 6472 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6473 | } |
| 6474 | |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6475 | if ((type == UNRES_TYPE_LEAFREF) || (type == UNRES_USES) || (type == UNRES_TYPE_DFLT) || |
Radek Krejci | b69f323 | 2016-09-16 17:41:07 +0200 | [diff] [blame] | 6476 | (type == UNRES_FEATURE) || (type == UNRES_LIST_UNIQ)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6477 | 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] | 6478 | LOGINT; |
| 6479 | return -1; |
| 6480 | } |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6481 | } else if (type == UNRES_IFFEAT) { |
| 6482 | /* duplicate unres_iffeature_data */ |
| 6483 | iff_data = malloc(sizeof *iff_data); |
| 6484 | iff_data->fname = lydict_insert(mod->ctx, ((struct unres_iffeat_data *)unres->str_snode[i])->fname, 0); |
| 6485 | iff_data->node = ((struct unres_iffeat_data *)unres->str_snode[i])->node; |
| 6486 | if (unres_schema_add_node(mod, unres, new_item, type, (struct lys_node *)iff_data) == -1) { |
| 6487 | LOGINT; |
| 6488 | return -1; |
| 6489 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6490 | } else { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6491 | 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] | 6492 | LOGINT; |
| 6493 | return -1; |
| 6494 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6495 | } |
Michal Vasko | dad1940 | 2015-08-06 09:51:53 +0200 | [diff] [blame] | 6496 | |
| 6497 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6498 | } |
| 6499 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6500 | /* does not log */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6501 | int |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 6502 | 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] | 6503 | { |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 6504 | int i; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6505 | struct unres_list_uniq *aux_uniq1, *aux_uniq2; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6506 | |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 6507 | if (start_on_backwards > 0) { |
| 6508 | i = start_on_backwards; |
| 6509 | } else { |
| 6510 | i = unres->count - 1; |
| 6511 | } |
| 6512 | for (; i > -1; i--) { |
| 6513 | if (unres->type[i] != type) { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6514 | continue; |
| 6515 | } |
| 6516 | if (type != UNRES_LIST_UNIQ) { |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 6517 | if (unres->item[i] == item) { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6518 | break; |
| 6519 | } |
| 6520 | } else { |
| 6521 | aux_uniq1 = (struct unres_list_uniq *)unres->item[i - 1]; |
| 6522 | aux_uniq2 = (struct unres_list_uniq *)item; |
| 6523 | 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] | 6524 | break; |
| 6525 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6526 | } |
| 6527 | } |
| 6528 | |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 6529 | return i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6530 | } |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6531 | |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 6532 | static void |
| 6533 | unres_schema_free_item(struct ly_ctx *ctx, struct unres_schema *unres, uint32_t i) |
| 6534 | { |
| 6535 | struct lyxml_elem *yin; |
| 6536 | struct yang_type *yang; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6537 | struct unres_iffeat_data *iff_data; |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 6538 | |
| 6539 | switch (unres->type[i]) { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 6540 | case UNRES_TYPE_DER_TPDF: |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 6541 | case UNRES_TYPE_DER: |
| 6542 | yin = (struct lyxml_elem *)((struct lys_type *)unres->item[i])->der; |
| 6543 | if (yin->flags & LY_YANG_STRUCTURE_FLAG) { |
| 6544 | yang =(struct yang_type *)yin; |
| 6545 | yang->type->base = yang->base; |
| 6546 | lydict_remove(ctx, yang->name); |
| 6547 | free(yang); |
| 6548 | } else { |
| 6549 | lyxml_free(ctx, yin); |
| 6550 | } |
| 6551 | break; |
Pavol Vican | 88e16c9 | 2016-09-07 15:41:50 +0200 | [diff] [blame] | 6552 | case UNRES_IFFEAT: |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6553 | iff_data = (struct unres_iffeat_data *)unres->str_snode[i]; |
| 6554 | lydict_remove(ctx, iff_data->fname); |
| 6555 | free(unres->str_snode[i]); |
Pavol Vican | 88e16c9 | 2016-09-07 15:41:50 +0200 | [diff] [blame] | 6556 | break; |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 6557 | case UNRES_IDENT: |
| 6558 | case UNRES_TYPE_IDENTREF: |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 6559 | case UNRES_TYPE_DFLT: |
| 6560 | case UNRES_CHOICE_DFLT: |
| 6561 | case UNRES_LIST_KEYS: |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 6562 | lydict_remove(ctx, (const char *)unres->str_snode[i]); |
| 6563 | break; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6564 | case UNRES_LIST_UNIQ: |
| 6565 | free(unres->item[i]); |
| 6566 | break; |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 6567 | default: |
| 6568 | break; |
| 6569 | } |
| 6570 | unres->type[i] = UNRES_RESOLVED; |
| 6571 | } |
| 6572 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6573 | void |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6574 | unres_schema_free(struct lys_module *module, struct unres_schema **unres) |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6575 | { |
| 6576 | uint32_t i; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6577 | unsigned int unresolved = 0; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6578 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6579 | if (!unres || !(*unres)) { |
| 6580 | return; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6581 | } |
| 6582 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6583 | assert(module || (*unres)->count == 0); |
| 6584 | |
| 6585 | for (i = 0; i < (*unres)->count; ++i) { |
| 6586 | if ((*unres)->module[i] != module) { |
| 6587 | if ((*unres)->type[i] != UNRES_RESOLVED) { |
| 6588 | unresolved++; |
| 6589 | } |
| 6590 | continue; |
| 6591 | } |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 6592 | |
| 6593 | /* free heap memory for the specific item */ |
| 6594 | unres_schema_free_item(module->ctx, *unres, i); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6595 | } |
| 6596 | |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 6597 | /* free it all */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6598 | if (!module || (!unresolved && !module->type)) { |
| 6599 | free((*unres)->item); |
| 6600 | free((*unres)->type); |
| 6601 | free((*unres)->str_snode); |
| 6602 | free((*unres)->module); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6603 | free((*unres)); |
| 6604 | (*unres) = NULL; |
| 6605 | } |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6606 | } |
| 6607 | |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 6608 | static int |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6609 | resolve_leafref(struct lyd_node_leaf_list *leaf, struct lys_type *type) |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 6610 | { |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 6611 | struct unres_data matches; |
| 6612 | uint32_t i; |
| 6613 | |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6614 | assert(type->base == LY_TYPE_LEAFREF); |
| 6615 | |
| 6616 | /* init */ |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 6617 | memset(&matches, 0, sizeof matches); |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 6618 | |
| 6619 | /* EXIT_FAILURE return keeps leaf->value.lefref NULL, handled later */ |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6620 | 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] | 6621 | return -1; |
| 6622 | } |
| 6623 | |
| 6624 | /* check that value matches */ |
| 6625 | for (i = 0; i < matches.count; ++i) { |
| 6626 | if (ly_strequal(leaf->value_str, ((struct lyd_node_leaf_list *)matches.node[i])->value_str, 1)) { |
| 6627 | leaf->value.leafref = matches.node[i]; |
| 6628 | break; |
| 6629 | } |
| 6630 | } |
| 6631 | |
| 6632 | free(matches.node); |
| 6633 | |
| 6634 | if (!leaf->value.leafref) { |
| 6635 | /* reference not found */ |
| 6636 | if (type->info.lref.req > -1) { |
| 6637 | LOGVAL(LYE_NOLEAFREF, LY_VLOG_LYD, leaf, type->info.lref.path, leaf->value_str); |
| 6638 | return EXIT_FAILURE; |
| 6639 | } else { |
| 6640 | LOGVRB("There is no leafref with the value \"%s\", but it is not required.", leaf->value_str); |
| 6641 | } |
| 6642 | } |
| 6643 | |
| 6644 | return EXIT_SUCCESS; |
| 6645 | } |
| 6646 | |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6647 | API LY_DATA_TYPE |
| 6648 | lyd_leaf_type(const struct lyd_node_leaf_list *leaf) |
| 6649 | { |
| 6650 | struct lyd_node *node; |
| 6651 | struct lys_type *type, *type_iter; |
| 6652 | lyd_val value; |
| 6653 | int f = 0, r; |
| 6654 | |
| 6655 | if (!leaf || !(leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 6656 | return LY_TYPE_ERR; |
| 6657 | } |
| 6658 | |
| 6659 | if (leaf->value_type > 0 && (leaf->value_type & LY_DATA_TYPE_MASK) != LY_TYPE_UNION && |
| 6660 | (leaf->value_type & LY_DATA_TYPE_MASK) != LY_TYPE_LEAFREF) { |
| 6661 | /* we can get the type directly from the data node (it was already resolved) */ |
| 6662 | return leaf->value_type & LY_DATA_TYPE_MASK; |
| 6663 | } |
| 6664 | |
| 6665 | /* init */ |
| 6666 | type = &((struct lys_node_leaf *)leaf->schema)->type; |
| 6667 | value = leaf->value; |
| 6668 | ly_vlog_hide(1); |
| 6669 | |
| 6670 | /* resolve until we get the real data type */ |
| 6671 | while (1) { |
| 6672 | /* get the correct data type from schema */ |
| 6673 | switch (type->base) { |
| 6674 | case LY_TYPE_LEAFREF: |
| 6675 | type = &type->info.lref.target->type; |
| 6676 | break; /* continue in while loop */ |
| 6677 | case LY_TYPE_UNION: |
| 6678 | type_iter = NULL; |
| 6679 | while ((type_iter = lyp_get_next_union_type(type, type_iter, &f))) { |
| 6680 | if (type_iter->base == LY_TYPE_LEAFREF) { |
| 6681 | if (type_iter->info.lref.req == -1) { |
| 6682 | /* target not required, so it always succeeds */ |
| 6683 | break; |
| 6684 | } else { |
| 6685 | /* try to resolve leafref */ |
| 6686 | memset(&((struct lyd_node_leaf_list *)leaf)->value, 0, sizeof leaf->value); |
| 6687 | r = resolve_leafref((struct lyd_node_leaf_list *)leaf, type_iter); |
| 6688 | /* revert leaf's content affected by resolve_leafref */ |
| 6689 | ((struct lyd_node_leaf_list *)leaf)->value = value; |
| 6690 | if (!r) { |
| 6691 | /* success, we can continue with the leafref type */ |
| 6692 | break; |
| 6693 | } |
| 6694 | } |
| 6695 | } else if (type_iter->base == LY_TYPE_INST) { |
| 6696 | if (type_iter->info.inst.req == -1) { |
| 6697 | /* target not required, so it always succeeds */ |
| 6698 | return LY_TYPE_INST; |
| 6699 | } else { |
| 6700 | /* try to resolve instance-identifier */ |
| 6701 | ly_errno = 0; |
| 6702 | node = resolve_instid((struct lyd_node *)leaf, leaf->value_str); |
| 6703 | if (!ly_errno && node) { |
| 6704 | /* the real type is instance-identifier */ |
| 6705 | return LY_TYPE_INST; |
| 6706 | } |
| 6707 | } |
| 6708 | } else { |
Radek Krejci | f9837a8 | 2016-10-27 10:14:05 +0200 | [diff] [blame] | 6709 | r = lyp_parse_value_type((struct lyd_node_leaf_list *)leaf, type_iter, NULL, 1); |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6710 | /* revert leaf's content affected by resolve_leafref */ |
| 6711 | ((struct lyd_node_leaf_list *)leaf)->value = value; |
| 6712 | if (!r) { |
| 6713 | /* we have the real type */ |
| 6714 | return type_iter->base; |
| 6715 | } |
| 6716 | } |
| 6717 | f = 0; |
| 6718 | } |
| 6719 | /* erase ly_errno and ly_vecode */ |
| 6720 | ly_errno = LY_SUCCESS; |
| 6721 | ly_vecode = LYVE_SUCCESS; |
| 6722 | |
| 6723 | if (!type_iter) { |
| 6724 | LOGERR(LY_EINVAL, "Unable to get type from union \"%s\" with no valid type.", type->parent->name) |
| 6725 | return LY_TYPE_ERR; |
| 6726 | } |
| 6727 | type = type_iter; |
| 6728 | break; |
| 6729 | default: |
| 6730 | /* we have the real type */ |
| 6731 | ly_vlog_hide(0); |
| 6732 | return type->base; |
| 6733 | } |
| 6734 | } |
| 6735 | |
| 6736 | ly_vlog_hide(0); |
| 6737 | return LY_TYPE_ERR; |
| 6738 | } |
| 6739 | |
| 6740 | static int |
| 6741 | resolve_union(struct lyd_node_leaf_list *leaf, struct lys_type *type) |
| 6742 | { |
| 6743 | struct lys_type *datatype = NULL; |
| 6744 | int f = 0; |
| 6745 | |
| 6746 | assert(type->base == LY_TYPE_UNION); |
| 6747 | |
| 6748 | memset(&leaf->value, 0, sizeof leaf->value); |
| 6749 | while ((datatype = lyp_get_next_union_type(type, datatype, &f))) { |
| 6750 | leaf->value_type = datatype->base; |
| 6751 | |
| 6752 | if (datatype->base == LY_TYPE_LEAFREF) { |
| 6753 | /* try to resolve leafref */ |
| 6754 | if (!resolve_leafref(leaf, datatype)) { |
| 6755 | /* success */ |
| 6756 | break; |
| 6757 | } |
| 6758 | } else if (datatype->base == LY_TYPE_INST) { |
| 6759 | /* try to resolve instance-identifier */ |
| 6760 | ly_errno = 0; |
| 6761 | leaf->value.instance = resolve_instid((struct lyd_node *)leaf, leaf->value_str); |
| 6762 | if (!ly_errno && (leaf->value.instance || datatype->info.inst.req == -1)) { |
| 6763 | /* success */ |
| 6764 | break; |
| 6765 | } |
| 6766 | } else { |
Radek Krejci | f9837a8 | 2016-10-27 10:14:05 +0200 | [diff] [blame] | 6767 | if (!lyp_parse_value_type(leaf, datatype, NULL, 1)) { |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6768 | /* success */ |
| 6769 | break; |
| 6770 | } |
| 6771 | } |
| 6772 | f = 0; |
| 6773 | } |
| 6774 | /* erase ly_errno and ly_vecode */ |
| 6775 | ly_errno = LY_SUCCESS; |
| 6776 | ly_vecode = LYVE_SUCCESS; |
| 6777 | |
| 6778 | if (!datatype) { |
| 6779 | /* failure */ |
| 6780 | LOGVAL(LYE_INVAL, LY_VLOG_LYD, leaf, (leaf->value_str ? leaf->value_str : ""), leaf->schema->name); |
| 6781 | return EXIT_FAILURE; |
| 6782 | } |
| 6783 | |
| 6784 | return EXIT_SUCCESS; |
| 6785 | } |
| 6786 | |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6787 | /** |
| 6788 | * @brief Resolve a single unres data item. Logs directly. |
| 6789 | * |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6790 | * @param[in] node Data node to resolve. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6791 | * @param[in] type Type of the unresolved item. |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6792 | * |
| 6793 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 6794 | */ |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 6795 | int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6796 | resolve_unres_data_item(struct lyd_node *node, enum UNRES_ITEM type) |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6797 | { |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 6798 | int rc; |
Michal Vasko | 83a6c46 | 2015-10-08 16:43:53 +0200 | [diff] [blame] | 6799 | struct lyd_node_leaf_list *leaf; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6800 | struct lys_node_leaf *sleaf; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6801 | |
Michal Vasko | 83a6c46 | 2015-10-08 16:43:53 +0200 | [diff] [blame] | 6802 | leaf = (struct lyd_node_leaf_list *)node; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6803 | sleaf = (struct lys_node_leaf *)leaf->schema; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6804 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6805 | switch (type) { |
| 6806 | case UNRES_LEAFREF: |
| 6807 | assert(sleaf->type.base == LY_TYPE_LEAFREF); |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6808 | return resolve_leafref(leaf, &sleaf->type); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6809 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6810 | case UNRES_INSTID: |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 6811 | assert(sleaf->type.base == LY_TYPE_INST); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6812 | ly_errno = 0; |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6813 | leaf->value.instance = resolve_instid(node, leaf->value_str); |
Radek Krejci | 40f17b9 | 2016-02-03 14:30:43 +0100 | [diff] [blame] | 6814 | if (!leaf->value.instance) { |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6815 | if (ly_errno) { |
| 6816 | return -1; |
| 6817 | } else if (sleaf->type.info.inst.req > -1) { |
Michal Vasko | 6ac6828 | 2016-04-11 10:56:47 +0200 | [diff] [blame] | 6818 | LOGVAL(LYE_NOREQINS, LY_VLOG_LYD, leaf, leaf->value_str); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6819 | return EXIT_FAILURE; |
| 6820 | } else { |
Michal Vasko | 9925e28 | 2016-09-02 12:45:14 +0200 | [diff] [blame] | 6821 | 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] | 6822 | } |
| 6823 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6824 | break; |
| 6825 | |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 6826 | case UNRES_UNION: |
| 6827 | assert(sleaf->type.base == LY_TYPE_UNION); |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6828 | return resolve_union(leaf, &sleaf->type); |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 6829 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6830 | case UNRES_WHEN: |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 6831 | if ((rc = resolve_when(node, NULL))) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6832 | return rc; |
| 6833 | } |
| 6834 | break; |
| 6835 | |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 6836 | case UNRES_MUST: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6837 | if ((rc = resolve_must(node, 0))) { |
| 6838 | return rc; |
| 6839 | } |
| 6840 | break; |
| 6841 | |
| 6842 | case UNRES_MUST_INOUT: |
| 6843 | if ((rc = resolve_must(node, 1))) { |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 6844 | return rc; |
| 6845 | } |
| 6846 | break; |
| 6847 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6848 | default: |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6849 | LOGINT; |
| 6850 | return -1; |
| 6851 | } |
| 6852 | |
| 6853 | return EXIT_SUCCESS; |
| 6854 | } |
| 6855 | |
| 6856 | /** |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6857 | * @brief add data unres item |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6858 | * |
| 6859 | * @param[in] unres Unres data structure to use. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6860 | * @param[in] node Data node to use. |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6861 | * |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6862 | * @return 0 on success, -1 on error. |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6863 | */ |
| 6864 | int |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6865 | 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] | 6866 | { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6867 | assert(unres && node); |
Michal Vasko | c428084 | 2016-04-19 16:10:42 +0200 | [diff] [blame] | 6868 | assert((type == UNRES_LEAFREF) || (type == UNRES_INSTID) || (type == UNRES_WHEN) || (type == UNRES_MUST) |
Radek Krejci | bacc744 | 2016-10-27 13:39:56 +0200 | [diff] [blame] | 6869 | || (type == UNRES_MUST_INOUT) || (type == UNRES_UNION)); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6870 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6871 | unres->count++; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 6872 | unres->node = ly_realloc(unres->node, unres->count * sizeof *unres->node); |
| 6873 | if (!unres->node) { |
| 6874 | LOGMEM; |
| 6875 | return -1; |
| 6876 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6877 | unres->node[unres->count - 1] = node; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 6878 | unres->type = ly_realloc(unres->type, unres->count * sizeof *unres->type); |
| 6879 | if (!unres->type) { |
| 6880 | LOGMEM; |
| 6881 | return -1; |
| 6882 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6883 | unres->type[unres->count - 1] = type; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6884 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6885 | if (type == UNRES_WHEN) { |
| 6886 | /* remove previous result */ |
| 6887 | node->when_status = LYD_WHEN; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6888 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6889 | |
| 6890 | return EXIT_SUCCESS; |
| 6891 | } |
| 6892 | |
| 6893 | /** |
| 6894 | * @brief Resolve every unres data item in the structure. Logs directly. |
| 6895 | * |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 6896 | * If options includes LYD_OPT_TRUSTED, the data are considered trusted (when, must conditions are not expected, |
| 6897 | * unresolved leafrefs/instids are accepted). |
| 6898 | * |
| 6899 | * If options includes LYD_OPT_NOAUTODEL, the false resulting when condition on non-default nodes, the error is raised. |
| 6900 | * |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6901 | * @param[in] unres Unres data structure to use. |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 6902 | * @param[in,out] root Root node of the data tree, can be changed due to autodeletion. |
| 6903 | * @param[in] options Data options as described above. |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6904 | * |
| 6905 | * @return EXIT_SUCCESS on success, -1 on error. |
| 6906 | */ |
| 6907 | int |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 6908 | 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] | 6909 | { |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 6910 | 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] | 6911 | int rc, progress; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6912 | char *msg, *path; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6913 | struct lyd_node *parent; |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 6914 | struct lyd_node_leaf_list *leaf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6915 | |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 6916 | assert(root); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6917 | assert(unres); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6918 | |
| 6919 | if (!unres->count) { |
| 6920 | return EXIT_SUCCESS; |
| 6921 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6922 | |
Michal Vasko | a0ffcab | 2016-05-02 14:52:08 +0200 | [diff] [blame] | 6923 | LOGVRB("Resolving unresolved data nodes and their constraints..."); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6924 | ly_vlog_hide(1); |
| 6925 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6926 | /* when-stmt first */ |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6927 | ly_errno = LY_SUCCESS; |
| 6928 | ly_vecode = LYVE_SUCCESS; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6929 | do { |
| 6930 | progress = 0; |
Michal Vasko | 6df9413 | 2016-09-22 11:08:09 +0200 | [diff] [blame] | 6931 | for (i = 0; i < unres->count; i++) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6932 | if (unres->type[i] != UNRES_WHEN) { |
| 6933 | continue; |
| 6934 | } |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 6935 | assert(!(options & LYD_OPT_TRUSTED)); |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 6936 | if (first) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6937 | /* count when-stmt nodes in unres list */ |
| 6938 | when_stmt++; |
| 6939 | } |
| 6940 | |
| 6941 | /* resolve when condition only when all parent when conditions are already resolved */ |
| 6942 | for (parent = unres->node[i]->parent; |
| 6943 | parent && LYD_WHEN_DONE(parent->when_status); |
| 6944 | parent = parent->parent) { |
| 6945 | if (!parent->parent && (parent->when_status & LYD_WHEN_FALSE)) { |
| 6946 | /* the parent node was already unlinked, do not resolve this node, |
| 6947 | * it will be removed anyway, so just mark it as resolved |
| 6948 | */ |
| 6949 | unres->node[i]->when_status |= LYD_WHEN_FALSE; |
| 6950 | unres->type[i] = UNRES_RESOLVED; |
| 6951 | resolved++; |
| 6952 | break; |
| 6953 | } |
| 6954 | } |
| 6955 | if (parent) { |
| 6956 | continue; |
| 6957 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6958 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6959 | rc = resolve_unres_data_item(unres->node[i], unres->type[i]); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6960 | if (!rc) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6961 | if (unres->node[i]->when_status & LYD_WHEN_FALSE) { |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 6962 | if ((options & LYD_OPT_NOAUTODEL) && !unres->node[i]->dflt) { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6963 | /* false when condition */ |
| 6964 | ly_vlog_hide(0); |
Radek Krejci | 76e15e1 | 2016-06-22 11:02:24 +0200 | [diff] [blame] | 6965 | if (ly_log_level >= LY_LLERR) { |
| 6966 | path = strdup(ly_errpath()); |
| 6967 | msg = strdup(ly_errmsg()); |
| 6968 | LOGERR(LY_EVALID, "%s%s%s%s", msg, |
| 6969 | path[0] ? " (path: " : "", path[0] ? path : "", path[0] ? ")" : ""); |
| 6970 | free(path); |
| 6971 | free(msg); |
| 6972 | } |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6973 | return -1; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6974 | } /* follows else */ |
| 6975 | |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 6976 | /* only unlink now, the subtree can contain another nodes stored in the unres list */ |
| 6977 | /* if it has parent non-presence containers that would be empty, we should actually |
| 6978 | * remove the container |
| 6979 | */ |
Radek Krejci | 2537fd3 | 2016-09-07 16:22:41 +0200 | [diff] [blame] | 6980 | for (parent = unres->node[i]; |
| 6981 | parent->parent && parent->parent->schema->nodetype == LYS_CONTAINER; |
| 6982 | parent = parent->parent) { |
| 6983 | if (((struct lys_node_container *)parent->parent->schema)->presence) { |
| 6984 | /* presence container */ |
| 6985 | break; |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 6986 | } |
Radek Krejci | 2537fd3 | 2016-09-07 16:22:41 +0200 | [diff] [blame] | 6987 | if (parent->next || parent->prev != parent) { |
| 6988 | /* non empty (the child we are in and we are going to remove is not the only child) */ |
| 6989 | break; |
| 6990 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 6991 | } |
Radek Krejci | 2537fd3 | 2016-09-07 16:22:41 +0200 | [diff] [blame] | 6992 | unres->node[i] = parent; |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 6993 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6994 | /* auto-delete */ |
| 6995 | LOGVRB("auto-delete node \"%s\" due to when condition (%s)", ly_errpath(), |
| 6996 | ((struct lys_node_leaf *)unres->node[i]->schema)->when->cond); |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 6997 | if (*root && *root == unres->node[i]) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6998 | *root = (*root)->next; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6999 | } |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7000 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7001 | lyd_unlink(unres->node[i]); |
| 7002 | unres->type[i] = UNRES_DELETE; |
| 7003 | del_items++; |
Radek Krejci | 51fd816 | 2016-03-24 15:49:51 +0100 | [diff] [blame] | 7004 | |
| 7005 | /* update the rest of unres items */ |
| 7006 | for (j = 0; j < unres->count; j++) { |
Radek Krejci | 3db819b | 2016-03-24 16:29:48 +0100 | [diff] [blame] | 7007 | if (unres->type[j] == UNRES_RESOLVED || unres->type[j] == UNRES_DELETE) { |
Radek Krejci | 51fd816 | 2016-03-24 15:49:51 +0100 | [diff] [blame] | 7008 | continue; |
| 7009 | } |
| 7010 | |
| 7011 | /* test if the node is in subtree to be deleted */ |
| 7012 | for (parent = unres->node[j]; parent; parent = parent->parent) { |
| 7013 | if (parent == unres->node[i]) { |
| 7014 | /* yes, it is */ |
| 7015 | unres->type[j] = UNRES_RESOLVED; |
| 7016 | resolved++; |
| 7017 | break; |
| 7018 | } |
| 7019 | } |
| 7020 | } |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7021 | } else { |
| 7022 | unres->type[i] = UNRES_RESOLVED; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7023 | } |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7024 | ly_errno = LY_SUCCESS; |
| 7025 | ly_vecode = LYVE_SUCCESS; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7026 | resolved++; |
| 7027 | progress = 1; |
| 7028 | } else if (rc == -1) { |
| 7029 | ly_vlog_hide(0); |
Michal Vasko | 76e7340 | 2016-08-24 16:00:13 +0200 | [diff] [blame] | 7030 | /* print only this last error */ |
| 7031 | resolve_unres_data_item(unres->node[i], unres->type[i]); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7032 | return -1; |
Radek Krejci | c2a180f | 2016-06-22 13:28:16 +0200 | [diff] [blame] | 7033 | } else { |
| 7034 | /* forward reference, erase ly_errno */ |
| 7035 | ly_errno = LY_SUCCESS; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7036 | } |
| 7037 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7038 | first = 0; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7039 | } while (progress && resolved < when_stmt); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7040 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7041 | /* do we have some unresolved when-stmt? */ |
Radek Krejci | d940d73 | 2016-03-24 16:02:28 +0100 | [diff] [blame] | 7042 | if (when_stmt > resolved) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7043 | ly_vlog_hide(0); |
Radek Krejci | 76e15e1 | 2016-06-22 11:02:24 +0200 | [diff] [blame] | 7044 | if (ly_log_level >= LY_LLERR) { |
| 7045 | path = strdup(ly_errpath()); |
| 7046 | msg = strdup(ly_errmsg()); |
| 7047 | LOGERR(LY_EVALID, "%s%s%s%s", msg, path[0] ? " (path: " : "", path[0] ? path : "", path[0] ? ")" : ""); |
| 7048 | free(path); |
| 7049 | free(msg); |
| 7050 | } |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7051 | return -1; |
| 7052 | } |
| 7053 | |
| 7054 | for (i = 0; del_items && i < unres->count; i++) { |
| 7055 | /* we had some when-stmt resulted to false, so now we have to sanitize the unres list */ |
| 7056 | if (unres->type[i] != UNRES_DELETE) { |
| 7057 | continue; |
| 7058 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7059 | if (!unres->node[i]) { |
| 7060 | unres->type[i] = UNRES_RESOLVED; |
| 7061 | del_items--; |
| 7062 | continue; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7063 | } |
| 7064 | |
| 7065 | /* really remove the complete subtree */ |
| 7066 | lyd_free(unres->node[i]); |
| 7067 | unres->type[i] = UNRES_RESOLVED; |
| 7068 | del_items--; |
| 7069 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7070 | |
| 7071 | /* rest */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7072 | for (i = 0; i < unres->count; ++i) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7073 | if (unres->type[i] == UNRES_RESOLVED) { |
| 7074 | continue; |
| 7075 | } |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 7076 | 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] | 7077 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7078 | rc = resolve_unres_data_item(unres->node[i], unres->type[i]); |
Michal Vasko | 6df9413 | 2016-09-22 11:08:09 +0200 | [diff] [blame] | 7079 | if (rc == -1) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7080 | ly_vlog_hide(0); |
Michal Vasko | 96b846c | 2016-05-18 13:28:58 +0200 | [diff] [blame] | 7081 | /* print only this last error */ |
| 7082 | resolve_unres_data_item(unres->node[i], unres->type[i]); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7083 | return -1; |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 7084 | } 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] | 7085 | unres->type[i] = UNRES_RESOLVED; |
| 7086 | resolved++; |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 7087 | if (options & LYD_OPT_TRUSTED) { |
Michal Vasko | 6df9413 | 2016-09-22 11:08:09 +0200 | [diff] [blame] | 7088 | /* accept it in this case */ |
| 7089 | if (unres->type[i] == UNRES_LEAFREF) { |
| 7090 | LOGVRB("Leafref \"%s\" with value \"%s\" failed to be resolved.", |
| 7091 | ((struct lys_node_leaf *)unres->node[i]->schema)->type.info.lref.path, |
| 7092 | ((struct lyd_node_leaf_list *)unres->node[i])->value_str); |
| 7093 | } else { |
| 7094 | LOGVRB("Instance identifier \"%s\" failed to be resolved.", |
| 7095 | ((struct lyd_node_leaf_list *)unres->node[i])->value_str); |
| 7096 | } |
| 7097 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7098 | } |
| 7099 | } |
| 7100 | |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7101 | ly_vlog_hide(0); |
| 7102 | if (resolved < unres->count) { |
| 7103 | /* try to resolve the unresolved data again, it will not resolve anything, but it will print |
| 7104 | * all the validation errors |
| 7105 | */ |
| 7106 | for (i = 0; i < unres->count; ++i) { |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7107 | if (unres->type[i] == UNRES_UNION) { |
| 7108 | /* does not make sense to print specific errors for all |
| 7109 | * the data types, just print that the value is invalid */ |
| 7110 | leaf = (struct lyd_node_leaf_list *)unres->node[i]; |
| 7111 | LOGVAL(LYE_INVAL, LY_VLOG_LYD, unres->node[i], (leaf->value_str ? leaf->value_str : ""), |
| 7112 | leaf->schema->name); |
| 7113 | } else if (unres->type[i] != UNRES_RESOLVED) { |
| 7114 | resolve_unres_data_item(unres->node[i], unres->type[i]); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7115 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7116 | } |
| 7117 | return -1; |
| 7118 | } |
| 7119 | |
Michal Vasko | a0ffcab | 2016-05-02 14:52:08 +0200 | [diff] [blame] | 7120 | LOGVRB("All data nodes and constraints resolved."); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7121 | unres->count = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7122 | return EXIT_SUCCESS; |
| 7123 | } |