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 | 6966ea6 | 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 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 485 | * @param[in] id Identifier to use. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 486 | * @param[out] prefix Points to the prefix, NULL if there is not any. |
| 487 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 488 | * @param[out] name Points to the node name. |
| 489 | * @param[out] nam_len Length of the node name. |
| 490 | * @param[out] parent_times Number of ".." in the path. Must be 0 on the first call, |
| 491 | * must not be changed between consecutive calls. -1 if the |
| 492 | * path is relative. |
| 493 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 494 | * |
| 495 | * @return Number of characters successfully parsed, |
| 496 | * positive on success, negative on failure. |
| 497 | */ |
| 498 | static int |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 499 | parse_path_arg(const char *id, const char **prefix, int *pref_len, const char **name, int *nam_len, int *parent_times, |
| 500 | int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 501 | { |
| 502 | int parsed = 0, ret, par_times = 0; |
| 503 | |
| 504 | assert(id); |
| 505 | assert(parent_times); |
| 506 | if (prefix) { |
| 507 | *prefix = NULL; |
| 508 | } |
| 509 | if (pref_len) { |
| 510 | *pref_len = 0; |
| 511 | } |
| 512 | if (name) { |
| 513 | *name = NULL; |
| 514 | } |
| 515 | if (nam_len) { |
| 516 | *nam_len = 0; |
| 517 | } |
| 518 | if (has_predicate) { |
| 519 | *has_predicate = 0; |
| 520 | } |
| 521 | |
| 522 | if (!*parent_times && !strncmp(id, "..", 2)) { |
| 523 | ++par_times; |
| 524 | |
| 525 | parsed += 2; |
| 526 | id += 2; |
| 527 | |
| 528 | while (!strncmp(id, "/..", 3)) { |
| 529 | ++par_times; |
| 530 | |
| 531 | parsed += 3; |
| 532 | id += 3; |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | if (!*parent_times) { |
| 537 | if (par_times) { |
| 538 | *parent_times = par_times; |
| 539 | } else { |
| 540 | *parent_times = -1; |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | if (id[0] != '/') { |
| 545 | return -parsed; |
| 546 | } |
| 547 | |
| 548 | /* skip '/' */ |
| 549 | ++parsed; |
| 550 | ++id; |
| 551 | |
| 552 | /* node-identifier ([prefix:]identifier) */ |
| 553 | if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len)) < 1) { |
| 554 | return -parsed-ret; |
| 555 | } |
| 556 | |
| 557 | parsed += ret; |
| 558 | id += ret; |
| 559 | |
| 560 | /* there is no predicate */ |
| 561 | if ((id[0] == '/') || !id[0]) { |
| 562 | return parsed; |
| 563 | } else if (id[0] != '[') { |
| 564 | return -parsed; |
| 565 | } |
| 566 | |
| 567 | if (has_predicate) { |
| 568 | *has_predicate = 1; |
| 569 | } |
| 570 | |
| 571 | return parsed; |
| 572 | } |
| 573 | |
| 574 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 575 | * @brief Parse instance-identifier in JSON data format. That means that prefixes |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 576 | * (which are mandatory for every node-identifier) are actually model names. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 577 | * |
| 578 | * instance-identifier = 1*("/" (node-identifier *predicate)) |
| 579 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 580 | * @param[in] id Identifier to use. |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 581 | * @param[out] model Points to the model name. |
| 582 | * @param[out] mod_len Length of the model name. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 583 | * @param[out] name Points to the node name. |
| 584 | * @param[out] nam_len Length of the node name. |
| 585 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 586 | * |
| 587 | * @return Number of characters successfully parsed, |
| 588 | * positive on success, negative on failure. |
| 589 | */ |
| 590 | static int |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 591 | parse_instance_identifier(const char *id, const char **model, int *mod_len, const char **name, int *nam_len, |
| 592 | int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 593 | { |
| 594 | int parsed = 0, ret; |
| 595 | |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 596 | if (has_predicate) { |
| 597 | *has_predicate = 0; |
| 598 | } |
| 599 | |
| 600 | if (id[0] != '/') { |
| 601 | return -parsed; |
| 602 | } |
| 603 | |
| 604 | ++parsed; |
| 605 | ++id; |
| 606 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 607 | if ((ret = parse_identifier(id)) < 1) { |
| 608 | return ret; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 609 | } |
| 610 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 611 | *model = id; |
| 612 | *mod_len = ret; |
| 613 | |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 614 | parsed += ret; |
| 615 | id += ret; |
| 616 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 617 | if (id[0] != ':') { |
| 618 | return -parsed; |
| 619 | } |
| 620 | |
| 621 | ++parsed; |
| 622 | ++id; |
| 623 | |
| 624 | if ((ret = parse_identifier(id)) < 1) { |
| 625 | return ret; |
| 626 | } |
| 627 | |
| 628 | *name = id; |
| 629 | *nam_len = ret; |
| 630 | |
| 631 | parsed += ret; |
| 632 | id += ret; |
| 633 | |
Radek Krejci | 4967cb6 | 2016-09-14 16:40:28 +0200 | [diff] [blame] | 634 | if (id[0] == '[' && has_predicate) { |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 635 | *has_predicate = 1; |
| 636 | } |
| 637 | |
| 638 | return parsed; |
| 639 | } |
| 640 | |
| 641 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 642 | * @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] | 643 | * (which are mandatory) are actually model names. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 644 | * |
| 645 | * predicate = "[" *WSP (predicate-expr / pos) *WSP "]" |
| 646 | * predicate-expr = (node-identifier / ".") *WSP "=" *WSP |
| 647 | * ((DQUOTE string DQUOTE) / |
| 648 | * (SQUOTE string SQUOTE)) |
| 649 | * pos = non-negative-integer-value |
| 650 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 651 | * @param[in] id Identifier to use. |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 652 | * @param[out] model Points to the model name. |
| 653 | * @param[out] mod_len Length of the model name. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 654 | * @param[out] name Points to the node name. Can be identifier (from node-identifier), "." or pos. |
| 655 | * @param[out] nam_len Length of the node name. |
| 656 | * @param[out] value Value the node-identifier must have (string from the grammar), |
| 657 | * NULL if there is not any. |
| 658 | * @param[out] val_len Length of the value, 0 if there is not any. |
| 659 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 660 | * |
| 661 | * @return Number of characters successfully parsed, |
| 662 | * positive on success, negative on failure. |
| 663 | */ |
| 664 | static int |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 665 | parse_predicate(const char *id, const char **model, int *mod_len, const char **name, int *nam_len, |
| 666 | const char **value, int *val_len, int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 667 | { |
| 668 | const char *ptr; |
| 669 | int parsed = 0, ret; |
| 670 | char quote; |
| 671 | |
| 672 | assert(id); |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 673 | if (model) { |
| 674 | *model = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 675 | } |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 676 | if (mod_len) { |
| 677 | *mod_len = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 678 | } |
| 679 | if (name) { |
| 680 | *name = NULL; |
| 681 | } |
| 682 | if (nam_len) { |
| 683 | *nam_len = 0; |
| 684 | } |
| 685 | if (value) { |
| 686 | *value = NULL; |
| 687 | } |
| 688 | if (val_len) { |
| 689 | *val_len = 0; |
| 690 | } |
| 691 | if (has_predicate) { |
| 692 | *has_predicate = 0; |
| 693 | } |
| 694 | |
| 695 | if (id[0] != '[') { |
| 696 | return -parsed; |
| 697 | } |
| 698 | |
| 699 | ++parsed; |
| 700 | ++id; |
| 701 | |
| 702 | while (isspace(id[0])) { |
| 703 | ++parsed; |
| 704 | ++id; |
| 705 | } |
| 706 | |
| 707 | /* pos */ |
| 708 | if (isdigit(id[0])) { |
| 709 | if (name) { |
| 710 | *name = id; |
| 711 | } |
| 712 | |
| 713 | if (id[0] == '0') { |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 714 | return -parsed; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | while (isdigit(id[0])) { |
| 718 | ++parsed; |
| 719 | ++id; |
| 720 | } |
| 721 | |
| 722 | if (nam_len) { |
| 723 | *nam_len = id-(*name); |
| 724 | } |
| 725 | |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 726 | /* "." or node-identifier */ |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 727 | } else { |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 728 | if (id[0] == '.') { |
| 729 | if (name) { |
| 730 | *name = id; |
| 731 | } |
| 732 | if (nam_len) { |
| 733 | *nam_len = 1; |
| 734 | } |
| 735 | |
| 736 | ++parsed; |
| 737 | ++id; |
| 738 | |
| 739 | } else { |
| 740 | if ((ret = parse_node_identifier(id, model, mod_len, name, nam_len)) < 1) { |
| 741 | return -parsed+ret; |
| 742 | } else if (model && !*model) { |
| 743 | return -parsed; |
| 744 | } |
| 745 | |
| 746 | parsed += ret; |
| 747 | id += ret; |
| 748 | } |
| 749 | |
| 750 | while (isspace(id[0])) { |
| 751 | ++parsed; |
| 752 | ++id; |
| 753 | } |
| 754 | |
| 755 | if (id[0] != '=') { |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 756 | return -parsed; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 757 | } |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 758 | |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 759 | ++parsed; |
| 760 | ++id; |
| 761 | |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 762 | while (isspace(id[0])) { |
| 763 | ++parsed; |
| 764 | ++id; |
| 765 | } |
| 766 | |
| 767 | /* ((DQUOTE string DQUOTE) / (SQUOTE string SQUOTE)) */ |
| 768 | if ((id[0] == '\"') || (id[0] == '\'')) { |
| 769 | quote = id[0]; |
| 770 | |
| 771 | ++parsed; |
| 772 | ++id; |
| 773 | |
| 774 | if ((ptr = strchr(id, quote)) == NULL) { |
| 775 | return -parsed; |
| 776 | } |
| 777 | ret = ptr-id; |
| 778 | |
| 779 | if (value) { |
| 780 | *value = id; |
| 781 | } |
| 782 | if (val_len) { |
| 783 | *val_len = ret; |
| 784 | } |
| 785 | |
| 786 | parsed += ret+1; |
| 787 | id += ret+1; |
| 788 | } else { |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 789 | return -parsed; |
| 790 | } |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | while (isspace(id[0])) { |
| 794 | ++parsed; |
| 795 | ++id; |
| 796 | } |
| 797 | |
| 798 | if (id[0] != ']') { |
| 799 | return -parsed; |
| 800 | } |
| 801 | |
| 802 | ++parsed; |
| 803 | ++id; |
| 804 | |
| 805 | if ((id[0] == '[') && has_predicate) { |
| 806 | *has_predicate = 1; |
| 807 | } |
| 808 | |
| 809 | return parsed; |
| 810 | } |
| 811 | |
| 812 | /** |
| 813 | * @brief Parse schema-nodeid. |
| 814 | * |
| 815 | * schema-nodeid = absolute-schema-nodeid / |
| 816 | * descendant-schema-nodeid |
| 817 | * absolute-schema-nodeid = 1*("/" node-identifier) |
Michal Vasko | 4893535 | 2016-03-29 11:52:36 +0200 | [diff] [blame] | 818 | * descendant-schema-nodeid = ["." "/"] |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 819 | * node-identifier |
| 820 | * absolute-schema-nodeid |
| 821 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 822 | * @param[in] id Identifier to use. |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 823 | * @param[out] mod_name Points to the module name, NULL if there is not any. |
| 824 | * @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] | 825 | * @param[out] name Points to the node name. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 826 | * @param[out] nam_len Length of the node name. |
| 827 | * @param[out] is_relative Flag to mark whether the nodeid is absolute or descendant. Must be -1 |
| 828 | * on the first call, must not be changed between consecutive calls. |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 829 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. It cannot be |
| 830 | * based on the grammar, in those cases use NULL. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 831 | * |
| 832 | * @return Number of characters successfully parsed, |
| 833 | * positive on success, negative on failure. |
| 834 | */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 835 | int |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 836 | 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] | 837 | int *is_relative, int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 838 | { |
| 839 | int parsed = 0, ret; |
| 840 | |
| 841 | assert(id); |
| 842 | assert(is_relative); |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 843 | if (mod_name) { |
| 844 | *mod_name = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 845 | } |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 846 | if (mod_name_len) { |
| 847 | *mod_name_len = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 848 | } |
| 849 | if (name) { |
| 850 | *name = NULL; |
| 851 | } |
| 852 | if (nam_len) { |
| 853 | *nam_len = 0; |
| 854 | } |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 855 | if (has_predicate) { |
| 856 | *has_predicate = 0; |
| 857 | } |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 858 | |
| 859 | if (id[0] != '/') { |
| 860 | if (*is_relative != -1) { |
| 861 | return -parsed; |
| 862 | } else { |
| 863 | *is_relative = 1; |
| 864 | } |
Michal Vasko | 4893535 | 2016-03-29 11:52:36 +0200 | [diff] [blame] | 865 | if (!strncmp(id, "./", 2)) { |
| 866 | parsed += 2; |
| 867 | id += 2; |
| 868 | } |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 869 | } else { |
| 870 | if (*is_relative == -1) { |
| 871 | *is_relative = 0; |
| 872 | } |
| 873 | ++parsed; |
| 874 | ++id; |
| 875 | } |
| 876 | |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 877 | 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] | 878 | return -parsed+ret; |
| 879 | } |
| 880 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 881 | parsed += ret; |
| 882 | id += ret; |
| 883 | |
| 884 | if ((id[0] == '[') && has_predicate) { |
| 885 | *has_predicate = 1; |
| 886 | } |
| 887 | |
| 888 | return parsed; |
| 889 | } |
| 890 | |
| 891 | /** |
| 892 | * @brief Parse schema predicate (special format internally used). |
| 893 | * |
| 894 | * predicate = "[" *WSP predicate-expr *WSP "]" |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 895 | * predicate-expr = "." / identifier / key-with-value |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 896 | * key-with-value = identifier *WSP "=" *WSP |
| 897 | * ((DQUOTE string DQUOTE) / |
| 898 | * (SQUOTE string SQUOTE)) |
| 899 | * |
| 900 | * @param[in] id Identifier to use. |
| 901 | * @param[out] name Points to the list key name. |
| 902 | * @param[out] nam_len Length of \p name. |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 903 | * @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] | 904 | * @param[out] val_len Length of \p value. |
| 905 | * @param[out] has_predicate Flag to mark whether there is another predicate specified. |
| 906 | */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 907 | int |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 908 | 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] | 909 | int *has_predicate) |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 910 | { |
| 911 | const char *ptr; |
| 912 | int parsed = 0, ret; |
| 913 | char quote; |
| 914 | |
| 915 | assert(id); |
| 916 | if (name) { |
| 917 | *name = NULL; |
| 918 | } |
| 919 | if (nam_len) { |
| 920 | *nam_len = 0; |
| 921 | } |
| 922 | if (value) { |
| 923 | *value = NULL; |
| 924 | } |
| 925 | if (val_len) { |
| 926 | *val_len = 0; |
| 927 | } |
| 928 | if (has_predicate) { |
| 929 | *has_predicate = 0; |
| 930 | } |
| 931 | |
| 932 | if (id[0] != '[') { |
| 933 | return -parsed; |
| 934 | } |
| 935 | |
| 936 | ++parsed; |
| 937 | ++id; |
| 938 | |
| 939 | while (isspace(id[0])) { |
| 940 | ++parsed; |
| 941 | ++id; |
| 942 | } |
| 943 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 944 | /* identifier */ |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 945 | if (id[0] == '.') { |
| 946 | ret = 1; |
| 947 | } else if ((ret = parse_identifier(id)) < 1) { |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 948 | return -parsed + ret; |
| 949 | } |
| 950 | if (name) { |
| 951 | *name = id; |
| 952 | } |
| 953 | if (nam_len) { |
| 954 | *nam_len = ret; |
| 955 | } |
| 956 | |
| 957 | parsed += ret; |
| 958 | id += ret; |
| 959 | |
| 960 | while (isspace(id[0])) { |
| 961 | ++parsed; |
| 962 | ++id; |
| 963 | } |
| 964 | |
| 965 | /* there is value as well */ |
| 966 | if (id[0] == '=') { |
| 967 | ++parsed; |
| 968 | ++id; |
| 969 | |
| 970 | while (isspace(id[0])) { |
| 971 | ++parsed; |
| 972 | ++id; |
| 973 | } |
| 974 | |
| 975 | /* ((DQUOTE string DQUOTE) / (SQUOTE string SQUOTE)) */ |
| 976 | if ((id[0] == '\"') || (id[0] == '\'')) { |
| 977 | quote = id[0]; |
| 978 | |
| 979 | ++parsed; |
| 980 | ++id; |
| 981 | |
| 982 | if ((ptr = strchr(id, quote)) == NULL) { |
| 983 | return -parsed; |
| 984 | } |
| 985 | ret = ptr - id; |
| 986 | |
| 987 | if (value) { |
| 988 | *value = id; |
| 989 | } |
| 990 | if (val_len) { |
| 991 | *val_len = ret; |
| 992 | } |
| 993 | |
| 994 | parsed += ret + 1; |
| 995 | id += ret + 1; |
| 996 | } else { |
| 997 | return -parsed; |
| 998 | } |
| 999 | |
| 1000 | while (isspace(id[0])) { |
| 1001 | ++parsed; |
| 1002 | ++id; |
| 1003 | } |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1004 | } else if (value) { |
| 1005 | /* if value was expected, it's mandatory */ |
| 1006 | return -parsed; |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1007 | } |
| 1008 | |
| 1009 | if (id[0] != ']') { |
| 1010 | return -parsed; |
| 1011 | } |
| 1012 | |
| 1013 | ++parsed; |
| 1014 | ++id; |
| 1015 | |
| 1016 | if ((id[0] == '[') && has_predicate) { |
| 1017 | *has_predicate = 1; |
| 1018 | } |
| 1019 | |
| 1020 | return parsed; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 1021 | } |
| 1022 | |
| 1023 | /** |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1024 | * @brief Resolve (find) a feature definition. Logs directly. |
| 1025 | * |
| 1026 | * @param[in] feat_name Feature name to resolve. |
| 1027 | * @param[in] len Length of \p feat_name. |
| 1028 | * @param[in] node Node with the if-feature expression. |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1029 | * @param[out] feature Pointer to be set to point to the feature definition, if feature not found |
| 1030 | * (return code 1), the pointer is untouched. |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1031 | * |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1032 | * @return 0 on success, 1 on forward reference, -1 on error. |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1033 | */ |
| 1034 | static int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1035 | 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] | 1036 | { |
| 1037 | char *str; |
| 1038 | const char *mod_name, *name; |
| 1039 | int mod_name_len, nam_len, i, j; |
| 1040 | const struct lys_module *module; |
| 1041 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1042 | assert(feature); |
| 1043 | |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1044 | /* check prefix */ |
| 1045 | if ((i = parse_node_identifier(feat_name, &mod_name, &mod_name_len, &name, &nam_len)) < 1) { |
| 1046 | LOGVAL(LYE_INCHAR, LY_VLOG_NONE, NULL, feat_name[-i], &feat_name[-i]); |
| 1047 | return -1; |
| 1048 | } |
| 1049 | |
| 1050 | module = lys_get_import_module(lys_node_module(node), NULL, 0, mod_name, mod_name_len); |
| 1051 | if (!module) { |
| 1052 | /* identity refers unknown data model */ |
| 1053 | LOGVAL(LYE_INMOD_LEN, LY_VLOG_NONE, NULL, mod_name_len, mod_name); |
| 1054 | return -1; |
| 1055 | } |
| 1056 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1057 | if (module != node->module && module == lys_node_module(node)) { |
| 1058 | /* first, try to search directly in submodule where the feature was mentioned */ |
| 1059 | for (j = 0; j < node->module->features_size; j++) { |
| 1060 | if (!strncmp(name, node->module->features[j].name, nam_len) && !node->module->features[j].name[nam_len]) { |
| 1061 | /* check status */ |
| 1062 | 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] | 1063 | node->module->features[j].module, node->module->features[j].name, NULL)) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1064 | return -1; |
| 1065 | } |
| 1066 | *feature = &node->module->features[j]; |
| 1067 | return 0; |
| 1068 | } |
| 1069 | } |
| 1070 | } |
| 1071 | |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1072 | /* search in the identified module ... */ |
| 1073 | for (j = 0; j < module->features_size; j++) { |
Michal Vasko | 3def867 | 2016-07-01 11:43:09 +0200 | [diff] [blame] | 1074 | 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] | 1075 | /* check status */ |
| 1076 | 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] | 1077 | module->features[j].module, module->features[j].name, NULL)) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1078 | return -1; |
| 1079 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1080 | *feature = &module->features[j]; |
| 1081 | return 0; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1082 | } |
| 1083 | } |
| 1084 | /* ... and all its submodules */ |
| 1085 | for (i = 0; i < module->inc_size; i++) { |
| 1086 | if (!module->inc[i].submodule) { |
| 1087 | /* not yet resolved */ |
| 1088 | continue; |
| 1089 | } |
| 1090 | for (j = 0; j < module->inc[i].submodule->features_size; j++) { |
Michal Vasko | 3def867 | 2016-07-01 11:43:09 +0200 | [diff] [blame] | 1091 | if (!strncmp(name, module->inc[i].submodule->features[j].name, nam_len) |
| 1092 | && !module->inc[i].submodule->features[j].name[nam_len]) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1093 | /* check status */ |
| 1094 | if (lyp_check_status(node->flags, lys_node_module(node), node->name, |
| 1095 | module->inc[i].submodule->features[j].flags, |
| 1096 | module->inc[i].submodule->features[j].module, |
Pavol Vican | fdab9f9 | 2016-09-07 15:23:27 +0200 | [diff] [blame] | 1097 | module->inc[i].submodule->features[j].name, NULL)) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1098 | return -1; |
| 1099 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1100 | *feature = &module->inc[i].submodule->features[j]; |
| 1101 | return 0; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1102 | } |
| 1103 | } |
| 1104 | } |
| 1105 | |
| 1106 | /* not found */ |
| 1107 | str = strndup(feat_name, len); |
| 1108 | LOGVAL(LYE_INRESOLV, LY_VLOG_NONE, NULL, "feature", str); |
| 1109 | free(str); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1110 | return 1; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1111 | } |
| 1112 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1113 | /* |
| 1114 | * @return |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1115 | * - 1 if enabled |
| 1116 | * - 0 if disabled |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1117 | * - -1 if not usable by its if-feature expression |
| 1118 | */ |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1119 | static int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1120 | resolve_feature_value(const struct lys_feature *feat) |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1121 | { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1122 | int i; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1123 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1124 | for (i = 0; i < feat->iffeature_size; i++) { |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1125 | if (!resolve_iffeature(&feat->iffeature[i])) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1126 | return -1; |
| 1127 | } |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1128 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1129 | |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1130 | return feat->flags & LYS_FENABLED ? 1 : 0; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1131 | } |
| 1132 | |
| 1133 | static int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1134 | 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] | 1135 | { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1136 | uint8_t op; |
| 1137 | int rc, a, b; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1138 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1139 | op = iff_getop(expr->expr, *index_e); |
| 1140 | (*index_e)++; |
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 | switch (op) { |
| 1143 | case LYS_IFF_F: |
| 1144 | /* resolve feature */ |
| 1145 | return resolve_feature_value(expr->features[(*index_f)++]); |
| 1146 | case LYS_IFF_NOT: |
| 1147 | rc = resolve_iffeature_recursive(expr, index_e, index_f); |
| 1148 | if (rc == -1) { |
| 1149 | /* one of the referenced feature is hidden by its if-feature, |
| 1150 | * so this if-feature expression is always false */ |
| 1151 | return -1; |
| 1152 | } else { |
| 1153 | /* invert result */ |
| 1154 | return rc ? 0 : 1; |
| 1155 | } |
| 1156 | case LYS_IFF_AND: |
| 1157 | case LYS_IFF_OR: |
| 1158 | a = resolve_iffeature_recursive(expr, index_e, index_f); |
| 1159 | b = resolve_iffeature_recursive(expr, index_e, index_f); |
| 1160 | if (a == -1 || b == -1) { |
| 1161 | /* one of the referenced feature is hidden by its if-feature, |
| 1162 | * so this if-feature expression is always false */ |
| 1163 | return -1; |
| 1164 | } else if (op == LYS_IFF_AND) { |
| 1165 | return a && b; |
| 1166 | } else { /* LYS_IFF_OR */ |
| 1167 | return a || b; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1168 | } |
| 1169 | } |
| 1170 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1171 | return -1; |
| 1172 | } |
| 1173 | |
| 1174 | int |
| 1175 | resolve_iffeature(struct lys_iffeature *expr) |
| 1176 | { |
| 1177 | int rc = -1; |
| 1178 | int index_e = 0, index_f = 0; |
| 1179 | |
| 1180 | if (expr->expr) { |
| 1181 | rc = resolve_iffeature_recursive(expr, &index_e, &index_f); |
| 1182 | } |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1183 | return (rc == 1) ? 1 : 0; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1184 | } |
| 1185 | |
| 1186 | struct iff_stack { |
| 1187 | int size; |
| 1188 | int index; /* first empty item */ |
| 1189 | uint8_t *stack; |
| 1190 | }; |
| 1191 | |
| 1192 | static int |
| 1193 | iff_stack_push(struct iff_stack *stack, uint8_t value) |
| 1194 | { |
| 1195 | if (stack->index == stack->size) { |
| 1196 | stack->size += 4; |
| 1197 | stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack); |
| 1198 | if (!stack->stack) { |
| 1199 | LOGMEM; |
| 1200 | stack->size = 0; |
| 1201 | return EXIT_FAILURE; |
| 1202 | } |
| 1203 | } |
| 1204 | |
| 1205 | stack->stack[stack->index++] = value; |
| 1206 | return EXIT_SUCCESS; |
| 1207 | } |
| 1208 | |
| 1209 | static uint8_t |
| 1210 | iff_stack_pop(struct iff_stack *stack) |
| 1211 | { |
| 1212 | stack->index--; |
| 1213 | return stack->stack[stack->index]; |
| 1214 | } |
| 1215 | |
| 1216 | static void |
| 1217 | iff_stack_clean(struct iff_stack *stack) |
| 1218 | { |
| 1219 | stack->size = 0; |
| 1220 | free(stack->stack); |
| 1221 | } |
| 1222 | |
| 1223 | static void |
| 1224 | iff_setop(uint8_t *list, uint8_t op, int pos) |
| 1225 | { |
| 1226 | uint8_t *item; |
| 1227 | uint8_t mask = 3; |
| 1228 | |
| 1229 | assert(pos >= 0); |
| 1230 | assert(op <= 3); /* max 2 bits */ |
| 1231 | |
| 1232 | item = &list[pos / 4]; |
| 1233 | mask = mask << 2 * (pos % 4); |
| 1234 | *item = (*item) & ~mask; |
| 1235 | *item = (*item) | (op << 2 * (pos % 4)); |
| 1236 | } |
| 1237 | |
| 1238 | uint8_t |
| 1239 | iff_getop(uint8_t *list, int pos) |
| 1240 | { |
| 1241 | uint8_t *item; |
| 1242 | uint8_t mask = 3, result; |
| 1243 | |
| 1244 | assert(pos >= 0); |
| 1245 | |
| 1246 | item = &list[pos / 4]; |
| 1247 | result = (*item) & (mask << 2 * (pos % 4)); |
| 1248 | return result >> 2 * (pos % 4); |
| 1249 | } |
| 1250 | |
| 1251 | #define LYS_IFF_LP 0x04 /* ( */ |
| 1252 | #define LYS_IFF_RP 0x08 /* ) */ |
| 1253 | |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1254 | /* internal structure for passing data for UNRES_IFFEAT */ |
| 1255 | struct unres_iffeat_data { |
| 1256 | struct lys_node *node; |
| 1257 | const char *fname; |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 1258 | int infeature; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1259 | }; |
| 1260 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1261 | void |
| 1262 | resolve_iffeature_getsizes(struct lys_iffeature *iffeat, unsigned int *expr_size, unsigned int *feat_size) |
| 1263 | { |
| 1264 | unsigned int e = 0, f = 0, r = 0; |
| 1265 | uint8_t op; |
| 1266 | |
| 1267 | assert(iffeat); |
| 1268 | |
| 1269 | if (!iffeat->expr) { |
| 1270 | goto result; |
| 1271 | } |
| 1272 | |
| 1273 | do { |
| 1274 | op = iff_getop(iffeat->expr, e++); |
| 1275 | switch (op) { |
| 1276 | case LYS_IFF_NOT: |
| 1277 | if (!r) { |
| 1278 | r += 1; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1279 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1280 | break; |
| 1281 | case LYS_IFF_AND: |
| 1282 | case LYS_IFF_OR: |
| 1283 | if (!r) { |
| 1284 | r += 2; |
| 1285 | } else { |
| 1286 | r += 1; |
| 1287 | } |
| 1288 | break; |
| 1289 | case LYS_IFF_F: |
| 1290 | f++; |
| 1291 | if (r) { |
| 1292 | r--; |
| 1293 | } |
| 1294 | break; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1295 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1296 | } while(r); |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1297 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1298 | result: |
| 1299 | if (expr_size) { |
| 1300 | *expr_size = e; |
| 1301 | } |
| 1302 | if (feat_size) { |
| 1303 | *feat_size = f; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1304 | } |
| 1305 | } |
| 1306 | |
| 1307 | int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1308 | resolve_iffeature_compile(struct lys_iffeature *iffeat_expr, const char *value, struct lys_node *node, |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 1309 | int infeature, struct unres_schema *unres) |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1310 | { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1311 | const char *c = value; |
| 1312 | int r, rc = EXIT_FAILURE; |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1313 | int i, j, last_not, checkversion = 0; |
| 1314 | unsigned int f_size = 0, expr_size = 0, f_exp = 1; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1315 | uint8_t op; |
| 1316 | struct iff_stack stack = {0, 0, NULL}; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1317 | struct unres_iffeat_data *iff_data; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1318 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1319 | assert(c); |
| 1320 | |
| 1321 | if (isspace(c[0])) { |
| 1322 | LOGVAL(LYE_INCHAR, LY_VLOG_NONE, NULL, c[0], c); |
| 1323 | return EXIT_FAILURE; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1324 | } |
| 1325 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1326 | /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */ |
| 1327 | for (i = j = last_not = 0; c[i]; i++) { |
| 1328 | if (c[i] == '(') { |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1329 | checkversion = 1; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1330 | j++; |
| 1331 | continue; |
| 1332 | } else if (c[i] == ')') { |
| 1333 | j--; |
| 1334 | continue; |
| 1335 | } else if (isspace(c[i])) { |
| 1336 | continue; |
| 1337 | } |
| 1338 | |
| 1339 | if (!strncmp(&c[i], "not", r = 3) || !strncmp(&c[i], "and", r = 3) || !strncmp(&c[i], "or", r = 2)) { |
| 1340 | if (c[i + r] == '\0') { |
Radek Krejci | a98da3f | 2016-07-27 14:05:22 +0200 | [diff] [blame] | 1341 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1342 | return EXIT_FAILURE; |
| 1343 | } else if (!isspace(c[i + r])) { |
| 1344 | /* feature name starting with the not/and/or */ |
| 1345 | last_not = 0; |
| 1346 | f_size++; |
| 1347 | } else if (c[i] == 'n') { /* not operation */ |
| 1348 | if (last_not) { |
| 1349 | /* double not */ |
| 1350 | expr_size = expr_size - 2; |
| 1351 | last_not = 0; |
| 1352 | } else { |
| 1353 | last_not = 1; |
| 1354 | } |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1355 | } else { /* and, or */ |
| 1356 | f_exp++; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1357 | /* not a not operation */ |
| 1358 | last_not = 0; |
| 1359 | } |
| 1360 | i += r; |
| 1361 | } else { |
| 1362 | f_size++; |
| 1363 | last_not = 0; |
| 1364 | } |
| 1365 | expr_size++; |
| 1366 | |
| 1367 | while (!isspace(c[i])) { |
| 1368 | if (!c[i] || c[i] == ')') { |
| 1369 | i--; |
| 1370 | break; |
| 1371 | } |
| 1372 | i++; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1373 | } |
| 1374 | } |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1375 | if (j || f_exp != f_size) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1376 | /* not matching count of ( and ) */ |
| 1377 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
| 1378 | return EXIT_FAILURE; |
| 1379 | } |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1380 | |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1381 | if (checkversion || expr_size > 1) { |
| 1382 | /* check that we have 1.1 module */ |
| 1383 | if (node->module->version != 2) { |
| 1384 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
| 1385 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "YANG 1.1 if-feature expression found in 1.0 module."); |
| 1386 | return EXIT_FAILURE; |
| 1387 | } |
| 1388 | } |
| 1389 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1390 | /* allocate the memory */ |
| 1391 | 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] | 1392 | iffeat_expr->features = calloc(f_size, sizeof *iffeat_expr->features); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1393 | stack.size = expr_size; |
| 1394 | stack.stack = malloc(expr_size * sizeof *stack.stack); |
| 1395 | if (!stack.stack || !iffeat_expr->expr || !iffeat_expr->features) { |
| 1396 | LOGMEM; |
| 1397 | goto error; |
| 1398 | } |
| 1399 | f_size--; expr_size--; /* used as indexes from now */ |
| 1400 | |
| 1401 | for (i--; i >= 0; i--) { |
| 1402 | if (c[i] == ')') { |
| 1403 | /* push it on stack */ |
| 1404 | iff_stack_push(&stack, LYS_IFF_RP); |
| 1405 | continue; |
| 1406 | } else if (c[i] == '(') { |
| 1407 | /* pop from the stack into result all operators until ) */ |
| 1408 | while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) { |
| 1409 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1410 | } |
| 1411 | continue; |
| 1412 | } else if (isspace(c[i])) { |
| 1413 | continue; |
| 1414 | } |
| 1415 | |
| 1416 | /* end operator or operand -> find beginning and get what is it */ |
| 1417 | j = i + 1; |
| 1418 | while (i >= 0 && !isspace(c[i]) && c[i] != '(') { |
| 1419 | i--; |
| 1420 | } |
| 1421 | i++; /* get back by one step */ |
| 1422 | |
| 1423 | if (!strncmp(&c[i], "not ", 4)) { |
| 1424 | if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) { |
| 1425 | /* double not */ |
| 1426 | iff_stack_pop(&stack); |
| 1427 | } else { |
| 1428 | /* not has the highest priority, so do not pop from the stack |
| 1429 | * as in case of AND and OR */ |
| 1430 | iff_stack_push(&stack, LYS_IFF_NOT); |
| 1431 | } |
| 1432 | } else if (!strncmp(&c[i], "and ", 4)) { |
| 1433 | /* as for OR - pop from the stack all operators with the same or higher |
| 1434 | * priority and store them to the result, then push the AND to the stack */ |
| 1435 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) { |
| 1436 | op = iff_stack_pop(&stack); |
| 1437 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1438 | } |
| 1439 | iff_stack_push(&stack, LYS_IFF_AND); |
| 1440 | } else if (!strncmp(&c[i], "or ", 3)) { |
| 1441 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) { |
| 1442 | op = iff_stack_pop(&stack); |
| 1443 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1444 | } |
| 1445 | iff_stack_push(&stack, LYS_IFF_OR); |
| 1446 | } else { |
| 1447 | /* feature name, length is j - i */ |
| 1448 | |
| 1449 | /* add it to the result */ |
| 1450 | iff_setop(iffeat_expr->expr, LYS_IFF_F, expr_size--); |
| 1451 | |
| 1452 | /* now get the link to the feature definition. Since it can be |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1453 | * forward referenced, we have to keep the feature name in auxiliary |
| 1454 | * structure passed into unres */ |
| 1455 | iff_data = malloc(sizeof *iff_data); |
| 1456 | iff_data->node = node; |
| 1457 | iff_data->fname = lydict_insert(node->module->ctx, &c[i], j - i); |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 1458 | iff_data->infeature = infeature; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1459 | r = unres_schema_add_node(node->module, unres, &iffeat_expr->features[f_size], UNRES_IFFEAT, |
| 1460 | (struct lys_node *)iff_data); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1461 | f_size--; |
| 1462 | |
| 1463 | if (r == -1) { |
Pavol Vican | 4d08451 | 2016-09-29 16:38:12 +0200 | [diff] [blame] | 1464 | free(iff_data); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1465 | goto error; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1466 | } |
| 1467 | } |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1468 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1469 | while (stack.index) { |
| 1470 | op = iff_stack_pop(&stack); |
| 1471 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1472 | } |
| 1473 | |
| 1474 | if (++expr_size || ++f_size) { |
| 1475 | /* not all expected operators and operands found */ |
| 1476 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
| 1477 | rc = EXIT_FAILURE; |
| 1478 | } else { |
| 1479 | rc = EXIT_SUCCESS; |
| 1480 | } |
| 1481 | |
| 1482 | error: |
| 1483 | /* cleanup */ |
| 1484 | iff_stack_clean(&stack); |
| 1485 | |
| 1486 | return rc; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1487 | } |
| 1488 | |
| 1489 | /** |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1490 | * @brief Resolve (find) a data node based on a schema-nodeid. |
| 1491 | * |
| 1492 | * Used for resolving unique statements - so id is expected to be relative and local (without reference to a different |
| 1493 | * module). |
| 1494 | * |
| 1495 | */ |
| 1496 | struct lyd_node * |
| 1497 | resolve_data_descendant_schema_nodeid(const char *nodeid, struct lyd_node *start) |
| 1498 | { |
| 1499 | char *str, *token, *p; |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1500 | struct lyd_node *result = NULL, *iter; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1501 | const struct lys_node *schema = NULL; |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1502 | int shorthand = 0; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1503 | |
| 1504 | assert(nodeid && start); |
| 1505 | |
| 1506 | if (nodeid[0] == '/') { |
| 1507 | return NULL; |
| 1508 | } |
| 1509 | |
| 1510 | str = p = strdup(nodeid); |
| 1511 | if (!str) { |
| 1512 | LOGMEM; |
| 1513 | return NULL; |
| 1514 | } |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1515 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1516 | while (p) { |
| 1517 | token = p; |
| 1518 | p = strchr(p, '/'); |
| 1519 | if (p) { |
| 1520 | *p = '\0'; |
| 1521 | p++; |
| 1522 | } |
| 1523 | |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1524 | if (p) { |
| 1525 | /* inner node */ |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1526 | if (resolve_descendant_schema_nodeid(token, schema ? schema->child : start->schema, |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 1527 | LYS_CONTAINER | LYS_CHOICE | LYS_CASE | LYS_LEAF, 0, 0, &schema) |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1528 | || !schema) { |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1529 | result = NULL; |
| 1530 | break; |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1531 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1532 | |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1533 | if (schema->nodetype & (LYS_CHOICE | LYS_CASE)) { |
| 1534 | continue; |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 1535 | } else if (lys_parent(schema)->nodetype == LYS_CHOICE) { |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1536 | /* shorthand case */ |
| 1537 | if (!shorthand) { |
| 1538 | shorthand = 1; |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 1539 | schema = lys_parent(schema); |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1540 | continue; |
| 1541 | } else { |
| 1542 | shorthand = 0; |
| 1543 | if (schema->nodetype == LYS_LEAF) { |
| 1544 | /* should not be here, since we have leaf, which is not a shorthand nor final node */ |
| 1545 | result = NULL; |
| 1546 | break; |
| 1547 | } |
| 1548 | } |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1549 | } |
| 1550 | } else { |
| 1551 | /* final node */ |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 1552 | if (resolve_descendant_schema_nodeid(token, schema ? schema->child : start->schema, LYS_LEAF, |
| 1553 | shorthand ? 0 : 1, 0, &schema) |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1554 | || !schema) { |
| 1555 | result = NULL; |
| 1556 | break; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1557 | } |
| 1558 | } |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1559 | LY_TREE_FOR(result ? result->child : start, iter) { |
| 1560 | if (iter->schema == schema) { |
| 1561 | /* move in data tree according to returned schema */ |
| 1562 | result = iter; |
| 1563 | break; |
| 1564 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1565 | } |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1566 | if (!iter) { |
| 1567 | /* instance not found */ |
| 1568 | result = NULL; |
| 1569 | break; |
| 1570 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1571 | } |
| 1572 | free(str); |
| 1573 | |
| 1574 | return result; |
| 1575 | } |
| 1576 | |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1577 | /* |
| 1578 | * 0 - ok (done) |
| 1579 | * 1 - continue |
| 1580 | * 2 - break |
| 1581 | * -1 - error |
| 1582 | */ |
| 1583 | static int |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1584 | 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] | 1585 | 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] | 1586 | int implemented_mod, const struct lys_node **start) |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1587 | { |
| 1588 | const struct lys_module *prefix_mod; |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1589 | int sh = 0; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1590 | |
| 1591 | /* module check */ |
| 1592 | 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] | 1593 | if (implemented_mod) { |
| 1594 | prefix_mod = lys_get_implemented_module(prefix_mod); |
| 1595 | } |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1596 | if (!prefix_mod) { |
| 1597 | return -1; |
| 1598 | } |
| 1599 | if (prefix_mod != lys_node_module(sibling)) { |
| 1600 | return 1; |
| 1601 | } |
| 1602 | |
| 1603 | /* check for shorthand cases - then 'start' does not change */ |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 1604 | 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] | 1605 | if (*shorthand != -1) { |
| 1606 | *shorthand = *shorthand ? 0 : 1; |
| 1607 | } |
| 1608 | sh = 1; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1609 | } |
| 1610 | |
| 1611 | /* the result node? */ |
| 1612 | if (!id[0]) { |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1613 | if (*shorthand == 1) { |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1614 | return 1; |
| 1615 | } |
| 1616 | return 0; |
| 1617 | } |
| 1618 | |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1619 | if (!sh) { |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1620 | /* move down the tree, if possible */ |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 1621 | if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1622 | return -1; |
| 1623 | } |
| 1624 | *start = sibling->child; |
| 1625 | } |
| 1626 | |
| 1627 | return 2; |
| 1628 | } |
| 1629 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1630 | /* start - relative, module - absolute, -1 error, EXIT_SUCCESS ok (but ret can still be NULL), >0 unexpected char on ret - 1 */ |
| 1631 | int |
| 1632 | resolve_augment_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_module *module, |
| 1633 | const struct lys_node **ret) |
| 1634 | { |
| 1635 | const char *name, *mod_name, *id; |
| 1636 | const struct lys_node *sibling; |
| 1637 | int r, nam_len, mod_name_len, is_relative = -1; |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1638 | int8_t shorthand = 0; |
Radek Krejci | 0fa54e9 | 2016-09-14 14:01:05 +0200 | [diff] [blame] | 1639 | int implemented_search = 0; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1640 | /* 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] | 1641 | const struct lys_module *start_mod; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1642 | |
| 1643 | assert(nodeid && (start || module) && !(start && module) && ret); |
| 1644 | |
| 1645 | id = nodeid; |
| 1646 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1647 | 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] | 1648 | return ((id - nodeid) - r) + 1; |
| 1649 | } |
| 1650 | id += r; |
| 1651 | |
| 1652 | if ((is_relative && !start) || (!is_relative && !module)) { |
| 1653 | return -1; |
| 1654 | } |
| 1655 | |
| 1656 | /* descendant-schema-nodeid */ |
| 1657 | if (is_relative) { |
Michal Vasko | 4c06fd8 | 2016-02-17 13:43:38 +0100 | [diff] [blame] | 1658 | module = start_mod = start->module; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1659 | |
| 1660 | /* absolute-schema-nodeid */ |
| 1661 | } else { |
| 1662 | 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] | 1663 | if (start_mod != lys_main_module(module)) { |
| 1664 | /* if the submodule augments the mainmodule (or in general a module augments |
| 1665 | * itself, we don't want to search for the implemented module but augments |
| 1666 | * the module anyway. But when augmenting another module, we need the implemented |
| 1667 | * revision of the module if any */ |
| 1668 | start_mod = lys_get_implemented_module(start_mod); |
| 1669 | implemented_search = 1; |
| 1670 | } |
Michal Vasko | e290563 | 2016-02-11 15:42:24 +0100 | [diff] [blame] | 1671 | if (!start_mod) { |
| 1672 | return -1; |
| 1673 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1674 | start = start_mod->data; |
| 1675 | } |
| 1676 | |
| 1677 | while (1) { |
| 1678 | sibling = NULL; |
| 1679 | while ((sibling = lys_getnext(sibling, lys_parent(start), start_mod, |
| 1680 | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT))) { |
| 1681 | /* name match */ |
Michal Vasko | 0a1aaa4 | 2016-04-19 09:48:25 +0200 | [diff] [blame] | 1682 | 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] | 1683 | r = schema_nodeid_siblingcheck(sibling, &shorthand, id, module, mod_name, mod_name_len, |
| 1684 | implemented_search, &start); |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1685 | if (r == 0) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1686 | *ret = sibling; |
| 1687 | return EXIT_SUCCESS; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1688 | } else if (r == 1) { |
| 1689 | continue; |
| 1690 | } else if (r == 2) { |
| 1691 | break; |
| 1692 | } else { |
| 1693 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1694 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1695 | } |
| 1696 | } |
| 1697 | |
| 1698 | /* no match */ |
| 1699 | if (!sibling) { |
Michal Vasko | a426fef | 2016-03-07 10:47:31 +0100 | [diff] [blame] | 1700 | *ret = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1701 | return EXIT_SUCCESS; |
| 1702 | } |
| 1703 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1704 | 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] | 1705 | return ((id - nodeid) - r) + 1; |
| 1706 | } |
| 1707 | id += r; |
| 1708 | } |
| 1709 | |
| 1710 | /* cannot get here */ |
| 1711 | LOGINT; |
| 1712 | return -1; |
| 1713 | } |
| 1714 | |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 1715 | /* unique, refine, |
| 1716 | * >0 - unexpected char on position (ret - 1), |
| 1717 | * 0 - ok (but ret can still be NULL), |
| 1718 | * -1 - error, |
| 1719 | * -2 - violated no_innerlist */ |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1720 | int |
| 1721 | 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] | 1722 | int check_shorthand, int no_innerlist, const struct lys_node **ret) |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1723 | { |
| 1724 | const char *name, *mod_name, *id; |
| 1725 | const struct lys_node *sibling; |
| 1726 | int r, nam_len, mod_name_len, is_relative = -1; |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1727 | int8_t shorthand = check_shorthand ? 0 : -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1728 | /* 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] | 1729 | const struct lys_module *module; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1730 | |
| 1731 | assert(nodeid && start && ret); |
| 1732 | assert(!(ret_nodetype & (LYS_USES | LYS_AUGMENT)) && ((ret_nodetype == LYS_GROUPING) || !(ret_nodetype & LYS_GROUPING))); |
| 1733 | |
| 1734 | id = nodeid; |
| 1735 | module = start->module; |
| 1736 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1737 | 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] | 1738 | return ((id - nodeid) - r) + 1; |
| 1739 | } |
| 1740 | id += r; |
| 1741 | |
| 1742 | if (!is_relative) { |
| 1743 | return -1; |
| 1744 | } |
| 1745 | |
| 1746 | while (1) { |
| 1747 | sibling = NULL; |
| 1748 | while ((sibling = lys_getnext(sibling, lys_parent(start), module, |
| 1749 | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE))) { |
| 1750 | /* name match */ |
| 1751 | 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] | 1752 | 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] | 1753 | if (r == 0) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1754 | if (!(sibling->nodetype & ret_nodetype)) { |
| 1755 | /* wrong node type, too bad */ |
| 1756 | continue; |
| 1757 | } |
| 1758 | *ret = sibling; |
| 1759 | return EXIT_SUCCESS; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1760 | } else if (r == 1) { |
| 1761 | continue; |
| 1762 | } else if (r == 2) { |
| 1763 | break; |
| 1764 | } else { |
| 1765 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1766 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1767 | } |
| 1768 | } |
| 1769 | |
| 1770 | /* no match */ |
| 1771 | if (!sibling) { |
Michal Vasko | a426fef | 2016-03-07 10:47:31 +0100 | [diff] [blame] | 1772 | *ret = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1773 | return EXIT_SUCCESS; |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 1774 | } else if (no_innerlist && sibling->nodetype == LYS_LIST) { |
| 1775 | *ret = NULL; |
| 1776 | return -2; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1777 | } |
| 1778 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1779 | 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] | 1780 | return ((id - nodeid) - r) + 1; |
| 1781 | } |
| 1782 | id += r; |
| 1783 | } |
| 1784 | |
| 1785 | /* cannot get here */ |
| 1786 | LOGINT; |
| 1787 | return -1; |
| 1788 | } |
| 1789 | |
| 1790 | /* choice default */ |
| 1791 | int |
| 1792 | resolve_choice_default_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_node **ret) |
| 1793 | { |
| 1794 | /* cannot actually be a path */ |
| 1795 | if (strchr(nodeid, '/')) { |
| 1796 | return -1; |
| 1797 | } |
| 1798 | |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 1799 | 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] | 1800 | } |
| 1801 | |
| 1802 | /* uses, -1 error, EXIT_SUCCESS ok (but ret can still be NULL), >0 unexpected char on ret - 1 */ |
| 1803 | static int |
| 1804 | resolve_uses_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_node_grp **ret) |
| 1805 | { |
| 1806 | const struct lys_module *module; |
| 1807 | const char *mod_prefix, *name; |
| 1808 | int i, mod_prefix_len, nam_len; |
| 1809 | |
| 1810 | /* parse the identifier, it must be parsed on one call */ |
| 1811 | if (((i = parse_node_identifier(nodeid, &mod_prefix, &mod_prefix_len, &name, &nam_len)) < 1) || nodeid[i]) { |
| 1812 | return -i + 1; |
| 1813 | } |
| 1814 | |
| 1815 | module = lys_get_import_module(start->module, mod_prefix, mod_prefix_len, NULL, 0); |
| 1816 | if (!module) { |
| 1817 | return -1; |
| 1818 | } |
| 1819 | if (module != start->module) { |
| 1820 | start = module->data; |
| 1821 | } |
| 1822 | |
| 1823 | *ret = lys_find_grouping_up(name, (struct lys_node *)start); |
| 1824 | |
| 1825 | return EXIT_SUCCESS; |
| 1826 | } |
| 1827 | |
| 1828 | int |
| 1829 | resolve_absolute_schema_nodeid(const char *nodeid, const struct lys_module *module, int ret_nodetype, |
| 1830 | const struct lys_node **ret) |
| 1831 | { |
| 1832 | const char *name, *mod_name, *id; |
| 1833 | const struct lys_node *sibling, *start; |
| 1834 | int r, nam_len, mod_name_len, is_relative = -1; |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1835 | int8_t shorthand = 0; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1836 | const struct lys_module *abs_start_mod; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1837 | |
| 1838 | assert(nodeid && module && ret); |
| 1839 | assert(!(ret_nodetype & (LYS_USES | LYS_AUGMENT)) && ((ret_nodetype == LYS_GROUPING) || !(ret_nodetype & LYS_GROUPING))); |
| 1840 | |
| 1841 | id = nodeid; |
| 1842 | start = module->data; |
| 1843 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1844 | 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] | 1845 | return ((id - nodeid) - r) + 1; |
| 1846 | } |
| 1847 | id += r; |
| 1848 | |
| 1849 | if (is_relative) { |
| 1850 | return -1; |
| 1851 | } |
| 1852 | |
| 1853 | 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] | 1854 | if (!abs_start_mod) { |
| 1855 | return -1; |
| 1856 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1857 | |
| 1858 | while (1) { |
| 1859 | sibling = NULL; |
| 1860 | while ((sibling = lys_getnext(sibling, lys_parent(start), abs_start_mod, LYS_GETNEXT_WITHCHOICE |
| 1861 | | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT | LYS_GETNEXT_WITHGROUPING))) { |
| 1862 | /* name match */ |
Michal Vasko | 0a1aaa4 | 2016-04-19 09:48:25 +0200 | [diff] [blame] | 1863 | 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] | 1864 | 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] | 1865 | if (r == 0) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1866 | if (!(sibling->nodetype & ret_nodetype)) { |
| 1867 | /* wrong node type, too bad */ |
| 1868 | continue; |
| 1869 | } |
| 1870 | *ret = sibling; |
| 1871 | return EXIT_SUCCESS; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1872 | } else if (r == 1) { |
| 1873 | continue; |
| 1874 | } else if (r == 2) { |
| 1875 | break; |
| 1876 | } else { |
| 1877 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1878 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1879 | } |
| 1880 | } |
| 1881 | |
| 1882 | /* no match */ |
| 1883 | if (!sibling) { |
Michal Vasko | a426fef | 2016-03-07 10:47:31 +0100 | [diff] [blame] | 1884 | *ret = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1885 | return EXIT_SUCCESS; |
| 1886 | } |
| 1887 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1888 | 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] | 1889 | return ((id - nodeid) - r) + 1; |
| 1890 | } |
| 1891 | id += r; |
| 1892 | } |
| 1893 | |
| 1894 | /* cannot get here */ |
| 1895 | LOGINT; |
| 1896 | return -1; |
| 1897 | } |
| 1898 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1899 | static int |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1900 | 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] | 1901 | { |
| 1902 | const char *name; |
| 1903 | int nam_len, has_predicate, i; |
| 1904 | |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 1905 | if (((i = parse_schema_json_predicate(predicate, &name, &nam_len, NULL, NULL, &has_predicate)) < 1) |
| 1906 | || !strncmp(name, ".", nam_len)) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 1907 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-i], &predicate[-i]); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1908 | return -1; |
| 1909 | } |
| 1910 | |
| 1911 | predicate += i; |
| 1912 | *parsed += i; |
| 1913 | |
| 1914 | for (i = 0; i < list->keys_size; ++i) { |
| 1915 | if (!strncmp(list->keys[i]->name, name, nam_len) && !list->keys[i]->name[nam_len]) { |
| 1916 | break; |
| 1917 | } |
| 1918 | } |
| 1919 | |
| 1920 | if (i == list->keys_size) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 1921 | LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1922 | return -1; |
| 1923 | } |
| 1924 | |
| 1925 | /* more predicates? */ |
| 1926 | if (has_predicate) { |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1927 | return resolve_json_schema_list_predicate(predicate, list, parsed); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1928 | } |
| 1929 | |
| 1930 | return 0; |
| 1931 | } |
| 1932 | |
Michal Vasko | 8d26e5c | 2016-09-08 10:03:49 +0200 | [diff] [blame] | 1933 | /* cannot return LYS_GROUPING, LYS_AUGMENT, LYS_USES, logs directly */ |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1934 | const struct lys_node * |
Michal Vasko | 8d26e5c | 2016-09-08 10:03:49 +0200 | [diff] [blame] | 1935 | 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] | 1936 | { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 1937 | char *module_name = ly_buf(), *buf_backup = NULL, *str; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1938 | const char *name, *mod_name, *id; |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1939 | const struct lys_node *sibling; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1940 | 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] | 1941 | /* 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] | 1942 | const struct lys_module *prefix_mod, *module, *prev_mod; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1943 | |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1944 | assert(nodeid && (ctx || start)); |
| 1945 | if (!ctx) { |
| 1946 | ctx = start->module->ctx; |
| 1947 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1948 | |
| 1949 | id = nodeid; |
| 1950 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1951 | 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] | 1952 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1953 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1954 | } |
| 1955 | id += r; |
| 1956 | |
| 1957 | if (is_relative) { |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1958 | assert(start); |
| 1959 | start = start->child; |
| 1960 | if (!start) { |
| 1961 | /* no descendants, fail for sure */ |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 1962 | str = strndup(nodeid, (name + nam_len) - nodeid); |
| 1963 | LOGVAL(LYE_PATH_INNODE, LY_VLOG_STR, str); |
| 1964 | free(str); |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1965 | return NULL; |
| 1966 | } |
| 1967 | module = start->module; |
| 1968 | } else { |
| 1969 | if (!mod_name) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 1970 | str = strndup(nodeid, (name + nam_len) - nodeid); |
| 1971 | LOGVAL(LYE_PATH_MISSMOD, LY_VLOG_STR, nodeid); |
| 1972 | free(str); |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1973 | return NULL; |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 1974 | } else if (mod_name_len > LY_BUF_SIZE - 1) { |
| 1975 | LOGINT; |
| 1976 | return NULL; |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1977 | } |
| 1978 | |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 1979 | if (ly_buf_used && module_name[0]) { |
| 1980 | buf_backup = strndup(module_name, LY_BUF_SIZE - 1); |
| 1981 | } |
| 1982 | ly_buf_used++; |
| 1983 | |
Michal Vasko | df3f1ab | 2016-04-01 15:07:22 +0200 | [diff] [blame] | 1984 | memmove(module_name, mod_name, mod_name_len); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 1985 | module_name[mod_name_len] = '\0'; |
| 1986 | module = ly_ctx_get_module(ctx, module_name, NULL); |
| 1987 | |
| 1988 | if (buf_backup) { |
| 1989 | /* return previous internal buffer content */ |
| 1990 | strcpy(module_name, buf_backup); |
| 1991 | free(buf_backup); |
| 1992 | buf_backup = NULL; |
| 1993 | } |
| 1994 | ly_buf_used--; |
| 1995 | |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1996 | if (!module) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 1997 | str = strndup(nodeid, (mod_name + mod_name_len) - nodeid); |
| 1998 | LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str); |
| 1999 | free(str); |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2000 | return NULL; |
| 2001 | } |
| 2002 | start = module->data; |
| 2003 | |
| 2004 | /* now it's as if there was no module name */ |
| 2005 | mod_name = NULL; |
| 2006 | mod_name_len = 0; |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2007 | } |
| 2008 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2009 | prev_mod = module; |
| 2010 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2011 | while (1) { |
| 2012 | sibling = NULL; |
Michal Vasko | 8d26e5c | 2016-09-08 10:03:49 +0200 | [diff] [blame] | 2013 | while ((sibling = lys_getnext(sibling, lys_parent(start), module, |
| 2014 | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT))) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2015 | /* name match */ |
Michal Vasko | 0a1aaa4 | 2016-04-19 09:48:25 +0200 | [diff] [blame] | 2016 | 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] | 2017 | /* module check */ |
| 2018 | if (mod_name) { |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2019 | if (mod_name_len > LY_BUF_SIZE - 1) { |
Michal Vasko | 8757e7c | 2016-03-15 10:41:30 +0100 | [diff] [blame] | 2020 | LOGINT; |
| 2021 | return NULL; |
| 2022 | } |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2023 | |
| 2024 | if (ly_buf_used && module_name[0]) { |
| 2025 | buf_backup = strndup(module_name, LY_BUF_SIZE - 1); |
| 2026 | } |
| 2027 | ly_buf_used++; |
| 2028 | |
Michal Vasko | df3f1ab | 2016-04-01 15:07:22 +0200 | [diff] [blame] | 2029 | memmove(module_name, mod_name, mod_name_len); |
Michal Vasko | 8757e7c | 2016-03-15 10:41:30 +0100 | [diff] [blame] | 2030 | module_name[mod_name_len] = '\0'; |
| 2031 | /* will also find an augment module */ |
| 2032 | prefix_mod = ly_ctx_get_module(ctx, module_name, NULL); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2033 | |
| 2034 | if (buf_backup) { |
| 2035 | /* return previous internal buffer content */ |
Michal Vasko | 888a129 | 2016-04-05 12:08:54 +0200 | [diff] [blame] | 2036 | strncpy(module_name, buf_backup, LY_BUF_SIZE - 1); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2037 | free(buf_backup); |
| 2038 | buf_backup = NULL; |
| 2039 | } |
| 2040 | ly_buf_used--; |
| 2041 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2042 | if (!prefix_mod) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2043 | str = strndup(nodeid, (mod_name + mod_name_len) - nodeid); |
| 2044 | LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str); |
| 2045 | free(str); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2046 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2047 | } |
| 2048 | } else { |
| 2049 | prefix_mod = prev_mod; |
| 2050 | } |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 2051 | if (prefix_mod != lys_node_module(sibling)) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2052 | continue; |
| 2053 | } |
| 2054 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2055 | /* do we have some predicates on it? */ |
| 2056 | if (has_predicate) { |
| 2057 | r = 0; |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 2058 | if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 2059 | if ((r = parse_schema_json_predicate(id, NULL, NULL, NULL, NULL, &has_predicate)) < 1) { |
| 2060 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
| 2061 | return NULL; |
| 2062 | } |
| 2063 | } else if (sibling->nodetype == LYS_LIST) { |
| 2064 | if (resolve_json_schema_list_predicate(id, (const struct lys_node_list *)sibling, &r)) { |
| 2065 | return NULL; |
| 2066 | } |
| 2067 | } else { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2068 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2069 | return NULL; |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2070 | } |
| 2071 | id += r; |
| 2072 | } |
| 2073 | |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 2074 | /* check for shorthand cases - then 'start' does not change */ |
Michal Vasko | 8d26e5c | 2016-09-08 10:03:49 +0200 | [diff] [blame] | 2075 | 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] | 2076 | shorthand = ~shorthand; |
| 2077 | } |
| 2078 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2079 | /* the result node? */ |
| 2080 | if (!id[0]) { |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 2081 | if (shorthand) { |
| 2082 | /* wrong path for shorthand */ |
Michal Vasko | 025e045 | 2016-05-17 16:14:20 +0200 | [diff] [blame] | 2083 | str = strndup(nodeid, (name + nam_len) - nodeid); |
| 2084 | LOGVAL(LYE_PATH_INNODE, LY_VLOG_STR, str); |
Michal Vasko | 3c0f9f5 | 2016-05-17 16:38:10 +0200 | [diff] [blame] | 2085 | 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] | 2086 | free(str); |
Michal Vasko | 025e045 | 2016-05-17 16:14:20 +0200 | [diff] [blame] | 2087 | return NULL; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 2088 | } |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2089 | return sibling; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2090 | } |
| 2091 | |
Michal Vasko | 8d26e5c | 2016-09-08 10:03:49 +0200 | [diff] [blame] | 2092 | if (!shorthand) { |
Michal Vasko | 7dc71d0 | 2016-03-15 10:42:28 +0100 | [diff] [blame] | 2093 | /* move down the tree, if possible */ |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 2094 | if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2095 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
Michal Vasko | 7dc71d0 | 2016-03-15 10:42:28 +0100 | [diff] [blame] | 2096 | return NULL; |
| 2097 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2098 | start = sibling->child; |
| 2099 | } |
| 2100 | |
| 2101 | /* update prev mod */ |
| 2102 | prev_mod = start->module; |
| 2103 | break; |
| 2104 | } |
| 2105 | } |
| 2106 | |
| 2107 | /* no match */ |
| 2108 | if (!sibling) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2109 | str = strndup(nodeid, (name + nam_len) - nodeid); |
| 2110 | LOGVAL(LYE_PATH_INNODE, LY_VLOG_STR, str); |
| 2111 | free(str); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2112 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2113 | } |
| 2114 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2115 | if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate)) < 1) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2116 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2117 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2118 | } |
| 2119 | id += r; |
| 2120 | } |
| 2121 | |
| 2122 | /* cannot get here */ |
| 2123 | LOGINT; |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2124 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2125 | } |
| 2126 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2127 | static int |
| 2128 | resolve_partial_json_data_list_predicate(const char *predicate, const char *node_name, struct lyd_node *node, int *parsed) |
| 2129 | { |
| 2130 | const char *name, *value; |
| 2131 | int nam_len, val_len, has_predicate = 1, r; |
| 2132 | uint16_t i; |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2133 | struct lyd_node_leaf_list *key; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2134 | |
Radek Krejci | 61a86c6 | 2016-03-24 11:06:44 +0100 | [diff] [blame] | 2135 | assert(node); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2136 | assert(node->schema->nodetype == LYS_LIST); |
| 2137 | |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2138 | key = (struct lyd_node_leaf_list *)node->child; |
| 2139 | for (i = 0; i < ((struct lys_node_list *)node->schema)->keys_size; ++i) { |
| 2140 | if (!key) { |
| 2141 | /* invalid data */ |
| 2142 | LOGINT; |
| 2143 | return -1; |
| 2144 | } |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2145 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2146 | if (!has_predicate) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2147 | LOGVAL(LYE_PATH_MISSKEY, LY_VLOG_NONE, NULL, node_name); |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2148 | return -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2149 | } |
| 2150 | |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 2151 | if (((r = parse_schema_json_predicate(predicate, &name, &nam_len, &value, &val_len, &has_predicate)) < 1) |
| 2152 | || !strncmp(name, ".", nam_len)) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2153 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-r], &predicate[-r]); |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2154 | return -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2155 | } |
| 2156 | |
| 2157 | predicate += r; |
| 2158 | *parsed += r; |
| 2159 | |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2160 | 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] | 2161 | LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name); |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2162 | return -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2163 | } |
| 2164 | |
| 2165 | /* value does not match */ |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2166 | 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] | 2167 | return 1; |
| 2168 | } |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2169 | |
| 2170 | key = (struct lyd_node_leaf_list *)key->next; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2171 | } |
| 2172 | |
| 2173 | if (has_predicate) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2174 | LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2175 | return -1; |
| 2176 | } |
| 2177 | |
| 2178 | return 0; |
| 2179 | } |
| 2180 | |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 2181 | /** |
| 2182 | * @brief get the closest parent of the node (or the node itself) identified by the nodeid (path) |
| 2183 | * |
| 2184 | * @param[in] nodeid Node data path to find |
| 2185 | * @param[in] llist_value If the \p nodeid identifies leaf-list, this is expected value of the leaf-list instance. |
| 2186 | * @param[in] options Bitmask of options flags, see @ref pathoptions. |
| 2187 | * @param[out] parsed Number of characters processed in \p id |
| 2188 | * @return The closes parent (or the node itself) from the path |
| 2189 | */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2190 | struct lyd_node * |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2191 | resolve_partial_json_data_nodeid(const char *nodeid, const char *llist_value, struct lyd_node *start, int options, |
| 2192 | int *parsed) |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2193 | { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2194 | char *module_name = ly_buf(), *buf_backup = NULL, *str; |
Michal Vasko | ee385fa | 2016-10-19 15:51:36 +0200 | [diff] [blame] | 2195 | const char *id, *mod_name, *name, *pred_name; |
| 2196 | int r, ret, mod_name_len, nam_len, is_relative = -1; |
| 2197 | 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] | 2198 | struct lyd_node *sibling, *last_match = NULL; |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2199 | struct lyd_node_leaf_list *llist; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2200 | const struct lys_module *prefix_mod, *prev_mod; |
| 2201 | struct ly_ctx *ctx; |
| 2202 | |
| 2203 | assert(nodeid && start && parsed); |
| 2204 | |
| 2205 | ctx = start->schema->module->ctx; |
| 2206 | id = nodeid; |
| 2207 | |
| 2208 | 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] | 2209 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2210 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2211 | return NULL; |
| 2212 | } |
| 2213 | id += r; |
| 2214 | /* add it to parsed only after the data node was actually found */ |
| 2215 | last_parsed = r; |
| 2216 | |
| 2217 | if (is_relative) { |
| 2218 | prev_mod = start->schema->module; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2219 | start = start->child; |
| 2220 | } else { |
| 2221 | for (; start->parent; start = start->parent); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2222 | prev_mod = start->schema->module; |
| 2223 | } |
| 2224 | |
| 2225 | while (1) { |
| 2226 | LY_TREE_FOR(start, sibling) { |
Michal Vasko | 945b96b | 2016-10-18 11:49:12 +0200 | [diff] [blame] | 2227 | /* RPC/action data check, return simply invalid argument, because the data tree is invalid */ |
Michal Vasko | 2411b94 | 2016-03-23 13:50:03 +0100 | [diff] [blame] | 2228 | if (lys_parent(sibling->schema)) { |
| 2229 | if (options & LYD_PATH_OPT_OUTPUT) { |
| 2230 | if (lys_parent(sibling->schema)->nodetype == LYS_INPUT) { |
Michal Vasko | b724689 | 2016-04-19 10:37:35 +0200 | [diff] [blame] | 2231 | 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] | 2232 | *parsed = -1; |
| 2233 | return NULL; |
| 2234 | } |
| 2235 | } else { |
| 2236 | if (lys_parent(sibling->schema)->nodetype == LYS_OUTPUT) { |
Michal Vasko | b724689 | 2016-04-19 10:37:35 +0200 | [diff] [blame] | 2237 | 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] | 2238 | *parsed = -1; |
| 2239 | return NULL; |
| 2240 | } |
| 2241 | } |
| 2242 | } |
| 2243 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2244 | /* name match */ |
| 2245 | if (!strncmp(name, sibling->schema->name, nam_len) && !sibling->schema->name[nam_len]) { |
| 2246 | |
| 2247 | /* module check */ |
| 2248 | if (mod_name) { |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2249 | if (mod_name_len > LY_BUF_SIZE - 1) { |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2250 | LOGINT; |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2251 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2252 | return NULL; |
| 2253 | } |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2254 | |
| 2255 | if (ly_buf_used && module_name[0]) { |
| 2256 | buf_backup = strndup(module_name, LY_BUF_SIZE - 1); |
| 2257 | } |
| 2258 | ly_buf_used++; |
| 2259 | |
Michal Vasko | df3f1ab | 2016-04-01 15:07:22 +0200 | [diff] [blame] | 2260 | memmove(module_name, mod_name, mod_name_len); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2261 | module_name[mod_name_len] = '\0'; |
| 2262 | /* will also find an augment module */ |
| 2263 | prefix_mod = ly_ctx_get_module(ctx, module_name, NULL); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2264 | |
| 2265 | if (buf_backup) { |
| 2266 | /* return previous internal buffer content */ |
Michal Vasko | 888a129 | 2016-04-05 12:08:54 +0200 | [diff] [blame] | 2267 | strncpy(module_name, buf_backup, LY_BUF_SIZE - 1); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2268 | free(buf_backup); |
| 2269 | buf_backup = NULL; |
| 2270 | } |
| 2271 | ly_buf_used--; |
| 2272 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2273 | if (!prefix_mod) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2274 | str = strndup(nodeid, (mod_name + mod_name_len) - nodeid); |
| 2275 | LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str); |
| 2276 | free(str); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2277 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2278 | return NULL; |
| 2279 | } |
| 2280 | } else { |
| 2281 | prefix_mod = prev_mod; |
| 2282 | } |
| 2283 | if (prefix_mod != lys_node_module(sibling->schema)) { |
| 2284 | continue; |
| 2285 | } |
| 2286 | |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2287 | /* leaf-list, did we find it with the correct value or not? */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2288 | if (sibling->schema->nodetype == LYS_LEAFLIST) { |
Michal Vasko | ee385fa | 2016-10-19 15:51:36 +0200 | [diff] [blame] | 2289 | last_has_pred = 0; |
Michal Vasko | acb87b6 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2290 | if (has_predicate) { |
Michal Vasko | ee385fa | 2016-10-19 15:51:36 +0200 | [diff] [blame] | 2291 | if ((r = parse_schema_json_predicate(id, &pred_name, &pred_name_len, &llist_value, &val_len, &last_has_pred)) < 1) { |
Michal Vasko | acb87b6 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2292 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
| 2293 | *parsed = -1; |
| 2294 | return NULL; |
| 2295 | } |
Michal Vasko | ee385fa | 2016-10-19 15:51:36 +0200 | [diff] [blame] | 2296 | if ((pred_name[0] != '.') || (pred_name_len != 1)) { |
| 2297 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[1], id + 1); |
| 2298 | *parsed = -1; |
| 2299 | return NULL; |
| 2300 | } |
Michal Vasko | acb87b6 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2301 | } else { |
| 2302 | r = 0; |
| 2303 | if (llist_value) { |
| 2304 | val_len = strlen(llist_value); |
| 2305 | } else { |
| 2306 | val_len = 0; |
| 2307 | } |
| 2308 | } |
| 2309 | |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2310 | llist = (struct lyd_node_leaf_list *)sibling; |
Michal Vasko | acb87b6 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2311 | if ((!val_len && llist->value_str && llist->value_str[0]) |
| 2312 | || (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] | 2313 | continue; |
| 2314 | } |
Michal Vasko | acb87b6 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2315 | id += r; |
| 2316 | last_parsed += r; |
Michal Vasko | ee385fa | 2016-10-19 15:51:36 +0200 | [diff] [blame] | 2317 | has_predicate = last_has_pred; |
Michal Vasko | acb87b6 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2318 | |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 2319 | } else if (sibling->schema->nodetype == LYS_LIST) { |
| 2320 | /* list, we need predicates'n'stuff then */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2321 | r = 0; |
| 2322 | if (!has_predicate) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2323 | LOGVAL(LYE_PATH_MISSKEY, LY_VLOG_NONE, NULL, name); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2324 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2325 | return NULL; |
| 2326 | } |
| 2327 | ret = resolve_partial_json_data_list_predicate(id, name, sibling, &r); |
| 2328 | if (ret == -1) { |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2329 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2330 | return NULL; |
| 2331 | } else if (ret == 1) { |
| 2332 | /* this list instance does not match */ |
| 2333 | continue; |
| 2334 | } |
| 2335 | id += r; |
| 2336 | last_parsed += r; |
| 2337 | } |
| 2338 | |
| 2339 | *parsed += last_parsed; |
| 2340 | |
| 2341 | /* the result node? */ |
| 2342 | if (!id[0]) { |
| 2343 | return sibling; |
| 2344 | } |
| 2345 | |
| 2346 | /* move down the tree, if possible */ |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 2347 | if (sibling->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2348 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2349 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2350 | return NULL; |
| 2351 | } |
Michal Vasko | c6c52cf | 2016-03-29 11:53:04 +0200 | [diff] [blame] | 2352 | last_match = sibling; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2353 | start = sibling->child; |
| 2354 | if (start) { |
| 2355 | prev_mod = start->schema->module; |
| 2356 | } |
| 2357 | break; |
| 2358 | } |
| 2359 | } |
| 2360 | |
| 2361 | /* no match, return last match */ |
| 2362 | if (!sibling) { |
| 2363 | return last_match; |
| 2364 | } |
| 2365 | |
| 2366 | 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] | 2367 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2368 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2369 | return NULL; |
| 2370 | } |
| 2371 | id += r; |
| 2372 | last_parsed = r; |
| 2373 | } |
| 2374 | |
| 2375 | /* cannot get here */ |
| 2376 | LOGINT; |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2377 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2378 | return NULL; |
| 2379 | } |
| 2380 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2381 | /** |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2382 | * @brief Resolves length or range intervals. Does not log. |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2383 | * Syntax is assumed to be correct, *ret MUST be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2384 | * |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2385 | * @param[in] str_restr Restriction as a string. |
| 2386 | * @param[in] type Type of the restriction. |
| 2387 | * @param[out] ret Final interval structure that starts with |
| 2388 | * the interval of the initial type, continues with intervals |
| 2389 | * of any superior types derived from the initial one, and |
| 2390 | * finishes with intervals from our \p type. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2391 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2392 | * @return EXIT_SUCCESS on succes, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2393 | */ |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2394 | int |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2395 | 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] | 2396 | { |
| 2397 | /* 0 - unsigned, 1 - signed, 2 - floating point */ |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2398 | int kind; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2399 | int64_t local_smin, local_smax, local_fmin, local_fmax; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2400 | uint64_t local_umin, local_umax; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2401 | uint8_t local_fdig; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2402 | const char *seg_ptr, *ptr; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2403 | 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] | 2404 | |
| 2405 | switch (type->base) { |
| 2406 | case LY_TYPE_BINARY: |
| 2407 | kind = 0; |
| 2408 | local_umin = 0; |
| 2409 | local_umax = 18446744073709551615UL; |
| 2410 | |
| 2411 | if (!str_restr && type->info.binary.length) { |
| 2412 | str_restr = type->info.binary.length->expr; |
| 2413 | } |
| 2414 | break; |
| 2415 | case LY_TYPE_DEC64: |
| 2416 | kind = 2; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2417 | local_fmin = __INT64_C(-9223372036854775807) - __INT64_C(1); |
| 2418 | local_fmax = __INT64_C(9223372036854775807); |
| 2419 | local_fdig = type->info.dec64.dig; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2420 | |
| 2421 | if (!str_restr && type->info.dec64.range) { |
| 2422 | str_restr = type->info.dec64.range->expr; |
| 2423 | } |
| 2424 | break; |
| 2425 | case LY_TYPE_INT8: |
| 2426 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2427 | local_smin = __INT64_C(-128); |
| 2428 | local_smax = __INT64_C(127); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2429 | |
| 2430 | if (!str_restr && type->info.num.range) { |
| 2431 | str_restr = type->info.num.range->expr; |
| 2432 | } |
| 2433 | break; |
| 2434 | case LY_TYPE_INT16: |
| 2435 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2436 | local_smin = __INT64_C(-32768); |
| 2437 | local_smax = __INT64_C(32767); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2438 | |
| 2439 | if (!str_restr && type->info.num.range) { |
| 2440 | str_restr = type->info.num.range->expr; |
| 2441 | } |
| 2442 | break; |
| 2443 | case LY_TYPE_INT32: |
| 2444 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2445 | local_smin = __INT64_C(-2147483648); |
| 2446 | local_smax = __INT64_C(2147483647); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2447 | |
| 2448 | if (!str_restr && type->info.num.range) { |
| 2449 | str_restr = type->info.num.range->expr; |
| 2450 | } |
| 2451 | break; |
| 2452 | case LY_TYPE_INT64: |
| 2453 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2454 | local_smin = __INT64_C(-9223372036854775807) - __INT64_C(1); |
| 2455 | local_smax = __INT64_C(9223372036854775807); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2456 | |
| 2457 | if (!str_restr && type->info.num.range) { |
| 2458 | str_restr = type->info.num.range->expr; |
| 2459 | } |
| 2460 | break; |
| 2461 | case LY_TYPE_UINT8: |
| 2462 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2463 | local_umin = __UINT64_C(0); |
| 2464 | local_umax = __UINT64_C(255); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2465 | |
| 2466 | if (!str_restr && type->info.num.range) { |
| 2467 | str_restr = type->info.num.range->expr; |
| 2468 | } |
| 2469 | break; |
| 2470 | case LY_TYPE_UINT16: |
| 2471 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2472 | local_umin = __UINT64_C(0); |
| 2473 | local_umax = __UINT64_C(65535); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2474 | |
| 2475 | if (!str_restr && type->info.num.range) { |
| 2476 | str_restr = type->info.num.range->expr; |
| 2477 | } |
| 2478 | break; |
| 2479 | case LY_TYPE_UINT32: |
| 2480 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2481 | local_umin = __UINT64_C(0); |
| 2482 | local_umax = __UINT64_C(4294967295); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2483 | |
| 2484 | if (!str_restr && type->info.num.range) { |
| 2485 | str_restr = type->info.num.range->expr; |
| 2486 | } |
| 2487 | break; |
| 2488 | case LY_TYPE_UINT64: |
| 2489 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2490 | local_umin = __UINT64_C(0); |
| 2491 | local_umax = __UINT64_C(18446744073709551615); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2492 | |
| 2493 | if (!str_restr && type->info.num.range) { |
| 2494 | str_restr = type->info.num.range->expr; |
| 2495 | } |
| 2496 | break; |
| 2497 | case LY_TYPE_STRING: |
| 2498 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2499 | local_umin = __UINT64_C(0); |
| 2500 | local_umax = __UINT64_C(18446744073709551615); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2501 | |
| 2502 | if (!str_restr && type->info.str.length) { |
| 2503 | str_restr = type->info.str.length->expr; |
| 2504 | } |
| 2505 | break; |
| 2506 | default: |
| 2507 | LOGINT; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2508 | return -1; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2509 | } |
| 2510 | |
| 2511 | /* process superior types */ |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2512 | if (type->der) { |
| 2513 | if (resolve_len_ran_interval(NULL, &type->der->type, &intv)) { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2514 | LOGINT; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2515 | return -1; |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2516 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2517 | assert(!intv || (intv->kind == kind)); |
| 2518 | } |
| 2519 | |
| 2520 | if (!str_restr) { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2521 | /* we do not have any restriction, return superior ones */ |
| 2522 | *ret = intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2523 | return EXIT_SUCCESS; |
| 2524 | } |
| 2525 | |
| 2526 | /* adjust local min and max */ |
| 2527 | if (intv) { |
| 2528 | tmp_intv = intv; |
| 2529 | |
| 2530 | if (kind == 0) { |
| 2531 | local_umin = tmp_intv->value.uval.min; |
| 2532 | } else if (kind == 1) { |
| 2533 | local_smin = tmp_intv->value.sval.min; |
| 2534 | } else if (kind == 2) { |
| 2535 | local_fmin = tmp_intv->value.fval.min; |
| 2536 | } |
| 2537 | |
| 2538 | while (tmp_intv->next) { |
| 2539 | tmp_intv = tmp_intv->next; |
| 2540 | } |
| 2541 | |
| 2542 | if (kind == 0) { |
| 2543 | local_umax = tmp_intv->value.uval.max; |
| 2544 | } else if (kind == 1) { |
| 2545 | local_smax = tmp_intv->value.sval.max; |
| 2546 | } else if (kind == 2) { |
| 2547 | local_fmax = tmp_intv->value.fval.max; |
| 2548 | } |
| 2549 | } |
| 2550 | |
| 2551 | /* finally parse our restriction */ |
| 2552 | seg_ptr = str_restr; |
Pavol Vican | 9e7c01d | 2016-08-29 09:36:17 +0200 | [diff] [blame] | 2553 | tmp_intv = NULL; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2554 | while (1) { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2555 | if (!tmp_local_intv) { |
| 2556 | assert(!local_intv); |
| 2557 | local_intv = malloc(sizeof *local_intv); |
| 2558 | tmp_local_intv = local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2559 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2560 | tmp_local_intv->next = malloc(sizeof *tmp_local_intv); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2561 | tmp_local_intv = tmp_local_intv->next; |
| 2562 | } |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2563 | if (!tmp_local_intv) { |
| 2564 | LOGMEM; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2565 | goto error; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2566 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2567 | |
| 2568 | tmp_local_intv->kind = kind; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2569 | tmp_local_intv->type = type; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2570 | tmp_local_intv->next = NULL; |
| 2571 | |
| 2572 | /* min */ |
| 2573 | ptr = seg_ptr; |
| 2574 | while (isspace(ptr[0])) { |
| 2575 | ++ptr; |
| 2576 | } |
| 2577 | if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) { |
| 2578 | if (kind == 0) { |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2579 | tmp_local_intv->value.uval.min = strtol(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2580 | } else if (kind == 1) { |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2581 | tmp_local_intv->value.sval.min = strtol(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2582 | } else if (kind == 2) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 2583 | if (parse_range_dec64(&ptr, local_fdig, &tmp_local_intv->value.fval.min)) { |
| 2584 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, ptr, "range"); |
| 2585 | goto error; |
| 2586 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2587 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2588 | } else if (!strncmp(ptr, "min", 3)) { |
| 2589 | if (kind == 0) { |
| 2590 | tmp_local_intv->value.uval.min = local_umin; |
| 2591 | } else if (kind == 1) { |
| 2592 | tmp_local_intv->value.sval.min = local_smin; |
| 2593 | } else if (kind == 2) { |
| 2594 | tmp_local_intv->value.fval.min = local_fmin; |
| 2595 | } |
| 2596 | |
| 2597 | ptr += 3; |
| 2598 | } else if (!strncmp(ptr, "max", 3)) { |
| 2599 | if (kind == 0) { |
| 2600 | tmp_local_intv->value.uval.min = local_umax; |
| 2601 | } else if (kind == 1) { |
| 2602 | tmp_local_intv->value.sval.min = local_smax; |
| 2603 | } else if (kind == 2) { |
| 2604 | tmp_local_intv->value.fval.min = local_fmax; |
| 2605 | } |
| 2606 | |
| 2607 | ptr += 3; |
| 2608 | } else { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2609 | LOGINT; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2610 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2611 | } |
| 2612 | |
| 2613 | while (isspace(ptr[0])) { |
| 2614 | ptr++; |
| 2615 | } |
| 2616 | |
| 2617 | /* no interval or interval */ |
| 2618 | if ((ptr[0] == '|') || !ptr[0]) { |
| 2619 | if (kind == 0) { |
| 2620 | tmp_local_intv->value.uval.max = tmp_local_intv->value.uval.min; |
| 2621 | } else if (kind == 1) { |
| 2622 | tmp_local_intv->value.sval.max = tmp_local_intv->value.sval.min; |
| 2623 | } else if (kind == 2) { |
| 2624 | tmp_local_intv->value.fval.max = tmp_local_intv->value.fval.min; |
| 2625 | } |
| 2626 | } else if (!strncmp(ptr, "..", 2)) { |
| 2627 | /* skip ".." */ |
| 2628 | ptr += 2; |
| 2629 | while (isspace(ptr[0])) { |
| 2630 | ++ptr; |
| 2631 | } |
| 2632 | |
| 2633 | /* max */ |
| 2634 | if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) { |
| 2635 | if (kind == 0) { |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2636 | tmp_local_intv->value.uval.max = strtol(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2637 | } else if (kind == 1) { |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2638 | tmp_local_intv->value.sval.max = strtol(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2639 | } else if (kind == 2) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 2640 | if (parse_range_dec64(&ptr, local_fdig, &tmp_local_intv->value.fval.max)) { |
| 2641 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, ptr, "range"); |
| 2642 | goto error; |
| 2643 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2644 | } |
| 2645 | } else if (!strncmp(ptr, "max", 3)) { |
| 2646 | if (kind == 0) { |
| 2647 | tmp_local_intv->value.uval.max = local_umax; |
| 2648 | } else if (kind == 1) { |
| 2649 | tmp_local_intv->value.sval.max = local_smax; |
| 2650 | } else if (kind == 2) { |
| 2651 | tmp_local_intv->value.fval.max = local_fmax; |
| 2652 | } |
| 2653 | } else { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2654 | LOGINT; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2655 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 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 | |
Pavol Vican | 9e7c01d | 2016-08-29 09:36:17 +0200 | [diff] [blame] | 2662 | /* check min and max in correct order*/ |
| 2663 | if (kind == 0) { |
| 2664 | /* current segment */ |
| 2665 | if (tmp_local_intv->value.uval.min > tmp_local_intv->value.uval.max) { |
| 2666 | goto error; |
| 2667 | } |
| 2668 | if (tmp_local_intv->value.uval.min < local_umin || tmp_local_intv->value.uval.max > local_umax) { |
| 2669 | goto error; |
| 2670 | } |
| 2671 | /* segments sholud be ascending order */ |
Pavol Vican | 69f62c9 | 2016-08-30 09:06:25 +0200 | [diff] [blame] | 2672 | 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] | 2673 | goto error; |
| 2674 | } |
| 2675 | } else if (kind == 1) { |
| 2676 | if (tmp_local_intv->value.sval.min > tmp_local_intv->value.sval.max) { |
| 2677 | goto error; |
| 2678 | } |
| 2679 | if (tmp_local_intv->value.sval.min < local_smin || tmp_local_intv->value.sval.max > local_smax) { |
| 2680 | goto error; |
| 2681 | } |
Pavol Vican | 69f62c9 | 2016-08-30 09:06:25 +0200 | [diff] [blame] | 2682 | 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] | 2683 | goto error; |
| 2684 | } |
| 2685 | } else if (kind == 2) { |
| 2686 | if (tmp_local_intv->value.fval.min > tmp_local_intv->value.fval.max) { |
| 2687 | goto error; |
| 2688 | } |
| 2689 | if (tmp_local_intv->value.fval.min < local_fmin || tmp_local_intv->value.fval.max > local_fmax) { |
| 2690 | goto error; |
| 2691 | } |
Pavol Vican | 69f62c9 | 2016-08-30 09:06:25 +0200 | [diff] [blame] | 2692 | 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] | 2693 | /* 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] | 2694 | goto error; |
| 2695 | } |
| 2696 | } |
| 2697 | |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2698 | /* next segment (next OR) */ |
| 2699 | seg_ptr = strchr(seg_ptr, '|'); |
| 2700 | if (!seg_ptr) { |
| 2701 | break; |
| 2702 | } |
| 2703 | seg_ptr++; |
Pavol Vican | 9e7c01d | 2016-08-29 09:36:17 +0200 | [diff] [blame] | 2704 | tmp_intv = tmp_local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2705 | } |
| 2706 | |
| 2707 | /* check local restrictions against superior ones */ |
| 2708 | if (intv) { |
| 2709 | tmp_intv = intv; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2710 | tmp_local_intv = local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2711 | |
| 2712 | while (tmp_local_intv && tmp_intv) { |
| 2713 | /* reuse local variables */ |
| 2714 | if (kind == 0) { |
| 2715 | local_umin = tmp_local_intv->value.uval.min; |
| 2716 | local_umax = tmp_local_intv->value.uval.max; |
| 2717 | |
| 2718 | /* it must be in this interval */ |
| 2719 | if ((local_umin >= tmp_intv->value.uval.min) && (local_umin <= tmp_intv->value.uval.max)) { |
| 2720 | /* this interval is covered, next one */ |
| 2721 | if (local_umax <= tmp_intv->value.uval.max) { |
| 2722 | tmp_local_intv = tmp_local_intv->next; |
| 2723 | continue; |
| 2724 | /* ascending order of restrictions -> fail */ |
| 2725 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2726 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2727 | } |
| 2728 | } |
| 2729 | } else if (kind == 1) { |
| 2730 | local_smin = tmp_local_intv->value.sval.min; |
| 2731 | local_smax = tmp_local_intv->value.sval.max; |
| 2732 | |
| 2733 | if ((local_smin >= tmp_intv->value.sval.min) && (local_smin <= tmp_intv->value.sval.max)) { |
| 2734 | if (local_smax <= tmp_intv->value.sval.max) { |
| 2735 | tmp_local_intv = tmp_local_intv->next; |
| 2736 | continue; |
| 2737 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2738 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2739 | } |
| 2740 | } |
| 2741 | } else if (kind == 2) { |
| 2742 | local_fmin = tmp_local_intv->value.fval.min; |
| 2743 | local_fmax = tmp_local_intv->value.fval.max; |
| 2744 | |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2745 | 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] | 2746 | && (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] | 2747 | 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] | 2748 | tmp_local_intv = tmp_local_intv->next; |
| 2749 | continue; |
| 2750 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2751 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2752 | } |
| 2753 | } |
| 2754 | } |
| 2755 | |
| 2756 | tmp_intv = tmp_intv->next; |
| 2757 | } |
| 2758 | |
| 2759 | /* some interval left uncovered -> fail */ |
| 2760 | if (tmp_local_intv) { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2761 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2762 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2763 | } |
| 2764 | |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2765 | /* append the local intervals to all the intervals of the superior types, return it all */ |
| 2766 | if (intv) { |
| 2767 | for (tmp_intv = intv; tmp_intv->next; tmp_intv = tmp_intv->next); |
| 2768 | tmp_intv->next = local_intv; |
| 2769 | } else { |
| 2770 | intv = local_intv; |
| 2771 | } |
| 2772 | *ret = intv; |
| 2773 | |
| 2774 | return EXIT_SUCCESS; |
| 2775 | |
| 2776 | error: |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2777 | while (intv) { |
| 2778 | tmp_intv = intv->next; |
| 2779 | free(intv); |
| 2780 | intv = tmp_intv; |
| 2781 | } |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2782 | while (local_intv) { |
| 2783 | tmp_local_intv = local_intv->next; |
| 2784 | free(local_intv); |
| 2785 | local_intv = tmp_local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2786 | } |
| 2787 | |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2788 | return -1; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2789 | } |
| 2790 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2791 | /** |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2792 | * @brief Resolve a typedef, return only resolved typedefs if derived. If leafref, it must be |
| 2793 | * resolved for this function to return it. Does not log. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2794 | * |
| 2795 | * @param[in] name Typedef name. |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2796 | * @param[in] mod_name Typedef name module name. |
| 2797 | * @param[in] module Main module. |
| 2798 | * @param[in] parent Parent of the resolved type definition. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2799 | * @param[out] ret Pointer to the resolved typedef. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2800 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2801 | * @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] | 2802 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2803 | int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 2804 | resolve_superior_type(const char *name, const char *mod_name, const struct lys_module *module, |
| 2805 | const struct lys_node *parent, struct lys_tpdf **ret) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2806 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2807 | int i, j; |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2808 | struct lys_tpdf *tpdf, *match; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2809 | int tpdf_size; |
| 2810 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2811 | if (!mod_name) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2812 | /* no prefix, try built-in types */ |
| 2813 | for (i = 1; i < LY_DATA_TYPE_COUNT; i++) { |
| 2814 | if (!strcmp(ly_types[i].def->name, name)) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2815 | if (ret) { |
| 2816 | *ret = ly_types[i].def; |
| 2817 | } |
| 2818 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2819 | } |
| 2820 | } |
| 2821 | } else { |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2822 | if (!strcmp(mod_name, module->name)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2823 | /* prefix refers to the current module, ignore it */ |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2824 | mod_name = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2825 | } |
| 2826 | } |
| 2827 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2828 | if (!mod_name && parent) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2829 | /* search in local typedefs */ |
| 2830 | while (parent) { |
| 2831 | switch (parent->nodetype) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2832 | case LYS_CONTAINER: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 2833 | tpdf_size = ((struct lys_node_container *)parent)->tpdf_size; |
| 2834 | tpdf = ((struct lys_node_container *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2835 | break; |
| 2836 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2837 | case LYS_LIST: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 2838 | tpdf_size = ((struct lys_node_list *)parent)->tpdf_size; |
| 2839 | tpdf = ((struct lys_node_list *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2840 | break; |
| 2841 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2842 | case LYS_GROUPING: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 2843 | tpdf_size = ((struct lys_node_grp *)parent)->tpdf_size; |
| 2844 | tpdf = ((struct lys_node_grp *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2845 | break; |
| 2846 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2847 | case LYS_RPC: |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 2848 | case LYS_ACTION: |
| 2849 | tpdf_size = ((struct lys_node_rpc_action *)parent)->tpdf_size; |
| 2850 | tpdf = ((struct lys_node_rpc_action *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2851 | break; |
| 2852 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2853 | case LYS_NOTIF: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 2854 | tpdf_size = ((struct lys_node_notif *)parent)->tpdf_size; |
| 2855 | tpdf = ((struct lys_node_notif *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2856 | break; |
| 2857 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2858 | case LYS_INPUT: |
| 2859 | case LYS_OUTPUT: |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 2860 | tpdf_size = ((struct lys_node_inout *)parent)->tpdf_size; |
| 2861 | tpdf = ((struct lys_node_inout *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2862 | break; |
| 2863 | |
| 2864 | default: |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 2865 | parent = lys_parent(parent); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2866 | continue; |
| 2867 | } |
| 2868 | |
| 2869 | for (i = 0; i < tpdf_size; i++) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 2870 | if (!strcmp(tpdf[i].name, name) && tpdf[i].type.base > 0) { |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2871 | match = &tpdf[i]; |
| 2872 | goto check_leafref; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2873 | } |
| 2874 | } |
| 2875 | |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 2876 | parent = lys_parent(parent); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2877 | } |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2878 | } else { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2879 | /* get module where to search */ |
Michal Vasko | b6729c6 | 2015-10-21 12:09:47 +0200 | [diff] [blame] | 2880 | module = lys_get_import_module(module, NULL, 0, mod_name, 0); |
Michal Vasko | c935fff | 2015-08-17 14:02:06 +0200 | [diff] [blame] | 2881 | if (!module) { |
| 2882 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2883 | } |
| 2884 | } |
| 2885 | |
| 2886 | /* search in top level typedefs */ |
| 2887 | for (i = 0; i < module->tpdf_size; i++) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 2888 | 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] | 2889 | match = &module->tpdf[i]; |
| 2890 | goto check_leafref; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2891 | } |
| 2892 | } |
| 2893 | |
| 2894 | /* search in submodules */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2895 | for (i = 0; i < module->inc_size && module->inc[i].submodule; i++) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2896 | for (j = 0; j < module->inc[i].submodule->tpdf_size; j++) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 2897 | 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] | 2898 | match = &module->inc[i].submodule->tpdf[j]; |
| 2899 | goto check_leafref; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2900 | } |
| 2901 | } |
| 2902 | } |
| 2903 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2904 | return EXIT_FAILURE; |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2905 | |
| 2906 | check_leafref: |
| 2907 | if (ret) { |
| 2908 | *ret = match; |
| 2909 | } |
| 2910 | if (match->type.base == LY_TYPE_LEAFREF) { |
| 2911 | while (!match->type.info.lref.path) { |
| 2912 | match = match->type.der; |
| 2913 | assert(match); |
| 2914 | } |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2915 | } |
| 2916 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2917 | } |
| 2918 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2919 | /** |
| 2920 | * @brief Check the default \p value of the \p type. Logs directly. |
| 2921 | * |
| 2922 | * @param[in] type Type definition to use. |
| 2923 | * @param[in] value Default value to check. |
Michal Vasko | 5682640 | 2016-03-02 11:11:37 +0100 | [diff] [blame] | 2924 | * @param[in] module Type module. |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2925 | * |
| 2926 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 2927 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2928 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2929 | 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] | 2930 | { |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 2931 | struct lys_tpdf *base_tpdf = NULL; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2932 | struct lyd_node_leaf_list node; |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 2933 | int ret = EXIT_SUCCESS; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2934 | |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 2935 | if (type->base <= LY_TYPE_DER) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 2936 | /* the type was not resolved yet, nothing to do for now */ |
| 2937 | return EXIT_FAILURE; |
| 2938 | } |
| 2939 | |
| 2940 | if (!value) { |
| 2941 | /* we do not have a new default value, so is there any to check even, in some base type? */ |
| 2942 | for (base_tpdf = type->der; base_tpdf->type.der; base_tpdf = base_tpdf->type.der) { |
| 2943 | if (base_tpdf->dflt) { |
| 2944 | value = base_tpdf->dflt; |
| 2945 | break; |
| 2946 | } |
| 2947 | } |
| 2948 | |
| 2949 | if (!value) { |
| 2950 | /* no default value, nothing to check, all is well */ |
| 2951 | return EXIT_SUCCESS; |
| 2952 | } |
| 2953 | |
| 2954 | /* 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)? */ |
| 2955 | switch (type->base) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 2956 | case LY_TYPE_IDENT: |
| 2957 | case LY_TYPE_INST: |
| 2958 | case LY_TYPE_LEAFREF: |
| 2959 | case LY_TYPE_BOOL: |
| 2960 | case LY_TYPE_EMPTY: |
| 2961 | /* these have no restrictions, so we would do the exact same work as the unres in the base typedef */ |
| 2962 | return EXIT_SUCCESS; |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 2963 | case LY_TYPE_BITS: |
| 2964 | /* the default value must match the restricted list of values, if the type was restricted */ |
| 2965 | if (type->info.bits.count) { |
| 2966 | break; |
| 2967 | } |
| 2968 | return EXIT_SUCCESS; |
| 2969 | case LY_TYPE_ENUM: |
| 2970 | /* the default value must match the restricted list of values, if the type was restricted */ |
| 2971 | if (type->info.enums.count) { |
| 2972 | break; |
| 2973 | } |
| 2974 | return EXIT_SUCCESS; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 2975 | case LY_TYPE_DEC64: |
| 2976 | if (type->info.dec64.range) { |
| 2977 | break; |
| 2978 | } |
| 2979 | return EXIT_SUCCESS; |
| 2980 | case LY_TYPE_BINARY: |
| 2981 | if (type->info.binary.length) { |
| 2982 | break; |
| 2983 | } |
| 2984 | return EXIT_SUCCESS; |
| 2985 | case LY_TYPE_INT8: |
| 2986 | case LY_TYPE_INT16: |
| 2987 | case LY_TYPE_INT32: |
| 2988 | case LY_TYPE_INT64: |
| 2989 | case LY_TYPE_UINT8: |
| 2990 | case LY_TYPE_UINT16: |
| 2991 | case LY_TYPE_UINT32: |
| 2992 | case LY_TYPE_UINT64: |
| 2993 | if (type->info.num.range) { |
| 2994 | break; |
| 2995 | } |
| 2996 | return EXIT_SUCCESS; |
| 2997 | case LY_TYPE_STRING: |
| 2998 | if (type->info.str.length || type->info.str.patterns) { |
| 2999 | break; |
| 3000 | } |
| 3001 | return EXIT_SUCCESS; |
| 3002 | case LY_TYPE_UNION: |
| 3003 | /* way too much trouble learning whether we need to check the default again, so just do it */ |
| 3004 | break; |
| 3005 | default: |
| 3006 | LOGINT; |
| 3007 | return -1; |
| 3008 | } |
Radek Krejci | 55a161c | 2016-09-05 17:13:25 +0200 | [diff] [blame] | 3009 | } else if (type->base == LY_TYPE_EMPTY) { |
| 3010 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "default", type->parent->name); |
| 3011 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The \"empty\" data type cannot have a default value."); |
| 3012 | return -1; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3013 | } |
| 3014 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3015 | /* dummy leaf */ |
Radek Krejci | 4fe36bd | 2016-03-17 16:47:16 +0100 | [diff] [blame] | 3016 | memset(&node, 0, sizeof node); |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3017 | node.value_str = value; |
| 3018 | node.value_type = type->base; |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 3019 | node.schema = calloc(1, sizeof (struct lys_node_leaf)); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3020 | if (!node.schema) { |
| 3021 | LOGMEM; |
| 3022 | return -1; |
| 3023 | } |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3024 | node.schema->name = strdup("fake-default"); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3025 | if (!node.schema->name) { |
| 3026 | LOGMEM; |
Michal Vasko | f49eda0 | 2016-07-21 12:17:01 +0200 | [diff] [blame] | 3027 | free(node.schema); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3028 | return -1; |
| 3029 | } |
Michal Vasko | 5682640 | 2016-03-02 11:11:37 +0100 | [diff] [blame] | 3030 | node.schema->module = module; |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 3031 | memcpy(&((struct lys_node_leaf *)node.schema)->type, type, sizeof *type); |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3032 | |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 3033 | if (type->base == LY_TYPE_LEAFREF) { |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3034 | if (!type->info.lref.target) { |
| 3035 | ret = EXIT_FAILURE; |
| 3036 | goto finish; |
| 3037 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3038 | ret = check_default(&type->info.lref.target->type, value, module); |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3039 | |
| 3040 | } else if ((type->base == LY_TYPE_INST) || (type->base == LY_TYPE_IDENT)) { |
| 3041 | /* it was converted to JSON format before, nothing else sensible we can do */ |
| 3042 | |
| 3043 | } else { |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 3044 | if (lyp_parse_value(&node, NULL, 1)) { |
| 3045 | ret = -1; |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3046 | if (base_tpdf) { |
| 3047 | /* default value was is defined in some base typedef */ |
| 3048 | if ((type->base == LY_TYPE_BITS && type->der->type.der) || |
| 3049 | (type->base == LY_TYPE_ENUM && type->der->type.der)) { |
| 3050 | /* we have refined bits/enums */ |
| 3051 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, |
| 3052 | "Invalid value \"%s\" of the default statement inherited to \"%s\" from \"%s\" base type.", |
| 3053 | value, type->parent->name, base_tpdf->name); |
| 3054 | } |
| 3055 | } |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 3056 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3057 | } |
| 3058 | |
| 3059 | finish: |
| 3060 | if (node.value_type == LY_TYPE_BITS) { |
| 3061 | free(node.value.bit); |
| 3062 | } |
| 3063 | free((char *)node.schema->name); |
| 3064 | free(node.schema); |
| 3065 | |
| 3066 | return ret; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3067 | } |
| 3068 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3069 | /** |
| 3070 | * @brief Check a key for mandatory attributes. Logs directly. |
| 3071 | * |
| 3072 | * @param[in] key The key to check. |
| 3073 | * @param[in] flags What flags to check. |
| 3074 | * @param[in] list The list of all the keys. |
| 3075 | * @param[in] index Index of the key in the key list. |
| 3076 | * @param[in] name The name of the keys. |
| 3077 | * @param[in] len The name length. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3078 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3079 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3080 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3081 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3082 | 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] | 3083 | { |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3084 | struct lys_node_leaf *key = list->keys[index]; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3085 | char *dup = NULL; |
| 3086 | int j; |
| 3087 | |
| 3088 | /* existence */ |
| 3089 | if (!key) { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3090 | if (name[len] != '\0') { |
| 3091 | dup = strdup(name); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3092 | if (!dup) { |
| 3093 | LOGMEM; |
| 3094 | return -1; |
| 3095 | } |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3096 | dup[len] = '\0'; |
| 3097 | name = dup; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3098 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3099 | LOGVAL(LYE_KEY_MISS, LY_VLOG_LYS, list, name); |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 3100 | free(dup); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3101 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3102 | } |
| 3103 | |
| 3104 | /* uniqueness */ |
| 3105 | for (j = index - 1; j >= 0; j--) { |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3106 | if (key == list->keys[j]) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3107 | LOGVAL(LYE_KEY_DUP, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3108 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3109 | } |
| 3110 | } |
| 3111 | |
| 3112 | /* key is a leaf */ |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3113 | if (key->nodetype != LYS_LEAF) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3114 | LOGVAL(LYE_KEY_NLEAF, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3115 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3116 | } |
| 3117 | |
| 3118 | /* type of the leaf is not built-in empty */ |
Radek Krejci | c3738db | 2016-09-15 15:51:21 +0200 | [diff] [blame] | 3119 | if (key->type.base == LY_TYPE_EMPTY && key->module->version < 2) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3120 | LOGVAL(LYE_KEY_TYPE, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3121 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3122 | } |
| 3123 | |
| 3124 | /* config attribute is the same as of the list */ |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3125 | if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3126 | LOGVAL(LYE_KEY_CONFIG, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3127 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3128 | } |
| 3129 | |
Radek Krejci | 55e2cdc | 2016-03-11 13:51:09 +0100 | [diff] [blame] | 3130 | /* key is not placed from augment */ |
| 3131 | if (key->parent->nodetype == LYS_AUGMENT) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3132 | LOGVAL(LYE_KEY_MISS, LY_VLOG_LYS, key, key->name); |
| 3133 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, key, "Key inserted from augment."); |
Radek Krejci | 55e2cdc | 2016-03-11 13:51:09 +0100 | [diff] [blame] | 3134 | return -1; |
| 3135 | } |
| 3136 | |
Radek Krejci | 3f21ada | 2016-08-01 13:34:31 +0200 | [diff] [blame] | 3137 | /* key is not when/if-feature -conditional */ |
| 3138 | j = 0; |
| 3139 | if (key->when || (key->iffeature_size && (j = 1))) { |
| 3140 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, key, j ? "if-feature" : "when", "leaf"); |
| 3141 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, key, "Key definition cannot depend on a \"%s\" condition.", |
| 3142 | j ? "if-feature" : "when"); |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3143 | return -1; |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3144 | } |
| 3145 | |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3146 | return EXIT_SUCCESS; |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3147 | } |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3148 | |
| 3149 | /** |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3150 | * @brief Resolve (test the target exists) unique. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3151 | * |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3152 | * @param[in] parent The parent node of the unique structure. |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3153 | * @param[in] uniq_str_path One path from the unique string. |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3154 | * |
| 3155 | * @return EXIT_SUCCESS on succes, EXIT_FAILURE on forward reference, -1 on error. |
| 3156 | */ |
| 3157 | int |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3158 | 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] | 3159 | { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3160 | int rc; |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3161 | const struct lys_node *leaf = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3162 | |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 3163 | 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] | 3164 | if (rc || !leaf) { |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3165 | if (rc) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3166 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3167 | if (rc > 0) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3168 | 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] | 3169 | } else if (rc == -2) { |
Michal Vasko | c66c6d8 | 2016-04-12 11:37:31 +0200 | [diff] [blame] | 3170 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, parent, "Unique argument references list."); |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3171 | } |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3172 | rc = -1; |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3173 | } else { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3174 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
| 3175 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, parent, "Target leaf not found."); |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3176 | rc = EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3177 | } |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3178 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3179 | } |
Michal Vasko | 9bb061b | 2016-02-12 11:00:19 +0100 | [diff] [blame] | 3180 | if (leaf->nodetype != LYS_LEAF) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3181 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
| 3182 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, parent, "Target is not a leaf."); |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3183 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3184 | } |
| 3185 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3186 | /* check status */ |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3187 | 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] | 3188 | return -1; |
| 3189 | } |
| 3190 | |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3191 | /* check that all unique's targets are of the same config type */ |
| 3192 | if (*trg_type) { |
| 3193 | if (((*trg_type == 1) && (leaf->flags & LYS_CONFIG_R)) || ((*trg_type == 2) && (leaf->flags & LYS_CONFIG_W))) { |
| 3194 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
| 3195 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, parent, |
| 3196 | "Leaf \"%s\" referenced in unique statement is config %s, but previous referenced leaf is config %s.", |
| 3197 | uniq_str_path, *trg_type == 1 ? "false" : "true", *trg_type == 1 ? "true" : "false"); |
| 3198 | return -1; |
| 3199 | } |
| 3200 | } else { |
| 3201 | /* first unique */ |
| 3202 | if (leaf->flags & LYS_CONFIG_W) { |
| 3203 | *trg_type = 1; |
| 3204 | } else { |
| 3205 | *trg_type = 2; |
| 3206 | } |
| 3207 | } |
| 3208 | |
Radek Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame] | 3209 | /* set leaf's unique flag */ |
| 3210 | ((struct lys_node_leaf *)leaf)->flags |= LYS_UNIQUE; |
| 3211 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3212 | return EXIT_SUCCESS; |
| 3213 | |
| 3214 | error: |
| 3215 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3216 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3217 | } |
| 3218 | |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 3219 | void |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3220 | unres_data_del(struct unres_data *unres, uint32_t i) |
| 3221 | { |
| 3222 | /* there are items after the one deleted */ |
| 3223 | if (i+1 < unres->count) { |
| 3224 | /* we only move the data, memory is left allocated, why bother */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3225 | 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] | 3226 | |
| 3227 | /* deleting the last item */ |
| 3228 | } else if (i == 0) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3229 | free(unres->node); |
| 3230 | unres->node = NULL; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3231 | } |
| 3232 | |
| 3233 | /* if there are no items after and it is not the last one, just move the counter */ |
| 3234 | --unres->count; |
| 3235 | } |
| 3236 | |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3237 | /** |
| 3238 | * @brief Resolve (find) a data node from a specific module. Does not log. |
| 3239 | * |
| 3240 | * @param[in] mod Module to search in. |
| 3241 | * @param[in] name Name of the data node. |
| 3242 | * @param[in] nam_len Length of the name. |
| 3243 | * @param[in] start Data node to start the search from. |
| 3244 | * @param[in,out] parents Resolved nodes. If there are some parents, |
| 3245 | * they are replaced (!!) with the resolvents. |
| 3246 | * |
Michal Vasko | 2471e7f | 2016-04-11 11:00:15 +0200 | [diff] [blame] | 3247 | * @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] | 3248 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3249 | static int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3250 | 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] | 3251 | { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3252 | struct lyd_node *node; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3253 | int flag; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3254 | uint32_t i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3255 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3256 | if (!parents->count) { |
| 3257 | parents->count = 1; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3258 | parents->node = malloc(sizeof *parents->node); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3259 | if (!parents->node) { |
| 3260 | LOGMEM; |
Michal Vasko | 2471e7f | 2016-04-11 11:00:15 +0200 | [diff] [blame] | 3261 | return -1; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3262 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3263 | parents->node[0] = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3264 | } |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3265 | for (i = 0; i < parents->count;) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 3266 | 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] | 3267 | /* skip */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3268 | ++i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3269 | continue; |
| 3270 | } |
| 3271 | flag = 0; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3272 | LY_TREE_FOR(parents->node[i] ? parents->node[i]->child : start, node) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3273 | if (node->schema->module == mod && !strncmp(node->schema->name, name, nam_len) |
| 3274 | && node->schema->name[nam_len] == '\0') { |
| 3275 | /* matching target */ |
| 3276 | if (!flag) { |
Michal Vasko | 9a47e12 | 2015-09-03 14:26:32 +0200 | [diff] [blame] | 3277 | /* put node instead of the current parent */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3278 | parents->node[i] = node; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3279 | flag = 1; |
| 3280 | } else { |
Michal Vasko | 9a47e12 | 2015-09-03 14:26:32 +0200 | [diff] [blame] | 3281 | /* multiple matching, so create a new node */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3282 | ++parents->count; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3283 | parents->node = ly_realloc(parents->node, parents->count * sizeof *parents->node); |
| 3284 | if (!parents->node) { |
| 3285 | return EXIT_FAILURE; |
| 3286 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3287 | parents->node[parents->count-1] = node; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3288 | ++i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3289 | } |
| 3290 | } |
| 3291 | } |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3292 | |
| 3293 | if (!flag) { |
| 3294 | /* remove item from the parents list */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3295 | unres_data_del(parents, i); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3296 | } else { |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3297 | ++i; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3298 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3299 | } |
| 3300 | |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3301 | return parents->count ? EXIT_SUCCESS : EXIT_FAILURE; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3302 | } |
| 3303 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3304 | /** |
| 3305 | * @brief Resolve (find) a data node. Does not log. |
| 3306 | * |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3307 | * @param[in] mod_name Module name of the data node. |
| 3308 | * @param[in] mod_name_len Length of the module name. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3309 | * @param[in] name Name of the data node. |
| 3310 | * @param[in] nam_len Length of the name. |
| 3311 | * @param[in] start Data node to start the search from. |
| 3312 | * @param[in,out] parents Resolved nodes. If there are some parents, |
| 3313 | * they are replaced (!!) with the resolvents. |
| 3314 | * |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3315 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 otherwise. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3316 | */ |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3317 | static int |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3318 | 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] | 3319 | struct unres_data *parents) |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3320 | { |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3321 | const struct lys_module *mod; |
Michal Vasko | 31fc367 | 2015-10-21 12:08:13 +0200 | [diff] [blame] | 3322 | char *str; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3323 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3324 | assert(start); |
| 3325 | |
Michal Vasko | 31fc367 | 2015-10-21 12:08:13 +0200 | [diff] [blame] | 3326 | if (mod_name) { |
| 3327 | /* we have mod_name, find appropriate module */ |
| 3328 | str = strndup(mod_name, mod_name_len); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3329 | if (!str) { |
| 3330 | LOGMEM; |
| 3331 | return -1; |
| 3332 | } |
Michal Vasko | 31fc367 | 2015-10-21 12:08:13 +0200 | [diff] [blame] | 3333 | mod = ly_ctx_get_module(start->schema->module->ctx, str, NULL); |
| 3334 | free(str); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3335 | if (!mod) { |
| 3336 | /* invalid prefix */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3337 | return -1; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3338 | } |
| 3339 | } else { |
| 3340 | /* no prefix, module is the same as of current node */ |
| 3341 | mod = start->schema->module; |
| 3342 | } |
| 3343 | |
| 3344 | return resolve_data(mod, name, name_len, start, parents); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3345 | } |
| 3346 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3347 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3348 | * @brief Resolve a path predicate (leafref) in JSON data context. Logs directly |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3349 | * only specific errors, general no-resolvent error is left to the caller. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3350 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3351 | * @param[in] pred Predicate to use. |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3352 | * @param[in] node Node from which the predicate is being resolved |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3353 | * @param[in,out] node_match Nodes satisfying the restriction |
| 3354 | * without the predicate. Nodes not |
| 3355 | * satisfying the predicate are removed. |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3356 | * @param[out] parsed Number of characters parsed, negative on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3357 | * |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3358 | * @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] | 3359 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3360 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3361 | 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] | 3362 | int *parsed) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3363 | { |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3364 | /* ... /node[source = destination] ... */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3365 | struct unres_data source_match, dest_match; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3366 | const char *path_key_expr, *source, *sour_pref, *dest, *dest_pref; |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3367 | int pke_len, sour_len, sour_pref_len, dest_len, dest_pref_len, parsed_loc = 0, pke_parsed = 0; |
| 3368 | int has_predicate, dest_parent_times, i, rc; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3369 | uint32_t j; |
Radek Krejci | 0c2fa3a | 2016-06-13 15:18:03 +0200 | [diff] [blame] | 3370 | struct lyd_node_leaf_list *leaf_dst, *leaf_src; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3371 | |
| 3372 | source_match.count = 1; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3373 | source_match.node = malloc(sizeof *source_match.node); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3374 | if (!source_match.node) { |
| 3375 | LOGMEM; |
| 3376 | return -1; |
| 3377 | } |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3378 | dest_match.count = 1; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3379 | dest_match.node = malloc(sizeof *dest_match.node); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3380 | if (!dest_match.node) { |
| 3381 | LOGMEM; |
| 3382 | return -1; |
| 3383 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3384 | |
| 3385 | do { |
| 3386 | if ((i = parse_path_predicate(pred, &sour_pref, &sour_pref_len, &source, &sour_len, &path_key_expr, |
| 3387 | &pke_len, &has_predicate)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3388 | LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, pred[-i], &pred[-i]); |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3389 | rc = -1; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3390 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3391 | } |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3392 | parsed_loc += i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3393 | pred += i; |
| 3394 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3395 | for (j = 0; j < node_match->count;) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3396 | /* source */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3397 | source_match.node[0] = node_match->node[j]; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3398 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3399 | /* must be leaf (key of a list) */ |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3400 | 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] | 3401 | &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] | 3402 | i = 0; |
| 3403 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3404 | } |
| 3405 | |
| 3406 | /* destination */ |
Radek Krejci | 0c2fa3a | 2016-06-13 15:18:03 +0200 | [diff] [blame] | 3407 | dest_match.node[0] = node; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3408 | dest_parent_times = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3409 | if ((i = parse_path_key_expr(path_key_expr, &dest_pref, &dest_pref_len, &dest, &dest_len, |
| 3410 | &dest_parent_times)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3411 | 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] | 3412 | rc = -1; |
| 3413 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3414 | } |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3415 | pke_parsed = i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3416 | for (i = 0; i < dest_parent_times; ++i) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3417 | dest_match.node[0] = dest_match.node[0]->parent; |
| 3418 | if (!dest_match.node[0]) { |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3419 | i = 0; |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3420 | rc = EXIT_FAILURE; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3421 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3422 | } |
| 3423 | } |
| 3424 | while (1) { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3425 | 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] | 3426 | &dest_match)) || (dest_match.count != 1)) { |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3427 | i = 0; |
| 3428 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3429 | } |
| 3430 | |
| 3431 | if (pke_len == pke_parsed) { |
| 3432 | break; |
| 3433 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3434 | 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] | 3435 | &dest_parent_times)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3436 | 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] | 3437 | rc = -1; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3438 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3439 | } |
| 3440 | pke_parsed += i; |
| 3441 | } |
| 3442 | |
| 3443 | /* check match between source and destination nodes */ |
Radek Krejci | 0c2fa3a | 2016-06-13 15:18:03 +0200 | [diff] [blame] | 3444 | leaf_dst = (struct lyd_node_leaf_list *)dest_match.node[0]; |
| 3445 | while (leaf_dst->value_type == LY_TYPE_LEAFREF) { |
| 3446 | leaf_dst = (struct lyd_node_leaf_list *)leaf_dst->value.leafref; |
| 3447 | } |
| 3448 | leaf_src = (struct lyd_node_leaf_list *)source_match.node[0]; |
| 3449 | while (leaf_src->value_type == LY_TYPE_LEAFREF) { |
| 3450 | leaf_src = (struct lyd_node_leaf_list *)leaf_src->value.leafref; |
| 3451 | } |
| 3452 | if (leaf_src->value_type != leaf_dst->value_type) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3453 | goto remove_leafref; |
| 3454 | } |
| 3455 | |
Radek Krejci | 0c2fa3a | 2016-06-13 15:18:03 +0200 | [diff] [blame] | 3456 | if (!ly_strequal(leaf_src->value_str, leaf_dst->value_str, 1)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3457 | goto remove_leafref; |
| 3458 | } |
| 3459 | |
| 3460 | /* leafref is ok, continue check with next leafref */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3461 | ++j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3462 | continue; |
| 3463 | |
| 3464 | remove_leafref: |
| 3465 | /* does not fulfill conditions, remove leafref record */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3466 | unres_data_del(node_match, j); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3467 | } |
| 3468 | } while (has_predicate); |
| 3469 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3470 | free(source_match.node); |
| 3471 | free(dest_match.node); |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3472 | if (parsed) { |
| 3473 | *parsed = parsed_loc; |
| 3474 | } |
| 3475 | return EXIT_SUCCESS; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3476 | |
| 3477 | error: |
| 3478 | |
| 3479 | if (source_match.count) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3480 | free(source_match.node); |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3481 | } |
| 3482 | if (dest_match.count) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3483 | free(dest_match.node); |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3484 | } |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3485 | if (parsed) { |
| 3486 | *parsed = -parsed_loc+i; |
| 3487 | } |
| 3488 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3489 | } |
| 3490 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3491 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3492 | * @brief Resolve a path (leafref) in JSON data context. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3493 | * |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3494 | * @param[in] node Leafref data node. |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3495 | * @param[in] path Path of the leafref. |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3496 | * @param[out] ret Matching nodes. Expects an empty, but allocated structure. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3497 | * |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3498 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 otherwise. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3499 | */ |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3500 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3501 | 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] | 3502 | { |
Radek Krejci | 71b795b | 2015-08-10 16:20:39 +0200 | [diff] [blame] | 3503 | struct lyd_node *data = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3504 | const char *prefix, *name; |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3505 | int pref_len, nam_len, has_predicate, parent_times, i, parsed, rc; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3506 | uint32_t j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3507 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3508 | assert(node && path && ret && !ret->count); |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3509 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3510 | parent_times = 0; |
Michal Vasko | d917334 | 2015-08-17 14:35:36 +0200 | [diff] [blame] | 3511 | parsed = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3512 | |
| 3513 | /* searching for nodeset */ |
| 3514 | do { |
| 3515 | if ((i = parse_path_arg(path, &prefix, &pref_len, &name, &nam_len, &parent_times, &has_predicate)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3516 | LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, path[-i], &path[-i]); |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3517 | rc = -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3518 | goto error; |
| 3519 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3520 | path += i; |
Michal Vasko | d917334 | 2015-08-17 14:35:36 +0200 | [diff] [blame] | 3521 | parsed += i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3522 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3523 | if (!ret->count) { |
Michal Vasko | bfd98e6 | 2016-09-02 09:50:05 +0200 | [diff] [blame] | 3524 | if (parent_times > 0) { |
| 3525 | data = node; |
| 3526 | for (i = 1; i < parent_times; ++i) { |
| 3527 | data = data->parent; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3528 | } |
Michal Vasko | bfd98e6 | 2016-09-02 09:50:05 +0200 | [diff] [blame] | 3529 | } else if (!parent_times) { |
| 3530 | data = node->child; |
| 3531 | } else { |
| 3532 | /* absolute path */ |
| 3533 | for (data = node; data->parent; data = data->parent); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3534 | } |
| 3535 | |
Michal Vasko | bfd98e6 | 2016-09-02 09:50:05 +0200 | [diff] [blame] | 3536 | /* we may still be parsing it and the pointer is not correct yet */ |
| 3537 | if (data->prev) { |
| 3538 | while (data->prev->next) { |
| 3539 | data = data->prev; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 3540 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3541 | } |
| 3542 | } |
| 3543 | |
| 3544 | /* node identifier */ |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3545 | 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] | 3546 | if (rc == -1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3547 | LOGVAL(LYE_INELEM_LEN, LY_VLOG_LYD, node, nam_len, name); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3548 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3549 | goto error; |
| 3550 | } |
| 3551 | |
| 3552 | if (has_predicate) { |
| 3553 | /* we have predicate, so the current results must be lists */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3554 | for (j = 0; j < ret->count;) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3555 | if (ret->node[j]->schema->nodetype == LYS_LIST && |
| 3556 | ((struct lys_node_list *)ret->node[0]->schema)->keys) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3557 | /* leafref is ok, continue check with next leafref */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3558 | ++j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3559 | continue; |
| 3560 | } |
| 3561 | |
| 3562 | /* does not fulfill conditions, remove leafref record */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3563 | unres_data_del(ret, j); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3564 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3565 | if ((rc = resolve_path_predicate_data(path, node, ret, &i))) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 3566 | if (rc == -1) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3567 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYD, node, "leafref", path); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3568 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3569 | goto error; |
| 3570 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3571 | path += i; |
Michal Vasko | d917334 | 2015-08-17 14:35:36 +0200 | [diff] [blame] | 3572 | parsed += i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3573 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3574 | if (!ret->count) { |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3575 | rc = EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3576 | goto error; |
| 3577 | } |
| 3578 | } |
| 3579 | } while (path[0] != '\0'); |
| 3580 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3581 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3582 | |
| 3583 | error: |
| 3584 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3585 | free(ret->node); |
| 3586 | ret->node = NULL; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3587 | ret->count = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3588 | |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3589 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3590 | } |
| 3591 | |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3592 | static int |
| 3593 | resolve_path_arg_schema_valid_dep_flag(const struct lys_node *op_node, const struct lys_node *first_node, int abs_path) |
| 3594 | { |
| 3595 | int dep1, dep2; |
| 3596 | const struct lys_node *node; |
| 3597 | |
| 3598 | if (lys_parent(op_node)) { |
| 3599 | /* inner operation (notif/action) */ |
| 3600 | if (abs_path) { |
| 3601 | return 1; |
| 3602 | } else { |
| 3603 | /* compare depth of both nodes */ |
| 3604 | for (dep1 = 0, node = op_node; lys_parent(node); node = lys_parent(node)); |
| 3605 | for (dep2 = 0, node = first_node; lys_parent(node); node = lys_parent(node)); |
| 3606 | if ((dep2 > dep1) || ((dep2 == dep1) && (op_node != first_node))) { |
| 3607 | return 1; |
| 3608 | } |
| 3609 | } |
| 3610 | } else { |
| 3611 | /* top-level operation (notif/rpc) */ |
| 3612 | if (op_node != first_node) { |
| 3613 | return 1; |
| 3614 | } |
| 3615 | } |
| 3616 | |
| 3617 | return 0; |
| 3618 | } |
| 3619 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3620 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3621 | * @brief Resolve a path (leafref) predicate in JSON schema context. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3622 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3623 | * @param[in] path Path to use. |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3624 | * @param[in] context_node Predicate context node (where the predicate is placed). |
| 3625 | * @param[in] parent Path context node (where the path begins/is placed). |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3626 | * @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] | 3627 | * |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3628 | * @return 0 on forward reference, otherwise the number |
| 3629 | * of characters successfully parsed, |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3630 | * positive on success, negative on failure. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3631 | */ |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3632 | static int |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3633 | 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] | 3634 | struct lys_node *parent, const struct lys_node *op_node) |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3635 | { |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3636 | const struct lys_node *src_node, *dst_node; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3637 | const char *path_key_expr, *source, *sour_pref, *dest, *dest_pref; |
Michal Vasko | 6966ea6 | 2016-10-21 15:19:30 +0200 | [diff] [blame^] | 3638 | int pke_len, sour_len, sour_pref_len, dest_len, dest_pref_len, pke_parsed, parsed = 0; |
| 3639 | int has_predicate, dest_parent_times, i, rc, first_iter; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3640 | |
| 3641 | do { |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3642 | 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] | 3643 | &pke_len, &has_predicate)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3644 | 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] | 3645 | return -parsed+i; |
| 3646 | } |
| 3647 | parsed += i; |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3648 | path += i; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3649 | |
Michal Vasko | 5809090 | 2015-08-13 14:04:15 +0200 | [diff] [blame] | 3650 | /* source (must be leaf) */ |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3651 | if (!sour_pref) { |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3652 | sour_pref = context_node->module->name; |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3653 | } |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3654 | 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] | 3655 | LYS_LEAF | LYS_LEAFLIST | LYS_AUGMENT, &src_node); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3656 | if (rc) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3657 | 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] | 3658 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3659 | } |
| 3660 | |
| 3661 | /* destination */ |
Michal Vasko | 6966ea6 | 2016-10-21 15:19:30 +0200 | [diff] [blame^] | 3662 | dest_parent_times = 0; |
| 3663 | pke_parsed = 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3664 | if ((i = parse_path_key_expr(path_key_expr, &dest_pref, &dest_pref_len, &dest, &dest_len, |
| 3665 | &dest_parent_times)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3666 | 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] | 3667 | return -parsed; |
| 3668 | } |
| 3669 | pke_parsed += i; |
| 3670 | |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3671 | for (i = 0, dst_node = parent; i < dest_parent_times; ++i) { |
Michal Vasko | fbaead7 | 2016-10-07 10:54:48 +0200 | [diff] [blame] | 3672 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 3673 | * all schema nodes that cannot be instantiated in data tree */ |
| 3674 | for (dst_node = lys_parent(dst_node); |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3675 | 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] | 3676 | dst_node = lys_parent(dst_node)); |
| 3677 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3678 | if (!dst_node) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3679 | 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] | 3680 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3681 | } |
| 3682 | } |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3683 | first_iter = 1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3684 | while (1) { |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3685 | if (!dest_pref) { |
| 3686 | dest_pref = dst_node->module->name; |
| 3687 | } |
| 3688 | 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] | 3689 | LYS_CONTAINER | LYS_LIST | LYS_LEAF | LYS_AUGMENT, &dst_node); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3690 | if (rc) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3691 | 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] | 3692 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3693 | } |
| 3694 | |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3695 | if (first_iter) { |
| 3696 | if (resolve_path_arg_schema_valid_dep_flag(op_node, dst_node, 0)) { |
| 3697 | parent->flags |= LYS_VALID_DEP; |
| 3698 | } |
| 3699 | first_iter = 0; |
| 3700 | } |
| 3701 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3702 | if (pke_len == pke_parsed) { |
| 3703 | break; |
| 3704 | } |
| 3705 | |
| 3706 | if ((i = parse_path_key_expr(path_key_expr+pke_parsed, &dest_pref, &dest_pref_len, &dest, &dest_len, |
| 3707 | &dest_parent_times)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3708 | LOGVAL(LYE_INCHAR, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3709 | (path_key_expr+pke_parsed)[-i], (path_key_expr+pke_parsed)-i); |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3710 | return -parsed; |
| 3711 | } |
| 3712 | pke_parsed += i; |
| 3713 | } |
| 3714 | |
| 3715 | /* check source - dest match */ |
Michal Vasko | 59ad458 | 2016-09-16 13:15:41 +0200 | [diff] [blame] | 3716 | if (dst_node->nodetype != src_node->nodetype) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3717 | 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] | 3718 | LOGVAL(LYE_SPEC, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, "Destination node is not a %s, but a %s.", |
| 3719 | strnodetype(src_node->nodetype), strnodetype(dst_node->nodetype)); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3720 | return -parsed; |
| 3721 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3722 | } while (has_predicate); |
| 3723 | |
| 3724 | return parsed; |
| 3725 | } |
| 3726 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3727 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3728 | * @brief Resolve a path (leafref) in JSON schema context. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3729 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3730 | * @param[in] path Path to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3731 | * @param[in] parent_node Parent of the leafref. |
Radek Krejci | 2f12f85 | 2016-01-08 12:59:57 +0100 | [diff] [blame] | 3732 | * @param[in] parent_tpdf Flag if the parent node is actually typedef, in that case the path |
| 3733 | * has to contain absolute path |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3734 | * @param[out] ret Pointer to the resolved schema node. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3735 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3736 | * @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] | 3737 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3738 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3739 | 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] | 3740 | const struct lys_node **ret) |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3741 | { |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3742 | const struct lys_node *node, *op_node = NULL; |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 3743 | const struct lys_module *mod, *mod2; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3744 | const char *id, *prefix, *name; |
| 3745 | int pref_len, nam_len, parent_times, has_predicate; |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3746 | int i, first_iter, rc; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3747 | |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3748 | first_iter = 1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3749 | parent_times = 0; |
| 3750 | id = path; |
| 3751 | |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3752 | /* find operation schema we are in, if applicable */ |
Michal Vasko | e9914d1 | 2016-10-07 14:32:37 +0200 | [diff] [blame] | 3753 | if (!parent_tpdf) { |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3754 | for (op_node = lys_parent(parent); |
| 3755 | op_node && !(op_node->nodetype & (LYS_ACTION | LYS_NOTIF | LYS_RPC)); |
| 3756 | op_node = lys_parent(op_node)); |
Michal Vasko | e9914d1 | 2016-10-07 14:32:37 +0200 | [diff] [blame] | 3757 | } |
| 3758 | |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 3759 | mod2 = lys_node_module(parent); |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3760 | do { |
| 3761 | if ((i = parse_path_arg(id, &prefix, &pref_len, &name, &nam_len, &parent_times, &has_predicate)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3762 | 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] | 3763 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3764 | } |
| 3765 | id += i; |
| 3766 | |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3767 | if (first_iter) { |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3768 | if (parent_times == -1) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 3769 | /* resolve prefix of the module */ |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3770 | mod = lys_get_import_module(parent->module, NULL, 0, prefix, pref_len); |
Radek Krejci | 0fa54e9 | 2016-09-14 14:01:05 +0200 | [diff] [blame] | 3771 | mod = lys_get_implemented_module(mod); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 3772 | /* get start node */ |
| 3773 | node = mod ? mod->data : NULL; |
Michal Vasko | 5809090 | 2015-08-13 14:04:15 +0200 | [diff] [blame] | 3774 | if (!node) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3775 | LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, |
| 3776 | "leafref", path); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3777 | return EXIT_FAILURE; |
Michal Vasko | 5809090 | 2015-08-13 14:04:15 +0200 | [diff] [blame] | 3778 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3779 | } else if (parent_times > 0) { |
Radek Krejci | 2f12f85 | 2016-01-08 12:59:57 +0100 | [diff] [blame] | 3780 | if (parent_tpdf) { |
| 3781 | /* 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] | 3782 | LOGVAL(LYE_NORESOLV, 0, NULL, "leafref", path); |
Radek Krejci | 2f12f85 | 2016-01-08 12:59:57 +0100 | [diff] [blame] | 3783 | return -1; |
| 3784 | } |
| 3785 | |
Michal Vasko | 9445808 | 2016-10-07 14:34:36 +0200 | [diff] [blame] | 3786 | /* we are looking for a sibling of a node, node it's parent (that is why parent_times - 1) */ |
| 3787 | for (i = 0, node = parent; i < parent_times - 1; i++) { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 3788 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 3789 | * all schema nodes that cannot be instantiated in data tree */ |
| 3790 | for (node = lys_parent(node); |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3791 | 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] | 3792 | node = lys_parent(node)); |
| 3793 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3794 | if (!node) { |
Michal Vasko | e9914d1 | 2016-10-07 14:32:37 +0200 | [diff] [blame] | 3795 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3796 | return EXIT_FAILURE; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3797 | } |
| 3798 | } |
Michal Vasko | e01eca5 | 2015-08-13 14:42:02 +0200 | [diff] [blame] | 3799 | } else { |
| 3800 | LOGINT; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3801 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3802 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3803 | } else { |
Michal Vasko | 7dc71d0 | 2016-03-15 10:42:28 +0100 | [diff] [blame] | 3804 | /* move down the tree, if possible */ |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 3805 | if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
Michal Vasko | 2f5aceb | 2016-03-22 10:24:14 +0100 | [diff] [blame] | 3806 | 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] | 3807 | return -1; |
| 3808 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3809 | node = node->child; |
Radek Krejci | 6892c27 | 2016-10-18 20:40:06 +0200 | [diff] [blame] | 3810 | if (!node) { |
| 3811 | LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, |
| 3812 | "leafref", path); |
| 3813 | return EXIT_FAILURE; |
| 3814 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3815 | } |
| 3816 | |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 3817 | if (!prefix) { |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3818 | prefix = lys_node_module(parent)->name; |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 3819 | } |
| 3820 | |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3821 | 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] | 3822 | if (rc) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3823 | 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] | 3824 | return EXIT_FAILURE; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3825 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3826 | |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3827 | if (first_iter) { |
| 3828 | /* set external dependency flag, we can decide based on the first found node */ |
| 3829 | if (!parent_tpdf && op_node && parent_times && |
| 3830 | resolve_path_arg_schema_valid_dep_flag(op_node, node, (parent_times == -1 ? 1 : 0))) { |
| 3831 | parent->flags |= LYS_VALID_DEP; |
| 3832 | } |
| 3833 | first_iter = 0; |
| 3834 | } |
| 3835 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3836 | if (has_predicate) { |
| 3837 | /* we have predicate, so the current result must be list */ |
| 3838 | if (node->nodetype != LYS_LIST) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3839 | 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] | 3840 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3841 | } |
| 3842 | |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3843 | i = resolve_path_predicate_schema(id, node, parent, op_node); |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 3844 | if (i <= 0) { |
| 3845 | if (i == 0) { |
| 3846 | return EXIT_FAILURE; |
| 3847 | } else { /* i < 0 */ |
| 3848 | return -1; |
| 3849 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3850 | } |
| 3851 | id += i; |
Michal Vasko | 6966ea6 | 2016-10-21 15:19:30 +0200 | [diff] [blame^] | 3852 | has_predicate = 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3853 | } |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 3854 | mod = lys_node_module(node); |
| 3855 | if (!mod->implemented && mod != mod2) { |
| 3856 | /* set the module implemented */ |
| 3857 | if (lys_set_implemented(mod)) { |
| 3858 | return -1; |
| 3859 | } |
| 3860 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3861 | } while (id[0]); |
| 3862 | |
Michal Vasko | ca91768 | 2016-07-25 11:00:37 +0200 | [diff] [blame] | 3863 | /* the target must be leaf or leaf-list (in YANG 1.1 only) */ |
| 3864 | 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] | 3865 | 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] | 3866 | LOGVAL(LYE_SPEC, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, |
| 3867 | "Leafref target \"%s\" is not a leaf%s.", path, |
| 3868 | lys_node_module(parent)->version != 2 ? "" : " nor a leaf-list"); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3869 | return -1; |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 3870 | } |
| 3871 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3872 | /* check status */ |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3873 | if (lyp_check_status(parent->flags, parent->module, parent->name, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3874 | node->flags, node->module, node->name, node)) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3875 | return -1; |
| 3876 | } |
| 3877 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3878 | if (ret) { |
| 3879 | *ret = node; |
| 3880 | } |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 3881 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3882 | return EXIT_SUCCESS; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3883 | } |
| 3884 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3885 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3886 | * @brief Resolve instance-identifier predicate in JSON data format. |
| 3887 | * Does not log. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3888 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3889 | * @param[in] pred Predicate to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3890 | * @param[in,out] node_match Nodes matching the restriction without |
| 3891 | * the predicate. Nodes not satisfying |
| 3892 | * the predicate are removed. |
| 3893 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3894 | * @return Number of characters successfully parsed, |
| 3895 | * positive on success, negative on failure. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3896 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3897 | static int |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3898 | resolve_predicate(const char *pred, struct unres_data *node_match) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3899 | { |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3900 | /* ... /node[target = value] ... */ |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3901 | struct lyd_node *target; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3902 | const char *model, *name, *value; |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3903 | 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] | 3904 | uint32_t j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3905 | |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3906 | assert(pred && node_match->count); |
| 3907 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3908 | idx = -1; |
| 3909 | parsed = 0; |
| 3910 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3911 | pred_iter = -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3912 | do { |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3913 | 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] | 3914 | return -parsed+i; |
| 3915 | } |
| 3916 | parsed += i; |
| 3917 | pred += i; |
| 3918 | |
| 3919 | if (isdigit(name[0])) { |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3920 | /* pos */ |
| 3921 | assert(!value); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3922 | idx = atoi(name); |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3923 | } else if (name[0] != '.') { |
| 3924 | /* list keys */ |
| 3925 | if (pred_iter < 0) { |
| 3926 | pred_iter = 1; |
| 3927 | } else { |
| 3928 | ++pred_iter; |
| 3929 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3930 | } |
| 3931 | |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 3932 | for (cur_idx = 1, j = 0; j < node_match->count; ++cur_idx) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3933 | /* target */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3934 | if (name[0] == '.') { |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3935 | /* leaf-list value */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3936 | if (node_match->node[j]->schema->nodetype != LYS_LEAFLIST) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3937 | goto remove_instid; |
| 3938 | } |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3939 | |
| 3940 | target = node_match->node[j]; |
| 3941 | /* check the value */ |
| 3942 | if (strncmp(((struct lyd_node_leaf_list *)target)->value_str, value, val_len) |
| 3943 | || ((struct lyd_node_leaf_list *)target)->value_str[val_len]) { |
| 3944 | goto remove_instid; |
| 3945 | } |
| 3946 | |
| 3947 | } else if (!value) { |
| 3948 | /* keyless list position */ |
| 3949 | if ((node_match->node[j]->schema->nodetype != LYS_LIST) |
| 3950 | || ((struct lys_node_list *)node_match->node[j]->schema)->keys) { |
| 3951 | goto remove_instid; |
| 3952 | } |
| 3953 | |
| 3954 | if (idx != cur_idx) { |
| 3955 | goto remove_instid; |
| 3956 | } |
| 3957 | |
| 3958 | } else { |
| 3959 | /* list key value */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3960 | if (node_match->node[j]->schema->nodetype != LYS_LIST) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3961 | goto remove_instid; |
| 3962 | } |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3963 | |
| 3964 | /* key module must match the list module */ |
| 3965 | if (strncmp(node_match->node[j]->schema->module->name, model, mod_len) |
| 3966 | || node_match->node[j]->schema->module->name[mod_len]) { |
| 3967 | goto remove_instid; |
| 3968 | } |
| 3969 | /* find the key leaf */ |
Michal Vasko | 045182c | 2016-09-09 12:44:07 +0200 | [diff] [blame] | 3970 | 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] | 3971 | if (!target) { |
| 3972 | goto remove_instid; |
| 3973 | } |
| 3974 | if ((struct lys_node_leaf *)target->schema != |
| 3975 | ((struct lys_node_list *)node_match->node[j]->schema)->keys[pred_iter - 1]) { |
| 3976 | goto remove_instid; |
| 3977 | } |
| 3978 | |
| 3979 | /* check the value */ |
| 3980 | if (strncmp(((struct lyd_node_leaf_list *)target)->value_str, value, val_len) |
| 3981 | || ((struct lyd_node_leaf_list *)target)->value_str[val_len]) { |
| 3982 | goto remove_instid; |
| 3983 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3984 | } |
| 3985 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3986 | /* instid is ok, continue check with the next one */ |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3987 | ++j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3988 | continue; |
| 3989 | |
| 3990 | remove_instid: |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3991 | /* does not fulfill conditions, remove instid record */ |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3992 | unres_data_del(node_match, j); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3993 | } |
| 3994 | } while (has_predicate); |
| 3995 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3996 | /* check that all list keys were specified */ |
| 3997 | if ((pred_iter > 0) && node_match->count) { |
Michal Vasko | 045182c | 2016-09-09 12:44:07 +0200 | [diff] [blame] | 3998 | j = 0; |
| 3999 | while (j < node_match->count) { |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4000 | assert(node_match->node[j]->schema->nodetype == LYS_LIST); |
| 4001 | if (pred_iter < ((struct lys_node_list *)node_match->node[j]->schema)->keys_size) { |
| 4002 | /* not enough predicates, just remove the list instance */ |
| 4003 | unres_data_del(node_match, j); |
Michal Vasko | 045182c | 2016-09-09 12:44:07 +0200 | [diff] [blame] | 4004 | } else { |
| 4005 | ++j; |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4006 | } |
| 4007 | } |
| 4008 | |
| 4009 | if (!node_match->count) { |
| 4010 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier is missing some list keys."); |
| 4011 | } |
| 4012 | } |
| 4013 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4014 | return parsed; |
| 4015 | } |
| 4016 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4017 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 4018 | * @brief Resolve instance-identifier in JSON data format. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4019 | * |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 4020 | * @param[in] data Data node where the path is used |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4021 | * @param[in] path Instance-identifier node value. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4022 | * |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4023 | * @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] | 4024 | */ |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 4025 | static struct lyd_node * |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4026 | resolve_instid(struct lyd_node *data, const char *path) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4027 | { |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4028 | int i = 0, j; |
| 4029 | struct lyd_node *result = NULL; |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4030 | const struct lys_module *mod; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4031 | struct ly_ctx *ctx = data->schema->module->ctx; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4032 | const char *model, *name; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4033 | char *str; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4034 | int mod_len, name_len, has_predicate; |
| 4035 | struct unres_data node_match; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4036 | |
| 4037 | memset(&node_match, 0, sizeof node_match); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4038 | |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4039 | /* we need root to resolve absolute path */ |
| 4040 | for (; data->parent; data = data->parent); |
Michal Vasko | db9323c | 2015-10-16 10:32:44 +0200 | [diff] [blame] | 4041 | /* we're still parsing it and the pointer is not correct yet */ |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 4042 | if (data->prev) { |
| 4043 | for (; data->prev->next; data = data->prev); |
| 4044 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4045 | |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4046 | /* search for the instance node */ |
| 4047 | while (path[i]) { |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 4048 | 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] | 4049 | if (j <= 0) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4050 | 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] | 4051 | goto error; |
| 4052 | } |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4053 | i += j; |
Michal Vasko | b387c48 | 2015-08-12 09:32:59 +0200 | [diff] [blame] | 4054 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4055 | str = strndup(model, mod_len); |
| 4056 | if (!str) { |
| 4057 | LOGMEM; |
| 4058 | goto error; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4059 | } |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4060 | mod = ly_ctx_get_module(ctx, str, NULL); |
| 4061 | free(str); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4062 | |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4063 | if (resolve_data(mod, name, name_len, data, &node_match)) { |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4064 | /* no instance exists */ |
| 4065 | return NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4066 | } |
| 4067 | |
| 4068 | if (has_predicate) { |
| 4069 | /* 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] | 4070 | j = resolve_predicate(&path[i], &node_match); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4071 | if (j < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4072 | LOGVAL(LYE_INPRED, LY_VLOG_LYD, data, &path[i-j]); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4073 | goto error; |
| 4074 | } |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4075 | i += j; |
Michal Vasko | b387c48 | 2015-08-12 09:32:59 +0200 | [diff] [blame] | 4076 | |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4077 | if (!node_match.count) { |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4078 | /* no instance exists */ |
| 4079 | return NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4080 | } |
| 4081 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4082 | } |
| 4083 | |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4084 | if (!node_match.count) { |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4085 | /* no instance exists */ |
| 4086 | return NULL; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 4087 | } else if (node_match.count > 1) { |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4088 | /* instance identifier must resolve to a single node */ |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4089 | LOGVAL(LYE_TOOMANY, LY_VLOG_LYD, data, path, "data tree"); |
Michal Vasko | d6adbaa | 2016-04-11 11:01:09 +0200 | [diff] [blame] | 4090 | goto error; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4091 | } else { |
| 4092 | /* we have required result, remember it and cleanup */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4093 | result = node_match.node[0]; |
| 4094 | free(node_match.node); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4095 | return result; |
| 4096 | } |
| 4097 | |
| 4098 | error: |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4099 | /* cleanup */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4100 | free(node_match.node); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4101 | return NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4102 | } |
| 4103 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4104 | /** |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4105 | * @brief Check all XPath expressions of a node (when and must), set LYS_XPATH_DEP flag if required. |
| 4106 | * |
| 4107 | * @param[in] node Node to examine. |
| 4108 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 4109 | */ |
| 4110 | static int |
| 4111 | check_node_xpath(struct lys_node *node) |
| 4112 | { |
| 4113 | struct lys_node *parent, *elem; |
| 4114 | struct lyxp_set set; |
| 4115 | uint32_t i; |
| 4116 | int rc; |
| 4117 | |
| 4118 | parent = node; |
| 4119 | while (parent) { |
| 4120 | if (parent->nodetype == LYS_GROUPING) { |
| 4121 | /* unresolved grouping, skip for now (will be checked later) */ |
| 4122 | return EXIT_SUCCESS; |
| 4123 | } |
| 4124 | if (parent->nodetype == LYS_AUGMENT) { |
| 4125 | if (!((struct lys_node_augment *)parent)->target) { |
| 4126 | /* uresolved augment, skip for now (will be checked later) */ |
| 4127 | return EXIT_SUCCESS; |
| 4128 | } else { |
| 4129 | parent = ((struct lys_node_augment *)parent)->target; |
| 4130 | continue; |
| 4131 | } |
| 4132 | } |
| 4133 | parent = parent->parent; |
| 4134 | } |
| 4135 | |
| 4136 | rc = lyxp_node_atomize(node, &set); |
| 4137 | if (rc) { |
| 4138 | return rc; |
| 4139 | } |
| 4140 | |
| 4141 | for (parent = node; parent && !(parent->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); parent = lys_parent(parent)); |
| 4142 | |
| 4143 | for (i = 0; i < set.used; ++i) { |
| 4144 | /* skip roots'n'stuff */ |
| 4145 | if (set.val.snodes[i].type == LYXP_NODE_ELEM) { |
| 4146 | /* XPath expression cannot reference "lower" status than the node that has the definition */ |
| 4147 | if (lyp_check_status(node->flags, lys_node_module(node), node->name, set.val.snodes[i].snode->flags, |
| 4148 | lys_node_module(set.val.snodes[i].snode), set.val.snodes[i].snode->name, node)) { |
| 4149 | return -1; |
| 4150 | } |
| 4151 | |
| 4152 | if (parent) { |
| 4153 | for (elem = set.val.snodes[i].snode; elem && (elem != parent); elem = lys_parent(elem)); |
| 4154 | if (!elem) { |
| 4155 | /* not in node's RPC or notification subtree, set the flag */ |
| 4156 | node->flags |= LYS_VALID_DEP; |
| 4157 | break; |
| 4158 | } |
| 4159 | } |
| 4160 | } |
| 4161 | } |
| 4162 | |
| 4163 | free(set.val.snodes); |
| 4164 | return EXIT_SUCCESS; |
| 4165 | } |
| 4166 | |
| 4167 | /** |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4168 | * @brief Passes config flag down to children, skips nodes without config flags. |
| 4169 | * Does not log. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4170 | * |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4171 | * @param[in] node Siblings and their children to have flags changed. |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4172 | * @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] | 4173 | * @param[in] flags Flags to assign to all the nodes. |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4174 | * |
| 4175 | * @return 0 on success, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4176 | */ |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4177 | static int |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4178 | 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] | 4179 | { |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4180 | assert(!(flags ^ (flags & LYS_CONFIG_MASK))); |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4181 | LY_TREE_FOR(node, node) { |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4182 | if (check_xpath && check_node_xpath(node)) { |
| 4183 | return -1; |
| 4184 | } |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4185 | if (clear) { |
| 4186 | node->flags &= ~LYS_CONFIG_MASK; |
Michal Vasko | c2a8d36 | 2016-09-29 08:50:13 +0200 | [diff] [blame] | 4187 | node->flags &= ~LYS_CONFIG_SET; |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4188 | } else { |
| 4189 | if (node->flags & LYS_CONFIG_SET) { |
| 4190 | /* skip nodes with an explicit config value */ |
| 4191 | if ((flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) { |
| 4192 | LOGVAL(LYE_INARG, LY_VLOG_LYS, node, "true", "config"); |
| 4193 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, node, "State nodes cannot have configuration nodes as children."); |
| 4194 | return -1; |
| 4195 | } |
| 4196 | continue; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4197 | } |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4198 | |
| 4199 | if (!(node->nodetype & (LYS_USES | LYS_GROUPING))) { |
| 4200 | node->flags = (node->flags & ~LYS_CONFIG_MASK) | flags; |
| 4201 | /* check that configuration lists have keys */ |
| 4202 | if (check_list && (node->nodetype == LYS_LIST) |
| 4203 | && (node->flags & LYS_CONFIG_W) && !((struct lys_node_list *)node)->keys_size) { |
| 4204 | LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_LYS, node, "key", "list"); |
| 4205 | return -1; |
| 4206 | } |
| 4207 | } |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4208 | } |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4209 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) { |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4210 | if (inherit_config_flag(node->child, flags, clear, check_list, check_xpath)) { |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4211 | return -1; |
| 4212 | } |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 4213 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4214 | } |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4215 | |
| 4216 | return 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4217 | } |
| 4218 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4219 | /** |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 4220 | * @brief Resolve augment target. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4221 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4222 | * @param[in] aug Augment to use. |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 4223 | * @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] | 4224 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4225 | * @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] | 4226 | */ |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 4227 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4228 | resolve_augment(struct lys_node_augment *aug, struct lys_node *siblings) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4229 | { |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4230 | int rc, clear_config; |
Michal Vasko | 1d87a92 | 2015-08-21 12:57:16 +0200 | [diff] [blame] | 4231 | struct lys_node *sub; |
Pavol Vican | 4731993 | 2016-08-29 09:14:47 +0200 | [diff] [blame] | 4232 | const struct lys_node *aug_target, *parent; |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4233 | struct lys_module *mod; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4234 | |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4235 | assert(aug && !aug->target); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4236 | |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4237 | /* resolve target node */ |
| 4238 | rc = resolve_augment_schema_nodeid(aug->target_name, siblings, (siblings ? NULL : aug->module), &aug_target); |
| 4239 | if (rc == -1) { |
| 4240 | return -1; |
| 4241 | } |
| 4242 | if (rc > 0) { |
| 4243 | LOGVAL(LYE_INCHAR, LY_VLOG_LYS, aug, aug->target_name[rc - 1], &aug->target_name[rc - 1]); |
| 4244 | return -1; |
| 4245 | } |
| 4246 | if (!aug_target) { |
| 4247 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, aug, "augment", aug->target_name); |
| 4248 | return EXIT_FAILURE; |
| 4249 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4250 | |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4251 | /* check that we want to connect augment into its target */ |
| 4252 | mod = lys_main_module(aug->module); |
| 4253 | if (!mod->implemented) { |
| 4254 | /* it must be augment only to the same module, |
| 4255 | * otherwise we do not apply augment in not-implemented |
| 4256 | * module. If the module is set to be implemented in future, |
| 4257 | * the augment is being resolved and checked again */ |
| 4258 | for (sub = aug->target; sub; sub = lys_parent(sub)) { |
| 4259 | if (lys_node_module(sub) != mod) { |
| 4260 | /* this is not an implemented module and the augment |
| 4261 | * target some other module, so avoid its connecting |
| 4262 | * to the target */ |
| 4263 | return EXIT_SUCCESS; |
| 4264 | } |
| 4265 | } |
| 4266 | } |
| 4267 | |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4268 | if (!aug->child) { |
| 4269 | /* nothing to do */ |
| 4270 | LOGWRN("Augment \"%s\" without children.", aug->target_name); |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4271 | goto success; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4272 | } |
| 4273 | |
Michal Vasko | d58d596 | 2016-03-02 14:29:41 +0100 | [diff] [blame] | 4274 | /* check for mandatory nodes - if the target node is in another module |
| 4275 | * the added nodes cannot be mandatory |
| 4276 | */ |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4277 | 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] | 4278 | && (rc = lyp_check_mandatory_augment(aug))) { |
| 4279 | return rc; |
Michal Vasko | d58d596 | 2016-03-02 14:29:41 +0100 | [diff] [blame] | 4280 | } |
| 4281 | |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4282 | /* check augment target type and then augment nodes type */ |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4283 | 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] | 4284 | LY_TREE_FOR(aug->child, sub) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4285 | 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] | 4286 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment"); |
| 4287 | 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] | 4288 | strnodetype(aug_target->nodetype), strnodetype(sub->nodetype)); |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4289 | return -1; |
| 4290 | } |
| 4291 | } |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4292 | } else if (aug_target->nodetype == LYS_CHOICE) { |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4293 | LY_TREE_FOR(aug->child, sub) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4294 | 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] | 4295 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment"); |
| 4296 | 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] | 4297 | strnodetype(aug_target->nodetype), strnodetype(sub->nodetype)); |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4298 | return -1; |
| 4299 | } |
| 4300 | } |
| 4301 | } else { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4302 | LOGVAL(LYE_INARG, LY_VLOG_LYS, aug, aug->target_name, "target-node"); |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4303 | 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] | 4304 | return -1; |
| 4305 | } |
| 4306 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 4307 | /* check identifier uniqueness as in lys_node_addchild() */ |
Michal Vasko | 1d87a92 | 2015-08-21 12:57:16 +0200 | [diff] [blame] | 4308 | LY_TREE_FOR(aug->child, sub) { |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4309 | if (lys_check_id(sub, (struct lys_node *)aug_target, NULL)) { |
Michal Vasko | 3e6665f | 2015-08-17 14:00:38 +0200 | [diff] [blame] | 4310 | return -1; |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 4311 | } |
| 4312 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4313 | |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4314 | /* finally reconnect augmenting data into the target - add them to the target child list, |
| 4315 | * by setting aug->target we know the augment is fully resolved now */ |
| 4316 | aug->target = (struct lys_node *)aug_target; |
| 4317 | if (aug->target->child) { |
| 4318 | sub = aug->target->child->prev; /* remember current target's last node */ |
| 4319 | sub->next = aug->child; /* connect augmenting data after target's last node */ |
| 4320 | aug->target->child->prev = aug->child->prev; /* new target's last node is last augmenting node */ |
| 4321 | aug->child->prev = sub; /* finish connecting of both child lists */ |
| 4322 | } else { |
| 4323 | aug->target->child = aug->child; |
| 4324 | } |
| 4325 | |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4326 | /* inherit config information from actual parent */ |
| 4327 | for(parent = aug_target; parent && !(parent->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT | LYS_RPC)); parent = lys_parent(parent)); |
| 4328 | clear_config = (parent) ? 1 : 0; |
| 4329 | LY_TREE_FOR(aug->child, sub) { |
| 4330 | if (inherit_config_flag(sub, aug_target->flags & LYS_CONFIG_MASK, clear_config, 1, 1)) { |
| 4331 | return -1; |
| 4332 | } |
| 4333 | } |
| 4334 | |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4335 | success: |
| 4336 | if (mod->implemented) { |
| 4337 | /* make target modules also implemented */ |
| 4338 | for (sub = aug->target; sub; sub = lys_parent(sub)) { |
| 4339 | if (lys_set_implemented(sub->module)) { |
| 4340 | return -1; |
| 4341 | } |
| 4342 | } |
| 4343 | } |
| 4344 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4345 | return EXIT_SUCCESS; |
| 4346 | } |
| 4347 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4348 | /** |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4349 | * @brief Resolve (find) choice default case. Does not log. |
| 4350 | * |
| 4351 | * @param[in] choic Choice to use. |
| 4352 | * @param[in] dflt Name of the default case. |
| 4353 | * |
| 4354 | * @return Pointer to the default node or NULL. |
| 4355 | */ |
| 4356 | static struct lys_node * |
| 4357 | resolve_choice_dflt(struct lys_node_choice *choic, const char *dflt) |
| 4358 | { |
| 4359 | struct lys_node *child, *ret; |
| 4360 | |
| 4361 | LY_TREE_FOR(choic->child, child) { |
| 4362 | if (child->nodetype == LYS_USES) { |
| 4363 | ret = resolve_choice_dflt((struct lys_node_choice *)child, dflt); |
| 4364 | if (ret) { |
| 4365 | return ret; |
| 4366 | } |
| 4367 | } |
| 4368 | |
| 4369 | 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] | 4370 | | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CHOICE))) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4371 | return child; |
| 4372 | } |
| 4373 | } |
| 4374 | |
| 4375 | return NULL; |
| 4376 | } |
| 4377 | |
| 4378 | /** |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4379 | * @brief Resolve uses, apply augments, refines. Logs directly. |
| 4380 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4381 | * @param[in] uses Uses to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4382 | * @param[in,out] unres List of unresolved items. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4383 | * |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 4384 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4385 | */ |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 4386 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4387 | resolve_uses(struct lys_node_uses *uses, struct unres_schema *unres) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4388 | { |
| 4389 | struct ly_ctx *ctx; |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4390 | struct lys_node *node = NULL, *next, *iter, **refine_nodes = NULL; |
Pavol Vican | 55abd33 | 2016-07-12 15:54:49 +0200 | [diff] [blame] | 4391 | struct lys_node *node_aux, *parent, *tmp; |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4392 | struct lys_node_leaflist *llist; |
| 4393 | struct lys_node_leaf *leaf; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 4394 | struct lys_refine *rfn; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4395 | struct lys_restr *must, **old_must; |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4396 | struct lys_iffeature *iff, **old_iff; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4397 | 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] | 4398 | uint8_t size, *old_size; |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4399 | unsigned int usize, usize1, usize2; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4400 | |
Michal Vasko | 71e1aa8 | 2015-08-12 12:17:51 +0200 | [diff] [blame] | 4401 | assert(uses->grp); |
Michal Vasko | e770851 | 2016-03-11 10:26:55 +0100 | [diff] [blame] | 4402 | /* HACK just check that the grouping is resolved */ |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 4403 | assert(!uses->grp->nacm); |
Michal Vasko | 71e1aa8 | 2015-08-12 12:17:51 +0200 | [diff] [blame] | 4404 | |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4405 | if (!uses->grp->child) { |
| 4406 | /* grouping without children, warning was already displayed */ |
| 4407 | return EXIT_SUCCESS; |
| 4408 | } |
| 4409 | |
| 4410 | /* get proper parent (config) flags */ |
| 4411 | for (node_aux = lys_parent((struct lys_node *)uses); node_aux && (node_aux->nodetype == LYS_USES); node_aux = lys_parent(node_aux)); |
| 4412 | if (node_aux) { |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4413 | parent_config = node_aux->flags & LYS_CONFIG_MASK; |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4414 | } else { |
| 4415 | /* default */ |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4416 | parent_config = LYS_CONFIG_W; |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4417 | } |
| 4418 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4419 | /* copy the data nodes from grouping into the uses context */ |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 4420 | LY_TREE_FOR(uses->grp->child, node_aux) { |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4421 | 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] | 4422 | if (!node) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4423 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, uses->grp->name, "uses"); |
| 4424 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, "Copying data from grouping failed."); |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4425 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4426 | } |
Pavol Vican | 55abd33 | 2016-07-12 15:54:49 +0200 | [diff] [blame] | 4427 | /* test the name of siblings */ |
| 4428 | 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] | 4429 | 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] | 4430 | goto fail; |
Pavol Vican | 55abd33 | 2016-07-12 15:54:49 +0200 | [diff] [blame] | 4431 | } |
| 4432 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4433 | } |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4434 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4435 | ctx = uses->module->ctx; |
Michal Vasko | 4bc590c | 2016-09-30 12:19:51 +0200 | [diff] [blame] | 4436 | |
| 4437 | parent = node; |
| 4438 | while (parent && !(parent->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT | LYS_RPC | LYS_GROUPING))) { |
| 4439 | if (parent->nodetype == LYS_AUGMENT) { |
| 4440 | if (!((struct lys_node_augment *)parent)->target) { |
| 4441 | break; |
| 4442 | } else { |
| 4443 | parent = ((struct lys_node_augment *)parent)->target; |
| 4444 | } |
| 4445 | } else { |
| 4446 | parent = parent->parent; |
| 4447 | } |
| 4448 | } |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4449 | if (parent) { |
Michal Vasko | 4bc590c | 2016-09-30 12:19:51 +0200 | [diff] [blame] | 4450 | if (parent->nodetype & (LYS_GROUPING | LYS_AUGMENT)) { |
| 4451 | /* 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] | 4452 | check_list = 0; |
| 4453 | clear_config = 0; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4454 | check_xpath = 0; |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4455 | } else { |
| 4456 | check_list = 0; |
| 4457 | clear_config = 1; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4458 | check_xpath = 1; |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4459 | } |
| 4460 | } else { |
| 4461 | check_list = 1; |
| 4462 | clear_config = 0; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4463 | check_xpath = 1; |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4464 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4465 | |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4466 | if (parent_config) { |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4467 | assert(uses->child); |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4468 | 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] | 4469 | goto fail; |
| 4470 | } |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4471 | } |
| 4472 | |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 4473 | /* we managed to copy the grouping, the rest must be possible to resolve */ |
| 4474 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4475 | if (uses->refine_size) { |
| 4476 | refine_nodes = malloc(uses->refine_size * sizeof *refine_nodes); |
| 4477 | if (!refine_nodes) { |
| 4478 | LOGMEM; |
| 4479 | goto fail; |
| 4480 | } |
| 4481 | } |
| 4482 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4483 | /* apply refines */ |
| 4484 | for (i = 0; i < uses->refine_size; i++) { |
| 4485 | rfn = &uses->refine[i]; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 4486 | 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] | 4487 | 1, 0, (const struct lys_node **)&node); |
Michal Vasko | 9bb061b | 2016-02-12 11:00:19 +0100 | [diff] [blame] | 4488 | if (rc || !node) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4489 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine"); |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4490 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4491 | } |
| 4492 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4493 | if (rfn->target_type && !(node->nodetype & rfn->target_type)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4494 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine"); |
| 4495 | 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] | 4496 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4497 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4498 | refine_nodes[i] = node; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4499 | |
| 4500 | /* description on any nodetype */ |
| 4501 | if (rfn->dsc) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4502 | lydict_remove(ctx, node->dsc); |
| 4503 | node->dsc = lydict_insert(ctx, rfn->dsc, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4504 | } |
| 4505 | |
| 4506 | /* reference on any nodetype */ |
| 4507 | if (rfn->ref) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4508 | lydict_remove(ctx, node->ref); |
| 4509 | node->ref = lydict_insert(ctx, rfn->ref, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4510 | } |
| 4511 | |
| 4512 | /* config on any nodetype */ |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4513 | if ((rfn->flags & LYS_CONFIG_MASK) && !clear_config) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4514 | node->flags &= ~LYS_CONFIG_MASK; |
| 4515 | node->flags |= (rfn->flags & LYS_CONFIG_MASK); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4516 | } |
| 4517 | |
| 4518 | /* default value ... */ |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4519 | if (rfn->dflt_size) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4520 | if (node->nodetype == LYS_LEAF) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4521 | /* leaf */ |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4522 | leaf = (struct lys_node_leaf *)node; |
| 4523 | |
| 4524 | lydict_remove(ctx, leaf->dflt); |
| 4525 | leaf->dflt = lydict_insert(ctx, rfn->dflt[0], 0); |
| 4526 | |
| 4527 | /* check the default value */ |
| 4528 | 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] | 4529 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4530 | } |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4531 | } else if (node->nodetype == LYS_LEAFLIST) { |
| 4532 | /* leaf-list */ |
| 4533 | llist = (struct lys_node_leaflist *)node; |
| 4534 | |
| 4535 | /* remove complete set of defaults in target */ |
| 4536 | for (i = 0; i < llist->dflt_size; i++) { |
| 4537 | lydict_remove(ctx, llist->dflt[i]); |
| 4538 | } |
| 4539 | free(llist->dflt); |
| 4540 | |
| 4541 | /* copy the default set from refine */ |
| 4542 | llist->dflt_size = rfn->dflt_size; |
| 4543 | llist->dflt = malloc(llist->dflt_size * sizeof *llist->dflt); |
| 4544 | for (i = 0; i < llist->dflt_size; i++) { |
| 4545 | llist->dflt[i] = lydict_insert(ctx, rfn->dflt[i], 0); |
| 4546 | } |
| 4547 | |
| 4548 | /* check default value */ |
| 4549 | for (i = 0; i < llist->dflt_size; i++) { |
| 4550 | 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] | 4551 | goto fail; |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4552 | } |
| 4553 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4554 | } |
| 4555 | } |
| 4556 | |
| 4557 | /* mandatory on leaf, anyxml or choice */ |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 4558 | if (rfn->flags & LYS_MAND_MASK) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4559 | if (node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_CHOICE)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4560 | /* remove current value */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4561 | node->flags &= ~LYS_MAND_MASK; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4562 | |
| 4563 | /* set new value */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4564 | node->flags |= (rfn->flags & LYS_MAND_MASK); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4565 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4566 | if (rfn->flags & LYS_MAND_TRUE) { |
| 4567 | /* check if node has default value */ |
| 4568 | if ((node->nodetype & LYS_LEAF) && ((struct lys_node_leaf *)node)->dflt) { |
| 4569 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The \"mandatory\" statement is forbidden on leaf with \"default\"."); |
| 4570 | goto fail; |
| 4571 | } |
| 4572 | if ((node->nodetype & LYS_CHOICE) && ((struct lys_node_choice *)node)->dflt) { |
| 4573 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The \"mandatory\" statement is forbidden on choices with \"default\"."); |
| 4574 | goto fail; |
| 4575 | } |
| 4576 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4577 | } |
| 4578 | |
| 4579 | /* presence on container */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4580 | if ((node->nodetype & LYS_CONTAINER) && rfn->mod.presence) { |
| 4581 | lydict_remove(ctx, ((struct lys_node_container *)node)->presence); |
| 4582 | ((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] | 4583 | } |
| 4584 | |
| 4585 | /* min/max-elements on list or leaf-list */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4586 | if (node->nodetype == LYS_LIST) { |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 4587 | if (rfn->flags & LYS_RFN_MINSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4588 | ((struct lys_node_list *)node)->min = rfn->mod.list.min; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4589 | } |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 4590 | if (rfn->flags & LYS_RFN_MAXSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4591 | ((struct lys_node_list *)node)->max = rfn->mod.list.max; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4592 | } |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4593 | } else if (node->nodetype == LYS_LEAFLIST) { |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 4594 | if (rfn->flags & LYS_RFN_MINSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4595 | ((struct lys_node_leaflist *)node)->min = rfn->mod.list.min; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4596 | } |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 4597 | if (rfn->flags & LYS_RFN_MAXSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4598 | ((struct lys_node_leaflist *)node)->max = rfn->mod.list.max; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4599 | } |
| 4600 | } |
| 4601 | |
| 4602 | /* must in leaf, leaf-list, list, container or anyxml */ |
| 4603 | if (rfn->must_size) { |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4604 | switch (node->nodetype) { |
| 4605 | case LYS_LEAF: |
| 4606 | old_size = &((struct lys_node_leaf *)node)->must_size; |
| 4607 | old_must = &((struct lys_node_leaf *)node)->must; |
| 4608 | break; |
| 4609 | case LYS_LEAFLIST: |
| 4610 | old_size = &((struct lys_node_leaflist *)node)->must_size; |
| 4611 | old_must = &((struct lys_node_leaflist *)node)->must; |
| 4612 | break; |
| 4613 | case LYS_LIST: |
| 4614 | old_size = &((struct lys_node_list *)node)->must_size; |
| 4615 | old_must = &((struct lys_node_list *)node)->must; |
| 4616 | break; |
| 4617 | case LYS_CONTAINER: |
| 4618 | old_size = &((struct lys_node_container *)node)->must_size; |
| 4619 | old_must = &((struct lys_node_container *)node)->must; |
| 4620 | break; |
| 4621 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4622 | case LYS_ANYDATA: |
| 4623 | old_size = &((struct lys_node_anydata *)node)->must_size; |
| 4624 | old_must = &((struct lys_node_anydata *)node)->must; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4625 | break; |
| 4626 | default: |
| 4627 | LOGINT; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4628 | goto fail; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4629 | } |
| 4630 | |
| 4631 | size = *old_size + rfn->must_size; |
| 4632 | must = realloc(*old_must, size * sizeof *rfn->must); |
| 4633 | if (!must) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4634 | LOGMEM; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4635 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4636 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4637 | for (k = 0, j = *old_size; k < rfn->must_size; k++, j++) { |
| 4638 | must[j].expr = lydict_insert(ctx, rfn->must[k].expr, 0); |
| 4639 | must[j].dsc = lydict_insert(ctx, rfn->must[k].dsc, 0); |
| 4640 | must[j].ref = lydict_insert(ctx, rfn->must[k].ref, 0); |
| 4641 | must[j].eapptag = lydict_insert(ctx, rfn->must[k].eapptag, 0); |
| 4642 | must[j].emsg = lydict_insert(ctx, rfn->must[k].emsg, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4643 | } |
| 4644 | |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4645 | *old_must = must; |
| 4646 | *old_size = size; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 4647 | |
| 4648 | /* check XPath dependencies again */ |
| 4649 | if (unres_schema_add_node(node->module, unres, node, UNRES_XPATH, NULL) == -1) { |
| 4650 | goto fail; |
| 4651 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4652 | } |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4653 | |
| 4654 | /* if-feature in leaf, leaf-list, list, container or anyxml */ |
| 4655 | if (rfn->iffeature_size) { |
| 4656 | old_size = &node->iffeature_size; |
| 4657 | old_iff = &node->iffeature; |
| 4658 | |
| 4659 | size = *old_size + rfn->iffeature_size; |
| 4660 | iff = realloc(*old_iff, size * sizeof *rfn->iffeature); |
| 4661 | if (!iff) { |
| 4662 | LOGMEM; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4663 | goto fail; |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4664 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4665 | for (k = 0, j = *old_size; k < rfn->iffeature_size; k++, j++) { |
| 4666 | resolve_iffeature_getsizes(&rfn->iffeature[k], &usize1, &usize2); |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4667 | if (usize1) { |
| 4668 | /* there is something to duplicate */ |
| 4669 | /* duplicate compiled expression */ |
| 4670 | usize = (usize1 / 4) + (usize1 % 4) ? 1 : 0; |
| 4671 | iff[j].expr = malloc(usize * sizeof *iff[j].expr); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4672 | 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] | 4673 | |
| 4674 | /* duplicate list of feature pointers */ |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4675 | iff[j].features = malloc(usize2 * sizeof *iff[k].features); |
| 4676 | 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] | 4677 | } |
| 4678 | } |
| 4679 | |
| 4680 | *old_iff = iff; |
| 4681 | *old_size = size; |
| 4682 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4683 | } |
| 4684 | |
| 4685 | /* apply augments */ |
| 4686 | for (i = 0; i < uses->augment_size; i++) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4687 | rc = resolve_augment(&uses->augment[i], uses->child); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4688 | if (rc) { |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4689 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4690 | } |
| 4691 | } |
| 4692 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4693 | /* check refines */ |
| 4694 | for (i = 0; i < uses->refine_size; i++) { |
| 4695 | node = refine_nodes[i]; |
| 4696 | rfn = &uses->refine[i]; |
| 4697 | |
| 4698 | /* config on any nodetype */ |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4699 | if ((rfn->flags & LYS_CONFIG_MASK) && !clear_config) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4700 | for (parent = lys_parent(node); parent && parent->nodetype == LYS_USES; parent = lys_parent(parent)); |
| 4701 | if (parent && parent->nodetype != LYS_GROUPING && |
| 4702 | ((parent->flags & LYS_CONFIG_MASK) != (rfn->flags & LYS_CONFIG_MASK)) && |
| 4703 | (rfn->flags & LYS_CONFIG_W)) { |
| 4704 | /* setting config true under config false is prohibited */ |
| 4705 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, "config", "refine"); |
| 4706 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, |
| 4707 | "changing config from 'false' to 'true' is prohibited while " |
| 4708 | "the target's parent is still config 'false'."); |
| 4709 | goto fail; |
| 4710 | } |
| 4711 | |
| 4712 | /* inherit config change to the target children */ |
| 4713 | LY_TREE_DFS_BEGIN(node->child, next, iter) { |
| 4714 | if (rfn->flags & LYS_CONFIG_W) { |
| 4715 | if (iter->flags & LYS_CONFIG_SET) { |
| 4716 | /* config is set explicitely, go to next sibling */ |
| 4717 | next = NULL; |
| 4718 | goto nextsibling; |
| 4719 | } |
| 4720 | } else { /* LYS_CONFIG_R */ |
| 4721 | if ((iter->flags & LYS_CONFIG_SET) && (iter->flags & LYS_CONFIG_W)) { |
| 4722 | /* error - we would have config data under status data */ |
| 4723 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, "config", "refine"); |
| 4724 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, |
| 4725 | "changing config from 'true' to 'false' is prohibited while the target " |
| 4726 | "has still a children with explicit config 'true'."); |
| 4727 | goto fail; |
| 4728 | } |
| 4729 | } |
| 4730 | /* change config */ |
| 4731 | iter->flags &= ~LYS_CONFIG_MASK; |
| 4732 | iter->flags |= (rfn->flags & LYS_CONFIG_MASK); |
| 4733 | |
| 4734 | /* select next iter - modified LY_TREE_DFS_END */ |
| 4735 | if (iter->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
| 4736 | next = NULL; |
| 4737 | } else { |
| 4738 | next = iter->child; |
| 4739 | } |
| 4740 | nextsibling: |
| 4741 | if (!next) { |
| 4742 | /* try siblings */ |
| 4743 | next = iter->next; |
| 4744 | } |
| 4745 | while (!next) { |
| 4746 | /* parent is already processed, go to its sibling */ |
| 4747 | iter = lys_parent(iter); |
| 4748 | |
| 4749 | /* no siblings, go back through parents */ |
| 4750 | if (iter == node) { |
| 4751 | /* we are done, no next element to process */ |
| 4752 | break; |
| 4753 | } |
| 4754 | next = iter->next; |
| 4755 | } |
| 4756 | } |
| 4757 | } |
| 4758 | |
| 4759 | /* default value */ |
| 4760 | if (rfn->dflt_size && node->nodetype == LYS_CHOICE) { |
| 4761 | /* choice */ |
| 4762 | |
| 4763 | ((struct lys_node_choice *)node)->dflt = resolve_choice_dflt((struct lys_node_choice *)node, |
| 4764 | rfn->dflt[0]); |
| 4765 | if (!((struct lys_node_choice *)node)->dflt) { |
| 4766 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->dflt[0], "default"); |
| 4767 | goto fail; |
| 4768 | } |
| 4769 | if (lyp_check_mandatory_choice(node)) { |
| 4770 | goto fail; |
| 4771 | } |
| 4772 | } |
| 4773 | |
| 4774 | /* min/max-elements on list or leaf-list */ |
| 4775 | if (node->nodetype == LYS_LIST) { |
| 4776 | if (((struct lys_node_list *)node)->min > ((struct lys_node_list *)node)->max) { |
| 4777 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid value \"%d\" of \"%s\".", rfn->mod.list.min, "min-elements"); |
| 4778 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"min-elements\" is bigger than \"max-elements\"."); |
| 4779 | goto fail; |
| 4780 | } |
| 4781 | } else if (node->nodetype == LYS_LEAFLIST) { |
| 4782 | if (((struct lys_node_leaflist *)node)->min > ((struct lys_node_leaflist *)node)->max) { |
| 4783 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid value \"%d\" of \"%s\".", rfn->mod.list.min, "min-elements"); |
| 4784 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"min-elements\" is bigger than \"max-elements\"."); |
| 4785 | goto fail; |
| 4786 | } |
| 4787 | } |
| 4788 | |
| 4789 | /* additional checks */ |
| 4790 | if (node->nodetype == LYS_LEAFLIST) { |
| 4791 | llist = (struct lys_node_leaflist *)node; |
| 4792 | if (llist->dflt_size && llist->min) { |
| 4793 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, rfn->dflt_size ? "default" : "min-elements", "refine"); |
| 4794 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, |
| 4795 | "The \"min-elements\" statement with non-zero value is forbidden on leaf-lists with the \"default\" statement."); |
| 4796 | goto fail; |
| 4797 | } |
| 4798 | } |
| 4799 | if ((rfn->flags & LYS_MAND_TRUE) || rfn->mod.list.min) { |
| 4800 | /* check for mandatory node in default case, first find the closest parent choice to the changed node */ |
| 4801 | for (parent = node->parent; |
| 4802 | parent && !(parent->nodetype & (LYS_CHOICE | LYS_GROUPING | LYS_ACTION | LYS_USES)); |
| 4803 | parent = parent->parent) { |
| 4804 | if (parent->nodetype == LYS_CONTAINER && ((struct lys_node_container *)parent)->presence) { |
| 4805 | /* stop also on presence containers */ |
| 4806 | break; |
| 4807 | } |
| 4808 | } |
| 4809 | /* and if it is a choice with the default case, check it for presence of a mandatory node in it */ |
| 4810 | if (parent && parent->nodetype == LYS_CHOICE && ((struct lys_node_choice *)parent)->dflt) { |
| 4811 | if (lyp_check_mandatory_choice(parent)) { |
| 4812 | goto fail; |
| 4813 | } |
| 4814 | } |
| 4815 | } |
| 4816 | } |
| 4817 | free(refine_nodes); |
| 4818 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4819 | return EXIT_SUCCESS; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4820 | |
| 4821 | fail: |
| 4822 | LY_TREE_FOR_SAFE(uses->child, next, iter) { |
| 4823 | lys_node_free(iter, NULL, 0); |
| 4824 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4825 | free(refine_nodes); |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4826 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4827 | } |
| 4828 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4829 | static int |
| 4830 | identity_backlink_update(struct lys_ident *der, struct lys_ident *base) |
| 4831 | { |
| 4832 | int i; |
| 4833 | |
| 4834 | assert(der && base); |
| 4835 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4836 | if (!base->der) { |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 4837 | /* create a set for backlinks if it does not exist */ |
| 4838 | base->der = ly_set_new(); |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4839 | } |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 4840 | /* store backlink */ |
| 4841 | ly_set_add(base->der, der, LY_SET_OPT_USEASLIST); |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4842 | |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 4843 | /* do it recursively */ |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4844 | for (i = 0; i < base->base_size; i++) { |
| 4845 | if (identity_backlink_update(der, base->base[i])) { |
| 4846 | return EXIT_FAILURE; |
| 4847 | } |
| 4848 | } |
| 4849 | |
| 4850 | return EXIT_SUCCESS; |
| 4851 | } |
| 4852 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4853 | /** |
| 4854 | * @brief Resolve base identity recursively. Does not log. |
| 4855 | * |
| 4856 | * @param[in] module Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4857 | * @param[in] ident Identity to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4858 | * @param[in] basename Base name of the identity. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4859 | * @param[out] ret Pointer to the resolved identity. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4860 | * |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 4861 | * @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] | 4862 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4863 | static int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 4864 | 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] | 4865 | struct unres_schema *unres, struct lys_ident **ret) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4866 | { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 4867 | uint32_t i, j; |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4868 | struct lys_ident *base = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4869 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 4870 | assert(ret); |
| 4871 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4872 | /* search module */ |
| 4873 | for (i = 0; i < module->ident_size; i++) { |
| 4874 | if (!strcmp(basename, module->ident[i].name)) { |
| 4875 | |
| 4876 | if (!ident) { |
| 4877 | /* just search for type, so do not modify anything, just return |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 4878 | * the base identity pointer */ |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 4879 | *ret = &module->ident[i]; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4880 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4881 | } |
| 4882 | |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 4883 | base = &module->ident[i]; |
| 4884 | goto matchfound; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4885 | } |
| 4886 | } |
| 4887 | |
| 4888 | /* search submodules */ |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 4889 | for (j = 0; j < module->inc_size && module->inc[j].submodule; j++) { |
| 4890 | for (i = 0; i < module->inc[j].submodule->ident_size; i++) { |
| 4891 | if (!strcmp(basename, module->inc[j].submodule->ident[i].name)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4892 | |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 4893 | if (!ident) { |
| 4894 | *ret = &module->inc[j].submodule->ident[i]; |
| 4895 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4896 | } |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 4897 | |
| 4898 | base = &module->inc[j].submodule->ident[i]; |
| 4899 | goto matchfound; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4900 | } |
| 4901 | } |
| 4902 | } |
| 4903 | |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 4904 | matchfound: |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4905 | /* we found it somewhere */ |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 4906 | if (base) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4907 | /* is it already completely resolved? */ |
| 4908 | for (i = 0; i < unres->count; i++) { |
Radek Krejci | ba7b1cc | 2016-08-08 13:44:43 +0200 | [diff] [blame] | 4909 | if ((unres->item[i] == base) && (unres->type[i] == UNRES_IDENT)) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4910 | /* identity found, but not yet resolved, so do not return it in *res and try it again later */ |
| 4911 | |
| 4912 | /* simple check for circular reference, |
| 4913 | * the complete check is done as a side effect of using only completely |
| 4914 | * resolved identities (previous check of unres content) */ |
| 4915 | if (ly_strequal((const char *)unres->str_snode[i], ident->name, 1)) { |
| 4916 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, basename, "base"); |
| 4917 | 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] | 4918 | return -1; |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4919 | } |
| 4920 | |
Radek Krejci | 06f64ed | 2016-08-15 11:07:44 +0200 | [diff] [blame] | 4921 | return EXIT_FAILURE; |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 4922 | } |
| 4923 | } |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4924 | |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 4925 | /* checks done, store the result */ |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4926 | *ret = base; |
Pavol Vican | fdab9f9 | 2016-09-07 15:23:27 +0200 | [diff] [blame] | 4927 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4928 | } |
| 4929 | |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 4930 | /* base not found (maybe a forward reference) */ |
| 4931 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4932 | } |
| 4933 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4934 | /** |
| 4935 | * @brief Resolve base identity. Logs directly. |
| 4936 | * |
| 4937 | * @param[in] module Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4938 | * @param[in] ident Identity to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4939 | * @param[in] basename Base name of the identity. |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 4940 | * @param[in] parent Either "type" or "identity". |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 4941 | * @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] | 4942 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4943 | * @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] | 4944 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4945 | static int |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 4946 | 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] | 4947 | struct lys_type *type, struct unres_schema *unres) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4948 | { |
| 4949 | const char *name; |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 4950 | int mod_name_len = 0, rc; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 4951 | struct lys_ident *target, **ret; |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 4952 | uint16_t flags; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 4953 | struct lys_module *mod; |
| 4954 | |
| 4955 | assert((ident && !type) || (!ident && type)); |
| 4956 | |
| 4957 | if (!type) { |
| 4958 | /* have ident to resolve */ |
| 4959 | ret = ⌖ |
| 4960 | flags = ident->flags; |
| 4961 | mod = ident->module; |
| 4962 | } else { |
| 4963 | /* have type to fill */ |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 4964 | ++type->info.ident.count; |
| 4965 | type->info.ident.ref = ly_realloc(type->info.ident.ref, type->info.ident.count * sizeof *type->info.ident.ref); |
| 4966 | if (!type->info.ident.ref) { |
| 4967 | LOGMEM; |
| 4968 | return -1; |
| 4969 | } |
| 4970 | |
| 4971 | ret = &type->info.ident.ref[type->info.ident.count - 1]; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 4972 | flags = type->parent->flags; |
| 4973 | mod = type->parent->module; |
| 4974 | } |
Michal Vasko | f200600 | 2016-04-21 16:28:15 +0200 | [diff] [blame] | 4975 | *ret = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4976 | |
| 4977 | /* search for the base identity */ |
| 4978 | name = strchr(basename, ':'); |
| 4979 | if (name) { |
| 4980 | /* set name to correct position after colon */ |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 4981 | mod_name_len = name - basename; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4982 | name++; |
| 4983 | |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 4984 | 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] | 4985 | /* prefix refers to the current module, ignore it */ |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 4986 | mod_name_len = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4987 | } |
| 4988 | } else { |
| 4989 | name = basename; |
| 4990 | } |
| 4991 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 4992 | /* get module where to search */ |
| 4993 | module = lys_get_import_module(module, NULL, 0, mod_name_len ? basename : NULL, mod_name_len); |
| 4994 | if (!module) { |
| 4995 | /* identity refers unknown data model */ |
Radek Krejci | 02a0499 | 2016-03-17 16:06:37 +0100 | [diff] [blame] | 4996 | LOGVAL(LYE_INMOD, LY_VLOG_NONE, NULL, basename); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 4997 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4998 | } |
| 4999 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 5000 | /* search in the identified module ... */ |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5001 | rc = resolve_base_ident_sub(module, ident, name, unres, ret); |
| 5002 | if (!rc) { |
| 5003 | assert(*ret); |
| 5004 | |
| 5005 | /* check status */ |
| 5006 | if (lyp_check_status(flags, mod, ident ? ident->name : "of type", |
| 5007 | (*ret)->flags, (*ret)->module, (*ret)->name, NULL)) { |
| 5008 | rc = -1; |
Pavol Vican | fdab9f9 | 2016-09-07 15:23:27 +0200 | [diff] [blame] | 5009 | } else { |
| 5010 | if (ident) { |
| 5011 | ident->base[ident->base_size++] = *ret; |
| 5012 | |
| 5013 | /* maintain backlinks to the derived identities */ |
| 5014 | rc = identity_backlink_update(ident, *ret) ? -1 : EXIT_SUCCESS; |
| 5015 | } |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5016 | } |
| 5017 | } else if (rc == EXIT_FAILURE) { |
| 5018 | LOGVAL(LYE_INRESOLV, LY_VLOG_NONE, NULL, parent, basename); |
Pavol Vican | 053a288 | 2016-09-05 14:40:33 +0200 | [diff] [blame] | 5019 | if (type) { |
| 5020 | --type->info.ident.count; |
| 5021 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5022 | } |
| 5023 | |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5024 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5025 | } |
| 5026 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5027 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 5028 | * @brief Resolve JSON data format identityref. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5029 | * |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5030 | * @param[in] type Identityref type. |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 5031 | * @param[in] ident_name Identityref name. |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 5032 | * @param[in] node Node where the identityref is being resolved |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5033 | * |
| 5034 | * @return Pointer to the identity resolvent, NULL on error. |
| 5035 | */ |
Radek Krejci | a52656e | 2015-08-05 13:41:50 +0200 | [diff] [blame] | 5036 | struct lys_ident * |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5037 | 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] | 5038 | { |
Michal Vasko | c633ca0 | 2015-08-21 14:03:51 +0200 | [diff] [blame] | 5039 | const char *mod_name, *name; |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5040 | int mod_name_len, rc, i; |
| 5041 | unsigned int u; |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5042 | struct lys_ident *der, *cur; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5043 | |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5044 | if (!type || (!type->info.ident.count && !type->der) || !ident_name) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5045 | return NULL; |
| 5046 | } |
| 5047 | |
Michal Vasko | c633ca0 | 2015-08-21 14:03:51 +0200 | [diff] [blame] | 5048 | 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] | 5049 | if (rc < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5050 | 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] | 5051 | return NULL; |
Michal Vasko | b43bb3c | 2016-03-17 15:00:27 +0100 | [diff] [blame] | 5052 | } else if (rc < (signed)strlen(ident_name)) { |
Radek Krejci | 02a0499 | 2016-03-17 16:06:37 +0100 | [diff] [blame] | 5053 | 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] | 5054 | return NULL; |
| 5055 | } |
| 5056 | |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5057 | /* go through all the bases in all the derived types */ |
| 5058 | while (type->der) { |
| 5059 | for (i = 0; i < type->info.ident.count; ++i) { |
| 5060 | cur = type->info.ident.ref[i]; |
| 5061 | if (!strcmp(cur->name, name) && (!mod_name |
| 5062 | || (!strncmp(cur->module->name, mod_name, mod_name_len) && !cur->module->name[mod_name_len]))) { |
| 5063 | goto match; |
| 5064 | } |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 5065 | |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5066 | if (cur->der) { |
| 5067 | /* there are also some derived identities */ |
| 5068 | for (u = 0; u < cur->der->number; u++) { |
| 5069 | der = (struct lys_ident *)cur->der->set.g[u]; /* 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 | } |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5076 | } |
| 5077 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5078 | } |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5079 | type = &type->der->type; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5080 | } |
| 5081 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5082 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYD, node, "identityref", ident_name); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5083 | return NULL; |
Radek Krejci | f1ee2e2 | 2016-08-02 16:36:48 +0200 | [diff] [blame] | 5084 | |
| 5085 | match: |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5086 | for (i = 0; i < cur->iffeature_size; i++) { |
| 5087 | if (!resolve_iffeature(&cur->iffeature[i])) { |
| 5088 | LOGVAL(LYE_INVAL, LY_VLOG_LYD, node, cur->name, node->schema->name); |
Radek Krejci | f1ee2e2 | 2016-08-02 16:36:48 +0200 | [diff] [blame] | 5089 | 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] | 5090 | cur->name); |
Radek Krejci | f1ee2e2 | 2016-08-02 16:36:48 +0200 | [diff] [blame] | 5091 | return NULL; |
| 5092 | } |
| 5093 | } |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5094 | return cur; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5095 | } |
| 5096 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5097 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5098 | * @brief Resolve unresolved uses. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5099 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5100 | * @param[in] uses Uses to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5101 | * @param[in] unres Specific unres item. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5102 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5103 | * @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] | 5104 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5105 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5106 | 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] | 5107 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5108 | int rc; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5109 | struct lys_node *par_grp; |
Michal Vasko | e91afce | 2015-08-12 12:21:00 +0200 | [diff] [blame] | 5110 | |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5111 | /* HACK: when a grouping has uses inside, all such uses have to be resolved before the grouping itself |
| 5112 | * is used in some uses. When we see such a uses, the grouping's nacm member (not used in grouping) |
| 5113 | * is used to store number of so far unresolved uses. The grouping cannot be used unless the nacm |
| 5114 | * value is decreased back to 0. To remember that the uses already increased grouping's nacm, the |
| 5115 | * LYS_USESGRP flag is used. */ |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 5116 | 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] | 5117 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5118 | if (!uses->grp) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 5119 | rc = resolve_uses_schema_nodeid(uses->name, (const struct lys_node *)uses, (const struct lys_node_grp **)&uses->grp); |
| 5120 | if (rc == -1) { |
Michal Vasko | 92981a6 | 2016-10-14 10:25:16 +0200 | [diff] [blame] | 5121 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 5122 | return -1; |
| 5123 | } else if (rc > 0) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5124 | 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] | 5125 | return -1; |
| 5126 | } else if (!uses->grp) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5127 | if (par_grp && !(uses->flags & LYS_USESGRP)) { |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 5128 | /* hack - in contrast to lys_node, lys_node_grp has bigger nacm field |
| 5129 | * (and smaller flags - it uses only a limited set of flags) |
| 5130 | */ |
| 5131 | ((struct lys_node_grp *)par_grp)->nacm++; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5132 | uses->flags |= LYS_USESGRP; |
Michal Vasko | 407f1bb | 2015-09-23 15:51:07 +0200 | [diff] [blame] | 5133 | } |
Michal Vasko | 92981a6 | 2016-10-14 10:25:16 +0200 | [diff] [blame] | 5134 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 5135 | return EXIT_FAILURE; |
Michal Vasko | 12e3084 | 2015-08-04 11:54:00 +0200 | [diff] [blame] | 5136 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5137 | } |
| 5138 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5139 | if (uses->grp->nacm) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5140 | if (par_grp && !(uses->flags & LYS_USESGRP)) { |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 5141 | ((struct lys_node_grp *)par_grp)->nacm++; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5142 | uses->flags |= LYS_USESGRP; |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 5143 | } else { |
| 5144 | /* instantiate grouping only when it is completely resolved */ |
| 5145 | uses->grp = NULL; |
Michal Vasko | 407f1bb | 2015-09-23 15:51:07 +0200 | [diff] [blame] | 5146 | } |
Michal Vasko | 92981a6 | 2016-10-14 10:25:16 +0200 | [diff] [blame] | 5147 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5148 | return EXIT_FAILURE; |
| 5149 | } |
| 5150 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5151 | rc = resolve_uses(uses, unres); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5152 | if (!rc) { |
| 5153 | /* decrease unres count only if not first try */ |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5154 | if (par_grp && (uses->flags & LYS_USESGRP)) { |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 5155 | if (!((struct lys_node_grp *)par_grp)->nacm) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5156 | LOGINT; |
| 5157 | return -1; |
| 5158 | } |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 5159 | ((struct lys_node_grp *)par_grp)->nacm--; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5160 | uses->flags &= ~LYS_USESGRP; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5161 | } |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5162 | |
| 5163 | /* check status */ |
Radek Krejci | c655602 | 2016-01-27 15:16:45 +0100 | [diff] [blame] | 5164 | if (lyp_check_status(uses->flags, uses->module, "of uses", |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 5165 | uses->grp->flags, uses->grp->module, uses->grp->name, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5166 | (struct lys_node *)uses)) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5167 | return -1; |
| 5168 | } |
| 5169 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5170 | return EXIT_SUCCESS; |
| 5171 | } |
| 5172 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5173 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5174 | } |
| 5175 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5176 | /** |
Michal Vasko | 9957e59 | 2015-08-17 15:04:09 +0200 | [diff] [blame] | 5177 | * @brief Resolve list keys. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5178 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5179 | * @param[in] list List to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5180 | * @param[in] keys_str Keys node value. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5181 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5182 | * @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] | 5183 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5184 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5185 | resolve_list_keys(struct lys_node_list *list, const char *keys_str) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5186 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5187 | int i, len, rc; |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 5188 | const char *value; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5189 | |
| 5190 | for (i = 0; i < list->keys_size; ++i) { |
| 5191 | /* get the key name */ |
| 5192 | if ((value = strpbrk(keys_str, " \t\n"))) { |
| 5193 | len = value - keys_str; |
| 5194 | while (isspace(value[0])) { |
| 5195 | value++; |
| 5196 | } |
| 5197 | } else { |
| 5198 | len = strlen(keys_str); |
| 5199 | } |
| 5200 | |
Radek Krejci | c428344 | 2016-04-22 09:19:27 +0200 | [diff] [blame] | 5201 | 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] | 5202 | if (rc) { |
Michal Vasko | 7a55bea | 2016-05-02 14:51:20 +0200 | [diff] [blame] | 5203 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, list, "list keys", keys_str); |
| 5204 | return EXIT_FAILURE; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5205 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5206 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5207 | if (check_key(list, i, keys_str, len)) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5208 | /* check_key logs */ |
| 5209 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5210 | } |
| 5211 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5212 | /* check status */ |
Radek Krejci | c655602 | 2016-01-27 15:16:45 +0100 | [diff] [blame] | 5213 | if (lyp_check_status(list->flags, list->module, list->name, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5214 | list->keys[i]->flags, list->keys[i]->module, list->keys[i]->name, |
| 5215 | (struct lys_node *)list->keys[i])) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5216 | return -1; |
| 5217 | } |
| 5218 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5219 | /* prepare for next iteration */ |
| 5220 | while (value && isspace(value[0])) { |
| 5221 | value++; |
| 5222 | } |
| 5223 | keys_str = value; |
| 5224 | } |
| 5225 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5226 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5227 | } |
| 5228 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5229 | /** |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5230 | * @brief Resolve (check) all must conditions of \p node. |
| 5231 | * Logs directly. |
| 5232 | * |
| 5233 | * @param[in] node Data node with optional must statements. |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5234 | * @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] | 5235 | * |
| 5236 | * @return EXIT_SUCCESS on pass, EXIT_FAILURE on fail, -1 on error. |
| 5237 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5238 | static int |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5239 | resolve_must(struct lyd_node *node, int inout_parent) |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5240 | { |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5241 | uint8_t i, must_size; |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5242 | struct lys_node *schema; |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5243 | struct lys_restr *must; |
| 5244 | struct lyxp_set set; |
| 5245 | |
| 5246 | assert(node); |
| 5247 | memset(&set, 0, sizeof set); |
| 5248 | |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5249 | if (inout_parent) { |
| 5250 | for (schema = lys_parent(node->schema); |
| 5251 | schema && (schema->nodetype & (LYS_CHOICE | LYS_CASE | LYS_USES)); |
| 5252 | schema = lys_parent(schema)); |
| 5253 | if (!schema || !(schema->nodetype & (LYS_INPUT | LYS_OUTPUT))) { |
| 5254 | LOGINT; |
| 5255 | return -1; |
| 5256 | } |
| 5257 | must_size = ((struct lys_node_inout *)schema)->must_size; |
| 5258 | must = ((struct lys_node_inout *)schema)->must; |
| 5259 | |
| 5260 | /* context node is the RPC/action */ |
| 5261 | node = node->parent; |
| 5262 | if (!(node->schema->nodetype & (LYS_RPC | LYS_ACTION))) { |
| 5263 | LOGINT; |
| 5264 | return -1; |
| 5265 | } |
| 5266 | } else { |
| 5267 | switch (node->schema->nodetype) { |
| 5268 | case LYS_CONTAINER: |
| 5269 | must_size = ((struct lys_node_container *)node->schema)->must_size; |
| 5270 | must = ((struct lys_node_container *)node->schema)->must; |
| 5271 | break; |
| 5272 | case LYS_LEAF: |
| 5273 | must_size = ((struct lys_node_leaf *)node->schema)->must_size; |
| 5274 | must = ((struct lys_node_leaf *)node->schema)->must; |
| 5275 | break; |
| 5276 | case LYS_LEAFLIST: |
| 5277 | must_size = ((struct lys_node_leaflist *)node->schema)->must_size; |
| 5278 | must = ((struct lys_node_leaflist *)node->schema)->must; |
| 5279 | break; |
| 5280 | case LYS_LIST: |
| 5281 | must_size = ((struct lys_node_list *)node->schema)->must_size; |
| 5282 | must = ((struct lys_node_list *)node->schema)->must; |
| 5283 | break; |
| 5284 | case LYS_ANYXML: |
| 5285 | case LYS_ANYDATA: |
| 5286 | must_size = ((struct lys_node_anydata *)node->schema)->must_size; |
| 5287 | must = ((struct lys_node_anydata *)node->schema)->must; |
| 5288 | break; |
| 5289 | case LYS_NOTIF: |
| 5290 | must_size = ((struct lys_node_notif *)node->schema)->must_size; |
| 5291 | must = ((struct lys_node_notif *)node->schema)->must; |
| 5292 | break; |
| 5293 | default: |
| 5294 | must_size = 0; |
| 5295 | break; |
| 5296 | } |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5297 | } |
| 5298 | |
| 5299 | for (i = 0; i < must_size; ++i) { |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5300 | 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] | 5301 | return -1; |
| 5302 | } |
| 5303 | |
Michal Vasko | 944a564 | 2016-03-21 11:48:58 +0100 | [diff] [blame] | 5304 | lyxp_set_cast(&set, LYXP_SET_BOOLEAN, node, LYXP_MUST); |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5305 | |
Michal Vasko | 8146d4c | 2016-05-09 15:50:29 +0200 | [diff] [blame] | 5306 | if (!set.val.bool) { |
Michal Vasko | 6ac6828 | 2016-04-11 10:56:47 +0200 | [diff] [blame] | 5307 | LOGVAL(LYE_NOMUST, LY_VLOG_LYD, node, must[i].expr); |
| 5308 | if (must[i].emsg) { |
| 5309 | LOGVAL(LYE_SPEC, LY_VLOG_LYD, node, must[i].emsg); |
| 5310 | } |
| 5311 | if (must[i].eapptag) { |
| 5312 | strncpy(((struct ly_err *)&ly_errno)->apptag, must[i].eapptag, LY_APPTAG_LEN - 1); |
| 5313 | } |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5314 | return 1; |
| 5315 | } |
| 5316 | } |
| 5317 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5318 | return EXIT_SUCCESS; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5319 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5320 | |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5321 | /** |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5322 | * @brief Resolve (find) when condition schema context node. Does not log. |
| 5323 | * |
| 5324 | * @param[in] schema Schema node with the when condition. |
| 5325 | * @param[out] ctx_snode When schema context node. |
| 5326 | * @param[out] ctx_snode_type Schema context node type. |
| 5327 | */ |
| 5328 | void |
| 5329 | resolve_when_ctx_snode(const struct lys_node *schema, struct lys_node **ctx_snode, enum lyxp_node_type *ctx_snode_type) |
| 5330 | { |
| 5331 | const struct lys_node *sparent; |
| 5332 | |
| 5333 | /* find a not schema-only node */ |
| 5334 | *ctx_snode_type = LYXP_NODE_ELEM; |
| 5335 | while (schema->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE | LYS_AUGMENT | LYS_INPUT | LYS_OUTPUT)) { |
| 5336 | if (schema->nodetype == LYS_AUGMENT) { |
| 5337 | sparent = ((struct lys_node_augment *)schema)->target; |
| 5338 | } else { |
| 5339 | sparent = schema->parent; |
| 5340 | } |
| 5341 | if (!sparent) { |
| 5342 | /* context node is the document root (fake root in our case) */ |
| 5343 | if (schema->flags & LYS_CONFIG_W) { |
| 5344 | *ctx_snode_type = LYXP_NODE_ROOT_CONFIG; |
| 5345 | } else { |
Michal Vasko | b94a5e4 | 2016-09-08 14:01:56 +0200 | [diff] [blame] | 5346 | *ctx_snode_type = LYXP_NODE_ROOT; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5347 | } |
Michal Vasko | 2d2aec1 | 2016-09-08 11:42:50 +0200 | [diff] [blame] | 5348 | /* we need the first top-level sibling, but no uses or groupings */ |
Michal Vasko | b94a5e4 | 2016-09-08 14:01:56 +0200 | [diff] [blame] | 5349 | schema = lys_getnext(NULL, NULL, lys_node_module(schema), 0); |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5350 | break; |
| 5351 | } |
| 5352 | schema = sparent; |
| 5353 | } |
| 5354 | |
| 5355 | *ctx_snode = (struct lys_node *)schema; |
| 5356 | } |
| 5357 | |
| 5358 | /** |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5359 | * @brief Resolve (find) when condition context node. Does not log. |
| 5360 | * |
| 5361 | * @param[in] node Data node, whose conditional definition is being decided. |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5362 | * @param[in] schema Schema node with the when condition. |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5363 | * @param[out] ctx_node Context node. |
| 5364 | * @param[out] ctx_node_type Context node type. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5365 | * |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5366 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5367 | */ |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5368 | static int |
| 5369 | resolve_when_ctx_node(struct lyd_node *node, struct lys_node *schema, struct lyd_node **ctx_node, |
| 5370 | enum lyxp_node_type *ctx_node_type) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5371 | { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5372 | struct lyd_node *parent; |
| 5373 | struct lys_node *sparent; |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5374 | enum lyxp_node_type node_type; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5375 | uint16_t i, data_depth, schema_depth; |
| 5376 | |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5377 | resolve_when_ctx_snode(schema, &schema, &node_type); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5378 | |
Michal Vasko | fe98975 | 2016-09-08 08:47:26 +0200 | [diff] [blame] | 5379 | if (node_type == LYXP_NODE_ELEM) { |
| 5380 | /* standard element context node */ |
| 5381 | for (parent = node, data_depth = 0; parent; parent = parent->parent, ++data_depth); |
| 5382 | for (sparent = schema, schema_depth = 0; |
| 5383 | sparent; |
| 5384 | sparent = (sparent->nodetype == LYS_AUGMENT ? ((struct lys_node_augment *)sparent)->target : sparent->parent)) { |
| 5385 | if (sparent->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_NOTIF | LYS_RPC)) { |
| 5386 | ++schema_depth; |
| 5387 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5388 | } |
Michal Vasko | fe98975 | 2016-09-08 08:47:26 +0200 | [diff] [blame] | 5389 | if (data_depth < schema_depth) { |
| 5390 | return -1; |
| 5391 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5392 | |
Michal Vasko | 956e854 | 2016-08-26 09:43:35 +0200 | [diff] [blame] | 5393 | /* find the corresponding data node */ |
| 5394 | for (i = 0; i < data_depth - schema_depth; ++i) { |
| 5395 | node = node->parent; |
| 5396 | } |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5397 | if (node->schema != schema) { |
| 5398 | return -1; |
| 5399 | } |
Michal Vasko | fe98975 | 2016-09-08 08:47:26 +0200 | [diff] [blame] | 5400 | } else { |
| 5401 | /* root context node */ |
| 5402 | while (node->parent) { |
| 5403 | node = node->parent; |
| 5404 | } |
| 5405 | while (node->prev->next) { |
| 5406 | node = node->prev; |
| 5407 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5408 | } |
| 5409 | |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5410 | *ctx_node = node; |
| 5411 | *ctx_node_type = node_type; |
| 5412 | return EXIT_SUCCESS; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5413 | } |
| 5414 | |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5415 | /** |
| 5416 | * @brief Temporarily unlink nodes as per YANG 1.1 RFC section 7.21.5 for when XPath evaluation. |
| 5417 | * The context nodes is adjusted if needed. |
| 5418 | * |
| 5419 | * @param[in] snode Schema node, whose children instances need to be unlinked. |
| 5420 | * @param[in,out] node Data siblings where to look for the children of \p snode. If it is unlinked, |
| 5421 | * it is moved to point to another sibling still in the original tree. |
| 5422 | * @param[in,out] ctx_node When context node, adjusted if needed. |
| 5423 | * @param[in] ctx_node_type Context node type, just for information to detect invalid situations. |
| 5424 | * @param[out] unlinked_nodes Unlinked siblings. Can be safely appended to \p node afterwards. |
| 5425 | * Ordering may change, but there will be no semantic change. |
| 5426 | * |
| 5427 | * @return EXIT_SUCCESS on success, -1 on error. |
| 5428 | */ |
| 5429 | static int |
| 5430 | resolve_when_unlink_nodes(struct lys_node *snode, struct lyd_node **node, struct lyd_node **ctx_node, |
| 5431 | enum lyxp_node_type ctx_node_type, struct lyd_node **unlinked_nodes) |
| 5432 | { |
| 5433 | struct lyd_node *next, *elem; |
| 5434 | |
| 5435 | switch (snode->nodetype) { |
| 5436 | case LYS_AUGMENT: |
| 5437 | case LYS_USES: |
| 5438 | case LYS_CHOICE: |
| 5439 | case LYS_CASE: |
| 5440 | LY_TREE_FOR(snode->child, snode) { |
| 5441 | if (resolve_when_unlink_nodes(snode, node, ctx_node, ctx_node_type, unlinked_nodes)) { |
| 5442 | return -1; |
| 5443 | } |
| 5444 | } |
| 5445 | break; |
| 5446 | case LYS_CONTAINER: |
| 5447 | case LYS_LIST: |
| 5448 | case LYS_LEAF: |
| 5449 | case LYS_LEAFLIST: |
| 5450 | case LYS_ANYXML: |
| 5451 | case LYS_ANYDATA: |
| 5452 | LY_TREE_FOR_SAFE(lyd_first_sibling(*node), next, elem) { |
| 5453 | if (elem->schema == snode) { |
| 5454 | |
| 5455 | if (elem == *ctx_node) { |
| 5456 | /* We are going to unlink our context node! This normally cannot happen, |
| 5457 | * but we use normal top-level data nodes for faking a document root node, |
| 5458 | * so if this is the context node, we just use the next top-level node. |
| 5459 | * Additionally, it can even happen that there are no top-level data nodes left, |
| 5460 | * all were unlinked, so in this case we pass NULL as the context node/data tree, |
| 5461 | * lyxp_eval() can handle this special situation. |
| 5462 | */ |
| 5463 | if (ctx_node_type == LYXP_NODE_ELEM) { |
| 5464 | LOGINT; |
| 5465 | return -1; |
| 5466 | } |
| 5467 | |
| 5468 | if (elem->prev == elem) { |
| 5469 | /* unlinking last top-level element, use an empty data tree */ |
| 5470 | *ctx_node = NULL; |
| 5471 | } else { |
| 5472 | /* in this case just use the previous/last top-level data node */ |
| 5473 | *ctx_node = elem->prev; |
| 5474 | } |
| 5475 | } else if (elem == *node) { |
| 5476 | /* We are going to unlink the currently processed node. This does not matter that |
| 5477 | * much, but we would lose access to the original data tree, so just move our |
| 5478 | * pointer somewhere still inside it. |
| 5479 | */ |
| 5480 | if ((*node)->prev != *node) { |
| 5481 | *node = (*node)->prev; |
| 5482 | } else { |
| 5483 | /* the processed node with sibings were all unlinked, oh well */ |
| 5484 | *node = NULL; |
| 5485 | } |
| 5486 | } |
| 5487 | |
| 5488 | /* temporarily unlink the node */ |
| 5489 | lyd_unlink(elem); |
| 5490 | if (*unlinked_nodes) { |
| 5491 | if (lyd_insert_after(*unlinked_nodes, elem)) { |
| 5492 | LOGINT; |
| 5493 | return -1; |
| 5494 | } |
| 5495 | } else { |
| 5496 | *unlinked_nodes = elem; |
| 5497 | } |
| 5498 | |
| 5499 | if (snode->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_ANYDATA)) { |
| 5500 | /* there can be only one instance */ |
| 5501 | break; |
| 5502 | } |
| 5503 | } |
| 5504 | } |
| 5505 | break; |
| 5506 | default: |
| 5507 | LOGINT; |
| 5508 | return -1; |
| 5509 | } |
| 5510 | |
| 5511 | return EXIT_SUCCESS; |
| 5512 | } |
| 5513 | |
| 5514 | /** |
| 5515 | * @brief Relink the unlinked nodes back. |
| 5516 | * |
| 5517 | * @param[in] node Data node to link the nodes back to. It can actually be the adjusted context node, |
| 5518 | * we simply need a sibling from the original data tree. |
| 5519 | * @param[in] unlinked_nodes Unlinked nodes to relink to \p node. |
| 5520 | * @param[in] ctx_node_type Context node type to distinguish between \p node being the parent |
| 5521 | * or the sibling of \p unlinked_nodes. |
| 5522 | * |
| 5523 | * @return EXIT_SUCCESS on success, -1 on error. |
| 5524 | */ |
| 5525 | static int |
| 5526 | resolve_when_relink_nodes(struct lyd_node *node, struct lyd_node *unlinked_nodes, enum lyxp_node_type ctx_node_type) |
| 5527 | { |
| 5528 | struct lyd_node *elem; |
| 5529 | |
| 5530 | LY_TREE_FOR_SAFE(unlinked_nodes, unlinked_nodes, elem) { |
| 5531 | if (ctx_node_type == LYXP_NODE_ELEM) { |
| 5532 | if (lyd_insert(node, elem)) { |
| 5533 | return -1; |
| 5534 | } |
| 5535 | } else { |
| 5536 | if (lyd_insert_after(node, elem)) { |
| 5537 | return -1; |
| 5538 | } |
| 5539 | } |
| 5540 | } |
| 5541 | |
| 5542 | return EXIT_SUCCESS; |
| 5543 | } |
| 5544 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5545 | int |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5546 | resolve_applies_must(const struct lyd_node *node) |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5547 | { |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5548 | int ret = 0; |
| 5549 | uint8_t must_size; |
| 5550 | struct lys_node *schema, *iter; |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5551 | |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5552 | assert(node); |
| 5553 | |
| 5554 | schema = node->schema; |
| 5555 | |
| 5556 | /* their own must */ |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5557 | switch (schema->nodetype) { |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5558 | case LYS_CONTAINER: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5559 | must_size = ((struct lys_node_container *)schema)->must_size; |
| 5560 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5561 | case LYS_LEAF: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5562 | must_size = ((struct lys_node_leaf *)schema)->must_size; |
| 5563 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5564 | case LYS_LEAFLIST: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5565 | must_size = ((struct lys_node_leaflist *)schema)->must_size; |
| 5566 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5567 | case LYS_LIST: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5568 | must_size = ((struct lys_node_list *)schema)->must_size; |
| 5569 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5570 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 5571 | case LYS_ANYDATA: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5572 | must_size = ((struct lys_node_anydata *)schema)->must_size; |
| 5573 | break; |
| 5574 | case LYS_NOTIF: |
| 5575 | must_size = ((struct lys_node_notif *)schema)->must_size; |
| 5576 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5577 | default: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5578 | must_size = 0; |
| 5579 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5580 | } |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5581 | |
| 5582 | if (must_size) { |
| 5583 | ++ret; |
| 5584 | } |
| 5585 | |
| 5586 | /* schema may be a direct data child of input/output with must (but it must be first, it needs to be evaluated only once) */ |
| 5587 | if (!node->prev->next) { |
| 5588 | for (iter = lys_parent(schema); iter && (iter->nodetype & (LYS_CHOICE | LYS_CASE | LYS_USES)); iter = lys_parent(iter)); |
| 5589 | if (iter && (iter->nodetype & (LYS_INPUT | LYS_OUTPUT))) { |
| 5590 | ret += 0x2; |
| 5591 | } |
| 5592 | } |
| 5593 | |
| 5594 | return ret; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5595 | } |
| 5596 | |
| 5597 | int |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5598 | 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] | 5599 | { |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5600 | const struct lys_node *parent; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5601 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5602 | assert(schema); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5603 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5604 | 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] | 5605 | return 1; |
| 5606 | } |
| 5607 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5608 | parent = schema; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5609 | goto check_augment; |
| 5610 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5611 | while (parent) { |
| 5612 | /* stop conditions */ |
| 5613 | if (!mode) { |
| 5614 | /* stop on node that can be instantiated in data tree */ |
| 5615 | if (!(parent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) { |
| 5616 | break; |
| 5617 | } |
| 5618 | } else { |
| 5619 | /* stop on the specified node */ |
| 5620 | if (parent == stop) { |
| 5621 | break; |
| 5622 | } |
| 5623 | } |
| 5624 | |
| 5625 | if (((const struct lys_node_uses *)parent)->when) { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5626 | return 1; |
| 5627 | } |
| 5628 | check_augment: |
| 5629 | |
| 5630 | if ((parent->parent && (parent->parent->nodetype == LYS_AUGMENT) && |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5631 | (((const struct lys_node_augment *)parent->parent)->when))) { |
Michal Vasko | e365556 | 2016-08-24 15:56:17 +0200 | [diff] [blame] | 5632 | return 1; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5633 | } |
| 5634 | parent = lys_parent(parent); |
| 5635 | } |
| 5636 | |
| 5637 | return 0; |
| 5638 | } |
| 5639 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5640 | /** |
| 5641 | * @brief Resolve (check) all when conditions relevant for \p node. |
| 5642 | * Logs directly. |
| 5643 | * |
| 5644 | * @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] | 5645 | * |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5646 | * @return |
| 5647 | * -1 - error, ly_errno is set |
| 5648 | * 0 - true "when" statement |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5649 | * 0, ly_vecode = LYVE_NOWHEN - false "when" statement |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5650 | * 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] | 5651 | */ |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5652 | int |
| 5653 | resolve_when(struct lyd_node *node, int *result) |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5654 | { |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5655 | struct lyd_node *ctx_node = NULL, *unlinked_nodes, *tmp_node; |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5656 | struct lys_node *sparent; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5657 | struct lyxp_set set; |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5658 | enum lyxp_node_type ctx_node_type; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5659 | int rc = 0; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5660 | |
| 5661 | assert(node); |
| 5662 | memset(&set, 0, sizeof set); |
| 5663 | |
| 5664 | 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] | 5665 | /* make the node dummy for the evaluation */ |
| 5666 | node->validity |= LYD_VAL_INUSE; |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5667 | 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] | 5668 | node->validity &= ~LYD_VAL_INUSE; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5669 | if (rc) { |
| 5670 | if (rc == 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5671 | 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] | 5672 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5673 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5674 | } |
| 5675 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5676 | /* set boolean result of the condition */ |
Michal Vasko | 944a564 | 2016-03-21 11:48:58 +0100 | [diff] [blame] | 5677 | lyxp_set_cast(&set, LYXP_SET_BOOLEAN, node, LYXP_WHEN); |
Michal Vasko | 8146d4c | 2016-05-09 15:50:29 +0200 | [diff] [blame] | 5678 | if (!set.val.bool) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5679 | 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] | 5680 | node->when_status |= LYD_WHEN_FALSE; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5681 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5682 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5683 | |
| 5684 | /* free xpath set content */ |
| 5685 | lyxp_set_cast(&set, LYXP_SET_EMPTY, node, 0); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5686 | } |
| 5687 | |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5688 | sparent = node->schema; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5689 | goto check_augment; |
| 5690 | |
| 5691 | /* check when in every schema node that affects node */ |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5692 | while (sparent && (sparent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) { |
| 5693 | if (((struct lys_node_uses *)sparent)->when) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5694 | if (!ctx_node) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5695 | rc = resolve_when_ctx_node(node, sparent, &ctx_node, &ctx_node_type); |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5696 | if (rc) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5697 | LOGINT; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5698 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5699 | } |
| 5700 | } |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5701 | |
| 5702 | unlinked_nodes = NULL; |
| 5703 | /* we do not want our node pointer to change */ |
| 5704 | tmp_node = node; |
| 5705 | rc = resolve_when_unlink_nodes(sparent, &tmp_node, &ctx_node, ctx_node_type, &unlinked_nodes); |
| 5706 | if (rc) { |
| 5707 | goto cleanup; |
| 5708 | } |
| 5709 | |
| 5710 | rc = lyxp_eval(((struct lys_node_uses *)sparent)->when->cond, ctx_node, ctx_node_type, &set, LYXP_WHEN); |
| 5711 | |
| 5712 | if (unlinked_nodes && ctx_node) { |
| 5713 | if (resolve_when_relink_nodes(ctx_node, unlinked_nodes, ctx_node_type)) { |
| 5714 | rc = -1; |
| 5715 | goto cleanup; |
| 5716 | } |
| 5717 | } |
| 5718 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5719 | if (rc) { |
| 5720 | if (rc == 1) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5721 | 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] | 5722 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5723 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5724 | } |
| 5725 | |
Michal Vasko | 944a564 | 2016-03-21 11:48:58 +0100 | [diff] [blame] | 5726 | lyxp_set_cast(&set, LYXP_SET_BOOLEAN, ctx_node, LYXP_WHEN); |
Michal Vasko | 8146d4c | 2016-05-09 15:50:29 +0200 | [diff] [blame] | 5727 | if (!set.val.bool) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5728 | 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] | 5729 | node->when_status |= LYD_WHEN_FALSE; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5730 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5731 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5732 | |
| 5733 | /* free xpath set content */ |
| 5734 | lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node, 0); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5735 | } |
| 5736 | |
| 5737 | check_augment: |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5738 | 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] | 5739 | if (!ctx_node) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5740 | 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] | 5741 | if (rc) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5742 | LOGINT; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5743 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5744 | } |
| 5745 | } |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5746 | |
| 5747 | unlinked_nodes = NULL; |
| 5748 | tmp_node = node; |
| 5749 | rc = resolve_when_unlink_nodes(sparent->parent, &tmp_node, &ctx_node, ctx_node_type, &unlinked_nodes); |
| 5750 | if (rc) { |
| 5751 | goto cleanup; |
| 5752 | } |
| 5753 | |
| 5754 | rc = lyxp_eval(((struct lys_node_augment *)sparent->parent)->when->cond, ctx_node, ctx_node_type, &set, LYXP_WHEN); |
| 5755 | |
| 5756 | /* reconnect nodes, if ctx_node is NULL then all the nodes were unlinked, but linked together, |
| 5757 | * so the tree did not actually change and there is nothing for us to do |
| 5758 | */ |
| 5759 | if (unlinked_nodes && ctx_node) { |
| 5760 | if (resolve_when_relink_nodes(ctx_node, unlinked_nodes, ctx_node_type)) { |
| 5761 | rc = -1; |
| 5762 | goto cleanup; |
| 5763 | } |
| 5764 | } |
| 5765 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5766 | if (rc) { |
| 5767 | if (rc == 1) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5768 | 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] | 5769 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5770 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5771 | } |
| 5772 | |
Michal Vasko | 944a564 | 2016-03-21 11:48:58 +0100 | [diff] [blame] | 5773 | lyxp_set_cast(&set, LYXP_SET_BOOLEAN, ctx_node, LYXP_WHEN); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5774 | |
Michal Vasko | 8146d4c | 2016-05-09 15:50:29 +0200 | [diff] [blame] | 5775 | if (!set.val.bool) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5776 | 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] | 5777 | node->when_status |= LYD_WHEN_FALSE; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5778 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5779 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5780 | |
| 5781 | /* free xpath set content */ |
| 5782 | lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node, 0); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5783 | } |
| 5784 | |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5785 | sparent = lys_parent(sparent); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5786 | } |
| 5787 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5788 | node->when_status |= LYD_WHEN_TRUE; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5789 | |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5790 | cleanup: |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5791 | /* free xpath set content */ |
| 5792 | lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node ? ctx_node : node, 0); |
| 5793 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5794 | if (result) { |
| 5795 | if (node->when_status & LYD_WHEN_TRUE) { |
| 5796 | *result = 1; |
| 5797 | } else { |
| 5798 | *result = 0; |
| 5799 | } |
| 5800 | } |
| 5801 | |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5802 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5803 | } |
| 5804 | |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 5805 | static int |
| 5806 | check_leafref_features(struct lys_type *type) |
| 5807 | { |
| 5808 | struct lys_node *iter; |
| 5809 | struct ly_set *src_parents, *trg_parents, *features; |
| 5810 | unsigned int i, j, size, x; |
| 5811 | int ret = EXIT_SUCCESS; |
| 5812 | |
| 5813 | assert(type->parent); |
| 5814 | |
| 5815 | src_parents = ly_set_new(); |
| 5816 | trg_parents = ly_set_new(); |
| 5817 | features = ly_set_new(); |
| 5818 | |
| 5819 | /* get parents chain of source (leafref) */ |
| 5820 | for (iter = (struct lys_node *)type->parent; iter; iter = iter->parent) { |
| 5821 | if (iter->nodetype & (LYS_INPUT | LYS_OUTPUT)) { |
| 5822 | continue; |
| 5823 | } |
| 5824 | ly_set_add(src_parents, iter, LY_SET_OPT_USEASLIST); |
| 5825 | } |
| 5826 | /* get parents chain of target */ |
| 5827 | for (iter = (struct lys_node *)type->info.lref.target; iter; iter = iter->parent) { |
| 5828 | if (iter->nodetype & (LYS_INPUT | LYS_OUTPUT)) { |
| 5829 | continue; |
| 5830 | } |
| 5831 | ly_set_add(trg_parents, iter, LY_SET_OPT_USEASLIST); |
| 5832 | } |
| 5833 | |
| 5834 | /* compare the features used in if-feature statements in the rest of both |
| 5835 | * chains of parents. The set of features used for target must be a subset |
| 5836 | * of features used for the leafref. This is not a perfect, we should compare |
| 5837 | * the truth tables but it could require too much resources, so we simplify that */ |
| 5838 | for (i = 0; i < src_parents->number; i++) { |
| 5839 | iter = src_parents->set.s[i]; /* shortcut */ |
| 5840 | if (!iter->iffeature_size) { |
| 5841 | continue; |
| 5842 | } |
| 5843 | for (j = 0; j < iter->iffeature_size; j++) { |
| 5844 | resolve_iffeature_getsizes(&iter->iffeature[j], NULL, &size); |
| 5845 | for (; size; size--) { |
| 5846 | if (!iter->iffeature[j].features[size - 1]) { |
| 5847 | /* not yet resolved feature, postpone this check */ |
| 5848 | ret = EXIT_FAILURE; |
| 5849 | goto cleanup; |
| 5850 | } |
| 5851 | ly_set_add(features, iter->iffeature[j].features[size - 1], 0); |
| 5852 | } |
| 5853 | } |
| 5854 | } |
| 5855 | x = features->number; |
| 5856 | for (i = 0; i < trg_parents->number; i++) { |
| 5857 | iter = trg_parents->set.s[i]; /* shortcut */ |
| 5858 | if (!iter->iffeature_size) { |
| 5859 | continue; |
| 5860 | } |
| 5861 | for (j = 0; j < iter->iffeature_size; j++) { |
| 5862 | resolve_iffeature_getsizes(&iter->iffeature[j], NULL, &size); |
| 5863 | for (; size; size--) { |
| 5864 | if (!iter->iffeature[j].features[size - 1]) { |
| 5865 | /* not yet resolved feature, postpone this check */ |
| 5866 | ret = EXIT_FAILURE; |
| 5867 | goto cleanup; |
| 5868 | } |
| 5869 | if ((unsigned int)ly_set_add(features, iter->iffeature[j].features[size - 1], 0) >= x) { |
| 5870 | /* the feature is not present in features set of target's parents chain */ |
| 5871 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, type->parent, "leafref", type->info.lref.path); |
| 5872 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, type->parent, |
| 5873 | "Leafref is not conditional based on \"%s\" feature as its target.", |
| 5874 | iter->iffeature[j].features[size - 1]->name); |
| 5875 | ret = -1; |
| 5876 | goto cleanup; |
| 5877 | } |
| 5878 | } |
| 5879 | } |
| 5880 | } |
| 5881 | |
| 5882 | cleanup: |
| 5883 | ly_set_free(features); |
| 5884 | ly_set_free(src_parents); |
| 5885 | ly_set_free(trg_parents); |
| 5886 | |
| 5887 | return ret; |
| 5888 | } |
| 5889 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5890 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5891 | * @brief Resolve a single unres schema item. Logs indirectly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5892 | * |
| 5893 | * @param[in] mod Main module. |
| 5894 | * @param[in] item Item to resolve. Type determined by \p type. |
| 5895 | * @param[in] type Type of the unresolved item. |
| 5896 | * @param[in] str_snode String, a schema node, or NULL. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5897 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5898 | * |
| 5899 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 5900 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5901 | static int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 5902 | 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] | 5903 | struct unres_schema *unres) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5904 | { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 5905 | /* 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] | 5906 | int rc = -1, has_str = 0, tpdf_flag = 0, i, k; |
| 5907 | unsigned int j; |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 5908 | struct lys_node *node, *par_grp; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 5909 | const char *expr; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 5910 | |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 5911 | struct ly_set *refs, *procs; |
| 5912 | struct lys_feature *ref, *feat; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 5913 | struct lys_ident *ident; |
| 5914 | struct lys_type *stype; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 5915 | struct lys_node_choice *choic; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 5916 | struct lyxml_elem *yin; |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 5917 | struct yang_type *yang; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 5918 | struct unres_list_uniq *unique_info; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 5919 | struct unres_iffeat_data *iff_data; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5920 | |
| 5921 | switch (type) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5922 | case UNRES_IDENT: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 5923 | expr = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 5924 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 5925 | ident = item; |
| 5926 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5927 | rc = resolve_base_ident(mod, ident, expr, "identity", NULL, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5928 | break; |
| 5929 | case UNRES_TYPE_IDENTREF: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 5930 | expr = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 5931 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 5932 | stype = item; |
| 5933 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5934 | rc = resolve_base_ident(mod, NULL, expr, "type", stype, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5935 | break; |
| 5936 | case UNRES_TYPE_LEAFREF: |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 5937 | node = str_snode; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 5938 | stype = item; |
| 5939 | |
Radek Krejci | 2f12f85 | 2016-01-08 12:59:57 +0100 | [diff] [blame] | 5940 | /* HACK - when there is no parent, we are in top level typedef and in that |
| 5941 | * case, the path has to contain absolute path, so we let the resolve_path_arg_schema() |
| 5942 | * know it via tpdf_flag */ |
| 5943 | if (!node) { |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 5944 | tpdf_flag = 1; |
Radek Krejci | 2f12f85 | 2016-01-08 12:59:57 +0100 | [diff] [blame] | 5945 | node = (struct lys_node *)stype->parent; |
| 5946 | } |
| 5947 | |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 5948 | if (!lys_node_module(node)->implemented) { |
| 5949 | /* not implemented module, don't bother with resolving the leafref |
| 5950 | * if the module is set to be implemented, tha path will be resolved then */ |
| 5951 | rc = 0; |
| 5952 | break; |
| 5953 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5954 | rc = resolve_path_arg_schema(stype->info.lref.path, node, tpdf_flag, |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 5955 | (const struct lys_node **)&stype->info.lref.target); |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 5956 | if (!tpdf_flag && !rc) { |
| 5957 | assert(stype->info.lref.target); |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 5958 | /* check if leafref and its target are under a common if-features */ |
| 5959 | rc = check_leafref_features(stype); |
| 5960 | if (rc) { |
| 5961 | break; |
| 5962 | } |
| 5963 | |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 5964 | /* store the backlink from leafref target */ |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 5965 | if (lys_leaf_add_leafref_target(stype->info.lref.target, (struct lys_node *)stype->parent)) { |
| 5966 | rc = -1; |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 5967 | } |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 5968 | } |
| 5969 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5970 | break; |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 5971 | case UNRES_TYPE_DER_TPDF: |
| 5972 | tpdf_flag = 1; |
| 5973 | /* no break */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5974 | case UNRES_TYPE_DER: |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 5975 | /* parent */ |
| 5976 | node = str_snode; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 5977 | stype = item; |
| 5978 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 5979 | /* HACK type->der is temporarily unparsed type statement */ |
| 5980 | yin = (struct lyxml_elem *)stype->der; |
| 5981 | stype->der = NULL; |
| 5982 | |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 5983 | if (yin->flags & LY_YANG_STRUCTURE_FLAG) { |
| 5984 | yang = (struct yang_type *)yin; |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 5985 | rc = yang_check_type(mod, node, yang, tpdf_flag, unres); |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 5986 | |
| 5987 | if (rc) { |
Pavol Vican | 8bd72e4 | 2016-08-29 09:53:05 +0200 | [diff] [blame] | 5988 | /* may try again later */ |
| 5989 | stype->der = (struct lys_tpdf *)yang; |
Pavol Vican | d01d8ae | 2016-03-01 10:45:59 +0100 | [diff] [blame] | 5990 | } else { |
| 5991 | /* 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] | 5992 | lydict_remove(mod->ctx, yang->name); |
Pavol Vican | d01d8ae | 2016-03-01 10:45:59 +0100 | [diff] [blame] | 5993 | free(yang); |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 5994 | } |
| 5995 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 5996 | } else { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 5997 | rc = fill_yin_type(mod, node, yin, stype, tpdf_flag, unres); |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 5998 | if (!rc) { |
| 5999 | /* we need to always be able to free this, it's safe only in this case */ |
| 6000 | lyxml_free(mod->ctx, yin); |
| 6001 | } else { |
| 6002 | /* may try again later, put all back how it was */ |
| 6003 | stype->der = (struct lys_tpdf *)yin; |
| 6004 | } |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6005 | } |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 6006 | if (rc == EXIT_SUCCESS) { |
Radek Krejci | 2c2fce8 | 2016-08-01 13:52:26 +0200 | [diff] [blame] | 6007 | /* it does not make sense to have leaf-list of empty type */ |
| 6008 | if (!tpdf_flag && node->nodetype == LYS_LEAFLIST && stype->base == LY_TYPE_EMPTY) { |
| 6009 | LOGWRN("The leaf-list \"%s\" is of \"empty\" type, which does not make sense.", node->name); |
| 6010 | } |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6011 | } else if (rc == EXIT_FAILURE && stype->base != LY_TYPE_ERR) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 6012 | /* forward reference - in case the type is in grouping, we have to make the grouping unusable |
| 6013 | * by uses statement until the type is resolved. We do that the same way as uses statements inside |
| 6014 | * grouping - the grouping's nacm member (not used un grouping) is used to increase the number of |
| 6015 | * 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] | 6016 | * 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] | 6017 | * of the type's base member. */ |
| 6018 | for (par_grp = node; par_grp && (par_grp->nodetype != LYS_GROUPING); par_grp = lys_parent(par_grp)); |
| 6019 | if (par_grp) { |
| 6020 | ((struct lys_node_grp *)par_grp)->nacm++; |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6021 | stype->base = LY_TYPE_ERR; |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 6022 | } |
Radek Krejci | 2c2fce8 | 2016-08-01 13:52:26 +0200 | [diff] [blame] | 6023 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6024 | break; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6025 | case UNRES_IFFEAT: |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6026 | iff_data = str_snode; |
| 6027 | 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] | 6028 | if (!rc) { |
| 6029 | /* success */ |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 6030 | if (iff_data->infeature) { |
| 6031 | /* store backlink into the target feature to allow reverse changes in case of changing feature status */ |
| 6032 | feat = *((struct lys_feature **)item); |
| 6033 | if (!feat->depfeatures) { |
| 6034 | feat->depfeatures = ly_set_new(); |
| 6035 | } |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 6036 | ly_set_add(feat->depfeatures, iff_data->node, LY_SET_OPT_USEASLIST); |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 6037 | } |
| 6038 | /* cleanup temporary data */ |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6039 | lydict_remove(mod->ctx, iff_data->fname); |
| 6040 | free(iff_data); |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6041 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6042 | break; |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6043 | case UNRES_FEATURE: |
| 6044 | feat = (struct lys_feature *)item; |
| 6045 | |
| 6046 | if (feat->iffeature_size) { |
| 6047 | refs = ly_set_new(); |
| 6048 | procs = ly_set_new(); |
| 6049 | ly_set_add(procs, feat, 0); |
| 6050 | |
| 6051 | while (procs->number) { |
| 6052 | ref = procs->set.g[procs->number - 1]; |
| 6053 | ly_set_rm_index(procs, procs->number - 1); |
| 6054 | |
| 6055 | for (i = 0; i < ref->iffeature_size; i++) { |
| 6056 | resolve_iffeature_getsizes(&ref->iffeature[i], NULL, &j); |
| 6057 | for (; j > 0 ; j--) { |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6058 | if (ref->iffeature[i].features[j - 1]) { |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6059 | if (ref->iffeature[i].features[j - 1] == feat) { |
| 6060 | LOGVAL(LYE_CIRC_FEATURES, LY_VLOG_NONE, NULL, feat->name); |
| 6061 | goto featurecheckdone; |
| 6062 | } |
| 6063 | |
| 6064 | if (ref->iffeature[i].features[j - 1]->iffeature_size) { |
| 6065 | k = refs->number; |
| 6066 | if (ly_set_add(refs, ref->iffeature[i].features[j - 1], 0) == k) { |
| 6067 | /* not yet seen feature, add it for processing */ |
| 6068 | ly_set_add(procs, ref->iffeature[i].features[j - 1], 0); |
| 6069 | } |
| 6070 | } |
| 6071 | } else { |
| 6072 | /* forward reference */ |
| 6073 | rc = EXIT_FAILURE; |
| 6074 | goto featurecheckdone; |
| 6075 | } |
| 6076 | } |
| 6077 | |
| 6078 | } |
| 6079 | } |
| 6080 | rc = EXIT_SUCCESS; |
| 6081 | |
| 6082 | featurecheckdone: |
| 6083 | ly_set_free(refs); |
| 6084 | ly_set_free(procs); |
| 6085 | } |
| 6086 | |
| 6087 | break; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6088 | case UNRES_USES: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6089 | rc = resolve_unres_schema_uses(item, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6090 | break; |
| 6091 | case UNRES_TYPE_DFLT: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6092 | expr = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 6093 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6094 | stype = item; |
| 6095 | |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6096 | rc = check_default(stype, expr, mod); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6097 | break; |
| 6098 | case UNRES_CHOICE_DFLT: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6099 | expr = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 6100 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6101 | choic = item; |
| 6102 | |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 6103 | if (!choic->dflt) { |
| 6104 | choic->dflt = resolve_choice_dflt(choic, expr); |
| 6105 | } |
Michal Vasko | 7955b36 | 2015-09-04 14:18:15 +0200 | [diff] [blame] | 6106 | if (choic->dflt) { |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 6107 | rc = lyp_check_mandatory_choice((struct lys_node *)choic); |
Michal Vasko | 7955b36 | 2015-09-04 14:18:15 +0200 | [diff] [blame] | 6108 | } else { |
| 6109 | rc = EXIT_FAILURE; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6110 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6111 | break; |
| 6112 | case UNRES_LIST_KEYS: |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 6113 | has_str = 1; |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6114 | rc = resolve_list_keys(item, str_snode); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6115 | break; |
| 6116 | case UNRES_LIST_UNIQ: |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6117 | unique_info = (struct unres_list_uniq *)item; |
| 6118 | 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] | 6119 | break; |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 6120 | case UNRES_AUGMENT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6121 | rc = resolve_augment(item, NULL); |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 6122 | break; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6123 | case UNRES_XPATH: |
| 6124 | node = (struct lys_node *)item; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 6125 | rc = check_node_xpath(node); |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6126 | break; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6127 | default: |
| 6128 | LOGINT; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6129 | break; |
| 6130 | } |
| 6131 | |
Radek Krejci | 54081ce | 2016-08-12 15:21:47 +0200 | [diff] [blame] | 6132 | if (has_str && !rc) { |
| 6133 | /* the string is no more needed in case of success. |
| 6134 | * 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] | 6135 | lydict_remove(mod->ctx, str_snode); |
| 6136 | } |
| 6137 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6138 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6139 | } |
| 6140 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6141 | /* logs directly */ |
| 6142 | static void |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6143 | 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] | 6144 | { |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 6145 | struct lyxml_elem *xml; |
| 6146 | struct lyxml_attr *attr; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6147 | struct unres_iffeat_data *iff_data; |
Radek Krejci | 76e15e1 | 2016-06-22 11:02:24 +0200 | [diff] [blame] | 6148 | const char *type_name = NULL; |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 6149 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6150 | switch (type) { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6151 | case UNRES_IDENT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6152 | 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] | 6153 | break; |
| 6154 | case UNRES_TYPE_IDENTREF: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6155 | 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] | 6156 | break; |
| 6157 | case UNRES_TYPE_LEAFREF: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6158 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "leafref", |
| 6159 | ((struct lys_type *)item)->info.lref.path); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6160 | break; |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 6161 | case UNRES_TYPE_DER_TPDF: |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6162 | case UNRES_TYPE_DER: |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 6163 | xml = (struct lyxml_elem *)((struct lys_type *)item)->der; |
| 6164 | if (xml->flags & LY_YANG_STRUCTURE_FLAG) { |
| 6165 | type_name = ((struct yang_type *)xml)->name; |
| 6166 | } else { |
| 6167 | LY_TREE_FOR(xml->attr, attr) { |
| 6168 | if ((attr->type == LYXML_ATTR_STD) && !strcmp(attr->name, "name")) { |
| 6169 | type_name = attr->value; |
| 6170 | break; |
| 6171 | } |
| 6172 | } |
| 6173 | assert(attr); |
| 6174 | } |
| 6175 | 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] | 6176 | break; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6177 | case UNRES_IFFEAT: |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6178 | iff_data = str_node; |
| 6179 | 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] | 6180 | break; |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6181 | case UNRES_FEATURE: |
| 6182 | LOGVRB("There are unresolved if-features for \"%s\" feature circular dependency check, it will be attempted later", |
| 6183 | ((struct lys_feature *)item)->name); |
| 6184 | break; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6185 | case UNRES_USES: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6186 | 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] | 6187 | break; |
| 6188 | case UNRES_TYPE_DFLT: |
Radek Krejci | 2e2de83 | 2016-10-13 16:12:26 +0200 | [diff] [blame] | 6189 | if (str_node) { |
| 6190 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "type default", (char *)str_node); |
| 6191 | } /* else no default value in the type itself, but we are checking some restrictions against |
| 6192 | * possible default value of some base type. The failure is caused by not resolved base type, |
| 6193 | * so it was already reported */ |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6194 | break; |
| 6195 | case UNRES_CHOICE_DFLT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6196 | 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] | 6197 | break; |
| 6198 | case UNRES_LIST_KEYS: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6199 | 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] | 6200 | break; |
| 6201 | case UNRES_LIST_UNIQ: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6202 | 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] | 6203 | break; |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 6204 | case UNRES_AUGMENT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6205 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "augment target", |
| 6206 | ((struct lys_node_augment *)item)->target_name); |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 6207 | break; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6208 | case UNRES_XPATH: |
| 6209 | LOGVRB("Resolving %s \"%s\" with the context node \"%s\" failed, it will be attempted later.", "XPath", |
| 6210 | (char *)str_node, ((struct lys_node *)item)->name); |
| 6211 | break; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6212 | default: |
| 6213 | LOGINT; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6214 | break; |
| 6215 | } |
| 6216 | } |
| 6217 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6218 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6219 | * @brief Resolve every unres schema item in the structure. Logs directly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6220 | * |
| 6221 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6222 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6223 | * |
Michal Vasko | 92b8a38 | 2015-08-19 14:03:49 +0200 | [diff] [blame] | 6224 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6225 | */ |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6226 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 6227 | resolve_unres_schema(struct lys_module *mod, struct unres_schema *unres) |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6228 | { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6229 | uint32_t i, resolved = 0, unres_count, res_count; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6230 | int rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6231 | |
| 6232 | assert(unres); |
| 6233 | |
Michal Vasko | e873426 | 2016-09-29 14:12:06 +0200 | [diff] [blame] | 6234 | LOGVRB("Resolving \"%s\" unresolved schema nodes and their constraints...", mod->name); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6235 | ly_vlog_hide(1); |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6236 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6237 | /* uses */ |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6238 | do { |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6239 | unres_count = 0; |
| 6240 | res_count = 0; |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6241 | |
| 6242 | for (i = 0; i < unres->count; ++i) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6243 | /* UNRES_TYPE_LEAFREF must be resolved (for storing leafref target pointers); |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6244 | * if-features are resolved here to make sure that we will have all if-features for |
| 6245 | * later check of feature circular dependency */ |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6246 | if (unres->type[i] > UNRES_IDENT) { |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6247 | continue; |
| 6248 | } |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6249 | /* 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] | 6250 | * UNRES_CHOICE_DFLT and UNRES_IDENT */ |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6251 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6252 | ++unres_count; |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6253 | 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] | 6254 | if (!rc) { |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6255 | unres->type[i] = UNRES_RESOLVED; |
| 6256 | ++resolved; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6257 | ++res_count; |
Michal Vasko | 89e1532 | 2015-08-17 15:46:55 +0200 | [diff] [blame] | 6258 | } else if (rc == -1) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6259 | ly_vlog_hide(0); |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6260 | /* print the error */ |
| 6261 | 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] | 6262 | return -1; |
Radek Krejci | c2a180f | 2016-06-22 13:28:16 +0200 | [diff] [blame] | 6263 | } else { |
| 6264 | /* forward reference, erase ly_errno */ |
| 6265 | ly_errno = LY_SUCCESS; |
| 6266 | ly_vecode = LYVE_SUCCESS; |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6267 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6268 | } |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6269 | } while (res_count && (res_count < unres_count)); |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6270 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6271 | if (res_count < unres_count) { |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6272 | /* just print the errors */ |
| 6273 | ly_vlog_hide(0); |
| 6274 | |
| 6275 | for (i = 0; i < unres->count; ++i) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6276 | if (unres->type[i] > UNRES_IDENT) { |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6277 | continue; |
| 6278 | } |
| 6279 | resolve_unres_schema_item(mod, unres->item[i], unres->type[i], unres->str_snode[i], unres); |
| 6280 | } |
Michal Vasko | 92b8a38 | 2015-08-19 14:03:49 +0200 | [diff] [blame] | 6281 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6282 | } |
| 6283 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6284 | /* the rest */ |
| 6285 | for (i = 0; i < unres->count; ++i) { |
| 6286 | if (unres->type[i] == UNRES_RESOLVED) { |
| 6287 | continue; |
| 6288 | } |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 6289 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6290 | 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] | 6291 | if (rc == 0) { |
Pavol Vican | 88e16c9 | 2016-09-07 15:41:50 +0200 | [diff] [blame] | 6292 | if (unres->type[i] == UNRES_LIST_UNIQ) { |
| 6293 | /* free the allocated structure */ |
| 6294 | free(unres->item[i]); |
| 6295 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6296 | unres->type[i] = UNRES_RESOLVED; |
| 6297 | ++resolved; |
| 6298 | } else if (rc == -1) { |
| 6299 | ly_vlog_hide(0); |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6300 | /* print the error */ |
| 6301 | resolve_unres_schema_item(mod, unres->item[i], unres->type[i], unres->str_snode[i], unres); |
| 6302 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6303 | } |
| 6304 | } |
| 6305 | |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6306 | ly_vlog_hide(0); |
| 6307 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6308 | if (resolved < unres->count) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6309 | /* try to resolve the unresolved nodes again, it will not resolve anything, but it will print |
| 6310 | * all the validation errors |
| 6311 | */ |
| 6312 | for (i = 0; i < unres->count; ++i) { |
| 6313 | if (unres->type[i] == UNRES_RESOLVED) { |
| 6314 | continue; |
| 6315 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6316 | 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] | 6317 | } |
Michal Vasko | 92b8a38 | 2015-08-19 14:03:49 +0200 | [diff] [blame] | 6318 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6319 | } |
| 6320 | |
Michal Vasko | e873426 | 2016-09-29 14:12:06 +0200 | [diff] [blame] | 6321 | LOGVRB("All \"%s\" schema nodes and constraints resolved.", mod->name); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6322 | unres->count = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6323 | return EXIT_SUCCESS; |
| 6324 | } |
| 6325 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6326 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6327 | * @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] | 6328 | * |
| 6329 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6330 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6331 | * @param[in] item Item to resolve. Type determined by \p type. |
| 6332 | * @param[in] type Type of the unresolved item. |
| 6333 | * @param[in] str String argument. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6334 | * |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 6335 | * @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] | 6336 | */ |
| 6337 | int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6338 | unres_schema_add_str(struct lys_module *mod, struct unres_schema *unres, void *item, enum UNRES_ITEM type, |
| 6339 | const char *str) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6340 | { |
Radek Krejci | 54081ce | 2016-08-12 15:21:47 +0200 | [diff] [blame] | 6341 | int rc; |
| 6342 | const char *dictstr; |
| 6343 | |
| 6344 | dictstr = lydict_insert(mod->ctx, str, 0); |
| 6345 | rc = unres_schema_add_node(mod, unres, item, type, (struct lys_node *)dictstr); |
| 6346 | |
| 6347 | if (rc == -1) { |
| 6348 | lydict_remove(mod->ctx, dictstr); |
| 6349 | } |
| 6350 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6351 | } |
| 6352 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6353 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6354 | * @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] | 6355 | * |
| 6356 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6357 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6358 | * @param[in] item Item to resolve. Type determined by \p type. |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6359 | * @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] | 6360 | * @param[in] snode Schema node argument. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6361 | * |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 6362 | * @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] | 6363 | */ |
| 6364 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 6365 | 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] | 6366 | struct lys_node *snode) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6367 | { |
Michal Vasko | ef486d7 | 2016-09-27 12:10:44 +0200 | [diff] [blame] | 6368 | int rc, log_hidden; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6369 | struct lyxml_elem *yin; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6370 | char *path, *msg; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6371 | |
Michal Vasko | 9bf425b | 2015-10-22 11:42:03 +0200 | [diff] [blame] | 6372 | assert(unres && item && ((type != UNRES_LEAFREF) && (type != UNRES_INSTID) && (type != UNRES_WHEN) |
| 6373 | && (type != UNRES_MUST))); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6374 | |
Michal Vasko | ef486d7 | 2016-09-27 12:10:44 +0200 | [diff] [blame] | 6375 | if (*ly_vlog_hide_location()) { |
| 6376 | log_hidden = 1; |
| 6377 | } else { |
| 6378 | log_hidden = 0; |
| 6379 | ly_vlog_hide(1); |
| 6380 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6381 | rc = resolve_unres_schema_item(mod, item, type, snode, unres); |
Michal Vasko | ef486d7 | 2016-09-27 12:10:44 +0200 | [diff] [blame] | 6382 | if (!log_hidden) { |
| 6383 | ly_vlog_hide(0); |
| 6384 | } |
| 6385 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6386 | if (rc != EXIT_FAILURE) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6387 | if (rc == -1 && ly_errno == LY_EVALID) { |
Radek Krejci | 76e15e1 | 2016-06-22 11:02:24 +0200 | [diff] [blame] | 6388 | if (ly_log_level >= LY_LLERR) { |
| 6389 | path = strdup(ly_errpath()); |
| 6390 | msg = strdup(ly_errmsg()); |
| 6391 | LOGERR(LY_EVALID, "%s%s%s%s", msg, path[0] ? " (path: " : "", path[0] ? path : "", path[0] ? ")" : ""); |
| 6392 | free(path); |
| 6393 | free(msg); |
| 6394 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6395 | } |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6396 | if (type == UNRES_LIST_UNIQ) { |
| 6397 | /* free the allocated structure */ |
| 6398 | free(item); |
Pavol Vican | 88e16c9 | 2016-09-07 15:41:50 +0200 | [diff] [blame] | 6399 | } else if (rc == -1 && type == UNRES_IFFEAT) { |
| 6400 | /* free the allocated resources */ |
| 6401 | free(*((char **)item)); |
| 6402 | } |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6403 | return rc; |
Radek Krejci | f347abc | 2016-06-22 10:18:47 +0200 | [diff] [blame] | 6404 | } else { |
| 6405 | /* erase info about validation errors */ |
| 6406 | ly_errno = LY_SUCCESS; |
| 6407 | ly_vecode = LYVE_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6408 | } |
| 6409 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6410 | print_unres_schema_item_fail(item, type, snode); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6411 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6412 | /* 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] | 6413 | if (type == UNRES_TYPE_DER || type == UNRES_TYPE_DER_TPDF) { |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6414 | yin = (struct lyxml_elem *)((struct lys_type *)item)->der; |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6415 | if (!(yin->flags & LY_YANG_STRUCTURE_FLAG)) { |
| 6416 | lyxml_unlink_elem(mod->ctx, yin, 1); |
| 6417 | ((struct lys_type *)item)->der = (struct lys_tpdf *)yin; |
| 6418 | } |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6419 | } |
| 6420 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6421 | unres->count++; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 6422 | unres->item = ly_realloc(unres->item, unres->count*sizeof *unres->item); |
| 6423 | if (!unres->item) { |
| 6424 | LOGMEM; |
| 6425 | return -1; |
| 6426 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6427 | unres->item[unres->count-1] = item; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 6428 | unres->type = ly_realloc(unres->type, unres->count*sizeof *unres->type); |
| 6429 | if (!unres->type) { |
| 6430 | LOGMEM; |
| 6431 | return -1; |
| 6432 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6433 | unres->type[unres->count-1] = type; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 6434 | unres->str_snode = ly_realloc(unres->str_snode, unres->count*sizeof *unres->str_snode); |
| 6435 | if (!unres->str_snode) { |
| 6436 | LOGMEM; |
| 6437 | return -1; |
| 6438 | } |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6439 | unres->str_snode[unres->count-1] = snode; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6440 | unres->module = ly_realloc(unres->module, unres->count*sizeof *unres->module); |
| 6441 | if (!unres->module) { |
| 6442 | LOGMEM; |
| 6443 | return -1; |
| 6444 | } |
| 6445 | unres->module[unres->count-1] = mod; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6446 | |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 6447 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6448 | } |
| 6449 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6450 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6451 | * @brief Duplicate an unres schema item. Logs indirectly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6452 | * |
| 6453 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6454 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6455 | * @param[in] item Old item to be resolved. |
| 6456 | * @param[in] type Type of the old unresolved item. |
| 6457 | * @param[in] new_item New item to use in the duplicate. |
| 6458 | * |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 6459 | * @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] | 6460 | */ |
Michal Vasko | dad1940 | 2015-08-06 09:51:53 +0200 | [diff] [blame] | 6461 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 6462 | 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] | 6463 | { |
| 6464 | int i; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6465 | struct unres_list_uniq aux_uniq; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6466 | struct unres_iffeat_data *iff_data; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6467 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6468 | 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] | 6469 | |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6470 | /* hack for UNRES_LIST_UNIQ, which stores multiple items behind its item */ |
| 6471 | if (type == UNRES_LIST_UNIQ) { |
| 6472 | aux_uniq.list = item; |
| 6473 | aux_uniq.expr = ((struct unres_list_uniq *)new_item)->expr; |
| 6474 | item = &aux_uniq; |
| 6475 | } |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 6476 | i = unres_schema_find(unres, -1, item, type); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6477 | |
| 6478 | if (i == -1) { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6479 | if (type == UNRES_LIST_UNIQ) { |
| 6480 | free(new_item); |
| 6481 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 6482 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6483 | } |
| 6484 | |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6485 | if ((type == UNRES_TYPE_LEAFREF) || (type == UNRES_USES) || (type == UNRES_TYPE_DFLT) || |
Radek Krejci | b69f323 | 2016-09-16 17:41:07 +0200 | [diff] [blame] | 6486 | (type == UNRES_FEATURE) || (type == UNRES_LIST_UNIQ)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6487 | 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] | 6488 | LOGINT; |
| 6489 | return -1; |
| 6490 | } |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6491 | } else if (type == UNRES_IFFEAT) { |
| 6492 | /* duplicate unres_iffeature_data */ |
| 6493 | iff_data = malloc(sizeof *iff_data); |
| 6494 | iff_data->fname = lydict_insert(mod->ctx, ((struct unres_iffeat_data *)unres->str_snode[i])->fname, 0); |
| 6495 | iff_data->node = ((struct unres_iffeat_data *)unres->str_snode[i])->node; |
| 6496 | if (unres_schema_add_node(mod, unres, new_item, type, (struct lys_node *)iff_data) == -1) { |
| 6497 | LOGINT; |
| 6498 | return -1; |
| 6499 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6500 | } else { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6501 | 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] | 6502 | LOGINT; |
| 6503 | return -1; |
| 6504 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6505 | } |
Michal Vasko | dad1940 | 2015-08-06 09:51:53 +0200 | [diff] [blame] | 6506 | |
| 6507 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6508 | } |
| 6509 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6510 | /* does not log */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6511 | int |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 6512 | 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] | 6513 | { |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 6514 | int i; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6515 | struct unres_list_uniq *aux_uniq1, *aux_uniq2; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6516 | |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 6517 | if (start_on_backwards > 0) { |
| 6518 | i = start_on_backwards; |
| 6519 | } else { |
| 6520 | i = unres->count - 1; |
| 6521 | } |
| 6522 | for (; i > -1; i--) { |
| 6523 | if (unres->type[i] != type) { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6524 | continue; |
| 6525 | } |
| 6526 | if (type != UNRES_LIST_UNIQ) { |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 6527 | if (unres->item[i] == item) { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6528 | break; |
| 6529 | } |
| 6530 | } else { |
| 6531 | aux_uniq1 = (struct unres_list_uniq *)unres->item[i - 1]; |
| 6532 | aux_uniq2 = (struct unres_list_uniq *)item; |
| 6533 | 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] | 6534 | break; |
| 6535 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6536 | } |
| 6537 | } |
| 6538 | |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 6539 | return i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6540 | } |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6541 | |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 6542 | static void |
| 6543 | unres_schema_free_item(struct ly_ctx *ctx, struct unres_schema *unres, uint32_t i) |
| 6544 | { |
| 6545 | struct lyxml_elem *yin; |
| 6546 | struct yang_type *yang; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6547 | struct unres_iffeat_data *iff_data; |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 6548 | |
| 6549 | switch (unres->type[i]) { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 6550 | case UNRES_TYPE_DER_TPDF: |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 6551 | case UNRES_TYPE_DER: |
| 6552 | yin = (struct lyxml_elem *)((struct lys_type *)unres->item[i])->der; |
| 6553 | if (yin->flags & LY_YANG_STRUCTURE_FLAG) { |
| 6554 | yang =(struct yang_type *)yin; |
| 6555 | yang->type->base = yang->base; |
| 6556 | lydict_remove(ctx, yang->name); |
| 6557 | free(yang); |
| 6558 | } else { |
| 6559 | lyxml_free(ctx, yin); |
| 6560 | } |
| 6561 | break; |
Pavol Vican | 88e16c9 | 2016-09-07 15:41:50 +0200 | [diff] [blame] | 6562 | case UNRES_IFFEAT: |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6563 | iff_data = (struct unres_iffeat_data *)unres->str_snode[i]; |
| 6564 | lydict_remove(ctx, iff_data->fname); |
| 6565 | free(unres->str_snode[i]); |
Pavol Vican | 88e16c9 | 2016-09-07 15:41:50 +0200 | [diff] [blame] | 6566 | break; |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 6567 | case UNRES_IDENT: |
| 6568 | case UNRES_TYPE_IDENTREF: |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 6569 | case UNRES_TYPE_DFLT: |
| 6570 | case UNRES_CHOICE_DFLT: |
| 6571 | case UNRES_LIST_KEYS: |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 6572 | lydict_remove(ctx, (const char *)unres->str_snode[i]); |
| 6573 | break; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6574 | case UNRES_LIST_UNIQ: |
| 6575 | free(unres->item[i]); |
| 6576 | break; |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 6577 | default: |
| 6578 | break; |
| 6579 | } |
| 6580 | unres->type[i] = UNRES_RESOLVED; |
| 6581 | } |
| 6582 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6583 | void |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6584 | unres_schema_free(struct lys_module *module, struct unres_schema **unres) |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6585 | { |
| 6586 | uint32_t i; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6587 | unsigned int unresolved = 0; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6588 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6589 | if (!unres || !(*unres)) { |
| 6590 | return; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6591 | } |
| 6592 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6593 | assert(module || (*unres)->count == 0); |
| 6594 | |
| 6595 | for (i = 0; i < (*unres)->count; ++i) { |
| 6596 | if ((*unres)->module[i] != module) { |
| 6597 | if ((*unres)->type[i] != UNRES_RESOLVED) { |
| 6598 | unresolved++; |
| 6599 | } |
| 6600 | continue; |
| 6601 | } |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 6602 | |
| 6603 | /* free heap memory for the specific item */ |
| 6604 | unres_schema_free_item(module->ctx, *unres, i); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6605 | } |
| 6606 | |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 6607 | /* free it all */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6608 | if (!module || (!unresolved && !module->type)) { |
| 6609 | free((*unres)->item); |
| 6610 | free((*unres)->type); |
| 6611 | free((*unres)->str_snode); |
| 6612 | free((*unres)->module); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6613 | free((*unres)); |
| 6614 | (*unres) = NULL; |
| 6615 | } |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6616 | } |
| 6617 | |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 6618 | static int |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6619 | resolve_leafref(struct lyd_node_leaf_list *leaf, struct lys_type *type) |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 6620 | { |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 6621 | struct unres_data matches; |
| 6622 | uint32_t i; |
| 6623 | |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6624 | assert(type->base == LY_TYPE_LEAFREF); |
| 6625 | |
| 6626 | /* init */ |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 6627 | memset(&matches, 0, sizeof matches); |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 6628 | |
| 6629 | /* EXIT_FAILURE return keeps leaf->value.lefref NULL, handled later */ |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6630 | 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] | 6631 | return -1; |
| 6632 | } |
| 6633 | |
| 6634 | /* check that value matches */ |
| 6635 | for (i = 0; i < matches.count; ++i) { |
| 6636 | if (ly_strequal(leaf->value_str, ((struct lyd_node_leaf_list *)matches.node[i])->value_str, 1)) { |
| 6637 | leaf->value.leafref = matches.node[i]; |
| 6638 | break; |
| 6639 | } |
| 6640 | } |
| 6641 | |
| 6642 | free(matches.node); |
| 6643 | |
| 6644 | if (!leaf->value.leafref) { |
| 6645 | /* reference not found */ |
| 6646 | if (type->info.lref.req > -1) { |
| 6647 | LOGVAL(LYE_NOLEAFREF, LY_VLOG_LYD, leaf, type->info.lref.path, leaf->value_str); |
| 6648 | return EXIT_FAILURE; |
| 6649 | } else { |
| 6650 | LOGVRB("There is no leafref with the value \"%s\", but it is not required.", leaf->value_str); |
| 6651 | } |
| 6652 | } |
| 6653 | |
| 6654 | return EXIT_SUCCESS; |
| 6655 | } |
| 6656 | |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6657 | API LY_DATA_TYPE |
| 6658 | lyd_leaf_type(const struct lyd_node_leaf_list *leaf) |
| 6659 | { |
| 6660 | struct lyd_node *node; |
| 6661 | struct lys_type *type, *type_iter; |
| 6662 | lyd_val value; |
| 6663 | int f = 0, r; |
| 6664 | |
| 6665 | if (!leaf || !(leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 6666 | return LY_TYPE_ERR; |
| 6667 | } |
| 6668 | |
| 6669 | if (leaf->value_type > 0 && (leaf->value_type & LY_DATA_TYPE_MASK) != LY_TYPE_UNION && |
| 6670 | (leaf->value_type & LY_DATA_TYPE_MASK) != LY_TYPE_LEAFREF) { |
| 6671 | /* we can get the type directly from the data node (it was already resolved) */ |
| 6672 | return leaf->value_type & LY_DATA_TYPE_MASK; |
| 6673 | } |
| 6674 | |
| 6675 | /* init */ |
| 6676 | type = &((struct lys_node_leaf *)leaf->schema)->type; |
| 6677 | value = leaf->value; |
| 6678 | ly_vlog_hide(1); |
| 6679 | |
| 6680 | /* resolve until we get the real data type */ |
| 6681 | while (1) { |
| 6682 | /* get the correct data type from schema */ |
| 6683 | switch (type->base) { |
| 6684 | case LY_TYPE_LEAFREF: |
| 6685 | type = &type->info.lref.target->type; |
| 6686 | break; /* continue in while loop */ |
| 6687 | case LY_TYPE_UNION: |
| 6688 | type_iter = NULL; |
| 6689 | while ((type_iter = lyp_get_next_union_type(type, type_iter, &f))) { |
| 6690 | if (type_iter->base == LY_TYPE_LEAFREF) { |
| 6691 | if (type_iter->info.lref.req == -1) { |
| 6692 | /* target not required, so it always succeeds */ |
| 6693 | break; |
| 6694 | } else { |
| 6695 | /* try to resolve leafref */ |
| 6696 | memset(&((struct lyd_node_leaf_list *)leaf)->value, 0, sizeof leaf->value); |
| 6697 | r = resolve_leafref((struct lyd_node_leaf_list *)leaf, type_iter); |
| 6698 | /* revert leaf's content affected by resolve_leafref */ |
| 6699 | ((struct lyd_node_leaf_list *)leaf)->value = value; |
| 6700 | if (!r) { |
| 6701 | /* success, we can continue with the leafref type */ |
| 6702 | break; |
| 6703 | } |
| 6704 | } |
| 6705 | } else if (type_iter->base == LY_TYPE_INST) { |
| 6706 | if (type_iter->info.inst.req == -1) { |
| 6707 | /* target not required, so it always succeeds */ |
| 6708 | return LY_TYPE_INST; |
| 6709 | } else { |
| 6710 | /* try to resolve instance-identifier */ |
| 6711 | ly_errno = 0; |
| 6712 | node = resolve_instid((struct lyd_node *)leaf, leaf->value_str); |
| 6713 | if (!ly_errno && node) { |
| 6714 | /* the real type is instance-identifier */ |
| 6715 | return LY_TYPE_INST; |
| 6716 | } |
| 6717 | } |
| 6718 | } else { |
| 6719 | r = lyp_parse_value_type((struct lyd_node_leaf_list *)leaf, type_iter, 1); |
| 6720 | /* revert leaf's content affected by resolve_leafref */ |
| 6721 | ((struct lyd_node_leaf_list *)leaf)->value = value; |
| 6722 | if (!r) { |
| 6723 | /* we have the real type */ |
| 6724 | return type_iter->base; |
| 6725 | } |
| 6726 | } |
| 6727 | f = 0; |
| 6728 | } |
| 6729 | /* erase ly_errno and ly_vecode */ |
| 6730 | ly_errno = LY_SUCCESS; |
| 6731 | ly_vecode = LYVE_SUCCESS; |
| 6732 | |
| 6733 | if (!type_iter) { |
| 6734 | LOGERR(LY_EINVAL, "Unable to get type from union \"%s\" with no valid type.", type->parent->name) |
| 6735 | return LY_TYPE_ERR; |
| 6736 | } |
| 6737 | type = type_iter; |
| 6738 | break; |
| 6739 | default: |
| 6740 | /* we have the real type */ |
| 6741 | ly_vlog_hide(0); |
| 6742 | return type->base; |
| 6743 | } |
| 6744 | } |
| 6745 | |
| 6746 | ly_vlog_hide(0); |
| 6747 | return LY_TYPE_ERR; |
| 6748 | } |
| 6749 | |
| 6750 | static int |
| 6751 | resolve_union(struct lyd_node_leaf_list *leaf, struct lys_type *type) |
| 6752 | { |
| 6753 | struct lys_type *datatype = NULL; |
| 6754 | int f = 0; |
| 6755 | |
| 6756 | assert(type->base == LY_TYPE_UNION); |
| 6757 | |
| 6758 | memset(&leaf->value, 0, sizeof leaf->value); |
| 6759 | while ((datatype = lyp_get_next_union_type(type, datatype, &f))) { |
| 6760 | leaf->value_type = datatype->base; |
| 6761 | |
| 6762 | if (datatype->base == LY_TYPE_LEAFREF) { |
| 6763 | /* try to resolve leafref */ |
| 6764 | if (!resolve_leafref(leaf, datatype)) { |
| 6765 | /* success */ |
| 6766 | break; |
| 6767 | } |
| 6768 | } else if (datatype->base == LY_TYPE_INST) { |
| 6769 | /* try to resolve instance-identifier */ |
| 6770 | ly_errno = 0; |
| 6771 | leaf->value.instance = resolve_instid((struct lyd_node *)leaf, leaf->value_str); |
| 6772 | if (!ly_errno && (leaf->value.instance || datatype->info.inst.req == -1)) { |
| 6773 | /* success */ |
| 6774 | break; |
| 6775 | } |
| 6776 | } else { |
| 6777 | if (!lyp_parse_value_type(leaf, datatype, 1)) { |
| 6778 | /* success */ |
| 6779 | break; |
| 6780 | } |
| 6781 | } |
| 6782 | f = 0; |
| 6783 | } |
| 6784 | /* erase ly_errno and ly_vecode */ |
| 6785 | ly_errno = LY_SUCCESS; |
| 6786 | ly_vecode = LYVE_SUCCESS; |
| 6787 | |
| 6788 | if (!datatype) { |
| 6789 | /* failure */ |
| 6790 | LOGVAL(LYE_INVAL, LY_VLOG_LYD, leaf, (leaf->value_str ? leaf->value_str : ""), leaf->schema->name); |
| 6791 | return EXIT_FAILURE; |
| 6792 | } |
| 6793 | |
| 6794 | return EXIT_SUCCESS; |
| 6795 | } |
| 6796 | |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6797 | /** |
| 6798 | * @brief Resolve a single unres data item. Logs directly. |
| 6799 | * |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6800 | * @param[in] node Data node to resolve. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6801 | * @param[in] type Type of the unresolved item. |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6802 | * |
| 6803 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 6804 | */ |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 6805 | int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6806 | resolve_unres_data_item(struct lyd_node *node, enum UNRES_ITEM type) |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6807 | { |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 6808 | int rc; |
Michal Vasko | 83a6c46 | 2015-10-08 16:43:53 +0200 | [diff] [blame] | 6809 | struct lyd_node_leaf_list *leaf; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6810 | struct lys_node_leaf *sleaf; |
Michal Vasko | c428084 | 2016-04-19 16:10:42 +0200 | [diff] [blame] | 6811 | struct lyd_node *parent; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6812 | |
Michal Vasko | 83a6c46 | 2015-10-08 16:43:53 +0200 | [diff] [blame] | 6813 | leaf = (struct lyd_node_leaf_list *)node; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6814 | sleaf = (struct lys_node_leaf *)leaf->schema; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6815 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6816 | switch (type) { |
| 6817 | case UNRES_LEAFREF: |
| 6818 | assert(sleaf->type.base == LY_TYPE_LEAFREF); |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6819 | return resolve_leafref(leaf, &sleaf->type); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6820 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6821 | case UNRES_INSTID: |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 6822 | assert(sleaf->type.base == LY_TYPE_INST); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6823 | ly_errno = 0; |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6824 | leaf->value.instance = resolve_instid(node, leaf->value_str); |
Radek Krejci | 40f17b9 | 2016-02-03 14:30:43 +0100 | [diff] [blame] | 6825 | if (!leaf->value.instance) { |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6826 | if (ly_errno) { |
| 6827 | return -1; |
| 6828 | } else if (sleaf->type.info.inst.req > -1) { |
Michal Vasko | 6ac6828 | 2016-04-11 10:56:47 +0200 | [diff] [blame] | 6829 | LOGVAL(LYE_NOREQINS, LY_VLOG_LYD, leaf, leaf->value_str); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6830 | return EXIT_FAILURE; |
| 6831 | } else { |
Michal Vasko | 9925e28 | 2016-09-02 12:45:14 +0200 | [diff] [blame] | 6832 | 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] | 6833 | } |
| 6834 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6835 | break; |
| 6836 | |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 6837 | case UNRES_UNION: |
| 6838 | assert(sleaf->type.base == LY_TYPE_UNION); |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6839 | return resolve_union(leaf, &sleaf->type); |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 6840 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6841 | case UNRES_WHEN: |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 6842 | if ((rc = resolve_when(node, NULL))) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6843 | return rc; |
| 6844 | } |
| 6845 | break; |
| 6846 | |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 6847 | case UNRES_MUST: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6848 | if ((rc = resolve_must(node, 0))) { |
| 6849 | return rc; |
| 6850 | } |
| 6851 | break; |
| 6852 | |
| 6853 | case UNRES_MUST_INOUT: |
| 6854 | if ((rc = resolve_must(node, 1))) { |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 6855 | return rc; |
| 6856 | } |
| 6857 | break; |
| 6858 | |
Michal Vasko | c428084 | 2016-04-19 16:10:42 +0200 | [diff] [blame] | 6859 | case UNRES_EMPTYCONT: |
| 6860 | do { |
| 6861 | parent = node->parent; |
| 6862 | lyd_free(node); |
| 6863 | node = parent; |
| 6864 | } while (node && (node->schema->nodetype == LYS_CONTAINER) && !node->child |
| 6865 | && !((struct lys_node_container *)node->schema)->presence); |
| 6866 | break; |
| 6867 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6868 | default: |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6869 | LOGINT; |
| 6870 | return -1; |
| 6871 | } |
| 6872 | |
| 6873 | return EXIT_SUCCESS; |
| 6874 | } |
| 6875 | |
| 6876 | /** |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6877 | * @brief add data unres item |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6878 | * |
| 6879 | * @param[in] unres Unres data structure to use. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6880 | * @param[in] node Data node to use. |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6881 | * |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6882 | * @return 0 on success, -1 on error. |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6883 | */ |
| 6884 | int |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6885 | 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] | 6886 | { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6887 | assert(unres && node); |
Michal Vasko | c428084 | 2016-04-19 16:10:42 +0200 | [diff] [blame] | 6888 | assert((type == UNRES_LEAFREF) || (type == UNRES_INSTID) || (type == UNRES_WHEN) || (type == UNRES_MUST) |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6889 | || (type == UNRES_MUST_INOUT) || (type == UNRES_UNION) || (type == UNRES_EMPTYCONT)); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6890 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6891 | unres->count++; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 6892 | unres->node = ly_realloc(unres->node, unres->count * sizeof *unres->node); |
| 6893 | if (!unres->node) { |
| 6894 | LOGMEM; |
| 6895 | return -1; |
| 6896 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6897 | unres->node[unres->count - 1] = node; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 6898 | unres->type = ly_realloc(unres->type, unres->count * sizeof *unres->type); |
| 6899 | if (!unres->type) { |
| 6900 | LOGMEM; |
| 6901 | return -1; |
| 6902 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6903 | unres->type[unres->count - 1] = type; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6904 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6905 | if (type == UNRES_WHEN) { |
| 6906 | /* remove previous result */ |
| 6907 | node->when_status = LYD_WHEN; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6908 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6909 | |
| 6910 | return EXIT_SUCCESS; |
| 6911 | } |
| 6912 | |
| 6913 | /** |
| 6914 | * @brief Resolve every unres data item in the structure. Logs directly. |
| 6915 | * |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 6916 | * If options includes LYD_OPT_TRUSTED, the data are considered trusted (when, must conditions are not expected, |
| 6917 | * unresolved leafrefs/instids are accepted). |
| 6918 | * |
| 6919 | * If options includes LYD_OPT_NOAUTODEL, the false resulting when condition on non-default nodes, the error is raised. |
| 6920 | * |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6921 | * @param[in] unres Unres data structure to use. |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 6922 | * @param[in,out] root Root node of the data tree, can be changed due to autodeletion. |
| 6923 | * @param[in] options Data options as described above. |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6924 | * |
| 6925 | * @return EXIT_SUCCESS on success, -1 on error. |
| 6926 | */ |
| 6927 | int |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 6928 | 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] | 6929 | { |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 6930 | 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] | 6931 | int rc, progress; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6932 | char *msg, *path; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6933 | struct lyd_node *parent; |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 6934 | struct lyd_node_leaf_list *leaf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6935 | |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 6936 | assert(root); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6937 | assert(unres); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6938 | |
| 6939 | if (!unres->count) { |
| 6940 | return EXIT_SUCCESS; |
| 6941 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6942 | |
Michal Vasko | a0ffcab | 2016-05-02 14:52:08 +0200 | [diff] [blame] | 6943 | LOGVRB("Resolving unresolved data nodes and their constraints..."); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6944 | ly_vlog_hide(1); |
| 6945 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6946 | /* when-stmt first */ |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6947 | ly_errno = LY_SUCCESS; |
| 6948 | ly_vecode = LYVE_SUCCESS; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6949 | do { |
| 6950 | progress = 0; |
Michal Vasko | 6df9413 | 2016-09-22 11:08:09 +0200 | [diff] [blame] | 6951 | for (i = 0; i < unres->count; i++) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6952 | if (unres->type[i] != UNRES_WHEN) { |
| 6953 | continue; |
| 6954 | } |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 6955 | assert(!(options & LYD_OPT_TRUSTED)); |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 6956 | if (first) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6957 | /* count when-stmt nodes in unres list */ |
| 6958 | when_stmt++; |
| 6959 | } |
| 6960 | |
| 6961 | /* resolve when condition only when all parent when conditions are already resolved */ |
| 6962 | for (parent = unres->node[i]->parent; |
| 6963 | parent && LYD_WHEN_DONE(parent->when_status); |
| 6964 | parent = parent->parent) { |
| 6965 | if (!parent->parent && (parent->when_status & LYD_WHEN_FALSE)) { |
| 6966 | /* the parent node was already unlinked, do not resolve this node, |
| 6967 | * it will be removed anyway, so just mark it as resolved |
| 6968 | */ |
| 6969 | unres->node[i]->when_status |= LYD_WHEN_FALSE; |
| 6970 | unres->type[i] = UNRES_RESOLVED; |
| 6971 | resolved++; |
| 6972 | break; |
| 6973 | } |
| 6974 | } |
| 6975 | if (parent) { |
| 6976 | continue; |
| 6977 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6978 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6979 | rc = resolve_unres_data_item(unres->node[i], unres->type[i]); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6980 | if (!rc) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6981 | if (unres->node[i]->when_status & LYD_WHEN_FALSE) { |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 6982 | if ((options & LYD_OPT_NOAUTODEL) && !unres->node[i]->dflt) { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6983 | /* false when condition */ |
| 6984 | ly_vlog_hide(0); |
Radek Krejci | 76e15e1 | 2016-06-22 11:02:24 +0200 | [diff] [blame] | 6985 | if (ly_log_level >= LY_LLERR) { |
| 6986 | path = strdup(ly_errpath()); |
| 6987 | msg = strdup(ly_errmsg()); |
| 6988 | LOGERR(LY_EVALID, "%s%s%s%s", msg, |
| 6989 | path[0] ? " (path: " : "", path[0] ? path : "", path[0] ? ")" : ""); |
| 6990 | free(path); |
| 6991 | free(msg); |
| 6992 | } |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6993 | return -1; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6994 | } /* follows else */ |
| 6995 | |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 6996 | /* only unlink now, the subtree can contain another nodes stored in the unres list */ |
| 6997 | /* if it has parent non-presence containers that would be empty, we should actually |
| 6998 | * remove the container |
| 6999 | */ |
Radek Krejci | 2537fd3 | 2016-09-07 16:22:41 +0200 | [diff] [blame] | 7000 | for (parent = unres->node[i]; |
| 7001 | parent->parent && parent->parent->schema->nodetype == LYS_CONTAINER; |
| 7002 | parent = parent->parent) { |
| 7003 | if (((struct lys_node_container *)parent->parent->schema)->presence) { |
| 7004 | /* presence container */ |
| 7005 | break; |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7006 | } |
Radek Krejci | 2537fd3 | 2016-09-07 16:22:41 +0200 | [diff] [blame] | 7007 | if (parent->next || parent->prev != parent) { |
| 7008 | /* non empty (the child we are in and we are going to remove is not the only child) */ |
| 7009 | break; |
| 7010 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7011 | } |
Radek Krejci | 2537fd3 | 2016-09-07 16:22:41 +0200 | [diff] [blame] | 7012 | unres->node[i] = parent; |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7013 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7014 | /* auto-delete */ |
| 7015 | LOGVRB("auto-delete node \"%s\" due to when condition (%s)", ly_errpath(), |
| 7016 | ((struct lys_node_leaf *)unres->node[i]->schema)->when->cond); |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7017 | if (*root && *root == unres->node[i]) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7018 | *root = (*root)->next; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7019 | } |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7020 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7021 | lyd_unlink(unres->node[i]); |
| 7022 | unres->type[i] = UNRES_DELETE; |
| 7023 | del_items++; |
Radek Krejci | 51fd816 | 2016-03-24 15:49:51 +0100 | [diff] [blame] | 7024 | |
| 7025 | /* update the rest of unres items */ |
| 7026 | for (j = 0; j < unres->count; j++) { |
Radek Krejci | 3db819b | 2016-03-24 16:29:48 +0100 | [diff] [blame] | 7027 | if (unres->type[j] == UNRES_RESOLVED || unres->type[j] == UNRES_DELETE) { |
Radek Krejci | 51fd816 | 2016-03-24 15:49:51 +0100 | [diff] [blame] | 7028 | continue; |
| 7029 | } |
| 7030 | |
| 7031 | /* test if the node is in subtree to be deleted */ |
| 7032 | for (parent = unres->node[j]; parent; parent = parent->parent) { |
| 7033 | if (parent == unres->node[i]) { |
| 7034 | /* yes, it is */ |
| 7035 | unres->type[j] = UNRES_RESOLVED; |
| 7036 | resolved++; |
| 7037 | break; |
| 7038 | } |
| 7039 | } |
| 7040 | } |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7041 | } else { |
| 7042 | unres->type[i] = UNRES_RESOLVED; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7043 | } |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7044 | ly_errno = LY_SUCCESS; |
| 7045 | ly_vecode = LYVE_SUCCESS; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7046 | resolved++; |
| 7047 | progress = 1; |
| 7048 | } else if (rc == -1) { |
| 7049 | ly_vlog_hide(0); |
Michal Vasko | 76e7340 | 2016-08-24 16:00:13 +0200 | [diff] [blame] | 7050 | /* print only this last error */ |
| 7051 | resolve_unres_data_item(unres->node[i], unres->type[i]); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7052 | return -1; |
Radek Krejci | c2a180f | 2016-06-22 13:28:16 +0200 | [diff] [blame] | 7053 | } else { |
| 7054 | /* forward reference, erase ly_errno */ |
| 7055 | ly_errno = LY_SUCCESS; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7056 | } |
| 7057 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7058 | first = 0; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7059 | } while (progress && resolved < when_stmt); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7060 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7061 | /* do we have some unresolved when-stmt? */ |
Radek Krejci | d940d73 | 2016-03-24 16:02:28 +0100 | [diff] [blame] | 7062 | if (when_stmt > resolved) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7063 | ly_vlog_hide(0); |
Radek Krejci | 76e15e1 | 2016-06-22 11:02:24 +0200 | [diff] [blame] | 7064 | if (ly_log_level >= LY_LLERR) { |
| 7065 | path = strdup(ly_errpath()); |
| 7066 | msg = strdup(ly_errmsg()); |
| 7067 | LOGERR(LY_EVALID, "%s%s%s%s", msg, path[0] ? " (path: " : "", path[0] ? path : "", path[0] ? ")" : ""); |
| 7068 | free(path); |
| 7069 | free(msg); |
| 7070 | } |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7071 | return -1; |
| 7072 | } |
| 7073 | |
| 7074 | for (i = 0; del_items && i < unres->count; i++) { |
| 7075 | /* we had some when-stmt resulted to false, so now we have to sanitize the unres list */ |
| 7076 | if (unres->type[i] != UNRES_DELETE) { |
| 7077 | continue; |
| 7078 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7079 | if (!unres->node[i]) { |
| 7080 | unres->type[i] = UNRES_RESOLVED; |
| 7081 | del_items--; |
| 7082 | continue; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7083 | } |
| 7084 | |
| 7085 | /* really remove the complete subtree */ |
| 7086 | lyd_free(unres->node[i]); |
| 7087 | unres->type[i] = UNRES_RESOLVED; |
| 7088 | del_items--; |
| 7089 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7090 | |
| 7091 | /* rest */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7092 | for (i = 0; i < unres->count; ++i) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7093 | if (unres->type[i] == UNRES_RESOLVED) { |
| 7094 | continue; |
| 7095 | } |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 7096 | 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] | 7097 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7098 | rc = resolve_unres_data_item(unres->node[i], unres->type[i]); |
Michal Vasko | 6df9413 | 2016-09-22 11:08:09 +0200 | [diff] [blame] | 7099 | if (rc == -1) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7100 | ly_vlog_hide(0); |
Michal Vasko | 96b846c | 2016-05-18 13:28:58 +0200 | [diff] [blame] | 7101 | /* print only this last error */ |
| 7102 | resolve_unres_data_item(unres->node[i], unres->type[i]); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7103 | return -1; |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 7104 | } 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] | 7105 | unres->type[i] = UNRES_RESOLVED; |
| 7106 | resolved++; |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 7107 | if (options & LYD_OPT_TRUSTED) { |
Michal Vasko | 6df9413 | 2016-09-22 11:08:09 +0200 | [diff] [blame] | 7108 | /* accept it in this case */ |
| 7109 | if (unres->type[i] == UNRES_LEAFREF) { |
| 7110 | LOGVRB("Leafref \"%s\" with value \"%s\" failed to be resolved.", |
| 7111 | ((struct lys_node_leaf *)unres->node[i]->schema)->type.info.lref.path, |
| 7112 | ((struct lyd_node_leaf_list *)unres->node[i])->value_str); |
| 7113 | } else { |
| 7114 | LOGVRB("Instance identifier \"%s\" failed to be resolved.", |
| 7115 | ((struct lyd_node_leaf_list *)unres->node[i])->value_str); |
| 7116 | } |
| 7117 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7118 | } |
| 7119 | } |
| 7120 | |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7121 | ly_vlog_hide(0); |
| 7122 | if (resolved < unres->count) { |
| 7123 | /* try to resolve the unresolved data again, it will not resolve anything, but it will print |
| 7124 | * all the validation errors |
| 7125 | */ |
| 7126 | for (i = 0; i < unres->count; ++i) { |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7127 | if (unres->type[i] == UNRES_UNION) { |
| 7128 | /* does not make sense to print specific errors for all |
| 7129 | * the data types, just print that the value is invalid */ |
| 7130 | leaf = (struct lyd_node_leaf_list *)unres->node[i]; |
| 7131 | LOGVAL(LYE_INVAL, LY_VLOG_LYD, unres->node[i], (leaf->value_str ? leaf->value_str : ""), |
| 7132 | leaf->schema->name); |
| 7133 | } else if (unres->type[i] != UNRES_RESOLVED) { |
| 7134 | resolve_unres_data_item(unres->node[i], unres->type[i]); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7135 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7136 | } |
| 7137 | return -1; |
| 7138 | } |
| 7139 | |
Michal Vasko | a0ffcab | 2016-05-02 14:52:08 +0200 | [diff] [blame] | 7140 | LOGVRB("All data nodes and constraints resolved."); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7141 | unres->count = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7142 | return EXIT_SUCCESS; |
| 7143 | } |