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" |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 32 | #include "extensions.h" |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 33 | |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 34 | int |
| 35 | 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] | 36 | { |
| 37 | const char *ptr; |
| 38 | int minus = 0; |
| 39 | int64_t ret = 0; |
Radek Krejci | bf47a82 | 2016-11-04 10:06:08 +0100 | [diff] [blame] | 40 | int8_t str_exp, str_dig = -1, trailing_zeros = 0; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 41 | |
| 42 | ptr = *str_num; |
| 43 | |
| 44 | if (ptr[0] == '-') { |
| 45 | minus = 1; |
| 46 | ++ptr; |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 47 | } else if (ptr[0] == '+') { |
| 48 | ++ptr; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 49 | } |
| 50 | |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 51 | if (!isdigit(ptr[0])) { |
| 52 | /* there must be at least one */ |
| 53 | return 1; |
| 54 | } |
| 55 | |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 56 | for (str_exp = 0; isdigit(ptr[0]) || ((ptr[0] == '.') && (str_dig < 0)); ++ptr) { |
| 57 | if (str_exp > 18) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 58 | return 1; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | if (ptr[0] == '.') { |
| 62 | if (ptr[1] == '.') { |
| 63 | /* it's the next interval */ |
| 64 | break; |
| 65 | } |
| 66 | ++str_dig; |
| 67 | } else { |
Radek Krejci | bf47a82 | 2016-11-04 10:06:08 +0100 | [diff] [blame] | 68 | ret = ret * 10 + (ptr[0] - '0'); |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 69 | if (str_dig > -1) { |
| 70 | ++str_dig; |
Radek Krejci | bf47a82 | 2016-11-04 10:06:08 +0100 | [diff] [blame] | 71 | if (ptr[0] == '0') { |
| 72 | /* possibly trailing zero */ |
| 73 | trailing_zeros++; |
| 74 | } else { |
| 75 | trailing_zeros = 0; |
| 76 | } |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 77 | } |
| 78 | ++str_exp; |
| 79 | } |
| 80 | } |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 81 | if (str_dig == 0) { |
| 82 | /* no digits after '.' */ |
| 83 | return 1; |
| 84 | } else if (str_dig == -1) { |
| 85 | /* there are 0 numbers after the floating point */ |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 86 | str_dig = 0; |
| 87 | } |
Radek Krejci | bf47a82 | 2016-11-04 10:06:08 +0100 | [diff] [blame] | 88 | /* remove trailing zeros */ |
| 89 | if (trailing_zeros) { |
Michal Vasko | 6ca5ca7 | 2016-11-28 09:21:51 +0100 | [diff] [blame] | 90 | str_dig -= trailing_zeros; |
| 91 | str_exp -= trailing_zeros; |
Radek Krejci | bf47a82 | 2016-11-04 10:06:08 +0100 | [diff] [blame] | 92 | ret = ret / dec_pow(trailing_zeros); |
| 93 | } |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 94 | |
| 95 | /* it's parsed, now adjust the number based on fraction-digits, if needed */ |
| 96 | if (str_dig < dig) { |
| 97 | if ((str_exp - 1) + (dig - str_dig) > 18) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 98 | return 1; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 99 | } |
| 100 | ret *= dec_pow(dig - str_dig); |
| 101 | } |
| 102 | if (str_dig > dig) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 103 | return 1; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | if (minus) { |
| 107 | ret *= -1; |
| 108 | } |
| 109 | *str_num = ptr; |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 110 | *num = ret; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 111 | |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 112 | return 0; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /** |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 116 | * @brief Parse an identifier. |
| 117 | * |
| 118 | * ;; An identifier MUST NOT start with (('X'|'x') ('M'|'m') ('L'|'l')) |
| 119 | * identifier = (ALPHA / "_") |
| 120 | * *(ALPHA / DIGIT / "_" / "-" / ".") |
| 121 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 122 | * @param[in] id Identifier to use. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 123 | * |
| 124 | * @return Number of characters successfully parsed. |
| 125 | */ |
Michal Vasko | 249e6b5 | 2015-08-19 11:08:52 +0200 | [diff] [blame] | 126 | int |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 127 | parse_identifier(const char *id) |
| 128 | { |
| 129 | int parsed = 0; |
| 130 | |
Michal Vasko | 1ab90bc | 2016-03-15 10:40:22 +0100 | [diff] [blame] | 131 | assert(id); |
| 132 | |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 133 | if (!isalpha(id[0]) && (id[0] != '_')) { |
| 134 | return -parsed; |
| 135 | } |
| 136 | |
| 137 | ++parsed; |
| 138 | ++id; |
| 139 | |
| 140 | while (isalnum(id[0]) || (id[0] == '_') || (id[0] == '-') || (id[0] == '.')) { |
| 141 | ++parsed; |
| 142 | ++id; |
| 143 | } |
| 144 | |
| 145 | return parsed; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * @brief Parse a node-identifier. |
| 150 | * |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 151 | * node-identifier = [module-name ":"] identifier |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 152 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 153 | * @param[in] id Identifier to use. |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 154 | * @param[out] mod_name Points to the module name, NULL if there is not any. |
| 155 | * @param[out] mod_name_len Length of the module name, 0 if there is not any. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 156 | * @param[out] name Points to the node name. |
| 157 | * @param[out] nam_len Length of the node name. |
| 158 | * |
| 159 | * @return Number of characters successfully parsed, |
| 160 | * positive on success, negative on failure. |
| 161 | */ |
| 162 | static int |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 163 | parse_node_identifier(const char *id, const char **mod_name, int *mod_name_len, const char **name, int *nam_len) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 164 | { |
| 165 | int parsed = 0, ret; |
| 166 | |
| 167 | assert(id); |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 168 | if (mod_name) { |
| 169 | *mod_name = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 170 | } |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 171 | if (mod_name_len) { |
| 172 | *mod_name_len = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 173 | } |
| 174 | if (name) { |
| 175 | *name = NULL; |
| 176 | } |
| 177 | if (nam_len) { |
| 178 | *nam_len = 0; |
| 179 | } |
| 180 | |
| 181 | if ((ret = parse_identifier(id)) < 1) { |
| 182 | return ret; |
| 183 | } |
| 184 | |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 185 | if (mod_name) { |
| 186 | *mod_name = id; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 187 | } |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 188 | if (mod_name_len) { |
| 189 | *mod_name_len = ret; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | parsed += ret; |
| 193 | id += ret; |
| 194 | |
| 195 | /* there is prefix */ |
| 196 | if (id[0] == ':') { |
| 197 | ++parsed; |
| 198 | ++id; |
| 199 | |
| 200 | /* there isn't */ |
| 201 | } else { |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 202 | if (name && mod_name) { |
| 203 | *name = *mod_name; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 204 | } |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 205 | if (mod_name) { |
| 206 | *mod_name = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 207 | } |
| 208 | |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 209 | if (nam_len && mod_name_len) { |
| 210 | *nam_len = *mod_name_len; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 211 | } |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 212 | if (mod_name_len) { |
| 213 | *mod_name_len = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | return parsed; |
| 217 | } |
| 218 | |
| 219 | /* identifier (node name) */ |
| 220 | if ((ret = parse_identifier(id)) < 1) { |
| 221 | return -parsed+ret; |
| 222 | } |
| 223 | |
| 224 | if (name) { |
| 225 | *name = id; |
| 226 | } |
| 227 | if (nam_len) { |
| 228 | *nam_len = ret; |
| 229 | } |
| 230 | |
| 231 | return parsed+ret; |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * @brief Parse a path-predicate (leafref). |
| 236 | * |
| 237 | * path-predicate = "[" *WSP path-equality-expr *WSP "]" |
| 238 | * path-equality-expr = node-identifier *WSP "=" *WSP path-key-expr |
| 239 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 240 | * @param[in] id Identifier to use. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 241 | * @param[out] prefix Points to the prefix, NULL if there is not any. |
| 242 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 243 | * @param[out] name Points to the node name. |
| 244 | * @param[out] nam_len Length of the node name. |
| 245 | * @param[out] path_key_expr Points to the path-key-expr. |
| 246 | * @param[out] pke_len Length of the path-key-expr. |
| 247 | * @param[out] has_predicate Flag to mark whether there is another predicate following. |
| 248 | * |
| 249 | * @return Number of characters successfully parsed, |
| 250 | * positive on success, negative on failure. |
| 251 | */ |
| 252 | static int |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 253 | parse_path_predicate(const char *id, const char **prefix, int *pref_len, const char **name, int *nam_len, |
| 254 | const char **path_key_expr, int *pke_len, int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 255 | { |
| 256 | const char *ptr; |
| 257 | int parsed = 0, ret; |
| 258 | |
| 259 | assert(id); |
| 260 | if (prefix) { |
| 261 | *prefix = NULL; |
| 262 | } |
| 263 | if (pref_len) { |
| 264 | *pref_len = 0; |
| 265 | } |
| 266 | if (name) { |
| 267 | *name = NULL; |
| 268 | } |
| 269 | if (nam_len) { |
| 270 | *nam_len = 0; |
| 271 | } |
| 272 | if (path_key_expr) { |
| 273 | *path_key_expr = NULL; |
| 274 | } |
| 275 | if (pke_len) { |
| 276 | *pke_len = 0; |
| 277 | } |
| 278 | if (has_predicate) { |
| 279 | *has_predicate = 0; |
| 280 | } |
| 281 | |
| 282 | if (id[0] != '[') { |
| 283 | return -parsed; |
| 284 | } |
| 285 | |
| 286 | ++parsed; |
| 287 | ++id; |
| 288 | |
| 289 | while (isspace(id[0])) { |
| 290 | ++parsed; |
| 291 | ++id; |
| 292 | } |
| 293 | |
| 294 | if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len)) < 1) { |
| 295 | return -parsed+ret; |
| 296 | } |
| 297 | |
| 298 | parsed += ret; |
| 299 | id += ret; |
| 300 | |
| 301 | while (isspace(id[0])) { |
| 302 | ++parsed; |
| 303 | ++id; |
| 304 | } |
| 305 | |
| 306 | if (id[0] != '=') { |
| 307 | return -parsed; |
| 308 | } |
| 309 | |
| 310 | ++parsed; |
| 311 | ++id; |
| 312 | |
| 313 | while (isspace(id[0])) { |
| 314 | ++parsed; |
| 315 | ++id; |
| 316 | } |
| 317 | |
| 318 | if ((ptr = strchr(id, ']')) == NULL) { |
| 319 | return -parsed; |
| 320 | } |
| 321 | |
| 322 | --ptr; |
| 323 | while (isspace(ptr[0])) { |
| 324 | --ptr; |
| 325 | } |
| 326 | ++ptr; |
| 327 | |
| 328 | ret = ptr-id; |
| 329 | if (path_key_expr) { |
| 330 | *path_key_expr = id; |
| 331 | } |
| 332 | if (pke_len) { |
| 333 | *pke_len = ret; |
| 334 | } |
| 335 | |
| 336 | parsed += ret; |
| 337 | id += ret; |
| 338 | |
| 339 | while (isspace(id[0])) { |
| 340 | ++parsed; |
| 341 | ++id; |
| 342 | } |
| 343 | |
| 344 | assert(id[0] == ']'); |
| 345 | |
| 346 | if (id[1] == '[') { |
| 347 | *has_predicate = 1; |
| 348 | } |
| 349 | |
| 350 | return parsed+1; |
| 351 | } |
| 352 | |
| 353 | /** |
| 354 | * @brief Parse a path-key-expr (leafref). First call parses "current()", all |
| 355 | * the ".." and the first node-identifier, other calls parse a single |
| 356 | * node-identifier each. |
| 357 | * |
| 358 | * path-key-expr = current-function-invocation *WSP "/" *WSP |
| 359 | * rel-path-keyexpr |
| 360 | * rel-path-keyexpr = 1*(".." *WSP "/" *WSP) |
| 361 | * *(node-identifier *WSP "/" *WSP) |
| 362 | * node-identifier |
| 363 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 364 | * @param[in] id Identifier to use. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 365 | * @param[out] prefix Points to the prefix, NULL if there is not any. |
| 366 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 367 | * @param[out] name Points to the node name. |
| 368 | * @param[out] nam_len Length of the node name. |
| 369 | * @param[out] parent_times Number of ".." in the path. Must be 0 on the first call, |
| 370 | * must not be changed between consecutive calls. |
| 371 | * @return Number of characters successfully parsed, |
| 372 | * positive on success, negative on failure. |
| 373 | */ |
| 374 | static int |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 375 | parse_path_key_expr(const char *id, const char **prefix, int *pref_len, const char **name, int *nam_len, |
| 376 | int *parent_times) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 377 | { |
| 378 | int parsed = 0, ret, par_times = 0; |
| 379 | |
| 380 | assert(id); |
| 381 | assert(parent_times); |
| 382 | if (prefix) { |
| 383 | *prefix = NULL; |
| 384 | } |
| 385 | if (pref_len) { |
| 386 | *pref_len = 0; |
| 387 | } |
| 388 | if (name) { |
| 389 | *name = NULL; |
| 390 | } |
| 391 | if (nam_len) { |
| 392 | *nam_len = 0; |
| 393 | } |
| 394 | |
| 395 | if (!*parent_times) { |
| 396 | /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */ |
| 397 | if (strncmp(id, "current()", 9)) { |
| 398 | return -parsed; |
| 399 | } |
| 400 | |
| 401 | parsed += 9; |
| 402 | id += 9; |
| 403 | |
| 404 | while (isspace(id[0])) { |
| 405 | ++parsed; |
| 406 | ++id; |
| 407 | } |
| 408 | |
| 409 | if (id[0] != '/') { |
| 410 | return -parsed; |
| 411 | } |
| 412 | |
| 413 | ++parsed; |
| 414 | ++id; |
| 415 | |
| 416 | while (isspace(id[0])) { |
| 417 | ++parsed; |
| 418 | ++id; |
| 419 | } |
| 420 | |
| 421 | /* rel-path-keyexpr */ |
| 422 | if (strncmp(id, "..", 2)) { |
| 423 | return -parsed; |
| 424 | } |
| 425 | ++par_times; |
| 426 | |
| 427 | parsed += 2; |
| 428 | id += 2; |
| 429 | |
| 430 | while (isspace(id[0])) { |
| 431 | ++parsed; |
| 432 | ++id; |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | /* 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier |
| 437 | * |
| 438 | * first parent reference with whitespaces already parsed |
| 439 | */ |
| 440 | if (id[0] != '/') { |
| 441 | return -parsed; |
| 442 | } |
| 443 | |
| 444 | ++parsed; |
| 445 | ++id; |
| 446 | |
| 447 | while (isspace(id[0])) { |
| 448 | ++parsed; |
| 449 | ++id; |
| 450 | } |
| 451 | |
| 452 | while (!strncmp(id, "..", 2) && !*parent_times) { |
| 453 | ++par_times; |
| 454 | |
| 455 | parsed += 2; |
| 456 | id += 2; |
| 457 | |
| 458 | while (isspace(id[0])) { |
| 459 | ++parsed; |
| 460 | ++id; |
| 461 | } |
| 462 | |
| 463 | if (id[0] != '/') { |
| 464 | return -parsed; |
| 465 | } |
| 466 | |
| 467 | ++parsed; |
| 468 | ++id; |
| 469 | |
| 470 | while (isspace(id[0])) { |
| 471 | ++parsed; |
| 472 | ++id; |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | if (!*parent_times) { |
| 477 | *parent_times = par_times; |
| 478 | } |
| 479 | |
| 480 | /* all parent references must be parsed at this point */ |
| 481 | if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len)) < 1) { |
| 482 | return -parsed+ret; |
| 483 | } |
| 484 | |
| 485 | parsed += ret; |
| 486 | id += ret; |
| 487 | |
| 488 | return parsed; |
| 489 | } |
| 490 | |
| 491 | /** |
| 492 | * @brief Parse path-arg (leafref). |
| 493 | * |
| 494 | * path-arg = absolute-path / relative-path |
| 495 | * absolute-path = 1*("/" (node-identifier *path-predicate)) |
| 496 | * relative-path = 1*(".." "/") descendant-path |
| 497 | * |
Radek Krejci | f7ed4c3 | 2016-10-27 16:20:03 +0200 | [diff] [blame] | 498 | * @param[in] mod Module of the context node to get correct prefix in case it is not explicitly specified |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 499 | * @param[in] id Identifier to use. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 500 | * @param[out] prefix Points to the prefix, NULL if there is not any. |
| 501 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 502 | * @param[out] name Points to the node name. |
| 503 | * @param[out] nam_len Length of the node name. |
| 504 | * @param[out] parent_times Number of ".." in the path. Must be 0 on the first call, |
| 505 | * must not be changed between consecutive calls. -1 if the |
| 506 | * path is relative. |
| 507 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 508 | * |
| 509 | * @return Number of characters successfully parsed, |
| 510 | * positive on success, negative on failure. |
| 511 | */ |
| 512 | static int |
Radek Krejci | f7ed4c3 | 2016-10-27 16:20:03 +0200 | [diff] [blame] | 513 | parse_path_arg(struct lys_module *mod, const char *id, const char **prefix, int *pref_len, |
| 514 | const char **name, int *nam_len, int *parent_times, int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 515 | { |
| 516 | int parsed = 0, ret, par_times = 0; |
| 517 | |
| 518 | assert(id); |
| 519 | assert(parent_times); |
| 520 | if (prefix) { |
| 521 | *prefix = NULL; |
| 522 | } |
| 523 | if (pref_len) { |
| 524 | *pref_len = 0; |
| 525 | } |
| 526 | if (name) { |
| 527 | *name = NULL; |
| 528 | } |
| 529 | if (nam_len) { |
| 530 | *nam_len = 0; |
| 531 | } |
| 532 | if (has_predicate) { |
| 533 | *has_predicate = 0; |
| 534 | } |
| 535 | |
| 536 | if (!*parent_times && !strncmp(id, "..", 2)) { |
| 537 | ++par_times; |
| 538 | |
| 539 | parsed += 2; |
| 540 | id += 2; |
| 541 | |
| 542 | while (!strncmp(id, "/..", 3)) { |
| 543 | ++par_times; |
| 544 | |
| 545 | parsed += 3; |
| 546 | id += 3; |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | if (!*parent_times) { |
| 551 | if (par_times) { |
| 552 | *parent_times = par_times; |
| 553 | } else { |
| 554 | *parent_times = -1; |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | if (id[0] != '/') { |
| 559 | return -parsed; |
| 560 | } |
| 561 | |
| 562 | /* skip '/' */ |
| 563 | ++parsed; |
| 564 | ++id; |
| 565 | |
| 566 | /* node-identifier ([prefix:]identifier) */ |
| 567 | if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len)) < 1) { |
| 568 | return -parsed-ret; |
| 569 | } |
Radek Krejci | f7ed4c3 | 2016-10-27 16:20:03 +0200 | [diff] [blame] | 570 | if (!(*prefix)) { |
| 571 | /* actually we always need prefix even it is not specified */ |
| 572 | *prefix = lys_main_module(mod)->name; |
| 573 | *pref_len = strlen(*prefix); |
| 574 | } |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 575 | |
| 576 | parsed += ret; |
| 577 | id += ret; |
| 578 | |
| 579 | /* there is no predicate */ |
| 580 | if ((id[0] == '/') || !id[0]) { |
| 581 | return parsed; |
| 582 | } else if (id[0] != '[') { |
| 583 | return -parsed; |
| 584 | } |
| 585 | |
| 586 | if (has_predicate) { |
| 587 | *has_predicate = 1; |
| 588 | } |
| 589 | |
| 590 | return parsed; |
| 591 | } |
| 592 | |
| 593 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 594 | * @brief Parse instance-identifier in JSON data format. That means that prefixes |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 595 | * (which are mandatory for every node-identifier) are actually model names. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 596 | * |
| 597 | * instance-identifier = 1*("/" (node-identifier *predicate)) |
| 598 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 599 | * @param[in] id Identifier to use. |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 600 | * @param[out] model Points to the model name. |
| 601 | * @param[out] mod_len Length of the model name. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 602 | * @param[out] name Points to the node name. |
| 603 | * @param[out] nam_len Length of the node name. |
| 604 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 605 | * |
| 606 | * @return Number of characters successfully parsed, |
| 607 | * positive on success, negative on failure. |
| 608 | */ |
| 609 | static int |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 610 | parse_instance_identifier(const char *id, const char **model, int *mod_len, const char **name, int *nam_len, |
| 611 | int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 612 | { |
| 613 | int parsed = 0, ret; |
| 614 | |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 615 | if (has_predicate) { |
| 616 | *has_predicate = 0; |
| 617 | } |
| 618 | |
| 619 | if (id[0] != '/') { |
| 620 | return -parsed; |
| 621 | } |
| 622 | |
| 623 | ++parsed; |
| 624 | ++id; |
| 625 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 626 | if ((ret = parse_identifier(id)) < 1) { |
| 627 | return ret; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 628 | } |
| 629 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 630 | *model = id; |
| 631 | *mod_len = ret; |
| 632 | |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 633 | parsed += ret; |
| 634 | id += ret; |
| 635 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 636 | if (id[0] != ':') { |
| 637 | return -parsed; |
| 638 | } |
| 639 | |
| 640 | ++parsed; |
| 641 | ++id; |
| 642 | |
| 643 | if ((ret = parse_identifier(id)) < 1) { |
| 644 | return ret; |
| 645 | } |
| 646 | |
| 647 | *name = id; |
| 648 | *nam_len = ret; |
| 649 | |
| 650 | parsed += ret; |
| 651 | id += ret; |
| 652 | |
Radek Krejci | 4967cb6 | 2016-09-14 16:40:28 +0200 | [diff] [blame] | 653 | if (id[0] == '[' && has_predicate) { |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 654 | *has_predicate = 1; |
| 655 | } |
| 656 | |
| 657 | return parsed; |
| 658 | } |
| 659 | |
| 660 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 661 | * @brief Parse predicate (instance-identifier) in JSON data format. That means that prefixes |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 662 | * (which are mandatory) are actually model names. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 663 | * |
| 664 | * predicate = "[" *WSP (predicate-expr / pos) *WSP "]" |
| 665 | * predicate-expr = (node-identifier / ".") *WSP "=" *WSP |
| 666 | * ((DQUOTE string DQUOTE) / |
| 667 | * (SQUOTE string SQUOTE)) |
| 668 | * pos = non-negative-integer-value |
| 669 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 670 | * @param[in] id Identifier to use. |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 671 | * @param[out] model Points to the model name. |
| 672 | * @param[out] mod_len Length of the model name. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 673 | * @param[out] name Points to the node name. Can be identifier (from node-identifier), "." or pos. |
| 674 | * @param[out] nam_len Length of the node name. |
| 675 | * @param[out] value Value the node-identifier must have (string from the grammar), |
| 676 | * NULL if there is not any. |
| 677 | * @param[out] val_len Length of the value, 0 if there is not any. |
| 678 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 679 | * |
| 680 | * @return Number of characters successfully parsed, |
| 681 | * positive on success, negative on failure. |
| 682 | */ |
| 683 | static int |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 684 | parse_predicate(const char *id, const char **model, int *mod_len, const char **name, int *nam_len, |
| 685 | const char **value, int *val_len, int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 686 | { |
| 687 | const char *ptr; |
| 688 | int parsed = 0, ret; |
| 689 | char quote; |
| 690 | |
| 691 | assert(id); |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 692 | if (model) { |
| 693 | *model = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 694 | } |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 695 | if (mod_len) { |
| 696 | *mod_len = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 697 | } |
| 698 | if (name) { |
| 699 | *name = NULL; |
| 700 | } |
| 701 | if (nam_len) { |
| 702 | *nam_len = 0; |
| 703 | } |
| 704 | if (value) { |
| 705 | *value = NULL; |
| 706 | } |
| 707 | if (val_len) { |
| 708 | *val_len = 0; |
| 709 | } |
| 710 | if (has_predicate) { |
| 711 | *has_predicate = 0; |
| 712 | } |
| 713 | |
| 714 | if (id[0] != '[') { |
| 715 | return -parsed; |
| 716 | } |
| 717 | |
| 718 | ++parsed; |
| 719 | ++id; |
| 720 | |
| 721 | while (isspace(id[0])) { |
| 722 | ++parsed; |
| 723 | ++id; |
| 724 | } |
| 725 | |
| 726 | /* pos */ |
| 727 | if (isdigit(id[0])) { |
| 728 | if (name) { |
| 729 | *name = id; |
| 730 | } |
| 731 | |
| 732 | if (id[0] == '0') { |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 733 | return -parsed; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | while (isdigit(id[0])) { |
| 737 | ++parsed; |
| 738 | ++id; |
| 739 | } |
| 740 | |
| 741 | if (nam_len) { |
| 742 | *nam_len = id-(*name); |
| 743 | } |
| 744 | |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 745 | /* "." or node-identifier */ |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 746 | } else { |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 747 | if (id[0] == '.') { |
| 748 | if (name) { |
| 749 | *name = id; |
| 750 | } |
| 751 | if (nam_len) { |
| 752 | *nam_len = 1; |
| 753 | } |
| 754 | |
| 755 | ++parsed; |
| 756 | ++id; |
| 757 | |
| 758 | } else { |
| 759 | if ((ret = parse_node_identifier(id, model, mod_len, name, nam_len)) < 1) { |
| 760 | return -parsed+ret; |
| 761 | } else if (model && !*model) { |
| 762 | return -parsed; |
| 763 | } |
| 764 | |
| 765 | parsed += ret; |
| 766 | id += ret; |
| 767 | } |
| 768 | |
| 769 | while (isspace(id[0])) { |
| 770 | ++parsed; |
| 771 | ++id; |
| 772 | } |
| 773 | |
| 774 | if (id[0] != '=') { |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 775 | return -parsed; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 776 | } |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 777 | |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 778 | ++parsed; |
| 779 | ++id; |
| 780 | |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 781 | while (isspace(id[0])) { |
| 782 | ++parsed; |
| 783 | ++id; |
| 784 | } |
| 785 | |
| 786 | /* ((DQUOTE string DQUOTE) / (SQUOTE string SQUOTE)) */ |
| 787 | if ((id[0] == '\"') || (id[0] == '\'')) { |
| 788 | quote = id[0]; |
| 789 | |
| 790 | ++parsed; |
| 791 | ++id; |
| 792 | |
| 793 | if ((ptr = strchr(id, quote)) == NULL) { |
| 794 | return -parsed; |
| 795 | } |
| 796 | ret = ptr-id; |
| 797 | |
| 798 | if (value) { |
| 799 | *value = id; |
| 800 | } |
| 801 | if (val_len) { |
| 802 | *val_len = ret; |
| 803 | } |
| 804 | |
| 805 | parsed += ret+1; |
| 806 | id += ret+1; |
| 807 | } else { |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 808 | return -parsed; |
| 809 | } |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | while (isspace(id[0])) { |
| 813 | ++parsed; |
| 814 | ++id; |
| 815 | } |
| 816 | |
| 817 | if (id[0] != ']') { |
| 818 | return -parsed; |
| 819 | } |
| 820 | |
| 821 | ++parsed; |
| 822 | ++id; |
| 823 | |
| 824 | if ((id[0] == '[') && has_predicate) { |
| 825 | *has_predicate = 1; |
| 826 | } |
| 827 | |
| 828 | return parsed; |
| 829 | } |
| 830 | |
| 831 | /** |
| 832 | * @brief Parse schema-nodeid. |
| 833 | * |
| 834 | * schema-nodeid = absolute-schema-nodeid / |
| 835 | * descendant-schema-nodeid |
| 836 | * absolute-schema-nodeid = 1*("/" node-identifier) |
Michal Vasko | 4893535 | 2016-03-29 11:52:36 +0200 | [diff] [blame] | 837 | * descendant-schema-nodeid = ["." "/"] |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 838 | * node-identifier |
| 839 | * absolute-schema-nodeid |
| 840 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 841 | * @param[in] id Identifier to use. |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 842 | * @param[out] mod_name Points to the module name, NULL if there is not any. |
| 843 | * @param[out] mod_name_len Length of the module name, 0 if there is not any. |
Michal Vasko | 4893535 | 2016-03-29 11:52:36 +0200 | [diff] [blame] | 844 | * @param[out] name Points to the node name. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 845 | * @param[out] nam_len Length of the node name. |
| 846 | * @param[out] is_relative Flag to mark whether the nodeid is absolute or descendant. Must be -1 |
| 847 | * on the first call, must not be changed between consecutive calls. |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 848 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. It cannot be |
| 849 | * based on the grammar, in those cases use NULL. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 850 | * |
| 851 | * @return Number of characters successfully parsed, |
| 852 | * positive on success, negative on failure. |
| 853 | */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 854 | int |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 855 | parse_schema_nodeid(const char *id, const char **mod_name, int *mod_name_len, const char **name, int *nam_len, |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 856 | int *is_relative, int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 857 | { |
| 858 | int parsed = 0, ret; |
| 859 | |
| 860 | assert(id); |
| 861 | assert(is_relative); |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 862 | if (mod_name) { |
| 863 | *mod_name = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 864 | } |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 865 | if (mod_name_len) { |
| 866 | *mod_name_len = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 867 | } |
| 868 | if (name) { |
| 869 | *name = NULL; |
| 870 | } |
| 871 | if (nam_len) { |
| 872 | *nam_len = 0; |
| 873 | } |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 874 | if (has_predicate) { |
| 875 | *has_predicate = 0; |
| 876 | } |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 877 | |
| 878 | if (id[0] != '/') { |
| 879 | if (*is_relative != -1) { |
| 880 | return -parsed; |
| 881 | } else { |
| 882 | *is_relative = 1; |
| 883 | } |
Michal Vasko | 4893535 | 2016-03-29 11:52:36 +0200 | [diff] [blame] | 884 | if (!strncmp(id, "./", 2)) { |
| 885 | parsed += 2; |
| 886 | id += 2; |
| 887 | } |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 888 | } else { |
| 889 | if (*is_relative == -1) { |
| 890 | *is_relative = 0; |
| 891 | } |
| 892 | ++parsed; |
| 893 | ++id; |
| 894 | } |
| 895 | |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 896 | if ((ret = parse_node_identifier(id, mod_name, mod_name_len, name, nam_len)) < 1) { |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 897 | return -parsed+ret; |
| 898 | } |
| 899 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 900 | parsed += ret; |
| 901 | id += ret; |
| 902 | |
| 903 | if ((id[0] == '[') && has_predicate) { |
| 904 | *has_predicate = 1; |
| 905 | } |
| 906 | |
| 907 | return parsed; |
| 908 | } |
| 909 | |
| 910 | /** |
| 911 | * @brief Parse schema predicate (special format internally used). |
| 912 | * |
| 913 | * predicate = "[" *WSP predicate-expr *WSP "]" |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 914 | * predicate-expr = "." / identifier / positive-integer / key-with-value |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 915 | * key-with-value = identifier *WSP "=" *WSP |
| 916 | * ((DQUOTE string DQUOTE) / |
| 917 | * (SQUOTE string SQUOTE)) |
| 918 | * |
| 919 | * @param[in] id Identifier to use. |
| 920 | * @param[out] name Points to the list key name. |
| 921 | * @param[out] nam_len Length of \p name. |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 922 | * @param[out] value Points to the key value. If specified, key-with-value is expected. |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 923 | * @param[out] val_len Length of \p value. |
| 924 | * @param[out] has_predicate Flag to mark whether there is another predicate specified. |
| 925 | */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 926 | int |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 927 | parse_schema_json_predicate(const char *id, const char **name, int *nam_len, const char **value, int *val_len, |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 928 | int *has_predicate) |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 929 | { |
| 930 | const char *ptr; |
| 931 | int parsed = 0, ret; |
| 932 | char quote; |
| 933 | |
| 934 | assert(id); |
| 935 | if (name) { |
| 936 | *name = NULL; |
| 937 | } |
| 938 | if (nam_len) { |
| 939 | *nam_len = 0; |
| 940 | } |
| 941 | if (value) { |
| 942 | *value = NULL; |
| 943 | } |
| 944 | if (val_len) { |
| 945 | *val_len = 0; |
| 946 | } |
| 947 | if (has_predicate) { |
| 948 | *has_predicate = 0; |
| 949 | } |
| 950 | |
| 951 | if (id[0] != '[') { |
| 952 | return -parsed; |
| 953 | } |
| 954 | |
| 955 | ++parsed; |
| 956 | ++id; |
| 957 | |
| 958 | while (isspace(id[0])) { |
| 959 | ++parsed; |
| 960 | ++id; |
| 961 | } |
| 962 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 963 | /* identifier */ |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 964 | if (id[0] == '.') { |
| 965 | ret = 1; |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 966 | } else if (isdigit(id[0])) { |
| 967 | if (id[0] == '0') { |
| 968 | return -parsed; |
| 969 | } |
| 970 | ret = 1; |
| 971 | while (isdigit(id[ret])) { |
| 972 | ++ret; |
| 973 | } |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 974 | } else if ((ret = parse_identifier(id)) < 1) { |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 975 | return -parsed + ret; |
| 976 | } |
| 977 | if (name) { |
| 978 | *name = id; |
| 979 | } |
| 980 | if (nam_len) { |
| 981 | *nam_len = ret; |
| 982 | } |
| 983 | |
| 984 | parsed += ret; |
| 985 | id += ret; |
| 986 | |
| 987 | while (isspace(id[0])) { |
| 988 | ++parsed; |
| 989 | ++id; |
| 990 | } |
| 991 | |
| 992 | /* there is value as well */ |
| 993 | if (id[0] == '=') { |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 994 | if (name && isdigit(**name)) { |
| 995 | return -parsed; |
| 996 | } |
| 997 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 998 | ++parsed; |
| 999 | ++id; |
| 1000 | |
| 1001 | while (isspace(id[0])) { |
| 1002 | ++parsed; |
| 1003 | ++id; |
| 1004 | } |
| 1005 | |
| 1006 | /* ((DQUOTE string DQUOTE) / (SQUOTE string SQUOTE)) */ |
| 1007 | if ((id[0] == '\"') || (id[0] == '\'')) { |
| 1008 | quote = id[0]; |
| 1009 | |
| 1010 | ++parsed; |
| 1011 | ++id; |
| 1012 | |
| 1013 | if ((ptr = strchr(id, quote)) == NULL) { |
| 1014 | return -parsed; |
| 1015 | } |
| 1016 | ret = ptr - id; |
| 1017 | |
| 1018 | if (value) { |
| 1019 | *value = id; |
| 1020 | } |
| 1021 | if (val_len) { |
| 1022 | *val_len = ret; |
| 1023 | } |
| 1024 | |
| 1025 | parsed += ret + 1; |
| 1026 | id += ret + 1; |
| 1027 | } else { |
| 1028 | return -parsed; |
| 1029 | } |
| 1030 | |
| 1031 | while (isspace(id[0])) { |
| 1032 | ++parsed; |
| 1033 | ++id; |
| 1034 | } |
| 1035 | } |
| 1036 | |
| 1037 | if (id[0] != ']') { |
| 1038 | return -parsed; |
| 1039 | } |
| 1040 | |
| 1041 | ++parsed; |
| 1042 | ++id; |
| 1043 | |
| 1044 | if ((id[0] == '[') && has_predicate) { |
| 1045 | *has_predicate = 1; |
| 1046 | } |
| 1047 | |
| 1048 | return parsed; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | /** |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1052 | * @brief Resolve (find) a feature definition. Logs directly. |
| 1053 | * |
| 1054 | * @param[in] feat_name Feature name to resolve. |
| 1055 | * @param[in] len Length of \p feat_name. |
| 1056 | * @param[in] node Node with the if-feature expression. |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1057 | * @param[out] feature Pointer to be set to point to the feature definition, if feature not found |
| 1058 | * (return code 1), the pointer is untouched. |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1059 | * |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1060 | * @return 0 on success, 1 on forward reference, -1 on error. |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1061 | */ |
| 1062 | static int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1063 | 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] | 1064 | { |
| 1065 | char *str; |
| 1066 | const char *mod_name, *name; |
| 1067 | int mod_name_len, nam_len, i, j; |
| 1068 | const struct lys_module *module; |
| 1069 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1070 | assert(feature); |
| 1071 | |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1072 | /* check prefix */ |
| 1073 | if ((i = parse_node_identifier(feat_name, &mod_name, &mod_name_len, &name, &nam_len)) < 1) { |
| 1074 | LOGVAL(LYE_INCHAR, LY_VLOG_NONE, NULL, feat_name[-i], &feat_name[-i]); |
| 1075 | return -1; |
| 1076 | } |
| 1077 | |
| 1078 | module = lys_get_import_module(lys_node_module(node), NULL, 0, mod_name, mod_name_len); |
| 1079 | if (!module) { |
| 1080 | /* identity refers unknown data model */ |
| 1081 | LOGVAL(LYE_INMOD_LEN, LY_VLOG_NONE, NULL, mod_name_len, mod_name); |
| 1082 | return -1; |
| 1083 | } |
| 1084 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1085 | if (module != node->module && module == lys_node_module(node)) { |
| 1086 | /* first, try to search directly in submodule where the feature was mentioned */ |
| 1087 | for (j = 0; j < node->module->features_size; j++) { |
| 1088 | if (!strncmp(name, node->module->features[j].name, nam_len) && !node->module->features[j].name[nam_len]) { |
| 1089 | /* check status */ |
| 1090 | 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] | 1091 | node->module->features[j].module, node->module->features[j].name, NULL)) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1092 | return -1; |
| 1093 | } |
| 1094 | *feature = &node->module->features[j]; |
| 1095 | return 0; |
| 1096 | } |
| 1097 | } |
| 1098 | } |
| 1099 | |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1100 | /* search in the identified module ... */ |
| 1101 | for (j = 0; j < module->features_size; j++) { |
Michal Vasko | 3def867 | 2016-07-01 11:43:09 +0200 | [diff] [blame] | 1102 | 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] | 1103 | /* check status */ |
| 1104 | 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] | 1105 | module->features[j].module, module->features[j].name, NULL)) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1106 | return -1; |
| 1107 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1108 | *feature = &module->features[j]; |
| 1109 | return 0; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1110 | } |
| 1111 | } |
| 1112 | /* ... and all its submodules */ |
Radek Krejci | d4c1d0f | 2017-01-19 16:11:38 +0100 | [diff] [blame] | 1113 | for (i = 0; i < module->inc_size && module->inc[i].submodule; i++) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1114 | for (j = 0; j < module->inc[i].submodule->features_size; j++) { |
Michal Vasko | 3def867 | 2016-07-01 11:43:09 +0200 | [diff] [blame] | 1115 | if (!strncmp(name, module->inc[i].submodule->features[j].name, nam_len) |
| 1116 | && !module->inc[i].submodule->features[j].name[nam_len]) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1117 | /* check status */ |
| 1118 | if (lyp_check_status(node->flags, lys_node_module(node), node->name, |
| 1119 | module->inc[i].submodule->features[j].flags, |
| 1120 | module->inc[i].submodule->features[j].module, |
Pavol Vican | fdab9f9 | 2016-09-07 15:23:27 +0200 | [diff] [blame] | 1121 | module->inc[i].submodule->features[j].name, NULL)) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1122 | return -1; |
| 1123 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1124 | *feature = &module->inc[i].submodule->features[j]; |
| 1125 | return 0; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1126 | } |
| 1127 | } |
| 1128 | } |
| 1129 | |
| 1130 | /* not found */ |
| 1131 | str = strndup(feat_name, len); |
| 1132 | LOGVAL(LYE_INRESOLV, LY_VLOG_NONE, NULL, "feature", str); |
| 1133 | free(str); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1134 | return 1; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1135 | } |
| 1136 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1137 | /* |
| 1138 | * @return |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1139 | * - 1 if enabled |
| 1140 | * - 0 if disabled |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1141 | * - -1 if not usable by its if-feature expression |
| 1142 | */ |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1143 | static int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1144 | resolve_feature_value(const struct lys_feature *feat) |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1145 | { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1146 | int i; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1147 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1148 | for (i = 0; i < feat->iffeature_size; i++) { |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1149 | if (!resolve_iffeature(&feat->iffeature[i])) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1150 | return -1; |
| 1151 | } |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1152 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1153 | |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1154 | return feat->flags & LYS_FENABLED ? 1 : 0; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1155 | } |
| 1156 | |
| 1157 | static int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1158 | 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] | 1159 | { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1160 | uint8_t op; |
| 1161 | int rc, a, b; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1162 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1163 | op = iff_getop(expr->expr, *index_e); |
| 1164 | (*index_e)++; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1165 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1166 | switch (op) { |
| 1167 | case LYS_IFF_F: |
| 1168 | /* resolve feature */ |
| 1169 | return resolve_feature_value(expr->features[(*index_f)++]); |
| 1170 | case LYS_IFF_NOT: |
| 1171 | rc = resolve_iffeature_recursive(expr, index_e, index_f); |
| 1172 | if (rc == -1) { |
| 1173 | /* one of the referenced feature is hidden by its if-feature, |
| 1174 | * so this if-feature expression is always false */ |
| 1175 | return -1; |
| 1176 | } else { |
| 1177 | /* invert result */ |
| 1178 | return rc ? 0 : 1; |
| 1179 | } |
| 1180 | case LYS_IFF_AND: |
| 1181 | case LYS_IFF_OR: |
| 1182 | a = resolve_iffeature_recursive(expr, index_e, index_f); |
| 1183 | b = resolve_iffeature_recursive(expr, index_e, index_f); |
| 1184 | if (a == -1 || b == -1) { |
| 1185 | /* one of the referenced feature is hidden by its if-feature, |
| 1186 | * so this if-feature expression is always false */ |
| 1187 | return -1; |
| 1188 | } else if (op == LYS_IFF_AND) { |
| 1189 | return a && b; |
| 1190 | } else { /* LYS_IFF_OR */ |
| 1191 | return a || b; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1192 | } |
| 1193 | } |
| 1194 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1195 | return -1; |
| 1196 | } |
| 1197 | |
| 1198 | int |
| 1199 | resolve_iffeature(struct lys_iffeature *expr) |
| 1200 | { |
| 1201 | int rc = -1; |
| 1202 | int index_e = 0, index_f = 0; |
| 1203 | |
| 1204 | if (expr->expr) { |
| 1205 | rc = resolve_iffeature_recursive(expr, &index_e, &index_f); |
| 1206 | } |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1207 | return (rc == 1) ? 1 : 0; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1208 | } |
| 1209 | |
| 1210 | struct iff_stack { |
| 1211 | int size; |
| 1212 | int index; /* first empty item */ |
| 1213 | uint8_t *stack; |
| 1214 | }; |
| 1215 | |
| 1216 | static int |
| 1217 | iff_stack_push(struct iff_stack *stack, uint8_t value) |
| 1218 | { |
| 1219 | if (stack->index == stack->size) { |
| 1220 | stack->size += 4; |
| 1221 | stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack); |
| 1222 | if (!stack->stack) { |
| 1223 | LOGMEM; |
| 1224 | stack->size = 0; |
| 1225 | return EXIT_FAILURE; |
| 1226 | } |
| 1227 | } |
| 1228 | |
| 1229 | stack->stack[stack->index++] = value; |
| 1230 | return EXIT_SUCCESS; |
| 1231 | } |
| 1232 | |
| 1233 | static uint8_t |
| 1234 | iff_stack_pop(struct iff_stack *stack) |
| 1235 | { |
| 1236 | stack->index--; |
| 1237 | return stack->stack[stack->index]; |
| 1238 | } |
| 1239 | |
| 1240 | static void |
| 1241 | iff_stack_clean(struct iff_stack *stack) |
| 1242 | { |
| 1243 | stack->size = 0; |
| 1244 | free(stack->stack); |
| 1245 | } |
| 1246 | |
| 1247 | static void |
| 1248 | iff_setop(uint8_t *list, uint8_t op, int pos) |
| 1249 | { |
| 1250 | uint8_t *item; |
| 1251 | uint8_t mask = 3; |
| 1252 | |
| 1253 | assert(pos >= 0); |
| 1254 | assert(op <= 3); /* max 2 bits */ |
| 1255 | |
| 1256 | item = &list[pos / 4]; |
| 1257 | mask = mask << 2 * (pos % 4); |
| 1258 | *item = (*item) & ~mask; |
| 1259 | *item = (*item) | (op << 2 * (pos % 4)); |
| 1260 | } |
| 1261 | |
| 1262 | uint8_t |
| 1263 | iff_getop(uint8_t *list, int pos) |
| 1264 | { |
| 1265 | uint8_t *item; |
| 1266 | uint8_t mask = 3, result; |
| 1267 | |
| 1268 | assert(pos >= 0); |
| 1269 | |
| 1270 | item = &list[pos / 4]; |
| 1271 | result = (*item) & (mask << 2 * (pos % 4)); |
| 1272 | return result >> 2 * (pos % 4); |
| 1273 | } |
| 1274 | |
| 1275 | #define LYS_IFF_LP 0x04 /* ( */ |
| 1276 | #define LYS_IFF_RP 0x08 /* ) */ |
| 1277 | |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1278 | /* internal structure for passing data for UNRES_IFFEAT */ |
| 1279 | struct unres_iffeat_data { |
| 1280 | struct lys_node *node; |
| 1281 | const char *fname; |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 1282 | int infeature; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1283 | }; |
| 1284 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1285 | void |
| 1286 | resolve_iffeature_getsizes(struct lys_iffeature *iffeat, unsigned int *expr_size, unsigned int *feat_size) |
| 1287 | { |
| 1288 | unsigned int e = 0, f = 0, r = 0; |
| 1289 | uint8_t op; |
| 1290 | |
| 1291 | assert(iffeat); |
| 1292 | |
| 1293 | if (!iffeat->expr) { |
| 1294 | goto result; |
| 1295 | } |
| 1296 | |
| 1297 | do { |
| 1298 | op = iff_getop(iffeat->expr, e++); |
| 1299 | switch (op) { |
| 1300 | case LYS_IFF_NOT: |
| 1301 | if (!r) { |
| 1302 | r += 1; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1303 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1304 | break; |
| 1305 | case LYS_IFF_AND: |
| 1306 | case LYS_IFF_OR: |
| 1307 | if (!r) { |
| 1308 | r += 2; |
| 1309 | } else { |
| 1310 | r += 1; |
| 1311 | } |
| 1312 | break; |
| 1313 | case LYS_IFF_F: |
| 1314 | f++; |
| 1315 | if (r) { |
| 1316 | r--; |
| 1317 | } |
| 1318 | break; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1319 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1320 | } while(r); |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1321 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1322 | result: |
| 1323 | if (expr_size) { |
| 1324 | *expr_size = e; |
| 1325 | } |
| 1326 | if (feat_size) { |
| 1327 | *feat_size = f; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1328 | } |
| 1329 | } |
| 1330 | |
| 1331 | int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1332 | 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] | 1333 | int infeature, struct unres_schema *unres) |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1334 | { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1335 | const char *c = value; |
| 1336 | int r, rc = EXIT_FAILURE; |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1337 | int i, j, last_not, checkversion = 0; |
| 1338 | unsigned int f_size = 0, expr_size = 0, f_exp = 1; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1339 | uint8_t op; |
| 1340 | struct iff_stack stack = {0, 0, NULL}; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1341 | struct unres_iffeat_data *iff_data; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1342 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1343 | assert(c); |
| 1344 | |
| 1345 | if (isspace(c[0])) { |
| 1346 | LOGVAL(LYE_INCHAR, LY_VLOG_NONE, NULL, c[0], c); |
| 1347 | return EXIT_FAILURE; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1348 | } |
| 1349 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1350 | /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */ |
| 1351 | for (i = j = last_not = 0; c[i]; i++) { |
| 1352 | if (c[i] == '(') { |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1353 | checkversion = 1; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1354 | j++; |
| 1355 | continue; |
| 1356 | } else if (c[i] == ')') { |
| 1357 | j--; |
| 1358 | continue; |
| 1359 | } else if (isspace(c[i])) { |
| 1360 | continue; |
| 1361 | } |
| 1362 | |
| 1363 | if (!strncmp(&c[i], "not", r = 3) || !strncmp(&c[i], "and", r = 3) || !strncmp(&c[i], "or", r = 2)) { |
| 1364 | if (c[i + r] == '\0') { |
Radek Krejci | a98da3f | 2016-07-27 14:05:22 +0200 | [diff] [blame] | 1365 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1366 | return EXIT_FAILURE; |
| 1367 | } else if (!isspace(c[i + r])) { |
| 1368 | /* feature name starting with the not/and/or */ |
| 1369 | last_not = 0; |
| 1370 | f_size++; |
| 1371 | } else if (c[i] == 'n') { /* not operation */ |
| 1372 | if (last_not) { |
| 1373 | /* double not */ |
| 1374 | expr_size = expr_size - 2; |
| 1375 | last_not = 0; |
| 1376 | } else { |
| 1377 | last_not = 1; |
| 1378 | } |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1379 | } else { /* and, or */ |
| 1380 | f_exp++; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1381 | /* not a not operation */ |
| 1382 | last_not = 0; |
| 1383 | } |
| 1384 | i += r; |
| 1385 | } else { |
| 1386 | f_size++; |
| 1387 | last_not = 0; |
| 1388 | } |
| 1389 | expr_size++; |
| 1390 | |
| 1391 | while (!isspace(c[i])) { |
| 1392 | if (!c[i] || c[i] == ')') { |
| 1393 | i--; |
| 1394 | break; |
| 1395 | } |
| 1396 | i++; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1397 | } |
| 1398 | } |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1399 | if (j || f_exp != f_size) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1400 | /* not matching count of ( and ) */ |
| 1401 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
| 1402 | return EXIT_FAILURE; |
| 1403 | } |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1404 | |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1405 | if (checkversion || expr_size > 1) { |
| 1406 | /* check that we have 1.1 module */ |
| 1407 | if (node->module->version != 2) { |
| 1408 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
| 1409 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "YANG 1.1 if-feature expression found in 1.0 module."); |
| 1410 | return EXIT_FAILURE; |
| 1411 | } |
| 1412 | } |
| 1413 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1414 | /* allocate the memory */ |
| 1415 | 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] | 1416 | iffeat_expr->features = calloc(f_size, sizeof *iffeat_expr->features); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1417 | stack.size = expr_size; |
| 1418 | stack.stack = malloc(expr_size * sizeof *stack.stack); |
| 1419 | if (!stack.stack || !iffeat_expr->expr || !iffeat_expr->features) { |
| 1420 | LOGMEM; |
| 1421 | goto error; |
| 1422 | } |
| 1423 | f_size--; expr_size--; /* used as indexes from now */ |
| 1424 | |
| 1425 | for (i--; i >= 0; i--) { |
| 1426 | if (c[i] == ')') { |
| 1427 | /* push it on stack */ |
| 1428 | iff_stack_push(&stack, LYS_IFF_RP); |
| 1429 | continue; |
| 1430 | } else if (c[i] == '(') { |
| 1431 | /* pop from the stack into result all operators until ) */ |
| 1432 | while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) { |
| 1433 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1434 | } |
| 1435 | continue; |
| 1436 | } else if (isspace(c[i])) { |
| 1437 | continue; |
| 1438 | } |
| 1439 | |
| 1440 | /* end operator or operand -> find beginning and get what is it */ |
| 1441 | j = i + 1; |
| 1442 | while (i >= 0 && !isspace(c[i]) && c[i] != '(') { |
| 1443 | i--; |
| 1444 | } |
| 1445 | i++; /* get back by one step */ |
| 1446 | |
| 1447 | if (!strncmp(&c[i], "not ", 4)) { |
| 1448 | if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) { |
| 1449 | /* double not */ |
| 1450 | iff_stack_pop(&stack); |
| 1451 | } else { |
| 1452 | /* not has the highest priority, so do not pop from the stack |
| 1453 | * as in case of AND and OR */ |
| 1454 | iff_stack_push(&stack, LYS_IFF_NOT); |
| 1455 | } |
| 1456 | } else if (!strncmp(&c[i], "and ", 4)) { |
| 1457 | /* as for OR - pop from the stack all operators with the same or higher |
| 1458 | * priority and store them to the result, then push the AND to the stack */ |
| 1459 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) { |
| 1460 | op = iff_stack_pop(&stack); |
| 1461 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1462 | } |
| 1463 | iff_stack_push(&stack, LYS_IFF_AND); |
| 1464 | } else if (!strncmp(&c[i], "or ", 3)) { |
| 1465 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) { |
| 1466 | op = iff_stack_pop(&stack); |
| 1467 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1468 | } |
| 1469 | iff_stack_push(&stack, LYS_IFF_OR); |
| 1470 | } else { |
| 1471 | /* feature name, length is j - i */ |
| 1472 | |
| 1473 | /* add it to the result */ |
| 1474 | iff_setop(iffeat_expr->expr, LYS_IFF_F, expr_size--); |
| 1475 | |
| 1476 | /* now get the link to the feature definition. Since it can be |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1477 | * forward referenced, we have to keep the feature name in auxiliary |
| 1478 | * structure passed into unres */ |
| 1479 | iff_data = malloc(sizeof *iff_data); |
| 1480 | iff_data->node = node; |
| 1481 | iff_data->fname = lydict_insert(node->module->ctx, &c[i], j - i); |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 1482 | iff_data->infeature = infeature; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1483 | r = unres_schema_add_node(node->module, unres, &iffeat_expr->features[f_size], UNRES_IFFEAT, |
| 1484 | (struct lys_node *)iff_data); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1485 | f_size--; |
| 1486 | |
| 1487 | if (r == -1) { |
Pavol Vican | 4d08451 | 2016-09-29 16:38:12 +0200 | [diff] [blame] | 1488 | free(iff_data); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1489 | goto error; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1490 | } |
| 1491 | } |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1492 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1493 | while (stack.index) { |
| 1494 | op = iff_stack_pop(&stack); |
| 1495 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1496 | } |
| 1497 | |
| 1498 | if (++expr_size || ++f_size) { |
| 1499 | /* not all expected operators and operands found */ |
| 1500 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
| 1501 | rc = EXIT_FAILURE; |
| 1502 | } else { |
| 1503 | rc = EXIT_SUCCESS; |
| 1504 | } |
| 1505 | |
| 1506 | error: |
| 1507 | /* cleanup */ |
| 1508 | iff_stack_clean(&stack); |
| 1509 | |
| 1510 | return rc; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1511 | } |
| 1512 | |
| 1513 | /** |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1514 | * @brief Resolve (find) a data node based on a schema-nodeid. |
| 1515 | * |
| 1516 | * Used for resolving unique statements - so id is expected to be relative and local (without reference to a different |
| 1517 | * module). |
| 1518 | * |
| 1519 | */ |
| 1520 | struct lyd_node * |
| 1521 | resolve_data_descendant_schema_nodeid(const char *nodeid, struct lyd_node *start) |
| 1522 | { |
| 1523 | char *str, *token, *p; |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1524 | struct lyd_node *result = NULL, *iter; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1525 | const struct lys_node *schema = NULL; |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1526 | int shorthand = 0; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1527 | |
| 1528 | assert(nodeid && start); |
| 1529 | |
| 1530 | if (nodeid[0] == '/') { |
| 1531 | return NULL; |
| 1532 | } |
| 1533 | |
| 1534 | str = p = strdup(nodeid); |
| 1535 | if (!str) { |
| 1536 | LOGMEM; |
| 1537 | return NULL; |
| 1538 | } |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1539 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1540 | while (p) { |
| 1541 | token = p; |
| 1542 | p = strchr(p, '/'); |
| 1543 | if (p) { |
| 1544 | *p = '\0'; |
| 1545 | p++; |
| 1546 | } |
| 1547 | |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1548 | if (p) { |
| 1549 | /* inner node */ |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1550 | if (resolve_descendant_schema_nodeid(token, schema ? schema->child : start->schema, |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 1551 | LYS_CONTAINER | LYS_CHOICE | LYS_CASE | LYS_LEAF, 0, 0, &schema) |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1552 | || !schema) { |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1553 | result = NULL; |
| 1554 | break; |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1555 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1556 | |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1557 | if (schema->nodetype & (LYS_CHOICE | LYS_CASE)) { |
| 1558 | continue; |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 1559 | } else if (lys_parent(schema)->nodetype == LYS_CHOICE) { |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1560 | /* shorthand case */ |
| 1561 | if (!shorthand) { |
| 1562 | shorthand = 1; |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 1563 | schema = lys_parent(schema); |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1564 | continue; |
| 1565 | } else { |
| 1566 | shorthand = 0; |
| 1567 | if (schema->nodetype == LYS_LEAF) { |
| 1568 | /* should not be here, since we have leaf, which is not a shorthand nor final node */ |
| 1569 | result = NULL; |
| 1570 | break; |
| 1571 | } |
| 1572 | } |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1573 | } |
| 1574 | } else { |
| 1575 | /* final node */ |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 1576 | if (resolve_descendant_schema_nodeid(token, schema ? schema->child : start->schema, LYS_LEAF, |
| 1577 | shorthand ? 0 : 1, 0, &schema) |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1578 | || !schema) { |
| 1579 | result = NULL; |
| 1580 | break; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1581 | } |
| 1582 | } |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1583 | LY_TREE_FOR(result ? result->child : start, iter) { |
| 1584 | if (iter->schema == schema) { |
| 1585 | /* move in data tree according to returned schema */ |
| 1586 | result = iter; |
| 1587 | break; |
| 1588 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1589 | } |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1590 | if (!iter) { |
| 1591 | /* instance not found */ |
| 1592 | result = NULL; |
| 1593 | break; |
| 1594 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1595 | } |
| 1596 | free(str); |
| 1597 | |
| 1598 | return result; |
| 1599 | } |
| 1600 | |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1601 | /* |
| 1602 | * 0 - ok (done) |
| 1603 | * 1 - continue |
| 1604 | * 2 - break |
| 1605 | * -1 - error |
| 1606 | */ |
| 1607 | static int |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1608 | 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] | 1609 | 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] | 1610 | int implemented_mod, const struct lys_node **start) |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1611 | { |
| 1612 | const struct lys_module *prefix_mod; |
| 1613 | |
| 1614 | /* module check */ |
| 1615 | 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] | 1616 | if (prefix_mod && implemented_mod) { |
Radek Krejci | 2eee5c0 | 2016-12-06 19:18:05 +0100 | [diff] [blame] | 1617 | prefix_mod = lys_implemented_module(prefix_mod); |
Radek Krejci | 0fa54e9 | 2016-09-14 14:01:05 +0200 | [diff] [blame] | 1618 | } |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1619 | if (!prefix_mod) { |
| 1620 | return -1; |
| 1621 | } |
| 1622 | if (prefix_mod != lys_node_module(sibling)) { |
| 1623 | return 1; |
| 1624 | } |
| 1625 | |
| 1626 | /* check for shorthand cases - then 'start' does not change */ |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 1627 | 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] | 1628 | if (*shorthand != -1) { |
| 1629 | *shorthand = *shorthand ? 0 : 1; |
| 1630 | } |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1631 | } |
| 1632 | |
| 1633 | /* the result node? */ |
| 1634 | if (!id[0]) { |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1635 | if (*shorthand == 1) { |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1636 | return 1; |
| 1637 | } |
| 1638 | return 0; |
| 1639 | } |
| 1640 | |
Radek Krejci | 3a13016 | 2016-11-07 16:21:20 +0100 | [diff] [blame] | 1641 | if (!(*shorthand)) { |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1642 | /* move down the tree, if possible */ |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 1643 | if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1644 | return -1; |
| 1645 | } |
| 1646 | *start = sibling->child; |
| 1647 | } |
| 1648 | |
| 1649 | return 2; |
| 1650 | } |
| 1651 | |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 1652 | /* start - relative, module - absolute, -1 error, EXIT_SUCCESS ok (but ret can still be NULL), >0 unexpected char on ret - 1 |
| 1653 | * implement: 0 - do not change the implemented status of the affected modules, 1 - change implemented status of the affected modules |
| 1654 | */ |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1655 | int |
| 1656 | 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] | 1657 | int implement, const struct lys_node **ret) |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1658 | { |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 1659 | const char *name, *mod_name, *mod_name_prev, *id; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1660 | const struct lys_node *sibling; |
| 1661 | int r, nam_len, mod_name_len, is_relative = -1; |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1662 | int8_t shorthand = 0; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1663 | /* 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] | 1664 | const struct lys_module *start_mod, *aux_mod; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1665 | |
| 1666 | assert(nodeid && (start || module) && !(start && module) && ret); |
| 1667 | |
| 1668 | id = nodeid; |
| 1669 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1670 | 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] | 1671 | return ((id - nodeid) - r) + 1; |
| 1672 | } |
| 1673 | id += r; |
| 1674 | |
| 1675 | if ((is_relative && !start) || (!is_relative && !module)) { |
| 1676 | return -1; |
| 1677 | } |
| 1678 | |
| 1679 | /* descendant-schema-nodeid */ |
| 1680 | if (is_relative) { |
Michal Vasko | 4c06fd8 | 2016-02-17 13:43:38 +0100 | [diff] [blame] | 1681 | module = start_mod = start->module; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1682 | |
| 1683 | /* absolute-schema-nodeid */ |
| 1684 | } else { |
| 1685 | 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] | 1686 | if (start_mod != lys_main_module(module) && start_mod && !start_mod->implemented) { |
Radek Krejci | 0fa54e9 | 2016-09-14 14:01:05 +0200 | [diff] [blame] | 1687 | /* if the submodule augments the mainmodule (or in general a module augments |
| 1688 | * itself, we don't want to search for the implemented module but augments |
| 1689 | * the module anyway. But when augmenting another module, we need the implemented |
| 1690 | * revision of the module if any */ |
Radek Krejci | 2eee5c0 | 2016-12-06 19:18:05 +0100 | [diff] [blame] | 1691 | aux_mod = lys_implemented_module(start_mod); |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 1692 | if (!aux_mod->implemented && implement) { |
| 1693 | /* make the found module implemented */ |
| 1694 | if (lys_set_implemented(aux_mod)) { |
| 1695 | return -1; |
| 1696 | } |
| 1697 | } |
| 1698 | start_mod = aux_mod; |
| 1699 | implement++; |
Radek Krejci | 0fa54e9 | 2016-09-14 14:01:05 +0200 | [diff] [blame] | 1700 | } |
Michal Vasko | e290563 | 2016-02-11 15:42:24 +0100 | [diff] [blame] | 1701 | if (!start_mod) { |
| 1702 | return -1; |
| 1703 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1704 | start = start_mod->data; |
| 1705 | } |
| 1706 | |
| 1707 | while (1) { |
| 1708 | sibling = NULL; |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 1709 | mod_name_prev = mod_name; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1710 | while ((sibling = lys_getnext(sibling, lys_parent(start), start_mod, |
| 1711 | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT))) { |
| 1712 | /* name match */ |
Michal Vasko | 0a1aaa4 | 2016-04-19 09:48:25 +0200 | [diff] [blame] | 1713 | 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] | 1714 | 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] | 1715 | implement, &start); |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1716 | if (r == 0) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1717 | *ret = sibling; |
| 1718 | return EXIT_SUCCESS; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1719 | } else if (r == 1) { |
| 1720 | continue; |
| 1721 | } else if (r == 2) { |
| 1722 | break; |
| 1723 | } else { |
| 1724 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1725 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1726 | } |
| 1727 | } |
| 1728 | |
| 1729 | /* no match */ |
| 1730 | if (!sibling) { |
Michal Vasko | a426fef | 2016-03-07 10:47:31 +0100 | [diff] [blame] | 1731 | *ret = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1732 | return EXIT_SUCCESS; |
| 1733 | } |
| 1734 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1735 | 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] | 1736 | return ((id - nodeid) - r) + 1; |
| 1737 | } |
| 1738 | id += r; |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 1739 | |
| 1740 | if ((mod_name && mod_name_prev && strncmp(mod_name, mod_name_prev, mod_name_len + 1)) || |
| 1741 | (mod_name != mod_name_prev && (!mod_name || !mod_name_prev))) { |
| 1742 | /* we are getting into another module (augment) */ |
| 1743 | if (implement) { |
| 1744 | /* we have to check that also target modules are implemented, if not, we have to change it */ |
| 1745 | aux_mod = lys_get_import_module(module, NULL, 0, mod_name, mod_name_len); |
| 1746 | if (!aux_mod) { |
| 1747 | return -1; |
| 1748 | } |
| 1749 | if (!aux_mod->implemented) { |
Radek Krejci | 2eee5c0 | 2016-12-06 19:18:05 +0100 | [diff] [blame] | 1750 | aux_mod = lys_implemented_module(aux_mod); |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 1751 | if (!aux_mod->implemented) { |
| 1752 | /* make the found module implemented */ |
| 1753 | if (lys_set_implemented(aux_mod)) { |
| 1754 | return -1; |
| 1755 | } |
| 1756 | } |
| 1757 | } |
| 1758 | } else { |
| 1759 | /* we are not implementing the module itself, so the augments outside the module are ignored */ |
| 1760 | *ret = NULL; |
| 1761 | return EXIT_SUCCESS; |
| 1762 | } |
| 1763 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1764 | } |
| 1765 | |
| 1766 | /* cannot get here */ |
| 1767 | LOGINT; |
| 1768 | return -1; |
| 1769 | } |
| 1770 | |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 1771 | /* unique, refine, |
| 1772 | * >0 - unexpected char on position (ret - 1), |
| 1773 | * 0 - ok (but ret can still be NULL), |
| 1774 | * -1 - error, |
| 1775 | * -2 - violated no_innerlist */ |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1776 | int |
| 1777 | 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] | 1778 | int check_shorthand, int no_innerlist, const struct lys_node **ret) |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1779 | { |
| 1780 | const char *name, *mod_name, *id; |
| 1781 | const struct lys_node *sibling; |
| 1782 | int r, nam_len, mod_name_len, is_relative = -1; |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1783 | int8_t shorthand = check_shorthand ? 0 : -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1784 | /* 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] | 1785 | const struct lys_module *module; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1786 | |
| 1787 | assert(nodeid && start && ret); |
| 1788 | assert(!(ret_nodetype & (LYS_USES | LYS_AUGMENT)) && ((ret_nodetype == LYS_GROUPING) || !(ret_nodetype & LYS_GROUPING))); |
| 1789 | |
| 1790 | id = nodeid; |
| 1791 | module = start->module; |
| 1792 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1793 | 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] | 1794 | return ((id - nodeid) - r) + 1; |
| 1795 | } |
| 1796 | id += r; |
| 1797 | |
| 1798 | if (!is_relative) { |
| 1799 | return -1; |
| 1800 | } |
| 1801 | |
| 1802 | while (1) { |
| 1803 | sibling = NULL; |
| 1804 | while ((sibling = lys_getnext(sibling, lys_parent(start), module, |
| 1805 | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE))) { |
| 1806 | /* name match */ |
| 1807 | 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] | 1808 | 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] | 1809 | if (r == 0) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1810 | if (!(sibling->nodetype & ret_nodetype)) { |
| 1811 | /* wrong node type, too bad */ |
| 1812 | continue; |
| 1813 | } |
| 1814 | *ret = sibling; |
| 1815 | return EXIT_SUCCESS; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1816 | } else if (r == 1) { |
| 1817 | continue; |
| 1818 | } else if (r == 2) { |
| 1819 | break; |
| 1820 | } else { |
| 1821 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1822 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1823 | } |
| 1824 | } |
| 1825 | |
| 1826 | /* no match */ |
| 1827 | if (!sibling) { |
Michal Vasko | a426fef | 2016-03-07 10:47:31 +0100 | [diff] [blame] | 1828 | *ret = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1829 | return EXIT_SUCCESS; |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 1830 | } else if (no_innerlist && sibling->nodetype == LYS_LIST) { |
| 1831 | *ret = NULL; |
| 1832 | return -2; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1833 | } |
| 1834 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1835 | 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] | 1836 | return ((id - nodeid) - r) + 1; |
| 1837 | } |
| 1838 | id += r; |
| 1839 | } |
| 1840 | |
| 1841 | /* cannot get here */ |
| 1842 | LOGINT; |
| 1843 | return -1; |
| 1844 | } |
| 1845 | |
| 1846 | /* choice default */ |
| 1847 | int |
| 1848 | resolve_choice_default_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_node **ret) |
| 1849 | { |
| 1850 | /* cannot actually be a path */ |
| 1851 | if (strchr(nodeid, '/')) { |
| 1852 | return -1; |
| 1853 | } |
| 1854 | |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 1855 | 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] | 1856 | } |
| 1857 | |
| 1858 | /* uses, -1 error, EXIT_SUCCESS ok (but ret can still be NULL), >0 unexpected char on ret - 1 */ |
| 1859 | static int |
| 1860 | resolve_uses_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_node_grp **ret) |
| 1861 | { |
| 1862 | const struct lys_module *module; |
| 1863 | const char *mod_prefix, *name; |
| 1864 | int i, mod_prefix_len, nam_len; |
| 1865 | |
| 1866 | /* parse the identifier, it must be parsed on one call */ |
| 1867 | if (((i = parse_node_identifier(nodeid, &mod_prefix, &mod_prefix_len, &name, &nam_len)) < 1) || nodeid[i]) { |
| 1868 | return -i + 1; |
| 1869 | } |
| 1870 | |
| 1871 | module = lys_get_import_module(start->module, mod_prefix, mod_prefix_len, NULL, 0); |
| 1872 | if (!module) { |
| 1873 | return -1; |
| 1874 | } |
| 1875 | if (module != start->module) { |
| 1876 | start = module->data; |
| 1877 | } |
| 1878 | |
| 1879 | *ret = lys_find_grouping_up(name, (struct lys_node *)start); |
| 1880 | |
| 1881 | return EXIT_SUCCESS; |
| 1882 | } |
| 1883 | |
| 1884 | int |
| 1885 | resolve_absolute_schema_nodeid(const char *nodeid, const struct lys_module *module, int ret_nodetype, |
| 1886 | const struct lys_node **ret) |
| 1887 | { |
| 1888 | const char *name, *mod_name, *id; |
| 1889 | const struct lys_node *sibling, *start; |
| 1890 | int r, nam_len, mod_name_len, is_relative = -1; |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1891 | int8_t shorthand = 0; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1892 | const struct lys_module *abs_start_mod; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1893 | |
| 1894 | assert(nodeid && module && ret); |
| 1895 | assert(!(ret_nodetype & (LYS_USES | LYS_AUGMENT)) && ((ret_nodetype == LYS_GROUPING) || !(ret_nodetype & LYS_GROUPING))); |
| 1896 | |
| 1897 | id = nodeid; |
| 1898 | start = module->data; |
| 1899 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1900 | 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] | 1901 | return ((id - nodeid) - r) + 1; |
| 1902 | } |
| 1903 | id += r; |
| 1904 | |
| 1905 | if (is_relative) { |
| 1906 | return -1; |
| 1907 | } |
| 1908 | |
| 1909 | 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] | 1910 | if (!abs_start_mod) { |
| 1911 | return -1; |
| 1912 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1913 | |
| 1914 | while (1) { |
| 1915 | sibling = NULL; |
| 1916 | while ((sibling = lys_getnext(sibling, lys_parent(start), abs_start_mod, LYS_GETNEXT_WITHCHOICE |
| 1917 | | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT | LYS_GETNEXT_WITHGROUPING))) { |
| 1918 | /* name match */ |
Michal Vasko | 0a1aaa4 | 2016-04-19 09:48:25 +0200 | [diff] [blame] | 1919 | 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] | 1920 | 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] | 1921 | if (r == 0) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1922 | if (!(sibling->nodetype & ret_nodetype)) { |
| 1923 | /* wrong node type, too bad */ |
| 1924 | continue; |
| 1925 | } |
| 1926 | *ret = sibling; |
| 1927 | return EXIT_SUCCESS; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1928 | } else if (r == 1) { |
| 1929 | continue; |
| 1930 | } else if (r == 2) { |
| 1931 | break; |
| 1932 | } else { |
| 1933 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1934 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1935 | } |
| 1936 | } |
| 1937 | |
| 1938 | /* no match */ |
| 1939 | if (!sibling) { |
Michal Vasko | a426fef | 2016-03-07 10:47:31 +0100 | [diff] [blame] | 1940 | *ret = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1941 | return EXIT_SUCCESS; |
| 1942 | } |
| 1943 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1944 | 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] | 1945 | return ((id - nodeid) - r) + 1; |
| 1946 | } |
| 1947 | id += r; |
| 1948 | } |
| 1949 | |
| 1950 | /* cannot get here */ |
| 1951 | LOGINT; |
| 1952 | return -1; |
| 1953 | } |
| 1954 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1955 | static int |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1956 | 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] | 1957 | { |
| 1958 | const char *name; |
| 1959 | int nam_len, has_predicate, i; |
| 1960 | |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 1961 | if (((i = parse_schema_json_predicate(predicate, &name, &nam_len, NULL, NULL, &has_predicate)) < 1) |
| 1962 | || !strncmp(name, ".", nam_len)) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 1963 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-i], &predicate[-i]); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1964 | return -1; |
| 1965 | } |
| 1966 | |
| 1967 | predicate += i; |
| 1968 | *parsed += i; |
| 1969 | |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 1970 | if (!isdigit(name[0])) { |
| 1971 | for (i = 0; i < list->keys_size; ++i) { |
| 1972 | if (!strncmp(list->keys[i]->name, name, nam_len) && !list->keys[i]->name[nam_len]) { |
| 1973 | break; |
| 1974 | } |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1975 | } |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1976 | |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 1977 | if (i == list->keys_size) { |
| 1978 | LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name); |
| 1979 | return -1; |
| 1980 | } |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1981 | } |
| 1982 | |
| 1983 | /* more predicates? */ |
| 1984 | if (has_predicate) { |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1985 | return resolve_json_schema_list_predicate(predicate, list, parsed); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1986 | } |
| 1987 | |
| 1988 | return 0; |
| 1989 | } |
| 1990 | |
Michal Vasko | 8d26e5c | 2016-09-08 10:03:49 +0200 | [diff] [blame] | 1991 | /* cannot return LYS_GROUPING, LYS_AUGMENT, LYS_USES, logs directly */ |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1992 | const struct lys_node * |
Michal Vasko | 8d26e5c | 2016-09-08 10:03:49 +0200 | [diff] [blame] | 1993 | 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] | 1994 | { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 1995 | char *module_name = ly_buf(), *buf_backup = NULL, *str; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1996 | const char *name, *mod_name, *id; |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1997 | const struct lys_node *sibling; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1998 | 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] | 1999 | /* 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] | 2000 | const struct lys_module *prefix_mod, *module, *prev_mod; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2001 | |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2002 | assert(nodeid && (ctx || start)); |
| 2003 | if (!ctx) { |
| 2004 | ctx = start->module->ctx; |
| 2005 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2006 | |
| 2007 | id = nodeid; |
| 2008 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2009 | 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] | 2010 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2011 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2012 | } |
| 2013 | id += r; |
| 2014 | |
| 2015 | if (is_relative) { |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2016 | assert(start); |
| 2017 | start = start->child; |
| 2018 | if (!start) { |
| 2019 | /* no descendants, fail for sure */ |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2020 | str = strndup(nodeid, (name + nam_len) - nodeid); |
| 2021 | LOGVAL(LYE_PATH_INNODE, LY_VLOG_STR, str); |
| 2022 | free(str); |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2023 | return NULL; |
| 2024 | } |
| 2025 | module = start->module; |
| 2026 | } else { |
| 2027 | if (!mod_name) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2028 | str = strndup(nodeid, (name + nam_len) - nodeid); |
| 2029 | LOGVAL(LYE_PATH_MISSMOD, LY_VLOG_STR, nodeid); |
| 2030 | free(str); |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2031 | return NULL; |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2032 | } else if (mod_name_len > LY_BUF_SIZE - 1) { |
| 2033 | LOGINT; |
| 2034 | return NULL; |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2035 | } |
| 2036 | |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2037 | if (ly_buf_used && module_name[0]) { |
| 2038 | buf_backup = strndup(module_name, LY_BUF_SIZE - 1); |
| 2039 | } |
| 2040 | ly_buf_used++; |
| 2041 | |
Michal Vasko | df3f1ab | 2016-04-01 15:07:22 +0200 | [diff] [blame] | 2042 | memmove(module_name, mod_name, mod_name_len); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2043 | module_name[mod_name_len] = '\0'; |
| 2044 | module = ly_ctx_get_module(ctx, module_name, NULL); |
| 2045 | |
| 2046 | if (buf_backup) { |
| 2047 | /* return previous internal buffer content */ |
| 2048 | strcpy(module_name, buf_backup); |
| 2049 | free(buf_backup); |
| 2050 | buf_backup = NULL; |
| 2051 | } |
| 2052 | ly_buf_used--; |
| 2053 | |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2054 | if (!module) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2055 | str = strndup(nodeid, (mod_name + mod_name_len) - nodeid); |
| 2056 | LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str); |
| 2057 | free(str); |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2058 | return NULL; |
| 2059 | } |
| 2060 | start = module->data; |
| 2061 | |
| 2062 | /* now it's as if there was no module name */ |
| 2063 | mod_name = NULL; |
| 2064 | mod_name_len = 0; |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2065 | } |
| 2066 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2067 | prev_mod = module; |
| 2068 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2069 | while (1) { |
| 2070 | sibling = NULL; |
Michal Vasko | 8d26e5c | 2016-09-08 10:03:49 +0200 | [diff] [blame] | 2071 | while ((sibling = lys_getnext(sibling, lys_parent(start), module, |
| 2072 | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT))) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2073 | /* name match */ |
Michal Vasko | 0a1aaa4 | 2016-04-19 09:48:25 +0200 | [diff] [blame] | 2074 | 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] | 2075 | /* module check */ |
| 2076 | if (mod_name) { |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2077 | if (mod_name_len > LY_BUF_SIZE - 1) { |
Michal Vasko | 8757e7c | 2016-03-15 10:41:30 +0100 | [diff] [blame] | 2078 | LOGINT; |
| 2079 | return NULL; |
| 2080 | } |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2081 | |
| 2082 | if (ly_buf_used && module_name[0]) { |
| 2083 | buf_backup = strndup(module_name, LY_BUF_SIZE - 1); |
| 2084 | } |
| 2085 | ly_buf_used++; |
| 2086 | |
Michal Vasko | df3f1ab | 2016-04-01 15:07:22 +0200 | [diff] [blame] | 2087 | memmove(module_name, mod_name, mod_name_len); |
Michal Vasko | 8757e7c | 2016-03-15 10:41:30 +0100 | [diff] [blame] | 2088 | module_name[mod_name_len] = '\0'; |
| 2089 | /* will also find an augment module */ |
| 2090 | prefix_mod = ly_ctx_get_module(ctx, module_name, NULL); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2091 | |
| 2092 | if (buf_backup) { |
| 2093 | /* return previous internal buffer content */ |
Michal Vasko | 888a129 | 2016-04-05 12:08:54 +0200 | [diff] [blame] | 2094 | strncpy(module_name, buf_backup, LY_BUF_SIZE - 1); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2095 | free(buf_backup); |
| 2096 | buf_backup = NULL; |
| 2097 | } |
| 2098 | ly_buf_used--; |
| 2099 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2100 | if (!prefix_mod) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2101 | str = strndup(nodeid, (mod_name + mod_name_len) - nodeid); |
| 2102 | LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str); |
| 2103 | free(str); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2104 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2105 | } |
| 2106 | } else { |
| 2107 | prefix_mod = prev_mod; |
| 2108 | } |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 2109 | if (prefix_mod != lys_node_module(sibling)) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2110 | continue; |
| 2111 | } |
| 2112 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2113 | /* do we have some predicates on it? */ |
| 2114 | if (has_predicate) { |
| 2115 | r = 0; |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 2116 | if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 2117 | if ((r = parse_schema_json_predicate(id, NULL, NULL, NULL, NULL, &has_predicate)) < 1) { |
| 2118 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
| 2119 | return NULL; |
| 2120 | } |
| 2121 | } else if (sibling->nodetype == LYS_LIST) { |
| 2122 | if (resolve_json_schema_list_predicate(id, (const struct lys_node_list *)sibling, &r)) { |
| 2123 | return NULL; |
| 2124 | } |
| 2125 | } else { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2126 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2127 | return NULL; |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2128 | } |
| 2129 | id += r; |
| 2130 | } |
| 2131 | |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 2132 | /* check for shorthand cases - then 'start' does not change */ |
Michal Vasko | 8d26e5c | 2016-09-08 10:03:49 +0200 | [diff] [blame] | 2133 | 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] | 2134 | shorthand = ~shorthand; |
| 2135 | } |
| 2136 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2137 | /* the result node? */ |
| 2138 | if (!id[0]) { |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 2139 | if (shorthand) { |
| 2140 | /* wrong path for shorthand */ |
Michal Vasko | 025e045 | 2016-05-17 16:14:20 +0200 | [diff] [blame] | 2141 | str = strndup(nodeid, (name + nam_len) - nodeid); |
| 2142 | LOGVAL(LYE_PATH_INNODE, LY_VLOG_STR, str); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 2143 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Schema shorthand case path must include the virtual case statement."); |
Radek Krejci | 9a5fccc | 2016-05-18 15:44:58 +0200 | [diff] [blame] | 2144 | free(str); |
Michal Vasko | 025e045 | 2016-05-17 16:14:20 +0200 | [diff] [blame] | 2145 | return NULL; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 2146 | } |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2147 | return sibling; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2148 | } |
| 2149 | |
Michal Vasko | 8d26e5c | 2016-09-08 10:03:49 +0200 | [diff] [blame] | 2150 | if (!shorthand) { |
Michal Vasko | 7dc71d0 | 2016-03-15 10:42:28 +0100 | [diff] [blame] | 2151 | /* move down the tree, if possible */ |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 2152 | if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2153 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
Michal Vasko | 7dc71d0 | 2016-03-15 10:42:28 +0100 | [diff] [blame] | 2154 | return NULL; |
| 2155 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2156 | start = sibling->child; |
| 2157 | } |
| 2158 | |
| 2159 | /* update prev mod */ |
| 2160 | prev_mod = start->module; |
| 2161 | break; |
| 2162 | } |
| 2163 | } |
| 2164 | |
| 2165 | /* no match */ |
| 2166 | if (!sibling) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2167 | str = strndup(nodeid, (name + nam_len) - nodeid); |
| 2168 | LOGVAL(LYE_PATH_INNODE, LY_VLOG_STR, str); |
| 2169 | free(str); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2170 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2171 | } |
| 2172 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2173 | 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] | 2174 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2175 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2176 | } |
| 2177 | id += r; |
| 2178 | } |
| 2179 | |
| 2180 | /* cannot get here */ |
| 2181 | LOGINT; |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2182 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2183 | } |
| 2184 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2185 | static int |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2186 | resolve_partial_json_data_list_predicate(const char *predicate, const char *node_name, struct lyd_node *node, |
| 2187 | int position, int *parsed) |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2188 | { |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2189 | const char *name, *value, *key_val; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2190 | int nam_len, val_len, has_predicate = 1, r; |
| 2191 | uint16_t i; |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2192 | struct lyd_node_leaf_list *key; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2193 | |
Radek Krejci | 61a86c6 | 2016-03-24 11:06:44 +0100 | [diff] [blame] | 2194 | assert(node); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2195 | assert(node->schema->nodetype == LYS_LIST); |
| 2196 | |
Michal Vasko | 53adfc7 | 2017-01-06 10:39:10 +0100 | [diff] [blame] | 2197 | /* is the predicate a number? */ |
| 2198 | if (((r = parse_schema_json_predicate(predicate, &name, &nam_len, &value, &val_len, &has_predicate)) < 1) |
| 2199 | || !strncmp(name, ".", nam_len)) { |
| 2200 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-r], &predicate[-r]); |
| 2201 | return -1; |
| 2202 | } |
| 2203 | |
| 2204 | if (isdigit(name[0])) { |
| 2205 | if (position == atoi(name)) { |
| 2206 | /* match */ |
| 2207 | *parsed += r; |
| 2208 | return 0; |
| 2209 | } else { |
| 2210 | /* not a match */ |
| 2211 | return 1; |
| 2212 | } |
| 2213 | } |
| 2214 | |
| 2215 | if (!((struct lys_node_list *)node->schema)->keys_size) { |
| 2216 | /* no keys in schema - causes an error later */ |
| 2217 | return 0; |
| 2218 | } |
| 2219 | |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2220 | key = (struct lyd_node_leaf_list *)node->child; |
Michal Vasko | 53adfc7 | 2017-01-06 10:39:10 +0100 | [diff] [blame] | 2221 | if (!key) { |
| 2222 | /* it is not a position, so we need a key for it to be a match */ |
| 2223 | return 1; |
| 2224 | } |
| 2225 | |
| 2226 | /* go through all the keys */ |
| 2227 | i = 0; |
| 2228 | goto check_parsed_values; |
| 2229 | |
| 2230 | for (; i < ((struct lys_node_list *)node->schema)->keys_size; ++i) { |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2231 | if (!has_predicate) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2232 | LOGVAL(LYE_PATH_MISSKEY, LY_VLOG_NONE, NULL, node_name); |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2233 | return -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2234 | } |
| 2235 | |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 2236 | if (((r = parse_schema_json_predicate(predicate, &name, &nam_len, &value, &val_len, &has_predicate)) < 1) |
| 2237 | || !strncmp(name, ".", nam_len)) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2238 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-r], &predicate[-r]); |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2239 | return -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2240 | } |
| 2241 | |
Michal Vasko | 53adfc7 | 2017-01-06 10:39:10 +0100 | [diff] [blame] | 2242 | check_parsed_values: |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2243 | predicate += r; |
| 2244 | *parsed += r; |
| 2245 | |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2246 | 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] | 2247 | LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name); |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2248 | return -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2249 | } |
| 2250 | |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2251 | /* make value canonical */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 2252 | if ((key->value_type & LY_TYPE_IDENT) |
Michal Vasko | 6a938d6 | 2016-12-21 09:21:30 +0100 | [diff] [blame] | 2253 | && !strncmp(key->value_str, lyd_node_module(node)->name, strlen(lyd_node_module(node)->name)) |
| 2254 | && (key->value_str[strlen(lyd_node_module(node)->name)] == ':')) { |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2255 | key_val = key->value_str + strlen(lyd_node_module(node)->name) + 1; |
| 2256 | } else { |
| 2257 | key_val = key->value_str; |
| 2258 | } |
| 2259 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2260 | /* value does not match */ |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2261 | if (strncmp(key_val, value, val_len) || key_val[val_len]) { |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2262 | return 1; |
| 2263 | } |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2264 | |
| 2265 | key = (struct lyd_node_leaf_list *)key->next; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2266 | } |
| 2267 | |
| 2268 | if (has_predicate) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2269 | LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2270 | return -1; |
| 2271 | } |
| 2272 | |
| 2273 | return 0; |
| 2274 | } |
| 2275 | |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 2276 | /** |
| 2277 | * @brief get the closest parent of the node (or the node itself) identified by the nodeid (path) |
| 2278 | * |
| 2279 | * @param[in] nodeid Node data path to find |
| 2280 | * @param[in] llist_value If the \p nodeid identifies leaf-list, this is expected value of the leaf-list instance. |
| 2281 | * @param[in] options Bitmask of options flags, see @ref pathoptions. |
| 2282 | * @param[out] parsed Number of characters processed in \p id |
| 2283 | * @return The closes parent (or the node itself) from the path |
| 2284 | */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2285 | struct lyd_node * |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2286 | resolve_partial_json_data_nodeid(const char *nodeid, const char *llist_value, struct lyd_node *start, int options, |
| 2287 | int *parsed) |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2288 | { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2289 | char *module_name = ly_buf(), *buf_backup = NULL, *str; |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2290 | const char *id, *mod_name, *name, *pred_name, *data_val; |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2291 | int r, ret, mod_name_len, nam_len, is_relative = -1, list_instance_position; |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2292 | 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] | 2293 | struct lyd_node *sibling, *last_match = NULL; |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2294 | struct lyd_node_leaf_list *llist; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2295 | const struct lys_module *prefix_mod, *prev_mod; |
| 2296 | struct ly_ctx *ctx; |
| 2297 | |
| 2298 | assert(nodeid && start && parsed); |
| 2299 | |
| 2300 | ctx = start->schema->module->ctx; |
| 2301 | id = nodeid; |
| 2302 | |
| 2303 | 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] | 2304 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2305 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2306 | return NULL; |
| 2307 | } |
| 2308 | id += r; |
| 2309 | /* add it to parsed only after the data node was actually found */ |
| 2310 | last_parsed = r; |
| 2311 | |
| 2312 | if (is_relative) { |
Michal Vasko | 1adc724 | 2016-11-16 11:05:28 +0100 | [diff] [blame] | 2313 | prev_mod = lyd_node_module(start); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2314 | start = start->child; |
| 2315 | } else { |
| 2316 | for (; start->parent; start = start->parent); |
Michal Vasko | 1adc724 | 2016-11-16 11:05:28 +0100 | [diff] [blame] | 2317 | prev_mod = lyd_node_module(start); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2318 | } |
| 2319 | |
| 2320 | while (1) { |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2321 | list_instance_position = 0; |
| 2322 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2323 | LY_TREE_FOR(start, sibling) { |
Michal Vasko | 945b96b | 2016-10-18 11:49:12 +0200 | [diff] [blame] | 2324 | /* 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] | 2325 | if (lys_parent(sibling->schema)) { |
| 2326 | if (options & LYD_PATH_OPT_OUTPUT) { |
| 2327 | if (lys_parent(sibling->schema)->nodetype == LYS_INPUT) { |
Michal Vasko | b724689 | 2016-04-19 10:37:35 +0200 | [diff] [blame] | 2328 | 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] | 2329 | *parsed = -1; |
| 2330 | return NULL; |
| 2331 | } |
| 2332 | } else { |
| 2333 | if (lys_parent(sibling->schema)->nodetype == LYS_OUTPUT) { |
Michal Vasko | b724689 | 2016-04-19 10:37:35 +0200 | [diff] [blame] | 2334 | 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] | 2335 | *parsed = -1; |
| 2336 | return NULL; |
| 2337 | } |
| 2338 | } |
| 2339 | } |
| 2340 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2341 | /* name match */ |
| 2342 | if (!strncmp(name, sibling->schema->name, nam_len) && !sibling->schema->name[nam_len]) { |
| 2343 | |
| 2344 | /* module check */ |
| 2345 | if (mod_name) { |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2346 | if (mod_name_len > LY_BUF_SIZE - 1) { |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2347 | LOGINT; |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2348 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2349 | return NULL; |
| 2350 | } |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2351 | |
| 2352 | if (ly_buf_used && module_name[0]) { |
| 2353 | buf_backup = strndup(module_name, LY_BUF_SIZE - 1); |
| 2354 | } |
| 2355 | ly_buf_used++; |
| 2356 | |
Michal Vasko | df3f1ab | 2016-04-01 15:07:22 +0200 | [diff] [blame] | 2357 | memmove(module_name, mod_name, mod_name_len); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2358 | module_name[mod_name_len] = '\0'; |
| 2359 | /* will also find an augment module */ |
| 2360 | prefix_mod = ly_ctx_get_module(ctx, module_name, NULL); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2361 | |
| 2362 | if (buf_backup) { |
| 2363 | /* return previous internal buffer content */ |
Michal Vasko | 888a129 | 2016-04-05 12:08:54 +0200 | [diff] [blame] | 2364 | strncpy(module_name, buf_backup, LY_BUF_SIZE - 1); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2365 | free(buf_backup); |
| 2366 | buf_backup = NULL; |
| 2367 | } |
| 2368 | ly_buf_used--; |
| 2369 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2370 | if (!prefix_mod) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2371 | str = strndup(nodeid, (mod_name + mod_name_len) - nodeid); |
| 2372 | LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str); |
| 2373 | free(str); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2374 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2375 | return NULL; |
| 2376 | } |
| 2377 | } else { |
| 2378 | prefix_mod = prev_mod; |
| 2379 | } |
Michal Vasko | 1adc724 | 2016-11-16 11:05:28 +0100 | [diff] [blame] | 2380 | if (prefix_mod != lyd_node_module(sibling)) { |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2381 | continue; |
| 2382 | } |
| 2383 | |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2384 | /* leaf-list, did we find it with the correct value or not? */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2385 | if (sibling->schema->nodetype == LYS_LEAFLIST) { |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2386 | llist = (struct lyd_node_leaf_list *)sibling; |
| 2387 | |
Michal Vasko | 0a8d17f | 2016-10-19 15:51:36 +0200 | [diff] [blame] | 2388 | last_has_pred = 0; |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2389 | if (has_predicate) { |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2390 | 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] | 2391 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
| 2392 | *parsed = -1; |
| 2393 | return NULL; |
| 2394 | } |
Michal Vasko | 0a8d17f | 2016-10-19 15:51:36 +0200 | [diff] [blame] | 2395 | if ((pred_name[0] != '.') || (pred_name_len != 1)) { |
| 2396 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[1], id + 1); |
| 2397 | *parsed = -1; |
| 2398 | return NULL; |
| 2399 | } |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2400 | } else { |
| 2401 | r = 0; |
| 2402 | if (llist_value) { |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2403 | llval_len = strlen(llist_value); |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2404 | } |
| 2405 | } |
| 2406 | |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2407 | /* make value canonical */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 2408 | if ((llist->value_type & LY_TYPE_IDENT) |
Michal Vasko | 6a938d6 | 2016-12-21 09:21:30 +0100 | [diff] [blame] | 2409 | && !strncmp(llist->value_str, lyd_node_module(sibling)->name, strlen(lyd_node_module(sibling)->name)) |
| 2410 | && (llist->value_str[strlen(lyd_node_module(sibling)->name)] == ':')) { |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2411 | data_val = llist->value_str + strlen(lyd_node_module(sibling)->name) + 1; |
| 2412 | } else { |
| 2413 | data_val = llist->value_str; |
| 2414 | } |
| 2415 | |
| 2416 | if ((!llist_value && data_val && data_val[0]) |
| 2417 | || (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] | 2418 | continue; |
| 2419 | } |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2420 | |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2421 | id += r; |
| 2422 | last_parsed += r; |
Michal Vasko | 0a8d17f | 2016-10-19 15:51:36 +0200 | [diff] [blame] | 2423 | has_predicate = last_has_pred; |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2424 | |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 2425 | } else if (sibling->schema->nodetype == LYS_LIST) { |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2426 | /* list, we likely need predicates'n'stuff then, but if without a predicate, we are always creating it */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2427 | if (!has_predicate) { |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2428 | /* none match */ |
| 2429 | return last_match; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2430 | } |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2431 | |
| 2432 | ++list_instance_position; |
| 2433 | r = 0; |
| 2434 | ret = resolve_partial_json_data_list_predicate(id, name, sibling, list_instance_position, &r); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2435 | if (ret == -1) { |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2436 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2437 | return NULL; |
| 2438 | } else if (ret == 1) { |
| 2439 | /* this list instance does not match */ |
| 2440 | continue; |
| 2441 | } |
| 2442 | id += r; |
| 2443 | last_parsed += r; |
| 2444 | } |
| 2445 | |
| 2446 | *parsed += last_parsed; |
| 2447 | |
| 2448 | /* the result node? */ |
| 2449 | if (!id[0]) { |
| 2450 | return sibling; |
| 2451 | } |
| 2452 | |
| 2453 | /* move down the tree, if possible */ |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 2454 | if (sibling->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2455 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2456 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2457 | return NULL; |
| 2458 | } |
Michal Vasko | c6c52cf | 2016-03-29 11:53:04 +0200 | [diff] [blame] | 2459 | last_match = sibling; |
Michal Vasko | fc11b68 | 2016-11-18 09:52:41 +0100 | [diff] [blame] | 2460 | prev_mod = lyd_node_module(sibling); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2461 | start = sibling->child; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2462 | break; |
| 2463 | } |
| 2464 | } |
| 2465 | |
| 2466 | /* no match, return last match */ |
| 2467 | if (!sibling) { |
| 2468 | return last_match; |
| 2469 | } |
| 2470 | |
| 2471 | 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] | 2472 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2473 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2474 | return NULL; |
| 2475 | } |
| 2476 | id += r; |
| 2477 | last_parsed = r; |
| 2478 | } |
| 2479 | |
| 2480 | /* cannot get here */ |
| 2481 | LOGINT; |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2482 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2483 | return NULL; |
| 2484 | } |
| 2485 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2486 | /** |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2487 | * @brief Resolves length or range intervals. Does not log. |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2488 | * Syntax is assumed to be correct, *ret MUST be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2489 | * |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2490 | * @param[in] str_restr Restriction as a string. |
| 2491 | * @param[in] type Type of the restriction. |
| 2492 | * @param[out] ret Final interval structure that starts with |
| 2493 | * the interval of the initial type, continues with intervals |
| 2494 | * of any superior types derived from the initial one, and |
| 2495 | * finishes with intervals from our \p type. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2496 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2497 | * @return EXIT_SUCCESS on succes, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2498 | */ |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2499 | int |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2500 | 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] | 2501 | { |
| 2502 | /* 0 - unsigned, 1 - signed, 2 - floating point */ |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2503 | int kind; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2504 | int64_t local_smin, local_smax, local_fmin, local_fmax; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2505 | uint64_t local_umin, local_umax; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2506 | uint8_t local_fdig; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2507 | const char *seg_ptr, *ptr; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2508 | 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] | 2509 | |
| 2510 | switch (type->base) { |
| 2511 | case LY_TYPE_BINARY: |
| 2512 | kind = 0; |
| 2513 | local_umin = 0; |
| 2514 | local_umax = 18446744073709551615UL; |
| 2515 | |
| 2516 | if (!str_restr && type->info.binary.length) { |
| 2517 | str_restr = type->info.binary.length->expr; |
| 2518 | } |
| 2519 | break; |
| 2520 | case LY_TYPE_DEC64: |
| 2521 | kind = 2; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2522 | local_fmin = __INT64_C(-9223372036854775807) - __INT64_C(1); |
| 2523 | local_fmax = __INT64_C(9223372036854775807); |
| 2524 | local_fdig = type->info.dec64.dig; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2525 | |
| 2526 | if (!str_restr && type->info.dec64.range) { |
| 2527 | str_restr = type->info.dec64.range->expr; |
| 2528 | } |
| 2529 | break; |
| 2530 | case LY_TYPE_INT8: |
| 2531 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2532 | local_smin = __INT64_C(-128); |
| 2533 | local_smax = __INT64_C(127); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2534 | |
| 2535 | if (!str_restr && type->info.num.range) { |
| 2536 | str_restr = type->info.num.range->expr; |
| 2537 | } |
| 2538 | break; |
| 2539 | case LY_TYPE_INT16: |
| 2540 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2541 | local_smin = __INT64_C(-32768); |
| 2542 | local_smax = __INT64_C(32767); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2543 | |
| 2544 | if (!str_restr && type->info.num.range) { |
| 2545 | str_restr = type->info.num.range->expr; |
| 2546 | } |
| 2547 | break; |
| 2548 | case LY_TYPE_INT32: |
| 2549 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2550 | local_smin = __INT64_C(-2147483648); |
| 2551 | local_smax = __INT64_C(2147483647); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2552 | |
| 2553 | if (!str_restr && type->info.num.range) { |
| 2554 | str_restr = type->info.num.range->expr; |
| 2555 | } |
| 2556 | break; |
| 2557 | case LY_TYPE_INT64: |
| 2558 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2559 | local_smin = __INT64_C(-9223372036854775807) - __INT64_C(1); |
| 2560 | local_smax = __INT64_C(9223372036854775807); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2561 | |
| 2562 | if (!str_restr && type->info.num.range) { |
| 2563 | str_restr = type->info.num.range->expr; |
| 2564 | } |
| 2565 | break; |
| 2566 | case LY_TYPE_UINT8: |
| 2567 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2568 | local_umin = __UINT64_C(0); |
| 2569 | local_umax = __UINT64_C(255); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2570 | |
| 2571 | if (!str_restr && type->info.num.range) { |
| 2572 | str_restr = type->info.num.range->expr; |
| 2573 | } |
| 2574 | break; |
| 2575 | case LY_TYPE_UINT16: |
| 2576 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2577 | local_umin = __UINT64_C(0); |
| 2578 | local_umax = __UINT64_C(65535); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2579 | |
| 2580 | if (!str_restr && type->info.num.range) { |
| 2581 | str_restr = type->info.num.range->expr; |
| 2582 | } |
| 2583 | break; |
| 2584 | case LY_TYPE_UINT32: |
| 2585 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2586 | local_umin = __UINT64_C(0); |
| 2587 | local_umax = __UINT64_C(4294967295); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2588 | |
| 2589 | if (!str_restr && type->info.num.range) { |
| 2590 | str_restr = type->info.num.range->expr; |
| 2591 | } |
| 2592 | break; |
| 2593 | case LY_TYPE_UINT64: |
| 2594 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2595 | local_umin = __UINT64_C(0); |
| 2596 | local_umax = __UINT64_C(18446744073709551615); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2597 | |
| 2598 | if (!str_restr && type->info.num.range) { |
| 2599 | str_restr = type->info.num.range->expr; |
| 2600 | } |
| 2601 | break; |
| 2602 | case LY_TYPE_STRING: |
| 2603 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2604 | local_umin = __UINT64_C(0); |
| 2605 | local_umax = __UINT64_C(18446744073709551615); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2606 | |
| 2607 | if (!str_restr && type->info.str.length) { |
| 2608 | str_restr = type->info.str.length->expr; |
| 2609 | } |
| 2610 | break; |
| 2611 | default: |
| 2612 | LOGINT; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2613 | return -1; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2614 | } |
| 2615 | |
| 2616 | /* process superior types */ |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2617 | if (type->der) { |
| 2618 | if (resolve_len_ran_interval(NULL, &type->der->type, &intv)) { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2619 | LOGINT; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2620 | return -1; |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2621 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2622 | assert(!intv || (intv->kind == kind)); |
| 2623 | } |
| 2624 | |
| 2625 | if (!str_restr) { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2626 | /* we do not have any restriction, return superior ones */ |
| 2627 | *ret = intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2628 | return EXIT_SUCCESS; |
| 2629 | } |
| 2630 | |
| 2631 | /* adjust local min and max */ |
| 2632 | if (intv) { |
| 2633 | tmp_intv = intv; |
| 2634 | |
| 2635 | if (kind == 0) { |
| 2636 | local_umin = tmp_intv->value.uval.min; |
| 2637 | } else if (kind == 1) { |
| 2638 | local_smin = tmp_intv->value.sval.min; |
| 2639 | } else if (kind == 2) { |
| 2640 | local_fmin = tmp_intv->value.fval.min; |
| 2641 | } |
| 2642 | |
| 2643 | while (tmp_intv->next) { |
| 2644 | tmp_intv = tmp_intv->next; |
| 2645 | } |
| 2646 | |
| 2647 | if (kind == 0) { |
| 2648 | local_umax = tmp_intv->value.uval.max; |
| 2649 | } else if (kind == 1) { |
| 2650 | local_smax = tmp_intv->value.sval.max; |
| 2651 | } else if (kind == 2) { |
| 2652 | local_fmax = tmp_intv->value.fval.max; |
| 2653 | } |
| 2654 | } |
| 2655 | |
| 2656 | /* finally parse our restriction */ |
| 2657 | seg_ptr = str_restr; |
Pavol Vican | 9e7c01d | 2016-08-29 09:36:17 +0200 | [diff] [blame] | 2658 | tmp_intv = NULL; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2659 | while (1) { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2660 | if (!tmp_local_intv) { |
| 2661 | assert(!local_intv); |
| 2662 | local_intv = malloc(sizeof *local_intv); |
| 2663 | tmp_local_intv = local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2664 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2665 | tmp_local_intv->next = malloc(sizeof *tmp_local_intv); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2666 | tmp_local_intv = tmp_local_intv->next; |
| 2667 | } |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2668 | if (!tmp_local_intv) { |
| 2669 | LOGMEM; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2670 | goto error; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2671 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2672 | |
| 2673 | tmp_local_intv->kind = kind; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2674 | tmp_local_intv->type = type; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2675 | tmp_local_intv->next = NULL; |
| 2676 | |
| 2677 | /* min */ |
| 2678 | ptr = seg_ptr; |
| 2679 | while (isspace(ptr[0])) { |
| 2680 | ++ptr; |
| 2681 | } |
| 2682 | if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) { |
| 2683 | if (kind == 0) { |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2684 | tmp_local_intv->value.uval.min = strtol(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2685 | } else if (kind == 1) { |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2686 | tmp_local_intv->value.sval.min = strtol(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2687 | } else if (kind == 2) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 2688 | if (parse_range_dec64(&ptr, local_fdig, &tmp_local_intv->value.fval.min)) { |
| 2689 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, ptr, "range"); |
| 2690 | goto error; |
| 2691 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2692 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2693 | } else if (!strncmp(ptr, "min", 3)) { |
| 2694 | if (kind == 0) { |
| 2695 | tmp_local_intv->value.uval.min = local_umin; |
| 2696 | } else if (kind == 1) { |
| 2697 | tmp_local_intv->value.sval.min = local_smin; |
| 2698 | } else if (kind == 2) { |
| 2699 | tmp_local_intv->value.fval.min = local_fmin; |
| 2700 | } |
| 2701 | |
| 2702 | ptr += 3; |
| 2703 | } else if (!strncmp(ptr, "max", 3)) { |
| 2704 | if (kind == 0) { |
| 2705 | tmp_local_intv->value.uval.min = local_umax; |
| 2706 | } else if (kind == 1) { |
| 2707 | tmp_local_intv->value.sval.min = local_smax; |
| 2708 | } else if (kind == 2) { |
| 2709 | tmp_local_intv->value.fval.min = local_fmax; |
| 2710 | } |
| 2711 | |
| 2712 | ptr += 3; |
| 2713 | } else { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2714 | LOGINT; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2715 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2716 | } |
| 2717 | |
| 2718 | while (isspace(ptr[0])) { |
| 2719 | ptr++; |
| 2720 | } |
| 2721 | |
| 2722 | /* no interval or interval */ |
| 2723 | if ((ptr[0] == '|') || !ptr[0]) { |
| 2724 | if (kind == 0) { |
| 2725 | tmp_local_intv->value.uval.max = tmp_local_intv->value.uval.min; |
| 2726 | } else if (kind == 1) { |
| 2727 | tmp_local_intv->value.sval.max = tmp_local_intv->value.sval.min; |
| 2728 | } else if (kind == 2) { |
| 2729 | tmp_local_intv->value.fval.max = tmp_local_intv->value.fval.min; |
| 2730 | } |
| 2731 | } else if (!strncmp(ptr, "..", 2)) { |
| 2732 | /* skip ".." */ |
| 2733 | ptr += 2; |
| 2734 | while (isspace(ptr[0])) { |
| 2735 | ++ptr; |
| 2736 | } |
| 2737 | |
| 2738 | /* max */ |
| 2739 | if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) { |
| 2740 | if (kind == 0) { |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2741 | tmp_local_intv->value.uval.max = strtol(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2742 | } else if (kind == 1) { |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2743 | tmp_local_intv->value.sval.max = strtol(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2744 | } else if (kind == 2) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 2745 | if (parse_range_dec64(&ptr, local_fdig, &tmp_local_intv->value.fval.max)) { |
| 2746 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, ptr, "range"); |
| 2747 | goto error; |
| 2748 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2749 | } |
| 2750 | } else if (!strncmp(ptr, "max", 3)) { |
| 2751 | if (kind == 0) { |
| 2752 | tmp_local_intv->value.uval.max = local_umax; |
| 2753 | } else if (kind == 1) { |
| 2754 | tmp_local_intv->value.sval.max = local_smax; |
| 2755 | } else if (kind == 2) { |
| 2756 | tmp_local_intv->value.fval.max = local_fmax; |
| 2757 | } |
| 2758 | } else { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2759 | LOGINT; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2760 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2761 | } |
| 2762 | } else { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2763 | LOGINT; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2764 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2765 | } |
| 2766 | |
Pavol Vican | 9e7c01d | 2016-08-29 09:36:17 +0200 | [diff] [blame] | 2767 | /* check min and max in correct order*/ |
| 2768 | if (kind == 0) { |
| 2769 | /* current segment */ |
| 2770 | if (tmp_local_intv->value.uval.min > tmp_local_intv->value.uval.max) { |
| 2771 | goto error; |
| 2772 | } |
| 2773 | if (tmp_local_intv->value.uval.min < local_umin || tmp_local_intv->value.uval.max > local_umax) { |
| 2774 | goto error; |
| 2775 | } |
| 2776 | /* segments sholud be ascending order */ |
Pavol Vican | 69f62c9 | 2016-08-30 09:06:25 +0200 | [diff] [blame] | 2777 | 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] | 2778 | goto error; |
| 2779 | } |
| 2780 | } else if (kind == 1) { |
| 2781 | if (tmp_local_intv->value.sval.min > tmp_local_intv->value.sval.max) { |
| 2782 | goto error; |
| 2783 | } |
| 2784 | if (tmp_local_intv->value.sval.min < local_smin || tmp_local_intv->value.sval.max > local_smax) { |
| 2785 | goto error; |
| 2786 | } |
Pavol Vican | 69f62c9 | 2016-08-30 09:06:25 +0200 | [diff] [blame] | 2787 | 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] | 2788 | goto error; |
| 2789 | } |
| 2790 | } else if (kind == 2) { |
| 2791 | if (tmp_local_intv->value.fval.min > tmp_local_intv->value.fval.max) { |
| 2792 | goto error; |
| 2793 | } |
| 2794 | if (tmp_local_intv->value.fval.min < local_fmin || tmp_local_intv->value.fval.max > local_fmax) { |
| 2795 | goto error; |
| 2796 | } |
Pavol Vican | 69f62c9 | 2016-08-30 09:06:25 +0200 | [diff] [blame] | 2797 | 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] | 2798 | /* 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] | 2799 | goto error; |
| 2800 | } |
| 2801 | } |
| 2802 | |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2803 | /* next segment (next OR) */ |
| 2804 | seg_ptr = strchr(seg_ptr, '|'); |
| 2805 | if (!seg_ptr) { |
| 2806 | break; |
| 2807 | } |
| 2808 | seg_ptr++; |
Pavol Vican | 9e7c01d | 2016-08-29 09:36:17 +0200 | [diff] [blame] | 2809 | tmp_intv = tmp_local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2810 | } |
| 2811 | |
| 2812 | /* check local restrictions against superior ones */ |
| 2813 | if (intv) { |
| 2814 | tmp_intv = intv; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2815 | tmp_local_intv = local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2816 | |
| 2817 | while (tmp_local_intv && tmp_intv) { |
| 2818 | /* reuse local variables */ |
| 2819 | if (kind == 0) { |
| 2820 | local_umin = tmp_local_intv->value.uval.min; |
| 2821 | local_umax = tmp_local_intv->value.uval.max; |
| 2822 | |
| 2823 | /* it must be in this interval */ |
| 2824 | if ((local_umin >= tmp_intv->value.uval.min) && (local_umin <= tmp_intv->value.uval.max)) { |
| 2825 | /* this interval is covered, next one */ |
| 2826 | if (local_umax <= tmp_intv->value.uval.max) { |
| 2827 | tmp_local_intv = tmp_local_intv->next; |
| 2828 | continue; |
| 2829 | /* ascending order of restrictions -> fail */ |
| 2830 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2831 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2832 | } |
| 2833 | } |
| 2834 | } else if (kind == 1) { |
| 2835 | local_smin = tmp_local_intv->value.sval.min; |
| 2836 | local_smax = tmp_local_intv->value.sval.max; |
| 2837 | |
| 2838 | if ((local_smin >= tmp_intv->value.sval.min) && (local_smin <= tmp_intv->value.sval.max)) { |
| 2839 | if (local_smax <= tmp_intv->value.sval.max) { |
| 2840 | tmp_local_intv = tmp_local_intv->next; |
| 2841 | continue; |
| 2842 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2843 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2844 | } |
| 2845 | } |
| 2846 | } else if (kind == 2) { |
| 2847 | local_fmin = tmp_local_intv->value.fval.min; |
| 2848 | local_fmax = tmp_local_intv->value.fval.max; |
| 2849 | |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2850 | 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] | 2851 | && (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] | 2852 | 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] | 2853 | tmp_local_intv = tmp_local_intv->next; |
| 2854 | continue; |
| 2855 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2856 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2857 | } |
| 2858 | } |
| 2859 | } |
| 2860 | |
| 2861 | tmp_intv = tmp_intv->next; |
| 2862 | } |
| 2863 | |
| 2864 | /* some interval left uncovered -> fail */ |
| 2865 | if (tmp_local_intv) { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2866 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2867 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2868 | } |
| 2869 | |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2870 | /* append the local intervals to all the intervals of the superior types, return it all */ |
| 2871 | if (intv) { |
| 2872 | for (tmp_intv = intv; tmp_intv->next; tmp_intv = tmp_intv->next); |
| 2873 | tmp_intv->next = local_intv; |
| 2874 | } else { |
| 2875 | intv = local_intv; |
| 2876 | } |
| 2877 | *ret = intv; |
| 2878 | |
| 2879 | return EXIT_SUCCESS; |
| 2880 | |
| 2881 | error: |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2882 | while (intv) { |
| 2883 | tmp_intv = intv->next; |
| 2884 | free(intv); |
| 2885 | intv = tmp_intv; |
| 2886 | } |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2887 | while (local_intv) { |
| 2888 | tmp_local_intv = local_intv->next; |
| 2889 | free(local_intv); |
| 2890 | local_intv = tmp_local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2891 | } |
| 2892 | |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2893 | return -1; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2894 | } |
| 2895 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2896 | /** |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2897 | * @brief Resolve a typedef, return only resolved typedefs if derived. If leafref, it must be |
| 2898 | * resolved for this function to return it. Does not log. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2899 | * |
| 2900 | * @param[in] name Typedef name. |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2901 | * @param[in] mod_name Typedef name module name. |
| 2902 | * @param[in] module Main module. |
| 2903 | * @param[in] parent Parent of the resolved type definition. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2904 | * @param[out] ret Pointer to the resolved typedef. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2905 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2906 | * @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] | 2907 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2908 | int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 2909 | resolve_superior_type(const char *name, const char *mod_name, const struct lys_module *module, |
| 2910 | const struct lys_node *parent, struct lys_tpdf **ret) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2911 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2912 | int i, j; |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2913 | struct lys_tpdf *tpdf, *match; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2914 | int tpdf_size; |
| 2915 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2916 | if (!mod_name) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2917 | /* no prefix, try built-in types */ |
| 2918 | for (i = 1; i < LY_DATA_TYPE_COUNT; i++) { |
| 2919 | if (!strcmp(ly_types[i].def->name, name)) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2920 | if (ret) { |
| 2921 | *ret = ly_types[i].def; |
| 2922 | } |
| 2923 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2924 | } |
| 2925 | } |
| 2926 | } else { |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2927 | if (!strcmp(mod_name, module->name)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2928 | /* prefix refers to the current module, ignore it */ |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2929 | mod_name = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2930 | } |
| 2931 | } |
| 2932 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2933 | if (!mod_name && parent) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2934 | /* search in local typedefs */ |
| 2935 | while (parent) { |
| 2936 | switch (parent->nodetype) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2937 | case LYS_CONTAINER: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 2938 | tpdf_size = ((struct lys_node_container *)parent)->tpdf_size; |
| 2939 | tpdf = ((struct lys_node_container *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2940 | break; |
| 2941 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2942 | case LYS_LIST: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 2943 | tpdf_size = ((struct lys_node_list *)parent)->tpdf_size; |
| 2944 | tpdf = ((struct lys_node_list *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2945 | break; |
| 2946 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2947 | case LYS_GROUPING: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 2948 | tpdf_size = ((struct lys_node_grp *)parent)->tpdf_size; |
| 2949 | tpdf = ((struct lys_node_grp *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2950 | break; |
| 2951 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2952 | case LYS_RPC: |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 2953 | case LYS_ACTION: |
| 2954 | tpdf_size = ((struct lys_node_rpc_action *)parent)->tpdf_size; |
| 2955 | tpdf = ((struct lys_node_rpc_action *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2956 | break; |
| 2957 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2958 | case LYS_NOTIF: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 2959 | tpdf_size = ((struct lys_node_notif *)parent)->tpdf_size; |
| 2960 | tpdf = ((struct lys_node_notif *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2961 | break; |
| 2962 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2963 | case LYS_INPUT: |
| 2964 | case LYS_OUTPUT: |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 2965 | tpdf_size = ((struct lys_node_inout *)parent)->tpdf_size; |
| 2966 | tpdf = ((struct lys_node_inout *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2967 | break; |
| 2968 | |
| 2969 | default: |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 2970 | parent = lys_parent(parent); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2971 | continue; |
| 2972 | } |
| 2973 | |
| 2974 | for (i = 0; i < tpdf_size; i++) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 2975 | if (!strcmp(tpdf[i].name, name) && tpdf[i].type.base > 0) { |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2976 | match = &tpdf[i]; |
| 2977 | goto check_leafref; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2978 | } |
| 2979 | } |
| 2980 | |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 2981 | parent = lys_parent(parent); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2982 | } |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2983 | } else { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2984 | /* get module where to search */ |
Michal Vasko | b6729c6 | 2015-10-21 12:09:47 +0200 | [diff] [blame] | 2985 | module = lys_get_import_module(module, NULL, 0, mod_name, 0); |
Michal Vasko | c935fff | 2015-08-17 14:02:06 +0200 | [diff] [blame] | 2986 | if (!module) { |
| 2987 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2988 | } |
| 2989 | } |
| 2990 | |
| 2991 | /* search in top level typedefs */ |
| 2992 | for (i = 0; i < module->tpdf_size; i++) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 2993 | 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] | 2994 | match = &module->tpdf[i]; |
| 2995 | goto check_leafref; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2996 | } |
| 2997 | } |
| 2998 | |
| 2999 | /* search in submodules */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 3000 | for (i = 0; i < module->inc_size && module->inc[i].submodule; i++) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3001 | for (j = 0; j < module->inc[i].submodule->tpdf_size; j++) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 3002 | 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] | 3003 | match = &module->inc[i].submodule->tpdf[j]; |
| 3004 | goto check_leafref; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3005 | } |
| 3006 | } |
| 3007 | } |
| 3008 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3009 | return EXIT_FAILURE; |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 3010 | |
| 3011 | check_leafref: |
| 3012 | if (ret) { |
| 3013 | *ret = match; |
| 3014 | } |
| 3015 | if (match->type.base == LY_TYPE_LEAFREF) { |
| 3016 | while (!match->type.info.lref.path) { |
| 3017 | match = match->type.der; |
| 3018 | assert(match); |
| 3019 | } |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 3020 | } |
| 3021 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3022 | } |
| 3023 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3024 | /** |
| 3025 | * @brief Check the default \p value of the \p type. Logs directly. |
| 3026 | * |
| 3027 | * @param[in] type Type definition to use. |
| 3028 | * @param[in] value Default value to check. |
Michal Vasko | 5682640 | 2016-03-02 11:11:37 +0100 | [diff] [blame] | 3029 | * @param[in] module Type module. |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3030 | * |
| 3031 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 3032 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3033 | static int |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3034 | 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] | 3035 | { |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3036 | struct lys_tpdf *base_tpdf = NULL; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3037 | struct lyd_node_leaf_list node; |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3038 | const char *dflt = NULL; |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 3039 | int ret = EXIT_SUCCESS; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3040 | |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3041 | assert(value); |
| 3042 | |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 3043 | if (type->base <= LY_TYPE_DER) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3044 | /* the type was not resolved yet, nothing to do for now */ |
| 3045 | return EXIT_FAILURE; |
| 3046 | } |
| 3047 | |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3048 | dflt = *value; |
| 3049 | if (!dflt) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3050 | /* we do not have a new default value, so is there any to check even, in some base type? */ |
| 3051 | for (base_tpdf = type->der; base_tpdf->type.der; base_tpdf = base_tpdf->type.der) { |
| 3052 | if (base_tpdf->dflt) { |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3053 | dflt = base_tpdf->dflt; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3054 | break; |
| 3055 | } |
| 3056 | } |
| 3057 | |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3058 | if (!dflt) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3059 | /* no default value, nothing to check, all is well */ |
| 3060 | return EXIT_SUCCESS; |
| 3061 | } |
| 3062 | |
| 3063 | /* 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)? */ |
| 3064 | switch (type->base) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3065 | case LY_TYPE_IDENT: |
| 3066 | case LY_TYPE_INST: |
| 3067 | case LY_TYPE_LEAFREF: |
| 3068 | case LY_TYPE_BOOL: |
| 3069 | case LY_TYPE_EMPTY: |
| 3070 | /* these have no restrictions, so we would do the exact same work as the unres in the base typedef */ |
| 3071 | return EXIT_SUCCESS; |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3072 | case LY_TYPE_BITS: |
| 3073 | /* the default value must match the restricted list of values, if the type was restricted */ |
| 3074 | if (type->info.bits.count) { |
| 3075 | break; |
| 3076 | } |
| 3077 | return EXIT_SUCCESS; |
| 3078 | case LY_TYPE_ENUM: |
| 3079 | /* the default value must match the restricted list of values, if the type was restricted */ |
| 3080 | if (type->info.enums.count) { |
| 3081 | break; |
| 3082 | } |
| 3083 | return EXIT_SUCCESS; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3084 | case LY_TYPE_DEC64: |
| 3085 | if (type->info.dec64.range) { |
| 3086 | break; |
| 3087 | } |
| 3088 | return EXIT_SUCCESS; |
| 3089 | case LY_TYPE_BINARY: |
| 3090 | if (type->info.binary.length) { |
| 3091 | break; |
| 3092 | } |
| 3093 | return EXIT_SUCCESS; |
| 3094 | case LY_TYPE_INT8: |
| 3095 | case LY_TYPE_INT16: |
| 3096 | case LY_TYPE_INT32: |
| 3097 | case LY_TYPE_INT64: |
| 3098 | case LY_TYPE_UINT8: |
| 3099 | case LY_TYPE_UINT16: |
| 3100 | case LY_TYPE_UINT32: |
| 3101 | case LY_TYPE_UINT64: |
| 3102 | if (type->info.num.range) { |
| 3103 | break; |
| 3104 | } |
| 3105 | return EXIT_SUCCESS; |
| 3106 | case LY_TYPE_STRING: |
| 3107 | if (type->info.str.length || type->info.str.patterns) { |
| 3108 | break; |
| 3109 | } |
| 3110 | return EXIT_SUCCESS; |
| 3111 | case LY_TYPE_UNION: |
| 3112 | /* way too much trouble learning whether we need to check the default again, so just do it */ |
| 3113 | break; |
| 3114 | default: |
| 3115 | LOGINT; |
| 3116 | return -1; |
| 3117 | } |
Radek Krejci | 55a161c | 2016-09-05 17:13:25 +0200 | [diff] [blame] | 3118 | } else if (type->base == LY_TYPE_EMPTY) { |
| 3119 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "default", type->parent->name); |
| 3120 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The \"empty\" data type cannot have a default value."); |
| 3121 | return -1; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3122 | } |
| 3123 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3124 | /* dummy leaf */ |
Radek Krejci | 4fe36bd | 2016-03-17 16:47:16 +0100 | [diff] [blame] | 3125 | memset(&node, 0, sizeof node); |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3126 | node.value_str = dflt; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3127 | node.value_type = type->base; |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 3128 | node.schema = calloc(1, sizeof (struct lys_node_leaf)); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3129 | if (!node.schema) { |
| 3130 | LOGMEM; |
| 3131 | return -1; |
| 3132 | } |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3133 | node.schema->name = strdup("fake-default"); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3134 | if (!node.schema->name) { |
| 3135 | LOGMEM; |
Michal Vasko | f49eda0 | 2016-07-21 12:17:01 +0200 | [diff] [blame] | 3136 | free(node.schema); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3137 | return -1; |
| 3138 | } |
Michal Vasko | 5682640 | 2016-03-02 11:11:37 +0100 | [diff] [blame] | 3139 | node.schema->module = module; |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 3140 | memcpy(&((struct lys_node_leaf *)node.schema)->type, type, sizeof *type); |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3141 | |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 3142 | if (type->base == LY_TYPE_LEAFREF) { |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3143 | if (!type->info.lref.target) { |
| 3144 | ret = EXIT_FAILURE; |
| 3145 | goto finish; |
| 3146 | } |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3147 | ret = check_default(&type->info.lref.target->type, &dflt, module); |
| 3148 | if (!ret) { |
| 3149 | /* adopt possibly changed default value to its canonical form */ |
| 3150 | if (*value) { |
| 3151 | *value = dflt; |
| 3152 | } |
| 3153 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3154 | } else { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 3155 | if (!lyp_parse_value(&((struct lys_node_leaf *)node.schema)->type, &node.value_str, NULL, &node, 1, 1)) { |
Radek Krejci | 5dca593 | 2016-11-04 14:30:47 +0100 | [diff] [blame] | 3156 | /* possible forward reference */ |
| 3157 | ret = 1; |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3158 | if (base_tpdf) { |
Radek Krejci | 9ad23f4 | 2016-10-31 15:46:52 +0100 | [diff] [blame] | 3159 | /* default value is defined in some base typedef */ |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3160 | if ((type->base == LY_TYPE_BITS && type->der->type.der) || |
| 3161 | (type->base == LY_TYPE_ENUM && type->der->type.der)) { |
| 3162 | /* we have refined bits/enums */ |
| 3163 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, |
| 3164 | "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] | 3165 | dflt, type->parent->name, base_tpdf->name); |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3166 | } |
| 3167 | } |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3168 | } else { |
| 3169 | /* success - adopt canonical form from the node into the default value */ |
| 3170 | if (dflt != node.value_str) { |
| 3171 | /* this can happen only if we have non-inherited default value, |
| 3172 | * inherited default values are already in canonical form */ |
| 3173 | assert(dflt == *value); |
| 3174 | *value = node.value_str; |
| 3175 | } |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 3176 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3177 | } |
| 3178 | |
| 3179 | finish: |
| 3180 | if (node.value_type == LY_TYPE_BITS) { |
| 3181 | free(node.value.bit); |
| 3182 | } |
| 3183 | free((char *)node.schema->name); |
| 3184 | free(node.schema); |
| 3185 | |
| 3186 | return ret; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3187 | } |
| 3188 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3189 | /** |
| 3190 | * @brief Check a key for mandatory attributes. Logs directly. |
| 3191 | * |
| 3192 | * @param[in] key The key to check. |
| 3193 | * @param[in] flags What flags to check. |
| 3194 | * @param[in] list The list of all the keys. |
| 3195 | * @param[in] index Index of the key in the key list. |
| 3196 | * @param[in] name The name of the keys. |
| 3197 | * @param[in] len The name length. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3198 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3199 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3200 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3201 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3202 | 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] | 3203 | { |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3204 | struct lys_node_leaf *key = list->keys[index]; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3205 | char *dup = NULL; |
| 3206 | int j; |
| 3207 | |
| 3208 | /* existence */ |
| 3209 | if (!key) { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3210 | if (name[len] != '\0') { |
| 3211 | dup = strdup(name); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3212 | if (!dup) { |
| 3213 | LOGMEM; |
| 3214 | return -1; |
| 3215 | } |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3216 | dup[len] = '\0'; |
| 3217 | name = dup; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3218 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3219 | LOGVAL(LYE_KEY_MISS, LY_VLOG_LYS, list, name); |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 3220 | free(dup); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3221 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3222 | } |
| 3223 | |
| 3224 | /* uniqueness */ |
| 3225 | for (j = index - 1; j >= 0; j--) { |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3226 | if (key == list->keys[j]) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3227 | LOGVAL(LYE_KEY_DUP, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3228 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3229 | } |
| 3230 | } |
| 3231 | |
| 3232 | /* key is a leaf */ |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3233 | if (key->nodetype != LYS_LEAF) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3234 | LOGVAL(LYE_KEY_NLEAF, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3235 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3236 | } |
| 3237 | |
| 3238 | /* type of the leaf is not built-in empty */ |
Radek Krejci | c3738db | 2016-09-15 15:51:21 +0200 | [diff] [blame] | 3239 | if (key->type.base == LY_TYPE_EMPTY && key->module->version < 2) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3240 | LOGVAL(LYE_KEY_TYPE, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3241 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3242 | } |
| 3243 | |
| 3244 | /* config attribute is the same as of the list */ |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 3245 | 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] | 3246 | LOGVAL(LYE_KEY_CONFIG, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3247 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3248 | } |
| 3249 | |
Radek Krejci | 55e2cdc | 2016-03-11 13:51:09 +0100 | [diff] [blame] | 3250 | /* key is not placed from augment */ |
| 3251 | if (key->parent->nodetype == LYS_AUGMENT) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3252 | LOGVAL(LYE_KEY_MISS, LY_VLOG_LYS, key, key->name); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 3253 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Key inserted from augment."); |
Radek Krejci | 55e2cdc | 2016-03-11 13:51:09 +0100 | [diff] [blame] | 3254 | return -1; |
| 3255 | } |
| 3256 | |
Radek Krejci | 3f21ada | 2016-08-01 13:34:31 +0200 | [diff] [blame] | 3257 | /* key is not when/if-feature -conditional */ |
| 3258 | j = 0; |
| 3259 | if (key->when || (key->iffeature_size && (j = 1))) { |
| 3260 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, key, j ? "if-feature" : "when", "leaf"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 3261 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Key definition cannot depend on a \"%s\" condition.", |
Radek Krejci | 3f21ada | 2016-08-01 13:34:31 +0200 | [diff] [blame] | 3262 | j ? "if-feature" : "when"); |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3263 | return -1; |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3264 | } |
| 3265 | |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3266 | return EXIT_SUCCESS; |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3267 | } |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3268 | |
| 3269 | /** |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3270 | * @brief Resolve (test the target exists) unique. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3271 | * |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3272 | * @param[in] parent The parent node of the unique structure. |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3273 | * @param[in] uniq_str_path One path from the unique string. |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3274 | * |
| 3275 | * @return EXIT_SUCCESS on succes, EXIT_FAILURE on forward reference, -1 on error. |
| 3276 | */ |
| 3277 | int |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3278 | 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] | 3279 | { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3280 | int rc; |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3281 | const struct lys_node *leaf = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3282 | |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 3283 | 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] | 3284 | if (rc || !leaf) { |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3285 | if (rc) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3286 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3287 | if (rc > 0) { |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 3288 | LOGVAL(LYE_INCHAR, LY_VLOG_PREV, NULL, uniq_str_path[rc - 1], &uniq_str_path[rc - 1]); |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 3289 | } else if (rc == -2) { |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 3290 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Unique argument references list."); |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3291 | } |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3292 | rc = -1; |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3293 | } else { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3294 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 3295 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Target leaf not found."); |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3296 | rc = EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3297 | } |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3298 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3299 | } |
Michal Vasko | 9bb061b | 2016-02-12 11:00:19 +0100 | [diff] [blame] | 3300 | if (leaf->nodetype != LYS_LEAF) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3301 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 3302 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Target is not a leaf."); |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3303 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3304 | } |
| 3305 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3306 | /* check status */ |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3307 | 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] | 3308 | return -1; |
| 3309 | } |
| 3310 | |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3311 | /* check that all unique's targets are of the same config type */ |
| 3312 | if (*trg_type) { |
| 3313 | if (((*trg_type == 1) && (leaf->flags & LYS_CONFIG_R)) || ((*trg_type == 2) && (leaf->flags & LYS_CONFIG_W))) { |
| 3314 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 3315 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3316 | "Leaf \"%s\" referenced in unique statement is config %s, but previous referenced leaf is config %s.", |
| 3317 | uniq_str_path, *trg_type == 1 ? "false" : "true", *trg_type == 1 ? "true" : "false"); |
| 3318 | return -1; |
| 3319 | } |
| 3320 | } else { |
| 3321 | /* first unique */ |
| 3322 | if (leaf->flags & LYS_CONFIG_W) { |
| 3323 | *trg_type = 1; |
| 3324 | } else { |
| 3325 | *trg_type = 2; |
| 3326 | } |
| 3327 | } |
| 3328 | |
Radek Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame] | 3329 | /* set leaf's unique flag */ |
| 3330 | ((struct lys_node_leaf *)leaf)->flags |= LYS_UNIQUE; |
| 3331 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3332 | return EXIT_SUCCESS; |
| 3333 | |
| 3334 | error: |
| 3335 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3336 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3337 | } |
| 3338 | |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 3339 | void |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3340 | unres_data_del(struct unres_data *unres, uint32_t i) |
| 3341 | { |
| 3342 | /* there are items after the one deleted */ |
| 3343 | if (i+1 < unres->count) { |
| 3344 | /* we only move the data, memory is left allocated, why bother */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3345 | 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] | 3346 | |
| 3347 | /* deleting the last item */ |
| 3348 | } else if (i == 0) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3349 | free(unres->node); |
| 3350 | unres->node = NULL; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3351 | } |
| 3352 | |
| 3353 | /* if there are no items after and it is not the last one, just move the counter */ |
| 3354 | --unres->count; |
| 3355 | } |
| 3356 | |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3357 | /** |
| 3358 | * @brief Resolve (find) a data node from a specific module. Does not log. |
| 3359 | * |
| 3360 | * @param[in] mod Module to search in. |
| 3361 | * @param[in] name Name of the data node. |
| 3362 | * @param[in] nam_len Length of the name. |
| 3363 | * @param[in] start Data node to start the search from. |
| 3364 | * @param[in,out] parents Resolved nodes. If there are some parents, |
| 3365 | * they are replaced (!!) with the resolvents. |
| 3366 | * |
Michal Vasko | 2471e7f | 2016-04-11 11:00:15 +0200 | [diff] [blame] | 3367 | * @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] | 3368 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3369 | static int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3370 | 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] | 3371 | { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3372 | struct lyd_node *node; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3373 | int flag; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3374 | uint32_t i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3375 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3376 | if (!parents->count) { |
| 3377 | parents->count = 1; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3378 | parents->node = malloc(sizeof *parents->node); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3379 | if (!parents->node) { |
| 3380 | LOGMEM; |
Michal Vasko | 2471e7f | 2016-04-11 11:00:15 +0200 | [diff] [blame] | 3381 | return -1; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3382 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3383 | parents->node[0] = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3384 | } |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3385 | for (i = 0; i < parents->count;) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 3386 | 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] | 3387 | /* skip */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3388 | ++i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3389 | continue; |
| 3390 | } |
| 3391 | flag = 0; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3392 | LY_TREE_FOR(parents->node[i] ? parents->node[i]->child : start, node) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3393 | if (node->schema->module == mod && !strncmp(node->schema->name, name, nam_len) |
| 3394 | && node->schema->name[nam_len] == '\0') { |
| 3395 | /* matching target */ |
| 3396 | if (!flag) { |
Michal Vasko | 9a47e12 | 2015-09-03 14:26:32 +0200 | [diff] [blame] | 3397 | /* put node instead of the current parent */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3398 | parents->node[i] = node; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3399 | flag = 1; |
| 3400 | } else { |
Michal Vasko | 9a47e12 | 2015-09-03 14:26:32 +0200 | [diff] [blame] | 3401 | /* multiple matching, so create a new node */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3402 | ++parents->count; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3403 | parents->node = ly_realloc(parents->node, parents->count * sizeof *parents->node); |
| 3404 | if (!parents->node) { |
| 3405 | return EXIT_FAILURE; |
| 3406 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3407 | parents->node[parents->count-1] = node; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3408 | ++i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3409 | } |
| 3410 | } |
| 3411 | } |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3412 | |
| 3413 | if (!flag) { |
| 3414 | /* remove item from the parents list */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3415 | unres_data_del(parents, i); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3416 | } else { |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3417 | ++i; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3418 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3419 | } |
| 3420 | |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3421 | return parents->count ? EXIT_SUCCESS : EXIT_FAILURE; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3422 | } |
| 3423 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3424 | /** |
| 3425 | * @brief Resolve (find) a data node. Does not log. |
| 3426 | * |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3427 | * @param[in] mod_name Module name of the data node. |
| 3428 | * @param[in] mod_name_len Length of the module name. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3429 | * @param[in] name Name of the data node. |
| 3430 | * @param[in] nam_len Length of the name. |
| 3431 | * @param[in] start Data node to start the search from. |
| 3432 | * @param[in,out] parents Resolved nodes. If there are some parents, |
| 3433 | * they are replaced (!!) with the resolvents. |
| 3434 | * |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3435 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 otherwise. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3436 | */ |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3437 | static int |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3438 | 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] | 3439 | struct unres_data *parents) |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3440 | { |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3441 | const struct lys_module *mod; |
Michal Vasko | 31fc367 | 2015-10-21 12:08:13 +0200 | [diff] [blame] | 3442 | char *str; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3443 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3444 | assert(start); |
| 3445 | |
Michal Vasko | 31fc367 | 2015-10-21 12:08:13 +0200 | [diff] [blame] | 3446 | if (mod_name) { |
| 3447 | /* we have mod_name, find appropriate module */ |
| 3448 | str = strndup(mod_name, mod_name_len); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3449 | if (!str) { |
| 3450 | LOGMEM; |
| 3451 | return -1; |
| 3452 | } |
Michal Vasko | 31fc367 | 2015-10-21 12:08:13 +0200 | [diff] [blame] | 3453 | mod = ly_ctx_get_module(start->schema->module->ctx, str, NULL); |
| 3454 | free(str); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3455 | if (!mod) { |
| 3456 | /* invalid prefix */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3457 | return -1; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3458 | } |
| 3459 | } else { |
| 3460 | /* no prefix, module is the same as of current node */ |
| 3461 | mod = start->schema->module; |
| 3462 | } |
| 3463 | |
| 3464 | return resolve_data(mod, name, name_len, start, parents); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3465 | } |
| 3466 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3467 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3468 | * @brief Resolve a path predicate (leafref) in JSON data context. Logs directly |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3469 | * only specific errors, general no-resolvent error is left to the caller. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3470 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3471 | * @param[in] pred Predicate to use. |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3472 | * @param[in] node Node from which the predicate is being resolved |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3473 | * @param[in,out] node_match Nodes satisfying the restriction |
| 3474 | * without the predicate. Nodes not |
| 3475 | * satisfying the predicate are removed. |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3476 | * @param[out] parsed Number of characters parsed, negative on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3477 | * |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3478 | * @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] | 3479 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3480 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3481 | 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] | 3482 | int *parsed) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3483 | { |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3484 | /* ... /node[source = destination] ... */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3485 | struct unres_data source_match, dest_match; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3486 | const char *path_key_expr, *source, *sour_pref, *dest, *dest_pref; |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3487 | int pke_len, sour_len, sour_pref_len, dest_len, dest_pref_len, parsed_loc = 0, pke_parsed = 0; |
| 3488 | int has_predicate, dest_parent_times, i, rc; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3489 | uint32_t j; |
Radek Krejci | 0c2fa3a | 2016-06-13 15:18:03 +0200 | [diff] [blame] | 3490 | struct lyd_node_leaf_list *leaf_dst, *leaf_src; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3491 | |
| 3492 | source_match.count = 1; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3493 | source_match.node = malloc(sizeof *source_match.node); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3494 | if (!source_match.node) { |
| 3495 | LOGMEM; |
| 3496 | return -1; |
| 3497 | } |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3498 | dest_match.count = 1; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3499 | dest_match.node = malloc(sizeof *dest_match.node); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3500 | if (!dest_match.node) { |
| 3501 | LOGMEM; |
| 3502 | return -1; |
| 3503 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3504 | |
| 3505 | do { |
| 3506 | if ((i = parse_path_predicate(pred, &sour_pref, &sour_pref_len, &source, &sour_len, &path_key_expr, |
| 3507 | &pke_len, &has_predicate)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3508 | LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, pred[-i], &pred[-i]); |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3509 | rc = -1; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3510 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3511 | } |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3512 | parsed_loc += i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3513 | pred += i; |
| 3514 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3515 | for (j = 0; j < node_match->count;) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3516 | /* source */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3517 | source_match.node[0] = node_match->node[j]; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3518 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3519 | /* must be leaf (key of a list) */ |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3520 | 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] | 3521 | &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] | 3522 | i = 0; |
| 3523 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3524 | } |
| 3525 | |
| 3526 | /* destination */ |
Radek Krejci | 0c2fa3a | 2016-06-13 15:18:03 +0200 | [diff] [blame] | 3527 | dest_match.node[0] = node; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3528 | dest_parent_times = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3529 | if ((i = parse_path_key_expr(path_key_expr, &dest_pref, &dest_pref_len, &dest, &dest_len, |
| 3530 | &dest_parent_times)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3531 | 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] | 3532 | rc = -1; |
| 3533 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3534 | } |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3535 | pke_parsed = i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3536 | for (i = 0; i < dest_parent_times; ++i) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3537 | dest_match.node[0] = dest_match.node[0]->parent; |
| 3538 | if (!dest_match.node[0]) { |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3539 | i = 0; |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3540 | rc = EXIT_FAILURE; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3541 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3542 | } |
| 3543 | } |
| 3544 | while (1) { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3545 | 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] | 3546 | &dest_match)) || (dest_match.count != 1)) { |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3547 | i = 0; |
| 3548 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3549 | } |
| 3550 | |
| 3551 | if (pke_len == pke_parsed) { |
| 3552 | break; |
| 3553 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3554 | 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] | 3555 | &dest_parent_times)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3556 | 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] | 3557 | rc = -1; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3558 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3559 | } |
| 3560 | pke_parsed += i; |
| 3561 | } |
| 3562 | |
| 3563 | /* check match between source and destination nodes */ |
Radek Krejci | 0c2fa3a | 2016-06-13 15:18:03 +0200 | [diff] [blame] | 3564 | leaf_dst = (struct lyd_node_leaf_list *)dest_match.node[0]; |
| 3565 | while (leaf_dst->value_type == LY_TYPE_LEAFREF) { |
| 3566 | leaf_dst = (struct lyd_node_leaf_list *)leaf_dst->value.leafref; |
| 3567 | } |
| 3568 | leaf_src = (struct lyd_node_leaf_list *)source_match.node[0]; |
| 3569 | while (leaf_src->value_type == LY_TYPE_LEAFREF) { |
| 3570 | leaf_src = (struct lyd_node_leaf_list *)leaf_src->value.leafref; |
| 3571 | } |
| 3572 | if (leaf_src->value_type != leaf_dst->value_type) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3573 | goto remove_leafref; |
| 3574 | } |
| 3575 | |
Radek Krejci | 0c2fa3a | 2016-06-13 15:18:03 +0200 | [diff] [blame] | 3576 | if (!ly_strequal(leaf_src->value_str, leaf_dst->value_str, 1)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3577 | goto remove_leafref; |
| 3578 | } |
| 3579 | |
| 3580 | /* leafref is ok, continue check with next leafref */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3581 | ++j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3582 | continue; |
| 3583 | |
| 3584 | remove_leafref: |
| 3585 | /* does not fulfill conditions, remove leafref record */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3586 | unres_data_del(node_match, j); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3587 | } |
| 3588 | } while (has_predicate); |
| 3589 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3590 | free(source_match.node); |
| 3591 | free(dest_match.node); |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3592 | if (parsed) { |
| 3593 | *parsed = parsed_loc; |
| 3594 | } |
| 3595 | return EXIT_SUCCESS; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3596 | |
| 3597 | error: |
| 3598 | |
| 3599 | if (source_match.count) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3600 | free(source_match.node); |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3601 | } |
| 3602 | if (dest_match.count) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3603 | free(dest_match.node); |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3604 | } |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3605 | if (parsed) { |
| 3606 | *parsed = -parsed_loc+i; |
| 3607 | } |
| 3608 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3609 | } |
| 3610 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3611 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3612 | * @brief Resolve a path (leafref) in JSON data context. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3613 | * |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3614 | * @param[in] node Leafref data node. |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3615 | * @param[in] path Path of the leafref. |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3616 | * @param[out] ret Matching nodes. Expects an empty, but allocated structure. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3617 | * |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3618 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 otherwise. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3619 | */ |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3620 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3621 | 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] | 3622 | { |
Radek Krejci | 71b795b | 2015-08-10 16:20:39 +0200 | [diff] [blame] | 3623 | struct lyd_node *data = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3624 | const char *prefix, *name; |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3625 | int pref_len, nam_len, has_predicate, parent_times, i, parsed, rc; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3626 | uint32_t j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3627 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3628 | assert(node && path && ret && !ret->count); |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3629 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3630 | parent_times = 0; |
Michal Vasko | d917334 | 2015-08-17 14:35:36 +0200 | [diff] [blame] | 3631 | parsed = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3632 | |
| 3633 | /* searching for nodeset */ |
| 3634 | do { |
Radek Krejci | f7ed4c3 | 2016-10-27 16:20:03 +0200 | [diff] [blame] | 3635 | 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] | 3636 | LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, path[-i], &path[-i]); |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3637 | rc = -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3638 | goto error; |
| 3639 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3640 | path += i; |
Michal Vasko | d917334 | 2015-08-17 14:35:36 +0200 | [diff] [blame] | 3641 | parsed += i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3642 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3643 | if (!ret->count) { |
Michal Vasko | bfd98e6 | 2016-09-02 09:50:05 +0200 | [diff] [blame] | 3644 | if (parent_times > 0) { |
| 3645 | data = node; |
| 3646 | for (i = 1; i < parent_times; ++i) { |
| 3647 | data = data->parent; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3648 | } |
Michal Vasko | bfd98e6 | 2016-09-02 09:50:05 +0200 | [diff] [blame] | 3649 | } else if (!parent_times) { |
| 3650 | data = node->child; |
| 3651 | } else { |
| 3652 | /* absolute path */ |
| 3653 | for (data = node; data->parent; data = data->parent); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3654 | } |
| 3655 | |
Michal Vasko | bfd98e6 | 2016-09-02 09:50:05 +0200 | [diff] [blame] | 3656 | /* we may still be parsing it and the pointer is not correct yet */ |
| 3657 | if (data->prev) { |
| 3658 | while (data->prev->next) { |
| 3659 | data = data->prev; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 3660 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3661 | } |
| 3662 | } |
| 3663 | |
| 3664 | /* node identifier */ |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3665 | 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] | 3666 | if (rc == -1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3667 | LOGVAL(LYE_INELEM_LEN, LY_VLOG_LYD, node, nam_len, name); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3668 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3669 | goto error; |
| 3670 | } |
| 3671 | |
| 3672 | if (has_predicate) { |
| 3673 | /* we have predicate, so the current results must be lists */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3674 | for (j = 0; j < ret->count;) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3675 | if (ret->node[j]->schema->nodetype == LYS_LIST && |
| 3676 | ((struct lys_node_list *)ret->node[0]->schema)->keys) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3677 | /* leafref is ok, continue check with next leafref */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3678 | ++j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3679 | continue; |
| 3680 | } |
| 3681 | |
| 3682 | /* does not fulfill conditions, remove leafref record */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3683 | unres_data_del(ret, j); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3684 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3685 | if ((rc = resolve_path_predicate_data(path, node, ret, &i))) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 3686 | if (rc == -1) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3687 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYD, node, "leafref", path); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3688 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3689 | goto error; |
| 3690 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3691 | path += i; |
Michal Vasko | d917334 | 2015-08-17 14:35:36 +0200 | [diff] [blame] | 3692 | parsed += i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3693 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3694 | if (!ret->count) { |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3695 | rc = EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3696 | goto error; |
| 3697 | } |
| 3698 | } |
| 3699 | } while (path[0] != '\0'); |
| 3700 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3701 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3702 | |
| 3703 | error: |
| 3704 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3705 | free(ret->node); |
| 3706 | ret->node = NULL; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3707 | ret->count = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3708 | |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3709 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3710 | } |
| 3711 | |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3712 | static int |
| 3713 | resolve_path_arg_schema_valid_dep_flag(const struct lys_node *op_node, const struct lys_node *first_node, int abs_path) |
| 3714 | { |
| 3715 | int dep1, dep2; |
| 3716 | const struct lys_node *node; |
| 3717 | |
| 3718 | if (lys_parent(op_node)) { |
| 3719 | /* inner operation (notif/action) */ |
| 3720 | if (abs_path) { |
| 3721 | return 1; |
| 3722 | } else { |
| 3723 | /* compare depth of both nodes */ |
| 3724 | for (dep1 = 0, node = op_node; lys_parent(node); node = lys_parent(node)); |
| 3725 | for (dep2 = 0, node = first_node; lys_parent(node); node = lys_parent(node)); |
| 3726 | if ((dep2 > dep1) || ((dep2 == dep1) && (op_node != first_node))) { |
| 3727 | return 1; |
| 3728 | } |
| 3729 | } |
| 3730 | } else { |
| 3731 | /* top-level operation (notif/rpc) */ |
| 3732 | if (op_node != first_node) { |
| 3733 | return 1; |
| 3734 | } |
| 3735 | } |
| 3736 | |
| 3737 | return 0; |
| 3738 | } |
| 3739 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3740 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3741 | * @brief Resolve a path (leafref) predicate in JSON schema context. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3742 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3743 | * @param[in] path Path to use. |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3744 | * @param[in] context_node Predicate context node (where the predicate is placed). |
| 3745 | * @param[in] parent Path context node (where the path begins/is placed). |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3746 | * @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] | 3747 | * |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3748 | * @return 0 on forward reference, otherwise the number |
| 3749 | * of characters successfully parsed, |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3750 | * positive on success, negative on failure. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3751 | */ |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3752 | static int |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3753 | 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] | 3754 | struct lys_node *parent, const struct lys_node *op_node) |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3755 | { |
Michal Vasko | 0f99d3e | 2017-01-10 10:50:40 +0100 | [diff] [blame] | 3756 | const struct lys_module *trg_mod; |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3757 | const struct lys_node *src_node, *dst_node; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3758 | const char *path_key_expr, *source, *sour_pref, *dest, *dest_pref; |
Michal Vasko | f9b35d9 | 2016-10-21 15:19:30 +0200 | [diff] [blame] | 3759 | int pke_len, sour_len, sour_pref_len, dest_len, dest_pref_len, pke_parsed, parsed = 0; |
| 3760 | int has_predicate, dest_parent_times, i, rc, first_iter; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3761 | |
| 3762 | do { |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3763 | 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] | 3764 | &pke_len, &has_predicate)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3765 | 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] | 3766 | return -parsed+i; |
| 3767 | } |
| 3768 | parsed += i; |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3769 | path += i; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3770 | |
Michal Vasko | 5809090 | 2015-08-13 14:04:15 +0200 | [diff] [blame] | 3771 | /* source (must be leaf) */ |
Michal Vasko | 0f99d3e | 2017-01-10 10:50:40 +0100 | [diff] [blame] | 3772 | if (sour_pref) { |
| 3773 | trg_mod = lys_get_import_module(lys_node_module(parent), NULL, 0, sour_pref, sour_pref_len); |
| 3774 | } else { |
| 3775 | trg_mod = NULL; |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3776 | } |
Michal Vasko | 0f99d3e | 2017-01-10 10:50:40 +0100 | [diff] [blame] | 3777 | rc = lys_get_data_sibling(trg_mod, context_node->child, source, sour_len, LYS_LEAF | LYS_LEAFLIST, &src_node); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3778 | if (rc) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3779 | 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] | 3780 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3781 | } |
| 3782 | |
| 3783 | /* destination */ |
Michal Vasko | f9b35d9 | 2016-10-21 15:19:30 +0200 | [diff] [blame] | 3784 | dest_parent_times = 0; |
| 3785 | pke_parsed = 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3786 | if ((i = parse_path_key_expr(path_key_expr, &dest_pref, &dest_pref_len, &dest, &dest_len, |
| 3787 | &dest_parent_times)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3788 | 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] | 3789 | return -parsed; |
| 3790 | } |
| 3791 | pke_parsed += i; |
| 3792 | |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3793 | for (i = 0, dst_node = parent; i < dest_parent_times; ++i) { |
Michal Vasko | fbaead7 | 2016-10-07 10:54:48 +0200 | [diff] [blame] | 3794 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 3795 | * all schema nodes that cannot be instantiated in data tree */ |
| 3796 | for (dst_node = lys_parent(dst_node); |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3797 | 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] | 3798 | dst_node = lys_parent(dst_node)); |
| 3799 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3800 | if (!dst_node) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3801 | 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] | 3802 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3803 | } |
| 3804 | } |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3805 | first_iter = 1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3806 | while (1) { |
Michal Vasko | 0f99d3e | 2017-01-10 10:50:40 +0100 | [diff] [blame] | 3807 | if (dest_pref) { |
| 3808 | trg_mod = lys_get_import_module(lys_node_module(parent), NULL, 0, dest_pref, dest_pref_len); |
| 3809 | } else { |
| 3810 | trg_mod = NULL; |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3811 | } |
Michal Vasko | 0f99d3e | 2017-01-10 10:50:40 +0100 | [diff] [blame] | 3812 | rc = lys_get_data_sibling(trg_mod, dst_node->child, dest, dest_len, LYS_CONTAINER | LYS_LIST | LYS_LEAF, &dst_node); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3813 | if (rc) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3814 | 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] | 3815 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3816 | } |
| 3817 | |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3818 | if (first_iter) { |
| 3819 | if (resolve_path_arg_schema_valid_dep_flag(op_node, dst_node, 0)) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 3820 | parent->flags |= LYS_LEAFREF_DEP; |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3821 | } |
| 3822 | first_iter = 0; |
| 3823 | } |
| 3824 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3825 | if (pke_len == pke_parsed) { |
| 3826 | break; |
| 3827 | } |
| 3828 | |
| 3829 | if ((i = parse_path_key_expr(path_key_expr+pke_parsed, &dest_pref, &dest_pref_len, &dest, &dest_len, |
| 3830 | &dest_parent_times)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3831 | LOGVAL(LYE_INCHAR, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3832 | (path_key_expr+pke_parsed)[-i], (path_key_expr+pke_parsed)-i); |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3833 | return -parsed; |
| 3834 | } |
| 3835 | pke_parsed += i; |
| 3836 | } |
| 3837 | |
| 3838 | /* check source - dest match */ |
Michal Vasko | 59ad458 | 2016-09-16 13:15:41 +0200 | [diff] [blame] | 3839 | if (dst_node->nodetype != src_node->nodetype) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3840 | LOGVAL(LYE_NORESOLV, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, "leafref predicate", path-parsed); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 3841 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Destination node is not a %s, but a %s.", |
Michal Vasko | 59ad458 | 2016-09-16 13:15:41 +0200 | [diff] [blame] | 3842 | strnodetype(src_node->nodetype), strnodetype(dst_node->nodetype)); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3843 | return -parsed; |
| 3844 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3845 | } while (has_predicate); |
| 3846 | |
| 3847 | return parsed; |
| 3848 | } |
| 3849 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3850 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3851 | * @brief Resolve a path (leafref) in JSON schema context. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3852 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3853 | * @param[in] path Path to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3854 | * @param[in] parent_node Parent of the leafref. |
Radek Krejci | 2f12f85 | 2016-01-08 12:59:57 +0100 | [diff] [blame] | 3855 | * @param[in] parent_tpdf Flag if the parent node is actually typedef, in that case the path |
| 3856 | * has to contain absolute path |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3857 | * @param[out] ret Pointer to the resolved schema node. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3858 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3859 | * @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] | 3860 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3861 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3862 | 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] | 3863 | const struct lys_node **ret) |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3864 | { |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3865 | const struct lys_node *node, *op_node = NULL; |
Radek Krejci | 990af1f | 2016-11-09 13:53:36 +0100 | [diff] [blame] | 3866 | const struct lys_module *mod; |
| 3867 | struct lys_module *mod_start; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3868 | const char *id, *prefix, *name; |
| 3869 | int pref_len, nam_len, parent_times, has_predicate; |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3870 | int i, first_iter, rc; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3871 | |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3872 | first_iter = 1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3873 | parent_times = 0; |
| 3874 | id = path; |
| 3875 | |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3876 | /* find operation schema we are in, if applicable */ |
Michal Vasko | e9914d1 | 2016-10-07 14:32:37 +0200 | [diff] [blame] | 3877 | if (!parent_tpdf) { |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3878 | for (op_node = lys_parent(parent); |
| 3879 | op_node && !(op_node->nodetype & (LYS_ACTION | LYS_NOTIF | LYS_RPC)); |
| 3880 | op_node = lys_parent(op_node)); |
Michal Vasko | e9914d1 | 2016-10-07 14:32:37 +0200 | [diff] [blame] | 3881 | } |
| 3882 | |
Radek Krejci | 990af1f | 2016-11-09 13:53:36 +0100 | [diff] [blame] | 3883 | mod_start = lys_node_module(parent); |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3884 | do { |
Radek Krejci | 990af1f | 2016-11-09 13:53:36 +0100 | [diff] [blame] | 3885 | 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] | 3886 | 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] | 3887 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3888 | } |
| 3889 | id += i; |
| 3890 | |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3891 | if (first_iter) { |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3892 | if (parent_times == -1) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 3893 | /* resolve prefix of the module */ |
Radek Krejci | 990af1f | 2016-11-09 13:53:36 +0100 | [diff] [blame] | 3894 | mod = prefix ? lys_get_import_module(mod_start, NULL, 0, prefix, pref_len) : mod_start; |
| 3895 | if (!mod) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3896 | LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, |
| 3897 | "leafref", path); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3898 | return EXIT_FAILURE; |
Michal Vasko | 5809090 | 2015-08-13 14:04:15 +0200 | [diff] [blame] | 3899 | } |
Radek Krejci | 990af1f | 2016-11-09 13:53:36 +0100 | [diff] [blame] | 3900 | if (!mod->implemented) { |
Radek Krejci | 2eee5c0 | 2016-12-06 19:18:05 +0100 | [diff] [blame] | 3901 | mod = lys_implemented_module(mod); |
Radek Krejci | 990af1f | 2016-11-09 13:53:36 +0100 | [diff] [blame] | 3902 | if (!mod->implemented) { |
| 3903 | /* make the found module implemented */ |
| 3904 | if (lys_set_implemented(mod)) { |
| 3905 | return EXIT_FAILURE; |
| 3906 | } |
| 3907 | } |
| 3908 | } |
| 3909 | /* get start node */ |
| 3910 | if (!mod->data) { |
| 3911 | LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, |
| 3912 | "leafref", path); |
| 3913 | return EXIT_FAILURE; |
| 3914 | } |
| 3915 | node = mod->data; |
| 3916 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3917 | } else if (parent_times > 0) { |
Radek Krejci | 2f12f85 | 2016-01-08 12:59:57 +0100 | [diff] [blame] | 3918 | if (parent_tpdf) { |
| 3919 | /* 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] | 3920 | LOGVAL(LYE_NORESOLV, 0, NULL, "leafref", path); |
Radek Krejci | 2f12f85 | 2016-01-08 12:59:57 +0100 | [diff] [blame] | 3921 | return -1; |
| 3922 | } |
| 3923 | |
Michal Vasko | 9445808 | 2016-10-07 14:34:36 +0200 | [diff] [blame] | 3924 | /* we are looking for a sibling of a node, node it's parent (that is why parent_times - 1) */ |
| 3925 | for (i = 0, node = parent; i < parent_times - 1; i++) { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 3926 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 3927 | * all schema nodes that cannot be instantiated in data tree */ |
| 3928 | for (node = lys_parent(node); |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3929 | 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] | 3930 | node = lys_parent(node)); |
| 3931 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3932 | if (!node) { |
Michal Vasko | e9914d1 | 2016-10-07 14:32:37 +0200 | [diff] [blame] | 3933 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3934 | return EXIT_FAILURE; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3935 | } |
| 3936 | } |
Radek Krejci | 990af1f | 2016-11-09 13:53:36 +0100 | [diff] [blame] | 3937 | |
| 3938 | /* now we have to check that if we are going into a node from a different module, |
| 3939 | * the module is implemented (so its augments are applied) */ |
| 3940 | mod = prefix ? lys_get_import_module(mod_start, NULL, 0, prefix, pref_len) : mod_start; |
| 3941 | if (!mod) { |
| 3942 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path); |
| 3943 | return EXIT_FAILURE; |
| 3944 | } |
| 3945 | if (!mod->implemented) { |
Radek Krejci | 2eee5c0 | 2016-12-06 19:18:05 +0100 | [diff] [blame] | 3946 | mod = lys_implemented_module(mod); |
Radek Krejci | 990af1f | 2016-11-09 13:53:36 +0100 | [diff] [blame] | 3947 | if (!mod->implemented) { |
| 3948 | /* make the found module implemented */ |
| 3949 | if (lys_set_implemented(mod)) { |
| 3950 | return EXIT_FAILURE; |
| 3951 | } |
| 3952 | } |
| 3953 | } |
Michal Vasko | e01eca5 | 2015-08-13 14:42:02 +0200 | [diff] [blame] | 3954 | } else { |
| 3955 | LOGINT; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3956 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3957 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3958 | } else { |
Radek Krejci | 990af1f | 2016-11-09 13:53:36 +0100 | [diff] [blame] | 3959 | /* we have to first check that the module we are going into is implemented */ |
| 3960 | mod = prefix ? lys_get_import_module(mod_start, NULL, 0, prefix, pref_len) : mod_start; |
| 3961 | if (!mod) { |
| 3962 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path); |
| 3963 | return EXIT_FAILURE; |
| 3964 | } |
| 3965 | if (!mod->implemented) { |
Radek Krejci | 2eee5c0 | 2016-12-06 19:18:05 +0100 | [diff] [blame] | 3966 | mod = lys_implemented_module(mod); |
Radek Krejci | 990af1f | 2016-11-09 13:53:36 +0100 | [diff] [blame] | 3967 | if (!mod->implemented) { |
| 3968 | /* make the found module implemented */ |
| 3969 | if (lys_set_implemented(mod)) { |
| 3970 | return EXIT_FAILURE; |
| 3971 | } |
| 3972 | } |
| 3973 | } |
| 3974 | |
Michal Vasko | 7dc71d0 | 2016-03-15 10:42:28 +0100 | [diff] [blame] | 3975 | /* move down the tree, if possible */ |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 3976 | if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
Michal Vasko | 2f5aceb | 2016-03-22 10:24:14 +0100 | [diff] [blame] | 3977 | 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] | 3978 | return -1; |
| 3979 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3980 | node = node->child; |
Radek Krejci | 43ccc4c | 2016-10-18 20:40:06 +0200 | [diff] [blame] | 3981 | if (!node) { |
| 3982 | LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, |
| 3983 | "leafref", path); |
| 3984 | return EXIT_FAILURE; |
| 3985 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3986 | } |
| 3987 | |
Michal Vasko | 0f99d3e | 2017-01-10 10:50:40 +0100 | [diff] [blame] | 3988 | rc = lys_get_data_sibling(mod, node, name, nam_len, LYS_LIST | LYS_CONTAINER | LYS_RPC | LYS_ACTION | LYS_NOTIF |
| 3989 | | LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA, &node); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3990 | if (rc) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3991 | 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] | 3992 | return EXIT_FAILURE; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3993 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3994 | |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3995 | if (first_iter) { |
| 3996 | /* set external dependency flag, we can decide based on the first found node */ |
| 3997 | if (!parent_tpdf && op_node && parent_times && |
| 3998 | resolve_path_arg_schema_valid_dep_flag(op_node, node, (parent_times == -1 ? 1 : 0))) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 3999 | parent->flags |= LYS_LEAFREF_DEP; |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 4000 | } |
| 4001 | first_iter = 0; |
| 4002 | } |
| 4003 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 4004 | if (has_predicate) { |
| 4005 | /* we have predicate, so the current result must be list */ |
| 4006 | if (node->nodetype != LYS_LIST) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 4007 | 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] | 4008 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 4009 | } |
| 4010 | |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 4011 | i = resolve_path_predicate_schema(id, node, parent, op_node); |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4012 | if (i <= 0) { |
| 4013 | if (i == 0) { |
| 4014 | return EXIT_FAILURE; |
| 4015 | } else { /* i < 0 */ |
| 4016 | return -1; |
| 4017 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 4018 | } |
| 4019 | id += i; |
Michal Vasko | f9b35d9 | 2016-10-21 15:19:30 +0200 | [diff] [blame] | 4020 | has_predicate = 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 4021 | } |
| 4022 | } while (id[0]); |
| 4023 | |
Michal Vasko | ca91768 | 2016-07-25 11:00:37 +0200 | [diff] [blame] | 4024 | /* 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] | 4025 | if ((node->nodetype != LYS_LEAF) && (node->nodetype != LYS_LEAFLIST)) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 4026 | LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, "leafref", path); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 4027 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Leafref target \"%s\" is not a leaf nor a leaf-list.", path); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4028 | return -1; |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 4029 | } |
| 4030 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 4031 | /* check status */ |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 4032 | if (lyp_check_status(parent->flags, parent->module, parent->name, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4033 | node->flags, node->module, node->name, node)) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 4034 | return -1; |
| 4035 | } |
| 4036 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4037 | if (ret) { |
| 4038 | *ret = node; |
| 4039 | } |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4040 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4041 | return EXIT_SUCCESS; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 4042 | } |
| 4043 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4044 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 4045 | * @brief Resolve instance-identifier predicate in JSON data format. |
| 4046 | * Does not log. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4047 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4048 | * @param[in] pred Predicate to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4049 | * @param[in,out] node_match Nodes matching the restriction without |
| 4050 | * the predicate. Nodes not satisfying |
| 4051 | * the predicate are removed. |
| 4052 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4053 | * @return Number of characters successfully parsed, |
| 4054 | * positive on success, negative on failure. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4055 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4056 | static int |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 4057 | resolve_predicate(const char *pred, struct unres_data *node_match) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4058 | { |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4059 | /* ... /node[target = value] ... */ |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4060 | struct lyd_node *target; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4061 | const char *model, *name, *value; |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4062 | 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] | 4063 | uint32_t j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4064 | |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4065 | assert(pred && node_match->count); |
| 4066 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4067 | idx = -1; |
| 4068 | parsed = 0; |
| 4069 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4070 | pred_iter = -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4071 | do { |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 4072 | 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] | 4073 | return -parsed+i; |
| 4074 | } |
| 4075 | parsed += i; |
| 4076 | pred += i; |
| 4077 | |
| 4078 | if (isdigit(name[0])) { |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4079 | /* pos */ |
| 4080 | assert(!value); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4081 | idx = atoi(name); |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4082 | } else if (name[0] != '.') { |
| 4083 | /* list keys */ |
| 4084 | if (pred_iter < 0) { |
| 4085 | pred_iter = 1; |
| 4086 | } else { |
| 4087 | ++pred_iter; |
| 4088 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4089 | } |
| 4090 | |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 4091 | for (cur_idx = 1, j = 0; j < node_match->count; ++cur_idx) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4092 | /* target */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4093 | if (name[0] == '.') { |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4094 | /* leaf-list value */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4095 | if (node_match->node[j]->schema->nodetype != LYS_LEAFLIST) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4096 | goto remove_instid; |
| 4097 | } |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4098 | |
| 4099 | target = node_match->node[j]; |
| 4100 | /* check the value */ |
| 4101 | if (strncmp(((struct lyd_node_leaf_list *)target)->value_str, value, val_len) |
| 4102 | || ((struct lyd_node_leaf_list *)target)->value_str[val_len]) { |
| 4103 | goto remove_instid; |
| 4104 | } |
| 4105 | |
| 4106 | } else if (!value) { |
| 4107 | /* keyless list position */ |
| 4108 | if ((node_match->node[j]->schema->nodetype != LYS_LIST) |
| 4109 | || ((struct lys_node_list *)node_match->node[j]->schema)->keys) { |
| 4110 | goto remove_instid; |
| 4111 | } |
| 4112 | |
| 4113 | if (idx != cur_idx) { |
| 4114 | goto remove_instid; |
| 4115 | } |
| 4116 | |
| 4117 | } else { |
| 4118 | /* list key value */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4119 | if (node_match->node[j]->schema->nodetype != LYS_LIST) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4120 | goto remove_instid; |
| 4121 | } |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4122 | |
| 4123 | /* key module must match the list module */ |
| 4124 | if (strncmp(node_match->node[j]->schema->module->name, model, mod_len) |
| 4125 | || node_match->node[j]->schema->module->name[mod_len]) { |
| 4126 | goto remove_instid; |
| 4127 | } |
| 4128 | /* find the key leaf */ |
Michal Vasko | 045182c | 2016-09-09 12:44:07 +0200 | [diff] [blame] | 4129 | 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] | 4130 | if (!target) { |
| 4131 | goto remove_instid; |
| 4132 | } |
| 4133 | if ((struct lys_node_leaf *)target->schema != |
| 4134 | ((struct lys_node_list *)node_match->node[j]->schema)->keys[pred_iter - 1]) { |
| 4135 | goto remove_instid; |
| 4136 | } |
| 4137 | |
| 4138 | /* check the value */ |
| 4139 | if (strncmp(((struct lyd_node_leaf_list *)target)->value_str, value, val_len) |
| 4140 | || ((struct lyd_node_leaf_list *)target)->value_str[val_len]) { |
| 4141 | goto remove_instid; |
| 4142 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4143 | } |
| 4144 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4145 | /* instid is ok, continue check with the next one */ |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4146 | ++j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4147 | continue; |
| 4148 | |
| 4149 | remove_instid: |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4150 | /* does not fulfill conditions, remove instid record */ |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4151 | unres_data_del(node_match, j); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4152 | } |
| 4153 | } while (has_predicate); |
| 4154 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4155 | /* check that all list keys were specified */ |
| 4156 | if ((pred_iter > 0) && node_match->count) { |
Michal Vasko | 045182c | 2016-09-09 12:44:07 +0200 | [diff] [blame] | 4157 | j = 0; |
| 4158 | while (j < node_match->count) { |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4159 | assert(node_match->node[j]->schema->nodetype == LYS_LIST); |
| 4160 | if (pred_iter < ((struct lys_node_list *)node_match->node[j]->schema)->keys_size) { |
| 4161 | /* not enough predicates, just remove the list instance */ |
| 4162 | unres_data_del(node_match, j); |
Michal Vasko | 045182c | 2016-09-09 12:44:07 +0200 | [diff] [blame] | 4163 | } else { |
| 4164 | ++j; |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4165 | } |
| 4166 | } |
| 4167 | |
| 4168 | if (!node_match->count) { |
| 4169 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier is missing some list keys."); |
| 4170 | } |
| 4171 | } |
| 4172 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4173 | return parsed; |
| 4174 | } |
| 4175 | |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4176 | int |
Michal Vasko | 769f803 | 2017-01-24 13:11:55 +0100 | [diff] [blame] | 4177 | lys_check_xpath(struct lys_node *node, int check_place, int warn_on_fwd_ref) |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4178 | { |
| 4179 | struct lys_node *parent, *elem; |
| 4180 | struct lyxp_set set; |
| 4181 | uint32_t i; |
Michal Vasko | 769f803 | 2017-01-24 13:11:55 +0100 | [diff] [blame] | 4182 | int ret; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4183 | |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4184 | if (check_place) { |
| 4185 | parent = node; |
| 4186 | while (parent) { |
| 4187 | if (parent->nodetype == LYS_GROUPING) { |
| 4188 | /* unresolved grouping, skip for now (will be checked later) */ |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4189 | return EXIT_SUCCESS; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4190 | } |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4191 | if (parent->nodetype == LYS_AUGMENT) { |
| 4192 | if (!((struct lys_node_augment *)parent)->target) { |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 4193 | /* unresolved augment */ |
| 4194 | if (parent->module->implemented) { |
| 4195 | /* skip for now (will be checked later) */ |
| 4196 | return EXIT_FAILURE; |
| 4197 | } else { |
| 4198 | /* not implemented augment, skip resolving */ |
| 4199 | return EXIT_SUCCESS; |
| 4200 | } |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4201 | } else { |
| 4202 | parent = ((struct lys_node_augment *)parent)->target; |
| 4203 | continue; |
| 4204 | } |
| 4205 | } |
| 4206 | parent = parent->parent; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4207 | } |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4208 | } |
| 4209 | |
Michal Vasko | 769f803 | 2017-01-24 13:11:55 +0100 | [diff] [blame] | 4210 | ret = lyxp_node_atomize(node, &set, warn_on_fwd_ref); |
| 4211 | if (ret == -1) { |
| 4212 | return -1; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4213 | } |
| 4214 | |
| 4215 | for (parent = node; parent && !(parent->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); parent = lys_parent(parent)); |
| 4216 | |
| 4217 | for (i = 0; i < set.used; ++i) { |
| 4218 | /* skip roots'n'stuff */ |
| 4219 | if (set.val.snodes[i].type == LYXP_NODE_ELEM) { |
| 4220 | /* XPath expression cannot reference "lower" status than the node that has the definition */ |
| 4221 | if (lyp_check_status(node->flags, lys_node_module(node), node->name, set.val.snodes[i].snode->flags, |
| 4222 | lys_node_module(set.val.snodes[i].snode), set.val.snodes[i].snode->name, node)) { |
| 4223 | return -1; |
| 4224 | } |
| 4225 | |
| 4226 | if (parent) { |
| 4227 | for (elem = set.val.snodes[i].snode; elem && (elem != parent); elem = lys_parent(elem)); |
| 4228 | if (!elem) { |
| 4229 | /* not in node's RPC or notification subtree, set the flag */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 4230 | node->flags |= LYS_XPATH_DEP; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4231 | break; |
| 4232 | } |
| 4233 | } |
| 4234 | } |
| 4235 | } |
| 4236 | |
| 4237 | free(set.val.snodes); |
Michal Vasko | 769f803 | 2017-01-24 13:11:55 +0100 | [diff] [blame] | 4238 | return ret; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4239 | } |
| 4240 | |
Radek Krejci | f71f48f | 2016-10-25 16:37:24 +0200 | [diff] [blame] | 4241 | static int |
| 4242 | check_leafref_config(struct lys_node_leaf *leaf, struct lys_type *type) |
| 4243 | { |
| 4244 | int i; |
| 4245 | |
| 4246 | if (type->base == LY_TYPE_LEAFREF) { |
| 4247 | if ((leaf->flags & LYS_CONFIG_W) && type->info.lref.target && (type->info.lref.target->flags & LYS_CONFIG_R)) { |
| 4248 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, leaf, "The %s is config but refers to a non-config %s.", |
| 4249 | strnodetype(leaf->nodetype), strnodetype(type->info.lref.target->nodetype)); |
| 4250 | return -1; |
| 4251 | } |
| 4252 | /* we can skip the test in case the leafref is not yet resolved. In that case the test is done in the time |
| 4253 | * of leafref resolving (lys_leaf_add_leafref_target()) */ |
| 4254 | } else if (type->base == LY_TYPE_UNION) { |
| 4255 | for (i = 0; i < type->info.uni.count; i++) { |
| 4256 | if (check_leafref_config(leaf, &type->info.uni.types[i])) { |
| 4257 | return -1; |
| 4258 | } |
| 4259 | } |
| 4260 | } |
| 4261 | return 0; |
| 4262 | } |
| 4263 | |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4264 | /** |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4265 | * @brief Passes config flag down to children, skips nodes without config flags. |
| 4266 | * Does not log. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4267 | * |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4268 | * @param[in] node Siblings and their children to have flags changed. |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4269 | * @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] | 4270 | * @param[in] flags Flags to assign to all the nodes. |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 4271 | * @param[in,out] unres List of unresolved items. |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4272 | * |
| 4273 | * @return 0 on success, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4274 | */ |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4275 | static int |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 4276 | 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] | 4277 | { |
Radek Krejci | f71f48f | 2016-10-25 16:37:24 +0200 | [diff] [blame] | 4278 | struct lys_node_leaf *leaf; |
| 4279 | |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4280 | assert(!(flags ^ (flags & LYS_CONFIG_MASK))); |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4281 | LY_TREE_FOR(node, node) { |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 4282 | 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] | 4283 | return -1; |
| 4284 | } |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4285 | if (clear) { |
| 4286 | node->flags &= ~LYS_CONFIG_MASK; |
Michal Vasko | c2a8d36 | 2016-09-29 08:50:13 +0200 | [diff] [blame] | 4287 | node->flags &= ~LYS_CONFIG_SET; |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4288 | } else { |
| 4289 | if (node->flags & LYS_CONFIG_SET) { |
| 4290 | /* skip nodes with an explicit config value */ |
| 4291 | if ((flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) { |
| 4292 | LOGVAL(LYE_INARG, LY_VLOG_LYS, node, "true", "config"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 4293 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "State nodes cannot have configuration nodes as children."); |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4294 | return -1; |
| 4295 | } |
| 4296 | continue; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4297 | } |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4298 | |
| 4299 | if (!(node->nodetype & (LYS_USES | LYS_GROUPING))) { |
| 4300 | node->flags = (node->flags & ~LYS_CONFIG_MASK) | flags; |
| 4301 | /* check that configuration lists have keys */ |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4302 | if ((node->nodetype == LYS_LIST) && (node->flags & LYS_CONFIG_W) |
| 4303 | && !((struct lys_node_list *)node)->keys_size) { |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4304 | LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_LYS, node, "key", "list"); |
| 4305 | return -1; |
| 4306 | } |
| 4307 | } |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4308 | } |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4309 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) { |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 4310 | if (inherit_config_flag(node->child, flags, clear, unres)) { |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4311 | return -1; |
| 4312 | } |
Radek Krejci | f71f48f | 2016-10-25 16:37:24 +0200 | [diff] [blame] | 4313 | } else if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 4314 | leaf = (struct lys_node_leaf *)node; |
| 4315 | if (check_leafref_config(leaf, &leaf->type)) { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 4316 | return -1; |
| 4317 | } |
| 4318 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4319 | } |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4320 | |
| 4321 | return 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4322 | } |
| 4323 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4324 | /** |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 4325 | * @brief Resolve augment target. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4326 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4327 | * @param[in] aug Augment to use. |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 4328 | * @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] | 4329 | * @param[in,out] unres List of unresolved items. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4330 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4331 | * @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] | 4332 | */ |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 4333 | static int |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 4334 | 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] | 4335 | { |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4336 | int rc, clear_config; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 4337 | unsigned int u; |
Michal Vasko | 1d87a92 | 2015-08-21 12:57:16 +0200 | [diff] [blame] | 4338 | struct lys_node *sub; |
Pavol Vican | 4731993 | 2016-08-29 09:14:47 +0200 | [diff] [blame] | 4339 | const struct lys_node *aug_target, *parent; |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4340 | struct lys_module *mod; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 4341 | struct lys_ext_instance *ext; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4342 | |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4343 | assert(aug && !aug->target); |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 4344 | mod = lys_main_module(aug->module); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4345 | |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4346 | /* resolve target node */ |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 4347 | 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] | 4348 | if (rc == -1) { |
| 4349 | return -1; |
Radek Krejci | 5fb4c38 | 2016-11-28 09:21:42 +0100 | [diff] [blame] | 4350 | } else if (rc > 0) { |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4351 | LOGVAL(LYE_INCHAR, LY_VLOG_LYS, aug, aug->target_name[rc - 1], &aug->target_name[rc - 1]); |
| 4352 | return -1; |
Radek Krejci | 5fb4c38 | 2016-11-28 09:21:42 +0100 | [diff] [blame] | 4353 | } else if (rc == 0 && aug->target) { |
| 4354 | /* augment was resolved as a side effect of setting module implemented when |
| 4355 | * resolving augment schema nodeid, so we are done here */ |
| 4356 | return 0; |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4357 | } |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 4358 | if (!aug_target && mod->implemented) { |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4359 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, aug, "augment", aug->target_name); |
| 4360 | return EXIT_FAILURE; |
| 4361 | } |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4362 | /* check that we want to connect augment into its target */ |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4363 | if (!mod->implemented) { |
| 4364 | /* it must be augment only to the same module, |
| 4365 | * otherwise we do not apply augment in not-implemented |
| 4366 | * module. If the module is set to be implemented in future, |
| 4367 | * the augment is being resolved and checked again */ |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 4368 | if (!aug_target) { |
| 4369 | /* target was not even resolved */ |
| 4370 | return EXIT_SUCCESS; |
| 4371 | } |
| 4372 | /* target was resolved, but it may refer another module */ |
| 4373 | for (sub = (struct lys_node *)aug_target; sub; sub = lys_parent(sub)) { |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4374 | if (lys_node_module(sub) != mod) { |
| 4375 | /* this is not an implemented module and the augment |
| 4376 | * target some other module, so avoid its connecting |
| 4377 | * to the target */ |
| 4378 | return EXIT_SUCCESS; |
| 4379 | } |
| 4380 | } |
| 4381 | } |
| 4382 | |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4383 | if (!aug->child) { |
| 4384 | /* nothing to do */ |
| 4385 | LOGWRN("Augment \"%s\" without children.", aug->target_name); |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4386 | goto success; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4387 | } |
| 4388 | |
Michal Vasko | d58d596 | 2016-03-02 14:29:41 +0100 | [diff] [blame] | 4389 | /* check for mandatory nodes - if the target node is in another module |
| 4390 | * the added nodes cannot be mandatory |
| 4391 | */ |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4392 | 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] | 4393 | && (rc = lyp_check_mandatory_augment(aug, aug_target))) { |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 4394 | return rc; |
Michal Vasko | d58d596 | 2016-03-02 14:29:41 +0100 | [diff] [blame] | 4395 | } |
| 4396 | |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4397 | /* check augment target type and then augment nodes type */ |
Michal Vasko | db01726 | 2017-01-24 13:10:04 +0100 | [diff] [blame] | 4398 | if (aug_target->nodetype & (LYS_CONTAINER | LYS_LIST)) { |
| 4399 | LY_TREE_FOR(aug->child, sub) { |
| 4400 | if (!(sub->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_LEAF | LYS_LIST | LYS_LEAFLIST | LYS_USES |
| 4401 | | LYS_CHOICE | LYS_ACTION | LYS_NOTIF))) { |
| 4402 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment"); |
| 4403 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Cannot augment \"%s\" with a \"%s\".", |
| 4404 | strnodetype(aug_target->nodetype), strnodetype(sub->nodetype)); |
| 4405 | return -1; |
| 4406 | } |
| 4407 | } |
| 4408 | } else if (aug_target->nodetype & (LYS_CASE | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF)) { |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4409 | LY_TREE_FOR(aug->child, sub) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4410 | 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] | 4411 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 4412 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Cannot augment \"%s\" with a \"%s\".", |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4413 | strnodetype(aug_target->nodetype), strnodetype(sub->nodetype)); |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4414 | return -1; |
| 4415 | } |
| 4416 | } |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4417 | } else if (aug_target->nodetype == LYS_CHOICE) { |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4418 | LY_TREE_FOR(aug->child, sub) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4419 | 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] | 4420 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 4421 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Cannot augment \"%s\" with a \"%s\".", |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4422 | strnodetype(aug_target->nodetype), strnodetype(sub->nodetype)); |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4423 | return -1; |
| 4424 | } |
| 4425 | } |
| 4426 | } else { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4427 | LOGVAL(LYE_INARG, LY_VLOG_LYS, aug, aug->target_name, "target-node"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 4428 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Invalid augment target node type \"%s\".", strnodetype(aug_target->nodetype)); |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4429 | return -1; |
| 4430 | } |
| 4431 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 4432 | /* check identifier uniqueness as in lys_node_addchild() */ |
Michal Vasko | 1d87a92 | 2015-08-21 12:57:16 +0200 | [diff] [blame] | 4433 | LY_TREE_FOR(aug->child, sub) { |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4434 | if (lys_check_id(sub, (struct lys_node *)aug_target, NULL)) { |
Michal Vasko | 3e6665f | 2015-08-17 14:00:38 +0200 | [diff] [blame] | 4435 | return -1; |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 4436 | } |
| 4437 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4438 | |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4439 | /* finally reconnect augmenting data into the target - add them to the target child list, |
| 4440 | * by setting aug->target we know the augment is fully resolved now */ |
| 4441 | aug->target = (struct lys_node *)aug_target; |
| 4442 | if (aug->target->child) { |
| 4443 | sub = aug->target->child->prev; /* remember current target's last node */ |
| 4444 | sub->next = aug->child; /* connect augmenting data after target's last node */ |
| 4445 | aug->target->child->prev = aug->child->prev; /* new target's last node is last augmenting node */ |
| 4446 | aug->child->prev = sub; /* finish connecting of both child lists */ |
| 4447 | } else { |
| 4448 | aug->target->child = aug->child; |
| 4449 | } |
| 4450 | |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4451 | /* inherit config information from actual parent */ |
| 4452 | for(parent = aug_target; parent && !(parent->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT | LYS_RPC)); parent = lys_parent(parent)); |
| 4453 | clear_config = (parent) ? 1 : 0; |
| 4454 | LY_TREE_FOR(aug->child, sub) { |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 4455 | 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] | 4456 | return -1; |
| 4457 | } |
| 4458 | } |
| 4459 | |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 4460 | /* inherit extensions if any */ |
| 4461 | for (u = 0; u < aug->target->ext_size; u++) { |
| 4462 | ext = aug->target->ext[u]; /* shortcut */ |
| 4463 | if (ext && ext->def->plugin && (ext->def->plugin->flags & LYEXT_OPT_INHERIT)) { |
| 4464 | unres_schema_add_node(mod, unres, &ext, UNRES_EXT_FINALIZE, NULL); |
| 4465 | } |
| 4466 | } |
| 4467 | |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4468 | success: |
| 4469 | if (mod->implemented) { |
| 4470 | /* make target modules also implemented */ |
| 4471 | for (sub = aug->target; sub; sub = lys_parent(sub)) { |
| 4472 | if (lys_set_implemented(sub->module)) { |
| 4473 | return -1; |
| 4474 | } |
| 4475 | } |
| 4476 | } |
| 4477 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4478 | return EXIT_SUCCESS; |
| 4479 | } |
| 4480 | |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4481 | static int |
Radek Krejci | a7db970 | 2017-01-20 12:55:14 +0100 | [diff] [blame] | 4482 | resolve_extension(struct unres_ext *info, struct lys_ext_instance **ext, struct unres_schema *unres) |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4483 | { |
| 4484 | enum LY_VLOG_ELEM vlog_type; |
| 4485 | void *vlog_node; |
| 4486 | unsigned int i, j; |
| 4487 | int c_ext = -1, rc; |
| 4488 | struct lys_ext *e; |
| 4489 | const char *value; |
| 4490 | struct lyxml_elem *next_yin, *yin; |
Radek Krejci | a7db970 | 2017-01-20 12:55:14 +0100 | [diff] [blame] | 4491 | const struct lys_module *mod; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4492 | |
| 4493 | switch (info->parent_type) { |
Radek Krejci | 0aa821a | 2016-12-08 11:21:35 +0100 | [diff] [blame] | 4494 | case LYEXT_PAR_NODE: |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4495 | vlog_node = info->parent; |
| 4496 | vlog_type = LY_VLOG_LYS; |
| 4497 | break; |
Radek Krejci | 0aa821a | 2016-12-08 11:21:35 +0100 | [diff] [blame] | 4498 | case LYEXT_PAR_MODULE: |
| 4499 | case LYEXT_PAR_IMPORT: |
| 4500 | case LYEXT_PAR_INCLUDE: |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4501 | vlog_node = NULL; |
| 4502 | vlog_type = LY_VLOG_LYS; |
| 4503 | break; |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 4504 | default: |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4505 | vlog_node = NULL; |
| 4506 | vlog_node = LY_VLOG_NONE; |
| 4507 | break; |
| 4508 | } |
| 4509 | |
| 4510 | if (info->datatype == LYS_IN_YIN) { |
| 4511 | /* get the module where the extension is supposed to be defined */ |
Radek Krejci | a7db970 | 2017-01-20 12:55:14 +0100 | [diff] [blame] | 4512 | mod = lys_get_import_module_ns(info->mod, info->data.yin->ns->value); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4513 | if (!mod) { |
| 4514 | LOGVAL(LYE_INSTMT, vlog_type, vlog_node, info->data.yin->name); |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 4515 | return EXIT_FAILURE; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4516 | } |
| 4517 | |
| 4518 | /* find the extension definition */ |
| 4519 | e = NULL; |
| 4520 | for (i = 0; i < mod->extensions_size; i++) { |
| 4521 | if (ly_strequal(mod->extensions[i].name, info->data.yin->name, 1)) { |
| 4522 | e = &mod->extensions[i]; |
| 4523 | break; |
| 4524 | } |
| 4525 | } |
| 4526 | /* try submodules */ |
| 4527 | for (j = 0; !e && j < mod->inc_size; j++) { |
| 4528 | for (i = 0; i < mod->inc[j].submodule->extensions_size; i++) { |
| 4529 | if (ly_strequal(mod->inc[j].submodule->extensions[i].name, info->data.yin->name, 1)) { |
| 4530 | e = &mod->inc[j].submodule->extensions[i]; |
| 4531 | break; |
| 4532 | } |
| 4533 | } |
| 4534 | } |
| 4535 | if (!e) { |
| 4536 | LOGVAL(LYE_INSTMT, vlog_type, vlog_node, info->data.yin->name); |
| 4537 | return EXIT_FAILURE; |
| 4538 | } |
| 4539 | |
| 4540 | /* we have the extension definition, so now it cannot be forward referenced and error is always fatal */ |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4541 | |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4542 | if (e->plugin && e->plugin->check_position) { |
| 4543 | /* common part - we have plugin with position checking function, use it first */ |
| 4544 | if ((*e->plugin->check_position)(info->parent, info->parent_type, info->substmt)) { |
| 4545 | /* extension is not allowed here */ |
| 4546 | LOGVAL(LYE_INSTMT, vlog_type, vlog_node, e->name); |
| 4547 | return -1; |
| 4548 | } |
| 4549 | } |
| 4550 | |
| 4551 | /* common part, even if there is no plugin */ |
| 4552 | if (e->flags & LYS_YINELEM) { |
| 4553 | value = NULL; |
| 4554 | c_ext = 0; |
| 4555 | LY_TREE_FOR_SAFE(info->data.yin->child, next_yin, yin) { |
| 4556 | if (!yin->ns) { |
| 4557 | /* garbage */ |
| 4558 | lyxml_free(mod->ctx, yin); |
| 4559 | continue; |
| 4560 | } else if (!strcmp(yin->ns->value, LY_NSYIN)) { |
| 4561 | /* standard YANG statements are not expected here */ |
| 4562 | LOGVAL(LYE_INCHILDSTMT, vlog_type, vlog_node, yin->name, info->data.yin->name); |
| 4563 | return -1; |
| 4564 | } else if (yin->ns == info->data.yin->ns && ly_strequal(yin->name, e->argument, 1)) { |
| 4565 | /* we have the extension's argument */ |
| 4566 | if (value) { |
| 4567 | LOGVAL(LYE_TOOMANY, vlog_type, vlog_node, yin->name, info->data.yin->name); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4568 | return -1; |
| 4569 | } |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4570 | value = yin->content; |
| 4571 | yin->content = NULL; |
| 4572 | lyxml_free(mod->ctx, yin); |
| 4573 | } else { |
| 4574 | /* possible extension instance, processed later */ |
| 4575 | c_ext++; |
| 4576 | continue; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4577 | } |
| 4578 | } |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4579 | if (!value) { |
| 4580 | LOGVAL(LYE_MISSCHILDSTMT, vlog_type, vlog_node, e->argument, info->data.yin->name); |
| 4581 | return -1; |
| 4582 | } |
| 4583 | } else if (e->argument) { |
| 4584 | value = lyxml_get_attr(info->data.yin, e->argument, NULL); |
| 4585 | if (!value) { |
| 4586 | LOGVAL(LYE_MISSARG, LY_VLOG_NONE, NULL, e->argument, info->data.yin->name); |
| 4587 | return -1; |
| 4588 | } |
| 4589 | value = lydict_insert(mod->ctx, value, 0); |
| 4590 | } else { |
| 4591 | value = NULL; |
| 4592 | } |
| 4593 | (*ext) = calloc(1, sizeof(struct lys_ext_instance)); |
| 4594 | ((struct lys_ext_instance *)(*ext))->def = e; |
| 4595 | ((struct lys_ext_instance *)(*ext))->arg_value = value; |
| 4596 | ((struct lys_ext_instance *)(*ext))->parent = info->parent; |
| 4597 | ((struct lys_ext_instance *)(*ext))->parent_type = info->parent_type; |
| 4598 | ((struct lys_ext_instance *)(*ext))->substmt = info->substmt; |
| 4599 | ((struct lys_ext_instance *)(*ext))->substmt_index = info->substmt_index; |
| 4600 | |
| 4601 | /* extension instances in extension */ |
| 4602 | if (c_ext == -1) { |
| 4603 | c_ext = 0; |
| 4604 | /* first, we have to count them */ |
| 4605 | LY_TREE_FOR_SAFE(info->data.yin->child, next_yin, yin) { |
| 4606 | if (!yin->ns) { |
| 4607 | /* garbage */ |
| 4608 | lyxml_free(mod->ctx, yin); |
| 4609 | continue; |
| 4610 | } else if (strcmp(yin->ns->value, LY_NSYIN)) { |
| 4611 | /* possible extension instance */ |
| 4612 | c_ext++; |
| 4613 | } else { |
| 4614 | /* unexpected statement */ |
| 4615 | LOGVAL(LYE_INCHILDSTMT, vlog_type, vlog_node, yin->name, info->data.yin->name); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4616 | return -1; |
| 4617 | } |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4618 | } |
| 4619 | } |
| 4620 | if (c_ext) { |
| 4621 | (*ext)->ext = calloc(c_ext, sizeof *(*ext)->ext); |
| 4622 | if (!(*ext)->ext) { |
| 4623 | LOGMEM; |
| 4624 | return -1; |
| 4625 | } |
| 4626 | LY_TREE_FOR_SAFE(info->data.yin->child, next_yin, yin) { |
Radek Krejci | 07d0fb9 | 2017-01-13 14:11:05 +0100 | [diff] [blame] | 4627 | rc = lyp_yin_fill_ext(*ext, LYEXT_PAR_EXTINST, LYEXT_SUBSTMT_SELF, 0, (struct lys_module *)mod, yin, |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 4628 | &(*ext)->ext, (*ext)->ext_size, unres); |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4629 | (*ext)->ext_size++; |
| 4630 | if (rc == -1) { |
| 4631 | return EXIT_FAILURE; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4632 | } |
| 4633 | } |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4634 | } |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4635 | |
| 4636 | /* TODO - lyext_check_result_clb, other than LYEXT_FLAG plugins */ |
| 4637 | |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4638 | } else { |
| 4639 | /* TODO YANG */ |
| 4640 | |
| 4641 | return -1; |
| 4642 | } |
| 4643 | |
| 4644 | return EXIT_SUCCESS; |
| 4645 | } |
| 4646 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4647 | /** |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4648 | * @brief Resolve (find) choice default case. Does not log. |
| 4649 | * |
| 4650 | * @param[in] choic Choice to use. |
| 4651 | * @param[in] dflt Name of the default case. |
| 4652 | * |
| 4653 | * @return Pointer to the default node or NULL. |
| 4654 | */ |
| 4655 | static struct lys_node * |
| 4656 | resolve_choice_dflt(struct lys_node_choice *choic, const char *dflt) |
| 4657 | { |
| 4658 | struct lys_node *child, *ret; |
| 4659 | |
| 4660 | LY_TREE_FOR(choic->child, child) { |
| 4661 | if (child->nodetype == LYS_USES) { |
| 4662 | ret = resolve_choice_dflt((struct lys_node_choice *)child, dflt); |
| 4663 | if (ret) { |
| 4664 | return ret; |
| 4665 | } |
| 4666 | } |
| 4667 | |
| 4668 | 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] | 4669 | | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CHOICE))) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4670 | return child; |
| 4671 | } |
| 4672 | } |
| 4673 | |
| 4674 | return NULL; |
| 4675 | } |
| 4676 | |
| 4677 | /** |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4678 | * @brief Resolve uses, apply augments, refines. Logs directly. |
| 4679 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4680 | * @param[in] uses Uses to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4681 | * @param[in,out] unres List of unresolved items. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4682 | * |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 4683 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4684 | */ |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 4685 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4686 | resolve_uses(struct lys_node_uses *uses, struct unres_schema *unres) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4687 | { |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4688 | struct ly_ctx *ctx = uses->module->ctx; /* shortcut */ |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4689 | struct lys_node *node = NULL, *next, *iter, **refine_nodes = NULL; |
Pavol Vican | 55abd33 | 2016-07-12 15:54:49 +0200 | [diff] [blame] | 4690 | struct lys_node *node_aux, *parent, *tmp; |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4691 | struct lys_node_leaflist *llist; |
| 4692 | struct lys_node_leaf *leaf; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 4693 | struct lys_refine *rfn; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4694 | struct lys_restr *must, **old_must; |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4695 | struct lys_iffeature *iff, **old_iff; |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4696 | int i, j, k, rc; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4697 | uint8_t size, *old_size; |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4698 | unsigned int usize, usize1, usize2; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4699 | |
Michal Vasko | 71e1aa8 | 2015-08-12 12:17:51 +0200 | [diff] [blame] | 4700 | assert(uses->grp); |
Radek Krejci | 6ff885d | 2017-01-03 14:06:22 +0100 | [diff] [blame] | 4701 | |
| 4702 | /* HACK just check that the grouping is resolved |
| 4703 | * - the higher byte in flags is always empty in grouping (no flags there apply to the groupings) |
| 4704 | * so we use it to count unresolved uses inside the grouping */ |
| 4705 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
| 4706 | assert(!((uint8_t*)&uses->grp->flags)[1]); |
| 4707 | #else |
| 4708 | assert(!((uint8_t*)&uses->grp->flags)[0]); |
| 4709 | #endif |
Michal Vasko | 71e1aa8 | 2015-08-12 12:17:51 +0200 | [diff] [blame] | 4710 | |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4711 | if (!uses->grp->child) { |
| 4712 | /* grouping without children, warning was already displayed */ |
| 4713 | return EXIT_SUCCESS; |
| 4714 | } |
| 4715 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4716 | /* copy the data nodes from grouping into the uses context */ |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 4717 | LY_TREE_FOR(uses->grp->child, node_aux) { |
Radek Krejci | 6ff885d | 2017-01-03 14:06:22 +0100 | [diff] [blame] | 4718 | node = lys_node_dup(uses->module, (struct lys_node *)uses, node_aux, unres, 0); |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 4719 | if (!node) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4720 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, uses->grp->name, "uses"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 4721 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Copying data from grouping failed."); |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4722 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4723 | } |
Pavol Vican | 55abd33 | 2016-07-12 15:54:49 +0200 | [diff] [blame] | 4724 | /* test the name of siblings */ |
| 4725 | 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] | 4726 | 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] | 4727 | goto fail; |
Pavol Vican | 55abd33 | 2016-07-12 15:54:49 +0200 | [diff] [blame] | 4728 | } |
| 4729 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4730 | } |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4731 | |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 4732 | /* we managed to copy the grouping, the rest must be possible to resolve */ |
| 4733 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4734 | if (uses->refine_size) { |
| 4735 | refine_nodes = malloc(uses->refine_size * sizeof *refine_nodes); |
| 4736 | if (!refine_nodes) { |
| 4737 | LOGMEM; |
| 4738 | goto fail; |
| 4739 | } |
| 4740 | } |
| 4741 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4742 | /* apply refines */ |
| 4743 | for (i = 0; i < uses->refine_size; i++) { |
| 4744 | rfn = &uses->refine[i]; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 4745 | 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] | 4746 | 1, 0, (const struct lys_node **)&node); |
Michal Vasko | 9bb061b | 2016-02-12 11:00:19 +0100 | [diff] [blame] | 4747 | if (rc || !node) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4748 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine"); |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4749 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4750 | } |
| 4751 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4752 | if (rfn->target_type && !(node->nodetype & rfn->target_type)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4753 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 4754 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Refine substatements not applicable to the target-node."); |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4755 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4756 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4757 | refine_nodes[i] = node; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4758 | |
| 4759 | /* description on any nodetype */ |
| 4760 | if (rfn->dsc) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4761 | lydict_remove(ctx, node->dsc); |
| 4762 | node->dsc = lydict_insert(ctx, rfn->dsc, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4763 | } |
| 4764 | |
| 4765 | /* reference on any nodetype */ |
| 4766 | if (rfn->ref) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4767 | lydict_remove(ctx, node->ref); |
| 4768 | node->ref = lydict_insert(ctx, rfn->ref, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4769 | } |
| 4770 | |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4771 | /* config on any nodetype, |
| 4772 | * in case of notification or rpc/action, the config is not applicable (there is no config status) */ |
| 4773 | if ((rfn->flags & LYS_CONFIG_MASK) && (node->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4774 | node->flags &= ~LYS_CONFIG_MASK; |
| 4775 | node->flags |= (rfn->flags & LYS_CONFIG_MASK); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4776 | } |
| 4777 | |
| 4778 | /* default value ... */ |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4779 | if (rfn->dflt_size) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4780 | if (node->nodetype == LYS_LEAF) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4781 | /* leaf */ |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4782 | leaf = (struct lys_node_leaf *)node; |
| 4783 | |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 4784 | /* replace default value */ |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4785 | lydict_remove(ctx, leaf->dflt); |
| 4786 | leaf->dflt = lydict_insert(ctx, rfn->dflt[0], 0); |
| 4787 | |
| 4788 | /* check the default value */ |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 4789 | if (unres_schema_add_node(leaf->module, unres, &leaf->type, UNRES_TYPE_DFLT, |
| 4790 | (struct lys_node *)(&leaf->dflt)) == -1) { |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4791 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4792 | } |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4793 | } else if (node->nodetype == LYS_LEAFLIST) { |
| 4794 | /* leaf-list */ |
| 4795 | llist = (struct lys_node_leaflist *)node; |
| 4796 | |
| 4797 | /* remove complete set of defaults in target */ |
Radek Krejci | 542ab14 | 2017-01-23 15:57:08 +0100 | [diff] [blame] | 4798 | for (j = 0; j < llist->dflt_size; j++) { |
| 4799 | lydict_remove(ctx, llist->dflt[j]); |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4800 | } |
| 4801 | free(llist->dflt); |
| 4802 | |
| 4803 | /* copy the default set from refine */ |
| 4804 | llist->dflt_size = rfn->dflt_size; |
| 4805 | llist->dflt = malloc(llist->dflt_size * sizeof *llist->dflt); |
Radek Krejci | 542ab14 | 2017-01-23 15:57:08 +0100 | [diff] [blame] | 4806 | for (j = 0; j < llist->dflt_size; j++) { |
| 4807 | llist->dflt[j] = lydict_insert(ctx, rfn->dflt[j], 0); |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4808 | } |
| 4809 | |
| 4810 | /* check default value */ |
Radek Krejci | 542ab14 | 2017-01-23 15:57:08 +0100 | [diff] [blame] | 4811 | for (j = 0; j < llist->dflt_size; j++) { |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 4812 | if (unres_schema_add_node(llist->module, unres, &llist->type, UNRES_TYPE_DFLT, |
Radek Krejci | 542ab14 | 2017-01-23 15:57:08 +0100 | [diff] [blame] | 4813 | (struct lys_node *)(&llist->dflt[j])) == -1) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4814 | goto fail; |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4815 | } |
| 4816 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4817 | } |
| 4818 | } |
| 4819 | |
| 4820 | /* mandatory on leaf, anyxml or choice */ |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 4821 | if (rfn->flags & LYS_MAND_MASK) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4822 | if (node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_CHOICE)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4823 | /* remove current value */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4824 | node->flags &= ~LYS_MAND_MASK; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4825 | |
| 4826 | /* set new value */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4827 | node->flags |= (rfn->flags & LYS_MAND_MASK); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4828 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4829 | if (rfn->flags & LYS_MAND_TRUE) { |
| 4830 | /* check if node has default value */ |
| 4831 | if ((node->nodetype & LYS_LEAF) && ((struct lys_node_leaf *)node)->dflt) { |
| 4832 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The \"mandatory\" statement is forbidden on leaf with \"default\"."); |
| 4833 | goto fail; |
| 4834 | } |
| 4835 | if ((node->nodetype & LYS_CHOICE) && ((struct lys_node_choice *)node)->dflt) { |
| 4836 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The \"mandatory\" statement is forbidden on choices with \"default\"."); |
| 4837 | goto fail; |
| 4838 | } |
| 4839 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4840 | } |
| 4841 | |
| 4842 | /* presence on container */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4843 | if ((node->nodetype & LYS_CONTAINER) && rfn->mod.presence) { |
| 4844 | lydict_remove(ctx, ((struct lys_node_container *)node)->presence); |
| 4845 | ((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] | 4846 | } |
| 4847 | |
| 4848 | /* min/max-elements on list or leaf-list */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4849 | if (node->nodetype == LYS_LIST) { |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 4850 | if (rfn->flags & LYS_RFN_MINSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4851 | ((struct lys_node_list *)node)->min = rfn->mod.list.min; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4852 | } |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 4853 | if (rfn->flags & LYS_RFN_MAXSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4854 | ((struct lys_node_list *)node)->max = rfn->mod.list.max; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4855 | } |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4856 | } else if (node->nodetype == LYS_LEAFLIST) { |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 4857 | if (rfn->flags & LYS_RFN_MINSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4858 | ((struct lys_node_leaflist *)node)->min = rfn->mod.list.min; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4859 | } |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 4860 | if (rfn->flags & LYS_RFN_MAXSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4861 | ((struct lys_node_leaflist *)node)->max = rfn->mod.list.max; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4862 | } |
| 4863 | } |
| 4864 | |
| 4865 | /* must in leaf, leaf-list, list, container or anyxml */ |
| 4866 | if (rfn->must_size) { |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4867 | switch (node->nodetype) { |
| 4868 | case LYS_LEAF: |
| 4869 | old_size = &((struct lys_node_leaf *)node)->must_size; |
| 4870 | old_must = &((struct lys_node_leaf *)node)->must; |
| 4871 | break; |
| 4872 | case LYS_LEAFLIST: |
| 4873 | old_size = &((struct lys_node_leaflist *)node)->must_size; |
| 4874 | old_must = &((struct lys_node_leaflist *)node)->must; |
| 4875 | break; |
| 4876 | case LYS_LIST: |
| 4877 | old_size = &((struct lys_node_list *)node)->must_size; |
| 4878 | old_must = &((struct lys_node_list *)node)->must; |
| 4879 | break; |
| 4880 | case LYS_CONTAINER: |
| 4881 | old_size = &((struct lys_node_container *)node)->must_size; |
| 4882 | old_must = &((struct lys_node_container *)node)->must; |
| 4883 | break; |
| 4884 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4885 | case LYS_ANYDATA: |
| 4886 | old_size = &((struct lys_node_anydata *)node)->must_size; |
| 4887 | old_must = &((struct lys_node_anydata *)node)->must; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4888 | break; |
| 4889 | default: |
| 4890 | LOGINT; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4891 | goto fail; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4892 | } |
| 4893 | |
| 4894 | size = *old_size + rfn->must_size; |
| 4895 | must = realloc(*old_must, size * sizeof *rfn->must); |
| 4896 | if (!must) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4897 | LOGMEM; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4898 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4899 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4900 | for (k = 0, j = *old_size; k < rfn->must_size; k++, j++) { |
Radek Krejci | 77f22b2 | 2017-01-17 15:23:03 +0100 | [diff] [blame] | 4901 | must[j].ext_size = rfn[k].ext_size; |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 4902 | lys_ext_dup(ctx, rfn->must[k].ext, rfn->must[k].ext_size, &rfn->must[k], LYEXT_PAR_RESTR, |
| 4903 | &must[j].ext, unres); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4904 | must[j].expr = lydict_insert(ctx, rfn->must[k].expr, 0); |
| 4905 | must[j].dsc = lydict_insert(ctx, rfn->must[k].dsc, 0); |
| 4906 | must[j].ref = lydict_insert(ctx, rfn->must[k].ref, 0); |
| 4907 | must[j].eapptag = lydict_insert(ctx, rfn->must[k].eapptag, 0); |
| 4908 | must[j].emsg = lydict_insert(ctx, rfn->must[k].emsg, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4909 | } |
| 4910 | |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4911 | *old_must = must; |
| 4912 | *old_size = size; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 4913 | |
| 4914 | /* check XPath dependencies again */ |
| 4915 | if (unres_schema_add_node(node->module, unres, node, UNRES_XPATH, NULL) == -1) { |
| 4916 | goto fail; |
| 4917 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4918 | } |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4919 | |
| 4920 | /* if-feature in leaf, leaf-list, list, container or anyxml */ |
| 4921 | if (rfn->iffeature_size) { |
| 4922 | old_size = &node->iffeature_size; |
| 4923 | old_iff = &node->iffeature; |
| 4924 | |
| 4925 | size = *old_size + rfn->iffeature_size; |
| 4926 | iff = realloc(*old_iff, size * sizeof *rfn->iffeature); |
| 4927 | if (!iff) { |
| 4928 | LOGMEM; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4929 | goto fail; |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4930 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4931 | for (k = 0, j = *old_size; k < rfn->iffeature_size; k++, j++) { |
| 4932 | resolve_iffeature_getsizes(&rfn->iffeature[k], &usize1, &usize2); |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4933 | if (usize1) { |
| 4934 | /* there is something to duplicate */ |
| 4935 | /* duplicate compiled expression */ |
| 4936 | usize = (usize1 / 4) + (usize1 % 4) ? 1 : 0; |
| 4937 | iff[j].expr = malloc(usize * sizeof *iff[j].expr); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4938 | 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] | 4939 | |
| 4940 | /* duplicate list of feature pointers */ |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4941 | iff[j].features = malloc(usize2 * sizeof *iff[k].features); |
| 4942 | 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] | 4943 | } |
| 4944 | } |
| 4945 | |
| 4946 | *old_iff = iff; |
| 4947 | *old_size = size; |
| 4948 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4949 | } |
| 4950 | |
| 4951 | /* apply augments */ |
| 4952 | for (i = 0; i < uses->augment_size; i++) { |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 4953 | rc = resolve_augment(&uses->augment[i], uses->child, unres); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4954 | if (rc) { |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4955 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4956 | } |
| 4957 | } |
| 4958 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4959 | /* check refines */ |
| 4960 | for (i = 0; i < uses->refine_size; i++) { |
| 4961 | node = refine_nodes[i]; |
| 4962 | rfn = &uses->refine[i]; |
| 4963 | |
| 4964 | /* config on any nodetype */ |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4965 | if ((rfn->flags & LYS_CONFIG_MASK) && (node->flags & LYS_CONFIG_MASK)) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4966 | 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] | 4967 | if (parent && parent->nodetype != LYS_GROUPING && (parent->flags & LYS_CONFIG_MASK) && |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4968 | ((parent->flags & LYS_CONFIG_MASK) != (rfn->flags & LYS_CONFIG_MASK)) && |
| 4969 | (rfn->flags & LYS_CONFIG_W)) { |
| 4970 | /* setting config true under config false is prohibited */ |
| 4971 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, "config", "refine"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 4972 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4973 | "changing config from 'false' to 'true' is prohibited while " |
| 4974 | "the target's parent is still config 'false'."); |
| 4975 | goto fail; |
| 4976 | } |
| 4977 | |
| 4978 | /* inherit config change to the target children */ |
| 4979 | LY_TREE_DFS_BEGIN(node->child, next, iter) { |
| 4980 | if (rfn->flags & LYS_CONFIG_W) { |
| 4981 | if (iter->flags & LYS_CONFIG_SET) { |
| 4982 | /* config is set explicitely, go to next sibling */ |
| 4983 | next = NULL; |
| 4984 | goto nextsibling; |
| 4985 | } |
| 4986 | } else { /* LYS_CONFIG_R */ |
| 4987 | if ((iter->flags & LYS_CONFIG_SET) && (iter->flags & LYS_CONFIG_W)) { |
| 4988 | /* error - we would have config data under status data */ |
| 4989 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, "config", "refine"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 4990 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4991 | "changing config from 'true' to 'false' is prohibited while the target " |
| 4992 | "has still a children with explicit config 'true'."); |
| 4993 | goto fail; |
| 4994 | } |
| 4995 | } |
| 4996 | /* change config */ |
| 4997 | iter->flags &= ~LYS_CONFIG_MASK; |
| 4998 | iter->flags |= (rfn->flags & LYS_CONFIG_MASK); |
| 4999 | |
| 5000 | /* select next iter - modified LY_TREE_DFS_END */ |
| 5001 | if (iter->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
| 5002 | next = NULL; |
| 5003 | } else { |
| 5004 | next = iter->child; |
| 5005 | } |
| 5006 | nextsibling: |
| 5007 | if (!next) { |
| 5008 | /* try siblings */ |
| 5009 | next = iter->next; |
| 5010 | } |
| 5011 | while (!next) { |
| 5012 | /* parent is already processed, go to its sibling */ |
| 5013 | iter = lys_parent(iter); |
| 5014 | |
| 5015 | /* no siblings, go back through parents */ |
| 5016 | if (iter == node) { |
| 5017 | /* we are done, no next element to process */ |
| 5018 | break; |
| 5019 | } |
| 5020 | next = iter->next; |
| 5021 | } |
| 5022 | } |
| 5023 | } |
| 5024 | |
| 5025 | /* default value */ |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 5026 | if (rfn->dflt_size) { |
| 5027 | if (node->nodetype == LYS_CHOICE) { |
| 5028 | /* choice */ |
| 5029 | ((struct lys_node_choice *)node)->dflt = resolve_choice_dflt((struct lys_node_choice *)node, |
| 5030 | rfn->dflt[0]); |
| 5031 | if (!((struct lys_node_choice *)node)->dflt) { |
| 5032 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->dflt[0], "default"); |
| 5033 | goto fail; |
| 5034 | } |
| 5035 | if (lyp_check_mandatory_choice(node)) { |
| 5036 | goto fail; |
| 5037 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5038 | } |
| 5039 | } |
| 5040 | |
| 5041 | /* min/max-elements on list or leaf-list */ |
| 5042 | if (node->nodetype == LYS_LIST) { |
| 5043 | if (((struct lys_node_list *)node)->min > ((struct lys_node_list *)node)->max) { |
| 5044 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid value \"%d\" of \"%s\".", rfn->mod.list.min, "min-elements"); |
| 5045 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"min-elements\" is bigger than \"max-elements\"."); |
| 5046 | goto fail; |
| 5047 | } |
| 5048 | } else if (node->nodetype == LYS_LEAFLIST) { |
| 5049 | if (((struct lys_node_leaflist *)node)->min > ((struct lys_node_leaflist *)node)->max) { |
| 5050 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid value \"%d\" of \"%s\".", rfn->mod.list.min, "min-elements"); |
| 5051 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"min-elements\" is bigger than \"max-elements\"."); |
| 5052 | goto fail; |
| 5053 | } |
| 5054 | } |
| 5055 | |
| 5056 | /* additional checks */ |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 5057 | /* default value with mandatory/min-elements */ |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5058 | if (node->nodetype == LYS_LEAFLIST) { |
| 5059 | llist = (struct lys_node_leaflist *)node; |
| 5060 | if (llist->dflt_size && llist->min) { |
| 5061 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, rfn->dflt_size ? "default" : "min-elements", "refine"); |
| 5062 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, |
| 5063 | "The \"min-elements\" statement with non-zero value is forbidden on leaf-lists with the \"default\" statement."); |
| 5064 | goto fail; |
| 5065 | } |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 5066 | } else if (node->nodetype == LYS_LEAF) { |
| 5067 | leaf = (struct lys_node_leaf *)node; |
| 5068 | if (leaf->dflt && (leaf->flags & LYS_MAND_TRUE)) { |
| 5069 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, rfn->dflt_size ? "default" : "mandatory", "refine"); |
| 5070 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, |
| 5071 | "The \"mandatory\" statement is forbidden on leafs with the \"default\" statement."); |
| 5072 | goto fail; |
| 5073 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5074 | } |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 5075 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5076 | /* 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] | 5077 | if ((rfn->flags & LYS_MAND_TRUE) || rfn->mod.list.min) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5078 | for (parent = node->parent; |
| 5079 | parent && !(parent->nodetype & (LYS_CHOICE | LYS_GROUPING | LYS_ACTION | LYS_USES)); |
| 5080 | parent = parent->parent) { |
| 5081 | if (parent->nodetype == LYS_CONTAINER && ((struct lys_node_container *)parent)->presence) { |
| 5082 | /* stop also on presence containers */ |
| 5083 | break; |
| 5084 | } |
| 5085 | } |
| 5086 | /* and if it is a choice with the default case, check it for presence of a mandatory node in it */ |
| 5087 | if (parent && parent->nodetype == LYS_CHOICE && ((struct lys_node_choice *)parent)->dflt) { |
| 5088 | if (lyp_check_mandatory_choice(parent)) { |
| 5089 | goto fail; |
| 5090 | } |
| 5091 | } |
| 5092 | } |
| 5093 | } |
| 5094 | free(refine_nodes); |
| 5095 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5096 | return EXIT_SUCCESS; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 5097 | |
| 5098 | fail: |
| 5099 | LY_TREE_FOR_SAFE(uses->child, next, iter) { |
| 5100 | lys_node_free(iter, NULL, 0); |
| 5101 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5102 | free(refine_nodes); |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 5103 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5104 | } |
| 5105 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5106 | static int |
| 5107 | identity_backlink_update(struct lys_ident *der, struct lys_ident *base) |
| 5108 | { |
| 5109 | int i; |
| 5110 | |
| 5111 | assert(der && base); |
| 5112 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5113 | if (!base->der) { |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5114 | /* create a set for backlinks if it does not exist */ |
| 5115 | base->der = ly_set_new(); |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5116 | } |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5117 | /* store backlink */ |
| 5118 | ly_set_add(base->der, der, LY_SET_OPT_USEASLIST); |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5119 | |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5120 | /* do it recursively */ |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5121 | for (i = 0; i < base->base_size; i++) { |
| 5122 | if (identity_backlink_update(der, base->base[i])) { |
| 5123 | return EXIT_FAILURE; |
| 5124 | } |
| 5125 | } |
| 5126 | |
| 5127 | return EXIT_SUCCESS; |
| 5128 | } |
| 5129 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5130 | /** |
| 5131 | * @brief Resolve base identity recursively. Does not log. |
| 5132 | * |
| 5133 | * @param[in] module Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5134 | * @param[in] ident Identity to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5135 | * @param[in] basename Base name of the identity. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5136 | * @param[out] ret Pointer to the resolved identity. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5137 | * |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5138 | * @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] | 5139 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5140 | static int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 5141 | 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] | 5142 | struct unres_schema *unres, struct lys_ident **ret) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5143 | { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5144 | uint32_t i, j; |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5145 | struct lys_ident *base = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5146 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5147 | assert(ret); |
| 5148 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5149 | /* search module */ |
| 5150 | for (i = 0; i < module->ident_size; i++) { |
| 5151 | if (!strcmp(basename, module->ident[i].name)) { |
| 5152 | |
| 5153 | if (!ident) { |
| 5154 | /* just search for type, so do not modify anything, just return |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5155 | * the base identity pointer */ |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5156 | *ret = &module->ident[i]; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5157 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5158 | } |
| 5159 | |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5160 | base = &module->ident[i]; |
| 5161 | goto matchfound; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5162 | } |
| 5163 | } |
| 5164 | |
| 5165 | /* search submodules */ |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5166 | for (j = 0; j < module->inc_size && module->inc[j].submodule; j++) { |
| 5167 | for (i = 0; i < module->inc[j].submodule->ident_size; i++) { |
| 5168 | if (!strcmp(basename, module->inc[j].submodule->ident[i].name)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5169 | |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5170 | if (!ident) { |
| 5171 | *ret = &module->inc[j].submodule->ident[i]; |
| 5172 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5173 | } |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5174 | |
| 5175 | base = &module->inc[j].submodule->ident[i]; |
| 5176 | goto matchfound; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5177 | } |
| 5178 | } |
| 5179 | } |
| 5180 | |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5181 | matchfound: |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5182 | /* we found it somewhere */ |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 5183 | if (base) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5184 | /* is it already completely resolved? */ |
| 5185 | for (i = 0; i < unres->count; i++) { |
Radek Krejci | ba7b1cc | 2016-08-08 13:44:43 +0200 | [diff] [blame] | 5186 | if ((unres->item[i] == base) && (unres->type[i] == UNRES_IDENT)) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5187 | /* identity found, but not yet resolved, so do not return it in *res and try it again later */ |
| 5188 | |
| 5189 | /* simple check for circular reference, |
| 5190 | * the complete check is done as a side effect of using only completely |
| 5191 | * resolved identities (previous check of unres content) */ |
| 5192 | if (ly_strequal((const char *)unres->str_snode[i], ident->name, 1)) { |
| 5193 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, basename, "base"); |
| 5194 | 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] | 5195 | return -1; |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5196 | } |
| 5197 | |
Radek Krejci | 06f64ed | 2016-08-15 11:07:44 +0200 | [diff] [blame] | 5198 | return EXIT_FAILURE; |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 5199 | } |
| 5200 | } |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5201 | |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 5202 | /* checks done, store the result */ |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5203 | *ret = base; |
Pavol Vican | fdab9f9 | 2016-09-07 15:23:27 +0200 | [diff] [blame] | 5204 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5205 | } |
| 5206 | |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5207 | /* base not found (maybe a forward reference) */ |
| 5208 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5209 | } |
| 5210 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5211 | /** |
| 5212 | * @brief Resolve base identity. Logs directly. |
| 5213 | * |
| 5214 | * @param[in] module Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5215 | * @param[in] ident Identity to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5216 | * @param[in] basename Base name of the identity. |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 5217 | * @param[in] parent Either "type" or "identity". |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5218 | * @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] | 5219 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5220 | * @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] | 5221 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5222 | static int |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5223 | 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] | 5224 | struct lys_type *type, struct unres_schema *unres) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5225 | { |
| 5226 | const char *name; |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5227 | int mod_name_len = 0, rc; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5228 | struct lys_ident *target, **ret; |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 5229 | uint16_t flags; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5230 | struct lys_module *mod; |
| 5231 | |
| 5232 | assert((ident && !type) || (!ident && type)); |
| 5233 | |
| 5234 | if (!type) { |
| 5235 | /* have ident to resolve */ |
| 5236 | ret = ⌖ |
| 5237 | flags = ident->flags; |
| 5238 | mod = ident->module; |
| 5239 | } else { |
| 5240 | /* have type to fill */ |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5241 | ++type->info.ident.count; |
| 5242 | type->info.ident.ref = ly_realloc(type->info.ident.ref, type->info.ident.count * sizeof *type->info.ident.ref); |
| 5243 | if (!type->info.ident.ref) { |
| 5244 | LOGMEM; |
| 5245 | return -1; |
| 5246 | } |
| 5247 | |
| 5248 | ret = &type->info.ident.ref[type->info.ident.count - 1]; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5249 | flags = type->parent->flags; |
| 5250 | mod = type->parent->module; |
| 5251 | } |
Michal Vasko | f200600 | 2016-04-21 16:28:15 +0200 | [diff] [blame] | 5252 | *ret = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5253 | |
| 5254 | /* search for the base identity */ |
| 5255 | name = strchr(basename, ':'); |
| 5256 | if (name) { |
| 5257 | /* set name to correct position after colon */ |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 5258 | mod_name_len = name - basename; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5259 | name++; |
| 5260 | |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 5261 | 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] | 5262 | /* prefix refers to the current module, ignore it */ |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 5263 | mod_name_len = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5264 | } |
| 5265 | } else { |
| 5266 | name = basename; |
| 5267 | } |
| 5268 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 5269 | /* get module where to search */ |
| 5270 | module = lys_get_import_module(module, NULL, 0, mod_name_len ? basename : NULL, mod_name_len); |
| 5271 | if (!module) { |
| 5272 | /* identity refers unknown data model */ |
Radek Krejci | 02a0499 | 2016-03-17 16:06:37 +0100 | [diff] [blame] | 5273 | LOGVAL(LYE_INMOD, LY_VLOG_NONE, NULL, basename); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 5274 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5275 | } |
| 5276 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 5277 | /* search in the identified module ... */ |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5278 | rc = resolve_base_ident_sub(module, ident, name, unres, ret); |
| 5279 | if (!rc) { |
| 5280 | assert(*ret); |
| 5281 | |
| 5282 | /* check status */ |
| 5283 | if (lyp_check_status(flags, mod, ident ? ident->name : "of type", |
| 5284 | (*ret)->flags, (*ret)->module, (*ret)->name, NULL)) { |
| 5285 | rc = -1; |
Pavol Vican | fdab9f9 | 2016-09-07 15:23:27 +0200 | [diff] [blame] | 5286 | } else { |
| 5287 | if (ident) { |
| 5288 | ident->base[ident->base_size++] = *ret; |
| 5289 | |
| 5290 | /* maintain backlinks to the derived identities */ |
| 5291 | rc = identity_backlink_update(ident, *ret) ? -1 : EXIT_SUCCESS; |
| 5292 | } |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5293 | } |
| 5294 | } else if (rc == EXIT_FAILURE) { |
| 5295 | LOGVAL(LYE_INRESOLV, LY_VLOG_NONE, NULL, parent, basename); |
Pavol Vican | 053a288 | 2016-09-05 14:40:33 +0200 | [diff] [blame] | 5296 | if (type) { |
| 5297 | --type->info.ident.count; |
| 5298 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5299 | } |
| 5300 | |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5301 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5302 | } |
| 5303 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5304 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 5305 | * @brief Resolve JSON data format identityref. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5306 | * |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5307 | * @param[in] type Identityref type. |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 5308 | * @param[in] ident_name Identityref name. |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 5309 | * @param[in] node Node where the identityref is being resolved |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5310 | * |
| 5311 | * @return Pointer to the identity resolvent, NULL on error. |
| 5312 | */ |
Radek Krejci | a52656e | 2015-08-05 13:41:50 +0200 | [diff] [blame] | 5313 | struct lys_ident * |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5314 | 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] | 5315 | { |
Radek Krejci | 639491e | 2016-12-05 13:30:42 +0100 | [diff] [blame] | 5316 | const char *mod_name, *name, *mod_name_iter; |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5317 | int mod_name_len, rc, i; |
| 5318 | unsigned int u; |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5319 | struct lys_ident *der, *cur; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5320 | |
Radek Krejci | bb1ce0f | 2016-12-05 13:24:33 +0100 | [diff] [blame] | 5321 | assert(type && ident_name && node); |
| 5322 | |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5323 | if (!type || (!type->info.ident.count && !type->der) || !ident_name) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5324 | return NULL; |
| 5325 | } |
| 5326 | |
Michal Vasko | c633ca0 | 2015-08-21 14:03:51 +0200 | [diff] [blame] | 5327 | 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] | 5328 | if (rc < 1) { |
Radek Krejci | bb1ce0f | 2016-12-05 13:24:33 +0100 | [diff] [blame] | 5329 | 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] | 5330 | return NULL; |
Michal Vasko | b43bb3c | 2016-03-17 15:00:27 +0100 | [diff] [blame] | 5331 | } else if (rc < (signed)strlen(ident_name)) { |
Radek Krejci | bb1ce0f | 2016-12-05 13:24:33 +0100 | [diff] [blame] | 5332 | 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] | 5333 | return NULL; |
| 5334 | } |
Radek Krejci | f32c5f6 | 2016-12-05 09:27:38 +0100 | [diff] [blame] | 5335 | if (!mod_name) { |
| 5336 | /* no prefix, identity must be defined in the same module as node */ |
Radek Krejci | 639491e | 2016-12-05 13:30:42 +0100 | [diff] [blame] | 5337 | mod_name = lys_main_module(node->schema->module)->name; |
Radek Krejci | f32c5f6 | 2016-12-05 09:27:38 +0100 | [diff] [blame] | 5338 | mod_name_len = strlen(mod_name); |
| 5339 | } |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 5340 | |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5341 | /* go through all the bases in all the derived types */ |
| 5342 | while (type->der) { |
| 5343 | for (i = 0; i < type->info.ident.count; ++i) { |
| 5344 | cur = type->info.ident.ref[i]; |
Radek Krejci | 639491e | 2016-12-05 13:30:42 +0100 | [diff] [blame] | 5345 | mod_name_iter = lys_main_module(cur->module)->name; |
Radek Krejci | f32c5f6 | 2016-12-05 09:27:38 +0100 | [diff] [blame] | 5346 | if (!strcmp(cur->name, name) && |
Radek Krejci | 639491e | 2016-12-05 13:30:42 +0100 | [diff] [blame] | 5347 | !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] | 5348 | goto match; |
| 5349 | } |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 5350 | |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5351 | if (cur->der) { |
| 5352 | /* there are also some derived identities */ |
| 5353 | for (u = 0; u < cur->der->number; u++) { |
| 5354 | der = (struct lys_ident *)cur->der->set.g[u]; /* shortcut */ |
Radek Krejci | 639491e | 2016-12-05 13:30:42 +0100 | [diff] [blame] | 5355 | mod_name_iter = lys_main_module(der->module)->name; |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5356 | if (!strcmp(der->name, name) && |
Radek Krejci | 639491e | 2016-12-05 13:30:42 +0100 | [diff] [blame] | 5357 | !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] | 5358 | /* we have match */ |
| 5359 | cur = der; |
| 5360 | goto match; |
| 5361 | } |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5362 | } |
| 5363 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5364 | } |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5365 | type = &type->der->type; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5366 | } |
| 5367 | |
Radek Krejci | bb1ce0f | 2016-12-05 13:24:33 +0100 | [diff] [blame] | 5368 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYD, node, "identityref", ident_name); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5369 | return NULL; |
Radek Krejci | f1ee2e2 | 2016-08-02 16:36:48 +0200 | [diff] [blame] | 5370 | |
| 5371 | match: |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5372 | for (i = 0; i < cur->iffeature_size; i++) { |
| 5373 | if (!resolve_iffeature(&cur->iffeature[i])) { |
Radek Krejci | bb1ce0f | 2016-12-05 13:24:33 +0100 | [diff] [blame] | 5374 | LOGVAL(LYE_INVAL, LY_VLOG_LYD, node, cur->name, node->schema->name); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 5375 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Identity \"%s\" is disabled by its if-feature condition.", cur->name); |
Radek Krejci | f1ee2e2 | 2016-08-02 16:36:48 +0200 | [diff] [blame] | 5376 | return NULL; |
| 5377 | } |
| 5378 | } |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5379 | return cur; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5380 | } |
| 5381 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5382 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5383 | * @brief Resolve unresolved uses. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5384 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5385 | * @param[in] uses Uses to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5386 | * @param[in] unres Specific unres item. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5387 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5388 | * @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] | 5389 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5390 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5391 | 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] | 5392 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5393 | int rc; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5394 | struct lys_node *par_grp; |
Michal Vasko | e91afce | 2015-08-12 12:21:00 +0200 | [diff] [blame] | 5395 | |
Radek Krejci | 6ff885d | 2017-01-03 14:06:22 +0100 | [diff] [blame] | 5396 | /* HACK: when a grouping has uses inside, all such uses have to be resolved before the grouping itself is used |
| 5397 | * in some uses. When we see such a uses, the grouping's higher byte of the flags member (not used in |
| 5398 | * grouping) is used to store number of so far unresolved uses. The grouping cannot be used unless this |
| 5399 | * counter is decreased back to 0. To remember that the uses already increased grouping's counter, the |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5400 | * LYS_USESGRP flag is used. */ |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 5401 | 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] | 5402 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5403 | if (!uses->grp) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 5404 | rc = resolve_uses_schema_nodeid(uses->name, (const struct lys_node *)uses, (const struct lys_node_grp **)&uses->grp); |
| 5405 | if (rc == -1) { |
Michal Vasko | 92981a6 | 2016-10-14 10:25:16 +0200 | [diff] [blame] | 5406 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 5407 | return -1; |
| 5408 | } else if (rc > 0) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5409 | 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] | 5410 | return -1; |
| 5411 | } else if (!uses->grp) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5412 | if (par_grp && !(uses->flags & LYS_USESGRP)) { |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 5413 | /* hack - in contrast to lys_node, lys_node_grp has bigger nacm field |
| 5414 | * (and smaller flags - it uses only a limited set of flags) |
| 5415 | */ |
Radek Krejci | 6ff885d | 2017-01-03 14:06:22 +0100 | [diff] [blame] | 5416 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
| 5417 | ((uint8_t*)&((struct lys_node_grp *)par_grp)->flags)[1]++; |
| 5418 | #else |
| 5419 | ((uint8_t*)&((struct lys_node_grp *)par_grp)->flags)[0]++; |
| 5420 | #endif |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5421 | uses->flags |= LYS_USESGRP; |
Michal Vasko | 407f1bb | 2015-09-23 15:51:07 +0200 | [diff] [blame] | 5422 | } |
Michal Vasko | 92981a6 | 2016-10-14 10:25:16 +0200 | [diff] [blame] | 5423 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 5424 | return EXIT_FAILURE; |
Michal Vasko | 12e3084 | 2015-08-04 11:54:00 +0200 | [diff] [blame] | 5425 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5426 | } |
| 5427 | |
Radek Krejci | 6ff885d | 2017-01-03 14:06:22 +0100 | [diff] [blame] | 5428 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
| 5429 | if (((uint8_t*)&uses->grp->flags)[1]) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5430 | if (par_grp && !(uses->flags & LYS_USESGRP)) { |
Radek Krejci | 6ff885d | 2017-01-03 14:06:22 +0100 | [diff] [blame] | 5431 | ((uint8_t*)&((struct lys_node_grp *)par_grp)->flags)[1]++; |
| 5432 | #else |
| 5433 | if (((uint8_t*)&uses->grp->flags)[0]) { |
| 5434 | if (par_grp && !(uses->flags & LYS_USESGRP)) { |
| 5435 | ((uint8_t*)&((struct lys_node_grp *)par_grp)->flags)[0]++; |
| 5436 | #endif |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5437 | uses->flags |= LYS_USESGRP; |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 5438 | } else { |
| 5439 | /* instantiate grouping only when it is completely resolved */ |
| 5440 | uses->grp = NULL; |
Michal Vasko | 407f1bb | 2015-09-23 15:51:07 +0200 | [diff] [blame] | 5441 | } |
Michal Vasko | 92981a6 | 2016-10-14 10:25:16 +0200 | [diff] [blame] | 5442 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5443 | return EXIT_FAILURE; |
| 5444 | } |
| 5445 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5446 | rc = resolve_uses(uses, unres); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5447 | if (!rc) { |
| 5448 | /* decrease unres count only if not first try */ |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5449 | if (par_grp && (uses->flags & LYS_USESGRP)) { |
Radek Krejci | 6ff885d | 2017-01-03 14:06:22 +0100 | [diff] [blame] | 5450 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
| 5451 | if (!((uint8_t*)&((struct lys_node_grp *)par_grp)->flags)[1]) {; |
| 5452 | #else |
| 5453 | if (!((uint8_t*)&((struct lys_node_grp *)par_grp)->flags)[0]) {; |
| 5454 | #endif |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5455 | LOGINT; |
| 5456 | return -1; |
| 5457 | } |
Radek Krejci | 6ff885d | 2017-01-03 14:06:22 +0100 | [diff] [blame] | 5458 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
| 5459 | ((uint8_t*)&((struct lys_node_grp *)par_grp)->flags)[1]--; |
| 5460 | #else |
| 5461 | ((uint8_t*)&((struct lys_node_grp *)par_grp)->flags)[0]--; |
| 5462 | #endif |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5463 | uses->flags &= ~LYS_USESGRP; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5464 | } |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5465 | |
| 5466 | /* check status */ |
Radek Krejci | c655602 | 2016-01-27 15:16:45 +0100 | [diff] [blame] | 5467 | if (lyp_check_status(uses->flags, uses->module, "of uses", |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 5468 | uses->grp->flags, uses->grp->module, uses->grp->name, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5469 | (struct lys_node *)uses)) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5470 | return -1; |
| 5471 | } |
| 5472 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5473 | return EXIT_SUCCESS; |
| 5474 | } |
| 5475 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5476 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5477 | } |
| 5478 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5479 | /** |
Michal Vasko | 9957e59 | 2015-08-17 15:04:09 +0200 | [diff] [blame] | 5480 | * @brief Resolve list keys. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5481 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5482 | * @param[in] list List to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5483 | * @param[in] keys_str Keys node value. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5484 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5485 | * @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] | 5486 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5487 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5488 | resolve_list_keys(struct lys_node_list *list, const char *keys_str) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5489 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5490 | int i, len, rc; |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 5491 | const char *value; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5492 | |
| 5493 | for (i = 0; i < list->keys_size; ++i) { |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 5494 | if (!list->child) { |
| 5495 | /* no child, possible forward reference */ |
| 5496 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, list, "list keys", keys_str); |
| 5497 | return EXIT_FAILURE; |
| 5498 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5499 | /* get the key name */ |
| 5500 | if ((value = strpbrk(keys_str, " \t\n"))) { |
| 5501 | len = value - keys_str; |
| 5502 | while (isspace(value[0])) { |
| 5503 | value++; |
| 5504 | } |
| 5505 | } else { |
| 5506 | len = strlen(keys_str); |
| 5507 | } |
| 5508 | |
Michal Vasko | 0f99d3e | 2017-01-10 10:50:40 +0100 | [diff] [blame] | 5509 | rc = lys_get_data_sibling(lys_node_module((struct lys_node *)list), list->child, keys_str, len, LYS_LEAF, |
| 5510 | (const struct lys_node **)&list->keys[i]); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5511 | if (rc) { |
Michal Vasko | 7a55bea | 2016-05-02 14:51:20 +0200 | [diff] [blame] | 5512 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, list, "list keys", keys_str); |
| 5513 | return EXIT_FAILURE; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5514 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5515 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5516 | if (check_key(list, i, keys_str, len)) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5517 | /* check_key logs */ |
| 5518 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5519 | } |
| 5520 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5521 | /* check status */ |
Radek Krejci | c655602 | 2016-01-27 15:16:45 +0100 | [diff] [blame] | 5522 | if (lyp_check_status(list->flags, list->module, list->name, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5523 | list->keys[i]->flags, list->keys[i]->module, list->keys[i]->name, |
| 5524 | (struct lys_node *)list->keys[i])) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5525 | return -1; |
| 5526 | } |
| 5527 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5528 | /* prepare for next iteration */ |
| 5529 | while (value && isspace(value[0])) { |
| 5530 | value++; |
| 5531 | } |
| 5532 | keys_str = value; |
| 5533 | } |
| 5534 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5535 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5536 | } |
| 5537 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5538 | /** |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5539 | * @brief Resolve (check) all must conditions of \p node. |
| 5540 | * Logs directly. |
| 5541 | * |
| 5542 | * @param[in] node Data node with optional must statements. |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5543 | * @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] | 5544 | * |
| 5545 | * @return EXIT_SUCCESS on pass, EXIT_FAILURE on fail, -1 on error. |
| 5546 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5547 | static int |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5548 | resolve_must(struct lyd_node *node, int inout_parent, int ignore_fail) |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5549 | { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 5550 | int node_flags; |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5551 | uint8_t i, must_size; |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5552 | struct lys_node *schema; |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5553 | struct lys_restr *must; |
| 5554 | struct lyxp_set set; |
| 5555 | |
| 5556 | assert(node); |
| 5557 | memset(&set, 0, sizeof set); |
| 5558 | |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5559 | if (inout_parent) { |
| 5560 | for (schema = lys_parent(node->schema); |
| 5561 | schema && (schema->nodetype & (LYS_CHOICE | LYS_CASE | LYS_USES)); |
| 5562 | schema = lys_parent(schema)); |
| 5563 | if (!schema || !(schema->nodetype & (LYS_INPUT | LYS_OUTPUT))) { |
| 5564 | LOGINT; |
| 5565 | return -1; |
| 5566 | } |
| 5567 | must_size = ((struct lys_node_inout *)schema)->must_size; |
| 5568 | must = ((struct lys_node_inout *)schema)->must; |
| 5569 | |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 5570 | node_flags = schema->flags; |
| 5571 | |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5572 | /* context node is the RPC/action */ |
| 5573 | node = node->parent; |
| 5574 | if (!(node->schema->nodetype & (LYS_RPC | LYS_ACTION))) { |
| 5575 | LOGINT; |
| 5576 | return -1; |
| 5577 | } |
| 5578 | } else { |
| 5579 | switch (node->schema->nodetype) { |
| 5580 | case LYS_CONTAINER: |
| 5581 | must_size = ((struct lys_node_container *)node->schema)->must_size; |
| 5582 | must = ((struct lys_node_container *)node->schema)->must; |
| 5583 | break; |
| 5584 | case LYS_LEAF: |
| 5585 | must_size = ((struct lys_node_leaf *)node->schema)->must_size; |
| 5586 | must = ((struct lys_node_leaf *)node->schema)->must; |
| 5587 | break; |
| 5588 | case LYS_LEAFLIST: |
| 5589 | must_size = ((struct lys_node_leaflist *)node->schema)->must_size; |
| 5590 | must = ((struct lys_node_leaflist *)node->schema)->must; |
| 5591 | break; |
| 5592 | case LYS_LIST: |
| 5593 | must_size = ((struct lys_node_list *)node->schema)->must_size; |
| 5594 | must = ((struct lys_node_list *)node->schema)->must; |
| 5595 | break; |
| 5596 | case LYS_ANYXML: |
| 5597 | case LYS_ANYDATA: |
| 5598 | must_size = ((struct lys_node_anydata *)node->schema)->must_size; |
| 5599 | must = ((struct lys_node_anydata *)node->schema)->must; |
| 5600 | break; |
| 5601 | case LYS_NOTIF: |
| 5602 | must_size = ((struct lys_node_notif *)node->schema)->must_size; |
| 5603 | must = ((struct lys_node_notif *)node->schema)->must; |
| 5604 | break; |
| 5605 | default: |
| 5606 | must_size = 0; |
| 5607 | break; |
| 5608 | } |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 5609 | |
| 5610 | node_flags = node->schema->flags; |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5611 | } |
| 5612 | |
| 5613 | for (i = 0; i < must_size; ++i) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5614 | if (lyxp_eval(must[i].expr, node, LYXP_NODE_ELEM, lyd_node_module(node), &set, LYXP_MUST)) { |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5615 | return -1; |
| 5616 | } |
| 5617 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5618 | lyxp_set_cast(&set, LYXP_SET_BOOLEAN, node, lyd_node_module(node), LYXP_MUST); |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5619 | |
Michal Vasko | 8146d4c | 2016-05-09 15:50:29 +0200 | [diff] [blame] | 5620 | if (!set.val.bool) { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 5621 | if ((ignore_fail == 1) || ((node_flags & LYS_XPATH_DEP) && (ignore_fail == 2))) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5622 | LOGVRB("Must condition \"%s\" not satisfied, but it is not required.", must[i].expr); |
| 5623 | } else { |
| 5624 | LOGVAL(LYE_NOMUST, LY_VLOG_LYD, node, must[i].expr); |
| 5625 | if (must[i].emsg) { |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 5626 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, must[i].emsg); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5627 | } |
| 5628 | if (must[i].eapptag) { |
| 5629 | strncpy(((struct ly_err *)&ly_errno)->apptag, must[i].eapptag, LY_APPTAG_LEN - 1); |
| 5630 | } |
| 5631 | return 1; |
Michal Vasko | 6ac6828 | 2016-04-11 10:56:47 +0200 | [diff] [blame] | 5632 | } |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5633 | } |
| 5634 | } |
| 5635 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5636 | return EXIT_SUCCESS; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5637 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5638 | |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5639 | /** |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5640 | * @brief Resolve (find) when condition schema context node. Does not log. |
| 5641 | * |
| 5642 | * @param[in] schema Schema node with the when condition. |
| 5643 | * @param[out] ctx_snode When schema context node. |
| 5644 | * @param[out] ctx_snode_type Schema context node type. |
| 5645 | */ |
| 5646 | void |
| 5647 | resolve_when_ctx_snode(const struct lys_node *schema, struct lys_node **ctx_snode, enum lyxp_node_type *ctx_snode_type) |
| 5648 | { |
| 5649 | const struct lys_node *sparent; |
| 5650 | |
| 5651 | /* find a not schema-only node */ |
| 5652 | *ctx_snode_type = LYXP_NODE_ELEM; |
| 5653 | while (schema->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE | LYS_AUGMENT | LYS_INPUT | LYS_OUTPUT)) { |
| 5654 | if (schema->nodetype == LYS_AUGMENT) { |
| 5655 | sparent = ((struct lys_node_augment *)schema)->target; |
| 5656 | } else { |
| 5657 | sparent = schema->parent; |
| 5658 | } |
| 5659 | if (!sparent) { |
| 5660 | /* context node is the document root (fake root in our case) */ |
| 5661 | if (schema->flags & LYS_CONFIG_W) { |
| 5662 | *ctx_snode_type = LYXP_NODE_ROOT_CONFIG; |
| 5663 | } else { |
Michal Vasko | b94a5e4 | 2016-09-08 14:01:56 +0200 | [diff] [blame] | 5664 | *ctx_snode_type = LYXP_NODE_ROOT; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5665 | } |
Michal Vasko | 2d2aec1 | 2016-09-08 11:42:50 +0200 | [diff] [blame] | 5666 | /* we need the first top-level sibling, but no uses or groupings */ |
Michal Vasko | b94a5e4 | 2016-09-08 14:01:56 +0200 | [diff] [blame] | 5667 | schema = lys_getnext(NULL, NULL, lys_node_module(schema), 0); |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5668 | break; |
| 5669 | } |
| 5670 | schema = sparent; |
| 5671 | } |
| 5672 | |
| 5673 | *ctx_snode = (struct lys_node *)schema; |
| 5674 | } |
| 5675 | |
| 5676 | /** |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5677 | * @brief Resolve (find) when condition context node. Does not log. |
| 5678 | * |
| 5679 | * @param[in] node Data node, whose conditional definition is being decided. |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5680 | * @param[in] schema Schema node with the when condition. |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5681 | * @param[out] ctx_node Context node. |
| 5682 | * @param[out] ctx_node_type Context node type. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5683 | * |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5684 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5685 | */ |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5686 | static int |
| 5687 | resolve_when_ctx_node(struct lyd_node *node, struct lys_node *schema, struct lyd_node **ctx_node, |
| 5688 | enum lyxp_node_type *ctx_node_type) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5689 | { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5690 | struct lyd_node *parent; |
| 5691 | struct lys_node *sparent; |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5692 | enum lyxp_node_type node_type; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5693 | uint16_t i, data_depth, schema_depth; |
| 5694 | |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5695 | resolve_when_ctx_snode(schema, &schema, &node_type); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5696 | |
Michal Vasko | fe98975 | 2016-09-08 08:47:26 +0200 | [diff] [blame] | 5697 | if (node_type == LYXP_NODE_ELEM) { |
| 5698 | /* standard element context node */ |
| 5699 | for (parent = node, data_depth = 0; parent; parent = parent->parent, ++data_depth); |
| 5700 | for (sparent = schema, schema_depth = 0; |
| 5701 | sparent; |
| 5702 | sparent = (sparent->nodetype == LYS_AUGMENT ? ((struct lys_node_augment *)sparent)->target : sparent->parent)) { |
| 5703 | if (sparent->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_NOTIF | LYS_RPC)) { |
| 5704 | ++schema_depth; |
| 5705 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5706 | } |
Michal Vasko | fe98975 | 2016-09-08 08:47:26 +0200 | [diff] [blame] | 5707 | if (data_depth < schema_depth) { |
| 5708 | return -1; |
| 5709 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5710 | |
Michal Vasko | 956e854 | 2016-08-26 09:43:35 +0200 | [diff] [blame] | 5711 | /* find the corresponding data node */ |
| 5712 | for (i = 0; i < data_depth - schema_depth; ++i) { |
| 5713 | node = node->parent; |
| 5714 | } |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5715 | if (node->schema != schema) { |
| 5716 | return -1; |
| 5717 | } |
Michal Vasko | fe98975 | 2016-09-08 08:47:26 +0200 | [diff] [blame] | 5718 | } else { |
| 5719 | /* root context node */ |
| 5720 | while (node->parent) { |
| 5721 | node = node->parent; |
| 5722 | } |
| 5723 | while (node->prev->next) { |
| 5724 | node = node->prev; |
| 5725 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5726 | } |
| 5727 | |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5728 | *ctx_node = node; |
| 5729 | *ctx_node_type = node_type; |
| 5730 | return EXIT_SUCCESS; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5731 | } |
| 5732 | |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5733 | /** |
| 5734 | * @brief Temporarily unlink nodes as per YANG 1.1 RFC section 7.21.5 for when XPath evaluation. |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5735 | * The context node is adjusted if needed. |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5736 | * |
| 5737 | * @param[in] snode Schema node, whose children instances need to be unlinked. |
| 5738 | * @param[in,out] node Data siblings where to look for the children of \p snode. If it is unlinked, |
| 5739 | * it is moved to point to another sibling still in the original tree. |
| 5740 | * @param[in,out] ctx_node When context node, adjusted if needed. |
| 5741 | * @param[in] ctx_node_type Context node type, just for information to detect invalid situations. |
| 5742 | * @param[out] unlinked_nodes Unlinked siblings. Can be safely appended to \p node afterwards. |
| 5743 | * Ordering may change, but there will be no semantic change. |
| 5744 | * |
| 5745 | * @return EXIT_SUCCESS on success, -1 on error. |
| 5746 | */ |
| 5747 | static int |
| 5748 | resolve_when_unlink_nodes(struct lys_node *snode, struct lyd_node **node, struct lyd_node **ctx_node, |
| 5749 | enum lyxp_node_type ctx_node_type, struct lyd_node **unlinked_nodes) |
| 5750 | { |
| 5751 | struct lyd_node *next, *elem; |
| 5752 | |
| 5753 | switch (snode->nodetype) { |
| 5754 | case LYS_AUGMENT: |
| 5755 | case LYS_USES: |
| 5756 | case LYS_CHOICE: |
| 5757 | case LYS_CASE: |
| 5758 | LY_TREE_FOR(snode->child, snode) { |
| 5759 | if (resolve_when_unlink_nodes(snode, node, ctx_node, ctx_node_type, unlinked_nodes)) { |
| 5760 | return -1; |
| 5761 | } |
| 5762 | } |
| 5763 | break; |
| 5764 | case LYS_CONTAINER: |
| 5765 | case LYS_LIST: |
| 5766 | case LYS_LEAF: |
| 5767 | case LYS_LEAFLIST: |
| 5768 | case LYS_ANYXML: |
| 5769 | case LYS_ANYDATA: |
| 5770 | LY_TREE_FOR_SAFE(lyd_first_sibling(*node), next, elem) { |
| 5771 | if (elem->schema == snode) { |
| 5772 | |
| 5773 | if (elem == *ctx_node) { |
| 5774 | /* We are going to unlink our context node! This normally cannot happen, |
| 5775 | * but we use normal top-level data nodes for faking a document root node, |
| 5776 | * so if this is the context node, we just use the next top-level node. |
| 5777 | * Additionally, it can even happen that there are no top-level data nodes left, |
| 5778 | * all were unlinked, so in this case we pass NULL as the context node/data tree, |
| 5779 | * lyxp_eval() can handle this special situation. |
| 5780 | */ |
| 5781 | if (ctx_node_type == LYXP_NODE_ELEM) { |
| 5782 | LOGINT; |
| 5783 | return -1; |
| 5784 | } |
| 5785 | |
| 5786 | if (elem->prev == elem) { |
| 5787 | /* unlinking last top-level element, use an empty data tree */ |
| 5788 | *ctx_node = NULL; |
| 5789 | } else { |
| 5790 | /* in this case just use the previous/last top-level data node */ |
| 5791 | *ctx_node = elem->prev; |
| 5792 | } |
| 5793 | } else if (elem == *node) { |
| 5794 | /* We are going to unlink the currently processed node. This does not matter that |
| 5795 | * much, but we would lose access to the original data tree, so just move our |
| 5796 | * pointer somewhere still inside it. |
| 5797 | */ |
| 5798 | if ((*node)->prev != *node) { |
| 5799 | *node = (*node)->prev; |
| 5800 | } else { |
| 5801 | /* the processed node with sibings were all unlinked, oh well */ |
| 5802 | *node = NULL; |
| 5803 | } |
| 5804 | } |
| 5805 | |
| 5806 | /* temporarily unlink the node */ |
| 5807 | lyd_unlink(elem); |
| 5808 | if (*unlinked_nodes) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5809 | if (lyd_insert_after((*unlinked_nodes)->prev, elem)) { |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5810 | LOGINT; |
| 5811 | return -1; |
| 5812 | } |
| 5813 | } else { |
| 5814 | *unlinked_nodes = elem; |
| 5815 | } |
| 5816 | |
| 5817 | if (snode->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_ANYDATA)) { |
| 5818 | /* there can be only one instance */ |
| 5819 | break; |
| 5820 | } |
| 5821 | } |
| 5822 | } |
| 5823 | break; |
| 5824 | default: |
| 5825 | LOGINT; |
| 5826 | return -1; |
| 5827 | } |
| 5828 | |
| 5829 | return EXIT_SUCCESS; |
| 5830 | } |
| 5831 | |
| 5832 | /** |
| 5833 | * @brief Relink the unlinked nodes back. |
| 5834 | * |
| 5835 | * @param[in] node Data node to link the nodes back to. It can actually be the adjusted context node, |
| 5836 | * we simply need a sibling from the original data tree. |
| 5837 | * @param[in] unlinked_nodes Unlinked nodes to relink to \p node. |
| 5838 | * @param[in] ctx_node_type Context node type to distinguish between \p node being the parent |
| 5839 | * or the sibling of \p unlinked_nodes. |
| 5840 | * |
| 5841 | * @return EXIT_SUCCESS on success, -1 on error. |
| 5842 | */ |
| 5843 | static int |
| 5844 | resolve_when_relink_nodes(struct lyd_node *node, struct lyd_node *unlinked_nodes, enum lyxp_node_type ctx_node_type) |
| 5845 | { |
| 5846 | struct lyd_node *elem; |
| 5847 | |
| 5848 | LY_TREE_FOR_SAFE(unlinked_nodes, unlinked_nodes, elem) { |
Michal Vasko | de1feeb | 2016-12-20 14:48:42 +0100 | [diff] [blame] | 5849 | lyd_unlink(elem); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5850 | if (ctx_node_type == LYXP_NODE_ELEM) { |
| 5851 | if (lyd_insert(node, elem)) { |
| 5852 | return -1; |
| 5853 | } |
| 5854 | } else { |
| 5855 | if (lyd_insert_after(node, elem)) { |
| 5856 | return -1; |
| 5857 | } |
| 5858 | } |
| 5859 | } |
| 5860 | |
| 5861 | return EXIT_SUCCESS; |
| 5862 | } |
| 5863 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5864 | int |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5865 | resolve_applies_must(const struct lyd_node *node) |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5866 | { |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5867 | int ret = 0; |
| 5868 | uint8_t must_size; |
| 5869 | struct lys_node *schema, *iter; |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5870 | |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5871 | assert(node); |
| 5872 | |
| 5873 | schema = node->schema; |
| 5874 | |
| 5875 | /* their own must */ |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5876 | switch (schema->nodetype) { |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5877 | case LYS_CONTAINER: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5878 | must_size = ((struct lys_node_container *)schema)->must_size; |
| 5879 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5880 | case LYS_LEAF: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5881 | must_size = ((struct lys_node_leaf *)schema)->must_size; |
| 5882 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5883 | case LYS_LEAFLIST: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5884 | must_size = ((struct lys_node_leaflist *)schema)->must_size; |
| 5885 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5886 | case LYS_LIST: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5887 | must_size = ((struct lys_node_list *)schema)->must_size; |
| 5888 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5889 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 5890 | case LYS_ANYDATA: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5891 | must_size = ((struct lys_node_anydata *)schema)->must_size; |
| 5892 | break; |
| 5893 | case LYS_NOTIF: |
| 5894 | must_size = ((struct lys_node_notif *)schema)->must_size; |
| 5895 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5896 | default: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5897 | must_size = 0; |
| 5898 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5899 | } |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5900 | |
| 5901 | if (must_size) { |
| 5902 | ++ret; |
| 5903 | } |
| 5904 | |
| 5905 | /* schema may be a direct data child of input/output with must (but it must be first, it needs to be evaluated only once) */ |
| 5906 | if (!node->prev->next) { |
| 5907 | for (iter = lys_parent(schema); iter && (iter->nodetype & (LYS_CHOICE | LYS_CASE | LYS_USES)); iter = lys_parent(iter)); |
| 5908 | if (iter && (iter->nodetype & (LYS_INPUT | LYS_OUTPUT))) { |
| 5909 | ret += 0x2; |
| 5910 | } |
| 5911 | } |
| 5912 | |
| 5913 | return ret; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5914 | } |
| 5915 | |
| 5916 | int |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5917 | 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] | 5918 | { |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5919 | const struct lys_node *parent; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5920 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5921 | assert(schema); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5922 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5923 | 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] | 5924 | return 1; |
| 5925 | } |
| 5926 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5927 | parent = schema; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5928 | goto check_augment; |
| 5929 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5930 | while (parent) { |
| 5931 | /* stop conditions */ |
| 5932 | if (!mode) { |
| 5933 | /* stop on node that can be instantiated in data tree */ |
| 5934 | if (!(parent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) { |
| 5935 | break; |
| 5936 | } |
| 5937 | } else { |
| 5938 | /* stop on the specified node */ |
| 5939 | if (parent == stop) { |
| 5940 | break; |
| 5941 | } |
| 5942 | } |
| 5943 | |
| 5944 | if (((const struct lys_node_uses *)parent)->when) { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5945 | return 1; |
| 5946 | } |
| 5947 | check_augment: |
| 5948 | |
| 5949 | if ((parent->parent && (parent->parent->nodetype == LYS_AUGMENT) && |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5950 | (((const struct lys_node_augment *)parent->parent)->when))) { |
Michal Vasko | e365556 | 2016-08-24 15:56:17 +0200 | [diff] [blame] | 5951 | return 1; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5952 | } |
| 5953 | parent = lys_parent(parent); |
| 5954 | } |
| 5955 | |
| 5956 | return 0; |
| 5957 | } |
| 5958 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5959 | /** |
| 5960 | * @brief Resolve (check) all when conditions relevant for \p node. |
| 5961 | * Logs directly. |
| 5962 | * |
| 5963 | * @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] | 5964 | * |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5965 | * @return |
| 5966 | * -1 - error, ly_errno is set |
| 5967 | * 0 - true "when" statement |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5968 | * 0, ly_vecode = LYVE_NOWHEN - false "when" statement |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5969 | * 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] | 5970 | */ |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5971 | int |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5972 | resolve_when(struct lyd_node *node, int *result, int ignore_fail) |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5973 | { |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5974 | struct lyd_node *ctx_node = NULL, *unlinked_nodes, *tmp_node; |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5975 | struct lys_node *sparent; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5976 | struct lyxp_set set; |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5977 | enum lyxp_node_type ctx_node_type; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5978 | int rc = 0; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5979 | |
| 5980 | assert(node); |
| 5981 | memset(&set, 0, sizeof set); |
| 5982 | |
| 5983 | 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] | 5984 | /* make the node dummy for the evaluation */ |
| 5985 | node->validity |= LYD_VAL_INUSE; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5986 | rc = lyxp_eval(((struct lys_node_container *)node->schema)->when->cond, node, LYXP_NODE_ELEM, lyd_node_module(node), |
| 5987 | &set, LYXP_WHEN); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5988 | node->validity &= ~LYD_VAL_INUSE; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5989 | if (rc) { |
| 5990 | if (rc == 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5991 | 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] | 5992 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5993 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5994 | } |
| 5995 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5996 | /* set boolean result of the condition */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5997 | lyxp_set_cast(&set, LYXP_SET_BOOLEAN, node, lyd_node_module(node), LYXP_WHEN); |
Michal Vasko | 8146d4c | 2016-05-09 15:50:29 +0200 | [diff] [blame] | 5998 | if (!set.val.bool) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5999 | node->when_status |= LYD_WHEN_FALSE; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 6000 | if ((ignore_fail == 1) || ((node->schema->flags & LYS_XPATH_DEP) && (ignore_fail == 2))) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6001 | LOGVRB("When condition \"%s\" is not satisfied, but it is not required.", |
| 6002 | ((struct lys_node_container *)node->schema)->when->cond); |
| 6003 | } else { |
| 6004 | LOGVAL(LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_container *)node->schema)->when->cond); |
| 6005 | goto cleanup; |
| 6006 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6007 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6008 | |
| 6009 | /* free xpath set content */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6010 | lyxp_set_cast(&set, LYXP_SET_EMPTY, node, lyd_node_module(node), 0); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6011 | } |
| 6012 | |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6013 | sparent = node->schema; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6014 | goto check_augment; |
| 6015 | |
| 6016 | /* check when in every schema node that affects node */ |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6017 | while (sparent && (sparent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) { |
| 6018 | if (((struct lys_node_uses *)sparent)->when) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6019 | if (!ctx_node) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6020 | rc = resolve_when_ctx_node(node, sparent, &ctx_node, &ctx_node_type); |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 6021 | if (rc) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6022 | LOGINT; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6023 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6024 | } |
| 6025 | } |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6026 | |
| 6027 | unlinked_nodes = NULL; |
| 6028 | /* we do not want our node pointer to change */ |
| 6029 | tmp_node = node; |
| 6030 | rc = resolve_when_unlink_nodes(sparent, &tmp_node, &ctx_node, ctx_node_type, &unlinked_nodes); |
| 6031 | if (rc) { |
| 6032 | goto cleanup; |
| 6033 | } |
| 6034 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6035 | rc = lyxp_eval(((struct lys_node_uses *)sparent)->when->cond, ctx_node, ctx_node_type, lys_node_module(sparent), |
| 6036 | &set, LYXP_WHEN); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6037 | |
| 6038 | if (unlinked_nodes && ctx_node) { |
| 6039 | if (resolve_when_relink_nodes(ctx_node, unlinked_nodes, ctx_node_type)) { |
| 6040 | rc = -1; |
| 6041 | goto cleanup; |
| 6042 | } |
| 6043 | } |
| 6044 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6045 | if (rc) { |
| 6046 | if (rc == 1) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6047 | 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] | 6048 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6049 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6050 | } |
| 6051 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6052 | lyxp_set_cast(&set, LYXP_SET_BOOLEAN, ctx_node, lys_node_module(sparent), LYXP_WHEN); |
Michal Vasko | 8146d4c | 2016-05-09 15:50:29 +0200 | [diff] [blame] | 6053 | if (!set.val.bool) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6054 | node->when_status |= LYD_WHEN_FALSE; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 6055 | if ((ignore_fail == 1) || ((sparent->flags & LYS_XPATH_DEP) || (ignore_fail == 2))) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6056 | LOGVRB("When condition \"%s\" is not satisfied, but it is not required.", |
| 6057 | ((struct lys_node_uses *)sparent)->when->cond); |
| 6058 | } else { |
| 6059 | LOGVAL(LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_uses *)sparent)->when->cond); |
| 6060 | goto cleanup; |
| 6061 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6062 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6063 | |
| 6064 | /* free xpath set content */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6065 | lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node, lys_node_module(sparent), 0); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6066 | } |
| 6067 | |
| 6068 | check_augment: |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6069 | 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] | 6070 | if (!ctx_node) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6071 | 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] | 6072 | if (rc) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6073 | LOGINT; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6074 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6075 | } |
| 6076 | } |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6077 | |
| 6078 | unlinked_nodes = NULL; |
| 6079 | tmp_node = node; |
| 6080 | rc = resolve_when_unlink_nodes(sparent->parent, &tmp_node, &ctx_node, ctx_node_type, &unlinked_nodes); |
| 6081 | if (rc) { |
| 6082 | goto cleanup; |
| 6083 | } |
| 6084 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6085 | rc = lyxp_eval(((struct lys_node_augment *)sparent->parent)->when->cond, ctx_node, ctx_node_type, |
| 6086 | lys_node_module(sparent->parent), &set, LYXP_WHEN); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6087 | |
| 6088 | /* reconnect nodes, if ctx_node is NULL then all the nodes were unlinked, but linked together, |
| 6089 | * so the tree did not actually change and there is nothing for us to do |
| 6090 | */ |
| 6091 | if (unlinked_nodes && ctx_node) { |
| 6092 | if (resolve_when_relink_nodes(ctx_node, unlinked_nodes, ctx_node_type)) { |
| 6093 | rc = -1; |
| 6094 | goto cleanup; |
| 6095 | } |
| 6096 | } |
| 6097 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6098 | if (rc) { |
| 6099 | if (rc == 1) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6100 | 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] | 6101 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6102 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6103 | } |
| 6104 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6105 | lyxp_set_cast(&set, LYXP_SET_BOOLEAN, ctx_node, lys_node_module(sparent->parent), LYXP_WHEN); |
Michal Vasko | 8146d4c | 2016-05-09 15:50:29 +0200 | [diff] [blame] | 6106 | if (!set.val.bool) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6107 | node->when_status |= LYD_WHEN_FALSE; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 6108 | if ((ignore_fail == 1) || ((sparent->parent->flags & LYS_XPATH_DEP) && (ignore_fail == 2))) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6109 | LOGVRB("When condition \"%s\" is not satisfied, but it is not required.", |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 6110 | ((struct lys_node_augment *)sparent->parent)->when->cond); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6111 | } else { |
| 6112 | LOGVAL(LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_augment *)sparent->parent)->when->cond); |
| 6113 | goto cleanup; |
| 6114 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6115 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6116 | |
| 6117 | /* free xpath set content */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6118 | lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node, lys_node_module(sparent->parent), 0); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6119 | } |
| 6120 | |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6121 | sparent = lys_parent(sparent); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6122 | } |
| 6123 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6124 | node->when_status |= LYD_WHEN_TRUE; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6125 | |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6126 | cleanup: |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6127 | /* free xpath set content */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6128 | lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node ? ctx_node : node, NULL, 0); |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6129 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 6130 | if (result) { |
| 6131 | if (node->when_status & LYD_WHEN_TRUE) { |
| 6132 | *result = 1; |
| 6133 | } else { |
| 6134 | *result = 0; |
| 6135 | } |
| 6136 | } |
| 6137 | |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6138 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6139 | } |
| 6140 | |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6141 | static int |
| 6142 | check_leafref_features(struct lys_type *type) |
| 6143 | { |
| 6144 | struct lys_node *iter; |
| 6145 | struct ly_set *src_parents, *trg_parents, *features; |
| 6146 | unsigned int i, j, size, x; |
| 6147 | int ret = EXIT_SUCCESS; |
| 6148 | |
| 6149 | assert(type->parent); |
| 6150 | |
| 6151 | src_parents = ly_set_new(); |
| 6152 | trg_parents = ly_set_new(); |
| 6153 | features = ly_set_new(); |
| 6154 | |
| 6155 | /* get parents chain of source (leafref) */ |
| 6156 | for (iter = (struct lys_node *)type->parent; iter; iter = iter->parent) { |
| 6157 | if (iter->nodetype & (LYS_INPUT | LYS_OUTPUT)) { |
| 6158 | continue; |
| 6159 | } |
| 6160 | ly_set_add(src_parents, iter, LY_SET_OPT_USEASLIST); |
| 6161 | } |
| 6162 | /* get parents chain of target */ |
| 6163 | for (iter = (struct lys_node *)type->info.lref.target; iter; iter = iter->parent) { |
| 6164 | if (iter->nodetype & (LYS_INPUT | LYS_OUTPUT)) { |
| 6165 | continue; |
| 6166 | } |
| 6167 | ly_set_add(trg_parents, iter, LY_SET_OPT_USEASLIST); |
| 6168 | } |
| 6169 | |
| 6170 | /* compare the features used in if-feature statements in the rest of both |
| 6171 | * chains of parents. The set of features used for target must be a subset |
| 6172 | * of features used for the leafref. This is not a perfect, we should compare |
| 6173 | * the truth tables but it could require too much resources, so we simplify that */ |
| 6174 | for (i = 0; i < src_parents->number; i++) { |
| 6175 | iter = src_parents->set.s[i]; /* shortcut */ |
| 6176 | if (!iter->iffeature_size) { |
| 6177 | continue; |
| 6178 | } |
| 6179 | for (j = 0; j < iter->iffeature_size; j++) { |
| 6180 | resolve_iffeature_getsizes(&iter->iffeature[j], NULL, &size); |
| 6181 | for (; size; size--) { |
| 6182 | if (!iter->iffeature[j].features[size - 1]) { |
| 6183 | /* not yet resolved feature, postpone this check */ |
| 6184 | ret = EXIT_FAILURE; |
| 6185 | goto cleanup; |
| 6186 | } |
| 6187 | ly_set_add(features, iter->iffeature[j].features[size - 1], 0); |
| 6188 | } |
| 6189 | } |
| 6190 | } |
| 6191 | x = features->number; |
| 6192 | for (i = 0; i < trg_parents->number; i++) { |
| 6193 | iter = trg_parents->set.s[i]; /* shortcut */ |
| 6194 | if (!iter->iffeature_size) { |
| 6195 | continue; |
| 6196 | } |
| 6197 | for (j = 0; j < iter->iffeature_size; j++) { |
| 6198 | resolve_iffeature_getsizes(&iter->iffeature[j], NULL, &size); |
| 6199 | for (; size; size--) { |
| 6200 | if (!iter->iffeature[j].features[size - 1]) { |
| 6201 | /* not yet resolved feature, postpone this check */ |
| 6202 | ret = EXIT_FAILURE; |
| 6203 | goto cleanup; |
| 6204 | } |
| 6205 | if ((unsigned int)ly_set_add(features, iter->iffeature[j].features[size - 1], 0) >= x) { |
| 6206 | /* the feature is not present in features set of target's parents chain */ |
| 6207 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, type->parent, "leafref", type->info.lref.path); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 6208 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6209 | "Leafref is not conditional based on \"%s\" feature as its target.", |
| 6210 | iter->iffeature[j].features[size - 1]->name); |
| 6211 | ret = -1; |
| 6212 | goto cleanup; |
| 6213 | } |
| 6214 | } |
| 6215 | } |
| 6216 | } |
| 6217 | |
| 6218 | cleanup: |
| 6219 | ly_set_free(features); |
| 6220 | ly_set_free(src_parents); |
| 6221 | ly_set_free(trg_parents); |
| 6222 | |
| 6223 | return ret; |
| 6224 | } |
| 6225 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6226 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6227 | * @brief Resolve a single unres schema item. Logs indirectly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6228 | * |
| 6229 | * @param[in] mod Main module. |
| 6230 | * @param[in] item Item to resolve. Type determined by \p type. |
| 6231 | * @param[in] type Type of the unresolved item. |
| 6232 | * @param[in] str_snode String, a schema node, or NULL. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6233 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 769f803 | 2017-01-24 13:11:55 +0100 | [diff] [blame] | 6234 | * @param[in] final_fail Whether we are just printing errors of the failed unres items. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6235 | * |
| 6236 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 6237 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6238 | static int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 6239 | resolve_unres_schema_item(struct lys_module *mod, void *item, enum UNRES_ITEM type, void *str_snode, |
Michal Vasko | 769f803 | 2017-01-24 13:11:55 +0100 | [diff] [blame] | 6240 | struct unres_schema *unres, int final_fail) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6241 | { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6242 | /* 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] | 6243 | int rc = -1, has_str = 0, tpdf_flag = 0, i, k; |
| 6244 | unsigned int j; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6245 | struct lys_node *root, *next, *node, *par_grp; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6246 | const char *expr; |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 6247 | uint8_t *u; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6248 | |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6249 | struct ly_set *refs, *procs; |
| 6250 | struct lys_feature *ref, *feat; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6251 | struct lys_ident *ident; |
| 6252 | struct lys_type *stype; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6253 | struct lys_node_choice *choic; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6254 | struct lyxml_elem *yin; |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6255 | struct yang_type *yang; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6256 | struct unres_list_uniq *unique_info; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6257 | struct unres_iffeat_data *iff_data; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6258 | struct unres_ext *ext_data; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6259 | struct lys_ext_instance *ext, **extlist; |
| 6260 | struct lyext_plugin *eplugin; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6261 | |
| 6262 | switch (type) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6263 | case UNRES_IDENT: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6264 | expr = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 6265 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6266 | ident = item; |
| 6267 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6268 | rc = resolve_base_ident(mod, ident, expr, "identity", NULL, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6269 | break; |
| 6270 | case UNRES_TYPE_IDENTREF: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6271 | expr = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 6272 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6273 | stype = item; |
| 6274 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6275 | rc = resolve_base_ident(mod, NULL, expr, "type", stype, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6276 | break; |
| 6277 | case UNRES_TYPE_LEAFREF: |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 6278 | node = str_snode; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6279 | stype = item; |
| 6280 | |
Radek Krejci | 2f12f85 | 2016-01-08 12:59:57 +0100 | [diff] [blame] | 6281 | /* HACK - when there is no parent, we are in top level typedef and in that |
| 6282 | * case, the path has to contain absolute path, so we let the resolve_path_arg_schema() |
| 6283 | * know it via tpdf_flag */ |
| 6284 | if (!node) { |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 6285 | tpdf_flag = 1; |
Radek Krejci | 2f12f85 | 2016-01-08 12:59:57 +0100 | [diff] [blame] | 6286 | node = (struct lys_node *)stype->parent; |
| 6287 | } |
| 6288 | |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 6289 | if (!lys_node_module(node)->implemented) { |
| 6290 | /* not implemented module, don't bother with resolving the leafref |
Radek Krejci | 990af1f | 2016-11-09 13:53:36 +0100 | [diff] [blame] | 6291 | * 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] | 6292 | rc = 0; |
| 6293 | break; |
| 6294 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6295 | rc = resolve_path_arg_schema(stype->info.lref.path, node, tpdf_flag, |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 6296 | (const struct lys_node **)&stype->info.lref.target); |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 6297 | if (!tpdf_flag && !rc) { |
| 6298 | assert(stype->info.lref.target); |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6299 | /* check if leafref and its target are under a common if-features */ |
| 6300 | rc = check_leafref_features(stype); |
| 6301 | if (rc) { |
| 6302 | break; |
| 6303 | } |
| 6304 | |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 6305 | /* store the backlink from leafref target */ |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 6306 | if (lys_leaf_add_leafref_target(stype->info.lref.target, (struct lys_node *)stype->parent)) { |
| 6307 | rc = -1; |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 6308 | } |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 6309 | } |
| 6310 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6311 | break; |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 6312 | case UNRES_TYPE_DER_TPDF: |
| 6313 | tpdf_flag = 1; |
| 6314 | /* no break */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6315 | case UNRES_TYPE_DER: |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6316 | /* parent */ |
| 6317 | node = str_snode; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6318 | stype = item; |
| 6319 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6320 | /* HACK type->der is temporarily unparsed type statement */ |
| 6321 | yin = (struct lyxml_elem *)stype->der; |
| 6322 | stype->der = NULL; |
| 6323 | |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6324 | if (yin->flags & LY_YANG_STRUCTURE_FLAG) { |
| 6325 | yang = (struct yang_type *)yin; |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 6326 | rc = yang_check_type(mod, node, yang, tpdf_flag, unres); |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6327 | |
| 6328 | if (rc) { |
Pavol Vican | 8bd72e4 | 2016-08-29 09:53:05 +0200 | [diff] [blame] | 6329 | /* may try again later */ |
| 6330 | stype->der = (struct lys_tpdf *)yang; |
Pavol Vican | d01d8ae | 2016-03-01 10:45:59 +0100 | [diff] [blame] | 6331 | } else { |
| 6332 | /* 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] | 6333 | lydict_remove(mod->ctx, yang->name); |
Pavol Vican | d01d8ae | 2016-03-01 10:45:59 +0100 | [diff] [blame] | 6334 | free(yang); |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6335 | } |
| 6336 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6337 | } else { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 6338 | rc = fill_yin_type(mod, node, yin, stype, tpdf_flag, unres); |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6339 | if (!rc) { |
| 6340 | /* we need to always be able to free this, it's safe only in this case */ |
| 6341 | lyxml_free(mod->ctx, yin); |
| 6342 | } else { |
| 6343 | /* may try again later, put all back how it was */ |
| 6344 | stype->der = (struct lys_tpdf *)yin; |
| 6345 | } |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6346 | } |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 6347 | if (rc == EXIT_SUCCESS) { |
Radek Krejci | 2c2fce8 | 2016-08-01 13:52:26 +0200 | [diff] [blame] | 6348 | /* it does not make sense to have leaf-list of empty type */ |
| 6349 | if (!tpdf_flag && node->nodetype == LYS_LEAFLIST && stype->base == LY_TYPE_EMPTY) { |
| 6350 | LOGWRN("The leaf-list \"%s\" is of \"empty\" type, which does not make sense.", node->name); |
| 6351 | } |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6352 | } else if (rc == EXIT_FAILURE && stype->base != LY_TYPE_ERR) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 6353 | /* forward reference - in case the type is in grouping, we have to make the grouping unusable |
| 6354 | * by uses statement until the type is resolved. We do that the same way as uses statements inside |
| 6355 | * grouping - the grouping's nacm member (not used un grouping) is used to increase the number of |
| 6356 | * 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] | 6357 | * 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] | 6358 | * of the type's base member. */ |
| 6359 | for (par_grp = node; par_grp && (par_grp->nodetype != LYS_GROUPING); par_grp = lys_parent(par_grp)); |
| 6360 | if (par_grp) { |
Radek Krejci | 6ff885d | 2017-01-03 14:06:22 +0100 | [diff] [blame] | 6361 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
| 6362 | ((uint8_t*)&((struct lys_node_grp *)par_grp)->flags)[1]++; |
| 6363 | #else |
| 6364 | ((uint8_t*)&((struct lys_node_grp *)par_grp)->flags)[0]++; |
| 6365 | #endif |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6366 | stype->base = LY_TYPE_ERR; |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 6367 | } |
Radek Krejci | 2c2fce8 | 2016-08-01 13:52:26 +0200 | [diff] [blame] | 6368 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6369 | break; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6370 | case UNRES_IFFEAT: |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6371 | iff_data = str_snode; |
| 6372 | 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] | 6373 | if (!rc) { |
| 6374 | /* success */ |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 6375 | if (iff_data->infeature) { |
| 6376 | /* store backlink into the target feature to allow reverse changes in case of changing feature status */ |
| 6377 | feat = *((struct lys_feature **)item); |
| 6378 | if (!feat->depfeatures) { |
| 6379 | feat->depfeatures = ly_set_new(); |
| 6380 | } |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 6381 | ly_set_add(feat->depfeatures, iff_data->node, LY_SET_OPT_USEASLIST); |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 6382 | } |
| 6383 | /* cleanup temporary data */ |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6384 | lydict_remove(mod->ctx, iff_data->fname); |
| 6385 | free(iff_data); |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6386 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6387 | break; |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6388 | case UNRES_FEATURE: |
| 6389 | feat = (struct lys_feature *)item; |
| 6390 | |
| 6391 | if (feat->iffeature_size) { |
| 6392 | refs = ly_set_new(); |
| 6393 | procs = ly_set_new(); |
| 6394 | ly_set_add(procs, feat, 0); |
| 6395 | |
| 6396 | while (procs->number) { |
| 6397 | ref = procs->set.g[procs->number - 1]; |
| 6398 | ly_set_rm_index(procs, procs->number - 1); |
| 6399 | |
| 6400 | for (i = 0; i < ref->iffeature_size; i++) { |
| 6401 | resolve_iffeature_getsizes(&ref->iffeature[i], NULL, &j); |
| 6402 | for (; j > 0 ; j--) { |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6403 | if (ref->iffeature[i].features[j - 1]) { |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6404 | if (ref->iffeature[i].features[j - 1] == feat) { |
| 6405 | LOGVAL(LYE_CIRC_FEATURES, LY_VLOG_NONE, NULL, feat->name); |
| 6406 | goto featurecheckdone; |
| 6407 | } |
| 6408 | |
| 6409 | if (ref->iffeature[i].features[j - 1]->iffeature_size) { |
| 6410 | k = refs->number; |
| 6411 | if (ly_set_add(refs, ref->iffeature[i].features[j - 1], 0) == k) { |
| 6412 | /* not yet seen feature, add it for processing */ |
| 6413 | ly_set_add(procs, ref->iffeature[i].features[j - 1], 0); |
| 6414 | } |
| 6415 | } |
| 6416 | } else { |
| 6417 | /* forward reference */ |
| 6418 | rc = EXIT_FAILURE; |
| 6419 | goto featurecheckdone; |
| 6420 | } |
| 6421 | } |
| 6422 | |
| 6423 | } |
| 6424 | } |
| 6425 | rc = EXIT_SUCCESS; |
| 6426 | |
| 6427 | featurecheckdone: |
| 6428 | ly_set_free(refs); |
| 6429 | ly_set_free(procs); |
| 6430 | } |
| 6431 | |
| 6432 | break; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6433 | case UNRES_USES: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6434 | rc = resolve_unres_schema_uses(item, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6435 | break; |
| 6436 | case UNRES_TYPE_DFLT: |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6437 | stype = item; |
| 6438 | |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 6439 | rc = check_default(stype, (const char **)str_snode, mod); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6440 | break; |
| 6441 | case UNRES_CHOICE_DFLT: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6442 | expr = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 6443 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6444 | choic = item; |
| 6445 | |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 6446 | if (!choic->dflt) { |
| 6447 | choic->dflt = resolve_choice_dflt(choic, expr); |
| 6448 | } |
Michal Vasko | 7955b36 | 2015-09-04 14:18:15 +0200 | [diff] [blame] | 6449 | if (choic->dflt) { |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 6450 | rc = lyp_check_mandatory_choice((struct lys_node *)choic); |
Michal Vasko | 7955b36 | 2015-09-04 14:18:15 +0200 | [diff] [blame] | 6451 | } else { |
| 6452 | rc = EXIT_FAILURE; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6453 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6454 | break; |
| 6455 | case UNRES_LIST_KEYS: |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 6456 | rc = resolve_list_keys(item, ((struct lys_node_list *)item)->keys_str); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6457 | break; |
| 6458 | case UNRES_LIST_UNIQ: |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6459 | unique_info = (struct unres_list_uniq *)item; |
| 6460 | 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] | 6461 | break; |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 6462 | case UNRES_AUGMENT: |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 6463 | rc = resolve_augment(item, NULL, unres); |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 6464 | break; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6465 | case UNRES_XPATH: |
| 6466 | node = (struct lys_node *)item; |
Michal Vasko | 769f803 | 2017-01-24 13:11:55 +0100 | [diff] [blame] | 6467 | rc = lys_check_xpath(node, 1, final_fail); |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6468 | break; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6469 | case UNRES_EXT: |
| 6470 | ext_data = (struct unres_ext *)str_snode; |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 6471 | extlist = &(*(struct lys_ext_instance ***)item)[ext_data->ext_index]; |
Radek Krejci | a7db970 | 2017-01-20 12:55:14 +0100 | [diff] [blame] | 6472 | rc = resolve_extension(ext_data, extlist, unres); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6473 | if (!rc) { |
| 6474 | if (ext_data->datatype == LYS_IN_YIN) { |
| 6475 | /* YIN */ |
| 6476 | lyxml_free(mod->ctx, ext_data->data.yin); |
| 6477 | } else { |
| 6478 | /* TODO YANG */ |
| 6479 | free(ext_data->data.yang); |
| 6480 | } |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6481 | |
| 6482 | /* is there a callback to be done to finalize the extension? */ |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 6483 | eplugin = extlist[0]->def->plugin; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6484 | if (eplugin) { |
| 6485 | if (eplugin->check_result || (eplugin->flags & LYEXT_OPT_INHERIT)) { |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 6486 | u = malloc(sizeof *u); |
| 6487 | (*u) = ext_data->ext_index; |
| 6488 | unres_schema_add_node(mod, unres, item, UNRES_EXT_FINALIZE, (struct lys_node *)u); |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6489 | } |
| 6490 | } |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 6491 | |
| 6492 | free(ext_data); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6493 | } |
| 6494 | break; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6495 | case UNRES_EXT_FINALIZE: |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 6496 | u = (uint8_t *)str_snode; |
| 6497 | ext = (*(struct lys_ext_instance ***)item)[*u]; |
| 6498 | free(u); |
| 6499 | |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6500 | eplugin = ext->def->plugin; |
| 6501 | |
| 6502 | /* inherit */ |
| 6503 | if ((eplugin->flags & LYEXT_OPT_INHERIT) && (ext->parent_type == LYEXT_PAR_NODE)) { |
| 6504 | root = (struct lys_node *)ext->parent; |
| 6505 | if (!(root->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) { |
| 6506 | LY_TREE_DFS_BEGIN(root->child, next, node) { |
| 6507 | /* first, check if the node already contain instance of the same extension, |
| 6508 | * in such a case we won't inherit. In case the node was actually defined as |
| 6509 | * augment data, we are supposed to check the same way also the augment node itself */ |
| 6510 | if (lys_ext_instance_presence(ext->def, node->ext, node->ext_size) != -1) { |
| 6511 | goto inherit_dfs_sibling; |
| 6512 | } else if (node->parent != root && node->parent->nodetype == LYS_AUGMENT && |
| 6513 | lys_ext_instance_presence(ext->def, node->parent->ext, node->parent->ext_size) != -1) { |
| 6514 | goto inherit_dfs_sibling; |
| 6515 | } |
| 6516 | |
| 6517 | if (eplugin->check_inherit) { |
| 6518 | /* we have a callback to check the inheritance, use it */ |
| 6519 | switch ((rc = (*eplugin->check_inherit)(ext, node))) { |
| 6520 | case 0: |
| 6521 | /* yes - continue with the inheriting code */ |
| 6522 | break; |
| 6523 | case 1: |
| 6524 | /* no - continue with the node's sibling */ |
| 6525 | goto inherit_dfs_sibling; |
| 6526 | case 2: |
| 6527 | /* no, but continue with the children, just skip the inheriting code for this node */ |
| 6528 | goto inherit_dfs_child; |
| 6529 | default: |
| 6530 | LOGERR(LY_EINT, "Plugin's (%s:%s) check_inherit callback returns invalid value (%d),", |
| 6531 | ext->def->module->name, ext->def->name, rc); |
| 6532 | } |
| 6533 | } |
| 6534 | |
| 6535 | /* inherit the extension */ |
| 6536 | extlist = realloc(node->ext, (node->ext_size + 1) * sizeof *node->ext); |
| 6537 | if (!extlist) { |
| 6538 | LOGMEM; |
| 6539 | return -1; |
| 6540 | } |
| 6541 | extlist[node->ext_size] = malloc(sizeof **extlist); |
| 6542 | if (!extlist[node->ext_size]) { |
| 6543 | LOGMEM; |
| 6544 | node->ext = extlist; |
| 6545 | return -1; |
| 6546 | } |
| 6547 | memcpy(extlist[node->ext_size], ext, sizeof *ext); |
| 6548 | extlist[node->ext_size]->flags |= LYEXT_OPT_INHERIT; |
| 6549 | |
| 6550 | node->ext = extlist; |
| 6551 | node->ext_size++; |
| 6552 | |
| 6553 | inherit_dfs_child: |
| 6554 | /* modification of - select element for the next run - children first */ |
| 6555 | if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
| 6556 | next = NULL; |
| 6557 | } else { |
| 6558 | next = node->child; |
| 6559 | } |
| 6560 | if (!next) { |
| 6561 | inherit_dfs_sibling: |
| 6562 | /* no children, try siblings */ |
| 6563 | next = node->next; |
| 6564 | } |
| 6565 | while (!next) { |
| 6566 | /* go to the parent */ |
| 6567 | node = lys_parent(node); |
| 6568 | |
| 6569 | /* we are done if we are back in the root (the starter's parent */ |
| 6570 | if (node == root) { |
| 6571 | break; |
| 6572 | } |
| 6573 | |
| 6574 | /* parent is already processed, go to its sibling */ |
| 6575 | next = node->next; |
| 6576 | } |
| 6577 | } |
| 6578 | } |
| 6579 | } |
| 6580 | |
| 6581 | /* final check */ |
| 6582 | if (eplugin->check_result) { |
| 6583 | if ((*eplugin->check_result)(ext)) { |
| 6584 | return -1; |
| 6585 | } |
| 6586 | } |
| 6587 | |
| 6588 | rc = 0; |
| 6589 | break; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6590 | default: |
| 6591 | LOGINT; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6592 | break; |
| 6593 | } |
| 6594 | |
Radek Krejci | 54081ce | 2016-08-12 15:21:47 +0200 | [diff] [blame] | 6595 | if (has_str && !rc) { |
| 6596 | /* the string is no more needed in case of success. |
| 6597 | * 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] | 6598 | lydict_remove(mod->ctx, str_snode); |
| 6599 | } |
| 6600 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6601 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6602 | } |
| 6603 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6604 | /* logs directly */ |
| 6605 | static void |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6606 | 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] | 6607 | { |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 6608 | struct lyxml_elem *xml; |
| 6609 | struct lyxml_attr *attr; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6610 | struct unres_iffeat_data *iff_data; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6611 | const char *name = NULL; |
| 6612 | struct unres_ext *extinfo; |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 6613 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6614 | switch (type) { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6615 | case UNRES_IDENT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6616 | 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] | 6617 | break; |
| 6618 | case UNRES_TYPE_IDENTREF: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6619 | 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] | 6620 | break; |
| 6621 | case UNRES_TYPE_LEAFREF: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6622 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "leafref", |
| 6623 | ((struct lys_type *)item)->info.lref.path); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6624 | break; |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 6625 | case UNRES_TYPE_DER_TPDF: |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6626 | case UNRES_TYPE_DER: |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 6627 | xml = (struct lyxml_elem *)((struct lys_type *)item)->der; |
| 6628 | if (xml->flags & LY_YANG_STRUCTURE_FLAG) { |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6629 | name = ((struct yang_type *)xml)->name; |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 6630 | } else { |
| 6631 | LY_TREE_FOR(xml->attr, attr) { |
| 6632 | if ((attr->type == LYXML_ATTR_STD) && !strcmp(attr->name, "name")) { |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6633 | name = attr->value; |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 6634 | break; |
| 6635 | } |
| 6636 | } |
| 6637 | assert(attr); |
| 6638 | } |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6639 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "derived type", name); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6640 | break; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6641 | case UNRES_IFFEAT: |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6642 | iff_data = str_node; |
| 6643 | 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] | 6644 | break; |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6645 | case UNRES_FEATURE: |
| 6646 | LOGVRB("There are unresolved if-features for \"%s\" feature circular dependency check, it will be attempted later", |
| 6647 | ((struct lys_feature *)item)->name); |
| 6648 | break; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6649 | case UNRES_USES: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6650 | 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] | 6651 | break; |
| 6652 | case UNRES_TYPE_DFLT: |
Radek Krejci | 2e2de83 | 2016-10-13 16:12:26 +0200 | [diff] [blame] | 6653 | if (str_node) { |
| 6654 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "type default", (char *)str_node); |
| 6655 | } /* else no default value in the type itself, but we are checking some restrictions against |
| 6656 | * possible default value of some base type. The failure is caused by not resolved base type, |
| 6657 | * so it was already reported */ |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6658 | break; |
| 6659 | case UNRES_CHOICE_DFLT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6660 | 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] | 6661 | break; |
| 6662 | case UNRES_LIST_KEYS: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6663 | 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] | 6664 | break; |
| 6665 | case UNRES_LIST_UNIQ: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6666 | 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] | 6667 | break; |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 6668 | case UNRES_AUGMENT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6669 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "augment target", |
| 6670 | ((struct lys_node_augment *)item)->target_name); |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 6671 | break; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6672 | case UNRES_XPATH: |
Michal Vasko | 0d19837 | 2016-11-16 11:40:03 +0100 | [diff] [blame] | 6673 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "XPath expressions of", |
| 6674 | ((struct lys_node *)item)->name); |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6675 | break; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6676 | case UNRES_EXT: |
| 6677 | extinfo = (struct unres_ext *)str_node; |
| 6678 | name = extinfo->datatype == LYS_IN_YIN ? extinfo->data.yin->name : NULL; /* TODO YANG extension */ |
| 6679 | LOGVRB("Resolving extension \"%s\" failed, it will be attempted later.", name); |
| 6680 | break; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6681 | default: |
| 6682 | LOGINT; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6683 | break; |
| 6684 | } |
| 6685 | } |
| 6686 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6687 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6688 | * @brief Resolve every unres schema item in the structure. Logs directly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6689 | * |
| 6690 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6691 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6692 | * |
Michal Vasko | 92b8a38 | 2015-08-19 14:03:49 +0200 | [diff] [blame] | 6693 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6694 | */ |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6695 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 6696 | resolve_unres_schema(struct lys_module *mod, struct unres_schema *unres) |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6697 | { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6698 | uint32_t i, resolved = 0, unres_count, res_count; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6699 | int rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6700 | |
| 6701 | assert(unres); |
| 6702 | |
Michal Vasko | e873426 | 2016-09-29 14:12:06 +0200 | [diff] [blame] | 6703 | LOGVRB("Resolving \"%s\" unresolved schema nodes and their constraints...", mod->name); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6704 | ly_vlog_hide(1); |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6705 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6706 | /* uses */ |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6707 | do { |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6708 | unres_count = 0; |
| 6709 | res_count = 0; |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6710 | |
| 6711 | for (i = 0; i < unres->count; ++i) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6712 | /* UNRES_TYPE_LEAFREF must be resolved (for storing leafref target pointers); |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6713 | * if-features are resolved here to make sure that we will have all if-features for |
| 6714 | * later check of feature circular dependency */ |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6715 | if (unres->type[i] > UNRES_IDENT) { |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6716 | continue; |
| 6717 | } |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6718 | /* 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] | 6719 | * UNRES_AUGMENT, UNRES_CHOICE_DFLT and UNRES_IDENT */ |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6720 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6721 | ++unres_count; |
Michal Vasko | 769f803 | 2017-01-24 13:11:55 +0100 | [diff] [blame] | 6722 | rc = resolve_unres_schema_item(mod, unres->item[i], unres->type[i], unres->str_snode[i], unres, 0); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6723 | if (!rc) { |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6724 | unres->type[i] = UNRES_RESOLVED; |
| 6725 | ++resolved; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6726 | ++res_count; |
Michal Vasko | 89e1532 | 2015-08-17 15:46:55 +0200 | [diff] [blame] | 6727 | } else if (rc == -1) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6728 | ly_vlog_hide(0); |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6729 | /* print the error */ |
Michal Vasko | 769f803 | 2017-01-24 13:11:55 +0100 | [diff] [blame] | 6730 | resolve_unres_schema_item(mod, unres->item[i], unres->type[i], unres->str_snode[i], unres, 1); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6731 | return -1; |
Radek Krejci | c2a180f | 2016-06-22 13:28:16 +0200 | [diff] [blame] | 6732 | } else { |
| 6733 | /* forward reference, erase ly_errno */ |
Radek Krejci | 00a0e71 | 2016-10-26 10:24:46 +0200 | [diff] [blame] | 6734 | ly_err_clean(1); |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6735 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6736 | } |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6737 | } while (res_count && (res_count < unres_count)); |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6738 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6739 | if (res_count < unres_count) { |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6740 | /* just print the errors */ |
| 6741 | ly_vlog_hide(0); |
| 6742 | |
| 6743 | for (i = 0; i < unres->count; ++i) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6744 | if (unres->type[i] > UNRES_IDENT) { |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6745 | continue; |
| 6746 | } |
Michal Vasko | 769f803 | 2017-01-24 13:11:55 +0100 | [diff] [blame] | 6747 | resolve_unres_schema_item(mod, unres->item[i], unres->type[i], unres->str_snode[i], unres, 1); |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6748 | } |
Michal Vasko | 92b8a38 | 2015-08-19 14:03:49 +0200 | [diff] [blame] | 6749 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6750 | } |
| 6751 | |
Radek Krejci | 07d0fb9 | 2017-01-13 14:11:05 +0100 | [diff] [blame] | 6752 | /* the rest except finalizing extensions */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6753 | for (i = 0; i < unres->count; ++i) { |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6754 | if (unres->type[i] == UNRES_RESOLVED || unres->type[i] == UNRES_EXT_FINALIZE) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6755 | continue; |
| 6756 | } |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 6757 | |
Michal Vasko | 769f803 | 2017-01-24 13:11:55 +0100 | [diff] [blame] | 6758 | rc = resolve_unres_schema_item(mod, unres->item[i], unres->type[i], unres->str_snode[i], unres, 0); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6759 | if (rc == 0) { |
Pavol Vican | 88e16c9 | 2016-09-07 15:41:50 +0200 | [diff] [blame] | 6760 | if (unres->type[i] == UNRES_LIST_UNIQ) { |
| 6761 | /* free the allocated structure */ |
| 6762 | free(unres->item[i]); |
| 6763 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6764 | unres->type[i] = UNRES_RESOLVED; |
| 6765 | ++resolved; |
| 6766 | } else if (rc == -1) { |
| 6767 | ly_vlog_hide(0); |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6768 | /* print the error */ |
Michal Vasko | 769f803 | 2017-01-24 13:11:55 +0100 | [diff] [blame] | 6769 | resolve_unres_schema_item(mod, unres->item[i], unres->type[i], unres->str_snode[i], unres, 1); |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6770 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6771 | } |
| 6772 | } |
| 6773 | |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6774 | ly_vlog_hide(0); |
| 6775 | |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6776 | /* finalize extensions, keep it last to provide the complete schema tree information to the plugin's checkers */ |
| 6777 | for (i = 0; i < unres->count; ++i) { |
| 6778 | if (unres->type[i] != UNRES_EXT_FINALIZE) { |
| 6779 | continue; |
| 6780 | } |
| 6781 | |
Radek Krejci | cbba57c | 2017-01-24 13:43:20 +0100 | [diff] [blame^] | 6782 | rc = resolve_unres_schema_item(mod, unres->item[i], unres->type[i], unres->str_snode[i], unres, 0); |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6783 | if (rc == 0) { |
| 6784 | unres->type[i] = UNRES_RESOLVED; |
| 6785 | ++resolved; |
| 6786 | } |
| 6787 | } |
| 6788 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6789 | if (resolved < unres->count) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6790 | /* try to resolve the unresolved nodes again, it will not resolve anything, but it will print |
| 6791 | * all the validation errors |
| 6792 | */ |
| 6793 | for (i = 0; i < unres->count; ++i) { |
| 6794 | if (unres->type[i] == UNRES_RESOLVED) { |
| 6795 | continue; |
| 6796 | } |
Michal Vasko | 769f803 | 2017-01-24 13:11:55 +0100 | [diff] [blame] | 6797 | resolve_unres_schema_item(mod, unres->item[i], unres->type[i], unres->str_snode[i], unres, 1); |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 6798 | if (unres->type[i] == UNRES_XPATH) { |
Michal Vasko | 769f803 | 2017-01-24 13:11:55 +0100 | [diff] [blame] | 6799 | /* XPath referencing an unknown node is actually supposed to be just a warning */ |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 6800 | unres->type[i] = UNRES_RESOLVED; |
| 6801 | resolved++; |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 6802 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6803 | } |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 6804 | if (resolved < unres->count) { |
| 6805 | return -1; |
| 6806 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6807 | } |
| 6808 | |
Michal Vasko | e873426 | 2016-09-29 14:12:06 +0200 | [diff] [blame] | 6809 | LOGVRB("All \"%s\" schema nodes and constraints resolved.", mod->name); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6810 | unres->count = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6811 | return EXIT_SUCCESS; |
| 6812 | } |
| 6813 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6814 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6815 | * @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] | 6816 | * |
| 6817 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6818 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6819 | * @param[in] item Item to resolve. Type determined by \p type. |
| 6820 | * @param[in] type Type of the unresolved item. |
| 6821 | * @param[in] str String argument. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6822 | * |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 6823 | * @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] | 6824 | */ |
| 6825 | int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6826 | unres_schema_add_str(struct lys_module *mod, struct unres_schema *unres, void *item, enum UNRES_ITEM type, |
| 6827 | const char *str) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6828 | { |
Radek Krejci | 54081ce | 2016-08-12 15:21:47 +0200 | [diff] [blame] | 6829 | int rc; |
| 6830 | const char *dictstr; |
| 6831 | |
| 6832 | dictstr = lydict_insert(mod->ctx, str, 0); |
| 6833 | rc = unres_schema_add_node(mod, unres, item, type, (struct lys_node *)dictstr); |
| 6834 | |
Radek Krejci | d9c0ce2 | 2017-01-20 15:20:16 +0100 | [diff] [blame] | 6835 | if (rc < 0) { |
Radek Krejci | 54081ce | 2016-08-12 15:21:47 +0200 | [diff] [blame] | 6836 | lydict_remove(mod->ctx, dictstr); |
| 6837 | } |
| 6838 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6839 | } |
| 6840 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6841 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6842 | * @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] | 6843 | * |
| 6844 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6845 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6846 | * @param[in] item Item to resolve. Type determined by \p type. |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6847 | * @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] | 6848 | * @param[in] snode Schema node argument. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6849 | * |
Radek Krejci | d9c0ce2 | 2017-01-20 15:20:16 +0100 | [diff] [blame] | 6850 | * @return EXIT_SUCCESS on success, EXIT_FIALURE on storing the item in unres, -1 on error, -2 if the unres item |
| 6851 | * is already in the unres list. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6852 | */ |
| 6853 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 6854 | 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] | 6855 | struct lys_node *snode) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6856 | { |
Michal Vasko | ef486d7 | 2016-09-27 12:10:44 +0200 | [diff] [blame] | 6857 | int rc, log_hidden; |
Radek Krejci | 850a5de | 2016-11-08 14:06:40 +0100 | [diff] [blame] | 6858 | uint32_t u; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6859 | struct lyxml_elem *yin; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6860 | |
Michal Vasko | 9bf425b | 2015-10-22 11:42:03 +0200 | [diff] [blame] | 6861 | assert(unres && item && ((type != UNRES_LEAFREF) && (type != UNRES_INSTID) && (type != UNRES_WHEN) |
| 6862 | && (type != UNRES_MUST))); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6863 | |
Radek Krejci | 850a5de | 2016-11-08 14:06:40 +0100 | [diff] [blame] | 6864 | /* check for duplicities in unres */ |
| 6865 | for (u = 0; u < unres->count; u++) { |
| 6866 | if (unres->type[u] == type && unres->item[u] == item && |
| 6867 | unres->str_snode[u] == snode && unres->module[u] == mod) { |
| 6868 | /* duplication, will be resolved later */ |
Radek Krejci | d9c0ce2 | 2017-01-20 15:20:16 +0100 | [diff] [blame] | 6869 | return -2; |
Radek Krejci | 850a5de | 2016-11-08 14:06:40 +0100 | [diff] [blame] | 6870 | } |
| 6871 | } |
| 6872 | |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6873 | if (type != UNRES_EXT_FINALIZE) { |
| 6874 | /* extension finalization is not even tried when adding the item into the inres list */ |
Michal Vasko | ef486d7 | 2016-09-27 12:10:44 +0200 | [diff] [blame] | 6875 | |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6876 | if (*ly_vlog_hide_location()) { |
| 6877 | log_hidden = 1; |
| 6878 | } else { |
| 6879 | log_hidden = 0; |
| 6880 | ly_vlog_hide(1); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6881 | } |
Radek Krejci | cbba57c | 2017-01-24 13:43:20 +0100 | [diff] [blame^] | 6882 | rc = resolve_unres_schema_item(mod, item, type, snode, unres, 0); |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6883 | if (!log_hidden) { |
| 6884 | ly_vlog_hide(0); |
| 6885 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6886 | |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6887 | if (rc != EXIT_FAILURE) { |
| 6888 | if (rc == -1 && ly_errno == LY_EVALID) { |
| 6889 | ly_err_repeat(); |
| 6890 | } |
| 6891 | if (type == UNRES_LIST_UNIQ) { |
| 6892 | /* free the allocated structure */ |
| 6893 | free(item); |
| 6894 | } else if (rc == -1 && type == UNRES_IFFEAT) { |
| 6895 | /* free the allocated resources */ |
| 6896 | free(*((char **)item)); |
| 6897 | } |
| 6898 | return rc; |
| 6899 | } else { |
| 6900 | /* erase info about validation errors */ |
| 6901 | ly_err_clean(1); |
| 6902 | } |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6903 | |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6904 | print_unres_schema_item_fail(item, type, snode); |
| 6905 | |
| 6906 | /* HACK unlinking is performed here so that we do not do any (NS) copying in vain */ |
| 6907 | if (type == UNRES_TYPE_DER || type == UNRES_TYPE_DER_TPDF) { |
| 6908 | yin = (struct lyxml_elem *)((struct lys_type *)item)->der; |
| 6909 | if (!(yin->flags & LY_YANG_STRUCTURE_FLAG)) { |
| 6910 | lyxml_unlink_elem(mod->ctx, yin, 1); |
| 6911 | ((struct lys_type *)item)->der = (struct lys_tpdf *)yin; |
| 6912 | } |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6913 | } |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6914 | } |
| 6915 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6916 | unres->count++; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 6917 | unres->item = ly_realloc(unres->item, unres->count*sizeof *unres->item); |
| 6918 | if (!unres->item) { |
| 6919 | LOGMEM; |
| 6920 | return -1; |
| 6921 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6922 | unres->item[unres->count-1] = item; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 6923 | unres->type = ly_realloc(unres->type, unres->count*sizeof *unres->type); |
| 6924 | if (!unres->type) { |
| 6925 | LOGMEM; |
| 6926 | return -1; |
| 6927 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6928 | unres->type[unres->count-1] = type; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 6929 | unres->str_snode = ly_realloc(unres->str_snode, unres->count*sizeof *unres->str_snode); |
| 6930 | if (!unres->str_snode) { |
| 6931 | LOGMEM; |
| 6932 | return -1; |
| 6933 | } |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6934 | unres->str_snode[unres->count-1] = snode; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6935 | unres->module = ly_realloc(unres->module, unres->count*sizeof *unres->module); |
| 6936 | if (!unres->module) { |
| 6937 | LOGMEM; |
| 6938 | return -1; |
| 6939 | } |
| 6940 | unres->module[unres->count-1] = mod; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6941 | |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 6942 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6943 | } |
| 6944 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6945 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6946 | * @brief Duplicate an unres schema item. Logs indirectly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6947 | * |
| 6948 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6949 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6950 | * @param[in] item Old item to be resolved. |
| 6951 | * @param[in] type Type of the old unresolved item. |
| 6952 | * @param[in] new_item New item to use in the duplicate. |
| 6953 | * |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 6954 | * @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] | 6955 | */ |
Michal Vasko | dad1940 | 2015-08-06 09:51:53 +0200 | [diff] [blame] | 6956 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 6957 | 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] | 6958 | { |
| 6959 | int i; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6960 | struct unres_list_uniq aux_uniq; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6961 | struct unres_iffeat_data *iff_data; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6962 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6963 | 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] | 6964 | |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6965 | /* hack for UNRES_LIST_UNIQ, which stores multiple items behind its item */ |
| 6966 | if (type == UNRES_LIST_UNIQ) { |
| 6967 | aux_uniq.list = item; |
| 6968 | aux_uniq.expr = ((struct unres_list_uniq *)new_item)->expr; |
| 6969 | item = &aux_uniq; |
| 6970 | } |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 6971 | i = unres_schema_find(unres, -1, item, type); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6972 | |
| 6973 | if (i == -1) { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6974 | if (type == UNRES_LIST_UNIQ) { |
| 6975 | free(new_item); |
| 6976 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 6977 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6978 | } |
| 6979 | |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6980 | if ((type == UNRES_TYPE_LEAFREF) || (type == UNRES_USES) || (type == UNRES_TYPE_DFLT) || |
Radek Krejci | b69f323 | 2016-09-16 17:41:07 +0200 | [diff] [blame] | 6981 | (type == UNRES_FEATURE) || (type == UNRES_LIST_UNIQ)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6982 | 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] | 6983 | LOGINT; |
| 6984 | return -1; |
| 6985 | } |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6986 | } else if (type == UNRES_IFFEAT) { |
| 6987 | /* duplicate unres_iffeature_data */ |
| 6988 | iff_data = malloc(sizeof *iff_data); |
| 6989 | iff_data->fname = lydict_insert(mod->ctx, ((struct unres_iffeat_data *)unres->str_snode[i])->fname, 0); |
| 6990 | iff_data->node = ((struct unres_iffeat_data *)unres->str_snode[i])->node; |
| 6991 | if (unres_schema_add_node(mod, unres, new_item, type, (struct lys_node *)iff_data) == -1) { |
| 6992 | LOGINT; |
| 6993 | return -1; |
| 6994 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6995 | } else { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6996 | 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] | 6997 | LOGINT; |
| 6998 | return -1; |
| 6999 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7000 | } |
Michal Vasko | dad1940 | 2015-08-06 09:51:53 +0200 | [diff] [blame] | 7001 | |
| 7002 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7003 | } |
| 7004 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7005 | /* does not log */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7006 | int |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7007 | 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] | 7008 | { |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7009 | int i; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7010 | struct unres_list_uniq *aux_uniq1, *aux_uniq2; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7011 | |
Radek Krejci | ddddd0d | 2017-01-20 15:20:46 +0100 | [diff] [blame] | 7012 | if (start_on_backwards >= 0) { |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7013 | i = start_on_backwards; |
| 7014 | } else { |
| 7015 | i = unres->count - 1; |
| 7016 | } |
| 7017 | for (; i > -1; i--) { |
| 7018 | if (unres->type[i] != type) { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7019 | continue; |
| 7020 | } |
| 7021 | if (type != UNRES_LIST_UNIQ) { |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7022 | if (unres->item[i] == item) { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7023 | break; |
| 7024 | } |
| 7025 | } else { |
| 7026 | aux_uniq1 = (struct unres_list_uniq *)unres->item[i - 1]; |
| 7027 | aux_uniq2 = (struct unres_list_uniq *)item; |
| 7028 | 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] | 7029 | break; |
| 7030 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7031 | } |
| 7032 | } |
| 7033 | |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7034 | return i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7035 | } |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7036 | |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7037 | static void |
| 7038 | unres_schema_free_item(struct ly_ctx *ctx, struct unres_schema *unres, uint32_t i) |
| 7039 | { |
| 7040 | struct lyxml_elem *yin; |
| 7041 | struct yang_type *yang; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7042 | struct unres_iffeat_data *iff_data; |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7043 | |
| 7044 | switch (unres->type[i]) { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 7045 | case UNRES_TYPE_DER_TPDF: |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7046 | case UNRES_TYPE_DER: |
| 7047 | yin = (struct lyxml_elem *)((struct lys_type *)unres->item[i])->der; |
| 7048 | if (yin->flags & LY_YANG_STRUCTURE_FLAG) { |
| 7049 | yang =(struct yang_type *)yin; |
Pavol Vican | cf2af4d | 2016-12-21 14:13:06 +0100 | [diff] [blame] | 7050 | ((struct lys_type *)unres->item[i])->base = yang->base; |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7051 | lydict_remove(ctx, yang->name); |
| 7052 | free(yang); |
Pavol Vican | cf2af4d | 2016-12-21 14:13:06 +0100 | [diff] [blame] | 7053 | if (((struct lys_type *)unres->item[i])->base == LY_TYPE_UNION) { |
| 7054 | yang_free_type_union(ctx, (struct lys_type *)unres->item[i]); |
| 7055 | } |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7056 | } else { |
| 7057 | lyxml_free(ctx, yin); |
| 7058 | } |
| 7059 | break; |
Pavol Vican | 88e16c9 | 2016-09-07 15:41:50 +0200 | [diff] [blame] | 7060 | case UNRES_IFFEAT: |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7061 | iff_data = (struct unres_iffeat_data *)unres->str_snode[i]; |
| 7062 | lydict_remove(ctx, iff_data->fname); |
| 7063 | free(unres->str_snode[i]); |
Pavol Vican | 88e16c9 | 2016-09-07 15:41:50 +0200 | [diff] [blame] | 7064 | break; |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7065 | case UNRES_IDENT: |
| 7066 | case UNRES_TYPE_IDENTREF: |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7067 | case UNRES_CHOICE_DFLT: |
| 7068 | case UNRES_LIST_KEYS: |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7069 | lydict_remove(ctx, (const char *)unres->str_snode[i]); |
| 7070 | break; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7071 | case UNRES_LIST_UNIQ: |
| 7072 | free(unres->item[i]); |
| 7073 | break; |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7074 | default: |
| 7075 | break; |
| 7076 | } |
| 7077 | unres->type[i] = UNRES_RESOLVED; |
| 7078 | } |
| 7079 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7080 | void |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7081 | unres_schema_free(struct lys_module *module, struct unres_schema **unres) |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7082 | { |
| 7083 | uint32_t i; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7084 | unsigned int unresolved = 0; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7085 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7086 | if (!unres || !(*unres)) { |
| 7087 | return; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7088 | } |
| 7089 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7090 | assert(module || (*unres)->count == 0); |
| 7091 | |
| 7092 | for (i = 0; i < (*unres)->count; ++i) { |
| 7093 | if ((*unres)->module[i] != module) { |
| 7094 | if ((*unres)->type[i] != UNRES_RESOLVED) { |
| 7095 | unresolved++; |
| 7096 | } |
| 7097 | continue; |
| 7098 | } |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7099 | |
| 7100 | /* free heap memory for the specific item */ |
| 7101 | unres_schema_free_item(module->ctx, *unres, i); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7102 | } |
| 7103 | |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7104 | /* free it all */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7105 | if (!module || (!unresolved && !module->type)) { |
| 7106 | free((*unres)->item); |
| 7107 | free((*unres)->type); |
| 7108 | free((*unres)->str_snode); |
| 7109 | free((*unres)->module); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7110 | free((*unres)); |
| 7111 | (*unres) = NULL; |
| 7112 | } |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7113 | } |
| 7114 | |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7115 | static int |
| 7116 | check_instid_ext_dep(const struct lys_node *sleaf, const char *json_instid) |
| 7117 | { |
| 7118 | struct ly_set *set; |
Michal Vasko | 3c77709 | 2017-01-17 14:10:09 +0100 | [diff] [blame] | 7119 | struct lys_node *op_node, *first_node; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7120 | char *buf; |
| 7121 | |
| 7122 | for (op_node = lys_parent(sleaf); |
| 7123 | op_node && !(op_node->nodetype & (LYS_NOTIF | LYS_RPC | LYS_ACTION)); |
| 7124 | op_node = lys_parent(op_node)); |
| 7125 | |
| 7126 | if (op_node && lys_parent(op_node)) { |
| 7127 | /* nested operation - any absolute path is external */ |
| 7128 | return 1; |
| 7129 | } |
| 7130 | |
| 7131 | /* get the first node from the instid */ |
| 7132 | buf = strndup(json_instid, strchr(json_instid + 1, '/') - json_instid); |
| 7133 | if (!buf) { |
| 7134 | LOGMEM; |
| 7135 | return -1; |
| 7136 | } |
| 7137 | |
| 7138 | /* there is a predicate, remove it */ |
| 7139 | if (buf[strlen(buf) - 1] == ']') { |
| 7140 | assert(strchr(buf, '[')); |
| 7141 | *strchr(buf, '[') = '\0'; |
| 7142 | } |
| 7143 | |
| 7144 | /* find the first schema node */ |
Michal Vasko | 2611e19 | 2017-01-23 10:33:21 +0100 | [diff] [blame] | 7145 | set = lys_find_xpath(NULL, sleaf, buf, 0); |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7146 | if (!set || !set->number) { |
| 7147 | free(buf); |
Michal Vasko | 29fd974 | 2017-01-23 09:55:44 +0100 | [diff] [blame] | 7148 | ly_set_free(set); |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7149 | return 1; |
| 7150 | } |
| 7151 | free(buf); |
| 7152 | |
Michal Vasko | 3c77709 | 2017-01-17 14:10:09 +0100 | [diff] [blame] | 7153 | first_node = set->set.s[0]; |
| 7154 | ly_set_free(set); |
| 7155 | |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7156 | /* based on the first schema node in the path we can decide whether it points to an external tree or not */ |
| 7157 | |
| 7158 | if (op_node) { |
| 7159 | /* it is an operation, so we're good if it points somewhere inside it */ |
Michal Vasko | 3c77709 | 2017-01-17 14:10:09 +0100 | [diff] [blame] | 7160 | if (op_node == first_node) { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7161 | assert(set->number == 1); |
| 7162 | return 0; |
| 7163 | } else { |
| 7164 | return 1; |
| 7165 | } |
| 7166 | } |
| 7167 | |
| 7168 | /* we cannot know whether it points to a tree that is going to be unlinked (application must handle |
| 7169 | * this itself), so we say it's not external */ |
| 7170 | return 0; |
| 7171 | } |
| 7172 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7173 | /** |
| 7174 | * @brief Resolve instance-identifier in JSON data format. Logs directly. |
| 7175 | * |
| 7176 | * @param[in] data Data node where the path is used |
| 7177 | * @param[in] path Instance-identifier node value. |
| 7178 | * @param[in,out] ret Resolved instance or NULL. |
| 7179 | * |
| 7180 | * @return 0 on success (even if unresolved and \p ret is NULL), -1 on error. |
| 7181 | */ |
| 7182 | static int |
| 7183 | resolve_instid(struct lyd_node *data, const char *path, int req_inst, struct lyd_node **ret) |
| 7184 | { |
| 7185 | int i = 0, j; |
| 7186 | const struct lys_module *mod; |
| 7187 | struct ly_ctx *ctx = data->schema->module->ctx; |
| 7188 | const char *model, *name; |
| 7189 | char *str; |
| 7190 | int mod_len, name_len, has_predicate; |
| 7191 | struct unres_data node_match; |
| 7192 | |
| 7193 | memset(&node_match, 0, sizeof node_match); |
| 7194 | *ret = NULL; |
| 7195 | |
| 7196 | /* we need root to resolve absolute path */ |
| 7197 | for (; data->parent; data = data->parent); |
| 7198 | /* we're still parsing it and the pointer is not correct yet */ |
| 7199 | if (data->prev) { |
| 7200 | for (; data->prev->next; data = data->prev); |
| 7201 | } |
| 7202 | |
| 7203 | /* search for the instance node */ |
| 7204 | while (path[i]) { |
| 7205 | j = parse_instance_identifier(&path[i], &model, &mod_len, &name, &name_len, &has_predicate); |
| 7206 | if (j <= 0) { |
| 7207 | LOGVAL(LYE_INCHAR, LY_VLOG_LYD, data, path[i-j], &path[i-j]); |
| 7208 | goto error; |
| 7209 | } |
| 7210 | i += j; |
| 7211 | |
| 7212 | str = strndup(model, mod_len); |
| 7213 | if (!str) { |
| 7214 | LOGMEM; |
| 7215 | goto error; |
| 7216 | } |
| 7217 | mod = ly_ctx_get_module(ctx, str, NULL); |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 7218 | if (ctx->data_clb) { |
| 7219 | if (!mod) { |
| 7220 | mod = ctx->data_clb(ctx, str, NULL, 0, ctx->data_clb_data); |
| 7221 | } else if (!mod->implemented) { |
| 7222 | mod = ctx->data_clb(ctx, mod->name, mod->ns, LY_MODCLB_NOT_IMPLEMENTED, ctx->data_clb_data); |
| 7223 | } |
| 7224 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7225 | free(str); |
| 7226 | |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 7227 | if (!mod || !mod->implemented || mod->disabled) { |
| 7228 | break; |
| 7229 | } |
| 7230 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7231 | if (resolve_data(mod, name, name_len, data, &node_match)) { |
| 7232 | /* no instance exists */ |
| 7233 | break; |
| 7234 | } |
| 7235 | |
| 7236 | if (has_predicate) { |
| 7237 | /* we have predicate, so the current results must be list or leaf-list */ |
| 7238 | j = resolve_predicate(&path[i], &node_match); |
| 7239 | if (j < 1) { |
| 7240 | LOGVAL(LYE_INPRED, LY_VLOG_LYD, data, &path[i-j]); |
| 7241 | goto error; |
| 7242 | } |
| 7243 | i += j; |
| 7244 | |
| 7245 | if (!node_match.count) { |
| 7246 | /* no instance exists */ |
| 7247 | break; |
| 7248 | } |
| 7249 | } |
| 7250 | } |
| 7251 | |
| 7252 | if (!node_match.count) { |
| 7253 | /* no instance exists */ |
| 7254 | if (req_inst > -1) { |
| 7255 | LOGVAL(LYE_NOREQINS, LY_VLOG_NONE, NULL, path); |
| 7256 | return EXIT_FAILURE; |
| 7257 | } |
| 7258 | LOGVRB("There is no instance of \"%s\", but it is not required.", path); |
| 7259 | return EXIT_SUCCESS; |
| 7260 | } else if (node_match.count > 1) { |
| 7261 | /* instance identifier must resolve to a single node */ |
| 7262 | LOGVAL(LYE_TOOMANY, LY_VLOG_LYD, data, path, "data tree"); |
| 7263 | goto error; |
| 7264 | } else { |
| 7265 | /* we have required result, remember it and cleanup */ |
| 7266 | *ret = node_match.node[0]; |
| 7267 | free(node_match.node); |
| 7268 | return EXIT_SUCCESS; |
| 7269 | } |
| 7270 | |
| 7271 | error: |
| 7272 | /* cleanup */ |
| 7273 | free(node_match.node); |
| 7274 | return -1; |
| 7275 | } |
| 7276 | |
| 7277 | static int |
| 7278 | resolve_leafref(struct lyd_node_leaf_list *leaf, const char *path, int req_inst, struct lyd_node **ret) |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7279 | { |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7280 | struct unres_data matches; |
| 7281 | uint32_t i; |
| 7282 | |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 7283 | /* init */ |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7284 | memset(&matches, 0, sizeof matches); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7285 | *ret = NULL; |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7286 | |
| 7287 | /* EXIT_FAILURE return keeps leaf->value.lefref NULL, handled later */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7288 | if (resolve_path_arg_data((struct lyd_node *)leaf, path, &matches) == -1) { |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7289 | return -1; |
| 7290 | } |
| 7291 | |
| 7292 | /* check that value matches */ |
| 7293 | for (i = 0; i < matches.count; ++i) { |
Radek Krejci | 1899d6a | 2016-11-03 13:48:07 +0100 | [diff] [blame] | 7294 | /* not that the value is already in canonical form since the parsers does the conversion, |
| 7295 | * so we can simply compare just the values */ |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7296 | 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] | 7297 | /* we have the match */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7298 | *ret = matches.node[i]; |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7299 | break; |
| 7300 | } |
| 7301 | } |
| 7302 | |
| 7303 | free(matches.node); |
| 7304 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7305 | if (!*ret) { |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7306 | /* reference not found */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7307 | if (req_inst > -1) { |
| 7308 | LOGVAL(LYE_NOLEAFREF, LY_VLOG_LYD, leaf, path, leaf->value_str); |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7309 | return EXIT_FAILURE; |
| 7310 | } else { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7311 | LOGVRB("There is no leafref \"%s\" with the value \"%s\", but it is not required.", path, leaf->value_str); |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7312 | } |
| 7313 | } |
| 7314 | |
| 7315 | return EXIT_SUCCESS; |
| 7316 | } |
| 7317 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7318 | /* ignore fail because we are parsing edit-config, get, or get-config - but only if the union includes leafref or instid */ |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7319 | int |
| 7320 | resolve_union(struct lyd_node_leaf_list *leaf, struct lys_type *type, int store, int ignore_fail, |
| 7321 | struct lys_type **resolved_type) |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 7322 | { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7323 | struct lys_type *t; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7324 | struct lyd_node *ret; |
| 7325 | int found, hidden, success = 0, ext_dep, req_inst; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7326 | const char *json_val = NULL; |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 7327 | |
| 7328 | assert(type->base == LY_TYPE_UNION); |
| 7329 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7330 | if ((leaf->value_type == LY_TYPE_UNION) || (leaf->value_type == (LY_TYPE_INST | LY_TYPE_INST_UNRES))) { |
| 7331 | /* either NULL or instid previously converted to JSON */ |
| 7332 | json_val = leaf->value.string; |
| 7333 | } |
Michal Vasko | 1c8567a | 2017-01-05 13:42:27 +0100 | [diff] [blame] | 7334 | |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7335 | if (store) { |
| 7336 | if ((leaf->value_type & LY_DATA_TYPE_MASK) == LY_TYPE_BITS) { |
| 7337 | free(leaf->value.bit); |
| 7338 | } |
| 7339 | memset(&leaf->value, 0, sizeof leaf->value); |
Michal Vasko | 1c8567a | 2017-01-05 13:42:27 +0100 | [diff] [blame] | 7340 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7341 | |
| 7342 | /* turn logging off, we are going to try to validate the value with all the types in order */ |
| 7343 | hidden = *ly_vlog_hide_location(); |
| 7344 | ly_vlog_hide(1); |
| 7345 | |
| 7346 | t = NULL; |
| 7347 | found = 0; |
| 7348 | while ((t = lyp_get_next_union_type(type, t, &found))) { |
| 7349 | found = 0; |
| 7350 | |
| 7351 | switch (t->base) { |
| 7352 | case LY_TYPE_LEAFREF: |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7353 | if ((ignore_fail == 1) || ((leaf->schema->flags & LYS_LEAFREF_DEP) && (ignore_fail == 2))) { |
| 7354 | req_inst = -1; |
| 7355 | } else { |
| 7356 | req_inst = t->info.lref.req; |
| 7357 | } |
| 7358 | |
| 7359 | if (!resolve_leafref(leaf, t->info.lref.path, req_inst, &ret)) { |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7360 | if (store) { |
| 7361 | if (ret && !(leaf->schema->flags & LYS_LEAFREF_DEP)) { |
| 7362 | /* valid resolved */ |
| 7363 | leaf->value.leafref = ret; |
| 7364 | leaf->value_type = LY_TYPE_LEAFREF; |
| 7365 | } else { |
| 7366 | /* valid unresolved */ |
| 7367 | if (!lyp_parse_value(t, &leaf->value_str, NULL, leaf, 1, 0)) { |
| 7368 | return -1; |
| 7369 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7370 | } |
| 7371 | } |
| 7372 | |
| 7373 | success = 1; |
| 7374 | } |
| 7375 | break; |
| 7376 | case LY_TYPE_INST: |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7377 | ext_dep = check_instid_ext_dep(leaf->schema, (json_val ? json_val : leaf->value_str)); |
| 7378 | if ((ignore_fail == 1) || (ext_dep && (ignore_fail == 2))) { |
| 7379 | req_inst = -1; |
| 7380 | } else { |
| 7381 | req_inst = t->info.inst.req; |
| 7382 | } |
| 7383 | |
Michal Vasko | d3a0311 | 2017-01-23 09:56:02 +0100 | [diff] [blame] | 7384 | if (!resolve_instid((struct lyd_node *)leaf, (json_val ? json_val : leaf->value_str), req_inst, &ret)) { |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7385 | if (store) { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7386 | if (ret && !ext_dep) { |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7387 | /* valid resolved */ |
| 7388 | leaf->value.instance = ret; |
| 7389 | leaf->value_type = LY_TYPE_INST; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7390 | |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7391 | if (json_val) { |
| 7392 | lydict_remove(leaf->schema->module->ctx, leaf->value_str); |
| 7393 | leaf->value_str = json_val; |
| 7394 | json_val = NULL; |
| 7395 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7396 | } else { |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7397 | /* valid unresolved */ |
| 7398 | if (json_val) { |
| 7399 | /* put the JSON val back */ |
| 7400 | leaf->value.string = json_val; |
| 7401 | json_val = NULL; |
| 7402 | } else { |
| 7403 | leaf->value.instance = NULL; |
| 7404 | } |
| 7405 | leaf->value_type = LY_TYPE_INST | LY_TYPE_INST_UNRES; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7406 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7407 | } |
| 7408 | |
| 7409 | success = 1; |
| 7410 | } |
| 7411 | break; |
| 7412 | default: |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7413 | if (lyp_parse_value(t, &leaf->value_str, NULL, leaf, store, 0)) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7414 | success = 1; |
| 7415 | } |
| 7416 | break; |
| 7417 | } |
| 7418 | |
| 7419 | if (success) { |
| 7420 | break; |
| 7421 | } |
| 7422 | |
| 7423 | /* erase information about errors - they are false or irrelevant |
| 7424 | * and will be replaced by a single error messages */ |
| 7425 | ly_err_clean(1); |
| 7426 | |
| 7427 | /* erase possible present and invalid value data */ |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7428 | if (store) { |
| 7429 | if (t->base == LY_TYPE_BITS) { |
| 7430 | free(leaf->value.bit); |
| 7431 | } |
| 7432 | memset(&leaf->value, 0, sizeof leaf->value); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7433 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7434 | } |
| 7435 | |
| 7436 | /* turn logging back on */ |
| 7437 | if (!hidden) { |
| 7438 | ly_vlog_hide(0); |
| 7439 | } |
| 7440 | |
| 7441 | if (json_val) { |
| 7442 | if (!success) { |
| 7443 | /* put the value back for now */ |
| 7444 | assert(leaf->value_type == LY_TYPE_UNION); |
| 7445 | leaf->value.string = json_val; |
| 7446 | } else { |
| 7447 | /* value was ultimately useless, but we could not have known */ |
| 7448 | lydict_remove(leaf->schema->module->ctx, json_val); |
| 7449 | } |
| 7450 | } |
| 7451 | |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7452 | if (success) { |
| 7453 | if (resolved_type) { |
| 7454 | *resolved_type = t; |
| 7455 | } |
| 7456 | } else if (!ignore_fail || !type->info.uni.has_ptr_type) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7457 | /* not found and it is required */ |
| 7458 | LOGVAL(LYE_INVAL, LY_VLOG_LYD, leaf, leaf->value_str ? leaf->value_str : "", leaf->schema->name); |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 7459 | return EXIT_FAILURE; |
| 7460 | } |
| 7461 | |
| 7462 | return EXIT_SUCCESS; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7463 | |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 7464 | } |
| 7465 | |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7466 | /** |
| 7467 | * @brief Resolve a single unres data item. Logs directly. |
| 7468 | * |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7469 | * @param[in] node Data node to resolve. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7470 | * @param[in] type Type of the unresolved item. |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7471 | * @param[in] ignore_fail 0 - no, 1 - yes, 2 - yes, but only for external dependencies. |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7472 | * |
| 7473 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 7474 | */ |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 7475 | int |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7476 | resolve_unres_data_item(struct lyd_node *node, enum UNRES_ITEM type, int ignore_fail) |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7477 | { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7478 | int rc, req_inst, ext_dep; |
Michal Vasko | 83a6c46 | 2015-10-08 16:43:53 +0200 | [diff] [blame] | 7479 | struct lyd_node_leaf_list *leaf; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7480 | struct lyd_node *ret; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7481 | struct lys_node_leaf *sleaf; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7482 | |
Michal Vasko | 83a6c46 | 2015-10-08 16:43:53 +0200 | [diff] [blame] | 7483 | leaf = (struct lyd_node_leaf_list *)node; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7484 | sleaf = (struct lys_node_leaf *)leaf->schema; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7485 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7486 | switch (type) { |
| 7487 | case UNRES_LEAFREF: |
| 7488 | assert(sleaf->type.base == LY_TYPE_LEAFREF); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7489 | assert(leaf->validity & LYD_VAL_LEAFREF); |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7490 | if ((ignore_fail == 1) || ((leaf->schema->flags & LYS_LEAFREF_DEP) && (ignore_fail == 2))) { |
| 7491 | req_inst = -1; |
| 7492 | } else { |
| 7493 | req_inst = sleaf->type.info.lref.req; |
| 7494 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7495 | rc = resolve_leafref(leaf, sleaf->type.info.lref.path, req_inst, &ret); |
| 7496 | if (!rc) { |
Michal Vasko | b1ac872 | 2017-01-02 13:04:25 +0100 | [diff] [blame] | 7497 | if (ret && !(leaf->schema->flags & LYS_LEAFREF_DEP)) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7498 | /* valid resolved */ |
Michal Vasko | 1c8567a | 2017-01-05 13:42:27 +0100 | [diff] [blame] | 7499 | if ((leaf->value_type & LY_DATA_TYPE_MASK) == LY_TYPE_BITS) { |
| 7500 | free(leaf->value.bit); |
| 7501 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7502 | leaf->value.leafref = ret; |
| 7503 | leaf->value_type = LY_TYPE_LEAFREF; |
| 7504 | } else { |
| 7505 | /* valid unresolved */ |
| 7506 | if (!(leaf->value_type & LY_TYPE_LEAFREF_UNRES)) { |
| 7507 | if (!lyp_parse_value(&sleaf->type, &leaf->value_str, NULL, leaf, 1, 0)) { |
| 7508 | return -1; |
| 7509 | } |
| 7510 | } |
| 7511 | } |
| 7512 | leaf->validity &= ~LYD_VAL_LEAFREF; |
| 7513 | } else { |
| 7514 | return rc; |
| 7515 | } |
| 7516 | break; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7517 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7518 | case UNRES_INSTID: |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7519 | assert(sleaf->type.base == LY_TYPE_INST); |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7520 | ext_dep = check_instid_ext_dep(leaf->schema, leaf->value_str); |
| 7521 | if (ext_dep == -1) { |
| 7522 | return -1; |
| 7523 | } |
| 7524 | |
| 7525 | if ((ignore_fail == 1) || (ext_dep && (ignore_fail == 2))) { |
| 7526 | req_inst = -1; |
| 7527 | } else { |
| 7528 | req_inst = sleaf->type.info.inst.req; |
| 7529 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7530 | rc = resolve_instid(node, leaf->value_str, req_inst, &ret); |
| 7531 | if (!rc) { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7532 | if (ret && !ext_dep) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7533 | /* valid resolved */ |
| 7534 | leaf->value.instance = ret; |
| 7535 | leaf->value_type = LY_TYPE_INST; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7536 | } else { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7537 | /* valid unresolved */ |
| 7538 | leaf->value.instance = NULL; |
| 7539 | leaf->value_type = LY_TYPE_INST | LY_TYPE_INST_UNRES; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7540 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7541 | } else { |
| 7542 | return rc; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7543 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7544 | break; |
| 7545 | |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7546 | case UNRES_UNION: |
| 7547 | assert(sleaf->type.base == LY_TYPE_UNION); |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7548 | return resolve_union(leaf, &sleaf->type, 1, ignore_fail, NULL); |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7549 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7550 | case UNRES_WHEN: |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7551 | if ((rc = resolve_when(node, NULL, ignore_fail))) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7552 | return rc; |
| 7553 | } |
| 7554 | break; |
| 7555 | |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 7556 | case UNRES_MUST: |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7557 | if ((rc = resolve_must(node, 0, ignore_fail))) { |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 7558 | return rc; |
| 7559 | } |
| 7560 | break; |
| 7561 | |
| 7562 | case UNRES_MUST_INOUT: |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7563 | if ((rc = resolve_must(node, 1, ignore_fail))) { |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 7564 | return rc; |
| 7565 | } |
| 7566 | break; |
| 7567 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7568 | default: |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7569 | LOGINT; |
| 7570 | return -1; |
| 7571 | } |
| 7572 | |
| 7573 | return EXIT_SUCCESS; |
| 7574 | } |
| 7575 | |
| 7576 | /** |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7577 | * @brief add data unres item |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7578 | * |
| 7579 | * @param[in] unres Unres data structure to use. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7580 | * @param[in] node Data node to use. |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7581 | * |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7582 | * @return 0 on success, -1 on error. |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7583 | */ |
| 7584 | int |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7585 | 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] | 7586 | { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7587 | assert(unres && node); |
Michal Vasko | c428084 | 2016-04-19 16:10:42 +0200 | [diff] [blame] | 7588 | 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] | 7589 | || (type == UNRES_MUST_INOUT) || (type == UNRES_UNION)); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7590 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7591 | unres->count++; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 7592 | unres->node = ly_realloc(unres->node, unres->count * sizeof *unres->node); |
| 7593 | if (!unres->node) { |
| 7594 | LOGMEM; |
| 7595 | return -1; |
| 7596 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7597 | unres->node[unres->count - 1] = node; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 7598 | unres->type = ly_realloc(unres->type, unres->count * sizeof *unres->type); |
| 7599 | if (!unres->type) { |
| 7600 | LOGMEM; |
| 7601 | return -1; |
| 7602 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7603 | unres->type[unres->count - 1] = type; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7604 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7605 | if (type == UNRES_WHEN) { |
| 7606 | /* remove previous result */ |
| 7607 | node->when_status = LYD_WHEN; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7608 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7609 | |
| 7610 | return EXIT_SUCCESS; |
| 7611 | } |
| 7612 | |
| 7613 | /** |
| 7614 | * @brief Resolve every unres data item in the structure. Logs directly. |
| 7615 | * |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 7616 | * If options includes LYD_OPT_TRUSTED, the data are considered trusted (when, must conditions are not expected, |
| 7617 | * unresolved leafrefs/instids are accepted). |
| 7618 | * |
| 7619 | * If options includes LYD_OPT_NOAUTODEL, the false resulting when condition on non-default nodes, the error is raised. |
| 7620 | * |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7621 | * @param[in] unres Unres data structure to use. |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 7622 | * @param[in,out] root Root node of the data tree, can be changed due to autodeletion. |
| 7623 | * @param[in] options Data options as described above. |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7624 | * |
| 7625 | * @return EXIT_SUCCESS on success, -1 on error. |
| 7626 | */ |
| 7627 | int |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 7628 | 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] | 7629 | { |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7630 | uint32_t i, j, first = 1, resolved = 0, del_items = 0, when_stmt = 0; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7631 | int rc, progress, ignore_fail; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7632 | struct lyd_node *parent; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7633 | |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 7634 | assert(root); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7635 | assert(unres); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7636 | |
| 7637 | if (!unres->count) { |
| 7638 | return EXIT_SUCCESS; |
| 7639 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7640 | |
Michal Vasko | ad2e44a | 2017-01-03 10:31:35 +0100 | [diff] [blame] | 7641 | if (options & (LYD_OPT_TRUSTED | LYD_OPT_NOTIF_FILTER | LYD_OPT_GET | LYD_OPT_GETCONFIG | LYD_OPT_EDIT)) { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7642 | ignore_fail = 1; |
| 7643 | } else if (options & LYD_OPT_NOEXTDEPS) { |
| 7644 | ignore_fail = 2; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7645 | } else { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7646 | ignore_fail = 0; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7647 | } |
| 7648 | |
Michal Vasko | a0ffcab | 2016-05-02 14:52:08 +0200 | [diff] [blame] | 7649 | LOGVRB("Resolving unresolved data nodes and their constraints..."); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7650 | ly_vlog_hide(1); |
| 7651 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7652 | /* when-stmt first */ |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7653 | do { |
Radek Krejci | 00a0e71 | 2016-10-26 10:24:46 +0200 | [diff] [blame] | 7654 | ly_err_clean(1); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7655 | progress = 0; |
Michal Vasko | 6df9413 | 2016-09-22 11:08:09 +0200 | [diff] [blame] | 7656 | for (i = 0; i < unres->count; i++) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7657 | if (unres->type[i] != UNRES_WHEN) { |
| 7658 | continue; |
| 7659 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7660 | if (first) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7661 | /* count when-stmt nodes in unres list */ |
| 7662 | when_stmt++; |
| 7663 | } |
| 7664 | |
| 7665 | /* resolve when condition only when all parent when conditions are already resolved */ |
| 7666 | for (parent = unres->node[i]->parent; |
| 7667 | parent && LYD_WHEN_DONE(parent->when_status); |
| 7668 | parent = parent->parent) { |
| 7669 | if (!parent->parent && (parent->when_status & LYD_WHEN_FALSE)) { |
| 7670 | /* the parent node was already unlinked, do not resolve this node, |
| 7671 | * it will be removed anyway, so just mark it as resolved |
| 7672 | */ |
| 7673 | unres->node[i]->when_status |= LYD_WHEN_FALSE; |
| 7674 | unres->type[i] = UNRES_RESOLVED; |
| 7675 | resolved++; |
| 7676 | break; |
| 7677 | } |
| 7678 | } |
| 7679 | if (parent) { |
| 7680 | continue; |
| 7681 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7682 | |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7683 | rc = resolve_unres_data_item(unres->node[i], unres->type[i], ignore_fail); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7684 | if (!rc) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7685 | if (unres->node[i]->when_status & LYD_WHEN_FALSE) { |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 7686 | if ((options & LYD_OPT_NOAUTODEL) && !unres->node[i]->dflt) { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7687 | /* false when condition */ |
| 7688 | ly_vlog_hide(0); |
Radek Krejci | 2467a49 | 2016-10-24 15:16:59 +0200 | [diff] [blame] | 7689 | ly_err_repeat(); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7690 | return -1; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7691 | } /* follows else */ |
| 7692 | |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7693 | /* only unlink now, the subtree can contain another nodes stored in the unres list */ |
| 7694 | /* if it has parent non-presence containers that would be empty, we should actually |
| 7695 | * remove the container |
| 7696 | */ |
Radek Krejci | 2537fd3 | 2016-09-07 16:22:41 +0200 | [diff] [blame] | 7697 | for (parent = unres->node[i]; |
| 7698 | parent->parent && parent->parent->schema->nodetype == LYS_CONTAINER; |
| 7699 | parent = parent->parent) { |
| 7700 | if (((struct lys_node_container *)parent->parent->schema)->presence) { |
| 7701 | /* presence container */ |
| 7702 | break; |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7703 | } |
Radek Krejci | 2537fd3 | 2016-09-07 16:22:41 +0200 | [diff] [blame] | 7704 | if (parent->next || parent->prev != parent) { |
| 7705 | /* non empty (the child we are in and we are going to remove is not the only child) */ |
| 7706 | break; |
| 7707 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7708 | } |
Radek Krejci | 2537fd3 | 2016-09-07 16:22:41 +0200 | [diff] [blame] | 7709 | unres->node[i] = parent; |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7710 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7711 | /* auto-delete */ |
| 7712 | LOGVRB("auto-delete node \"%s\" due to when condition (%s)", ly_errpath(), |
| 7713 | ((struct lys_node_leaf *)unres->node[i]->schema)->when->cond); |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7714 | if (*root && *root == unres->node[i]) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7715 | *root = (*root)->next; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7716 | } |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7717 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7718 | lyd_unlink(unres->node[i]); |
| 7719 | unres->type[i] = UNRES_DELETE; |
| 7720 | del_items++; |
Radek Krejci | 51fd816 | 2016-03-24 15:49:51 +0100 | [diff] [blame] | 7721 | |
| 7722 | /* update the rest of unres items */ |
| 7723 | for (j = 0; j < unres->count; j++) { |
Radek Krejci | 3db819b | 2016-03-24 16:29:48 +0100 | [diff] [blame] | 7724 | if (unres->type[j] == UNRES_RESOLVED || unres->type[j] == UNRES_DELETE) { |
Radek Krejci | 51fd816 | 2016-03-24 15:49:51 +0100 | [diff] [blame] | 7725 | continue; |
| 7726 | } |
| 7727 | |
| 7728 | /* test if the node is in subtree to be deleted */ |
| 7729 | for (parent = unres->node[j]; parent; parent = parent->parent) { |
| 7730 | if (parent == unres->node[i]) { |
| 7731 | /* yes, it is */ |
| 7732 | unres->type[j] = UNRES_RESOLVED; |
| 7733 | resolved++; |
| 7734 | break; |
| 7735 | } |
| 7736 | } |
| 7737 | } |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7738 | } else { |
| 7739 | unres->type[i] = UNRES_RESOLVED; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7740 | } |
Radek Krejci | 00a0e71 | 2016-10-26 10:24:46 +0200 | [diff] [blame] | 7741 | ly_err_clean(1); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7742 | resolved++; |
| 7743 | progress = 1; |
| 7744 | } else if (rc == -1) { |
| 7745 | ly_vlog_hide(0); |
Michal Vasko | 76e7340 | 2016-08-24 16:00:13 +0200 | [diff] [blame] | 7746 | /* print only this last error */ |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7747 | resolve_unres_data_item(unres->node[i], unres->type[i], ignore_fail); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7748 | return -1; |
Radek Krejci | 2467a49 | 2016-10-24 15:16:59 +0200 | [diff] [blame] | 7749 | } /* else forward reference */ |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7750 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7751 | first = 0; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7752 | } while (progress && resolved < when_stmt); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7753 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7754 | /* do we have some unresolved when-stmt? */ |
Radek Krejci | d940d73 | 2016-03-24 16:02:28 +0100 | [diff] [blame] | 7755 | if (when_stmt > resolved) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7756 | ly_vlog_hide(0); |
Radek Krejci | 2467a49 | 2016-10-24 15:16:59 +0200 | [diff] [blame] | 7757 | ly_err_repeat(); |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7758 | return -1; |
| 7759 | } |
| 7760 | |
| 7761 | for (i = 0; del_items && i < unres->count; i++) { |
| 7762 | /* we had some when-stmt resulted to false, so now we have to sanitize the unres list */ |
| 7763 | if (unres->type[i] != UNRES_DELETE) { |
| 7764 | continue; |
| 7765 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7766 | if (!unres->node[i]) { |
| 7767 | unres->type[i] = UNRES_RESOLVED; |
| 7768 | del_items--; |
| 7769 | continue; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7770 | } |
| 7771 | |
| 7772 | /* really remove the complete subtree */ |
| 7773 | lyd_free(unres->node[i]); |
| 7774 | unres->type[i] = UNRES_RESOLVED; |
| 7775 | del_items--; |
| 7776 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7777 | ly_vlog_hide(0); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7778 | |
| 7779 | /* rest */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7780 | for (i = 0; i < unres->count; ++i) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7781 | if (unres->type[i] == UNRES_RESOLVED) { |
| 7782 | continue; |
| 7783 | } |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 7784 | 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] | 7785 | |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7786 | rc = resolve_unres_data_item(unres->node[i], unres->type[i], ignore_fail); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7787 | if (rc) { |
| 7788 | /* since when was already resolved, a forward reference is an error */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7789 | return -1; |
| 7790 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7791 | |
| 7792 | unres->type[i] = UNRES_RESOLVED; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7793 | } |
| 7794 | |
Michal Vasko | a0ffcab | 2016-05-02 14:52:08 +0200 | [diff] [blame] | 7795 | LOGVRB("All data nodes and constraints resolved."); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7796 | unres->count = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7797 | return EXIT_SUCCESS; |
| 7798 | } |