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) { |
Michal Vasko | 6ca5ca7 | 2016-11-28 09:21:51 +0100 | [diff] [blame] | 91 | str_dig -= trailing_zeros; |
| 92 | str_exp -= trailing_zeros; |
Radek Krejci | bf47a82 | 2016-11-04 10:06:08 +0100 | [diff] [blame] | 93 | ret = ret / dec_pow(trailing_zeros); |
| 94 | } |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 95 | |
| 96 | /* it's parsed, now adjust the number based on fraction-digits, if needed */ |
| 97 | if (str_dig < dig) { |
| 98 | if ((str_exp - 1) + (dig - str_dig) > 18) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 99 | return 1; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 100 | } |
| 101 | ret *= dec_pow(dig - str_dig); |
| 102 | } |
| 103 | if (str_dig > dig) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 104 | return 1; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | if (minus) { |
| 108 | ret *= -1; |
| 109 | } |
| 110 | *str_num = ptr; |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 111 | *num = ret; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 112 | |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 113 | return 0; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | /** |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 117 | * @brief Parse an identifier. |
| 118 | * |
| 119 | * ;; An identifier MUST NOT start with (('X'|'x') ('M'|'m') ('L'|'l')) |
| 120 | * identifier = (ALPHA / "_") |
| 121 | * *(ALPHA / DIGIT / "_" / "-" / ".") |
| 122 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 123 | * @param[in] id Identifier to use. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 124 | * |
| 125 | * @return Number of characters successfully parsed. |
| 126 | */ |
Michal Vasko | 249e6b5 | 2015-08-19 11:08:52 +0200 | [diff] [blame] | 127 | int |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 128 | parse_identifier(const char *id) |
| 129 | { |
| 130 | int parsed = 0; |
| 131 | |
Michal Vasko | 1ab90bc | 2016-03-15 10:40:22 +0100 | [diff] [blame] | 132 | assert(id); |
| 133 | |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 134 | if (!isalpha(id[0]) && (id[0] != '_')) { |
| 135 | return -parsed; |
| 136 | } |
| 137 | |
| 138 | ++parsed; |
| 139 | ++id; |
| 140 | |
| 141 | while (isalnum(id[0]) || (id[0] == '_') || (id[0] == '-') || (id[0] == '.')) { |
| 142 | ++parsed; |
| 143 | ++id; |
| 144 | } |
| 145 | |
| 146 | return parsed; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * @brief Parse a node-identifier. |
| 151 | * |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 152 | * node-identifier = [module-name ":"] identifier |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 153 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 154 | * @param[in] id Identifier to use. |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 155 | * @param[out] mod_name Points to the module name, NULL if there is not any. |
| 156 | * @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] | 157 | * @param[out] name Points to the node name. |
| 158 | * @param[out] nam_len Length of the node name. |
| 159 | * |
| 160 | * @return Number of characters successfully parsed, |
| 161 | * positive on success, negative on failure. |
| 162 | */ |
| 163 | static int |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 164 | 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] | 165 | { |
| 166 | int parsed = 0, ret; |
| 167 | |
| 168 | assert(id); |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 169 | if (mod_name) { |
| 170 | *mod_name = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 171 | } |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 172 | if (mod_name_len) { |
| 173 | *mod_name_len = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 174 | } |
| 175 | if (name) { |
| 176 | *name = NULL; |
| 177 | } |
| 178 | if (nam_len) { |
| 179 | *nam_len = 0; |
| 180 | } |
| 181 | |
| 182 | if ((ret = parse_identifier(id)) < 1) { |
| 183 | return ret; |
| 184 | } |
| 185 | |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 186 | if (mod_name) { |
| 187 | *mod_name = id; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 188 | } |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 189 | if (mod_name_len) { |
| 190 | *mod_name_len = ret; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | parsed += ret; |
| 194 | id += ret; |
| 195 | |
| 196 | /* there is prefix */ |
| 197 | if (id[0] == ':') { |
| 198 | ++parsed; |
| 199 | ++id; |
| 200 | |
| 201 | /* there isn't */ |
| 202 | } else { |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 203 | if (name && mod_name) { |
| 204 | *name = *mod_name; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 205 | } |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 206 | if (mod_name) { |
| 207 | *mod_name = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 208 | } |
| 209 | |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 210 | if (nam_len && mod_name_len) { |
| 211 | *nam_len = *mod_name_len; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 212 | } |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 213 | if (mod_name_len) { |
| 214 | *mod_name_len = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | return parsed; |
| 218 | } |
| 219 | |
| 220 | /* identifier (node name) */ |
| 221 | if ((ret = parse_identifier(id)) < 1) { |
| 222 | return -parsed+ret; |
| 223 | } |
| 224 | |
| 225 | if (name) { |
| 226 | *name = id; |
| 227 | } |
| 228 | if (nam_len) { |
| 229 | *nam_len = ret; |
| 230 | } |
| 231 | |
| 232 | return parsed+ret; |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * @brief Parse a path-predicate (leafref). |
| 237 | * |
| 238 | * path-predicate = "[" *WSP path-equality-expr *WSP "]" |
| 239 | * path-equality-expr = node-identifier *WSP "=" *WSP path-key-expr |
| 240 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 241 | * @param[in] id Identifier to use. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 242 | * @param[out] prefix Points to the prefix, NULL if there is not any. |
| 243 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 244 | * @param[out] name Points to the node name. |
| 245 | * @param[out] nam_len Length of the node name. |
| 246 | * @param[out] path_key_expr Points to the path-key-expr. |
| 247 | * @param[out] pke_len Length of the path-key-expr. |
| 248 | * @param[out] has_predicate Flag to mark whether there is another predicate following. |
| 249 | * |
| 250 | * @return Number of characters successfully parsed, |
| 251 | * positive on success, negative on failure. |
| 252 | */ |
| 253 | static int |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 254 | parse_path_predicate(const char *id, const char **prefix, int *pref_len, const char **name, int *nam_len, |
| 255 | const char **path_key_expr, int *pke_len, int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 256 | { |
| 257 | const char *ptr; |
| 258 | int parsed = 0, ret; |
| 259 | |
| 260 | assert(id); |
| 261 | if (prefix) { |
| 262 | *prefix = NULL; |
| 263 | } |
| 264 | if (pref_len) { |
| 265 | *pref_len = 0; |
| 266 | } |
| 267 | if (name) { |
| 268 | *name = NULL; |
| 269 | } |
| 270 | if (nam_len) { |
| 271 | *nam_len = 0; |
| 272 | } |
| 273 | if (path_key_expr) { |
| 274 | *path_key_expr = NULL; |
| 275 | } |
| 276 | if (pke_len) { |
| 277 | *pke_len = 0; |
| 278 | } |
| 279 | if (has_predicate) { |
| 280 | *has_predicate = 0; |
| 281 | } |
| 282 | |
| 283 | if (id[0] != '[') { |
| 284 | return -parsed; |
| 285 | } |
| 286 | |
| 287 | ++parsed; |
| 288 | ++id; |
| 289 | |
| 290 | while (isspace(id[0])) { |
| 291 | ++parsed; |
| 292 | ++id; |
| 293 | } |
| 294 | |
| 295 | if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len)) < 1) { |
| 296 | return -parsed+ret; |
| 297 | } |
| 298 | |
| 299 | parsed += ret; |
| 300 | id += ret; |
| 301 | |
| 302 | while (isspace(id[0])) { |
| 303 | ++parsed; |
| 304 | ++id; |
| 305 | } |
| 306 | |
| 307 | if (id[0] != '=') { |
| 308 | return -parsed; |
| 309 | } |
| 310 | |
| 311 | ++parsed; |
| 312 | ++id; |
| 313 | |
| 314 | while (isspace(id[0])) { |
| 315 | ++parsed; |
| 316 | ++id; |
| 317 | } |
| 318 | |
| 319 | if ((ptr = strchr(id, ']')) == NULL) { |
| 320 | return -parsed; |
| 321 | } |
| 322 | |
| 323 | --ptr; |
| 324 | while (isspace(ptr[0])) { |
| 325 | --ptr; |
| 326 | } |
| 327 | ++ptr; |
| 328 | |
| 329 | ret = ptr-id; |
| 330 | if (path_key_expr) { |
| 331 | *path_key_expr = id; |
| 332 | } |
| 333 | if (pke_len) { |
| 334 | *pke_len = ret; |
| 335 | } |
| 336 | |
| 337 | parsed += ret; |
| 338 | id += ret; |
| 339 | |
| 340 | while (isspace(id[0])) { |
| 341 | ++parsed; |
| 342 | ++id; |
| 343 | } |
| 344 | |
| 345 | assert(id[0] == ']'); |
| 346 | |
| 347 | if (id[1] == '[') { |
| 348 | *has_predicate = 1; |
| 349 | } |
| 350 | |
| 351 | return parsed+1; |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * @brief Parse a path-key-expr (leafref). First call parses "current()", all |
| 356 | * the ".." and the first node-identifier, other calls parse a single |
| 357 | * node-identifier each. |
| 358 | * |
| 359 | * path-key-expr = current-function-invocation *WSP "/" *WSP |
| 360 | * rel-path-keyexpr |
| 361 | * rel-path-keyexpr = 1*(".." *WSP "/" *WSP) |
| 362 | * *(node-identifier *WSP "/" *WSP) |
| 363 | * node-identifier |
| 364 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 365 | * @param[in] id Identifier to use. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 366 | * @param[out] prefix Points to the prefix, NULL if there is not any. |
| 367 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 368 | * @param[out] name Points to the node name. |
| 369 | * @param[out] nam_len Length of the node name. |
| 370 | * @param[out] parent_times Number of ".." in the path. Must be 0 on the first call, |
| 371 | * must not be changed between consecutive calls. |
| 372 | * @return Number of characters successfully parsed, |
| 373 | * positive on success, negative on failure. |
| 374 | */ |
| 375 | static int |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 376 | parse_path_key_expr(const char *id, const char **prefix, int *pref_len, const char **name, int *nam_len, |
| 377 | int *parent_times) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 378 | { |
| 379 | int parsed = 0, ret, par_times = 0; |
| 380 | |
| 381 | assert(id); |
| 382 | assert(parent_times); |
| 383 | if (prefix) { |
| 384 | *prefix = NULL; |
| 385 | } |
| 386 | if (pref_len) { |
| 387 | *pref_len = 0; |
| 388 | } |
| 389 | if (name) { |
| 390 | *name = NULL; |
| 391 | } |
| 392 | if (nam_len) { |
| 393 | *nam_len = 0; |
| 394 | } |
| 395 | |
| 396 | if (!*parent_times) { |
| 397 | /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */ |
| 398 | if (strncmp(id, "current()", 9)) { |
| 399 | return -parsed; |
| 400 | } |
| 401 | |
| 402 | parsed += 9; |
| 403 | id += 9; |
| 404 | |
| 405 | while (isspace(id[0])) { |
| 406 | ++parsed; |
| 407 | ++id; |
| 408 | } |
| 409 | |
| 410 | if (id[0] != '/') { |
| 411 | return -parsed; |
| 412 | } |
| 413 | |
| 414 | ++parsed; |
| 415 | ++id; |
| 416 | |
| 417 | while (isspace(id[0])) { |
| 418 | ++parsed; |
| 419 | ++id; |
| 420 | } |
| 421 | |
| 422 | /* rel-path-keyexpr */ |
| 423 | if (strncmp(id, "..", 2)) { |
| 424 | return -parsed; |
| 425 | } |
| 426 | ++par_times; |
| 427 | |
| 428 | parsed += 2; |
| 429 | id += 2; |
| 430 | |
| 431 | while (isspace(id[0])) { |
| 432 | ++parsed; |
| 433 | ++id; |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | /* 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier |
| 438 | * |
| 439 | * first parent reference with whitespaces already parsed |
| 440 | */ |
| 441 | if (id[0] != '/') { |
| 442 | return -parsed; |
| 443 | } |
| 444 | |
| 445 | ++parsed; |
| 446 | ++id; |
| 447 | |
| 448 | while (isspace(id[0])) { |
| 449 | ++parsed; |
| 450 | ++id; |
| 451 | } |
| 452 | |
| 453 | while (!strncmp(id, "..", 2) && !*parent_times) { |
| 454 | ++par_times; |
| 455 | |
| 456 | parsed += 2; |
| 457 | id += 2; |
| 458 | |
| 459 | while (isspace(id[0])) { |
| 460 | ++parsed; |
| 461 | ++id; |
| 462 | } |
| 463 | |
| 464 | if (id[0] != '/') { |
| 465 | return -parsed; |
| 466 | } |
| 467 | |
| 468 | ++parsed; |
| 469 | ++id; |
| 470 | |
| 471 | while (isspace(id[0])) { |
| 472 | ++parsed; |
| 473 | ++id; |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | if (!*parent_times) { |
| 478 | *parent_times = par_times; |
| 479 | } |
| 480 | |
| 481 | /* all parent references must be parsed at this point */ |
| 482 | if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len)) < 1) { |
| 483 | return -parsed+ret; |
| 484 | } |
| 485 | |
| 486 | parsed += ret; |
| 487 | id += ret; |
| 488 | |
| 489 | return parsed; |
| 490 | } |
| 491 | |
| 492 | /** |
| 493 | * @brief Parse path-arg (leafref). |
| 494 | * |
| 495 | * path-arg = absolute-path / relative-path |
| 496 | * absolute-path = 1*("/" (node-identifier *path-predicate)) |
| 497 | * relative-path = 1*(".." "/") descendant-path |
| 498 | * |
Radek Krejci | f7ed4c3 | 2016-10-27 16:20:03 +0200 | [diff] [blame] | 499 | * @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] | 500 | * @param[in] id Identifier to use. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 501 | * @param[out] prefix Points to the prefix, NULL if there is not any. |
| 502 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 503 | * @param[out] name Points to the node name. |
| 504 | * @param[out] nam_len Length of the node name. |
| 505 | * @param[out] parent_times Number of ".." in the path. Must be 0 on the first call, |
| 506 | * must not be changed between consecutive calls. -1 if the |
| 507 | * path is relative. |
| 508 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 509 | * |
| 510 | * @return Number of characters successfully parsed, |
| 511 | * positive on success, negative on failure. |
| 512 | */ |
| 513 | static int |
Radek Krejci | f7ed4c3 | 2016-10-27 16:20:03 +0200 | [diff] [blame] | 514 | parse_path_arg(struct lys_module *mod, const char *id, const char **prefix, int *pref_len, |
| 515 | const char **name, int *nam_len, int *parent_times, int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 516 | { |
| 517 | int parsed = 0, ret, par_times = 0; |
| 518 | |
| 519 | assert(id); |
| 520 | assert(parent_times); |
| 521 | if (prefix) { |
| 522 | *prefix = NULL; |
| 523 | } |
| 524 | if (pref_len) { |
| 525 | *pref_len = 0; |
| 526 | } |
| 527 | if (name) { |
| 528 | *name = NULL; |
| 529 | } |
| 530 | if (nam_len) { |
| 531 | *nam_len = 0; |
| 532 | } |
| 533 | if (has_predicate) { |
| 534 | *has_predicate = 0; |
| 535 | } |
| 536 | |
| 537 | if (!*parent_times && !strncmp(id, "..", 2)) { |
| 538 | ++par_times; |
| 539 | |
| 540 | parsed += 2; |
| 541 | id += 2; |
| 542 | |
| 543 | while (!strncmp(id, "/..", 3)) { |
| 544 | ++par_times; |
| 545 | |
| 546 | parsed += 3; |
| 547 | id += 3; |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | if (!*parent_times) { |
| 552 | if (par_times) { |
| 553 | *parent_times = par_times; |
| 554 | } else { |
| 555 | *parent_times = -1; |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | if (id[0] != '/') { |
| 560 | return -parsed; |
| 561 | } |
| 562 | |
| 563 | /* skip '/' */ |
| 564 | ++parsed; |
| 565 | ++id; |
| 566 | |
| 567 | /* node-identifier ([prefix:]identifier) */ |
| 568 | if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len)) < 1) { |
| 569 | return -parsed-ret; |
| 570 | } |
Radek Krejci | f7ed4c3 | 2016-10-27 16:20:03 +0200 | [diff] [blame] | 571 | if (!(*prefix)) { |
| 572 | /* actually we always need prefix even it is not specified */ |
| 573 | *prefix = lys_main_module(mod)->name; |
| 574 | *pref_len = strlen(*prefix); |
| 575 | } |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 576 | |
| 577 | parsed += ret; |
| 578 | id += ret; |
| 579 | |
| 580 | /* there is no predicate */ |
| 581 | if ((id[0] == '/') || !id[0]) { |
| 582 | return parsed; |
| 583 | } else if (id[0] != '[') { |
| 584 | return -parsed; |
| 585 | } |
| 586 | |
| 587 | if (has_predicate) { |
| 588 | *has_predicate = 1; |
| 589 | } |
| 590 | |
| 591 | return parsed; |
| 592 | } |
| 593 | |
| 594 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 595 | * @brief Parse instance-identifier in JSON data format. That means that prefixes |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 596 | * (which are mandatory for every node-identifier) are actually model names. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 597 | * |
| 598 | * instance-identifier = 1*("/" (node-identifier *predicate)) |
| 599 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 600 | * @param[in] id Identifier to use. |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 601 | * @param[out] model Points to the model name. |
| 602 | * @param[out] mod_len Length of the model name. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 603 | * @param[out] name Points to the node name. |
| 604 | * @param[out] nam_len Length of the node name. |
| 605 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 606 | * |
| 607 | * @return Number of characters successfully parsed, |
| 608 | * positive on success, negative on failure. |
| 609 | */ |
| 610 | static int |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 611 | parse_instance_identifier(const char *id, const char **model, int *mod_len, const char **name, int *nam_len, |
| 612 | int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 613 | { |
| 614 | int parsed = 0, ret; |
| 615 | |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 616 | if (has_predicate) { |
| 617 | *has_predicate = 0; |
| 618 | } |
| 619 | |
| 620 | if (id[0] != '/') { |
| 621 | return -parsed; |
| 622 | } |
| 623 | |
| 624 | ++parsed; |
| 625 | ++id; |
| 626 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 627 | if ((ret = parse_identifier(id)) < 1) { |
| 628 | return ret; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 629 | } |
| 630 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 631 | *model = id; |
| 632 | *mod_len = ret; |
| 633 | |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 634 | parsed += ret; |
| 635 | id += ret; |
| 636 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 637 | if (id[0] != ':') { |
| 638 | return -parsed; |
| 639 | } |
| 640 | |
| 641 | ++parsed; |
| 642 | ++id; |
| 643 | |
| 644 | if ((ret = parse_identifier(id)) < 1) { |
| 645 | return ret; |
| 646 | } |
| 647 | |
| 648 | *name = id; |
| 649 | *nam_len = ret; |
| 650 | |
| 651 | parsed += ret; |
| 652 | id += ret; |
| 653 | |
Radek Krejci | 4967cb6 | 2016-09-14 16:40:28 +0200 | [diff] [blame] | 654 | if (id[0] == '[' && has_predicate) { |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 655 | *has_predicate = 1; |
| 656 | } |
| 657 | |
| 658 | return parsed; |
| 659 | } |
| 660 | |
| 661 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 662 | * @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] | 663 | * (which are mandatory) are actually model names. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 664 | * |
| 665 | * predicate = "[" *WSP (predicate-expr / pos) *WSP "]" |
| 666 | * predicate-expr = (node-identifier / ".") *WSP "=" *WSP |
| 667 | * ((DQUOTE string DQUOTE) / |
| 668 | * (SQUOTE string SQUOTE)) |
| 669 | * pos = non-negative-integer-value |
| 670 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 671 | * @param[in] id Identifier to use. |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 672 | * @param[out] model Points to the model name. |
| 673 | * @param[out] mod_len Length of the model name. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 674 | * @param[out] name Points to the node name. Can be identifier (from node-identifier), "." or pos. |
| 675 | * @param[out] nam_len Length of the node name. |
| 676 | * @param[out] value Value the node-identifier must have (string from the grammar), |
| 677 | * NULL if there is not any. |
| 678 | * @param[out] val_len Length of the value, 0 if there is not any. |
| 679 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 680 | * |
| 681 | * @return Number of characters successfully parsed, |
| 682 | * positive on success, negative on failure. |
| 683 | */ |
| 684 | static int |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 685 | parse_predicate(const char *id, const char **model, int *mod_len, const char **name, int *nam_len, |
| 686 | const char **value, int *val_len, int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 687 | { |
| 688 | const char *ptr; |
| 689 | int parsed = 0, ret; |
| 690 | char quote; |
| 691 | |
| 692 | assert(id); |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 693 | if (model) { |
| 694 | *model = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 695 | } |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 696 | if (mod_len) { |
| 697 | *mod_len = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 698 | } |
| 699 | if (name) { |
| 700 | *name = NULL; |
| 701 | } |
| 702 | if (nam_len) { |
| 703 | *nam_len = 0; |
| 704 | } |
| 705 | if (value) { |
| 706 | *value = NULL; |
| 707 | } |
| 708 | if (val_len) { |
| 709 | *val_len = 0; |
| 710 | } |
| 711 | if (has_predicate) { |
| 712 | *has_predicate = 0; |
| 713 | } |
| 714 | |
| 715 | if (id[0] != '[') { |
| 716 | return -parsed; |
| 717 | } |
| 718 | |
| 719 | ++parsed; |
| 720 | ++id; |
| 721 | |
| 722 | while (isspace(id[0])) { |
| 723 | ++parsed; |
| 724 | ++id; |
| 725 | } |
| 726 | |
| 727 | /* pos */ |
| 728 | if (isdigit(id[0])) { |
| 729 | if (name) { |
| 730 | *name = id; |
| 731 | } |
| 732 | |
| 733 | if (id[0] == '0') { |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 734 | return -parsed; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | while (isdigit(id[0])) { |
| 738 | ++parsed; |
| 739 | ++id; |
| 740 | } |
| 741 | |
| 742 | if (nam_len) { |
| 743 | *nam_len = id-(*name); |
| 744 | } |
| 745 | |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 746 | /* "." or node-identifier */ |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 747 | } else { |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 748 | if (id[0] == '.') { |
| 749 | if (name) { |
| 750 | *name = id; |
| 751 | } |
| 752 | if (nam_len) { |
| 753 | *nam_len = 1; |
| 754 | } |
| 755 | |
| 756 | ++parsed; |
| 757 | ++id; |
| 758 | |
| 759 | } else { |
| 760 | if ((ret = parse_node_identifier(id, model, mod_len, name, nam_len)) < 1) { |
| 761 | return -parsed+ret; |
| 762 | } else if (model && !*model) { |
| 763 | return -parsed; |
| 764 | } |
| 765 | |
| 766 | parsed += ret; |
| 767 | id += ret; |
| 768 | } |
| 769 | |
| 770 | while (isspace(id[0])) { |
| 771 | ++parsed; |
| 772 | ++id; |
| 773 | } |
| 774 | |
| 775 | if (id[0] != '=') { |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 776 | return -parsed; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 777 | } |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 778 | |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 779 | ++parsed; |
| 780 | ++id; |
| 781 | |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 782 | while (isspace(id[0])) { |
| 783 | ++parsed; |
| 784 | ++id; |
| 785 | } |
| 786 | |
| 787 | /* ((DQUOTE string DQUOTE) / (SQUOTE string SQUOTE)) */ |
| 788 | if ((id[0] == '\"') || (id[0] == '\'')) { |
| 789 | quote = id[0]; |
| 790 | |
| 791 | ++parsed; |
| 792 | ++id; |
| 793 | |
| 794 | if ((ptr = strchr(id, quote)) == NULL) { |
| 795 | return -parsed; |
| 796 | } |
| 797 | ret = ptr-id; |
| 798 | |
| 799 | if (value) { |
| 800 | *value = id; |
| 801 | } |
| 802 | if (val_len) { |
| 803 | *val_len = ret; |
| 804 | } |
| 805 | |
| 806 | parsed += ret+1; |
| 807 | id += ret+1; |
| 808 | } else { |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 809 | return -parsed; |
| 810 | } |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | while (isspace(id[0])) { |
| 814 | ++parsed; |
| 815 | ++id; |
| 816 | } |
| 817 | |
| 818 | if (id[0] != ']') { |
| 819 | return -parsed; |
| 820 | } |
| 821 | |
| 822 | ++parsed; |
| 823 | ++id; |
| 824 | |
| 825 | if ((id[0] == '[') && has_predicate) { |
| 826 | *has_predicate = 1; |
| 827 | } |
| 828 | |
| 829 | return parsed; |
| 830 | } |
| 831 | |
| 832 | /** |
| 833 | * @brief Parse schema-nodeid. |
| 834 | * |
| 835 | * schema-nodeid = absolute-schema-nodeid / |
| 836 | * descendant-schema-nodeid |
| 837 | * absolute-schema-nodeid = 1*("/" node-identifier) |
Michal Vasko | 4893535 | 2016-03-29 11:52:36 +0200 | [diff] [blame] | 838 | * descendant-schema-nodeid = ["." "/"] |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 839 | * node-identifier |
| 840 | * absolute-schema-nodeid |
| 841 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 842 | * @param[in] id Identifier to use. |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 843 | * @param[out] mod_name Points to the module name, NULL if there is not any. |
| 844 | * @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] | 845 | * @param[out] name Points to the node name. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 846 | * @param[out] nam_len Length of the node name. |
| 847 | * @param[out] is_relative Flag to mark whether the nodeid is absolute or descendant. Must be -1 |
| 848 | * on the first call, must not be changed between consecutive calls. |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 849 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. It cannot be |
| 850 | * based on the grammar, in those cases use NULL. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 851 | * |
| 852 | * @return Number of characters successfully parsed, |
| 853 | * positive on success, negative on failure. |
| 854 | */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 855 | int |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 856 | 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] | 857 | int *is_relative, int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 858 | { |
| 859 | int parsed = 0, ret; |
| 860 | |
| 861 | assert(id); |
| 862 | assert(is_relative); |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 863 | if (mod_name) { |
| 864 | *mod_name = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 865 | } |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 866 | if (mod_name_len) { |
| 867 | *mod_name_len = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 868 | } |
| 869 | if (name) { |
| 870 | *name = NULL; |
| 871 | } |
| 872 | if (nam_len) { |
| 873 | *nam_len = 0; |
| 874 | } |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 875 | if (has_predicate) { |
| 876 | *has_predicate = 0; |
| 877 | } |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 878 | |
| 879 | if (id[0] != '/') { |
| 880 | if (*is_relative != -1) { |
| 881 | return -parsed; |
| 882 | } else { |
| 883 | *is_relative = 1; |
| 884 | } |
Michal Vasko | 4893535 | 2016-03-29 11:52:36 +0200 | [diff] [blame] | 885 | if (!strncmp(id, "./", 2)) { |
| 886 | parsed += 2; |
| 887 | id += 2; |
| 888 | } |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 889 | } else { |
| 890 | if (*is_relative == -1) { |
| 891 | *is_relative = 0; |
| 892 | } |
| 893 | ++parsed; |
| 894 | ++id; |
| 895 | } |
| 896 | |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 897 | 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] | 898 | return -parsed+ret; |
| 899 | } |
| 900 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 901 | parsed += ret; |
| 902 | id += ret; |
| 903 | |
| 904 | if ((id[0] == '[') && has_predicate) { |
| 905 | *has_predicate = 1; |
| 906 | } |
| 907 | |
| 908 | return parsed; |
| 909 | } |
| 910 | |
| 911 | /** |
| 912 | * @brief Parse schema predicate (special format internally used). |
| 913 | * |
| 914 | * predicate = "[" *WSP predicate-expr *WSP "]" |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 915 | * predicate-expr = "." / identifier / key-with-value |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 916 | * key-with-value = identifier *WSP "=" *WSP |
| 917 | * ((DQUOTE string DQUOTE) / |
| 918 | * (SQUOTE string SQUOTE)) |
| 919 | * |
| 920 | * @param[in] id Identifier to use. |
| 921 | * @param[out] name Points to the list key name. |
| 922 | * @param[out] nam_len Length of \p name. |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 923 | * @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] | 924 | * @param[out] val_len Length of \p value. |
| 925 | * @param[out] has_predicate Flag to mark whether there is another predicate specified. |
| 926 | */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 927 | int |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 928 | 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] | 929 | int *has_predicate) |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 930 | { |
| 931 | const char *ptr; |
| 932 | int parsed = 0, ret; |
| 933 | char quote; |
| 934 | |
| 935 | assert(id); |
| 936 | if (name) { |
| 937 | *name = NULL; |
| 938 | } |
| 939 | if (nam_len) { |
| 940 | *nam_len = 0; |
| 941 | } |
| 942 | if (value) { |
| 943 | *value = NULL; |
| 944 | } |
| 945 | if (val_len) { |
| 946 | *val_len = 0; |
| 947 | } |
| 948 | if (has_predicate) { |
| 949 | *has_predicate = 0; |
| 950 | } |
| 951 | |
| 952 | if (id[0] != '[') { |
| 953 | return -parsed; |
| 954 | } |
| 955 | |
| 956 | ++parsed; |
| 957 | ++id; |
| 958 | |
| 959 | while (isspace(id[0])) { |
| 960 | ++parsed; |
| 961 | ++id; |
| 962 | } |
| 963 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 964 | /* identifier */ |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 965 | if (id[0] == '.') { |
| 966 | ret = 1; |
| 967 | } else if ((ret = parse_identifier(id)) < 1) { |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 968 | return -parsed + ret; |
| 969 | } |
| 970 | if (name) { |
| 971 | *name = id; |
| 972 | } |
| 973 | if (nam_len) { |
| 974 | *nam_len = ret; |
| 975 | } |
| 976 | |
| 977 | parsed += ret; |
| 978 | id += ret; |
| 979 | |
| 980 | while (isspace(id[0])) { |
| 981 | ++parsed; |
| 982 | ++id; |
| 983 | } |
| 984 | |
| 985 | /* there is value as well */ |
| 986 | if (id[0] == '=') { |
| 987 | ++parsed; |
| 988 | ++id; |
| 989 | |
| 990 | while (isspace(id[0])) { |
| 991 | ++parsed; |
| 992 | ++id; |
| 993 | } |
| 994 | |
| 995 | /* ((DQUOTE string DQUOTE) / (SQUOTE string SQUOTE)) */ |
| 996 | if ((id[0] == '\"') || (id[0] == '\'')) { |
| 997 | quote = id[0]; |
| 998 | |
| 999 | ++parsed; |
| 1000 | ++id; |
| 1001 | |
| 1002 | if ((ptr = strchr(id, quote)) == NULL) { |
| 1003 | return -parsed; |
| 1004 | } |
| 1005 | ret = ptr - id; |
| 1006 | |
| 1007 | if (value) { |
| 1008 | *value = id; |
| 1009 | } |
| 1010 | if (val_len) { |
| 1011 | *val_len = ret; |
| 1012 | } |
| 1013 | |
| 1014 | parsed += ret + 1; |
| 1015 | id += ret + 1; |
| 1016 | } else { |
| 1017 | return -parsed; |
| 1018 | } |
| 1019 | |
| 1020 | while (isspace(id[0])) { |
| 1021 | ++parsed; |
| 1022 | ++id; |
| 1023 | } |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1024 | } else if (value) { |
| 1025 | /* if value was expected, it's mandatory */ |
| 1026 | return -parsed; |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1027 | } |
| 1028 | |
| 1029 | if (id[0] != ']') { |
| 1030 | return -parsed; |
| 1031 | } |
| 1032 | |
| 1033 | ++parsed; |
| 1034 | ++id; |
| 1035 | |
| 1036 | if ((id[0] == '[') && has_predicate) { |
| 1037 | *has_predicate = 1; |
| 1038 | } |
| 1039 | |
| 1040 | return parsed; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 1041 | } |
| 1042 | |
| 1043 | /** |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1044 | * @brief Resolve (find) a feature definition. Logs directly. |
| 1045 | * |
| 1046 | * @param[in] feat_name Feature name to resolve. |
| 1047 | * @param[in] len Length of \p feat_name. |
| 1048 | * @param[in] node Node with the if-feature expression. |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1049 | * @param[out] feature Pointer to be set to point to the feature definition, if feature not found |
| 1050 | * (return code 1), the pointer is untouched. |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1051 | * |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1052 | * @return 0 on success, 1 on forward reference, -1 on error. |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1053 | */ |
| 1054 | static int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1055 | 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] | 1056 | { |
| 1057 | char *str; |
| 1058 | const char *mod_name, *name; |
| 1059 | int mod_name_len, nam_len, i, j; |
| 1060 | const struct lys_module *module; |
| 1061 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1062 | assert(feature); |
| 1063 | |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1064 | /* check prefix */ |
| 1065 | if ((i = parse_node_identifier(feat_name, &mod_name, &mod_name_len, &name, &nam_len)) < 1) { |
| 1066 | LOGVAL(LYE_INCHAR, LY_VLOG_NONE, NULL, feat_name[-i], &feat_name[-i]); |
| 1067 | return -1; |
| 1068 | } |
| 1069 | |
| 1070 | module = lys_get_import_module(lys_node_module(node), NULL, 0, mod_name, mod_name_len); |
| 1071 | if (!module) { |
| 1072 | /* identity refers unknown data model */ |
| 1073 | LOGVAL(LYE_INMOD_LEN, LY_VLOG_NONE, NULL, mod_name_len, mod_name); |
| 1074 | return -1; |
| 1075 | } |
| 1076 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1077 | if (module != node->module && module == lys_node_module(node)) { |
| 1078 | /* first, try to search directly in submodule where the feature was mentioned */ |
| 1079 | for (j = 0; j < node->module->features_size; j++) { |
| 1080 | if (!strncmp(name, node->module->features[j].name, nam_len) && !node->module->features[j].name[nam_len]) { |
| 1081 | /* check status */ |
| 1082 | 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] | 1083 | node->module->features[j].module, node->module->features[j].name, NULL)) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1084 | return -1; |
| 1085 | } |
| 1086 | *feature = &node->module->features[j]; |
| 1087 | return 0; |
| 1088 | } |
| 1089 | } |
| 1090 | } |
| 1091 | |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1092 | /* search in the identified module ... */ |
| 1093 | for (j = 0; j < module->features_size; j++) { |
Michal Vasko | 3def867 | 2016-07-01 11:43:09 +0200 | [diff] [blame] | 1094 | 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] | 1095 | /* check status */ |
| 1096 | 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] | 1097 | module->features[j].module, module->features[j].name, NULL)) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1098 | return -1; |
| 1099 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1100 | *feature = &module->features[j]; |
| 1101 | return 0; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1102 | } |
| 1103 | } |
| 1104 | /* ... and all its submodules */ |
| 1105 | for (i = 0; i < module->inc_size; i++) { |
| 1106 | if (!module->inc[i].submodule) { |
| 1107 | /* not yet resolved */ |
| 1108 | continue; |
| 1109 | } |
| 1110 | for (j = 0; j < module->inc[i].submodule->features_size; j++) { |
Michal Vasko | 3def867 | 2016-07-01 11:43:09 +0200 | [diff] [blame] | 1111 | if (!strncmp(name, module->inc[i].submodule->features[j].name, nam_len) |
| 1112 | && !module->inc[i].submodule->features[j].name[nam_len]) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1113 | /* check status */ |
| 1114 | if (lyp_check_status(node->flags, lys_node_module(node), node->name, |
| 1115 | module->inc[i].submodule->features[j].flags, |
| 1116 | module->inc[i].submodule->features[j].module, |
Pavol Vican | fdab9f9 | 2016-09-07 15:23:27 +0200 | [diff] [blame] | 1117 | module->inc[i].submodule->features[j].name, NULL)) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1118 | return -1; |
| 1119 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1120 | *feature = &module->inc[i].submodule->features[j]; |
| 1121 | return 0; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1122 | } |
| 1123 | } |
| 1124 | } |
| 1125 | |
| 1126 | /* not found */ |
| 1127 | str = strndup(feat_name, len); |
| 1128 | LOGVAL(LYE_INRESOLV, LY_VLOG_NONE, NULL, "feature", str); |
| 1129 | free(str); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1130 | return 1; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1131 | } |
| 1132 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1133 | /* |
| 1134 | * @return |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1135 | * - 1 if enabled |
| 1136 | * - 0 if disabled |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1137 | * - -1 if not usable by its if-feature expression |
| 1138 | */ |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1139 | static int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1140 | resolve_feature_value(const struct lys_feature *feat) |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1141 | { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1142 | int i; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1143 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1144 | for (i = 0; i < feat->iffeature_size; i++) { |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1145 | if (!resolve_iffeature(&feat->iffeature[i])) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1146 | return -1; |
| 1147 | } |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1148 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1149 | |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1150 | return feat->flags & LYS_FENABLED ? 1 : 0; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | static int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1154 | 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] | 1155 | { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1156 | uint8_t op; |
| 1157 | int rc, a, b; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1158 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1159 | op = iff_getop(expr->expr, *index_e); |
| 1160 | (*index_e)++; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1161 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1162 | switch (op) { |
| 1163 | case LYS_IFF_F: |
| 1164 | /* resolve feature */ |
| 1165 | return resolve_feature_value(expr->features[(*index_f)++]); |
| 1166 | case LYS_IFF_NOT: |
| 1167 | rc = resolve_iffeature_recursive(expr, index_e, index_f); |
| 1168 | if (rc == -1) { |
| 1169 | /* one of the referenced feature is hidden by its if-feature, |
| 1170 | * so this if-feature expression is always false */ |
| 1171 | return -1; |
| 1172 | } else { |
| 1173 | /* invert result */ |
| 1174 | return rc ? 0 : 1; |
| 1175 | } |
| 1176 | case LYS_IFF_AND: |
| 1177 | case LYS_IFF_OR: |
| 1178 | a = resolve_iffeature_recursive(expr, index_e, index_f); |
| 1179 | b = resolve_iffeature_recursive(expr, index_e, index_f); |
| 1180 | if (a == -1 || b == -1) { |
| 1181 | /* one of the referenced feature is hidden by its if-feature, |
| 1182 | * so this if-feature expression is always false */ |
| 1183 | return -1; |
| 1184 | } else if (op == LYS_IFF_AND) { |
| 1185 | return a && b; |
| 1186 | } else { /* LYS_IFF_OR */ |
| 1187 | return a || b; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1188 | } |
| 1189 | } |
| 1190 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1191 | return -1; |
| 1192 | } |
| 1193 | |
| 1194 | int |
| 1195 | resolve_iffeature(struct lys_iffeature *expr) |
| 1196 | { |
| 1197 | int rc = -1; |
| 1198 | int index_e = 0, index_f = 0; |
| 1199 | |
| 1200 | if (expr->expr) { |
| 1201 | rc = resolve_iffeature_recursive(expr, &index_e, &index_f); |
| 1202 | } |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1203 | return (rc == 1) ? 1 : 0; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1204 | } |
| 1205 | |
| 1206 | struct iff_stack { |
| 1207 | int size; |
| 1208 | int index; /* first empty item */ |
| 1209 | uint8_t *stack; |
| 1210 | }; |
| 1211 | |
| 1212 | static int |
| 1213 | iff_stack_push(struct iff_stack *stack, uint8_t value) |
| 1214 | { |
| 1215 | if (stack->index == stack->size) { |
| 1216 | stack->size += 4; |
| 1217 | stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack); |
| 1218 | if (!stack->stack) { |
| 1219 | LOGMEM; |
| 1220 | stack->size = 0; |
| 1221 | return EXIT_FAILURE; |
| 1222 | } |
| 1223 | } |
| 1224 | |
| 1225 | stack->stack[stack->index++] = value; |
| 1226 | return EXIT_SUCCESS; |
| 1227 | } |
| 1228 | |
| 1229 | static uint8_t |
| 1230 | iff_stack_pop(struct iff_stack *stack) |
| 1231 | { |
| 1232 | stack->index--; |
| 1233 | return stack->stack[stack->index]; |
| 1234 | } |
| 1235 | |
| 1236 | static void |
| 1237 | iff_stack_clean(struct iff_stack *stack) |
| 1238 | { |
| 1239 | stack->size = 0; |
| 1240 | free(stack->stack); |
| 1241 | } |
| 1242 | |
| 1243 | static void |
| 1244 | iff_setop(uint8_t *list, uint8_t op, int pos) |
| 1245 | { |
| 1246 | uint8_t *item; |
| 1247 | uint8_t mask = 3; |
| 1248 | |
| 1249 | assert(pos >= 0); |
| 1250 | assert(op <= 3); /* max 2 bits */ |
| 1251 | |
| 1252 | item = &list[pos / 4]; |
| 1253 | mask = mask << 2 * (pos % 4); |
| 1254 | *item = (*item) & ~mask; |
| 1255 | *item = (*item) | (op << 2 * (pos % 4)); |
| 1256 | } |
| 1257 | |
| 1258 | uint8_t |
| 1259 | iff_getop(uint8_t *list, int pos) |
| 1260 | { |
| 1261 | uint8_t *item; |
| 1262 | uint8_t mask = 3, result; |
| 1263 | |
| 1264 | assert(pos >= 0); |
| 1265 | |
| 1266 | item = &list[pos / 4]; |
| 1267 | result = (*item) & (mask << 2 * (pos % 4)); |
| 1268 | return result >> 2 * (pos % 4); |
| 1269 | } |
| 1270 | |
| 1271 | #define LYS_IFF_LP 0x04 /* ( */ |
| 1272 | #define LYS_IFF_RP 0x08 /* ) */ |
| 1273 | |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1274 | /* internal structure for passing data for UNRES_IFFEAT */ |
| 1275 | struct unres_iffeat_data { |
| 1276 | struct lys_node *node; |
| 1277 | const char *fname; |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 1278 | int infeature; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1279 | }; |
| 1280 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1281 | void |
| 1282 | resolve_iffeature_getsizes(struct lys_iffeature *iffeat, unsigned int *expr_size, unsigned int *feat_size) |
| 1283 | { |
| 1284 | unsigned int e = 0, f = 0, r = 0; |
| 1285 | uint8_t op; |
| 1286 | |
| 1287 | assert(iffeat); |
| 1288 | |
| 1289 | if (!iffeat->expr) { |
| 1290 | goto result; |
| 1291 | } |
| 1292 | |
| 1293 | do { |
| 1294 | op = iff_getop(iffeat->expr, e++); |
| 1295 | switch (op) { |
| 1296 | case LYS_IFF_NOT: |
| 1297 | if (!r) { |
| 1298 | r += 1; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1299 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1300 | break; |
| 1301 | case LYS_IFF_AND: |
| 1302 | case LYS_IFF_OR: |
| 1303 | if (!r) { |
| 1304 | r += 2; |
| 1305 | } else { |
| 1306 | r += 1; |
| 1307 | } |
| 1308 | break; |
| 1309 | case LYS_IFF_F: |
| 1310 | f++; |
| 1311 | if (r) { |
| 1312 | r--; |
| 1313 | } |
| 1314 | break; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1315 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1316 | } while(r); |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1317 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1318 | result: |
| 1319 | if (expr_size) { |
| 1320 | *expr_size = e; |
| 1321 | } |
| 1322 | if (feat_size) { |
| 1323 | *feat_size = f; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1324 | } |
| 1325 | } |
| 1326 | |
| 1327 | int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1328 | 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] | 1329 | int infeature, struct unres_schema *unres) |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1330 | { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1331 | const char *c = value; |
| 1332 | int r, rc = EXIT_FAILURE; |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1333 | int i, j, last_not, checkversion = 0; |
| 1334 | unsigned int f_size = 0, expr_size = 0, f_exp = 1; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1335 | uint8_t op; |
| 1336 | struct iff_stack stack = {0, 0, NULL}; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1337 | struct unres_iffeat_data *iff_data; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1338 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1339 | assert(c); |
| 1340 | |
| 1341 | if (isspace(c[0])) { |
| 1342 | LOGVAL(LYE_INCHAR, LY_VLOG_NONE, NULL, c[0], c); |
| 1343 | return EXIT_FAILURE; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1344 | } |
| 1345 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1346 | /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */ |
| 1347 | for (i = j = last_not = 0; c[i]; i++) { |
| 1348 | if (c[i] == '(') { |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1349 | checkversion = 1; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1350 | j++; |
| 1351 | continue; |
| 1352 | } else if (c[i] == ')') { |
| 1353 | j--; |
| 1354 | continue; |
| 1355 | } else if (isspace(c[i])) { |
| 1356 | continue; |
| 1357 | } |
| 1358 | |
| 1359 | if (!strncmp(&c[i], "not", r = 3) || !strncmp(&c[i], "and", r = 3) || !strncmp(&c[i], "or", r = 2)) { |
| 1360 | if (c[i + r] == '\0') { |
Radek Krejci | a98da3f | 2016-07-27 14:05:22 +0200 | [diff] [blame] | 1361 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1362 | return EXIT_FAILURE; |
| 1363 | } else if (!isspace(c[i + r])) { |
| 1364 | /* feature name starting with the not/and/or */ |
| 1365 | last_not = 0; |
| 1366 | f_size++; |
| 1367 | } else if (c[i] == 'n') { /* not operation */ |
| 1368 | if (last_not) { |
| 1369 | /* double not */ |
| 1370 | expr_size = expr_size - 2; |
| 1371 | last_not = 0; |
| 1372 | } else { |
| 1373 | last_not = 1; |
| 1374 | } |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1375 | } else { /* and, or */ |
| 1376 | f_exp++; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1377 | /* not a not operation */ |
| 1378 | last_not = 0; |
| 1379 | } |
| 1380 | i += r; |
| 1381 | } else { |
| 1382 | f_size++; |
| 1383 | last_not = 0; |
| 1384 | } |
| 1385 | expr_size++; |
| 1386 | |
| 1387 | while (!isspace(c[i])) { |
| 1388 | if (!c[i] || c[i] == ')') { |
| 1389 | i--; |
| 1390 | break; |
| 1391 | } |
| 1392 | i++; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1393 | } |
| 1394 | } |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1395 | if (j || f_exp != f_size) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1396 | /* not matching count of ( and ) */ |
| 1397 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
| 1398 | return EXIT_FAILURE; |
| 1399 | } |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1400 | |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1401 | if (checkversion || expr_size > 1) { |
| 1402 | /* check that we have 1.1 module */ |
| 1403 | if (node->module->version != 2) { |
| 1404 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
| 1405 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "YANG 1.1 if-feature expression found in 1.0 module."); |
| 1406 | return EXIT_FAILURE; |
| 1407 | } |
| 1408 | } |
| 1409 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1410 | /* allocate the memory */ |
| 1411 | 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] | 1412 | iffeat_expr->features = calloc(f_size, sizeof *iffeat_expr->features); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1413 | stack.size = expr_size; |
| 1414 | stack.stack = malloc(expr_size * sizeof *stack.stack); |
| 1415 | if (!stack.stack || !iffeat_expr->expr || !iffeat_expr->features) { |
| 1416 | LOGMEM; |
| 1417 | goto error; |
| 1418 | } |
| 1419 | f_size--; expr_size--; /* used as indexes from now */ |
| 1420 | |
| 1421 | for (i--; i >= 0; i--) { |
| 1422 | if (c[i] == ')') { |
| 1423 | /* push it on stack */ |
| 1424 | iff_stack_push(&stack, LYS_IFF_RP); |
| 1425 | continue; |
| 1426 | } else if (c[i] == '(') { |
| 1427 | /* pop from the stack into result all operators until ) */ |
| 1428 | while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) { |
| 1429 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1430 | } |
| 1431 | continue; |
| 1432 | } else if (isspace(c[i])) { |
| 1433 | continue; |
| 1434 | } |
| 1435 | |
| 1436 | /* end operator or operand -> find beginning and get what is it */ |
| 1437 | j = i + 1; |
| 1438 | while (i >= 0 && !isspace(c[i]) && c[i] != '(') { |
| 1439 | i--; |
| 1440 | } |
| 1441 | i++; /* get back by one step */ |
| 1442 | |
| 1443 | if (!strncmp(&c[i], "not ", 4)) { |
| 1444 | if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) { |
| 1445 | /* double not */ |
| 1446 | iff_stack_pop(&stack); |
| 1447 | } else { |
| 1448 | /* not has the highest priority, so do not pop from the stack |
| 1449 | * as in case of AND and OR */ |
| 1450 | iff_stack_push(&stack, LYS_IFF_NOT); |
| 1451 | } |
| 1452 | } else if (!strncmp(&c[i], "and ", 4)) { |
| 1453 | /* as for OR - pop from the stack all operators with the same or higher |
| 1454 | * priority and store them to the result, then push the AND to the stack */ |
| 1455 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) { |
| 1456 | op = iff_stack_pop(&stack); |
| 1457 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1458 | } |
| 1459 | iff_stack_push(&stack, LYS_IFF_AND); |
| 1460 | } else if (!strncmp(&c[i], "or ", 3)) { |
| 1461 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) { |
| 1462 | op = iff_stack_pop(&stack); |
| 1463 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1464 | } |
| 1465 | iff_stack_push(&stack, LYS_IFF_OR); |
| 1466 | } else { |
| 1467 | /* feature name, length is j - i */ |
| 1468 | |
| 1469 | /* add it to the result */ |
| 1470 | iff_setop(iffeat_expr->expr, LYS_IFF_F, expr_size--); |
| 1471 | |
| 1472 | /* now get the link to the feature definition. Since it can be |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1473 | * forward referenced, we have to keep the feature name in auxiliary |
| 1474 | * structure passed into unres */ |
| 1475 | iff_data = malloc(sizeof *iff_data); |
| 1476 | iff_data->node = node; |
| 1477 | iff_data->fname = lydict_insert(node->module->ctx, &c[i], j - i); |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 1478 | iff_data->infeature = infeature; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1479 | r = unres_schema_add_node(node->module, unres, &iffeat_expr->features[f_size], UNRES_IFFEAT, |
| 1480 | (struct lys_node *)iff_data); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1481 | f_size--; |
| 1482 | |
| 1483 | if (r == -1) { |
Pavol Vican | 4d08451 | 2016-09-29 16:38:12 +0200 | [diff] [blame] | 1484 | free(iff_data); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1485 | goto error; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1486 | } |
| 1487 | } |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1488 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1489 | while (stack.index) { |
| 1490 | op = iff_stack_pop(&stack); |
| 1491 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1492 | } |
| 1493 | |
| 1494 | if (++expr_size || ++f_size) { |
| 1495 | /* not all expected operators and operands found */ |
| 1496 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
| 1497 | rc = EXIT_FAILURE; |
| 1498 | } else { |
| 1499 | rc = EXIT_SUCCESS; |
| 1500 | } |
| 1501 | |
| 1502 | error: |
| 1503 | /* cleanup */ |
| 1504 | iff_stack_clean(&stack); |
| 1505 | |
| 1506 | return rc; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1507 | } |
| 1508 | |
| 1509 | /** |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1510 | * @brief Resolve (find) a data node based on a schema-nodeid. |
| 1511 | * |
| 1512 | * Used for resolving unique statements - so id is expected to be relative and local (without reference to a different |
| 1513 | * module). |
| 1514 | * |
| 1515 | */ |
| 1516 | struct lyd_node * |
| 1517 | resolve_data_descendant_schema_nodeid(const char *nodeid, struct lyd_node *start) |
| 1518 | { |
| 1519 | char *str, *token, *p; |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1520 | struct lyd_node *result = NULL, *iter; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1521 | const struct lys_node *schema = NULL; |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1522 | int shorthand = 0; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1523 | |
| 1524 | assert(nodeid && start); |
| 1525 | |
| 1526 | if (nodeid[0] == '/') { |
| 1527 | return NULL; |
| 1528 | } |
| 1529 | |
| 1530 | str = p = strdup(nodeid); |
| 1531 | if (!str) { |
| 1532 | LOGMEM; |
| 1533 | return NULL; |
| 1534 | } |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1535 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1536 | while (p) { |
| 1537 | token = p; |
| 1538 | p = strchr(p, '/'); |
| 1539 | if (p) { |
| 1540 | *p = '\0'; |
| 1541 | p++; |
| 1542 | } |
| 1543 | |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1544 | if (p) { |
| 1545 | /* inner node */ |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1546 | if (resolve_descendant_schema_nodeid(token, schema ? schema->child : start->schema, |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 1547 | LYS_CONTAINER | LYS_CHOICE | LYS_CASE | LYS_LEAF, 0, 0, &schema) |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1548 | || !schema) { |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1549 | result = NULL; |
| 1550 | break; |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1551 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1552 | |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1553 | if (schema->nodetype & (LYS_CHOICE | LYS_CASE)) { |
| 1554 | continue; |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 1555 | } else if (lys_parent(schema)->nodetype == LYS_CHOICE) { |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1556 | /* shorthand case */ |
| 1557 | if (!shorthand) { |
| 1558 | shorthand = 1; |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 1559 | schema = lys_parent(schema); |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1560 | continue; |
| 1561 | } else { |
| 1562 | shorthand = 0; |
| 1563 | if (schema->nodetype == LYS_LEAF) { |
| 1564 | /* should not be here, since we have leaf, which is not a shorthand nor final node */ |
| 1565 | result = NULL; |
| 1566 | break; |
| 1567 | } |
| 1568 | } |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1569 | } |
| 1570 | } else { |
| 1571 | /* final node */ |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 1572 | if (resolve_descendant_schema_nodeid(token, schema ? schema->child : start->schema, LYS_LEAF, |
| 1573 | shorthand ? 0 : 1, 0, &schema) |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1574 | || !schema) { |
| 1575 | result = NULL; |
| 1576 | break; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1577 | } |
| 1578 | } |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1579 | LY_TREE_FOR(result ? result->child : start, iter) { |
| 1580 | if (iter->schema == schema) { |
| 1581 | /* move in data tree according to returned schema */ |
| 1582 | result = iter; |
| 1583 | break; |
| 1584 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1585 | } |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1586 | if (!iter) { |
| 1587 | /* instance not found */ |
| 1588 | result = NULL; |
| 1589 | break; |
| 1590 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1591 | } |
| 1592 | free(str); |
| 1593 | |
| 1594 | return result; |
| 1595 | } |
| 1596 | |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1597 | /* |
| 1598 | * 0 - ok (done) |
| 1599 | * 1 - continue |
| 1600 | * 2 - break |
| 1601 | * -1 - error |
| 1602 | */ |
| 1603 | static int |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1604 | 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] | 1605 | 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] | 1606 | int implemented_mod, const struct lys_node **start) |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1607 | { |
| 1608 | const struct lys_module *prefix_mod; |
| 1609 | |
| 1610 | /* module check */ |
| 1611 | 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] | 1612 | if (prefix_mod && implemented_mod) { |
Radek Krejci | 2eee5c0 | 2016-12-06 19:18:05 +0100 | [diff] [blame] | 1613 | prefix_mod = lys_implemented_module(prefix_mod); |
Radek Krejci | 0fa54e9 | 2016-09-14 14:01:05 +0200 | [diff] [blame] | 1614 | } |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1615 | if (!prefix_mod) { |
| 1616 | return -1; |
| 1617 | } |
| 1618 | if (prefix_mod != lys_node_module(sibling)) { |
| 1619 | return 1; |
| 1620 | } |
| 1621 | |
| 1622 | /* check for shorthand cases - then 'start' does not change */ |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 1623 | 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] | 1624 | if (*shorthand != -1) { |
| 1625 | *shorthand = *shorthand ? 0 : 1; |
| 1626 | } |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1627 | } |
| 1628 | |
| 1629 | /* the result node? */ |
| 1630 | if (!id[0]) { |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1631 | if (*shorthand == 1) { |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1632 | return 1; |
| 1633 | } |
| 1634 | return 0; |
| 1635 | } |
| 1636 | |
Radek Krejci | 3a13016 | 2016-11-07 16:21:20 +0100 | [diff] [blame] | 1637 | if (!(*shorthand)) { |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1638 | /* move down the tree, if possible */ |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 1639 | if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1640 | return -1; |
| 1641 | } |
| 1642 | *start = sibling->child; |
| 1643 | } |
| 1644 | |
| 1645 | return 2; |
| 1646 | } |
| 1647 | |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 1648 | /* start - relative, module - absolute, -1 error, EXIT_SUCCESS ok (but ret can still be NULL), >0 unexpected char on ret - 1 |
| 1649 | * implement: 0 - do not change the implemented status of the affected modules, 1 - change implemented status of the affected modules |
| 1650 | */ |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1651 | int |
| 1652 | 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] | 1653 | int implement, const struct lys_node **ret) |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1654 | { |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 1655 | const char *name, *mod_name, *mod_name_prev, *id; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1656 | const struct lys_node *sibling; |
| 1657 | int r, nam_len, mod_name_len, is_relative = -1; |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1658 | int8_t shorthand = 0; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1659 | /* 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] | 1660 | const struct lys_module *start_mod, *aux_mod; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1661 | |
| 1662 | assert(nodeid && (start || module) && !(start && module) && ret); |
| 1663 | |
| 1664 | id = nodeid; |
| 1665 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1666 | 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] | 1667 | return ((id - nodeid) - r) + 1; |
| 1668 | } |
| 1669 | id += r; |
| 1670 | |
| 1671 | if ((is_relative && !start) || (!is_relative && !module)) { |
| 1672 | return -1; |
| 1673 | } |
| 1674 | |
| 1675 | /* descendant-schema-nodeid */ |
| 1676 | if (is_relative) { |
Michal Vasko | 4c06fd8 | 2016-02-17 13:43:38 +0100 | [diff] [blame] | 1677 | module = start_mod = start->module; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1678 | |
| 1679 | /* absolute-schema-nodeid */ |
| 1680 | } else { |
| 1681 | 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] | 1682 | if (start_mod != lys_main_module(module) && start_mod && !start_mod->implemented) { |
Radek Krejci | 0fa54e9 | 2016-09-14 14:01:05 +0200 | [diff] [blame] | 1683 | /* if the submodule augments the mainmodule (or in general a module augments |
| 1684 | * itself, we don't want to search for the implemented module but augments |
| 1685 | * the module anyway. But when augmenting another module, we need the implemented |
| 1686 | * revision of the module if any */ |
Radek Krejci | 2eee5c0 | 2016-12-06 19:18:05 +0100 | [diff] [blame] | 1687 | aux_mod = lys_implemented_module(start_mod); |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 1688 | if (!aux_mod->implemented && implement) { |
| 1689 | /* make the found module implemented */ |
| 1690 | if (lys_set_implemented(aux_mod)) { |
| 1691 | return -1; |
| 1692 | } |
| 1693 | } |
| 1694 | start_mod = aux_mod; |
| 1695 | implement++; |
Radek Krejci | 0fa54e9 | 2016-09-14 14:01:05 +0200 | [diff] [blame] | 1696 | } |
Michal Vasko | e290563 | 2016-02-11 15:42:24 +0100 | [diff] [blame] | 1697 | if (!start_mod) { |
| 1698 | return -1; |
| 1699 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1700 | start = start_mod->data; |
| 1701 | } |
| 1702 | |
| 1703 | while (1) { |
| 1704 | sibling = NULL; |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 1705 | mod_name_prev = mod_name; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1706 | while ((sibling = lys_getnext(sibling, lys_parent(start), start_mod, |
| 1707 | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT))) { |
| 1708 | /* name match */ |
Michal Vasko | 0a1aaa4 | 2016-04-19 09:48:25 +0200 | [diff] [blame] | 1709 | 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] | 1710 | 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] | 1711 | implement, &start); |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1712 | if (r == 0) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1713 | *ret = sibling; |
| 1714 | return EXIT_SUCCESS; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1715 | } else if (r == 1) { |
| 1716 | continue; |
| 1717 | } else if (r == 2) { |
| 1718 | break; |
| 1719 | } else { |
| 1720 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1721 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1722 | } |
| 1723 | } |
| 1724 | |
| 1725 | /* no match */ |
| 1726 | if (!sibling) { |
Michal Vasko | a426fef | 2016-03-07 10:47:31 +0100 | [diff] [blame] | 1727 | *ret = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1728 | return EXIT_SUCCESS; |
| 1729 | } |
| 1730 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1731 | 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] | 1732 | return ((id - nodeid) - r) + 1; |
| 1733 | } |
| 1734 | id += r; |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 1735 | |
| 1736 | if ((mod_name && mod_name_prev && strncmp(mod_name, mod_name_prev, mod_name_len + 1)) || |
| 1737 | (mod_name != mod_name_prev && (!mod_name || !mod_name_prev))) { |
| 1738 | /* we are getting into another module (augment) */ |
| 1739 | if (implement) { |
| 1740 | /* we have to check that also target modules are implemented, if not, we have to change it */ |
| 1741 | aux_mod = lys_get_import_module(module, NULL, 0, mod_name, mod_name_len); |
| 1742 | if (!aux_mod) { |
| 1743 | return -1; |
| 1744 | } |
| 1745 | if (!aux_mod->implemented) { |
Radek Krejci | 2eee5c0 | 2016-12-06 19:18:05 +0100 | [diff] [blame] | 1746 | aux_mod = lys_implemented_module(aux_mod); |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 1747 | if (!aux_mod->implemented) { |
| 1748 | /* make the found module implemented */ |
| 1749 | if (lys_set_implemented(aux_mod)) { |
| 1750 | return -1; |
| 1751 | } |
| 1752 | } |
| 1753 | } |
| 1754 | } else { |
| 1755 | /* we are not implementing the module itself, so the augments outside the module are ignored */ |
| 1756 | *ret = NULL; |
| 1757 | return EXIT_SUCCESS; |
| 1758 | } |
| 1759 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1760 | } |
| 1761 | |
| 1762 | /* cannot get here */ |
| 1763 | LOGINT; |
| 1764 | return -1; |
| 1765 | } |
| 1766 | |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 1767 | /* unique, refine, |
| 1768 | * >0 - unexpected char on position (ret - 1), |
| 1769 | * 0 - ok (but ret can still be NULL), |
| 1770 | * -1 - error, |
| 1771 | * -2 - violated no_innerlist */ |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1772 | int |
| 1773 | 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] | 1774 | int check_shorthand, int no_innerlist, const struct lys_node **ret) |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1775 | { |
| 1776 | const char *name, *mod_name, *id; |
| 1777 | const struct lys_node *sibling; |
| 1778 | int r, nam_len, mod_name_len, is_relative = -1; |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1779 | int8_t shorthand = check_shorthand ? 0 : -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1780 | /* 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] | 1781 | const struct lys_module *module; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1782 | |
| 1783 | assert(nodeid && start && ret); |
| 1784 | assert(!(ret_nodetype & (LYS_USES | LYS_AUGMENT)) && ((ret_nodetype == LYS_GROUPING) || !(ret_nodetype & LYS_GROUPING))); |
| 1785 | |
| 1786 | id = nodeid; |
| 1787 | module = start->module; |
| 1788 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1789 | 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] | 1790 | return ((id - nodeid) - r) + 1; |
| 1791 | } |
| 1792 | id += r; |
| 1793 | |
| 1794 | if (!is_relative) { |
| 1795 | return -1; |
| 1796 | } |
| 1797 | |
| 1798 | while (1) { |
| 1799 | sibling = NULL; |
| 1800 | while ((sibling = lys_getnext(sibling, lys_parent(start), module, |
| 1801 | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE))) { |
| 1802 | /* name match */ |
| 1803 | 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] | 1804 | 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] | 1805 | if (r == 0) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1806 | if (!(sibling->nodetype & ret_nodetype)) { |
| 1807 | /* wrong node type, too bad */ |
| 1808 | continue; |
| 1809 | } |
| 1810 | *ret = sibling; |
| 1811 | return EXIT_SUCCESS; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1812 | } else if (r == 1) { |
| 1813 | continue; |
| 1814 | } else if (r == 2) { |
| 1815 | break; |
| 1816 | } else { |
| 1817 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1818 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1819 | } |
| 1820 | } |
| 1821 | |
| 1822 | /* no match */ |
| 1823 | if (!sibling) { |
Michal Vasko | a426fef | 2016-03-07 10:47:31 +0100 | [diff] [blame] | 1824 | *ret = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1825 | return EXIT_SUCCESS; |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 1826 | } else if (no_innerlist && sibling->nodetype == LYS_LIST) { |
| 1827 | *ret = NULL; |
| 1828 | return -2; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1829 | } |
| 1830 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1831 | 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] | 1832 | return ((id - nodeid) - r) + 1; |
| 1833 | } |
| 1834 | id += r; |
| 1835 | } |
| 1836 | |
| 1837 | /* cannot get here */ |
| 1838 | LOGINT; |
| 1839 | return -1; |
| 1840 | } |
| 1841 | |
| 1842 | /* choice default */ |
| 1843 | int |
| 1844 | resolve_choice_default_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_node **ret) |
| 1845 | { |
| 1846 | /* cannot actually be a path */ |
| 1847 | if (strchr(nodeid, '/')) { |
| 1848 | return -1; |
| 1849 | } |
| 1850 | |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 1851 | 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] | 1852 | } |
| 1853 | |
| 1854 | /* uses, -1 error, EXIT_SUCCESS ok (but ret can still be NULL), >0 unexpected char on ret - 1 */ |
| 1855 | static int |
| 1856 | resolve_uses_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_node_grp **ret) |
| 1857 | { |
| 1858 | const struct lys_module *module; |
| 1859 | const char *mod_prefix, *name; |
| 1860 | int i, mod_prefix_len, nam_len; |
| 1861 | |
| 1862 | /* parse the identifier, it must be parsed on one call */ |
| 1863 | if (((i = parse_node_identifier(nodeid, &mod_prefix, &mod_prefix_len, &name, &nam_len)) < 1) || nodeid[i]) { |
| 1864 | return -i + 1; |
| 1865 | } |
| 1866 | |
| 1867 | module = lys_get_import_module(start->module, mod_prefix, mod_prefix_len, NULL, 0); |
| 1868 | if (!module) { |
| 1869 | return -1; |
| 1870 | } |
| 1871 | if (module != start->module) { |
| 1872 | start = module->data; |
| 1873 | } |
| 1874 | |
| 1875 | *ret = lys_find_grouping_up(name, (struct lys_node *)start); |
| 1876 | |
| 1877 | return EXIT_SUCCESS; |
| 1878 | } |
| 1879 | |
| 1880 | int |
| 1881 | resolve_absolute_schema_nodeid(const char *nodeid, const struct lys_module *module, int ret_nodetype, |
| 1882 | const struct lys_node **ret) |
| 1883 | { |
| 1884 | const char *name, *mod_name, *id; |
| 1885 | const struct lys_node *sibling, *start; |
| 1886 | int r, nam_len, mod_name_len, is_relative = -1; |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1887 | int8_t shorthand = 0; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1888 | const struct lys_module *abs_start_mod; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1889 | |
| 1890 | assert(nodeid && module && ret); |
| 1891 | assert(!(ret_nodetype & (LYS_USES | LYS_AUGMENT)) && ((ret_nodetype == LYS_GROUPING) || !(ret_nodetype & LYS_GROUPING))); |
| 1892 | |
| 1893 | id = nodeid; |
| 1894 | start = module->data; |
| 1895 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1896 | 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] | 1897 | return ((id - nodeid) - r) + 1; |
| 1898 | } |
| 1899 | id += r; |
| 1900 | |
| 1901 | if (is_relative) { |
| 1902 | return -1; |
| 1903 | } |
| 1904 | |
| 1905 | 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] | 1906 | if (!abs_start_mod) { |
| 1907 | return -1; |
| 1908 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1909 | |
| 1910 | while (1) { |
| 1911 | sibling = NULL; |
| 1912 | while ((sibling = lys_getnext(sibling, lys_parent(start), abs_start_mod, LYS_GETNEXT_WITHCHOICE |
| 1913 | | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT | LYS_GETNEXT_WITHGROUPING))) { |
| 1914 | /* name match */ |
Michal Vasko | 0a1aaa4 | 2016-04-19 09:48:25 +0200 | [diff] [blame] | 1915 | 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] | 1916 | 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] | 1917 | if (r == 0) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1918 | if (!(sibling->nodetype & ret_nodetype)) { |
| 1919 | /* wrong node type, too bad */ |
| 1920 | continue; |
| 1921 | } |
| 1922 | *ret = sibling; |
| 1923 | return EXIT_SUCCESS; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1924 | } else if (r == 1) { |
| 1925 | continue; |
| 1926 | } else if (r == 2) { |
| 1927 | break; |
| 1928 | } else { |
| 1929 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1930 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1931 | } |
| 1932 | } |
| 1933 | |
| 1934 | /* no match */ |
| 1935 | if (!sibling) { |
Michal Vasko | a426fef | 2016-03-07 10:47:31 +0100 | [diff] [blame] | 1936 | *ret = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1937 | return EXIT_SUCCESS; |
| 1938 | } |
| 1939 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1940 | 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] | 1941 | return ((id - nodeid) - r) + 1; |
| 1942 | } |
| 1943 | id += r; |
| 1944 | } |
| 1945 | |
| 1946 | /* cannot get here */ |
| 1947 | LOGINT; |
| 1948 | return -1; |
| 1949 | } |
| 1950 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1951 | static int |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1952 | 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] | 1953 | { |
| 1954 | const char *name; |
| 1955 | int nam_len, has_predicate, i; |
| 1956 | |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 1957 | if (((i = parse_schema_json_predicate(predicate, &name, &nam_len, NULL, NULL, &has_predicate)) < 1) |
| 1958 | || !strncmp(name, ".", nam_len)) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 1959 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-i], &predicate[-i]); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1960 | return -1; |
| 1961 | } |
| 1962 | |
| 1963 | predicate += i; |
| 1964 | *parsed += i; |
| 1965 | |
| 1966 | for (i = 0; i < list->keys_size; ++i) { |
| 1967 | if (!strncmp(list->keys[i]->name, name, nam_len) && !list->keys[i]->name[nam_len]) { |
| 1968 | break; |
| 1969 | } |
| 1970 | } |
| 1971 | |
| 1972 | if (i == list->keys_size) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 1973 | LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1974 | return -1; |
| 1975 | } |
| 1976 | |
| 1977 | /* more predicates? */ |
| 1978 | if (has_predicate) { |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1979 | return resolve_json_schema_list_predicate(predicate, list, parsed); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1980 | } |
| 1981 | |
| 1982 | return 0; |
| 1983 | } |
| 1984 | |
Michal Vasko | 8d26e5c | 2016-09-08 10:03:49 +0200 | [diff] [blame] | 1985 | /* cannot return LYS_GROUPING, LYS_AUGMENT, LYS_USES, logs directly */ |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1986 | const struct lys_node * |
Michal Vasko | 8d26e5c | 2016-09-08 10:03:49 +0200 | [diff] [blame] | 1987 | 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] | 1988 | { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 1989 | char *module_name = ly_buf(), *buf_backup = NULL, *str; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1990 | const char *name, *mod_name, *id; |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1991 | const struct lys_node *sibling; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1992 | 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] | 1993 | /* 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] | 1994 | const struct lys_module *prefix_mod, *module, *prev_mod; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1995 | |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1996 | assert(nodeid && (ctx || start)); |
| 1997 | if (!ctx) { |
| 1998 | ctx = start->module->ctx; |
| 1999 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2000 | |
| 2001 | id = nodeid; |
| 2002 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2003 | 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] | 2004 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2005 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2006 | } |
| 2007 | id += r; |
| 2008 | |
| 2009 | if (is_relative) { |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2010 | assert(start); |
| 2011 | start = start->child; |
| 2012 | if (!start) { |
| 2013 | /* no descendants, fail for sure */ |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2014 | str = strndup(nodeid, (name + nam_len) - nodeid); |
| 2015 | LOGVAL(LYE_PATH_INNODE, LY_VLOG_STR, str); |
| 2016 | free(str); |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2017 | return NULL; |
| 2018 | } |
| 2019 | module = start->module; |
| 2020 | } else { |
| 2021 | if (!mod_name) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2022 | str = strndup(nodeid, (name + nam_len) - nodeid); |
| 2023 | LOGVAL(LYE_PATH_MISSMOD, LY_VLOG_STR, nodeid); |
| 2024 | free(str); |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2025 | return NULL; |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2026 | } else if (mod_name_len > LY_BUF_SIZE - 1) { |
| 2027 | LOGINT; |
| 2028 | return NULL; |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2029 | } |
| 2030 | |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2031 | if (ly_buf_used && module_name[0]) { |
| 2032 | buf_backup = strndup(module_name, LY_BUF_SIZE - 1); |
| 2033 | } |
| 2034 | ly_buf_used++; |
| 2035 | |
Michal Vasko | df3f1ab | 2016-04-01 15:07:22 +0200 | [diff] [blame] | 2036 | memmove(module_name, mod_name, mod_name_len); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2037 | module_name[mod_name_len] = '\0'; |
| 2038 | module = ly_ctx_get_module(ctx, module_name, NULL); |
| 2039 | |
| 2040 | if (buf_backup) { |
| 2041 | /* return previous internal buffer content */ |
| 2042 | strcpy(module_name, buf_backup); |
| 2043 | free(buf_backup); |
| 2044 | buf_backup = NULL; |
| 2045 | } |
| 2046 | ly_buf_used--; |
| 2047 | |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2048 | if (!module) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2049 | str = strndup(nodeid, (mod_name + mod_name_len) - nodeid); |
| 2050 | LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str); |
| 2051 | free(str); |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2052 | return NULL; |
| 2053 | } |
| 2054 | start = module->data; |
| 2055 | |
| 2056 | /* now it's as if there was no module name */ |
| 2057 | mod_name = NULL; |
| 2058 | mod_name_len = 0; |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2059 | } |
| 2060 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2061 | prev_mod = module; |
| 2062 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2063 | while (1) { |
| 2064 | sibling = NULL; |
Michal Vasko | 8d26e5c | 2016-09-08 10:03:49 +0200 | [diff] [blame] | 2065 | while ((sibling = lys_getnext(sibling, lys_parent(start), module, |
| 2066 | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT))) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2067 | /* name match */ |
Michal Vasko | 0a1aaa4 | 2016-04-19 09:48:25 +0200 | [diff] [blame] | 2068 | 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] | 2069 | /* module check */ |
| 2070 | if (mod_name) { |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2071 | if (mod_name_len > LY_BUF_SIZE - 1) { |
Michal Vasko | 8757e7c | 2016-03-15 10:41:30 +0100 | [diff] [blame] | 2072 | LOGINT; |
| 2073 | return NULL; |
| 2074 | } |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2075 | |
| 2076 | if (ly_buf_used && module_name[0]) { |
| 2077 | buf_backup = strndup(module_name, LY_BUF_SIZE - 1); |
| 2078 | } |
| 2079 | ly_buf_used++; |
| 2080 | |
Michal Vasko | df3f1ab | 2016-04-01 15:07:22 +0200 | [diff] [blame] | 2081 | memmove(module_name, mod_name, mod_name_len); |
Michal Vasko | 8757e7c | 2016-03-15 10:41:30 +0100 | [diff] [blame] | 2082 | module_name[mod_name_len] = '\0'; |
| 2083 | /* will also find an augment module */ |
| 2084 | prefix_mod = ly_ctx_get_module(ctx, module_name, NULL); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2085 | |
| 2086 | if (buf_backup) { |
| 2087 | /* return previous internal buffer content */ |
Michal Vasko | 888a129 | 2016-04-05 12:08:54 +0200 | [diff] [blame] | 2088 | strncpy(module_name, buf_backup, LY_BUF_SIZE - 1); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2089 | free(buf_backup); |
| 2090 | buf_backup = NULL; |
| 2091 | } |
| 2092 | ly_buf_used--; |
| 2093 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2094 | if (!prefix_mod) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2095 | str = strndup(nodeid, (mod_name + mod_name_len) - nodeid); |
| 2096 | LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str); |
| 2097 | free(str); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2098 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2099 | } |
| 2100 | } else { |
| 2101 | prefix_mod = prev_mod; |
| 2102 | } |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 2103 | if (prefix_mod != lys_node_module(sibling)) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2104 | continue; |
| 2105 | } |
| 2106 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2107 | /* do we have some predicates on it? */ |
| 2108 | if (has_predicate) { |
| 2109 | r = 0; |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 2110 | if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 2111 | if ((r = parse_schema_json_predicate(id, NULL, NULL, NULL, NULL, &has_predicate)) < 1) { |
| 2112 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
| 2113 | return NULL; |
| 2114 | } |
| 2115 | } else if (sibling->nodetype == LYS_LIST) { |
| 2116 | if (resolve_json_schema_list_predicate(id, (const struct lys_node_list *)sibling, &r)) { |
| 2117 | return NULL; |
| 2118 | } |
| 2119 | } else { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2120 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2121 | return NULL; |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2122 | } |
| 2123 | id += r; |
| 2124 | } |
| 2125 | |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 2126 | /* check for shorthand cases - then 'start' does not change */ |
Michal Vasko | 8d26e5c | 2016-09-08 10:03:49 +0200 | [diff] [blame] | 2127 | 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] | 2128 | shorthand = ~shorthand; |
| 2129 | } |
| 2130 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2131 | /* the result node? */ |
| 2132 | if (!id[0]) { |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 2133 | if (shorthand) { |
| 2134 | /* wrong path for shorthand */ |
Michal Vasko | 025e045 | 2016-05-17 16:14:20 +0200 | [diff] [blame] | 2135 | str = strndup(nodeid, (name + nam_len) - nodeid); |
| 2136 | LOGVAL(LYE_PATH_INNODE, LY_VLOG_STR, str); |
Michal Vasko | 3c0f9f5 | 2016-05-17 16:38:10 +0200 | [diff] [blame] | 2137 | 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] | 2138 | free(str); |
Michal Vasko | 025e045 | 2016-05-17 16:14:20 +0200 | [diff] [blame] | 2139 | return NULL; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 2140 | } |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2141 | return sibling; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2142 | } |
| 2143 | |
Michal Vasko | 8d26e5c | 2016-09-08 10:03:49 +0200 | [diff] [blame] | 2144 | if (!shorthand) { |
Michal Vasko | 7dc71d0 | 2016-03-15 10:42:28 +0100 | [diff] [blame] | 2145 | /* move down the tree, if possible */ |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 2146 | if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2147 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
Michal Vasko | 7dc71d0 | 2016-03-15 10:42:28 +0100 | [diff] [blame] | 2148 | return NULL; |
| 2149 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2150 | start = sibling->child; |
| 2151 | } |
| 2152 | |
| 2153 | /* update prev mod */ |
| 2154 | prev_mod = start->module; |
| 2155 | break; |
| 2156 | } |
| 2157 | } |
| 2158 | |
| 2159 | /* no match */ |
| 2160 | if (!sibling) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2161 | str = strndup(nodeid, (name + nam_len) - nodeid); |
| 2162 | LOGVAL(LYE_PATH_INNODE, LY_VLOG_STR, str); |
| 2163 | free(str); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2164 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2165 | } |
| 2166 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2167 | 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] | 2168 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2169 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2170 | } |
| 2171 | id += r; |
| 2172 | } |
| 2173 | |
| 2174 | /* cannot get here */ |
| 2175 | LOGINT; |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2176 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2177 | } |
| 2178 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2179 | static int |
| 2180 | resolve_partial_json_data_list_predicate(const char *predicate, const char *node_name, struct lyd_node *node, int *parsed) |
| 2181 | { |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2182 | const char *name, *value, *key_val; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2183 | int nam_len, val_len, has_predicate = 1, r; |
| 2184 | uint16_t i; |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2185 | struct lyd_node_leaf_list *key; |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2186 | const struct lys_type *type; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2187 | |
Radek Krejci | 61a86c6 | 2016-03-24 11:06:44 +0100 | [diff] [blame] | 2188 | assert(node); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2189 | assert(node->schema->nodetype == LYS_LIST); |
| 2190 | |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2191 | key = (struct lyd_node_leaf_list *)node->child; |
| 2192 | for (i = 0; i < ((struct lys_node_list *)node->schema)->keys_size; ++i) { |
| 2193 | if (!key) { |
| 2194 | /* invalid data */ |
| 2195 | LOGINT; |
| 2196 | return -1; |
| 2197 | } |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2198 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2199 | if (!has_predicate) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2200 | LOGVAL(LYE_PATH_MISSKEY, LY_VLOG_NONE, NULL, node_name); |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2201 | return -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2202 | } |
| 2203 | |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 2204 | if (((r = parse_schema_json_predicate(predicate, &name, &nam_len, &value, &val_len, &has_predicate)) < 1) |
| 2205 | || !strncmp(name, ".", nam_len)) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2206 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-r], &predicate[-r]); |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2207 | return -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2208 | } |
| 2209 | |
| 2210 | predicate += r; |
| 2211 | *parsed += r; |
| 2212 | |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2213 | 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] | 2214 | LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name); |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2215 | return -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2216 | } |
| 2217 | |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2218 | /* make value canonical */ |
| 2219 | type = lyd_leaf_type(key, 1); |
| 2220 | if ((type->base == LY_TYPE_IDENT) |
Michal Vasko | 6a938d6 | 2016-12-21 09:21:30 +0100 | [diff] [blame] | 2221 | && !strncmp(key->value_str, lyd_node_module(node)->name, strlen(lyd_node_module(node)->name)) |
| 2222 | && (key->value_str[strlen(lyd_node_module(node)->name)] == ':')) { |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2223 | key_val = key->value_str + strlen(lyd_node_module(node)->name) + 1; |
| 2224 | } else { |
| 2225 | key_val = key->value_str; |
| 2226 | } |
| 2227 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2228 | /* value does not match */ |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2229 | if (strncmp(key_val, value, val_len) || key_val[val_len]) { |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2230 | return 1; |
| 2231 | } |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2232 | |
| 2233 | key = (struct lyd_node_leaf_list *)key->next; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2234 | } |
| 2235 | |
| 2236 | if (has_predicate) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2237 | LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2238 | return -1; |
| 2239 | } |
| 2240 | |
| 2241 | return 0; |
| 2242 | } |
| 2243 | |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 2244 | /** |
| 2245 | * @brief get the closest parent of the node (or the node itself) identified by the nodeid (path) |
| 2246 | * |
| 2247 | * @param[in] nodeid Node data path to find |
| 2248 | * @param[in] llist_value If the \p nodeid identifies leaf-list, this is expected value of the leaf-list instance. |
| 2249 | * @param[in] options Bitmask of options flags, see @ref pathoptions. |
| 2250 | * @param[out] parsed Number of characters processed in \p id |
| 2251 | * @return The closes parent (or the node itself) from the path |
| 2252 | */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2253 | struct lyd_node * |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2254 | resolve_partial_json_data_nodeid(const char *nodeid, const char *llist_value, struct lyd_node *start, int options, |
| 2255 | int *parsed) |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2256 | { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2257 | char *module_name = ly_buf(), *buf_backup = NULL, *str; |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2258 | const char *id, *mod_name, *name, *pred_name, *data_val; |
Michal Vasko | 0a8d17f | 2016-10-19 15:51:36 +0200 | [diff] [blame] | 2259 | int r, ret, mod_name_len, nam_len, is_relative = -1; |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2260 | int has_predicate, last_parsed, llval_len, pred_name_len, last_has_pred; |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2261 | struct lyd_node *sibling, *last_match = NULL; |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2262 | struct lyd_node_leaf_list *llist; |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2263 | const struct lys_type *type; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2264 | const struct lys_module *prefix_mod, *prev_mod; |
| 2265 | struct ly_ctx *ctx; |
| 2266 | |
| 2267 | assert(nodeid && start && parsed); |
| 2268 | |
| 2269 | ctx = start->schema->module->ctx; |
| 2270 | id = nodeid; |
| 2271 | |
| 2272 | 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] | 2273 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2274 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2275 | return NULL; |
| 2276 | } |
| 2277 | id += r; |
| 2278 | /* add it to parsed only after the data node was actually found */ |
| 2279 | last_parsed = r; |
| 2280 | |
| 2281 | if (is_relative) { |
Michal Vasko | 1adc724 | 2016-11-16 11:05:28 +0100 | [diff] [blame] | 2282 | prev_mod = lyd_node_module(start); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2283 | start = start->child; |
| 2284 | } else { |
| 2285 | for (; start->parent; start = start->parent); |
Michal Vasko | 1adc724 | 2016-11-16 11:05:28 +0100 | [diff] [blame] | 2286 | prev_mod = lyd_node_module(start); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2287 | } |
| 2288 | |
| 2289 | while (1) { |
| 2290 | LY_TREE_FOR(start, sibling) { |
Michal Vasko | 945b96b | 2016-10-18 11:49:12 +0200 | [diff] [blame] | 2291 | /* 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] | 2292 | if (lys_parent(sibling->schema)) { |
| 2293 | if (options & LYD_PATH_OPT_OUTPUT) { |
| 2294 | if (lys_parent(sibling->schema)->nodetype == LYS_INPUT) { |
Michal Vasko | b724689 | 2016-04-19 10:37:35 +0200 | [diff] [blame] | 2295 | 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] | 2296 | *parsed = -1; |
| 2297 | return NULL; |
| 2298 | } |
| 2299 | } else { |
| 2300 | if (lys_parent(sibling->schema)->nodetype == LYS_OUTPUT) { |
Michal Vasko | b724689 | 2016-04-19 10:37:35 +0200 | [diff] [blame] | 2301 | 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] | 2302 | *parsed = -1; |
| 2303 | return NULL; |
| 2304 | } |
| 2305 | } |
| 2306 | } |
| 2307 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2308 | /* name match */ |
| 2309 | if (!strncmp(name, sibling->schema->name, nam_len) && !sibling->schema->name[nam_len]) { |
| 2310 | |
| 2311 | /* module check */ |
| 2312 | if (mod_name) { |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2313 | if (mod_name_len > LY_BUF_SIZE - 1) { |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2314 | LOGINT; |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2315 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2316 | return NULL; |
| 2317 | } |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2318 | |
| 2319 | if (ly_buf_used && module_name[0]) { |
| 2320 | buf_backup = strndup(module_name, LY_BUF_SIZE - 1); |
| 2321 | } |
| 2322 | ly_buf_used++; |
| 2323 | |
Michal Vasko | df3f1ab | 2016-04-01 15:07:22 +0200 | [diff] [blame] | 2324 | memmove(module_name, mod_name, mod_name_len); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2325 | module_name[mod_name_len] = '\0'; |
| 2326 | /* will also find an augment module */ |
| 2327 | prefix_mod = ly_ctx_get_module(ctx, module_name, NULL); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2328 | |
| 2329 | if (buf_backup) { |
| 2330 | /* return previous internal buffer content */ |
Michal Vasko | 888a129 | 2016-04-05 12:08:54 +0200 | [diff] [blame] | 2331 | strncpy(module_name, buf_backup, LY_BUF_SIZE - 1); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2332 | free(buf_backup); |
| 2333 | buf_backup = NULL; |
| 2334 | } |
| 2335 | ly_buf_used--; |
| 2336 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2337 | if (!prefix_mod) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2338 | str = strndup(nodeid, (mod_name + mod_name_len) - nodeid); |
| 2339 | LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str); |
| 2340 | free(str); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2341 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2342 | return NULL; |
| 2343 | } |
| 2344 | } else { |
| 2345 | prefix_mod = prev_mod; |
| 2346 | } |
Michal Vasko | 1adc724 | 2016-11-16 11:05:28 +0100 | [diff] [blame] | 2347 | if (prefix_mod != lyd_node_module(sibling)) { |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2348 | continue; |
| 2349 | } |
| 2350 | |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2351 | /* leaf-list, did we find it with the correct value or not? */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2352 | if (sibling->schema->nodetype == LYS_LEAFLIST) { |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2353 | llist = (struct lyd_node_leaf_list *)sibling; |
| 2354 | |
Michal Vasko | 0a8d17f | 2016-10-19 15:51:36 +0200 | [diff] [blame] | 2355 | last_has_pred = 0; |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2356 | if (has_predicate) { |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2357 | if ((r = parse_schema_json_predicate(id, &pred_name, &pred_name_len, &llist_value, &llval_len, &last_has_pred)) < 1) { |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2358 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
| 2359 | *parsed = -1; |
| 2360 | return NULL; |
| 2361 | } |
Michal Vasko | 0a8d17f | 2016-10-19 15:51:36 +0200 | [diff] [blame] | 2362 | if ((pred_name[0] != '.') || (pred_name_len != 1)) { |
| 2363 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[1], id + 1); |
| 2364 | *parsed = -1; |
| 2365 | return NULL; |
| 2366 | } |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2367 | } else { |
| 2368 | r = 0; |
| 2369 | if (llist_value) { |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2370 | llval_len = strlen(llist_value); |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2371 | } |
| 2372 | } |
| 2373 | |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2374 | /* make value canonical */ |
| 2375 | type = lyd_leaf_type(llist, 1); |
| 2376 | if ((type->base == LY_TYPE_IDENT) |
Michal Vasko | 6a938d6 | 2016-12-21 09:21:30 +0100 | [diff] [blame] | 2377 | && !strncmp(llist->value_str, lyd_node_module(sibling)->name, strlen(lyd_node_module(sibling)->name)) |
| 2378 | && (llist->value_str[strlen(lyd_node_module(sibling)->name)] == ':')) { |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2379 | data_val = llist->value_str + strlen(lyd_node_module(sibling)->name) + 1; |
| 2380 | } else { |
| 2381 | data_val = llist->value_str; |
| 2382 | } |
| 2383 | |
| 2384 | if ((!llist_value && data_val && data_val[0]) |
| 2385 | || (llist_value && (strncmp(llist_value, data_val, llval_len) || data_val[llval_len]))) { |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2386 | continue; |
| 2387 | } |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2388 | |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2389 | id += r; |
| 2390 | last_parsed += r; |
Michal Vasko | 0a8d17f | 2016-10-19 15:51:36 +0200 | [diff] [blame] | 2391 | has_predicate = last_has_pred; |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2392 | |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 2393 | } else if (sibling->schema->nodetype == LYS_LIST) { |
| 2394 | /* list, we need predicates'n'stuff then */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2395 | r = 0; |
| 2396 | if (!has_predicate) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2397 | LOGVAL(LYE_PATH_MISSKEY, LY_VLOG_NONE, NULL, name); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2398 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2399 | return NULL; |
| 2400 | } |
| 2401 | ret = resolve_partial_json_data_list_predicate(id, name, sibling, &r); |
| 2402 | if (ret == -1) { |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2403 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2404 | return NULL; |
| 2405 | } else if (ret == 1) { |
| 2406 | /* this list instance does not match */ |
| 2407 | continue; |
| 2408 | } |
| 2409 | id += r; |
| 2410 | last_parsed += r; |
| 2411 | } |
| 2412 | |
| 2413 | *parsed += last_parsed; |
| 2414 | |
| 2415 | /* the result node? */ |
| 2416 | if (!id[0]) { |
| 2417 | return sibling; |
| 2418 | } |
| 2419 | |
| 2420 | /* move down the tree, if possible */ |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 2421 | if (sibling->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2422 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2423 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2424 | return NULL; |
| 2425 | } |
Michal Vasko | c6c52cf | 2016-03-29 11:53:04 +0200 | [diff] [blame] | 2426 | last_match = sibling; |
Michal Vasko | fc11b68 | 2016-11-18 09:52:41 +0100 | [diff] [blame] | 2427 | prev_mod = lyd_node_module(sibling); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2428 | start = sibling->child; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2429 | break; |
| 2430 | } |
| 2431 | } |
| 2432 | |
| 2433 | /* no match, return last match */ |
| 2434 | if (!sibling) { |
| 2435 | return last_match; |
| 2436 | } |
| 2437 | |
| 2438 | 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] | 2439 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2440 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2441 | return NULL; |
| 2442 | } |
| 2443 | id += r; |
| 2444 | last_parsed = r; |
| 2445 | } |
| 2446 | |
| 2447 | /* cannot get here */ |
| 2448 | LOGINT; |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2449 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2450 | return NULL; |
| 2451 | } |
| 2452 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2453 | /** |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2454 | * @brief Resolves length or range intervals. Does not log. |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2455 | * Syntax is assumed to be correct, *ret MUST be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2456 | * |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2457 | * @param[in] str_restr Restriction as a string. |
| 2458 | * @param[in] type Type of the restriction. |
| 2459 | * @param[out] ret Final interval structure that starts with |
| 2460 | * the interval of the initial type, continues with intervals |
| 2461 | * of any superior types derived from the initial one, and |
| 2462 | * finishes with intervals from our \p type. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2463 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2464 | * @return EXIT_SUCCESS on succes, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2465 | */ |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2466 | int |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2467 | 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] | 2468 | { |
| 2469 | /* 0 - unsigned, 1 - signed, 2 - floating point */ |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2470 | int kind; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2471 | int64_t local_smin, local_smax, local_fmin, local_fmax; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2472 | uint64_t local_umin, local_umax; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2473 | uint8_t local_fdig; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2474 | const char *seg_ptr, *ptr; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2475 | 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] | 2476 | |
| 2477 | switch (type->base) { |
| 2478 | case LY_TYPE_BINARY: |
| 2479 | kind = 0; |
| 2480 | local_umin = 0; |
| 2481 | local_umax = 18446744073709551615UL; |
| 2482 | |
| 2483 | if (!str_restr && type->info.binary.length) { |
| 2484 | str_restr = type->info.binary.length->expr; |
| 2485 | } |
| 2486 | break; |
| 2487 | case LY_TYPE_DEC64: |
| 2488 | kind = 2; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2489 | local_fmin = __INT64_C(-9223372036854775807) - __INT64_C(1); |
| 2490 | local_fmax = __INT64_C(9223372036854775807); |
| 2491 | local_fdig = type->info.dec64.dig; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2492 | |
| 2493 | if (!str_restr && type->info.dec64.range) { |
| 2494 | str_restr = type->info.dec64.range->expr; |
| 2495 | } |
| 2496 | break; |
| 2497 | case LY_TYPE_INT8: |
| 2498 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2499 | local_smin = __INT64_C(-128); |
| 2500 | local_smax = __INT64_C(127); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2501 | |
| 2502 | if (!str_restr && type->info.num.range) { |
| 2503 | str_restr = type->info.num.range->expr; |
| 2504 | } |
| 2505 | break; |
| 2506 | case LY_TYPE_INT16: |
| 2507 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2508 | local_smin = __INT64_C(-32768); |
| 2509 | local_smax = __INT64_C(32767); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2510 | |
| 2511 | if (!str_restr && type->info.num.range) { |
| 2512 | str_restr = type->info.num.range->expr; |
| 2513 | } |
| 2514 | break; |
| 2515 | case LY_TYPE_INT32: |
| 2516 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2517 | local_smin = __INT64_C(-2147483648); |
| 2518 | local_smax = __INT64_C(2147483647); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2519 | |
| 2520 | if (!str_restr && type->info.num.range) { |
| 2521 | str_restr = type->info.num.range->expr; |
| 2522 | } |
| 2523 | break; |
| 2524 | case LY_TYPE_INT64: |
| 2525 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2526 | local_smin = __INT64_C(-9223372036854775807) - __INT64_C(1); |
| 2527 | local_smax = __INT64_C(9223372036854775807); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2528 | |
| 2529 | if (!str_restr && type->info.num.range) { |
| 2530 | str_restr = type->info.num.range->expr; |
| 2531 | } |
| 2532 | break; |
| 2533 | case LY_TYPE_UINT8: |
| 2534 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2535 | local_umin = __UINT64_C(0); |
| 2536 | local_umax = __UINT64_C(255); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2537 | |
| 2538 | if (!str_restr && type->info.num.range) { |
| 2539 | str_restr = type->info.num.range->expr; |
| 2540 | } |
| 2541 | break; |
| 2542 | case LY_TYPE_UINT16: |
| 2543 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2544 | local_umin = __UINT64_C(0); |
| 2545 | local_umax = __UINT64_C(65535); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2546 | |
| 2547 | if (!str_restr && type->info.num.range) { |
| 2548 | str_restr = type->info.num.range->expr; |
| 2549 | } |
| 2550 | break; |
| 2551 | case LY_TYPE_UINT32: |
| 2552 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2553 | local_umin = __UINT64_C(0); |
| 2554 | local_umax = __UINT64_C(4294967295); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2555 | |
| 2556 | if (!str_restr && type->info.num.range) { |
| 2557 | str_restr = type->info.num.range->expr; |
| 2558 | } |
| 2559 | break; |
| 2560 | case LY_TYPE_UINT64: |
| 2561 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2562 | local_umin = __UINT64_C(0); |
| 2563 | local_umax = __UINT64_C(18446744073709551615); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2564 | |
| 2565 | if (!str_restr && type->info.num.range) { |
| 2566 | str_restr = type->info.num.range->expr; |
| 2567 | } |
| 2568 | break; |
| 2569 | case LY_TYPE_STRING: |
| 2570 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2571 | local_umin = __UINT64_C(0); |
| 2572 | local_umax = __UINT64_C(18446744073709551615); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2573 | |
| 2574 | if (!str_restr && type->info.str.length) { |
| 2575 | str_restr = type->info.str.length->expr; |
| 2576 | } |
| 2577 | break; |
| 2578 | default: |
| 2579 | LOGINT; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2580 | return -1; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2581 | } |
| 2582 | |
| 2583 | /* process superior types */ |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2584 | if (type->der) { |
| 2585 | if (resolve_len_ran_interval(NULL, &type->der->type, &intv)) { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2586 | LOGINT; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2587 | return -1; |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2588 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2589 | assert(!intv || (intv->kind == kind)); |
| 2590 | } |
| 2591 | |
| 2592 | if (!str_restr) { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2593 | /* we do not have any restriction, return superior ones */ |
| 2594 | *ret = intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2595 | return EXIT_SUCCESS; |
| 2596 | } |
| 2597 | |
| 2598 | /* adjust local min and max */ |
| 2599 | if (intv) { |
| 2600 | tmp_intv = intv; |
| 2601 | |
| 2602 | if (kind == 0) { |
| 2603 | local_umin = tmp_intv->value.uval.min; |
| 2604 | } else if (kind == 1) { |
| 2605 | local_smin = tmp_intv->value.sval.min; |
| 2606 | } else if (kind == 2) { |
| 2607 | local_fmin = tmp_intv->value.fval.min; |
| 2608 | } |
| 2609 | |
| 2610 | while (tmp_intv->next) { |
| 2611 | tmp_intv = tmp_intv->next; |
| 2612 | } |
| 2613 | |
| 2614 | if (kind == 0) { |
| 2615 | local_umax = tmp_intv->value.uval.max; |
| 2616 | } else if (kind == 1) { |
| 2617 | local_smax = tmp_intv->value.sval.max; |
| 2618 | } else if (kind == 2) { |
| 2619 | local_fmax = tmp_intv->value.fval.max; |
| 2620 | } |
| 2621 | } |
| 2622 | |
| 2623 | /* finally parse our restriction */ |
| 2624 | seg_ptr = str_restr; |
Pavol Vican | 9e7c01d | 2016-08-29 09:36:17 +0200 | [diff] [blame] | 2625 | tmp_intv = NULL; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2626 | while (1) { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2627 | if (!tmp_local_intv) { |
| 2628 | assert(!local_intv); |
| 2629 | local_intv = malloc(sizeof *local_intv); |
| 2630 | tmp_local_intv = local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2631 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2632 | tmp_local_intv->next = malloc(sizeof *tmp_local_intv); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2633 | tmp_local_intv = tmp_local_intv->next; |
| 2634 | } |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2635 | if (!tmp_local_intv) { |
| 2636 | LOGMEM; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2637 | goto error; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2638 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2639 | |
| 2640 | tmp_local_intv->kind = kind; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2641 | tmp_local_intv->type = type; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2642 | tmp_local_intv->next = NULL; |
| 2643 | |
| 2644 | /* min */ |
| 2645 | ptr = seg_ptr; |
| 2646 | while (isspace(ptr[0])) { |
| 2647 | ++ptr; |
| 2648 | } |
| 2649 | if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) { |
| 2650 | if (kind == 0) { |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2651 | tmp_local_intv->value.uval.min = strtol(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2652 | } else if (kind == 1) { |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2653 | tmp_local_intv->value.sval.min = strtol(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2654 | } else if (kind == 2) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 2655 | if (parse_range_dec64(&ptr, local_fdig, &tmp_local_intv->value.fval.min)) { |
| 2656 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, ptr, "range"); |
| 2657 | goto error; |
| 2658 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2659 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2660 | } else if (!strncmp(ptr, "min", 3)) { |
| 2661 | if (kind == 0) { |
| 2662 | tmp_local_intv->value.uval.min = local_umin; |
| 2663 | } else if (kind == 1) { |
| 2664 | tmp_local_intv->value.sval.min = local_smin; |
| 2665 | } else if (kind == 2) { |
| 2666 | tmp_local_intv->value.fval.min = local_fmin; |
| 2667 | } |
| 2668 | |
| 2669 | ptr += 3; |
| 2670 | } else if (!strncmp(ptr, "max", 3)) { |
| 2671 | if (kind == 0) { |
| 2672 | tmp_local_intv->value.uval.min = local_umax; |
| 2673 | } else if (kind == 1) { |
| 2674 | tmp_local_intv->value.sval.min = local_smax; |
| 2675 | } else if (kind == 2) { |
| 2676 | tmp_local_intv->value.fval.min = local_fmax; |
| 2677 | } |
| 2678 | |
| 2679 | ptr += 3; |
| 2680 | } else { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2681 | LOGINT; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2682 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2683 | } |
| 2684 | |
| 2685 | while (isspace(ptr[0])) { |
| 2686 | ptr++; |
| 2687 | } |
| 2688 | |
| 2689 | /* no interval or interval */ |
| 2690 | if ((ptr[0] == '|') || !ptr[0]) { |
| 2691 | if (kind == 0) { |
| 2692 | tmp_local_intv->value.uval.max = tmp_local_intv->value.uval.min; |
| 2693 | } else if (kind == 1) { |
| 2694 | tmp_local_intv->value.sval.max = tmp_local_intv->value.sval.min; |
| 2695 | } else if (kind == 2) { |
| 2696 | tmp_local_intv->value.fval.max = tmp_local_intv->value.fval.min; |
| 2697 | } |
| 2698 | } else if (!strncmp(ptr, "..", 2)) { |
| 2699 | /* skip ".." */ |
| 2700 | ptr += 2; |
| 2701 | while (isspace(ptr[0])) { |
| 2702 | ++ptr; |
| 2703 | } |
| 2704 | |
| 2705 | /* max */ |
| 2706 | if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) { |
| 2707 | if (kind == 0) { |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2708 | tmp_local_intv->value.uval.max = strtol(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2709 | } else if (kind == 1) { |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2710 | tmp_local_intv->value.sval.max = strtol(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2711 | } else if (kind == 2) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 2712 | if (parse_range_dec64(&ptr, local_fdig, &tmp_local_intv->value.fval.max)) { |
| 2713 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, ptr, "range"); |
| 2714 | goto error; |
| 2715 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2716 | } |
| 2717 | } else if (!strncmp(ptr, "max", 3)) { |
| 2718 | if (kind == 0) { |
| 2719 | tmp_local_intv->value.uval.max = local_umax; |
| 2720 | } else if (kind == 1) { |
| 2721 | tmp_local_intv->value.sval.max = local_smax; |
| 2722 | } else if (kind == 2) { |
| 2723 | tmp_local_intv->value.fval.max = local_fmax; |
| 2724 | } |
| 2725 | } else { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2726 | LOGINT; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2727 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2728 | } |
| 2729 | } else { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2730 | LOGINT; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2731 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2732 | } |
| 2733 | |
Pavol Vican | 9e7c01d | 2016-08-29 09:36:17 +0200 | [diff] [blame] | 2734 | /* check min and max in correct order*/ |
| 2735 | if (kind == 0) { |
| 2736 | /* current segment */ |
| 2737 | if (tmp_local_intv->value.uval.min > tmp_local_intv->value.uval.max) { |
| 2738 | goto error; |
| 2739 | } |
| 2740 | if (tmp_local_intv->value.uval.min < local_umin || tmp_local_intv->value.uval.max > local_umax) { |
| 2741 | goto error; |
| 2742 | } |
| 2743 | /* segments sholud be ascending order */ |
Pavol Vican | 69f62c9 | 2016-08-30 09:06:25 +0200 | [diff] [blame] | 2744 | 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] | 2745 | goto error; |
| 2746 | } |
| 2747 | } else if (kind == 1) { |
| 2748 | if (tmp_local_intv->value.sval.min > tmp_local_intv->value.sval.max) { |
| 2749 | goto error; |
| 2750 | } |
| 2751 | if (tmp_local_intv->value.sval.min < local_smin || tmp_local_intv->value.sval.max > local_smax) { |
| 2752 | goto error; |
| 2753 | } |
Pavol Vican | 69f62c9 | 2016-08-30 09:06:25 +0200 | [diff] [blame] | 2754 | 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] | 2755 | goto error; |
| 2756 | } |
| 2757 | } else if (kind == 2) { |
| 2758 | if (tmp_local_intv->value.fval.min > tmp_local_intv->value.fval.max) { |
| 2759 | goto error; |
| 2760 | } |
| 2761 | if (tmp_local_intv->value.fval.min < local_fmin || tmp_local_intv->value.fval.max > local_fmax) { |
| 2762 | goto error; |
| 2763 | } |
Pavol Vican | 69f62c9 | 2016-08-30 09:06:25 +0200 | [diff] [blame] | 2764 | 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] | 2765 | /* 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] | 2766 | goto error; |
| 2767 | } |
| 2768 | } |
| 2769 | |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2770 | /* next segment (next OR) */ |
| 2771 | seg_ptr = strchr(seg_ptr, '|'); |
| 2772 | if (!seg_ptr) { |
| 2773 | break; |
| 2774 | } |
| 2775 | seg_ptr++; |
Pavol Vican | 9e7c01d | 2016-08-29 09:36:17 +0200 | [diff] [blame] | 2776 | tmp_intv = tmp_local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2777 | } |
| 2778 | |
| 2779 | /* check local restrictions against superior ones */ |
| 2780 | if (intv) { |
| 2781 | tmp_intv = intv; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2782 | tmp_local_intv = local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2783 | |
| 2784 | while (tmp_local_intv && tmp_intv) { |
| 2785 | /* reuse local variables */ |
| 2786 | if (kind == 0) { |
| 2787 | local_umin = tmp_local_intv->value.uval.min; |
| 2788 | local_umax = tmp_local_intv->value.uval.max; |
| 2789 | |
| 2790 | /* it must be in this interval */ |
| 2791 | if ((local_umin >= tmp_intv->value.uval.min) && (local_umin <= tmp_intv->value.uval.max)) { |
| 2792 | /* this interval is covered, next one */ |
| 2793 | if (local_umax <= tmp_intv->value.uval.max) { |
| 2794 | tmp_local_intv = tmp_local_intv->next; |
| 2795 | continue; |
| 2796 | /* ascending order of restrictions -> fail */ |
| 2797 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2798 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2799 | } |
| 2800 | } |
| 2801 | } else if (kind == 1) { |
| 2802 | local_smin = tmp_local_intv->value.sval.min; |
| 2803 | local_smax = tmp_local_intv->value.sval.max; |
| 2804 | |
| 2805 | if ((local_smin >= tmp_intv->value.sval.min) && (local_smin <= tmp_intv->value.sval.max)) { |
| 2806 | if (local_smax <= tmp_intv->value.sval.max) { |
| 2807 | tmp_local_intv = tmp_local_intv->next; |
| 2808 | continue; |
| 2809 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2810 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2811 | } |
| 2812 | } |
| 2813 | } else if (kind == 2) { |
| 2814 | local_fmin = tmp_local_intv->value.fval.min; |
| 2815 | local_fmax = tmp_local_intv->value.fval.max; |
| 2816 | |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2817 | 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] | 2818 | && (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] | 2819 | 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] | 2820 | tmp_local_intv = tmp_local_intv->next; |
| 2821 | continue; |
| 2822 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2823 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2824 | } |
| 2825 | } |
| 2826 | } |
| 2827 | |
| 2828 | tmp_intv = tmp_intv->next; |
| 2829 | } |
| 2830 | |
| 2831 | /* some interval left uncovered -> fail */ |
| 2832 | if (tmp_local_intv) { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2833 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2834 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2835 | } |
| 2836 | |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2837 | /* append the local intervals to all the intervals of the superior types, return it all */ |
| 2838 | if (intv) { |
| 2839 | for (tmp_intv = intv; tmp_intv->next; tmp_intv = tmp_intv->next); |
| 2840 | tmp_intv->next = local_intv; |
| 2841 | } else { |
| 2842 | intv = local_intv; |
| 2843 | } |
| 2844 | *ret = intv; |
| 2845 | |
| 2846 | return EXIT_SUCCESS; |
| 2847 | |
| 2848 | error: |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2849 | while (intv) { |
| 2850 | tmp_intv = intv->next; |
| 2851 | free(intv); |
| 2852 | intv = tmp_intv; |
| 2853 | } |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2854 | while (local_intv) { |
| 2855 | tmp_local_intv = local_intv->next; |
| 2856 | free(local_intv); |
| 2857 | local_intv = tmp_local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2858 | } |
| 2859 | |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2860 | return -1; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2861 | } |
| 2862 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2863 | /** |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2864 | * @brief Resolve a typedef, return only resolved typedefs if derived. If leafref, it must be |
| 2865 | * resolved for this function to return it. Does not log. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2866 | * |
| 2867 | * @param[in] name Typedef name. |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2868 | * @param[in] mod_name Typedef name module name. |
| 2869 | * @param[in] module Main module. |
| 2870 | * @param[in] parent Parent of the resolved type definition. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2871 | * @param[out] ret Pointer to the resolved typedef. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2872 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2873 | * @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] | 2874 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2875 | int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 2876 | resolve_superior_type(const char *name, const char *mod_name, const struct lys_module *module, |
| 2877 | const struct lys_node *parent, struct lys_tpdf **ret) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2878 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2879 | int i, j; |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2880 | struct lys_tpdf *tpdf, *match; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2881 | int tpdf_size; |
| 2882 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2883 | if (!mod_name) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2884 | /* no prefix, try built-in types */ |
| 2885 | for (i = 1; i < LY_DATA_TYPE_COUNT; i++) { |
| 2886 | if (!strcmp(ly_types[i].def->name, name)) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2887 | if (ret) { |
| 2888 | *ret = ly_types[i].def; |
| 2889 | } |
| 2890 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2891 | } |
| 2892 | } |
| 2893 | } else { |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2894 | if (!strcmp(mod_name, module->name)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2895 | /* prefix refers to the current module, ignore it */ |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2896 | mod_name = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2897 | } |
| 2898 | } |
| 2899 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2900 | if (!mod_name && parent) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2901 | /* search in local typedefs */ |
| 2902 | while (parent) { |
| 2903 | switch (parent->nodetype) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2904 | case LYS_CONTAINER: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 2905 | tpdf_size = ((struct lys_node_container *)parent)->tpdf_size; |
| 2906 | tpdf = ((struct lys_node_container *)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_LIST: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 2910 | tpdf_size = ((struct lys_node_list *)parent)->tpdf_size; |
| 2911 | tpdf = ((struct lys_node_list *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2912 | break; |
| 2913 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2914 | case LYS_GROUPING: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 2915 | tpdf_size = ((struct lys_node_grp *)parent)->tpdf_size; |
| 2916 | tpdf = ((struct lys_node_grp *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2917 | break; |
| 2918 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2919 | case LYS_RPC: |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 2920 | case LYS_ACTION: |
| 2921 | tpdf_size = ((struct lys_node_rpc_action *)parent)->tpdf_size; |
| 2922 | tpdf = ((struct lys_node_rpc_action *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2923 | break; |
| 2924 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2925 | case LYS_NOTIF: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 2926 | tpdf_size = ((struct lys_node_notif *)parent)->tpdf_size; |
| 2927 | tpdf = ((struct lys_node_notif *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2928 | break; |
| 2929 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2930 | case LYS_INPUT: |
| 2931 | case LYS_OUTPUT: |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 2932 | tpdf_size = ((struct lys_node_inout *)parent)->tpdf_size; |
| 2933 | tpdf = ((struct lys_node_inout *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2934 | break; |
| 2935 | |
| 2936 | default: |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 2937 | parent = lys_parent(parent); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2938 | continue; |
| 2939 | } |
| 2940 | |
| 2941 | for (i = 0; i < tpdf_size; i++) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 2942 | if (!strcmp(tpdf[i].name, name) && tpdf[i].type.base > 0) { |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2943 | match = &tpdf[i]; |
| 2944 | goto check_leafref; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2945 | } |
| 2946 | } |
| 2947 | |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 2948 | parent = lys_parent(parent); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2949 | } |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2950 | } else { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2951 | /* get module where to search */ |
Michal Vasko | b6729c6 | 2015-10-21 12:09:47 +0200 | [diff] [blame] | 2952 | module = lys_get_import_module(module, NULL, 0, mod_name, 0); |
Michal Vasko | c935fff | 2015-08-17 14:02:06 +0200 | [diff] [blame] | 2953 | if (!module) { |
| 2954 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2955 | } |
| 2956 | } |
| 2957 | |
| 2958 | /* search in top level typedefs */ |
| 2959 | for (i = 0; i < module->tpdf_size; i++) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 2960 | 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] | 2961 | match = &module->tpdf[i]; |
| 2962 | goto check_leafref; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2963 | } |
| 2964 | } |
| 2965 | |
| 2966 | /* search in submodules */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2967 | for (i = 0; i < module->inc_size && module->inc[i].submodule; i++) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2968 | for (j = 0; j < module->inc[i].submodule->tpdf_size; j++) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 2969 | 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] | 2970 | match = &module->inc[i].submodule->tpdf[j]; |
| 2971 | goto check_leafref; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2972 | } |
| 2973 | } |
| 2974 | } |
| 2975 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2976 | return EXIT_FAILURE; |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2977 | |
| 2978 | check_leafref: |
| 2979 | if (ret) { |
| 2980 | *ret = match; |
| 2981 | } |
| 2982 | if (match->type.base == LY_TYPE_LEAFREF) { |
| 2983 | while (!match->type.info.lref.path) { |
| 2984 | match = match->type.der; |
| 2985 | assert(match); |
| 2986 | } |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2987 | } |
| 2988 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2989 | } |
| 2990 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2991 | /** |
| 2992 | * @brief Check the default \p value of the \p type. Logs directly. |
| 2993 | * |
| 2994 | * @param[in] type Type definition to use. |
| 2995 | * @param[in] value Default value to check. |
Michal Vasko | 5682640 | 2016-03-02 11:11:37 +0100 | [diff] [blame] | 2996 | * @param[in] module Type module. |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2997 | * |
| 2998 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 2999 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3000 | static int |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3001 | 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] | 3002 | { |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3003 | struct lys_tpdf *base_tpdf = NULL; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3004 | struct lyd_node_leaf_list node; |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3005 | const char *dflt = NULL; |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 3006 | int ret = EXIT_SUCCESS; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3007 | |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3008 | assert(value); |
| 3009 | |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 3010 | if (type->base <= LY_TYPE_DER) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3011 | /* the type was not resolved yet, nothing to do for now */ |
| 3012 | return EXIT_FAILURE; |
| 3013 | } |
| 3014 | |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3015 | dflt = *value; |
| 3016 | if (!dflt) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3017 | /* we do not have a new default value, so is there any to check even, in some base type? */ |
| 3018 | for (base_tpdf = type->der; base_tpdf->type.der; base_tpdf = base_tpdf->type.der) { |
| 3019 | if (base_tpdf->dflt) { |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3020 | dflt = base_tpdf->dflt; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3021 | break; |
| 3022 | } |
| 3023 | } |
| 3024 | |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3025 | if (!dflt) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3026 | /* no default value, nothing to check, all is well */ |
| 3027 | return EXIT_SUCCESS; |
| 3028 | } |
| 3029 | |
| 3030 | /* 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)? */ |
| 3031 | switch (type->base) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3032 | case LY_TYPE_IDENT: |
| 3033 | case LY_TYPE_INST: |
| 3034 | case LY_TYPE_LEAFREF: |
| 3035 | case LY_TYPE_BOOL: |
| 3036 | case LY_TYPE_EMPTY: |
| 3037 | /* these have no restrictions, so we would do the exact same work as the unres in the base typedef */ |
| 3038 | return EXIT_SUCCESS; |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3039 | case LY_TYPE_BITS: |
| 3040 | /* the default value must match the restricted list of values, if the type was restricted */ |
| 3041 | if (type->info.bits.count) { |
| 3042 | break; |
| 3043 | } |
| 3044 | return EXIT_SUCCESS; |
| 3045 | case LY_TYPE_ENUM: |
| 3046 | /* the default value must match the restricted list of values, if the type was restricted */ |
| 3047 | if (type->info.enums.count) { |
| 3048 | break; |
| 3049 | } |
| 3050 | return EXIT_SUCCESS; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3051 | case LY_TYPE_DEC64: |
| 3052 | if (type->info.dec64.range) { |
| 3053 | break; |
| 3054 | } |
| 3055 | return EXIT_SUCCESS; |
| 3056 | case LY_TYPE_BINARY: |
| 3057 | if (type->info.binary.length) { |
| 3058 | break; |
| 3059 | } |
| 3060 | return EXIT_SUCCESS; |
| 3061 | case LY_TYPE_INT8: |
| 3062 | case LY_TYPE_INT16: |
| 3063 | case LY_TYPE_INT32: |
| 3064 | case LY_TYPE_INT64: |
| 3065 | case LY_TYPE_UINT8: |
| 3066 | case LY_TYPE_UINT16: |
| 3067 | case LY_TYPE_UINT32: |
| 3068 | case LY_TYPE_UINT64: |
| 3069 | if (type->info.num.range) { |
| 3070 | break; |
| 3071 | } |
| 3072 | return EXIT_SUCCESS; |
| 3073 | case LY_TYPE_STRING: |
| 3074 | if (type->info.str.length || type->info.str.patterns) { |
| 3075 | break; |
| 3076 | } |
| 3077 | return EXIT_SUCCESS; |
| 3078 | case LY_TYPE_UNION: |
| 3079 | /* way too much trouble learning whether we need to check the default again, so just do it */ |
| 3080 | break; |
| 3081 | default: |
| 3082 | LOGINT; |
| 3083 | return -1; |
| 3084 | } |
Radek Krejci | 55a161c | 2016-09-05 17:13:25 +0200 | [diff] [blame] | 3085 | } else if (type->base == LY_TYPE_EMPTY) { |
| 3086 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "default", type->parent->name); |
| 3087 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The \"empty\" data type cannot have a default value."); |
| 3088 | return -1; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3089 | } |
| 3090 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3091 | /* dummy leaf */ |
Radek Krejci | 4fe36bd | 2016-03-17 16:47:16 +0100 | [diff] [blame] | 3092 | memset(&node, 0, sizeof node); |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3093 | node.value_str = dflt; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3094 | node.value_type = type->base; |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 3095 | node.schema = calloc(1, sizeof (struct lys_node_leaf)); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3096 | if (!node.schema) { |
| 3097 | LOGMEM; |
| 3098 | return -1; |
| 3099 | } |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3100 | node.schema->name = strdup("fake-default"); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3101 | if (!node.schema->name) { |
| 3102 | LOGMEM; |
Michal Vasko | f49eda0 | 2016-07-21 12:17:01 +0200 | [diff] [blame] | 3103 | free(node.schema); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3104 | return -1; |
| 3105 | } |
Michal Vasko | 5682640 | 2016-03-02 11:11:37 +0100 | [diff] [blame] | 3106 | node.schema->module = module; |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 3107 | memcpy(&((struct lys_node_leaf *)node.schema)->type, type, sizeof *type); |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3108 | |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 3109 | if (type->base == LY_TYPE_LEAFREF) { |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3110 | if (!type->info.lref.target) { |
| 3111 | ret = EXIT_FAILURE; |
| 3112 | goto finish; |
| 3113 | } |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3114 | ret = check_default(&type->info.lref.target->type, &dflt, module); |
| 3115 | if (!ret) { |
| 3116 | /* adopt possibly changed default value to its canonical form */ |
| 3117 | if (*value) { |
| 3118 | *value = dflt; |
| 3119 | } |
| 3120 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3121 | } else { |
Radek Krejci | bb1ce0f | 2016-12-05 13:24:33 +0100 | [diff] [blame] | 3122 | if (!lyp_parse_value(&((struct lys_node_leaf *)node.schema)->type, &node.value_str, NULL, NULL, &node, 1, 1, 1)) { |
Radek Krejci | 5dca593 | 2016-11-04 14:30:47 +0100 | [diff] [blame] | 3123 | /* possible forward reference */ |
| 3124 | ret = 1; |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3125 | if (base_tpdf) { |
Radek Krejci | 9ad23f4 | 2016-10-31 15:46:52 +0100 | [diff] [blame] | 3126 | /* default value is defined in some base typedef */ |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3127 | if ((type->base == LY_TYPE_BITS && type->der->type.der) || |
| 3128 | (type->base == LY_TYPE_ENUM && type->der->type.der)) { |
| 3129 | /* we have refined bits/enums */ |
| 3130 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, |
| 3131 | "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] | 3132 | dflt, type->parent->name, base_tpdf->name); |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3133 | } |
| 3134 | } |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3135 | } else { |
| 3136 | /* success - adopt canonical form from the node into the default value */ |
| 3137 | if (dflt != node.value_str) { |
| 3138 | /* this can happen only if we have non-inherited default value, |
| 3139 | * inherited default values are already in canonical form */ |
| 3140 | assert(dflt == *value); |
| 3141 | *value = node.value_str; |
| 3142 | } |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 3143 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3144 | } |
| 3145 | |
| 3146 | finish: |
| 3147 | if (node.value_type == LY_TYPE_BITS) { |
| 3148 | free(node.value.bit); |
| 3149 | } |
| 3150 | free((char *)node.schema->name); |
| 3151 | free(node.schema); |
| 3152 | |
| 3153 | return ret; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3154 | } |
| 3155 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3156 | /** |
| 3157 | * @brief Check a key for mandatory attributes. Logs directly. |
| 3158 | * |
| 3159 | * @param[in] key The key to check. |
| 3160 | * @param[in] flags What flags to check. |
| 3161 | * @param[in] list The list of all the keys. |
| 3162 | * @param[in] index Index of the key in the key list. |
| 3163 | * @param[in] name The name of the keys. |
| 3164 | * @param[in] len The name length. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3165 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3166 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3167 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3168 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3169 | 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] | 3170 | { |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3171 | struct lys_node_leaf *key = list->keys[index]; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3172 | char *dup = NULL; |
| 3173 | int j; |
| 3174 | |
| 3175 | /* existence */ |
| 3176 | if (!key) { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3177 | if (name[len] != '\0') { |
| 3178 | dup = strdup(name); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3179 | if (!dup) { |
| 3180 | LOGMEM; |
| 3181 | return -1; |
| 3182 | } |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3183 | dup[len] = '\0'; |
| 3184 | name = dup; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3185 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3186 | LOGVAL(LYE_KEY_MISS, LY_VLOG_LYS, list, name); |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 3187 | free(dup); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3188 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3189 | } |
| 3190 | |
| 3191 | /* uniqueness */ |
| 3192 | for (j = index - 1; j >= 0; j--) { |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3193 | if (key == list->keys[j]) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3194 | LOGVAL(LYE_KEY_DUP, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3195 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3196 | } |
| 3197 | } |
| 3198 | |
| 3199 | /* key is a leaf */ |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3200 | if (key->nodetype != LYS_LEAF) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3201 | LOGVAL(LYE_KEY_NLEAF, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3202 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3203 | } |
| 3204 | |
| 3205 | /* type of the leaf is not built-in empty */ |
Radek Krejci | c3738db | 2016-09-15 15:51:21 +0200 | [diff] [blame] | 3206 | if (key->type.base == LY_TYPE_EMPTY && key->module->version < 2) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3207 | LOGVAL(LYE_KEY_TYPE, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3208 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3209 | } |
| 3210 | |
| 3211 | /* config attribute is the same as of the list */ |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 3212 | 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] | 3213 | LOGVAL(LYE_KEY_CONFIG, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3214 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3215 | } |
| 3216 | |
Radek Krejci | 55e2cdc | 2016-03-11 13:51:09 +0100 | [diff] [blame] | 3217 | /* key is not placed from augment */ |
| 3218 | if (key->parent->nodetype == LYS_AUGMENT) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3219 | LOGVAL(LYE_KEY_MISS, LY_VLOG_LYS, key, key->name); |
| 3220 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, key, "Key inserted from augment."); |
Radek Krejci | 55e2cdc | 2016-03-11 13:51:09 +0100 | [diff] [blame] | 3221 | return -1; |
| 3222 | } |
| 3223 | |
Radek Krejci | 3f21ada | 2016-08-01 13:34:31 +0200 | [diff] [blame] | 3224 | /* key is not when/if-feature -conditional */ |
| 3225 | j = 0; |
| 3226 | if (key->when || (key->iffeature_size && (j = 1))) { |
| 3227 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, key, j ? "if-feature" : "when", "leaf"); |
| 3228 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, key, "Key definition cannot depend on a \"%s\" condition.", |
| 3229 | j ? "if-feature" : "when"); |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3230 | return -1; |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3231 | } |
| 3232 | |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3233 | return EXIT_SUCCESS; |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3234 | } |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3235 | |
| 3236 | /** |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3237 | * @brief Resolve (test the target exists) unique. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3238 | * |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3239 | * @param[in] parent The parent node of the unique structure. |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3240 | * @param[in] uniq_str_path One path from the unique string. |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3241 | * |
| 3242 | * @return EXIT_SUCCESS on succes, EXIT_FAILURE on forward reference, -1 on error. |
| 3243 | */ |
| 3244 | int |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3245 | 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] | 3246 | { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3247 | int rc; |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3248 | const struct lys_node *leaf = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3249 | |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 3250 | 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] | 3251 | if (rc || !leaf) { |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3252 | if (rc) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3253 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3254 | if (rc > 0) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3255 | 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] | 3256 | } else if (rc == -2) { |
Michal Vasko | c66c6d8 | 2016-04-12 11:37:31 +0200 | [diff] [blame] | 3257 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, parent, "Unique argument references list."); |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3258 | } |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3259 | rc = -1; |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3260 | } else { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3261 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
| 3262 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, parent, "Target leaf not found."); |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3263 | rc = EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3264 | } |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3265 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3266 | } |
Michal Vasko | 9bb061b | 2016-02-12 11:00:19 +0100 | [diff] [blame] | 3267 | if (leaf->nodetype != LYS_LEAF) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3268 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
| 3269 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, parent, "Target is not a leaf."); |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3270 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3271 | } |
| 3272 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3273 | /* check status */ |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3274 | 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] | 3275 | return -1; |
| 3276 | } |
| 3277 | |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3278 | /* check that all unique's targets are of the same config type */ |
| 3279 | if (*trg_type) { |
| 3280 | if (((*trg_type == 1) && (leaf->flags & LYS_CONFIG_R)) || ((*trg_type == 2) && (leaf->flags & LYS_CONFIG_W))) { |
| 3281 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
| 3282 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, parent, |
| 3283 | "Leaf \"%s\" referenced in unique statement is config %s, but previous referenced leaf is config %s.", |
| 3284 | uniq_str_path, *trg_type == 1 ? "false" : "true", *trg_type == 1 ? "true" : "false"); |
| 3285 | return -1; |
| 3286 | } |
| 3287 | } else { |
| 3288 | /* first unique */ |
| 3289 | if (leaf->flags & LYS_CONFIG_W) { |
| 3290 | *trg_type = 1; |
| 3291 | } else { |
| 3292 | *trg_type = 2; |
| 3293 | } |
| 3294 | } |
| 3295 | |
Radek Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame] | 3296 | /* set leaf's unique flag */ |
| 3297 | ((struct lys_node_leaf *)leaf)->flags |= LYS_UNIQUE; |
| 3298 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3299 | return EXIT_SUCCESS; |
| 3300 | |
| 3301 | error: |
| 3302 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3303 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3304 | } |
| 3305 | |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 3306 | void |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3307 | unres_data_del(struct unres_data *unres, uint32_t i) |
| 3308 | { |
| 3309 | /* there are items after the one deleted */ |
| 3310 | if (i+1 < unres->count) { |
| 3311 | /* we only move the data, memory is left allocated, why bother */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3312 | 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] | 3313 | |
| 3314 | /* deleting the last item */ |
| 3315 | } else if (i == 0) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3316 | free(unres->node); |
| 3317 | unres->node = NULL; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3318 | } |
| 3319 | |
| 3320 | /* if there are no items after and it is not the last one, just move the counter */ |
| 3321 | --unres->count; |
| 3322 | } |
| 3323 | |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3324 | /** |
| 3325 | * @brief Resolve (find) a data node from a specific module. Does not log. |
| 3326 | * |
| 3327 | * @param[in] mod Module to search in. |
| 3328 | * @param[in] name Name of the data node. |
| 3329 | * @param[in] nam_len Length of the name. |
| 3330 | * @param[in] start Data node to start the search from. |
| 3331 | * @param[in,out] parents Resolved nodes. If there are some parents, |
| 3332 | * they are replaced (!!) with the resolvents. |
| 3333 | * |
Michal Vasko | 2471e7f | 2016-04-11 11:00:15 +0200 | [diff] [blame] | 3334 | * @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] | 3335 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3336 | static int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3337 | 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] | 3338 | { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3339 | struct lyd_node *node; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3340 | int flag; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3341 | uint32_t i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3342 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3343 | if (!parents->count) { |
| 3344 | parents->count = 1; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3345 | parents->node = malloc(sizeof *parents->node); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3346 | if (!parents->node) { |
| 3347 | LOGMEM; |
Michal Vasko | 2471e7f | 2016-04-11 11:00:15 +0200 | [diff] [blame] | 3348 | return -1; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3349 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3350 | parents->node[0] = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3351 | } |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3352 | for (i = 0; i < parents->count;) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 3353 | 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] | 3354 | /* skip */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3355 | ++i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3356 | continue; |
| 3357 | } |
| 3358 | flag = 0; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3359 | LY_TREE_FOR(parents->node[i] ? parents->node[i]->child : start, node) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3360 | if (node->schema->module == mod && !strncmp(node->schema->name, name, nam_len) |
| 3361 | && node->schema->name[nam_len] == '\0') { |
| 3362 | /* matching target */ |
| 3363 | if (!flag) { |
Michal Vasko | 9a47e12 | 2015-09-03 14:26:32 +0200 | [diff] [blame] | 3364 | /* put node instead of the current parent */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3365 | parents->node[i] = node; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3366 | flag = 1; |
| 3367 | } else { |
Michal Vasko | 9a47e12 | 2015-09-03 14:26:32 +0200 | [diff] [blame] | 3368 | /* multiple matching, so create a new node */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3369 | ++parents->count; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3370 | parents->node = ly_realloc(parents->node, parents->count * sizeof *parents->node); |
| 3371 | if (!parents->node) { |
| 3372 | return EXIT_FAILURE; |
| 3373 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3374 | parents->node[parents->count-1] = node; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3375 | ++i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3376 | } |
| 3377 | } |
| 3378 | } |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3379 | |
| 3380 | if (!flag) { |
| 3381 | /* remove item from the parents list */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3382 | unres_data_del(parents, i); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3383 | } else { |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3384 | ++i; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3385 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3386 | } |
| 3387 | |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3388 | return parents->count ? EXIT_SUCCESS : EXIT_FAILURE; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3389 | } |
| 3390 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3391 | /** |
| 3392 | * @brief Resolve (find) a data node. Does not log. |
| 3393 | * |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3394 | * @param[in] mod_name Module name of the data node. |
| 3395 | * @param[in] mod_name_len Length of the module name. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3396 | * @param[in] name Name of the data node. |
| 3397 | * @param[in] nam_len Length of the name. |
| 3398 | * @param[in] start Data node to start the search from. |
| 3399 | * @param[in,out] parents Resolved nodes. If there are some parents, |
| 3400 | * they are replaced (!!) with the resolvents. |
| 3401 | * |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3402 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 otherwise. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3403 | */ |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3404 | static int |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3405 | 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] | 3406 | struct unres_data *parents) |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3407 | { |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3408 | const struct lys_module *mod; |
Michal Vasko | 31fc367 | 2015-10-21 12:08:13 +0200 | [diff] [blame] | 3409 | char *str; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3410 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3411 | assert(start); |
| 3412 | |
Michal Vasko | 31fc367 | 2015-10-21 12:08:13 +0200 | [diff] [blame] | 3413 | if (mod_name) { |
| 3414 | /* we have mod_name, find appropriate module */ |
| 3415 | str = strndup(mod_name, mod_name_len); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3416 | if (!str) { |
| 3417 | LOGMEM; |
| 3418 | return -1; |
| 3419 | } |
Michal Vasko | 31fc367 | 2015-10-21 12:08:13 +0200 | [diff] [blame] | 3420 | mod = ly_ctx_get_module(start->schema->module->ctx, str, NULL); |
| 3421 | free(str); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3422 | if (!mod) { |
| 3423 | /* invalid prefix */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3424 | return -1; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3425 | } |
| 3426 | } else { |
| 3427 | /* no prefix, module is the same as of current node */ |
| 3428 | mod = start->schema->module; |
| 3429 | } |
| 3430 | |
| 3431 | return resolve_data(mod, name, name_len, start, parents); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3432 | } |
| 3433 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3434 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3435 | * @brief Resolve a path predicate (leafref) in JSON data context. Logs directly |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3436 | * only specific errors, general no-resolvent error is left to the caller. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3437 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3438 | * @param[in] pred Predicate to use. |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3439 | * @param[in] node Node from which the predicate is being resolved |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3440 | * @param[in,out] node_match Nodes satisfying the restriction |
| 3441 | * without the predicate. Nodes not |
| 3442 | * satisfying the predicate are removed. |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3443 | * @param[out] parsed Number of characters parsed, negative on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3444 | * |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3445 | * @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] | 3446 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3447 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3448 | 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] | 3449 | int *parsed) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3450 | { |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3451 | /* ... /node[source = destination] ... */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3452 | struct unres_data source_match, dest_match; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3453 | const char *path_key_expr, *source, *sour_pref, *dest, *dest_pref; |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3454 | int pke_len, sour_len, sour_pref_len, dest_len, dest_pref_len, parsed_loc = 0, pke_parsed = 0; |
| 3455 | int has_predicate, dest_parent_times, i, rc; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3456 | uint32_t j; |
Radek Krejci | 0c2fa3a | 2016-06-13 15:18:03 +0200 | [diff] [blame] | 3457 | struct lyd_node_leaf_list *leaf_dst, *leaf_src; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3458 | |
| 3459 | source_match.count = 1; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3460 | source_match.node = malloc(sizeof *source_match.node); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3461 | if (!source_match.node) { |
| 3462 | LOGMEM; |
| 3463 | return -1; |
| 3464 | } |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3465 | dest_match.count = 1; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3466 | dest_match.node = malloc(sizeof *dest_match.node); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3467 | if (!dest_match.node) { |
| 3468 | LOGMEM; |
| 3469 | return -1; |
| 3470 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3471 | |
| 3472 | do { |
| 3473 | if ((i = parse_path_predicate(pred, &sour_pref, &sour_pref_len, &source, &sour_len, &path_key_expr, |
| 3474 | &pke_len, &has_predicate)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3475 | LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, pred[-i], &pred[-i]); |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3476 | rc = -1; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3477 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3478 | } |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3479 | parsed_loc += i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3480 | pred += i; |
| 3481 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3482 | for (j = 0; j < node_match->count;) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3483 | /* source */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3484 | source_match.node[0] = node_match->node[j]; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3485 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3486 | /* must be leaf (key of a list) */ |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3487 | 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] | 3488 | &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] | 3489 | i = 0; |
| 3490 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3491 | } |
| 3492 | |
| 3493 | /* destination */ |
Radek Krejci | 0c2fa3a | 2016-06-13 15:18:03 +0200 | [diff] [blame] | 3494 | dest_match.node[0] = node; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3495 | dest_parent_times = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3496 | if ((i = parse_path_key_expr(path_key_expr, &dest_pref, &dest_pref_len, &dest, &dest_len, |
| 3497 | &dest_parent_times)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3498 | 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] | 3499 | rc = -1; |
| 3500 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3501 | } |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3502 | pke_parsed = i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3503 | for (i = 0; i < dest_parent_times; ++i) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3504 | dest_match.node[0] = dest_match.node[0]->parent; |
| 3505 | if (!dest_match.node[0]) { |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3506 | i = 0; |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3507 | rc = EXIT_FAILURE; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3508 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3509 | } |
| 3510 | } |
| 3511 | while (1) { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3512 | 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] | 3513 | &dest_match)) || (dest_match.count != 1)) { |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3514 | i = 0; |
| 3515 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3516 | } |
| 3517 | |
| 3518 | if (pke_len == pke_parsed) { |
| 3519 | break; |
| 3520 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3521 | 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] | 3522 | &dest_parent_times)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3523 | 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] | 3524 | rc = -1; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3525 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3526 | } |
| 3527 | pke_parsed += i; |
| 3528 | } |
| 3529 | |
| 3530 | /* check match between source and destination nodes */ |
Radek Krejci | 0c2fa3a | 2016-06-13 15:18:03 +0200 | [diff] [blame] | 3531 | leaf_dst = (struct lyd_node_leaf_list *)dest_match.node[0]; |
| 3532 | while (leaf_dst->value_type == LY_TYPE_LEAFREF) { |
| 3533 | leaf_dst = (struct lyd_node_leaf_list *)leaf_dst->value.leafref; |
| 3534 | } |
| 3535 | leaf_src = (struct lyd_node_leaf_list *)source_match.node[0]; |
| 3536 | while (leaf_src->value_type == LY_TYPE_LEAFREF) { |
| 3537 | leaf_src = (struct lyd_node_leaf_list *)leaf_src->value.leafref; |
| 3538 | } |
| 3539 | if (leaf_src->value_type != leaf_dst->value_type) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3540 | goto remove_leafref; |
| 3541 | } |
| 3542 | |
Radek Krejci | 0c2fa3a | 2016-06-13 15:18:03 +0200 | [diff] [blame] | 3543 | if (!ly_strequal(leaf_src->value_str, leaf_dst->value_str, 1)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3544 | goto remove_leafref; |
| 3545 | } |
| 3546 | |
| 3547 | /* leafref is ok, continue check with next leafref */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3548 | ++j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3549 | continue; |
| 3550 | |
| 3551 | remove_leafref: |
| 3552 | /* does not fulfill conditions, remove leafref record */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3553 | unres_data_del(node_match, j); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3554 | } |
| 3555 | } while (has_predicate); |
| 3556 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3557 | free(source_match.node); |
| 3558 | free(dest_match.node); |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3559 | if (parsed) { |
| 3560 | *parsed = parsed_loc; |
| 3561 | } |
| 3562 | return EXIT_SUCCESS; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3563 | |
| 3564 | error: |
| 3565 | |
| 3566 | if (source_match.count) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3567 | free(source_match.node); |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3568 | } |
| 3569 | if (dest_match.count) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3570 | free(dest_match.node); |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3571 | } |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3572 | if (parsed) { |
| 3573 | *parsed = -parsed_loc+i; |
| 3574 | } |
| 3575 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3576 | } |
| 3577 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3578 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3579 | * @brief Resolve a path (leafref) in JSON data context. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3580 | * |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3581 | * @param[in] node Leafref data node. |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3582 | * @param[in] path Path of the leafref. |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3583 | * @param[out] ret Matching nodes. Expects an empty, but allocated structure. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3584 | * |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3585 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 otherwise. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3586 | */ |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3587 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3588 | 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] | 3589 | { |
Radek Krejci | 71b795b | 2015-08-10 16:20:39 +0200 | [diff] [blame] | 3590 | struct lyd_node *data = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3591 | const char *prefix, *name; |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3592 | int pref_len, nam_len, has_predicate, parent_times, i, parsed, rc; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3593 | uint32_t j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3594 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3595 | assert(node && path && ret && !ret->count); |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3596 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3597 | parent_times = 0; |
Michal Vasko | d917334 | 2015-08-17 14:35:36 +0200 | [diff] [blame] | 3598 | parsed = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3599 | |
| 3600 | /* searching for nodeset */ |
| 3601 | do { |
Radek Krejci | f7ed4c3 | 2016-10-27 16:20:03 +0200 | [diff] [blame] | 3602 | 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] | 3603 | LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, path[-i], &path[-i]); |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3604 | rc = -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3605 | goto error; |
| 3606 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3607 | path += i; |
Michal Vasko | d917334 | 2015-08-17 14:35:36 +0200 | [diff] [blame] | 3608 | parsed += i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3609 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3610 | if (!ret->count) { |
Michal Vasko | bfd98e6 | 2016-09-02 09:50:05 +0200 | [diff] [blame] | 3611 | if (parent_times > 0) { |
| 3612 | data = node; |
| 3613 | for (i = 1; i < parent_times; ++i) { |
| 3614 | data = data->parent; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3615 | } |
Michal Vasko | bfd98e6 | 2016-09-02 09:50:05 +0200 | [diff] [blame] | 3616 | } else if (!parent_times) { |
| 3617 | data = node->child; |
| 3618 | } else { |
| 3619 | /* absolute path */ |
| 3620 | for (data = node; data->parent; data = data->parent); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3621 | } |
| 3622 | |
Michal Vasko | bfd98e6 | 2016-09-02 09:50:05 +0200 | [diff] [blame] | 3623 | /* we may still be parsing it and the pointer is not correct yet */ |
| 3624 | if (data->prev) { |
| 3625 | while (data->prev->next) { |
| 3626 | data = data->prev; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 3627 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3628 | } |
| 3629 | } |
| 3630 | |
| 3631 | /* node identifier */ |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3632 | 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] | 3633 | if (rc == -1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3634 | LOGVAL(LYE_INELEM_LEN, LY_VLOG_LYD, node, nam_len, name); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3635 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3636 | goto error; |
| 3637 | } |
| 3638 | |
| 3639 | if (has_predicate) { |
| 3640 | /* we have predicate, so the current results must be lists */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3641 | for (j = 0; j < ret->count;) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3642 | if (ret->node[j]->schema->nodetype == LYS_LIST && |
| 3643 | ((struct lys_node_list *)ret->node[0]->schema)->keys) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3644 | /* leafref is ok, continue check with next leafref */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3645 | ++j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3646 | continue; |
| 3647 | } |
| 3648 | |
| 3649 | /* does not fulfill conditions, remove leafref record */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3650 | unres_data_del(ret, j); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3651 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3652 | if ((rc = resolve_path_predicate_data(path, node, ret, &i))) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 3653 | if (rc == -1) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3654 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYD, node, "leafref", path); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3655 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3656 | goto error; |
| 3657 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3658 | path += i; |
Michal Vasko | d917334 | 2015-08-17 14:35:36 +0200 | [diff] [blame] | 3659 | parsed += i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3660 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3661 | if (!ret->count) { |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3662 | rc = EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3663 | goto error; |
| 3664 | } |
| 3665 | } |
| 3666 | } while (path[0] != '\0'); |
| 3667 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3668 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3669 | |
| 3670 | error: |
| 3671 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3672 | free(ret->node); |
| 3673 | ret->node = NULL; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3674 | ret->count = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3675 | |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3676 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3677 | } |
| 3678 | |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3679 | static int |
| 3680 | resolve_path_arg_schema_valid_dep_flag(const struct lys_node *op_node, const struct lys_node *first_node, int abs_path) |
| 3681 | { |
| 3682 | int dep1, dep2; |
| 3683 | const struct lys_node *node; |
| 3684 | |
| 3685 | if (lys_parent(op_node)) { |
| 3686 | /* inner operation (notif/action) */ |
| 3687 | if (abs_path) { |
| 3688 | return 1; |
| 3689 | } else { |
| 3690 | /* compare depth of both nodes */ |
| 3691 | for (dep1 = 0, node = op_node; lys_parent(node); node = lys_parent(node)); |
| 3692 | for (dep2 = 0, node = first_node; lys_parent(node); node = lys_parent(node)); |
| 3693 | if ((dep2 > dep1) || ((dep2 == dep1) && (op_node != first_node))) { |
| 3694 | return 1; |
| 3695 | } |
| 3696 | } |
| 3697 | } else { |
| 3698 | /* top-level operation (notif/rpc) */ |
| 3699 | if (op_node != first_node) { |
| 3700 | return 1; |
| 3701 | } |
| 3702 | } |
| 3703 | |
| 3704 | return 0; |
| 3705 | } |
| 3706 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3707 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3708 | * @brief Resolve a path (leafref) predicate in JSON schema context. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3709 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3710 | * @param[in] path Path to use. |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3711 | * @param[in] context_node Predicate context node (where the predicate is placed). |
| 3712 | * @param[in] parent Path context node (where the path begins/is placed). |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3713 | * @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] | 3714 | * |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3715 | * @return 0 on forward reference, otherwise the number |
| 3716 | * of characters successfully parsed, |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3717 | * positive on success, negative on failure. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3718 | */ |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3719 | static int |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3720 | 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] | 3721 | struct lys_node *parent, const struct lys_node *op_node) |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3722 | { |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3723 | const struct lys_node *src_node, *dst_node; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3724 | const char *path_key_expr, *source, *sour_pref, *dest, *dest_pref; |
Michal Vasko | f9b35d9 | 2016-10-21 15:19:30 +0200 | [diff] [blame] | 3725 | int pke_len, sour_len, sour_pref_len, dest_len, dest_pref_len, pke_parsed, parsed = 0; |
| 3726 | int has_predicate, dest_parent_times, i, rc, first_iter; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3727 | |
| 3728 | do { |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3729 | 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] | 3730 | &pke_len, &has_predicate)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3731 | 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] | 3732 | return -parsed+i; |
| 3733 | } |
| 3734 | parsed += i; |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3735 | path += i; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3736 | |
Michal Vasko | 5809090 | 2015-08-13 14:04:15 +0200 | [diff] [blame] | 3737 | /* source (must be leaf) */ |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3738 | if (!sour_pref) { |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3739 | sour_pref = context_node->module->name; |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3740 | } |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3741 | 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] | 3742 | LYS_LEAF | LYS_LEAFLIST | LYS_AUGMENT, &src_node); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3743 | if (rc) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3744 | 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] | 3745 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3746 | } |
| 3747 | |
| 3748 | /* destination */ |
Michal Vasko | f9b35d9 | 2016-10-21 15:19:30 +0200 | [diff] [blame] | 3749 | dest_parent_times = 0; |
| 3750 | pke_parsed = 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3751 | if ((i = parse_path_key_expr(path_key_expr, &dest_pref, &dest_pref_len, &dest, &dest_len, |
| 3752 | &dest_parent_times)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3753 | 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] | 3754 | return -parsed; |
| 3755 | } |
| 3756 | pke_parsed += i; |
| 3757 | |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3758 | for (i = 0, dst_node = parent; i < dest_parent_times; ++i) { |
Michal Vasko | fbaead7 | 2016-10-07 10:54:48 +0200 | [diff] [blame] | 3759 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 3760 | * all schema nodes that cannot be instantiated in data tree */ |
| 3761 | for (dst_node = lys_parent(dst_node); |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3762 | 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] | 3763 | dst_node = lys_parent(dst_node)); |
| 3764 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3765 | if (!dst_node) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3766 | 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] | 3767 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3768 | } |
| 3769 | } |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3770 | first_iter = 1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3771 | while (1) { |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3772 | if (!dest_pref) { |
| 3773 | dest_pref = dst_node->module->name; |
| 3774 | } |
| 3775 | 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] | 3776 | LYS_CONTAINER | LYS_LIST | LYS_LEAF | LYS_AUGMENT, &dst_node); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3777 | if (rc) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3778 | 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] | 3779 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3780 | } |
| 3781 | |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3782 | if (first_iter) { |
| 3783 | if (resolve_path_arg_schema_valid_dep_flag(op_node, dst_node, 0)) { |
| 3784 | parent->flags |= LYS_VALID_DEP; |
| 3785 | } |
| 3786 | first_iter = 0; |
| 3787 | } |
| 3788 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3789 | if (pke_len == pke_parsed) { |
| 3790 | break; |
| 3791 | } |
| 3792 | |
| 3793 | if ((i = parse_path_key_expr(path_key_expr+pke_parsed, &dest_pref, &dest_pref_len, &dest, &dest_len, |
| 3794 | &dest_parent_times)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3795 | LOGVAL(LYE_INCHAR, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3796 | (path_key_expr+pke_parsed)[-i], (path_key_expr+pke_parsed)-i); |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3797 | return -parsed; |
| 3798 | } |
| 3799 | pke_parsed += i; |
| 3800 | } |
| 3801 | |
| 3802 | /* check source - dest match */ |
Michal Vasko | 59ad458 | 2016-09-16 13:15:41 +0200 | [diff] [blame] | 3803 | if (dst_node->nodetype != src_node->nodetype) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3804 | 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] | 3805 | LOGVAL(LYE_SPEC, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, "Destination node is not a %s, but a %s.", |
| 3806 | strnodetype(src_node->nodetype), strnodetype(dst_node->nodetype)); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3807 | return -parsed; |
| 3808 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3809 | } while (has_predicate); |
| 3810 | |
| 3811 | return parsed; |
| 3812 | } |
| 3813 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3814 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3815 | * @brief Resolve a path (leafref) in JSON schema context. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3816 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3817 | * @param[in] path Path to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3818 | * @param[in] parent_node Parent of the leafref. |
Radek Krejci | 2f12f85 | 2016-01-08 12:59:57 +0100 | [diff] [blame] | 3819 | * @param[in] parent_tpdf Flag if the parent node is actually typedef, in that case the path |
| 3820 | * has to contain absolute path |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3821 | * @param[out] ret Pointer to the resolved schema node. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3822 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3823 | * @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] | 3824 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3825 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3826 | 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] | 3827 | const struct lys_node **ret) |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3828 | { |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3829 | const struct lys_node *node, *op_node = NULL; |
Radek Krejci | 990af1f | 2016-11-09 13:53:36 +0100 | [diff] [blame] | 3830 | const struct lys_module *mod; |
| 3831 | struct lys_module *mod_start; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3832 | const char *id, *prefix, *name; |
| 3833 | int pref_len, nam_len, parent_times, has_predicate; |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3834 | int i, first_iter, rc; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3835 | |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3836 | first_iter = 1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3837 | parent_times = 0; |
| 3838 | id = path; |
| 3839 | |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3840 | /* find operation schema we are in, if applicable */ |
Michal Vasko | e9914d1 | 2016-10-07 14:32:37 +0200 | [diff] [blame] | 3841 | if (!parent_tpdf) { |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3842 | for (op_node = lys_parent(parent); |
| 3843 | op_node && !(op_node->nodetype & (LYS_ACTION | LYS_NOTIF | LYS_RPC)); |
| 3844 | op_node = lys_parent(op_node)); |
Michal Vasko | e9914d1 | 2016-10-07 14:32:37 +0200 | [diff] [blame] | 3845 | } |
| 3846 | |
Radek Krejci | 990af1f | 2016-11-09 13:53:36 +0100 | [diff] [blame] | 3847 | mod_start = lys_node_module(parent); |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3848 | do { |
Radek Krejci | 990af1f | 2016-11-09 13:53:36 +0100 | [diff] [blame] | 3849 | if ((i = parse_path_arg(mod_start, id, &prefix, &pref_len, &name, &nam_len, &parent_times, &has_predicate)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3850 | 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] | 3851 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3852 | } |
| 3853 | id += i; |
| 3854 | |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3855 | if (first_iter) { |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3856 | if (parent_times == -1) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 3857 | /* resolve prefix of the module */ |
Radek Krejci | 990af1f | 2016-11-09 13:53:36 +0100 | [diff] [blame] | 3858 | mod = prefix ? lys_get_import_module(mod_start, NULL, 0, prefix, pref_len) : mod_start; |
| 3859 | if (!mod) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3860 | LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, |
| 3861 | "leafref", path); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3862 | return EXIT_FAILURE; |
Michal Vasko | 5809090 | 2015-08-13 14:04:15 +0200 | [diff] [blame] | 3863 | } |
Radek Krejci | 990af1f | 2016-11-09 13:53:36 +0100 | [diff] [blame] | 3864 | if (!mod->implemented) { |
Radek Krejci | 2eee5c0 | 2016-12-06 19:18:05 +0100 | [diff] [blame] | 3865 | mod = lys_implemented_module(mod); |
Radek Krejci | 990af1f | 2016-11-09 13:53:36 +0100 | [diff] [blame] | 3866 | if (!mod->implemented) { |
| 3867 | /* make the found module implemented */ |
| 3868 | if (lys_set_implemented(mod)) { |
| 3869 | return EXIT_FAILURE; |
| 3870 | } |
| 3871 | } |
| 3872 | } |
| 3873 | /* get start node */ |
| 3874 | if (!mod->data) { |
| 3875 | LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, |
| 3876 | "leafref", path); |
| 3877 | return EXIT_FAILURE; |
| 3878 | } |
| 3879 | node = mod->data; |
| 3880 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3881 | } else if (parent_times > 0) { |
Radek Krejci | 2f12f85 | 2016-01-08 12:59:57 +0100 | [diff] [blame] | 3882 | if (parent_tpdf) { |
| 3883 | /* 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] | 3884 | LOGVAL(LYE_NORESOLV, 0, NULL, "leafref", path); |
Radek Krejci | 2f12f85 | 2016-01-08 12:59:57 +0100 | [diff] [blame] | 3885 | return -1; |
| 3886 | } |
| 3887 | |
Michal Vasko | 9445808 | 2016-10-07 14:34:36 +0200 | [diff] [blame] | 3888 | /* we are looking for a sibling of a node, node it's parent (that is why parent_times - 1) */ |
| 3889 | for (i = 0, node = parent; i < parent_times - 1; i++) { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 3890 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 3891 | * all schema nodes that cannot be instantiated in data tree */ |
| 3892 | for (node = lys_parent(node); |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3893 | 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] | 3894 | node = lys_parent(node)); |
| 3895 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3896 | if (!node) { |
Michal Vasko | e9914d1 | 2016-10-07 14:32:37 +0200 | [diff] [blame] | 3897 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3898 | return EXIT_FAILURE; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3899 | } |
| 3900 | } |
Radek Krejci | 990af1f | 2016-11-09 13:53:36 +0100 | [diff] [blame] | 3901 | |
| 3902 | /* now we have to check that if we are going into a node from a different module, |
| 3903 | * the module is implemented (so its augments are applied) */ |
| 3904 | mod = prefix ? lys_get_import_module(mod_start, NULL, 0, prefix, pref_len) : mod_start; |
| 3905 | if (!mod) { |
| 3906 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path); |
| 3907 | return EXIT_FAILURE; |
| 3908 | } |
| 3909 | if (!mod->implemented) { |
Radek Krejci | 2eee5c0 | 2016-12-06 19:18:05 +0100 | [diff] [blame] | 3910 | mod = lys_implemented_module(mod); |
Radek Krejci | 990af1f | 2016-11-09 13:53:36 +0100 | [diff] [blame] | 3911 | if (!mod->implemented) { |
| 3912 | /* make the found module implemented */ |
| 3913 | if (lys_set_implemented(mod)) { |
| 3914 | return EXIT_FAILURE; |
| 3915 | } |
| 3916 | } |
| 3917 | } |
Michal Vasko | e01eca5 | 2015-08-13 14:42:02 +0200 | [diff] [blame] | 3918 | } else { |
| 3919 | LOGINT; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3920 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3921 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3922 | } else { |
Radek Krejci | 990af1f | 2016-11-09 13:53:36 +0100 | [diff] [blame] | 3923 | /* we have to first check that the module we are going into is implemented */ |
| 3924 | mod = prefix ? lys_get_import_module(mod_start, NULL, 0, prefix, pref_len) : mod_start; |
| 3925 | if (!mod) { |
| 3926 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path); |
| 3927 | return EXIT_FAILURE; |
| 3928 | } |
| 3929 | if (!mod->implemented) { |
Radek Krejci | 2eee5c0 | 2016-12-06 19:18:05 +0100 | [diff] [blame] | 3930 | mod = lys_implemented_module(mod); |
Radek Krejci | 990af1f | 2016-11-09 13:53:36 +0100 | [diff] [blame] | 3931 | if (!mod->implemented) { |
| 3932 | /* make the found module implemented */ |
| 3933 | if (lys_set_implemented(mod)) { |
| 3934 | return EXIT_FAILURE; |
| 3935 | } |
| 3936 | } |
| 3937 | } |
| 3938 | |
Michal Vasko | 7dc71d0 | 2016-03-15 10:42:28 +0100 | [diff] [blame] | 3939 | /* move down the tree, if possible */ |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 3940 | if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
Michal Vasko | 2f5aceb | 2016-03-22 10:24:14 +0100 | [diff] [blame] | 3941 | 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] | 3942 | return -1; |
| 3943 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3944 | node = node->child; |
Radek Krejci | 43ccc4c | 2016-10-18 20:40:06 +0200 | [diff] [blame] | 3945 | if (!node) { |
| 3946 | LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, |
| 3947 | "leafref", path); |
| 3948 | return EXIT_FAILURE; |
| 3949 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3950 | } |
| 3951 | |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 3952 | if (!prefix) { |
Radek Krejci | 990af1f | 2016-11-09 13:53:36 +0100 | [diff] [blame] | 3953 | prefix = mod_start->name; |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 3954 | } |
| 3955 | |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3956 | 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] | 3957 | if (rc) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3958 | 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] | 3959 | return EXIT_FAILURE; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3960 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3961 | |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3962 | if (first_iter) { |
| 3963 | /* set external dependency flag, we can decide based on the first found node */ |
| 3964 | if (!parent_tpdf && op_node && parent_times && |
| 3965 | resolve_path_arg_schema_valid_dep_flag(op_node, node, (parent_times == -1 ? 1 : 0))) { |
| 3966 | parent->flags |= LYS_VALID_DEP; |
| 3967 | } |
| 3968 | first_iter = 0; |
| 3969 | } |
| 3970 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3971 | if (has_predicate) { |
| 3972 | /* we have predicate, so the current result must be list */ |
| 3973 | if (node->nodetype != LYS_LIST) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3974 | 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] | 3975 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3976 | } |
| 3977 | |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3978 | i = resolve_path_predicate_schema(id, node, parent, op_node); |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 3979 | if (i <= 0) { |
| 3980 | if (i == 0) { |
| 3981 | return EXIT_FAILURE; |
| 3982 | } else { /* i < 0 */ |
| 3983 | return -1; |
| 3984 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3985 | } |
| 3986 | id += i; |
Michal Vasko | f9b35d9 | 2016-10-21 15:19:30 +0200 | [diff] [blame] | 3987 | has_predicate = 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3988 | } |
| 3989 | } while (id[0]); |
| 3990 | |
Michal Vasko | ca91768 | 2016-07-25 11:00:37 +0200 | [diff] [blame] | 3991 | /* 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] | 3992 | if ((node->nodetype != LYS_LEAF) && (node->nodetype != LYS_LEAFLIST)) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3993 | 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] | 3994 | 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] | 3995 | "Leafref target \"%s\" is not a leaf nor a leaf-list.", path); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3996 | return -1; |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 3997 | } |
| 3998 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3999 | /* check status */ |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 4000 | if (lyp_check_status(parent->flags, parent->module, parent->name, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4001 | node->flags, node->module, node->name, node)) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 4002 | return -1; |
| 4003 | } |
| 4004 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4005 | if (ret) { |
| 4006 | *ret = node; |
| 4007 | } |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4008 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4009 | return EXIT_SUCCESS; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 4010 | } |
| 4011 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4012 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 4013 | * @brief Resolve instance-identifier predicate in JSON data format. |
| 4014 | * Does not log. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4015 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4016 | * @param[in] pred Predicate to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4017 | * @param[in,out] node_match Nodes matching the restriction without |
| 4018 | * the predicate. Nodes not satisfying |
| 4019 | * the predicate are removed. |
| 4020 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4021 | * @return Number of characters successfully parsed, |
| 4022 | * positive on success, negative on failure. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4023 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4024 | static int |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 4025 | resolve_predicate(const char *pred, struct unres_data *node_match) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4026 | { |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4027 | /* ... /node[target = value] ... */ |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4028 | struct lyd_node *target; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4029 | const char *model, *name, *value; |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4030 | 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] | 4031 | uint32_t j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4032 | |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4033 | assert(pred && node_match->count); |
| 4034 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4035 | idx = -1; |
| 4036 | parsed = 0; |
| 4037 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4038 | pred_iter = -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4039 | do { |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 4040 | 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] | 4041 | return -parsed+i; |
| 4042 | } |
| 4043 | parsed += i; |
| 4044 | pred += i; |
| 4045 | |
| 4046 | if (isdigit(name[0])) { |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4047 | /* pos */ |
| 4048 | assert(!value); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4049 | idx = atoi(name); |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4050 | } else if (name[0] != '.') { |
| 4051 | /* list keys */ |
| 4052 | if (pred_iter < 0) { |
| 4053 | pred_iter = 1; |
| 4054 | } else { |
| 4055 | ++pred_iter; |
| 4056 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4057 | } |
| 4058 | |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 4059 | for (cur_idx = 1, j = 0; j < node_match->count; ++cur_idx) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4060 | /* target */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4061 | if (name[0] == '.') { |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4062 | /* leaf-list value */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4063 | if (node_match->node[j]->schema->nodetype != LYS_LEAFLIST) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4064 | goto remove_instid; |
| 4065 | } |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4066 | |
| 4067 | target = node_match->node[j]; |
| 4068 | /* check the value */ |
| 4069 | if (strncmp(((struct lyd_node_leaf_list *)target)->value_str, value, val_len) |
| 4070 | || ((struct lyd_node_leaf_list *)target)->value_str[val_len]) { |
| 4071 | goto remove_instid; |
| 4072 | } |
| 4073 | |
| 4074 | } else if (!value) { |
| 4075 | /* keyless list position */ |
| 4076 | if ((node_match->node[j]->schema->nodetype != LYS_LIST) |
| 4077 | || ((struct lys_node_list *)node_match->node[j]->schema)->keys) { |
| 4078 | goto remove_instid; |
| 4079 | } |
| 4080 | |
| 4081 | if (idx != cur_idx) { |
| 4082 | goto remove_instid; |
| 4083 | } |
| 4084 | |
| 4085 | } else { |
| 4086 | /* list key value */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4087 | if (node_match->node[j]->schema->nodetype != LYS_LIST) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4088 | goto remove_instid; |
| 4089 | } |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4090 | |
| 4091 | /* key module must match the list module */ |
| 4092 | if (strncmp(node_match->node[j]->schema->module->name, model, mod_len) |
| 4093 | || node_match->node[j]->schema->module->name[mod_len]) { |
| 4094 | goto remove_instid; |
| 4095 | } |
| 4096 | /* find the key leaf */ |
Michal Vasko | 045182c | 2016-09-09 12:44:07 +0200 | [diff] [blame] | 4097 | 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] | 4098 | if (!target) { |
| 4099 | goto remove_instid; |
| 4100 | } |
| 4101 | if ((struct lys_node_leaf *)target->schema != |
| 4102 | ((struct lys_node_list *)node_match->node[j]->schema)->keys[pred_iter - 1]) { |
| 4103 | goto remove_instid; |
| 4104 | } |
| 4105 | |
| 4106 | /* check the value */ |
| 4107 | if (strncmp(((struct lyd_node_leaf_list *)target)->value_str, value, val_len) |
| 4108 | || ((struct lyd_node_leaf_list *)target)->value_str[val_len]) { |
| 4109 | goto remove_instid; |
| 4110 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4111 | } |
| 4112 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4113 | /* instid is ok, continue check with the next one */ |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4114 | ++j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4115 | continue; |
| 4116 | |
| 4117 | remove_instid: |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4118 | /* does not fulfill conditions, remove instid record */ |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4119 | unres_data_del(node_match, j); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4120 | } |
| 4121 | } while (has_predicate); |
| 4122 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4123 | /* check that all list keys were specified */ |
| 4124 | if ((pred_iter > 0) && node_match->count) { |
Michal Vasko | 045182c | 2016-09-09 12:44:07 +0200 | [diff] [blame] | 4125 | j = 0; |
| 4126 | while (j < node_match->count) { |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4127 | assert(node_match->node[j]->schema->nodetype == LYS_LIST); |
| 4128 | if (pred_iter < ((struct lys_node_list *)node_match->node[j]->schema)->keys_size) { |
| 4129 | /* not enough predicates, just remove the list instance */ |
| 4130 | unres_data_del(node_match, j); |
Michal Vasko | 045182c | 2016-09-09 12:44:07 +0200 | [diff] [blame] | 4131 | } else { |
| 4132 | ++j; |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4133 | } |
| 4134 | } |
| 4135 | |
| 4136 | if (!node_match->count) { |
| 4137 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier is missing some list keys."); |
| 4138 | } |
| 4139 | } |
| 4140 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4141 | return parsed; |
| 4142 | } |
| 4143 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4144 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 4145 | * @brief Resolve instance-identifier in JSON data format. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4146 | * |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 4147 | * @param[in] data Data node where the path is used |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4148 | * @param[in] path Instance-identifier node value. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4149 | * |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4150 | * @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] | 4151 | */ |
Radek Krejci | 1899d6a | 2016-11-03 13:48:07 +0100 | [diff] [blame] | 4152 | struct lyd_node * |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4153 | resolve_instid(struct lyd_node *data, const char *path) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4154 | { |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4155 | int i = 0, j; |
| 4156 | struct lyd_node *result = NULL; |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4157 | const struct lys_module *mod; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4158 | struct ly_ctx *ctx = data->schema->module->ctx; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4159 | const char *model, *name; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4160 | char *str; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4161 | int mod_len, name_len, has_predicate; |
| 4162 | struct unres_data node_match; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4163 | |
| 4164 | memset(&node_match, 0, sizeof node_match); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4165 | |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4166 | /* we need root to resolve absolute path */ |
| 4167 | for (; data->parent; data = data->parent); |
Michal Vasko | db9323c | 2015-10-16 10:32:44 +0200 | [diff] [blame] | 4168 | /* we're still parsing it and the pointer is not correct yet */ |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 4169 | if (data->prev) { |
| 4170 | for (; data->prev->next; data = data->prev); |
| 4171 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4172 | |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4173 | /* search for the instance node */ |
| 4174 | while (path[i]) { |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 4175 | 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] | 4176 | if (j <= 0) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4177 | 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] | 4178 | goto error; |
| 4179 | } |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4180 | i += j; |
Michal Vasko | b387c48 | 2015-08-12 09:32:59 +0200 | [diff] [blame] | 4181 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4182 | str = strndup(model, mod_len); |
| 4183 | if (!str) { |
| 4184 | LOGMEM; |
| 4185 | goto error; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4186 | } |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4187 | mod = ly_ctx_get_module(ctx, str, NULL); |
| 4188 | free(str); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4189 | |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4190 | if (resolve_data(mod, name, name_len, data, &node_match)) { |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4191 | /* no instance exists */ |
| 4192 | return NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4193 | } |
| 4194 | |
| 4195 | if (has_predicate) { |
| 4196 | /* 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] | 4197 | j = resolve_predicate(&path[i], &node_match); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4198 | if (j < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4199 | LOGVAL(LYE_INPRED, LY_VLOG_LYD, data, &path[i-j]); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4200 | goto error; |
| 4201 | } |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4202 | i += j; |
Michal Vasko | b387c48 | 2015-08-12 09:32:59 +0200 | [diff] [blame] | 4203 | |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4204 | if (!node_match.count) { |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4205 | /* no instance exists */ |
| 4206 | return NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4207 | } |
| 4208 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4209 | } |
| 4210 | |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4211 | if (!node_match.count) { |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4212 | /* no instance exists */ |
| 4213 | return NULL; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 4214 | } else if (node_match.count > 1) { |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4215 | /* instance identifier must resolve to a single node */ |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4216 | LOGVAL(LYE_TOOMANY, LY_VLOG_LYD, data, path, "data tree"); |
Michal Vasko | d6adbaa | 2016-04-11 11:01:09 +0200 | [diff] [blame] | 4217 | goto error; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4218 | } else { |
| 4219 | /* we have required result, remember it and cleanup */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4220 | result = node_match.node[0]; |
| 4221 | free(node_match.node); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4222 | return result; |
| 4223 | } |
| 4224 | |
| 4225 | error: |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4226 | /* cleanup */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4227 | free(node_match.node); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 4228 | return NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4229 | } |
| 4230 | |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4231 | int |
| 4232 | lys_check_xpath(struct lys_node *node, int check_place) |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4233 | { |
| 4234 | struct lys_node *parent, *elem; |
| 4235 | struct lyxp_set set; |
| 4236 | uint32_t i; |
| 4237 | int rc; |
| 4238 | |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4239 | if (check_place) { |
| 4240 | parent = node; |
| 4241 | while (parent) { |
| 4242 | if (parent->nodetype == LYS_GROUPING) { |
| 4243 | /* unresolved grouping, skip for now (will be checked later) */ |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4244 | return EXIT_SUCCESS; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4245 | } |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4246 | if (parent->nodetype == LYS_AUGMENT) { |
| 4247 | if (!((struct lys_node_augment *)parent)->target) { |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 4248 | /* unresolved augment */ |
| 4249 | if (parent->module->implemented) { |
| 4250 | /* skip for now (will be checked later) */ |
| 4251 | return EXIT_FAILURE; |
| 4252 | } else { |
| 4253 | /* not implemented augment, skip resolving */ |
| 4254 | return EXIT_SUCCESS; |
| 4255 | } |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4256 | } else { |
| 4257 | parent = ((struct lys_node_augment *)parent)->target; |
| 4258 | continue; |
| 4259 | } |
| 4260 | } |
| 4261 | parent = parent->parent; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4262 | } |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4263 | } |
| 4264 | |
| 4265 | rc = lyxp_node_atomize(node, &set); |
| 4266 | if (rc) { |
| 4267 | return rc; |
| 4268 | } |
| 4269 | |
| 4270 | for (parent = node; parent && !(parent->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); parent = lys_parent(parent)); |
| 4271 | |
| 4272 | for (i = 0; i < set.used; ++i) { |
| 4273 | /* skip roots'n'stuff */ |
| 4274 | if (set.val.snodes[i].type == LYXP_NODE_ELEM) { |
| 4275 | /* XPath expression cannot reference "lower" status than the node that has the definition */ |
| 4276 | if (lyp_check_status(node->flags, lys_node_module(node), node->name, set.val.snodes[i].snode->flags, |
| 4277 | lys_node_module(set.val.snodes[i].snode), set.val.snodes[i].snode->name, node)) { |
| 4278 | return -1; |
| 4279 | } |
| 4280 | |
| 4281 | if (parent) { |
| 4282 | for (elem = set.val.snodes[i].snode; elem && (elem != parent); elem = lys_parent(elem)); |
| 4283 | if (!elem) { |
| 4284 | /* not in node's RPC or notification subtree, set the flag */ |
| 4285 | node->flags |= LYS_VALID_DEP; |
| 4286 | break; |
| 4287 | } |
| 4288 | } |
| 4289 | } |
| 4290 | } |
| 4291 | |
| 4292 | free(set.val.snodes); |
| 4293 | return EXIT_SUCCESS; |
| 4294 | } |
| 4295 | |
Radek Krejci | f71f48f | 2016-10-25 16:37:24 +0200 | [diff] [blame] | 4296 | static int |
| 4297 | check_leafref_config(struct lys_node_leaf *leaf, struct lys_type *type) |
| 4298 | { |
| 4299 | int i; |
| 4300 | |
| 4301 | if (type->base == LY_TYPE_LEAFREF) { |
| 4302 | if ((leaf->flags & LYS_CONFIG_W) && type->info.lref.target && (type->info.lref.target->flags & LYS_CONFIG_R)) { |
| 4303 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, leaf, "The %s is config but refers to a non-config %s.", |
| 4304 | strnodetype(leaf->nodetype), strnodetype(type->info.lref.target->nodetype)); |
| 4305 | return -1; |
| 4306 | } |
| 4307 | /* we can skip the test in case the leafref is not yet resolved. In that case the test is done in the time |
| 4308 | * of leafref resolving (lys_leaf_add_leafref_target()) */ |
| 4309 | } else if (type->base == LY_TYPE_UNION) { |
| 4310 | for (i = 0; i < type->info.uni.count; i++) { |
| 4311 | if (check_leafref_config(leaf, &type->info.uni.types[i])) { |
| 4312 | return -1; |
| 4313 | } |
| 4314 | } |
| 4315 | } |
| 4316 | return 0; |
| 4317 | } |
| 4318 | |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4319 | /** |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4320 | * @brief Passes config flag down to children, skips nodes without config flags. |
| 4321 | * Does not log. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4322 | * |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4323 | * @param[in] node Siblings and their children to have flags changed. |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4324 | * @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] | 4325 | * @param[in] flags Flags to assign to all the nodes. |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 4326 | * @param[in,out] unres List of unresolved items. |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4327 | * |
| 4328 | * @return 0 on success, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4329 | */ |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4330 | static int |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 4331 | inherit_config_flag(struct lys_node *node, int flags, int clear, struct unres_schema *unres) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4332 | { |
Radek Krejci | f71f48f | 2016-10-25 16:37:24 +0200 | [diff] [blame] | 4333 | struct lys_node_leaf *leaf; |
| 4334 | |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4335 | assert(!(flags ^ (flags & LYS_CONFIG_MASK))); |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4336 | LY_TREE_FOR(node, node) { |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 4337 | if (lys_has_xpath(node) && unres_schema_add_node(node->module, unres, node, UNRES_XPATH, NULL) == -1) { |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4338 | return -1; |
| 4339 | } |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4340 | if (clear) { |
| 4341 | node->flags &= ~LYS_CONFIG_MASK; |
Michal Vasko | c2a8d36 | 2016-09-29 08:50:13 +0200 | [diff] [blame] | 4342 | node->flags &= ~LYS_CONFIG_SET; |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4343 | } else { |
| 4344 | if (node->flags & LYS_CONFIG_SET) { |
| 4345 | /* skip nodes with an explicit config value */ |
| 4346 | if ((flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) { |
| 4347 | LOGVAL(LYE_INARG, LY_VLOG_LYS, node, "true", "config"); |
| 4348 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, node, "State nodes cannot have configuration nodes as children."); |
| 4349 | return -1; |
| 4350 | } |
| 4351 | continue; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4352 | } |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4353 | |
| 4354 | if (!(node->nodetype & (LYS_USES | LYS_GROUPING))) { |
| 4355 | node->flags = (node->flags & ~LYS_CONFIG_MASK) | flags; |
| 4356 | /* check that configuration lists have keys */ |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4357 | if ((node->nodetype == LYS_LIST) && (node->flags & LYS_CONFIG_W) |
| 4358 | && !((struct lys_node_list *)node)->keys_size) { |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4359 | LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_LYS, node, "key", "list"); |
| 4360 | return -1; |
| 4361 | } |
| 4362 | } |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4363 | } |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4364 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) { |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 4365 | if (inherit_config_flag(node->child, flags, clear, unres)) { |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4366 | return -1; |
| 4367 | } |
Radek Krejci | f71f48f | 2016-10-25 16:37:24 +0200 | [diff] [blame] | 4368 | } else if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 4369 | leaf = (struct lys_node_leaf *)node; |
| 4370 | if (check_leafref_config(leaf, &leaf->type)) { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 4371 | return -1; |
| 4372 | } |
| 4373 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4374 | } |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4375 | |
| 4376 | return 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4377 | } |
| 4378 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4379 | /** |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 4380 | * @brief Resolve augment target. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4381 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4382 | * @param[in] aug Augment to use. |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 4383 | * @param[in] siblings Nodes where to start the search in. If set, uses augment, if not, standalone augment. |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 4384 | * @param[in,out] unres List of unresolved items. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4385 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4386 | * @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] | 4387 | */ |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 4388 | static int |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 4389 | resolve_augment(struct lys_node_augment *aug, struct lys_node *siblings, struct unres_schema *unres) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4390 | { |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4391 | int rc, clear_config; |
Michal Vasko | 1d87a92 | 2015-08-21 12:57:16 +0200 | [diff] [blame] | 4392 | struct lys_node *sub; |
Pavol Vican | 4731993 | 2016-08-29 09:14:47 +0200 | [diff] [blame] | 4393 | const struct lys_node *aug_target, *parent; |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4394 | struct lys_module *mod; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4395 | |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4396 | assert(aug && !aug->target); |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 4397 | mod = lys_main_module(aug->module); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4398 | |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4399 | /* resolve target node */ |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 4400 | 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] | 4401 | if (rc == -1) { |
| 4402 | return -1; |
Radek Krejci | 5fb4c38 | 2016-11-28 09:21:42 +0100 | [diff] [blame] | 4403 | } else if (rc > 0) { |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4404 | LOGVAL(LYE_INCHAR, LY_VLOG_LYS, aug, aug->target_name[rc - 1], &aug->target_name[rc - 1]); |
| 4405 | return -1; |
Radek Krejci | 5fb4c38 | 2016-11-28 09:21:42 +0100 | [diff] [blame] | 4406 | } else if (rc == 0 && aug->target) { |
| 4407 | /* augment was resolved as a side effect of setting module implemented when |
| 4408 | * resolving augment schema nodeid, so we are done here */ |
| 4409 | return 0; |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4410 | } |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 4411 | if (!aug_target && mod->implemented) { |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4412 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, aug, "augment", aug->target_name); |
| 4413 | return EXIT_FAILURE; |
| 4414 | } |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4415 | /* check that we want to connect augment into its target */ |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4416 | if (!mod->implemented) { |
| 4417 | /* it must be augment only to the same module, |
| 4418 | * otherwise we do not apply augment in not-implemented |
| 4419 | * module. If the module is set to be implemented in future, |
| 4420 | * the augment is being resolved and checked again */ |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 4421 | if (!aug_target) { |
| 4422 | /* target was not even resolved */ |
| 4423 | return EXIT_SUCCESS; |
| 4424 | } |
| 4425 | /* target was resolved, but it may refer another module */ |
| 4426 | for (sub = (struct lys_node *)aug_target; sub; sub = lys_parent(sub)) { |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4427 | if (lys_node_module(sub) != mod) { |
| 4428 | /* this is not an implemented module and the augment |
| 4429 | * target some other module, so avoid its connecting |
| 4430 | * to the target */ |
| 4431 | return EXIT_SUCCESS; |
| 4432 | } |
| 4433 | } |
| 4434 | } |
| 4435 | |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4436 | if (!aug->child) { |
| 4437 | /* nothing to do */ |
| 4438 | LOGWRN("Augment \"%s\" without children.", aug->target_name); |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4439 | goto success; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4440 | } |
| 4441 | |
Michal Vasko | d58d596 | 2016-03-02 14:29:41 +0100 | [diff] [blame] | 4442 | /* check for mandatory nodes - if the target node is in another module |
| 4443 | * the added nodes cannot be mandatory |
| 4444 | */ |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4445 | 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] | 4446 | && (rc = lyp_check_mandatory_augment(aug, aug_target))) { |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 4447 | return rc; |
Michal Vasko | d58d596 | 2016-03-02 14:29:41 +0100 | [diff] [blame] | 4448 | } |
| 4449 | |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4450 | /* check augment target type and then augment nodes type */ |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4451 | 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] | 4452 | LY_TREE_FOR(aug->child, sub) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4453 | 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] | 4454 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment"); |
| 4455 | 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] | 4456 | strnodetype(aug_target->nodetype), strnodetype(sub->nodetype)); |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4457 | return -1; |
| 4458 | } |
| 4459 | } |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4460 | } else if (aug_target->nodetype == LYS_CHOICE) { |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4461 | LY_TREE_FOR(aug->child, sub) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4462 | 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] | 4463 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment"); |
| 4464 | 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] | 4465 | strnodetype(aug_target->nodetype), strnodetype(sub->nodetype)); |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4466 | return -1; |
| 4467 | } |
| 4468 | } |
| 4469 | } else { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4470 | LOGVAL(LYE_INARG, LY_VLOG_LYS, aug, aug->target_name, "target-node"); |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4471 | 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] | 4472 | return -1; |
| 4473 | } |
| 4474 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 4475 | /* check identifier uniqueness as in lys_node_addchild() */ |
Michal Vasko | 1d87a92 | 2015-08-21 12:57:16 +0200 | [diff] [blame] | 4476 | LY_TREE_FOR(aug->child, sub) { |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4477 | if (lys_check_id(sub, (struct lys_node *)aug_target, NULL)) { |
Michal Vasko | 3e6665f | 2015-08-17 14:00:38 +0200 | [diff] [blame] | 4478 | return -1; |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 4479 | } |
| 4480 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4481 | |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4482 | /* finally reconnect augmenting data into the target - add them to the target child list, |
| 4483 | * by setting aug->target we know the augment is fully resolved now */ |
| 4484 | aug->target = (struct lys_node *)aug_target; |
| 4485 | if (aug->target->child) { |
| 4486 | sub = aug->target->child->prev; /* remember current target's last node */ |
| 4487 | sub->next = aug->child; /* connect augmenting data after target's last node */ |
| 4488 | aug->target->child->prev = aug->child->prev; /* new target's last node is last augmenting node */ |
| 4489 | aug->child->prev = sub; /* finish connecting of both child lists */ |
| 4490 | } else { |
| 4491 | aug->target->child = aug->child; |
| 4492 | } |
| 4493 | |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4494 | /* inherit config information from actual parent */ |
| 4495 | for(parent = aug_target; parent && !(parent->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT | LYS_RPC)); parent = lys_parent(parent)); |
| 4496 | clear_config = (parent) ? 1 : 0; |
| 4497 | LY_TREE_FOR(aug->child, sub) { |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 4498 | if (inherit_config_flag(sub, aug_target->flags & LYS_CONFIG_MASK, clear_config, unres)) { |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4499 | return -1; |
| 4500 | } |
| 4501 | } |
| 4502 | |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4503 | success: |
| 4504 | if (mod->implemented) { |
| 4505 | /* make target modules also implemented */ |
| 4506 | for (sub = aug->target; sub; sub = lys_parent(sub)) { |
| 4507 | if (lys_set_implemented(sub->module)) { |
| 4508 | return -1; |
| 4509 | } |
| 4510 | } |
| 4511 | } |
| 4512 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4513 | return EXIT_SUCCESS; |
| 4514 | } |
| 4515 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4516 | /** |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4517 | * @brief Resolve (find) choice default case. Does not log. |
| 4518 | * |
| 4519 | * @param[in] choic Choice to use. |
| 4520 | * @param[in] dflt Name of the default case. |
| 4521 | * |
| 4522 | * @return Pointer to the default node or NULL. |
| 4523 | */ |
| 4524 | static struct lys_node * |
| 4525 | resolve_choice_dflt(struct lys_node_choice *choic, const char *dflt) |
| 4526 | { |
| 4527 | struct lys_node *child, *ret; |
| 4528 | |
| 4529 | LY_TREE_FOR(choic->child, child) { |
| 4530 | if (child->nodetype == LYS_USES) { |
| 4531 | ret = resolve_choice_dflt((struct lys_node_choice *)child, dflt); |
| 4532 | if (ret) { |
| 4533 | return ret; |
| 4534 | } |
| 4535 | } |
| 4536 | |
| 4537 | 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] | 4538 | | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CHOICE))) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4539 | return child; |
| 4540 | } |
| 4541 | } |
| 4542 | |
| 4543 | return NULL; |
| 4544 | } |
| 4545 | |
| 4546 | /** |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4547 | * @brief Resolve uses, apply augments, refines. Logs directly. |
| 4548 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4549 | * @param[in] uses Uses to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4550 | * @param[in,out] unres List of unresolved items. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4551 | * |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 4552 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4553 | */ |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 4554 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4555 | resolve_uses(struct lys_node_uses *uses, struct unres_schema *unres) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4556 | { |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4557 | struct ly_ctx *ctx = uses->module->ctx; /* shortcut */ |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4558 | struct lys_node *node = NULL, *next, *iter, **refine_nodes = NULL; |
Pavol Vican | 55abd33 | 2016-07-12 15:54:49 +0200 | [diff] [blame] | 4559 | struct lys_node *node_aux, *parent, *tmp; |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4560 | struct lys_node_leaflist *llist; |
| 4561 | struct lys_node_leaf *leaf; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 4562 | struct lys_refine *rfn; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4563 | struct lys_restr *must, **old_must; |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4564 | struct lys_iffeature *iff, **old_iff; |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4565 | int i, j, k, rc; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4566 | uint8_t size, *old_size; |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4567 | unsigned int usize, usize1, usize2; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4568 | |
Michal Vasko | 71e1aa8 | 2015-08-12 12:17:51 +0200 | [diff] [blame] | 4569 | assert(uses->grp); |
Michal Vasko | e770851 | 2016-03-11 10:26:55 +0100 | [diff] [blame] | 4570 | /* HACK just check that the grouping is resolved */ |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 4571 | assert(!uses->grp->nacm); |
Michal Vasko | 71e1aa8 | 2015-08-12 12:17:51 +0200 | [diff] [blame] | 4572 | |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4573 | if (!uses->grp->child) { |
| 4574 | /* grouping without children, warning was already displayed */ |
| 4575 | return EXIT_SUCCESS; |
| 4576 | } |
| 4577 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4578 | /* copy the data nodes from grouping into the uses context */ |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 4579 | LY_TREE_FOR(uses->grp->child, node_aux) { |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4580 | 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] | 4581 | if (!node) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4582 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, uses->grp->name, "uses"); |
| 4583 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, "Copying data from grouping failed."); |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4584 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4585 | } |
Pavol Vican | 55abd33 | 2016-07-12 15:54:49 +0200 | [diff] [blame] | 4586 | /* test the name of siblings */ |
| 4587 | 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] | 4588 | 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] | 4589 | goto fail; |
Pavol Vican | 55abd33 | 2016-07-12 15:54:49 +0200 | [diff] [blame] | 4590 | } |
| 4591 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4592 | } |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4593 | |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 4594 | /* we managed to copy the grouping, the rest must be possible to resolve */ |
| 4595 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4596 | if (uses->refine_size) { |
| 4597 | refine_nodes = malloc(uses->refine_size * sizeof *refine_nodes); |
| 4598 | if (!refine_nodes) { |
| 4599 | LOGMEM; |
| 4600 | goto fail; |
| 4601 | } |
| 4602 | } |
| 4603 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4604 | /* apply refines */ |
| 4605 | for (i = 0; i < uses->refine_size; i++) { |
| 4606 | rfn = &uses->refine[i]; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 4607 | 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] | 4608 | 1, 0, (const struct lys_node **)&node); |
Michal Vasko | 9bb061b | 2016-02-12 11:00:19 +0100 | [diff] [blame] | 4609 | if (rc || !node) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4610 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine"); |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4611 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4612 | } |
| 4613 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4614 | if (rfn->target_type && !(node->nodetype & rfn->target_type)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4615 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine"); |
| 4616 | 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] | 4617 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4618 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4619 | refine_nodes[i] = node; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4620 | |
| 4621 | /* description on any nodetype */ |
| 4622 | if (rfn->dsc) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4623 | lydict_remove(ctx, node->dsc); |
| 4624 | node->dsc = lydict_insert(ctx, rfn->dsc, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4625 | } |
| 4626 | |
| 4627 | /* reference on any nodetype */ |
| 4628 | if (rfn->ref) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4629 | lydict_remove(ctx, node->ref); |
| 4630 | node->ref = lydict_insert(ctx, rfn->ref, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4631 | } |
| 4632 | |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4633 | /* config on any nodetype, |
| 4634 | * in case of notification or rpc/action, the config is not applicable (there is no config status) */ |
| 4635 | if ((rfn->flags & LYS_CONFIG_MASK) && (node->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4636 | node->flags &= ~LYS_CONFIG_MASK; |
| 4637 | node->flags |= (rfn->flags & LYS_CONFIG_MASK); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4638 | } |
| 4639 | |
| 4640 | /* default value ... */ |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4641 | if (rfn->dflt_size) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4642 | if (node->nodetype == LYS_LEAF) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4643 | /* leaf */ |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4644 | leaf = (struct lys_node_leaf *)node; |
| 4645 | |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 4646 | /* replace default value */ |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4647 | lydict_remove(ctx, leaf->dflt); |
| 4648 | leaf->dflt = lydict_insert(ctx, rfn->dflt[0], 0); |
| 4649 | |
| 4650 | /* check the default value */ |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 4651 | if (unres_schema_add_node(leaf->module, unres, &leaf->type, UNRES_TYPE_DFLT, |
| 4652 | (struct lys_node *)(&leaf->dflt)) == -1) { |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4653 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4654 | } |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4655 | } else if (node->nodetype == LYS_LEAFLIST) { |
| 4656 | /* leaf-list */ |
| 4657 | llist = (struct lys_node_leaflist *)node; |
| 4658 | |
| 4659 | /* remove complete set of defaults in target */ |
| 4660 | for (i = 0; i < llist->dflt_size; i++) { |
| 4661 | lydict_remove(ctx, llist->dflt[i]); |
| 4662 | } |
| 4663 | free(llist->dflt); |
| 4664 | |
| 4665 | /* copy the default set from refine */ |
| 4666 | llist->dflt_size = rfn->dflt_size; |
| 4667 | llist->dflt = malloc(llist->dflt_size * sizeof *llist->dflt); |
| 4668 | for (i = 0; i < llist->dflt_size; i++) { |
| 4669 | llist->dflt[i] = lydict_insert(ctx, rfn->dflt[i], 0); |
| 4670 | } |
| 4671 | |
| 4672 | /* check default value */ |
| 4673 | for (i = 0; i < llist->dflt_size; i++) { |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 4674 | if (unres_schema_add_node(llist->module, unres, &llist->type, UNRES_TYPE_DFLT, |
| 4675 | (struct lys_node *)(&llist->dflt[i])) == -1) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4676 | goto fail; |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4677 | } |
| 4678 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4679 | } |
| 4680 | } |
| 4681 | |
| 4682 | /* mandatory on leaf, anyxml or choice */ |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 4683 | if (rfn->flags & LYS_MAND_MASK) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4684 | if (node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_CHOICE)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4685 | /* remove current value */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4686 | node->flags &= ~LYS_MAND_MASK; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4687 | |
| 4688 | /* set new value */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4689 | node->flags |= (rfn->flags & LYS_MAND_MASK); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4690 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4691 | if (rfn->flags & LYS_MAND_TRUE) { |
| 4692 | /* check if node has default value */ |
| 4693 | if ((node->nodetype & LYS_LEAF) && ((struct lys_node_leaf *)node)->dflt) { |
| 4694 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The \"mandatory\" statement is forbidden on leaf with \"default\"."); |
| 4695 | goto fail; |
| 4696 | } |
| 4697 | if ((node->nodetype & LYS_CHOICE) && ((struct lys_node_choice *)node)->dflt) { |
| 4698 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The \"mandatory\" statement is forbidden on choices with \"default\"."); |
| 4699 | goto fail; |
| 4700 | } |
| 4701 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4702 | } |
| 4703 | |
| 4704 | /* presence on container */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4705 | if ((node->nodetype & LYS_CONTAINER) && rfn->mod.presence) { |
| 4706 | lydict_remove(ctx, ((struct lys_node_container *)node)->presence); |
| 4707 | ((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] | 4708 | } |
| 4709 | |
| 4710 | /* min/max-elements on list or leaf-list */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4711 | if (node->nodetype == LYS_LIST) { |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 4712 | if (rfn->flags & LYS_RFN_MINSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4713 | ((struct lys_node_list *)node)->min = rfn->mod.list.min; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4714 | } |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 4715 | if (rfn->flags & LYS_RFN_MAXSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4716 | ((struct lys_node_list *)node)->max = rfn->mod.list.max; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4717 | } |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4718 | } else if (node->nodetype == LYS_LEAFLIST) { |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 4719 | if (rfn->flags & LYS_RFN_MINSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4720 | ((struct lys_node_leaflist *)node)->min = rfn->mod.list.min; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4721 | } |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 4722 | if (rfn->flags & LYS_RFN_MAXSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4723 | ((struct lys_node_leaflist *)node)->max = rfn->mod.list.max; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4724 | } |
| 4725 | } |
| 4726 | |
| 4727 | /* must in leaf, leaf-list, list, container or anyxml */ |
| 4728 | if (rfn->must_size) { |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4729 | switch (node->nodetype) { |
| 4730 | case LYS_LEAF: |
| 4731 | old_size = &((struct lys_node_leaf *)node)->must_size; |
| 4732 | old_must = &((struct lys_node_leaf *)node)->must; |
| 4733 | break; |
| 4734 | case LYS_LEAFLIST: |
| 4735 | old_size = &((struct lys_node_leaflist *)node)->must_size; |
| 4736 | old_must = &((struct lys_node_leaflist *)node)->must; |
| 4737 | break; |
| 4738 | case LYS_LIST: |
| 4739 | old_size = &((struct lys_node_list *)node)->must_size; |
| 4740 | old_must = &((struct lys_node_list *)node)->must; |
| 4741 | break; |
| 4742 | case LYS_CONTAINER: |
| 4743 | old_size = &((struct lys_node_container *)node)->must_size; |
| 4744 | old_must = &((struct lys_node_container *)node)->must; |
| 4745 | break; |
| 4746 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4747 | case LYS_ANYDATA: |
| 4748 | old_size = &((struct lys_node_anydata *)node)->must_size; |
| 4749 | old_must = &((struct lys_node_anydata *)node)->must; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4750 | break; |
| 4751 | default: |
| 4752 | LOGINT; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4753 | goto fail; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4754 | } |
| 4755 | |
| 4756 | size = *old_size + rfn->must_size; |
| 4757 | must = realloc(*old_must, size * sizeof *rfn->must); |
| 4758 | if (!must) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4759 | LOGMEM; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4760 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4761 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4762 | for (k = 0, j = *old_size; k < rfn->must_size; k++, j++) { |
| 4763 | must[j].expr = lydict_insert(ctx, rfn->must[k].expr, 0); |
| 4764 | must[j].dsc = lydict_insert(ctx, rfn->must[k].dsc, 0); |
| 4765 | must[j].ref = lydict_insert(ctx, rfn->must[k].ref, 0); |
| 4766 | must[j].eapptag = lydict_insert(ctx, rfn->must[k].eapptag, 0); |
| 4767 | must[j].emsg = lydict_insert(ctx, rfn->must[k].emsg, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4768 | } |
| 4769 | |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4770 | *old_must = must; |
| 4771 | *old_size = size; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 4772 | |
| 4773 | /* check XPath dependencies again */ |
| 4774 | if (unres_schema_add_node(node->module, unres, node, UNRES_XPATH, NULL) == -1) { |
| 4775 | goto fail; |
| 4776 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4777 | } |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4778 | |
| 4779 | /* if-feature in leaf, leaf-list, list, container or anyxml */ |
| 4780 | if (rfn->iffeature_size) { |
| 4781 | old_size = &node->iffeature_size; |
| 4782 | old_iff = &node->iffeature; |
| 4783 | |
| 4784 | size = *old_size + rfn->iffeature_size; |
| 4785 | iff = realloc(*old_iff, size * sizeof *rfn->iffeature); |
| 4786 | if (!iff) { |
| 4787 | LOGMEM; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4788 | goto fail; |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4789 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4790 | for (k = 0, j = *old_size; k < rfn->iffeature_size; k++, j++) { |
| 4791 | resolve_iffeature_getsizes(&rfn->iffeature[k], &usize1, &usize2); |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4792 | if (usize1) { |
| 4793 | /* there is something to duplicate */ |
| 4794 | /* duplicate compiled expression */ |
| 4795 | usize = (usize1 / 4) + (usize1 % 4) ? 1 : 0; |
| 4796 | iff[j].expr = malloc(usize * sizeof *iff[j].expr); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4797 | 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] | 4798 | |
| 4799 | /* duplicate list of feature pointers */ |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4800 | iff[j].features = malloc(usize2 * sizeof *iff[k].features); |
| 4801 | 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] | 4802 | } |
| 4803 | } |
| 4804 | |
| 4805 | *old_iff = iff; |
| 4806 | *old_size = size; |
| 4807 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4808 | } |
| 4809 | |
| 4810 | /* apply augments */ |
| 4811 | for (i = 0; i < uses->augment_size; i++) { |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 4812 | rc = resolve_augment(&uses->augment[i], uses->child, unres); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4813 | if (rc) { |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4814 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4815 | } |
| 4816 | } |
| 4817 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4818 | /* check refines */ |
| 4819 | for (i = 0; i < uses->refine_size; i++) { |
| 4820 | node = refine_nodes[i]; |
| 4821 | rfn = &uses->refine[i]; |
| 4822 | |
| 4823 | /* config on any nodetype */ |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4824 | if ((rfn->flags & LYS_CONFIG_MASK) && (node->flags & LYS_CONFIG_MASK)) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4825 | 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] | 4826 | if (parent && parent->nodetype != LYS_GROUPING && (parent->flags & LYS_CONFIG_MASK) && |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4827 | ((parent->flags & LYS_CONFIG_MASK) != (rfn->flags & LYS_CONFIG_MASK)) && |
| 4828 | (rfn->flags & LYS_CONFIG_W)) { |
| 4829 | /* setting config true under config false is prohibited */ |
| 4830 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, "config", "refine"); |
| 4831 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, |
| 4832 | "changing config from 'false' to 'true' is prohibited while " |
| 4833 | "the target's parent is still config 'false'."); |
| 4834 | goto fail; |
| 4835 | } |
| 4836 | |
| 4837 | /* inherit config change to the target children */ |
| 4838 | LY_TREE_DFS_BEGIN(node->child, next, iter) { |
| 4839 | if (rfn->flags & LYS_CONFIG_W) { |
| 4840 | if (iter->flags & LYS_CONFIG_SET) { |
| 4841 | /* config is set explicitely, go to next sibling */ |
| 4842 | next = NULL; |
| 4843 | goto nextsibling; |
| 4844 | } |
| 4845 | } else { /* LYS_CONFIG_R */ |
| 4846 | if ((iter->flags & LYS_CONFIG_SET) && (iter->flags & LYS_CONFIG_W)) { |
| 4847 | /* error - we would have config data under status data */ |
| 4848 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, "config", "refine"); |
| 4849 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, |
| 4850 | "changing config from 'true' to 'false' is prohibited while the target " |
| 4851 | "has still a children with explicit config 'true'."); |
| 4852 | goto fail; |
| 4853 | } |
| 4854 | } |
| 4855 | /* change config */ |
| 4856 | iter->flags &= ~LYS_CONFIG_MASK; |
| 4857 | iter->flags |= (rfn->flags & LYS_CONFIG_MASK); |
| 4858 | |
| 4859 | /* select next iter - modified LY_TREE_DFS_END */ |
| 4860 | if (iter->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
| 4861 | next = NULL; |
| 4862 | } else { |
| 4863 | next = iter->child; |
| 4864 | } |
| 4865 | nextsibling: |
| 4866 | if (!next) { |
| 4867 | /* try siblings */ |
| 4868 | next = iter->next; |
| 4869 | } |
| 4870 | while (!next) { |
| 4871 | /* parent is already processed, go to its sibling */ |
| 4872 | iter = lys_parent(iter); |
| 4873 | |
| 4874 | /* no siblings, go back through parents */ |
| 4875 | if (iter == node) { |
| 4876 | /* we are done, no next element to process */ |
| 4877 | break; |
| 4878 | } |
| 4879 | next = iter->next; |
| 4880 | } |
| 4881 | } |
| 4882 | } |
| 4883 | |
| 4884 | /* default value */ |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 4885 | if (rfn->dflt_size) { |
| 4886 | if (node->nodetype == LYS_CHOICE) { |
| 4887 | /* choice */ |
| 4888 | ((struct lys_node_choice *)node)->dflt = resolve_choice_dflt((struct lys_node_choice *)node, |
| 4889 | rfn->dflt[0]); |
| 4890 | if (!((struct lys_node_choice *)node)->dflt) { |
| 4891 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->dflt[0], "default"); |
| 4892 | goto fail; |
| 4893 | } |
| 4894 | if (lyp_check_mandatory_choice(node)) { |
| 4895 | goto fail; |
| 4896 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4897 | } |
| 4898 | } |
| 4899 | |
| 4900 | /* min/max-elements on list or leaf-list */ |
| 4901 | if (node->nodetype == LYS_LIST) { |
| 4902 | if (((struct lys_node_list *)node)->min > ((struct lys_node_list *)node)->max) { |
| 4903 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid value \"%d\" of \"%s\".", rfn->mod.list.min, "min-elements"); |
| 4904 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"min-elements\" is bigger than \"max-elements\"."); |
| 4905 | goto fail; |
| 4906 | } |
| 4907 | } else if (node->nodetype == LYS_LEAFLIST) { |
| 4908 | if (((struct lys_node_leaflist *)node)->min > ((struct lys_node_leaflist *)node)->max) { |
| 4909 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid value \"%d\" of \"%s\".", rfn->mod.list.min, "min-elements"); |
| 4910 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"min-elements\" is bigger than \"max-elements\"."); |
| 4911 | goto fail; |
| 4912 | } |
| 4913 | } |
| 4914 | |
| 4915 | /* additional checks */ |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 4916 | /* default value with mandatory/min-elements */ |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4917 | if (node->nodetype == LYS_LEAFLIST) { |
| 4918 | llist = (struct lys_node_leaflist *)node; |
| 4919 | if (llist->dflt_size && llist->min) { |
| 4920 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, rfn->dflt_size ? "default" : "min-elements", "refine"); |
| 4921 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, |
| 4922 | "The \"min-elements\" statement with non-zero value is forbidden on leaf-lists with the \"default\" statement."); |
| 4923 | goto fail; |
| 4924 | } |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 4925 | } else if (node->nodetype == LYS_LEAF) { |
| 4926 | leaf = (struct lys_node_leaf *)node; |
| 4927 | if (leaf->dflt && (leaf->flags & LYS_MAND_TRUE)) { |
| 4928 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, rfn->dflt_size ? "default" : "mandatory", "refine"); |
| 4929 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, |
| 4930 | "The \"mandatory\" statement is forbidden on leafs with the \"default\" statement."); |
| 4931 | goto fail; |
| 4932 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4933 | } |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 4934 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4935 | /* 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] | 4936 | if ((rfn->flags & LYS_MAND_TRUE) || rfn->mod.list.min) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4937 | for (parent = node->parent; |
| 4938 | parent && !(parent->nodetype & (LYS_CHOICE | LYS_GROUPING | LYS_ACTION | LYS_USES)); |
| 4939 | parent = parent->parent) { |
| 4940 | if (parent->nodetype == LYS_CONTAINER && ((struct lys_node_container *)parent)->presence) { |
| 4941 | /* stop also on presence containers */ |
| 4942 | break; |
| 4943 | } |
| 4944 | } |
| 4945 | /* and if it is a choice with the default case, check it for presence of a mandatory node in it */ |
| 4946 | if (parent && parent->nodetype == LYS_CHOICE && ((struct lys_node_choice *)parent)->dflt) { |
| 4947 | if (lyp_check_mandatory_choice(parent)) { |
| 4948 | goto fail; |
| 4949 | } |
| 4950 | } |
| 4951 | } |
| 4952 | } |
| 4953 | free(refine_nodes); |
| 4954 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4955 | return EXIT_SUCCESS; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4956 | |
| 4957 | fail: |
| 4958 | LY_TREE_FOR_SAFE(uses->child, next, iter) { |
| 4959 | lys_node_free(iter, NULL, 0); |
| 4960 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4961 | free(refine_nodes); |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4962 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4963 | } |
| 4964 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4965 | static int |
| 4966 | identity_backlink_update(struct lys_ident *der, struct lys_ident *base) |
| 4967 | { |
| 4968 | int i; |
| 4969 | |
| 4970 | assert(der && base); |
| 4971 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4972 | if (!base->der) { |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 4973 | /* create a set for backlinks if it does not exist */ |
| 4974 | base->der = ly_set_new(); |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4975 | } |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 4976 | /* store backlink */ |
| 4977 | ly_set_add(base->der, der, LY_SET_OPT_USEASLIST); |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4978 | |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 4979 | /* do it recursively */ |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4980 | for (i = 0; i < base->base_size; i++) { |
| 4981 | if (identity_backlink_update(der, base->base[i])) { |
| 4982 | return EXIT_FAILURE; |
| 4983 | } |
| 4984 | } |
| 4985 | |
| 4986 | return EXIT_SUCCESS; |
| 4987 | } |
| 4988 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4989 | /** |
| 4990 | * @brief Resolve base identity recursively. Does not log. |
| 4991 | * |
| 4992 | * @param[in] module Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4993 | * @param[in] ident Identity to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4994 | * @param[in] basename Base name of the identity. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4995 | * @param[out] ret Pointer to the resolved identity. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4996 | * |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 4997 | * @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] | 4998 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4999 | static int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 5000 | 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] | 5001 | struct unres_schema *unres, struct lys_ident **ret) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5002 | { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5003 | uint32_t i, j; |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5004 | struct lys_ident *base = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5005 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5006 | assert(ret); |
| 5007 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5008 | /* search module */ |
| 5009 | for (i = 0; i < module->ident_size; i++) { |
| 5010 | if (!strcmp(basename, module->ident[i].name)) { |
| 5011 | |
| 5012 | if (!ident) { |
| 5013 | /* just search for type, so do not modify anything, just return |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5014 | * the base identity pointer */ |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5015 | *ret = &module->ident[i]; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5016 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5017 | } |
| 5018 | |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5019 | base = &module->ident[i]; |
| 5020 | goto matchfound; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5021 | } |
| 5022 | } |
| 5023 | |
| 5024 | /* search submodules */ |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5025 | for (j = 0; j < module->inc_size && module->inc[j].submodule; j++) { |
| 5026 | for (i = 0; i < module->inc[j].submodule->ident_size; i++) { |
| 5027 | if (!strcmp(basename, module->inc[j].submodule->ident[i].name)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5028 | |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5029 | if (!ident) { |
| 5030 | *ret = &module->inc[j].submodule->ident[i]; |
| 5031 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5032 | } |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5033 | |
| 5034 | base = &module->inc[j].submodule->ident[i]; |
| 5035 | goto matchfound; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5036 | } |
| 5037 | } |
| 5038 | } |
| 5039 | |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5040 | matchfound: |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5041 | /* we found it somewhere */ |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 5042 | if (base) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5043 | /* is it already completely resolved? */ |
| 5044 | for (i = 0; i < unres->count; i++) { |
Radek Krejci | ba7b1cc | 2016-08-08 13:44:43 +0200 | [diff] [blame] | 5045 | if ((unres->item[i] == base) && (unres->type[i] == UNRES_IDENT)) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5046 | /* identity found, but not yet resolved, so do not return it in *res and try it again later */ |
| 5047 | |
| 5048 | /* simple check for circular reference, |
| 5049 | * the complete check is done as a side effect of using only completely |
| 5050 | * resolved identities (previous check of unres content) */ |
| 5051 | if (ly_strequal((const char *)unres->str_snode[i], ident->name, 1)) { |
| 5052 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, basename, "base"); |
| 5053 | 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] | 5054 | return -1; |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5055 | } |
| 5056 | |
Radek Krejci | 06f64ed | 2016-08-15 11:07:44 +0200 | [diff] [blame] | 5057 | return EXIT_FAILURE; |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 5058 | } |
| 5059 | } |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5060 | |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 5061 | /* checks done, store the result */ |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5062 | *ret = base; |
Pavol Vican | fdab9f9 | 2016-09-07 15:23:27 +0200 | [diff] [blame] | 5063 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5064 | } |
| 5065 | |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5066 | /* base not found (maybe a forward reference) */ |
| 5067 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5068 | } |
| 5069 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5070 | /** |
| 5071 | * @brief Resolve base identity. Logs directly. |
| 5072 | * |
| 5073 | * @param[in] module Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5074 | * @param[in] ident Identity to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5075 | * @param[in] basename Base name of the identity. |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 5076 | * @param[in] parent Either "type" or "identity". |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5077 | * @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] | 5078 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5079 | * @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] | 5080 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5081 | static int |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5082 | 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] | 5083 | struct lys_type *type, struct unres_schema *unres) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5084 | { |
| 5085 | const char *name; |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5086 | int mod_name_len = 0, rc; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5087 | struct lys_ident *target, **ret; |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 5088 | uint16_t flags; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5089 | struct lys_module *mod; |
| 5090 | |
| 5091 | assert((ident && !type) || (!ident && type)); |
| 5092 | |
| 5093 | if (!type) { |
| 5094 | /* have ident to resolve */ |
| 5095 | ret = ⌖ |
| 5096 | flags = ident->flags; |
| 5097 | mod = ident->module; |
| 5098 | } else { |
| 5099 | /* have type to fill */ |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5100 | ++type->info.ident.count; |
| 5101 | type->info.ident.ref = ly_realloc(type->info.ident.ref, type->info.ident.count * sizeof *type->info.ident.ref); |
| 5102 | if (!type->info.ident.ref) { |
| 5103 | LOGMEM; |
| 5104 | return -1; |
| 5105 | } |
| 5106 | |
| 5107 | ret = &type->info.ident.ref[type->info.ident.count - 1]; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5108 | flags = type->parent->flags; |
| 5109 | mod = type->parent->module; |
| 5110 | } |
Michal Vasko | f200600 | 2016-04-21 16:28:15 +0200 | [diff] [blame] | 5111 | *ret = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5112 | |
| 5113 | /* search for the base identity */ |
| 5114 | name = strchr(basename, ':'); |
| 5115 | if (name) { |
| 5116 | /* set name to correct position after colon */ |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 5117 | mod_name_len = name - basename; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5118 | name++; |
| 5119 | |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 5120 | 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] | 5121 | /* prefix refers to the current module, ignore it */ |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 5122 | mod_name_len = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5123 | } |
| 5124 | } else { |
| 5125 | name = basename; |
| 5126 | } |
| 5127 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 5128 | /* get module where to search */ |
| 5129 | module = lys_get_import_module(module, NULL, 0, mod_name_len ? basename : NULL, mod_name_len); |
| 5130 | if (!module) { |
| 5131 | /* identity refers unknown data model */ |
Radek Krejci | 02a0499 | 2016-03-17 16:06:37 +0100 | [diff] [blame] | 5132 | LOGVAL(LYE_INMOD, LY_VLOG_NONE, NULL, basename); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 5133 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5134 | } |
| 5135 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 5136 | /* search in the identified module ... */ |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5137 | rc = resolve_base_ident_sub(module, ident, name, unres, ret); |
| 5138 | if (!rc) { |
| 5139 | assert(*ret); |
| 5140 | |
| 5141 | /* check status */ |
| 5142 | if (lyp_check_status(flags, mod, ident ? ident->name : "of type", |
| 5143 | (*ret)->flags, (*ret)->module, (*ret)->name, NULL)) { |
| 5144 | rc = -1; |
Pavol Vican | fdab9f9 | 2016-09-07 15:23:27 +0200 | [diff] [blame] | 5145 | } else { |
| 5146 | if (ident) { |
| 5147 | ident->base[ident->base_size++] = *ret; |
| 5148 | |
| 5149 | /* maintain backlinks to the derived identities */ |
| 5150 | rc = identity_backlink_update(ident, *ret) ? -1 : EXIT_SUCCESS; |
| 5151 | } |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5152 | } |
| 5153 | } else if (rc == EXIT_FAILURE) { |
| 5154 | LOGVAL(LYE_INRESOLV, LY_VLOG_NONE, NULL, parent, basename); |
Pavol Vican | 053a288 | 2016-09-05 14:40:33 +0200 | [diff] [blame] | 5155 | if (type) { |
| 5156 | --type->info.ident.count; |
| 5157 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5158 | } |
| 5159 | |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5160 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5161 | } |
| 5162 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5163 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 5164 | * @brief Resolve JSON data format identityref. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5165 | * |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5166 | * @param[in] type Identityref type. |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 5167 | * @param[in] ident_name Identityref name. |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 5168 | * @param[in] node Node where the identityref is being resolved |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5169 | * |
| 5170 | * @return Pointer to the identity resolvent, NULL on error. |
| 5171 | */ |
Radek Krejci | a52656e | 2015-08-05 13:41:50 +0200 | [diff] [blame] | 5172 | struct lys_ident * |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5173 | 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] | 5174 | { |
Radek Krejci | 639491e | 2016-12-05 13:30:42 +0100 | [diff] [blame] | 5175 | const char *mod_name, *name, *mod_name_iter; |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5176 | int mod_name_len, rc, i; |
| 5177 | unsigned int u; |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5178 | struct lys_ident *der, *cur; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5179 | |
Radek Krejci | bb1ce0f | 2016-12-05 13:24:33 +0100 | [diff] [blame] | 5180 | assert(type && ident_name && node); |
| 5181 | |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5182 | if (!type || (!type->info.ident.count && !type->der) || !ident_name) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5183 | return NULL; |
| 5184 | } |
| 5185 | |
Michal Vasko | c633ca0 | 2015-08-21 14:03:51 +0200 | [diff] [blame] | 5186 | 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] | 5187 | if (rc < 1) { |
Radek Krejci | bb1ce0f | 2016-12-05 13:24:33 +0100 | [diff] [blame] | 5188 | LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, ident_name[-rc], &ident_name[-rc]); |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 5189 | return NULL; |
Michal Vasko | b43bb3c | 2016-03-17 15:00:27 +0100 | [diff] [blame] | 5190 | } else if (rc < (signed)strlen(ident_name)) { |
Radek Krejci | bb1ce0f | 2016-12-05 13:24:33 +0100 | [diff] [blame] | 5191 | LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, ident_name[rc], &ident_name[rc]); |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 5192 | return NULL; |
| 5193 | } |
Radek Krejci | f32c5f6 | 2016-12-05 09:27:38 +0100 | [diff] [blame] | 5194 | if (!mod_name) { |
| 5195 | /* no prefix, identity must be defined in the same module as node */ |
Radek Krejci | 639491e | 2016-12-05 13:30:42 +0100 | [diff] [blame] | 5196 | mod_name = lys_main_module(node->schema->module)->name; |
Radek Krejci | f32c5f6 | 2016-12-05 09:27:38 +0100 | [diff] [blame] | 5197 | mod_name_len = strlen(mod_name); |
| 5198 | } |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 5199 | |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5200 | /* go through all the bases in all the derived types */ |
| 5201 | while (type->der) { |
| 5202 | for (i = 0; i < type->info.ident.count; ++i) { |
| 5203 | cur = type->info.ident.ref[i]; |
Radek Krejci | 639491e | 2016-12-05 13:30:42 +0100 | [diff] [blame] | 5204 | mod_name_iter = lys_main_module(cur->module)->name; |
Radek Krejci | f32c5f6 | 2016-12-05 09:27:38 +0100 | [diff] [blame] | 5205 | if (!strcmp(cur->name, name) && |
Radek Krejci | 639491e | 2016-12-05 13:30:42 +0100 | [diff] [blame] | 5206 | !strncmp(mod_name_iter, mod_name, mod_name_len) && !mod_name_iter[mod_name_len]) { |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5207 | goto match; |
| 5208 | } |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 5209 | |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5210 | if (cur->der) { |
| 5211 | /* there are also some derived identities */ |
| 5212 | for (u = 0; u < cur->der->number; u++) { |
| 5213 | der = (struct lys_ident *)cur->der->set.g[u]; /* shortcut */ |
Radek Krejci | 639491e | 2016-12-05 13:30:42 +0100 | [diff] [blame] | 5214 | mod_name_iter = lys_main_module(der->module)->name; |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5215 | if (!strcmp(der->name, name) && |
Radek Krejci | 639491e | 2016-12-05 13:30:42 +0100 | [diff] [blame] | 5216 | !strncmp(mod_name_iter, mod_name, mod_name_len) && !mod_name_iter[mod_name_len]) { |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5217 | /* we have match */ |
| 5218 | cur = der; |
| 5219 | goto match; |
| 5220 | } |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5221 | } |
| 5222 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5223 | } |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5224 | type = &type->der->type; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5225 | } |
| 5226 | |
Radek Krejci | bb1ce0f | 2016-12-05 13:24:33 +0100 | [diff] [blame] | 5227 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYD, node, "identityref", ident_name); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5228 | return NULL; |
Radek Krejci | f1ee2e2 | 2016-08-02 16:36:48 +0200 | [diff] [blame] | 5229 | |
| 5230 | match: |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5231 | for (i = 0; i < cur->iffeature_size; i++) { |
| 5232 | if (!resolve_iffeature(&cur->iffeature[i])) { |
Radek Krejci | bb1ce0f | 2016-12-05 13:24:33 +0100 | [diff] [blame] | 5233 | LOGVAL(LYE_INVAL, LY_VLOG_LYD, node, cur->name, node->schema->name); |
| 5234 | LOGVAL(LYE_SPEC, LY_VLOG_LYD, node, "Identity \"%s\" is disabled by its if-feature condition.", cur->name); |
Radek Krejci | f1ee2e2 | 2016-08-02 16:36:48 +0200 | [diff] [blame] | 5235 | return NULL; |
| 5236 | } |
| 5237 | } |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5238 | return cur; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5239 | } |
| 5240 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5241 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5242 | * @brief Resolve unresolved uses. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5243 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5244 | * @param[in] uses Uses to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5245 | * @param[in] unres Specific unres item. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5246 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5247 | * @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] | 5248 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5249 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5250 | 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] | 5251 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5252 | int rc; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5253 | struct lys_node *par_grp; |
Michal Vasko | e91afce | 2015-08-12 12:21:00 +0200 | [diff] [blame] | 5254 | |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5255 | /* HACK: when a grouping has uses inside, all such uses have to be resolved before the grouping itself |
| 5256 | * is used in some uses. When we see such a uses, the grouping's nacm member (not used in grouping) |
| 5257 | * is used to store number of so far unresolved uses. The grouping cannot be used unless the nacm |
| 5258 | * value is decreased back to 0. To remember that the uses already increased grouping's nacm, the |
| 5259 | * LYS_USESGRP flag is used. */ |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 5260 | 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] | 5261 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5262 | if (!uses->grp) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 5263 | rc = resolve_uses_schema_nodeid(uses->name, (const struct lys_node *)uses, (const struct lys_node_grp **)&uses->grp); |
| 5264 | if (rc == -1) { |
Michal Vasko | 92981a6 | 2016-10-14 10:25:16 +0200 | [diff] [blame] | 5265 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 5266 | return -1; |
| 5267 | } else if (rc > 0) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5268 | 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] | 5269 | return -1; |
| 5270 | } else if (!uses->grp) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5271 | if (par_grp && !(uses->flags & LYS_USESGRP)) { |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 5272 | /* hack - in contrast to lys_node, lys_node_grp has bigger nacm field |
| 5273 | * (and smaller flags - it uses only a limited set of flags) |
| 5274 | */ |
| 5275 | ((struct lys_node_grp *)par_grp)->nacm++; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5276 | uses->flags |= LYS_USESGRP; |
Michal Vasko | 407f1bb | 2015-09-23 15:51:07 +0200 | [diff] [blame] | 5277 | } |
Michal Vasko | 92981a6 | 2016-10-14 10:25:16 +0200 | [diff] [blame] | 5278 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 5279 | return EXIT_FAILURE; |
Michal Vasko | 12e3084 | 2015-08-04 11:54:00 +0200 | [diff] [blame] | 5280 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5281 | } |
| 5282 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5283 | if (uses->grp->nacm) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5284 | if (par_grp && !(uses->flags & LYS_USESGRP)) { |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 5285 | ((struct lys_node_grp *)par_grp)->nacm++; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5286 | uses->flags |= LYS_USESGRP; |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 5287 | } else { |
| 5288 | /* instantiate grouping only when it is completely resolved */ |
| 5289 | uses->grp = NULL; |
Michal Vasko | 407f1bb | 2015-09-23 15:51:07 +0200 | [diff] [blame] | 5290 | } |
Michal Vasko | 92981a6 | 2016-10-14 10:25:16 +0200 | [diff] [blame] | 5291 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5292 | return EXIT_FAILURE; |
| 5293 | } |
| 5294 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5295 | rc = resolve_uses(uses, unres); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5296 | if (!rc) { |
| 5297 | /* decrease unres count only if not first try */ |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5298 | if (par_grp && (uses->flags & LYS_USESGRP)) { |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 5299 | if (!((struct lys_node_grp *)par_grp)->nacm) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5300 | LOGINT; |
| 5301 | return -1; |
| 5302 | } |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 5303 | ((struct lys_node_grp *)par_grp)->nacm--; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5304 | uses->flags &= ~LYS_USESGRP; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5305 | } |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5306 | |
| 5307 | /* check status */ |
Radek Krejci | c655602 | 2016-01-27 15:16:45 +0100 | [diff] [blame] | 5308 | if (lyp_check_status(uses->flags, uses->module, "of uses", |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 5309 | uses->grp->flags, uses->grp->module, uses->grp->name, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5310 | (struct lys_node *)uses)) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5311 | return -1; |
| 5312 | } |
| 5313 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5314 | return EXIT_SUCCESS; |
| 5315 | } |
| 5316 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5317 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5318 | } |
| 5319 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5320 | /** |
Michal Vasko | 9957e59 | 2015-08-17 15:04:09 +0200 | [diff] [blame] | 5321 | * @brief Resolve list keys. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5322 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5323 | * @param[in] list List to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5324 | * @param[in] keys_str Keys node value. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5325 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5326 | * @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] | 5327 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5328 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5329 | resolve_list_keys(struct lys_node_list *list, const char *keys_str) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5330 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5331 | int i, len, rc; |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 5332 | const char *value; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5333 | |
| 5334 | for (i = 0; i < list->keys_size; ++i) { |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 5335 | if (!list->child) { |
| 5336 | /* no child, possible forward reference */ |
| 5337 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, list, "list keys", keys_str); |
| 5338 | return EXIT_FAILURE; |
| 5339 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5340 | /* get the key name */ |
| 5341 | if ((value = strpbrk(keys_str, " \t\n"))) { |
| 5342 | len = value - keys_str; |
| 5343 | while (isspace(value[0])) { |
| 5344 | value++; |
| 5345 | } |
| 5346 | } else { |
| 5347 | len = strlen(keys_str); |
| 5348 | } |
| 5349 | |
Radek Krejci | c428344 | 2016-04-22 09:19:27 +0200 | [diff] [blame] | 5350 | 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] | 5351 | if (rc) { |
Michal Vasko | 7a55bea | 2016-05-02 14:51:20 +0200 | [diff] [blame] | 5352 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, list, "list keys", keys_str); |
| 5353 | return EXIT_FAILURE; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5354 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5355 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5356 | if (check_key(list, i, keys_str, len)) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5357 | /* check_key logs */ |
| 5358 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5359 | } |
| 5360 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5361 | /* check status */ |
Radek Krejci | c655602 | 2016-01-27 15:16:45 +0100 | [diff] [blame] | 5362 | if (lyp_check_status(list->flags, list->module, list->name, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5363 | list->keys[i]->flags, list->keys[i]->module, list->keys[i]->name, |
| 5364 | (struct lys_node *)list->keys[i])) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5365 | return -1; |
| 5366 | } |
| 5367 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5368 | /* prepare for next iteration */ |
| 5369 | while (value && isspace(value[0])) { |
| 5370 | value++; |
| 5371 | } |
| 5372 | keys_str = value; |
| 5373 | } |
| 5374 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5375 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5376 | } |
| 5377 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5378 | /** |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5379 | * @brief Resolve (check) all must conditions of \p node. |
| 5380 | * Logs directly. |
| 5381 | * |
| 5382 | * @param[in] node Data node with optional must statements. |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5383 | * @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] | 5384 | * |
| 5385 | * @return EXIT_SUCCESS on pass, EXIT_FAILURE on fail, -1 on error. |
| 5386 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5387 | static int |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5388 | resolve_must(struct lyd_node *node, int inout_parent) |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5389 | { |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5390 | uint8_t i, must_size; |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5391 | struct lys_node *schema; |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5392 | struct lys_restr *must; |
| 5393 | struct lyxp_set set; |
| 5394 | |
| 5395 | assert(node); |
| 5396 | memset(&set, 0, sizeof set); |
| 5397 | |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5398 | if (inout_parent) { |
| 5399 | for (schema = lys_parent(node->schema); |
| 5400 | schema && (schema->nodetype & (LYS_CHOICE | LYS_CASE | LYS_USES)); |
| 5401 | schema = lys_parent(schema)); |
| 5402 | if (!schema || !(schema->nodetype & (LYS_INPUT | LYS_OUTPUT))) { |
| 5403 | LOGINT; |
| 5404 | return -1; |
| 5405 | } |
| 5406 | must_size = ((struct lys_node_inout *)schema)->must_size; |
| 5407 | must = ((struct lys_node_inout *)schema)->must; |
| 5408 | |
| 5409 | /* context node is the RPC/action */ |
| 5410 | node = node->parent; |
| 5411 | if (!(node->schema->nodetype & (LYS_RPC | LYS_ACTION))) { |
| 5412 | LOGINT; |
| 5413 | return -1; |
| 5414 | } |
| 5415 | } else { |
| 5416 | switch (node->schema->nodetype) { |
| 5417 | case LYS_CONTAINER: |
| 5418 | must_size = ((struct lys_node_container *)node->schema)->must_size; |
| 5419 | must = ((struct lys_node_container *)node->schema)->must; |
| 5420 | break; |
| 5421 | case LYS_LEAF: |
| 5422 | must_size = ((struct lys_node_leaf *)node->schema)->must_size; |
| 5423 | must = ((struct lys_node_leaf *)node->schema)->must; |
| 5424 | break; |
| 5425 | case LYS_LEAFLIST: |
| 5426 | must_size = ((struct lys_node_leaflist *)node->schema)->must_size; |
| 5427 | must = ((struct lys_node_leaflist *)node->schema)->must; |
| 5428 | break; |
| 5429 | case LYS_LIST: |
| 5430 | must_size = ((struct lys_node_list *)node->schema)->must_size; |
| 5431 | must = ((struct lys_node_list *)node->schema)->must; |
| 5432 | break; |
| 5433 | case LYS_ANYXML: |
| 5434 | case LYS_ANYDATA: |
| 5435 | must_size = ((struct lys_node_anydata *)node->schema)->must_size; |
| 5436 | must = ((struct lys_node_anydata *)node->schema)->must; |
| 5437 | break; |
| 5438 | case LYS_NOTIF: |
| 5439 | must_size = ((struct lys_node_notif *)node->schema)->must_size; |
| 5440 | must = ((struct lys_node_notif *)node->schema)->must; |
| 5441 | break; |
| 5442 | default: |
| 5443 | must_size = 0; |
| 5444 | break; |
| 5445 | } |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5446 | } |
| 5447 | |
| 5448 | for (i = 0; i < must_size; ++i) { |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5449 | 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] | 5450 | return -1; |
| 5451 | } |
| 5452 | |
Michal Vasko | 944a564 | 2016-03-21 11:48:58 +0100 | [diff] [blame] | 5453 | lyxp_set_cast(&set, LYXP_SET_BOOLEAN, node, LYXP_MUST); |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5454 | |
Michal Vasko | 8146d4c | 2016-05-09 15:50:29 +0200 | [diff] [blame] | 5455 | if (!set.val.bool) { |
Michal Vasko | 6ac6828 | 2016-04-11 10:56:47 +0200 | [diff] [blame] | 5456 | LOGVAL(LYE_NOMUST, LY_VLOG_LYD, node, must[i].expr); |
| 5457 | if (must[i].emsg) { |
| 5458 | LOGVAL(LYE_SPEC, LY_VLOG_LYD, node, must[i].emsg); |
| 5459 | } |
| 5460 | if (must[i].eapptag) { |
| 5461 | strncpy(((struct ly_err *)&ly_errno)->apptag, must[i].eapptag, LY_APPTAG_LEN - 1); |
| 5462 | } |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5463 | return 1; |
| 5464 | } |
| 5465 | } |
| 5466 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5467 | return EXIT_SUCCESS; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5468 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5469 | |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5470 | /** |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5471 | * @brief Resolve (find) when condition schema context node. Does not log. |
| 5472 | * |
| 5473 | * @param[in] schema Schema node with the when condition. |
| 5474 | * @param[out] ctx_snode When schema context node. |
| 5475 | * @param[out] ctx_snode_type Schema context node type. |
| 5476 | */ |
| 5477 | void |
| 5478 | resolve_when_ctx_snode(const struct lys_node *schema, struct lys_node **ctx_snode, enum lyxp_node_type *ctx_snode_type) |
| 5479 | { |
| 5480 | const struct lys_node *sparent; |
| 5481 | |
| 5482 | /* find a not schema-only node */ |
| 5483 | *ctx_snode_type = LYXP_NODE_ELEM; |
| 5484 | while (schema->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE | LYS_AUGMENT | LYS_INPUT | LYS_OUTPUT)) { |
| 5485 | if (schema->nodetype == LYS_AUGMENT) { |
| 5486 | sparent = ((struct lys_node_augment *)schema)->target; |
| 5487 | } else { |
| 5488 | sparent = schema->parent; |
| 5489 | } |
| 5490 | if (!sparent) { |
| 5491 | /* context node is the document root (fake root in our case) */ |
| 5492 | if (schema->flags & LYS_CONFIG_W) { |
| 5493 | *ctx_snode_type = LYXP_NODE_ROOT_CONFIG; |
| 5494 | } else { |
Michal Vasko | b94a5e4 | 2016-09-08 14:01:56 +0200 | [diff] [blame] | 5495 | *ctx_snode_type = LYXP_NODE_ROOT; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5496 | } |
Michal Vasko | 2d2aec1 | 2016-09-08 11:42:50 +0200 | [diff] [blame] | 5497 | /* we need the first top-level sibling, but no uses or groupings */ |
Michal Vasko | b94a5e4 | 2016-09-08 14:01:56 +0200 | [diff] [blame] | 5498 | schema = lys_getnext(NULL, NULL, lys_node_module(schema), 0); |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5499 | break; |
| 5500 | } |
| 5501 | schema = sparent; |
| 5502 | } |
| 5503 | |
| 5504 | *ctx_snode = (struct lys_node *)schema; |
| 5505 | } |
| 5506 | |
| 5507 | /** |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5508 | * @brief Resolve (find) when condition context node. Does not log. |
| 5509 | * |
| 5510 | * @param[in] node Data node, whose conditional definition is being decided. |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5511 | * @param[in] schema Schema node with the when condition. |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5512 | * @param[out] ctx_node Context node. |
| 5513 | * @param[out] ctx_node_type Context node type. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5514 | * |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5515 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5516 | */ |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5517 | static int |
| 5518 | resolve_when_ctx_node(struct lyd_node *node, struct lys_node *schema, struct lyd_node **ctx_node, |
| 5519 | enum lyxp_node_type *ctx_node_type) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5520 | { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5521 | struct lyd_node *parent; |
| 5522 | struct lys_node *sparent; |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5523 | enum lyxp_node_type node_type; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5524 | uint16_t i, data_depth, schema_depth; |
| 5525 | |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5526 | resolve_when_ctx_snode(schema, &schema, &node_type); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5527 | |
Michal Vasko | fe98975 | 2016-09-08 08:47:26 +0200 | [diff] [blame] | 5528 | if (node_type == LYXP_NODE_ELEM) { |
| 5529 | /* standard element context node */ |
| 5530 | for (parent = node, data_depth = 0; parent; parent = parent->parent, ++data_depth); |
| 5531 | for (sparent = schema, schema_depth = 0; |
| 5532 | sparent; |
| 5533 | sparent = (sparent->nodetype == LYS_AUGMENT ? ((struct lys_node_augment *)sparent)->target : sparent->parent)) { |
| 5534 | if (sparent->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_NOTIF | LYS_RPC)) { |
| 5535 | ++schema_depth; |
| 5536 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5537 | } |
Michal Vasko | fe98975 | 2016-09-08 08:47:26 +0200 | [diff] [blame] | 5538 | if (data_depth < schema_depth) { |
| 5539 | return -1; |
| 5540 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5541 | |
Michal Vasko | 956e854 | 2016-08-26 09:43:35 +0200 | [diff] [blame] | 5542 | /* find the corresponding data node */ |
| 5543 | for (i = 0; i < data_depth - schema_depth; ++i) { |
| 5544 | node = node->parent; |
| 5545 | } |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5546 | if (node->schema != schema) { |
| 5547 | return -1; |
| 5548 | } |
Michal Vasko | fe98975 | 2016-09-08 08:47:26 +0200 | [diff] [blame] | 5549 | } else { |
| 5550 | /* root context node */ |
| 5551 | while (node->parent) { |
| 5552 | node = node->parent; |
| 5553 | } |
| 5554 | while (node->prev->next) { |
| 5555 | node = node->prev; |
| 5556 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5557 | } |
| 5558 | |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5559 | *ctx_node = node; |
| 5560 | *ctx_node_type = node_type; |
| 5561 | return EXIT_SUCCESS; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5562 | } |
| 5563 | |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5564 | /** |
| 5565 | * @brief Temporarily unlink nodes as per YANG 1.1 RFC section 7.21.5 for when XPath evaluation. |
| 5566 | * The context nodes is adjusted if needed. |
| 5567 | * |
| 5568 | * @param[in] snode Schema node, whose children instances need to be unlinked. |
| 5569 | * @param[in,out] node Data siblings where to look for the children of \p snode. If it is unlinked, |
| 5570 | * it is moved to point to another sibling still in the original tree. |
| 5571 | * @param[in,out] ctx_node When context node, adjusted if needed. |
| 5572 | * @param[in] ctx_node_type Context node type, just for information to detect invalid situations. |
| 5573 | * @param[out] unlinked_nodes Unlinked siblings. Can be safely appended to \p node afterwards. |
| 5574 | * Ordering may change, but there will be no semantic change. |
| 5575 | * |
| 5576 | * @return EXIT_SUCCESS on success, -1 on error. |
| 5577 | */ |
| 5578 | static int |
| 5579 | resolve_when_unlink_nodes(struct lys_node *snode, struct lyd_node **node, struct lyd_node **ctx_node, |
| 5580 | enum lyxp_node_type ctx_node_type, struct lyd_node **unlinked_nodes) |
| 5581 | { |
| 5582 | struct lyd_node *next, *elem; |
| 5583 | |
| 5584 | switch (snode->nodetype) { |
| 5585 | case LYS_AUGMENT: |
| 5586 | case LYS_USES: |
| 5587 | case LYS_CHOICE: |
| 5588 | case LYS_CASE: |
| 5589 | LY_TREE_FOR(snode->child, snode) { |
| 5590 | if (resolve_when_unlink_nodes(snode, node, ctx_node, ctx_node_type, unlinked_nodes)) { |
| 5591 | return -1; |
| 5592 | } |
| 5593 | } |
| 5594 | break; |
| 5595 | case LYS_CONTAINER: |
| 5596 | case LYS_LIST: |
| 5597 | case LYS_LEAF: |
| 5598 | case LYS_LEAFLIST: |
| 5599 | case LYS_ANYXML: |
| 5600 | case LYS_ANYDATA: |
| 5601 | LY_TREE_FOR_SAFE(lyd_first_sibling(*node), next, elem) { |
| 5602 | if (elem->schema == snode) { |
| 5603 | |
| 5604 | if (elem == *ctx_node) { |
| 5605 | /* We are going to unlink our context node! This normally cannot happen, |
| 5606 | * but we use normal top-level data nodes for faking a document root node, |
| 5607 | * so if this is the context node, we just use the next top-level node. |
| 5608 | * Additionally, it can even happen that there are no top-level data nodes left, |
| 5609 | * all were unlinked, so in this case we pass NULL as the context node/data tree, |
| 5610 | * lyxp_eval() can handle this special situation. |
| 5611 | */ |
| 5612 | if (ctx_node_type == LYXP_NODE_ELEM) { |
| 5613 | LOGINT; |
| 5614 | return -1; |
| 5615 | } |
| 5616 | |
| 5617 | if (elem->prev == elem) { |
| 5618 | /* unlinking last top-level element, use an empty data tree */ |
| 5619 | *ctx_node = NULL; |
| 5620 | } else { |
| 5621 | /* in this case just use the previous/last top-level data node */ |
| 5622 | *ctx_node = elem->prev; |
| 5623 | } |
| 5624 | } else if (elem == *node) { |
| 5625 | /* We are going to unlink the currently processed node. This does not matter that |
| 5626 | * much, but we would lose access to the original data tree, so just move our |
| 5627 | * pointer somewhere still inside it. |
| 5628 | */ |
| 5629 | if ((*node)->prev != *node) { |
| 5630 | *node = (*node)->prev; |
| 5631 | } else { |
| 5632 | /* the processed node with sibings were all unlinked, oh well */ |
| 5633 | *node = NULL; |
| 5634 | } |
| 5635 | } |
| 5636 | |
| 5637 | /* temporarily unlink the node */ |
| 5638 | lyd_unlink(elem); |
| 5639 | if (*unlinked_nodes) { |
| 5640 | if (lyd_insert_after(*unlinked_nodes, elem)) { |
| 5641 | LOGINT; |
| 5642 | return -1; |
| 5643 | } |
| 5644 | } else { |
| 5645 | *unlinked_nodes = elem; |
| 5646 | } |
| 5647 | |
| 5648 | if (snode->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_ANYDATA)) { |
| 5649 | /* there can be only one instance */ |
| 5650 | break; |
| 5651 | } |
| 5652 | } |
| 5653 | } |
| 5654 | break; |
| 5655 | default: |
| 5656 | LOGINT; |
| 5657 | return -1; |
| 5658 | } |
| 5659 | |
| 5660 | return EXIT_SUCCESS; |
| 5661 | } |
| 5662 | |
| 5663 | /** |
| 5664 | * @brief Relink the unlinked nodes back. |
| 5665 | * |
| 5666 | * @param[in] node Data node to link the nodes back to. It can actually be the adjusted context node, |
| 5667 | * we simply need a sibling from the original data tree. |
| 5668 | * @param[in] unlinked_nodes Unlinked nodes to relink to \p node. |
| 5669 | * @param[in] ctx_node_type Context node type to distinguish between \p node being the parent |
| 5670 | * or the sibling of \p unlinked_nodes. |
| 5671 | * |
| 5672 | * @return EXIT_SUCCESS on success, -1 on error. |
| 5673 | */ |
| 5674 | static int |
| 5675 | resolve_when_relink_nodes(struct lyd_node *node, struct lyd_node *unlinked_nodes, enum lyxp_node_type ctx_node_type) |
| 5676 | { |
| 5677 | struct lyd_node *elem; |
| 5678 | |
| 5679 | LY_TREE_FOR_SAFE(unlinked_nodes, unlinked_nodes, elem) { |
Michal Vasko | de1feeb | 2016-12-20 14:48:42 +0100 | [diff] [blame] | 5680 | lyd_unlink(elem); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5681 | if (ctx_node_type == LYXP_NODE_ELEM) { |
| 5682 | if (lyd_insert(node, elem)) { |
| 5683 | return -1; |
| 5684 | } |
| 5685 | } else { |
| 5686 | if (lyd_insert_after(node, elem)) { |
| 5687 | return -1; |
| 5688 | } |
| 5689 | } |
| 5690 | } |
| 5691 | |
| 5692 | return EXIT_SUCCESS; |
| 5693 | } |
| 5694 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5695 | int |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5696 | resolve_applies_must(const struct lyd_node *node) |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5697 | { |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5698 | int ret = 0; |
| 5699 | uint8_t must_size; |
| 5700 | struct lys_node *schema, *iter; |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5701 | |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5702 | assert(node); |
| 5703 | |
| 5704 | schema = node->schema; |
| 5705 | |
| 5706 | /* their own must */ |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5707 | switch (schema->nodetype) { |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5708 | case LYS_CONTAINER: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5709 | must_size = ((struct lys_node_container *)schema)->must_size; |
| 5710 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5711 | case LYS_LEAF: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5712 | must_size = ((struct lys_node_leaf *)schema)->must_size; |
| 5713 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5714 | case LYS_LEAFLIST: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5715 | must_size = ((struct lys_node_leaflist *)schema)->must_size; |
| 5716 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5717 | case LYS_LIST: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5718 | must_size = ((struct lys_node_list *)schema)->must_size; |
| 5719 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5720 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 5721 | case LYS_ANYDATA: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5722 | must_size = ((struct lys_node_anydata *)schema)->must_size; |
| 5723 | break; |
| 5724 | case LYS_NOTIF: |
| 5725 | must_size = ((struct lys_node_notif *)schema)->must_size; |
| 5726 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5727 | default: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5728 | must_size = 0; |
| 5729 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5730 | } |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5731 | |
| 5732 | if (must_size) { |
| 5733 | ++ret; |
| 5734 | } |
| 5735 | |
| 5736 | /* schema may be a direct data child of input/output with must (but it must be first, it needs to be evaluated only once) */ |
| 5737 | if (!node->prev->next) { |
| 5738 | for (iter = lys_parent(schema); iter && (iter->nodetype & (LYS_CHOICE | LYS_CASE | LYS_USES)); iter = lys_parent(iter)); |
| 5739 | if (iter && (iter->nodetype & (LYS_INPUT | LYS_OUTPUT))) { |
| 5740 | ret += 0x2; |
| 5741 | } |
| 5742 | } |
| 5743 | |
| 5744 | return ret; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5745 | } |
| 5746 | |
| 5747 | int |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5748 | 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] | 5749 | { |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5750 | const struct lys_node *parent; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5751 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5752 | assert(schema); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5753 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5754 | 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] | 5755 | return 1; |
| 5756 | } |
| 5757 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5758 | parent = schema; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5759 | goto check_augment; |
| 5760 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5761 | while (parent) { |
| 5762 | /* stop conditions */ |
| 5763 | if (!mode) { |
| 5764 | /* stop on node that can be instantiated in data tree */ |
| 5765 | if (!(parent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) { |
| 5766 | break; |
| 5767 | } |
| 5768 | } else { |
| 5769 | /* stop on the specified node */ |
| 5770 | if (parent == stop) { |
| 5771 | break; |
| 5772 | } |
| 5773 | } |
| 5774 | |
| 5775 | if (((const struct lys_node_uses *)parent)->when) { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5776 | return 1; |
| 5777 | } |
| 5778 | check_augment: |
| 5779 | |
| 5780 | if ((parent->parent && (parent->parent->nodetype == LYS_AUGMENT) && |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5781 | (((const struct lys_node_augment *)parent->parent)->when))) { |
Michal Vasko | e365556 | 2016-08-24 15:56:17 +0200 | [diff] [blame] | 5782 | return 1; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5783 | } |
| 5784 | parent = lys_parent(parent); |
| 5785 | } |
| 5786 | |
| 5787 | return 0; |
| 5788 | } |
| 5789 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5790 | /** |
| 5791 | * @brief Resolve (check) all when conditions relevant for \p node. |
| 5792 | * Logs directly. |
| 5793 | * |
| 5794 | * @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] | 5795 | * |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5796 | * @return |
| 5797 | * -1 - error, ly_errno is set |
| 5798 | * 0 - true "when" statement |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5799 | * 0, ly_vecode = LYVE_NOWHEN - false "when" statement |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5800 | * 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] | 5801 | */ |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5802 | int |
| 5803 | resolve_when(struct lyd_node *node, int *result) |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5804 | { |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5805 | struct lyd_node *ctx_node = NULL, *unlinked_nodes, *tmp_node; |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5806 | struct lys_node *sparent; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5807 | struct lyxp_set set; |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5808 | enum lyxp_node_type ctx_node_type; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5809 | int rc = 0; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5810 | |
| 5811 | assert(node); |
| 5812 | memset(&set, 0, sizeof set); |
| 5813 | |
| 5814 | 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] | 5815 | /* make the node dummy for the evaluation */ |
| 5816 | node->validity |= LYD_VAL_INUSE; |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5817 | 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] | 5818 | node->validity &= ~LYD_VAL_INUSE; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5819 | if (rc) { |
| 5820 | if (rc == 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5821 | 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] | 5822 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5823 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5824 | } |
| 5825 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5826 | /* set boolean result of the condition */ |
Michal Vasko | 944a564 | 2016-03-21 11:48:58 +0100 | [diff] [blame] | 5827 | lyxp_set_cast(&set, LYXP_SET_BOOLEAN, node, LYXP_WHEN); |
Michal Vasko | 8146d4c | 2016-05-09 15:50:29 +0200 | [diff] [blame] | 5828 | if (!set.val.bool) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5829 | 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] | 5830 | node->when_status |= LYD_WHEN_FALSE; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5831 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5832 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5833 | |
| 5834 | /* free xpath set content */ |
| 5835 | lyxp_set_cast(&set, LYXP_SET_EMPTY, node, 0); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5836 | } |
| 5837 | |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5838 | sparent = node->schema; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5839 | goto check_augment; |
| 5840 | |
| 5841 | /* check when in every schema node that affects node */ |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5842 | while (sparent && (sparent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) { |
| 5843 | if (((struct lys_node_uses *)sparent)->when) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5844 | if (!ctx_node) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5845 | rc = resolve_when_ctx_node(node, sparent, &ctx_node, &ctx_node_type); |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5846 | if (rc) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5847 | LOGINT; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5848 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5849 | } |
| 5850 | } |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5851 | |
| 5852 | unlinked_nodes = NULL; |
| 5853 | /* we do not want our node pointer to change */ |
| 5854 | tmp_node = node; |
| 5855 | rc = resolve_when_unlink_nodes(sparent, &tmp_node, &ctx_node, ctx_node_type, &unlinked_nodes); |
| 5856 | if (rc) { |
| 5857 | goto cleanup; |
| 5858 | } |
| 5859 | |
| 5860 | rc = lyxp_eval(((struct lys_node_uses *)sparent)->when->cond, ctx_node, ctx_node_type, &set, LYXP_WHEN); |
| 5861 | |
| 5862 | if (unlinked_nodes && ctx_node) { |
| 5863 | if (resolve_when_relink_nodes(ctx_node, unlinked_nodes, ctx_node_type)) { |
| 5864 | rc = -1; |
| 5865 | goto cleanup; |
| 5866 | } |
| 5867 | } |
| 5868 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5869 | if (rc) { |
| 5870 | if (rc == 1) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5871 | 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] | 5872 | } |
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 | } |
| 5875 | |
Michal Vasko | 944a564 | 2016-03-21 11:48:58 +0100 | [diff] [blame] | 5876 | lyxp_set_cast(&set, LYXP_SET_BOOLEAN, ctx_node, LYXP_WHEN); |
Michal Vasko | 8146d4c | 2016-05-09 15:50:29 +0200 | [diff] [blame] | 5877 | if (!set.val.bool) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5878 | 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] | 5879 | node->when_status |= LYD_WHEN_FALSE; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5880 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5881 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5882 | |
| 5883 | /* free xpath set content */ |
| 5884 | lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node, 0); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5885 | } |
| 5886 | |
| 5887 | check_augment: |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5888 | 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] | 5889 | if (!ctx_node) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5890 | 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] | 5891 | if (rc) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5892 | LOGINT; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5893 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5894 | } |
| 5895 | } |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5896 | |
| 5897 | unlinked_nodes = NULL; |
| 5898 | tmp_node = node; |
| 5899 | rc = resolve_when_unlink_nodes(sparent->parent, &tmp_node, &ctx_node, ctx_node_type, &unlinked_nodes); |
| 5900 | if (rc) { |
| 5901 | goto cleanup; |
| 5902 | } |
| 5903 | |
| 5904 | rc = lyxp_eval(((struct lys_node_augment *)sparent->parent)->when->cond, ctx_node, ctx_node_type, &set, LYXP_WHEN); |
| 5905 | |
| 5906 | /* reconnect nodes, if ctx_node is NULL then all the nodes were unlinked, but linked together, |
| 5907 | * so the tree did not actually change and there is nothing for us to do |
| 5908 | */ |
| 5909 | if (unlinked_nodes && ctx_node) { |
| 5910 | if (resolve_when_relink_nodes(ctx_node, unlinked_nodes, ctx_node_type)) { |
| 5911 | rc = -1; |
| 5912 | goto cleanup; |
| 5913 | } |
| 5914 | } |
| 5915 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5916 | if (rc) { |
| 5917 | if (rc == 1) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5918 | 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] | 5919 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5920 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5921 | } |
| 5922 | |
Michal Vasko | 944a564 | 2016-03-21 11:48:58 +0100 | [diff] [blame] | 5923 | lyxp_set_cast(&set, LYXP_SET_BOOLEAN, ctx_node, LYXP_WHEN); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5924 | |
Michal Vasko | 8146d4c | 2016-05-09 15:50:29 +0200 | [diff] [blame] | 5925 | if (!set.val.bool) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5926 | 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] | 5927 | node->when_status |= LYD_WHEN_FALSE; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5928 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5929 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5930 | |
| 5931 | /* free xpath set content */ |
| 5932 | lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node, 0); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5933 | } |
| 5934 | |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5935 | sparent = lys_parent(sparent); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5936 | } |
| 5937 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5938 | node->when_status |= LYD_WHEN_TRUE; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5939 | |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5940 | cleanup: |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5941 | /* free xpath set content */ |
| 5942 | lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node ? ctx_node : node, 0); |
| 5943 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5944 | if (result) { |
| 5945 | if (node->when_status & LYD_WHEN_TRUE) { |
| 5946 | *result = 1; |
| 5947 | } else { |
| 5948 | *result = 0; |
| 5949 | } |
| 5950 | } |
| 5951 | |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5952 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5953 | } |
| 5954 | |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 5955 | static int |
| 5956 | check_leafref_features(struct lys_type *type) |
| 5957 | { |
| 5958 | struct lys_node *iter; |
| 5959 | struct ly_set *src_parents, *trg_parents, *features; |
| 5960 | unsigned int i, j, size, x; |
| 5961 | int ret = EXIT_SUCCESS; |
| 5962 | |
| 5963 | assert(type->parent); |
| 5964 | |
| 5965 | src_parents = ly_set_new(); |
| 5966 | trg_parents = ly_set_new(); |
| 5967 | features = ly_set_new(); |
| 5968 | |
| 5969 | /* get parents chain of source (leafref) */ |
| 5970 | for (iter = (struct lys_node *)type->parent; iter; iter = iter->parent) { |
| 5971 | if (iter->nodetype & (LYS_INPUT | LYS_OUTPUT)) { |
| 5972 | continue; |
| 5973 | } |
| 5974 | ly_set_add(src_parents, iter, LY_SET_OPT_USEASLIST); |
| 5975 | } |
| 5976 | /* get parents chain of target */ |
| 5977 | for (iter = (struct lys_node *)type->info.lref.target; iter; iter = iter->parent) { |
| 5978 | if (iter->nodetype & (LYS_INPUT | LYS_OUTPUT)) { |
| 5979 | continue; |
| 5980 | } |
| 5981 | ly_set_add(trg_parents, iter, LY_SET_OPT_USEASLIST); |
| 5982 | } |
| 5983 | |
| 5984 | /* compare the features used in if-feature statements in the rest of both |
| 5985 | * chains of parents. The set of features used for target must be a subset |
| 5986 | * of features used for the leafref. This is not a perfect, we should compare |
| 5987 | * the truth tables but it could require too much resources, so we simplify that */ |
| 5988 | for (i = 0; i < src_parents->number; i++) { |
| 5989 | iter = src_parents->set.s[i]; /* shortcut */ |
| 5990 | if (!iter->iffeature_size) { |
| 5991 | continue; |
| 5992 | } |
| 5993 | for (j = 0; j < iter->iffeature_size; j++) { |
| 5994 | resolve_iffeature_getsizes(&iter->iffeature[j], NULL, &size); |
| 5995 | for (; size; size--) { |
| 5996 | if (!iter->iffeature[j].features[size - 1]) { |
| 5997 | /* not yet resolved feature, postpone this check */ |
| 5998 | ret = EXIT_FAILURE; |
| 5999 | goto cleanup; |
| 6000 | } |
| 6001 | ly_set_add(features, iter->iffeature[j].features[size - 1], 0); |
| 6002 | } |
| 6003 | } |
| 6004 | } |
| 6005 | x = features->number; |
| 6006 | for (i = 0; i < trg_parents->number; i++) { |
| 6007 | iter = trg_parents->set.s[i]; /* shortcut */ |
| 6008 | if (!iter->iffeature_size) { |
| 6009 | continue; |
| 6010 | } |
| 6011 | for (j = 0; j < iter->iffeature_size; j++) { |
| 6012 | resolve_iffeature_getsizes(&iter->iffeature[j], NULL, &size); |
| 6013 | for (; size; size--) { |
| 6014 | if (!iter->iffeature[j].features[size - 1]) { |
| 6015 | /* not yet resolved feature, postpone this check */ |
| 6016 | ret = EXIT_FAILURE; |
| 6017 | goto cleanup; |
| 6018 | } |
| 6019 | if ((unsigned int)ly_set_add(features, iter->iffeature[j].features[size - 1], 0) >= x) { |
| 6020 | /* the feature is not present in features set of target's parents chain */ |
| 6021 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, type->parent, "leafref", type->info.lref.path); |
| 6022 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, type->parent, |
| 6023 | "Leafref is not conditional based on \"%s\" feature as its target.", |
| 6024 | iter->iffeature[j].features[size - 1]->name); |
| 6025 | ret = -1; |
| 6026 | goto cleanup; |
| 6027 | } |
| 6028 | } |
| 6029 | } |
| 6030 | } |
| 6031 | |
| 6032 | cleanup: |
| 6033 | ly_set_free(features); |
| 6034 | ly_set_free(src_parents); |
| 6035 | ly_set_free(trg_parents); |
| 6036 | |
| 6037 | return ret; |
| 6038 | } |
| 6039 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6040 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6041 | * @brief Resolve a single unres schema item. Logs indirectly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6042 | * |
| 6043 | * @param[in] mod Main module. |
| 6044 | * @param[in] item Item to resolve. Type determined by \p type. |
| 6045 | * @param[in] type Type of the unresolved item. |
| 6046 | * @param[in] str_snode String, a schema node, or NULL. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6047 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6048 | * |
| 6049 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 6050 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6051 | static int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 6052 | 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] | 6053 | struct unres_schema *unres) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6054 | { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6055 | /* 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] | 6056 | int rc = -1, has_str = 0, tpdf_flag = 0, i, k; |
| 6057 | unsigned int j; |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 6058 | struct lys_node *node, *par_grp; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6059 | const char *expr; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6060 | |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6061 | struct ly_set *refs, *procs; |
| 6062 | struct lys_feature *ref, *feat; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6063 | struct lys_ident *ident; |
| 6064 | struct lys_type *stype; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6065 | struct lys_node_choice *choic; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6066 | struct lyxml_elem *yin; |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6067 | struct yang_type *yang; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6068 | struct unres_list_uniq *unique_info; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6069 | struct unres_iffeat_data *iff_data; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6070 | |
| 6071 | switch (type) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6072 | case UNRES_IDENT: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6073 | expr = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 6074 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6075 | ident = item; |
| 6076 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6077 | rc = resolve_base_ident(mod, ident, expr, "identity", NULL, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6078 | break; |
| 6079 | case UNRES_TYPE_IDENTREF: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6080 | expr = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 6081 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6082 | stype = item; |
| 6083 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6084 | rc = resolve_base_ident(mod, NULL, expr, "type", stype, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6085 | break; |
| 6086 | case UNRES_TYPE_LEAFREF: |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 6087 | node = str_snode; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6088 | stype = item; |
| 6089 | |
Radek Krejci | 2f12f85 | 2016-01-08 12:59:57 +0100 | [diff] [blame] | 6090 | /* HACK - when there is no parent, we are in top level typedef and in that |
| 6091 | * case, the path has to contain absolute path, so we let the resolve_path_arg_schema() |
| 6092 | * know it via tpdf_flag */ |
| 6093 | if (!node) { |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 6094 | tpdf_flag = 1; |
Radek Krejci | 2f12f85 | 2016-01-08 12:59:57 +0100 | [diff] [blame] | 6095 | node = (struct lys_node *)stype->parent; |
| 6096 | } |
| 6097 | |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 6098 | if (!lys_node_module(node)->implemented) { |
| 6099 | /* not implemented module, don't bother with resolving the leafref |
Radek Krejci | 990af1f | 2016-11-09 13:53:36 +0100 | [diff] [blame] | 6100 | * if the module is set to be implemented, the path will be resolved then */ |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 6101 | rc = 0; |
| 6102 | break; |
| 6103 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6104 | rc = resolve_path_arg_schema(stype->info.lref.path, node, tpdf_flag, |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 6105 | (const struct lys_node **)&stype->info.lref.target); |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 6106 | if (!tpdf_flag && !rc) { |
| 6107 | assert(stype->info.lref.target); |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6108 | /* check if leafref and its target are under a common if-features */ |
| 6109 | rc = check_leafref_features(stype); |
| 6110 | if (rc) { |
| 6111 | break; |
| 6112 | } |
| 6113 | |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 6114 | /* store the backlink from leafref target */ |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 6115 | if (lys_leaf_add_leafref_target(stype->info.lref.target, (struct lys_node *)stype->parent)) { |
| 6116 | rc = -1; |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 6117 | } |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 6118 | } |
| 6119 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6120 | break; |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 6121 | case UNRES_TYPE_DER_TPDF: |
| 6122 | tpdf_flag = 1; |
| 6123 | /* no break */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6124 | case UNRES_TYPE_DER: |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6125 | /* parent */ |
| 6126 | node = str_snode; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6127 | stype = item; |
| 6128 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6129 | /* HACK type->der is temporarily unparsed type statement */ |
| 6130 | yin = (struct lyxml_elem *)stype->der; |
| 6131 | stype->der = NULL; |
| 6132 | |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6133 | if (yin->flags & LY_YANG_STRUCTURE_FLAG) { |
| 6134 | yang = (struct yang_type *)yin; |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 6135 | rc = yang_check_type(mod, node, yang, tpdf_flag, unres); |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6136 | |
| 6137 | if (rc) { |
Pavol Vican | 8bd72e4 | 2016-08-29 09:53:05 +0200 | [diff] [blame] | 6138 | /* may try again later */ |
| 6139 | stype->der = (struct lys_tpdf *)yang; |
Pavol Vican | d01d8ae | 2016-03-01 10:45:59 +0100 | [diff] [blame] | 6140 | } else { |
| 6141 | /* 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] | 6142 | lydict_remove(mod->ctx, yang->name); |
Pavol Vican | d01d8ae | 2016-03-01 10:45:59 +0100 | [diff] [blame] | 6143 | free(yang); |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6144 | } |
| 6145 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6146 | } else { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 6147 | rc = fill_yin_type(mod, node, yin, stype, tpdf_flag, unres); |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6148 | if (!rc) { |
| 6149 | /* we need to always be able to free this, it's safe only in this case */ |
| 6150 | lyxml_free(mod->ctx, yin); |
| 6151 | } else { |
| 6152 | /* may try again later, put all back how it was */ |
| 6153 | stype->der = (struct lys_tpdf *)yin; |
| 6154 | } |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6155 | } |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 6156 | if (rc == EXIT_SUCCESS) { |
Radek Krejci | 2c2fce8 | 2016-08-01 13:52:26 +0200 | [diff] [blame] | 6157 | /* it does not make sense to have leaf-list of empty type */ |
| 6158 | if (!tpdf_flag && node->nodetype == LYS_LEAFLIST && stype->base == LY_TYPE_EMPTY) { |
| 6159 | LOGWRN("The leaf-list \"%s\" is of \"empty\" type, which does not make sense.", node->name); |
| 6160 | } |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6161 | } else if (rc == EXIT_FAILURE && stype->base != LY_TYPE_ERR) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 6162 | /* forward reference - in case the type is in grouping, we have to make the grouping unusable |
| 6163 | * by uses statement until the type is resolved. We do that the same way as uses statements inside |
| 6164 | * grouping - the grouping's nacm member (not used un grouping) is used to increase the number of |
| 6165 | * 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] | 6166 | * 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] | 6167 | * of the type's base member. */ |
| 6168 | for (par_grp = node; par_grp && (par_grp->nodetype != LYS_GROUPING); par_grp = lys_parent(par_grp)); |
| 6169 | if (par_grp) { |
| 6170 | ((struct lys_node_grp *)par_grp)->nacm++; |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6171 | stype->base = LY_TYPE_ERR; |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 6172 | } |
Radek Krejci | 2c2fce8 | 2016-08-01 13:52:26 +0200 | [diff] [blame] | 6173 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6174 | break; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6175 | case UNRES_IFFEAT: |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6176 | iff_data = str_snode; |
| 6177 | 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] | 6178 | if (!rc) { |
| 6179 | /* success */ |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 6180 | if (iff_data->infeature) { |
| 6181 | /* store backlink into the target feature to allow reverse changes in case of changing feature status */ |
| 6182 | feat = *((struct lys_feature **)item); |
| 6183 | if (!feat->depfeatures) { |
| 6184 | feat->depfeatures = ly_set_new(); |
| 6185 | } |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 6186 | ly_set_add(feat->depfeatures, iff_data->node, LY_SET_OPT_USEASLIST); |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 6187 | } |
| 6188 | /* cleanup temporary data */ |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6189 | lydict_remove(mod->ctx, iff_data->fname); |
| 6190 | free(iff_data); |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6191 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6192 | break; |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6193 | case UNRES_FEATURE: |
| 6194 | feat = (struct lys_feature *)item; |
| 6195 | |
| 6196 | if (feat->iffeature_size) { |
| 6197 | refs = ly_set_new(); |
| 6198 | procs = ly_set_new(); |
| 6199 | ly_set_add(procs, feat, 0); |
| 6200 | |
| 6201 | while (procs->number) { |
| 6202 | ref = procs->set.g[procs->number - 1]; |
| 6203 | ly_set_rm_index(procs, procs->number - 1); |
| 6204 | |
| 6205 | for (i = 0; i < ref->iffeature_size; i++) { |
| 6206 | resolve_iffeature_getsizes(&ref->iffeature[i], NULL, &j); |
| 6207 | for (; j > 0 ; j--) { |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6208 | if (ref->iffeature[i].features[j - 1]) { |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6209 | if (ref->iffeature[i].features[j - 1] == feat) { |
| 6210 | LOGVAL(LYE_CIRC_FEATURES, LY_VLOG_NONE, NULL, feat->name); |
| 6211 | goto featurecheckdone; |
| 6212 | } |
| 6213 | |
| 6214 | if (ref->iffeature[i].features[j - 1]->iffeature_size) { |
| 6215 | k = refs->number; |
| 6216 | if (ly_set_add(refs, ref->iffeature[i].features[j - 1], 0) == k) { |
| 6217 | /* not yet seen feature, add it for processing */ |
| 6218 | ly_set_add(procs, ref->iffeature[i].features[j - 1], 0); |
| 6219 | } |
| 6220 | } |
| 6221 | } else { |
| 6222 | /* forward reference */ |
| 6223 | rc = EXIT_FAILURE; |
| 6224 | goto featurecheckdone; |
| 6225 | } |
| 6226 | } |
| 6227 | |
| 6228 | } |
| 6229 | } |
| 6230 | rc = EXIT_SUCCESS; |
| 6231 | |
| 6232 | featurecheckdone: |
| 6233 | ly_set_free(refs); |
| 6234 | ly_set_free(procs); |
| 6235 | } |
| 6236 | |
| 6237 | break; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6238 | case UNRES_USES: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6239 | rc = resolve_unres_schema_uses(item, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6240 | break; |
| 6241 | case UNRES_TYPE_DFLT: |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6242 | stype = item; |
| 6243 | |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 6244 | rc = check_default(stype, (const char **)str_snode, mod); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6245 | break; |
| 6246 | case UNRES_CHOICE_DFLT: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6247 | expr = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 6248 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6249 | choic = item; |
| 6250 | |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 6251 | if (!choic->dflt) { |
| 6252 | choic->dflt = resolve_choice_dflt(choic, expr); |
| 6253 | } |
Michal Vasko | 7955b36 | 2015-09-04 14:18:15 +0200 | [diff] [blame] | 6254 | if (choic->dflt) { |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 6255 | rc = lyp_check_mandatory_choice((struct lys_node *)choic); |
Michal Vasko | 7955b36 | 2015-09-04 14:18:15 +0200 | [diff] [blame] | 6256 | } else { |
| 6257 | rc = EXIT_FAILURE; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6258 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6259 | break; |
| 6260 | case UNRES_LIST_KEYS: |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 6261 | rc = resolve_list_keys(item, ((struct lys_node_list *)item)->keys_str); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6262 | break; |
| 6263 | case UNRES_LIST_UNIQ: |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6264 | unique_info = (struct unres_list_uniq *)item; |
| 6265 | 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] | 6266 | break; |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 6267 | case UNRES_AUGMENT: |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 6268 | rc = resolve_augment(item, NULL, unres); |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 6269 | break; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6270 | case UNRES_XPATH: |
| 6271 | node = (struct lys_node *)item; |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 6272 | rc = lys_check_xpath(node, 1); |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6273 | break; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6274 | default: |
| 6275 | LOGINT; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6276 | break; |
| 6277 | } |
| 6278 | |
Radek Krejci | 54081ce | 2016-08-12 15:21:47 +0200 | [diff] [blame] | 6279 | if (has_str && !rc) { |
| 6280 | /* the string is no more needed in case of success. |
| 6281 | * 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] | 6282 | lydict_remove(mod->ctx, str_snode); |
| 6283 | } |
| 6284 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6285 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6286 | } |
| 6287 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6288 | /* logs directly */ |
| 6289 | static void |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6290 | 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] | 6291 | { |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 6292 | struct lyxml_elem *xml; |
| 6293 | struct lyxml_attr *attr; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6294 | struct unres_iffeat_data *iff_data; |
Radek Krejci | 76e15e1 | 2016-06-22 11:02:24 +0200 | [diff] [blame] | 6295 | const char *type_name = NULL; |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 6296 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6297 | switch (type) { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6298 | case UNRES_IDENT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6299 | 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] | 6300 | break; |
| 6301 | case UNRES_TYPE_IDENTREF: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6302 | 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] | 6303 | break; |
| 6304 | case UNRES_TYPE_LEAFREF: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6305 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "leafref", |
| 6306 | ((struct lys_type *)item)->info.lref.path); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6307 | break; |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 6308 | case UNRES_TYPE_DER_TPDF: |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6309 | case UNRES_TYPE_DER: |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 6310 | xml = (struct lyxml_elem *)((struct lys_type *)item)->der; |
| 6311 | if (xml->flags & LY_YANG_STRUCTURE_FLAG) { |
| 6312 | type_name = ((struct yang_type *)xml)->name; |
| 6313 | } else { |
| 6314 | LY_TREE_FOR(xml->attr, attr) { |
| 6315 | if ((attr->type == LYXML_ATTR_STD) && !strcmp(attr->name, "name")) { |
| 6316 | type_name = attr->value; |
| 6317 | break; |
| 6318 | } |
| 6319 | } |
| 6320 | assert(attr); |
| 6321 | } |
| 6322 | 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] | 6323 | break; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6324 | case UNRES_IFFEAT: |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6325 | iff_data = str_node; |
| 6326 | 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] | 6327 | break; |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6328 | case UNRES_FEATURE: |
| 6329 | LOGVRB("There are unresolved if-features for \"%s\" feature circular dependency check, it will be attempted later", |
| 6330 | ((struct lys_feature *)item)->name); |
| 6331 | break; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6332 | case UNRES_USES: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6333 | 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] | 6334 | break; |
| 6335 | case UNRES_TYPE_DFLT: |
Radek Krejci | 2e2de83 | 2016-10-13 16:12:26 +0200 | [diff] [blame] | 6336 | if (str_node) { |
| 6337 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "type default", (char *)str_node); |
| 6338 | } /* else no default value in the type itself, but we are checking some restrictions against |
| 6339 | * possible default value of some base type. The failure is caused by not resolved base type, |
| 6340 | * so it was already reported */ |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6341 | break; |
| 6342 | case UNRES_CHOICE_DFLT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6343 | 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] | 6344 | break; |
| 6345 | case UNRES_LIST_KEYS: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6346 | 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] | 6347 | break; |
| 6348 | case UNRES_LIST_UNIQ: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6349 | 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] | 6350 | break; |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 6351 | case UNRES_AUGMENT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6352 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "augment target", |
| 6353 | ((struct lys_node_augment *)item)->target_name); |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 6354 | break; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6355 | case UNRES_XPATH: |
Michal Vasko | 0d19837 | 2016-11-16 11:40:03 +0100 | [diff] [blame] | 6356 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "XPath expressions of", |
| 6357 | ((struct lys_node *)item)->name); |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6358 | break; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6359 | default: |
| 6360 | LOGINT; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6361 | break; |
| 6362 | } |
| 6363 | } |
| 6364 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6365 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6366 | * @brief Resolve every unres schema item in the structure. Logs directly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6367 | * |
| 6368 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6369 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6370 | * |
Michal Vasko | 92b8a38 | 2015-08-19 14:03:49 +0200 | [diff] [blame] | 6371 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6372 | */ |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6373 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 6374 | resolve_unres_schema(struct lys_module *mod, struct unres_schema *unres) |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6375 | { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6376 | uint32_t i, resolved = 0, unres_count, res_count; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6377 | int rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6378 | |
| 6379 | assert(unres); |
| 6380 | |
Michal Vasko | e873426 | 2016-09-29 14:12:06 +0200 | [diff] [blame] | 6381 | LOGVRB("Resolving \"%s\" unresolved schema nodes and their constraints...", mod->name); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6382 | ly_vlog_hide(1); |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6383 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6384 | /* uses */ |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6385 | do { |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6386 | unres_count = 0; |
| 6387 | res_count = 0; |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6388 | |
| 6389 | for (i = 0; i < unres->count; ++i) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6390 | /* UNRES_TYPE_LEAFREF must be resolved (for storing leafref target pointers); |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6391 | * if-features are resolved here to make sure that we will have all if-features for |
| 6392 | * later check of feature circular dependency */ |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6393 | if (unres->type[i] > UNRES_IDENT) { |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6394 | continue; |
| 6395 | } |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6396 | /* processes UNRES_USES, UNRES_IFFEAT, UNRES_TYPE_DER, UNRES_TYPE_DER_TPDF, UNRES_TYPE_LEAFREF, |
Radek Krejci | 818b0c5 | 2016-11-09 15:10:51 +0100 | [diff] [blame] | 6397 | * UNRES_AUGMENT, UNRES_CHOICE_DFLT and UNRES_IDENT */ |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6398 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6399 | ++unres_count; |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6400 | 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] | 6401 | if (!rc) { |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6402 | unres->type[i] = UNRES_RESOLVED; |
| 6403 | ++resolved; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6404 | ++res_count; |
Michal Vasko | 89e1532 | 2015-08-17 15:46:55 +0200 | [diff] [blame] | 6405 | } else if (rc == -1) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6406 | ly_vlog_hide(0); |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6407 | /* print the error */ |
| 6408 | 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] | 6409 | return -1; |
Radek Krejci | c2a180f | 2016-06-22 13:28:16 +0200 | [diff] [blame] | 6410 | } else { |
| 6411 | /* forward reference, erase ly_errno */ |
Radek Krejci | 00a0e71 | 2016-10-26 10:24:46 +0200 | [diff] [blame] | 6412 | ly_err_clean(1); |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6413 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6414 | } |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6415 | } while (res_count && (res_count < unres_count)); |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6416 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6417 | if (res_count < unres_count) { |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6418 | /* just print the errors */ |
| 6419 | ly_vlog_hide(0); |
| 6420 | |
| 6421 | for (i = 0; i < unres->count; ++i) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6422 | if (unres->type[i] > UNRES_IDENT) { |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6423 | continue; |
| 6424 | } |
| 6425 | resolve_unres_schema_item(mod, unres->item[i], unres->type[i], unres->str_snode[i], unres); |
| 6426 | } |
Michal Vasko | 92b8a38 | 2015-08-19 14:03:49 +0200 | [diff] [blame] | 6427 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6428 | } |
| 6429 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6430 | /* the rest */ |
| 6431 | for (i = 0; i < unres->count; ++i) { |
| 6432 | if (unres->type[i] == UNRES_RESOLVED) { |
| 6433 | continue; |
| 6434 | } |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 6435 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6436 | 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] | 6437 | if (rc == 0) { |
Pavol Vican | 88e16c9 | 2016-09-07 15:41:50 +0200 | [diff] [blame] | 6438 | if (unres->type[i] == UNRES_LIST_UNIQ) { |
| 6439 | /* free the allocated structure */ |
| 6440 | free(unres->item[i]); |
| 6441 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6442 | unres->type[i] = UNRES_RESOLVED; |
| 6443 | ++resolved; |
| 6444 | } else if (rc == -1) { |
| 6445 | ly_vlog_hide(0); |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6446 | /* print the error */ |
| 6447 | resolve_unres_schema_item(mod, unres->item[i], unres->type[i], unres->str_snode[i], unres); |
| 6448 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6449 | } |
| 6450 | } |
| 6451 | |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6452 | ly_vlog_hide(0); |
| 6453 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6454 | if (resolved < unres->count) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6455 | /* try to resolve the unresolved nodes again, it will not resolve anything, but it will print |
| 6456 | * all the validation errors |
| 6457 | */ |
| 6458 | for (i = 0; i < unres->count; ++i) { |
| 6459 | if (unres->type[i] == UNRES_RESOLVED) { |
| 6460 | continue; |
| 6461 | } |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 6462 | if (unres->type[i] == UNRES_XPATH) { |
| 6463 | /* unresolvable XPaths are actually supposed to be warnings - they may be |
| 6464 | * unresolved due to the not implemented target module so it shouldn't avoid |
| 6465 | * parsing the module, but we still want to announce some issue here */ |
| 6466 | ly_vlog_hide(0xff); |
| 6467 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6468 | resolve_unres_schema_item(mod, unres->item[i], unres->type[i], unres->str_snode[i], unres); |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 6469 | if (unres->type[i] == UNRES_XPATH && *ly_vlog_hide_location() == 0xff) { |
| 6470 | unres->type[i] = UNRES_RESOLVED; |
| 6471 | resolved++; |
| 6472 | ly_vlog_hide(0); |
| 6473 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6474 | } |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 6475 | if (resolved < unres->count) { |
| 6476 | return -1; |
| 6477 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6478 | } |
| 6479 | |
Michal Vasko | e873426 | 2016-09-29 14:12:06 +0200 | [diff] [blame] | 6480 | LOGVRB("All \"%s\" schema nodes and constraints resolved.", mod->name); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6481 | unres->count = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6482 | return EXIT_SUCCESS; |
| 6483 | } |
| 6484 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6485 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6486 | * @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] | 6487 | * |
| 6488 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6489 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6490 | * @param[in] item Item to resolve. Type determined by \p type. |
| 6491 | * @param[in] type Type of the unresolved item. |
| 6492 | * @param[in] str String argument. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6493 | * |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 6494 | * @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] | 6495 | */ |
| 6496 | int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6497 | unres_schema_add_str(struct lys_module *mod, struct unres_schema *unres, void *item, enum UNRES_ITEM type, |
| 6498 | const char *str) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6499 | { |
Radek Krejci | 54081ce | 2016-08-12 15:21:47 +0200 | [diff] [blame] | 6500 | int rc; |
| 6501 | const char *dictstr; |
| 6502 | |
| 6503 | dictstr = lydict_insert(mod->ctx, str, 0); |
| 6504 | rc = unres_schema_add_node(mod, unres, item, type, (struct lys_node *)dictstr); |
| 6505 | |
| 6506 | if (rc == -1) { |
| 6507 | lydict_remove(mod->ctx, dictstr); |
| 6508 | } |
| 6509 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6510 | } |
| 6511 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6512 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6513 | * @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] | 6514 | * |
| 6515 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6516 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6517 | * @param[in] item Item to resolve. Type determined by \p type. |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6518 | * @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] | 6519 | * @param[in] snode Schema node argument. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6520 | * |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 6521 | * @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] | 6522 | */ |
| 6523 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 6524 | 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] | 6525 | struct lys_node *snode) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6526 | { |
Michal Vasko | ef486d7 | 2016-09-27 12:10:44 +0200 | [diff] [blame] | 6527 | int rc, log_hidden; |
Radek Krejci | 850a5de | 2016-11-08 14:06:40 +0100 | [diff] [blame] | 6528 | uint32_t u; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6529 | struct lyxml_elem *yin; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6530 | |
Michal Vasko | 9bf425b | 2015-10-22 11:42:03 +0200 | [diff] [blame] | 6531 | assert(unres && item && ((type != UNRES_LEAFREF) && (type != UNRES_INSTID) && (type != UNRES_WHEN) |
| 6532 | && (type != UNRES_MUST))); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6533 | |
Radek Krejci | 850a5de | 2016-11-08 14:06:40 +0100 | [diff] [blame] | 6534 | /* check for duplicities in unres */ |
| 6535 | for (u = 0; u < unres->count; u++) { |
| 6536 | if (unres->type[u] == type && unres->item[u] == item && |
| 6537 | unres->str_snode[u] == snode && unres->module[u] == mod) { |
| 6538 | /* duplication, will be resolved later */ |
| 6539 | return EXIT_FAILURE; |
| 6540 | } |
| 6541 | } |
| 6542 | |
Michal Vasko | ef486d7 | 2016-09-27 12:10:44 +0200 | [diff] [blame] | 6543 | if (*ly_vlog_hide_location()) { |
| 6544 | log_hidden = 1; |
| 6545 | } else { |
| 6546 | log_hidden = 0; |
| 6547 | ly_vlog_hide(1); |
| 6548 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6549 | rc = resolve_unres_schema_item(mod, item, type, snode, unres); |
Michal Vasko | ef486d7 | 2016-09-27 12:10:44 +0200 | [diff] [blame] | 6550 | if (!log_hidden) { |
| 6551 | ly_vlog_hide(0); |
| 6552 | } |
| 6553 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6554 | if (rc != EXIT_FAILURE) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6555 | if (rc == -1 && ly_errno == LY_EVALID) { |
Radek Krejci | 2467a49 | 2016-10-24 15:16:59 +0200 | [diff] [blame] | 6556 | ly_err_repeat(); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6557 | } |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6558 | if (type == UNRES_LIST_UNIQ) { |
| 6559 | /* free the allocated structure */ |
| 6560 | free(item); |
Pavol Vican | 88e16c9 | 2016-09-07 15:41:50 +0200 | [diff] [blame] | 6561 | } else if (rc == -1 && type == UNRES_IFFEAT) { |
| 6562 | /* free the allocated resources */ |
| 6563 | free(*((char **)item)); |
| 6564 | } |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6565 | return rc; |
Radek Krejci | f347abc | 2016-06-22 10:18:47 +0200 | [diff] [blame] | 6566 | } else { |
| 6567 | /* erase info about validation errors */ |
Radek Krejci | 00a0e71 | 2016-10-26 10:24:46 +0200 | [diff] [blame] | 6568 | ly_err_clean(1); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6569 | } |
| 6570 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6571 | print_unres_schema_item_fail(item, type, snode); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6572 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6573 | /* 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] | 6574 | if (type == UNRES_TYPE_DER || type == UNRES_TYPE_DER_TPDF) { |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6575 | yin = (struct lyxml_elem *)((struct lys_type *)item)->der; |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6576 | if (!(yin->flags & LY_YANG_STRUCTURE_FLAG)) { |
| 6577 | lyxml_unlink_elem(mod->ctx, yin, 1); |
| 6578 | ((struct lys_type *)item)->der = (struct lys_tpdf *)yin; |
| 6579 | } |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6580 | } |
| 6581 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6582 | unres->count++; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 6583 | unres->item = ly_realloc(unres->item, unres->count*sizeof *unres->item); |
| 6584 | if (!unres->item) { |
| 6585 | LOGMEM; |
| 6586 | return -1; |
| 6587 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6588 | unres->item[unres->count-1] = item; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 6589 | unres->type = ly_realloc(unres->type, unres->count*sizeof *unres->type); |
| 6590 | if (!unres->type) { |
| 6591 | LOGMEM; |
| 6592 | return -1; |
| 6593 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6594 | unres->type[unres->count-1] = type; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 6595 | unres->str_snode = ly_realloc(unres->str_snode, unres->count*sizeof *unres->str_snode); |
| 6596 | if (!unres->str_snode) { |
| 6597 | LOGMEM; |
| 6598 | return -1; |
| 6599 | } |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6600 | unres->str_snode[unres->count-1] = snode; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6601 | unres->module = ly_realloc(unres->module, unres->count*sizeof *unres->module); |
| 6602 | if (!unres->module) { |
| 6603 | LOGMEM; |
| 6604 | return -1; |
| 6605 | } |
| 6606 | unres->module[unres->count-1] = mod; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6607 | |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 6608 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6609 | } |
| 6610 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6611 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6612 | * @brief Duplicate an unres schema item. Logs indirectly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6613 | * |
| 6614 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6615 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6616 | * @param[in] item Old item to be resolved. |
| 6617 | * @param[in] type Type of the old unresolved item. |
| 6618 | * @param[in] new_item New item to use in the duplicate. |
| 6619 | * |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 6620 | * @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] | 6621 | */ |
Michal Vasko | dad1940 | 2015-08-06 09:51:53 +0200 | [diff] [blame] | 6622 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 6623 | 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] | 6624 | { |
| 6625 | int i; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6626 | struct unres_list_uniq aux_uniq; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6627 | struct unres_iffeat_data *iff_data; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6628 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6629 | 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] | 6630 | |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6631 | /* hack for UNRES_LIST_UNIQ, which stores multiple items behind its item */ |
| 6632 | if (type == UNRES_LIST_UNIQ) { |
| 6633 | aux_uniq.list = item; |
| 6634 | aux_uniq.expr = ((struct unres_list_uniq *)new_item)->expr; |
| 6635 | item = &aux_uniq; |
| 6636 | } |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 6637 | i = unres_schema_find(unres, -1, item, type); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6638 | |
| 6639 | if (i == -1) { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6640 | if (type == UNRES_LIST_UNIQ) { |
| 6641 | free(new_item); |
| 6642 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 6643 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6644 | } |
| 6645 | |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6646 | if ((type == UNRES_TYPE_LEAFREF) || (type == UNRES_USES) || (type == UNRES_TYPE_DFLT) || |
Radek Krejci | b69f323 | 2016-09-16 17:41:07 +0200 | [diff] [blame] | 6647 | (type == UNRES_FEATURE) || (type == UNRES_LIST_UNIQ)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6648 | 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] | 6649 | LOGINT; |
| 6650 | return -1; |
| 6651 | } |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6652 | } else if (type == UNRES_IFFEAT) { |
| 6653 | /* duplicate unres_iffeature_data */ |
| 6654 | iff_data = malloc(sizeof *iff_data); |
| 6655 | iff_data->fname = lydict_insert(mod->ctx, ((struct unres_iffeat_data *)unres->str_snode[i])->fname, 0); |
| 6656 | iff_data->node = ((struct unres_iffeat_data *)unres->str_snode[i])->node; |
| 6657 | if (unres_schema_add_node(mod, unres, new_item, type, (struct lys_node *)iff_data) == -1) { |
| 6658 | LOGINT; |
| 6659 | return -1; |
| 6660 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6661 | } else { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6662 | 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] | 6663 | LOGINT; |
| 6664 | return -1; |
| 6665 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6666 | } |
Michal Vasko | dad1940 | 2015-08-06 09:51:53 +0200 | [diff] [blame] | 6667 | |
| 6668 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6669 | } |
| 6670 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6671 | /* does not log */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6672 | int |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 6673 | 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] | 6674 | { |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 6675 | int i; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6676 | struct unres_list_uniq *aux_uniq1, *aux_uniq2; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6677 | |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 6678 | if (start_on_backwards > 0) { |
| 6679 | i = start_on_backwards; |
| 6680 | } else { |
| 6681 | i = unres->count - 1; |
| 6682 | } |
| 6683 | for (; i > -1; i--) { |
| 6684 | if (unres->type[i] != type) { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6685 | continue; |
| 6686 | } |
| 6687 | if (type != UNRES_LIST_UNIQ) { |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 6688 | if (unres->item[i] == item) { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6689 | break; |
| 6690 | } |
| 6691 | } else { |
| 6692 | aux_uniq1 = (struct unres_list_uniq *)unres->item[i - 1]; |
| 6693 | aux_uniq2 = (struct unres_list_uniq *)item; |
| 6694 | 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] | 6695 | break; |
| 6696 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6697 | } |
| 6698 | } |
| 6699 | |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 6700 | return i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6701 | } |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6702 | |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 6703 | static void |
| 6704 | unres_schema_free_item(struct ly_ctx *ctx, struct unres_schema *unres, uint32_t i) |
| 6705 | { |
| 6706 | struct lyxml_elem *yin; |
| 6707 | struct yang_type *yang; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6708 | struct unres_iffeat_data *iff_data; |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 6709 | |
| 6710 | switch (unres->type[i]) { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 6711 | case UNRES_TYPE_DER_TPDF: |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 6712 | case UNRES_TYPE_DER: |
| 6713 | yin = (struct lyxml_elem *)((struct lys_type *)unres->item[i])->der; |
| 6714 | if (yin->flags & LY_YANG_STRUCTURE_FLAG) { |
| 6715 | yang =(struct yang_type *)yin; |
| 6716 | yang->type->base = yang->base; |
| 6717 | lydict_remove(ctx, yang->name); |
| 6718 | free(yang); |
| 6719 | } else { |
| 6720 | lyxml_free(ctx, yin); |
| 6721 | } |
| 6722 | break; |
Pavol Vican | 88e16c9 | 2016-09-07 15:41:50 +0200 | [diff] [blame] | 6723 | case UNRES_IFFEAT: |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6724 | iff_data = (struct unres_iffeat_data *)unres->str_snode[i]; |
| 6725 | lydict_remove(ctx, iff_data->fname); |
| 6726 | free(unres->str_snode[i]); |
Pavol Vican | 88e16c9 | 2016-09-07 15:41:50 +0200 | [diff] [blame] | 6727 | break; |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 6728 | case UNRES_IDENT: |
| 6729 | case UNRES_TYPE_IDENTREF: |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 6730 | case UNRES_CHOICE_DFLT: |
| 6731 | case UNRES_LIST_KEYS: |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 6732 | lydict_remove(ctx, (const char *)unres->str_snode[i]); |
| 6733 | break; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6734 | case UNRES_LIST_UNIQ: |
| 6735 | free(unres->item[i]); |
| 6736 | break; |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 6737 | default: |
| 6738 | break; |
| 6739 | } |
| 6740 | unres->type[i] = UNRES_RESOLVED; |
| 6741 | } |
| 6742 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6743 | void |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6744 | unres_schema_free(struct lys_module *module, struct unres_schema **unres) |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6745 | { |
| 6746 | uint32_t i; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6747 | unsigned int unresolved = 0; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6748 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6749 | if (!unres || !(*unres)) { |
| 6750 | return; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6751 | } |
| 6752 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6753 | assert(module || (*unres)->count == 0); |
| 6754 | |
| 6755 | for (i = 0; i < (*unres)->count; ++i) { |
| 6756 | if ((*unres)->module[i] != module) { |
| 6757 | if ((*unres)->type[i] != UNRES_RESOLVED) { |
| 6758 | unresolved++; |
| 6759 | } |
| 6760 | continue; |
| 6761 | } |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 6762 | |
| 6763 | /* free heap memory for the specific item */ |
| 6764 | unres_schema_free_item(module->ctx, *unres, i); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6765 | } |
| 6766 | |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 6767 | /* free it all */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6768 | if (!module || (!unresolved && !module->type)) { |
| 6769 | free((*unres)->item); |
| 6770 | free((*unres)->type); |
| 6771 | free((*unres)->str_snode); |
| 6772 | free((*unres)->module); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6773 | free((*unres)); |
| 6774 | (*unres) = NULL; |
| 6775 | } |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6776 | } |
| 6777 | |
Radek Krejci | 1899d6a | 2016-11-03 13:48:07 +0100 | [diff] [blame] | 6778 | int |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6779 | resolve_leafref(struct lyd_node_leaf_list *leaf, struct lys_type *type) |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 6780 | { |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 6781 | struct unres_data matches; |
| 6782 | uint32_t i; |
| 6783 | |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6784 | assert(type->base == LY_TYPE_LEAFREF); |
| 6785 | |
| 6786 | /* init */ |
Michal Vasko | 16dc4f3 | 2016-12-09 09:43:50 +0100 | [diff] [blame] | 6787 | leaf->value.leafref = NULL; |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 6788 | memset(&matches, 0, sizeof matches); |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 6789 | |
| 6790 | /* EXIT_FAILURE return keeps leaf->value.lefref NULL, handled later */ |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6791 | 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] | 6792 | return -1; |
| 6793 | } |
| 6794 | |
| 6795 | /* check that value matches */ |
| 6796 | for (i = 0; i < matches.count; ++i) { |
Radek Krejci | 1899d6a | 2016-11-03 13:48:07 +0100 | [diff] [blame] | 6797 | /* not that the value is already in canonical form since the parsers does the conversion, |
| 6798 | * so we can simply compare just the values */ |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 6799 | 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] | 6800 | /* we have the match */ |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 6801 | leaf->value.leafref = matches.node[i]; |
| 6802 | break; |
| 6803 | } |
| 6804 | } |
| 6805 | |
| 6806 | free(matches.node); |
| 6807 | |
| 6808 | if (!leaf->value.leafref) { |
| 6809 | /* reference not found */ |
| 6810 | if (type->info.lref.req > -1) { |
| 6811 | LOGVAL(LYE_NOLEAFREF, LY_VLOG_LYD, leaf, type->info.lref.path, leaf->value_str); |
| 6812 | return EXIT_FAILURE; |
| 6813 | } else { |
| 6814 | LOGVRB("There is no leafref with the value \"%s\", but it is not required.", leaf->value_str); |
| 6815 | } |
| 6816 | } |
| 6817 | |
| 6818 | return EXIT_SUCCESS; |
| 6819 | } |
| 6820 | |
Radek Krejci | 9ad23f4 | 2016-10-31 15:46:52 +0100 | [diff] [blame] | 6821 | API const struct lys_type * |
Radek Krejci | 1899d6a | 2016-11-03 13:48:07 +0100 | [diff] [blame] | 6822 | lyd_leaf_type(struct lyd_node_leaf_list *leaf, int resolve) |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6823 | { |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6824 | if (!leaf || !(leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Radek Krejci | 9ad23f4 | 2016-10-31 15:46:52 +0100 | [diff] [blame] | 6825 | return NULL; |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6826 | } |
Radek Krejci | 1899d6a | 2016-11-03 13:48:07 +0100 | [diff] [blame] | 6827 | if (((struct lys_node_leaf *)leaf->schema)->type.base == LY_TYPE_BITS) { |
| 6828 | free(leaf->value.bit); |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6829 | } |
Radek Krejci | 1899d6a | 2016-11-03 13:48:07 +0100 | [diff] [blame] | 6830 | memset(&leaf->value, 0, sizeof leaf->value); |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6831 | |
Radek Krejci | 1899d6a | 2016-11-03 13:48:07 +0100 | [diff] [blame] | 6832 | /* resolve */ |
| 6833 | return lyp_parse_value(&((struct lys_node_leaf *)leaf->schema)->type, (const char **)&leaf->value_str, NULL, |
Radek Krejci | bb1ce0f | 2016-12-05 13:24:33 +0100 | [diff] [blame] | 6834 | (struct lyd_node *)leaf, leaf, resolve, 1, 0); |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6835 | } |
| 6836 | |
| 6837 | static int |
| 6838 | resolve_union(struct lyd_node_leaf_list *leaf, struct lys_type *type) |
| 6839 | { |
| 6840 | struct lys_type *datatype = NULL; |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6841 | |
| 6842 | assert(type->base == LY_TYPE_UNION); |
| 6843 | |
| 6844 | memset(&leaf->value, 0, sizeof leaf->value); |
Radek Krejci | bb1ce0f | 2016-12-05 13:24:33 +0100 | [diff] [blame] | 6845 | datatype = lyp_parse_value(type, &leaf->value_str, NULL, (struct lyd_node *)leaf, leaf, 1, 1, 0); |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6846 | if (!datatype) { |
| 6847 | /* failure */ |
| 6848 | LOGVAL(LYE_INVAL, LY_VLOG_LYD, leaf, (leaf->value_str ? leaf->value_str : ""), leaf->schema->name); |
| 6849 | return EXIT_FAILURE; |
| 6850 | } |
| 6851 | |
| 6852 | return EXIT_SUCCESS; |
| 6853 | } |
| 6854 | |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6855 | /** |
| 6856 | * @brief Resolve a single unres data item. Logs directly. |
| 6857 | * |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6858 | * @param[in] node Data node to resolve. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6859 | * @param[in] type Type of the unresolved item. |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6860 | * |
| 6861 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 6862 | */ |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 6863 | int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6864 | resolve_unres_data_item(struct lyd_node *node, enum UNRES_ITEM type) |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6865 | { |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 6866 | int rc; |
Michal Vasko | 83a6c46 | 2015-10-08 16:43:53 +0200 | [diff] [blame] | 6867 | struct lyd_node_leaf_list *leaf; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6868 | struct lys_node_leaf *sleaf; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6869 | |
Michal Vasko | 83a6c46 | 2015-10-08 16:43:53 +0200 | [diff] [blame] | 6870 | leaf = (struct lyd_node_leaf_list *)node; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6871 | sleaf = (struct lys_node_leaf *)leaf->schema; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6872 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6873 | switch (type) { |
| 6874 | case UNRES_LEAFREF: |
| 6875 | assert(sleaf->type.base == LY_TYPE_LEAFREF); |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6876 | return resolve_leafref(leaf, &sleaf->type); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6877 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6878 | case UNRES_INSTID: |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 6879 | assert(sleaf->type.base == LY_TYPE_INST); |
Radek Krejci | 00a0e71 | 2016-10-26 10:24:46 +0200 | [diff] [blame] | 6880 | ly_err_clean(1); |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6881 | leaf->value.instance = resolve_instid(node, leaf->value_str); |
Radek Krejci | 40f17b9 | 2016-02-03 14:30:43 +0100 | [diff] [blame] | 6882 | if (!leaf->value.instance) { |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6883 | if (ly_errno) { |
| 6884 | return -1; |
| 6885 | } else if (sleaf->type.info.inst.req > -1) { |
Michal Vasko | 6ac6828 | 2016-04-11 10:56:47 +0200 | [diff] [blame] | 6886 | LOGVAL(LYE_NOREQINS, LY_VLOG_LYD, leaf, leaf->value_str); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6887 | return EXIT_FAILURE; |
| 6888 | } else { |
Michal Vasko | 9925e28 | 2016-09-02 12:45:14 +0200 | [diff] [blame] | 6889 | 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] | 6890 | } |
| 6891 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6892 | break; |
| 6893 | |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 6894 | case UNRES_UNION: |
| 6895 | assert(sleaf->type.base == LY_TYPE_UNION); |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6896 | return resolve_union(leaf, &sleaf->type); |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 6897 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6898 | case UNRES_WHEN: |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 6899 | if ((rc = resolve_when(node, NULL))) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6900 | return rc; |
| 6901 | } |
| 6902 | break; |
| 6903 | |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 6904 | case UNRES_MUST: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6905 | if ((rc = resolve_must(node, 0))) { |
| 6906 | return rc; |
| 6907 | } |
| 6908 | break; |
| 6909 | |
| 6910 | case UNRES_MUST_INOUT: |
| 6911 | if ((rc = resolve_must(node, 1))) { |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 6912 | return rc; |
| 6913 | } |
| 6914 | break; |
| 6915 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6916 | default: |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6917 | LOGINT; |
| 6918 | return -1; |
| 6919 | } |
| 6920 | |
| 6921 | return EXIT_SUCCESS; |
| 6922 | } |
| 6923 | |
| 6924 | /** |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6925 | * @brief add data unres item |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6926 | * |
| 6927 | * @param[in] unres Unres data structure to use. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6928 | * @param[in] node Data node to use. |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6929 | * |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6930 | * @return 0 on success, -1 on error. |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6931 | */ |
| 6932 | int |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6933 | 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] | 6934 | { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6935 | assert(unres && node); |
Michal Vasko | c428084 | 2016-04-19 16:10:42 +0200 | [diff] [blame] | 6936 | 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] | 6937 | || (type == UNRES_MUST_INOUT) || (type == UNRES_UNION)); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6938 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6939 | unres->count++; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 6940 | unres->node = ly_realloc(unres->node, unres->count * sizeof *unres->node); |
| 6941 | if (!unres->node) { |
| 6942 | LOGMEM; |
| 6943 | return -1; |
| 6944 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6945 | unres->node[unres->count - 1] = node; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 6946 | unres->type = ly_realloc(unres->type, unres->count * sizeof *unres->type); |
| 6947 | if (!unres->type) { |
| 6948 | LOGMEM; |
| 6949 | return -1; |
| 6950 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6951 | unres->type[unres->count - 1] = type; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6952 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6953 | if (type == UNRES_WHEN) { |
| 6954 | /* remove previous result */ |
| 6955 | node->when_status = LYD_WHEN; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6956 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6957 | |
| 6958 | return EXIT_SUCCESS; |
| 6959 | } |
| 6960 | |
| 6961 | /** |
| 6962 | * @brief Resolve every unres data item in the structure. Logs directly. |
| 6963 | * |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 6964 | * If options includes LYD_OPT_TRUSTED, the data are considered trusted (when, must conditions are not expected, |
| 6965 | * unresolved leafrefs/instids are accepted). |
| 6966 | * |
| 6967 | * If options includes LYD_OPT_NOAUTODEL, the false resulting when condition on non-default nodes, the error is raised. |
| 6968 | * |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6969 | * @param[in] unres Unres data structure to use. |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 6970 | * @param[in,out] root Root node of the data tree, can be changed due to autodeletion. |
| 6971 | * @param[in] options Data options as described above. |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6972 | * |
| 6973 | * @return EXIT_SUCCESS on success, -1 on error. |
| 6974 | */ |
| 6975 | int |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 6976 | 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] | 6977 | { |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 6978 | 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] | 6979 | int rc, progress; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6980 | struct lyd_node *parent; |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 6981 | struct lyd_node_leaf_list *leaf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6982 | |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 6983 | assert(root); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6984 | assert(unres); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6985 | |
| 6986 | if (!unres->count) { |
| 6987 | return EXIT_SUCCESS; |
| 6988 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6989 | |
Michal Vasko | a0ffcab | 2016-05-02 14:52:08 +0200 | [diff] [blame] | 6990 | LOGVRB("Resolving unresolved data nodes and their constraints..."); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6991 | ly_vlog_hide(1); |
| 6992 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6993 | /* when-stmt first */ |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6994 | do { |
Radek Krejci | 00a0e71 | 2016-10-26 10:24:46 +0200 | [diff] [blame] | 6995 | ly_err_clean(1); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6996 | progress = 0; |
Michal Vasko | 6df9413 | 2016-09-22 11:08:09 +0200 | [diff] [blame] | 6997 | for (i = 0; i < unres->count; i++) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6998 | if (unres->type[i] != UNRES_WHEN) { |
| 6999 | continue; |
| 7000 | } |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 7001 | assert(!(options & LYD_OPT_TRUSTED)); |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7002 | if (first) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7003 | /* count when-stmt nodes in unres list */ |
| 7004 | when_stmt++; |
| 7005 | } |
| 7006 | |
| 7007 | /* resolve when condition only when all parent when conditions are already resolved */ |
| 7008 | for (parent = unres->node[i]->parent; |
| 7009 | parent && LYD_WHEN_DONE(parent->when_status); |
| 7010 | parent = parent->parent) { |
| 7011 | if (!parent->parent && (parent->when_status & LYD_WHEN_FALSE)) { |
| 7012 | /* the parent node was already unlinked, do not resolve this node, |
| 7013 | * it will be removed anyway, so just mark it as resolved |
| 7014 | */ |
| 7015 | unres->node[i]->when_status |= LYD_WHEN_FALSE; |
| 7016 | unres->type[i] = UNRES_RESOLVED; |
| 7017 | resolved++; |
| 7018 | break; |
| 7019 | } |
| 7020 | } |
| 7021 | if (parent) { |
| 7022 | continue; |
| 7023 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7024 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7025 | rc = resolve_unres_data_item(unres->node[i], unres->type[i]); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7026 | if (!rc) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7027 | if (unres->node[i]->when_status & LYD_WHEN_FALSE) { |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 7028 | if ((options & LYD_OPT_NOAUTODEL) && !unres->node[i]->dflt) { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7029 | /* false when condition */ |
| 7030 | ly_vlog_hide(0); |
Radek Krejci | 2467a49 | 2016-10-24 15:16:59 +0200 | [diff] [blame] | 7031 | ly_err_repeat(); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7032 | return -1; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7033 | } /* follows else */ |
| 7034 | |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7035 | /* only unlink now, the subtree can contain another nodes stored in the unres list */ |
| 7036 | /* if it has parent non-presence containers that would be empty, we should actually |
| 7037 | * remove the container |
| 7038 | */ |
Radek Krejci | 2537fd3 | 2016-09-07 16:22:41 +0200 | [diff] [blame] | 7039 | for (parent = unres->node[i]; |
| 7040 | parent->parent && parent->parent->schema->nodetype == LYS_CONTAINER; |
| 7041 | parent = parent->parent) { |
| 7042 | if (((struct lys_node_container *)parent->parent->schema)->presence) { |
| 7043 | /* presence container */ |
| 7044 | break; |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7045 | } |
Radek Krejci | 2537fd3 | 2016-09-07 16:22:41 +0200 | [diff] [blame] | 7046 | if (parent->next || parent->prev != parent) { |
| 7047 | /* non empty (the child we are in and we are going to remove is not the only child) */ |
| 7048 | break; |
| 7049 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7050 | } |
Radek Krejci | 2537fd3 | 2016-09-07 16:22:41 +0200 | [diff] [blame] | 7051 | unres->node[i] = parent; |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7052 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7053 | /* auto-delete */ |
| 7054 | LOGVRB("auto-delete node \"%s\" due to when condition (%s)", ly_errpath(), |
| 7055 | ((struct lys_node_leaf *)unres->node[i]->schema)->when->cond); |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7056 | if (*root && *root == unres->node[i]) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7057 | *root = (*root)->next; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7058 | } |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7059 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7060 | lyd_unlink(unres->node[i]); |
| 7061 | unres->type[i] = UNRES_DELETE; |
| 7062 | del_items++; |
Radek Krejci | 51fd816 | 2016-03-24 15:49:51 +0100 | [diff] [blame] | 7063 | |
| 7064 | /* update the rest of unres items */ |
| 7065 | for (j = 0; j < unres->count; j++) { |
Radek Krejci | 3db819b | 2016-03-24 16:29:48 +0100 | [diff] [blame] | 7066 | if (unres->type[j] == UNRES_RESOLVED || unres->type[j] == UNRES_DELETE) { |
Radek Krejci | 51fd816 | 2016-03-24 15:49:51 +0100 | [diff] [blame] | 7067 | continue; |
| 7068 | } |
| 7069 | |
| 7070 | /* test if the node is in subtree to be deleted */ |
| 7071 | for (parent = unres->node[j]; parent; parent = parent->parent) { |
| 7072 | if (parent == unres->node[i]) { |
| 7073 | /* yes, it is */ |
| 7074 | unres->type[j] = UNRES_RESOLVED; |
| 7075 | resolved++; |
| 7076 | break; |
| 7077 | } |
| 7078 | } |
| 7079 | } |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7080 | } else { |
| 7081 | unres->type[i] = UNRES_RESOLVED; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7082 | } |
Radek Krejci | 00a0e71 | 2016-10-26 10:24:46 +0200 | [diff] [blame] | 7083 | ly_err_clean(1); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7084 | resolved++; |
| 7085 | progress = 1; |
| 7086 | } else if (rc == -1) { |
| 7087 | ly_vlog_hide(0); |
Michal Vasko | 76e7340 | 2016-08-24 16:00:13 +0200 | [diff] [blame] | 7088 | /* print only this last error */ |
| 7089 | resolve_unres_data_item(unres->node[i], unres->type[i]); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7090 | return -1; |
Radek Krejci | 2467a49 | 2016-10-24 15:16:59 +0200 | [diff] [blame] | 7091 | } /* else forward reference */ |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7092 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7093 | first = 0; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7094 | } while (progress && resolved < when_stmt); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7095 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7096 | /* do we have some unresolved when-stmt? */ |
Radek Krejci | d940d73 | 2016-03-24 16:02:28 +0100 | [diff] [blame] | 7097 | if (when_stmt > resolved) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7098 | ly_vlog_hide(0); |
Radek Krejci | 2467a49 | 2016-10-24 15:16:59 +0200 | [diff] [blame] | 7099 | ly_err_repeat(); |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7100 | return -1; |
| 7101 | } |
| 7102 | |
| 7103 | for (i = 0; del_items && i < unres->count; i++) { |
| 7104 | /* we had some when-stmt resulted to false, so now we have to sanitize the unres list */ |
| 7105 | if (unres->type[i] != UNRES_DELETE) { |
| 7106 | continue; |
| 7107 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7108 | if (!unres->node[i]) { |
| 7109 | unres->type[i] = UNRES_RESOLVED; |
| 7110 | del_items--; |
| 7111 | continue; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7112 | } |
| 7113 | |
| 7114 | /* really remove the complete subtree */ |
| 7115 | lyd_free(unres->node[i]); |
| 7116 | unres->type[i] = UNRES_RESOLVED; |
| 7117 | del_items--; |
| 7118 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7119 | |
| 7120 | /* rest */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7121 | for (i = 0; i < unres->count; ++i) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7122 | if (unres->type[i] == UNRES_RESOLVED) { |
| 7123 | continue; |
| 7124 | } |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 7125 | 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] | 7126 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7127 | rc = resolve_unres_data_item(unres->node[i], unres->type[i]); |
Michal Vasko | 6df9413 | 2016-09-22 11:08:09 +0200 | [diff] [blame] | 7128 | if (rc == -1) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7129 | ly_vlog_hide(0); |
Michal Vasko | 96b846c | 2016-05-18 13:28:58 +0200 | [diff] [blame] | 7130 | /* print only this last error */ |
| 7131 | resolve_unres_data_item(unres->node[i], unres->type[i]); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7132 | return -1; |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 7133 | } 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] | 7134 | unres->type[i] = UNRES_RESOLVED; |
| 7135 | resolved++; |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 7136 | if (options & LYD_OPT_TRUSTED) { |
Michal Vasko | 6df9413 | 2016-09-22 11:08:09 +0200 | [diff] [blame] | 7137 | /* accept it in this case */ |
| 7138 | if (unres->type[i] == UNRES_LEAFREF) { |
| 7139 | LOGVRB("Leafref \"%s\" with value \"%s\" failed to be resolved.", |
| 7140 | ((struct lys_node_leaf *)unres->node[i]->schema)->type.info.lref.path, |
| 7141 | ((struct lyd_node_leaf_list *)unres->node[i])->value_str); |
| 7142 | } else { |
| 7143 | LOGVRB("Instance identifier \"%s\" failed to be resolved.", |
| 7144 | ((struct lyd_node_leaf_list *)unres->node[i])->value_str); |
| 7145 | } |
| 7146 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7147 | } |
| 7148 | } |
| 7149 | |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7150 | ly_vlog_hide(0); |
| 7151 | if (resolved < unres->count) { |
| 7152 | /* try to resolve the unresolved data again, it will not resolve anything, but it will print |
| 7153 | * all the validation errors |
| 7154 | */ |
| 7155 | for (i = 0; i < unres->count; ++i) { |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7156 | if (unres->type[i] == UNRES_UNION) { |
| 7157 | /* does not make sense to print specific errors for all |
| 7158 | * the data types, just print that the value is invalid */ |
| 7159 | leaf = (struct lyd_node_leaf_list *)unres->node[i]; |
| 7160 | LOGVAL(LYE_INVAL, LY_VLOG_LYD, unres->node[i], (leaf->value_str ? leaf->value_str : ""), |
| 7161 | leaf->schema->name); |
| 7162 | } else if (unres->type[i] != UNRES_RESOLVED) { |
| 7163 | resolve_unres_data_item(unres->node[i], unres->type[i]); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7164 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7165 | } |
| 7166 | return -1; |
| 7167 | } |
| 7168 | |
Michal Vasko | a0ffcab | 2016-05-02 14:52:08 +0200 | [diff] [blame] | 7169 | LOGVRB("All data nodes and constraints resolved."); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7170 | unres->count = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7171 | return EXIT_SUCCESS; |
| 7172 | } |