Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file resolve.c |
| 3 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * @brief libyang resolve functions |
| 5 | * |
| 6 | * Copyright (c) 2015 CESNET, z.s.p.o. |
| 7 | * |
Radek Krejci | 54f6fb3 | 2016-02-24 12:56:39 +0100 | [diff] [blame] | 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
Michal Vasko | 8de098c | 2016-02-26 10:00:25 +0100 | [diff] [blame] | 11 | * |
Radek Krejci | 54f6fb3 | 2016-02-24 12:56:39 +0100 | [diff] [blame] | 12 | * https://opensource.org/licenses/BSD-3-Clause |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 13 | */ |
| 14 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 15 | #define _GNU_SOURCE |
| 16 | |
| 17 | #include <stdlib.h> |
| 18 | #include <assert.h> |
| 19 | #include <string.h> |
| 20 | #include <ctype.h> |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 21 | #include <limits.h> |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 22 | |
| 23 | #include "libyang.h" |
| 24 | #include "resolve.h" |
| 25 | #include "common.h" |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 26 | #include "xpath.h" |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 27 | #include "parser.h" |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 28 | #include "parser_yang.h" |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 29 | #include "xml_internal.h" |
Radek Krejci | 41912fe | 2015-10-22 10:22:12 +0200 | [diff] [blame] | 30 | #include "dict_private.h" |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 31 | #include "tree_internal.h" |
| 32 | |
Michal Vasko | f9b35d9 | 2016-10-21 15:19:30 +0200 | [diff] [blame] | 33 | static int resolve_union(struct lyd_node_leaf_list *leaf, struct lys_type *type); |
| 34 | |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 35 | int |
| 36 | parse_range_dec64(const char **str_num, uint8_t dig, int64_t *num) |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 37 | { |
| 38 | const char *ptr; |
| 39 | int minus = 0; |
| 40 | int64_t ret = 0; |
Radek Krejci | bf47a82 | 2016-11-04 10:06:08 +0100 | [diff] [blame] | 41 | int8_t str_exp, str_dig = -1, trailing_zeros = 0; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 42 | |
| 43 | ptr = *str_num; |
| 44 | |
| 45 | if (ptr[0] == '-') { |
| 46 | minus = 1; |
| 47 | ++ptr; |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 48 | } else if (ptr[0] == '+') { |
| 49 | ++ptr; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 50 | } |
| 51 | |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 52 | if (!isdigit(ptr[0])) { |
| 53 | /* there must be at least one */ |
| 54 | return 1; |
| 55 | } |
| 56 | |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 57 | for (str_exp = 0; isdigit(ptr[0]) || ((ptr[0] == '.') && (str_dig < 0)); ++ptr) { |
| 58 | if (str_exp > 18) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 59 | return 1; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | if (ptr[0] == '.') { |
| 63 | if (ptr[1] == '.') { |
| 64 | /* it's the next interval */ |
| 65 | break; |
| 66 | } |
| 67 | ++str_dig; |
| 68 | } else { |
Radek Krejci | bf47a82 | 2016-11-04 10:06:08 +0100 | [diff] [blame] | 69 | ret = ret * 10 + (ptr[0] - '0'); |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 70 | if (str_dig > -1) { |
| 71 | ++str_dig; |
Radek Krejci | bf47a82 | 2016-11-04 10:06:08 +0100 | [diff] [blame] | 72 | if (ptr[0] == '0') { |
| 73 | /* possibly trailing zero */ |
| 74 | trailing_zeros++; |
| 75 | } else { |
| 76 | trailing_zeros = 0; |
| 77 | } |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 78 | } |
| 79 | ++str_exp; |
| 80 | } |
| 81 | } |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 82 | if (str_dig == 0) { |
| 83 | /* no digits after '.' */ |
| 84 | return 1; |
| 85 | } else if (str_dig == -1) { |
| 86 | /* there are 0 numbers after the floating point */ |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 87 | str_dig = 0; |
| 88 | } |
Radek Krejci | bf47a82 | 2016-11-04 10:06:08 +0100 | [diff] [blame] | 89 | /* remove trailing zeros */ |
| 90 | if (trailing_zeros) { |
| 91 | str_dig = str_dig - trailing_zeros; |
| 92 | ret = ret / dec_pow(trailing_zeros); |
| 93 | } |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 94 | |
| 95 | /* it's parsed, now adjust the number based on fraction-digits, if needed */ |
| 96 | if (str_dig < dig) { |
| 97 | if ((str_exp - 1) + (dig - str_dig) > 18) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 98 | return 1; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 99 | } |
| 100 | ret *= dec_pow(dig - str_dig); |
| 101 | } |
| 102 | if (str_dig > dig) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 103 | return 1; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | if (minus) { |
| 107 | ret *= -1; |
| 108 | } |
| 109 | *str_num = ptr; |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 110 | *num = ret; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 111 | |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 112 | return 0; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /** |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 116 | * @brief Parse an identifier. |
| 117 | * |
| 118 | * ;; An identifier MUST NOT start with (('X'|'x') ('M'|'m') ('L'|'l')) |
| 119 | * identifier = (ALPHA / "_") |
| 120 | * *(ALPHA / DIGIT / "_" / "-" / ".") |
| 121 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 122 | * @param[in] id Identifier to use. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 123 | * |
| 124 | * @return Number of characters successfully parsed. |
| 125 | */ |
Michal Vasko | 249e6b5 | 2015-08-19 11:08:52 +0200 | [diff] [blame] | 126 | int |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 127 | parse_identifier(const char *id) |
| 128 | { |
| 129 | int parsed = 0; |
| 130 | |
Michal Vasko | 1ab90bc | 2016-03-15 10:40:22 +0100 | [diff] [blame] | 131 | assert(id); |
| 132 | |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 133 | if (!isalpha(id[0]) && (id[0] != '_')) { |
| 134 | return -parsed; |
| 135 | } |
| 136 | |
| 137 | ++parsed; |
| 138 | ++id; |
| 139 | |
| 140 | while (isalnum(id[0]) || (id[0] == '_') || (id[0] == '-') || (id[0] == '.')) { |
| 141 | ++parsed; |
| 142 | ++id; |
| 143 | } |
| 144 | |
| 145 | return parsed; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * @brief Parse a node-identifier. |
| 150 | * |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 151 | * node-identifier = [module-name ":"] identifier |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 152 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 153 | * @param[in] id Identifier to use. |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 154 | * @param[out] mod_name Points to the module name, NULL if there is not any. |
| 155 | * @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] | 156 | * @param[out] name Points to the node name. |
| 157 | * @param[out] nam_len Length of the node name. |
| 158 | * |
| 159 | * @return Number of characters successfully parsed, |
| 160 | * positive on success, negative on failure. |
| 161 | */ |
| 162 | static int |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 163 | 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] | 164 | { |
| 165 | int parsed = 0, ret; |
| 166 | |
| 167 | assert(id); |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 168 | if (mod_name) { |
| 169 | *mod_name = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 170 | } |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 171 | if (mod_name_len) { |
| 172 | *mod_name_len = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 173 | } |
| 174 | if (name) { |
| 175 | *name = NULL; |
| 176 | } |
| 177 | if (nam_len) { |
| 178 | *nam_len = 0; |
| 179 | } |
| 180 | |
| 181 | if ((ret = parse_identifier(id)) < 1) { |
| 182 | return ret; |
| 183 | } |
| 184 | |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 185 | if (mod_name) { |
| 186 | *mod_name = id; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 187 | } |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 188 | if (mod_name_len) { |
| 189 | *mod_name_len = ret; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | parsed += ret; |
| 193 | id += ret; |
| 194 | |
| 195 | /* there is prefix */ |
| 196 | if (id[0] == ':') { |
| 197 | ++parsed; |
| 198 | ++id; |
| 199 | |
| 200 | /* there isn't */ |
| 201 | } else { |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 202 | if (name && mod_name) { |
| 203 | *name = *mod_name; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 204 | } |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 205 | if (mod_name) { |
| 206 | *mod_name = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 207 | } |
| 208 | |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 209 | if (nam_len && mod_name_len) { |
| 210 | *nam_len = *mod_name_len; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 211 | } |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 212 | if (mod_name_len) { |
| 213 | *mod_name_len = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | return parsed; |
| 217 | } |
| 218 | |
| 219 | /* identifier (node name) */ |
| 220 | if ((ret = parse_identifier(id)) < 1) { |
| 221 | return -parsed+ret; |
| 222 | } |
| 223 | |
| 224 | if (name) { |
| 225 | *name = id; |
| 226 | } |
| 227 | if (nam_len) { |
| 228 | *nam_len = ret; |
| 229 | } |
| 230 | |
| 231 | return parsed+ret; |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * @brief Parse a path-predicate (leafref). |
| 236 | * |
| 237 | * path-predicate = "[" *WSP path-equality-expr *WSP "]" |
| 238 | * path-equality-expr = node-identifier *WSP "=" *WSP path-key-expr |
| 239 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 240 | * @param[in] id Identifier to use. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 241 | * @param[out] prefix Points to the prefix, NULL if there is not any. |
| 242 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 243 | * @param[out] name Points to the node name. |
| 244 | * @param[out] nam_len Length of the node name. |
| 245 | * @param[out] path_key_expr Points to the path-key-expr. |
| 246 | * @param[out] pke_len Length of the path-key-expr. |
| 247 | * @param[out] has_predicate Flag to mark whether there is another predicate following. |
| 248 | * |
| 249 | * @return Number of characters successfully parsed, |
| 250 | * positive on success, negative on failure. |
| 251 | */ |
| 252 | static int |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 253 | parse_path_predicate(const char *id, const char **prefix, int *pref_len, const char **name, int *nam_len, |
| 254 | const char **path_key_expr, int *pke_len, int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 255 | { |
| 256 | const char *ptr; |
| 257 | int parsed = 0, ret; |
| 258 | |
| 259 | assert(id); |
| 260 | if (prefix) { |
| 261 | *prefix = NULL; |
| 262 | } |
| 263 | if (pref_len) { |
| 264 | *pref_len = 0; |
| 265 | } |
| 266 | if (name) { |
| 267 | *name = NULL; |
| 268 | } |
| 269 | if (nam_len) { |
| 270 | *nam_len = 0; |
| 271 | } |
| 272 | if (path_key_expr) { |
| 273 | *path_key_expr = NULL; |
| 274 | } |
| 275 | if (pke_len) { |
| 276 | *pke_len = 0; |
| 277 | } |
| 278 | if (has_predicate) { |
| 279 | *has_predicate = 0; |
| 280 | } |
| 281 | |
| 282 | if (id[0] != '[') { |
| 283 | return -parsed; |
| 284 | } |
| 285 | |
| 286 | ++parsed; |
| 287 | ++id; |
| 288 | |
| 289 | while (isspace(id[0])) { |
| 290 | ++parsed; |
| 291 | ++id; |
| 292 | } |
| 293 | |
| 294 | if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len)) < 1) { |
| 295 | return -parsed+ret; |
| 296 | } |
| 297 | |
| 298 | parsed += ret; |
| 299 | id += ret; |
| 300 | |
| 301 | while (isspace(id[0])) { |
| 302 | ++parsed; |
| 303 | ++id; |
| 304 | } |
| 305 | |
| 306 | if (id[0] != '=') { |
| 307 | return -parsed; |
| 308 | } |
| 309 | |
| 310 | ++parsed; |
| 311 | ++id; |
| 312 | |
| 313 | while (isspace(id[0])) { |
| 314 | ++parsed; |
| 315 | ++id; |
| 316 | } |
| 317 | |
| 318 | if ((ptr = strchr(id, ']')) == NULL) { |
| 319 | return -parsed; |
| 320 | } |
| 321 | |
| 322 | --ptr; |
| 323 | while (isspace(ptr[0])) { |
| 324 | --ptr; |
| 325 | } |
| 326 | ++ptr; |
| 327 | |
| 328 | ret = ptr-id; |
| 329 | if (path_key_expr) { |
| 330 | *path_key_expr = id; |
| 331 | } |
| 332 | if (pke_len) { |
| 333 | *pke_len = ret; |
| 334 | } |
| 335 | |
| 336 | parsed += ret; |
| 337 | id += ret; |
| 338 | |
| 339 | while (isspace(id[0])) { |
| 340 | ++parsed; |
| 341 | ++id; |
| 342 | } |
| 343 | |
| 344 | assert(id[0] == ']'); |
| 345 | |
| 346 | if (id[1] == '[') { |
| 347 | *has_predicate = 1; |
| 348 | } |
| 349 | |
| 350 | return parsed+1; |
| 351 | } |
| 352 | |
| 353 | /** |
| 354 | * @brief Parse a path-key-expr (leafref). First call parses "current()", all |
| 355 | * the ".." and the first node-identifier, other calls parse a single |
| 356 | * node-identifier each. |
| 357 | * |
| 358 | * path-key-expr = current-function-invocation *WSP "/" *WSP |
| 359 | * rel-path-keyexpr |
| 360 | * rel-path-keyexpr = 1*(".." *WSP "/" *WSP) |
| 361 | * *(node-identifier *WSP "/" *WSP) |
| 362 | * node-identifier |
| 363 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 364 | * @param[in] id Identifier to use. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 365 | * @param[out] prefix Points to the prefix, NULL if there is not any. |
| 366 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 367 | * @param[out] name Points to the node name. |
| 368 | * @param[out] nam_len Length of the node name. |
| 369 | * @param[out] parent_times Number of ".." in the path. Must be 0 on the first call, |
| 370 | * must not be changed between consecutive calls. |
| 371 | * @return Number of characters successfully parsed, |
| 372 | * positive on success, negative on failure. |
| 373 | */ |
| 374 | static int |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 375 | parse_path_key_expr(const char *id, const char **prefix, int *pref_len, const char **name, int *nam_len, |
| 376 | int *parent_times) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 377 | { |
| 378 | int parsed = 0, ret, par_times = 0; |
| 379 | |
| 380 | assert(id); |
| 381 | assert(parent_times); |
| 382 | if (prefix) { |
| 383 | *prefix = NULL; |
| 384 | } |
| 385 | if (pref_len) { |
| 386 | *pref_len = 0; |
| 387 | } |
| 388 | if (name) { |
| 389 | *name = NULL; |
| 390 | } |
| 391 | if (nam_len) { |
| 392 | *nam_len = 0; |
| 393 | } |
| 394 | |
| 395 | if (!*parent_times) { |
| 396 | /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */ |
| 397 | if (strncmp(id, "current()", 9)) { |
| 398 | return -parsed; |
| 399 | } |
| 400 | |
| 401 | parsed += 9; |
| 402 | id += 9; |
| 403 | |
| 404 | while (isspace(id[0])) { |
| 405 | ++parsed; |
| 406 | ++id; |
| 407 | } |
| 408 | |
| 409 | if (id[0] != '/') { |
| 410 | return -parsed; |
| 411 | } |
| 412 | |
| 413 | ++parsed; |
| 414 | ++id; |
| 415 | |
| 416 | while (isspace(id[0])) { |
| 417 | ++parsed; |
| 418 | ++id; |
| 419 | } |
| 420 | |
| 421 | /* rel-path-keyexpr */ |
| 422 | if (strncmp(id, "..", 2)) { |
| 423 | return -parsed; |
| 424 | } |
| 425 | ++par_times; |
| 426 | |
| 427 | parsed += 2; |
| 428 | id += 2; |
| 429 | |
| 430 | while (isspace(id[0])) { |
| 431 | ++parsed; |
| 432 | ++id; |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | /* 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier |
| 437 | * |
| 438 | * first parent reference with whitespaces already parsed |
| 439 | */ |
| 440 | if (id[0] != '/') { |
| 441 | return -parsed; |
| 442 | } |
| 443 | |
| 444 | ++parsed; |
| 445 | ++id; |
| 446 | |
| 447 | while (isspace(id[0])) { |
| 448 | ++parsed; |
| 449 | ++id; |
| 450 | } |
| 451 | |
| 452 | while (!strncmp(id, "..", 2) && !*parent_times) { |
| 453 | ++par_times; |
| 454 | |
| 455 | parsed += 2; |
| 456 | id += 2; |
| 457 | |
| 458 | while (isspace(id[0])) { |
| 459 | ++parsed; |
| 460 | ++id; |
| 461 | } |
| 462 | |
| 463 | if (id[0] != '/') { |
| 464 | return -parsed; |
| 465 | } |
| 466 | |
| 467 | ++parsed; |
| 468 | ++id; |
| 469 | |
| 470 | while (isspace(id[0])) { |
| 471 | ++parsed; |
| 472 | ++id; |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | if (!*parent_times) { |
| 477 | *parent_times = par_times; |
| 478 | } |
| 479 | |
| 480 | /* all parent references must be parsed at this point */ |
| 481 | if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len)) < 1) { |
| 482 | return -parsed+ret; |
| 483 | } |
| 484 | |
| 485 | parsed += ret; |
| 486 | id += ret; |
| 487 | |
| 488 | return parsed; |
| 489 | } |
| 490 | |
| 491 | /** |
| 492 | * @brief Parse path-arg (leafref). |
| 493 | * |
| 494 | * path-arg = absolute-path / relative-path |
| 495 | * absolute-path = 1*("/" (node-identifier *path-predicate)) |
| 496 | * relative-path = 1*(".." "/") descendant-path |
| 497 | * |
Radek Krejci | f7ed4c3 | 2016-10-27 16:20:03 +0200 | [diff] [blame] | 498 | * @param[in] mod Module of the context node to get correct prefix in case it is not explicitly specified |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 499 | * @param[in] id Identifier to use. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 500 | * @param[out] prefix Points to the prefix, NULL if there is not any. |
| 501 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 502 | * @param[out] name Points to the node name. |
| 503 | * @param[out] nam_len Length of the node name. |
| 504 | * @param[out] parent_times Number of ".." in the path. Must be 0 on the first call, |
| 505 | * must not be changed between consecutive calls. -1 if the |
| 506 | * path is relative. |
| 507 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 508 | * |
| 509 | * @return Number of characters successfully parsed, |
| 510 | * positive on success, negative on failure. |
| 511 | */ |
| 512 | static int |
Radek Krejci | f7ed4c3 | 2016-10-27 16:20:03 +0200 | [diff] [blame] | 513 | parse_path_arg(struct lys_module *mod, const char *id, const char **prefix, int *pref_len, |
| 514 | const char **name, int *nam_len, int *parent_times, int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 515 | { |
| 516 | int parsed = 0, ret, par_times = 0; |
| 517 | |
| 518 | assert(id); |
| 519 | assert(parent_times); |
| 520 | if (prefix) { |
| 521 | *prefix = NULL; |
| 522 | } |
| 523 | if (pref_len) { |
| 524 | *pref_len = 0; |
| 525 | } |
| 526 | if (name) { |
| 527 | *name = NULL; |
| 528 | } |
| 529 | if (nam_len) { |
| 530 | *nam_len = 0; |
| 531 | } |
| 532 | if (has_predicate) { |
| 533 | *has_predicate = 0; |
| 534 | } |
| 535 | |
| 536 | if (!*parent_times && !strncmp(id, "..", 2)) { |
| 537 | ++par_times; |
| 538 | |
| 539 | parsed += 2; |
| 540 | id += 2; |
| 541 | |
| 542 | while (!strncmp(id, "/..", 3)) { |
| 543 | ++par_times; |
| 544 | |
| 545 | parsed += 3; |
| 546 | id += 3; |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | if (!*parent_times) { |
| 551 | if (par_times) { |
| 552 | *parent_times = par_times; |
| 553 | } else { |
| 554 | *parent_times = -1; |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | if (id[0] != '/') { |
| 559 | return -parsed; |
| 560 | } |
| 561 | |
| 562 | /* skip '/' */ |
| 563 | ++parsed; |
| 564 | ++id; |
| 565 | |
| 566 | /* node-identifier ([prefix:]identifier) */ |
| 567 | if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len)) < 1) { |
| 568 | return -parsed-ret; |
| 569 | } |
Radek Krejci | f7ed4c3 | 2016-10-27 16:20:03 +0200 | [diff] [blame] | 570 | if (!(*prefix)) { |
| 571 | /* actually we always need prefix even it is not specified */ |
| 572 | *prefix = lys_main_module(mod)->name; |
| 573 | *pref_len = strlen(*prefix); |
| 574 | } |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 575 | |
| 576 | parsed += ret; |
| 577 | id += ret; |
| 578 | |
| 579 | /* there is no predicate */ |
| 580 | if ((id[0] == '/') || !id[0]) { |
| 581 | return parsed; |
| 582 | } else if (id[0] != '[') { |
| 583 | return -parsed; |
| 584 | } |
| 585 | |
| 586 | if (has_predicate) { |
| 587 | *has_predicate = 1; |
| 588 | } |
| 589 | |
| 590 | return parsed; |
| 591 | } |
| 592 | |
| 593 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 594 | * @brief Parse instance-identifier in JSON data format. That means that prefixes |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 595 | * (which are mandatory for every node-identifier) are actually model names. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 596 | * |
| 597 | * instance-identifier = 1*("/" (node-identifier *predicate)) |
| 598 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 599 | * @param[in] id Identifier to use. |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 600 | * @param[out] model Points to the model name. |
| 601 | * @param[out] mod_len Length of the model name. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 602 | * @param[out] name Points to the node name. |
| 603 | * @param[out] nam_len Length of the node name. |
| 604 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 605 | * |
| 606 | * @return Number of characters successfully parsed, |
| 607 | * positive on success, negative on failure. |
| 608 | */ |
| 609 | static int |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 610 | parse_instance_identifier(const char *id, const char **model, int *mod_len, const char **name, int *nam_len, |
| 611 | int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 612 | { |
| 613 | int parsed = 0, ret; |
| 614 | |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 615 | if (has_predicate) { |
| 616 | *has_predicate = 0; |
| 617 | } |
| 618 | |
| 619 | if (id[0] != '/') { |
| 620 | return -parsed; |
| 621 | } |
| 622 | |
| 623 | ++parsed; |
| 624 | ++id; |
| 625 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 626 | if ((ret = parse_identifier(id)) < 1) { |
| 627 | return ret; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 628 | } |
| 629 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 630 | *model = id; |
| 631 | *mod_len = ret; |
| 632 | |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 633 | parsed += ret; |
| 634 | id += ret; |
| 635 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 636 | if (id[0] != ':') { |
| 637 | return -parsed; |
| 638 | } |
| 639 | |
| 640 | ++parsed; |
| 641 | ++id; |
| 642 | |
| 643 | if ((ret = parse_identifier(id)) < 1) { |
| 644 | return ret; |
| 645 | } |
| 646 | |
| 647 | *name = id; |
| 648 | *nam_len = ret; |
| 649 | |
| 650 | parsed += ret; |
| 651 | id += ret; |
| 652 | |
Radek Krejci | 4967cb6 | 2016-09-14 16:40:28 +0200 | [diff] [blame] | 653 | if (id[0] == '[' && has_predicate) { |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 654 | *has_predicate = 1; |
| 655 | } |
| 656 | |
| 657 | return parsed; |
| 658 | } |
| 659 | |
| 660 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 661 | * @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] | 662 | * (which are mandatory) are actually model names. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 663 | * |
| 664 | * predicate = "[" *WSP (predicate-expr / pos) *WSP "]" |
| 665 | * predicate-expr = (node-identifier / ".") *WSP "=" *WSP |
| 666 | * ((DQUOTE string DQUOTE) / |
| 667 | * (SQUOTE string SQUOTE)) |
| 668 | * pos = non-negative-integer-value |
| 669 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 670 | * @param[in] id Identifier to use. |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 671 | * @param[out] model Points to the model name. |
| 672 | * @param[out] mod_len Length of the model name. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 673 | * @param[out] name Points to the node name. Can be identifier (from node-identifier), "." or pos. |
| 674 | * @param[out] nam_len Length of the node name. |
| 675 | * @param[out] value Value the node-identifier must have (string from the grammar), |
| 676 | * NULL if there is not any. |
| 677 | * @param[out] val_len Length of the value, 0 if there is not any. |
| 678 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 679 | * |
| 680 | * @return Number of characters successfully parsed, |
| 681 | * positive on success, negative on failure. |
| 682 | */ |
| 683 | static int |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 684 | parse_predicate(const char *id, const char **model, int *mod_len, const char **name, int *nam_len, |
| 685 | const char **value, int *val_len, int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 686 | { |
| 687 | const char *ptr; |
| 688 | int parsed = 0, ret; |
| 689 | char quote; |
| 690 | |
| 691 | assert(id); |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 692 | if (model) { |
| 693 | *model = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 694 | } |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 695 | if (mod_len) { |
| 696 | *mod_len = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 697 | } |
| 698 | if (name) { |
| 699 | *name = NULL; |
| 700 | } |
| 701 | if (nam_len) { |
| 702 | *nam_len = 0; |
| 703 | } |
| 704 | if (value) { |
| 705 | *value = NULL; |
| 706 | } |
| 707 | if (val_len) { |
| 708 | *val_len = 0; |
| 709 | } |
| 710 | if (has_predicate) { |
| 711 | *has_predicate = 0; |
| 712 | } |
| 713 | |
| 714 | if (id[0] != '[') { |
| 715 | return -parsed; |
| 716 | } |
| 717 | |
| 718 | ++parsed; |
| 719 | ++id; |
| 720 | |
| 721 | while (isspace(id[0])) { |
| 722 | ++parsed; |
| 723 | ++id; |
| 724 | } |
| 725 | |
| 726 | /* pos */ |
| 727 | if (isdigit(id[0])) { |
| 728 | if (name) { |
| 729 | *name = id; |
| 730 | } |
| 731 | |
| 732 | if (id[0] == '0') { |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 733 | return -parsed; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | while (isdigit(id[0])) { |
| 737 | ++parsed; |
| 738 | ++id; |
| 739 | } |
| 740 | |
| 741 | if (nam_len) { |
| 742 | *nam_len = id-(*name); |
| 743 | } |
| 744 | |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 745 | /* "." or node-identifier */ |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 746 | } else { |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 747 | if (id[0] == '.') { |
| 748 | if (name) { |
| 749 | *name = id; |
| 750 | } |
| 751 | if (nam_len) { |
| 752 | *nam_len = 1; |
| 753 | } |
| 754 | |
| 755 | ++parsed; |
| 756 | ++id; |
| 757 | |
| 758 | } else { |
| 759 | if ((ret = parse_node_identifier(id, model, mod_len, name, nam_len)) < 1) { |
| 760 | return -parsed+ret; |
| 761 | } else if (model && !*model) { |
| 762 | return -parsed; |
| 763 | } |
| 764 | |
| 765 | parsed += ret; |
| 766 | id += ret; |
| 767 | } |
| 768 | |
| 769 | while (isspace(id[0])) { |
| 770 | ++parsed; |
| 771 | ++id; |
| 772 | } |
| 773 | |
| 774 | if (id[0] != '=') { |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 775 | return -parsed; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 776 | } |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 777 | |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 778 | ++parsed; |
| 779 | ++id; |
| 780 | |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 781 | while (isspace(id[0])) { |
| 782 | ++parsed; |
| 783 | ++id; |
| 784 | } |
| 785 | |
| 786 | /* ((DQUOTE string DQUOTE) / (SQUOTE string SQUOTE)) */ |
| 787 | if ((id[0] == '\"') || (id[0] == '\'')) { |
| 788 | quote = id[0]; |
| 789 | |
| 790 | ++parsed; |
| 791 | ++id; |
| 792 | |
| 793 | if ((ptr = strchr(id, quote)) == NULL) { |
| 794 | return -parsed; |
| 795 | } |
| 796 | ret = ptr-id; |
| 797 | |
| 798 | if (value) { |
| 799 | *value = id; |
| 800 | } |
| 801 | if (val_len) { |
| 802 | *val_len = ret; |
| 803 | } |
| 804 | |
| 805 | parsed += ret+1; |
| 806 | id += ret+1; |
| 807 | } else { |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 808 | return -parsed; |
| 809 | } |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | while (isspace(id[0])) { |
| 813 | ++parsed; |
| 814 | ++id; |
| 815 | } |
| 816 | |
| 817 | if (id[0] != ']') { |
| 818 | return -parsed; |
| 819 | } |
| 820 | |
| 821 | ++parsed; |
| 822 | ++id; |
| 823 | |
| 824 | if ((id[0] == '[') && has_predicate) { |
| 825 | *has_predicate = 1; |
| 826 | } |
| 827 | |
| 828 | return parsed; |
| 829 | } |
| 830 | |
| 831 | /** |
| 832 | * @brief Parse schema-nodeid. |
| 833 | * |
| 834 | * schema-nodeid = absolute-schema-nodeid / |
| 835 | * descendant-schema-nodeid |
| 836 | * absolute-schema-nodeid = 1*("/" node-identifier) |
Michal Vasko | 4893535 | 2016-03-29 11:52:36 +0200 | [diff] [blame] | 837 | * descendant-schema-nodeid = ["." "/"] |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 838 | * node-identifier |
| 839 | * absolute-schema-nodeid |
| 840 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 841 | * @param[in] id Identifier to use. |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 842 | * @param[out] mod_name Points to the module name, NULL if there is not any. |
| 843 | * @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] | 844 | * @param[out] name Points to the node name. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 845 | * @param[out] nam_len Length of the node name. |
| 846 | * @param[out] is_relative Flag to mark whether the nodeid is absolute or descendant. Must be -1 |
| 847 | * on the first call, must not be changed between consecutive calls. |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 848 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. It cannot be |
| 849 | * based on the grammar, in those cases use NULL. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 850 | * |
| 851 | * @return Number of characters successfully parsed, |
| 852 | * positive on success, negative on failure. |
| 853 | */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 854 | int |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 855 | 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] | 856 | int *is_relative, int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 857 | { |
| 858 | int parsed = 0, ret; |
| 859 | |
| 860 | assert(id); |
| 861 | assert(is_relative); |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 862 | if (mod_name) { |
| 863 | *mod_name = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 864 | } |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 865 | if (mod_name_len) { |
| 866 | *mod_name_len = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 867 | } |
| 868 | if (name) { |
| 869 | *name = NULL; |
| 870 | } |
| 871 | if (nam_len) { |
| 872 | *nam_len = 0; |
| 873 | } |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 874 | if (has_predicate) { |
| 875 | *has_predicate = 0; |
| 876 | } |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 877 | |
| 878 | if (id[0] != '/') { |
| 879 | if (*is_relative != -1) { |
| 880 | return -parsed; |
| 881 | } else { |
| 882 | *is_relative = 1; |
| 883 | } |
Michal Vasko | 4893535 | 2016-03-29 11:52:36 +0200 | [diff] [blame] | 884 | if (!strncmp(id, "./", 2)) { |
| 885 | parsed += 2; |
| 886 | id += 2; |
| 887 | } |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 888 | } else { |
| 889 | if (*is_relative == -1) { |
| 890 | *is_relative = 0; |
| 891 | } |
| 892 | ++parsed; |
| 893 | ++id; |
| 894 | } |
| 895 | |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 896 | 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] | 897 | return -parsed+ret; |
| 898 | } |
| 899 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 900 | parsed += ret; |
| 901 | id += ret; |
| 902 | |
| 903 | if ((id[0] == '[') && has_predicate) { |
| 904 | *has_predicate = 1; |
| 905 | } |
| 906 | |
| 907 | return parsed; |
| 908 | } |
| 909 | |
| 910 | /** |
| 911 | * @brief Parse schema predicate (special format internally used). |
| 912 | * |
| 913 | * predicate = "[" *WSP predicate-expr *WSP "]" |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 914 | * predicate-expr = "." / identifier / key-with-value |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 915 | * key-with-value = identifier *WSP "=" *WSP |
| 916 | * ((DQUOTE string DQUOTE) / |
| 917 | * (SQUOTE string SQUOTE)) |
| 918 | * |
| 919 | * @param[in] id Identifier to use. |
| 920 | * @param[out] name Points to the list key name. |
| 921 | * @param[out] nam_len Length of \p name. |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 922 | * @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] | 923 | * @param[out] val_len Length of \p value. |
| 924 | * @param[out] has_predicate Flag to mark whether there is another predicate specified. |
| 925 | */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 926 | int |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 927 | 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] | 928 | int *has_predicate) |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 929 | { |
| 930 | const char *ptr; |
| 931 | int parsed = 0, ret; |
| 932 | char quote; |
| 933 | |
| 934 | assert(id); |
| 935 | if (name) { |
| 936 | *name = NULL; |
| 937 | } |
| 938 | if (nam_len) { |
| 939 | *nam_len = 0; |
| 940 | } |
| 941 | if (value) { |
| 942 | *value = NULL; |
| 943 | } |
| 944 | if (val_len) { |
| 945 | *val_len = 0; |
| 946 | } |
| 947 | if (has_predicate) { |
| 948 | *has_predicate = 0; |
| 949 | } |
| 950 | |
| 951 | if (id[0] != '[') { |
| 952 | return -parsed; |
| 953 | } |
| 954 | |
| 955 | ++parsed; |
| 956 | ++id; |
| 957 | |
| 958 | while (isspace(id[0])) { |
| 959 | ++parsed; |
| 960 | ++id; |
| 961 | } |
| 962 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 963 | /* identifier */ |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 964 | if (id[0] == '.') { |
| 965 | ret = 1; |
| 966 | } else if ((ret = parse_identifier(id)) < 1) { |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 967 | return -parsed + ret; |
| 968 | } |
| 969 | if (name) { |
| 970 | *name = id; |
| 971 | } |
| 972 | if (nam_len) { |
| 973 | *nam_len = ret; |
| 974 | } |
| 975 | |
| 976 | parsed += ret; |
| 977 | id += ret; |
| 978 | |
| 979 | while (isspace(id[0])) { |
| 980 | ++parsed; |
| 981 | ++id; |
| 982 | } |
| 983 | |
| 984 | /* there is value as well */ |
| 985 | if (id[0] == '=') { |
| 986 | ++parsed; |
| 987 | ++id; |
| 988 | |
| 989 | while (isspace(id[0])) { |
| 990 | ++parsed; |
| 991 | ++id; |
| 992 | } |
| 993 | |
| 994 | /* ((DQUOTE string DQUOTE) / (SQUOTE string SQUOTE)) */ |
| 995 | if ((id[0] == '\"') || (id[0] == '\'')) { |
| 996 | quote = id[0]; |
| 997 | |
| 998 | ++parsed; |
| 999 | ++id; |
| 1000 | |
| 1001 | if ((ptr = strchr(id, quote)) == NULL) { |
| 1002 | return -parsed; |
| 1003 | } |
| 1004 | ret = ptr - id; |
| 1005 | |
| 1006 | if (value) { |
| 1007 | *value = id; |
| 1008 | } |
| 1009 | if (val_len) { |
| 1010 | *val_len = ret; |
| 1011 | } |
| 1012 | |
| 1013 | parsed += ret + 1; |
| 1014 | id += ret + 1; |
| 1015 | } else { |
| 1016 | return -parsed; |
| 1017 | } |
| 1018 | |
| 1019 | while (isspace(id[0])) { |
| 1020 | ++parsed; |
| 1021 | ++id; |
| 1022 | } |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1023 | } else if (value) { |
| 1024 | /* if value was expected, it's mandatory */ |
| 1025 | return -parsed; |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1026 | } |
| 1027 | |
| 1028 | if (id[0] != ']') { |
| 1029 | return -parsed; |
| 1030 | } |
| 1031 | |
| 1032 | ++parsed; |
| 1033 | ++id; |
| 1034 | |
| 1035 | if ((id[0] == '[') && has_predicate) { |
| 1036 | *has_predicate = 1; |
| 1037 | } |
| 1038 | |
| 1039 | return parsed; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 1040 | } |
| 1041 | |
| 1042 | /** |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1043 | * @brief Resolve (find) a feature definition. Logs directly. |
| 1044 | * |
| 1045 | * @param[in] feat_name Feature name to resolve. |
| 1046 | * @param[in] len Length of \p feat_name. |
| 1047 | * @param[in] node Node with the if-feature expression. |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1048 | * @param[out] feature Pointer to be set to point to the feature definition, if feature not found |
| 1049 | * (return code 1), the pointer is untouched. |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1050 | * |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1051 | * @return 0 on success, 1 on forward reference, -1 on error. |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1052 | */ |
| 1053 | static int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1054 | 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] | 1055 | { |
| 1056 | char *str; |
| 1057 | const char *mod_name, *name; |
| 1058 | int mod_name_len, nam_len, i, j; |
| 1059 | const struct lys_module *module; |
| 1060 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1061 | assert(feature); |
| 1062 | |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1063 | /* check prefix */ |
| 1064 | if ((i = parse_node_identifier(feat_name, &mod_name, &mod_name_len, &name, &nam_len)) < 1) { |
| 1065 | LOGVAL(LYE_INCHAR, LY_VLOG_NONE, NULL, feat_name[-i], &feat_name[-i]); |
| 1066 | return -1; |
| 1067 | } |
| 1068 | |
| 1069 | module = lys_get_import_module(lys_node_module(node), NULL, 0, mod_name, mod_name_len); |
| 1070 | if (!module) { |
| 1071 | /* identity refers unknown data model */ |
| 1072 | LOGVAL(LYE_INMOD_LEN, LY_VLOG_NONE, NULL, mod_name_len, mod_name); |
| 1073 | return -1; |
| 1074 | } |
| 1075 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1076 | if (module != node->module && module == lys_node_module(node)) { |
| 1077 | /* first, try to search directly in submodule where the feature was mentioned */ |
| 1078 | for (j = 0; j < node->module->features_size; j++) { |
| 1079 | if (!strncmp(name, node->module->features[j].name, nam_len) && !node->module->features[j].name[nam_len]) { |
| 1080 | /* check status */ |
| 1081 | 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] | 1082 | node->module->features[j].module, node->module->features[j].name, NULL)) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1083 | return -1; |
| 1084 | } |
| 1085 | *feature = &node->module->features[j]; |
| 1086 | return 0; |
| 1087 | } |
| 1088 | } |
| 1089 | } |
| 1090 | |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1091 | /* search in the identified module ... */ |
| 1092 | for (j = 0; j < module->features_size; j++) { |
Michal Vasko | 3def867 | 2016-07-01 11:43:09 +0200 | [diff] [blame] | 1093 | 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] | 1094 | /* check status */ |
| 1095 | 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] | 1096 | module->features[j].module, module->features[j].name, NULL)) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1097 | return -1; |
| 1098 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1099 | *feature = &module->features[j]; |
| 1100 | return 0; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1101 | } |
| 1102 | } |
| 1103 | /* ... and all its submodules */ |
| 1104 | for (i = 0; i < module->inc_size; i++) { |
| 1105 | if (!module->inc[i].submodule) { |
| 1106 | /* not yet resolved */ |
| 1107 | continue; |
| 1108 | } |
| 1109 | for (j = 0; j < module->inc[i].submodule->features_size; j++) { |
Michal Vasko | 3def867 | 2016-07-01 11:43:09 +0200 | [diff] [blame] | 1110 | if (!strncmp(name, module->inc[i].submodule->features[j].name, nam_len) |
| 1111 | && !module->inc[i].submodule->features[j].name[nam_len]) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1112 | /* check status */ |
| 1113 | if (lyp_check_status(node->flags, lys_node_module(node), node->name, |
| 1114 | module->inc[i].submodule->features[j].flags, |
| 1115 | module->inc[i].submodule->features[j].module, |
Pavol Vican | fdab9f9 | 2016-09-07 15:23:27 +0200 | [diff] [blame] | 1116 | module->inc[i].submodule->features[j].name, NULL)) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1117 | return -1; |
| 1118 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1119 | *feature = &module->inc[i].submodule->features[j]; |
| 1120 | return 0; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1121 | } |
| 1122 | } |
| 1123 | } |
| 1124 | |
| 1125 | /* not found */ |
| 1126 | str = strndup(feat_name, len); |
| 1127 | LOGVAL(LYE_INRESOLV, LY_VLOG_NONE, NULL, "feature", str); |
| 1128 | free(str); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1129 | return 1; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1130 | } |
| 1131 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1132 | /* |
| 1133 | * @return |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1134 | * - 1 if enabled |
| 1135 | * - 0 if disabled |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1136 | * - -1 if not usable by its if-feature expression |
| 1137 | */ |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1138 | static int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1139 | resolve_feature_value(const struct lys_feature *feat) |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1140 | { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1141 | int i; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1142 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1143 | for (i = 0; i < feat->iffeature_size; i++) { |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1144 | if (!resolve_iffeature(&feat->iffeature[i])) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1145 | return -1; |
| 1146 | } |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1147 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1148 | |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1149 | return feat->flags & LYS_FENABLED ? 1 : 0; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1150 | } |
| 1151 | |
| 1152 | static int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1153 | 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] | 1154 | { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1155 | uint8_t op; |
| 1156 | int rc, a, b; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1157 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1158 | op = iff_getop(expr->expr, *index_e); |
| 1159 | (*index_e)++; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1160 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1161 | switch (op) { |
| 1162 | case LYS_IFF_F: |
| 1163 | /* resolve feature */ |
| 1164 | return resolve_feature_value(expr->features[(*index_f)++]); |
| 1165 | case LYS_IFF_NOT: |
| 1166 | rc = resolve_iffeature_recursive(expr, index_e, index_f); |
| 1167 | if (rc == -1) { |
| 1168 | /* one of the referenced feature is hidden by its if-feature, |
| 1169 | * so this if-feature expression is always false */ |
| 1170 | return -1; |
| 1171 | } else { |
| 1172 | /* invert result */ |
| 1173 | return rc ? 0 : 1; |
| 1174 | } |
| 1175 | case LYS_IFF_AND: |
| 1176 | case LYS_IFF_OR: |
| 1177 | a = resolve_iffeature_recursive(expr, index_e, index_f); |
| 1178 | b = resolve_iffeature_recursive(expr, index_e, index_f); |
| 1179 | if (a == -1 || b == -1) { |
| 1180 | /* one of the referenced feature is hidden by its if-feature, |
| 1181 | * so this if-feature expression is always false */ |
| 1182 | return -1; |
| 1183 | } else if (op == LYS_IFF_AND) { |
| 1184 | return a && b; |
| 1185 | } else { /* LYS_IFF_OR */ |
| 1186 | return a || b; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1187 | } |
| 1188 | } |
| 1189 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1190 | return -1; |
| 1191 | } |
| 1192 | |
| 1193 | int |
| 1194 | resolve_iffeature(struct lys_iffeature *expr) |
| 1195 | { |
| 1196 | int rc = -1; |
| 1197 | int index_e = 0, index_f = 0; |
| 1198 | |
| 1199 | if (expr->expr) { |
| 1200 | rc = resolve_iffeature_recursive(expr, &index_e, &index_f); |
| 1201 | } |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1202 | return (rc == 1) ? 1 : 0; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1203 | } |
| 1204 | |
| 1205 | struct iff_stack { |
| 1206 | int size; |
| 1207 | int index; /* first empty item */ |
| 1208 | uint8_t *stack; |
| 1209 | }; |
| 1210 | |
| 1211 | static int |
| 1212 | iff_stack_push(struct iff_stack *stack, uint8_t value) |
| 1213 | { |
| 1214 | if (stack->index == stack->size) { |
| 1215 | stack->size += 4; |
| 1216 | stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack); |
| 1217 | if (!stack->stack) { |
| 1218 | LOGMEM; |
| 1219 | stack->size = 0; |
| 1220 | return EXIT_FAILURE; |
| 1221 | } |
| 1222 | } |
| 1223 | |
| 1224 | stack->stack[stack->index++] = value; |
| 1225 | return EXIT_SUCCESS; |
| 1226 | } |
| 1227 | |
| 1228 | static uint8_t |
| 1229 | iff_stack_pop(struct iff_stack *stack) |
| 1230 | { |
| 1231 | stack->index--; |
| 1232 | return stack->stack[stack->index]; |
| 1233 | } |
| 1234 | |
| 1235 | static void |
| 1236 | iff_stack_clean(struct iff_stack *stack) |
| 1237 | { |
| 1238 | stack->size = 0; |
| 1239 | free(stack->stack); |
| 1240 | } |
| 1241 | |
| 1242 | static void |
| 1243 | iff_setop(uint8_t *list, uint8_t op, int pos) |
| 1244 | { |
| 1245 | uint8_t *item; |
| 1246 | uint8_t mask = 3; |
| 1247 | |
| 1248 | assert(pos >= 0); |
| 1249 | assert(op <= 3); /* max 2 bits */ |
| 1250 | |
| 1251 | item = &list[pos / 4]; |
| 1252 | mask = mask << 2 * (pos % 4); |
| 1253 | *item = (*item) & ~mask; |
| 1254 | *item = (*item) | (op << 2 * (pos % 4)); |
| 1255 | } |
| 1256 | |
| 1257 | uint8_t |
| 1258 | iff_getop(uint8_t *list, int pos) |
| 1259 | { |
| 1260 | uint8_t *item; |
| 1261 | uint8_t mask = 3, result; |
| 1262 | |
| 1263 | assert(pos >= 0); |
| 1264 | |
| 1265 | item = &list[pos / 4]; |
| 1266 | result = (*item) & (mask << 2 * (pos % 4)); |
| 1267 | return result >> 2 * (pos % 4); |
| 1268 | } |
| 1269 | |
| 1270 | #define LYS_IFF_LP 0x04 /* ( */ |
| 1271 | #define LYS_IFF_RP 0x08 /* ) */ |
| 1272 | |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1273 | /* internal structure for passing data for UNRES_IFFEAT */ |
| 1274 | struct unres_iffeat_data { |
| 1275 | struct lys_node *node; |
| 1276 | const char *fname; |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 1277 | int infeature; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1278 | }; |
| 1279 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1280 | void |
| 1281 | resolve_iffeature_getsizes(struct lys_iffeature *iffeat, unsigned int *expr_size, unsigned int *feat_size) |
| 1282 | { |
| 1283 | unsigned int e = 0, f = 0, r = 0; |
| 1284 | uint8_t op; |
| 1285 | |
| 1286 | assert(iffeat); |
| 1287 | |
| 1288 | if (!iffeat->expr) { |
| 1289 | goto result; |
| 1290 | } |
| 1291 | |
| 1292 | do { |
| 1293 | op = iff_getop(iffeat->expr, e++); |
| 1294 | switch (op) { |
| 1295 | case LYS_IFF_NOT: |
| 1296 | if (!r) { |
| 1297 | r += 1; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1298 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1299 | break; |
| 1300 | case LYS_IFF_AND: |
| 1301 | case LYS_IFF_OR: |
| 1302 | if (!r) { |
| 1303 | r += 2; |
| 1304 | } else { |
| 1305 | r += 1; |
| 1306 | } |
| 1307 | break; |
| 1308 | case LYS_IFF_F: |
| 1309 | f++; |
| 1310 | if (r) { |
| 1311 | r--; |
| 1312 | } |
| 1313 | break; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1314 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1315 | } while(r); |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1316 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1317 | result: |
| 1318 | if (expr_size) { |
| 1319 | *expr_size = e; |
| 1320 | } |
| 1321 | if (feat_size) { |
| 1322 | *feat_size = f; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1323 | } |
| 1324 | } |
| 1325 | |
| 1326 | int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1327 | 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] | 1328 | int infeature, struct unres_schema *unres) |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1329 | { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1330 | const char *c = value; |
| 1331 | int r, rc = EXIT_FAILURE; |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1332 | int i, j, last_not, checkversion = 0; |
| 1333 | unsigned int f_size = 0, expr_size = 0, f_exp = 1; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1334 | uint8_t op; |
| 1335 | struct iff_stack stack = {0, 0, NULL}; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1336 | struct unres_iffeat_data *iff_data; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1337 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1338 | assert(c); |
| 1339 | |
| 1340 | if (isspace(c[0])) { |
| 1341 | LOGVAL(LYE_INCHAR, LY_VLOG_NONE, NULL, c[0], c); |
| 1342 | return EXIT_FAILURE; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1343 | } |
| 1344 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1345 | /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */ |
| 1346 | for (i = j = last_not = 0; c[i]; i++) { |
| 1347 | if (c[i] == '(') { |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1348 | checkversion = 1; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1349 | j++; |
| 1350 | continue; |
| 1351 | } else if (c[i] == ')') { |
| 1352 | j--; |
| 1353 | continue; |
| 1354 | } else if (isspace(c[i])) { |
| 1355 | continue; |
| 1356 | } |
| 1357 | |
| 1358 | if (!strncmp(&c[i], "not", r = 3) || !strncmp(&c[i], "and", r = 3) || !strncmp(&c[i], "or", r = 2)) { |
| 1359 | if (c[i + r] == '\0') { |
Radek Krejci | a98da3f | 2016-07-27 14:05:22 +0200 | [diff] [blame] | 1360 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1361 | return EXIT_FAILURE; |
| 1362 | } else if (!isspace(c[i + r])) { |
| 1363 | /* feature name starting with the not/and/or */ |
| 1364 | last_not = 0; |
| 1365 | f_size++; |
| 1366 | } else if (c[i] == 'n') { /* not operation */ |
| 1367 | if (last_not) { |
| 1368 | /* double not */ |
| 1369 | expr_size = expr_size - 2; |
| 1370 | last_not = 0; |
| 1371 | } else { |
| 1372 | last_not = 1; |
| 1373 | } |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1374 | } else { /* and, or */ |
| 1375 | f_exp++; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1376 | /* not a not operation */ |
| 1377 | last_not = 0; |
| 1378 | } |
| 1379 | i += r; |
| 1380 | } else { |
| 1381 | f_size++; |
| 1382 | last_not = 0; |
| 1383 | } |
| 1384 | expr_size++; |
| 1385 | |
| 1386 | while (!isspace(c[i])) { |
| 1387 | if (!c[i] || c[i] == ')') { |
| 1388 | i--; |
| 1389 | break; |
| 1390 | } |
| 1391 | i++; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1392 | } |
| 1393 | } |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1394 | if (j || f_exp != f_size) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1395 | /* not matching count of ( and ) */ |
| 1396 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
| 1397 | return EXIT_FAILURE; |
| 1398 | } |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1399 | |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1400 | if (checkversion || expr_size > 1) { |
| 1401 | /* check that we have 1.1 module */ |
| 1402 | if (node->module->version != 2) { |
| 1403 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
| 1404 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "YANG 1.1 if-feature expression found in 1.0 module."); |
| 1405 | return EXIT_FAILURE; |
| 1406 | } |
| 1407 | } |
| 1408 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1409 | /* allocate the memory */ |
| 1410 | 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] | 1411 | iffeat_expr->features = calloc(f_size, sizeof *iffeat_expr->features); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1412 | stack.size = expr_size; |
| 1413 | stack.stack = malloc(expr_size * sizeof *stack.stack); |
| 1414 | if (!stack.stack || !iffeat_expr->expr || !iffeat_expr->features) { |
| 1415 | LOGMEM; |
| 1416 | goto error; |
| 1417 | } |
| 1418 | f_size--; expr_size--; /* used as indexes from now */ |
| 1419 | |
| 1420 | for (i--; i >= 0; i--) { |
| 1421 | if (c[i] == ')') { |
| 1422 | /* push it on stack */ |
| 1423 | iff_stack_push(&stack, LYS_IFF_RP); |
| 1424 | continue; |
| 1425 | } else if (c[i] == '(') { |
| 1426 | /* pop from the stack into result all operators until ) */ |
| 1427 | while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) { |
| 1428 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1429 | } |
| 1430 | continue; |
| 1431 | } else if (isspace(c[i])) { |
| 1432 | continue; |
| 1433 | } |
| 1434 | |
| 1435 | /* end operator or operand -> find beginning and get what is it */ |
| 1436 | j = i + 1; |
| 1437 | while (i >= 0 && !isspace(c[i]) && c[i] != '(') { |
| 1438 | i--; |
| 1439 | } |
| 1440 | i++; /* get back by one step */ |
| 1441 | |
| 1442 | if (!strncmp(&c[i], "not ", 4)) { |
| 1443 | if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) { |
| 1444 | /* double not */ |
| 1445 | iff_stack_pop(&stack); |
| 1446 | } else { |
| 1447 | /* not has the highest priority, so do not pop from the stack |
| 1448 | * as in case of AND and OR */ |
| 1449 | iff_stack_push(&stack, LYS_IFF_NOT); |
| 1450 | } |
| 1451 | } else if (!strncmp(&c[i], "and ", 4)) { |
| 1452 | /* as for OR - pop from the stack all operators with the same or higher |
| 1453 | * priority and store them to the result, then push the AND to the stack */ |
| 1454 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) { |
| 1455 | op = iff_stack_pop(&stack); |
| 1456 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1457 | } |
| 1458 | iff_stack_push(&stack, LYS_IFF_AND); |
| 1459 | } else if (!strncmp(&c[i], "or ", 3)) { |
| 1460 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) { |
| 1461 | op = iff_stack_pop(&stack); |
| 1462 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1463 | } |
| 1464 | iff_stack_push(&stack, LYS_IFF_OR); |
| 1465 | } else { |
| 1466 | /* feature name, length is j - i */ |
| 1467 | |
| 1468 | /* add it to the result */ |
| 1469 | iff_setop(iffeat_expr->expr, LYS_IFF_F, expr_size--); |
| 1470 | |
| 1471 | /* now get the link to the feature definition. Since it can be |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1472 | * forward referenced, we have to keep the feature name in auxiliary |
| 1473 | * structure passed into unres */ |
| 1474 | iff_data = malloc(sizeof *iff_data); |
| 1475 | iff_data->node = node; |
| 1476 | iff_data->fname = lydict_insert(node->module->ctx, &c[i], j - i); |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 1477 | iff_data->infeature = infeature; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1478 | r = unres_schema_add_node(node->module, unres, &iffeat_expr->features[f_size], UNRES_IFFEAT, |
| 1479 | (struct lys_node *)iff_data); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1480 | f_size--; |
| 1481 | |
| 1482 | if (r == -1) { |
Pavol Vican | 4d08451 | 2016-09-29 16:38:12 +0200 | [diff] [blame] | 1483 | free(iff_data); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1484 | goto error; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1485 | } |
| 1486 | } |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1487 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1488 | while (stack.index) { |
| 1489 | op = iff_stack_pop(&stack); |
| 1490 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1491 | } |
| 1492 | |
| 1493 | if (++expr_size || ++f_size) { |
| 1494 | /* not all expected operators and operands found */ |
| 1495 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
| 1496 | rc = EXIT_FAILURE; |
| 1497 | } else { |
| 1498 | rc = EXIT_SUCCESS; |
| 1499 | } |
| 1500 | |
| 1501 | error: |
| 1502 | /* cleanup */ |
| 1503 | iff_stack_clean(&stack); |
| 1504 | |
| 1505 | return rc; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1506 | } |
| 1507 | |
| 1508 | /** |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1509 | * @brief Resolve (find) a data node based on a schema-nodeid. |
| 1510 | * |
| 1511 | * Used for resolving unique statements - so id is expected to be relative and local (without reference to a different |
| 1512 | * module). |
| 1513 | * |
| 1514 | */ |
| 1515 | struct lyd_node * |
| 1516 | resolve_data_descendant_schema_nodeid(const char *nodeid, struct lyd_node *start) |
| 1517 | { |
| 1518 | char *str, *token, *p; |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1519 | struct lyd_node *result = NULL, *iter; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1520 | const struct lys_node *schema = NULL; |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1521 | int shorthand = 0; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1522 | |
| 1523 | assert(nodeid && start); |
| 1524 | |
| 1525 | if (nodeid[0] == '/') { |
| 1526 | return NULL; |
| 1527 | } |
| 1528 | |
| 1529 | str = p = strdup(nodeid); |
| 1530 | if (!str) { |
| 1531 | LOGMEM; |
| 1532 | return NULL; |
| 1533 | } |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1534 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1535 | while (p) { |
| 1536 | token = p; |
| 1537 | p = strchr(p, '/'); |
| 1538 | if (p) { |
| 1539 | *p = '\0'; |
| 1540 | p++; |
| 1541 | } |
| 1542 | |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1543 | if (p) { |
| 1544 | /* inner node */ |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1545 | if (resolve_descendant_schema_nodeid(token, schema ? schema->child : start->schema, |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 1546 | LYS_CONTAINER | LYS_CHOICE | LYS_CASE | LYS_LEAF, 0, 0, &schema) |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1547 | || !schema) { |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1548 | result = NULL; |
| 1549 | break; |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1550 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1551 | |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1552 | if (schema->nodetype & (LYS_CHOICE | LYS_CASE)) { |
| 1553 | continue; |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 1554 | } else if (lys_parent(schema)->nodetype == LYS_CHOICE) { |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1555 | /* shorthand case */ |
| 1556 | if (!shorthand) { |
| 1557 | shorthand = 1; |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 1558 | schema = lys_parent(schema); |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1559 | continue; |
| 1560 | } else { |
| 1561 | shorthand = 0; |
| 1562 | if (schema->nodetype == LYS_LEAF) { |
| 1563 | /* should not be here, since we have leaf, which is not a shorthand nor final node */ |
| 1564 | result = NULL; |
| 1565 | break; |
| 1566 | } |
| 1567 | } |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1568 | } |
| 1569 | } else { |
| 1570 | /* final node */ |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 1571 | if (resolve_descendant_schema_nodeid(token, schema ? schema->child : start->schema, LYS_LEAF, |
| 1572 | shorthand ? 0 : 1, 0, &schema) |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1573 | || !schema) { |
| 1574 | result = NULL; |
| 1575 | break; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1576 | } |
| 1577 | } |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1578 | LY_TREE_FOR(result ? result->child : start, iter) { |
| 1579 | if (iter->schema == schema) { |
| 1580 | /* move in data tree according to returned schema */ |
| 1581 | result = iter; |
| 1582 | break; |
| 1583 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1584 | } |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1585 | if (!iter) { |
| 1586 | /* instance not found */ |
| 1587 | result = NULL; |
| 1588 | break; |
| 1589 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1590 | } |
| 1591 | free(str); |
| 1592 | |
| 1593 | return result; |
| 1594 | } |
| 1595 | |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1596 | /* |
| 1597 | * 0 - ok (done) |
| 1598 | * 1 - continue |
| 1599 | * 2 - break |
| 1600 | * -1 - error |
| 1601 | */ |
| 1602 | static int |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1603 | 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] | 1604 | 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] | 1605 | int implemented_mod, const struct lys_node **start) |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1606 | { |
| 1607 | const struct lys_module *prefix_mod; |
| 1608 | |
| 1609 | /* module check */ |
| 1610 | prefix_mod = lys_get_import_module(module, NULL, 0, mod_name, mod_name_len); |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame^] | 1611 | if (prefix_mod && implemented_mod) { |
Radek Krejci | 0fa54e9 | 2016-09-14 14:01:05 +0200 | [diff] [blame] | 1612 | prefix_mod = lys_get_implemented_module(prefix_mod); |
| 1613 | } |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1614 | if (!prefix_mod) { |
| 1615 | return -1; |
| 1616 | } |
| 1617 | if (prefix_mod != lys_node_module(sibling)) { |
| 1618 | return 1; |
| 1619 | } |
| 1620 | |
| 1621 | /* check for shorthand cases - then 'start' does not change */ |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 1622 | 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] | 1623 | if (*shorthand != -1) { |
| 1624 | *shorthand = *shorthand ? 0 : 1; |
| 1625 | } |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1626 | } |
| 1627 | |
| 1628 | /* the result node? */ |
| 1629 | if (!id[0]) { |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1630 | if (*shorthand == 1) { |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1631 | return 1; |
| 1632 | } |
| 1633 | return 0; |
| 1634 | } |
| 1635 | |
Radek Krejci | 3a13016 | 2016-11-07 16:21:20 +0100 | [diff] [blame] | 1636 | if (!(*shorthand)) { |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1637 | /* move down the tree, if possible */ |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 1638 | if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1639 | return -1; |
| 1640 | } |
| 1641 | *start = sibling->child; |
| 1642 | } |
| 1643 | |
| 1644 | return 2; |
| 1645 | } |
| 1646 | |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame^] | 1647 | /* start - relative, module - absolute, -1 error, EXIT_SUCCESS ok (but ret can still be NULL), >0 unexpected char on ret - 1 |
| 1648 | * implement: 0 - do not change the implemented status of the affected modules, 1 - change implemented status of the affected modules |
| 1649 | */ |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1650 | int |
| 1651 | resolve_augment_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_module *module, |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame^] | 1652 | int implement, const struct lys_node **ret) |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1653 | { |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame^] | 1654 | const char *name, *mod_name, *mod_name_prev, *id; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1655 | const struct lys_node *sibling; |
| 1656 | int r, nam_len, mod_name_len, is_relative = -1; |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1657 | int8_t shorthand = 0; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1658 | /* resolved import module from the start module, it must match the next node-name-match sibling */ |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame^] | 1659 | const struct lys_module *start_mod, *aux_mod; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1660 | |
| 1661 | assert(nodeid && (start || module) && !(start && module) && ret); |
| 1662 | |
| 1663 | id = nodeid; |
| 1664 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1665 | 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] | 1666 | return ((id - nodeid) - r) + 1; |
| 1667 | } |
| 1668 | id += r; |
| 1669 | |
| 1670 | if ((is_relative && !start) || (!is_relative && !module)) { |
| 1671 | return -1; |
| 1672 | } |
| 1673 | |
| 1674 | /* descendant-schema-nodeid */ |
| 1675 | if (is_relative) { |
Michal Vasko | 4c06fd8 | 2016-02-17 13:43:38 +0100 | [diff] [blame] | 1676 | module = start_mod = start->module; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1677 | |
| 1678 | /* absolute-schema-nodeid */ |
| 1679 | } else { |
| 1680 | start_mod = lys_get_import_module(module, NULL, 0, mod_name, mod_name_len); |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame^] | 1681 | if (start_mod != lys_main_module(module) && start_mod && !start_mod->implemented) { |
Radek Krejci | 0fa54e9 | 2016-09-14 14:01:05 +0200 | [diff] [blame] | 1682 | /* if the submodule augments the mainmodule (or in general a module augments |
| 1683 | * itself, we don't want to search for the implemented module but augments |
| 1684 | * the module anyway. But when augmenting another module, we need the implemented |
| 1685 | * revision of the module if any */ |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame^] | 1686 | aux_mod = lys_get_implemented_module(start_mod); |
| 1687 | if (!aux_mod->implemented && implement) { |
| 1688 | /* make the found module implemented */ |
| 1689 | if (lys_set_implemented(aux_mod)) { |
| 1690 | return -1; |
| 1691 | } |
| 1692 | } |
| 1693 | start_mod = aux_mod; |
| 1694 | implement++; |
Radek Krejci | 0fa54e9 | 2016-09-14 14:01:05 +0200 | [diff] [blame] | 1695 | } |
Michal Vasko | e290563 | 2016-02-11 15:42:24 +0100 | [diff] [blame] | 1696 | if (!start_mod) { |
| 1697 | return -1; |
| 1698 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1699 | start = start_mod->data; |
| 1700 | } |
| 1701 | |
| 1702 | while (1) { |
| 1703 | sibling = NULL; |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame^] | 1704 | mod_name_prev = mod_name; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1705 | while ((sibling = lys_getnext(sibling, lys_parent(start), start_mod, |
| 1706 | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT))) { |
| 1707 | /* name match */ |
Michal Vasko | 0a1aaa4 | 2016-04-19 09:48:25 +0200 | [diff] [blame] | 1708 | 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] | 1709 | r = schema_nodeid_siblingcheck(sibling, &shorthand, id, module, mod_name, mod_name_len, |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame^] | 1710 | implement, &start); |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1711 | if (r == 0) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1712 | *ret = sibling; |
| 1713 | return EXIT_SUCCESS; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1714 | } else if (r == 1) { |
| 1715 | continue; |
| 1716 | } else if (r == 2) { |
| 1717 | break; |
| 1718 | } else { |
| 1719 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1720 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1721 | } |
| 1722 | } |
| 1723 | |
| 1724 | /* no match */ |
| 1725 | if (!sibling) { |
Michal Vasko | a426fef | 2016-03-07 10:47:31 +0100 | [diff] [blame] | 1726 | *ret = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1727 | return EXIT_SUCCESS; |
| 1728 | } |
| 1729 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1730 | 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] | 1731 | return ((id - nodeid) - r) + 1; |
| 1732 | } |
| 1733 | id += r; |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame^] | 1734 | |
| 1735 | if ((mod_name && mod_name_prev && strncmp(mod_name, mod_name_prev, mod_name_len + 1)) || |
| 1736 | (mod_name != mod_name_prev && (!mod_name || !mod_name_prev))) { |
| 1737 | /* we are getting into another module (augment) */ |
| 1738 | if (implement) { |
| 1739 | /* we have to check that also target modules are implemented, if not, we have to change it */ |
| 1740 | aux_mod = lys_get_import_module(module, NULL, 0, mod_name, mod_name_len); |
| 1741 | if (!aux_mod) { |
| 1742 | return -1; |
| 1743 | } |
| 1744 | if (!aux_mod->implemented) { |
| 1745 | aux_mod = lys_get_implemented_module(aux_mod); |
| 1746 | if (!aux_mod->implemented) { |
| 1747 | /* make the found module implemented */ |
| 1748 | if (lys_set_implemented(aux_mod)) { |
| 1749 | return -1; |
| 1750 | } |
| 1751 | } |
| 1752 | } |
| 1753 | } else { |
| 1754 | /* we are not implementing the module itself, so the augments outside the module are ignored */ |
| 1755 | *ret = NULL; |
| 1756 | return EXIT_SUCCESS; |
| 1757 | } |
| 1758 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1759 | } |
| 1760 | |
| 1761 | /* cannot get here */ |
| 1762 | LOGINT; |
| 1763 | return -1; |
| 1764 | } |
| 1765 | |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 1766 | /* unique, refine, |
| 1767 | * >0 - unexpected char on position (ret - 1), |
| 1768 | * 0 - ok (but ret can still be NULL), |
| 1769 | * -1 - error, |
| 1770 | * -2 - violated no_innerlist */ |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1771 | int |
| 1772 | 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] | 1773 | int check_shorthand, int no_innerlist, const struct lys_node **ret) |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1774 | { |
| 1775 | const char *name, *mod_name, *id; |
| 1776 | const struct lys_node *sibling; |
| 1777 | int r, nam_len, mod_name_len, is_relative = -1; |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1778 | int8_t shorthand = check_shorthand ? 0 : -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1779 | /* 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] | 1780 | const struct lys_module *module; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1781 | |
| 1782 | assert(nodeid && start && ret); |
| 1783 | assert(!(ret_nodetype & (LYS_USES | LYS_AUGMENT)) && ((ret_nodetype == LYS_GROUPING) || !(ret_nodetype & LYS_GROUPING))); |
| 1784 | |
| 1785 | id = nodeid; |
| 1786 | module = start->module; |
| 1787 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1788 | 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] | 1789 | return ((id - nodeid) - r) + 1; |
| 1790 | } |
| 1791 | id += r; |
| 1792 | |
| 1793 | if (!is_relative) { |
| 1794 | return -1; |
| 1795 | } |
| 1796 | |
| 1797 | while (1) { |
| 1798 | sibling = NULL; |
| 1799 | while ((sibling = lys_getnext(sibling, lys_parent(start), module, |
| 1800 | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE))) { |
| 1801 | /* name match */ |
| 1802 | 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] | 1803 | 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] | 1804 | if (r == 0) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1805 | if (!(sibling->nodetype & ret_nodetype)) { |
| 1806 | /* wrong node type, too bad */ |
| 1807 | continue; |
| 1808 | } |
| 1809 | *ret = sibling; |
| 1810 | return EXIT_SUCCESS; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1811 | } else if (r == 1) { |
| 1812 | continue; |
| 1813 | } else if (r == 2) { |
| 1814 | break; |
| 1815 | } else { |
| 1816 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1817 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1818 | } |
| 1819 | } |
| 1820 | |
| 1821 | /* no match */ |
| 1822 | if (!sibling) { |
Michal Vasko | a426fef | 2016-03-07 10:47:31 +0100 | [diff] [blame] | 1823 | *ret = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1824 | return EXIT_SUCCESS; |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 1825 | } else if (no_innerlist && sibling->nodetype == LYS_LIST) { |
| 1826 | *ret = NULL; |
| 1827 | return -2; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1828 | } |
| 1829 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1830 | 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] | 1831 | return ((id - nodeid) - r) + 1; |
| 1832 | } |
| 1833 | id += r; |
| 1834 | } |
| 1835 | |
| 1836 | /* cannot get here */ |
| 1837 | LOGINT; |
| 1838 | return -1; |
| 1839 | } |
| 1840 | |
| 1841 | /* choice default */ |
| 1842 | int |
| 1843 | resolve_choice_default_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_node **ret) |
| 1844 | { |
| 1845 | /* cannot actually be a path */ |
| 1846 | if (strchr(nodeid, '/')) { |
| 1847 | return -1; |
| 1848 | } |
| 1849 | |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 1850 | 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] | 1851 | } |
| 1852 | |
| 1853 | /* uses, -1 error, EXIT_SUCCESS ok (but ret can still be NULL), >0 unexpected char on ret - 1 */ |
| 1854 | static int |
| 1855 | resolve_uses_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_node_grp **ret) |
| 1856 | { |
| 1857 | const struct lys_module *module; |
| 1858 | const char *mod_prefix, *name; |
| 1859 | int i, mod_prefix_len, nam_len; |
| 1860 | |
| 1861 | /* parse the identifier, it must be parsed on one call */ |
| 1862 | if (((i = parse_node_identifier(nodeid, &mod_prefix, &mod_prefix_len, &name, &nam_len)) < 1) || nodeid[i]) { |
| 1863 | return -i + 1; |
| 1864 | } |
| 1865 | |
| 1866 | module = lys_get_import_module(start->module, mod_prefix, mod_prefix_len, NULL, 0); |
| 1867 | if (!module) { |
| 1868 | return -1; |
| 1869 | } |
| 1870 | if (module != start->module) { |
| 1871 | start = module->data; |
| 1872 | } |
| 1873 | |
| 1874 | *ret = lys_find_grouping_up(name, (struct lys_node *)start); |
| 1875 | |
| 1876 | return EXIT_SUCCESS; |
| 1877 | } |
| 1878 | |
| 1879 | int |
| 1880 | resolve_absolute_schema_nodeid(const char *nodeid, const struct lys_module *module, int ret_nodetype, |
| 1881 | const struct lys_node **ret) |
| 1882 | { |
| 1883 | const char *name, *mod_name, *id; |
| 1884 | const struct lys_node *sibling, *start; |
| 1885 | int r, nam_len, mod_name_len, is_relative = -1; |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1886 | int8_t shorthand = 0; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1887 | const struct lys_module *abs_start_mod; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1888 | |
| 1889 | assert(nodeid && module && ret); |
| 1890 | assert(!(ret_nodetype & (LYS_USES | LYS_AUGMENT)) && ((ret_nodetype == LYS_GROUPING) || !(ret_nodetype & LYS_GROUPING))); |
| 1891 | |
| 1892 | id = nodeid; |
| 1893 | start = module->data; |
| 1894 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1895 | 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] | 1896 | return ((id - nodeid) - r) + 1; |
| 1897 | } |
| 1898 | id += r; |
| 1899 | |
| 1900 | if (is_relative) { |
| 1901 | return -1; |
| 1902 | } |
| 1903 | |
| 1904 | 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] | 1905 | if (!abs_start_mod) { |
| 1906 | return -1; |
| 1907 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1908 | |
| 1909 | while (1) { |
| 1910 | sibling = NULL; |
| 1911 | while ((sibling = lys_getnext(sibling, lys_parent(start), abs_start_mod, LYS_GETNEXT_WITHCHOICE |
| 1912 | | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT | LYS_GETNEXT_WITHGROUPING))) { |
| 1913 | /* name match */ |
Michal Vasko | 0a1aaa4 | 2016-04-19 09:48:25 +0200 | [diff] [blame] | 1914 | 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] | 1915 | 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] | 1916 | if (r == 0) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1917 | if (!(sibling->nodetype & ret_nodetype)) { |
| 1918 | /* wrong node type, too bad */ |
| 1919 | continue; |
| 1920 | } |
| 1921 | *ret = sibling; |
| 1922 | return EXIT_SUCCESS; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1923 | } else if (r == 1) { |
| 1924 | continue; |
| 1925 | } else if (r == 2) { |
| 1926 | break; |
| 1927 | } else { |
| 1928 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1929 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1930 | } |
| 1931 | } |
| 1932 | |
| 1933 | /* no match */ |
| 1934 | if (!sibling) { |
Michal Vasko | a426fef | 2016-03-07 10:47:31 +0100 | [diff] [blame] | 1935 | *ret = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1936 | return EXIT_SUCCESS; |
| 1937 | } |
| 1938 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1939 | 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] | 1940 | return ((id - nodeid) - r) + 1; |
| 1941 | } |
| 1942 | id += r; |
| 1943 | } |
| 1944 | |
| 1945 | /* cannot get here */ |
| 1946 | LOGINT; |
| 1947 | return -1; |
| 1948 | } |
| 1949 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1950 | static int |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1951 | 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] | 1952 | { |
| 1953 | const char *name; |
| 1954 | int nam_len, has_predicate, i; |
| 1955 | |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 1956 | if (((i = parse_schema_json_predicate(predicate, &name, &nam_len, NULL, NULL, &has_predicate)) < 1) |
| 1957 | || !strncmp(name, ".", nam_len)) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 1958 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-i], &predicate[-i]); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1959 | return -1; |
| 1960 | } |
| 1961 | |
| 1962 | predicate += i; |
| 1963 | *parsed += i; |
| 1964 | |
| 1965 | for (i = 0; i < list->keys_size; ++i) { |
| 1966 | if (!strncmp(list->keys[i]->name, name, nam_len) && !list->keys[i]->name[nam_len]) { |
| 1967 | break; |
| 1968 | } |
| 1969 | } |
| 1970 | |
| 1971 | if (i == list->keys_size) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 1972 | LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1973 | return -1; |
| 1974 | } |
| 1975 | |
| 1976 | /* more predicates? */ |
| 1977 | if (has_predicate) { |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1978 | return resolve_json_schema_list_predicate(predicate, list, parsed); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1979 | } |
| 1980 | |
| 1981 | return 0; |
| 1982 | } |
| 1983 | |
Michal Vasko | 8d26e5c | 2016-09-08 10:03:49 +0200 | [diff] [blame] | 1984 | /* cannot return LYS_GROUPING, LYS_AUGMENT, LYS_USES, logs directly */ |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1985 | const struct lys_node * |
Michal Vasko | 8d26e5c | 2016-09-08 10:03:49 +0200 | [diff] [blame] | 1986 | 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] | 1987 | { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 1988 | char *module_name = ly_buf(), *buf_backup = NULL, *str; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1989 | const char *name, *mod_name, *id; |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1990 | const struct lys_node *sibling; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1991 | 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] | 1992 | /* 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] | 1993 | const struct lys_module *prefix_mod, *module, *prev_mod; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1994 | |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1995 | assert(nodeid && (ctx || start)); |
| 1996 | if (!ctx) { |
| 1997 | ctx = start->module->ctx; |
| 1998 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1999 | |
| 2000 | id = nodeid; |
| 2001 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2002 | 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] | 2003 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2004 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2005 | } |
| 2006 | id += r; |
| 2007 | |
| 2008 | if (is_relative) { |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2009 | assert(start); |
| 2010 | start = start->child; |
| 2011 | if (!start) { |
| 2012 | /* no descendants, fail for sure */ |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2013 | str = strndup(nodeid, (name + nam_len) - nodeid); |
| 2014 | LOGVAL(LYE_PATH_INNODE, LY_VLOG_STR, str); |
| 2015 | free(str); |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2016 | return NULL; |
| 2017 | } |
| 2018 | module = start->module; |
| 2019 | } else { |
| 2020 | if (!mod_name) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2021 | str = strndup(nodeid, (name + nam_len) - nodeid); |
| 2022 | LOGVAL(LYE_PATH_MISSMOD, LY_VLOG_STR, nodeid); |
| 2023 | free(str); |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2024 | return NULL; |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2025 | } else if (mod_name_len > LY_BUF_SIZE - 1) { |
| 2026 | LOGINT; |
| 2027 | return NULL; |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2028 | } |
| 2029 | |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2030 | if (ly_buf_used && module_name[0]) { |
| 2031 | buf_backup = strndup(module_name, LY_BUF_SIZE - 1); |
| 2032 | } |
| 2033 | ly_buf_used++; |
| 2034 | |
Michal Vasko | df3f1ab | 2016-04-01 15:07:22 +0200 | [diff] [blame] | 2035 | memmove(module_name, mod_name, mod_name_len); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2036 | module_name[mod_name_len] = '\0'; |
| 2037 | module = ly_ctx_get_module(ctx, module_name, NULL); |
| 2038 | |
| 2039 | if (buf_backup) { |
| 2040 | /* return previous internal buffer content */ |
| 2041 | strcpy(module_name, buf_backup); |
| 2042 | free(buf_backup); |
| 2043 | buf_backup = NULL; |
| 2044 | } |
| 2045 | ly_buf_used--; |
| 2046 | |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2047 | if (!module) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2048 | str = strndup(nodeid, (mod_name + mod_name_len) - nodeid); |
| 2049 | LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str); |
| 2050 | free(str); |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2051 | return NULL; |
| 2052 | } |
| 2053 | start = module->data; |
| 2054 | |
| 2055 | /* now it's as if there was no module name */ |
| 2056 | mod_name = NULL; |
| 2057 | mod_name_len = 0; |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2058 | } |
| 2059 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2060 | prev_mod = module; |
| 2061 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2062 | while (1) { |
| 2063 | sibling = NULL; |
Michal Vasko | 8d26e5c | 2016-09-08 10:03:49 +0200 | [diff] [blame] | 2064 | while ((sibling = lys_getnext(sibling, lys_parent(start), module, |
| 2065 | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT))) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2066 | /* name match */ |
Michal Vasko | 0a1aaa4 | 2016-04-19 09:48:25 +0200 | [diff] [blame] | 2067 | 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] | 2068 | /* module check */ |
| 2069 | if (mod_name) { |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2070 | if (mod_name_len > LY_BUF_SIZE - 1) { |
Michal Vasko | 8757e7c | 2016-03-15 10:41:30 +0100 | [diff] [blame] | 2071 | LOGINT; |
| 2072 | return NULL; |
| 2073 | } |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2074 | |
| 2075 | if (ly_buf_used && module_name[0]) { |
| 2076 | buf_backup = strndup(module_name, LY_BUF_SIZE - 1); |
| 2077 | } |
| 2078 | ly_buf_used++; |
| 2079 | |
Michal Vasko | df3f1ab | 2016-04-01 15:07:22 +0200 | [diff] [blame] | 2080 | memmove(module_name, mod_name, mod_name_len); |
Michal Vasko | 8757e7c | 2016-03-15 10:41:30 +0100 | [diff] [blame] | 2081 | module_name[mod_name_len] = '\0'; |
| 2082 | /* will also find an augment module */ |
| 2083 | prefix_mod = ly_ctx_get_module(ctx, module_name, NULL); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2084 | |
| 2085 | if (buf_backup) { |
| 2086 | /* return previous internal buffer content */ |
Michal Vasko | 888a129 | 2016-04-05 12:08:54 +0200 | [diff] [blame] | 2087 | strncpy(module_name, buf_backup, LY_BUF_SIZE - 1); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2088 | free(buf_backup); |
| 2089 | buf_backup = NULL; |
| 2090 | } |
| 2091 | ly_buf_used--; |
| 2092 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2093 | if (!prefix_mod) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2094 | str = strndup(nodeid, (mod_name + mod_name_len) - nodeid); |
| 2095 | LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str); |
| 2096 | free(str); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2097 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2098 | } |
| 2099 | } else { |
| 2100 | prefix_mod = prev_mod; |
| 2101 | } |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 2102 | if (prefix_mod != lys_node_module(sibling)) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2103 | continue; |
| 2104 | } |
| 2105 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2106 | /* do we have some predicates on it? */ |
| 2107 | if (has_predicate) { |
| 2108 | r = 0; |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 2109 | if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 2110 | if ((r = parse_schema_json_predicate(id, NULL, NULL, NULL, NULL, &has_predicate)) < 1) { |
| 2111 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
| 2112 | return NULL; |
| 2113 | } |
| 2114 | } else if (sibling->nodetype == LYS_LIST) { |
| 2115 | if (resolve_json_schema_list_predicate(id, (const struct lys_node_list *)sibling, &r)) { |
| 2116 | return NULL; |
| 2117 | } |
| 2118 | } else { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2119 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2120 | return NULL; |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2121 | } |
| 2122 | id += r; |
| 2123 | } |
| 2124 | |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 2125 | /* check for shorthand cases - then 'start' does not change */ |
Michal Vasko | 8d26e5c | 2016-09-08 10:03:49 +0200 | [diff] [blame] | 2126 | 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] | 2127 | shorthand = ~shorthand; |
| 2128 | } |
| 2129 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2130 | /* the result node? */ |
| 2131 | if (!id[0]) { |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 2132 | if (shorthand) { |
| 2133 | /* wrong path for shorthand */ |
Michal Vasko | 025e045 | 2016-05-17 16:14:20 +0200 | [diff] [blame] | 2134 | str = strndup(nodeid, (name + nam_len) - nodeid); |
| 2135 | LOGVAL(LYE_PATH_INNODE, LY_VLOG_STR, str); |
Michal Vasko | 3c0f9f5 | 2016-05-17 16:38:10 +0200 | [diff] [blame] | 2136 | 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] | 2137 | free(str); |
Michal Vasko | 025e045 | 2016-05-17 16:14:20 +0200 | [diff] [blame] | 2138 | return NULL; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 2139 | } |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2140 | return sibling; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2141 | } |
| 2142 | |
Michal Vasko | 8d26e5c | 2016-09-08 10:03:49 +0200 | [diff] [blame] | 2143 | if (!shorthand) { |
Michal Vasko | 7dc71d0 | 2016-03-15 10:42:28 +0100 | [diff] [blame] | 2144 | /* move down the tree, if possible */ |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 2145 | if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2146 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
Michal Vasko | 7dc71d0 | 2016-03-15 10:42:28 +0100 | [diff] [blame] | 2147 | return NULL; |
| 2148 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2149 | start = sibling->child; |
| 2150 | } |
| 2151 | |
| 2152 | /* update prev mod */ |
| 2153 | prev_mod = start->module; |
| 2154 | break; |
| 2155 | } |
| 2156 | } |
| 2157 | |
| 2158 | /* no match */ |
| 2159 | if (!sibling) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2160 | str = strndup(nodeid, (name + nam_len) - nodeid); |
| 2161 | LOGVAL(LYE_PATH_INNODE, LY_VLOG_STR, str); |
| 2162 | free(str); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2163 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2164 | } |
| 2165 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2166 | 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] | 2167 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2168 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2169 | } |
| 2170 | id += r; |
| 2171 | } |
| 2172 | |
| 2173 | /* cannot get here */ |
| 2174 | LOGINT; |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2175 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2176 | } |
| 2177 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2178 | static int |
| 2179 | resolve_partial_json_data_list_predicate(const char *predicate, const char *node_name, struct lyd_node *node, int *parsed) |
| 2180 | { |
| 2181 | const char *name, *value; |
| 2182 | int nam_len, val_len, has_predicate = 1, r; |
| 2183 | uint16_t i; |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2184 | struct lyd_node_leaf_list *key; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2185 | |
Radek Krejci | 61a86c6 | 2016-03-24 11:06:44 +0100 | [diff] [blame] | 2186 | assert(node); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2187 | assert(node->schema->nodetype == LYS_LIST); |
| 2188 | |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2189 | key = (struct lyd_node_leaf_list *)node->child; |
| 2190 | for (i = 0; i < ((struct lys_node_list *)node->schema)->keys_size; ++i) { |
| 2191 | if (!key) { |
| 2192 | /* invalid data */ |
| 2193 | LOGINT; |
| 2194 | return -1; |
| 2195 | } |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2196 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2197 | if (!has_predicate) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2198 | LOGVAL(LYE_PATH_MISSKEY, LY_VLOG_NONE, NULL, node_name); |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2199 | return -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2200 | } |
| 2201 | |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 2202 | if (((r = parse_schema_json_predicate(predicate, &name, &nam_len, &value, &val_len, &has_predicate)) < 1) |
| 2203 | || !strncmp(name, ".", nam_len)) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2204 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-r], &predicate[-r]); |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2205 | return -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2206 | } |
| 2207 | |
| 2208 | predicate += r; |
| 2209 | *parsed += r; |
| 2210 | |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2211 | 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] | 2212 | LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name); |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2213 | return -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2214 | } |
| 2215 | |
| 2216 | /* value does not match */ |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2217 | 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] | 2218 | return 1; |
| 2219 | } |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2220 | |
| 2221 | key = (struct lyd_node_leaf_list *)key->next; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2222 | } |
| 2223 | |
| 2224 | if (has_predicate) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2225 | LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2226 | return -1; |
| 2227 | } |
| 2228 | |
| 2229 | return 0; |
| 2230 | } |
| 2231 | |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 2232 | /** |
| 2233 | * @brief get the closest parent of the node (or the node itself) identified by the nodeid (path) |
| 2234 | * |
| 2235 | * @param[in] nodeid Node data path to find |
| 2236 | * @param[in] llist_value If the \p nodeid identifies leaf-list, this is expected value of the leaf-list instance. |
| 2237 | * @param[in] options Bitmask of options flags, see @ref pathoptions. |
| 2238 | * @param[out] parsed Number of characters processed in \p id |
| 2239 | * @return The closes parent (or the node itself) from the path |
| 2240 | */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2241 | struct lyd_node * |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2242 | resolve_partial_json_data_nodeid(const char *nodeid, const char *llist_value, struct lyd_node *start, int options, |
| 2243 | int *parsed) |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2244 | { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2245 | char *module_name = ly_buf(), *buf_backup = NULL, *str; |
Michal Vasko | 0a8d17f | 2016-10-19 15:51:36 +0200 | [diff] [blame] | 2246 | const char *id, *mod_name, *name, *pred_name; |
| 2247 | int r, ret, mod_name_len, nam_len, is_relative = -1; |
| 2248 | 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] | 2249 | struct lyd_node *sibling, *last_match = NULL; |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2250 | struct lyd_node_leaf_list *llist; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2251 | const struct lys_module *prefix_mod, *prev_mod; |
| 2252 | struct ly_ctx *ctx; |
| 2253 | |
| 2254 | assert(nodeid && start && parsed); |
| 2255 | |
| 2256 | ctx = start->schema->module->ctx; |
| 2257 | id = nodeid; |
| 2258 | |
| 2259 | 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] | 2260 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2261 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2262 | return NULL; |
| 2263 | } |
| 2264 | id += r; |
| 2265 | /* add it to parsed only after the data node was actually found */ |
| 2266 | last_parsed = r; |
| 2267 | |
| 2268 | if (is_relative) { |
| 2269 | prev_mod = start->schema->module; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2270 | start = start->child; |
| 2271 | } else { |
| 2272 | for (; start->parent; start = start->parent); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2273 | prev_mod = start->schema->module; |
| 2274 | } |
| 2275 | |
| 2276 | while (1) { |
| 2277 | LY_TREE_FOR(start, sibling) { |
Michal Vasko | 945b96b | 2016-10-18 11:49:12 +0200 | [diff] [blame] | 2278 | /* 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] | 2279 | if (lys_parent(sibling->schema)) { |
| 2280 | if (options & LYD_PATH_OPT_OUTPUT) { |
| 2281 | if (lys_parent(sibling->schema)->nodetype == LYS_INPUT) { |
Michal Vasko | b724689 | 2016-04-19 10:37:35 +0200 | [diff] [blame] | 2282 | 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] | 2283 | *parsed = -1; |
| 2284 | return NULL; |
| 2285 | } |
| 2286 | } else { |
| 2287 | if (lys_parent(sibling->schema)->nodetype == LYS_OUTPUT) { |
Michal Vasko | b724689 | 2016-04-19 10:37:35 +0200 | [diff] [blame] | 2288 | 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] | 2289 | *parsed = -1; |
| 2290 | return NULL; |
| 2291 | } |
| 2292 | } |
| 2293 | } |
| 2294 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2295 | /* name match */ |
| 2296 | if (!strncmp(name, sibling->schema->name, nam_len) && !sibling->schema->name[nam_len]) { |
| 2297 | |
| 2298 | /* module check */ |
| 2299 | if (mod_name) { |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2300 | if (mod_name_len > LY_BUF_SIZE - 1) { |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2301 | LOGINT; |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2302 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2303 | return NULL; |
| 2304 | } |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2305 | |
| 2306 | if (ly_buf_used && module_name[0]) { |
| 2307 | buf_backup = strndup(module_name, LY_BUF_SIZE - 1); |
| 2308 | } |
| 2309 | ly_buf_used++; |
| 2310 | |
Michal Vasko | df3f1ab | 2016-04-01 15:07:22 +0200 | [diff] [blame] | 2311 | memmove(module_name, mod_name, mod_name_len); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2312 | module_name[mod_name_len] = '\0'; |
| 2313 | /* will also find an augment module */ |
| 2314 | prefix_mod = ly_ctx_get_module(ctx, module_name, NULL); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2315 | |
| 2316 | if (buf_backup) { |
| 2317 | /* return previous internal buffer content */ |
Michal Vasko | 888a129 | 2016-04-05 12:08:54 +0200 | [diff] [blame] | 2318 | strncpy(module_name, buf_backup, LY_BUF_SIZE - 1); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2319 | free(buf_backup); |
| 2320 | buf_backup = NULL; |
| 2321 | } |
| 2322 | ly_buf_used--; |
| 2323 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2324 | if (!prefix_mod) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2325 | str = strndup(nodeid, (mod_name + mod_name_len) - nodeid); |
| 2326 | LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str); |
| 2327 | free(str); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2328 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2329 | return NULL; |
| 2330 | } |
| 2331 | } else { |
| 2332 | prefix_mod = prev_mod; |
| 2333 | } |
| 2334 | if (prefix_mod != lys_node_module(sibling->schema)) { |
| 2335 | continue; |
| 2336 | } |
| 2337 | |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2338 | /* leaf-list, did we find it with the correct value or not? */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2339 | if (sibling->schema->nodetype == LYS_LEAFLIST) { |
Michal Vasko | 0a8d17f | 2016-10-19 15:51:36 +0200 | [diff] [blame] | 2340 | last_has_pred = 0; |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2341 | if (has_predicate) { |
Michal Vasko | 0a8d17f | 2016-10-19 15:51:36 +0200 | [diff] [blame] | 2342 | if ((r = parse_schema_json_predicate(id, &pred_name, &pred_name_len, &llist_value, &val_len, &last_has_pred)) < 1) { |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2343 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
| 2344 | *parsed = -1; |
| 2345 | return NULL; |
| 2346 | } |
Michal Vasko | 0a8d17f | 2016-10-19 15:51:36 +0200 | [diff] [blame] | 2347 | if ((pred_name[0] != '.') || (pred_name_len != 1)) { |
| 2348 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[1], id + 1); |
| 2349 | *parsed = -1; |
| 2350 | return NULL; |
| 2351 | } |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2352 | } else { |
| 2353 | r = 0; |
| 2354 | if (llist_value) { |
| 2355 | val_len = strlen(llist_value); |
| 2356 | } else { |
| 2357 | val_len = 0; |
| 2358 | } |
| 2359 | } |
| 2360 | |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2361 | llist = (struct lyd_node_leaf_list *)sibling; |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2362 | if ((!val_len && llist->value_str && llist->value_str[0]) |
| 2363 | || (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] | 2364 | continue; |
| 2365 | } |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2366 | id += r; |
| 2367 | last_parsed += r; |
Michal Vasko | 0a8d17f | 2016-10-19 15:51:36 +0200 | [diff] [blame] | 2368 | has_predicate = last_has_pred; |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2369 | |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 2370 | } else if (sibling->schema->nodetype == LYS_LIST) { |
| 2371 | /* list, we need predicates'n'stuff then */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2372 | r = 0; |
| 2373 | if (!has_predicate) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2374 | LOGVAL(LYE_PATH_MISSKEY, LY_VLOG_NONE, NULL, name); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2375 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2376 | return NULL; |
| 2377 | } |
| 2378 | ret = resolve_partial_json_data_list_predicate(id, name, sibling, &r); |
| 2379 | if (ret == -1) { |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2380 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2381 | return NULL; |
| 2382 | } else if (ret == 1) { |
| 2383 | /* this list instance does not match */ |
| 2384 | continue; |
| 2385 | } |
| 2386 | id += r; |
| 2387 | last_parsed += r; |
| 2388 | } |
| 2389 | |
| 2390 | *parsed += last_parsed; |
| 2391 | |
| 2392 | /* the result node? */ |
| 2393 | if (!id[0]) { |
| 2394 | return sibling; |
| 2395 | } |
| 2396 | |
| 2397 | /* move down the tree, if possible */ |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 2398 | if (sibling->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2399 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2400 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2401 | return NULL; |
| 2402 | } |
Michal Vasko | c6c52cf | 2016-03-29 11:53:04 +0200 | [diff] [blame] | 2403 | last_match = sibling; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2404 | start = sibling->child; |
| 2405 | if (start) { |
| 2406 | prev_mod = start->schema->module; |
| 2407 | } |
| 2408 | break; |
| 2409 | } |
| 2410 | } |
| 2411 | |
| 2412 | /* no match, return last match */ |
| 2413 | if (!sibling) { |
| 2414 | return last_match; |
| 2415 | } |
| 2416 | |
| 2417 | 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] | 2418 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2419 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2420 | return NULL; |
| 2421 | } |
| 2422 | id += r; |
| 2423 | last_parsed = r; |
| 2424 | } |
| 2425 | |
| 2426 | /* cannot get here */ |
| 2427 | LOGINT; |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2428 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2429 | return NULL; |
| 2430 | } |
| 2431 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2432 | /** |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2433 | * @brief Resolves length or range intervals. Does not log. |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2434 | * Syntax is assumed to be correct, *ret MUST be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2435 | * |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2436 | * @param[in] str_restr Restriction as a string. |
| 2437 | * @param[in] type Type of the restriction. |
| 2438 | * @param[out] ret Final interval structure that starts with |
| 2439 | * the interval of the initial type, continues with intervals |
| 2440 | * of any superior types derived from the initial one, and |
| 2441 | * finishes with intervals from our \p type. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2442 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2443 | * @return EXIT_SUCCESS on succes, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2444 | */ |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2445 | int |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2446 | 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] | 2447 | { |
| 2448 | /* 0 - unsigned, 1 - signed, 2 - floating point */ |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2449 | int kind; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2450 | int64_t local_smin, local_smax, local_fmin, local_fmax; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2451 | uint64_t local_umin, local_umax; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2452 | uint8_t local_fdig; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2453 | const char *seg_ptr, *ptr; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2454 | 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] | 2455 | |
| 2456 | switch (type->base) { |
| 2457 | case LY_TYPE_BINARY: |
| 2458 | kind = 0; |
| 2459 | local_umin = 0; |
| 2460 | local_umax = 18446744073709551615UL; |
| 2461 | |
| 2462 | if (!str_restr && type->info.binary.length) { |
| 2463 | str_restr = type->info.binary.length->expr; |
| 2464 | } |
| 2465 | break; |
| 2466 | case LY_TYPE_DEC64: |
| 2467 | kind = 2; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2468 | local_fmin = __INT64_C(-9223372036854775807) - __INT64_C(1); |
| 2469 | local_fmax = __INT64_C(9223372036854775807); |
| 2470 | local_fdig = type->info.dec64.dig; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2471 | |
| 2472 | if (!str_restr && type->info.dec64.range) { |
| 2473 | str_restr = type->info.dec64.range->expr; |
| 2474 | } |
| 2475 | break; |
| 2476 | case LY_TYPE_INT8: |
| 2477 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2478 | local_smin = __INT64_C(-128); |
| 2479 | local_smax = __INT64_C(127); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2480 | |
| 2481 | if (!str_restr && type->info.num.range) { |
| 2482 | str_restr = type->info.num.range->expr; |
| 2483 | } |
| 2484 | break; |
| 2485 | case LY_TYPE_INT16: |
| 2486 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2487 | local_smin = __INT64_C(-32768); |
| 2488 | local_smax = __INT64_C(32767); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2489 | |
| 2490 | if (!str_restr && type->info.num.range) { |
| 2491 | str_restr = type->info.num.range->expr; |
| 2492 | } |
| 2493 | break; |
| 2494 | case LY_TYPE_INT32: |
| 2495 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2496 | local_smin = __INT64_C(-2147483648); |
| 2497 | local_smax = __INT64_C(2147483647); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2498 | |
| 2499 | if (!str_restr && type->info.num.range) { |
| 2500 | str_restr = type->info.num.range->expr; |
| 2501 | } |
| 2502 | break; |
| 2503 | case LY_TYPE_INT64: |
| 2504 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2505 | local_smin = __INT64_C(-9223372036854775807) - __INT64_C(1); |
| 2506 | local_smax = __INT64_C(9223372036854775807); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2507 | |
| 2508 | if (!str_restr && type->info.num.range) { |
| 2509 | str_restr = type->info.num.range->expr; |
| 2510 | } |
| 2511 | break; |
| 2512 | case LY_TYPE_UINT8: |
| 2513 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2514 | local_umin = __UINT64_C(0); |
| 2515 | local_umax = __UINT64_C(255); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2516 | |
| 2517 | if (!str_restr && type->info.num.range) { |
| 2518 | str_restr = type->info.num.range->expr; |
| 2519 | } |
| 2520 | break; |
| 2521 | case LY_TYPE_UINT16: |
| 2522 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2523 | local_umin = __UINT64_C(0); |
| 2524 | local_umax = __UINT64_C(65535); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2525 | |
| 2526 | if (!str_restr && type->info.num.range) { |
| 2527 | str_restr = type->info.num.range->expr; |
| 2528 | } |
| 2529 | break; |
| 2530 | case LY_TYPE_UINT32: |
| 2531 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2532 | local_umin = __UINT64_C(0); |
| 2533 | local_umax = __UINT64_C(4294967295); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2534 | |
| 2535 | if (!str_restr && type->info.num.range) { |
| 2536 | str_restr = type->info.num.range->expr; |
| 2537 | } |
| 2538 | break; |
| 2539 | case LY_TYPE_UINT64: |
| 2540 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2541 | local_umin = __UINT64_C(0); |
| 2542 | local_umax = __UINT64_C(18446744073709551615); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2543 | |
| 2544 | if (!str_restr && type->info.num.range) { |
| 2545 | str_restr = type->info.num.range->expr; |
| 2546 | } |
| 2547 | break; |
| 2548 | case LY_TYPE_STRING: |
| 2549 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2550 | local_umin = __UINT64_C(0); |
| 2551 | local_umax = __UINT64_C(18446744073709551615); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2552 | |
| 2553 | if (!str_restr && type->info.str.length) { |
| 2554 | str_restr = type->info.str.length->expr; |
| 2555 | } |
| 2556 | break; |
| 2557 | default: |
| 2558 | LOGINT; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2559 | return -1; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2560 | } |
| 2561 | |
| 2562 | /* process superior types */ |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2563 | if (type->der) { |
| 2564 | if (resolve_len_ran_interval(NULL, &type->der->type, &intv)) { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2565 | LOGINT; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2566 | return -1; |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2567 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2568 | assert(!intv || (intv->kind == kind)); |
| 2569 | } |
| 2570 | |
| 2571 | if (!str_restr) { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2572 | /* we do not have any restriction, return superior ones */ |
| 2573 | *ret = intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2574 | return EXIT_SUCCESS; |
| 2575 | } |
| 2576 | |
| 2577 | /* adjust local min and max */ |
| 2578 | if (intv) { |
| 2579 | tmp_intv = intv; |
| 2580 | |
| 2581 | if (kind == 0) { |
| 2582 | local_umin = tmp_intv->value.uval.min; |
| 2583 | } else if (kind == 1) { |
| 2584 | local_smin = tmp_intv->value.sval.min; |
| 2585 | } else if (kind == 2) { |
| 2586 | local_fmin = tmp_intv->value.fval.min; |
| 2587 | } |
| 2588 | |
| 2589 | while (tmp_intv->next) { |
| 2590 | tmp_intv = tmp_intv->next; |
| 2591 | } |
| 2592 | |
| 2593 | if (kind == 0) { |
| 2594 | local_umax = tmp_intv->value.uval.max; |
| 2595 | } else if (kind == 1) { |
| 2596 | local_smax = tmp_intv->value.sval.max; |
| 2597 | } else if (kind == 2) { |
| 2598 | local_fmax = tmp_intv->value.fval.max; |
| 2599 | } |
| 2600 | } |
| 2601 | |
| 2602 | /* finally parse our restriction */ |
| 2603 | seg_ptr = str_restr; |
Pavol Vican | 9e7c01d | 2016-08-29 09:36:17 +0200 | [diff] [blame] | 2604 | tmp_intv = NULL; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2605 | while (1) { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2606 | if (!tmp_local_intv) { |
| 2607 | assert(!local_intv); |
| 2608 | local_intv = malloc(sizeof *local_intv); |
| 2609 | tmp_local_intv = local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2610 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2611 | tmp_local_intv->next = malloc(sizeof *tmp_local_intv); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2612 | tmp_local_intv = tmp_local_intv->next; |
| 2613 | } |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2614 | if (!tmp_local_intv) { |
| 2615 | LOGMEM; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2616 | goto error; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2617 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2618 | |
| 2619 | tmp_local_intv->kind = kind; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2620 | tmp_local_intv->type = type; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2621 | tmp_local_intv->next = NULL; |
| 2622 | |
| 2623 | /* min */ |
| 2624 | ptr = seg_ptr; |
| 2625 | while (isspace(ptr[0])) { |
| 2626 | ++ptr; |
| 2627 | } |
| 2628 | if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) { |
| 2629 | if (kind == 0) { |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2630 | tmp_local_intv->value.uval.min = strtol(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2631 | } else if (kind == 1) { |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2632 | tmp_local_intv->value.sval.min = strtol(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2633 | } else if (kind == 2) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 2634 | if (parse_range_dec64(&ptr, local_fdig, &tmp_local_intv->value.fval.min)) { |
| 2635 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, ptr, "range"); |
| 2636 | goto error; |
| 2637 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2638 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2639 | } else if (!strncmp(ptr, "min", 3)) { |
| 2640 | if (kind == 0) { |
| 2641 | tmp_local_intv->value.uval.min = local_umin; |
| 2642 | } else if (kind == 1) { |
| 2643 | tmp_local_intv->value.sval.min = local_smin; |
| 2644 | } else if (kind == 2) { |
| 2645 | tmp_local_intv->value.fval.min = local_fmin; |
| 2646 | } |
| 2647 | |
| 2648 | ptr += 3; |
| 2649 | } else if (!strncmp(ptr, "max", 3)) { |
| 2650 | if (kind == 0) { |
| 2651 | tmp_local_intv->value.uval.min = local_umax; |
| 2652 | } else if (kind == 1) { |
| 2653 | tmp_local_intv->value.sval.min = local_smax; |
| 2654 | } else if (kind == 2) { |
| 2655 | tmp_local_intv->value.fval.min = local_fmax; |
| 2656 | } |
| 2657 | |
| 2658 | ptr += 3; |
| 2659 | } else { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2660 | LOGINT; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2661 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2662 | } |
| 2663 | |
| 2664 | while (isspace(ptr[0])) { |
| 2665 | ptr++; |
| 2666 | } |
| 2667 | |
| 2668 | /* no interval or interval */ |
| 2669 | if ((ptr[0] == '|') || !ptr[0]) { |
| 2670 | if (kind == 0) { |
| 2671 | tmp_local_intv->value.uval.max = tmp_local_intv->value.uval.min; |
| 2672 | } else if (kind == 1) { |
| 2673 | tmp_local_intv->value.sval.max = tmp_local_intv->value.sval.min; |
| 2674 | } else if (kind == 2) { |
| 2675 | tmp_local_intv->value.fval.max = tmp_local_intv->value.fval.min; |
| 2676 | } |
| 2677 | } else if (!strncmp(ptr, "..", 2)) { |
| 2678 | /* skip ".." */ |
| 2679 | ptr += 2; |
| 2680 | while (isspace(ptr[0])) { |
| 2681 | ++ptr; |
| 2682 | } |
| 2683 | |
| 2684 | /* max */ |
| 2685 | if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) { |
| 2686 | if (kind == 0) { |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2687 | tmp_local_intv->value.uval.max = strtol(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2688 | } else if (kind == 1) { |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2689 | tmp_local_intv->value.sval.max = strtol(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2690 | } else if (kind == 2) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 2691 | if (parse_range_dec64(&ptr, local_fdig, &tmp_local_intv->value.fval.max)) { |
| 2692 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, ptr, "range"); |
| 2693 | goto error; |
| 2694 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2695 | } |
| 2696 | } else if (!strncmp(ptr, "max", 3)) { |
| 2697 | if (kind == 0) { |
| 2698 | tmp_local_intv->value.uval.max = local_umax; |
| 2699 | } else if (kind == 1) { |
| 2700 | tmp_local_intv->value.sval.max = local_smax; |
| 2701 | } else if (kind == 2) { |
| 2702 | tmp_local_intv->value.fval.max = local_fmax; |
| 2703 | } |
| 2704 | } else { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2705 | LOGINT; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2706 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2707 | } |
| 2708 | } else { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2709 | LOGINT; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2710 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2711 | } |
| 2712 | |
Pavol Vican | 9e7c01d | 2016-08-29 09:36:17 +0200 | [diff] [blame] | 2713 | /* check min and max in correct order*/ |
| 2714 | if (kind == 0) { |
| 2715 | /* current segment */ |
| 2716 | if (tmp_local_intv->value.uval.min > tmp_local_intv->value.uval.max) { |
| 2717 | goto error; |
| 2718 | } |
| 2719 | if (tmp_local_intv->value.uval.min < local_umin || tmp_local_intv->value.uval.max > local_umax) { |
| 2720 | goto error; |
| 2721 | } |
| 2722 | /* segments sholud be ascending order */ |
Pavol Vican | 69f62c9 | 2016-08-30 09:06:25 +0200 | [diff] [blame] | 2723 | 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] | 2724 | goto error; |
| 2725 | } |
| 2726 | } else if (kind == 1) { |
| 2727 | if (tmp_local_intv->value.sval.min > tmp_local_intv->value.sval.max) { |
| 2728 | goto error; |
| 2729 | } |
| 2730 | if (tmp_local_intv->value.sval.min < local_smin || tmp_local_intv->value.sval.max > local_smax) { |
| 2731 | goto error; |
| 2732 | } |
Pavol Vican | 69f62c9 | 2016-08-30 09:06:25 +0200 | [diff] [blame] | 2733 | 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] | 2734 | goto error; |
| 2735 | } |
| 2736 | } else if (kind == 2) { |
| 2737 | if (tmp_local_intv->value.fval.min > tmp_local_intv->value.fval.max) { |
| 2738 | goto error; |
| 2739 | } |
| 2740 | if (tmp_local_intv->value.fval.min < local_fmin || tmp_local_intv->value.fval.max > local_fmax) { |
| 2741 | goto error; |
| 2742 | } |
Pavol Vican | 69f62c9 | 2016-08-30 09:06:25 +0200 | [diff] [blame] | 2743 | 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] | 2744 | /* 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] | 2745 | goto error; |
| 2746 | } |
| 2747 | } |
| 2748 | |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2749 | /* next segment (next OR) */ |
| 2750 | seg_ptr = strchr(seg_ptr, '|'); |
| 2751 | if (!seg_ptr) { |
| 2752 | break; |
| 2753 | } |
| 2754 | seg_ptr++; |
Pavol Vican | 9e7c01d | 2016-08-29 09:36:17 +0200 | [diff] [blame] | 2755 | tmp_intv = tmp_local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2756 | } |
| 2757 | |
| 2758 | /* check local restrictions against superior ones */ |
| 2759 | if (intv) { |
| 2760 | tmp_intv = intv; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2761 | tmp_local_intv = local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2762 | |
| 2763 | while (tmp_local_intv && tmp_intv) { |
| 2764 | /* reuse local variables */ |
| 2765 | if (kind == 0) { |
| 2766 | local_umin = tmp_local_intv->value.uval.min; |
| 2767 | local_umax = tmp_local_intv->value.uval.max; |
| 2768 | |
| 2769 | /* it must be in this interval */ |
| 2770 | if ((local_umin >= tmp_intv->value.uval.min) && (local_umin <= tmp_intv->value.uval.max)) { |
| 2771 | /* this interval is covered, next one */ |
| 2772 | if (local_umax <= tmp_intv->value.uval.max) { |
| 2773 | tmp_local_intv = tmp_local_intv->next; |
| 2774 | continue; |
| 2775 | /* ascending order of restrictions -> fail */ |
| 2776 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2777 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2778 | } |
| 2779 | } |
| 2780 | } else if (kind == 1) { |
| 2781 | local_smin = tmp_local_intv->value.sval.min; |
| 2782 | local_smax = tmp_local_intv->value.sval.max; |
| 2783 | |
| 2784 | if ((local_smin >= tmp_intv->value.sval.min) && (local_smin <= tmp_intv->value.sval.max)) { |
| 2785 | if (local_smax <= tmp_intv->value.sval.max) { |
| 2786 | tmp_local_intv = tmp_local_intv->next; |
| 2787 | continue; |
| 2788 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2789 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2790 | } |
| 2791 | } |
| 2792 | } else if (kind == 2) { |
| 2793 | local_fmin = tmp_local_intv->value.fval.min; |
| 2794 | local_fmax = tmp_local_intv->value.fval.max; |
| 2795 | |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2796 | 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] | 2797 | && (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] | 2798 | 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] | 2799 | tmp_local_intv = tmp_local_intv->next; |
| 2800 | continue; |
| 2801 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2802 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2803 | } |
| 2804 | } |
| 2805 | } |
| 2806 | |
| 2807 | tmp_intv = tmp_intv->next; |
| 2808 | } |
| 2809 | |
| 2810 | /* some interval left uncovered -> fail */ |
| 2811 | if (tmp_local_intv) { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2812 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2813 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2814 | } |
| 2815 | |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2816 | /* append the local intervals to all the intervals of the superior types, return it all */ |
| 2817 | if (intv) { |
| 2818 | for (tmp_intv = intv; tmp_intv->next; tmp_intv = tmp_intv->next); |
| 2819 | tmp_intv->next = local_intv; |
| 2820 | } else { |
| 2821 | intv = local_intv; |
| 2822 | } |
| 2823 | *ret = intv; |
| 2824 | |
| 2825 | return EXIT_SUCCESS; |
| 2826 | |
| 2827 | error: |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2828 | while (intv) { |
| 2829 | tmp_intv = intv->next; |
| 2830 | free(intv); |
| 2831 | intv = tmp_intv; |
| 2832 | } |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2833 | while (local_intv) { |
| 2834 | tmp_local_intv = local_intv->next; |
| 2835 | free(local_intv); |
| 2836 | local_intv = tmp_local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2837 | } |
| 2838 | |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2839 | return -1; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2840 | } |
| 2841 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2842 | /** |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2843 | * @brief Resolve a typedef, return only resolved typedefs if derived. If leafref, it must be |
| 2844 | * resolved for this function to return it. Does not log. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2845 | * |
| 2846 | * @param[in] name Typedef name. |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2847 | * @param[in] mod_name Typedef name module name. |
| 2848 | * @param[in] module Main module. |
| 2849 | * @param[in] parent Parent of the resolved type definition. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2850 | * @param[out] ret Pointer to the resolved typedef. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2851 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2852 | * @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] | 2853 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2854 | int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 2855 | resolve_superior_type(const char *name, const char *mod_name, const struct lys_module *module, |
| 2856 | const struct lys_node *parent, struct lys_tpdf **ret) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2857 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2858 | int i, j; |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2859 | struct lys_tpdf *tpdf, *match; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2860 | int tpdf_size; |
| 2861 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2862 | if (!mod_name) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2863 | /* no prefix, try built-in types */ |
| 2864 | for (i = 1; i < LY_DATA_TYPE_COUNT; i++) { |
| 2865 | if (!strcmp(ly_types[i].def->name, name)) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2866 | if (ret) { |
| 2867 | *ret = ly_types[i].def; |
| 2868 | } |
| 2869 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2870 | } |
| 2871 | } |
| 2872 | } else { |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2873 | if (!strcmp(mod_name, module->name)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2874 | /* prefix refers to the current module, ignore it */ |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2875 | mod_name = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2876 | } |
| 2877 | } |
| 2878 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2879 | if (!mod_name && parent) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2880 | /* search in local typedefs */ |
| 2881 | while (parent) { |
| 2882 | switch (parent->nodetype) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2883 | case LYS_CONTAINER: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 2884 | tpdf_size = ((struct lys_node_container *)parent)->tpdf_size; |
| 2885 | tpdf = ((struct lys_node_container *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2886 | break; |
| 2887 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2888 | case LYS_LIST: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 2889 | tpdf_size = ((struct lys_node_list *)parent)->tpdf_size; |
| 2890 | tpdf = ((struct lys_node_list *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2891 | break; |
| 2892 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2893 | case LYS_GROUPING: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 2894 | tpdf_size = ((struct lys_node_grp *)parent)->tpdf_size; |
| 2895 | tpdf = ((struct lys_node_grp *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2896 | break; |
| 2897 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2898 | case LYS_RPC: |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 2899 | case LYS_ACTION: |
| 2900 | tpdf_size = ((struct lys_node_rpc_action *)parent)->tpdf_size; |
| 2901 | tpdf = ((struct lys_node_rpc_action *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2902 | break; |
| 2903 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2904 | case LYS_NOTIF: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 2905 | tpdf_size = ((struct lys_node_notif *)parent)->tpdf_size; |
| 2906 | tpdf = ((struct lys_node_notif *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2907 | break; |
| 2908 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2909 | case LYS_INPUT: |
| 2910 | case LYS_OUTPUT: |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 2911 | tpdf_size = ((struct lys_node_inout *)parent)->tpdf_size; |
| 2912 | tpdf = ((struct lys_node_inout *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2913 | break; |
| 2914 | |
| 2915 | default: |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 2916 | parent = lys_parent(parent); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2917 | continue; |
| 2918 | } |
| 2919 | |
| 2920 | for (i = 0; i < tpdf_size; i++) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 2921 | if (!strcmp(tpdf[i].name, name) && tpdf[i].type.base > 0) { |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2922 | match = &tpdf[i]; |
| 2923 | goto check_leafref; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2924 | } |
| 2925 | } |
| 2926 | |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 2927 | parent = lys_parent(parent); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2928 | } |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2929 | } else { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2930 | /* get module where to search */ |
Michal Vasko | b6729c6 | 2015-10-21 12:09:47 +0200 | [diff] [blame] | 2931 | module = lys_get_import_module(module, NULL, 0, mod_name, 0); |
Michal Vasko | c935fff | 2015-08-17 14:02:06 +0200 | [diff] [blame] | 2932 | if (!module) { |
| 2933 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2934 | } |
| 2935 | } |
| 2936 | |
| 2937 | /* search in top level typedefs */ |
| 2938 | for (i = 0; i < module->tpdf_size; i++) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 2939 | 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] | 2940 | match = &module->tpdf[i]; |
| 2941 | goto check_leafref; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2942 | } |
| 2943 | } |
| 2944 | |
| 2945 | /* search in submodules */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2946 | for (i = 0; i < module->inc_size && module->inc[i].submodule; i++) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2947 | for (j = 0; j < module->inc[i].submodule->tpdf_size; j++) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 2948 | 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] | 2949 | match = &module->inc[i].submodule->tpdf[j]; |
| 2950 | goto check_leafref; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2951 | } |
| 2952 | } |
| 2953 | } |
| 2954 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2955 | return EXIT_FAILURE; |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2956 | |
| 2957 | check_leafref: |
| 2958 | if (ret) { |
| 2959 | *ret = match; |
| 2960 | } |
| 2961 | if (match->type.base == LY_TYPE_LEAFREF) { |
| 2962 | while (!match->type.info.lref.path) { |
| 2963 | match = match->type.der; |
| 2964 | assert(match); |
| 2965 | } |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2966 | } |
| 2967 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2968 | } |
| 2969 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2970 | /** |
| 2971 | * @brief Check the default \p value of the \p type. Logs directly. |
| 2972 | * |
| 2973 | * @param[in] type Type definition to use. |
| 2974 | * @param[in] value Default value to check. |
Michal Vasko | 5682640 | 2016-03-02 11:11:37 +0100 | [diff] [blame] | 2975 | * @param[in] module Type module. |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2976 | * |
| 2977 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 2978 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2979 | static int |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 2980 | 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] | 2981 | { |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 2982 | struct lys_tpdf *base_tpdf = NULL; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2983 | struct lyd_node_leaf_list node; |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 2984 | const char *dflt = NULL; |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 2985 | int ret = EXIT_SUCCESS; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2986 | |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 2987 | assert(value); |
| 2988 | |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 2989 | if (type->base <= LY_TYPE_DER) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 2990 | /* the type was not resolved yet, nothing to do for now */ |
| 2991 | return EXIT_FAILURE; |
| 2992 | } |
| 2993 | |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 2994 | dflt = *value; |
| 2995 | if (!dflt) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 2996 | /* we do not have a new default value, so is there any to check even, in some base type? */ |
| 2997 | for (base_tpdf = type->der; base_tpdf->type.der; base_tpdf = base_tpdf->type.der) { |
| 2998 | if (base_tpdf->dflt) { |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 2999 | dflt = base_tpdf->dflt; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3000 | break; |
| 3001 | } |
| 3002 | } |
| 3003 | |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3004 | if (!dflt) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3005 | /* no default value, nothing to check, all is well */ |
| 3006 | return EXIT_SUCCESS; |
| 3007 | } |
| 3008 | |
| 3009 | /* 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)? */ |
| 3010 | switch (type->base) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3011 | case LY_TYPE_IDENT: |
| 3012 | case LY_TYPE_INST: |
| 3013 | case LY_TYPE_LEAFREF: |
| 3014 | case LY_TYPE_BOOL: |
| 3015 | case LY_TYPE_EMPTY: |
| 3016 | /* these have no restrictions, so we would do the exact same work as the unres in the base typedef */ |
| 3017 | return EXIT_SUCCESS; |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3018 | case LY_TYPE_BITS: |
| 3019 | /* the default value must match the restricted list of values, if the type was restricted */ |
| 3020 | if (type->info.bits.count) { |
| 3021 | break; |
| 3022 | } |
| 3023 | return EXIT_SUCCESS; |
| 3024 | case LY_TYPE_ENUM: |
| 3025 | /* the default value must match the restricted list of values, if the type was restricted */ |
| 3026 | if (type->info.enums.count) { |
| 3027 | break; |
| 3028 | } |
| 3029 | return EXIT_SUCCESS; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3030 | case LY_TYPE_DEC64: |
| 3031 | if (type->info.dec64.range) { |
| 3032 | break; |
| 3033 | } |
| 3034 | return EXIT_SUCCESS; |
| 3035 | case LY_TYPE_BINARY: |
| 3036 | if (type->info.binary.length) { |
| 3037 | break; |
| 3038 | } |
| 3039 | return EXIT_SUCCESS; |
| 3040 | case LY_TYPE_INT8: |
| 3041 | case LY_TYPE_INT16: |
| 3042 | case LY_TYPE_INT32: |
| 3043 | case LY_TYPE_INT64: |
| 3044 | case LY_TYPE_UINT8: |
| 3045 | case LY_TYPE_UINT16: |
| 3046 | case LY_TYPE_UINT32: |
| 3047 | case LY_TYPE_UINT64: |
| 3048 | if (type->info.num.range) { |
| 3049 | break; |
| 3050 | } |
| 3051 | return EXIT_SUCCESS; |
| 3052 | case LY_TYPE_STRING: |
| 3053 | if (type->info.str.length || type->info.str.patterns) { |
| 3054 | break; |
| 3055 | } |
| 3056 | return EXIT_SUCCESS; |
| 3057 | case LY_TYPE_UNION: |
| 3058 | /* way too much trouble learning whether we need to check the default again, so just do it */ |
| 3059 | break; |
| 3060 | default: |
| 3061 | LOGINT; |
| 3062 | return -1; |
| 3063 | } |
Radek Krejci | 55a161c | 2016-09-05 17:13:25 +0200 | [diff] [blame] | 3064 | } else if (type->base == LY_TYPE_EMPTY) { |
| 3065 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "default", type->parent->name); |
| 3066 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The \"empty\" data type cannot have a default value."); |
| 3067 | return -1; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3068 | } |
| 3069 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3070 | /* dummy leaf */ |
Radek Krejci | 4fe36bd | 2016-03-17 16:47:16 +0100 | [diff] [blame] | 3071 | memset(&node, 0, sizeof node); |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3072 | node.value_str = dflt; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3073 | node.value_type = type->base; |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 3074 | node.schema = calloc(1, sizeof (struct lys_node_leaf)); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3075 | if (!node.schema) { |
| 3076 | LOGMEM; |
| 3077 | return -1; |
| 3078 | } |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3079 | node.schema->name = strdup("fake-default"); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3080 | if (!node.schema->name) { |
| 3081 | LOGMEM; |
Michal Vasko | f49eda0 | 2016-07-21 12:17:01 +0200 | [diff] [blame] | 3082 | free(node.schema); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3083 | return -1; |
| 3084 | } |
Michal Vasko | 5682640 | 2016-03-02 11:11:37 +0100 | [diff] [blame] | 3085 | node.schema->module = module; |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 3086 | memcpy(&((struct lys_node_leaf *)node.schema)->type, type, sizeof *type); |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3087 | |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 3088 | if (type->base == LY_TYPE_LEAFREF) { |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3089 | if (!type->info.lref.target) { |
| 3090 | ret = EXIT_FAILURE; |
| 3091 | goto finish; |
| 3092 | } |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3093 | ret = check_default(&type->info.lref.target->type, &dflt, module); |
| 3094 | if (!ret) { |
| 3095 | /* adopt possibly changed default value to its canonical form */ |
| 3096 | if (*value) { |
| 3097 | *value = dflt; |
| 3098 | } |
| 3099 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3100 | } else { |
Radek Krejci | 1899d6a | 2016-11-03 13:48:07 +0100 | [diff] [blame] | 3101 | if (!lyp_parse_value(&((struct lys_node_leaf *)node.schema)->type, &node.value_str, NULL, NULL, &node, 1, 1)) { |
Radek Krejci | 5dca593 | 2016-11-04 14:30:47 +0100 | [diff] [blame] | 3102 | /* possible forward reference */ |
| 3103 | ret = 1; |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3104 | if (base_tpdf) { |
Radek Krejci | 9ad23f4 | 2016-10-31 15:46:52 +0100 | [diff] [blame] | 3105 | /* default value is defined in some base typedef */ |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3106 | if ((type->base == LY_TYPE_BITS && type->der->type.der) || |
| 3107 | (type->base == LY_TYPE_ENUM && type->der->type.der)) { |
| 3108 | /* we have refined bits/enums */ |
| 3109 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, |
| 3110 | "Invalid value \"%s\" of the default statement inherited to \"%s\" from \"%s\" base type.", |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3111 | dflt, type->parent->name, base_tpdf->name); |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3112 | } |
| 3113 | } |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3114 | } else { |
| 3115 | /* success - adopt canonical form from the node into the default value */ |
| 3116 | if (dflt != node.value_str) { |
| 3117 | /* this can happen only if we have non-inherited default value, |
| 3118 | * inherited default values are already in canonical form */ |
| 3119 | assert(dflt == *value); |
| 3120 | *value = node.value_str; |
| 3121 | } |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 3122 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3123 | } |
| 3124 | |
| 3125 | finish: |
| 3126 | if (node.value_type == LY_TYPE_BITS) { |
| 3127 | free(node.value.bit); |
| 3128 | } |
| 3129 | free((char *)node.schema->name); |
| 3130 | free(node.schema); |
| 3131 | |
| 3132 | return ret; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3133 | } |
| 3134 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3135 | /** |
| 3136 | * @brief Check a key for mandatory attributes. Logs directly. |
| 3137 | * |
| 3138 | * @param[in] key The key to check. |
| 3139 | * @param[in] flags What flags to check. |
| 3140 | * @param[in] list The list of all the keys. |
| 3141 | * @param[in] index Index of the key in the key list. |
| 3142 | * @param[in] name The name of the keys. |
| 3143 | * @param[in] len The name length. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3144 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3145 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3146 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3147 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3148 | 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] | 3149 | { |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3150 | struct lys_node_leaf *key = list->keys[index]; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3151 | char *dup = NULL; |
| 3152 | int j; |
| 3153 | |
| 3154 | /* existence */ |
| 3155 | if (!key) { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3156 | if (name[len] != '\0') { |
| 3157 | dup = strdup(name); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3158 | if (!dup) { |
| 3159 | LOGMEM; |
| 3160 | return -1; |
| 3161 | } |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3162 | dup[len] = '\0'; |
| 3163 | name = dup; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3164 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3165 | LOGVAL(LYE_KEY_MISS, LY_VLOG_LYS, list, name); |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 3166 | free(dup); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3167 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3168 | } |
| 3169 | |
| 3170 | /* uniqueness */ |
| 3171 | for (j = index - 1; j >= 0; j--) { |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3172 | if (key == list->keys[j]) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3173 | LOGVAL(LYE_KEY_DUP, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3174 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3175 | } |
| 3176 | } |
| 3177 | |
| 3178 | /* key is a leaf */ |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3179 | if (key->nodetype != LYS_LEAF) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3180 | LOGVAL(LYE_KEY_NLEAF, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3181 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3182 | } |
| 3183 | |
| 3184 | /* type of the leaf is not built-in empty */ |
Radek Krejci | c3738db | 2016-09-15 15:51:21 +0200 | [diff] [blame] | 3185 | if (key->type.base == LY_TYPE_EMPTY && key->module->version < 2) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3186 | LOGVAL(LYE_KEY_TYPE, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3187 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3188 | } |
| 3189 | |
| 3190 | /* config attribute is the same as of the list */ |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 3191 | if ((key->flags & LYS_CONFIG_MASK) && (list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3192 | LOGVAL(LYE_KEY_CONFIG, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3193 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3194 | } |
| 3195 | |
Radek Krejci | 55e2cdc | 2016-03-11 13:51:09 +0100 | [diff] [blame] | 3196 | /* key is not placed from augment */ |
| 3197 | if (key->parent->nodetype == LYS_AUGMENT) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3198 | LOGVAL(LYE_KEY_MISS, LY_VLOG_LYS, key, key->name); |
| 3199 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, key, "Key inserted from augment."); |
Radek Krejci | 55e2cdc | 2016-03-11 13:51:09 +0100 | [diff] [blame] | 3200 | return -1; |
| 3201 | } |
| 3202 | |
Radek Krejci | 3f21ada | 2016-08-01 13:34:31 +0200 | [diff] [blame] | 3203 | /* key is not when/if-feature -conditional */ |
| 3204 | j = 0; |
| 3205 | if (key->when || (key->iffeature_size && (j = 1))) { |
| 3206 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, key, j ? "if-feature" : "when", "leaf"); |
| 3207 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, key, "Key definition cannot depend on a \"%s\" condition.", |
| 3208 | j ? "if-feature" : "when"); |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3209 | return -1; |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3210 | } |
| 3211 | |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3212 | return EXIT_SUCCESS; |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3213 | } |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3214 | |
| 3215 | /** |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3216 | * @brief Resolve (test the target exists) unique. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3217 | * |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3218 | * @param[in] parent The parent node of the unique structure. |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3219 | * @param[in] uniq_str_path One path from the unique string. |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3220 | * |
| 3221 | * @return EXIT_SUCCESS on succes, EXIT_FAILURE on forward reference, -1 on error. |
| 3222 | */ |
| 3223 | int |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3224 | 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] | 3225 | { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3226 | int rc; |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3227 | const struct lys_node *leaf = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3228 | |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 3229 | 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] | 3230 | if (rc || !leaf) { |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3231 | if (rc) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3232 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3233 | if (rc > 0) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3234 | 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] | 3235 | } else if (rc == -2) { |
Michal Vasko | c66c6d8 | 2016-04-12 11:37:31 +0200 | [diff] [blame] | 3236 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, parent, "Unique argument references list."); |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3237 | } |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3238 | rc = -1; |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3239 | } else { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3240 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
| 3241 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, parent, "Target leaf not found."); |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3242 | rc = EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3243 | } |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3244 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3245 | } |
Michal Vasko | 9bb061b | 2016-02-12 11:00:19 +0100 | [diff] [blame] | 3246 | if (leaf->nodetype != LYS_LEAF) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3247 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
| 3248 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, parent, "Target is not a leaf."); |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3249 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3250 | } |
| 3251 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3252 | /* check status */ |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3253 | 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] | 3254 | return -1; |
| 3255 | } |
| 3256 | |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3257 | /* check that all unique's targets are of the same config type */ |
| 3258 | if (*trg_type) { |
| 3259 | if (((*trg_type == 1) && (leaf->flags & LYS_CONFIG_R)) || ((*trg_type == 2) && (leaf->flags & LYS_CONFIG_W))) { |
| 3260 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
| 3261 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, parent, |
| 3262 | "Leaf \"%s\" referenced in unique statement is config %s, but previous referenced leaf is config %s.", |
| 3263 | uniq_str_path, *trg_type == 1 ? "false" : "true", *trg_type == 1 ? "true" : "false"); |
| 3264 | return -1; |
| 3265 | } |
| 3266 | } else { |
| 3267 | /* first unique */ |
| 3268 | if (leaf->flags & LYS_CONFIG_W) { |
| 3269 | *trg_type = 1; |
| 3270 | } else { |
| 3271 | *trg_type = 2; |
| 3272 | } |
| 3273 | } |
| 3274 | |
Radek Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame] | 3275 | /* set leaf's unique flag */ |
| 3276 | ((struct lys_node_leaf *)leaf)->flags |= LYS_UNIQUE; |
| 3277 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3278 | return EXIT_SUCCESS; |
| 3279 | |
| 3280 | error: |
| 3281 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3282 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3283 | } |
| 3284 | |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 3285 | void |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3286 | unres_data_del(struct unres_data *unres, uint32_t i) |
| 3287 | { |
| 3288 | /* there are items after the one deleted */ |
| 3289 | if (i+1 < unres->count) { |
| 3290 | /* we only move the data, memory is left allocated, why bother */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3291 | 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] | 3292 | |
| 3293 | /* deleting the last item */ |
| 3294 | } else if (i == 0) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3295 | free(unres->node); |
| 3296 | unres->node = NULL; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3297 | } |
| 3298 | |
| 3299 | /* if there are no items after and it is not the last one, just move the counter */ |
| 3300 | --unres->count; |
| 3301 | } |
| 3302 | |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3303 | /** |
| 3304 | * @brief Resolve (find) a data node from a specific module. Does not log. |
| 3305 | * |
| 3306 | * @param[in] mod Module to search in. |
| 3307 | * @param[in] name Name of the data node. |
| 3308 | * @param[in] nam_len Length of the name. |
| 3309 | * @param[in] start Data node to start the search from. |
| 3310 | * @param[in,out] parents Resolved nodes. If there are some parents, |
| 3311 | * they are replaced (!!) with the resolvents. |
| 3312 | * |
Michal Vasko | 2471e7f | 2016-04-11 11:00:15 +0200 | [diff] [blame] | 3313 | * @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] | 3314 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3315 | static int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3316 | 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] | 3317 | { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3318 | struct lyd_node *node; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3319 | int flag; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3320 | uint32_t i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3321 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3322 | if (!parents->count) { |
| 3323 | parents->count = 1; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3324 | parents->node = malloc(sizeof *parents->node); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3325 | if (!parents->node) { |
| 3326 | LOGMEM; |
Michal Vasko | 2471e7f | 2016-04-11 11:00:15 +0200 | [diff] [blame] | 3327 | return -1; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3328 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3329 | parents->node[0] = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3330 | } |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3331 | for (i = 0; i < parents->count;) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 3332 | 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] | 3333 | /* skip */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3334 | ++i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3335 | continue; |
| 3336 | } |
| 3337 | flag = 0; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3338 | LY_TREE_FOR(parents->node[i] ? parents->node[i]->child : start, node) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3339 | if (node->schema->module == mod && !strncmp(node->schema->name, name, nam_len) |
| 3340 | && node->schema->name[nam_len] == '\0') { |
| 3341 | /* matching target */ |
| 3342 | if (!flag) { |
Michal Vasko | 9a47e12 | 2015-09-03 14:26:32 +0200 | [diff] [blame] | 3343 | /* put node instead of the current parent */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3344 | parents->node[i] = node; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3345 | flag = 1; |
| 3346 | } else { |
Michal Vasko | 9a47e12 | 2015-09-03 14:26:32 +0200 | [diff] [blame] | 3347 | /* multiple matching, so create a new node */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3348 | ++parents->count; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3349 | parents->node = ly_realloc(parents->node, parents->count * sizeof *parents->node); |
| 3350 | if (!parents->node) { |
| 3351 | return EXIT_FAILURE; |
| 3352 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3353 | parents->node[parents->count-1] = node; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3354 | ++i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3355 | } |
| 3356 | } |
| 3357 | } |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3358 | |
| 3359 | if (!flag) { |
| 3360 | /* remove item from the parents list */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3361 | unres_data_del(parents, i); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3362 | } else { |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3363 | ++i; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3364 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3365 | } |
| 3366 | |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3367 | return parents->count ? EXIT_SUCCESS : EXIT_FAILURE; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3368 | } |
| 3369 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3370 | /** |
| 3371 | * @brief Resolve (find) a data node. Does not log. |
| 3372 | * |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3373 | * @param[in] mod_name Module name of the data node. |
| 3374 | * @param[in] mod_name_len Length of the module name. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3375 | * @param[in] name Name of the data node. |
| 3376 | * @param[in] nam_len Length of the name. |
| 3377 | * @param[in] start Data node to start the search from. |
| 3378 | * @param[in,out] parents Resolved nodes. If there are some parents, |
| 3379 | * they are replaced (!!) with the resolvents. |
| 3380 | * |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3381 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 otherwise. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3382 | */ |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3383 | static int |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3384 | 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] | 3385 | struct unres_data *parents) |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3386 | { |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3387 | const struct lys_module *mod; |
Michal Vasko | 31fc367 | 2015-10-21 12:08:13 +0200 | [diff] [blame] | 3388 | char *str; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3389 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3390 | assert(start); |
| 3391 | |
Michal Vasko | 31fc367 | 2015-10-21 12:08:13 +0200 | [diff] [blame] | 3392 | if (mod_name) { |
| 3393 | /* we have mod_name, find appropriate module */ |
| 3394 | str = strndup(mod_name, mod_name_len); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3395 | if (!str) { |
| 3396 | LOGMEM; |
| 3397 | return -1; |
| 3398 | } |
Michal Vasko | 31fc367 | 2015-10-21 12:08:13 +0200 | [diff] [blame] | 3399 | mod = ly_ctx_get_module(start->schema->module->ctx, str, NULL); |
| 3400 | free(str); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3401 | if (!mod) { |
| 3402 | /* invalid prefix */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3403 | return -1; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3404 | } |
| 3405 | } else { |
| 3406 | /* no prefix, module is the same as of current node */ |
| 3407 | mod = start->schema->module; |
| 3408 | } |
| 3409 | |
| 3410 | return resolve_data(mod, name, name_len, start, parents); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3411 | } |
| 3412 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3413 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3414 | * @brief Resolve a path predicate (leafref) in JSON data context. Logs directly |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3415 | * only specific errors, general no-resolvent error is left to the caller. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3416 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3417 | * @param[in] pred Predicate to use. |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3418 | * @param[in] node Node from which the predicate is being resolved |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3419 | * @param[in,out] node_match Nodes satisfying the restriction |
| 3420 | * without the predicate. Nodes not |
| 3421 | * satisfying the predicate are removed. |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3422 | * @param[out] parsed Number of characters parsed, negative on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3423 | * |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3424 | * @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] | 3425 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3426 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3427 | 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] | 3428 | int *parsed) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3429 | { |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3430 | /* ... /node[source = destination] ... */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3431 | struct unres_data source_match, dest_match; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3432 | const char *path_key_expr, *source, *sour_pref, *dest, *dest_pref; |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3433 | int pke_len, sour_len, sour_pref_len, dest_len, dest_pref_len, parsed_loc = 0, pke_parsed = 0; |
| 3434 | int has_predicate, dest_parent_times, i, rc; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3435 | uint32_t j; |
Radek Krejci | 0c2fa3a | 2016-06-13 15:18:03 +0200 | [diff] [blame] | 3436 | struct lyd_node_leaf_list *leaf_dst, *leaf_src; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3437 | |
| 3438 | source_match.count = 1; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3439 | source_match.node = malloc(sizeof *source_match.node); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3440 | if (!source_match.node) { |
| 3441 | LOGMEM; |
| 3442 | return -1; |
| 3443 | } |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3444 | dest_match.count = 1; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3445 | dest_match.node = malloc(sizeof *dest_match.node); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3446 | if (!dest_match.node) { |
| 3447 | LOGMEM; |
| 3448 | return -1; |
| 3449 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3450 | |
| 3451 | do { |
| 3452 | if ((i = parse_path_predicate(pred, &sour_pref, &sour_pref_len, &source, &sour_len, &path_key_expr, |
| 3453 | &pke_len, &has_predicate)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3454 | LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, pred[-i], &pred[-i]); |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3455 | rc = -1; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3456 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3457 | } |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3458 | parsed_loc += i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3459 | pred += i; |
| 3460 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3461 | for (j = 0; j < node_match->count;) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3462 | /* source */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3463 | source_match.node[0] = node_match->node[j]; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3464 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3465 | /* must be leaf (key of a list) */ |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3466 | 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] | 3467 | &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] | 3468 | i = 0; |
| 3469 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3470 | } |
| 3471 | |
| 3472 | /* destination */ |
Radek Krejci | 0c2fa3a | 2016-06-13 15:18:03 +0200 | [diff] [blame] | 3473 | dest_match.node[0] = node; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3474 | dest_parent_times = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3475 | if ((i = parse_path_key_expr(path_key_expr, &dest_pref, &dest_pref_len, &dest, &dest_len, |
| 3476 | &dest_parent_times)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3477 | 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] | 3478 | rc = -1; |
| 3479 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3480 | } |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3481 | pke_parsed = i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3482 | for (i = 0; i < dest_parent_times; ++i) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3483 | dest_match.node[0] = dest_match.node[0]->parent; |
| 3484 | if (!dest_match.node[0]) { |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3485 | i = 0; |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3486 | rc = EXIT_FAILURE; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3487 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3488 | } |
| 3489 | } |
| 3490 | while (1) { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3491 | 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] | 3492 | &dest_match)) || (dest_match.count != 1)) { |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3493 | i = 0; |
| 3494 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3495 | } |
| 3496 | |
| 3497 | if (pke_len == pke_parsed) { |
| 3498 | break; |
| 3499 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3500 | 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] | 3501 | &dest_parent_times)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3502 | 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] | 3503 | rc = -1; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3504 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3505 | } |
| 3506 | pke_parsed += i; |
| 3507 | } |
| 3508 | |
| 3509 | /* check match between source and destination nodes */ |
Radek Krejci | 0c2fa3a | 2016-06-13 15:18:03 +0200 | [diff] [blame] | 3510 | leaf_dst = (struct lyd_node_leaf_list *)dest_match.node[0]; |
| 3511 | while (leaf_dst->value_type == LY_TYPE_LEAFREF) { |
| 3512 | leaf_dst = (struct lyd_node_leaf_list *)leaf_dst->value.leafref; |
| 3513 | } |
| 3514 | leaf_src = (struct lyd_node_leaf_list *)source_match.node[0]; |
| 3515 | while (leaf_src->value_type == LY_TYPE_LEAFREF) { |
| 3516 | leaf_src = (struct lyd_node_leaf_list *)leaf_src->value.leafref; |
| 3517 | } |
| 3518 | if (leaf_src->value_type != leaf_dst->value_type) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3519 | goto remove_leafref; |
| 3520 | } |
| 3521 | |
Radek Krejci | 0c2fa3a | 2016-06-13 15:18:03 +0200 | [diff] [blame] | 3522 | if (!ly_strequal(leaf_src->value_str, leaf_dst->value_str, 1)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3523 | goto remove_leafref; |
| 3524 | } |
| 3525 | |
| 3526 | /* leafref is ok, continue check with next leafref */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3527 | ++j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3528 | continue; |
| 3529 | |
| 3530 | remove_leafref: |
| 3531 | /* does not fulfill conditions, remove leafref record */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3532 | unres_data_del(node_match, j); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3533 | } |
| 3534 | } while (has_predicate); |
| 3535 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3536 | free(source_match.node); |
| 3537 | free(dest_match.node); |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3538 | if (parsed) { |
| 3539 | *parsed = parsed_loc; |
| 3540 | } |
| 3541 | return EXIT_SUCCESS; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3542 | |
| 3543 | error: |
| 3544 | |
| 3545 | if (source_match.count) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3546 | free(source_match.node); |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3547 | } |
| 3548 | if (dest_match.count) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3549 | free(dest_match.node); |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3550 | } |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3551 | if (parsed) { |
| 3552 | *parsed = -parsed_loc+i; |
| 3553 | } |
| 3554 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3555 | } |
| 3556 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3557 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3558 | * @brief Resolve a path (leafref) in JSON data context. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3559 | * |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3560 | * @param[in] node Leafref data node. |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3561 | * @param[in] path Path of the leafref. |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3562 | * @param[out] ret Matching nodes. Expects an empty, but allocated structure. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3563 | * |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3564 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 otherwise. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3565 | */ |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3566 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3567 | 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] | 3568 | { |
Radek Krejci | 71b795b | 2015-08-10 16:20:39 +0200 | [diff] [blame] | 3569 | struct lyd_node *data = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3570 | const char *prefix, *name; |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3571 | int pref_len, nam_len, has_predicate, parent_times, i, parsed, rc; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3572 | uint32_t j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3573 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3574 | assert(node && path && ret && !ret->count); |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3575 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3576 | parent_times = 0; |
Michal Vasko | d917334 | 2015-08-17 14:35:36 +0200 | [diff] [blame] | 3577 | parsed = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3578 | |
| 3579 | /* searching for nodeset */ |
| 3580 | do { |
Radek Krejci | f7ed4c3 | 2016-10-27 16:20:03 +0200 | [diff] [blame] | 3581 | if ((i = parse_path_arg(node->schema->module, path, &prefix, &pref_len, &name, &nam_len, &parent_times, &has_predicate)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3582 | LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, path[-i], &path[-i]); |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3583 | rc = -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3584 | goto error; |
| 3585 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3586 | path += i; |
Michal Vasko | d917334 | 2015-08-17 14:35:36 +0200 | [diff] [blame] | 3587 | parsed += i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3588 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3589 | if (!ret->count) { |
Michal Vasko | bfd98e6 | 2016-09-02 09:50:05 +0200 | [diff] [blame] | 3590 | if (parent_times > 0) { |
| 3591 | data = node; |
| 3592 | for (i = 1; i < parent_times; ++i) { |
| 3593 | data = data->parent; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3594 | } |
Michal Vasko | bfd98e6 | 2016-09-02 09:50:05 +0200 | [diff] [blame] | 3595 | } else if (!parent_times) { |
| 3596 | data = node->child; |
| 3597 | } else { |
| 3598 | /* absolute path */ |
| 3599 | for (data = node; data->parent; data = data->parent); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3600 | } |
| 3601 | |
Michal Vasko | bfd98e6 | 2016-09-02 09:50:05 +0200 | [diff] [blame] | 3602 | /* we may still be parsing it and the pointer is not correct yet */ |
| 3603 | if (data->prev) { |
| 3604 | while (data->prev->next) { |
| 3605 | data = data->prev; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 3606 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3607 | } |
| 3608 | } |
| 3609 | |
| 3610 | /* node identifier */ |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3611 | 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] | 3612 | if (rc == -1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3613 | LOGVAL(LYE_INELEM_LEN, LY_VLOG_LYD, node, nam_len, name); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3614 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3615 | goto error; |
| 3616 | } |
| 3617 | |
| 3618 | if (has_predicate) { |
| 3619 | /* we have predicate, so the current results must be lists */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3620 | for (j = 0; j < ret->count;) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3621 | if (ret->node[j]->schema->nodetype == LYS_LIST && |
| 3622 | ((struct lys_node_list *)ret->node[0]->schema)->keys) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3623 | /* leafref is ok, continue check with next leafref */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3624 | ++j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3625 | continue; |
| 3626 | } |
| 3627 | |
| 3628 | /* does not fulfill conditions, remove leafref record */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3629 | unres_data_del(ret, j); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3630 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3631 | if ((rc = resolve_path_predicate_data(path, node, ret, &i))) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 3632 | if (rc == -1) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3633 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYD, node, "leafref", path); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3634 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3635 | goto error; |
| 3636 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3637 | path += i; |
Michal Vasko | d917334 | 2015-08-17 14:35:36 +0200 | [diff] [blame] | 3638 | parsed += i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3639 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3640 | if (!ret->count) { |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3641 | rc = EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3642 | goto error; |
| 3643 | } |
| 3644 | } |
| 3645 | } while (path[0] != '\0'); |
| 3646 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3647 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3648 | |
| 3649 | error: |
| 3650 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3651 | free(ret->node); |
| 3652 | ret->node = NULL; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3653 | ret->count = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3654 | |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3655 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3656 | } |
| 3657 | |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3658 | static int |
| 3659 | resolve_path_arg_schema_valid_dep_flag(const struct lys_node *op_node, const struct lys_node *first_node, int abs_path) |
| 3660 | { |
| 3661 | int dep1, dep2; |
| 3662 | const struct lys_node *node; |
| 3663 | |
| 3664 | if (lys_parent(op_node)) { |
| 3665 | /* inner operation (notif/action) */ |
| 3666 | if (abs_path) { |
| 3667 | return 1; |
| 3668 | } else { |
| 3669 | /* compare depth of both nodes */ |
| 3670 | for (dep1 = 0, node = op_node; lys_parent(node); node = lys_parent(node)); |
| 3671 | for (dep2 = 0, node = first_node; lys_parent(node); node = lys_parent(node)); |
| 3672 | if ((dep2 > dep1) || ((dep2 == dep1) && (op_node != first_node))) { |
| 3673 | return 1; |
| 3674 | } |
| 3675 | } |
| 3676 | } else { |
| 3677 | /* top-level operation (notif/rpc) */ |
| 3678 | if (op_node != first_node) { |
| 3679 | return 1; |
| 3680 | } |
| 3681 | } |
| 3682 | |
| 3683 | return 0; |
| 3684 | } |
| 3685 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3686 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3687 | * @brief Resolve a path (leafref) predicate in JSON schema context. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3688 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3689 | * @param[in] path Path to use. |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3690 | * @param[in] context_node Predicate context node (where the predicate is placed). |
| 3691 | * @param[in] parent Path context node (where the path begins/is placed). |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3692 | * @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] | 3693 | * |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3694 | * @return 0 on forward reference, otherwise the number |
| 3695 | * of characters successfully parsed, |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3696 | * positive on success, negative on failure. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3697 | */ |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3698 | static int |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3699 | 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] | 3700 | struct lys_node *parent, const struct lys_node *op_node) |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3701 | { |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3702 | const struct lys_node *src_node, *dst_node; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3703 | const char *path_key_expr, *source, *sour_pref, *dest, *dest_pref; |
Michal Vasko | f9b35d9 | 2016-10-21 15:19:30 +0200 | [diff] [blame] | 3704 | int pke_len, sour_len, sour_pref_len, dest_len, dest_pref_len, pke_parsed, parsed = 0; |
| 3705 | int has_predicate, dest_parent_times, i, rc, first_iter; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3706 | |
| 3707 | do { |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3708 | 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] | 3709 | &pke_len, &has_predicate)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3710 | 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] | 3711 | return -parsed+i; |
| 3712 | } |
| 3713 | parsed += i; |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3714 | path += i; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3715 | |
Michal Vasko | 5809090 | 2015-08-13 14:04:15 +0200 | [diff] [blame] | 3716 | /* source (must be leaf) */ |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3717 | if (!sour_pref) { |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3718 | sour_pref = context_node->module->name; |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3719 | } |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3720 | 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] | 3721 | LYS_LEAF | LYS_LEAFLIST | LYS_AUGMENT, &src_node); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3722 | if (rc) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3723 | 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] | 3724 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3725 | } |
| 3726 | |
| 3727 | /* destination */ |
Michal Vasko | f9b35d9 | 2016-10-21 15:19:30 +0200 | [diff] [blame] | 3728 | dest_parent_times = 0; |
| 3729 | pke_parsed = 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3730 | if ((i = parse_path_key_expr(path_key_expr, &dest_pref, &dest_pref_len, &dest, &dest_len, |
| 3731 | &dest_parent_times)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3732 | 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] | 3733 | return -parsed; |
| 3734 | } |
| 3735 | pke_parsed += i; |
| 3736 | |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3737 | for (i = 0, dst_node = parent; i < dest_parent_times; ++i) { |
Michal Vasko | fbaead7 | 2016-10-07 10:54:48 +0200 | [diff] [blame] | 3738 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 3739 | * all schema nodes that cannot be instantiated in data tree */ |
| 3740 | for (dst_node = lys_parent(dst_node); |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3741 | 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] | 3742 | dst_node = lys_parent(dst_node)); |
| 3743 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3744 | if (!dst_node) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3745 | 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] | 3746 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3747 | } |
| 3748 | } |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3749 | first_iter = 1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3750 | while (1) { |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3751 | if (!dest_pref) { |
| 3752 | dest_pref = dst_node->module->name; |
| 3753 | } |
| 3754 | 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] | 3755 | LYS_CONTAINER | LYS_LIST | LYS_LEAF | LYS_AUGMENT, &dst_node); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3756 | if (rc) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3757 | 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] | 3758 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3759 | } |
| 3760 | |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3761 | if (first_iter) { |
| 3762 | if (resolve_path_arg_schema_valid_dep_flag(op_node, dst_node, 0)) { |
| 3763 | parent->flags |= LYS_VALID_DEP; |
| 3764 | } |
| 3765 | first_iter = 0; |
| 3766 | } |
| 3767 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3768 | if (pke_len == pke_parsed) { |
| 3769 | break; |
| 3770 | } |
| 3771 | |
| 3772 | if ((i = parse_path_key_expr(path_key_expr+pke_parsed, &dest_pref, &dest_pref_len, &dest, &dest_len, |
| 3773 | &dest_parent_times)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3774 | LOGVAL(LYE_INCHAR, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3775 | (path_key_expr+pke_parsed)[-i], (path_key_expr+pke_parsed)-i); |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3776 | return -parsed; |
| 3777 | } |
| 3778 | pke_parsed += i; |
| 3779 | } |
| 3780 | |
| 3781 | /* check source - dest match */ |
Michal Vasko | 59ad458 | 2016-09-16 13:15:41 +0200 | [diff] [blame] | 3782 | if (dst_node->nodetype != src_node->nodetype) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3783 | 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] | 3784 | LOGVAL(LYE_SPEC, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, "Destination node is not a %s, but a %s.", |
| 3785 | strnodetype(src_node->nodetype), strnodetype(dst_node->nodetype)); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3786 | return -parsed; |
| 3787 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3788 | } while (has_predicate); |
| 3789 | |
| 3790 | return parsed; |
| 3791 | } |
| 3792 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3793 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3794 | * @brief Resolve a path (leafref) in JSON schema context. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3795 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3796 | * @param[in] path Path to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3797 | * @param[in] parent_node Parent of the leafref. |
Radek Krejci | 2f12f85 | 2016-01-08 12:59:57 +0100 | [diff] [blame] | 3798 | * @param[in] parent_tpdf Flag if the parent node is actually typedef, in that case the path |
| 3799 | * has to contain absolute path |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3800 | * @param[out] ret Pointer to the resolved schema node. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3801 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3802 | * @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] | 3803 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3804 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3805 | 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] | 3806 | const struct lys_node **ret) |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3807 | { |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3808 | const struct lys_node *node, *op_node = NULL; |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 3809 | const struct lys_module *mod, *mod2; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3810 | const char *id, *prefix, *name; |
| 3811 | int pref_len, nam_len, parent_times, has_predicate; |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3812 | int i, first_iter, rc; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3813 | |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3814 | first_iter = 1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3815 | parent_times = 0; |
| 3816 | id = path; |
| 3817 | |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3818 | /* find operation schema we are in, if applicable */ |
Michal Vasko | e9914d1 | 2016-10-07 14:32:37 +0200 | [diff] [blame] | 3819 | if (!parent_tpdf) { |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3820 | for (op_node = lys_parent(parent); |
| 3821 | op_node && !(op_node->nodetype & (LYS_ACTION | LYS_NOTIF | LYS_RPC)); |
| 3822 | op_node = lys_parent(op_node)); |
Michal Vasko | e9914d1 | 2016-10-07 14:32:37 +0200 | [diff] [blame] | 3823 | } |
| 3824 | |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 3825 | mod2 = lys_node_module(parent); |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3826 | do { |
Radek Krejci | f7ed4c3 | 2016-10-27 16:20:03 +0200 | [diff] [blame] | 3827 | if ((i = parse_path_arg(parent->module, id, &prefix, &pref_len, &name, &nam_len, &parent_times, &has_predicate)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3828 | 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] | 3829 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3830 | } |
| 3831 | id += i; |
| 3832 | |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3833 | if (first_iter) { |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3834 | if (parent_times == -1) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 3835 | /* resolve prefix of the module */ |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3836 | mod = lys_get_import_module(parent->module, NULL, 0, prefix, pref_len); |
Radek Krejci | 0fa54e9 | 2016-09-14 14:01:05 +0200 | [diff] [blame] | 3837 | mod = lys_get_implemented_module(mod); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 3838 | /* get start node */ |
| 3839 | node = mod ? mod->data : NULL; |
Michal Vasko | 5809090 | 2015-08-13 14:04:15 +0200 | [diff] [blame] | 3840 | if (!node) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3841 | LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, |
| 3842 | "leafref", path); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3843 | return EXIT_FAILURE; |
Michal Vasko | 5809090 | 2015-08-13 14:04:15 +0200 | [diff] [blame] | 3844 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3845 | } else if (parent_times > 0) { |
Radek Krejci | 2f12f85 | 2016-01-08 12:59:57 +0100 | [diff] [blame] | 3846 | if (parent_tpdf) { |
| 3847 | /* 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] | 3848 | LOGVAL(LYE_NORESOLV, 0, NULL, "leafref", path); |
Radek Krejci | 2f12f85 | 2016-01-08 12:59:57 +0100 | [diff] [blame] | 3849 | return -1; |
| 3850 | } |
| 3851 | |
Michal Vasko | 9445808 | 2016-10-07 14:34:36 +0200 | [diff] [blame] | 3852 | /* we are looking for a sibling of a node, node it's parent (that is why parent_times - 1) */ |
| 3853 | for (i = 0, node = parent; i < parent_times - 1; i++) { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 3854 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 3855 | * all schema nodes that cannot be instantiated in data tree */ |
| 3856 | for (node = lys_parent(node); |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3857 | 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] | 3858 | node = lys_parent(node)); |
| 3859 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3860 | if (!node) { |
Michal Vasko | e9914d1 | 2016-10-07 14:32:37 +0200 | [diff] [blame] | 3861 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3862 | return EXIT_FAILURE; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3863 | } |
| 3864 | } |
Michal Vasko | e01eca5 | 2015-08-13 14:42:02 +0200 | [diff] [blame] | 3865 | } else { |
| 3866 | LOGINT; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3867 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3868 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3869 | } else { |
Michal Vasko | 7dc71d0 | 2016-03-15 10:42:28 +0100 | [diff] [blame] | 3870 | /* move down the tree, if possible */ |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 3871 | if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
Michal Vasko | 2f5aceb | 2016-03-22 10:24:14 +0100 | [diff] [blame] | 3872 | 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] | 3873 | return -1; |
| 3874 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3875 | node = node->child; |
Radek Krejci | 43ccc4c | 2016-10-18 20:40:06 +0200 | [diff] [blame] | 3876 | if (!node) { |
| 3877 | LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, |
| 3878 | "leafref", path); |
| 3879 | return EXIT_FAILURE; |
| 3880 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3881 | } |
| 3882 | |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 3883 | if (!prefix) { |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3884 | prefix = lys_node_module(parent)->name; |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 3885 | } |
| 3886 | |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3887 | 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] | 3888 | if (rc) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3889 | 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] | 3890 | return EXIT_FAILURE; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3891 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3892 | |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3893 | if (first_iter) { |
| 3894 | /* set external dependency flag, we can decide based on the first found node */ |
| 3895 | if (!parent_tpdf && op_node && parent_times && |
| 3896 | resolve_path_arg_schema_valid_dep_flag(op_node, node, (parent_times == -1 ? 1 : 0))) { |
| 3897 | parent->flags |= LYS_VALID_DEP; |
| 3898 | } |
| 3899 | first_iter = 0; |
| 3900 | } |
| 3901 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3902 | if (has_predicate) { |
| 3903 | /* we have predicate, so the current result must be list */ |
| 3904 | if (node->nodetype != LYS_LIST) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3905 | 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] | 3906 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3907 | } |
| 3908 | |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3909 | i = resolve_path_predicate_schema(id, node, parent, op_node); |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 3910 | if (i <= 0) { |
| 3911 | if (i == 0) { |
| 3912 | return EXIT_FAILURE; |
| 3913 | } else { /* i < 0 */ |
| 3914 | return -1; |
| 3915 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3916 | } |
| 3917 | id += i; |
Michal Vasko | f9b35d9 | 2016-10-21 15:19:30 +0200 | [diff] [blame] | 3918 | has_predicate = 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3919 | } |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 3920 | mod = lys_node_module(node); |
| 3921 | if (!mod->implemented && mod != mod2) { |
| 3922 | /* set the module implemented */ |
| 3923 | if (lys_set_implemented(mod)) { |
| 3924 | return -1; |
| 3925 | } |
| 3926 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3927 | } while (id[0]); |
| 3928 | |
Michal Vasko | ca91768 | 2016-07-25 11:00:37 +0200 | [diff] [blame] | 3929 | /* the target must be leaf or leaf-list (in YANG 1.1 only) */ |
Radek Krejci | 2a5a960 | 2016-11-04 10:21:13 +0100 | [diff] [blame] | 3930 | if ((node->nodetype != LYS_LEAF) && (node->nodetype != LYS_LEAFLIST)) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3931 | 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] | 3932 | LOGVAL(LYE_SPEC, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, |
Radek Krejci | 2a5a960 | 2016-11-04 10:21:13 +0100 | [diff] [blame] | 3933 | "Leafref target \"%s\" is not a leaf nor a leaf-list.", path); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3934 | return -1; |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 3935 | } |
| 3936 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3937 | /* check status */ |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3938 | if (lyp_check_status(parent->flags, parent->module, parent->name, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3939 | node->flags, node->module, node->name, node)) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3940 | return -1; |
| 3941 | } |
| 3942 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3943 | if (ret) { |
| 3944 | *ret = node; |
| 3945 | } |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 3946 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3947 | return EXIT_SUCCESS; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3948 | } |
| 3949 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3950 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3951 | * @brief Resolve instance-identifier predicate in JSON data format. |
| 3952 | * Does not log. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3953 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3954 | * @param[in] pred Predicate to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3955 | * @param[in,out] node_match Nodes matching the restriction without |
| 3956 | * the predicate. Nodes not satisfying |
| 3957 | * the predicate are removed. |
| 3958 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3959 | * @return Number of characters successfully parsed, |
| 3960 | * positive on success, negative on failure. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3961 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3962 | static int |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3963 | resolve_predicate(const char *pred, struct unres_data *node_match) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3964 | { |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3965 | /* ... /node[target = value] ... */ |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3966 | struct lyd_node *target; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3967 | const char *model, *name, *value; |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3968 | 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] | 3969 | uint32_t j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3970 | |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3971 | assert(pred && node_match->count); |
| 3972 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3973 | idx = -1; |
| 3974 | parsed = 0; |
| 3975 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3976 | pred_iter = -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3977 | do { |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3978 | 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] | 3979 | return -parsed+i; |
| 3980 | } |
| 3981 | parsed += i; |
| 3982 | pred += i; |
| 3983 | |
| 3984 | if (isdigit(name[0])) { |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3985 | /* pos */ |
| 3986 | assert(!value); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3987 | idx = atoi(name); |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3988 | } else if (name[0] != '.') { |
| 3989 | /* list keys */ |
| 3990 | if (pred_iter < 0) { |
| 3991 | pred_iter = 1; |
| 3992 | } else { |
| 3993 | ++pred_iter; |
| 3994 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3995 | } |
| 3996 | |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 3997 | for (cur_idx = 1, j = 0; j < node_match->count; ++cur_idx) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3998 | /* target */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3999 | if (name[0] == '.') { |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4000 | /* leaf-list value */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4001 | if (node_match->node[j]->schema->nodetype != LYS_LEAFLIST) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4002 | goto remove_instid; |
| 4003 | } |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4004 | |
| 4005 | target = node_match->node[j]; |
| 4006 | /* check the value */ |
| 4007 | if (strncmp(((struct lyd_node_leaf_list *)target)->value_str, value, val_len) |
| 4008 | || ((struct lyd_node_leaf_list *)target)->value_str[val_len]) { |
| 4009 | goto remove_instid; |
| 4010 | } |
| 4011 | |
| 4012 | } else if (!value) { |
| 4013 | /* keyless list position */ |
| 4014 | if ((node_match->node[j]->schema->nodetype != LYS_LIST) |
| 4015 | || ((struct lys_node_list *)node_match->node[j]->schema)->keys) { |
| 4016 | goto remove_instid; |
| 4017 | } |
| 4018 | |
| 4019 | if (idx != cur_idx) { |
| 4020 | goto remove_instid; |
| 4021 | } |
| 4022 | |
| 4023 | } else { |
| 4024 | /* list key value */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4025 | if (node_match->node[j]->schema->nodetype != LYS_LIST) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4026 | goto remove_instid; |
| 4027 | } |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4028 | |
| 4029 | /* key module must match the list module */ |
| 4030 | if (strncmp(node_match->node[j]->schema->module->name, model, mod_len) |
| 4031 | || node_match->node[j]->schema->module->name[mod_len]) { |
| 4032 | goto remove_instid; |
| 4033 | } |
| 4034 | /* find the key leaf */ |
Michal Vasko | 045182c | 2016-09-09 12:44:07 +0200 | [diff] [blame] | 4035 | 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] | 4036 | if (!target) { |
| 4037 | goto remove_instid; |
| 4038 | } |
| 4039 | if ((struct lys_node_leaf *)target->schema != |
| 4040 | ((struct lys_node_list *)node_match->node[j]->schema)->keys[pred_iter - 1]) { |
| 4041 | goto remove_instid; |
| 4042 | } |
| 4043 | |
| 4044 | /* check the value */ |
| 4045 | if (strncmp(((struct lyd_node_leaf_list *)target)->value_str, value, val_len) |
| 4046 | || ((struct lyd_node_leaf_list *)target)->value_str[val_len]) { |
| 4047 | goto remove_instid; |
| 4048 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4049 | } |
| 4050 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4051 | /* instid is ok, continue check with the next one */ |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4052 | ++j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4053 | continue; |
| 4054 | |
| 4055 | remove_instid: |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4056 | /* does not fulfill conditions, remove instid record */ |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4057 | unres_data_del(node_match, j); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4058 | } |
| 4059 | } while (has_predicate); |
| 4060 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4061 | /* check that all list keys were specified */ |
| 4062 | if ((pred_iter > 0) && node_match->count) { |
Michal Vasko | 045182c | 2016-09-09 12:44:07 +0200 | [diff] [blame] | 4063 | j = 0; |
| 4064 | while (j < node_match->count) { |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4065 | assert(node_match->node[j]->schema->nodetype == LYS_LIST); |
| 4066 | if (pred_iter < ((struct lys_node_list *)node_match->node[j]->schema)->keys_size) { |
| 4067 | /* not enough predicates, just remove the list instance */ |
| 4068 | unres_data_del(node_match, j); |
Michal Vasko | 045182c | 2016-09-09 12:44:07 +0200 | [diff] [blame] | 4069 | } else { |
| 4070 | ++j; |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4071 | } |
| 4072 | } |
| 4073 | |
| 4074 | if (!node_match->count) { |
| 4075 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier is missing some list keys."); |
| 4076 | } |
| 4077 | } |
| 4078 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4079 | return parsed; |
| 4080 | } |
| 4081 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4082 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 4083 | * @brief Resolve instance-identifier in JSON data format. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4084 | * |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 4085 | * @param[in] data Data node where the path is used |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4086 | * @param[in] path Instance-identifier node value. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4087 | * |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4088 | * @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] | 4089 | */ |
Radek Krejci | 1899d6a | 2016-11-03 13:48:07 +0100 | [diff] [blame] | 4090 | struct lyd_node * |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4091 | resolve_instid(struct lyd_node *data, const char *path) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4092 | { |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4093 | int i = 0, j; |
| 4094 | struct lyd_node *result = NULL; |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4095 | const struct lys_module *mod; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4096 | struct ly_ctx *ctx = data->schema->module->ctx; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4097 | const char *model, *name; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4098 | char *str; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4099 | int mod_len, name_len, has_predicate; |
| 4100 | struct unres_data node_match; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4101 | |
| 4102 | memset(&node_match, 0, sizeof node_match); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4103 | |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4104 | /* we need root to resolve absolute path */ |
| 4105 | for (; data->parent; data = data->parent); |
Michal Vasko | db9323c | 2015-10-16 10:32:44 +0200 | [diff] [blame] | 4106 | /* we're still parsing it and the pointer is not correct yet */ |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 4107 | if (data->prev) { |
| 4108 | for (; data->prev->next; data = data->prev); |
| 4109 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4110 | |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4111 | /* search for the instance node */ |
| 4112 | while (path[i]) { |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 4113 | 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] | 4114 | if (j <= 0) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4115 | 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] | 4116 | goto error; |
| 4117 | } |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4118 | i += j; |
Michal Vasko | b387c48 | 2015-08-12 09:32:59 +0200 | [diff] [blame] | 4119 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4120 | str = strndup(model, mod_len); |
| 4121 | if (!str) { |
| 4122 | LOGMEM; |
| 4123 | goto error; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4124 | } |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4125 | mod = ly_ctx_get_module(ctx, str, NULL); |
| 4126 | free(str); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4127 | |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4128 | if (resolve_data(mod, name, name_len, data, &node_match)) { |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4129 | /* no instance exists */ |
| 4130 | return NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4131 | } |
| 4132 | |
| 4133 | if (has_predicate) { |
| 4134 | /* 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] | 4135 | j = resolve_predicate(&path[i], &node_match); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4136 | if (j < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4137 | LOGVAL(LYE_INPRED, LY_VLOG_LYD, data, &path[i-j]); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4138 | goto error; |
| 4139 | } |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4140 | i += j; |
Michal Vasko | b387c48 | 2015-08-12 09:32:59 +0200 | [diff] [blame] | 4141 | |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4142 | if (!node_match.count) { |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4143 | /* no instance exists */ |
| 4144 | return NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4145 | } |
| 4146 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4147 | } |
| 4148 | |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4149 | if (!node_match.count) { |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4150 | /* no instance exists */ |
| 4151 | return NULL; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 4152 | } else if (node_match.count > 1) { |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4153 | /* instance identifier must resolve to a single node */ |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4154 | LOGVAL(LYE_TOOMANY, LY_VLOG_LYD, data, path, "data tree"); |
Michal Vasko | d6adbaa | 2016-04-11 11:01:09 +0200 | [diff] [blame] | 4155 | goto error; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4156 | } else { |
| 4157 | /* we have required result, remember it and cleanup */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4158 | result = node_match.node[0]; |
| 4159 | free(node_match.node); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4160 | return result; |
| 4161 | } |
| 4162 | |
| 4163 | error: |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4164 | /* cleanup */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4165 | free(node_match.node); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4166 | return NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4167 | } |
| 4168 | |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4169 | int |
| 4170 | lys_check_xpath(struct lys_node *node, int check_place) |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4171 | { |
| 4172 | struct lys_node *parent, *elem; |
| 4173 | struct lyxp_set set; |
| 4174 | uint32_t i; |
| 4175 | int rc; |
| 4176 | |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4177 | if (check_place) { |
| 4178 | parent = node; |
| 4179 | while (parent) { |
| 4180 | if (parent->nodetype == LYS_GROUPING) { |
| 4181 | /* unresolved grouping, skip for now (will be checked later) */ |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4182 | return EXIT_SUCCESS; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4183 | } |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4184 | if (parent->nodetype == LYS_AUGMENT) { |
| 4185 | if (!((struct lys_node_augment *)parent)->target) { |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame^] | 4186 | /* unresolved augment */ |
| 4187 | if (parent->module->implemented) { |
| 4188 | /* skip for now (will be checked later) */ |
| 4189 | return EXIT_FAILURE; |
| 4190 | } else { |
| 4191 | /* not implemented augment, skip resolving */ |
| 4192 | return EXIT_SUCCESS; |
| 4193 | } |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4194 | } else { |
| 4195 | parent = ((struct lys_node_augment *)parent)->target; |
| 4196 | continue; |
| 4197 | } |
| 4198 | } |
| 4199 | parent = parent->parent; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4200 | } |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4201 | } |
| 4202 | |
| 4203 | rc = lyxp_node_atomize(node, &set); |
| 4204 | if (rc) { |
| 4205 | return rc; |
| 4206 | } |
| 4207 | |
| 4208 | for (parent = node; parent && !(parent->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); parent = lys_parent(parent)); |
| 4209 | |
| 4210 | for (i = 0; i < set.used; ++i) { |
| 4211 | /* skip roots'n'stuff */ |
| 4212 | if (set.val.snodes[i].type == LYXP_NODE_ELEM) { |
| 4213 | /* XPath expression cannot reference "lower" status than the node that has the definition */ |
| 4214 | if (lyp_check_status(node->flags, lys_node_module(node), node->name, set.val.snodes[i].snode->flags, |
| 4215 | lys_node_module(set.val.snodes[i].snode), set.val.snodes[i].snode->name, node)) { |
| 4216 | return -1; |
| 4217 | } |
| 4218 | |
| 4219 | if (parent) { |
| 4220 | for (elem = set.val.snodes[i].snode; elem && (elem != parent); elem = lys_parent(elem)); |
| 4221 | if (!elem) { |
| 4222 | /* not in node's RPC or notification subtree, set the flag */ |
| 4223 | node->flags |= LYS_VALID_DEP; |
| 4224 | break; |
| 4225 | } |
| 4226 | } |
| 4227 | } |
| 4228 | } |
| 4229 | |
| 4230 | free(set.val.snodes); |
| 4231 | return EXIT_SUCCESS; |
| 4232 | } |
| 4233 | |
Radek Krejci | f71f48f | 2016-10-25 16:37:24 +0200 | [diff] [blame] | 4234 | static int |
| 4235 | check_leafref_config(struct lys_node_leaf *leaf, struct lys_type *type) |
| 4236 | { |
| 4237 | int i; |
| 4238 | |
| 4239 | if (type->base == LY_TYPE_LEAFREF) { |
| 4240 | if ((leaf->flags & LYS_CONFIG_W) && type->info.lref.target && (type->info.lref.target->flags & LYS_CONFIG_R)) { |
| 4241 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, leaf, "The %s is config but refers to a non-config %s.", |
| 4242 | strnodetype(leaf->nodetype), strnodetype(type->info.lref.target->nodetype)); |
| 4243 | return -1; |
| 4244 | } |
| 4245 | /* we can skip the test in case the leafref is not yet resolved. In that case the test is done in the time |
| 4246 | * of leafref resolving (lys_leaf_add_leafref_target()) */ |
| 4247 | } else if (type->base == LY_TYPE_UNION) { |
| 4248 | for (i = 0; i < type->info.uni.count; i++) { |
| 4249 | if (check_leafref_config(leaf, &type->info.uni.types[i])) { |
| 4250 | return -1; |
| 4251 | } |
| 4252 | } |
| 4253 | } |
| 4254 | return 0; |
| 4255 | } |
| 4256 | |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4257 | /** |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4258 | * @brief Passes config flag down to children, skips nodes without config flags. |
| 4259 | * Does not log. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4260 | * |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4261 | * @param[in] node Siblings and their children to have flags changed. |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4262 | * @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] | 4263 | * @param[in] flags Flags to assign to all the nodes. |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4264 | * |
| 4265 | * @return 0 on success, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4266 | */ |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4267 | static int |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4268 | inherit_config_flag(struct lys_node *node, int flags, int clear) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4269 | { |
Radek Krejci | f71f48f | 2016-10-25 16:37:24 +0200 | [diff] [blame] | 4270 | struct lys_node_leaf *leaf; |
| 4271 | |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4272 | assert(!(flags ^ (flags & LYS_CONFIG_MASK))); |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4273 | LY_TREE_FOR(node, node) { |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4274 | if (lys_check_xpath(node, 0)) { |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4275 | return -1; |
| 4276 | } |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4277 | if (clear) { |
| 4278 | node->flags &= ~LYS_CONFIG_MASK; |
Michal Vasko | c2a8d36 | 2016-09-29 08:50:13 +0200 | [diff] [blame] | 4279 | node->flags &= ~LYS_CONFIG_SET; |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4280 | } else { |
| 4281 | if (node->flags & LYS_CONFIG_SET) { |
| 4282 | /* skip nodes with an explicit config value */ |
| 4283 | if ((flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) { |
| 4284 | LOGVAL(LYE_INARG, LY_VLOG_LYS, node, "true", "config"); |
| 4285 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, node, "State nodes cannot have configuration nodes as children."); |
| 4286 | return -1; |
| 4287 | } |
| 4288 | continue; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4289 | } |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4290 | |
| 4291 | if (!(node->nodetype & (LYS_USES | LYS_GROUPING))) { |
| 4292 | node->flags = (node->flags & ~LYS_CONFIG_MASK) | flags; |
| 4293 | /* check that configuration lists have keys */ |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4294 | if ((node->nodetype == LYS_LIST) && (node->flags & LYS_CONFIG_W) |
| 4295 | && !((struct lys_node_list *)node)->keys_size) { |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4296 | LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_LYS, node, "key", "list"); |
| 4297 | return -1; |
| 4298 | } |
| 4299 | } |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4300 | } |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4301 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) { |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4302 | if (inherit_config_flag(node->child, flags, clear)) { |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4303 | return -1; |
| 4304 | } |
Radek Krejci | f71f48f | 2016-10-25 16:37:24 +0200 | [diff] [blame] | 4305 | } else if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 4306 | leaf = (struct lys_node_leaf *)node; |
| 4307 | if (check_leafref_config(leaf, &leaf->type)) { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 4308 | return -1; |
| 4309 | } |
| 4310 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4311 | } |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4312 | |
| 4313 | return 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4314 | } |
| 4315 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4316 | /** |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 4317 | * @brief Resolve augment target. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4318 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4319 | * @param[in] aug Augment to use. |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 4320 | * @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] | 4321 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4322 | * @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] | 4323 | */ |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 4324 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4325 | resolve_augment(struct lys_node_augment *aug, struct lys_node *siblings) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4326 | { |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4327 | int rc, clear_config; |
Michal Vasko | 1d87a92 | 2015-08-21 12:57:16 +0200 | [diff] [blame] | 4328 | struct lys_node *sub; |
Pavol Vican | 4731993 | 2016-08-29 09:14:47 +0200 | [diff] [blame] | 4329 | const struct lys_node *aug_target, *parent; |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4330 | struct lys_module *mod; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4331 | |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4332 | assert(aug && !aug->target); |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame^] | 4333 | mod = lys_main_module(aug->module); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4334 | |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4335 | /* resolve target node */ |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame^] | 4336 | rc = resolve_augment_schema_nodeid(aug->target_name, siblings, (siblings ? NULL : aug->module), mod->implemented, &aug_target); |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4337 | if (rc == -1) { |
| 4338 | return -1; |
| 4339 | } |
| 4340 | if (rc > 0) { |
| 4341 | LOGVAL(LYE_INCHAR, LY_VLOG_LYS, aug, aug->target_name[rc - 1], &aug->target_name[rc - 1]); |
| 4342 | return -1; |
| 4343 | } |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame^] | 4344 | if (!aug_target && mod->implemented) { |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4345 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, aug, "augment", aug->target_name); |
| 4346 | return EXIT_FAILURE; |
| 4347 | } |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4348 | /* check that we want to connect augment into its target */ |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4349 | if (!mod->implemented) { |
| 4350 | /* it must be augment only to the same module, |
| 4351 | * otherwise we do not apply augment in not-implemented |
| 4352 | * module. If the module is set to be implemented in future, |
| 4353 | * the augment is being resolved and checked again */ |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame^] | 4354 | if (!aug_target) { |
| 4355 | /* target was not even resolved */ |
| 4356 | return EXIT_SUCCESS; |
| 4357 | } |
| 4358 | /* target was resolved, but it may refer another module */ |
| 4359 | for (sub = (struct lys_node *)aug_target; sub; sub = lys_parent(sub)) { |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4360 | if (lys_node_module(sub) != mod) { |
| 4361 | /* this is not an implemented module and the augment |
| 4362 | * target some other module, so avoid its connecting |
| 4363 | * to the target */ |
| 4364 | return EXIT_SUCCESS; |
| 4365 | } |
| 4366 | } |
| 4367 | } |
| 4368 | |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4369 | if (!aug->child) { |
| 4370 | /* nothing to do */ |
| 4371 | LOGWRN("Augment \"%s\" without children.", aug->target_name); |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4372 | goto success; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4373 | } |
| 4374 | |
Michal Vasko | d58d596 | 2016-03-02 14:29:41 +0100 | [diff] [blame] | 4375 | /* check for mandatory nodes - if the target node is in another module |
| 4376 | * the added nodes cannot be mandatory |
| 4377 | */ |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4378 | if (!aug->parent && (lys_node_module((struct lys_node *)aug) != lys_node_module(aug_target)) |
Radek Krejci | df24cbe | 2016-11-08 11:55:51 +0100 | [diff] [blame] | 4379 | && (rc = lyp_check_mandatory_augment(aug, aug_target))) { |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 4380 | return rc; |
Michal Vasko | d58d596 | 2016-03-02 14:29:41 +0100 | [diff] [blame] | 4381 | } |
| 4382 | |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4383 | /* check augment target type and then augment nodes type */ |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4384 | 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] | 4385 | LY_TREE_FOR(aug->child, sub) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4386 | 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] | 4387 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment"); |
| 4388 | 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] | 4389 | strnodetype(aug_target->nodetype), strnodetype(sub->nodetype)); |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4390 | return -1; |
| 4391 | } |
| 4392 | } |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4393 | } else if (aug_target->nodetype == LYS_CHOICE) { |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4394 | LY_TREE_FOR(aug->child, sub) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4395 | 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] | 4396 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment"); |
| 4397 | 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] | 4398 | strnodetype(aug_target->nodetype), strnodetype(sub->nodetype)); |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4399 | return -1; |
| 4400 | } |
| 4401 | } |
| 4402 | } else { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4403 | LOGVAL(LYE_INARG, LY_VLOG_LYS, aug, aug->target_name, "target-node"); |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4404 | 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] | 4405 | return -1; |
| 4406 | } |
| 4407 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 4408 | /* check identifier uniqueness as in lys_node_addchild() */ |
Michal Vasko | 1d87a92 | 2015-08-21 12:57:16 +0200 | [diff] [blame] | 4409 | LY_TREE_FOR(aug->child, sub) { |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4410 | if (lys_check_id(sub, (struct lys_node *)aug_target, NULL)) { |
Michal Vasko | 3e6665f | 2015-08-17 14:00:38 +0200 | [diff] [blame] | 4411 | return -1; |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 4412 | } |
| 4413 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4414 | |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4415 | /* finally reconnect augmenting data into the target - add them to the target child list, |
| 4416 | * by setting aug->target we know the augment is fully resolved now */ |
| 4417 | aug->target = (struct lys_node *)aug_target; |
| 4418 | if (aug->target->child) { |
| 4419 | sub = aug->target->child->prev; /* remember current target's last node */ |
| 4420 | sub->next = aug->child; /* connect augmenting data after target's last node */ |
| 4421 | aug->target->child->prev = aug->child->prev; /* new target's last node is last augmenting node */ |
| 4422 | aug->child->prev = sub; /* finish connecting of both child lists */ |
| 4423 | } else { |
| 4424 | aug->target->child = aug->child; |
| 4425 | } |
| 4426 | |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4427 | /* inherit config information from actual parent */ |
| 4428 | for(parent = aug_target; parent && !(parent->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT | LYS_RPC)); parent = lys_parent(parent)); |
| 4429 | clear_config = (parent) ? 1 : 0; |
| 4430 | LY_TREE_FOR(aug->child, sub) { |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4431 | if (inherit_config_flag(sub, aug_target->flags & LYS_CONFIG_MASK, clear_config)) { |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4432 | return -1; |
| 4433 | } |
| 4434 | } |
| 4435 | |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4436 | success: |
| 4437 | if (mod->implemented) { |
| 4438 | /* make target modules also implemented */ |
| 4439 | for (sub = aug->target; sub; sub = lys_parent(sub)) { |
| 4440 | if (lys_set_implemented(sub->module)) { |
| 4441 | return -1; |
| 4442 | } |
| 4443 | } |
| 4444 | } |
| 4445 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4446 | return EXIT_SUCCESS; |
| 4447 | } |
| 4448 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4449 | /** |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4450 | * @brief Resolve (find) choice default case. Does not log. |
| 4451 | * |
| 4452 | * @param[in] choic Choice to use. |
| 4453 | * @param[in] dflt Name of the default case. |
| 4454 | * |
| 4455 | * @return Pointer to the default node or NULL. |
| 4456 | */ |
| 4457 | static struct lys_node * |
| 4458 | resolve_choice_dflt(struct lys_node_choice *choic, const char *dflt) |
| 4459 | { |
| 4460 | struct lys_node *child, *ret; |
| 4461 | |
| 4462 | LY_TREE_FOR(choic->child, child) { |
| 4463 | if (child->nodetype == LYS_USES) { |
| 4464 | ret = resolve_choice_dflt((struct lys_node_choice *)child, dflt); |
| 4465 | if (ret) { |
| 4466 | return ret; |
| 4467 | } |
| 4468 | } |
| 4469 | |
| 4470 | 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] | 4471 | | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CHOICE))) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4472 | return child; |
| 4473 | } |
| 4474 | } |
| 4475 | |
| 4476 | return NULL; |
| 4477 | } |
| 4478 | |
| 4479 | /** |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4480 | * @brief Resolve uses, apply augments, refines. Logs directly. |
| 4481 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4482 | * @param[in] uses Uses to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4483 | * @param[in,out] unres List of unresolved items. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4484 | * |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 4485 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4486 | */ |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 4487 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4488 | resolve_uses(struct lys_node_uses *uses, struct unres_schema *unres) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4489 | { |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4490 | struct ly_ctx *ctx = uses->module->ctx; /* shortcut */ |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4491 | struct lys_node *node = NULL, *next, *iter, **refine_nodes = NULL; |
Pavol Vican | 55abd33 | 2016-07-12 15:54:49 +0200 | [diff] [blame] | 4492 | struct lys_node *node_aux, *parent, *tmp; |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4493 | struct lys_node_leaflist *llist; |
| 4494 | struct lys_node_leaf *leaf; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 4495 | struct lys_refine *rfn; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4496 | struct lys_restr *must, **old_must; |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4497 | struct lys_iffeature *iff, **old_iff; |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4498 | int i, j, k, rc; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4499 | uint8_t size, *old_size; |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4500 | unsigned int usize, usize1, usize2; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4501 | |
Michal Vasko | 71e1aa8 | 2015-08-12 12:17:51 +0200 | [diff] [blame] | 4502 | assert(uses->grp); |
Michal Vasko | e770851 | 2016-03-11 10:26:55 +0100 | [diff] [blame] | 4503 | /* HACK just check that the grouping is resolved */ |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 4504 | assert(!uses->grp->nacm); |
Michal Vasko | 71e1aa8 | 2015-08-12 12:17:51 +0200 | [diff] [blame] | 4505 | |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4506 | if (!uses->grp->child) { |
| 4507 | /* grouping without children, warning was already displayed */ |
| 4508 | return EXIT_SUCCESS; |
| 4509 | } |
| 4510 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4511 | /* copy the data nodes from grouping into the uses context */ |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 4512 | LY_TREE_FOR(uses->grp->child, node_aux) { |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4513 | 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] | 4514 | if (!node) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4515 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, uses->grp->name, "uses"); |
| 4516 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, "Copying data from grouping failed."); |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4517 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4518 | } |
Pavol Vican | 55abd33 | 2016-07-12 15:54:49 +0200 | [diff] [blame] | 4519 | /* test the name of siblings */ |
| 4520 | 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] | 4521 | 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] | 4522 | goto fail; |
Pavol Vican | 55abd33 | 2016-07-12 15:54:49 +0200 | [diff] [blame] | 4523 | } |
| 4524 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4525 | } |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4526 | |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 4527 | /* we managed to copy the grouping, the rest must be possible to resolve */ |
| 4528 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4529 | if (uses->refine_size) { |
| 4530 | refine_nodes = malloc(uses->refine_size * sizeof *refine_nodes); |
| 4531 | if (!refine_nodes) { |
| 4532 | LOGMEM; |
| 4533 | goto fail; |
| 4534 | } |
| 4535 | } |
| 4536 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4537 | /* apply refines */ |
| 4538 | for (i = 0; i < uses->refine_size; i++) { |
| 4539 | rfn = &uses->refine[i]; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 4540 | 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] | 4541 | 1, 0, (const struct lys_node **)&node); |
Michal Vasko | 9bb061b | 2016-02-12 11:00:19 +0100 | [diff] [blame] | 4542 | if (rc || !node) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4543 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine"); |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4544 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4545 | } |
| 4546 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4547 | if (rfn->target_type && !(node->nodetype & rfn->target_type)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4548 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine"); |
| 4549 | 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] | 4550 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4551 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4552 | refine_nodes[i] = node; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4553 | |
| 4554 | /* description on any nodetype */ |
| 4555 | if (rfn->dsc) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4556 | lydict_remove(ctx, node->dsc); |
| 4557 | node->dsc = lydict_insert(ctx, rfn->dsc, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4558 | } |
| 4559 | |
| 4560 | /* reference on any nodetype */ |
| 4561 | if (rfn->ref) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4562 | lydict_remove(ctx, node->ref); |
| 4563 | node->ref = lydict_insert(ctx, rfn->ref, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4564 | } |
| 4565 | |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4566 | /* config on any nodetype, |
| 4567 | * in case of notification or rpc/action, the config is not applicable (there is no config status) */ |
| 4568 | if ((rfn->flags & LYS_CONFIG_MASK) && (node->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4569 | node->flags &= ~LYS_CONFIG_MASK; |
| 4570 | node->flags |= (rfn->flags & LYS_CONFIG_MASK); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4571 | } |
| 4572 | |
| 4573 | /* default value ... */ |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4574 | if (rfn->dflt_size) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4575 | if (node->nodetype == LYS_LEAF) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4576 | /* leaf */ |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4577 | leaf = (struct lys_node_leaf *)node; |
| 4578 | |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 4579 | /* replace default value */ |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4580 | lydict_remove(ctx, leaf->dflt); |
| 4581 | leaf->dflt = lydict_insert(ctx, rfn->dflt[0], 0); |
| 4582 | |
| 4583 | /* check the default value */ |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 4584 | if (unres_schema_add_node(leaf->module, unres, &leaf->type, UNRES_TYPE_DFLT, |
| 4585 | (struct lys_node *)(&leaf->dflt)) == -1) { |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4586 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4587 | } |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4588 | } else if (node->nodetype == LYS_LEAFLIST) { |
| 4589 | /* leaf-list */ |
| 4590 | llist = (struct lys_node_leaflist *)node; |
| 4591 | |
| 4592 | /* remove complete set of defaults in target */ |
| 4593 | for (i = 0; i < llist->dflt_size; i++) { |
| 4594 | lydict_remove(ctx, llist->dflt[i]); |
| 4595 | } |
| 4596 | free(llist->dflt); |
| 4597 | |
| 4598 | /* copy the default set from refine */ |
| 4599 | llist->dflt_size = rfn->dflt_size; |
| 4600 | llist->dflt = malloc(llist->dflt_size * sizeof *llist->dflt); |
| 4601 | for (i = 0; i < llist->dflt_size; i++) { |
| 4602 | llist->dflt[i] = lydict_insert(ctx, rfn->dflt[i], 0); |
| 4603 | } |
| 4604 | |
| 4605 | /* check default value */ |
| 4606 | for (i = 0; i < llist->dflt_size; i++) { |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 4607 | if (unres_schema_add_node(llist->module, unres, &llist->type, UNRES_TYPE_DFLT, |
| 4608 | (struct lys_node *)(&llist->dflt[i])) == -1) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4609 | goto fail; |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4610 | } |
| 4611 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4612 | } |
| 4613 | } |
| 4614 | |
| 4615 | /* mandatory on leaf, anyxml or choice */ |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 4616 | if (rfn->flags & LYS_MAND_MASK) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4617 | if (node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_CHOICE)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4618 | /* remove current value */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4619 | node->flags &= ~LYS_MAND_MASK; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4620 | |
| 4621 | /* set new value */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4622 | node->flags |= (rfn->flags & LYS_MAND_MASK); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4623 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4624 | if (rfn->flags & LYS_MAND_TRUE) { |
| 4625 | /* check if node has default value */ |
| 4626 | if ((node->nodetype & LYS_LEAF) && ((struct lys_node_leaf *)node)->dflt) { |
| 4627 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The \"mandatory\" statement is forbidden on leaf with \"default\"."); |
| 4628 | goto fail; |
| 4629 | } |
| 4630 | if ((node->nodetype & LYS_CHOICE) && ((struct lys_node_choice *)node)->dflt) { |
| 4631 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The \"mandatory\" statement is forbidden on choices with \"default\"."); |
| 4632 | goto fail; |
| 4633 | } |
| 4634 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4635 | } |
| 4636 | |
| 4637 | /* presence on container */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4638 | if ((node->nodetype & LYS_CONTAINER) && rfn->mod.presence) { |
| 4639 | lydict_remove(ctx, ((struct lys_node_container *)node)->presence); |
| 4640 | ((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] | 4641 | } |
| 4642 | |
| 4643 | /* min/max-elements on list or leaf-list */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4644 | if (node->nodetype == LYS_LIST) { |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 4645 | if (rfn->flags & LYS_RFN_MINSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4646 | ((struct lys_node_list *)node)->min = rfn->mod.list.min; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4647 | } |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 4648 | if (rfn->flags & LYS_RFN_MAXSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4649 | ((struct lys_node_list *)node)->max = rfn->mod.list.max; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4650 | } |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4651 | } else if (node->nodetype == LYS_LEAFLIST) { |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 4652 | if (rfn->flags & LYS_RFN_MINSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4653 | ((struct lys_node_leaflist *)node)->min = rfn->mod.list.min; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4654 | } |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 4655 | if (rfn->flags & LYS_RFN_MAXSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4656 | ((struct lys_node_leaflist *)node)->max = rfn->mod.list.max; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4657 | } |
| 4658 | } |
| 4659 | |
| 4660 | /* must in leaf, leaf-list, list, container or anyxml */ |
| 4661 | if (rfn->must_size) { |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4662 | switch (node->nodetype) { |
| 4663 | case LYS_LEAF: |
| 4664 | old_size = &((struct lys_node_leaf *)node)->must_size; |
| 4665 | old_must = &((struct lys_node_leaf *)node)->must; |
| 4666 | break; |
| 4667 | case LYS_LEAFLIST: |
| 4668 | old_size = &((struct lys_node_leaflist *)node)->must_size; |
| 4669 | old_must = &((struct lys_node_leaflist *)node)->must; |
| 4670 | break; |
| 4671 | case LYS_LIST: |
| 4672 | old_size = &((struct lys_node_list *)node)->must_size; |
| 4673 | old_must = &((struct lys_node_list *)node)->must; |
| 4674 | break; |
| 4675 | case LYS_CONTAINER: |
| 4676 | old_size = &((struct lys_node_container *)node)->must_size; |
| 4677 | old_must = &((struct lys_node_container *)node)->must; |
| 4678 | break; |
| 4679 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4680 | case LYS_ANYDATA: |
| 4681 | old_size = &((struct lys_node_anydata *)node)->must_size; |
| 4682 | old_must = &((struct lys_node_anydata *)node)->must; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4683 | break; |
| 4684 | default: |
| 4685 | LOGINT; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4686 | goto fail; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4687 | } |
| 4688 | |
| 4689 | size = *old_size + rfn->must_size; |
| 4690 | must = realloc(*old_must, size * sizeof *rfn->must); |
| 4691 | if (!must) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4692 | LOGMEM; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4693 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4694 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4695 | for (k = 0, j = *old_size; k < rfn->must_size; k++, j++) { |
| 4696 | must[j].expr = lydict_insert(ctx, rfn->must[k].expr, 0); |
| 4697 | must[j].dsc = lydict_insert(ctx, rfn->must[k].dsc, 0); |
| 4698 | must[j].ref = lydict_insert(ctx, rfn->must[k].ref, 0); |
| 4699 | must[j].eapptag = lydict_insert(ctx, rfn->must[k].eapptag, 0); |
| 4700 | must[j].emsg = lydict_insert(ctx, rfn->must[k].emsg, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4701 | } |
| 4702 | |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4703 | *old_must = must; |
| 4704 | *old_size = size; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 4705 | |
| 4706 | /* check XPath dependencies again */ |
| 4707 | if (unres_schema_add_node(node->module, unres, node, UNRES_XPATH, NULL) == -1) { |
| 4708 | goto fail; |
| 4709 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4710 | } |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4711 | |
| 4712 | /* if-feature in leaf, leaf-list, list, container or anyxml */ |
| 4713 | if (rfn->iffeature_size) { |
| 4714 | old_size = &node->iffeature_size; |
| 4715 | old_iff = &node->iffeature; |
| 4716 | |
| 4717 | size = *old_size + rfn->iffeature_size; |
| 4718 | iff = realloc(*old_iff, size * sizeof *rfn->iffeature); |
| 4719 | if (!iff) { |
| 4720 | LOGMEM; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4721 | goto fail; |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4722 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4723 | for (k = 0, j = *old_size; k < rfn->iffeature_size; k++, j++) { |
| 4724 | resolve_iffeature_getsizes(&rfn->iffeature[k], &usize1, &usize2); |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4725 | if (usize1) { |
| 4726 | /* there is something to duplicate */ |
| 4727 | /* duplicate compiled expression */ |
| 4728 | usize = (usize1 / 4) + (usize1 % 4) ? 1 : 0; |
| 4729 | iff[j].expr = malloc(usize * sizeof *iff[j].expr); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4730 | 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] | 4731 | |
| 4732 | /* duplicate list of feature pointers */ |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4733 | iff[j].features = malloc(usize2 * sizeof *iff[k].features); |
| 4734 | 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] | 4735 | } |
| 4736 | } |
| 4737 | |
| 4738 | *old_iff = iff; |
| 4739 | *old_size = size; |
| 4740 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4741 | } |
| 4742 | |
| 4743 | /* apply augments */ |
| 4744 | for (i = 0; i < uses->augment_size; i++) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4745 | rc = resolve_augment(&uses->augment[i], uses->child); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4746 | if (rc) { |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4747 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4748 | } |
| 4749 | } |
| 4750 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4751 | /* check refines */ |
| 4752 | for (i = 0; i < uses->refine_size; i++) { |
| 4753 | node = refine_nodes[i]; |
| 4754 | rfn = &uses->refine[i]; |
| 4755 | |
| 4756 | /* config on any nodetype */ |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4757 | if ((rfn->flags & LYS_CONFIG_MASK) && (node->flags & LYS_CONFIG_MASK)) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4758 | for (parent = lys_parent(node); parent && parent->nodetype == LYS_USES; parent = lys_parent(parent)); |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 4759 | if (parent && parent->nodetype != LYS_GROUPING && (parent->flags & LYS_CONFIG_MASK) && |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4760 | ((parent->flags & LYS_CONFIG_MASK) != (rfn->flags & LYS_CONFIG_MASK)) && |
| 4761 | (rfn->flags & LYS_CONFIG_W)) { |
| 4762 | /* setting config true under config false is prohibited */ |
| 4763 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, "config", "refine"); |
| 4764 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, |
| 4765 | "changing config from 'false' to 'true' is prohibited while " |
| 4766 | "the target's parent is still config 'false'."); |
| 4767 | goto fail; |
| 4768 | } |
| 4769 | |
| 4770 | /* inherit config change to the target children */ |
| 4771 | LY_TREE_DFS_BEGIN(node->child, next, iter) { |
| 4772 | if (rfn->flags & LYS_CONFIG_W) { |
| 4773 | if (iter->flags & LYS_CONFIG_SET) { |
| 4774 | /* config is set explicitely, go to next sibling */ |
| 4775 | next = NULL; |
| 4776 | goto nextsibling; |
| 4777 | } |
| 4778 | } else { /* LYS_CONFIG_R */ |
| 4779 | if ((iter->flags & LYS_CONFIG_SET) && (iter->flags & LYS_CONFIG_W)) { |
| 4780 | /* error - we would have config data under status data */ |
| 4781 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, "config", "refine"); |
| 4782 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, |
| 4783 | "changing config from 'true' to 'false' is prohibited while the target " |
| 4784 | "has still a children with explicit config 'true'."); |
| 4785 | goto fail; |
| 4786 | } |
| 4787 | } |
| 4788 | /* change config */ |
| 4789 | iter->flags &= ~LYS_CONFIG_MASK; |
| 4790 | iter->flags |= (rfn->flags & LYS_CONFIG_MASK); |
| 4791 | |
| 4792 | /* select next iter - modified LY_TREE_DFS_END */ |
| 4793 | if (iter->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
| 4794 | next = NULL; |
| 4795 | } else { |
| 4796 | next = iter->child; |
| 4797 | } |
| 4798 | nextsibling: |
| 4799 | if (!next) { |
| 4800 | /* try siblings */ |
| 4801 | next = iter->next; |
| 4802 | } |
| 4803 | while (!next) { |
| 4804 | /* parent is already processed, go to its sibling */ |
| 4805 | iter = lys_parent(iter); |
| 4806 | |
| 4807 | /* no siblings, go back through parents */ |
| 4808 | if (iter == node) { |
| 4809 | /* we are done, no next element to process */ |
| 4810 | break; |
| 4811 | } |
| 4812 | next = iter->next; |
| 4813 | } |
| 4814 | } |
| 4815 | } |
| 4816 | |
| 4817 | /* default value */ |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 4818 | if (rfn->dflt_size) { |
| 4819 | if (node->nodetype == LYS_CHOICE) { |
| 4820 | /* choice */ |
| 4821 | ((struct lys_node_choice *)node)->dflt = resolve_choice_dflt((struct lys_node_choice *)node, |
| 4822 | rfn->dflt[0]); |
| 4823 | if (!((struct lys_node_choice *)node)->dflt) { |
| 4824 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->dflt[0], "default"); |
| 4825 | goto fail; |
| 4826 | } |
| 4827 | if (lyp_check_mandatory_choice(node)) { |
| 4828 | goto fail; |
| 4829 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4830 | } |
| 4831 | } |
| 4832 | |
| 4833 | /* min/max-elements on list or leaf-list */ |
| 4834 | if (node->nodetype == LYS_LIST) { |
| 4835 | if (((struct lys_node_list *)node)->min > ((struct lys_node_list *)node)->max) { |
| 4836 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid value \"%d\" of \"%s\".", rfn->mod.list.min, "min-elements"); |
| 4837 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"min-elements\" is bigger than \"max-elements\"."); |
| 4838 | goto fail; |
| 4839 | } |
| 4840 | } else if (node->nodetype == LYS_LEAFLIST) { |
| 4841 | if (((struct lys_node_leaflist *)node)->min > ((struct lys_node_leaflist *)node)->max) { |
| 4842 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid value \"%d\" of \"%s\".", rfn->mod.list.min, "min-elements"); |
| 4843 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"min-elements\" is bigger than \"max-elements\"."); |
| 4844 | goto fail; |
| 4845 | } |
| 4846 | } |
| 4847 | |
| 4848 | /* additional checks */ |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 4849 | /* default value with mandatory/min-elements */ |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4850 | if (node->nodetype == LYS_LEAFLIST) { |
| 4851 | llist = (struct lys_node_leaflist *)node; |
| 4852 | if (llist->dflt_size && llist->min) { |
| 4853 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, rfn->dflt_size ? "default" : "min-elements", "refine"); |
| 4854 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, |
| 4855 | "The \"min-elements\" statement with non-zero value is forbidden on leaf-lists with the \"default\" statement."); |
| 4856 | goto fail; |
| 4857 | } |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 4858 | } else if (node->nodetype == LYS_LEAF) { |
| 4859 | leaf = (struct lys_node_leaf *)node; |
| 4860 | if (leaf->dflt && (leaf->flags & LYS_MAND_TRUE)) { |
| 4861 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, rfn->dflt_size ? "default" : "mandatory", "refine"); |
| 4862 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, |
| 4863 | "The \"mandatory\" statement is forbidden on leafs with the \"default\" statement."); |
| 4864 | goto fail; |
| 4865 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4866 | } |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 4867 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4868 | /* check for mandatory node in default case, first find the closest parent choice to the changed node */ |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 4869 | if ((rfn->flags & LYS_MAND_TRUE) || rfn->mod.list.min) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4870 | for (parent = node->parent; |
| 4871 | parent && !(parent->nodetype & (LYS_CHOICE | LYS_GROUPING | LYS_ACTION | LYS_USES)); |
| 4872 | parent = parent->parent) { |
| 4873 | if (parent->nodetype == LYS_CONTAINER && ((struct lys_node_container *)parent)->presence) { |
| 4874 | /* stop also on presence containers */ |
| 4875 | break; |
| 4876 | } |
| 4877 | } |
| 4878 | /* and if it is a choice with the default case, check it for presence of a mandatory node in it */ |
| 4879 | if (parent && parent->nodetype == LYS_CHOICE && ((struct lys_node_choice *)parent)->dflt) { |
| 4880 | if (lyp_check_mandatory_choice(parent)) { |
| 4881 | goto fail; |
| 4882 | } |
| 4883 | } |
| 4884 | } |
| 4885 | } |
| 4886 | free(refine_nodes); |
| 4887 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4888 | return EXIT_SUCCESS; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4889 | |
| 4890 | fail: |
| 4891 | LY_TREE_FOR_SAFE(uses->child, next, iter) { |
| 4892 | lys_node_free(iter, NULL, 0); |
| 4893 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4894 | free(refine_nodes); |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4895 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4896 | } |
| 4897 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4898 | static int |
| 4899 | identity_backlink_update(struct lys_ident *der, struct lys_ident *base) |
| 4900 | { |
| 4901 | int i; |
| 4902 | |
| 4903 | assert(der && base); |
| 4904 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4905 | if (!base->der) { |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 4906 | /* create a set for backlinks if it does not exist */ |
| 4907 | base->der = ly_set_new(); |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4908 | } |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 4909 | /* store backlink */ |
| 4910 | ly_set_add(base->der, der, LY_SET_OPT_USEASLIST); |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4911 | |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 4912 | /* do it recursively */ |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4913 | for (i = 0; i < base->base_size; i++) { |
| 4914 | if (identity_backlink_update(der, base->base[i])) { |
| 4915 | return EXIT_FAILURE; |
| 4916 | } |
| 4917 | } |
| 4918 | |
| 4919 | return EXIT_SUCCESS; |
| 4920 | } |
| 4921 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4922 | /** |
| 4923 | * @brief Resolve base identity recursively. Does not log. |
| 4924 | * |
| 4925 | * @param[in] module Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4926 | * @param[in] ident Identity to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4927 | * @param[in] basename Base name of the identity. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4928 | * @param[out] ret Pointer to the resolved identity. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4929 | * |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 4930 | * @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] | 4931 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4932 | static int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 4933 | 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] | 4934 | struct unres_schema *unres, struct lys_ident **ret) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4935 | { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 4936 | uint32_t i, j; |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4937 | struct lys_ident *base = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4938 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 4939 | assert(ret); |
| 4940 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4941 | /* search module */ |
| 4942 | for (i = 0; i < module->ident_size; i++) { |
| 4943 | if (!strcmp(basename, module->ident[i].name)) { |
| 4944 | |
| 4945 | if (!ident) { |
| 4946 | /* just search for type, so do not modify anything, just return |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 4947 | * the base identity pointer */ |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 4948 | *ret = &module->ident[i]; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4949 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4950 | } |
| 4951 | |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 4952 | base = &module->ident[i]; |
| 4953 | goto matchfound; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4954 | } |
| 4955 | } |
| 4956 | |
| 4957 | /* search submodules */ |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 4958 | for (j = 0; j < module->inc_size && module->inc[j].submodule; j++) { |
| 4959 | for (i = 0; i < module->inc[j].submodule->ident_size; i++) { |
| 4960 | if (!strcmp(basename, module->inc[j].submodule->ident[i].name)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4961 | |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 4962 | if (!ident) { |
| 4963 | *ret = &module->inc[j].submodule->ident[i]; |
| 4964 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4965 | } |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 4966 | |
| 4967 | base = &module->inc[j].submodule->ident[i]; |
| 4968 | goto matchfound; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4969 | } |
| 4970 | } |
| 4971 | } |
| 4972 | |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 4973 | matchfound: |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4974 | /* we found it somewhere */ |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 4975 | if (base) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4976 | /* is it already completely resolved? */ |
| 4977 | for (i = 0; i < unres->count; i++) { |
Radek Krejci | ba7b1cc | 2016-08-08 13:44:43 +0200 | [diff] [blame] | 4978 | if ((unres->item[i] == base) && (unres->type[i] == UNRES_IDENT)) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4979 | /* identity found, but not yet resolved, so do not return it in *res and try it again later */ |
| 4980 | |
| 4981 | /* simple check for circular reference, |
| 4982 | * the complete check is done as a side effect of using only completely |
| 4983 | * resolved identities (previous check of unres content) */ |
| 4984 | if (ly_strequal((const char *)unres->str_snode[i], ident->name, 1)) { |
| 4985 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, basename, "base"); |
| 4986 | 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] | 4987 | return -1; |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4988 | } |
| 4989 | |
Radek Krejci | 06f64ed | 2016-08-15 11:07:44 +0200 | [diff] [blame] | 4990 | return EXIT_FAILURE; |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 4991 | } |
| 4992 | } |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4993 | |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 4994 | /* checks done, store the result */ |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4995 | *ret = base; |
Pavol Vican | fdab9f9 | 2016-09-07 15:23:27 +0200 | [diff] [blame] | 4996 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4997 | } |
| 4998 | |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 4999 | /* base not found (maybe a forward reference) */ |
| 5000 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5001 | } |
| 5002 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5003 | /** |
| 5004 | * @brief Resolve base identity. Logs directly. |
| 5005 | * |
| 5006 | * @param[in] module Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5007 | * @param[in] ident Identity to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5008 | * @param[in] basename Base name of the identity. |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 5009 | * @param[in] parent Either "type" or "identity". |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5010 | * @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] | 5011 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5012 | * @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] | 5013 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5014 | static int |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5015 | 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] | 5016 | struct lys_type *type, struct unres_schema *unres) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5017 | { |
| 5018 | const char *name; |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5019 | int mod_name_len = 0, rc; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5020 | struct lys_ident *target, **ret; |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 5021 | uint16_t flags; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5022 | struct lys_module *mod; |
| 5023 | |
| 5024 | assert((ident && !type) || (!ident && type)); |
| 5025 | |
| 5026 | if (!type) { |
| 5027 | /* have ident to resolve */ |
| 5028 | ret = ⌖ |
| 5029 | flags = ident->flags; |
| 5030 | mod = ident->module; |
| 5031 | } else { |
| 5032 | /* have type to fill */ |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5033 | ++type->info.ident.count; |
| 5034 | type->info.ident.ref = ly_realloc(type->info.ident.ref, type->info.ident.count * sizeof *type->info.ident.ref); |
| 5035 | if (!type->info.ident.ref) { |
| 5036 | LOGMEM; |
| 5037 | return -1; |
| 5038 | } |
| 5039 | |
| 5040 | ret = &type->info.ident.ref[type->info.ident.count - 1]; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5041 | flags = type->parent->flags; |
| 5042 | mod = type->parent->module; |
| 5043 | } |
Michal Vasko | f200600 | 2016-04-21 16:28:15 +0200 | [diff] [blame] | 5044 | *ret = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5045 | |
| 5046 | /* search for the base identity */ |
| 5047 | name = strchr(basename, ':'); |
| 5048 | if (name) { |
| 5049 | /* set name to correct position after colon */ |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 5050 | mod_name_len = name - basename; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5051 | name++; |
| 5052 | |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 5053 | 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] | 5054 | /* prefix refers to the current module, ignore it */ |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 5055 | mod_name_len = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5056 | } |
| 5057 | } else { |
| 5058 | name = basename; |
| 5059 | } |
| 5060 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 5061 | /* get module where to search */ |
| 5062 | module = lys_get_import_module(module, NULL, 0, mod_name_len ? basename : NULL, mod_name_len); |
| 5063 | if (!module) { |
| 5064 | /* identity refers unknown data model */ |
Radek Krejci | 02a0499 | 2016-03-17 16:06:37 +0100 | [diff] [blame] | 5065 | LOGVAL(LYE_INMOD, LY_VLOG_NONE, NULL, basename); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 5066 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5067 | } |
| 5068 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 5069 | /* search in the identified module ... */ |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5070 | rc = resolve_base_ident_sub(module, ident, name, unres, ret); |
| 5071 | if (!rc) { |
| 5072 | assert(*ret); |
| 5073 | |
| 5074 | /* check status */ |
| 5075 | if (lyp_check_status(flags, mod, ident ? ident->name : "of type", |
| 5076 | (*ret)->flags, (*ret)->module, (*ret)->name, NULL)) { |
| 5077 | rc = -1; |
Pavol Vican | fdab9f9 | 2016-09-07 15:23:27 +0200 | [diff] [blame] | 5078 | } else { |
| 5079 | if (ident) { |
| 5080 | ident->base[ident->base_size++] = *ret; |
| 5081 | |
| 5082 | /* maintain backlinks to the derived identities */ |
| 5083 | rc = identity_backlink_update(ident, *ret) ? -1 : EXIT_SUCCESS; |
| 5084 | } |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5085 | } |
| 5086 | } else if (rc == EXIT_FAILURE) { |
| 5087 | LOGVAL(LYE_INRESOLV, LY_VLOG_NONE, NULL, parent, basename); |
Pavol Vican | 053a288 | 2016-09-05 14:40:33 +0200 | [diff] [blame] | 5088 | if (type) { |
| 5089 | --type->info.ident.count; |
| 5090 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5091 | } |
| 5092 | |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5093 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5094 | } |
| 5095 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5096 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 5097 | * @brief Resolve JSON data format identityref. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5098 | * |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5099 | * @param[in] type Identityref type. |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 5100 | * @param[in] ident_name Identityref name. |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 5101 | * @param[in] node Node where the identityref is being resolved |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5102 | * |
| 5103 | * @return Pointer to the identity resolvent, NULL on error. |
| 5104 | */ |
Radek Krejci | a52656e | 2015-08-05 13:41:50 +0200 | [diff] [blame] | 5105 | struct lys_ident * |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5106 | 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] | 5107 | { |
Michal Vasko | c633ca0 | 2015-08-21 14:03:51 +0200 | [diff] [blame] | 5108 | const char *mod_name, *name; |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5109 | int mod_name_len, rc, i; |
| 5110 | unsigned int u; |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5111 | struct lys_ident *der, *cur; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5112 | |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5113 | if (!type || (!type->info.ident.count && !type->der) || !ident_name) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5114 | return NULL; |
| 5115 | } |
| 5116 | |
Michal Vasko | c633ca0 | 2015-08-21 14:03:51 +0200 | [diff] [blame] | 5117 | 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] | 5118 | if (rc < 1) { |
Radek Krejci | 1899d6a | 2016-11-03 13:48:07 +0100 | [diff] [blame] | 5119 | if (node) { |
| 5120 | LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, ident_name[-rc], &ident_name[-rc]); |
Radek Krejci | 5dca593 | 2016-11-04 14:30:47 +0100 | [diff] [blame] | 5121 | } else { |
| 5122 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid identityref value \"%s\".", ident_name); |
| 5123 | ly_vecode = LYVE_INCHAR; |
Radek Krejci | 1899d6a | 2016-11-03 13:48:07 +0100 | [diff] [blame] | 5124 | } |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 5125 | return NULL; |
Michal Vasko | b43bb3c | 2016-03-17 15:00:27 +0100 | [diff] [blame] | 5126 | } else if (rc < (signed)strlen(ident_name)) { |
Radek Krejci | 1899d6a | 2016-11-03 13:48:07 +0100 | [diff] [blame] | 5127 | if (node) { |
| 5128 | LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, ident_name[rc], &ident_name[rc]); |
Radek Krejci | 5dca593 | 2016-11-04 14:30:47 +0100 | [diff] [blame] | 5129 | } else { |
| 5130 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid identityref value \"%s\".", ident_name); |
| 5131 | ly_vecode = LYVE_INCHAR; |
Radek Krejci | 1899d6a | 2016-11-03 13:48:07 +0100 | [diff] [blame] | 5132 | } |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 5133 | return NULL; |
| 5134 | } |
| 5135 | |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5136 | /* go through all the bases in all the derived types */ |
| 5137 | while (type->der) { |
| 5138 | for (i = 0; i < type->info.ident.count; ++i) { |
| 5139 | cur = type->info.ident.ref[i]; |
| 5140 | if (!strcmp(cur->name, name) && (!mod_name |
| 5141 | || (!strncmp(cur->module->name, mod_name, mod_name_len) && !cur->module->name[mod_name_len]))) { |
| 5142 | goto match; |
| 5143 | } |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 5144 | |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5145 | if (cur->der) { |
| 5146 | /* there are also some derived identities */ |
| 5147 | for (u = 0; u < cur->der->number; u++) { |
| 5148 | der = (struct lys_ident *)cur->der->set.g[u]; /* shortcut */ |
| 5149 | if (!strcmp(der->name, name) && |
| 5150 | (!mod_name || (!strncmp(der->module->name, mod_name, mod_name_len) && !der->module->name[mod_name_len]))) { |
| 5151 | /* we have match */ |
| 5152 | cur = der; |
| 5153 | goto match; |
| 5154 | } |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5155 | } |
| 5156 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5157 | } |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5158 | type = &type->der->type; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5159 | } |
| 5160 | |
Radek Krejci | 1899d6a | 2016-11-03 13:48:07 +0100 | [diff] [blame] | 5161 | if (node) { |
| 5162 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYD, node, "identityref", ident_name); |
Radek Krejci | 5dca593 | 2016-11-04 14:30:47 +0100 | [diff] [blame] | 5163 | } else { |
| 5164 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid identityref value \"%s\".", ident_name); |
| 5165 | ly_vecode = LYVE_INRESOLV; |
Radek Krejci | 1899d6a | 2016-11-03 13:48:07 +0100 | [diff] [blame] | 5166 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5167 | return NULL; |
Radek Krejci | f1ee2e2 | 2016-08-02 16:36:48 +0200 | [diff] [blame] | 5168 | |
| 5169 | match: |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5170 | for (i = 0; i < cur->iffeature_size; i++) { |
| 5171 | if (!resolve_iffeature(&cur->iffeature[i])) { |
Radek Krejci | 1899d6a | 2016-11-03 13:48:07 +0100 | [diff] [blame] | 5172 | if (node) { |
| 5173 | LOGVAL(LYE_INVAL, LY_VLOG_LYD, node, cur->name, node->schema->name); |
| 5174 | LOGVAL(LYE_SPEC, LY_VLOG_LYD, node, "Identity \"%s\" is disabled by its if-feature condition.", |
| 5175 | cur->name); |
Radek Krejci | 5dca593 | 2016-11-04 14:30:47 +0100 | [diff] [blame] | 5176 | } else { |
| 5177 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Identity \"%s\" is disabled by its if-feature condition.", |
| 5178 | cur->name); |
| 5179 | ly_vecode = LYVE_INVAL; |
Radek Krejci | 1899d6a | 2016-11-03 13:48:07 +0100 | [diff] [blame] | 5180 | } |
Radek Krejci | f1ee2e2 | 2016-08-02 16:36:48 +0200 | [diff] [blame] | 5181 | return NULL; |
| 5182 | } |
| 5183 | } |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5184 | return cur; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5185 | } |
| 5186 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5187 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5188 | * @brief Resolve unresolved uses. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5189 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5190 | * @param[in] uses Uses to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5191 | * @param[in] unres Specific unres item. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5192 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5193 | * @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] | 5194 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5195 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5196 | 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] | 5197 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5198 | int rc; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5199 | struct lys_node *par_grp; |
Michal Vasko | e91afce | 2015-08-12 12:21:00 +0200 | [diff] [blame] | 5200 | |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5201 | /* HACK: when a grouping has uses inside, all such uses have to be resolved before the grouping itself |
| 5202 | * is used in some uses. When we see such a uses, the grouping's nacm member (not used in grouping) |
| 5203 | * is used to store number of so far unresolved uses. The grouping cannot be used unless the nacm |
| 5204 | * value is decreased back to 0. To remember that the uses already increased grouping's nacm, the |
| 5205 | * LYS_USESGRP flag is used. */ |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 5206 | 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] | 5207 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5208 | if (!uses->grp) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 5209 | rc = resolve_uses_schema_nodeid(uses->name, (const struct lys_node *)uses, (const struct lys_node_grp **)&uses->grp); |
| 5210 | if (rc == -1) { |
Michal Vasko | 92981a6 | 2016-10-14 10:25:16 +0200 | [diff] [blame] | 5211 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 5212 | return -1; |
| 5213 | } else if (rc > 0) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5214 | 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] | 5215 | return -1; |
| 5216 | } else if (!uses->grp) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5217 | if (par_grp && !(uses->flags & LYS_USESGRP)) { |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 5218 | /* hack - in contrast to lys_node, lys_node_grp has bigger nacm field |
| 5219 | * (and smaller flags - it uses only a limited set of flags) |
| 5220 | */ |
| 5221 | ((struct lys_node_grp *)par_grp)->nacm++; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5222 | uses->flags |= LYS_USESGRP; |
Michal Vasko | 407f1bb | 2015-09-23 15:51:07 +0200 | [diff] [blame] | 5223 | } |
Michal Vasko | 92981a6 | 2016-10-14 10:25:16 +0200 | [diff] [blame] | 5224 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 5225 | return EXIT_FAILURE; |
Michal Vasko | 12e3084 | 2015-08-04 11:54:00 +0200 | [diff] [blame] | 5226 | } |
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 | if (uses->grp->nacm) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5230 | if (par_grp && !(uses->flags & LYS_USESGRP)) { |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 5231 | ((struct lys_node_grp *)par_grp)->nacm++; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5232 | uses->flags |= LYS_USESGRP; |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 5233 | } else { |
| 5234 | /* instantiate grouping only when it is completely resolved */ |
| 5235 | uses->grp = NULL; |
Michal Vasko | 407f1bb | 2015-09-23 15:51:07 +0200 | [diff] [blame] | 5236 | } |
Michal Vasko | 92981a6 | 2016-10-14 10:25:16 +0200 | [diff] [blame] | 5237 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5238 | return EXIT_FAILURE; |
| 5239 | } |
| 5240 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5241 | rc = resolve_uses(uses, unres); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5242 | if (!rc) { |
| 5243 | /* decrease unres count only if not first try */ |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5244 | if (par_grp && (uses->flags & LYS_USESGRP)) { |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 5245 | if (!((struct lys_node_grp *)par_grp)->nacm) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5246 | LOGINT; |
| 5247 | return -1; |
| 5248 | } |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 5249 | ((struct lys_node_grp *)par_grp)->nacm--; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5250 | uses->flags &= ~LYS_USESGRP; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5251 | } |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5252 | |
| 5253 | /* check status */ |
Radek Krejci | c655602 | 2016-01-27 15:16:45 +0100 | [diff] [blame] | 5254 | if (lyp_check_status(uses->flags, uses->module, "of uses", |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 5255 | uses->grp->flags, uses->grp->module, uses->grp->name, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5256 | (struct lys_node *)uses)) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5257 | return -1; |
| 5258 | } |
| 5259 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5260 | return EXIT_SUCCESS; |
| 5261 | } |
| 5262 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5263 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5264 | } |
| 5265 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5266 | /** |
Michal Vasko | 9957e59 | 2015-08-17 15:04:09 +0200 | [diff] [blame] | 5267 | * @brief Resolve list keys. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5268 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5269 | * @param[in] list List to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5270 | * @param[in] keys_str Keys node value. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5271 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5272 | * @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] | 5273 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5274 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5275 | resolve_list_keys(struct lys_node_list *list, const char *keys_str) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5276 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5277 | int i, len, rc; |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 5278 | const char *value; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5279 | |
| 5280 | for (i = 0; i < list->keys_size; ++i) { |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 5281 | if (!list->child) { |
| 5282 | /* no child, possible forward reference */ |
| 5283 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, list, "list keys", keys_str); |
| 5284 | return EXIT_FAILURE; |
| 5285 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5286 | /* get the key name */ |
| 5287 | if ((value = strpbrk(keys_str, " \t\n"))) { |
| 5288 | len = value - keys_str; |
| 5289 | while (isspace(value[0])) { |
| 5290 | value++; |
| 5291 | } |
| 5292 | } else { |
| 5293 | len = strlen(keys_str); |
| 5294 | } |
| 5295 | |
Radek Krejci | c428344 | 2016-04-22 09:19:27 +0200 | [diff] [blame] | 5296 | 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] | 5297 | if (rc) { |
Michal Vasko | 7a55bea | 2016-05-02 14:51:20 +0200 | [diff] [blame] | 5298 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, list, "list keys", keys_str); |
| 5299 | return EXIT_FAILURE; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5300 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5301 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5302 | if (check_key(list, i, keys_str, len)) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5303 | /* check_key logs */ |
| 5304 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5305 | } |
| 5306 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5307 | /* check status */ |
Radek Krejci | c655602 | 2016-01-27 15:16:45 +0100 | [diff] [blame] | 5308 | if (lyp_check_status(list->flags, list->module, list->name, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5309 | list->keys[i]->flags, list->keys[i]->module, list->keys[i]->name, |
| 5310 | (struct lys_node *)list->keys[i])) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5311 | return -1; |
| 5312 | } |
| 5313 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5314 | /* prepare for next iteration */ |
| 5315 | while (value && isspace(value[0])) { |
| 5316 | value++; |
| 5317 | } |
| 5318 | keys_str = value; |
| 5319 | } |
| 5320 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5321 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5322 | } |
| 5323 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5324 | /** |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5325 | * @brief Resolve (check) all must conditions of \p node. |
| 5326 | * Logs directly. |
| 5327 | * |
| 5328 | * @param[in] node Data node with optional must statements. |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5329 | * @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] | 5330 | * |
| 5331 | * @return EXIT_SUCCESS on pass, EXIT_FAILURE on fail, -1 on error. |
| 5332 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5333 | static int |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5334 | resolve_must(struct lyd_node *node, int inout_parent) |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5335 | { |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5336 | uint8_t i, must_size; |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5337 | struct lys_node *schema; |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5338 | struct lys_restr *must; |
| 5339 | struct lyxp_set set; |
| 5340 | |
| 5341 | assert(node); |
| 5342 | memset(&set, 0, sizeof set); |
| 5343 | |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5344 | if (inout_parent) { |
| 5345 | for (schema = lys_parent(node->schema); |
| 5346 | schema && (schema->nodetype & (LYS_CHOICE | LYS_CASE | LYS_USES)); |
| 5347 | schema = lys_parent(schema)); |
| 5348 | if (!schema || !(schema->nodetype & (LYS_INPUT | LYS_OUTPUT))) { |
| 5349 | LOGINT; |
| 5350 | return -1; |
| 5351 | } |
| 5352 | must_size = ((struct lys_node_inout *)schema)->must_size; |
| 5353 | must = ((struct lys_node_inout *)schema)->must; |
| 5354 | |
| 5355 | /* context node is the RPC/action */ |
| 5356 | node = node->parent; |
| 5357 | if (!(node->schema->nodetype & (LYS_RPC | LYS_ACTION))) { |
| 5358 | LOGINT; |
| 5359 | return -1; |
| 5360 | } |
| 5361 | } else { |
| 5362 | switch (node->schema->nodetype) { |
| 5363 | case LYS_CONTAINER: |
| 5364 | must_size = ((struct lys_node_container *)node->schema)->must_size; |
| 5365 | must = ((struct lys_node_container *)node->schema)->must; |
| 5366 | break; |
| 5367 | case LYS_LEAF: |
| 5368 | must_size = ((struct lys_node_leaf *)node->schema)->must_size; |
| 5369 | must = ((struct lys_node_leaf *)node->schema)->must; |
| 5370 | break; |
| 5371 | case LYS_LEAFLIST: |
| 5372 | must_size = ((struct lys_node_leaflist *)node->schema)->must_size; |
| 5373 | must = ((struct lys_node_leaflist *)node->schema)->must; |
| 5374 | break; |
| 5375 | case LYS_LIST: |
| 5376 | must_size = ((struct lys_node_list *)node->schema)->must_size; |
| 5377 | must = ((struct lys_node_list *)node->schema)->must; |
| 5378 | break; |
| 5379 | case LYS_ANYXML: |
| 5380 | case LYS_ANYDATA: |
| 5381 | must_size = ((struct lys_node_anydata *)node->schema)->must_size; |
| 5382 | must = ((struct lys_node_anydata *)node->schema)->must; |
| 5383 | break; |
| 5384 | case LYS_NOTIF: |
| 5385 | must_size = ((struct lys_node_notif *)node->schema)->must_size; |
| 5386 | must = ((struct lys_node_notif *)node->schema)->must; |
| 5387 | break; |
| 5388 | default: |
| 5389 | must_size = 0; |
| 5390 | break; |
| 5391 | } |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5392 | } |
| 5393 | |
| 5394 | for (i = 0; i < must_size; ++i) { |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5395 | 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] | 5396 | return -1; |
| 5397 | } |
| 5398 | |
Michal Vasko | 944a564 | 2016-03-21 11:48:58 +0100 | [diff] [blame] | 5399 | lyxp_set_cast(&set, LYXP_SET_BOOLEAN, node, LYXP_MUST); |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5400 | |
Michal Vasko | 8146d4c | 2016-05-09 15:50:29 +0200 | [diff] [blame] | 5401 | if (!set.val.bool) { |
Michal Vasko | 6ac6828 | 2016-04-11 10:56:47 +0200 | [diff] [blame] | 5402 | LOGVAL(LYE_NOMUST, LY_VLOG_LYD, node, must[i].expr); |
| 5403 | if (must[i].emsg) { |
| 5404 | LOGVAL(LYE_SPEC, LY_VLOG_LYD, node, must[i].emsg); |
| 5405 | } |
| 5406 | if (must[i].eapptag) { |
| 5407 | strncpy(((struct ly_err *)&ly_errno)->apptag, must[i].eapptag, LY_APPTAG_LEN - 1); |
| 5408 | } |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5409 | return 1; |
| 5410 | } |
| 5411 | } |
| 5412 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5413 | return EXIT_SUCCESS; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5414 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5415 | |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5416 | /** |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5417 | * @brief Resolve (find) when condition schema context node. Does not log. |
| 5418 | * |
| 5419 | * @param[in] schema Schema node with the when condition. |
| 5420 | * @param[out] ctx_snode When schema context node. |
| 5421 | * @param[out] ctx_snode_type Schema context node type. |
| 5422 | */ |
| 5423 | void |
| 5424 | resolve_when_ctx_snode(const struct lys_node *schema, struct lys_node **ctx_snode, enum lyxp_node_type *ctx_snode_type) |
| 5425 | { |
| 5426 | const struct lys_node *sparent; |
| 5427 | |
| 5428 | /* find a not schema-only node */ |
| 5429 | *ctx_snode_type = LYXP_NODE_ELEM; |
| 5430 | while (schema->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE | LYS_AUGMENT | LYS_INPUT | LYS_OUTPUT)) { |
| 5431 | if (schema->nodetype == LYS_AUGMENT) { |
| 5432 | sparent = ((struct lys_node_augment *)schema)->target; |
| 5433 | } else { |
| 5434 | sparent = schema->parent; |
| 5435 | } |
| 5436 | if (!sparent) { |
| 5437 | /* context node is the document root (fake root in our case) */ |
| 5438 | if (schema->flags & LYS_CONFIG_W) { |
| 5439 | *ctx_snode_type = LYXP_NODE_ROOT_CONFIG; |
| 5440 | } else { |
Michal Vasko | b94a5e4 | 2016-09-08 14:01:56 +0200 | [diff] [blame] | 5441 | *ctx_snode_type = LYXP_NODE_ROOT; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5442 | } |
Michal Vasko | 2d2aec1 | 2016-09-08 11:42:50 +0200 | [diff] [blame] | 5443 | /* we need the first top-level sibling, but no uses or groupings */ |
Michal Vasko | b94a5e4 | 2016-09-08 14:01:56 +0200 | [diff] [blame] | 5444 | schema = lys_getnext(NULL, NULL, lys_node_module(schema), 0); |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5445 | break; |
| 5446 | } |
| 5447 | schema = sparent; |
| 5448 | } |
| 5449 | |
| 5450 | *ctx_snode = (struct lys_node *)schema; |
| 5451 | } |
| 5452 | |
| 5453 | /** |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5454 | * @brief Resolve (find) when condition context node. Does not log. |
| 5455 | * |
| 5456 | * @param[in] node Data node, whose conditional definition is being decided. |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5457 | * @param[in] schema Schema node with the when condition. |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5458 | * @param[out] ctx_node Context node. |
| 5459 | * @param[out] ctx_node_type Context node type. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5460 | * |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5461 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5462 | */ |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5463 | static int |
| 5464 | resolve_when_ctx_node(struct lyd_node *node, struct lys_node *schema, struct lyd_node **ctx_node, |
| 5465 | enum lyxp_node_type *ctx_node_type) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5466 | { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5467 | struct lyd_node *parent; |
| 5468 | struct lys_node *sparent; |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5469 | enum lyxp_node_type node_type; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5470 | uint16_t i, data_depth, schema_depth; |
| 5471 | |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5472 | resolve_when_ctx_snode(schema, &schema, &node_type); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5473 | |
Michal Vasko | fe98975 | 2016-09-08 08:47:26 +0200 | [diff] [blame] | 5474 | if (node_type == LYXP_NODE_ELEM) { |
| 5475 | /* standard element context node */ |
| 5476 | for (parent = node, data_depth = 0; parent; parent = parent->parent, ++data_depth); |
| 5477 | for (sparent = schema, schema_depth = 0; |
| 5478 | sparent; |
| 5479 | sparent = (sparent->nodetype == LYS_AUGMENT ? ((struct lys_node_augment *)sparent)->target : sparent->parent)) { |
| 5480 | if (sparent->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_NOTIF | LYS_RPC)) { |
| 5481 | ++schema_depth; |
| 5482 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5483 | } |
Michal Vasko | fe98975 | 2016-09-08 08:47:26 +0200 | [diff] [blame] | 5484 | if (data_depth < schema_depth) { |
| 5485 | return -1; |
| 5486 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5487 | |
Michal Vasko | 956e854 | 2016-08-26 09:43:35 +0200 | [diff] [blame] | 5488 | /* find the corresponding data node */ |
| 5489 | for (i = 0; i < data_depth - schema_depth; ++i) { |
| 5490 | node = node->parent; |
| 5491 | } |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5492 | if (node->schema != schema) { |
| 5493 | return -1; |
| 5494 | } |
Michal Vasko | fe98975 | 2016-09-08 08:47:26 +0200 | [diff] [blame] | 5495 | } else { |
| 5496 | /* root context node */ |
| 5497 | while (node->parent) { |
| 5498 | node = node->parent; |
| 5499 | } |
| 5500 | while (node->prev->next) { |
| 5501 | node = node->prev; |
| 5502 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5503 | } |
| 5504 | |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5505 | *ctx_node = node; |
| 5506 | *ctx_node_type = node_type; |
| 5507 | return EXIT_SUCCESS; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5508 | } |
| 5509 | |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5510 | /** |
| 5511 | * @brief Temporarily unlink nodes as per YANG 1.1 RFC section 7.21.5 for when XPath evaluation. |
| 5512 | * The context nodes is adjusted if needed. |
| 5513 | * |
| 5514 | * @param[in] snode Schema node, whose children instances need to be unlinked. |
| 5515 | * @param[in,out] node Data siblings where to look for the children of \p snode. If it is unlinked, |
| 5516 | * it is moved to point to another sibling still in the original tree. |
| 5517 | * @param[in,out] ctx_node When context node, adjusted if needed. |
| 5518 | * @param[in] ctx_node_type Context node type, just for information to detect invalid situations. |
| 5519 | * @param[out] unlinked_nodes Unlinked siblings. Can be safely appended to \p node afterwards. |
| 5520 | * Ordering may change, but there will be no semantic change. |
| 5521 | * |
| 5522 | * @return EXIT_SUCCESS on success, -1 on error. |
| 5523 | */ |
| 5524 | static int |
| 5525 | resolve_when_unlink_nodes(struct lys_node *snode, struct lyd_node **node, struct lyd_node **ctx_node, |
| 5526 | enum lyxp_node_type ctx_node_type, struct lyd_node **unlinked_nodes) |
| 5527 | { |
| 5528 | struct lyd_node *next, *elem; |
| 5529 | |
| 5530 | switch (snode->nodetype) { |
| 5531 | case LYS_AUGMENT: |
| 5532 | case LYS_USES: |
| 5533 | case LYS_CHOICE: |
| 5534 | case LYS_CASE: |
| 5535 | LY_TREE_FOR(snode->child, snode) { |
| 5536 | if (resolve_when_unlink_nodes(snode, node, ctx_node, ctx_node_type, unlinked_nodes)) { |
| 5537 | return -1; |
| 5538 | } |
| 5539 | } |
| 5540 | break; |
| 5541 | case LYS_CONTAINER: |
| 5542 | case LYS_LIST: |
| 5543 | case LYS_LEAF: |
| 5544 | case LYS_LEAFLIST: |
| 5545 | case LYS_ANYXML: |
| 5546 | case LYS_ANYDATA: |
| 5547 | LY_TREE_FOR_SAFE(lyd_first_sibling(*node), next, elem) { |
| 5548 | if (elem->schema == snode) { |
| 5549 | |
| 5550 | if (elem == *ctx_node) { |
| 5551 | /* We are going to unlink our context node! This normally cannot happen, |
| 5552 | * but we use normal top-level data nodes for faking a document root node, |
| 5553 | * so if this is the context node, we just use the next top-level node. |
| 5554 | * Additionally, it can even happen that there are no top-level data nodes left, |
| 5555 | * all were unlinked, so in this case we pass NULL as the context node/data tree, |
| 5556 | * lyxp_eval() can handle this special situation. |
| 5557 | */ |
| 5558 | if (ctx_node_type == LYXP_NODE_ELEM) { |
| 5559 | LOGINT; |
| 5560 | return -1; |
| 5561 | } |
| 5562 | |
| 5563 | if (elem->prev == elem) { |
| 5564 | /* unlinking last top-level element, use an empty data tree */ |
| 5565 | *ctx_node = NULL; |
| 5566 | } else { |
| 5567 | /* in this case just use the previous/last top-level data node */ |
| 5568 | *ctx_node = elem->prev; |
| 5569 | } |
| 5570 | } else if (elem == *node) { |
| 5571 | /* We are going to unlink the currently processed node. This does not matter that |
| 5572 | * much, but we would lose access to the original data tree, so just move our |
| 5573 | * pointer somewhere still inside it. |
| 5574 | */ |
| 5575 | if ((*node)->prev != *node) { |
| 5576 | *node = (*node)->prev; |
| 5577 | } else { |
| 5578 | /* the processed node with sibings were all unlinked, oh well */ |
| 5579 | *node = NULL; |
| 5580 | } |
| 5581 | } |
| 5582 | |
| 5583 | /* temporarily unlink the node */ |
| 5584 | lyd_unlink(elem); |
| 5585 | if (*unlinked_nodes) { |
| 5586 | if (lyd_insert_after(*unlinked_nodes, elem)) { |
| 5587 | LOGINT; |
| 5588 | return -1; |
| 5589 | } |
| 5590 | } else { |
| 5591 | *unlinked_nodes = elem; |
| 5592 | } |
| 5593 | |
| 5594 | if (snode->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_ANYDATA)) { |
| 5595 | /* there can be only one instance */ |
| 5596 | break; |
| 5597 | } |
| 5598 | } |
| 5599 | } |
| 5600 | break; |
| 5601 | default: |
| 5602 | LOGINT; |
| 5603 | return -1; |
| 5604 | } |
| 5605 | |
| 5606 | return EXIT_SUCCESS; |
| 5607 | } |
| 5608 | |
| 5609 | /** |
| 5610 | * @brief Relink the unlinked nodes back. |
| 5611 | * |
| 5612 | * @param[in] node Data node to link the nodes back to. It can actually be the adjusted context node, |
| 5613 | * we simply need a sibling from the original data tree. |
| 5614 | * @param[in] unlinked_nodes Unlinked nodes to relink to \p node. |
| 5615 | * @param[in] ctx_node_type Context node type to distinguish between \p node being the parent |
| 5616 | * or the sibling of \p unlinked_nodes. |
| 5617 | * |
| 5618 | * @return EXIT_SUCCESS on success, -1 on error. |
| 5619 | */ |
| 5620 | static int |
| 5621 | resolve_when_relink_nodes(struct lyd_node *node, struct lyd_node *unlinked_nodes, enum lyxp_node_type ctx_node_type) |
| 5622 | { |
| 5623 | struct lyd_node *elem; |
| 5624 | |
| 5625 | LY_TREE_FOR_SAFE(unlinked_nodes, unlinked_nodes, elem) { |
| 5626 | if (ctx_node_type == LYXP_NODE_ELEM) { |
| 5627 | if (lyd_insert(node, elem)) { |
| 5628 | return -1; |
| 5629 | } |
| 5630 | } else { |
| 5631 | if (lyd_insert_after(node, elem)) { |
| 5632 | return -1; |
| 5633 | } |
| 5634 | } |
| 5635 | } |
| 5636 | |
| 5637 | return EXIT_SUCCESS; |
| 5638 | } |
| 5639 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5640 | int |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5641 | resolve_applies_must(const struct lyd_node *node) |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5642 | { |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5643 | int ret = 0; |
| 5644 | uint8_t must_size; |
| 5645 | struct lys_node *schema, *iter; |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5646 | |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5647 | assert(node); |
| 5648 | |
| 5649 | schema = node->schema; |
| 5650 | |
| 5651 | /* their own must */ |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5652 | switch (schema->nodetype) { |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5653 | case LYS_CONTAINER: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5654 | must_size = ((struct lys_node_container *)schema)->must_size; |
| 5655 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5656 | case LYS_LEAF: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5657 | must_size = ((struct lys_node_leaf *)schema)->must_size; |
| 5658 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5659 | case LYS_LEAFLIST: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5660 | must_size = ((struct lys_node_leaflist *)schema)->must_size; |
| 5661 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5662 | case LYS_LIST: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5663 | must_size = ((struct lys_node_list *)schema)->must_size; |
| 5664 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5665 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 5666 | case LYS_ANYDATA: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5667 | must_size = ((struct lys_node_anydata *)schema)->must_size; |
| 5668 | break; |
| 5669 | case LYS_NOTIF: |
| 5670 | must_size = ((struct lys_node_notif *)schema)->must_size; |
| 5671 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5672 | default: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5673 | must_size = 0; |
| 5674 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5675 | } |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5676 | |
| 5677 | if (must_size) { |
| 5678 | ++ret; |
| 5679 | } |
| 5680 | |
| 5681 | /* schema may be a direct data child of input/output with must (but it must be first, it needs to be evaluated only once) */ |
| 5682 | if (!node->prev->next) { |
| 5683 | for (iter = lys_parent(schema); iter && (iter->nodetype & (LYS_CHOICE | LYS_CASE | LYS_USES)); iter = lys_parent(iter)); |
| 5684 | if (iter && (iter->nodetype & (LYS_INPUT | LYS_OUTPUT))) { |
| 5685 | ret += 0x2; |
| 5686 | } |
| 5687 | } |
| 5688 | |
| 5689 | return ret; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5690 | } |
| 5691 | |
| 5692 | int |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5693 | 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] | 5694 | { |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5695 | const struct lys_node *parent; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5696 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5697 | assert(schema); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5698 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5699 | 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] | 5700 | return 1; |
| 5701 | } |
| 5702 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5703 | parent = schema; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5704 | goto check_augment; |
| 5705 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5706 | while (parent) { |
| 5707 | /* stop conditions */ |
| 5708 | if (!mode) { |
| 5709 | /* stop on node that can be instantiated in data tree */ |
| 5710 | if (!(parent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) { |
| 5711 | break; |
| 5712 | } |
| 5713 | } else { |
| 5714 | /* stop on the specified node */ |
| 5715 | if (parent == stop) { |
| 5716 | break; |
| 5717 | } |
| 5718 | } |
| 5719 | |
| 5720 | if (((const struct lys_node_uses *)parent)->when) { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5721 | return 1; |
| 5722 | } |
| 5723 | check_augment: |
| 5724 | |
| 5725 | if ((parent->parent && (parent->parent->nodetype == LYS_AUGMENT) && |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5726 | (((const struct lys_node_augment *)parent->parent)->when))) { |
Michal Vasko | e365556 | 2016-08-24 15:56:17 +0200 | [diff] [blame] | 5727 | return 1; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5728 | } |
| 5729 | parent = lys_parent(parent); |
| 5730 | } |
| 5731 | |
| 5732 | return 0; |
| 5733 | } |
| 5734 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5735 | /** |
| 5736 | * @brief Resolve (check) all when conditions relevant for \p node. |
| 5737 | * Logs directly. |
| 5738 | * |
| 5739 | * @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] | 5740 | * |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5741 | * @return |
| 5742 | * -1 - error, ly_errno is set |
| 5743 | * 0 - true "when" statement |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5744 | * 0, ly_vecode = LYVE_NOWHEN - false "when" statement |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5745 | * 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] | 5746 | */ |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5747 | int |
| 5748 | resolve_when(struct lyd_node *node, int *result) |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5749 | { |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5750 | struct lyd_node *ctx_node = NULL, *unlinked_nodes, *tmp_node; |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5751 | struct lys_node *sparent; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5752 | struct lyxp_set set; |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5753 | enum lyxp_node_type ctx_node_type; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5754 | int rc = 0; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5755 | |
| 5756 | assert(node); |
| 5757 | memset(&set, 0, sizeof set); |
| 5758 | |
| 5759 | 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] | 5760 | /* make the node dummy for the evaluation */ |
| 5761 | node->validity |= LYD_VAL_INUSE; |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5762 | 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] | 5763 | node->validity &= ~LYD_VAL_INUSE; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5764 | if (rc) { |
| 5765 | if (rc == 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5766 | 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] | 5767 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5768 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5769 | } |
| 5770 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5771 | /* set boolean result of the condition */ |
Michal Vasko | 944a564 | 2016-03-21 11:48:58 +0100 | [diff] [blame] | 5772 | lyxp_set_cast(&set, LYXP_SET_BOOLEAN, node, LYXP_WHEN); |
Michal Vasko | 8146d4c | 2016-05-09 15:50:29 +0200 | [diff] [blame] | 5773 | if (!set.val.bool) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5774 | 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] | 5775 | node->when_status |= LYD_WHEN_FALSE; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5776 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5777 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5778 | |
| 5779 | /* free xpath set content */ |
| 5780 | lyxp_set_cast(&set, LYXP_SET_EMPTY, node, 0); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5781 | } |
| 5782 | |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5783 | sparent = node->schema; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5784 | goto check_augment; |
| 5785 | |
| 5786 | /* check when in every schema node that affects node */ |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5787 | while (sparent && (sparent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) { |
| 5788 | if (((struct lys_node_uses *)sparent)->when) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5789 | if (!ctx_node) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5790 | rc = resolve_when_ctx_node(node, sparent, &ctx_node, &ctx_node_type); |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5791 | if (rc) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5792 | LOGINT; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5793 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5794 | } |
| 5795 | } |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5796 | |
| 5797 | unlinked_nodes = NULL; |
| 5798 | /* we do not want our node pointer to change */ |
| 5799 | tmp_node = node; |
| 5800 | rc = resolve_when_unlink_nodes(sparent, &tmp_node, &ctx_node, ctx_node_type, &unlinked_nodes); |
| 5801 | if (rc) { |
| 5802 | goto cleanup; |
| 5803 | } |
| 5804 | |
| 5805 | rc = lyxp_eval(((struct lys_node_uses *)sparent)->when->cond, ctx_node, ctx_node_type, &set, LYXP_WHEN); |
| 5806 | |
| 5807 | if (unlinked_nodes && ctx_node) { |
| 5808 | if (resolve_when_relink_nodes(ctx_node, unlinked_nodes, ctx_node_type)) { |
| 5809 | rc = -1; |
| 5810 | goto cleanup; |
| 5811 | } |
| 5812 | } |
| 5813 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5814 | if (rc) { |
| 5815 | if (rc == 1) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5816 | 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] | 5817 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5818 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5819 | } |
| 5820 | |
Michal Vasko | 944a564 | 2016-03-21 11:48:58 +0100 | [diff] [blame] | 5821 | lyxp_set_cast(&set, LYXP_SET_BOOLEAN, ctx_node, LYXP_WHEN); |
Michal Vasko | 8146d4c | 2016-05-09 15:50:29 +0200 | [diff] [blame] | 5822 | if (!set.val.bool) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5823 | 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] | 5824 | node->when_status |= LYD_WHEN_FALSE; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5825 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5826 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5827 | |
| 5828 | /* free xpath set content */ |
| 5829 | lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node, 0); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5830 | } |
| 5831 | |
| 5832 | check_augment: |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5833 | 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] | 5834 | if (!ctx_node) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5835 | 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] | 5836 | if (rc) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5837 | LOGINT; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5838 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5839 | } |
| 5840 | } |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5841 | |
| 5842 | unlinked_nodes = NULL; |
| 5843 | tmp_node = node; |
| 5844 | rc = resolve_when_unlink_nodes(sparent->parent, &tmp_node, &ctx_node, ctx_node_type, &unlinked_nodes); |
| 5845 | if (rc) { |
| 5846 | goto cleanup; |
| 5847 | } |
| 5848 | |
| 5849 | rc = lyxp_eval(((struct lys_node_augment *)sparent->parent)->when->cond, ctx_node, ctx_node_type, &set, LYXP_WHEN); |
| 5850 | |
| 5851 | /* reconnect nodes, if ctx_node is NULL then all the nodes were unlinked, but linked together, |
| 5852 | * so the tree did not actually change and there is nothing for us to do |
| 5853 | */ |
| 5854 | if (unlinked_nodes && ctx_node) { |
| 5855 | if (resolve_when_relink_nodes(ctx_node, unlinked_nodes, ctx_node_type)) { |
| 5856 | rc = -1; |
| 5857 | goto cleanup; |
| 5858 | } |
| 5859 | } |
| 5860 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5861 | if (rc) { |
| 5862 | if (rc == 1) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5863 | 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] | 5864 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5865 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5866 | } |
| 5867 | |
Michal Vasko | 944a564 | 2016-03-21 11:48:58 +0100 | [diff] [blame] | 5868 | lyxp_set_cast(&set, LYXP_SET_BOOLEAN, ctx_node, LYXP_WHEN); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5869 | |
Michal Vasko | 8146d4c | 2016-05-09 15:50:29 +0200 | [diff] [blame] | 5870 | if (!set.val.bool) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5871 | 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] | 5872 | node->when_status |= LYD_WHEN_FALSE; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5873 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5874 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5875 | |
| 5876 | /* free xpath set content */ |
| 5877 | lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node, 0); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5878 | } |
| 5879 | |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5880 | sparent = lys_parent(sparent); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5881 | } |
| 5882 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5883 | node->when_status |= LYD_WHEN_TRUE; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5884 | |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5885 | cleanup: |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5886 | /* free xpath set content */ |
| 5887 | lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node ? ctx_node : node, 0); |
| 5888 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5889 | if (result) { |
| 5890 | if (node->when_status & LYD_WHEN_TRUE) { |
| 5891 | *result = 1; |
| 5892 | } else { |
| 5893 | *result = 0; |
| 5894 | } |
| 5895 | } |
| 5896 | |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5897 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5898 | } |
| 5899 | |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 5900 | static int |
| 5901 | check_leafref_features(struct lys_type *type) |
| 5902 | { |
| 5903 | struct lys_node *iter; |
| 5904 | struct ly_set *src_parents, *trg_parents, *features; |
| 5905 | unsigned int i, j, size, x; |
| 5906 | int ret = EXIT_SUCCESS; |
| 5907 | |
| 5908 | assert(type->parent); |
| 5909 | |
| 5910 | src_parents = ly_set_new(); |
| 5911 | trg_parents = ly_set_new(); |
| 5912 | features = ly_set_new(); |
| 5913 | |
| 5914 | /* get parents chain of source (leafref) */ |
| 5915 | for (iter = (struct lys_node *)type->parent; iter; iter = iter->parent) { |
| 5916 | if (iter->nodetype & (LYS_INPUT | LYS_OUTPUT)) { |
| 5917 | continue; |
| 5918 | } |
| 5919 | ly_set_add(src_parents, iter, LY_SET_OPT_USEASLIST); |
| 5920 | } |
| 5921 | /* get parents chain of target */ |
| 5922 | for (iter = (struct lys_node *)type->info.lref.target; iter; iter = iter->parent) { |
| 5923 | if (iter->nodetype & (LYS_INPUT | LYS_OUTPUT)) { |
| 5924 | continue; |
| 5925 | } |
| 5926 | ly_set_add(trg_parents, iter, LY_SET_OPT_USEASLIST); |
| 5927 | } |
| 5928 | |
| 5929 | /* compare the features used in if-feature statements in the rest of both |
| 5930 | * chains of parents. The set of features used for target must be a subset |
| 5931 | * of features used for the leafref. This is not a perfect, we should compare |
| 5932 | * the truth tables but it could require too much resources, so we simplify that */ |
| 5933 | for (i = 0; i < src_parents->number; i++) { |
| 5934 | iter = src_parents->set.s[i]; /* shortcut */ |
| 5935 | if (!iter->iffeature_size) { |
| 5936 | continue; |
| 5937 | } |
| 5938 | for (j = 0; j < iter->iffeature_size; j++) { |
| 5939 | resolve_iffeature_getsizes(&iter->iffeature[j], NULL, &size); |
| 5940 | for (; size; size--) { |
| 5941 | if (!iter->iffeature[j].features[size - 1]) { |
| 5942 | /* not yet resolved feature, postpone this check */ |
| 5943 | ret = EXIT_FAILURE; |
| 5944 | goto cleanup; |
| 5945 | } |
| 5946 | ly_set_add(features, iter->iffeature[j].features[size - 1], 0); |
| 5947 | } |
| 5948 | } |
| 5949 | } |
| 5950 | x = features->number; |
| 5951 | for (i = 0; i < trg_parents->number; i++) { |
| 5952 | iter = trg_parents->set.s[i]; /* shortcut */ |
| 5953 | if (!iter->iffeature_size) { |
| 5954 | continue; |
| 5955 | } |
| 5956 | for (j = 0; j < iter->iffeature_size; j++) { |
| 5957 | resolve_iffeature_getsizes(&iter->iffeature[j], NULL, &size); |
| 5958 | for (; size; size--) { |
| 5959 | if (!iter->iffeature[j].features[size - 1]) { |
| 5960 | /* not yet resolved feature, postpone this check */ |
| 5961 | ret = EXIT_FAILURE; |
| 5962 | goto cleanup; |
| 5963 | } |
| 5964 | if ((unsigned int)ly_set_add(features, iter->iffeature[j].features[size - 1], 0) >= x) { |
| 5965 | /* the feature is not present in features set of target's parents chain */ |
| 5966 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, type->parent, "leafref", type->info.lref.path); |
| 5967 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, type->parent, |
| 5968 | "Leafref is not conditional based on \"%s\" feature as its target.", |
| 5969 | iter->iffeature[j].features[size - 1]->name); |
| 5970 | ret = -1; |
| 5971 | goto cleanup; |
| 5972 | } |
| 5973 | } |
| 5974 | } |
| 5975 | } |
| 5976 | |
| 5977 | cleanup: |
| 5978 | ly_set_free(features); |
| 5979 | ly_set_free(src_parents); |
| 5980 | ly_set_free(trg_parents); |
| 5981 | |
| 5982 | return ret; |
| 5983 | } |
| 5984 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5985 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5986 | * @brief Resolve a single unres schema item. Logs indirectly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5987 | * |
| 5988 | * @param[in] mod Main module. |
| 5989 | * @param[in] item Item to resolve. Type determined by \p type. |
| 5990 | * @param[in] type Type of the unresolved item. |
| 5991 | * @param[in] str_snode String, a schema node, or NULL. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5992 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5993 | * |
| 5994 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 5995 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5996 | static int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 5997 | 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] | 5998 | struct unres_schema *unres) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5999 | { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6000 | /* 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] | 6001 | int rc = -1, has_str = 0, tpdf_flag = 0, i, k; |
| 6002 | unsigned int j; |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 6003 | struct lys_node *node, *par_grp; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6004 | const char *expr; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6005 | |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6006 | struct ly_set *refs, *procs; |
| 6007 | struct lys_feature *ref, *feat; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6008 | struct lys_ident *ident; |
| 6009 | struct lys_type *stype; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6010 | struct lys_node_choice *choic; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6011 | struct lyxml_elem *yin; |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6012 | struct yang_type *yang; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6013 | struct unres_list_uniq *unique_info; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6014 | struct unres_iffeat_data *iff_data; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6015 | |
| 6016 | switch (type) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6017 | case UNRES_IDENT: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6018 | expr = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 6019 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6020 | ident = item; |
| 6021 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6022 | rc = resolve_base_ident(mod, ident, expr, "identity", NULL, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6023 | break; |
| 6024 | case UNRES_TYPE_IDENTREF: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6025 | expr = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 6026 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6027 | stype = item; |
| 6028 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6029 | rc = resolve_base_ident(mod, NULL, expr, "type", stype, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6030 | break; |
| 6031 | case UNRES_TYPE_LEAFREF: |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 6032 | node = str_snode; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6033 | stype = item; |
| 6034 | |
Radek Krejci | 2f12f85 | 2016-01-08 12:59:57 +0100 | [diff] [blame] | 6035 | /* HACK - when there is no parent, we are in top level typedef and in that |
| 6036 | * case, the path has to contain absolute path, so we let the resolve_path_arg_schema() |
| 6037 | * know it via tpdf_flag */ |
| 6038 | if (!node) { |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 6039 | tpdf_flag = 1; |
Radek Krejci | 2f12f85 | 2016-01-08 12:59:57 +0100 | [diff] [blame] | 6040 | node = (struct lys_node *)stype->parent; |
| 6041 | } |
| 6042 | |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 6043 | if (!lys_node_module(node)->implemented) { |
| 6044 | /* not implemented module, don't bother with resolving the leafref |
| 6045 | * if the module is set to be implemented, tha path will be resolved then */ |
| 6046 | rc = 0; |
| 6047 | break; |
| 6048 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6049 | rc = resolve_path_arg_schema(stype->info.lref.path, node, tpdf_flag, |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 6050 | (const struct lys_node **)&stype->info.lref.target); |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 6051 | if (!tpdf_flag && !rc) { |
| 6052 | assert(stype->info.lref.target); |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6053 | /* check if leafref and its target are under a common if-features */ |
| 6054 | rc = check_leafref_features(stype); |
| 6055 | if (rc) { |
| 6056 | break; |
| 6057 | } |
| 6058 | |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 6059 | /* store the backlink from leafref target */ |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 6060 | if (lys_leaf_add_leafref_target(stype->info.lref.target, (struct lys_node *)stype->parent)) { |
| 6061 | rc = -1; |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 6062 | } |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 6063 | } |
| 6064 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6065 | break; |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 6066 | case UNRES_TYPE_DER_TPDF: |
| 6067 | tpdf_flag = 1; |
| 6068 | /* no break */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6069 | case UNRES_TYPE_DER: |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6070 | /* parent */ |
| 6071 | node = str_snode; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6072 | stype = item; |
| 6073 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6074 | /* HACK type->der is temporarily unparsed type statement */ |
| 6075 | yin = (struct lyxml_elem *)stype->der; |
| 6076 | stype->der = NULL; |
| 6077 | |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6078 | if (yin->flags & LY_YANG_STRUCTURE_FLAG) { |
| 6079 | yang = (struct yang_type *)yin; |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 6080 | rc = yang_check_type(mod, node, yang, tpdf_flag, unres); |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6081 | |
| 6082 | if (rc) { |
Pavol Vican | 8bd72e4 | 2016-08-29 09:53:05 +0200 | [diff] [blame] | 6083 | /* may try again later */ |
| 6084 | stype->der = (struct lys_tpdf *)yang; |
Pavol Vican | d01d8ae | 2016-03-01 10:45:59 +0100 | [diff] [blame] | 6085 | } else { |
| 6086 | /* 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] | 6087 | lydict_remove(mod->ctx, yang->name); |
Pavol Vican | d01d8ae | 2016-03-01 10:45:59 +0100 | [diff] [blame] | 6088 | free(yang); |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6089 | } |
| 6090 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6091 | } else { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 6092 | rc = fill_yin_type(mod, node, yin, stype, tpdf_flag, unres); |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6093 | if (!rc) { |
| 6094 | /* we need to always be able to free this, it's safe only in this case */ |
| 6095 | lyxml_free(mod->ctx, yin); |
| 6096 | } else { |
| 6097 | /* may try again later, put all back how it was */ |
| 6098 | stype->der = (struct lys_tpdf *)yin; |
| 6099 | } |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6100 | } |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 6101 | if (rc == EXIT_SUCCESS) { |
Radek Krejci | 2c2fce8 | 2016-08-01 13:52:26 +0200 | [diff] [blame] | 6102 | /* it does not make sense to have leaf-list of empty type */ |
| 6103 | if (!tpdf_flag && node->nodetype == LYS_LEAFLIST && stype->base == LY_TYPE_EMPTY) { |
| 6104 | LOGWRN("The leaf-list \"%s\" is of \"empty\" type, which does not make sense.", node->name); |
| 6105 | } |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6106 | } else if (rc == EXIT_FAILURE && stype->base != LY_TYPE_ERR) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 6107 | /* forward reference - in case the type is in grouping, we have to make the grouping unusable |
| 6108 | * by uses statement until the type is resolved. We do that the same way as uses statements inside |
| 6109 | * grouping - the grouping's nacm member (not used un grouping) is used to increase the number of |
| 6110 | * 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] | 6111 | * 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] | 6112 | * of the type's base member. */ |
| 6113 | for (par_grp = node; par_grp && (par_grp->nodetype != LYS_GROUPING); par_grp = lys_parent(par_grp)); |
| 6114 | if (par_grp) { |
| 6115 | ((struct lys_node_grp *)par_grp)->nacm++; |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6116 | stype->base = LY_TYPE_ERR; |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 6117 | } |
Radek Krejci | 2c2fce8 | 2016-08-01 13:52:26 +0200 | [diff] [blame] | 6118 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6119 | break; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6120 | case UNRES_IFFEAT: |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6121 | iff_data = str_snode; |
| 6122 | 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] | 6123 | if (!rc) { |
| 6124 | /* success */ |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 6125 | if (iff_data->infeature) { |
| 6126 | /* store backlink into the target feature to allow reverse changes in case of changing feature status */ |
| 6127 | feat = *((struct lys_feature **)item); |
| 6128 | if (!feat->depfeatures) { |
| 6129 | feat->depfeatures = ly_set_new(); |
| 6130 | } |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 6131 | ly_set_add(feat->depfeatures, iff_data->node, LY_SET_OPT_USEASLIST); |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 6132 | } |
| 6133 | /* cleanup temporary data */ |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6134 | lydict_remove(mod->ctx, iff_data->fname); |
| 6135 | free(iff_data); |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6136 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6137 | break; |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6138 | case UNRES_FEATURE: |
| 6139 | feat = (struct lys_feature *)item; |
| 6140 | |
| 6141 | if (feat->iffeature_size) { |
| 6142 | refs = ly_set_new(); |
| 6143 | procs = ly_set_new(); |
| 6144 | ly_set_add(procs, feat, 0); |
| 6145 | |
| 6146 | while (procs->number) { |
| 6147 | ref = procs->set.g[procs->number - 1]; |
| 6148 | ly_set_rm_index(procs, procs->number - 1); |
| 6149 | |
| 6150 | for (i = 0; i < ref->iffeature_size; i++) { |
| 6151 | resolve_iffeature_getsizes(&ref->iffeature[i], NULL, &j); |
| 6152 | for (; j > 0 ; j--) { |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6153 | if (ref->iffeature[i].features[j - 1]) { |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6154 | if (ref->iffeature[i].features[j - 1] == feat) { |
| 6155 | LOGVAL(LYE_CIRC_FEATURES, LY_VLOG_NONE, NULL, feat->name); |
| 6156 | goto featurecheckdone; |
| 6157 | } |
| 6158 | |
| 6159 | if (ref->iffeature[i].features[j - 1]->iffeature_size) { |
| 6160 | k = refs->number; |
| 6161 | if (ly_set_add(refs, ref->iffeature[i].features[j - 1], 0) == k) { |
| 6162 | /* not yet seen feature, add it for processing */ |
| 6163 | ly_set_add(procs, ref->iffeature[i].features[j - 1], 0); |
| 6164 | } |
| 6165 | } |
| 6166 | } else { |
| 6167 | /* forward reference */ |
| 6168 | rc = EXIT_FAILURE; |
| 6169 | goto featurecheckdone; |
| 6170 | } |
| 6171 | } |
| 6172 | |
| 6173 | } |
| 6174 | } |
| 6175 | rc = EXIT_SUCCESS; |
| 6176 | |
| 6177 | featurecheckdone: |
| 6178 | ly_set_free(refs); |
| 6179 | ly_set_free(procs); |
| 6180 | } |
| 6181 | |
| 6182 | break; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6183 | case UNRES_USES: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6184 | rc = resolve_unres_schema_uses(item, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6185 | break; |
| 6186 | case UNRES_TYPE_DFLT: |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6187 | stype = item; |
| 6188 | |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 6189 | rc = check_default(stype, (const char **)str_snode, mod); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6190 | break; |
| 6191 | case UNRES_CHOICE_DFLT: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6192 | expr = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 6193 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6194 | choic = item; |
| 6195 | |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 6196 | if (!choic->dflt) { |
| 6197 | choic->dflt = resolve_choice_dflt(choic, expr); |
| 6198 | } |
Michal Vasko | 7955b36 | 2015-09-04 14:18:15 +0200 | [diff] [blame] | 6199 | if (choic->dflt) { |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 6200 | rc = lyp_check_mandatory_choice((struct lys_node *)choic); |
Michal Vasko | 7955b36 | 2015-09-04 14:18:15 +0200 | [diff] [blame] | 6201 | } else { |
| 6202 | rc = EXIT_FAILURE; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6203 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6204 | break; |
| 6205 | case UNRES_LIST_KEYS: |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 6206 | rc = resolve_list_keys(item, ((struct lys_node_list *)item)->keys_str); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6207 | break; |
| 6208 | case UNRES_LIST_UNIQ: |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6209 | unique_info = (struct unres_list_uniq *)item; |
| 6210 | 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] | 6211 | break; |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 6212 | case UNRES_AUGMENT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6213 | rc = resolve_augment(item, NULL); |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 6214 | break; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6215 | case UNRES_XPATH: |
| 6216 | node = (struct lys_node *)item; |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 6217 | rc = lys_check_xpath(node, 1); |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6218 | break; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6219 | default: |
| 6220 | LOGINT; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6221 | break; |
| 6222 | } |
| 6223 | |
Radek Krejci | 54081ce | 2016-08-12 15:21:47 +0200 | [diff] [blame] | 6224 | if (has_str && !rc) { |
| 6225 | /* the string is no more needed in case of success. |
| 6226 | * 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] | 6227 | lydict_remove(mod->ctx, str_snode); |
| 6228 | } |
| 6229 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6230 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6231 | } |
| 6232 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6233 | /* logs directly */ |
| 6234 | static void |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6235 | 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] | 6236 | { |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 6237 | struct lyxml_elem *xml; |
| 6238 | struct lyxml_attr *attr; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6239 | struct unres_iffeat_data *iff_data; |
Radek Krejci | 76e15e1 | 2016-06-22 11:02:24 +0200 | [diff] [blame] | 6240 | const char *type_name = NULL; |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 6241 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6242 | switch (type) { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6243 | case UNRES_IDENT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6244 | 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] | 6245 | break; |
| 6246 | case UNRES_TYPE_IDENTREF: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6247 | 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] | 6248 | break; |
| 6249 | case UNRES_TYPE_LEAFREF: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6250 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "leafref", |
| 6251 | ((struct lys_type *)item)->info.lref.path); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6252 | break; |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 6253 | case UNRES_TYPE_DER_TPDF: |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6254 | case UNRES_TYPE_DER: |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 6255 | xml = (struct lyxml_elem *)((struct lys_type *)item)->der; |
| 6256 | if (xml->flags & LY_YANG_STRUCTURE_FLAG) { |
| 6257 | type_name = ((struct yang_type *)xml)->name; |
| 6258 | } else { |
| 6259 | LY_TREE_FOR(xml->attr, attr) { |
| 6260 | if ((attr->type == LYXML_ATTR_STD) && !strcmp(attr->name, "name")) { |
| 6261 | type_name = attr->value; |
| 6262 | break; |
| 6263 | } |
| 6264 | } |
| 6265 | assert(attr); |
| 6266 | } |
| 6267 | 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] | 6268 | break; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6269 | case UNRES_IFFEAT: |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6270 | iff_data = str_node; |
| 6271 | 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] | 6272 | break; |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6273 | case UNRES_FEATURE: |
| 6274 | LOGVRB("There are unresolved if-features for \"%s\" feature circular dependency check, it will be attempted later", |
| 6275 | ((struct lys_feature *)item)->name); |
| 6276 | break; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6277 | case UNRES_USES: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6278 | 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] | 6279 | break; |
| 6280 | case UNRES_TYPE_DFLT: |
Radek Krejci | 2e2de83 | 2016-10-13 16:12:26 +0200 | [diff] [blame] | 6281 | if (str_node) { |
| 6282 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "type default", (char *)str_node); |
| 6283 | } /* else no default value in the type itself, but we are checking some restrictions against |
| 6284 | * possible default value of some base type. The failure is caused by not resolved base type, |
| 6285 | * so it was already reported */ |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6286 | break; |
| 6287 | case UNRES_CHOICE_DFLT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6288 | 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] | 6289 | break; |
| 6290 | case UNRES_LIST_KEYS: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6291 | 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] | 6292 | break; |
| 6293 | case UNRES_LIST_UNIQ: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6294 | 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] | 6295 | break; |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 6296 | case UNRES_AUGMENT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6297 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "augment target", |
| 6298 | ((struct lys_node_augment *)item)->target_name); |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 6299 | break; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6300 | case UNRES_XPATH: |
| 6301 | LOGVRB("Resolving %s \"%s\" with the context node \"%s\" failed, it will be attempted later.", "XPath", |
| 6302 | (char *)str_node, ((struct lys_node *)item)->name); |
| 6303 | break; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6304 | default: |
| 6305 | LOGINT; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6306 | break; |
| 6307 | } |
| 6308 | } |
| 6309 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6310 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6311 | * @brief Resolve every unres schema item in the structure. Logs directly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6312 | * |
| 6313 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6314 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6315 | * |
Michal Vasko | 92b8a38 | 2015-08-19 14:03:49 +0200 | [diff] [blame] | 6316 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6317 | */ |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6318 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 6319 | resolve_unres_schema(struct lys_module *mod, struct unres_schema *unres) |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6320 | { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6321 | uint32_t i, resolved = 0, unres_count, res_count; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6322 | int rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6323 | |
| 6324 | assert(unres); |
| 6325 | |
Michal Vasko | e873426 | 2016-09-29 14:12:06 +0200 | [diff] [blame] | 6326 | LOGVRB("Resolving \"%s\" unresolved schema nodes and their constraints...", mod->name); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6327 | ly_vlog_hide(1); |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6328 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6329 | /* uses */ |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6330 | do { |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6331 | unres_count = 0; |
| 6332 | res_count = 0; |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6333 | |
| 6334 | for (i = 0; i < unres->count; ++i) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6335 | /* UNRES_TYPE_LEAFREF must be resolved (for storing leafref target pointers); |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6336 | * if-features are resolved here to make sure that we will have all if-features for |
| 6337 | * later check of feature circular dependency */ |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6338 | if (unres->type[i] > UNRES_IDENT) { |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6339 | continue; |
| 6340 | } |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6341 | /* 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] | 6342 | * UNRES_CHOICE_DFLT and UNRES_IDENT */ |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6343 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6344 | ++unres_count; |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6345 | 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] | 6346 | if (!rc) { |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6347 | unres->type[i] = UNRES_RESOLVED; |
| 6348 | ++resolved; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6349 | ++res_count; |
Michal Vasko | 89e1532 | 2015-08-17 15:46:55 +0200 | [diff] [blame] | 6350 | } else if (rc == -1) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6351 | ly_vlog_hide(0); |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6352 | /* print the error */ |
| 6353 | 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] | 6354 | return -1; |
Radek Krejci | c2a180f | 2016-06-22 13:28:16 +0200 | [diff] [blame] | 6355 | } else { |
| 6356 | /* forward reference, erase ly_errno */ |
Radek Krejci | 00a0e71 | 2016-10-26 10:24:46 +0200 | [diff] [blame] | 6357 | ly_err_clean(1); |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6358 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6359 | } |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6360 | } while (res_count && (res_count < unres_count)); |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6361 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6362 | if (res_count < unres_count) { |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6363 | /* just print the errors */ |
| 6364 | ly_vlog_hide(0); |
| 6365 | |
| 6366 | for (i = 0; i < unres->count; ++i) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6367 | if (unres->type[i] > UNRES_IDENT) { |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6368 | continue; |
| 6369 | } |
| 6370 | resolve_unres_schema_item(mod, unres->item[i], unres->type[i], unres->str_snode[i], unres); |
| 6371 | } |
Michal Vasko | 92b8a38 | 2015-08-19 14:03:49 +0200 | [diff] [blame] | 6372 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6373 | } |
| 6374 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6375 | /* the rest */ |
| 6376 | for (i = 0; i < unres->count; ++i) { |
| 6377 | if (unres->type[i] == UNRES_RESOLVED) { |
| 6378 | continue; |
| 6379 | } |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 6380 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6381 | 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] | 6382 | if (rc == 0) { |
Pavol Vican | 88e16c9 | 2016-09-07 15:41:50 +0200 | [diff] [blame] | 6383 | if (unres->type[i] == UNRES_LIST_UNIQ) { |
| 6384 | /* free the allocated structure */ |
| 6385 | free(unres->item[i]); |
| 6386 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6387 | unres->type[i] = UNRES_RESOLVED; |
| 6388 | ++resolved; |
| 6389 | } else if (rc == -1) { |
| 6390 | ly_vlog_hide(0); |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6391 | /* print the error */ |
| 6392 | resolve_unres_schema_item(mod, unres->item[i], unres->type[i], unres->str_snode[i], unres); |
| 6393 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6394 | } |
| 6395 | } |
| 6396 | |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6397 | ly_vlog_hide(0); |
| 6398 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6399 | if (resolved < unres->count) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6400 | /* try to resolve the unresolved nodes again, it will not resolve anything, but it will print |
| 6401 | * all the validation errors |
| 6402 | */ |
| 6403 | for (i = 0; i < unres->count; ++i) { |
| 6404 | if (unres->type[i] == UNRES_RESOLVED) { |
| 6405 | continue; |
| 6406 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6407 | 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] | 6408 | } |
Michal Vasko | 92b8a38 | 2015-08-19 14:03:49 +0200 | [diff] [blame] | 6409 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6410 | } |
| 6411 | |
Michal Vasko | e873426 | 2016-09-29 14:12:06 +0200 | [diff] [blame] | 6412 | LOGVRB("All \"%s\" schema nodes and constraints resolved.", mod->name); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6413 | unres->count = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6414 | return EXIT_SUCCESS; |
| 6415 | } |
| 6416 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6417 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6418 | * @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] | 6419 | * |
| 6420 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6421 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6422 | * @param[in] item Item to resolve. Type determined by \p type. |
| 6423 | * @param[in] type Type of the unresolved item. |
| 6424 | * @param[in] str String argument. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6425 | * |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 6426 | * @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] | 6427 | */ |
| 6428 | int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6429 | unres_schema_add_str(struct lys_module *mod, struct unres_schema *unres, void *item, enum UNRES_ITEM type, |
| 6430 | const char *str) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6431 | { |
Radek Krejci | 54081ce | 2016-08-12 15:21:47 +0200 | [diff] [blame] | 6432 | int rc; |
| 6433 | const char *dictstr; |
| 6434 | |
| 6435 | dictstr = lydict_insert(mod->ctx, str, 0); |
| 6436 | rc = unres_schema_add_node(mod, unres, item, type, (struct lys_node *)dictstr); |
| 6437 | |
| 6438 | if (rc == -1) { |
| 6439 | lydict_remove(mod->ctx, dictstr); |
| 6440 | } |
| 6441 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6442 | } |
| 6443 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6444 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6445 | * @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] | 6446 | * |
| 6447 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6448 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6449 | * @param[in] item Item to resolve. Type determined by \p type. |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6450 | * @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] | 6451 | * @param[in] snode Schema node argument. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6452 | * |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 6453 | * @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] | 6454 | */ |
| 6455 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 6456 | 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] | 6457 | struct lys_node *snode) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6458 | { |
Michal Vasko | ef486d7 | 2016-09-27 12:10:44 +0200 | [diff] [blame] | 6459 | int rc, log_hidden; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6460 | struct lyxml_elem *yin; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6461 | |
Michal Vasko | 9bf425b | 2015-10-22 11:42:03 +0200 | [diff] [blame] | 6462 | assert(unres && item && ((type != UNRES_LEAFREF) && (type != UNRES_INSTID) && (type != UNRES_WHEN) |
| 6463 | && (type != UNRES_MUST))); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6464 | |
Michal Vasko | ef486d7 | 2016-09-27 12:10:44 +0200 | [diff] [blame] | 6465 | if (*ly_vlog_hide_location()) { |
| 6466 | log_hidden = 1; |
| 6467 | } else { |
| 6468 | log_hidden = 0; |
| 6469 | ly_vlog_hide(1); |
| 6470 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6471 | rc = resolve_unres_schema_item(mod, item, type, snode, unres); |
Michal Vasko | ef486d7 | 2016-09-27 12:10:44 +0200 | [diff] [blame] | 6472 | if (!log_hidden) { |
| 6473 | ly_vlog_hide(0); |
| 6474 | } |
| 6475 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6476 | if (rc != EXIT_FAILURE) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6477 | if (rc == -1 && ly_errno == LY_EVALID) { |
Radek Krejci | 2467a49 | 2016-10-24 15:16:59 +0200 | [diff] [blame] | 6478 | ly_err_repeat(); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6479 | } |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6480 | if (type == UNRES_LIST_UNIQ) { |
| 6481 | /* free the allocated structure */ |
| 6482 | free(item); |
Pavol Vican | 88e16c9 | 2016-09-07 15:41:50 +0200 | [diff] [blame] | 6483 | } else if (rc == -1 && type == UNRES_IFFEAT) { |
| 6484 | /* free the allocated resources */ |
| 6485 | free(*((char **)item)); |
| 6486 | } |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6487 | return rc; |
Radek Krejci | f347abc | 2016-06-22 10:18:47 +0200 | [diff] [blame] | 6488 | } else { |
| 6489 | /* erase info about validation errors */ |
Radek Krejci | 00a0e71 | 2016-10-26 10:24:46 +0200 | [diff] [blame] | 6490 | ly_err_clean(1); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6491 | } |
| 6492 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6493 | print_unres_schema_item_fail(item, type, snode); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6494 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6495 | /* 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] | 6496 | if (type == UNRES_TYPE_DER || type == UNRES_TYPE_DER_TPDF) { |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6497 | yin = (struct lyxml_elem *)((struct lys_type *)item)->der; |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6498 | if (!(yin->flags & LY_YANG_STRUCTURE_FLAG)) { |
| 6499 | lyxml_unlink_elem(mod->ctx, yin, 1); |
| 6500 | ((struct lys_type *)item)->der = (struct lys_tpdf *)yin; |
| 6501 | } |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6502 | } |
| 6503 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6504 | unres->count++; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 6505 | unres->item = ly_realloc(unres->item, unres->count*sizeof *unres->item); |
| 6506 | if (!unres->item) { |
| 6507 | LOGMEM; |
| 6508 | return -1; |
| 6509 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6510 | unres->item[unres->count-1] = item; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 6511 | unres->type = ly_realloc(unres->type, unres->count*sizeof *unres->type); |
| 6512 | if (!unres->type) { |
| 6513 | LOGMEM; |
| 6514 | return -1; |
| 6515 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6516 | unres->type[unres->count-1] = type; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 6517 | unres->str_snode = ly_realloc(unres->str_snode, unres->count*sizeof *unres->str_snode); |
| 6518 | if (!unres->str_snode) { |
| 6519 | LOGMEM; |
| 6520 | return -1; |
| 6521 | } |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6522 | unres->str_snode[unres->count-1] = snode; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6523 | unres->module = ly_realloc(unres->module, unres->count*sizeof *unres->module); |
| 6524 | if (!unres->module) { |
| 6525 | LOGMEM; |
| 6526 | return -1; |
| 6527 | } |
| 6528 | unres->module[unres->count-1] = mod; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6529 | |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 6530 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6531 | } |
| 6532 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6533 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6534 | * @brief Duplicate an unres schema item. Logs indirectly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6535 | * |
| 6536 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6537 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6538 | * @param[in] item Old item to be resolved. |
| 6539 | * @param[in] type Type of the old unresolved item. |
| 6540 | * @param[in] new_item New item to use in the duplicate. |
| 6541 | * |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 6542 | * @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] | 6543 | */ |
Michal Vasko | dad1940 | 2015-08-06 09:51:53 +0200 | [diff] [blame] | 6544 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 6545 | 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] | 6546 | { |
| 6547 | int i; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6548 | struct unres_list_uniq aux_uniq; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6549 | struct unres_iffeat_data *iff_data; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6550 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6551 | 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] | 6552 | |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6553 | /* hack for UNRES_LIST_UNIQ, which stores multiple items behind its item */ |
| 6554 | if (type == UNRES_LIST_UNIQ) { |
| 6555 | aux_uniq.list = item; |
| 6556 | aux_uniq.expr = ((struct unres_list_uniq *)new_item)->expr; |
| 6557 | item = &aux_uniq; |
| 6558 | } |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 6559 | i = unres_schema_find(unres, -1, item, type); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6560 | |
| 6561 | if (i == -1) { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6562 | if (type == UNRES_LIST_UNIQ) { |
| 6563 | free(new_item); |
| 6564 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 6565 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6566 | } |
| 6567 | |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6568 | if ((type == UNRES_TYPE_LEAFREF) || (type == UNRES_USES) || (type == UNRES_TYPE_DFLT) || |
Radek Krejci | b69f323 | 2016-09-16 17:41:07 +0200 | [diff] [blame] | 6569 | (type == UNRES_FEATURE) || (type == UNRES_LIST_UNIQ)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6570 | 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] | 6571 | LOGINT; |
| 6572 | return -1; |
| 6573 | } |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6574 | } else if (type == UNRES_IFFEAT) { |
| 6575 | /* duplicate unres_iffeature_data */ |
| 6576 | iff_data = malloc(sizeof *iff_data); |
| 6577 | iff_data->fname = lydict_insert(mod->ctx, ((struct unres_iffeat_data *)unres->str_snode[i])->fname, 0); |
| 6578 | iff_data->node = ((struct unres_iffeat_data *)unres->str_snode[i])->node; |
| 6579 | if (unres_schema_add_node(mod, unres, new_item, type, (struct lys_node *)iff_data) == -1) { |
| 6580 | LOGINT; |
| 6581 | return -1; |
| 6582 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6583 | } else { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6584 | 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] | 6585 | LOGINT; |
| 6586 | return -1; |
| 6587 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6588 | } |
Michal Vasko | dad1940 | 2015-08-06 09:51:53 +0200 | [diff] [blame] | 6589 | |
| 6590 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6591 | } |
| 6592 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6593 | /* does not log */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6594 | int |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 6595 | 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] | 6596 | { |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 6597 | int i; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6598 | struct unres_list_uniq *aux_uniq1, *aux_uniq2; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6599 | |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 6600 | if (start_on_backwards > 0) { |
| 6601 | i = start_on_backwards; |
| 6602 | } else { |
| 6603 | i = unres->count - 1; |
| 6604 | } |
| 6605 | for (; i > -1; i--) { |
| 6606 | if (unres->type[i] != type) { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6607 | continue; |
| 6608 | } |
| 6609 | if (type != UNRES_LIST_UNIQ) { |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 6610 | if (unres->item[i] == item) { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6611 | break; |
| 6612 | } |
| 6613 | } else { |
| 6614 | aux_uniq1 = (struct unres_list_uniq *)unres->item[i - 1]; |
| 6615 | aux_uniq2 = (struct unres_list_uniq *)item; |
| 6616 | 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] | 6617 | break; |
| 6618 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6619 | } |
| 6620 | } |
| 6621 | |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 6622 | return i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6623 | } |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6624 | |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 6625 | static void |
| 6626 | unres_schema_free_item(struct ly_ctx *ctx, struct unres_schema *unres, uint32_t i) |
| 6627 | { |
| 6628 | struct lyxml_elem *yin; |
| 6629 | struct yang_type *yang; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6630 | struct unres_iffeat_data *iff_data; |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 6631 | |
| 6632 | switch (unres->type[i]) { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 6633 | case UNRES_TYPE_DER_TPDF: |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 6634 | case UNRES_TYPE_DER: |
| 6635 | yin = (struct lyxml_elem *)((struct lys_type *)unres->item[i])->der; |
| 6636 | if (yin->flags & LY_YANG_STRUCTURE_FLAG) { |
| 6637 | yang =(struct yang_type *)yin; |
| 6638 | yang->type->base = yang->base; |
| 6639 | lydict_remove(ctx, yang->name); |
| 6640 | free(yang); |
| 6641 | } else { |
| 6642 | lyxml_free(ctx, yin); |
| 6643 | } |
| 6644 | break; |
Pavol Vican | 88e16c9 | 2016-09-07 15:41:50 +0200 | [diff] [blame] | 6645 | case UNRES_IFFEAT: |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6646 | iff_data = (struct unres_iffeat_data *)unres->str_snode[i]; |
| 6647 | lydict_remove(ctx, iff_data->fname); |
| 6648 | free(unres->str_snode[i]); |
Pavol Vican | 88e16c9 | 2016-09-07 15:41:50 +0200 | [diff] [blame] | 6649 | break; |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 6650 | case UNRES_IDENT: |
| 6651 | case UNRES_TYPE_IDENTREF: |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 6652 | case UNRES_CHOICE_DFLT: |
| 6653 | case UNRES_LIST_KEYS: |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 6654 | lydict_remove(ctx, (const char *)unres->str_snode[i]); |
| 6655 | break; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6656 | case UNRES_LIST_UNIQ: |
| 6657 | free(unres->item[i]); |
| 6658 | break; |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 6659 | default: |
| 6660 | break; |
| 6661 | } |
| 6662 | unres->type[i] = UNRES_RESOLVED; |
| 6663 | } |
| 6664 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6665 | void |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6666 | unres_schema_free(struct lys_module *module, struct unres_schema **unres) |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6667 | { |
| 6668 | uint32_t i; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6669 | unsigned int unresolved = 0; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6670 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6671 | if (!unres || !(*unres)) { |
| 6672 | return; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6673 | } |
| 6674 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6675 | assert(module || (*unres)->count == 0); |
| 6676 | |
| 6677 | for (i = 0; i < (*unres)->count; ++i) { |
| 6678 | if ((*unres)->module[i] != module) { |
| 6679 | if ((*unres)->type[i] != UNRES_RESOLVED) { |
| 6680 | unresolved++; |
| 6681 | } |
| 6682 | continue; |
| 6683 | } |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 6684 | |
| 6685 | /* free heap memory for the specific item */ |
| 6686 | unres_schema_free_item(module->ctx, *unres, i); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6687 | } |
| 6688 | |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 6689 | /* free it all */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6690 | if (!module || (!unresolved && !module->type)) { |
| 6691 | free((*unres)->item); |
| 6692 | free((*unres)->type); |
| 6693 | free((*unres)->str_snode); |
| 6694 | free((*unres)->module); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6695 | free((*unres)); |
| 6696 | (*unres) = NULL; |
| 6697 | } |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6698 | } |
| 6699 | |
Radek Krejci | 1899d6a | 2016-11-03 13:48:07 +0100 | [diff] [blame] | 6700 | int |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6701 | resolve_leafref(struct lyd_node_leaf_list *leaf, struct lys_type *type) |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 6702 | { |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 6703 | struct unres_data matches; |
| 6704 | uint32_t i; |
| 6705 | |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6706 | assert(type->base == LY_TYPE_LEAFREF); |
| 6707 | |
| 6708 | /* init */ |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 6709 | memset(&matches, 0, sizeof matches); |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 6710 | |
| 6711 | /* EXIT_FAILURE return keeps leaf->value.lefref NULL, handled later */ |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6712 | 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] | 6713 | return -1; |
| 6714 | } |
| 6715 | |
| 6716 | /* check that value matches */ |
| 6717 | for (i = 0; i < matches.count; ++i) { |
Radek Krejci | 1899d6a | 2016-11-03 13:48:07 +0100 | [diff] [blame] | 6718 | /* not that the value is already in canonical form since the parsers does the conversion, |
| 6719 | * so we can simply compare just the values */ |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 6720 | if (ly_strequal(leaf->value_str, ((struct lyd_node_leaf_list *)matches.node[i])->value_str, 1)) { |
Radek Krejci | 1899d6a | 2016-11-03 13:48:07 +0100 | [diff] [blame] | 6721 | /* we have the match */ |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 6722 | leaf->value.leafref = matches.node[i]; |
| 6723 | break; |
| 6724 | } |
| 6725 | } |
| 6726 | |
| 6727 | free(matches.node); |
| 6728 | |
| 6729 | if (!leaf->value.leafref) { |
| 6730 | /* reference not found */ |
| 6731 | if (type->info.lref.req > -1) { |
| 6732 | LOGVAL(LYE_NOLEAFREF, LY_VLOG_LYD, leaf, type->info.lref.path, leaf->value_str); |
| 6733 | return EXIT_FAILURE; |
| 6734 | } else { |
| 6735 | LOGVRB("There is no leafref with the value \"%s\", but it is not required.", leaf->value_str); |
| 6736 | } |
| 6737 | } |
| 6738 | |
| 6739 | return EXIT_SUCCESS; |
| 6740 | } |
| 6741 | |
Radek Krejci | 9ad23f4 | 2016-10-31 15:46:52 +0100 | [diff] [blame] | 6742 | API const struct lys_type * |
Radek Krejci | 1899d6a | 2016-11-03 13:48:07 +0100 | [diff] [blame] | 6743 | lyd_leaf_type(struct lyd_node_leaf_list *leaf, int resolve) |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6744 | { |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6745 | if (!leaf || !(leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Radek Krejci | 9ad23f4 | 2016-10-31 15:46:52 +0100 | [diff] [blame] | 6746 | return NULL; |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6747 | } |
Radek Krejci | 1899d6a | 2016-11-03 13:48:07 +0100 | [diff] [blame] | 6748 | if (((struct lys_node_leaf *)leaf->schema)->type.base == LY_TYPE_BITS) { |
| 6749 | free(leaf->value.bit); |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6750 | } |
Radek Krejci | 1899d6a | 2016-11-03 13:48:07 +0100 | [diff] [blame] | 6751 | memset(&leaf->value, 0, sizeof leaf->value); |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6752 | |
Radek Krejci | 1899d6a | 2016-11-03 13:48:07 +0100 | [diff] [blame] | 6753 | /* resolve */ |
| 6754 | return lyp_parse_value(&((struct lys_node_leaf *)leaf->schema)->type, (const char **)&leaf->value_str, NULL, |
| 6755 | (struct lyd_node *)leaf, resolve ? leaf : NULL, 1, 0); |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6756 | } |
| 6757 | |
| 6758 | static int |
| 6759 | resolve_union(struct lyd_node_leaf_list *leaf, struct lys_type *type) |
| 6760 | { |
| 6761 | struct lys_type *datatype = NULL; |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6762 | |
| 6763 | assert(type->base == LY_TYPE_UNION); |
| 6764 | |
| 6765 | memset(&leaf->value, 0, sizeof leaf->value); |
Radek Krejci | 1899d6a | 2016-11-03 13:48:07 +0100 | [diff] [blame] | 6766 | datatype = lyp_parse_value(type, &leaf->value_str, NULL, (struct lyd_node *)leaf, leaf, 1, 0); |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6767 | if (!datatype) { |
| 6768 | /* failure */ |
| 6769 | LOGVAL(LYE_INVAL, LY_VLOG_LYD, leaf, (leaf->value_str ? leaf->value_str : ""), leaf->schema->name); |
| 6770 | return EXIT_FAILURE; |
| 6771 | } |
| 6772 | |
| 6773 | return EXIT_SUCCESS; |
| 6774 | } |
| 6775 | |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6776 | /** |
| 6777 | * @brief Resolve a single unres data item. Logs directly. |
| 6778 | * |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6779 | * @param[in] node Data node to resolve. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6780 | * @param[in] type Type of the unresolved item. |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6781 | * |
| 6782 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 6783 | */ |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 6784 | int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6785 | resolve_unres_data_item(struct lyd_node *node, enum UNRES_ITEM type) |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6786 | { |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 6787 | int rc; |
Michal Vasko | 83a6c46 | 2015-10-08 16:43:53 +0200 | [diff] [blame] | 6788 | struct lyd_node_leaf_list *leaf; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6789 | struct lys_node_leaf *sleaf; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6790 | |
Michal Vasko | 83a6c46 | 2015-10-08 16:43:53 +0200 | [diff] [blame] | 6791 | leaf = (struct lyd_node_leaf_list *)node; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6792 | sleaf = (struct lys_node_leaf *)leaf->schema; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6793 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6794 | switch (type) { |
| 6795 | case UNRES_LEAFREF: |
| 6796 | assert(sleaf->type.base == LY_TYPE_LEAFREF); |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6797 | return resolve_leafref(leaf, &sleaf->type); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6798 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6799 | case UNRES_INSTID: |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 6800 | assert(sleaf->type.base == LY_TYPE_INST); |
Radek Krejci | 00a0e71 | 2016-10-26 10:24:46 +0200 | [diff] [blame] | 6801 | ly_err_clean(1); |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6802 | leaf->value.instance = resolve_instid(node, leaf->value_str); |
Radek Krejci | 40f17b9 | 2016-02-03 14:30:43 +0100 | [diff] [blame] | 6803 | if (!leaf->value.instance) { |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6804 | if (ly_errno) { |
| 6805 | return -1; |
| 6806 | } else if (sleaf->type.info.inst.req > -1) { |
Michal Vasko | 6ac6828 | 2016-04-11 10:56:47 +0200 | [diff] [blame] | 6807 | LOGVAL(LYE_NOREQINS, LY_VLOG_LYD, leaf, leaf->value_str); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6808 | return EXIT_FAILURE; |
| 6809 | } else { |
Michal Vasko | 9925e28 | 2016-09-02 12:45:14 +0200 | [diff] [blame] | 6810 | 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] | 6811 | } |
| 6812 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6813 | break; |
| 6814 | |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 6815 | case UNRES_UNION: |
| 6816 | assert(sleaf->type.base == LY_TYPE_UNION); |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6817 | return resolve_union(leaf, &sleaf->type); |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 6818 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6819 | case UNRES_WHEN: |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 6820 | if ((rc = resolve_when(node, NULL))) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6821 | return rc; |
| 6822 | } |
| 6823 | break; |
| 6824 | |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 6825 | case UNRES_MUST: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6826 | if ((rc = resolve_must(node, 0))) { |
| 6827 | return rc; |
| 6828 | } |
| 6829 | break; |
| 6830 | |
| 6831 | case UNRES_MUST_INOUT: |
| 6832 | if ((rc = resolve_must(node, 1))) { |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 6833 | return rc; |
| 6834 | } |
| 6835 | break; |
| 6836 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6837 | default: |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6838 | LOGINT; |
| 6839 | return -1; |
| 6840 | } |
| 6841 | |
| 6842 | return EXIT_SUCCESS; |
| 6843 | } |
| 6844 | |
| 6845 | /** |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6846 | * @brief add data unres item |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6847 | * |
| 6848 | * @param[in] unres Unres data structure to use. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6849 | * @param[in] node Data node to use. |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6850 | * |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6851 | * @return 0 on success, -1 on error. |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6852 | */ |
| 6853 | int |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6854 | 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] | 6855 | { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6856 | assert(unres && node); |
Michal Vasko | c428084 | 2016-04-19 16:10:42 +0200 | [diff] [blame] | 6857 | assert((type == UNRES_LEAFREF) || (type == UNRES_INSTID) || (type == UNRES_WHEN) || (type == UNRES_MUST) |
Radek Krejci | bacc744 | 2016-10-27 13:39:56 +0200 | [diff] [blame] | 6858 | || (type == UNRES_MUST_INOUT) || (type == UNRES_UNION)); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6859 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6860 | unres->count++; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 6861 | unres->node = ly_realloc(unres->node, unres->count * sizeof *unres->node); |
| 6862 | if (!unres->node) { |
| 6863 | LOGMEM; |
| 6864 | return -1; |
| 6865 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6866 | unres->node[unres->count - 1] = node; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 6867 | unres->type = ly_realloc(unres->type, unres->count * sizeof *unres->type); |
| 6868 | if (!unres->type) { |
| 6869 | LOGMEM; |
| 6870 | return -1; |
| 6871 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6872 | unres->type[unres->count - 1] = type; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6873 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6874 | if (type == UNRES_WHEN) { |
| 6875 | /* remove previous result */ |
| 6876 | node->when_status = LYD_WHEN; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6877 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6878 | |
| 6879 | return EXIT_SUCCESS; |
| 6880 | } |
| 6881 | |
| 6882 | /** |
| 6883 | * @brief Resolve every unres data item in the structure. Logs directly. |
| 6884 | * |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 6885 | * If options includes LYD_OPT_TRUSTED, the data are considered trusted (when, must conditions are not expected, |
| 6886 | * unresolved leafrefs/instids are accepted). |
| 6887 | * |
| 6888 | * If options includes LYD_OPT_NOAUTODEL, the false resulting when condition on non-default nodes, the error is raised. |
| 6889 | * |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6890 | * @param[in] unres Unres data structure to use. |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 6891 | * @param[in,out] root Root node of the data tree, can be changed due to autodeletion. |
| 6892 | * @param[in] options Data options as described above. |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6893 | * |
| 6894 | * @return EXIT_SUCCESS on success, -1 on error. |
| 6895 | */ |
| 6896 | int |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 6897 | 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] | 6898 | { |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 6899 | 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] | 6900 | int rc, progress; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6901 | struct lyd_node *parent; |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 6902 | struct lyd_node_leaf_list *leaf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6903 | |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 6904 | assert(root); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6905 | assert(unres); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6906 | |
| 6907 | if (!unres->count) { |
| 6908 | return EXIT_SUCCESS; |
| 6909 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6910 | |
Michal Vasko | a0ffcab | 2016-05-02 14:52:08 +0200 | [diff] [blame] | 6911 | LOGVRB("Resolving unresolved data nodes and their constraints..."); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6912 | ly_vlog_hide(1); |
| 6913 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6914 | /* when-stmt first */ |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6915 | do { |
Radek Krejci | 00a0e71 | 2016-10-26 10:24:46 +0200 | [diff] [blame] | 6916 | ly_err_clean(1); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6917 | progress = 0; |
Michal Vasko | 6df9413 | 2016-09-22 11:08:09 +0200 | [diff] [blame] | 6918 | for (i = 0; i < unres->count; i++) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6919 | if (unres->type[i] != UNRES_WHEN) { |
| 6920 | continue; |
| 6921 | } |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 6922 | assert(!(options & LYD_OPT_TRUSTED)); |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 6923 | if (first) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6924 | /* count when-stmt nodes in unres list */ |
| 6925 | when_stmt++; |
| 6926 | } |
| 6927 | |
| 6928 | /* resolve when condition only when all parent when conditions are already resolved */ |
| 6929 | for (parent = unres->node[i]->parent; |
| 6930 | parent && LYD_WHEN_DONE(parent->when_status); |
| 6931 | parent = parent->parent) { |
| 6932 | if (!parent->parent && (parent->when_status & LYD_WHEN_FALSE)) { |
| 6933 | /* the parent node was already unlinked, do not resolve this node, |
| 6934 | * it will be removed anyway, so just mark it as resolved |
| 6935 | */ |
| 6936 | unres->node[i]->when_status |= LYD_WHEN_FALSE; |
| 6937 | unres->type[i] = UNRES_RESOLVED; |
| 6938 | resolved++; |
| 6939 | break; |
| 6940 | } |
| 6941 | } |
| 6942 | if (parent) { |
| 6943 | continue; |
| 6944 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6945 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6946 | rc = resolve_unres_data_item(unres->node[i], unres->type[i]); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6947 | if (!rc) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6948 | if (unres->node[i]->when_status & LYD_WHEN_FALSE) { |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 6949 | if ((options & LYD_OPT_NOAUTODEL) && !unres->node[i]->dflt) { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6950 | /* false when condition */ |
| 6951 | ly_vlog_hide(0); |
Radek Krejci | 2467a49 | 2016-10-24 15:16:59 +0200 | [diff] [blame] | 6952 | ly_err_repeat(); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6953 | return -1; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6954 | } /* follows else */ |
| 6955 | |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 6956 | /* only unlink now, the subtree can contain another nodes stored in the unres list */ |
| 6957 | /* if it has parent non-presence containers that would be empty, we should actually |
| 6958 | * remove the container |
| 6959 | */ |
Radek Krejci | 2537fd3 | 2016-09-07 16:22:41 +0200 | [diff] [blame] | 6960 | for (parent = unres->node[i]; |
| 6961 | parent->parent && parent->parent->schema->nodetype == LYS_CONTAINER; |
| 6962 | parent = parent->parent) { |
| 6963 | if (((struct lys_node_container *)parent->parent->schema)->presence) { |
| 6964 | /* presence container */ |
| 6965 | break; |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 6966 | } |
Radek Krejci | 2537fd3 | 2016-09-07 16:22:41 +0200 | [diff] [blame] | 6967 | if (parent->next || parent->prev != parent) { |
| 6968 | /* non empty (the child we are in and we are going to remove is not the only child) */ |
| 6969 | break; |
| 6970 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 6971 | } |
Radek Krejci | 2537fd3 | 2016-09-07 16:22:41 +0200 | [diff] [blame] | 6972 | unres->node[i] = parent; |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 6973 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6974 | /* auto-delete */ |
| 6975 | LOGVRB("auto-delete node \"%s\" due to when condition (%s)", ly_errpath(), |
| 6976 | ((struct lys_node_leaf *)unres->node[i]->schema)->when->cond); |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 6977 | if (*root && *root == unres->node[i]) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6978 | *root = (*root)->next; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6979 | } |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6980 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6981 | lyd_unlink(unres->node[i]); |
| 6982 | unres->type[i] = UNRES_DELETE; |
| 6983 | del_items++; |
Radek Krejci | 51fd816 | 2016-03-24 15:49:51 +0100 | [diff] [blame] | 6984 | |
| 6985 | /* update the rest of unres items */ |
| 6986 | for (j = 0; j < unres->count; j++) { |
Radek Krejci | 3db819b | 2016-03-24 16:29:48 +0100 | [diff] [blame] | 6987 | if (unres->type[j] == UNRES_RESOLVED || unres->type[j] == UNRES_DELETE) { |
Radek Krejci | 51fd816 | 2016-03-24 15:49:51 +0100 | [diff] [blame] | 6988 | continue; |
| 6989 | } |
| 6990 | |
| 6991 | /* test if the node is in subtree to be deleted */ |
| 6992 | for (parent = unres->node[j]; parent; parent = parent->parent) { |
| 6993 | if (parent == unres->node[i]) { |
| 6994 | /* yes, it is */ |
| 6995 | unres->type[j] = UNRES_RESOLVED; |
| 6996 | resolved++; |
| 6997 | break; |
| 6998 | } |
| 6999 | } |
| 7000 | } |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7001 | } else { |
| 7002 | unres->type[i] = UNRES_RESOLVED; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7003 | } |
Radek Krejci | 00a0e71 | 2016-10-26 10:24:46 +0200 | [diff] [blame] | 7004 | ly_err_clean(1); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7005 | resolved++; |
| 7006 | progress = 1; |
| 7007 | } else if (rc == -1) { |
| 7008 | ly_vlog_hide(0); |
Michal Vasko | 76e7340 | 2016-08-24 16:00:13 +0200 | [diff] [blame] | 7009 | /* print only this last error */ |
| 7010 | resolve_unres_data_item(unres->node[i], unres->type[i]); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7011 | return -1; |
Radek Krejci | 2467a49 | 2016-10-24 15:16:59 +0200 | [diff] [blame] | 7012 | } /* else forward reference */ |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7013 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7014 | first = 0; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7015 | } while (progress && resolved < when_stmt); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7016 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7017 | /* do we have some unresolved when-stmt? */ |
Radek Krejci | d940d73 | 2016-03-24 16:02:28 +0100 | [diff] [blame] | 7018 | if (when_stmt > resolved) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7019 | ly_vlog_hide(0); |
Radek Krejci | 2467a49 | 2016-10-24 15:16:59 +0200 | [diff] [blame] | 7020 | ly_err_repeat(); |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7021 | return -1; |
| 7022 | } |
| 7023 | |
| 7024 | for (i = 0; del_items && i < unres->count; i++) { |
| 7025 | /* we had some when-stmt resulted to false, so now we have to sanitize the unres list */ |
| 7026 | if (unres->type[i] != UNRES_DELETE) { |
| 7027 | continue; |
| 7028 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7029 | if (!unres->node[i]) { |
| 7030 | unres->type[i] = UNRES_RESOLVED; |
| 7031 | del_items--; |
| 7032 | continue; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7033 | } |
| 7034 | |
| 7035 | /* really remove the complete subtree */ |
| 7036 | lyd_free(unres->node[i]); |
| 7037 | unres->type[i] = UNRES_RESOLVED; |
| 7038 | del_items--; |
| 7039 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7040 | |
| 7041 | /* rest */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7042 | for (i = 0; i < unres->count; ++i) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7043 | if (unres->type[i] == UNRES_RESOLVED) { |
| 7044 | continue; |
| 7045 | } |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 7046 | 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] | 7047 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7048 | rc = resolve_unres_data_item(unres->node[i], unres->type[i]); |
Michal Vasko | 6df9413 | 2016-09-22 11:08:09 +0200 | [diff] [blame] | 7049 | if (rc == -1) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7050 | ly_vlog_hide(0); |
Michal Vasko | 96b846c | 2016-05-18 13:28:58 +0200 | [diff] [blame] | 7051 | /* print only this last error */ |
| 7052 | resolve_unres_data_item(unres->node[i], unres->type[i]); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7053 | return -1; |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 7054 | } 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] | 7055 | unres->type[i] = UNRES_RESOLVED; |
| 7056 | resolved++; |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 7057 | if (options & LYD_OPT_TRUSTED) { |
Michal Vasko | 6df9413 | 2016-09-22 11:08:09 +0200 | [diff] [blame] | 7058 | /* accept it in this case */ |
| 7059 | if (unres->type[i] == UNRES_LEAFREF) { |
| 7060 | LOGVRB("Leafref \"%s\" with value \"%s\" failed to be resolved.", |
| 7061 | ((struct lys_node_leaf *)unres->node[i]->schema)->type.info.lref.path, |
| 7062 | ((struct lyd_node_leaf_list *)unres->node[i])->value_str); |
| 7063 | } else { |
| 7064 | LOGVRB("Instance identifier \"%s\" failed to be resolved.", |
| 7065 | ((struct lyd_node_leaf_list *)unres->node[i])->value_str); |
| 7066 | } |
| 7067 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7068 | } |
| 7069 | } |
| 7070 | |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7071 | ly_vlog_hide(0); |
| 7072 | if (resolved < unres->count) { |
| 7073 | /* try to resolve the unresolved data again, it will not resolve anything, but it will print |
| 7074 | * all the validation errors |
| 7075 | */ |
| 7076 | for (i = 0; i < unres->count; ++i) { |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7077 | if (unres->type[i] == UNRES_UNION) { |
| 7078 | /* does not make sense to print specific errors for all |
| 7079 | * the data types, just print that the value is invalid */ |
| 7080 | leaf = (struct lyd_node_leaf_list *)unres->node[i]; |
| 7081 | LOGVAL(LYE_INVAL, LY_VLOG_LYD, unres->node[i], (leaf->value_str ? leaf->value_str : ""), |
| 7082 | leaf->schema->name); |
| 7083 | } else if (unres->type[i] != UNRES_RESOLVED) { |
| 7084 | resolve_unres_data_item(unres->node[i], unres->type[i]); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7085 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7086 | } |
| 7087 | return -1; |
| 7088 | } |
| 7089 | |
Michal Vasko | a0ffcab | 2016-05-02 14:52:08 +0200 | [diff] [blame] | 7090 | LOGVRB("All data nodes and constraints resolved."); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7091 | unres->count = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7092 | return EXIT_SUCCESS; |
| 7093 | } |